5d4e2a80c603c6b4424b979a77d6ed0bca2572b2
[vpp.git] / vnet / test / 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       0x00, 0x06,             /* 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
158   lcaf_t * lcaf = &gid_address_lcaf (gid_addr_copy);
159   vni_t * vni = (vni_t *) lcaf;
160   _assert (lcaf->type == LCAF_INSTANCE_ID);
161   _assert (vni->vni == 9);
162   _assert (vni->vni_mask_len == 0x18);
163
164   gid_address_t * g = vni_gid (vni);
165   _assert (gid_address_type (g) == GID_ADDR_IP_PREFIX);
166 done:
167   gid_address_free (gid_addr);
168   gid_address_free (gid_addr_copy);
169   return error;
170 }
171
172 static clib_error_t * test_gid_parse_lcaf_complex ()
173 {
174   clib_error_t * error = 0;
175   gid_address_t _gid_addr, * gid_addr = &_gid_addr;
176   gid_address_t _gid_addr_copy, * gid_addr_copy = &_gid_addr_copy;
177
178   memset (gid_addr, 0, sizeof (gid_addr[0]));
179   memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));
180
181   u8 data[] =
182     {
183       0x40, 0x03,             /* AFI = LCAF*/
184
185       /* LCAF header*/
186       0x00, 0x00,             /* reserved1, flags */
187       0x02,                   /* type = Instance ID */
188       0x18,                   /* IID mask-len */
189       0x00, 0x0a,             /* iid length + next AFI lenght */
190       /* LCAF Instance ID */
191       0x00, 0x00, 0x00, 0x0b, /* iid */
192
193       0x40, 0x03,             /* AFI = LCAF*/
194       /* LCAF header*/
195       0x00, 0x00,             /* reserved1, flags */
196       0x02,                   /* type = Instance ID */
197       0x17,                   /* IID mask-len */
198       0x00, 0x0a,             /* iid length + next AFI lenght */
199       /* LCAF Instance ID */
200       0x00, 0x00, 0x00, 0x0c, /* iid */
201
202       0x40, 0x03,             /* AFI = LCAF*/
203       /* LCAF header*/
204       0x00, 0x00,             /* reserved1, flags */
205       0x02,                   /* type = Instance ID */
206       0x16,                   /* IID mask-len */
207       0x00, 0x16,             /* iid length + next AFI lenght */
208       /* LCAF Instance ID */
209       0x00, 0x00, 0x00, 0x0d, /* iid */
210
211       0x00, 0x02,             /* AFI = IPv6 */
212
213       0x10, 0xbb, 0xcc, 0xdd,
214       0x10, 0xbb, 0xcc, 0xdd,
215       0x10, 0xbb, 0xcc, 0xdd,
216       0x10, 0xbb, 0xcc, 0xdd, /* ipv6 address */
217     };
218   u32 len = gid_address_parse (data, gid_addr);
219   _assert (54 == len);
220   _assert (gid_addr->type == GID_ADDR_LCAF);
221   gid_address_copy (gid_addr_copy, gid_addr);
222   _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
223   _assert (gid_addr_copy->type == GID_ADDR_LCAF);
224
225   lcaf_t * lcaf = &gid_address_lcaf (gid_addr_copy);
226   _assert (lcaf->type == LCAF_INSTANCE_ID);
227   vni_t * v = (vni_t *) lcaf;
228   _assert (v->vni == 0x0b);
229   _assert (v->vni_mask_len == 0x18);
230
231   gid_address_t * tmp = vni_gid (v);
232   _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
233   lcaf = &gid_address_lcaf (tmp);
234   _assert (lcaf->type == LCAF_INSTANCE_ID);
235
236   v = (vni_t *) lcaf;
237   _assert (v->vni == 0x0c);
238   _assert (v->vni_mask_len == 0x17);
239
240   tmp = vni_gid (v);
241   _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
242   lcaf = &gid_address_lcaf (tmp);
243
244   _assert (lcaf->type == LCAF_INSTANCE_ID);
245   v = (vni_t *) lcaf;
246   _assert (v->vni == 0x0d);
247   _assert (v->vni_mask_len == 0x16);
248
249   tmp = vni_gid (v);
250   _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);
251
252   ip_prefix_t * ip_pref = &gid_address_ippref (tmp);
253   ip6_address_t * ip6 = &ip_prefix_v6 (ip_pref);
254   _assert (ip6->as_u32[0] == 0xddccbb10);
255   _assert (ip6->as_u32[1] == 0xddccbb10);
256   _assert (ip6->as_u32[2] == 0xddccbb10);
257   _assert (ip6->as_u32[3] == 0xddccbb10);
258   _assert (ip_prefix_version (ip_pref) == IP6);
259
260 done:
261   gid_address_free (gid_addr);
262   gid_address_free (gid_addr_copy);
263   return error;
264 }
265
266 static clib_error_t * test_format_unformat_gid_address (void)
267 {
268   u8 * s = 0;
269   clib_error_t * error = 0;
270   unformat_input_t _input;
271   unformat_input_t * input = &_input;
272   gid_address_t _gid_addr, * gid_addr = &_gid_addr;
273   gid_address_t unformated_gid;
274
275   /* format/unformat IPv4 global ID address */
276   gid_address_type(gid_addr) = GID_ADDR_IP_PREFIX;
277   gid_address_ippref_len(gid_addr) = 24;
278   ip_prefix_version(&gid_addr->ippref) = IP4;
279   gid_addr->ippref.addr.ip.v4.as_u32 = 0x20304050;
280
281   s = format(0, "%U", format_gid_address, gid_addr);
282   vec_add1(s, 0);
283   unformat_init_string(input, (char *)s, vec_len(s));
284
285   _assert (unformat(input, "%U",
286         unformat_gid_address, &unformated_gid));
287   _assert (0 == gid_address_cmp (&unformated_gid, gid_addr));
288
289   unformat_free(input);
290   vec_free(s);
291   s = 0;
292
293   /* format/unformat IPv6 global ID address */
294   gid_address_type(gid_addr) = GID_ADDR_IP_PREFIX;
295   gid_address_ippref_len(gid_addr) = 64;
296   ip_prefix_version(&gid_addr->ippref) = IP6;
297   u8 ipv6[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
298   clib_memcpy(gid_addr->ippref.addr.ip.v6.as_u8, ipv6, sizeof(ipv6));
299
300   s = format(0, "%U", format_gid_address, gid_addr);
301   vec_add1(s, 0);
302   unformat_init_string(input, (char *)s, vec_len(s));
303
304   _assert (unformat (input, "%U", unformat_gid_address,
305         &unformated_gid));
306   _assert (0 == gid_address_cmp(&unformated_gid, gid_addr));
307
308   /* test address copy */
309   gid_address_t gid_addr_copy;
310   gid_address_copy(&gid_addr_copy, gid_addr);
311   _assert (0 == gid_address_cmp (&gid_addr_copy, gid_addr));
312
313 done:
314   unformat_free(input);
315   vec_free(s);
316   return error;
317 }
318
319 #if 0 /* uncomment this once VNI is supported */
320 static clib_error_t * test_write_mac_in_lcaf (void)
321 {
322   clib_error_t * error = 0;
323
324   u8 * b = clib_mem_alloc(500);
325   memset(b, 0, 500);
326
327   gid_address_t g =
328     {
329       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
330       .vni = 0x30,
331       .vni_mask = 0x10,
332       .type = GID_ADDR_MAC,
333     };
334
335   u16 len = gid_address_put (b, &g);
336   _assert (8 == len);
337
338   u8 expected[] =
339     {
340       0x40, 0x03,             /* AFI = LCAF */
341       0x00,                   /* reserved1 */
342       0x00,                   /* flags */
343       0x02,                   /* LCAF type = Instance ID */
344       0x20,                   /* IID/VNI mask len */
345       0x00, 0x0a,             /* length */
346       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
347
348       0x00, 0x06,             /* AFI = MAC */
349       0x01, 0x02, 0x03, 0x04,
350       0x05, 0x06              /* MAC */
351     }
352   _assert (0 == memcmp (expected, b, len));
353 done:
354   clib_mem_free (b);
355   return error;
356 }
357 #endif
358
359 static clib_error_t * test_mac_address_write (void)
360 {
361   clib_error_t * error = 0;
362
363   u8 * b = clib_mem_alloc(500);
364   memset(b, 0, 500);
365
366   gid_address_t g =
367     {
368       .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
369       .type = GID_ADDR_MAC,
370     };
371
372   u16 len = gid_address_put (b, &g);
373   _assert (8 == len);
374
375   u8 expected[] =
376     {
377       0x00, 0x06,             /* AFI = MAC */
378       0x01, 0x02, 0x03, 0x04,
379       0x05, 0x06              /* MAC */
380     };
381   _assert (0 == memcmp (expected, b, len));
382 done:
383   clib_mem_free (b);
384   return error;
385 }
386
387 static clib_error_t * test_gid_address_write (void)
388 {
389   clib_error_t * error = 0;
390   ip_prefix_t ippref_data, * ippref = &ippref_data;
391
392   u8 * b = clib_mem_alloc(500);
393   memset(b, 0, 500);
394
395   ip_prefix_version (ippref) = IP4;
396   ip4_address_t * ip4 = &ip_prefix_v4 (ippref);
397   ip4->as_u32 = 0xaabbccdd;
398
399   gid_address_t nested_gid =
400     {
401       .ippref = ippref[0],
402       .type = GID_ADDR_IP_PREFIX,
403     };
404
405   lcaf_t lcaf =
406     {
407       .type = LCAF_INSTANCE_ID,
408       .uni =
409         {
410           .vni_mask_len = 0x18,
411           .vni = 0x01020304,
412           .gid_addr = &nested_gid
413         }
414     };
415
416   gid_address_t gid =
417     {
418       .type = GID_ADDR_LCAF,
419       .lcaf = lcaf
420     };
421   _assert (18 == gid_address_size_to_put (&gid));
422
423   u16 write_len = gid_address_put (b, &gid);
424   _assert (18 == write_len);
425
426   u8 expected_gid_data[] =
427     {
428       0x40, 0x03,             /* AFI = LCAF */
429       0x00,                   /* reserved1 */
430       0x00,                   /* flags */
431       0x02,                   /* LCAF type = Instance ID */
432       0x18,                   /* IID/VNI mask len */
433       0x00, 0x0a,             /* length */
434       0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */
435
436       0x00, 0x01,             /* AFI = IPv4 */
437       0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
438     };
439   _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
440 done:
441   clib_mem_free (b);
442   return error;
443 }
444
445 #define foreach_test_case                 \
446   _(format_unformat_gid_address)          \
447   _(locator_type)                         \
448   _(gid_parse_ip_pref)                    \
449   _(gid_parse_mac)                        \
450   _(gid_parse_lcaf)                       \
451   _(gid_parse_lcaf_complex)               \
452   _(mac_address_write)                    \
453   _(gid_address_write)
454
455 int run_tests (void)
456 {
457   clib_error_t * error;
458
459 #define _(_test_name)                   \
460   error = test_ ## _test_name ();       \
461   if (error)                            \
462     {                                   \
463       clib_error_report (error);        \
464       return 0;                         \
465     }
466
467   foreach_test_case
468 #undef _
469
470   return 0;
471 }
472
473 int main()
474 {
475   return run_tests ();
476 }
477