Add unit test infrastructure for LISP protocol

Change-Id: I802700ad832de1dc6f4a1981e8985aa6e926c8ad
Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
This commit is contained in:
Filip Tehlar
2016-04-07 10:04:34 +02:00
committed by Gerrit Code Review
parent 42f2006975
commit 254b036dff
13 changed files with 649 additions and 36 deletions

1
.gitignore vendored
View File

@ -39,6 +39,7 @@ missing
stamp-h1
ltmain.sh
ylwrap
test-driver
.cproject
*.iml

View File

@ -18,6 +18,10 @@ vnet_LDFLAGS = $(call installed_libs_fn, \
vlib \
vlib-api)
ifeq ($($(PLATFORM)_enable_tests),yes)
vnet_configure_args += --enable-tests
endif
# Platform dependent configure flags
vnet_configure_args += $(vnet_configure_args_$(PLATFORM))

View File

@ -19,6 +19,7 @@ libvnet_la_SOURCES =
libvnetplugin_la_SOURCES =
nobase_include_HEADERS =
noinst_PROGRAMS =
TESTS =
########################################
# Generic stuff
@ -443,6 +444,34 @@ nobase_include_HEADERS += \
vnet/lisp-cp/lisp_msg_serdes.h \
vnet/lisp-cp/control.h
if ENABLE_TESTS
LDS = -lvppinfra -l:libvlib.a -l:libdpdk.a -l:libvlibmemory.a \
-l:libvlibapi.a -l:libsvm.a -lpthread -ldl -lrt -lm -l:libvlib_unix.a
TESTS += test_cp_serdes test_lisp_types
test_cp_serdes_SOURCES = \
test/lisp-cp/test_cp_serdes.c \
vnet/lisp-cp/lisp_msg_serdes.c \
vnet/lisp-cp/lisp_types.c \
vnet/lisp-cp/packets.c \
vnet/ip/ip_checksum.c
test_lisp_types_SOURCES = \
test/lisp-cp/test_lisp_types.c \
vnet/lisp-cp/lisp_types.c
test_cp_serdes_CPPFLAGS = $(AM_CPPFLAGS) -DCLIB_DEBUG
test_lisp_types_CPPFLAGS = $(AM_CPPFLAGS) -DCLIB_DEBUG
test_cp_serdes_LDADD = libvnet.la $(LDS)
test_lisp_types_LDADD = libvnet.la $(LDS)
test_cp_serdes_LDFLAGS = -static
test_lisp_types_LDFLAGS = -static
endif
########################################
# Tunnel protocol: lisp-gpe
########################################
@ -457,6 +486,19 @@ nobase_include_HEADERS += \
vnet/lisp-gpe/lisp_gpe_packet.h \
vnet/lisp-gpe/lisp_gpe_error.def
if ENABLE_TESTS
TESTS += test_test
test_test_SOURCES = test/lisp-gpe/test.c
test_test_CPPFLAGS = $(AM_CPPFLAGS) -DCLIB_DEBUG
test_test_LDADD = $(LIBOBJS)
noinst_PROGRAMS += $(TESTS)
check_PROGRAMS = $(TESTS)
endif
########################################
# DHCP client
########################################
@ -685,3 +727,5 @@ pcap2pg_LDFLAGS = -static
pcap2pg_LDADD = libvnet.la -l:libvppinfra.a -lpthread -lm -ldl
noinst_PROGRAMS += pcap2pg

View File

@ -32,6 +32,11 @@ AC_ARG_WITH(ipv6sr,
[with_ipv6sr=0],
[with_ipv6sr=1])
AC_ARG_ENABLE(tests,
AC_HELP_STRING([--enable-tests], [Build unit tests]),
[enable_tests=1],
[enable_tests=0])
AM_CONDITIONAL(WITH_DPDK, test "$with_dpdk" = "1")
AC_SUBST(DPDK,[-DDPDK=${with_dpdk}])
@ -47,4 +52,6 @@ AC_SUBST(VCGN,[-DVCGN=${with_vcgn}])
AM_CONDITIONAL(WITH_IPV6SR, test "$with_ipv6sr" = "1")
AC_SUBST(IPV6SR,[-DIPV6SR=${with_ipv6sr}])
AM_CONDITIONAL(ENABLE_TESTS, test "$enable_tests" = "1")
AC_OUTPUT([Makefile])

10
vnet/test/README Normal file
View File

