FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
mdata.c
Go to the documentation of this file.
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
33 
35 
36 /** @file mdata.c
37  * buffer metadata change tracker
38  */
39 
41 
42 /** Metadata tracking callback
43  before_or_after: 0 => before, 1=> after
44 */
45 static void
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 
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 
78  goto after_pass;
79 
80  /* Resize the per-thread "before" vector to cover the current frame */
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 
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 
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)
146 
147  if (vec_len (mmp->before_per_thread) == 0)
148  {
149  mdata_none.node_index = ~0;
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 < vec_len (vlib_mains); i++)
157  {
158  if (vlib_mains[i] == 0)
159  continue;
160 
162  (&vlib_mains[i]->vlib_node_runtime_perf_callbacks,
163  mdata_trace_callback, enable_disable);
164  }
165 
166  return rv;
167 }
168 
169 static clib_error_t *
171  unformat_input_t * input,
172  vlib_cli_command_t * cmd)
173 {
174  mdata_main_t *mmp = &mdata_main;
175  int enable_disable = 1;
176 
177  int rv;
178 
180  {
181  if (unformat (input, "disable") || unformat (input, "off"))
182  enable_disable = 0;
183  if (unformat (input, "enable") || unformat (input, "on"))
184  enable_disable = 1;
185  else
186  break;
187  }
188 
189  rv = mdata_enable_disable (mmp, enable_disable);
190 
191  switch (rv)
192  {
193  case 0:
194  break;
195 
196  default:
197  return clib_error_return (0, "mdata_enable_disable returned %d", rv);
198  }
199  return 0;
200 }
201 
202 /*?
203  * This command enables or disables buffer metadata change tracking
204  *
205  *@cliexpar
206  * To enable buffer metadata change tracking:
207  *@cliexstart{buffer metadata tracking on}
208  * Tracking enabled
209  *@cliexend
210  *
211  *@cliexstart{buffer metadata tracking off}
212  * Tracking disabled
213  *@cliexend
214 ?*/
215 
216 /* *INDENT-OFF* */
217 VLIB_CLI_COMMAND (mdata_enable_disable_command, static) =
218 {
219  .path = "buffer metadata tracking",
220  .short_help = "buffer metadata tracking [on][off]",
222 };
223 /* *INDENT-ON* */
224 
225 /* API message handler */
228 {
229  vl_api_mdata_enable_disable_reply_t *rmp;
230  mdata_main_t *mmp = &mdata_main;
231  int rv;
232 
233  rv = mdata_enable_disable (mmp, (int) (mp->enable_disable));
234 
235  REPLY_MACRO (VL_API_MDATA_ENABLE_DISABLE_REPLY);
236 }
237 
238 /* API definitions */
239 #include <mdata/mdata.api.c>
240 
241 static clib_error_t *
243 {
244  mdata_main_t *mmp = &mdata_main;
245  clib_error_t *error = 0;
246 
247  mmp->vlib_main = vm;
248  mmp->vnet_main = vnet_get_main ();
249 
250  /* Add our API messages to the global name_crc hash table */
252 
253  return error;
254 }
255 
257 
258 /* *INDENT-OFF* */
260 {
261  .version = VPP_BUILD_VER,
262  .description = "Buffer metadata change tracker."
263 };
264 /* *INDENT-ON* */
265 
266 
267 #define foreach_primary_metadata_field \
268 _(current_data) \
269 _(current_length) \
270 _(flags) \
271 _(flow_id) \
272 _(ref_count) \
273 _(buffer_pool_index) \
274 _(error) \
275 _(next_buffer) \
276 _(current_config_index) \
277 _(punt_reason)
278 
279 #define foreach_opaque_metadata_field \
280 _(sw_if_index[0]) \
281 _(sw_if_index[1]) \
282 _(l2_hdr_offset) \
283 _(l3_hdr_offset) \
284 _(l4_hdr_offset) \
285 _(feature_arc_index) \
286 _(ip.adj_index[0]) \
287 _(ip.adj_index[1]) \
288 _(ip.flow_hash) \
289 _(ip.save_protocol) \
290 _(ip.fib_index) \
291 _(ip.icmp.type) \
292 _(ip.icmp.code) \
293 _(ip.icmp.data) \
294 _(ip.reass.next_index) \
295 _(ip.reass.error_next_index) \
296 _(ip.reass.owner_thread_index) \
297 _(ip.reass.ip_proto) \
298 _(ip.reass.l4_src_port) \
299 _(ip.reass.l4_dst_port) \
300 _(ip.reass.estimated_mtu) \
301 _(ip.reass.fragment_first) \
302 _(ip.reass.fragment_last) \
303 _(ip.reass.range_first) \
304 _(ip.reass.range_last) \
305 _(ip.reass.next_range_bi) \
306 _(ip.reass.ip6_frag_hdr_offset) \
307 _(mpls.ttl) \
308 _(mpls.exp) \
309 _(mpls.first) \
310 _(mpls.save_rewrite_length) \
311 _(mpls.mpls_hdr_length) \
312 _(mpls.bier.n_bytes) \
313 _(l2.feature_bitmap) \
314 _(l2.bd_index) \
315 _(l2.l2fib_sn) \
316 _(l2.l2_len) \
317 _(l2.shg) \
318 _(l2.bd_age) \
319 _(l2t.next_index) \
320 _(l2t.session_index) \
321 _(l2_classify.table_index) \
322 _(l2_classify.opaque_index) \
323 _(l2_classify.hash) \
324 _(policer.index) \
325 _(ipsec.sad_index) \
326 _(ipsec.protect_index) \
327 _(map.mtu) \
328 _(map_t.map_domain_index) \
329 _(map_t.v6.saddr) \
330 _(map_t.v6.daddr) \
331 _(map_t.v6.frag_offset) \
332 _(map_t.v6.l4_offset) \
333 _(map_t.v6.l4_protocol) \
334 _(map_t.checksum_offset) \
335 _(map_t.mtu) \
336 _(ip_frag.mtu) \
337 _(ip_frag.next_index) \
338 _(ip_frag.flags) \
339 _(cop.current_config_index) \
340 _(lisp.overlay_afi) \
341 _(tcp.connection_index) \
342 _(tcp.seq_number) \
343 _(tcp.next_node_opaque) \
344 _(tcp.seq_end) \
345 _(tcp.ack_number) \
346 _(tcp.hdr_offset) \
347 _(tcp.data_offset) \
348 _(tcp.data_len) \
349 _(tcp.flags) \
350 _(snat.flags)
351 
352 #define foreach_opaque2_metadata_field \
353 _(qos.bits) \
354 _(qos.source) \
355 _(loop_counter) \
356 _(gbp.flags) \
357 _(gbp.sclass) \
358 _(gso_size) \
359 _(gso_l4_hdr_sz) \
360 _(pg_replay_timestamp)
361 
362 static u8 *
363 format_buffer_metadata_changes (u8 * s, va_list * args)
364 {
365  mdata_main_t *mm = va_arg (*args, mdata_main_t *);
366  int verbose = va_arg (*args, int);
367  mdata_t *modifies;
368  vlib_buffer_t *b;
371  vlib_node_t *node;
372  int i, j;
373  int printed;
374 
376 
377  for (i = 0; i < vec_len (mm->modifies); i++)
378  {
379  modifies = vec_elt_at_index (mm->modifies, i);
380  node = vlib_get_node (mm->vlib_main, i);
381 
382  /* No data for this node? */
383  if (modifies->node_index == ~0)
384  {
385  if (verbose)
386  s = format (s, "\n%v: no data\n", node->name);
387  continue;
388  }
389 
390  /* We visited the node, but it may not have changed any metadata... */
391  for (j = 0; j < ARRAY_LEN (modifies->mdata); j++)
392  {
393  if (modifies->mdata[j])
394  goto found;
395  }
396  s = format (s, "\n%v: no metadata changes\n", node->name);
397  continue;
398 
399  found:
400  /* Fields which the node modifies will be non-zero */
401  b = (vlib_buffer_t *) (modifies->mdata);
402 
403  /* Dump primary metadata changes */
404  s = format (s, "\n%v: ", node->name);
405 
406  printed = 0;
407 #define _(n) if (b->n) {s = format (s, "%s ", #n); printed = 1;}
409 #undef _
410 
411  if (printed == 0)
412  s = format (s, "no vlib_buffer_t metadata changes");
413 
414  vec_add1 (s, '\n');
415 
416  /*
417  * Dump opaque union changes.
418  * Hopefully this will give folks a clue about opaque
419  * union data conflicts. That's the point of the exercise...
420  */
421  o = vnet_buffer (b);
422  printed = 0;
423  s = format (s, " vnet_buffer_t: ");
424 
425 #define _(n) if (o->n) {s = format (s, "%s ", #n); printed = 1;}
427 #undef _
428 
429  if (printed == 0)
430  s = format (s, "no changes");
431 
432  vec_add1 (s, '\n');
433 
434  o2 = vnet_buffer2 (b);
435  printed = 0;
436  s = format (s, " vnet_buffer2_t: ");
437 
438 #define _(n) if (o2->n) {s = format (s, "%s ", #n); printed = 1;}
440 #undef _
441  if (printed == 0)
442  s = format (s, "no changes");
443 
444  vec_add1 (s, '\n');
445 
446  }
447 
449 
450  return s;
451 }
452 
453 static clib_error_t *
455  unformat_input_t * input, vlib_cli_command_t * cmd)
456 {
457  int verbose = 0;
458 
460  {
461  if (unformat (input, "verbose %=", &verbose, 1))
462  ;
463  else
464  break;
465  }
466 
467  vlib_cli_output (vm, "%U", format_buffer_metadata_changes, &mdata_main,
468  verbose);
469  return 0;
470 }
471 
472 /*?
473  * This command displays buffer metadata change information
474  *@cliexpar
475  * How to display buffer metadata change information
476  *@cliexstart{show buffer metadata}
477  * ethernet-input: current_data current_length flags error
478  * vnet_buffer_t: l2_hdr_offset l3_hdr_offset
479  * vnet_buffer2_t: no changes
480  *@cliexend
481 ?*/
482 
483 /* *INDENT-OFF* */
484 VLIB_CLI_COMMAND (show_metadata_command, static) =
485 {
486  .path = "show buffer metadata",
487  .short_help = "show buffer metadata",
488  .function = show_metadata_command_fn,
489 };
490 /* *INDENT-OFF* */
491 
492 /*
493  * fd.io coding-style-patch-verification: ON
494  *
495  * Local Variables:
496  * eval: (c-set-style "gnu")
497  * End:
498  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
static void vl_api_mdata_enable_disable_t_handler(vl_api_mdata_enable_disable_t *mp)
Definition: mdata.c:227
u16 msg_id_base
API message ID base.
Definition: mdata.h:41
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define vnet_buffer2(b)
Definition: buffer.h:482
#define foreach_primary_metadata_field
Definition: mdata.c:267
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:127
u32 thread_index
Definition: main.h:249
struct vlib_main_t * vm
Definition: main.h:93
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
mdata_main_t mdata_main
Definition: mdata.c:34
static u8 * format_buffer_metadata_changes(u8 *s, va_list *args)
Definition: mdata.c:363
vlib_main_t ** vlib_mains
Definition: buffer.c:332
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:89
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_spinlock_t modify_lock
Spinlock to protect modified metadata by node.
Definition: mdata.h:47
static void mdata_trace_callback(vlib_node_runtime_perf_callback_data_t *data, vlib_node_runtime_perf_callback_args_t *args)
Metadata tracking callback before_or_after: 0 => before, 1=> after.
Definition: mdata.c:46
VLIB_PLUGIN_REGISTER()
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
u8 mdata[128]
buffer metadata, cast to vlib_buffer_t as needed
Definition: mdata.h:35
int mdata_enable_disable(mdata_main_t *mmp, int enable_disable)
Definition: mdata.c:138
unsigned int u32
Definition: types.h:88
vnet_main_t * vnet_main
Definition: mdata.h:54
#define VLIB_FRAME_SIZE
Definition: node.h:377
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:63
Definition: mdata.h:30
struct _unformat_input_t unformat_input_t
#define PREDICT_FALSE(x)
Definition: clib.h:120
#define REPLY_MACRO(t)
#define clib_callback_data_enable_disable(set_, fp_, ena_)
Enable/Disable the specified callback.
u32 node_index
Node index.
Definition: node.h:487
u8 * name
Definition: node.h:263
API to enable / disable mdata on an interface.
Definition: mdata.api:37
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
u16 n_vectors
Definition: node.h:396
mdata_t ** before_per_thread
Per-thread buffer metadata before calling node fcn.
Definition: mdata.h:44
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
Callback multiplex scheme.
#define ARRAY_LEN(x)
Definition: clib.h:67
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
#define foreach_opaque_metadata_field
Definition: mdata.c:279
#define ASSERT(truth)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
static mdata_t mdata_none
Definition: mdata.c:40
static clib_error_t * mdata_init(vlib_main_t *vm)
Definition: mdata.c:242
u32 node_index
Node index, ~0 means no data from this run.
Definition: mdata.h:33
#define foreach_opaque2_metadata_field
Definition: mdata.c:352
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1583
VLIB buffer representation.
Definition: buffer.h:102
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
vlib_main_t * vlib_main
Definition: mdata.h:53
vlib_node_runtime_perf_call_type_t call_type
Definition: main.h:98
#define vnet_buffer(b)
Definition: buffer.h:417
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
vlib_node_runtime_t * node
Definition: main.h:94
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:85
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:2804
mdata_t * modifies
Modified metadata by node.
Definition: mdata.h:50
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:280
static clib_error_t * mdata_enable_disable_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: mdata.c:170
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:556
static clib_error_t * show_metadata_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: mdata.c:454
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:104
buffer metadata change tracker definitions
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171