Repair vlib API socket server
[vpp.git] / src / plugins / ioam / export / 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  * 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
30 /* Declare message IDs */
31 #include <ioam/export/ioam_export_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <ioam/export/ioam_export_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/export/ioam_export_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/export/ioam_export_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/export/ioam_export_all_api_h.h>
53 #undef vl_api_version
54
55
56 typedef struct
57 {
58   /* API message ID base */
59   u16 msg_id_base;
60   vat_main_t *vat_main;
61 } export_test_main_t;
62
63 export_test_main_t export_test_main;
64
65 #define foreach_standard_reply_retval_handler   \
66 _(ioam_export_ip6_enable_disable_reply)
67
68 #define _(n)                                            \
69     static void vl_api_##n##_t_handler                  \
70     (vl_api_##n##_t * mp)                               \
71     {                                                   \
72         vat_main_t * vam = export_test_main.vat_main;   \
73         i32 retval = ntohl(mp->retval);                 \
74         if (vam->async_mode) {                          \
75             vam->async_errors += (retval < 0);          \
76         } else {                                        \
77             vam->retval = retval;                       \
78             vam->result_ready = 1;                      \
79         }                                               \
80     }
81 foreach_standard_reply_retval_handler;
82 #undef _
83
84 /*
85  * Table of message reply handlers, must include boilerplate handlers
86  * we just generated
87  */
88 #define foreach_vpe_api_reply_msg                                       \
89 _(IOAM_EXPORT_IP6_ENABLE_DISABLE_REPLY, ioam_export_ip6_enable_disable_reply)
90
91
92 static int
93 api_ioam_export_ip6_enable_disable (vat_main_t * vam)
94 {
95   unformat_input_t *i = vam->input;
96   int is_disable = 0;
97   vl_api_ioam_export_ip6_enable_disable_t *mp;
98   int ret;
99
100   /* Parse args required to build the message */
101   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
102     {
103       if (unformat (i, "disable"))
104         is_disable = 1;
105       else
106         break;
107     }
108
109   /* Construct the API message */
110   M(IOAM_EXPORT_IP6_ENABLE_DISABLE, mp);
111   mp->is_disable = is_disable;
112
113   /* send it... */
114   S(mp);
115
116   /* Wait for a reply... */
117   W (ret);
118   return ret;
119 }
120
121 /*
122  * List of messages that the api test plugin sends,
123  * and that the data plane plugin processes
124  */
125 #define foreach_vpe_api_msg \
126 _(ioam_export_ip6_enable_disable, "<intfc> [disable]")
127
128 static void
129 ioam_export_vat_api_hookup (vat_main_t * vam)
130 {
131   export_test_main_t *sm = &export_test_main;
132   /* Hook up handlers for replies from the data plane plug-in */
133 #define _(N,n)                                                  \
134     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
135                            #n,                                  \
136                            vl_api_##n##_t_handler,              \
137                            vl_noop_handler,                     \
138                            vl_api_##n##_t_endian,               \
139                            vl_api_##n##_t_print,                \
140                            sizeof(vl_api_##n##_t), 1);
141   foreach_vpe_api_reply_msg;
142 #undef _
143
144   /* API messages we can send */
145 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
146   foreach_vpe_api_msg;
147 #undef _
148
149   /* Help strings */
150 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
151   foreach_vpe_api_msg;
152 #undef _
153 }
154
155 clib_error_t *
156 vat_plugin_register (vat_main_t * vam)
157 {
158   export_test_main_t *sm = &export_test_main;
159   u8 *name;
160
161   sm->vat_main = vam;
162
163   name = format (0, "ioam_export_%08x%c", api_version, 0);
164   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
165
166   if (sm->msg_id_base != (u16) ~ 0)
167     ioam_export_vat_api_hookup (vam);
168
169   vec_free (name);
170
171   return 0;
172 }