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