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 <vpp/app/version.h>
27 #include <mdata/mdata.api_enum.h>
28 #include <mdata/mdata.api_types.h>
30 #define REPLY_MSG_ID_BASE mmp->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
33 mdata_main_t mdata_main;
35 /** @file buffer metadata change tracker
38 static mdata_t mdata_none;
40 /** Metadata tracking callback
41 before_or_after: 0 => before, 1=> after
44 mdata_trace_callback (vlib_main_t * vm, u64 * c0, u64 * c1,
45 vlib_node_runtime_t * node,
46 vlib_frame_t * frame, int before_or_after)
49 mdata_main_t *mm = &mdata_main;
50 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
53 mdata_t *before, *modifies;
56 /* Input nodes don't have frames, etc. */
60 n_left_from = frame->n_vectors;
65 from = vlib_frame_vector_args (frame);
67 vlib_get_buffers (vm, from, bufs, n_left_from);
70 if (before_or_after == 1 /* after */ )
73 /* Resize the per-thread "before" vector to cover the current frame */
74 vec_reset_length (mm->before_per_thread[vm->thread_index]);
75 vec_validate (mm->before_per_thread[vm->thread_index], n_left_from - 1);
76 before = mm->before_per_thread[vm->thread_index];
77 before->node_index = ~0;
79 /* Before we call the dispatch fn, copy metadata. */
80 while (n_left_from > 0)
82 clib_memcpy_fast (before->mdata, b[0], sizeof (before->mdata));
91 /* Recover the metadata copy we saved a moment ago */
92 before = mm->before_per_thread[vm->thread_index];
94 /* We'd better have the same number of buffers... */
95 ASSERT (n_left_from == vec_len (before));
96 ASSERT (node->node_index);
98 clib_spinlock_lock_if_init (&mm->modify_lock);
101 * Resize the per-node accumulator vector as needed
102 * Paint the "no data" patter across any nodes we haven't seen yet
104 vec_validate_init_empty (mm->modifies, node->node_index, mdata_none);
105 modifies = vec_elt_at_index (mm->modifies, node->node_index);
106 modifies->node_index = node->node_index;
107 before = mm->before_per_thread[vm->thread_index];
110 while (n_left_from > 0)
114 /* Compare metadata before and after node dispatch fn */
115 for (i = 0; i < ARRAY_LEN (before->mdata); i++)
117 /* Mark mdata octet changed */
118 if (before->mdata[i] != after[i])
119 modifies->mdata[i] = 0xff;
127 clib_spinlock_unlock_if_init (&mm->modify_lock);
131 mdata_enable_disable (mdata_main_t * mmp, int enable_disable)
134 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
137 if (mmp->modify_lock == 0 && thread_main->n_vlib_mains > 1)
138 clib_spinlock_init (&mmp->modify_lock);
140 if (vec_len (mmp->before_per_thread) == 0)
142 mdata_none.node_index = ~0;
143 vec_validate (mmp->before_per_thread, vec_len (vlib_mains) - 1);
146 /* Reset the per-node accumulator, see vec_validate_init_empty above */
147 vec_reset_length (mmp->modifies);
149 for (i = 0; i < vec_len (vlib_mains); i++)
151 if (vlib_mains[i] == 0)
154 clib_callback_enable_disable
155 (vlib_mains[i]->vlib_node_runtime_perf_counter_cbs,
156 vlib_mains[i]->vlib_node_runtime_perf_counter_cb_tmp,
157 vlib_mains[i]->worker_thread_main_loop_callback_lock,
158 (void *) mdata_trace_callback, enable_disable);
164 static clib_error_t *
165 mdata_enable_disable_command_fn (vlib_main_t * vm,
166 unformat_input_t * input,
167 vlib_cli_command_t * cmd)
169 mdata_main_t *mmp = &mdata_main;
170 int enable_disable = 1;
174 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
176 if (unformat (input, "disable") || unformat (input, "off"))
178 if (unformat (input, "enable") || unformat (input, "on"))
184 rv = mdata_enable_disable (mmp, enable_disable);
192 return clib_error_return (0, "mdata_enable_disable returned %d", rv);
198 * This command enables or disables buffer metadata change tracking
201 * To enable buffer metadata change tracking:
202 *@cliexstart{buffer metadata tracking on}
206 *@cliexstart{buffer metadata tracking off}
212 VLIB_CLI_COMMAND (mdata_enable_disable_command, static) =
214 .path = "buffer metadata tracking",
215 .short_help = "buffer metadata tracking [on][off]",
216 .function = mdata_enable_disable_command_fn,
220 /* API message handler */
221 static void vl_api_mdata_enable_disable_t_handler
222 (vl_api_mdata_enable_disable_t * mp)
224 vl_api_mdata_enable_disable_reply_t *rmp;
225 mdata_main_t *mmp = &mdata_main;
228 rv = mdata_enable_disable (mmp, (int) (mp->enable_disable));
230 REPLY_MACRO (VL_API_MDATA_ENABLE_DISABLE_REPLY);
233 /* API definitions */
234 #include <mdata/mdata.api.c>
236 static clib_error_t *
237 mdata_init (vlib_main_t * vm)
239 mdata_main_t *mmp = &mdata_main;
240 clib_error_t *error = 0;
243 mmp->vnet_main = vnet_get_main ();
245 /* Add our API messages to the global name_crc hash table */
246 mmp->msg_id_base = setup_message_id_table ();
251 VLIB_INIT_FUNCTION (mdata_init);
254 VLIB_PLUGIN_REGISTER () =
256 .version = VPP_BUILD_VER,
257 .description = "Buffer metadata change tracker."
262 #define foreach_primary_metadata_field \
268 _(buffer_pool_index) \
271 _(current_config_index) \
274 #define foreach_opaque_metadata_field \
280 _(feature_arc_index) \
284 _(ip.save_protocol) \
289 _(ip.reass.next_index) \
290 _(ip.reass.error_next_index) \
291 _(ip.reass.owner_thread_index) \
292 _(ip.reass.ip_proto) \
293 _(ip.reass.l4_src_port) \
294 _(ip.reass.l4_dst_port) \
295 _(ip.reass.estimated_mtu) \
296 _(ip.reass.fragment_first) \
297 _(ip.reass.fragment_last) \
298 _(ip.reass.range_first) \
299 _(ip.reass.range_last) \
300 _(ip.reass.next_range_bi) \
301 _(ip.reass.ip6_frag_hdr_offset) \
305 _(mpls.save_rewrite_length) \
306 _(mpls.mpls_hdr_length) \
307 _(mpls.bier.n_bytes) \
308 _(l2.feature_bitmap) \
315 _(l2t.session_index) \
316 _(l2_classify.table_index) \
317 _(l2_classify.opaque_index) \
318 _(l2_classify.hash) \
321 _(ipsec.protect_index) \
323 _(map_t.map_domain_index) \
326 _(map_t.v6.frag_offset) \
327 _(map_t.v6.l4_offset) \
328 _(map_t.v6.l4_protocol) \
329 _(map_t.checksum_offset) \
332 _(ip_frag.next_index) \
334 _(cop.current_config_index) \
335 _(lisp.overlay_afi) \
336 _(tcp.connection_index) \
338 _(tcp.next_node_opaque) \
347 #define foreach_opaque2_metadata_field \
355 _(pg_replay_timestamp)
358 format_buffer_metadata_changes (u8 * s, va_list * args)
360 mdata_main_t *mm = va_arg (*args, mdata_main_t *);
361 int verbose = va_arg (*args, int);
364 vnet_buffer_opaque_t *o;
365 vnet_buffer_opaque2_t *o2;
370 clib_spinlock_lock_if_init (&mm->modify_lock);
372 for (i = 0; i < vec_len (mm->modifies); i++)
374 modifies = vec_elt_at_index (mm->modifies, i);
375 node = vlib_get_node (mm->vlib_main, i);
377 /* No data for this node? */
378 if (modifies->node_index == ~0)
381 s = format (s, "\n%v: no data\n", node->name);
385 /* We visited the node, but it may not have changed any metadata... */
386 for (j = 0; j < ARRAY_LEN (modifies->mdata); j++)
388 if (modifies->mdata[j])
391 s = format (s, "\n%v: no metadata changes\n", node->name);
395 /* Fields which the node modifies will be non-zero */
396 b = (vlib_buffer_t *) (modifies->mdata);
398 /* Dump primary metadata changes */
399 s = format (s, "\n%v: ", node->name);
402 #define _(n) if (b->n) {s = format (s, "%s ", #n); printed = 1;}
403 foreach_primary_metadata_field;
407 s = format (s, "no vlib_buffer_t metadata changes");
412 * Dump opaque union changes.
413 * Hopefully this will give folks a clue about opaque
414 * union data conflicts. That's the point of the exercise...
418 s = format (s, " vnet_buffer_t: ");
420 #define _(n) if (o->n) {s = format (s, "%s ", #n); printed = 1;}
421 foreach_opaque_metadata_field;
425 s = format (s, "no changes");
429 o2 = vnet_buffer2 (b);
431 s = format (s, " vnet_buffer2_t: ");
433 #define _(n) if (o2->n) {s = format (s, "%s ", #n); printed = 1;}
434 foreach_opaque2_metadata_field;
437 s = format (s, "no changes");
443 clib_spinlock_unlock_if_init (&mm->modify_lock);
448 static clib_error_t *
449 show_metadata_command_fn (vlib_main_t * vm,
450 unformat_input_t * input, vlib_cli_command_t * cmd)
454 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
456 if (unformat (input, "verbose %=", &verbose, 1))
462 vlib_cli_output (vm, "%U", format_buffer_metadata_changes, &mdata_main,
468 * This command displays buffer metadata change information
470 * How to display buffer metadata change information
471 *@cliexstart{show buffer metadata}
472 * ethernet-input: current_data current_length flags error
473 * vnet_buffer_t: l2_hdr_offset l3_hdr_offset
474 * vnet_buffer2_t: no changes
479 VLIB_CLI_COMMAND (show_metadata_command, static) =
481 .path = "show buffer metadata",
482 .short_help = "show buffer metadata",
483 .function = show_metadata_command_fn,
488 * fd.io coding-style-patch-verification: ON
491 * eval: (c-set-style "gnu")