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