nat: Include platform specific headers on FreeBSD
[vpp.git] / src / plugins / mdata / mdata.c
1 /*
2  * mdata.c - Buffer metadata change tracker
3  *
4  * Copyright (c) 2019 Cisco and/or its affiliates.
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
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <mdata/mdata.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vppinfra/callback_data.h>
25 #include <vpp/app/version.h>
26 #include <stdbool.h>
27
28 #include <mdata/mdata.api_enum.h>
29 #include <mdata/mdata.api_types.h>
30
31 #define REPLY_MSG_ID_BASE mmp->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
33
34 mdata_main_t mdata_main;
35
36 /** @file mdata.c
37  * buffer metadata change tracker
38  */
39
40 static mdata_t mdata_none;
41
42 /** Metadata tracking callback
43     before_or_after: 0 => before, 1=> after
44 */
45 static void
46 mdata_trace_callback (vlib_node_runtime_perf_callback_data_t * data,
47                       vlib_node_runtime_perf_callback_args_t * args)
48 {
49   int i;
50   mdata_main_t *mm = &mdata_main;
51   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
52   u32 *from;
53   u32 n_left_from;
54   mdata_t *before, *modifies;
55   u8 *after;
56   vlib_main_t *vm = args->vm;
57   vlib_frame_t *frame = args->frame;
58   vlib_node_runtime_t *node = args->node;
59
60   if (PREDICT_FALSE (args->call_type == VLIB_NODE_RUNTIME_PERF_RESET))
61     return;
62
63   /* Input nodes don't have frames, etc. */
64   if (frame == 0)
65     return;
66
67   n_left_from = frame->n_vectors;
68
69   if (n_left_from == 0)
70     return;
71
72   from = vlib_frame_vector_args (frame);
73
74   vlib_get_buffers (vm, from, bufs, n_left_from);
75   b = bufs;
76
77   if (args->call_type == VLIB_NODE_RUNTIME_PERF_AFTER)
78     goto after_pass;
79
80   /* Resize the per-thread "before" vector to cover the current frame */
81   vec_reset_length (mm->before_per_thread[vm->thread_index]);
82   vec_validate (mm->before_per_thread[vm->thread_index], n_left_from - 1);
83   before = mm->before_per_thread[vm->thread_index];
84   before->node_index = ~0;
85
86   /* Before we call the dispatch fn, copy metadata. */
87   while (n_left_from > 0)
88     {
89       clib_memcpy_fast (before->mdata, b[0], sizeof (before->mdata));
90       b++;
91       before++;
92       n_left_from--;
93     }
94   return;
95
96 after_pass:
97
98   /* Recover the metadata copy we saved a moment ago */
99   before = mm->before_per_thread[vm->thread_index];
100
101   /* We'd better have the same number of buffers... */
102   ASSERT (n_left_from == vec_len (before));
103   ASSERT (node->node_index);
104
105   clib_spinlock_lock_if_init (&mm->modify_lock);
106
107   /*
108    * Resize the per-node accumulator vector as needed
109    * Paint the "no data" patter across any nodes we haven't seen yet
110    */
111   vec_validate_init_empty (mm->modifies, node->node_index, mdata_none);
112   modifies = vec_elt_at_index (mm->modifies, node->node_index);
113   modifies->node_index = node->node_index;
114   before = mm->before_per_thread[vm->thread_index];
115
116   /* Walk the frame */
117   while (n_left_from > 0)
118     {
119       after = (u8 *) b[0];
120
121       /* Compare metadata before and after node dispatch fn */
122       for (i = 0; i < ARRAY_LEN (before->mdata); i++)
123         {
124           /* Mark mdata octet changed */
125           if (before->mdata[i] != after[i])
126             modifies->mdata[i] = 0xff;
127         }
128
129       b++;
130       before++;
131       n_left_from--;
132     }
133
134   clib_spinlock_unlock_if_init (&mm->modify_lock);
135 }
136
137 int
138 mdata_enable_disable (mdata_main_t * mmp, int enable_disable)
139 {
140   int rv = 0;
141   vlib_thread_main_t *thread_main = vlib_get_thread_main ();
142   int i;
143
144   if (mmp->modify_lock == 0 && thread_main->n_vlib_mains > 1)
145     clib_spinlock_init (&mmp->modify_lock);
146
147   if (vec_len (mmp->before_per_thread) == 0)
148     {
149       mdata_none.node_index = ~0;
150       vec_validate (mmp->before_per_thread, vlib_get_n_threads () - 1);
151     }
152
153   /* Reset the per-node accumulator, see vec_validate_init_empty above */
154   vec_reset_length (mmp->modifies);
155
156   for (i = 0; i < vlib_get_n_threads (); i++)
157     {
158       vlib_main_t *ovm = vlib_get_main_by_index (i);
159       if (ovm == 0)
160         continue;
161
162       clib_callback_data_enable_disable (
163         &ovm->vlib_node_runtime_perf_callbacks, mdata_trace_callback,
164         enable_disable);
165     }
166
167   return rv;
168 }
169
170 static clib_error_t *
171 mdata_enable_disable_command_fn (vlib_main_t * vm,
172                                  unformat_input_t * input,
173                                  vlib_cli_command_t * cmd)
174 {
175   mdata_main_t *mmp = &mdata_main;
176   int enable_disable = 1;
177
178   int rv;
179
180   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
181     {
182       if (unformat (input, "disable") || unformat (input, "off"))
183         enable_disable = 0;
184       if (unformat (input, "enable") || unformat (input, "on"))
185         enable_disable = 1;
186       else
187         break;
188     }
189
190   rv = mdata_enable_disable (mmp, enable_disable);
191
192   switch (rv)
193     {
194     case 0:
195       break;
196
197     default:
198       return clib_error_return (0, "mdata_enable_disable returned %d", rv);
199     }
200   return 0;
201 }
202
203 /*?
204  * This command enables or disables buffer metadata change tracking
205  *
206  * @cliexpar
207  * To enable buffer metadata change tracking:
208  * @cliexstart{buffer metadata tracking on}
209  * Tracking enabled
210  * @cliexend
211  *
212  * @cliexstart{buffer metadata tracking off}
213  * Tracking disabled
214  * @cliexend
215 ?*/
216
217 VLIB_CLI_COMMAND (mdata_enable_disable_command, static) =
218 {
219   .path = "buffer metadata tracking",
220   .short_help = "buffer metadata tracking [on][off]",
221   .function = mdata_enable_disable_command_fn,
222 };
223
224 /* API message handler */
225 static void vl_api_mdata_enable_disable_t_handler
226   (vl_api_mdata_enable_disable_t * mp)
227 {
228   vl_api_mdata_enable_disable_reply_t *rmp;
229   mdata_main_t *mmp = &mdata_main;
230   int rv;
231
232   rv = mdata_enable_disable (mmp, (int) (mp->enable_disable));
233
234   REPLY_MACRO (VL_API_MDATA_ENABLE_DISABLE_REPLY);
235 }
236
237 /* API definitions */
238 #include <mdata/mdata.api.c>
239
240 static clib_error_t *
241 mdata_init (vlib_main_t * vm)
242 {
243   mdata_main_t *mmp = &mdata_main;
244   clib_error_t *error = 0;
245
246   mmp->vlib_main = vm;
247   mmp->vnet_main = vnet_get_main ();
248
249   /* Add our API messages to the global name_crc hash table */
250   mmp->msg_id_base = setup_message_id_table ();
251
252   return error;
253 }
254
255 VLIB_INIT_FUNCTION (mdata_init);
256
257 VLIB_PLUGIN_REGISTER () =
258 {
259   .version = VPP_BUILD_VER,
260   .description = "Buffer metadata change tracker."
261 };
262
263
264 #define foreach_primary_metadata_field          \
265 _(current_data)                                 \
266 _(current_length)                               \
267 _(flags)                                        \
268 _(flow_id)                                      \
269 _(ref_count)                                    \
270 _(buffer_pool_index)                            \
271 _(error)                                        \
272 _(next_buffer)                                  \
273 _(current_config_index)                         \
274 _(punt_reason)
275
276 #define foreach_opaque_metadata_field           \
277 _(sw_if_index[0])                               \
278 _(sw_if_index[1])                               \
279 _(l2_hdr_offset)                                \
280 _(l3_hdr_offset)                                \
281 _(l4_hdr_offset)                                \
282 _(feature_arc_index)                            \
283 _(ip.adj_index[0])                              \
284 _(ip.adj_index[1])                              \
285 _(ip.flow_hash)                                 \
286 _(ip.save_protocol)                             \
287 _(ip.fib_index)                                 \
288 _(ip.icmp.type)                                 \
289 _(ip.icmp.code)                                 \
290 _(ip.icmp.data)                                 \
291 _(ip.reass.next_index)                          \
292 _(ip.reass.error_next_index)                    \
293 _(ip.reass.owner_thread_index)                  \
294 _(ip.reass.ip_proto)                            \
295 _(ip.reass.l4_src_port)                         \
296 _(ip.reass.l4_dst_port)                         \
297 _(ip.reass.estimated_mtu)                       \
298 _(ip.reass.fragment_first)                      \
299 _(ip.reass.fragment_last)                       \
300 _(ip.reass.range_first)                         \
301 _(ip.reass.range_last)                          \
302 _(ip.reass.next_range_bi)                       \
303 _(ip.reass.ip6_frag_hdr_offset)                 \
304 _(mpls.ttl)                                     \
305 _(mpls.exp)                                     \
306 _(mpls.first)                                   \
307 _(mpls.save_rewrite_length)                     \
308 _(mpls.mpls_hdr_length)                         \
309 _(mpls.bier.n_bytes)                            \
310 _(l2.feature_bitmap)                            \
311 _(l2.bd_index)                                  \
312 _(l2.l2fib_sn)                                  \
313 _(l2.l2_len)                                    \
314 _(l2.shg)                                       \
315 _(l2.bd_age)                                    \
316 _(l2t.next_index)                               \
317 _(l2t.session_index)                            \
318 _(l2_classify.table_index)                      \
319 _(l2_classify.opaque_index)                     \
320 _(l2_classify.hash)                             \
321 _(policer.index)                                \
322 _(ipsec.sad_index)                              \
323 _(ipsec.protect_index)                          \
324 _(map.mtu)                                      \
325 _(map_t.map_domain_index)                       \
326 _(map_t.v6.saddr)                               \
327 _(map_t.v6.daddr)                               \
328 _(map_t.v6.frag_offset)                         \
329 _(map_t.v6.l4_offset)                           \
330 _(map_t.v6.l4_protocol)                         \
331 _(map_t.checksum_offset)                        \
332 _(map_t.mtu)                                    \
333 _(ip_frag.mtu)                                  \
334 _(ip_frag.next_index)                           \
335 _(ip_frag.flags)                                \
336 _(cop.current_config_index)                     \
337 _(lisp.overlay_afi)                             \
338 _(tcp.connection_index)                         \
339 _(tcp.seq_number)                               \
340 _(tcp.next_node_opaque)                         \
341 _(tcp.seq_end)                                  \
342 _(tcp.ack_number)                               \
343 _(tcp.hdr_offset)                               \
344 _(tcp.data_offset)                              \
345 _(tcp.data_len)                                 \
346 _(tcp.flags)                                    \
347 _(snat.flags)
348
349 #define foreach_opaque2_metadata_field                                        \
350   _ (qos.bits)                                                                \
351   _ (qos.source)                                                              \
352   _ (loop_counter)                                                            \
353   _ (gso_size)                                                                \
354   _ (gso_l4_hdr_sz)
355
356 static u8 *
357 format_buffer_metadata_changes (u8 * s, va_list * args)
358 {
359   mdata_main_t *mm = va_arg (*args, mdata_main_t *);
360   int verbose = va_arg (*args, int);
361   mdata_t *modifies;
362   vlib_buffer_t *b;
363   vnet_buffer_opaque_t *o;
364   vnet_buffer_opaque2_t *o2;
365   vlib_node_t *node;
366   int i, j;
367   int printed;
368
369   clib_spinlock_lock_if_init (&mm->modify_lock);
370
371   for (i = 0; i < vec_len (mm->modifies); i++)
372     {
373       modifies = vec_elt_at_index (mm->modifies, i);
374       node = vlib_get_node (mm->vlib_main, i);
375
376       /* No data for this node? */
377       if (modifies->node_index == ~0)
378         {
379           if (verbose)
380             s = format (s, "\n%v: no data\n", node->name);
381           continue;
382         }
383
384       /* We visited the node, but it may not have changed any metadata... */
385       for (j = 0; j < ARRAY_LEN (modifies->mdata); j++)
386         {
387           if (modifies->mdata[j])
388             goto found;
389         }
390       s = format (s, "\n%v: no metadata changes\n", node->name);
391       continue;
392
393     found:
394       /* Fields which the node modifies will be non-zero */
395       b = (vlib_buffer_t *) (modifies->mdata);
396
397       /* Dump primary metadata changes */
398       s = format (s, "\n%v: ", node->name);
399
400       printed = 0;
401 #define _(n) if (b->n) {s = format (s, "%s ", #n); printed = 1;}
402       foreach_primary_metadata_field;
403 #undef _
404
405       if (printed == 0)
406         s = format (s, "no vlib_buffer_t metadata changes");
407
408       vec_add1 (s, '\n');
409
410       /*
411        * Dump opaque union changes.
412        * Hopefully this will give folks a clue about opaque
413        * union data conflicts. That's the point of the exercise...
414        */
415       o = vnet_buffer (b);
416       printed = 0;
417       s = format (s, "  vnet_buffer_t: ");
418
419 #define _(n) if (o->n) {s = format (s, "%s ", #n); printed = 1;}
420       foreach_opaque_metadata_field;
421 #undef _
422
423       if (printed == 0)
424         s = format (s, "no changes");
425
426       vec_add1 (s, '\n');
427
428       o2 = vnet_buffer2 (b);
429       printed = 0;
430       s = format (s, "  vnet_buffer2_t: ");
431
432 #define _(n) if (o2->n) {s = format (s, "%s ", #n); printed = 1;}
433       foreach_opaque2_metadata_field;
434 #undef _
435       if (printed == 0)
436         s = format (s, "no changes");
437
438       vec_add1 (s, '\n');
439
440     }
441
442   clib_spinlock_unlock_if_init (&mm->modify_lock);
443
444   return s;
445 }
446
447 static clib_error_t *
448 show_metadata_command_fn (vlib_main_t * vm,
449                           unformat_input_t * input, vlib_cli_command_t * cmd)
450 {
451   int verbose = 0;
452
453   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
454     {
455       if (unformat (input, "verbose %=", &verbose, 1))
456         ;
457       else
458         break;
459     }
460
461   vlib_cli_output (vm, "%U", format_buffer_metadata_changes, &mdata_main,
462                    verbose);
463   return 0;
464 }
465
466 /*?
467  * This command displays buffer metadata change information
468  * @cliexpar
469  * How to display buffer metadata change information
470  * @cliexstart{show buffer metadata}
471  * ethernet-input: current_data current_length flags error
472  * vnet_buffer_t: l2_hdr_offset l3_hdr_offset
473  * vnet_buffer2_t: no changes
474  * @cliexend
475 ?*/
476
477 VLIB_CLI_COMMAND (show_metadata_command, static) =
478 {
479   .path = "show buffer metadata",
480   .short_help = "show buffer metadata",
481   .function = show_metadata_command_fn,
482 };
483
484 /*
485  * fd.io coding-style-patch-verification: ON
486  *
487  * Local Variables:
488  * eval: (c-set-style "gnu")
489  * End:
490  */