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>
25 error = CLIB_ERROR_ASSERT (e); \
29 static clib_error_t * test_locator_type (void)
31 clib_error_t * error = 0;
32 gid_address_t _gid_addr, * gid = &_gid_addr;
34 gid_address_type (gid) = GID_ADDR_IP_PREFIX;
35 gid_address_ippref_len (gid) = 24;
36 ippref = &gid_address_ippref (gid);
37 ip_prefix_version (ippref) = IP4;
38 ip_prefix_len (ippref) = 0;
39 ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
40 ip4->as_u32 = 0x20304050;
43 locator_t loc1, loc2 = {
52 locator_copy (&loc1, &loc2);
53 _assert (0 == locator_cmp (&loc1, &loc2));
58 ip_prefix_t nested_ippref;
59 ip_prefix_version (&nested_ippref) = IP4;
60 ip_prefix_len (&nested_ippref) = 0;
61 ip4 = &ip_prefix_v4 (&nested_ippref);
62 ip4->as_u32 = 0x33882299;
63 gid_address_t nested_gid =
65 .type = GID_ADDR_IP_PREFIX,
66 .ippref = nested_ippref
71 .type = LCAF_INSTANCE_ID,
76 .gid_addr = &nested_gid
79 gid_address_type (gid) = GID_ADDR_LCAF;
80 gid_address_lcaf (gid) = lcaf;
82 loc2.address = gid[0];
83 locator_copy(&loc1, &loc2);
85 _assert (0 == locator_cmp (&loc1, &loc2));
92 static clib_error_t * test_gid_parse_ip_pref ()
94 clib_error_t * error = 0;
95 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
96 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
99 0x00, 0x01, /* AFI = IPv4 */
100 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
103 u32 len = gid_address_parse (data, gid_addr);
105 gid_address_copy (gid_addr_copy, gid_addr);
106 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
111 static clib_error_t * test_gid_parse_mac ()
113 clib_error_t * error = 0;
114 gid_address_t _gid, * gid = &_gid;
115 gid_address_t _gid_copy, * gid_copy = &_gid_copy;
119 0x40, 0x05, /* AFI = MAC address */
120 0x10, 0xbb, 0xcc, 0xdd, /* MAC */
124 u32 len = gid_address_parse (data, gid);
126 _assert (GID_ADDR_MAC == gid_address_type (gid));
127 gid_address_copy (gid_copy, gid);
128 _assert (0 == gid_address_cmp (gid_copy, gid));
133 static clib_error_t * test_gid_parse_lcaf ()
135 clib_error_t * error = 0;
136 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
137 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
139 memset (gid_addr, 0, sizeof (gid_addr[0]));
140 memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
144 0x40, 0x03, /* AFI = LCAF*/
147 0x00, 0x00, /* reserved1, flags */
148 0x02, /* type = Instance ID */
149 0x18, /* IID mask-len */
150 0x00, 0x0a, /* iid length + next AFI lenght */
151 /* LCAF Instance ID */
152 0x00, 0x00, 0x00, 0x09, /* iid */
153 0x00, 0x01, /* AFI = ipv4 */
154 0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
156 u32 len = gid_address_parse (data, gid_addr);
158 gid_address_copy (gid_addr_copy, gid_addr);
159 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
160 _assert (GID_ADDR_IP_PREFIX == gid_address_type (gid_addr));
161 _assert (9 == gid_address_vni (gid_addr));
162 _assert (0x18 == gid_address_vni_mask (gid_addr));
163 _assert (0xddccbb10 == gid_addr->ippref.addr.ip.v4.as_u32);
166 gid_address_free (gid_addr);
167 gid_address_free (gid_addr_copy);
171 /* recursive LCAFs are not supported */
173 static clib_error_t * test_gid_parse_lcaf_complex ()
175 clib_error_t * error = 0;
176 gid_address_t _gid_addr, * gid_addr = &_gid_addr;
177 gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
179 memset (gid_addr, 0, sizeof (gid_addr[0]));
180 memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
184 0x40, 0x03, /* AFI = LCAF*/
187 0x00, 0x00, /* reserved1, flags */
188 0x02, /* type = Instance ID */
189 0x18, /* IID mask-len */
190 0x00, 0x0a, /* iid length + next AFI lenght */
191 /* LCAF Instance ID */
192 0x00, 0x00, 0x00, 0x0b, /* iid */
194 0x40, 0x03, /* AFI = LCAF*/
196 0x00, 0x00, /* reserved1, flags */
197 0x02, /* type = Instance ID */
198 0x17, /* IID mask-len */
199 0x00, 0x0a, /* iid length + next AFI lenght */
200 /* LCAF Instance ID */
201 0x00, 0x00, 0x00, 0x0c, /* iid */
203 0x40, 0x03, /* AFI = LCAF*/
205 0x00, 0x00, /* reserved1, flags */
206 0x02, /* type = Instance ID */
207 0x16, /* IID mask-len */
208 0x00, 0x16, /* iid length + next AFI lenght */
209 /* LCAF Instance ID */
210 0x00, 0x00, 0x00, 0x0d, /* iid */
212 0x00, 0x02, /* AFI = IPv6 */
214 0x10, 0xbb, 0xcc, 0xdd,
215 0x10, 0xbb, 0xcc, 0xdd,
216 0x10, 0xbb, 0xcc, 0xdd,
217 0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
219 u32 len = gid_address_parse (data, gid_addr);
221 _assert (gid_addr->type == GID_ADDR_LCAF);
222 gid_address_copy (gid_addr_copy, gid_addr);
223 _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
224 _assert (gid_addr_copy->type == GID_ADDR_LCAF);
226 lcaf_t * lcaf = &gid_address_lcaf (gid_addr_copy);
227 _assert (lcaf->type == LCAF_INSTANCE_ID);
228 vni_t * v = (vni_t *) lcaf;
229 _assert (v->vni == 0x0b);
230 _assert (v->vni_mask_len == 0x18);
232 gid_address_t * tmp = vni_gid (v);
233 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
234 lcaf = &gid_address_lcaf (tmp);
235 _assert (lcaf->type == LCAF_INSTANCE_ID);
238 _assert (v->vni == 0x0c);
239 _assert (v->vni_mask_len == 0x17);
242 _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
243 lcaf = &gid_address_lcaf (tmp);
245 _assert (lcaf->type == LCAF_INSTANCE_ID);
247 _assert (v->vni == 0x0d);
248 _assert (v->vni_mask_len == 0x16);
251 _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
253 ip_prefix_t * ip_pref = &gid_address_ippref (tmp);
254 ip6_address_t * ip6 = &ip_prefix_v6 (ip_pref);
255 _assert (ip6->as_u32[0] == 0xddccbb10);
256 _assert (ip6->as_u32[1] == 0xddccbb10);
257 _assert (ip6->as_u32[2] == 0xddccbb10);
258 _assert (ip6->as_u32[3] == 0xddccbb10);
259 _assert (ip_prefix_version (ip_pref) == IP6);
262 gid_address_free (gid_addr);
263 gid_address_free (gid_addr_copy);
268 #if 0 /* uncomment this once VNI is supported */
269 static clib_error_t * test_write_mac_in_lcaf (void)
271 clib_error_t * error = 0;
273 u8 * b = clib_mem_alloc(500);
278 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
281 .type = GID_ADDR_MAC,
284 u16 len = gid_address_put (b, &g);
289 0x40, 0x03, /* AFI = LCAF */
290 0x00, /* reserved1 */
292 0x02, /* LCAF type = Instance ID */
293 0x20, /* IID/VNI mask len */
294 0x00, 0x0a, /* length */
295 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
297 0x00, 0x06, /* AFI = MAC */
298 0x01, 0x02, 0x03, 0x04,
301 _assert (0 == memcmp (expected, b, len));
308 static clib_error_t * test_mac_address_write (void)
310 clib_error_t * error = 0;
312 u8 * b = clib_mem_alloc(500);
317 .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
318 .type = GID_ADDR_MAC,
321 u16 len = gid_address_put (b, &g);
326 0x40, 0x05, /* AFI = MAC */
327 0x01, 0x02, 0x03, 0x04,
330 _assert (0 == memcmp (expected, b, len));
336 static clib_error_t *
337 test_src_dst_with_vni_serdes (void)
339 clib_error_t * error = 0;
340 u8 * b = clib_mem_alloc (500);
345 .type = FID_ADDR_IP_PREF,
352 .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
359 .type = FID_ADDR_IP_PREF,
366 .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
380 .type = GID_ADDR_SRC_DST,
385 u16 size_to_put = gid_address_size_to_put(&g);
386 _assert (36 == size_to_put);
387 _assert (0 == gid_address_len(&g));
389 u16 write_len = gid_address_put (b, &g);
390 printf("sizetoput %d; writelen %d\n", size_to_put, write_len);
391 _assert (size_to_put == write_len);
395 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
396 0x02, 0x09, 0x00, 0x1c, /* LCAF type = IID, IID mask-len, length */
397 0x12, 0x34, 0x56, 0x78, /* reserved; source-ML, Dest-ML */
399 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
400 0x0c, 0x00, 0x00, 0x10, /* LCAF type = source/dest key, rsvd, length */
401 0x00, 0x00, 0x18, 0x10, /* reserved; source-ML, Dest-ML */
403 0x00, 0x01, /* AFI = ip4 */
404 0x01, 0x02, 0x03, 0x00, /* source */
406 0x00, 0x01, /* AFI = ip4 */
407 0x09, 0x08, 0x00, 0x00, /* destination */
409 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
412 memset (&p, 0, sizeof (p));
413 _assert (write_len == gid_address_parse (b, &p));
414 _assert (0 == gid_address_cmp (&g, &p));
420 static clib_error_t *
421 test_src_dst_serdes (void)
423 clib_error_t * error = 0;
425 u8 * b = clib_mem_alloc (500);
430 .type = FID_ADDR_MAC,
431 .mac = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
436 .type = FID_ADDR_MAC,
437 .mac = { 0x10, 0x21, 0x32, 0x43, 0x54, 0x65 }
449 .type = GID_ADDR_SRC_DST,
454 u16 size_to_put = gid_address_size_to_put(&g);
455 _assert (28 == size_to_put);
456 _assert (0 == gid_address_len(&g));
458 u16 write_len = gid_address_put (b, &g);
459 _assert (size_to_put == write_len);
463 0x40, 0x03, 0x00, 0x00, /* AFI = LCAF, reserved1, flags */
464 0x0c, 0x00, 0x00, 0x14, /* LCAF type = source/dest key, rsvd, length */
465 0x00, 0x00, 0x00, 0x00, /* reserved; source-ML, Dest-ML */
467 0x40, 0x05, /* AFI = MAC */
468 0x11, 0x22, 0x33, 0x44,
469 0x55, 0x66, /* source */
471 0x40, 0x05, /* AFI = MAC */
472 0x10, 0x21, 0x32, 0x43,
473 0x54, 0x65, /* destination */
475 _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
478 memset (&p, 0, sizeof (p));
479 _assert (write_len == gid_address_parse (b, &p));
480 _assert (0 == gid_address_cmp (&g, &p));
486 static clib_error_t * test_gid_address_write (void)
488 clib_error_t * error = 0;
489 ip_prefix_t ippref_data, * ippref = &ippref_data;
491 u8 * b = clib_mem_alloc(500);
494 ip_prefix_version (ippref) = IP4;
495 ip_prefix_len (ippref) = 9;
496 ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
497 ip4->as_u32 = 0xaabbccdd;
502 .type = GID_ADDR_IP_PREFIX,
507 _assert (18 == gid_address_size_to_put (&g));
508 _assert (gid_address_len (&g) == 9);
510 u16 write_len = gid_address_put (b, &g);
511 _assert (18 == write_len);
513 u8 expected_gid_data[] =
515 0x40, 0x03, /* AFI = LCAF */
516 0x00, /* reserved1 */
518 0x02, /* LCAF type = Instance ID */
519 0x18, /* IID/VNI mask len */
520 0x00, 0x0a, /* length */
521 0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
523 0x00, 0x01, /* AFI = IPv4 */
524 0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
526 _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
532 #define foreach_test_case \
534 _(gid_parse_ip_pref) \
537 _(mac_address_write) \
538 _(gid_address_write) \
540 _(src_dst_with_vni_serdes)
544 clib_error_t * error;
546 #define _(_test_name) \
547 error = test_ ## _test_name (); \
550 clib_error_report (error); \