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 error = CLIB_ERROR_ASSERT (e); \
26 static clib_error_t * test_locator_type (void)
28 clib_error_t * error = 0;
29 gid_address_t _gid_addr, * gid = &_gid_addr;
31 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
32 gid_address_ippref_len (gid) = 24;
33 ippref = &gid_address_ippref (gid);
34 ip_prefix_version (ippref) = IP4;
35 ip_prefix_len (ippref) = 0;
36 ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
37 ip4->as_u32 = 0x20304050;
40 locator_t loc1, loc2 = {
49 locator_copy (&loc1, &loc2);
50 _assert (0 == locator_cmp (&loc1, &loc2));
55 ip_prefix_t nested_ippref;
56 ip_prefix_version (&nested_ippref) = IP4;
57 ip_prefix_len (&nested_ippref) = 0;
58 ip4 = &ip_prefix_v4 (&nested_ippref);
59 ip4->as_u32 = 0x33882299;
60 gid_address_t nested_gid =
62 .type = GID_ADDR_IP_PREFIX,
63 .ippref = nested_ippref
68 .type = LCAF_INSTANCE_ID,
73 .gid_addr = &nested_gid
76 gid_address_type (gid) = GID_ADDR_LCAF;
77 gid_address_lcaf (gid) = lcaf;
79 loc2.address = gid[0];
80 locator_copy(&loc1, &loc2);
82 _assert (0 == locator_cmp (&loc1, &loc2));
89 static clib_error_t * test_gid_parse_ip_pref ()
91 clib_error_t * error = 0;
92 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
93 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
96 0x00, 0x01, /* AFI = IPv4 */
97 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
100 u32 len = gid_address_parse (data, gid_addr);
102 gid_address_copy (gid_addr_copy, gid_addr);
103 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
108 static clib_error_t * test_gid_parse_mac ()
110 clib_error_t * error = 0;
111 gid_address_t _gid, * gid = &_gid;
112 gid_address_t _gid_copy, * gid_copy = &_gid_copy;
116 0x40, 0x05, /* AFI = MAC address */
117 0x10, 0xbb, 0xcc, 0xdd, /* MAC */
121 u32 len = gid_address_parse (data, gid);
123 _assert (GID_ADDR_MAC == gid_address_type (gid));
124 gid_address_copy (gid_copy, gid);
125 _assert (0 == gid_address_cmp (gid_copy, gid));
130 static clib_error_t * test_gid_parse_lcaf ()
132 clib_error_t * error = 0;
133 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
134 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
136 memset (gid_addr, 0, sizeof (gid_addr[0]));
137 memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
141 0x40, 0x03, /* AFI = LCAF*/
144 0x00, 0x00, /* reserved1, flags */
145 0x02, /* type = Instance ID */
146 0x18, /* IID mask-len */
147 0x00, 0x0a, /* iid length + next AFI lenght */
148 /* LCAF Instance ID */
149 0x00, 0x00, 0x00, 0x09, /* iid */
150 0x00, 0x01, /* AFI = ipv4 */
151 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
153 u32 len = gid_address_parse (data, gid_addr);
155 gid_address_copy (gid_addr_copy, gid_addr);
156 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
157 _assert (GID_ADDR_IP_PREFIX == gid_address_type (gid_addr));
158 _assert (9 == gid_address_vni (gid_addr));
159 _assert (0x18 == gid_address_vni_mask (gid_addr));
160 _assert (0xddccbb10 == gid_addr->ippref.addr.ip.v4.as_u32);
163 gid_address_free (gid_addr);
164 gid_address_free (gid_addr_copy);
168 /* recursive LCAFs are not supported */
170 static clib_error_t * test_gid_parse_lcaf_complex ()
172 clib_error_t * error = 0;
173 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
174 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
176 memset (gid_addr, 0, sizeof (gid_addr[0]));
177 memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
181 0x40, 0x03, /* AFI = LCAF*/
184 0x00, 0x00, /* reserved1, flags */
185 0x02, /* type = Instance ID */
186 0x18, /* IID mask-len */
187 0x00, 0x0a, /* iid length + next AFI lenght */
188 /* LCAF Instance ID */
189 0x00, 0x00, 0x00, 0x0b, /* iid */
191 0x40, 0x03, /* AFI = LCAF*/
193 0x00, 0x00, /* reserved1, flags */
194 0x02, /* type = Instance ID */
195 0x17, /* IID mask-len */
196 0x00, 0x0a, /* iid length + next AFI lenght */
197 /* LCAF Instance ID */
198 0x00, 0x00, 0x00, 0x0c, /* iid */
200 0x40, 0x03, /* AFI = LCAF*/
202 0x00, 0x00, /* reserved1, flags */
203 0x02, /* type = Instance ID */
204 0x16, /* IID mask-len */
205 0x00, 0x16, /* iid length + next AFI lenght */
206 /* LCAF Instance ID */
207 0x00, 0x00, 0x00, 0x0d, /* iid */
209 0x00, 0x02, /* AFI = IPv6 */
211 0x10, 0xbb, 0xcc, 0xdd,
212 0x10, 0xbb, 0xcc, 0xdd,
213 0x10, 0xbb, 0xcc, 0xdd,
214 0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
216 u32 len = gid_address_parse (data, gid_addr);
218 _assert (gid_addr->type == GID_ADDR_LCAF);
219 gid_address_copy (gid_addr_copy, gid_addr);
220 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
221 _assert (gid_addr_copy->type == GID_ADDR_LCAF);
223 lcaf_t * lcaf = &gid_address_lcaf (gid_addr_copy);
224 _assert (lcaf->type == LCAF_INSTANCE_ID);
225 vni_t * v = (vni_t *) lcaf;
226 _assert (v->vni == 0x0b);
227 _assert (v->vni_mask_len == 0x18);
229 gid_address_t * tmp = vni_gid (v);
230 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
231 lcaf = &gid_address_lcaf (tmp);
232 _assert (lcaf->type == LCAF_INSTANCE_ID);
235 _assert (v->vni == 0x0c);
236 _assert (v->vni_mask_len == 0x17);
239 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
240 lcaf = &gid_address_lcaf (tmp);
242 _assert (lcaf->type == LCAF_INSTANCE_ID);
244 _assert (v->vni == 0x0d);
245 _assert (v->vni_mask_len == 0x16);
248 _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
250 ip_prefix_t * ip_pref = &gid_address_ippref (tmp);
251 ip6_address_t * ip6 = &ip_prefix_v6 (ip_pref);
252 _assert (ip6->as_u32[0] == 0xddccbb10);
253 _assert (ip6->as_u32[1] == 0xddccbb10);
254 _assert (ip6->as_u32[2] == 0xddccbb10);
255 _assert (ip6->as_u32[3] == 0xddccbb10);
256 _assert (ip_prefix_version (ip_pref) == IP6);
259 gid_address_free (gid_addr);
260 gid_address_free (gid_addr_copy);
265 #if 0 /* uncomment this once VNI is supported */
266 static clib_error_t * test_write_mac_in_lcaf (void)
268 clib_error_t * error = 0;
270 u8 * b = clib_mem_alloc(500);
275 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
278 .type = GID_ADDR_MAC,
281 u16 len = gid_address_put (b, &g);
286 0x40, 0x03, /* AFI = LCAF */
287 0x00, /* reserved1 */
289 0x02, /* LCAF type = Instance ID */
290 0x20, /* IID/VNI mask len */
291 0x00, 0x0a, /* length */
292 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
294 0x00, 0x06, /* AFI = MAC */
295 0x01, 0x02, 0x03, 0x04,
298 _assert (0 == memcmp (expected, b, len));
305 static clib_error_t * test_mac_address_write (void)
307 clib_error_t * error = 0;
309 u8 * b = clib_mem_alloc(500);
314 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
315 .type = GID_ADDR_MAC,
318 u16 len = gid_address_put (b, &g);
323 0x40, 0x05, /* AFI = MAC */
324 0x01, 0x02, 0x03, 0x04,
327 _assert (0 == memcmp (expected, b, len));
333 static clib_error_t *
334 test_src_dst_with_vni_serdes (void)
336 clib_error_t * error = 0;
337 u8 * b = clib_mem_alloc (500);
342 .type = FID_ADDR_IP_PREF,
349 .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
356 .type = FID_ADDR_IP_PREF,
363 .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
377 .type = GID_ADDR_SRC_DST,
382 u16 size_to_put = gid_address_size_to_put(&g);
383 _assert (36 == size_to_put);
384 _assert (0 == gid_address_len(&g));
386 u16 write_len = gid_address_put (b, &g);
387 printf("sizetoput %d; writelen %d\n", size_to_put, write_len);
388 _assert (size_to_put == write_len);
392 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
393 0x02, 0x09, 0x00, 0x1c, /* LCAF type = IID, IID mask-len, length */
394 0x12, 0x34, 0x56, 0x78, /* reserved; source-ML, Dest-ML */
396 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
397 0x0c, 0x00, 0x00, 0x10, /* LCAF type = source/dest key, rsvd, length */
398 0x00, 0x00, 0x18, 0x10, /* reserved; source-ML, Dest-ML */
400 0x00, 0x01, /* AFI = ip4 */
401 0x01, 0x02, 0x03, 0x00, /* source */
403 0x00, 0x01, /* AFI = ip4 */
404 0x09, 0x08, 0x00, 0x00, /* destination */
406 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
409 memset (&p, 0, sizeof (p));
410 _assert (write_len == gid_address_parse (b, &p));
411 _assert (0 == gid_address_cmp (&g, &p));
417 static clib_error_t *
418 test_src_dst_serdes (void)
420 clib_error_t * error = 0;
422 u8 * b = clib_mem_alloc (500);
427 .type = FID_ADDR_MAC,
428 .mac = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
433 .type = FID_ADDR_MAC,
434 .mac = { 0x10, 0x21, 0x32, 0x43, 0x54, 0x65 }
446 .type = GID_ADDR_SRC_DST,
451 u16 size_to_put = gid_address_size_to_put(&g);
452 _assert (28 == size_to_put);
453 _assert (0 == gid_address_len(&g));
455 u16 write_len = gid_address_put (b, &g);
456 _assert (size_to_put == write_len);
460 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
461 0x0c, 0x00, 0x00, 0x14, /* LCAF type = source/dest key, rsvd, length */
462 0x00, 0x00, 0x00, 0x00, /* reserved; source-ML, Dest-ML */
464 0x40, 0x05, /* AFI = MAC */
465 0x11, 0x22, 0x33, 0x44,
466 0x55, 0x66, /* source */
468 0x40, 0x05, /* AFI = MAC */
469 0x10, 0x21, 0x32, 0x43,
470 0x54, 0x65, /* destination */
472 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
475 memset (&p, 0, sizeof (p));
476 _assert (write_len == gid_address_parse (b, &p));
477 _assert (0 == gid_address_cmp (&g, &p));
483 static clib_error_t * test_gid_address_write (void)
485 clib_error_t * error = 0;
486 ip_prefix_t ippref_data, * ippref = &ippref_data;
488 u8 * b = clib_mem_alloc(500);
491 ip_prefix_version (ippref) = IP4;
492 ip_prefix_len (ippref) = 9;
493 ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
494 ip4->as_u32 = 0xaabbccdd;
499 .type = GID_ADDR_IP_PREFIX,
504 _assert (18 == gid_address_size_to_put (&g));
505 _assert (gid_address_len (&g) == 9);
507 u16 write_len = gid_address_put (b, &g);
508 _assert (18 == write_len);
510 u8 expected_gid_data[] =
512 0x40, 0x03, /* AFI = LCAF */
513 0x00, /* reserved1 */
515 0x02, /* LCAF type = Instance ID */
516 0x18, /* IID/VNI mask len */
517 0x00, 0x0a, /* length */
518 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
520 0x00, 0x01, /* AFI = IPv4 */
521 0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
523 _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
529 #define foreach_test_case \
531 _(gid_parse_ip_pref) \
534 _(mac_address_write) \
535 _(gid_address_write) \
537 _(src_dst_with_vni_serdes)
541 clib_error_t * error;
543 #define _(_test_name) \
544 error = test_ ## _test_name (); \
547 clib_error_report (error); \