91793f552c97fc0ad7b399e94a5201a553667d43
[vpp.git] / src / plugins / flowprobe / flowprobe_test.c
1 /*
2  * flowprobe.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 #include <flowprobe/flowprobe.h>
23
24 #define __plugin_msg_base flowprobe_test_main.msg_id_base
25 #include <vlibapi/vat_helper_macros.h>
26
27 /**
28  * @file vpp_api_test plugin
29  */
30
31 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
32
33 /* Declare message IDs */
34 #include <flowprobe/flowprobe_msg_enum.h>
35
36 /* define message structures */
37 #define vl_typedefs
38 #include <flowprobe/flowprobe_all_api_h.h>
39 #undef vl_typedefs
40
41 /* declare message handlers for each api */
42
43 #define vl_endianfun            /* define message structures */
44 #include <flowprobe/flowprobe_all_api_h.h>
45 #undef vl_endianfun
46
47 /* instantiate all the print functions we know about */
48 #define vl_print(handle, ...)
49 #define vl_printfun
50 #include <flowprobe/flowprobe_all_api_h.h>
51 #undef vl_printfun
52
53 /* Get the API version number. */
54 #define vl_api_version(n,v) static u32 api_version=(v);
55 #include <flowprobe/flowprobe_all_api_h.h>
56 #undef vl_api_version
57
58 typedef struct
59 {
60     /** API message ID base */
61   u16 msg_id_base;
62     /** vat_main_t pointer */
63   vat_main_t *vat_main;
64 } flowprobe_test_main_t;
65
66 flowprobe_test_main_t flowprobe_test_main;
67
68 #define foreach_standard_reply_retval_handler   \
69 _(flowprobe_tx_interface_add_del_reply)        \
70 _(flowprobe_params_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 = flowprobe_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 _(FLOWPROBE_TX_INTERFACE_ADD_DEL_REPLY,        \
94   flowprobe_tx_interface_add_del_reply)        \
95 _(FLOWPROBE_PARAMS_REPLY, flowprobe_params_reply)
96
97 static int
98 api_flowprobe_tx_interface_add_del (vat_main_t * vam)
99 {
100   unformat_input_t *i = vam->input;
101   int enable_disable = 1;
102   u8 which = FLOW_VARIANT_IP4;
103   u32 sw_if_index = ~0;
104   vl_api_flowprobe_tx_interface_add_del_t *mp;
105   int ret;
106
107   /* Parse args required to build the message */
108   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
109     {
110       if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
111         ;
112       else if (unformat (i, "sw_if_index %d", &sw_if_index))
113         ;
114       else if (unformat (i, "disable"))
115         enable_disable = 0;
116       else if (unformat (i, "ip4"))
117         which = FLOW_VARIANT_IP4;
118       else if (unformat (i, "ip6"))
119         which = FLOW_VARIANT_IP6;
120       else if (unformat (i, "l2"))
121         which = FLOW_VARIANT_L2;
122       else
123         break;
124     }
125
126   if (sw_if_index == ~0)
127     {
128       errmsg ("missing interface name / explicit sw_if_index number \n");
129       return -99;
130     }
131
132   /* Construct the API message */
133   M (FLOWPROBE_TX_INTERFACE_ADD_DEL, mp);
134   mp->sw_if_index = ntohl (sw_if_index);
135   mp->is_add = enable_disable;
136   mp->which = which;
137
138   /* send it... */
139   S (mp);
140
141   /* Wait for a reply... */
142   W (ret);
143   return ret;
144 }
145
146 static int
147 api_flowprobe_params (vat_main_t * vam)
148 {
149   unformat_input_t *i = vam->input;
150   u8 record_l2 = 0, record_l3 = 0, record_l4 = 0;
151   u32 active_timer = ~0;
152   u32 passive_timer = ~0;
153   vl_api_flowprobe_params_t *mp;
154   int ret;
155
156   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
157     {
158       if (unformat (i, "active %d", &active_timer))
159         ;
160       else if (unformat (i, "passive %d", &passive_timer))
161         ;
162       else if (unformat (i, "record"))
163         while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
164           {
165             if (unformat (i, "l2"))
166               record_l2 = 1;
167             else if (unformat (i, "l3"))
168               record_l3 = 1;
169             else if (unformat (i, "l4"))
170               record_l4 = 1;
171             else
172               break;
173           }
174       else
175         break;
176     }
177
178   if (passive_timer > 0 && active_timer > passive_timer)
179     {
180       errmsg ("Passive timer has to be greater than active one...\n");
181       return -99;
182     }
183
184   /* Construct the API message */
185   M (FLOWPROBE_PARAMS, mp);
186   mp->record_l2 = record_l2;
187   mp->record_l3 = record_l3;
188   mp->record_l4 = record_l4;
189   mp->active_timer = ntohl (active_timer);
190   mp->passive_timer = ntohl (passive_timer);
191
192   /* send it... */
193   S (mp);
194
195   /* Wait for a reply... */
196   W (ret);
197
198   return ret;
199 }
200
201 /*
202  * List of messages that the api test plugin sends,
203  * and that the data plane plugin processes
204  */
205 #define foreach_vpe_api_msg \
206 _(flowprobe_tx_interface_add_del, "<intfc> [disable]") \
207 _(flowprobe_params, "record <[l2] [l3] [l4]> [active <timer> passive <timer>]")
208
209 static void
210 flowprobe_vat_api_hookup (vat_main_t * vam)
211 {
212   flowprobe_test_main_t *sm = &flowprobe_test_main;
213   /* Hook up handlers for replies from the data plane plug-in */
214 #define _(N,n)                                                  \
215     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
216                            #n,                                  \
217                            vl_api_##n##_t_handler,              \
218                            vl_noop_handler,                     \
219                            vl_api_##n##_t_endian,               \
220                            vl_api_##n##_t_print,                \
221                            sizeof(vl_api_##n##_t), 1);
222   foreach_vpe_api_reply_msg;
223 #undef _
224
225   /* API messages we can send */
226 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
227   foreach_vpe_api_msg;
228 #undef _
229
230   /* Help strings */
231 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
232   foreach_vpe_api_msg;
233 #undef _
234 }
235
236 clib_error_t *
237 vat_plugin_register (vat_main_t * vam)
238 {
239   flowprobe_test_main_t *sm = &flowprobe_test_main;
240   u8 *name;
241
242   sm->vat_main = vam;
243
244   /* Ask the vpp engine for the first assigned message-id */
245   name = format (0, "flowprobe_%08x%c", api_version, 0);
246   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
247
248   /* Don't attempt to hook up API messages if the data plane plugin is AWOL */
249   if (sm->msg_id_base != (u16) ~ 0)
250     flowprobe_vat_api_hookup (vam);
251
252   vec_free (name);
253
254   return 0;
255 }
256
257 /*
258  * fd.io coding-style-patch-verification: ON
259  *
260  * Local Variables:
261  * eval: (c-set-style "gnu")
262  * End:
263  */