21575015d7ff59f9af9d2c4adaa19d8fd7460842
[vpp.git] / src / tests / vnet / lisp-cp / test_lisp_types.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
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>
20
21 #define _assert(e)                    \
22   error = CLIB_ERROR_ASSERT (e);      \
23   if (error)                          \
24     goto done;
25
26 static clib_error_t * test_locator_type (void)
27 {
28   clib_error_t * error = 0;
29   gid_address_t _gid_addr, * gid = &_gid_addr;
30   ip_prefix_t * ippref;
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;
38
39   /* local locator */
40   locator_t loc1, loc2 = {
41     .local = 1,
42     .state = 2,
43     .sw_if_index = 8,
44     .priority = 3,
45     .weight = 100,
46     .mpriority = 4,
47     .mweight = 101
48   };
49   locator_copy (&loc1, &loc2);
50   _assert (0 == locator_cmp (&loc1, &loc2));
51
52   /* remote locator */
53   loc2.local = 0;
54
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 =
61     {
62       .type = GID_ADDR_IP_PREFIX,
63       .ippref = nested_ippref
64     };
65
66   lcaf_t lcaf =
67     {
68       .type = LCAF_INSTANCE_ID,
69       .uni =
70         {
71           .vni_mask_len = 5,
72           .vni = 0xa1b2c3d4,
73           .gid_addr = &nested_gid
74         }
75     };
76   gid_address_type (gid) = GID_ADDR_LCAF;
77   gid_address_lcaf (gid) = lcaf;
78
79   loc2.address = gid[0];
80   locator_copy(&loc1, &loc2);
81
82   _assert (0 == locator_cmp (&loc1, &loc2));
83
84 done:
85   locator_free (&loc1);
86   return error;
87 }
88
89 static clib_error_t * test_gid_parse_ip_pref ()
90 {
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;
94   u8 data[] =
95     {
96       0x00, 0x01,             /* AFI = IPv4 */
97       0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
98     };
99
100   u32 len = gid_address_parse (data, gid_addr);
101   _assert (6 == len);
102   gid_address_copy (gid_addr_copy, gid_addr);
103   _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
104 done:
105   return error;
106 }
107
108 static clib_error_t * test_gid_parse_mac ()
109 {
110   clib_error_t * error = 0;
111   gid_address_t _gid, * gid = &_gid;
112   gid_address_t _gid_copy, * gid_copy = &_gid_copy;
113
114   u8 data[] =
115     {
116       0x40, 0x05,             /* AFI = MAC address */
117       0x10, 0xbb, 0xcc, 0xdd, /* MAC */
118       0x77, 0x99,
119     };
120
121   u32 len = gid_address_parse (data, gid);
122   _assert (8 == len);
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));
126 done:
127   return error;
128 }
129
130 static clib_error_t * test_gid_parse_lcaf ()
131 {
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;
135
136   memset (gid_addr, 0, sizeof (gid_addr[0]));
137   memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
138
139   u8 data[] =
140     {
141       0x40, 0x03,             /* AFI = LCAF*/
142
143       /* LCAF header*/
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 */
152     };
153   u32 len = gid_address_parse (data, gid_addr);
154   _assert (18 == len);
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);
161
162 done:
163   gid_address_free (gid_addr);
164   gid_address_free (gid_addr_copy);
165   return error;
166 }
167
168 /* recursive LCAFs are not supported */
169 #if 0
170 static clib_error_t * test_gid_parse_lcaf_complex ()
171 {
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;
175
176   memset (gid_addr, 0, sizeof (gid_addr[0]));
177   memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
178
179   u8 data[] =
180     {
181       0x40, 0x03,             /* AFI = LCAF*/
182
183       /* LCAF header*/
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 */
190
191       0x40, 0x03,             /* AFI = LCAF*/
192       /* LCAF header*/
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 */
199
200       0x40, 0x03,             /* AFI = LCAF*/
201       /* LCAF header*/
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 */
208
209       0x00, 0x02,             /* AFI = IPv6 */
210
211       0x10, 0xbb, 0xcc, 0xdd,
212       0x10, 0xbb, 0xcc, 0xdd,
213       0x10, 0xbb, 0xcc, 0xdd,
214       0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
215     };
216   u32 len = gid_address_parse (data, gid_addr);
217   _assert (54 == len);
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);
222
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);
228
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);
233
234   v = (vni_t *) lcaf;
235   _assert (v->vni == 0x0c);
236   _assert (v->vni_mask_len == 0x17);
237
238   tmp = vni_gid (v);
239   _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
240   lcaf = &gid_address_lcaf (tmp);
241
242   _assert (lcaf->type == LCAF_INSTANCE_ID);
243   v = (vni_t *) lcaf;
244   _assert (v->vni == 0x0d);
245   _assert (v->vni_mask_len == 0x16);
246
247   tmp = vni_gid (v);
248   _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
249
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);
257
258 done:
259   gid_address_free (gid_addr);
260   gid_address_free (gid_addr_copy);
261   return error;
262 }
263 #endif
264
265 static clib_error_t * test_write_mac_in_lcaf (void)
266 {
267   clib_error_t * error = 0;
268
269   u8 * b = clib_mem_alloc(500);
270   memset(b, 0, 500);
271
272   gid_address_t g =
273     {
274       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
275       .vni = 0x01020304,
276       .vni_mask = 0x10,
277       .type = GID_ADDR_MAC,
278     };
279
280   u16 len = gid_address_put (b, &g);
281
282   u8 expected[] =
283     {
284       0x40, 0x03,             /* AFI = LCAF */
285       0x00,                   /* reserved1 */
286       0x00,                   /* flags */
287       0x02,                   /* LCAF type = Instance ID */
288       0x10,                   /* IID/IID mask len */
289       0x00, 0x0c,             /* length */
290       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
291
292       0x40, 0x05,             /* AFI = MAC */
293       0x01, 0x02, 0x03, 0x04,
294       0x05, 0x06              /* MAC */
295     };
296   _assert (sizeof (expected) == len);
297   _assert (0 == memcmp (expected, b, len));
298 done:
299   clib_mem_free (b);
300   return error;
301 }
302
303 static clib_error_t * test_mac_address_write (void)
304 {
305   clib_error_t * error = 0;
306
307   u8 * b = clib_mem_alloc(500);
308   memset(b, 0, 500);
309
310   gid_address_t g =
311     {
312       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
313       .type = GID_ADDR_MAC,
314     };
315
316   u16 len = gid_address_put (b, &g);
317   _assert (8 == len);
318
319   u8 expected[] =
320     {
321       0x40, 0x05,             /* AFI = MAC */
322       0x01, 0x02, 0x03, 0x04,
323       0x05, 0x06              /* MAC */
324     };
325   _assert (0 == memcmp (expected, b, len));
326 done:
327   clib_mem_free (b);
328   return error;
329 }
330
331 static clib_error_t *
332 test_src_dst_with_vni_serdes (void)
333 {
334   clib_error_t * error = 0;
335   u8 * b = clib_mem_alloc (500);
336   memset (b, 0, 500);
337
338   fid_address_t src =
339     {
340       .type = FID_ADDR_IP_PREF,
341       .ippref =
342         {
343           .len = 24,
344           .addr =
345             {
346               .version = IP4,
347               .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
348             }
349         }
350     };
351
352   fid_address_t dst =
353     {
354       .type = FID_ADDR_IP_PREF,
355       .ippref =
356         {
357           .len = 16,
358           .addr =
359             {
360               .version = IP4,
361               .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
362             }
363         }
364     };
365
366   source_dest_t sd =
367     {
368       .src = src,
369       .dst = dst
370     };
371
372   gid_address_t g =
373     {
374       .sd = sd,
375       .type = GID_ADDR_SRC_DST,
376       .vni = 0x12345678,
377       .vni_mask = 0x9
378     };
379
380   u16 size_to_put = gid_address_size_to_put(&g);
381   _assert (36 == size_to_put);
382   _assert (0 == gid_address_len(&g));
383
384   u16 write_len = gid_address_put (b, &g);
385   printf("sizetoput %d; writelen %d\n", size_to_put, write_len);
386   _assert (size_to_put == write_len);
387
388   u8 expected_data[] =
389     {
390       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
391       0x02, 0x09, 0x00, 0x1c,  /* LCAF type = IID, IID mask-len, length */
392       0x12, 0x34, 0x56, 0x78,  /* reserved; source-ML, Dest-ML */
393
394       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
395       0x0c, 0x00, 0x00, 0x10,  /* LCAF type = source/dest key, rsvd, length */
396       0x00, 0x00, 0x18, 0x10,  /* reserved; source-ML, Dest-ML */
397
398       0x00, 0x01,              /* AFI = ip4 */
399       0x01, 0x02, 0x03, 0x00,  /* source */
400
401       0x00, 0x01,              /* AFI = ip4 */
402       0x09, 0x08, 0x00, 0x00,  /* destination */
403     };
404   _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
405
406   gid_address_t p;
407   memset (&p, 0, sizeof (p));
408   _assert (write_len == gid_address_parse (b, &p));
409   _assert (0 == gid_address_cmp (&g, &p));
410 done:
411   clib_mem_free (b);
412   return error;
413 }
414
415 static clib_error_t *
416 test_src_dst_deser_bad_afi (void)
417 {
418   clib_error_t * error = 0;
419
420   u8 expected_data[] =
421     {
422       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
423       0x0c, 0x00, 0x00, 0x14,  /* LCAF type = source/dest key, rsvd, length */
424       0x00, 0x00, 0x00, 0x00,  /* reserved; source-ML, Dest-ML */
425
426       0xde, 0xad,              /* AFI = bad value */
427       0x11, 0x22, 0x33, 0x44,
428       0x55, 0x66,              /* source */
429
430       0x40, 0x05,              /* AFI = MAC */
431       0x10, 0x21, 0x32, 0x43,
432       0x54, 0x65,              /* destination */
433     };
434
435   gid_address_t p;
436   _assert (~0 == gid_address_parse (expected_data, &p));
437 done:
438   return error;
439 }
440
441 static clib_error_t *
442 test_src_dst_serdes (void)
443 {
444   clib_error_t * error = 0;
445
446   u8 * b = clib_mem_alloc (500);
447   memset (b, 0, 500);
448
449   fid_address_t src =
450     {
451       .type = FID_ADDR_MAC,
452       .mac = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
453     };
454
455   fid_address_t dst =
456     {
457       .type = FID_ADDR_MAC,
458       .mac = { 0x10, 0x21, 0x32, 0x43, 0x54, 0x65 }
459     };
460
461   source_dest_t sd =
462     {
463       .src = src,
464       .dst = dst
465     };
466
467   gid_address_t g =
468     {
469       .sd = sd,
470       .type = GID_ADDR_SRC_DST,
471       .vni = 0x0,
472       .vni_mask = 0x0
473     };
474
475   u16 size_to_put = gid_address_size_to_put(&g);
476   _assert (28 == size_to_put);
477   _assert (0 == gid_address_len(&g));
478
479   u16 write_len = gid_address_put (b, &g);
480   _assert (size_to_put == write_len);
481
482   u8 expected_data[] =
483     {
484       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
485       0x0c, 0x00, 0x00, 0x14,  /* LCAF type = source/dest key, rsvd, length */
486       0x00, 0x00, 0x00, 0x00,  /* reserved; source-ML, Dest-ML */
487
488       0x40, 0x05,              /* AFI = MAC */
489       0x11, 0x22, 0x33, 0x44,
490       0x55, 0x66,              /* source */
491
492       0x40, 0x05,              /* AFI = MAC */
493       0x10, 0x21, 0x32, 0x43,
494       0x54, 0x65,              /* destination */
495     };
496   _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
497
498   gid_address_t p;
499   memset (&p, 0, sizeof (p));
500   _assert (write_len == gid_address_parse (b, &p));
501   _assert (0 == gid_address_cmp (&g, &p));
502 done:
503   clib_mem_free (b);
504   return error;
505 }
506
507 static clib_error_t * test_gid_address_write (void)
508 {
509   clib_error_t * error = 0;
510   ip_prefix_t ippref_data, * ippref = &ippref_data;
511
512   u8 * b = clib_mem_alloc(500);
513   memset(b, 0, 500);
514
515   ip_prefix_version (ippref) = IP4;
516   ip_prefix_len (ippref) = 9;
517   ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
518   ip4->as_u32 = 0xaabbccdd;
519
520   gid_address_t g =
521     {
522       .ippref = ippref[0],
523       .type = GID_ADDR_IP_PREFIX,
524       .vni = 0x01020304,
525       .vni_mask = 0x18
526     };
527
528   _assert (18 == gid_address_size_to_put (&g));
529   _assert (gid_address_len (&g) == 9);
530
531   u16 write_len = gid_address_put (b, &g);
532   _assert (18 == write_len);
533
534   u8 expected_gid_data[] =
535     {
536       0x40, 0x03,             /* AFI = LCAF */
537       0x00,                   /* reserved1 */
538       0x00,                   /* flags */
539       0x02,                   /* LCAF type = Instance ID */
540       0x18,                   /* IID/VNI mask len */
541       0x00, 0x0a,             /* length */
542       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
543
544       0x00, 0x01,             /* AFI = IPv4 */
545       0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
546     };
547   _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
548 done:
549   clib_mem_free (b);
550   return error;
551 }
552
553 #define foreach_test_case                 \
554   _(locator_type)                         \
555   _(gid_parse_ip_pref)                    \
556   _(gid_parse_mac)                        \
557   _(gid_parse_lcaf)                       \
558   _(mac_address_write)                    \
559   _(gid_address_write)                    \
560   _(src_dst_serdes)                       \
561   _(write_mac_in_lcaf)                    \
562   _(src_dst_deser_bad_afi)                \
563   _(src_dst_with_vni_serdes)
564
565 int run_tests (void)
566 {
567   clib_error_t * error;
568
569 #define _(_test_name)                   \
570   error = test_ ## _test_name ();       \
571   if (error)                            \
572     {                                   \
573       clib_error_report (error);        \
574       return 0;                         \
575     }
576
577   foreach_test_case
578 #undef _
579
580   return 0;
581 }
582
583 int main()
584 {
585   return run_tests ();
586 }
587