2 * Copyright (c) 2019 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_cp_messages.h>
19 #include <vnet/lisp-cp/control.h>
20 #include <vnet/lisp-cp/lisp_msg_serdes.h>
21 #include <vlibapi/api.h>
22 #include <vnet/lisp-cp/packets.h>
25 error = CLIB_ERROR_ASSERT (e); \
28 fformat(stderr, "FAIL: line %d \n\n", __LINE__); \
33 print_chunk (u8 * b, int *offset, int c, char *des)
35 int i, n = offset[0] + c;;
36 for (i = offset[0]; i < n; i++)
38 printf ("0x%02x, ", b[i]);
40 printf (" // %s\n", des);
45 print_map_request (map_request_hdr_t * h)
47 #define pchunk(_count, _desc) \
48 print_chunk((u8 *)h, &offset, _count, _desc)
54 pchunk (2, "Source-EID-AFI");
55 pchunk (4, "Source EID Address");
56 pchunk (2, "ITR-RLOC-AFI 1");
57 pchunk (4, "ITR-RLOC Address 1");
58 pchunk (2, "ITR-RLOC-AFI 2");
59 pchunk (16, "ITR-RLOC Address 2");
60 pchunk (1, "REC: reserved");
61 pchunk (1, "REC: EID mask-len");
62 pchunk (2, "REC: EID-prefix-AFI");
63 pchunk (4, "REC: EID-prefix");
68 test_lisp_msg_push_ecm ()
70 vlib_main_t *vm = vlib_get_main ();
71 clib_error_t *error = 0;
75 int lp = 0x15, rp = 0x14;
77 b = clib_mem_alloc (buff_len);
78 clib_memset ((u8 *) b, 0, buff_len);
79 b->current_length = buff_len;
80 b->current_data = sizeof (udp_header_t) + sizeof (ip4_header_t) +
81 sizeof (ecm_hdr_t) + 1;
83 la.type = GID_ADDR_IP_PREFIX;
84 la.ippref.addr.ip.v4.as_u32 = 0xa1b2c3d4;
85 la.ippref.addr.version = IP4;
87 ra.type = GID_ADDR_IP_PREFIX;
88 ra.ippref.addr.ip.v4.as_u32 = 0x90817263;
89 ra.ippref.addr.version = IP4;
91 ecm_hdr_t *lh = lisp_msg_push_ecm (vm, b, lp, rp, &la, &ra);
93 u8 expected_ecm_hdr[] = {
94 0x80, 0x00, 0x00, 0x00
96 _assert (0 == memcmp (expected_ecm_hdr, lh, sizeof (expected_ecm_hdr)));
98 ip4_header_t *ih = (ip4_header_t *) (lh + 1);
99 /* clear ip checksum */
100 clib_memset ((u8 *) ih + 10, 0, 2);
103 u8 expected_ip4_hdr[] = {
104 0x45, /* version; IHL */
106 0x03, 0xa0, /* total length */
107 0x00, 0x00, /* identification */
108 0x40, 0x00, /* flags; fragment offset*/
111 0x00, 0x00, /* header checksum */
112 0xd4, 0xc3, 0xb2, 0xa1, /* src IP */
113 0x63, 0x72, 0x81, 0x90, /* dst IP */
117 _assert (0 == memcmp (ih, expected_ip4_hdr, sizeof (expected_ip4_hdr)));
119 udp_header_t *uh = (udp_header_t *) (ih + 1);
120 /* clear udp checksum */
121 clib_memset ((u8 *) uh + 6, 0, 2);
124 u8 expected_udp_hdr[] = {
125 0x00, 0x15, /* src port */
126 0x00, 0x14, /* dst port */
127 0x03, 0x8c, /* length */
128 0x00, 0x00, /* checksum */
132 _assert (0 == memcmp (uh, expected_udp_hdr, sizeof (expected_udp_hdr)));
139 static clib_error_t *
140 test_lisp_msg_parse_mapping_record ()
142 clib_error_t *error = 0;
145 vlib_buffer_t *b = 0;
149 b = clib_mem_alloc (buff_len);
150 clib_memset ((u8 *) b, 0, buff_len);
153 u8 map_reply_records[] = {
155 0x01, 0x02, 0x03, 0x04, /* record TTL */
156 0x01, /* locator count */
157 0x00, 0x00, 0x00, /* eid-mask-len; ... */
158 0x00, 0x00, /* reserved; map-version num */
159 0x00, 0x01, /* EID-Prefix-AFI */
160 0x33, 0x44, 0x55, 0x66, /* eid-prefix */
164 0x0c, /* m-prority */
166 0x00, 0x00, /* unused flags */
167 0x00, 0x01, /* Loc-AFI */
168 0xaa, 0xbb, 0xcc, 0xdd, /* Loator */
172 b->current_length = buff_len;
173 clib_memcpy (b->data, map_reply_records, sizeof (map_reply_records));
175 lisp_msg_parse_mapping_record (b, &eid, &locs, &probed);
176 _assert (vec_len (locs) == 1);
177 _assert (eid.ippref.addr.ip.v4.as_u32 == 0x66554433);
178 _assert (locs[0].local == 0);
179 _assert (locs[0].address.ippref.addr.ip.v4.as_u32 == 0xddccbbaa);
180 _assert (locs[0].address.type == GID_ADDR_IP_PREFIX);
181 _assert (locs[0].priority == 0xa);
182 _assert (locs[0].weight == 0xb);
183 _assert (locs[0].mpriority == 0xc);
184 _assert (locs[0].mweight == 0xd);
193 static map_request_hdr_t *
194 build_map_request (lisp_cp_main_t * lcm, vlib_buffer_t * b,
195 gid_address_t * rlocs)
197 gid_address_t _seid, *seid = &_seid;
198 gid_address_t _deid, *deid = &_deid;
199 u8 is_smr_invoked = 1;
200 u8 rloc_probe_set = 0;
202 map_request_hdr_t *h = 0;
203 clib_memset (deid, 0, sizeof (deid[0]));
204 clib_memset (seid, 0, sizeof (seid[0]));
206 gid_address_type (seid) = GID_ADDR_IP_PREFIX;
207 ip_address_t *ip_addr = &gid_address_ip (seid);
208 ip_addr_v4 (ip_addr).as_u32 = 0x12345678;
209 seid->ippref.addr.version = IP4;
211 gid_address_type (deid) = GID_ADDR_IP_PREFIX;
212 ip_address_t *ip_addr2 = &gid_address_ip (deid);
213 ip_addr_v4 (ip_addr2).as_u32 = 0x9abcdef0;
214 deid->ippref.addr.version = IP4;
215 gid_address_ippref_len (deid) = 24;
217 h = lisp_msg_put_mreq (lcm, b, seid, deid, rlocs,
218 is_smr_invoked, rloc_probe_set, &nonce);
224 generate_rlocs (gid_address_t ** rlocs, u32 * count)
226 gid_address_t gid_addr_data, *gid_addr = &gid_addr_data;
227 clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
228 ip_address_t *addr = &gid_address_ip (gid_addr);
230 gid_address_type (gid_addr) = GID_ADDR_IP_PREFIX;
232 ip_addr_version (addr) = IP4;
233 ip_addr_v4 (addr).data_u32 = 0x10203040;
234 vec_add1 (rlocs[0], gid_addr[0]);
236 ip_addr_v6 (addr).as_u32[0] = 0xffeeddcc;
237 ip_addr_v6 (addr).as_u32[1] = 0xbbaa9988;
238 ip_addr_v6 (addr).as_u32[2] = 0x77665544;
239 ip_addr_v6 (addr).as_u32[3] = 0x33221100;
240 ip_addr_version (addr) = IP6;
241 vec_add1 (rlocs[0], gid_addr[0]);
244 static clib_error_t *
245 test_lisp_msg_parse ()
248 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
249 map_request_hdr_t *h;
251 clib_error_t *error = 0;
253 gid_address_t *rlocs_decode = 0, *rlocs = 0;
254 u32 rloc_count_parse = 0;
256 u8 *data = clib_mem_alloc (500);
257 clib_memset (data, 0, 500);
258 b = (vlib_buffer_t *) data;
260 generate_rlocs (&rlocs_decode, &rloc_count_parse);
261 h = build_map_request (lcm, b, rlocs_decode);
263 vlib_buffer_pull (b, sizeof (*h));
264 u32 len = lisp_msg_parse_addr (b, &gid);
265 _assert (len == 2 + 4
266 /* Source-EID-AFI field lenght + IPv4 address length */ );
267 _assert (gid.ippref.addr.ip.v4.as_u32 == 0x12345678);
268 _assert (gid.ippref.addr.version == IP4);
270 u8 rloc_count = MREQ_ITR_RLOC_COUNT (h) + 1;
271 lisp_msg_parse_itr_rlocs (b, &rlocs, rloc_count);
273 _assert (vec_len (rlocs) == 2);
274 _assert (rlocs[0].ippref.addr.ip.v4.as_u32 == 0x10203040);
275 _assert (rlocs[0].ippref.addr.version == IP4);
277 _assert (rlocs[1].ippref.addr.ip.v6.as_u32[0] == 0xffeeddcc);
278 _assert (rlocs[1].ippref.addr.ip.v6.as_u32[1] == 0xbbaa9988);
279 _assert (rlocs[1].ippref.addr.ip.v6.as_u32[2] == 0x77665544);
280 _assert (rlocs[1].ippref.addr.ip.v6.as_u32[3] == 0x33221100);
281 _assert (rlocs[1].ippref.addr.version == IP6);
283 lisp_msg_parse_eid_rec (b, &eid);
284 _assert (eid.ippref.addr.ip.v4.as_u32 == 0x9abcdef0);
285 _assert (eid.ippref.addr.version == IP4);
286 _assert (eid.ippref.len == 24);
289 clib_mem_free (data);
295 static clib_error_t *
296 test_lisp_msg_put_mreq_with_lcaf ()
298 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
299 clib_error_t *error = 0;
300 map_request_hdr_t *h = 0;
301 gid_address_t *rlocs = 0;
304 ip_prefix_version (&ippref) = IP4;
305 ip4_address_t *ip = &ip_prefix_v4 (&ippref);
306 ip->as_u32 = 0x11223344;
309 .type = GID_ADDR_IP_PREFIX,
316 u8 *data = clib_mem_alloc (500);
317 clib_memset (data, 0, 500);
319 h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs);
321 /* clear Nonce to simplify comparison */
322 clib_memset ((u8 *) h + 4, 0, 8);
327 0x10, 0x40, 0x00, 0x01, /* type; flags; IRC; REC count */
328 0x00, 0x00, 0x00, 0x00,
329 0x00, 0x00, 0x00, 0x00, /* nonce */
330 0x00, 0x01, /* Source-EID-AFI */
331 0x78, 0x56, 0x34, 0x12, /* Source EID Address */
334 0x40, 0x03, /* AFI = LCAF*/
336 0x00, 0x00, /* reserved1, flags */
337 0x02, /* type = Instance ID */
338 0x17, /* IID mask-len */
339 0x00, 0x0a, /* lenght */
340 0x90, 0x91, 0x92, 0x93, /* IID / VNI */
342 0x00, 0x01, /* AFI = ipv4 */
343 0x44, 0x33, 0x22, 0x11, /* ITR-RLOC Address 1 */
347 0x18, /* EID mask-len */
348 0x00, 0x01, /* EID-prefix-AFI */
349 0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */
353 _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data)));
355 clib_mem_free (data);
359 static clib_error_t *
360 test_lisp_msg_put_mreq ()
362 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
363 clib_error_t *error = 0;
364 map_request_hdr_t *h;
365 gid_address_t *rlocs = 0;
368 u8 *data = clib_mem_alloc (500);
369 clib_memset (data, 0, 500);
371 generate_rlocs (&rlocs, &rloc_count);
372 h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs);
374 /* clear Nonce to simplify comparison */
375 clib_memset ((u8 *) h + 4, 0, 8);
377 print_map_request (h);
380 u8 expected_data[50] = {
381 0x10, 0x40, 0x01, 0x01, /* type; flags; IRC; REC count */
382 0x00, 0x00, 0x00, 0x00,
383 0x00, 0x00, 0x00, 0x00, /* nonce */
384 0x00, 0x01, /* Source-EID-AFI */
385 0x78, 0x56, 0x34, 0x12, /* Source EID Address */
388 0x00, 0x01, /* ITR-RLOC-AFI 1 */
389 0x40, 0x30, 0x20, 0x10, /* ITR-RLOC Address 1 */
390 0x00, 0x02, /* ITR-RLOC-AFI 2 */
391 0xcc, 0xdd, 0xee, 0xff,
392 0x88, 0x99, 0xaa, 0xbb,
393 0x44, 0x55, 0x66, 0x77,
394 0x00, 0x11, 0x22, 0x33, /* ITR-RLOC Address 2 */
398 0x18, /* EID mask-len */
399 0x00, 0x01, /* EID-prefix-AFI */
400 0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */
404 _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data)));
407 clib_mem_free (data);
411 /* generate a vector of eid records */
413 build_test_map_records ()
415 mapping_t *records = 0;
419 .ttl = MAP_REGISTER_DEFAULT_TTL,
421 .type = GID_ADDR_MAC,
422 .mac = {1, 2, 3, 4, 5, 6},
432 .type = GID_ADDR_IP_PREFIX,
435 .ip.v4.as_u32 = 0x99887766,
443 vec_add1 (r.locators, loc);
444 vec_add1 (records, r);
450 free_test_map_records (mapping_t * maps)
453 vec_foreach (map, maps)
455 vec_free (map->locators);
460 static clib_error_t *
461 test_lisp_map_register ()
464 clib_error_t *error = 0;
467 mapping_t *records = build_test_map_records ();
469 u8 *data = clib_mem_alloc (500);
470 clib_memset (data, 0, 500);
471 b = (vlib_buffer_t *) data;
473 lisp_msg_put_map_register (b, records, 1 /* want map notify */ ,
474 20 /* length of HMAC_SHA_1_96 */ ,
476 free_test_map_records (records);
478 /* clear Nonce to simplify comparison */
479 clib_memset ((u8 *) b->data + 4, 0, 8);
481 /* clear authentication data */
482 clib_memset ((u8 *) b->data + 16, 0, 20);
485 u8 expected_data[] = {
486 0x30, 0x00, 0x01, 0x01, /* type; rsvd; want notify; REC count */
487 0x00, 0x00, 0x00, 0x00,
488 0x00, 0x00, 0x00, 0x00, /* nonce */
489 0x00, 0x00, 0x00, 0x00, /* key id, auth data length:
490 both are zeroes because those are set in another
491 function (see auth_data_len_by_key_id())*/
492 0x00, 0x00, 0x00, 0x00,
493 0x00, 0x00, 0x00, 0x00,
494 0x00, 0x00, 0x00, 0x00,
495 0x00, 0x00, 0x00, 0x00,
496 0x00, 0x00, 0x00, 0x00, /* auth data */
499 /* 0x00, 0x00, 0x03, 0x84, */ /* default ttl (15 minues) */
500 0x00, 0x01, 0x51, 0x80, /* default ttl (24h = 86400s) */
501 0x01, 0x00, 0x00, 0x00, /* loc count, eid len, ACT, A */
502 0x00, 0x00, 0x40, 0x05, /* rsvd, map ver num, AFI = MAC */
503 0x01, 0x02, 0x03, 0x04,
504 0x05, 0x06, /* MAC EID */
507 0x02, 0x01, 0x00, 0x00, /* prio, weight, mprio, mweight */
508 0x00, 0x04, 0x00, 0x01, /* flags, AFI = ipv4 */
509 0x66, 0x77, 0x88, 0x99, /* ipv4 locator address */
513 _assert (0 == memcmp (expected_data, b->data, sizeof (expected_data)));
515 clib_mem_free (data);
519 static vlib_buffer_t *
520 create_buffer (u8 * data, u32 data_len)
524 u8 *buf_data = clib_mem_alloc (500);
525 clib_memset (buf_data, 0, 500);
526 b = (vlib_buffer_t *) buf_data;
528 u8 *p = vlib_buffer_put_uninit (b, data_len);
529 clib_memcpy (p, data, data_len);
534 static clib_error_t *
535 test_lisp_parse_map_reply ()
537 clib_error_t *error = 0;
540 u8 map_reply_data[] =
542 0x00, 0x00, 0x00, 0x01, /* type; rsvd; mapping count */
543 0x00, 0x00, 0x00, 0x00,
547 vlib_buffer_t *b = create_buffer (map_reply_data, sizeof (map_reply_data));
548 map_records_arg_t *mrecs = parse_map_reply (b);
549 _assert (0 == mrecs);
553 u8 map_reply_data2[] =
555 0x00, 0x00, 0x00, 0x01, /* type; rsvd */
556 0x00, 0x00, 0x00, 0x00,
557 0x00, 0x00, 0x00, 0x00, /* nonce */
559 /* 1. record - incomplete */
560 0x01, 0x02, 0x03, 0x04, /* record TTL */
561 0x01, /* locator count */
565 b = create_buffer (map_reply_data2, sizeof (map_reply_data2));
566 mrecs = parse_map_reply (b);
567 _assert (0 == mrecs);
573 static clib_error_t *
574 test_lisp_parse_lcaf ()
577 clib_error_t *error = 0;
581 vlib_buffer_t *b = 0;
584 b = clib_mem_alloc (buff_len);
585 clib_memset ((u8 *) b, 0, buff_len);
588 u8 map_reply_records[] =
591 0x01, 0x02, 0x03, 0x04, /* record TTL */
592 0x03, /* locator count */
593 0x00, 0x00, 0x00, /* eid-mask-len; ... */
594 0x00, 0x00, /* reserved; map-version num */
595 0x00, 0x01, /* EID-Prefix-AFI */
596 0x33, 0x44, 0x55, 0x66, /* eid-prefix */
601 0x0c, /* m-prority */
603 0x00, 0x00, /* unused flags */
604 0x40, 0x03, /* Loc-AFI = LCAF*/
607 0x00, 0x00, /* reserved1, flags */
608 0x02, /* type = Instance ID */
609 0x18, /* IID mask-len */
610 0x00, 0x0a, /* lenght */
611 /* LCAF Instance ID */
612 0x00, 0x00, 0x00, 0x09, /* iid */
613 0x00, 0x01, /* AFI = ipv4 */
614 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 loator address */
619 0x05, /* m-prority */
621 0x00, 0x00, /* unused flags */
622 0x40, 0x03, /* Loc-AFI = LCAF*/
625 0x00, 0x00, /* reserved1, flags */
626 0x02, /* type = Instance ID */
627 0x18, /* IID mask-len */
628 0x00, 0x16, /* iid length + next AFI lenght */
629 /* LCAF Instance ID */
630 0x22, 0x44, 0x66, 0x88, /* iid */
631 0x00, 0x02, /* AFI = ipv6 */
632 0xcc, 0xdd, 0xee, 0xff,
633 0x88, 0x99, 0xaa, 0xbb,
634 0x44, 0x55, 0x66, 0x77,
635 0x00, 0x11, 0x22, 0x33, /* ipv6 locator address */
640 0x0c, /* m-prority */
642 0x00, 0x00, /* unused flags */
643 0x00, 0x01, /* Loc-AFI */
644 0xaa, 0xbb, 0xcc, 0xdd, /* Loator */
648 b->current_length = buff_len;
649 memcpy (b->data, map_reply_records, sizeof (map_reply_records));
651 lisp_msg_parse_mapping_record (b, &eid, &locs, &probed);
652 _assert (vec_len (locs) == 3);
653 _assert (eid.ippref.addr.ip.v4.as_u32 == 0x66554433);
655 /* check 1st locator - an LCAF with ipv4 */
656 _assert (locs[0].local == 0);
657 _assert (locs[0].priority == 0xa);
658 _assert (locs[0].weight == 0xb);
659 _assert (locs[0].mpriority == 0xc);
660 _assert (locs[0].mweight == 0xd);
662 _assert (gid_address_type (&locs[0].address) == GID_ADDR_IP_PREFIX);
663 _assert (gid_address_vni (&locs[0].address) == 0x09);
664 ip_prefix_t *ip_pref = &gid_address_ippref (&locs[0].address);
665 _assert (IP4 == ip_prefix_version (ip_pref));
667 /* 2nd locator - LCAF entry with ipv6 address */
668 _assert (locs[1].local == 0);
669 _assert (locs[1].priority == 0x7);
670 _assert (locs[1].weight == 0x6);
671 _assert (locs[1].mpriority == 0x5);
672 _assert (locs[1].mweight == 0x4);
674 _assert (gid_address_type (&locs[1].address) == GID_ADDR_IP_PREFIX);
675 _assert (0x22446688 == gid_address_vni (&locs[1].address));
676 ip_pref = &gid_address_ippref (&locs[1].address);
677 _assert (IP6 == ip_prefix_version (ip_pref));
679 /* 3rd locator - simple ipv4 address */
680 _assert (gid_address_type (&locs[2].address) == GID_ADDR_IP_PREFIX);
684 for (i = 0; i < 3; i++)
685 locator_free (&locs[i]);
690 #define foreach_test_case \
691 _(lisp_msg_put_mreq) \
692 _(lisp_msg_put_mreq_with_lcaf) \
693 _(lisp_msg_push_ecm) \
695 _(lisp_msg_parse_mapping_record) \
696 _(lisp_parse_map_reply) \
701 lisp_cp_serdes_tests (vlib_main_t * vm, unformat_input_t * input)
705 #define _(_test_name) \
706 error = test_ ## _test_name (); \
709 fformat (stderr, "FAIL: test_" #_test_name "\n"); \
713 fformat (stderr, "PASS: test_" #_test_name "\n"); \
720 static clib_error_t *
721 test_locator_type (void)
723 clib_error_t *error = 0;
724 gid_address_t _gid_addr, *gid = &_gid_addr;
726 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
727 gid_address_ippref_len (gid) = 24;
728 ippref = &gid_address_ippref (gid);
729 ip_prefix_version (ippref) = IP4;
730 ip_prefix_len (ippref) = 0;
731 ip4_address_t *ip4 = &ip_prefix_v4 (ippref);
732 ip4->as_u32 = 0x20304050;
735 locator_t loc1, loc2 = {
744 locator_copy (&loc1, &loc2);
745 _assert (0 == locator_cmp (&loc1, &loc2));
750 ip_prefix_t nested_ippref;
751 ip_prefix_version (&nested_ippref) = IP4;
752 ip_prefix_len (&nested_ippref) = 0;
753 ip4 = &ip_prefix_v4 (&nested_ippref);
754 ip4->as_u32 = 0x33882299;
755 gid_address_t nested_gid = {
756 .type = GID_ADDR_IP_PREFIX,
757 .ippref = nested_ippref
761 .type = LCAF_INSTANCE_ID,
765 .gid_addr = &nested_gid}
767 gid_address_type (gid) = GID_ADDR_LCAF;
768 gid_address_lcaf (gid) = lcaf;
770 loc2.address = gid[0];
771 locator_copy (&loc1, &loc2);
773 _assert (0 == locator_cmp (&loc1, &loc2));
776 locator_free (&loc1);
780 static clib_error_t *
781 test_gid_parse_ip_pref ()
783 clib_error_t *error = 0;
784 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
785 gid_address_t _gid_addr_copy, *copy = &_gid_addr_copy;
790 0x00, 0x01, /* AFI = IPv4 */
791 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
795 u32 len = gid_address_parse (data, gid_addr);
797 gid_address_copy (copy, gid_addr);
798 _assert (0 == gid_address_cmp (copy, gid_addr));
803 static clib_error_t *
804 test_gid_parse_mac ()
806 clib_error_t *error = 0;
807 gid_address_t _gid, *gid = &_gid;
808 gid_address_t _gid_copy, *gid_copy = &_gid_copy;
813 0x40, 0x05, /* AFI = MAC address */
814 0x10, 0xbb, 0xcc, 0xdd, /* MAC */
819 u32 len = gid_address_parse (data, gid);
821 _assert (GID_ADDR_MAC == gid_address_type (gid));
822 gid_address_copy (gid_copy, gid);
823 _assert (0 == gid_address_cmp (gid_copy, gid));
828 static clib_error_t *
829 test_gid_write_nsh (void)
831 clib_error_t *error = 0;
833 u8 *b = clib_mem_alloc (500);
834 clib_memset (b, 0, 500);
840 .type = GID_ADDR_NSH,
843 u16 len = gid_address_put (b, &g);
848 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
849 0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */
851 /* Service Path ID, Service index */
852 0x11, 0x22, 0x33, 0x42, /* SPI, SI */
856 _assert (sizeof (expected) == len);
857 _assert (0 == memcmp (expected, b, len));
863 static clib_error_t *
864 test_gid_parse_nsh ()
866 clib_error_t *error = 0;
867 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
868 gid_address_t _gid_addr_copy, *copy = &_gid_addr_copy;
870 clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
871 clib_memset (copy, 0, sizeof (copy[0]));
876 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
877 0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */
879 /* Service Path ID, Service index */
880 0x55, 0x99, 0x42, 0x09, /* SPI, SI */
884 u32 len = gid_address_parse (data, gid_addr);
885 _assert (sizeof (data) == len);
886 gid_address_copy (copy, gid_addr);
887 _assert (0 == gid_address_cmp (gid_addr, copy));
888 _assert (GID_ADDR_NSH == gid_address_type (copy));
889 _assert (0 == gid_address_vni (copy));
890 _assert (gid_address_nsh_spi (copy) == 0x559942);
891 _assert (gid_address_nsh_si (copy) == 0x09);
894 gid_address_free (copy);
895 gid_address_free (gid_addr);
899 static clib_error_t *
900 test_gid_parse_lcaf ()
902 clib_error_t *error = 0;
903 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
904 gid_address_t _gid_addr_copy, *gid_addr_copy = &_gid_addr_copy;
906 clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
907 clib_memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
912 0x40, 0x03, /* AFI = LCAF*/
915 0x00, 0x00, /* reserved1, flags */
916 0x02, /* type = Instance ID */
917 0x18, /* IID mask-len */
918 0x00, 0x0a, /* iid length + next AFI lenght */
919 /* LCAF Instance ID */
920 0x00, 0x00, 0x00, 0x09, /* iid */
921 0x00, 0x01, /* AFI = ipv4 */
922 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
926 u32 len = gid_address_parse (data, gid_addr);
928 gid_address_copy (gid_addr_copy, gid_addr);
929 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
930 _assert (GID_ADDR_IP_PREFIX == gid_address_type (gid_addr));
931 _assert (9 == gid_address_vni (gid_addr));
932 _assert (0x18 == gid_address_vni_mask (gid_addr));
933 _assert (0xddccbb10 == gid_addr->ippref.addr.ip.v4.as_u32);
936 gid_address_free (gid_addr);
937 gid_address_free (gid_addr_copy);
941 /* recursive LCAFs are not supported */
943 static clib_error_t *
944 test_gid_parse_lcaf_complex ()
946 clib_error_t *error = 0;
947 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
948 gid_address_t _gid_addr_copy, *gid_addr_copy = &_gid_addr_copy;
950 clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
951 clib_memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
955 0x40, 0x03, /* AFI = LCAF */
958 0x00, 0x00, /* reserved1, flags */
959 0x02, /* type = Instance ID */
960 0x18, /* IID mask-len */
961 0x00, 0x0a, /* iid length + next AFI lenght */
962 /* LCAF Instance ID */
963 0x00, 0x00, 0x00, 0x0b, /* iid */
965 0x40, 0x03, /* AFI = LCAF */
967 0x00, 0x00, /* reserved1, flags */
968 0x02, /* type = Instance ID */
969 0x17, /* IID mask-len */
970 0x00, 0x0a, /* iid length + next AFI lenght */
971 /* LCAF Instance ID */
972 0x00, 0x00, 0x00, 0x0c, /* iid */
974 0x40, 0x03, /* AFI = LCAF */
976 0x00, 0x00, /* reserved1, flags */
977 0x02, /* type = Instance ID */
978 0x16, /* IID mask-len */
979 0x00, 0x16, /* iid length + next AFI lenght */
980 /* LCAF Instance ID */
981 0x00, 0x00, 0x00, 0x0d, /* iid */
983 0x00, 0x02, /* AFI = IPv6 */
985 0x10, 0xbb, 0xcc, 0xdd,
986 0x10, 0xbb, 0xcc, 0xdd,
987 0x10, 0xbb, 0xcc, 0xdd,
988 0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
992 u32 len = gid_address_parse (data, gid_addr);
994 _assert (gid_addr->type == GID_ADDR_LCAF);
995 gid_address_copy (gid_addr_copy, gid_addr);
996 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
997 _assert (gid_addr_copy->type == GID_ADDR_LCAF);
999 lcaf_t *lcaf = &gid_address_lcaf (gid_addr_copy);
1000 _assert (lcaf->type == LCAF_INSTANCE_ID);
1001 vni_t *v = (vni_t *) lcaf;
1002 _assert (v->vni == 0x0b);
1003 _assert (v->vni_mask_len == 0x18);
1005 gid_address_t *tmp = vni_gid (v);
1006 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
1007 lcaf = &gid_address_lcaf (tmp);
1008 _assert (lcaf->type == LCAF_INSTANCE_ID);
1011 _assert (v->vni == 0x0c);
1012 _assert (v->vni_mask_len == 0x17);
1015 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
1016 lcaf = &gid_address_lcaf (tmp);
1018 _assert (lcaf->type == LCAF_INSTANCE_ID);
1020 _assert (v->vni == 0x0d);
1021 _assert (v->vni_mask_len == 0x16);
1024 _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
1026 ip_prefix_t *ip_pref = &gid_address_ippref (tmp);
1027 ip6_address_t *ip6 = &ip_prefix_v6 (ip_pref);
1028 _assert (ip6->as_u32[0] == 0xddccbb10);
1029 _assert (ip6->as_u32[1] == 0xddccbb10);
1030 _assert (ip6->as_u32[2] == 0xddccbb10);
1031 _assert (ip6->as_u32[3] == 0xddccbb10);
1032 _assert (ip_prefix_version (ip_pref) == IP6);
1035 gid_address_free (gid_addr);
1036 gid_address_free (gid_addr_copy);
1041 static clib_error_t *
1042 test_write_mac_in_lcaf (void)
1044 clib_error_t *error = 0;
1046 u8 *b = clib_mem_alloc (500);
1047 clib_memset (b, 0, 500);
1050 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
1053 .type = GID_ADDR_MAC,
1056 u16 len = gid_address_put (b, &g);
1061 0x40, 0x03, /* AFI = LCAF */
1062 0x00, /* reserved1 */
1064 0x02, /* LCAF type = Instance ID */
1065 0x10, /* IID/IID mask len */
1066 0x00, 0x0c, /* length */
1067 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
1069 0x40, 0x05, /* AFI = MAC */
1070 0x01, 0x02, 0x03, 0x04,
1071 0x05, 0x06 /* MAC */
1075 _assert (sizeof (expected) == len);
1076 _assert (0 == memcmp (expected, b, len));
1082 static clib_error_t *
1083 test_mac_address_write (void)
1085 clib_error_t *error = 0;
1087 u8 *b = clib_mem_alloc (500);
1088 clib_memset (b, 0, 500);
1091 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
1092 .type = GID_ADDR_MAC,
1095 u16 len = gid_address_put (b, &g);
1101 0x40, 0x05, /* AFI = MAC */
1102 0x01, 0x02, 0x03, 0x04,
1103 0x05, 0x06 /* MAC */
1107 _assert (0 == memcmp (expected, b, len));
1113 static clib_error_t *
1114 test_src_dst_with_vni_serdes (void)
1116 clib_error_t *error = 0;
1117 u8 *b = clib_mem_alloc (500);
1118 clib_memset (b, 0, 500);
1123 .type = FID_ADDR_IP_PREF,
1130 .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
1137 .type = FID_ADDR_IP_PREF,
1144 .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
1158 .type = GID_ADDR_SRC_DST,
1165 u16 size_to_put = gid_address_size_to_put (&g);
1166 _assert (36 == size_to_put);
1167 _assert (0 == gid_address_len (&g));
1169 u16 write_len = gid_address_put (b, &g);
1170 _assert (size_to_put == write_len);
1173 u8 expected_data[] =
1175 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1176 0x02, 0x09, 0x00, 0x1c, /* LCAF type = IID, IID mask-len, length */
1177 0x12, 0x34, 0x56, 0x78, /* reserved; source-ML, Dest-ML */
1179 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1180 0x0c, 0x00, 0x00, 0x10, /* LCAF type = source/dest key, rsvd, length */
1181 0x00, 0x00, 0x18, 0x10, /* reserved; source-ML, Dest-ML */
1183 0x00, 0x01, /* AFI = ip4 */
1184 0x01, 0x02, 0x03, 0x00, /* source */
1186 0x00, 0x01, /* AFI = ip4 */
1187 0x09, 0x08, 0x00, 0x00, /* destination */
1191 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
1194 clib_memset (&p, 0, sizeof (p));
1195 _assert (write_len == gid_address_parse (b, &p));
1196 _assert (0 == gid_address_cmp (&g, &p));
1202 static clib_error_t *
1203 test_src_dst_deser_bad_afi (void)
1205 clib_error_t *error = 0;
1208 u8 expected_data[] =
1210 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1211 0x0c, 0x00, 0x00, 0x14, /* LCAF type = source/dest key, rsvd, length */
1212 0x00, 0x00, 0x00, 0x00, /* reserved; source-ML, Dest-ML */
1214 0xde, 0xad, /* AFI = bad value */
1215 0x11, 0x22, 0x33, 0x44,
1216 0x55, 0x66, /* source */
1218 0x40, 0x05, /* AFI = MAC */
1219 0x10, 0x21, 0x32, 0x43,
1220 0x54, 0x65, /* destination */
1225 _assert (~0 == gid_address_parse (expected_data, &p));
1230 static clib_error_t *
1231 test_src_dst_serdes (void)
1233 clib_error_t *error = 0;
1235 u8 *b = clib_mem_alloc (500);
1236 clib_memset (b, 0, 500);
1238 fid_address_t src = {
1239 .type = FID_ADDR_MAC,
1240 .mac = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
1243 fid_address_t dst = {
1244 .type = FID_ADDR_MAC,
1245 .mac = {0x10, 0x21, 0x32, 0x43, 0x54, 0x65}
1248 source_dest_t sd = {
1255 .type = GID_ADDR_SRC_DST,
1260 u16 size_to_put = gid_address_size_to_put (&g);
1261 _assert (28 == size_to_put);
1262 _assert (0 == gid_address_len (&g));
1264 u16 write_len = gid_address_put (b, &g);
1265 _assert (size_to_put == write_len);
1268 u8 expected_data[] =
1270 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1271 0x0c, 0x00, 0x00, 0x14, /* LCAF type = source/dest key, rsvd, length */
1272 0x00, 0x00, 0x00, 0x00, /* reserved; source-ML, Dest-ML */
1274 0x40, 0x05, /* AFI = MAC */
1275 0x11, 0x22, 0x33, 0x44,
1276 0x55, 0x66, /* source */
1278 0x40, 0x05, /* AFI = MAC */
1279 0x10, 0x21, 0x32, 0x43,
1280 0x54, 0x65, /* destination */
1284 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
1287 clib_memset (&p, 0, sizeof (p));
1288 _assert (write_len == gid_address_parse (b, &p));
1289 _assert (0 == gid_address_cmp (&g, &p));
1295 static clib_error_t *
1296 test_gid_address_write (void)
1298 clib_error_t *error = 0;
1299 ip_prefix_t ippref_data, *ippref = &ippref_data;
1301 u8 *b = clib_mem_alloc (500);
1302 clib_memset (b, 0, 500);
1304 ip_prefix_version (ippref) = IP4;
1305 ip_prefix_len (ippref) = 9;
1306 ip4_address_t *ip4 = &ip_prefix_v4 (ippref);
1307 ip4->as_u32 = 0xaabbccdd;
1310 .ippref = ippref[0],
1311 .type = GID_ADDR_IP_PREFIX,
1316 _assert (18 == gid_address_size_to_put (&g));
1317 _assert (gid_address_len (&g) == 9);
1319 u16 write_len = gid_address_put (b, &g);
1320 _assert (18 == write_len);
1323 u8 expected_gid_data[] =
1325 0x40, 0x03, /* AFI = LCAF */
1326 0x00, /* reserved1 */
1328 0x02, /* LCAF type = Instance ID */
1329 0x18, /* IID/VNI mask len */
1330 0x00, 0x0a, /* length */
1331 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
1333 0x00, 0x01, /* AFI = IPv4 */
1334 0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
1338 _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
1344 #undef foreach_test_case
1346 #define foreach_test_case \
1348 _(gid_parse_ip_pref) \
1353 _(mac_address_write) \
1354 _(gid_address_write) \
1356 _(write_mac_in_lcaf) \
1357 _(src_dst_deser_bad_afi) \
1358 _(src_dst_with_vni_serdes)
1361 lisp_cp_types_tests (vlib_main_t * vm, unformat_input_t * input)
1363 clib_error_t *error;
1365 #define _(_test_name) \
1366 error = test_ ## _test_name (); \
1369 fformat (stderr, "FAIL: test_" #_test_name "\n"); \
1373 fformat (stderr, "PASS: test_" #_test_name "\n"); \
1382 static clib_error_t *
1383 lisp_cp_test (vlib_main_t * vm, unformat_input_t * input,
1384 vlib_cli_command_t * cmd_arg)
1388 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1390 if (unformat (input, "serdes"))
1392 res = lisp_cp_serdes_tests (vm, input);
1394 if (unformat (input, "types"))
1396 res = lisp_cp_types_tests (vm, input);
1398 else if (unformat (input, "all"))
1400 if ((res = lisp_cp_serdes_tests (vm, input)))
1402 if ((res = lisp_cp_types_tests (vm, input)))
1411 return clib_error_return (0, "rbtree unit test failed");
1416 VLIB_CLI_COMMAND (lisp_cp_command, static) =
1418 .path = "test lisp cp",
1419 .short_help = "lisp cp internal unit tests",
1420 .function = lisp_cp_test,
1426 * fd.io coding-style-patch-verification: ON
1429 * eval: (c-set-style "gnu")