Repair vlib API socket server
[vpp.git] / src / plugins / ioam / export-vxlan-gpe / vxlan_gpe_ioam_export_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_ioam_export_test.c - test harness plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vppinfra/error.h>
25
26 #define __plugin_msg_base export_test_main.msg_id_base
27 #include <vlibapi/vat_helper_macros.h>
28
29 /* Declare message IDs */
30 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_msg_enum.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
35 #undef vl_typedefs
36
37 /* declare message handlers for each api */
38
39 #define vl_endianfun            /* define message structures */
40 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...)
45 #define vl_printfun
46 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
47 #undef vl_printfun
48
49 /* Get the API version number. */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
52 #undef vl_api_version
53
54
55 typedef struct
56 {
57   /* API message ID base */
58   u16 msg_id_base;
59   vat_main_t *vat_main;
60 } export_test_main_t;
61
62 export_test_main_t export_test_main;
63
64 #define foreach_standard_reply_retval_handler   \
65 _(vxlan_gpe_ioam_export_enable_disable_reply)
66
67 #define _(n)                                            \
68     static void vl_api_##n##_t_handler                  \
69     (vl_api_##n##_t * mp)                               \
70     {                                                   \
71         vat_main_t * vam = export_test_main.vat_main;   \
72         i32 retval = ntohl(mp->retval);                 \
73         if (vam->async_mode) {                          \
74             vam->async_errors += (retval < 0);          \
75         } else {                                        \
76             vam->retval = retval;                       \
77             vam->result_ready = 1;                      \
78         }                                               \
79     }
80 foreach_standard_reply_retval_handler;
81 #undef _
82
83 /*
84  * Table of message reply handlers, must include boilerplate handlers
85  * we just generated
86  */
87 #define foreach_vpe_api_reply_msg                                       \
88 _(VXLAN_GPE_IOAM_EXPORT_ENABLE_DISABLE_REPLY, vxlan_gpe_ioam_export_enable_disable_reply)
89
90 static int
91 api_vxlan_gpe_ioam_export_enable_disable (vat_main_t * vam)
92 {
93   unformat_input_t *i = vam->input;
94   int is_disable = 0;
95   vl_api_vxlan_gpe_ioam_export_enable_disable_t *mp;
96   int ret;
97
98   /* Parse args required to build the message */
99   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
100     {
101       if (unformat (i, "disable"))
102         is_disable = 1;
103       else
104         break;
105     }
106
107   /* Construct the API message */
108   M (VXLAN_GPE_IOAM_EXPORT_ENABLE_DISABLE, mp);
109   mp->is_disable = is_disable;
110
111   /* send it... */
112   S (mp);
113
114   /* Wait for a reply... */
115   W (ret);
116   return ret;
117 }
118
119 /*
120  * List of messages that the api test plugin sends,
121  * and that the data plane plugin processes
122  */
123 #define foreach_vpe_api_msg \
124 _(vxlan_gpe_ioam_export_enable_disable, "<intfc> [disable]")
125
126 static void
127 vxlan_gpe_ioam_vat_api_hookup (vat_main_t * vam)
128 {
129   export_test_main_t *sm = &export_test_main;
130   /* Hook up handlers for replies from the data plane plug-in */
131 #define _(N,n)                                                  \
132     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
133                            #n,                                  \
134                            vl_api_##n##_t_handler,              \
135                            vl_noop_handler,                     \
136                            vl_api_##n##_t_endian,               \
137                            vl_api_##n##_t_print,                \
138                            sizeof(vl_api_##n##_t), 1);
139   foreach_vpe_api_reply_msg;
140 #undef _
141
142   /* API messages we can send */
143 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
144   foreach_vpe_api_msg;
145 #undef _
146
147   /* Help strings */
148 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
149   foreach_vpe_api_msg;
150 #undef _
151 }
152
153 clib_error_t *
154 vat_plugin_register (vat_main_t * vam)
155 {
156   export_test_main_t *sm = &export_test_main;
157   u8 *name;
158
159   sm->vat_main = vam;
160
161   name = format (0, "vxlan_gpe_ioam_export_%08x%c", api_version, 0);
162   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
163
164   if (sm->msg_id_base != (u16) ~ 0)
165     vxlan_gpe_ioam_vat_api_hookup (vam);
166
167   vec_free (name);
168
169   return 0;
170 }
171
172 /*
173  * fd.io coding-style-patch-verification: ON
174  *
175  * Local Variables:
176  * eval: (c-set-style "gnu")
177  * End:
178  */