3e7300f5dd229d4ed8865f4b18d348b83a18403c
[vpp.git] / src / plugins / ioam / lib-vxlan-gpe / vxlan_gpe_test.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  *------------------------------------------------------------------
17  * vxlan_gpe_test.c - test harness for vxlan_gpe plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
26
27 #define __plugin_msg_base vxlan_gpe_test_main.msg_id_base
28 #include <vlibapi/vat_helper_macros.h>
29
30 /* Declare message IDs */
31 #include <ioam/lib-vxlan-gpe/vxlan_gpe_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
36 #undef vl_typedefs
37
38 /* declare message handlers for each api */
39
40 #define vl_endianfun            /* define message structures */
41 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
46 #define vl_printfun
47 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
48 #undef vl_printfun
49
50 /* Get the API version number. */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
53 #undef vl_api_version
54 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam_packet.h>
55 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
56
57 typedef struct
58 {
59   /* API message ID base */
60   u16 msg_id_base;
61   vat_main_t *vat_main;
62 } vxlan_gpe_test_main_t;
63
64 vxlan_gpe_test_main_t vxlan_gpe_test_main;
65
66 #define foreach_standard_reply_retval_handler     \
67 _(vxlan_gpe_ioam_enable_reply)                    \
68 _(vxlan_gpe_ioam_disable_reply)                   \
69 _(vxlan_gpe_ioam_vni_enable_reply)                \
70 _(vxlan_gpe_ioam_vni_disable_reply)               \
71 _(vxlan_gpe_ioam_transit_enable_reply)            \
72 _(vxlan_gpe_ioam_transit_disable_reply)
73
74 #define _(n)                                            \
75     static void vl_api_##n##_t_handler                  \
76     (vl_api_##n##_t * mp)                               \
77     {                                                   \
78         vat_main_t * vam = vxlan_gpe_test_main.vat_main;   \
79         i32 retval = ntohl(mp->retval);                 \
80         if (vam->async_mode) {                          \
81             vam->async_errors += (retval < 0);          \
82         } else {                                        \
83             vam->retval = retval;                       \
84             vam->result_ready = 1;                      \
85         }                                               \
86     }
87 foreach_standard_reply_retval_handler;
88 #undef _
89
90 /*
91  * Table of message reply handlers, must include boilerplate handlers
92  * we just generated
93  */
94 #define foreach_vpe_api_reply_msg                                              \
95 _(VXLAN_GPE_IOAM_ENABLE_REPLY, vxlan_gpe_ioam_enable_reply)                    \
96 _(VXLAN_GPE_IOAM_DISABLE_REPLY, vxlan_gpe_ioam_disable_reply)                  \
97 _(VXLAN_GPE_IOAM_VNI_ENABLE_REPLY, vxlan_gpe_ioam_vni_enable_reply)            \
98 _(VXLAN_GPE_IOAM_VNI_DISABLE_REPLY, vxlan_gpe_ioam_vni_disable_reply)          \
99 _(VXLAN_GPE_IOAM_TRANSIT_ENABLE_REPLY, vxlan_gpe_ioam_transit_enable_reply)    \
100 _(VXLAN_GPE_IOAM_TRANSIT_DISABLE_REPLY, vxlan_gpe_ioam_transit_disable_reply)  \
101
102 static int
103 api_vxlan_gpe_ioam_enable (vat_main_t * vam)
104 {
105   unformat_input_t *input = vam->input;
106   vl_api_vxlan_gpe_ioam_enable_t *mp;
107   u32 id = 0;
108   int has_trace_option = 0;
109   int has_pow_option = 0;
110   int has_ppc_option = 0;
111
112   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
113     {
114       if (unformat (input, "trace"))
115         has_trace_option = 1;
116       else if (unformat (input, "pow"))
117         has_pow_option = 1;
118       else if (unformat (input, "ppc encap"))
119         has_ppc_option = PPC_ENCAP;
120       else if (unformat (input, "ppc decap"))
121         has_ppc_option = PPC_DECAP;
122       else if (unformat (input, "ppc none"))
123         has_ppc_option = PPC_NONE;
124       else
125         break;
126     }
127   M (VXLAN_GPE_IOAM_ENABLE, mp);
128   mp->id = htons (id);
129   mp->trace_ppc = has_ppc_option;
130   mp->pow_enable = has_pow_option;
131   mp->trace_enable = has_trace_option;
132
133
134   S (mp);
135   W;
136
137   return (0);
138 }
139
140
141 static int
142 api_vxlan_gpe_ioam_disable (vat_main_t * vam)
143 {
144   vl_api_vxlan_gpe_ioam_disable_t *mp;
145
146   M (VXLAN_GPE_IOAM_DISABLE, mp);
147   S (mp);
148   W;
149   return 0;
150 }
151
152 static int
153 api_vxlan_gpe_ioam_vni_enable (vat_main_t * vam)
154 {
155   unformat_input_t *line_input = vam->input;
156   vl_api_vxlan_gpe_ioam_vni_enable_t *mp;
157   ip4_address_t local4, remote4;
158   ip6_address_t local6, remote6;
159   u8 ipv4_set = 0, ipv6_set = 0;
160   u8 local_set = 0;
161   u8 remote_set = 0;
162   u32 vni;
163   u8 vni_set = 0;
164
165
166   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
167     {
168       if (unformat (line_input, "local %U", unformat_ip4_address, &local4))
169         {
170           local_set = 1;
171           ipv4_set = 1;
172         }
173       else if (unformat (line_input, "remote %U",
174                          unformat_ip4_address, &remote4))
175         {
176           remote_set = 1;
177           ipv4_set = 1;
178         }
179       else if (unformat (line_input, "local %U",
180                          unformat_ip6_address, &local6))
181         {
182           local_set = 1;
183           ipv6_set = 1;
184         }
185       else if (unformat (line_input, "remote %U",
186                          unformat_ip6_address, &remote6))
187         {
188           remote_set = 1;
189           ipv6_set = 1;
190         }
191
192       else if (unformat (line_input, "vni %d", &vni))
193         vni_set = 1;
194       else
195         {
196           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
197           return -99;
198         }
199     }
200
201   if (local_set == 0)
202     {
203       errmsg ("tunnel local address not specified\n");
204       return -99;
205     }
206   if (remote_set == 0)
207     {
208       errmsg ("tunnel remote address not specified\n");
209       return -99;
210     }
211   if (ipv4_set && ipv6_set)
212     {
213       errmsg ("both IPv4 and IPv6 addresses specified");
214       return -99;
215     }
216
217   if (vni_set == 0)
218     {
219       errmsg ("vni not specified\n");
220       return -99;
221     }
222
223   M (VXLAN_GPE_IOAM_VNI_ENABLE, mp);
224
225
226   if (ipv6_set)
227     {
228       clib_memcpy (&mp->local, &local6, sizeof (local6));
229       clib_memcpy (&mp->remote, &remote6, sizeof (remote6));
230     }
231   else
232     {
233       clib_memcpy (&mp->local, &local4, sizeof (local4));
234       clib_memcpy (&mp->remote, &remote4, sizeof (remote4));
235     }
236
237   mp->vni = ntohl (vni);
238   mp->is_ipv6 = ipv6_set;
239
240   S (mp);
241   W;
242
243   return (0);
244 }
245
246 static int
247 api_vxlan_gpe_ioam_vni_disable (vat_main_t * vam)
248 {
249   unformat_input_t *line_input = vam->input;
250   vl_api_vxlan_gpe_ioam_vni_disable_t *mp;
251   ip4_address_t local4, remote4;
252   ip6_address_t local6, remote6;
253   u8 ipv4_set = 0, ipv6_set = 0;
254   u8 local_set = 0;
255   u8 remote_set = 0;
256   u32 vni;
257   u8 vni_set = 0;
258
259
260   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
261     {
262       if (unformat (line_input, "local %U", unformat_ip4_address, &local4))
263         {
264           local_set = 1;
265           ipv4_set = 1;
266         }
267       else if (unformat (line_input, "remote %U",
268                          unformat_ip4_address, &remote4))
269         {
270           remote_set = 1;
271           ipv4_set = 1;
272         }
273       else if (unformat (line_input, "local %U",
274                          unformat_ip6_address, &local6))
275         {
276           local_set = 1;
277           ipv6_set = 1;
278         }
279       else if (unformat (line_input, "remote %U",
280                          unformat_ip6_address, &remote6))
281         {
282           remote_set = 1;
283           ipv6_set = 1;
284         }
285
286       else if (unformat (line_input, "vni %d", &vni))
287         vni_set = 1;
288       else
289         {
290           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
291           return -99;
292         }
293     }
294
295   if (local_set == 0)
296     {
297       errmsg ("tunnel local address not specified\n");
298       return -99;
299     }
300   if (remote_set == 0)
301     {
302       errmsg ("tunnel remote address not specified\n");
303       return -99;
304     }
305   if (ipv4_set && ipv6_set)
306     {
307       errmsg ("both IPv4 and IPv6 addresses specified");
308       return -99;
309     }
310
311   if (vni_set == 0)
312     {
313       errmsg ("vni not specified\n");
314       return -99;
315     }
316
317   M (VXLAN_GPE_IOAM_VNI_DISABLE, mp);
318
319
320   if (ipv6_set)
321     {
322       clib_memcpy (&mp->local, &local6, sizeof (local6));
323       clib_memcpy (&mp->remote, &remote6, sizeof (remote6));
324     }
325   else
326     {
327       clib_memcpy (&mp->local, &local4, sizeof (local4));
328       clib_memcpy (&mp->remote, &remote4, sizeof (remote4));
329     }
330
331   mp->vni = ntohl (vni);
332   mp->is_ipv6 = ipv6_set;
333
334   S (mp);
335   W;
336
337   return 0;
338 }
339
340 static int
341 api_vxlan_gpe_ioam_transit_enable (vat_main_t * vam)
342 {
343   unformat_input_t *line_input = vam->input;
344   vl_api_vxlan_gpe_ioam_transit_enable_t *mp;
345   ip4_address_t local4;
346   ip6_address_t local6;
347   u8 ipv4_set = 0, ipv6_set = 0;
348   u8 local_set = 0;
349   u32 outer_fib_index = 0;
350
351
352   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
353     {
354       if (unformat (line_input, "dst-ip %U", unformat_ip4_address, &local4))
355         {
356           local_set = 1;
357           ipv4_set = 1;
358         }
359       else if (unformat (line_input, "dst-ip %U",
360                          unformat_ip6_address, &local6))
361         {
362           local_set = 1;
363           ipv6_set = 1;
364         }
365
366       else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
367         ;
368       else
369         {
370           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
371           return -99;
372         }
373     }
374
375   if (local_set == 0)
376     {
377       errmsg ("destination address not specified\n");
378       return -99;
379     }
380   if (ipv4_set && ipv6_set)
381     {
382       errmsg ("both IPv4 and IPv6 addresses specified");
383       return -99;
384     }
385
386
387   M (VXLAN_GPE_IOAM_TRANSIT_ENABLE, mp);
388
389
390   if (ipv6_set)
391     {
392       errmsg ("IPv6 currently unsupported");
393       return -1;
394     }
395   else
396     {
397       clib_memcpy (&mp->dst_addr, &local4, sizeof (local4));
398     }
399
400   mp->outer_fib_index = htonl (outer_fib_index);
401   mp->is_ipv6 = ipv6_set;
402
403   S (mp);
404   W;
405
406   return (0);
407 }
408
409 static int
410 api_vxlan_gpe_ioam_transit_disable (vat_main_t * vam)
411 {
412   unformat_input_t *line_input = vam->input;
413   vl_api_vxlan_gpe_ioam_transit_disable_t *mp;
414   ip4_address_t local4;
415   ip6_address_t local6;
416   u8 ipv4_set = 0, ipv6_set = 0;
417   u8 local_set = 0;
418   u32 outer_fib_index = 0;
419
420
421   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
422     {
423       if (unformat (line_input, "dst-ip %U", unformat_ip4_address, &local4))
424         {
425           local_set = 1;
426           ipv4_set = 1;
427         }
428       else if (unformat (line_input, "dst-ip %U",
429                          unformat_ip6_address, &local6))
430         {
431           local_set = 1;
432           ipv6_set = 1;
433         }
434
435       else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
436         ;
437       else
438         {
439           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
440           return -99;
441         }
442     }
443
444   if (local_set == 0)
445     {
446       errmsg ("destination address not specified\n");
447       return -99;
448     }
449   if (ipv4_set && ipv6_set)
450     {
451       errmsg ("both IPv4 and IPv6 addresses specified");
452       return -99;
453     }
454
455
456   M (VXLAN_GPE_IOAM_TRANSIT_DISABLE, mp);
457
458
459   if (ipv6_set)
460     {
461       return -1;
462     }
463   else
464     {
465       clib_memcpy (&mp->dst_addr, &local4, sizeof (local4));
466     }
467
468   mp->outer_fib_index = htonl (outer_fib_index);
469   mp->is_ipv6 = ipv6_set;
470
471   S (mp);
472   W;
473
474
475   return (0);
476 }
477
478 /*
479  * List of messages that the api test plugin sends,
480  * and that the data plane plugin processes
481  */
482 #define foreach_vpe_api_msg \
483 _(vxlan_gpe_ioam_enable, ""\
484   "[trace] [pow] [ppc <encap|ppc decap>]") \
485 _(vxlan_gpe_ioam_disable, "")                    \
486 _(vxlan_gpe_ioam_vni_enable, ""\
487   "local <local_vtep_ip> remote <remote_vtep_ip> vni <vnid>") \
488 _(vxlan_gpe_ioam_vni_disable, ""\
489   "local <local_vtep_ip> remote <remote_vtep_ip> vni <vnid>") \
490 _(vxlan_gpe_ioam_transit_enable, ""\
491   "dst-ip <dst_ip> [outer-fib-index <outer_fib_index>]") \
492 _(vxlan_gpe_ioam_transit_disable, ""\
493   "dst-ip <dst_ip> [outer-fib-index <outer_fib_index>]") \
494
495
496 static void
497 vxlan_gpe_vat_api_hookup (vat_main_t * vam)
498 {
499   vxlan_gpe_test_main_t *sm = &vxlan_gpe_test_main;
500   /* Hook up handlers for replies from the data plane plug-in */
501 #define _(N,n)                                                  \
502     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
503                            #n,                                  \
504                            vl_api_##n##_t_handler,              \
505                            vl_noop_handler,                     \
506                            vl_api_##n##_t_endian,               \
507                            vl_api_##n##_t_print,                \
508                            sizeof(vl_api_##n##_t), 1);
509   foreach_vpe_api_reply_msg;
510 #undef _
511
512   /* API messages we can send */
513 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
514   foreach_vpe_api_msg;
515 #undef _
516
517   /* Help strings */
518 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
519   foreach_vpe_api_msg;
520 #undef _
521 }
522
523 clib_error_t *
524 vat_plugin_register (vat_main_t * vam)
525 {
526   vxlan_gpe_test_main_t *sm = &vxlan_gpe_test_main;
527   u8 *name;
528
529   sm->vat_main = vam;
530
531   name = format (0, "ioam_vxlan_gpe_%08x%c", api_version, 0);
532   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
533
534   if (sm->msg_id_base != (u16) ~ 0)
535     vxlan_gpe_vat_api_hookup (vam);
536
537   vec_free (name);
538
539   return 0;
540 }
541
542 /*
543  * fd.io coding-style-patch-verification: ON
544  *
545  * Local Variables:
546  * eval: (c-set-style "gnu")
547  * End:
548  */