@ -0,0 +1,10 @@
Unit test infrastructure for vnet
To run unit tests do the following:
1. build vpp with 'vpp_enable_tests = yes' in build-data/platforms/vpp.mk
2. go to build-root/build-$tag-$arch/vnet
3. run
$ make check

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,145 @@
/*
* Copyright (c) 2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <vnet/vnet.h>
#include <vppinfra/error.h>
#include <vnet/lisp-cp/lisp_types.h>
#include <vnet/lisp-cp/lisp_cp_messages.h>
#define _assert(e) \
error = CLIB_ERROR_ASSERT (e); \
if (error) \
goto done;
static clib_error_t * test_locator_type (void)
{
clib_error_t * error = 0;
gid_address_t _gid_addr, * gid_addr = &_gid_addr;
gid_address_type(gid_addr) = IP_PREFIX;
gid_address_ippref_len(gid_addr) = 24;
ip_prefix_version(&gid_addr->ippref) = IP4;
gid_addr->ippref.addr.ip.v4.as_u32 = 0x20304050;
/* local locator */
locator_t loc1, loc2 = {
.local = 1,
.state = 2,
.sw_if_index = 8,
.priority = 3,
.weight = 100,
.mpriority = 4,
.mweight = 101
};
locator_copy (&loc1, &loc2);
_assert (0 == locator_cmp (&loc1, &loc2));
/* remote locator */
loc2.local = 0;
loc2.address = gid_addr[0];
locator_copy(&loc1, &loc2);
_assert (0 == locator_cmp (&loc1, &loc2));
done:
return error;
}
static clib_error_t * test_gid_parse ()
{
clib_error_t * error = 0;
return error;
}
static clib_error_t * test_format_unformat_gid_address (void)
{
u8 * s = 0;
clib_error_t * error = 0;
unformat_input_t _input;
unformat_input_t * input = &_input;
gid_address_t _gid_addr, * gid_addr = &_gid_addr;
gid_address_t unformated_gid;
/* format/unformat IPv4 global ID address */
gid_address_type(gid_addr) = IP_PREFIX;
gid_address_ippref_len(gid_addr) = 24;
ip_prefix_version(&gid_addr->ippref) = IP4;
gid_addr->ippref.addr.ip.v4.as_u32 = 0x20304050;
s = format(0, "%U", format_gid_address, gid_addr);
vec_add1(s, 0);
unformat_init_string(input, (char *)s, vec_len(s));
_assert (unformat(input, "%U",
unformat_gid_address, &unformated_gid));
_assert (0 == gid_address_cmp (&unformated_gid, gid_addr));
unformat_free(input);
vec_free(s);
s = 0;
/* format/unformat IPv6 global ID address */
gid_address_type(gid_addr) = IP_PREFIX;
gid_address_ippref_len(gid_addr) = 64;
ip_prefix_version(&gid_addr->ippref) = IP6;
u8 ipv6[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
memcpy(gid_addr->ippref.addr.ip.v6.as_u8, ipv6, sizeof(ipv6));
s = format(0, "%U", format_gid_address, gid_addr);
vec_add1(s, 0);
unformat_init_string(input, (char *)s, vec_len(s));
_assert (unformat (input, "%U", unformat_gid_address,
&unformated_gid));
_assert (0 == gid_address_cmp(&unformated_gid, gid_addr));
/* test address copy */
gid_address_t gid_addr_copy;
gid_address_copy(&gid_addr_copy, gid_addr);
_assert (0 == gid_address_cmp (&gid_addr_copy, gid_addr));
done:
unformat_free(input);
vec_free(s);
return error;
}
#define foreach_test_case \
_(format_unformat_gid_address) \
_(locator_type) \
_(gid_parse)
int run_tests (void)
{
clib_error_t * error;
#define _(_test_name) \
error = test_ ## _test_name (); \
if (error) \
{ \
clib_error_report (error); \
return 0; \
}
foreach_test_case
#undef _
return 0;
}
int main()
{
return run_tests ();
}

18
vnet/test/lisp-gpe/test.c Normal file
View File

@ -0,0 +1,18 @@
/*
* Copyright (c) 2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
int main(int argc, char **argv) {
return 0;
}

View File

@ -692,6 +692,47 @@ get_local_iface_ip_for_dst (lisp_cp_main_t *lcm, ip_address_t * dst,
}
}
static ip_address_t *
build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
{
ip4_address_t * l4;
ip6_address_t * l6;
u32 i;
locator_t * loc;
u32 * loc_indexp;
ip_interface_address_t * ia = 0;
ip_address_t * rlocs = 0;
ip_address_t _rloc, * rloc = &_rloc;
for (i = 0; i < vec_len(loc_set->locator_indices); i++)
{
loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
ip_addr_version(rloc) = IP4;
/* Add ipv4 locators first TODO sort them */
foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
loc->sw_if_index, 1 /* unnumbered */,
({
l4 = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
ip_addr_v4(rloc) = l4[0];
vec_add1(rlocs, rloc[0]);
}));
ip_addr_version(rloc) = IP6;
/* Add ipv6 locators */
foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
loc->sw_if_index, 1 /* unnumbered */,
({
l6 = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
ip_addr_v6(rloc) = l6[0];
vec_add1(rlocs, rloc[0]);
}));
}
return rlocs;
}
static vlib_buffer_t *
build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
gid_address_t * seid, gid_address_t * deid,
@ -701,6 +742,7 @@ build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
vlib_buffer_t * b;
u32 bi;
ip_address_t * mr_ip, sloc;
ip_address_t * rlocs = 0;
if (vlib_buffer_alloc (vm, &bi, 1) != 1)
{
@ -713,8 +755,11 @@ build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
/* leave some space for the encap headers */
vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
/* get rlocs */
rlocs = build_itr_rloc_list (lcm, loc_set);
/* put lisp msg */
lisp_msg_put_mreq (lcm, b, seid, deid, loc_set, is_smr_invoked, nonce_res);
lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
/* push ecm: udp-ip-lisp */
lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
@ -730,6 +775,9 @@ build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
mr_ip);
bi_res[0] = bi;
if (rlocs)
vec_free(rlocs);
return b;
}

