2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/vnet.h>
17 #include <vppinfra/error.h>
18 #include <vnet/lisp-cp/lisp_types.h>
19 #include <vnet/lisp-cp/lisp_cp_messages.h>
22 #include <vlibapi/api_helper_macros.h>
23 vpe_api_main_t vpe_api_main;
26 error = CLIB_ERROR_ASSERT (e); \
30 static clib_error_t * test_locator_type (void)
32 clib_error_t * error = 0;
33 gid_address_t _gid_addr, * gid = &_gid_addr;
35 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
36 gid_address_ippref_len (gid) = 24;
37 ippref = &gid_address_ippref (gid);
38 ip_prefix_version (ippref) = IP4;
39 ip_prefix_len (ippref) = 0;
40 ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
41 ip4->as_u32 = 0x20304050;
44 locator_t loc1, loc2 = {
53 locator_copy (&loc1, &loc2);
54 _assert (0 == locator_cmp (&loc1, &loc2));
59 ip_prefix_t nested_ippref;
60 ip_prefix_version (&nested_ippref) = IP4;
61 ip_prefix_len (&nested_ippref) = 0;
62 ip4 = &ip_prefix_v4 (&nested_ippref);
63 ip4->as_u32 = 0x33882299;
64 gid_address_t nested_gid =
66 .type = GID_ADDR_IP_PREFIX,
67 .ippref = nested_ippref
72 .type = LCAF_INSTANCE_ID,
77 .gid_addr = &nested_gid
80 gid_address_type (gid) = GID_ADDR_LCAF;
81 gid_address_lcaf (gid) = lcaf;
83 loc2.address = gid[0];
84 locator_copy(&loc1, &loc2);
86 _assert (0 == locator_cmp (&loc1, &loc2));
93 static clib_error_t * test_gid_parse_ip_pref ()
95 clib_error_t * error = 0;
96 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
97 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
100 0x00, 0x01, /* AFI = IPv4 */
101 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
104 u32 len = gid_address_parse (data, gid_addr);
106 gid_address_copy (gid_addr_copy, gid_addr);
107 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
112 static clib_error_t * test_gid_parse_mac ()
114 clib_error_t * error = 0;
115 gid_address_t _gid, * gid = &_gid;
116 gid_address_t _gid_copy, * gid_copy = &_gid_copy;
120 0x40, 0x05, /* AFI = MAC address */
121 0x10, 0xbb, 0xcc, 0xdd, /* MAC */
125 u32 len = gid_address_parse (data, gid);
127 _assert (GID_ADDR_MAC == gid_address_type (gid));
128 gid_address_copy (gid_copy, gid);
129 _assert (0 == gid_address_cmp (gid_copy, gid));
134 static clib_error_t * test_gid_parse_lcaf ()
136 clib_error_t * error = 0;
137 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
138 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
140 memset (gid_addr, 0, sizeof (gid_addr[0]));
141 memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
145 0x40, 0x03, /* AFI = LCAF*/
148 0x00, 0x00, /* reserved1, flags */
149 0x02, /* type = Instance ID */
150 0x18, /* IID mask-len */
151 0x00, 0x0a, /* iid length + next AFI lenght */
152 /* LCAF Instance ID */
153 0x00, 0x00, 0x00, 0x09, /* iid */
154 0x00, 0x01, /* AFI = ipv4 */
155 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
157 u32 len = gid_address_parse (data, gid_addr);
159 gid_address_copy (gid_addr_copy, gid_addr);
160 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
161 _assert (GID_ADDR_IP_PREFIX == gid_address_type (gid_addr));
162 _assert (9 == gid_address_vni (gid_addr));
163 _assert (0x18 == gid_address_vni_mask (gid_addr));
164 _assert (0xddccbb10 == gid_addr->ippref.addr.ip.v4.as_u32);
167 gid_address_free (gid_addr);
168 gid_address_free (gid_addr_copy);
172 /* recursive LCAFs are not supported */
174 static clib_error_t * test_gid_parse_lcaf_complex ()
176 clib_error_t * error = 0;
177 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
178 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
180 memset (gid_addr, 0, sizeof (gid_addr[0]));
181 memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
185 0x40, 0x03, /* AFI = LCAF*/
188 0x00, 0x00, /* reserved1, flags */
189 0x02, /* type = Instance ID */
190 0x18, /* IID mask-len */
191 0x00, 0x0a, /* iid length + next AFI lenght */
192 /* LCAF Instance ID */
193 0x00, 0x00, 0x00, 0x0b, /* iid */
195 0x40, 0x03, /* AFI = LCAF*/
197 0x00, 0x00, /* reserved1, flags */
198 0x02, /* type = Instance ID */
199 0x17, /* IID mask-len */
200 0x00, 0x0a, /* iid length + next AFI lenght */
201 /* LCAF Instance ID */
202 0x00, 0x00, 0x00, 0x0c, /* iid */
204 0x40, 0x03, /* AFI = LCAF*/
206 0x00, 0x00, /* reserved1, flags */
207 0x02, /* type = Instance ID */
208 0x16, /* IID mask-len */
209 0x00, 0x16, /* iid length + next AFI lenght */
210 /* LCAF Instance ID */
211 0x00, 0x00, 0x00, 0x0d, /* iid */
213 0x00, 0x02, /* AFI = IPv6 */
215 0x10, 0xbb, 0xcc, 0xdd,
216 0x10, 0xbb, 0xcc, 0xdd,
217 0x10, 0xbb, 0xcc, 0xdd,
218 0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
220 u32 len = gid_address_parse (data, gid_addr);
222 _assert (gid_addr->type == GID_ADDR_LCAF);
223 gid_address_copy (gid_addr_copy, gid_addr);
224 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
225 _assert (gid_addr_copy->type == GID_ADDR_LCAF);
227 lcaf_t * lcaf = &gid_address_lcaf (gid_addr_copy);
228 _assert (lcaf->type == LCAF_INSTANCE_ID);
229 vni_t * v = (vni_t *) lcaf;
230 _assert (v->vni == 0x0b);
231 _assert (v->vni_mask_len == 0x18);
233 gid_address_t * tmp = vni_gid (v);
234 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
235 lcaf = &gid_address_lcaf (tmp);
236 _assert (lcaf->type == LCAF_INSTANCE_ID);
239 _assert (v->vni == 0x0c);
240 _assert (v->vni_mask_len == 0x17);
243 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
244 lcaf = &gid_address_lcaf (tmp);
246 _assert (lcaf->type == LCAF_INSTANCE_ID);
248 _assert (v->vni == 0x0d);
249 _assert (v->vni_mask_len == 0x16);
252 _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
254 ip_prefix_t * ip_pref = &gid_address_ippref (tmp);
255 ip6_address_t * ip6 = &ip_prefix_v6 (ip_pref);
256 _assert (ip6->as_u32[0] == 0xddccbb10);
257 _assert (ip6->as_u32[1] == 0xddccbb10);
258 _assert (ip6->as_u32[2] == 0xddccbb10);
259 _assert (ip6->as_u32[3] == 0xddccbb10);
260 _assert (ip_prefix_version (ip_pref) == IP6);
263 gid_address_free (gid_addr);
264 gid_address_free (gid_addr_copy);
269 #if 0 /* uncomment this once VNI is supported */
270 static clib_error_t * test_write_mac_in_lcaf (void)
272 clib_error_t * error = 0;
274 u8 * b = clib_mem_alloc(500);
279 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
282 .type = GID_ADDR_MAC,
285 u16 len = gid_address_put (b, &g);
290 0x40, 0x03, /* AFI = LCAF */
291 0x00, /* reserved1 */
293 0x02, /* LCAF type = Instance ID */
294 0x20, /* IID/VNI mask len */
295 0x00, 0x0a, /* length */
296 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
298 0x00, 0x06, /* AFI = MAC */
299 0x01, 0x02, 0x03, 0x04,
302 _assert (0 == memcmp (expected, b, len));
309 static clib_error_t * test_mac_address_write (void)
311 clib_error_t * error = 0;
313 u8 * b = clib_mem_alloc(500);
318 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
319 .type = GID_ADDR_MAC,
322 u16 len = gid_address_put (b, &g);
327 0x40, 0x05, /* AFI = MAC */
328 0x01, 0x02, 0x03, 0x04,
331 _assert (0 == memcmp (expected, b, len));
337 static clib_error_t *
338 test_src_dst_with_vni_serdes (void)
340 clib_error_t * error = 0;
341 u8 * b = clib_mem_alloc (500);
346 .type = FID_ADDR_IP_PREF,
353 .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
360 .type = FID_ADDR_IP_PREF,
367 .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
381 .type = GID_ADDR_SRC_DST,
386 u16 size_to_put = gid_address_size_to_put(&g);
387 _assert (36 == size_to_put);
388 _assert (0 == gid_address_len(&g));
390 u16 write_len = gid_address_put (b, &g);
391 printf("sizetoput %d; writelen %d\n", size_to_put, write_len);
392 _assert (size_to_put == write_len);
396 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
397 0x02, 0x09, 0x00, 0x1c, /* LCAF type = IID, IID mask-len, length */
398 0x12, 0x34, 0x56, 0x78, /* reserved; source-ML, Dest-ML */
400 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
401 0x0c, 0x00, 0x00, 0x10, /* LCAF type = source/dest key, rsvd, length */
402 0x00, 0x00, 0x18, 0x10, /* reserved; source-ML, Dest-ML */
404 0x00, 0x01, /* AFI = ip4 */
405 0x01, 0x02, 0x03, 0x00, /* source */
407 0x00, 0x01, /* AFI = ip4 */
408 0x09, 0x08, 0x00, 0x00, /* destination */
410 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
413 memset (&p, 0, sizeof (p));
414 _assert (write_len == gid_address_parse (b, &p));
415 _assert (0 == gid_address_cmp (&g, &p));
421 static clib_error_t *
422 test_src_dst_serdes (void)
424 clib_error_t * error = 0;
426 u8 * b = clib_mem_alloc (500);
431 .type = FID_ADDR_MAC,
432 .mac = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
437 .type = FID_ADDR_MAC,
438 .mac = { 0x10, 0x21, 0x32, 0x43, 0x54, 0x65 }
450 .type = GID_ADDR_SRC_DST,
455 u16 size_to_put = gid_address_size_to_put(&g);
456 _assert (28 == size_to_put);
457 _assert (0 == gid_address_len(&g));
459 u16 write_len = gid_address_put (b, &g);
460 _assert (size_to_put == write_len);
464 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
465 0x0c, 0x00, 0x00, 0x14, /* LCAF type = source/dest key, rsvd, length */
466 0x00, 0x00, 0x00, 0x00, /* reserved; source-ML, Dest-ML */
468 0x40, 0x05, /* AFI = MAC */
469 0x11, 0x22, 0x33, 0x44,
470 0x55, 0x66, /* source */
472 0x40, 0x05, /* AFI = MAC */
473 0x10, 0x21, 0x32, 0x43,
474 0x54, 0x65, /* destination */
476 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
479 memset (&p, 0, sizeof (p));
480 _assert (write_len == gid_address_parse (b, &p));
481 _assert (0 == gid_address_cmp (&g, &p));
487 static clib_error_t * test_gid_address_write (void)
489 clib_error_t * error = 0;
490 ip_prefix_t ippref_data, * ippref = &ippref_data;
492 u8 * b = clib_mem_alloc(500);
495 ip_prefix_version (ippref) = IP4;
496 ip_prefix_len (ippref) = 9;
497 ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
498 ip4->as_u32 = 0xaabbccdd;
503 .type = GID_ADDR_IP_PREFIX,
508 _assert (18 == gid_address_size_to_put (&g));
509 _assert (gid_address_len (&g) == 9);
511 u16 write_len = gid_address_put (b, &g);
512 _assert (18 == write_len);
514 u8 expected_gid_data[] =
516 0x40, 0x03, /* AFI = LCAF */
517 0x00, /* reserved1 */
519 0x02, /* LCAF type = Instance ID */
520 0x18, /* IID/VNI mask len */
521 0x00, 0x0a, /* length */
522 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
524 0x00, 0x01, /* AFI = IPv4 */
525 0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
527 _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
533 #define foreach_test_case \
535 _(gid_parse_ip_pref) \
538 _(mac_address_write) \
539 _(gid_address_write) \
541 _(src_dst_with_vni_serdes)
545 clib_error_t * error;
547 #define _(_test_name) \
548 error = test_ ## _test_name (); \
551 clib_error_report (error); \