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.ip4.as_u32 = 0xa1b2c3d4;
85 la.ippref.addr.version = AF_IP4;
87 ra.type = GID_ADDR_IP_PREFIX;
88 ra.ippref.addr.ip.ip4.as_u32 = 0x90817263;
89 ra.ippref.addr.version = AF_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.ip4.as_u32 == 0x66554433);
178 _assert (locs[0].local == 0);
179 _assert (locs[0].address.ippref.addr.ip.ip4.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 = AF_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 = AF_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) = AF_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) = AF_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.ip4.as_u32 == 0x12345678);
268 _assert (gid.ippref.addr.version == AF_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.ip4.as_u32 == 0x10203040);
275 _assert (rlocs[0].ippref.addr.version == AF_IP4);
277 _assert (rlocs[1].ippref.addr.ip.ip6.as_u32[0] == 0xffeeddcc);
278 _assert (rlocs[1].ippref.addr.ip.ip6.as_u32[1] == 0xbbaa9988);
279 _assert (rlocs[1].ippref.addr.ip.ip6.as_u32[2] == 0x77665544);
280 _assert (rlocs[1].ippref.addr.ip.ip6.as_u32[3] == 0x33221100);
281 _assert (rlocs[1].ippref.addr.version == AF_IP6);
283 lisp_msg_parse_eid_rec (b, &eid);
284 _assert (eid.ippref.addr.ip.ip4.as_u32 == 0x9abcdef0);
285 _assert (eid.ippref.addr.version == AF_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) = AF_IP4;
305 ip4_address_t *ip = &ip_prefix_v4 (&ippref);
306 ip->as_u32 = 0x11223344;
310 .type = GID_ADDR_IP_PREFIX,
317 u8 *data = clib_mem_alloc (500);
318 clib_memset (data, 0, 500);
320 h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs);
322 /* clear Nonce to simplify comparison */
323 clib_memset ((u8 *) h + 4, 0, 8);
328 0x10, 0x40, 0x00, 0x01, /* type; flags; IRC; REC count */
329 0x00, 0x00, 0x00, 0x00,
330 0x00, 0x00, 0x00, 0x00, /* nonce */
331 0x00, 0x01, /* Source-EID-AFI */
332 0x78, 0x56, 0x34, 0x12, /* Source EID Address */
335 0x40, 0x03, /* AFI = LCAF*/
337 0x00, 0x00, /* reserved1, flags */
338 0x02, /* type = Instance ID */
339 0x17, /* IID mask-len */
340 0x00, 0x0a, /* lenght */
341 0x90, 0x91, 0x92, 0x93, /* IID / VNI */
343 0x00, 0x01, /* AFI = ipv4 */
344 0x44, 0x33, 0x22, 0x11, /* ITR-RLOC Address 1 */
348 0x18, /* EID mask-len */
349 0x00, 0x01, /* EID-prefix-AFI */
350 0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */
354 _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data)));
356 clib_mem_free (data);
360 static clib_error_t *
361 test_lisp_msg_put_mreq ()
363 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
364 clib_error_t *error = 0;
365 map_request_hdr_t *h;
366 gid_address_t *rlocs = 0;
369 u8 *data = clib_mem_alloc (500);
370 clib_memset (data, 0, 500);
372 generate_rlocs (&rlocs, &rloc_count);
373 h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs);
375 /* clear Nonce to simplify comparison */
376 clib_memset ((u8 *) h + 4, 0, 8);
378 print_map_request (h);
381 u8 expected_data[50] = {
382 0x10, 0x40, 0x01, 0x01, /* type; flags; IRC; REC count */
383 0x00, 0x00, 0x00, 0x00,
384 0x00, 0x00, 0x00, 0x00, /* nonce */
385 0x00, 0x01, /* Source-EID-AFI */
386 0x78, 0x56, 0x34, 0x12, /* Source EID Address */
389 0x00, 0x01, /* ITR-RLOC-AFI 1 */
390 0x40, 0x30, 0x20, 0x10, /* ITR-RLOC Address 1 */
391 0x00, 0x02, /* ITR-RLOC-AFI 2 */
392 0xcc, 0xdd, 0xee, 0xff,
393 0x88, 0x99, 0xaa, 0xbb,
394 0x44, 0x55, 0x66, 0x77,
395 0x00, 0x11, 0x22, 0x33, /* ITR-RLOC Address 2 */
399 0x18, /* EID mask-len */
400 0x00, 0x01, /* EID-prefix-AFI */
401 0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */
405 _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data)));
408 clib_mem_free (data);
412 /* generate a vector of eid records */
414 build_test_map_records ()
416 mapping_t *records = 0;
420 .ttl = MAP_REGISTER_DEFAULT_TTL,
422 .type = GID_ADDR_MAC,
423 .mac = {1, 2, 3, 4, 5, 6},
433 .type = GID_ADDR_IP_PREFIX,
436 .ip.ip4.as_u32 = 0x99887766,
444 vec_add1 (r.locators, loc);
445 vec_add1 (records, r);
451 free_test_map_records (mapping_t * maps)
454 vec_foreach (map, maps)
456 vec_free (map->locators);
461 static clib_error_t *
462 test_lisp_map_register ()
465 clib_error_t *error = 0;
468 mapping_t *records = build_test_map_records ();
470 u8 *data = clib_mem_alloc (500);
471 clib_memset (data, 0, 500);
472 b = (vlib_buffer_t *) data;
474 lisp_msg_put_map_register (b, records, 1 /* want map notify */ ,
475 20 /* length of HMAC_SHA_1_96 */ ,
477 free_test_map_records (records);
479 /* clear Nonce to simplify comparison */
480 clib_memset ((u8 *) b->data + 4, 0, 8);
482 /* clear authentication data */
483 clib_memset ((u8 *) b->data + 16, 0, 20);
486 u8 expected_data[] = {
487 0x30, 0x00, 0x01, 0x01, /* type; rsvd; want notify; REC count */
488 0x00, 0x00, 0x00, 0x00,
489 0x00, 0x00, 0x00, 0x00, /* nonce */
490 0x00, 0x00, 0x00, 0x00, /* key id, auth data length:
491 both are zeroes because those are set in another
492 function (see auth_data_len_by_key_id())*/
493 0x00, 0x00, 0x00, 0x00,
494 0x00, 0x00, 0x00, 0x00,
495 0x00, 0x00, 0x00, 0x00,
496 0x00, 0x00, 0x00, 0x00,
497 0x00, 0x00, 0x00, 0x00, /* auth data */
500 /* 0x00, 0x00, 0x03, 0x84, */ /* default ttl (15 minues) */
501 0x00, 0x01, 0x51, 0x80, /* default ttl (24h = 86400s) */
502 0x01, 0x00, 0x00, 0x00, /* loc count, eid len, ACT, A */
503 0x00, 0x00, 0x40, 0x05, /* rsvd, map ver num, AFI = MAC */
504 0x01, 0x02, 0x03, 0x04,
505 0x05, 0x06, /* MAC EID */
508 0x02, 0x01, 0x00, 0x00, /* prio, weight, mprio, mweight */
509 0x00, 0x04, 0x00, 0x01, /* flags, AFI = ipv4 */
510 0x66, 0x77, 0x88, 0x99, /* ipv4 locator address */
514 _assert (0 == memcmp (expected_data, b->data, sizeof (expected_data)));
516 clib_mem_free (data);
520 static vlib_buffer_t *
521 create_buffer (u8 * data, u32 data_len)
525 u8 *buf_data = clib_mem_alloc (500);
526 clib_memset (buf_data, 0, 500);
527 b = (vlib_buffer_t *) buf_data;
529 u8 *p = vlib_buffer_put_uninit (b, data_len);
530 clib_memcpy (p, data, data_len);
535 static clib_error_t *
536 test_lisp_parse_map_reply ()
538 clib_error_t *error = 0;
541 u8 map_reply_data[] =
543 0x00, 0x00, 0x00, 0x01, /* type; rsvd; mapping count */
544 0x00, 0x00, 0x00, 0x00,
548 vlib_buffer_t *b = create_buffer (map_reply_data, sizeof (map_reply_data));
549 map_records_arg_t *mrecs = parse_map_reply (b);
550 _assert (0 == mrecs);
554 u8 map_reply_data2[] =
556 0x00, 0x00, 0x00, 0x01, /* type; rsvd */
557 0x00, 0x00, 0x00, 0x00,
558 0x00, 0x00, 0x00, 0x00, /* nonce */
560 /* 1. record - incomplete */
561 0x01, 0x02, 0x03, 0x04, /* record TTL */
562 0x01, /* locator count */
566 b = create_buffer (map_reply_data2, sizeof (map_reply_data2));
567 mrecs = parse_map_reply (b);
568 _assert (0 == mrecs);
574 static clib_error_t *
575 test_lisp_parse_lcaf ()
578 clib_error_t *error = 0;
582 vlib_buffer_t *b = 0;
585 b = clib_mem_alloc (buff_len);
586 clib_memset ((u8 *) b, 0, buff_len);
589 u8 map_reply_records[] =
592 0x01, 0x02, 0x03, 0x04, /* record TTL */
593 0x03, /* locator count */
594 0x00, 0x00, 0x00, /* eid-mask-len; ... */
595 0x00, 0x00, /* reserved; map-version num */
596 0x00, 0x01, /* EID-Prefix-AFI */
597 0x33, 0x44, 0x55, 0x66, /* eid-prefix */
602 0x0c, /* m-prority */
604 0x00, 0x00, /* unused flags */
605 0x40, 0x03, /* Loc-AFI = LCAF*/
608 0x00, 0x00, /* reserved1, flags */
609 0x02, /* type = Instance ID */
610 0x18, /* IID mask-len */
611 0x00, 0x0a, /* lenght */
612 /* LCAF Instance ID */
613 0x00, 0x00, 0x00, 0x09, /* iid */
614 0x00, 0x01, /* AFI = ipv4 */
615 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 loator address */
620 0x05, /* m-prority */
622 0x00, 0x00, /* unused flags */
623 0x40, 0x03, /* Loc-AFI = LCAF*/
626 0x00, 0x00, /* reserved1, flags */
627 0x02, /* type = Instance ID */
628 0x18, /* IID mask-len */
629 0x00, 0x16, /* iid length + next AFI lenght */
630 /* LCAF Instance ID */
631 0x22, 0x44, 0x66, 0x88, /* iid */
632 0x00, 0x02, /* AFI = ipv6 */
633 0xcc, 0xdd, 0xee, 0xff,
634 0x88, 0x99, 0xaa, 0xbb,
635 0x44, 0x55, 0x66, 0x77,
636 0x00, 0x11, 0x22, 0x33, /* ipv6 locator address */
641 0x0c, /* m-prority */
643 0x00, 0x00, /* unused flags */
644 0x00, 0x01, /* Loc-AFI */
645 0xaa, 0xbb, 0xcc, 0xdd, /* Loator */
649 b->current_length = buff_len;
650 memcpy (b->data, map_reply_records, sizeof (map_reply_records));
652 lisp_msg_parse_mapping_record (b, &eid, &locs, &probed);
653 _assert (vec_len (locs) == 3);
654 _assert (eid.ippref.addr.ip.ip4.as_u32 == 0x66554433);
656 /* check 1st locator - an LCAF with ipv4 */
657 _assert (locs[0].local == 0);
658 _assert (locs[0].priority == 0xa);
659 _assert (locs[0].weight == 0xb);
660 _assert (locs[0].mpriority == 0xc);
661 _assert (locs[0].mweight == 0xd);
663 _assert (gid_address_type (&locs[0].address) == GID_ADDR_IP_PREFIX);
664 _assert (gid_address_vni (&locs[0].address) == 0x09);
665 ip_prefix_t *ip_pref = &gid_address_ippref (&locs[0].address);
666 _assert (AF_IP4 == ip_prefix_version (ip_pref));
668 /* 2nd locator - LCAF entry with ipv6 address */
669 _assert (locs[1].local == 0);
670 _assert (locs[1].priority == 0x7);
671 _assert (locs[1].weight == 0x6);
672 _assert (locs[1].mpriority == 0x5);
673 _assert (locs[1].mweight == 0x4);
675 _assert (gid_address_type (&locs[1].address) == GID_ADDR_IP_PREFIX);
676 _assert (0x22446688 == gid_address_vni (&locs[1].address));
677 ip_pref = &gid_address_ippref (&locs[1].address);
678 _assert (AF_IP6 == ip_prefix_version (ip_pref));
680 /* 3rd locator - simple ipv4 address */
681 _assert (gid_address_type (&locs[2].address) == GID_ADDR_IP_PREFIX);
685 for (i = 0; i < 3; i++)
686 locator_free (&locs[i]);
691 #define foreach_test_case \
692 _(lisp_msg_put_mreq) \
693 _(lisp_msg_put_mreq_with_lcaf) \
694 _(lisp_msg_push_ecm) \
696 _(lisp_msg_parse_mapping_record) \
697 _(lisp_parse_map_reply) \
702 lisp_cp_serdes_tests (vlib_main_t * vm, unformat_input_t * input)
706 #define _(_test_name) \
707 error = test_ ## _test_name (); \
710 fformat (stderr, "FAIL: test_" #_test_name "\n"); \
714 fformat (stderr, "PASS: test_" #_test_name "\n"); \
721 static clib_error_t *
722 test_locator_type (void)
724 clib_error_t *error = 0;
725 gid_address_t _gid_addr, *gid = &_gid_addr;
727 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
728 gid_address_ippref_len (gid) = 24;
729 ippref = &gid_address_ippref (gid);
730 ip_prefix_version (ippref) = AF_IP4;
731 ip_prefix_len (ippref) = 0;
732 ip4_address_t *ip4 = &ip_prefix_v4 (ippref);
733 ip4->as_u32 = 0x20304050;
736 locator_t loc1, loc2 = {
745 locator_copy (&loc1, &loc2);
746 _assert (0 == locator_cmp (&loc1, &loc2));
751 ip_prefix_t nested_ippref;
752 ip_prefix_version (&nested_ippref) = AF_IP4;
753 ip_prefix_len (&nested_ippref) = 0;
754 ip4 = &ip_prefix_v4 (&nested_ippref);
755 ip4->as_u32 = 0x33882299;
756 gid_address_t nested_gid = {
757 .type = GID_ADDR_IP_PREFIX,
758 .ippref = nested_ippref
762 .type = LCAF_INSTANCE_ID,
766 .gid_addr = &nested_gid}
768 gid_address_type (gid) = GID_ADDR_LCAF;
769 gid_address_lcaf (gid) = lcaf;
771 loc2.address = gid[0];
772 locator_copy (&loc1, &loc2);
774 _assert (0 == locator_cmp (&loc1, &loc2));
777 locator_free (&loc1);
781 static clib_error_t *
782 test_gid_parse_ip_pref ()
784 clib_error_t *error = 0;
785 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
786 gid_address_t _gid_addr_copy, *copy = &_gid_addr_copy;
791 0x00, 0x01, /* AFI = IPv4 */
792 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
796 u32 len = gid_address_parse (data, gid_addr);
798 gid_address_copy (copy, gid_addr);
799 _assert (0 == gid_address_cmp (copy, gid_addr));
804 static clib_error_t *
805 test_gid_parse_mac ()
807 clib_error_t *error = 0;
808 gid_address_t _gid, *gid = &_gid;
809 gid_address_t _gid_copy, *gid_copy = &_gid_copy;
814 0x40, 0x05, /* AFI = MAC address */
815 0x10, 0xbb, 0xcc, 0xdd, /* MAC */
820 u32 len = gid_address_parse (data, gid);
822 _assert (GID_ADDR_MAC == gid_address_type (gid));
823 gid_address_copy (gid_copy, gid);
824 _assert (0 == gid_address_cmp (gid_copy, gid));
829 static clib_error_t *
830 test_gid_write_nsh (void)
832 clib_error_t *error = 0;
834 u8 *b = clib_mem_alloc (500);
835 clib_memset (b, 0, 500);
841 .type = GID_ADDR_NSH,
844 u16 len = gid_address_put (b, &g);
849 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
850 0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */
852 /* Service Path ID, Service index */
853 0x11, 0x22, 0x33, 0x42, /* SPI, SI */
857 _assert (sizeof (expected) == len);
858 _assert (0 == memcmp (expected, b, len));
864 static clib_error_t *
865 test_gid_parse_nsh ()
867 clib_error_t *error = 0;
868 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
869 gid_address_t _gid_addr_copy, *copy = &_gid_addr_copy;
871 clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
872 clib_memset (copy, 0, sizeof (copy[0]));
877 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
878 0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */
880 /* Service Path ID, Service index */
881 0x55, 0x99, 0x42, 0x09, /* SPI, SI */
885 u32 len = gid_address_parse (data, gid_addr);
886 _assert (sizeof (data) == len);
887 gid_address_copy (copy, gid_addr);
888 _assert (0 == gid_address_cmp (gid_addr, copy));
889 _assert (GID_ADDR_NSH == gid_address_type (copy));
890 _assert (0 == gid_address_vni (copy));
891 _assert (gid_address_nsh_spi (copy) == 0x559942);
892 _assert (gid_address_nsh_si (copy) == 0x09);
895 gid_address_free (copy);
896 gid_address_free (gid_addr);
900 static clib_error_t *
901 test_gid_parse_lcaf ()
903 clib_error_t *error = 0;
904 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
905 gid_address_t _gid_addr_copy, *gid_addr_copy = &_gid_addr_copy;
907 clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
908 clib_memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
913 0x40, 0x03, /* AFI = LCAF*/
916 0x00, 0x00, /* reserved1, flags */
917 0x02, /* type = Instance ID */
918 0x18, /* IID mask-len */
919 0x00, 0x0a, /* iid length + next AFI lenght */
920 /* LCAF Instance ID */
921 0x00, 0x00, 0x00, 0x09, /* iid */
922 0x00, 0x01, /* AFI = ipv4 */
923 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
927 u32 len = gid_address_parse (data, gid_addr);
929 gid_address_copy (gid_addr_copy, gid_addr);
930 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
931 _assert (GID_ADDR_IP_PREFIX == gid_address_type (gid_addr));
932 _assert (9 == gid_address_vni (gid_addr));
933 _assert (0x18 == gid_address_vni_mask (gid_addr));
934 _assert (0xddccbb10 == gid_addr->ippref.addr.ip.ip4.as_u32);
937 gid_address_free (gid_addr);
938 gid_address_free (gid_addr_copy);
942 /* recursive LCAFs are not supported */
944 static clib_error_t *
945 test_gid_parse_lcaf_complex ()
947 clib_error_t *error = 0;
948 gid_address_t _gid_addr, *gid_addr = &_gid_addr;
949 gid_address_t _gid_addr_copy, *gid_addr_copy = &_gid_addr_copy;
951 clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
952 clib_memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
956 0x40, 0x03, /* AFI = LCAF */
959 0x00, 0x00, /* reserved1, flags */
960 0x02, /* type = Instance ID */
961 0x18, /* IID mask-len */
962 0x00, 0x0a, /* iid length + next AFI lenght */
963 /* LCAF Instance ID */
964 0x00, 0x00, 0x00, 0x0b, /* iid */
966 0x40, 0x03, /* AFI = LCAF */
968 0x00, 0x00, /* reserved1, flags */
969 0x02, /* type = Instance ID */
970 0x17, /* IID mask-len */
971 0x00, 0x0a, /* iid length + next AFI lenght */
972 /* LCAF Instance ID */
973 0x00, 0x00, 0x00, 0x0c, /* iid */
975 0x40, 0x03, /* AFI = LCAF */
977 0x00, 0x00, /* reserved1, flags */
978 0x02, /* type = Instance ID */
979 0x16, /* IID mask-len */
980 0x00, 0x16, /* iid length + next AFI lenght */
981 /* LCAF Instance ID */
982 0x00, 0x00, 0x00, 0x0d, /* iid */
984 0x00, 0x02, /* AFI = IPv6 */
986 0x10, 0xbb, 0xcc, 0xdd,
987 0x10, 0xbb, 0xcc, 0xdd,
988 0x10, 0xbb, 0xcc, 0xdd,
989 0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
993 u32 len = gid_address_parse (data, gid_addr);
995 _assert (gid_addr->type == GID_ADDR_LCAF);
996 gid_address_copy (gid_addr_copy, gid_addr);
997 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
998 _assert (gid_addr_copy->type == GID_ADDR_LCAF);
1000 lcaf_t *lcaf = &gid_address_lcaf (gid_addr_copy);
1001 _assert (lcaf->type == LCAF_INSTANCE_ID);
1002 vni_t *v = (vni_t *) lcaf;
1003 _assert (v->vni == 0x0b);
1004 _assert (v->vni_mask_len == 0x18);
1006 gid_address_t *tmp = vni_gid (v);
1007 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
1008 lcaf = &gid_address_lcaf (tmp);
1009 _assert (lcaf->type == LCAF_INSTANCE_ID);
1012 _assert (v->vni == 0x0c);
1013 _assert (v->vni_mask_len == 0x17);
1016 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
1017 lcaf = &gid_address_lcaf (tmp);
1019 _assert (lcaf->type == LCAF_INSTANCE_ID);
1021 _assert (v->vni == 0x0d);
1022 _assert (v->vni_mask_len == 0x16);
1025 _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
1027 ip_prefix_t *ip_pref = &gid_address_ippref (tmp);
1028 ip6_address_t *ip6 = &ip_prefix_v6 (ip_pref);
1029 _assert (ip6->as_u32[0] == 0xddccbb10);
1030 _assert (ip6->as_u32[1] == 0xddccbb10);
1031 _assert (ip6->as_u32[2] == 0xddccbb10);
1032 _assert (ip6->as_u32[3] == 0xddccbb10);
1033 _assert (ip_prefix_version (ip_pref) == AF_IP6);
1036 gid_address_free (gid_addr);
1037 gid_address_free (gid_addr_copy);
1042 static clib_error_t *
1043 test_write_mac_in_lcaf (void)
1045 clib_error_t *error = 0;
1047 u8 *b = clib_mem_alloc (500);
1048 clib_memset (b, 0, 500);
1051 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
1054 .type = GID_ADDR_MAC,
1057 u16 len = gid_address_put (b, &g);
1062 0x40, 0x03, /* AFI = LCAF */
1063 0x00, /* reserved1 */
1065 0x02, /* LCAF type = Instance ID */
1066 0x10, /* IID/IID mask len */
1067 0x00, 0x0c, /* length */
1068 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
1070 0x40, 0x05, /* AFI = MAC */
1071 0x01, 0x02, 0x03, 0x04,
1072 0x05, 0x06 /* MAC */
1076 _assert (sizeof (expected) == len);
1077 _assert (0 == memcmp (expected, b, len));
1083 static clib_error_t *
1084 test_mac_address_write (void)
1086 clib_error_t *error = 0;
1088 u8 *b = clib_mem_alloc (500);
1089 clib_memset (b, 0, 500);
1092 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
1093 .type = GID_ADDR_MAC,
1096 u16 len = gid_address_put (b, &g);
1102 0x40, 0x05, /* AFI = MAC */
1103 0x01, 0x02, 0x03, 0x04,
1104 0x05, 0x06 /* MAC */
1108 _assert (0 == memcmp (expected, b, len));
1114 static clib_error_t *
1115 test_src_dst_with_vni_serdes (void)
1117 clib_error_t *error = 0;
1118 u8 *b = clib_mem_alloc (500);
1119 clib_memset (b, 0, 500);
1124 .type = FID_ADDR_IP_PREF,
1131 .ip.ip4.data = { 0x1, 0x2, 0x3, 0x0 }
1138 .type = FID_ADDR_IP_PREF,
1145 .ip.ip4.data = { 0x9, 0x8, 0x0, 0x0 }
1159 .type = GID_ADDR_SRC_DST,
1166 u16 size_to_put = gid_address_size_to_put (&g);
1167 _assert (36 == size_to_put);
1168 _assert (0 == gid_address_len (&g));
1170 u16 write_len = gid_address_put (b, &g);
1171 _assert (size_to_put == write_len);
1174 u8 expected_data[] =
1176 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1177 0x02, 0x09, 0x00, 0x1c, /* LCAF type = IID, IID mask-len, length */
1178 0x12, 0x34, 0x56, 0x78, /* reserved; source-ML, Dest-ML */
1180 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1181 0x0c, 0x00, 0x00, 0x10, /* LCAF type = source/dest key, rsvd, length */
1182 0x00, 0x00, 0x18, 0x10, /* reserved; source-ML, Dest-ML */
1184 0x00, 0x01, /* AFI = ip4 */
1185 0x01, 0x02, 0x03, 0x00, /* source */
1187 0x00, 0x01, /* AFI = ip4 */
1188 0x09, 0x08, 0x00, 0x00, /* destination */
1192 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
1195 clib_memset (&p, 0, sizeof (p));
1196 _assert (write_len == gid_address_parse (b, &p));
1197 _assert (0 == gid_address_cmp (&g, &p));
1203 static clib_error_t *
1204 test_src_dst_deser_bad_afi (void)
1206 clib_error_t *error = 0;
1209 u8 expected_data[] =
1211 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1212 0x0c, 0x00, 0x00, 0x14, /* LCAF type = source/dest key, rsvd, length */
1213 0x00, 0x00, 0x00, 0x00, /* reserved; source-ML, Dest-ML */
1215 0xde, 0xad, /* AFI = bad value */
1216 0x11, 0x22, 0x33, 0x44,
1217 0x55, 0x66, /* source */
1219 0x40, 0x05, /* AFI = MAC */
1220 0x10, 0x21, 0x32, 0x43,
1221 0x54, 0x65, /* destination */
1226 _assert (~0 == gid_address_parse (expected_data, &p));
1231 static clib_error_t *
1232 test_src_dst_serdes (void)
1234 clib_error_t *error = 0;
1236 u8 *b = clib_mem_alloc (500);
1237 clib_memset (b, 0, 500);
1239 fid_address_t src = {
1240 .type = FID_ADDR_MAC,
1241 .mac = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
1244 fid_address_t dst = {
1245 .type = FID_ADDR_MAC,
1246 .mac = {0x10, 0x21, 0x32, 0x43, 0x54, 0x65}
1249 source_dest_t sd = {
1256 .type = GID_ADDR_SRC_DST,
1261 u16 size_to_put = gid_address_size_to_put (&g);
1262 _assert (28 == size_to_put);
1263 _assert (0 == gid_address_len (&g));
1265 u16 write_len = gid_address_put (b, &g);
1266 _assert (size_to_put == write_len);
1269 u8 expected_data[] =
1271 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
1272 0x0c, 0x00, 0x00, 0x14, /* LCAF type = source/dest key, rsvd, length */
1273 0x00, 0x00, 0x00, 0x00, /* reserved; source-ML, Dest-ML */
1275 0x40, 0x05, /* AFI = MAC */
1276 0x11, 0x22, 0x33, 0x44,
1277 0x55, 0x66, /* source */
1279 0x40, 0x05, /* AFI = MAC */
1280 0x10, 0x21, 0x32, 0x43,
1281 0x54, 0x65, /* destination */
1285 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
1288 clib_memset (&p, 0, sizeof (p));
1289 _assert (write_len == gid_address_parse (b, &p));
1290 _assert (0 == gid_address_cmp (&g, &p));
1296 static clib_error_t *
1297 test_gid_address_write (void)
1299 clib_error_t *error = 0;
1300 ip_prefix_t ippref_data, *ippref = &ippref_data;
1302 u8 *b = clib_mem_alloc (500);
1303 clib_memset (b, 0, 500);
1305 ip_prefix_version (ippref) = AF_IP4;
1306 ip_prefix_len (ippref) = 9;
1307 ip4_address_t *ip4 = &ip_prefix_v4 (ippref);
1308 ip4->as_u32 = 0xaabbccdd;
1311 .ippref = ippref[0],
1312 .type = GID_ADDR_IP_PREFIX,
1317 _assert (18 == gid_address_size_to_put (&g));
1318 _assert (gid_address_len (&g) == 9);
1320 u16 write_len = gid_address_put (b, &g);
1321 _assert (18 == write_len);
1324 u8 expected_gid_data[] =
1326 0x40, 0x03, /* AFI = LCAF */
1327 0x00, /* reserved1 */
1329 0x02, /* LCAF type = Instance ID */
1330 0x18, /* IID/VNI mask len */
1331 0x00, 0x0a, /* length */
1332 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
1334 0x00, 0x01, /* AFI = IPv4 */
1335 0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
1339 _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
1345 #undef foreach_test_case
1347 #define foreach_test_case \
1349 _(gid_parse_ip_pref) \
1354 _(mac_address_write) \
1355 _(gid_address_write) \
1357 _(write_mac_in_lcaf) \
1358 _(src_dst_deser_bad_afi) \
1359 _(src_dst_with_vni_serdes)
1362 lisp_cp_types_tests (vlib_main_t * vm, unformat_input_t * input)
1364 clib_error_t *error;
1366 #define _(_test_name) \
1367 error = test_ ## _test_name (); \
1370 fformat (stderr, "FAIL: test_" #_test_name "\n"); \
1374 fformat (stderr, "PASS: test_" #_test_name "\n"); \
1383 static clib_error_t *
1384 lisp_cp_test (vlib_main_t * vm, unformat_input_t * input,
1385 vlib_cli_command_t * cmd_arg)
1389 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1391 if (unformat (input, "serdes"))
1393 res = lisp_cp_serdes_tests (vm, input);
1395 if (unformat (input, "types"))
1397 res = lisp_cp_types_tests (vm, input);
1399 else if (unformat (input, "all"))
1401 if ((res = lisp_cp_serdes_tests (vm, input)))
1403 if ((res = lisp_cp_types_tests (vm, input)))
1412 return clib_error_return (0, "rbtree unit test failed");
1417 VLIB_CLI_COMMAND (lisp_cp_command, static) =
1419 .path = "test lisp cp",
1420 .short_help = "lisp cp internal unit tests",
1421 .function = lisp_cp_test,
1427 * fd.io coding-style-patch-verification: ON
1430 * eval: (c-set-style "gnu")