View File

@ -27,42 +27,31 @@ lisp_msg_put_gid (vlib_buffer_t * b, gid_address_t * gid)
void *
lisp_msg_put_itr_rlocs (lisp_cp_main_t * lcm, vlib_buffer_t * b,
locator_set_t * loc_set, u8 * locs_put)
ip_address_t * rlocs, u8 * locs_put)
{
ip_interface_address_t * ia = 0;
ip4_address_t * l4;
ip6_address_t * l6;
u32 * loc_indexp;
locator_t * loc;
u32 i;
u8 * p, * bp, count = 0;
u32 i;
ip_address_t * addr;
bp = vlib_buffer_get_current(b);
for (i = 0; i < vec_len(loc_set->locator_indices); i++)
for (i = 0; i < vec_len(rlocs); i++)
{
addr = &rlocs[i];
switch (ip_addr_version(addr))
{
loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
/* Add ipv4 locators first TODO sort them */
foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
loc->sw_if_index, 1 /* unnumbered */,
({
l4 = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
p = vlib_buffer_put_uninit (b, ip4_address_size_to_put());
ip4_address_put (p, l4);
count++;
}));
/* Add ipv6 locators */
foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
loc->sw_if_index, 1 /* unnumbered */,
({
l6 = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
p = vlib_buffer_put_uninit (b, ip6_address_size_to_put());
ip6_address_put (p, l6);
count++;
}));
case IP4:
p = vlib_buffer_put_uninit (b, ip4_address_size_to_put());
ip4_address_put (p, &ip_addr_v4(addr));
count++;
break;
case IP6:
p = vlib_buffer_put_uninit (b, ip6_address_size_to_put());
ip6_address_put (p, &ip_addr_v6(addr));
count++;
break;
}
}
*locs_put = count-1;
return bp;
}
@ -103,8 +92,8 @@ nonce_build (u32 seed)
void *
lisp_msg_put_mreq (lisp_cp_main_t * lcm, vlib_buffer_t * b,
gid_address_t * seid, gid_address_t * deid,
locator_set_t * loc_set, u8 is_smr_invoked, u64 * nonce)
gid_address_t * seid, gid_address_t * deid,
ip_address_t * rlocs, u8 is_smr_invoked, u64 * nonce)
{
u8 loc_count = 0;
@ -123,7 +112,7 @@ lisp_msg_put_mreq (lisp_cp_main_t * lcm, vlib_buffer_t * b,
lisp_msg_put_gid (b, seid);
/* Put itr rlocs */
lisp_msg_put_itr_rlocs(lcm, b, loc_set, &loc_count);
lisp_msg_put_itr_rlocs(lcm, b, rlocs, &loc_count);
MREQ_ITR_RLOC_COUNT(h) = loc_count;
/* Put eid record */

View File

@ -23,7 +23,7 @@
void *
lisp_msg_put_mreq (lisp_cp_main_t * lcm, vlib_buffer_t * b,
gid_address_t * seid, gid_address_t * deid,
locator_set_t * loc_set, u8 is_smr_invoked, u64 * nonce);
ip_address_t * rlocs, u8 is_smr_invoked, u64 * nonce);
void *
lisp_msg_push_ecm (vlib_main_t * vm, vlib_buffer_t *b, int lp, int rp,

View File

@ -313,7 +313,17 @@ ip_prefix_cmp(ip_prefix_t * p1, ip_prefix_t * p2)
int cmp = 0;
cmp = ip_address_cmp (&ip_prefix_addr(p1), &ip_prefix_addr(p2));
if (cmp == 0)
cmp = ip_prefix_len(p1) < ip_prefix_len(p2) ? 1 : 2; /* XXX ? */
{
if (ip_prefix_len(p1) < ip_prefix_len(p2))
{
cmp = 1;
}
else
{
if (ip_prefix_len(p1) > ip_prefix_len(p2))
cmp = 2;
}
}
return cmp;
}

View File

@ -96,6 +96,7 @@ typedef enum {
u8 *format_gid_address (u8 * s, va_list * args);
uword unformat_gid_address (unformat_input_t * input, va_list * args);
int gid_address_cmp (gid_address_t * a1, gid_address_t * a2);
u16 gid_address_size_to_put (gid_address_t * a);
u16 gid_address_put (u8 * b, gid_address_t * gid);