2 * mdata.c - Buffer metadata change tracker
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <mdata/mdata.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vppinfra/callback_data.h>
25 #include <vpp/app/version.h>
28 #include <mdata/mdata.api_enum.h>
29 #include <mdata/mdata.api_types.h>
31 #define REPLY_MSG_ID_BASE mmp->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
34 mdata_main_t mdata_main;
37 * buffer metadata change tracker
40 static mdata_t mdata_none;
42 /** Metadata tracking callback
43 before_or_after: 0 => before, 1=> after
46 mdata_trace_callback (vlib_node_runtime_perf_callback_data_t * data,
47 vlib_node_runtime_perf_callback_args_t * args)
50 mdata_main_t *mm = &mdata_main;
51 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
54 mdata_t *before, *modifies;
56 vlib_main_t *vm = args->vm;
57 vlib_frame_t *frame = args->frame;
58 vlib_node_runtime_t *node = args->node;
60 if (PREDICT_FALSE (args->call_type == VLIB_NODE_RUNTIME_PERF_RESET))
63 /* Input nodes don't have frames, etc. */
67 n_left_from = frame->n_vectors;
72 from = vlib_frame_vector_args (frame);
74 vlib_get_buffers (vm, from, bufs, n_left_from);
77 if (args->call_type == VLIB_NODE_RUNTIME_PERF_AFTER)
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;
86 /* Before we call the dispatch fn, copy metadata. */
87 while (n_left_from > 0)
89 clib_memcpy_fast (before->mdata, b[0], sizeof (before->mdata));
98 /* Recover the metadata copy we saved a moment ago */
99 before = mm->before_per_thread[vm->thread_index];
101 /* We'd better have the same number of buffers... */
102 ASSERT (n_left_from == vec_len (before));
103 ASSERT (node->node_index);
105 clib_spinlock_lock_if_init (&mm->modify_lock);
108 * Resize the per-node accumulator vector as needed
109 * Paint the "no data" patter across any nodes we haven't seen yet
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];
117 while (n_left_from > 0)
121 /* Compare metadata before and after node dispatch fn */
122 for (i = 0; i < ARRAY_LEN (before->mdata); i++)
124 /* Mark mdata octet changed */
125 if (before->mdata[i] != after[i])
126 modifies->mdata[i] = 0xff;
134 clib_spinlock_unlock_if_init (&mm->modify_lock);
138 mdata_enable_disable (mdata_main_t * mmp, int enable_disable)
141 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
144 if (mmp->modify_lock == 0 && thread_main->n_vlib_mains > 1)
145 clib_spinlock_init (&mmp->modify_lock);
147 if (vec_len (mmp->before_per_thread) == 0)
149 mdata_none.node_index = ~0;
150 vec_validate (mmp->before_per_thread, vlib_get_n_threads () - 1);
153 /* Reset the per-node accumulator, see vec_validate_init_empty above */
154 vec_reset_length (mmp->modifies);
156 for (i = 0; i < vlib_get_n_threads (); i++)
158 vlib_main_t *ovm = vlib_get_main_by_index (i);
162 clib_callback_data_enable_disable (
163 &ovm->vlib_node_runtime_perf_callbacks, mdata_trace_callback,
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)
175 mdata_main_t *mmp = &mdata_main;
176 int enable_disable = 1;
180 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
182 if (unformat (input, "disable") || unformat (input, "off"))
184 if (unformat (input, "enable") || unformat (input, "on"))
190 rv = mdata_enable_disable (mmp, enable_disable);
198 return clib_error_return (0, "mdata_enable_disable returned %d", rv);
204 * This command enables or disables buffer metadata change tracking
207 * To enable buffer metadata change tracking:
208 * @cliexstart{buffer metadata tracking on}
212 * @cliexstart{buffer metadata tracking off}
217 VLIB_CLI_COMMAND (mdata_enable_disable_command, static) =
219 .path = "buffer metadata tracking",
220 .short_help = "buffer metadata tracking [on][off]",
221 .function = mdata_enable_disable_command_fn,
224 /* API message handler */
225 static void vl_api_mdata_enable_disable_t_handler
226 (vl_api_mdata_enable_disable_t * mp)
228 vl_api_mdata_enable_disable_reply_t *rmp;
229 mdata_main_t *mmp = &mdata_main;
232 rv = mdata_enable_disable (mmp, (int) (mp->enable_disable));
234 REPLY_MACRO (VL_API_MDATA_ENABLE_DISABLE_REPLY);
237 /* API definitions */
238 #include <mdata/mdata.api.c>
240 static clib_error_t *
241 mdata_init (vlib_main_t * vm)
243 mdata_main_t *mmp = &mdata_main;
244 clib_error_t *error = 0;
247 mmp->vnet_main = vnet_get_main ();
249 /* Add our API messages to the global name_crc hash table */
250 mmp->msg_id_base = setup_message_id_table ();
255 VLIB_INIT_FUNCTION (mdata_init);
257 VLIB_PLUGIN_REGISTER () =
259 .version = VPP_BUILD_VER,
260 .description = "Buffer metadata change tracker."
264 #define foreach_primary_metadata_field \
270 _(buffer_pool_index) \
273 _(current_config_index) \
276 #define foreach_opaque_metadata_field \
282 _(feature_arc_index) \
286 _(ip.save_protocol) \
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) \
307 _(mpls.save_rewrite_length) \
308 _(mpls.mpls_hdr_length) \
309 _(mpls.bier.n_bytes) \
310 _(l2.feature_bitmap) \
317 _(l2t.session_index) \
318 _(l2_classify.table_index) \
319 _(l2_classify.opaque_index) \
320 _(l2_classify.hash) \
323 _(ipsec.protect_index) \
325 _(map_t.map_domain_index) \
328 _(map_t.v6.frag_offset) \
329 _(map_t.v6.l4_offset) \
330 _(map_t.v6.l4_protocol) \
331 _(map_t.checksum_offset) \
334 _(ip_frag.next_index) \
336 _(cop.current_config_index) \
337 _(lisp.overlay_afi) \
338 _(tcp.connection_index) \
340 _(tcp.next_node_opaque) \
349 #define foreach_opaque2_metadata_field \
357 format_buffer_metadata_changes (u8 * s, va_list * args)
359 mdata_main_t *mm = va_arg (*args, mdata_main_t *);
360 int verbose = va_arg (*args, int);
363 vnet_buffer_opaque_t *o;
364 vnet_buffer_opaque2_t *o2;
369 clib_spinlock_lock_if_init (&mm->modify_lock);
371 for (i = 0; i < vec_len (mm->modifies); i++)
373 modifies = vec_elt_at_index (mm->modifies, i);
374 node = vlib_get_node (mm->vlib_main, i);
376 /* No data for this node? */
377 if (modifies->node_index == ~0)
380 s = format (s, "\n%v: no data\n", node->name);
384 /* We visited the node, but it may not have changed any metadata... */
385 for (j = 0; j < ARRAY_LEN (modifies->mdata); j++)
387 if (modifies->mdata[j])
390 s = format (s, "\n%v: no metadata changes\n", node->name);
394 /* Fields which the node modifies will be non-zero */
395 b = (vlib_buffer_t *) (modifies->mdata);
397 /* Dump primary metadata changes */
398 s = format (s, "\n%v: ", node->name);
401 #define _(n) if (b->n) {s = format (s, "%s ", #n); printed = 1;}
402 foreach_primary_metadata_field;
406 s = format (s, "no vlib_buffer_t metadata changes");
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...
417 s = format (s, " vnet_buffer_t: ");
419 #define _(n) if (o->n) {s = format (s, "%s ", #n); printed = 1;}
420 foreach_opaque_metadata_field;
424 s = format (s, "no changes");
428 o2 = vnet_buffer2 (b);
430 s = format (s, " vnet_buffer2_t: ");
432 #define _(n) if (o2->n) {s = format (s, "%s ", #n); printed = 1;}
433 foreach_opaque2_metadata_field;
436 s = format (s, "no changes");
442 clib_spinlock_unlock_if_init (&mm->modify_lock);
447 static clib_error_t *
448 show_metadata_command_fn (vlib_main_t * vm,
449 unformat_input_t * input, vlib_cli_command_t * cmd)
453 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
455 if (unformat (input, "verbose %=", &verbose, 1))
461 vlib_cli_output (vm, "%U", format_buffer_metadata_changes, &mdata_main,
467 * This command displays buffer metadata change information
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
477 VLIB_CLI_COMMAND (show_metadata_command, static) =
479 .path = "show buffer metadata",
480 .short_help = "show buffer metadata",
481 .function = show_metadata_command_fn,
485 * fd.io coding-style-patch-verification: ON
488 * eval: (c-set-style "gnu")