Line data Source code
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 <vppinfra/error.h>
21 : #include <flowprobe/flowprobe.h>
22 :
23 : #define __plugin_msg_base flowprobe_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 <flowprobe/flowprobe.api_enum.h>
34 : #include <flowprobe/flowprobe.api_types.h>
35 :
36 : typedef struct
37 : {
38 : /** API message ID base */
39 : u16 msg_id_base;
40 : /** vat_main_t pointer */
41 : vat_main_t *vat_main;
42 : } flowprobe_test_main_t;
43 :
44 : flowprobe_test_main_t flowprobe_test_main;
45 :
46 : static int
47 0 : api_flowprobe_tx_interface_add_del (vat_main_t * vam)
48 : {
49 0 : unformat_input_t *i = vam->input;
50 0 : int enable_disable = 1;
51 0 : u8 which = FLOW_VARIANT_IP4;
52 0 : u32 sw_if_index = ~0;
53 : vl_api_flowprobe_tx_interface_add_del_t *mp;
54 : int ret;
55 :
56 : /* Parse args required to build the message */
57 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
58 : {
59 0 : if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
60 : ;
61 0 : else if (unformat (i, "sw_if_index %d", &sw_if_index))
62 : ;
63 0 : else if (unformat (i, "disable"))
64 0 : enable_disable = 0;
65 0 : else if (unformat (i, "ip4"))
66 0 : which = FLOW_VARIANT_IP4;
67 0 : else if (unformat (i, "ip6"))
68 0 : which = FLOW_VARIANT_IP6;
69 0 : else if (unformat (i, "l2"))
70 0 : which = FLOW_VARIANT_L2;
71 : else
72 0 : break;
73 : }
74 :
75 0 : if (sw_if_index == ~0)
76 : {
77 0 : errmsg ("missing interface name / explicit sw_if_index number \n");
78 0 : return -99;
79 : }
80 :
81 : /* Construct the API message */
82 0 : M (FLOWPROBE_TX_INTERFACE_ADD_DEL, mp);
83 0 : mp->sw_if_index = ntohl (sw_if_index);
84 0 : mp->is_add = enable_disable;
85 0 : mp->which = which;
86 :
87 : /* send it... */
88 0 : S (mp);
89 :
90 : /* Wait for a reply... */
91 0 : W (ret);
92 0 : return ret;
93 : }
94 :
95 : static int
96 0 : api_flowprobe_interface_add_del (vat_main_t *vam)
97 : {
98 0 : unformat_input_t *i = vam->input;
99 0 : int enable_disable = 1;
100 0 : u8 which = FLOWPROBE_WHICH_IP4;
101 0 : u8 direction = FLOWPROBE_DIRECTION_TX;
102 0 : u32 sw_if_index = ~0;
103 : vl_api_flowprobe_interface_add_del_t *mp;
104 : int ret;
105 :
106 : /* Parse args required to build the message */
107 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
108 : {
109 0 : if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
110 : ;
111 0 : else if (unformat (i, "sw_if_index %d", &sw_if_index))
112 : ;
113 0 : else if (unformat (i, "disable"))
114 0 : enable_disable = 0;
115 0 : else if (unformat (i, "ip4"))
116 0 : which = FLOWPROBE_WHICH_IP4;
117 0 : else if (unformat (i, "ip6"))
118 0 : which = FLOWPROBE_WHICH_IP6;
119 0 : else if (unformat (i, "l2"))
120 0 : which = FLOWPROBE_WHICH_L2;
121 0 : else if (unformat (i, "rx"))
122 0 : direction = FLOWPROBE_DIRECTION_RX;
123 0 : else if (unformat (i, "tx"))
124 0 : direction = FLOWPROBE_DIRECTION_TX;
125 0 : else if (unformat (i, "both"))
126 0 : direction = FLOWPROBE_DIRECTION_BOTH;
127 : else
128 0 : break;
129 : }
130 :
131 0 : if (sw_if_index == ~0)
132 : {
133 0 : errmsg ("Missing interface name / explicit sw_if_index number\n");
134 0 : return -99;
135 : }
136 :
137 : /* Construct the API message */
138 0 : M (FLOWPROBE_INTERFACE_ADD_DEL, mp);
139 0 : mp->sw_if_index = ntohl (sw_if_index);
140 0 : mp->is_add = enable_disable;
141 0 : mp->which = which;
142 0 : mp->direction = direction;
143 :
144 : /* Send it... */
145 0 : S (mp);
146 :
147 : /* Wait for a reply... */
148 0 : W (ret);
149 0 : return ret;
150 : }
151 :
152 : static int
153 0 : api_flowprobe_interface_dump (vat_main_t *vam)
154 : {
155 0 : unformat_input_t *i = vam->input;
156 : vl_api_flowprobe_interface_dump_t *mp;
157 : vl_api_control_ping_t *mp_ping;
158 0 : u32 sw_if_index = ~0;
159 : int ret;
160 :
161 : /* Parse args required to build the message */
162 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
163 : {
164 0 : if (unformat (i, "%d", &sw_if_index))
165 : ;
166 : else
167 0 : break;
168 : }
169 :
170 : /* Construct the API message */
171 0 : M (FLOWPROBE_INTERFACE_DUMP, mp);
172 0 : mp->sw_if_index = htonl (sw_if_index);
173 :
174 : /* Send it... */
175 0 : S (mp);
176 :
177 : /* Use control ping for synchronization */
178 0 : PING (&flowprobe_test_main, mp_ping);
179 0 : S (mp_ping);
180 :
181 : /* Wait for a reply... */
182 0 : W (ret);
183 0 : return ret;
184 : }
185 :
186 : static void
187 0 : vl_api_flowprobe_interface_details_t_handler (
188 : vl_api_flowprobe_interface_details_t *mp)
189 : {
190 0 : vat_main_t *vam = flowprobe_test_main.vat_main;
191 : u32 sw_if_index;
192 : u8 which;
193 : u8 direction;
194 0 : u8 *out = 0;
195 0 : const char *variants[] = {
196 : [FLOWPROBE_WHICH_IP4] = "ip4",
197 : [FLOWPROBE_WHICH_IP6] = "ip6",
198 : [FLOWPROBE_WHICH_L2] = "l2",
199 : "Erroneous variant",
200 : };
201 0 : const char *directions[] = {
202 : [FLOWPROBE_DIRECTION_RX] = "rx",
203 : [FLOWPROBE_DIRECTION_TX] = "tx",
204 : [FLOWPROBE_DIRECTION_BOTH] = "rx tx",
205 : "Erroneous direction",
206 : };
207 :
208 0 : sw_if_index = ntohl (mp->sw_if_index);
209 :
210 0 : which = mp->which;
211 0 : if (which > ARRAY_LEN (variants) - 2)
212 0 : which = ARRAY_LEN (variants) - 1;
213 :
214 0 : direction = mp->direction;
215 0 : if (direction > ARRAY_LEN (directions) - 2)
216 0 : direction = ARRAY_LEN (directions) - 1;
217 :
218 0 : out = format (0, "sw_if_index: %u, variant: %s, direction: %s\n%c",
219 : sw_if_index, variants[which], directions[direction], 0);
220 :
221 0 : fformat (vam->ofp, (char *) out);
222 0 : vec_free (out);
223 0 : }
224 :
225 : static int
226 0 : api_flowprobe_params (vat_main_t * vam)
227 : {
228 0 : unformat_input_t *i = vam->input;
229 0 : u32 active_timer = ~0;
230 0 : u32 passive_timer = ~0;
231 : vl_api_flowprobe_params_t *mp;
232 : int ret;
233 0 : u8 record_flags = 0;
234 :
235 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
236 : {
237 0 : if (unformat (i, "active %d", &active_timer))
238 : ;
239 0 : else if (unformat (i, "passive %d", &passive_timer))
240 : ;
241 0 : else if (unformat (i, "record"))
242 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
243 : {
244 0 : if (unformat (i, "l2"))
245 0 : record_flags |= FLOWPROBE_RECORD_FLAG_L2;
246 0 : else if (unformat (i, "l3"))
247 0 : record_flags |= FLOWPROBE_RECORD_FLAG_L3;
248 0 : else if (unformat (i, "l4"))
249 0 : record_flags |= FLOWPROBE_RECORD_FLAG_L4;
250 : else
251 0 : break;
252 : }
253 : else
254 0 : break;
255 : }
256 :
257 0 : if (passive_timer > 0 && active_timer > passive_timer)
258 : {
259 0 : errmsg ("Passive timer has to be greater than active one...\n");
260 0 : return -99;
261 : }
262 :
263 : /* Construct the API message */
264 0 : M (FLOWPROBE_PARAMS, mp);
265 0 : mp->record_flags = record_flags;
266 0 : mp->active_timer = ntohl (active_timer);
267 0 : mp->passive_timer = ntohl (passive_timer);
268 :
269 : /* send it... */
270 0 : S (mp);
271 :
272 : /* Wait for a reply... */
273 0 : W (ret);
274 :
275 0 : return ret;
276 : }
277 :
278 : static int
279 0 : api_flowprobe_set_params (vat_main_t *vam)
280 : {
281 0 : unformat_input_t *i = vam->input;
282 : vl_api_flowprobe_set_params_t *mp;
283 0 : u32 active_timer = ~0;
284 0 : u32 passive_timer = ~0;
285 0 : u8 record_flags = 0;
286 : int ret;
287 :
288 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
289 : {
290 0 : if (unformat (i, "active %d", &active_timer))
291 : ;
292 0 : else if (unformat (i, "passive %d", &passive_timer))
293 : ;
294 0 : else if (unformat (i, "record"))
295 0 : while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
296 : {
297 0 : if (unformat (i, "l2"))
298 0 : record_flags |= FLOWPROBE_RECORD_FLAG_L2;
299 0 : else if (unformat (i, "l3"))
300 0 : record_flags |= FLOWPROBE_RECORD_FLAG_L3;
301 0 : else if (unformat (i, "l4"))
302 0 : record_flags |= FLOWPROBE_RECORD_FLAG_L4;
303 : else
304 0 : break;
305 : }
306 : else
307 0 : break;
308 : }
309 :
310 : /* Construct the API message */
311 0 : M (FLOWPROBE_SET_PARAMS, mp);
312 0 : mp->record_flags = record_flags;
313 0 : mp->active_timer = ntohl (active_timer);
314 0 : mp->passive_timer = ntohl (passive_timer);
315 :
316 : /* send it... */
317 0 : S (mp);
318 :
319 : /* Wait for a reply... */
320 0 : W (ret);
321 :
322 0 : return ret;
323 : }
324 :
325 : static int
326 0 : api_flowprobe_get_params (vat_main_t *vam)
327 : {
328 : vl_api_flowprobe_get_params_t *mp;
329 : int ret;
330 :
331 : /* Construct the API message */
332 0 : M (FLOWPROBE_GET_PARAMS, mp);
333 :
334 : /* Send it... */
335 0 : S (mp);
336 :
337 : /* Wait for a reply... */
338 0 : W (ret);
339 0 : return ret;
340 : }
341 :
342 : static void
343 0 : vl_api_flowprobe_get_params_reply_t_handler (
344 : vl_api_flowprobe_get_params_reply_t *mp)
345 : {
346 0 : vat_main_t *vam = flowprobe_test_main.vat_main;
347 0 : u8 *out = 0;
348 :
349 0 : out =
350 0 : format (0, "active: %u, passive: %u, record:", ntohl (mp->active_timer),
351 : ntohl (mp->passive_timer));
352 :
353 0 : if (mp->record_flags & FLOWPROBE_RECORD_FLAG_L2)
354 0 : out = format (out, " l2");
355 0 : if (mp->record_flags & FLOWPROBE_RECORD_FLAG_L3)
356 0 : out = format (out, " l3");
357 0 : if (mp->record_flags & FLOWPROBE_RECORD_FLAG_L4)
358 0 : out = format (out, " l4");
359 :
360 0 : out = format (out, "\n%c", 0);
361 0 : fformat (vam->ofp, (char *) out);
362 0 : vec_free (out);
363 0 : vam->result_ready = 1;
364 0 : }
365 :
366 : /*
367 : * List of messages that the api test plugin sends,
368 : * and that the data plane plugin processes
369 : */
370 : #include <flowprobe/flowprobe.api_test.c>
371 :
372 : /*
373 : * fd.io coding-style-patch-verification: ON
374 : *
375 : * Local Variables:
376 : * eval: (c-set-style "gnu")
377 : * End:
378 : */
|