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;
36 * buffer metadata change tracker
39 static mdata_t mdata_none;
41 /** Metadata tracking callback
42 before_or_after: 0 => before, 1=> after
45 mdata_trace_callback (vlib_main_t * vm, u64 * c0, u64 * c1,
46 vlib_node_runtime_t * node,
47 vlib_frame_t * frame, int before_or_after)
50 mdata_main_t *mm = &mdata_main;
51 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
54 mdata_t *before, *modifies;
57 /* Input nodes don't have frames, etc. */
61 n_left_from = frame->n_vectors;
66 from = vlib_frame_vector_args (frame);
68 vlib_get_buffers (vm, from, bufs, n_left_from);
71 if (before_or_after == 1 /* after */ )
74 /* Resize the per-thread "before" vector to cover the current frame */
75 vec_reset_length (mm->before_per_thread[vm->thread_index]);
76 vec_validate (mm->before_per_thread[vm->thread_index], n_left_from - 1);
77 before = mm->before_per_thread[vm->thread_index];
78 before->node_index = ~0;
80 /* Before we call the dispatch fn, copy metadata. */
81 while (n_left_from > 0)
83 clib_memcpy_fast (before->mdata, b[0], sizeof (before->mdata));
92 /* Recover the metadata copy we saved a moment ago */
93 before = mm->before_per_thread[vm->thread_index];
95 /* We'd better have the same number of buffers... */
96 ASSERT (n_left_from == vec_len (before));
97 ASSERT (node->node_index);
99 clib_spinlock_lock_if_init (&mm->modify_lock);
102 * Resize the per-node accumulator vector as needed
103 * Paint the "no data" patter across any nodes we haven't seen yet
105 vec_validate_init_empty (mm->modifies, node->node_index, mdata_none);
106 modifies = vec_elt_at_index (mm->modifies, node->node_index);
107 modifies->node_index = node->node_index;
108 before = mm->before_per_thread[vm->thread_index];
111 while (n_left_from > 0)
115 /* Compare metadata before and after node dispatch fn */
116 for (i = 0; i < ARRAY_LEN (before->mdata); i++)
118 /* Mark mdata octet changed */
119 if (before->mdata[i] != after[i])
120 modifies->mdata[i] = 0xff;
128 clib_spinlock_unlock_if_init (&mm->modify_lock);
132 mdata_enable_disable (mdata_main_t * mmp, int enable_disable)
135 vlib_thread_main_t *thread_main = vlib_get_thread_main ();
138 if (mmp->modify_lock == 0 && thread_main->n_vlib_mains > 1)
139 clib_spinlock_init (&mmp->modify_lock);
141 if (vec_len (mmp->before_per_thread) == 0)
143 mdata_none.node_index = ~0;
144 vec_validate (mmp->before_per_thread, vec_len (vlib_mains) - 1);
147 /* Reset the per-node accumulator, see vec_validate_init_empty above */
148 vec_reset_length (mmp->modifies);
150 for (i = 0; i < vec_len (vlib_mains); i++)
152 if (vlib_mains[i] == 0)
155 clib_callback_enable_disable
156 (vlib_mains[i]->vlib_node_runtime_perf_counter_cbs,
157 vlib_mains[i]->vlib_node_runtime_perf_counter_cb_tmp,
158 vlib_mains[i]->worker_thread_main_loop_callback_lock,
159 (void *) mdata_trace_callback, enable_disable);
165 static clib_error_t *
166 mdata_enable_disable_command_fn (vlib_main_t * vm,
167 unformat_input_t * input,
168 vlib_cli_command_t * cmd)
170 mdata_main_t *mmp = &mdata_main;
171 int enable_disable = 1;
175 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
177 if (unformat (input, "disable") || unformat (input, "off"))
179 if (unformat (input, "enable") || unformat (input, "on"))
185 rv = mdata_enable_disable (mmp, enable_disable);
193 return clib_error_return (0, "mdata_enable_disable returned %d", rv);
199 * This command enables or disables buffer metadata change tracking
202 * To enable buffer metadata change tracking:
203 *@cliexstart{buffer metadata tracking on}
207 *@cliexstart{buffer metadata tracking off}
213 VLIB_CLI_COMMAND (mdata_enable_disable_command, static) =
215 .path = "buffer metadata tracking",
216 .short_help = "buffer metadata tracking [on][off]",
217 .function = mdata_enable_disable_command_fn,
221 /* API message handler */
222 static void vl_api_mdata_enable_disable_t_handler
223 (vl_api_mdata_enable_disable_t * mp)
225 vl_api_mdata_enable_disable_reply_t *rmp;
226 mdata_main_t *mmp = &mdata_main;
229 rv = mdata_enable_disable (mmp, (int) (mp->enable_disable));
231 REPLY_MACRO (VL_API_MDATA_ENABLE_DISABLE_REPLY);
234 /* API definitions */
235 #include <mdata/mdata.api.c>
237 static clib_error_t *
238 mdata_init (vlib_main_t * vm)
240 mdata_main_t *mmp = &mdata_main;
241 clib_error_t *error = 0;
244 mmp->vnet_main = vnet_get_main ();
246 /* Add our API messages to the global name_crc hash table */
247 mmp->msg_id_base = setup_message_id_table ();
252 VLIB_INIT_FUNCTION (mdata_init);
255 VLIB_PLUGIN_REGISTER () =
257 .version = VPP_BUILD_VER,
258 .description = "Buffer metadata change tracker."
263 #define foreach_primary_metadata_field \
269 _(buffer_pool_index) \
272 _(current_config_index) \
275 #define foreach_opaque_metadata_field \
281 _(feature_arc_index) \
285 _(ip.save_protocol) \
290 _(ip.reass.next_index) \
291 _(ip.reass.error_next_index) \
292 _(ip.reass.owner_thread_index) \
293 _(ip.reass.ip_proto) \
294 _(ip.reass.l4_src_port) \
295 _(ip.reass.l4_dst_port) \
296 _(ip.reass.estimated_mtu) \
297 _(ip.reass.fragment_first) \
298 _(ip.reass.fragment_last) \
299 _(ip.reass.range_first) \
300 _(ip.reass.range_last) \
301 _(ip.reass.next_range_bi) \
302 _(ip.reass.ip6_frag_hdr_offset) \
306 _(mpls.save_rewrite_length) \
307 _(mpls.mpls_hdr_length) \
308 _(mpls.bier.n_bytes) \
309 _(l2.feature_bitmap) \
316 _(l2t.session_index) \
317 _(l2_classify.table_index) \
318 _(l2_classify.opaque_index) \
319 _(l2_classify.hash) \
322 _(ipsec.protect_index) \
324 _(map_t.map_domain_index) \
327 _(map_t.v6.frag_offset) \
328 _(map_t.v6.l4_offset) \
329 _(map_t.v6.l4_protocol) \
330 _(map_t.checksum_offset) \
333 _(ip_frag.next_index) \
335 _(cop.current_config_index) \
336 _(lisp.overlay_afi) \
337 _(tcp.connection_index) \
339 _(tcp.next_node_opaque) \
348 #define foreach_opaque2_metadata_field \
356 _(pg_replay_timestamp)
359 format_buffer_metadata_changes (u8 * s, va_list * args)
361 mdata_main_t *mm = va_arg (*args, mdata_main_t *);
362 int verbose = va_arg (*args, int);
365 vnet_buffer_opaque_t *o;
366 vnet_buffer_opaque2_t *o2;
371 clib_spinlock_lock_if_init (&mm->modify_lock);
373 for (i = 0; i < vec_len (mm->modifies); i++)
375 modifies = vec_elt_at_index (mm->modifies, i);
376 node = vlib_get_node (mm->vlib_main, i);
378 /* No data for this node? */
379 if (modifies->node_index == ~0)
382 s = format (s, "\n%v: no data\n", node->name);
386 /* We visited the node, but it may not have changed any metadata... */
387 for (j = 0; j < ARRAY_LEN (modifies->mdata); j++)
389 if (modifies->mdata[j])
392 s = format (s, "\n%v: no metadata changes\n", node->name);
396 /* Fields which the node modifies will be non-zero */
397 b = (vlib_buffer_t *) (modifies->mdata);
399 /* Dump primary metadata changes */
400 s = format (s, "\n%v: ", node->name);
403 #define _(n) if (b->n) {s = format (s, "%s ", #n); printed = 1;}
404 foreach_primary_metadata_field;
408 s = format (s, "no vlib_buffer_t metadata changes");
413 * Dump opaque union changes.
414 * Hopefully this will give folks a clue about opaque
415 * union data conflicts. That's the point of the exercise...
419 s = format (s, " vnet_buffer_t: ");
421 #define _(n) if (o->n) {s = format (s, "%s ", #n); printed = 1;}
422 foreach_opaque_metadata_field;
426 s = format (s, "no changes");
430 o2 = vnet_buffer2 (b);
432 s = format (s, " vnet_buffer2_t: ");
434 #define _(n) if (o2->n) {s = format (s, "%s ", #n); printed = 1;}
435 foreach_opaque2_metadata_field;
438 s = format (s, "no changes");
444 clib_spinlock_unlock_if_init (&mm->modify_lock);
449 static clib_error_t *
450 show_metadata_command_fn (vlib_main_t * vm,
451 unformat_input_t * input, vlib_cli_command_t * cmd)
455 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
457 if (unformat (input, "verbose %=", &verbose, 1))
463 vlib_cli_output (vm, "%U", format_buffer_metadata_changes, &mdata_main,
469 * This command displays buffer metadata change information
471 * How to display buffer metadata change information
472 *@cliexstart{show buffer metadata}
473 * ethernet-input: current_data current_length flags error
474 * vnet_buffer_t: l2_hdr_offset l3_hdr_offset
475 * vnet_buffer2_t: no changes
480 VLIB_CLI_COMMAND (show_metadata_command, static) =
482 .path = "show buffer metadata",
483 .short_help = "show buffer metadata",
484 .function = show_metadata_command_fn,
489 * fd.io coding-style-patch-verification: ON
492 * eval: (c-set-style "gnu")