Repair vlib API socket server
[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
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   int ret;
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, mp);
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 (mp);
136   W (ret);
137   return ret;
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   int ret;
146
147   M (VXLAN_GPE_IOAM_DISABLE, mp);
148   S (mp);
149   W (ret);
150   return ret;
151 }
152
153 static int
154 api_vxlan_gpe_ioam_vni_enable (vat_main_t * vam)
155 {
156   unformat_input_t *line_input = vam->input;
157   vl_api_vxlan_gpe_ioam_vni_enable_t *mp;
158   ip4_address_t local4, remote4;
159   ip6_address_t local6, remote6;
160   u8 ipv4_set = 0, ipv6_set = 0;
161   u8 local_set = 0;
162   u8 remote_set = 0;
163   u32 vni;
164   u8 vni_set = 0;
165   int ret;
166
167
168   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
169     {
170       if (unformat (line_input, "local %U", unformat_ip4_address, &local4))
171         {
172           local_set = 1;
173           ipv4_set = 1;
174         }
175       else if (unformat (line_input, "remote %U",
176                          unformat_ip4_address, &remote4))
177         {
178           remote_set = 1;
179           ipv4_set = 1;
180         }
181       else if (unformat (line_input, "local %U",
182                          unformat_ip6_address, &local6))
183         {
184           local_set = 1;
185           ipv6_set = 1;
186         }
187       else if (unformat (line_input, "remote %U",
188                          unformat_ip6_address, &remote6))
189         {
190           remote_set = 1;
191           ipv6_set = 1;
192         }
193
194       else if (unformat (line_input, "vni %d", &vni))
195         vni_set = 1;
196       else
197         {
198           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
199           return -99;
200         }
201     }
202
203   if (local_set == 0)
204     {
205       errmsg ("tunnel local address not specified\n");
206       return -99;
207     }
208   if (remote_set == 0)
209     {
210       errmsg ("tunnel remote address not specified\n");
211       return -99;
212     }
213   if (ipv4_set && ipv6_set)
214     {
215       errmsg ("both IPv4 and IPv6 addresses specified");
216       return -99;
217     }
218
219   if (vni_set == 0)
220     {
221       errmsg ("vni not specified\n");
222       return -99;
223     }
224
225   M (VXLAN_GPE_IOAM_VNI_ENABLE, mp);
226
227
228   if (ipv6_set)
229     {
230       clib_memcpy (&mp->local, &local6, sizeof (local6));
231       clib_memcpy (&mp->remote, &remote6, sizeof (remote6));
232     }
233   else
234     {
235       clib_memcpy (&mp->local, &local4, sizeof (local4));
236       clib_memcpy (&mp->remote, &remote4, sizeof (remote4));
237     }
238
239   mp->vni = ntohl (vni);
240   mp->is_ipv6 = ipv6_set;
241
242   S (mp);
243   W (ret);
244   return ret;
245 }
246
247 static int
248 api_vxlan_gpe_ioam_vni_disable (vat_main_t * vam)
249 {
250   unformat_input_t *line_input = vam->input;
251   vl_api_vxlan_gpe_ioam_vni_disable_t *mp;
252   ip4_address_t local4, remote4;
253   ip6_address_t local6, remote6;
254   u8 ipv4_set = 0, ipv6_set = 0;
255   u8 local_set = 0;
256   u8 remote_set = 0;
257   u32 vni;
258   u8 vni_set = 0;
259   int ret;
260
261
262   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
263     {
264       if (unformat (line_input, "local %U", unformat_ip4_address, &local4))
265         {
266           local_set = 1;
267           ipv4_set = 1;
268         }
269       else if (unformat (line_input, "remote %U",
270                          unformat_ip4_address, &remote4))
271         {
272           remote_set = 1;
273           ipv4_set = 1;
274         }
275       else if (unformat (line_input, "local %U",
276                          unformat_ip6_address, &local6))
277         {
278           local_set = 1;
279           ipv6_set = 1;
280         }
281       else if (unformat (line_input, "remote %U",
282                          unformat_ip6_address, &remote6))
283         {
284           remote_set = 1;
285           ipv6_set = 1;
286         }
287
288       else if (unformat (line_input, "vni %d", &vni))
289         vni_set = 1;
290       else
291         {
292           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
293           return -99;
294         }
295     }
296
297   if (local_set == 0)
298     {
299       errmsg ("tunnel local address not specified\n");
300       return -99;
301     }
302   if (remote_set == 0)
303     {
304       errmsg ("tunnel remote address not specified\n");
305       return -99;
306     }
307   if (ipv4_set && ipv6_set)
308     {
309       errmsg ("both IPv4 and IPv6 addresses specified");
310       return -99;
311     }
312
313   if (vni_set == 0)
314     {
315       errmsg ("vni not specified\n");
316       return -99;
317     }
318
319   M (VXLAN_GPE_IOAM_VNI_DISABLE, mp);
320
321
322   if (ipv6_set)
323     {
324       clib_memcpy (&mp->local, &local6, sizeof (local6));
325       clib_memcpy (&mp->remote, &remote6, sizeof (remote6));
326     }
327   else
328     {
329       clib_memcpy (&mp->local, &local4, sizeof (local4));
330       clib_memcpy (&mp->remote, &remote4, sizeof (remote4));
331     }
332
333   mp->vni = ntohl (vni);
334   mp->is_ipv6 = ipv6_set;
335
336   S (mp);
337   W (ret);
338   return ret;
339 }
340
341 static int
342 api_vxlan_gpe_ioam_transit_enable (vat_main_t * vam)
343 {
344   unformat_input_t *line_input = vam->input;
345   vl_api_vxlan_gpe_ioam_transit_enable_t *mp;
346   ip4_address_t local4;
347   ip6_address_t local6;
348   u8 ipv4_set = 0, ipv6_set = 0;
349   u8 local_set = 0;
350   u32 outer_fib_index = 0;
351   int ret;
352
353
354   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
355     {
356       if (unformat (line_input, "dst-ip %U", unformat_ip4_address, &local4))
357         {
358           local_set = 1;
359           ipv4_set = 1;
360         }
361       else if (unformat (line_input, "dst-ip %U",
362                          unformat_ip6_address, &local6))
363         {
364           local_set = 1;
365           ipv6_set = 1;
366         }
367
368       else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
369         ;
370       else
371         {
372           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
373           return -99;
374         }
375     }
376
377   if (local_set == 0)
378     {
379       errmsg ("destination address not specified\n");
380       return -99;
381     }
382   if (ipv4_set && ipv6_set)
383     {
384       errmsg ("both IPv4 and IPv6 addresses specified");
385       return -99;
386     }
387
388
389   M (VXLAN_GPE_IOAM_TRANSIT_ENABLE, mp);
390
391
392   if (ipv6_set)
393     {
394       errmsg ("IPv6 currently unsupported");
395       return -1;
396     }
397   else
398     {
399       clib_memcpy (&mp->dst_addr, &local4, sizeof (local4));
400     }
401
402   mp->outer_fib_index = htonl (outer_fib_index);
403   mp->is_ipv6 = ipv6_set;
404
405   S (mp);
406   W (ret);
407   return ret;
408 }
409
410 static int
411 api_vxlan_gpe_ioam_transit_disable (vat_main_t * vam)
412 {
413   unformat_input_t *line_input = vam->input;
414   vl_api_vxlan_gpe_ioam_transit_disable_t *mp;
415   ip4_address_t local4;
416   ip6_address_t local6;
417   u8 ipv4_set = 0, ipv6_set = 0;
418   u8 local_set = 0;
419   u32 outer_fib_index = 0;
420   int ret;
421
422
423   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
424     {
425       if (unformat (line_input, "dst-ip %U", unformat_ip4_address, &local4))
426         {
427           local_set = 1;
428           ipv4_set = 1;
429         }
430       else if (unformat (line_input, "dst-ip %U",
431                          unformat_ip6_address, &local6))
432         {
433           local_set = 1;
434           ipv6_set = 1;
435         }
436
437       else if (unformat (line_input, "outer-fib-index %d", &outer_fib_index))
438         ;
439       else
440         {
441           errmsg ("parse error '%U'\n", format_unformat_error, line_input);
442           return -99;
443         }
444     }
445
446   if (local_set == 0)
447     {
448       errmsg ("destination address not specified\n");
449       return -99;
450     }
451   if (ipv4_set && ipv6_set)
452     {
453       errmsg ("both IPv4 and IPv6 addresses specified");
454       return -99;
455     }
456
457
458   M (VXLAN_GPE_IOAM_TRANSIT_DISABLE, mp);
459
460
461   if (ipv6_set)
462     {
463       return -1;
464     }
465   else
466     {
467       clib_memcpy (&mp->dst_addr, &local4, sizeof (local4));
468     }
469
470   mp->outer_fib_index = htonl (outer_fib_index);
471   mp->is_ipv6 = ipv6_set;
472
473   S (mp);
474   W (ret);
475   return ret;
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  */