fa34a3c6200eb9941ee38744397381711d39b28a
[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 /* FIXME */
22 #include <vlibapi/api_helper_macros.h>
23
24 #define _assert(e)                    \
25   error = CLIB_ERROR_ASSERT (e);      \
26   if (error)                          \
27     goto done;
28
29 static clib_error_t * test_locator_type (void)
30 {
31   clib_error_t * error = 0;
32   gid_address_t _gid_addr, * gid = &_gid_addr;
33   ip_prefix_t * ippref;
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;
41
42   /* local locator */
43   locator_t loc1, loc2 = {
44     .local = 1,
45     .state = 2,
46     .sw_if_index = 8,
47     .priority = 3,
48     .weight = 100,
49     .mpriority = 4,
50     .mweight = 101
51   };
52   locator_copy (&loc1, &loc2);
53   _assert (0 == locator_cmp (&loc1, &loc2));
54
55   /* remote locator */
56   loc2.local = 0;
57
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 =
64     {
65       .type = GID_ADDR_IP_PREFIX,
66       .ippref = nested_ippref
67     };
68
69   lcaf_t lcaf =
70     {
71       .type = LCAF_INSTANCE_ID,
72       .uni =
73         {
74           .vni_mask_len = 5,
75           .vni = 0xa1b2c3d4,
76           .gid_addr = &nested_gid
77         }
78     };
79   gid_address_type (gid) = GID_ADDR_LCAF;
80   gid_address_lcaf (gid) = lcaf;
81
82   loc2.address = gid[0];
83   locator_copy(&loc1, &loc2);
84
85   _assert (0 == locator_cmp (&loc1, &loc2));
86
87 done:
88   locator_free (&loc1);
89   return error;
90 }
91
92 static clib_error_t * test_gid_parse_ip_pref ()
93 {
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;
97   u8 data[] =
98     {
99       0x00, 0x01,             /* AFI = IPv4 */
100       0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
101     };
102
103   u32 len = gid_address_parse (data, gid_addr);
104   _assert (6 == len);
105   gid_address_copy (gid_addr_copy, gid_addr);
106   _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
107 done:
108   return error;
109 }
110
111 static clib_error_t * test_gid_parse_mac ()
112 {
113   clib_error_t * error = 0;
114   gid_address_t _gid, * gid = &_gid;
115   gid_address_t _gid_copy, * gid_copy = &_gid_copy;
116
117   u8 data[] =
118     {
119       0x40, 0x05,             /* AFI = MAC address */
120       0x10, 0xbb, 0xcc, 0xdd, /* MAC */
121       0x77, 0x99,
122     };
123
124   u32 len = gid_address_parse (data, gid);
125   _assert (8 == len);
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));
129 done:
130   return error;
131 }
132
133 static clib_error_t * test_gid_parse_lcaf ()
134 {
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;
138
139   memset (gid_addr, 0, sizeof (gid_addr[0]));
140   memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
141
142   u8 data[] =
143     {
144       0x40, 0x03,             /* AFI = LCAF*/
145
146       /* LCAF header*/
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 */
155     };
156   u32 len = gid_address_parse (data, gid_addr);
157   _assert (18 == len);
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);
164
165 done:
166   gid_address_free (gid_addr);
167   gid_address_free (gid_addr_copy);
168   return error;
169 }
170
171 /* recursive LCAFs are not supported */
172 #if 0
173 static clib_error_t * test_gid_parse_lcaf_complex ()
174 {
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;
178
179   memset (gid_addr, 0, sizeof (gid_addr[0]));
180   memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
181
182   u8 data[] =
183     {
184       0x40, 0x03,             /* AFI = LCAF*/
185
186       /* LCAF header*/
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 */
193
194       0x40, 0x03,             /* AFI = LCAF*/
195       /* LCAF header*/
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 */
202
203       0x40, 0x03,             /* AFI = LCAF*/
204       /* LCAF header*/
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 */
211
212       0x00, 0x02,             /* AFI = IPv6 */
213
214       0x10, 0xbb, 0xcc, 0xdd,
215       0x10, 0xbb, 0xcc, 0xdd,
216       0x10, 0xbb, 0xcc, 0xdd,
217       0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
218     };
219   u32 len = gid_address_parse (data, gid_addr);
220   _assert (54 == len);
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);
225
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);
231
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);
236
237   v = (vni_t *) lcaf;
238   _assert (v->vni == 0x0c);
239   _assert (v->vni_mask_len == 0x17);
240
241   tmp = vni_gid (v);
242   _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
243   lcaf = &gid_address_lcaf (tmp);
244
245   _assert (lcaf->type == LCAF_INSTANCE_ID);
246   v = (vni_t *) lcaf;
247   _assert (v->vni == 0x0d);
248   _assert (v->vni_mask_len == 0x16);
249
250   tmp = vni_gid (v);
251   _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
252
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);
260
261 done:
262   gid_address_free (gid_addr);
263   gid_address_free (gid_addr_copy);
264   return error;
265 }
266 #endif
267
268 #if 0 /* uncomment this once VNI is supported */
269 static clib_error_t * test_write_mac_in_lcaf (void)
270 {
271   clib_error_t * error = 0;
272
273   u8 * b = clib_mem_alloc(500);
274   memset(b, 0, 500);
275
276   gid_address_t g =
277     {
278       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
279       .vni = 0x30,
280       .vni_mask = 0x10,
281       .type = GID_ADDR_MAC,
282     };
283
284   u16 len = gid_address_put (b, &g);
285   _assert (8 == len);
286
287   u8 expected[] =
288     {
289       0x40, 0x03,             /* AFI = LCAF */
290       0x00,                   /* reserved1 */
291       0x00,                   /* flags */
292       0x02,                   /* LCAF type = Instance ID */
293       0x20,                   /* IID/VNI mask len */
294       0x00, 0x0a,             /* length */
295       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
296
297       0x00, 0x06,             /* AFI = MAC */
298       0x01, 0x02, 0x03, 0x04,
299       0x05, 0x06              /* MAC */
300     }
301   _assert (0 == memcmp (expected, b, len));
302 done:
303   clib_mem_free (b);
304   return error;
305 }
306 #endif
307
308 static clib_error_t * test_mac_address_write (void)
309 {
310   clib_error_t * error = 0;
311
312   u8 * b = clib_mem_alloc(500);
313   memset(b, 0, 500);
314
315   gid_address_t g =
316     {
317       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
318       .type = GID_ADDR_MAC,
319     };
320
321   u16 len = gid_address_put (b, &g);
322   _assert (8 == len);
323
324   u8 expected[] =
325     {
326       0x40, 0x05,             /* AFI = MAC */
327       0x01, 0x02, 0x03, 0x04,
328       0x05, 0x06              /* MAC */
329     };
330   _assert (0 == memcmp (expected, b, len));
331 done:
332   clib_mem_free (b);
333   return error;
334 }
335
336 static clib_error_t *
337 test_src_dst_with_vni_serdes (void)
338 {
339   clib_error_t * error = 0;
340   u8 * b = clib_mem_alloc (500);
341   memset (b, 0, 500);
342
343   fid_address_t src =
344     {
345       .type = FID_ADDR_IP_PREF,
346       .ippref =
347         {
348           .len = 24,
349           .addr =
350             {
351               .version = IP4,
352               .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
353             }
354         }
355     };
356
357   fid_address_t dst =
358     {
359       .type = FID_ADDR_IP_PREF,
360       .ippref =
361         {
362           .len = 16,
363           .addr =
364             {
365               .version = IP4,
366               .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
367             }
368         }
369     };
370
371   source_dest_t sd =
372     {
373       .src = src,
374       .dst = dst
375     };
376
377   gid_address_t g =
378     {
379       .sd = sd,
380       .type = GID_ADDR_SRC_DST,
381       .vni = 0x12345678,
382       .vni_mask = 0x9
383     };
384
385   u16 size_to_put = gid_address_size_to_put(&g);
386   _assert (36 == size_to_put);
387   _assert (0 == gid_address_len(&g));
388
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);
392
393   u8 expected_data[] =
394     {
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 */
398
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 */
402
403       0x00, 0x01,              /* AFI = ip4 */
404       0x01, 0x02, 0x03, 0x00,  /* source */
405
406       0x00, 0x01,              /* AFI = ip4 */
407       0x09, 0x08, 0x00, 0x00,  /* destination */
408     };
409   _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
410
411   gid_address_t p;
412   memset (&p, 0, sizeof (p));
413   _assert (write_len == gid_address_parse (b, &p));
414   _assert (0 == gid_address_cmp (&g, &p));
415 done:
416   clib_mem_free (b);
417   return error;
418 }
419
420 static clib_error_t *
421 test_src_dst_serdes (void)
422 {
423   clib_error_t * error = 0;
424
425   u8 * b = clib_mem_alloc (500);
426   memset (b, 0, 500);
427
428   fid_address_t src =
429     {
430       .type = FID_ADDR_MAC,
431       .mac = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 }
432     };
433
434   fid_address_t dst =
435     {
436       .type = FID_ADDR_MAC,
437       .mac = { 0x10, 0x21, 0x32, 0x43, 0x54, 0x65 }
438     };
439
440   source_dest_t sd =
441     {
442       .src = src,
443       .dst = dst
444     };
445
446   gid_address_t g =
447     {
448       .sd = sd,
449       .type = GID_ADDR_SRC_DST,
450       .vni = 0x0,
451       .vni_mask = 0x0
452     };
453
454   u16 size_to_put = gid_address_size_to_put(&g);
455   _assert (28 == size_to_put);
456   _assert (0 == gid_address_len(&g));
457
458   u16 write_len = gid_address_put (b, &g);
459   _assert (size_to_put == write_len);
460
461   u8 expected_data[] =
462     {
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 */
466
467       0x40, 0x05,              /* AFI = MAC */
468       0x11, 0x22, 0x33, 0x44,
469       0x55, 0x66,              /* source */
470
471       0x40, 0x05,              /* AFI = MAC */
472       0x10, 0x21, 0x32, 0x43,
473       0x54, 0x65,              /* destination */
474     };
475   _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));
476
477   gid_address_t p;
478   memset (&p, 0, sizeof (p));
479   _assert (write_len == gid_address_parse (b, &p));
480   _assert (0 == gid_address_cmp (&g, &p));
481 done:
482   clib_mem_free (b);
483   return error;
484 }
485
486 static clib_error_t * test_gid_address_write (void)
487 {
488   clib_error_t * error = 0;
489   ip_prefix_t ippref_data, * ippref = &ippref_data;
490
491   u8 * b = clib_mem_alloc(500);
492   memset(b, 0, 500);
493
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;
498
499   gid_address_t g =
500     {
501       .ippref = ippref[0],
502       .type = GID_ADDR_IP_PREFIX,
503       .vni = 0x01020304,
504       .vni_mask = 0x18
505     };
506
507   _assert (18 == gid_address_size_to_put (&g));
508   _assert (gid_address_len (&g) == 9);
509
510   u16 write_len = gid_address_put (b, &g);
511   _assert (18 == write_len);
512
513   u8 expected_gid_data[] =
514     {
515       0x40, 0x03,             /* AFI = LCAF */
516       0x00,                   /* reserved1 */
517       0x00,                   /* flags */
518       0x02,                   /* LCAF type = Instance ID */
519       0x18,                   /* IID/VNI mask len */
520       0x00, 0x0a,             /* length */
521       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
522
523       0x00, 0x01,             /* AFI = IPv4 */
524       0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
525     };
526   _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
527 done:
528   clib_mem_free (b);
529   return error;
530 }
531
532 #define foreach_test_case                 \
533   _(locator_type)                         \
534   _(gid_parse_ip_pref)                    \
535   _(gid_parse_mac)                        \
536   _(gid_parse_lcaf)                       \
537   _(mac_address_write)                    \
538   _(gid_address_write)                    \
539   _(src_dst_serdes)                       \
540   _(src_dst_with_vni_serdes)
541
542 int run_tests (void)
543 {
544   clib_error_t * error;
545
546 #define _(_test_name)                   \
547   error = test_ ## _test_name ();       \
548   if (error)                            \
549     {                                   \
550       clib_error_report (error);        \
551       return 0;                         \
552     }
553
554   foreach_test_case
555 #undef _
556
557   return 0;
558 }
559
560 int main()
561 {
562   return run_tests ();
563 }
564