ethernet: check destination mac for L3 in ethernet-input node
[vpp.git] / src / plugins / tracedump / tracedump_test.c
1 /*
2  * tracedump.c - tracedump 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 <vnet/api_errno.h>
22 #include <stdbool.h>
23
24 #define __plugin_msg_base tracedump_test_main.msg_id_base
25 #include <vlibapi/vat_helper_macros.h>
26
27 /* Declare message IDs */
28 #include <tracedump/tracedump.api_enum.h>
29 #include <tracedump/tracedump.api_types.h>
30
31 typedef struct
32 {
33   /* API message ID base */
34   u16 msg_id_base;
35   vat_main_t *vat_main;
36 } tracedump_test_main_t;
37
38 tracedump_test_main_t tracedump_test_main;
39
40
41 int
42 api_trace_set_filters (vat_main_t * vam)
43 {
44   unformat_input_t *i = vam->input;
45   vl_api_trace_set_filters_t *mp;
46   u32 flag;
47   u32 count;
48   u32 node_index;
49   u32 classifier;
50
51   flag = TRACE_FF_NONE;
52   count = 50;
53   node_index = ~0;
54   classifier = ~0;
55
56   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
57     {
58       if (unformat (i, "none"))
59         flag = TRACE_FF_NONE;
60       else if (unformat (i, "include_node %u", &node_index))
61         flag = TRACE_FF_INCLUDE_NODE;
62       else if (unformat (i, "exclude_node %u", &node_index))
63         flag = TRACE_FF_EXCLUDE_NODE;
64       else if (unformat (i, "include_classifier %u", &classifier))
65         flag = TRACE_FF_INCLUDE_CLASSIFIER;
66       else if (unformat (i, "exclude_classifier %u", &classifier))
67         flag = TRACE_FF_EXCLUDE_CLASSIFIER;
68       else if (unformat (i, "count %u", &count))
69         ;
70       else
71         {
72           clib_warning ("Unknown input: %U\n", format_unformat_error, i);
73           return -99;
74         }
75     }
76
77   M (TRACE_SET_FILTERS, mp);
78   mp->flag = htonl (flag);
79   mp->node_index = htonl (node_index);
80   mp->count = htonl (count);
81   mp->classifier_table_index = htonl (classifier);
82
83   int ret = 0;
84   S (mp);
85   W (ret);
86
87   return ret;
88 }
89
90
91 int
92 api_trace_capture_packets (vat_main_t * vam)
93 {
94   unformat_input_t *i = vam->input;
95   vl_api_trace_capture_packets_t *mp;
96   u32 node_index;
97   u32 max;
98   bool pre_capture_clear;
99   bool use_filter;
100   bool verbose;
101
102   node_index = ~0;
103   max = 50;
104   pre_capture_clear = use_filter = verbose = false;
105
106   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
107     {
108       if (unformat (i, "node_index %u", &node_index))
109         ;
110       else if (unformat (i, "max %u", &max))
111         ;
112       else if (unformat (i, "pre_capture_clear"))
113         pre_capture_clear = false;
114       else if (unformat (i, "use_filter"))
115         use_filter = false;
116       else if (unformat (i, "verbose"))
117         verbose = false;
118       else
119         {
120           clib_warning ("Unknown input: %U\n", format_unformat_error, i);
121           return -99;
122         }
123     }
124
125   M (TRACE_CAPTURE_PACKETS, mp);
126   mp->node_index = htonl (node_index);
127   mp->max_packets = htonl (max);
128   mp->use_filter = use_filter;
129   mp->verbose = verbose;
130   mp->pre_capture_clear = pre_capture_clear;
131
132   int ret = 0;
133   S (mp);
134   W (ret);
135
136   return ret;
137 }
138
139
140 static void
141 vl_api_trace_details_t_handler (vl_api_trace_details_t * dmp)
142 {
143   u32 packet_number;
144   u32 thread_id, position;
145
146   thread_id = clib_net_to_host_u32 (dmp->thread_id);
147   position = clib_net_to_host_u32 (dmp->position);
148   packet_number = clib_net_to_host_u32 (dmp->packet_number);
149   fformat
150     (stdout,
151      "thread %d position %d more_this_thread %d more_threads %d done %d\n",
152      thread_id, position, (u32) dmp->more_this_thread,
153      (u32) dmp->more_threads, (u32) dmp->done);
154   fformat (stdout, "Packet %d\n%U\n\n",
155            packet_number, vl_api_format_string, (&dmp->trace_data));
156 }
157
158 static void
159 vl_api_trace_v2_details_t_handler (vl_api_trace_v2_details_t *dmp)
160 {
161   u32 thread_id, position;
162
163   thread_id = clib_net_to_host_u32 (dmp->thread_id);
164   position = clib_net_to_host_u32 (dmp->position);
165   fformat (stdout, "thread %d position %d more %d", thread_id, position,
166            dmp->more);
167   fformat (stdout, "Packet %d\n%U\n\n", position, vl_api_format_string,
168            (&dmp->trace_data));
169 }
170
171 static void
172 vl_api_trace_dump_reply_t_handler (vl_api_trace_dump_reply_t * rmp)
173 {
174   tracedump_test_main_t *ttm = &tracedump_test_main;
175   vat_main_t *vam = ttm->vat_main;
176   vl_api_trace_dump_t *mp;
177   i32 retval = (i32) clib_net_to_host_u32 (rmp->retval);
178   u32 thread_id, position;
179
180   if (retval != 0 || rmp->done)
181     {
182       vam->result_ready = 1;
183       vam->retval = retval;
184
185       /* Clear the cache */
186       if (retval == 0 && rmp->flush_only == 0)
187         {
188           M (TRACE_DUMP, mp);
189           mp->clear_cache = 1;
190           mp->thread_id = 0xFFFFFFFF;
191           mp->position = 0xFFFFFFFF;
192           S (mp);
193         }
194       return;
195     }
196
197   /* Figure out where the next batch starts */
198   thread_id = clib_host_to_net_u32 (rmp->last_thread_id);
199   position = clib_host_to_net_u32 (rmp->last_position);
200
201   if (rmp->more_threads)
202     {
203       position = 0;
204       thread_id++;
205     }
206   else
207     position++;
208
209   M (TRACE_DUMP, mp);
210   mp->clear_cache = 0;
211   mp->thread_id = clib_host_to_net_u32 (thread_id);
212   mp->position = clib_host_to_net_u32 (position);
213   mp->max_records = clib_host_to_net_u32 (10);
214   S (mp);
215 }
216
217 static int
218 api_trace_dump (vat_main_t *vam)
219 {
220   vl_api_trace_dump_t *mp;
221   int ret;
222
223   M (TRACE_DUMP, mp);
224   mp->clear_cache = 1;
225   mp->thread_id = 0;
226   mp->position = 0;
227   mp->max_records = clib_host_to_net_u32 (10);
228
229   S (mp);
230
231   W (ret);
232   return ret;
233 }
234
235 static int
236 api_trace_v2_dump (vat_main_t *vam)
237 {
238   vl_api_trace_v2_dump_t *mp;
239   int ret;
240
241   M (TRACE_V2_DUMP, mp);
242   mp->clear_cache = 1;
243   mp->thread_id = ~0;
244   mp->position = 0;
245   mp->max = clib_host_to_net_u32 (10);
246
247   S (mp);
248
249   W (ret);
250   return ret;
251 }
252
253 int
254 api_trace_clear_capture (vat_main_t *vam)
255 {
256   vl_api_trace_clear_capture_t *mp;
257   int ret;
258
259   M (TRACE_CLEAR_CAPTURE, mp);
260   S (mp);
261   W (ret);
262   return ret;
263 }
264
265 static int
266 api_trace_clear_cache (vat_main_t *vam)
267 {
268   vl_api_trace_clear_capture_t *mp;
269   int ret;
270
271   M (TRACE_CLEAR_CACHE, mp);
272   S (mp);
273   W (ret);
274   return ret;
275 }
276
277 static int
278 api_trace_set_filter_function (vat_main_t *vam)
279 {
280   vl_api_trace_set_filter_function_t *mp;
281   int ret;
282
283   M (TRACE_SET_FILTER_FUNCTION, mp);
284   S (mp);
285   W (ret);
286   return ret;
287 }
288
289 static int
290 api_trace_filter_function_dump (vat_main_t *vam)
291 {
292   vl_api_trace_filter_function_dump_t *mp;
293   int ret;
294
295   M (TRACE_FILTER_FUNCTION_DUMP, mp);
296   S (mp);
297   W (ret);
298   return ret;
299 }
300
301 static void
302 vl_api_trace_filter_function_details_t_handler (
303   vl_api_trace_filter_function_details_t *dmp)
304 {
305   fformat (stdout, "name: %U, selected: %u\n\n", vl_api_format_string,
306            &dmp->name, dmp->selected);
307 }
308
309 #define vl_endianfun
310 #include <tracedump/tracedump.api.h>
311 #undef vl_endianfun
312 #define vl_printfun
313 #include <tracedump/tracedump.api.h>
314 #undef vl_printfun
315 #define vl_calcsizefun
316 #include <tracedump/tracedump.api.h>
317 #undef vl_calcsizefun
318
319 void
320 manual_setup_message_id_table (vat_main_t * vam)
321 {
322   vl_msg_api_config (&(vl_msg_api_msg_config_t){
323     .id = VL_API_TRACE_DETAILS + tracedump_test_main.msg_id_base,
324     .name = "trace_details",
325     .handler = vl_api_trace_details_t_handler,
326     .endian = vl_api_trace_details_t_endian,
327     .format_fn = vl_api_trace_details_t_format,
328     .size = sizeof (vl_api_trace_details_t),
329     .traced = 1,
330     .tojson = vl_api_trace_details_t_tojson,
331     .fromjson = vl_api_trace_details_t_fromjson,
332     .calc_size = vl_api_trace_details_t_calc_size,
333   });
334 }
335
336 #define VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE manual_setup_message_id_table
337 #define VL_API_TRACE_DUMP_REPLY_T_HANDLER
338
339 #include <tracedump/tracedump.api_test.c>
340
341 /*
342  * fd.io coding-style-patch-verification: ON
343  *
344  * Local Variables:
345  * eval: (c-set-style "gnu")
346  * End:
347  */