Repair plugin binary API message numbering
[vpp.git] / src / plugins / flowperpkt / flowperpkt_test.c
1 /*
2  * flowperpkt.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vlibsocket/api.h>
21 #include <vppinfra/error.h>
22
23 #define __plugin_msg_base flowperpkt_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
25
26 /**
27  * @file vpp_api_test plugin
28  */
29
30 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
31
32 /* Declare message IDs */
33 #include <flowperpkt/flowperpkt_msg_enum.h>
34
35 /* define message structures */
36 #define vl_typedefs
37 #include <flowperpkt/flowperpkt_all_api_h.h>
38 #undef vl_typedefs
39
40 /* declare message handlers for each api */
41
42 #define vl_endianfun            /* define message structures */
43 #include <flowperpkt/flowperpkt_all_api_h.h>
44 #undef vl_endianfun
45
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...)
48 #define vl_printfun
49 #include <flowperpkt/flowperpkt_all_api_h.h>
50 #undef vl_printfun
51
52 /* Get the API version number. */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <flowperpkt/flowperpkt_all_api_h.h>
55 #undef vl_api_version
56
57 typedef struct
58 {
59     /** API message ID base */
60   u16 msg_id_base;
61     /** vat_main_t pointer */
62   vat_main_t *vat_main;
63 } flowperpkt_test_main_t;
64
65 flowperpkt_test_main_t flowperpkt_test_main;
66
67 #define foreach_standard_reply_retval_handler   \
68 _(flowperpkt_tx_interface_add_del_reply)
69
70 #define _(n)                                            \
71     static void vl_api_##n##_t_handler                  \
72     (vl_api_##n##_t * mp)                               \
73     {                                                   \
74         vat_main_t * vam = flowperpkt_test_main.vat_main;   \
75         i32 retval = ntohl(mp->retval);                 \
76         if (vam->async_mode) {                          \
77             vam->async_errors += (retval < 0);          \
78         } else {                                        \
79             vam->retval = retval;                       \
80             vam->result_ready = 1;                      \
81         }                                               \
82     }
83 foreach_standard_reply_retval_handler;
84 #undef _
85
86 /*
87  * Table of message reply handlers, must include boilerplate handlers
88  * we just generated
89  */
90 #define foreach_vpe_api_reply_msg               \
91 _(FLOWPERPKT_TX_INTERFACE_ADD_DEL_REPLY,        \
92   flowperpkt_tx_interface_add_del_reply)
93
94 static int
95 api_flowperpkt_tx_interface_add_del (vat_main_t * vam)
96 {
97   unformat_input_t *i = vam->input;
98   f64 timeout;
99   int enable_disable = 1;
100   u8 which = 0;                 /* ipv4 by default */
101   u32 sw_if_index = ~0;
102   vl_api_flowperpkt_tx_interface_add_del_t *mp;
103
104   /* Parse args required to build the message */
105   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
106     {
107       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
108         ;
109       else if (unformat (i, "sw_if_index %d", &sw_if_index))
110         ;
111       else if (unformat (i, "disable"))
112         enable_disable = 0;
113       else if (unformat (i, "l2"))
114         which = 1;
115       else
116         break;
117     }
118
119   if (sw_if_index == ~0)
120     {
121       errmsg ("missing interface name / explicit sw_if_index number \n");
122       return -99;
123     }
124
125   /* Construct the API message */
126   M (FLOWPERPKT_TX_INTERFACE_ADD_DEL, flowperpkt_tx_interface_add_del);
127   mp->sw_if_index = ntohl (sw_if_index);
128   mp->is_add = enable_disable;
129   mp->which = which;
130
131   /* send it... */
132   S;
133
134   /* Wait for a reply... */
135   W;
136 }
137
138 /*
139  * List of messages that the api test plugin sends,
140  * and that the data plane plugin processes
141  */
142 #define foreach_vpe_api_msg \
143 _(flowperpkt_tx_interface_add_del, "<intfc> [disable]")
144
145 static void
146 flowperpkt_vat_api_hookup (vat_main_t * vam)
147 {
148   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
149   /* Hook up handlers for replies from the data plane plug-in */
150 #define _(N,n)                                                  \
151     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
152                            #n,                                  \
153                            vl_api_##n##_t_handler,              \
154                            vl_noop_handler,                     \
155                            vl_api_##n##_t_endian,               \
156                            vl_api_##n##_t_print,                \
157                            sizeof(vl_api_##n##_t), 1);
158   foreach_vpe_api_reply_msg;
159 #undef _
160
161   /* API messages we can send */
162 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
163   foreach_vpe_api_msg;
164 #undef _
165
166   /* Help strings */
167 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
168   foreach_vpe_api_msg;
169 #undef _
170 }
171
172 clib_error_t *
173 vat_plugin_register (vat_main_t * vam)
174 {
175   flowperpkt_test_main_t *sm = &flowperpkt_test_main;
176   u8 *name;
177
178   sm->vat_main = vam;
179
180   /* Ask the vpp engine for the first assigned message-id */
181   name = format (0, "flowperpkt_%08x%c", api_version, 0);
182   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
183
184   /* Don't attempt to hook up API messages if the data plane plugin is AWOL */
185   if (sm->msg_id_base != (u16) ~ 0)
186     flowperpkt_vat_api_hookup (vam);
187
188   vec_free (name);
189
190   return 0;
191 }
192
193 /*
194  * fd.io coding-style-patch-verification: ON
195  *
196  * Local Variables:
197  * eval: (c-set-style "gnu")
198  * End:
199  */