7c55a9c1cdaa798c2a251bc1c21f7e6a1261f9db
[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, * 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 (copy, gid_addr);
103   _assert (0 == gid_address_cmp (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 *
131 test_gid_write_nsh (void)
132 {
133   clib_error_t * error = 0;
134
135   u8 * b = clib_mem_alloc(500);
136   memset(b, 0, 500);
137
138   gid_address_t g =
139     {
140       .vni = 0,
141       .nsh.spi = 0x112233,
142       .nsh.si = 0x42,
143       .type = GID_ADDR_NSH,
144     };
145
146   u16 len = gid_address_put (b, &g);
147
148   u8 expected[] =
149     {
150       0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
151       0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */
152
153       /* Service Path ID, Service index */
154       0x11, 0x22, 0x33, 0x42, /* SPI, SI */
155     };
156
157   _assert (sizeof (expected) == len);
158   _assert (0 == memcmp (expected, b, len));
159 done:
160   clib_mem_free (b);
161   return error;
162 }
163
164 static clib_error_t *
165 test_gid_parse_nsh ()
166 {
167   clib_error_t * error = 0;
168   gid_address_t _gid_addr, * gid_addr = &_gid_addr;
169   gid_address_t _gid_addr_copy, * copy = &_gid_addr_copy;
170
171   memset (gid_addr, 0, sizeof (gid_addr[0]));
172   memset (copy, 0, sizeof (copy[0]));
173
174   u8 data[] =
175     {
176       0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
177       0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */
178
179       /* Service Path ID, Service index */
180       0x55, 0x99, 0x42, 0x09, /* SPI, SI */
181     };
182
183   u32 len = gid_address_parse (data, gid_addr);
184   _assert (sizeof (data) == len);
185   gid_address_copy (copy, gid_addr);
186   _assert (0 == gid_address_cmp (gid_addr, copy));
187   _assert (GID_ADDR_NSH == gid_address_type (copy));
188   _assert (0 == gid_address_vni (copy));
189   _assert (gid_address_nsh_spi (copy) == 0x559942);
190   _assert (gid_address_nsh_si (copy) == 0x09);
191
192 done:
193   gid_address_free (copy);
194   gid_address_free (gid_addr);
195   return error;
196 }
197
198 static clib_error_t * test_gid_parse_lcaf ()
199 {
200   clib_error_t * error = 0;
201   gid_address_t _gid_addr, * gid_addr = &_gid_addr;
202   gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
203
204   memset (gid_addr, 0, sizeof (gid_addr[0]));
205   memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
206
207   u8 data[] =
208     {
209       0x40, 0x03,             /* AFI = LCAF*/
210
211       /* LCAF header*/
212       0x00, 0x00,             /* reserved1, flags */
213       0x02,                   /* type = Instance ID */
214       0x18,                   /* IID mask-len */
215       0x00, 0x0a,             /* iid length + next AFI lenght */
216       /* LCAF Instance ID */
217       0x00, 0x00, 0x00, 0x09, /* iid */
218       0x00, 0x01,             /* AFI = ipv4 */
219       0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
220     };
221   u32 len = gid_address_parse (data, gid_addr);
222   _assert (18 == len);
223   gid_address_copy (gid_addr_copy, gid_addr);
224   _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
225   _assert (GID_ADDR_IP_PREFIX == gid_address_type (gid_addr));
226   _assert (9 == gid_address_vni (gid_addr));
227   _assert (0x18 == gid_address_vni_mask (gid_addr));
228   _assert (0xddccbb10 == gid_addr->ippref.addr.ip.v4.as_u32);
229
230 done:
231   gid_address_free (gid_addr);
232   gid_address_free (gid_addr_copy);
233   return error;
234 }
235
236 /* recursive LCAFs are not supported */
237 #if 0
238 static clib_error_t * test_gid_parse_lcaf_complex ()
239 {
240   clib_error_t * error = 0;
241   gid_address_t _gid_addr, * gid_addr = &_gid_addr;
242   gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
243
244   memset (gid_addr, 0, sizeof (gid_addr[0]));
245   memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
246
247   u8 data[] =
248     {
249       0x40, 0x03,             /* AFI = LCAF*/
250
251       /* LCAF header*/
252       0x00, 0x00,             /* reserved1, flags */
253       0x02,                   /* type = Instance ID */
254       0x18,                   /* IID mask-len */
255       0x00, 0x0a,             /* iid length + next AFI lenght */
256       /* LCAF Instance ID */
257       0x00, 0x00, 0x00, 0x0b, /* iid */
258
259       0x40, 0x03,             /* AFI = LCAF*/
260       /* LCAF header*/
261       0x00, 0x00,             /* reserved1, flags */
262       0x02,                   /* type = Instance ID */
263       0x17,                   /* IID mask-len */
264       0x00, 0x0a,             /* iid length + next AFI lenght */
265       /* LCAF Instance ID */
266       0x00, 0x00, 0x00, 0x0c, /* iid */
267
268       0x40, 0x03,             /* AFI = LCAF*/
269       /* LCAF header*/
270       0x00, 0x00,             /* reserved1, flags */
271       0x02,                   /* type = Instance ID */
272       0x16,                   /* IID mask-len */
273       0x00, 0x16,             /* iid length + next AFI lenght */
274       /* LCAF Instance ID */
275       0x00, 0x00, 0x00, 0x0d, /* iid */
276
277       0x00, 0x02,             /* AFI = IPv6 */
278
279       0x10, 0xbb, 0xcc, 0xdd,
280       0x10, 0xbb, 0xcc, 0xdd,
281       0x10, 0xbb, 0xcc, 0xdd,
282       0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
283     };
284   u32 len = gid_address_parse (data, gid_addr);
285   _assert (54 == len);
286   _assert (gid_addr->type == GID_ADDR_LCAF);
287   gid_address_copy (gid_addr_copy, gid_addr);
288   _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
289   _assert (gid_addr_copy->type == GID_ADDR_LCAF);
290
291   lcaf_t * lcaf = &gid_address_lcaf (gid_addr_copy);
292   _assert (lcaf->type == LCAF_INSTANCE_ID);
293   vni_t * v = (vni_t *) lcaf;
294   _assert (v->vni == 0x0b);
295   _assert (v->vni_mask_len == 0x18);
296
297   gid_address_t * tmp = vni_gid (v);
298   _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
299   lcaf = &gid_address_lcaf (tmp);
300   _assert (lcaf->type == LCAF_INSTANCE_ID);
301
302   v = (vni_t *) lcaf;
303   _assert (v->vni == 0x0c);
304   _assert (v->vni_mask_len == 0x17);
305
306   tmp = vni_gid (v);
307   _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
308   lcaf = &gid_address_lcaf (tmp);
309
310   _assert (lcaf->type == LCAF_INSTANCE_ID);
311   v = (vni_t *) lcaf;
312   _assert (v->vni == 0x0d);
313   _assert (v->vni_mask_len == 0x16);
314
315   tmp = vni_gid (v);
316   _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
317
318   ip_prefix_t * ip_pref = &gid_address_ippref (tmp);
319   ip6_address_t * ip6 = &ip_prefix_v6 (ip_pref);
320   _assert (ip6->as_u32[0] == 0xddccbb10);
321   _assert (ip6->as_u32[1] == 0xddccbb10);
322   _assert (ip6->as_u32[2] == 0xddccbb10);
323   _assert (ip6->as_u32[3] == 0xddccbb10);
324   _assert (ip_prefix_version (ip_pref) == IP6);
325
326 done:
327   gid_address_free (gid_addr);
328   gid_address_free (gid_addr_copy);
329   return error;
330 }
331 #endif
332
333 static clib_error_t * test_write_mac_in_lcaf (void)
334 {
335   clib_error_t * error = 0;
336
337   u8 * b = clib_mem_alloc(500);
338   memset(b, 0, 500);
339
340   gid_address_t g =
341     {
342       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
343       .vni = 0x01020304,
344       .vni_mask = 0x10,
345       .type = GID_ADDR_MAC,
346     };
347
348   u16 len = gid_address_put (b, &g);
349
350   u8 expected[] =
351     {
352       0x40, 0x03,             /* AFI = LCAF */
353       0x00,                   /* reserved1 */
354       0x00,                   /* flags */
355       0x02,                   /* LCAF type = Instance ID */
356       0x10,                   /* IID/IID mask len */
357       0x00, 0x0c,             /* length */
358       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
359
360       0x40, 0x05,             /* AFI = MAC */
361       0x01, 0x02, 0x03, 0x04,
362       0x05, 0x06              /* MAC */
363     };
364   _assert (sizeof (expected) == len);
365   _assert (0 == memcmp (expected, b, len));
366 done:
367   clib_mem_free (b);
368   return error;
369 }
370
371 static clib_error_t * test_mac_address_write (void)
372 {
373   clib_error_t * error = 0;
374
375   u8 * b = clib_mem_alloc(500);
376   memset(b, 0, 500);
377
378   gid_address_t g =
379     {
380       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
381       .type = GID_ADDR_MAC,
382     };
383
384   u16 len = gid_address_put (b, &g);
385   _assert (8 == len);
386
387   u8 expected[] =
388     {
389       0x40, 0x05,             /* AFI = MAC */
390       0x01, 0x02, 0x03, 0x04,
391       0x05, 0x06              /* MAC */
392     };
393   _assert (0 == memcmp (expected, b, len));
394 done:
395   clib_mem_free (b);
396   return error;
397 }
398
399 static clib_error_t *
400 test_src_dst_with_vni_serdes (void)
401 {
402   clib_error_t * error = 0;
403   u8 * b = clib_mem_alloc (500);
404   memset (b, 0, 500);
405
406   fid_address_t src =
407     {
408       .type = FID_ADDR_IP_PREF,
409       .ippref =
410         {
411           .len = 24,
412           .addr =
413             {
414               .version = IP4,
415               .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
416             }
417         }
418     };
419
420   fid_address_t dst =
421     {
422       .type = FID_ADDR_IP_PREF,
423       .ippref =
424         {
425           .len = 16,
426           .addr =
427             {
428               .version = IP4,
429               .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
430             }
431         }
432     };
433
434   source_dest_t sd =
435     {
436       .src = src,
437       .dst = dst
438     };
439
440   gid_address_t g =
441     {
442       .sd = sd,
443       .type = GID_ADDR_SRC_DST,
444       .vni = 0x12345678,
445       .vni_mask = 0x9
446     };
447
448   u16 size_to_put = gid_address_size_to_put(&g);
449   _assert (36 == size_to_put);
450   _assert (0 == gid_address_len(&g));
451
452   u16 write_len = gid_address_put (b, &g);
453   printf("sizetoput %d; writelen %d\n", size_to_put, write_len);
454   _assert (size_to_put == write_len);
455
456   u8 expected_data[] =
457     {
458       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
459       0x02, 0x09, 0x00, 0x1c,  /* LCAF type = IID, IID mask-len, length */
460       0x12, 0x34, 0x56, 0x78,  /* reserved; source-ML, Dest-ML */
461
462       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
463       0x0c, 0x00, 0x00, 0x10,  /* LCAF type = source/dest key, rsvd, length */
464       0x00, 0x00, 0x18, 0x10,  /* reserved; source-ML, Dest-ML */
465
466       0x00, 0x01,              /* AFI = ip4 */
467       0x01, 0x02, 0x03, 0x00,  /* source */
468
469       0x00, 0x01,              /* AFI = ip4 */
470       0x09, 0x08, 0x00, 0x00,  /* destination */
471     };
472   _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
473
474   gid_address_t p;
475   memset (&p, 0, sizeof (p));
476   _assert (write_len == gid_address_parse (b, &p));
477   _assert (0 == gid_address_cmp (&g, &p));
478 done:
479   clib_mem_free (b);
480   return error;
481 }
482
483 static clib_error_t *
484 test_src_dst_deser_bad_afi (void)
485 {
486   clib_error_t * error = 0;
487
488   u8 expected_data[] =
489     {
490       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
491       0x0c, 0x00, 0x00, 0x14,  /* LCAF type = source/dest key, rsvd, length */
492       0x00, 0x00, 0x00, 0x00,  /* reserved; source-ML, Dest-ML */
493
494       0xde, 0xad,              /* AFI = bad value */
495       0x11, 0x22, 0x33, 0x44,
496       0x55, 0x66,              /* source */
497
498       0x40, 0x05,              /* AFI = MAC */
499       0x10, 0x21, 0x32, 0x43,
500       0x54, 0x65,              /* destination */
501     };
502
503   gid_address_t p;
504   _assert (~0 == gid_address_parse (expected_data, &p));
505 done:
506   return error;
507 }
508
509 static clib_error_t *
510 test_src_dst_serdes (void)
511 {
512   clib_error_t * error = 0;
513
514   u8 * b = clib_mem_alloc (500);
515   memset (b, 0, 500);
516
517   fid_address_t src =
518     {
519       .type = FID_ADDR_MAC,
520       .mac = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
521     };
522
523   fid_address_t dst =
524     {
525       .type = FID_ADDR_MAC,
526       .mac = { 0x10, 0x21, 0x32, 0x43, 0x54, 0x65 }
527     };
528
529   source_dest_t sd =
530     {
531       .src = src,
532       .dst = dst
533     };
534
535   gid_address_t g =
536     {
537       .sd = sd,
538       .type = GID_ADDR_SRC_DST,
539       .vni = 0x0,
540       .vni_mask = 0x0
541     };
542
543   u16 size_to_put = gid_address_size_to_put(&g);
544   _assert (28 == size_to_put);
545   _assert (0 == gid_address_len(&g));
546
547   u16 write_len = gid_address_put (b, &g);
548   _assert (size_to_put == write_len);
549
550   u8 expected_data[] =
551     {
552       0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
553       0x0c, 0x00, 0x00, 0x14,  /* LCAF type = source/dest key, rsvd, length */
554       0x00, 0x00, 0x00, 0x00,  /* reserved; source-ML, Dest-ML */
555
556       0x40, 0x05,              /* AFI = MAC */
557       0x11, 0x22, 0x33, 0x44,
558       0x55, 0x66,              /* source */
559
560       0x40, 0x05,              /* AFI = MAC */
561       0x10, 0x21, 0x32, 0x43,
562       0x54, 0x65,              /* destination */
563     };
564   _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
565
566   gid_address_t p;
567   memset (&p, 0, sizeof (p));
568   _assert (write_len == gid_address_parse (b, &p));
569   _assert (0 == gid_address_cmp (&g, &p));
570 done:
571   clib_mem_free (b);
572   return error;
573 }
574
575 static clib_error_t * test_gid_address_write (void)
576 {
577   clib_error_t * error = 0;
578   ip_prefix_t ippref_data, * ippref = &ippref_data;
579
580   u8 * b = clib_mem_alloc(500);
581   memset(b, 0, 500);
582
583   ip_prefix_version (ippref) = IP4;
584   ip_prefix_len (ippref) = 9;
585   ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
586   ip4->as_u32 = 0xaabbccdd;
587
588   gid_address_t g =
589     {
590       .ippref = ippref[0],
591       .type = GID_ADDR_IP_PREFIX,
592       .vni = 0x01020304,
593       .vni_mask = 0x18
594     };
595
596   _assert (18 == gid_address_size_to_put (&g));
597   _assert (gid_address_len (&g) == 9);
598
599   u16 write_len = gid_address_put (b, &g);
600   _assert (18 == write_len);
601
602   u8 expected_gid_data[] =
603     {
604       0x40, 0x03,             /* AFI = LCAF */
605       0x00,                   /* reserved1 */
606       0x00,                   /* flags */
607       0x02,                   /* LCAF type = Instance ID */
608       0x18,                   /* IID/VNI mask len */
609       0x00, 0x0a,             /* length */
610       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
611
612       0x00, 0x01,             /* AFI = IPv4 */
613       0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
614     };
615   _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
616 done:
617   clib_mem_free (b);
618   return error;
619 }
620
621 #define foreach_test_case                 \
622   _(locator_type)                         \
623   _(gid_parse_ip_pref)                    \
624   _(gid_parse_mac)                        \
625   _(gid_parse_lcaf)                       \
626   _(gid_parse_nsh)                        \
627   _(gid_write_nsh)                        \
628   _(mac_address_write)                    \
629   _(gid_address_write)                    \
630   _(src_dst_serdes)                       \
631   _(write_mac_in_lcaf)                    \
632   _(src_dst_deser_bad_afi)                \
633   _(src_dst_with_vni_serdes)
634
635 int run_tests (void)
636 {
637   clib_error_t * error;
638
639 #define _(_test_name)                   \
640   error = test_ ## _test_name ();       \
641   if (error)                            \
642     {                                   \
643       clib_error_report (error);        \
644       return 0;                         \
645     }
646
647   foreach_test_case
648 #undef _
649
650   return 0;
651 }
652
653 int main()
654 {
655   return run_tests ();
656 }
657