Repair plugin binary API message numbering
[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   f64 timeout;
108   u32 id = 0;
109   int has_trace_option = 0;
110   int has_pow_option = 0;
111   int has_ppc_option = 0;
112
113   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
114     {
115       if (unformat (input, "trace"))
116         has_trace_option = 1;
117       else if (unformat (input, "pow"))
118         has_pow_option = 1;
119       else if (unformat (input, "ppc encap"))
120         has_ppc_option = PPC_ENCAP;
121       else if (unformat (input, "ppc decap"))
122         has_ppc_option = PPC_DECAP;
123       else if (unformat (input, "ppc none"))
124         has_ppc_option = PPC_NONE;
125       else
126         break;
127     }
128   M (VXLAN_GPE_IOAM_ENABLE, vxlan_gpe_ioam_enable);
129   mp->id = htons (id);
130   mp->trace_ppc = has_ppc_option;
131   mp->pow_enable = has_pow_option;
132   mp->trace_enable = has_trace_option;
133
134
135   S;
136   W;
137
138   return (0);
139 }
140
141
142 static int
143 api_vxlan_gpe_ioam_disable (vat_main_t * vam)
144 {
145   vl_api_vxlan_gpe_ioam_disable_t *mp;
146   f64 timeout;
147
148   M (VXLAN_GPE_IOAM_DISABLE, vxlan_gpe_ioam_disable);
149   S;
150   W;
151   return 0;
152 }
153
154 static int
155 api_vxlan_gpe_ioam_vni_enable (vat_main_t * vam)
156 {
157   unformat_input_t *line_input = vam->input;
158   vl_api_vxlan_gpe_ioam_vni_enable_t *mp;
159   ip4_address_t local4, remote4;
160   ip6_address_t local6, remote6;
161   u8 ipv4_set = 0, ipv6_set = 0;
162   u8 local_set = 0;
163   u8 remote_set = 0;
164   u32 vni;
165   u8 vni_set = 0;
166   f64 timeout;
167
168
169   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
170     {
171       if (unformat (line_input, "local %U", unformat_ip4_address, &local4))
172         {
173           local_set = 1;
174           ipv4_set = 1;
175         }
176       else if (unformat (line_input, "remote %U",
177                          unformat_ip4_address, &remote4))
178         {
179           remote_set = 1;
180           ipv4_set = 1;
181         }
182       else if (unformat (line_input, "local %U",
183                          unformat_ip6_address, &local6))
184         {
185           local_set = 1;
186           ipv6_set = 1;
187         }
188       else if (unformat (line_input, "remote %U",
189                          unformat_ip6_address, &remote6))
190         {
191           remote_set = 1;
192           ipv6_set = 1;
193         }
194
195       else if (unformat (line_input, "vni %d", &vni))
196         vni_set = 1;
197       else
198         {
199           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
200           return -99;
201         }
202     }
203
204   if (local_set == 0)
205     {
206       errmsg ("tunnel local address not specified\n");
207       return -99;
208     }
209   if (remote_set == 0)
210     {
211       errmsg ("tunnel remote address not specified\n");
212       return -99;
213     }
214   if (ipv4_set && ipv6_set)
215     {
216       errmsg ("both IPv4 and IPv6 addresses specified");
217       return -99;
218     }
219
220   if (vni_set == 0)
221     {
222       errmsg ("vni not specified\n");
223       return -99;
224     }
225
226   M (VXLAN_GPE_IOAM_VNI_ENABLE, vxlan_gpe_ioam_vni_enable);
227
228
229   if (ipv6_set)
230     {
231       clib_memcpy (&mp->local, &local6, sizeof (local6));
232       clib_memcpy (&mp->remote, &remote6, sizeof (remote6));
233     }
234   else
235     {
236       clib_memcpy (&mp->local, &local4, sizeof (local4));
237       clib_memcpy (&mp->remote, &remote4, sizeof (remote4));
238     }
239
240   mp->vni = ntohl (vni);
241   mp->is_ipv6 = ipv6_set;
242
243   S;
244   W;
245
246   return (0);
247 }
248
249 static int
250 api_vxlan_gpe_ioam_vni_disable (vat_main_t * vam)
251 {
252   unformat_input_t *line_input = vam->input;
253   vl_api_vxlan_gpe_ioam_vni_disable_t *mp;
254   ip4_address_t local4, remote4;
255   ip6_address_t local6, remote6;
256   u8 ipv4_set = 0, ipv6_set = 0;
257   u8 local_set = 0;
258   u8 remote_set = 0;
259   u32 vni;
260   u8 vni_set = 0;
261   f64 timeout;
262
263
264   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
265     {
266       if (unformat (line_input, "local %U", unformat_ip4_address, &local4))
267         {
268           local_set = 1;
269           ipv4_set = 1;
270         }
271       else if (unformat (line_input, "remote %U",
272                          unformat_ip4_address, &remote4))
273         {
274           remote_set = 1;
275           ipv4_set = 1;
276         }
277       else if (unformat (line_input, "local %U",
278                          unformat_ip6_address, &local6))
279         {
280           local_set = 1;
281           ipv6_set = 1;
282         }
283       else if (unformat (line_input, "remote %U",
284                          unformat_ip6_address, &remote6))
285         {
286           remote_set = 1;
287           ipv6_set = 1;
288         }
289
290       else if (unformat (line_input, "vni %d", &vni))
291         vni_set = 1;
292       else
293         {
294           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
295           return -99;
296         }
297     }
298
299   if (local_set == 0)
300     {
301       errmsg ("tunnel local address not specified\n");
302       return -99;
303     }
304   if (remote_set == 0)
305     {
306       errmsg ("tunnel remote address not specified\n");
307       return -99;
308     }
309   if (ipv4_set && ipv6_set)
310     {
311       errmsg ("both IPv4 and IPv6 addresses specified");
312       return -99;
313     }
314
315   if (vni_set == 0)
316     {
317       errmsg ("vni not specified\n");
318       return -99;
319     }
320
321   M (VXLAN_GPE_IOAM_VNI_DISABLE, vxlan_gpe_ioam_vni_disable);
322
323
324   if (ipv6_set)
325     {
326       clib_memcpy (&mp->local, &local6, sizeof (local6));
327       clib_memcpy (&mp->remote, &remote6, sizeof (remote6));
328     }
329   else
330     {
331       clib_memcpy (&mp->local, &local4, sizeof (local4));
332       clib_memcpy (&mp->remote, &remote4, sizeof (remote4));
333     }
334
335   mp->vni = ntohl (vni);
336   mp->is_ipv6 = ipv6_set;
337
338   S;
339   W;
340
341   return 0;
342 }
343
344 static int
345 api_vxlan_gpe_ioam_transit_enable (vat_main_t * vam)
346 {
347   unformat_input_t *line_input = vam->input;
348   vl_api_vxlan_gpe_ioam_transit_enable_t *mp;
349   ip4_address_t local4;
350   ip6_address_t local6;
351   u8 ipv4_set = 0, ipv6_set = 0;
352   u8 local_set = 0;
353   u32 outer_fib_index = 0;
354   f64 timeout;
355
356
357   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
358     {
359       if (unformat (line_input, "dst-ip %U", unformat_ip4_address, &local4))
360         {
361           local_set = 1;
362           ipv4_set = 1;
363         }
364       else if (unformat (line_input, "dst-ip %U",
365                          unformat_ip6_address, &local6))
366         {
367           local_set = 1;
368           ipv6_set = 1;
369         }
370
371       else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
372         ;
373       else
374         {
375           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
376           return -99;
377         }
378     }
379
380   if (local_set == 0)
381     {
382       errmsg ("destination address not specified\n");
383       return -99;
384     }
385   if (ipv4_set && ipv6_set)
386     {
387       errmsg ("both IPv4 and IPv6 addresses specified");
388       return -99;
389     }
390
391
392   M (VXLAN_GPE_IOAM_TRANSIT_ENABLE, vxlan_gpe_ioam_transit_enable);
393
394
395   if (ipv6_set)
396     {
397       errmsg ("IPv6 currently unsupported");
398       return -1;
399     }
400   else
401     {
402       clib_memcpy (&mp->dst_addr, &local4, sizeof (local4));
403     }
404
405   mp->outer_fib_index = htonl (outer_fib_index);
406   mp->is_ipv6 = ipv6_set;
407
408   S;
409   W;
410
411   return (0);
412 }
413
414 static int
415 api_vxlan_gpe_ioam_transit_disable (vat_main_t * vam)
416 {
417   unformat_input_t *line_input = vam->input;
418   vl_api_vxlan_gpe_ioam_transit_disable_t *mp;
419   ip4_address_t local4;
420   ip6_address_t local6;
421   u8 ipv4_set = 0, ipv6_set = 0;
422   u8 local_set = 0;
423   u32 outer_fib_index = 0;
424   f64 timeout;
425
426
427   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
428     {
429       if (unformat (line_input, "dst-ip %U", unformat_ip4_address, &local4))
430         {
431           local_set = 1;
432           ipv4_set = 1;
433         }
434       else if (unformat (line_input, "dst-ip %U",
435                          unformat_ip6_address, &local6))
436         {
437           local_set = 1;
438           ipv6_set = 1;
439         }
440
441       else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
442         ;
443       else
444         {
445           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
446           return -99;
447         }
448     }
449
450   if (local_set == 0)
451     {
452       errmsg ("destination address not specified\n");
453       return -99;
454     }
455   if (ipv4_set && ipv6_set)
456     {
457       errmsg ("both IPv4 and IPv6 addresses specified");
458       return -99;
459     }
460
461
462   M (VXLAN_GPE_IOAM_TRANSIT_DISABLE, vxlan_gpe_ioam_transit_disable);
463
464
465   if (ipv6_set)
466     {
467       return -1;
468     }
469   else
470     {
471       clib_memcpy (&mp->dst_addr, &local4, sizeof (local4));
472     }
473
474   mp->outer_fib_index = htonl (outer_fib_index);
475   mp->is_ipv6 = ipv6_set;
476
477   S;
478   W;
479
480
481   return (0);
482 }
483
484 /*
485  * List of messages that the api test plugin sends,
486  * and that the data plane plugin processes
487  */
488 #define foreach_vpe_api_msg \
489 _(vxlan_gpe_ioam_enable, ""\
490   "[trace] [pow] [ppc <encap|ppc decap>]") \
491 _(vxlan_gpe_ioam_disable, "")                    \
492 _(vxlan_gpe_ioam_vni_enable, ""\
493   "local <local_vtep_ip> remote <remote_vtep_ip> vni <vnid>") \
494 _(vxlan_gpe_ioam_vni_disable, ""\
495   "local <local_vtep_ip> remote <remote_vtep_ip> vni <vnid>") \
496 _(vxlan_gpe_ioam_transit_enable, ""\
497   "dst-ip <dst_ip> [outer-fib-index <outer_fib_index>]") \
498 _(vxlan_gpe_ioam_transit_disable, ""\
499   "dst-ip <dst_ip> [outer-fib-index <outer_fib_index>]") \
500
501
502 static void
503 vxlan_gpe_vat_api_hookup (vat_main_t * vam)
504 {
505   vxlan_gpe_test_main_t *sm = &vxlan_gpe_test_main;
506   /* Hook up handlers for replies from the data plane plug-in */
507 #define _(N,n)                                                  \
508     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
509                            #n,                                  \
510                            vl_api_##n##_t_handler,              \
511                            vl_noop_handler,                     \
512                            vl_api_##n##_t_endian,               \
513                            vl_api_##n##_t_print,                \
514                            sizeof(vl_api_##n##_t), 1);
515   foreach_vpe_api_reply_msg;
516 #undef _
517
518   /* API messages we can send */
519 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
520   foreach_vpe_api_msg;
521 #undef _
522
523   /* Help strings */
524 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
525   foreach_vpe_api_msg;
526 #undef _
527 }
528
529 clib_error_t *
530 vat_plugin_register (vat_main_t * vam)
531 {
532   vxlan_gpe_test_main_t *sm = &vxlan_gpe_test_main;
533   u8 *name;
534
535   sm->vat_main = vam;
536
537   name = format (0, "ioam_vxlan_gpe_%08x%c", api_version, 0);
538   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
539
540   if (sm->msg_id_base != (u16) ~ 0)
541     vxlan_gpe_vat_api_hookup (vam);
542
543   vec_free (name);
544
545   return 0;
546 }
547
548 /*
549  * fd.io coding-style-patch-verification: ON
550  *
551  * Local Variables:
552  * eval: (c-set-style "gnu")
553  * End:
554  */