FD.io VPP  v21.06-3-gbb25fbf28
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;
52  u32 *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 
73 
75  b = bufs;
76 
78  goto after_pass;
79 
80  /* Resize the per-thread "before" vector to cover the current frame */
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 < vlib_get_n_threads (); i++)
157  {
159  if (ovm == 0)
160  continue;
161 
164  enable_disable);
165  }
166 
167  return rv;
168 }
169 
170 static clib_error_t *
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 
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 /* *INDENT-OFF* */
219 {
220  .path = "buffer metadata tracking",
221  .short_help = "buffer metadata tracking [on][off]",
223 };
224 /* *INDENT-ON* */
225 
226 /* API message handler */
229 {
230  vl_api_mdata_enable_disable_reply_t *rmp;
231  mdata_main_t *mmp = &mdata_main;
232  int rv;
233 
234  rv = mdata_enable_disable (mmp, (int) (mp->enable_disable));
235 
236  REPLY_MACRO (VL_API_MDATA_ENABLE_DISABLE_REPLY);
237 }
238 
239 /* API definitions */
240 #include <mdata/mdata.api.c>
241 
242 static clib_error_t *
244 {
245  mdata_main_t *mmp = &mdata_main;
246  clib_error_t *error = 0;
247 
248  mmp->vlib_main = vm;
249  mmp->vnet_main = vnet_get_main ();
250 
251  /* Add our API messages to the global name_crc hash table */
253 
254  return error;
255 }
256 
258 
259 /* *INDENT-OFF* */
261 {
262  .version = VPP_BUILD_VER,
263  .description = "Buffer metadata change tracker."
264 };
265 /* *INDENT-ON* */
266 
267 
268 #define foreach_primary_metadata_field \
269 _(current_data) \
270 _(current_length) \
271 _(flags) \
272 _(flow_id) \
273 _(ref_count) \
274 _(buffer_pool_index) \
275 _(error) \
276 _(next_buffer) \
277 _(current_config_index) \
278 _(punt_reason)
279 
280 #define foreach_opaque_metadata_field \
281 _(sw_if_index[0]) \
282 _(sw_if_index[1]) \
283 _(l2_hdr_offset) \
284 _(l3_hdr_offset) \
285 _(l4_hdr_offset) \
286 _(feature_arc_index) \
287 _(ip.adj_index[0]) \
288 _(ip.adj_index[1]) \
289 _(ip.flow_hash) \
290 _(ip.save_protocol) \
291 _(ip.fib_index) \
292 _(ip.icmp.type) \
293 _(ip.icmp.code) \
294 _(ip.icmp.data) \
295 _(ip.reass.next_index) \
296 _(ip.reass.error_next_index) \
297 _(ip.reass.owner_thread_index) \
298 _(ip.reass.ip_proto) \
299 _(ip.reass.l4_src_port) \
300 _(ip.reass.l4_dst_port) \
301 _(ip.reass.estimated_mtu) \
302 _(ip.reass.fragment_first) \
303 _(ip.reass.fragment_last) \
304 _(ip.reass.range_first) \
305 _(ip.reass.range_last) \
306 _(ip.reass.next_range_bi) \
307 _(ip.reass.ip6_frag_hdr_offset) \
308 _(mpls.ttl) \
309 _(mpls.exp) \
310 _(mpls.first) \
311 _(mpls.save_rewrite_length) \
312 _(mpls.mpls_hdr_length) \
313 _(mpls.bier.n_bytes) \
314 _(l2.feature_bitmap) \
315 _(l2.bd_index) \
316 _(l2.l2fib_sn) \
317 _(l2.l2_len) \
318 _(l2.shg) \
319 _(l2.bd_age) \
320 _(l2t.next_index) \
321 _(l2t.session_index) \
322 _(l2_classify.table_index) \
323 _(l2_classify.opaque_index) \
324 _(l2_classify.hash) \
325 _(policer.index) \
326 _(ipsec.sad_index) \
327 _(ipsec.protect_index) \
328 _(map.mtu) \
329 _(map_t.map_domain_index) \
330 _(map_t.v6.saddr) \
331 _(map_t.v6.daddr) \
332 _(map_t.v6.frag_offset) \
333 _(map_t.v6.l4_offset) \
334 _(map_t.v6.l4_protocol) \
335 _(map_t.checksum_offset) \
336 _(map_t.mtu) \
337 _(ip_frag.mtu) \
338 _(ip_frag.next_index) \
339 _(ip_frag.flags) \
340 _(cop.current_config_index) \
341 _(lisp.overlay_afi) \
342 _(tcp.connection_index) \
343 _(tcp.seq_number) \
344 _(tcp.next_node_opaque) \
345 _(tcp.seq_end) \
346 _(tcp.ack_number) \
347 _(tcp.hdr_offset) \
348 _(tcp.data_offset) \
349 _(tcp.data_len) \
350 _(tcp.flags) \
351 _(snat.flags)
352 
353 #define foreach_opaque2_metadata_field \
354 _(qos.bits) \
355 _(qos.source) \
356 _(loop_counter) \
357 _(gbp.flags) \
358 _(gbp.sclass) \
359 _(gso_size) \
360 _(gso_l4_hdr_sz) \
361 _(pg_replay_timestamp)
362 
363 static u8 *
364 format_buffer_metadata_changes (u8 * s, va_list * args)
365 {
366  mdata_main_t *mm = va_arg (*args, mdata_main_t *);
367  int verbose = va_arg (*args, int);
368  mdata_t *modifies;
369  vlib_buffer_t *b;
372  vlib_node_t *node;
373  int i, j;
374  int printed;
375 
377 
378  for (i = 0; i < vec_len (mm->modifies); i++)
379  {
380  modifies = vec_elt_at_index (mm->modifies, i);
381  node = vlib_get_node (mm->vlib_main, i);
382 
383  /* No data for this node? */
384  if (modifies->node_index == ~0)
385  {
386  if (verbose)
387  s = format (s, "\n%v: no data\n", node->name);
388  continue;
389  }
390 
391  /* We visited the node, but it may not have changed any metadata... */
392  for (j = 0; j < ARRAY_LEN (modifies->mdata); j++)
393  {
394  if (modifies->mdata[j])
395  goto found;
396  }
397  s = format (s, "\n%v: no metadata changes\n", node->name);
398  continue;
399 
400  found:
401  /* Fields which the node modifies will be non-zero */
402  b = (vlib_buffer_t *) (modifies->mdata);
403 
404  /* Dump primary metadata changes */
405  s = format (s, "\n%v: ", node->name);
406 
407  printed = 0;
408 #define _(n) if (b->n) {s = format (s, "%s ", #n); printed = 1;}
410 #undef _
411 
412  if (printed == 0)
413  s = format (s, "no vlib_buffer_t metadata changes");
414 
415  vec_add1 (s, '\n');
416 
417  /*
418  * Dump opaque union changes.
419  * Hopefully this will give folks a clue about opaque
420  * union data conflicts. That's the point of the exercise...
421  */
422  o = vnet_buffer (b);
423  printed = 0;
424  s = format (s, " vnet_buffer_t: ");
425 
426 #define _(n) if (o->n) {s = format (s, "%s ", #n); printed = 1;}
428 #undef _
429 
430  if (printed == 0)
431  s = format (s, "no changes");
432 
433  vec_add1 (s, '\n');
434 
435  o2 = vnet_buffer2 (b);
436  printed = 0;
437  s = format (s, " vnet_buffer2_t: ");
438 
439 #define _(n) if (o2->n) {s = format (s, "%s ", #n); printed = 1;}
441 #undef _
442  if (printed == 0)
443  s = format (s, "no changes");
444 
445  vec_add1 (s, '\n');
446 
447  }
448 
450 
451  return s;
452 }
453 
454 static clib_error_t *
456  unformat_input_t * input, vlib_cli_command_t * cmd)
457 {
458  int verbose = 0;
459 
461  {
462  if (unformat (input, "verbose %=", &verbose, 1))
463  ;
464  else
465  break;
466  }
467 
469  verbose);
470  return 0;
471 }
472 
473 /*?
474  * This command displays buffer metadata change information
475  *@cliexpar
476  * How to display buffer metadata change information
477  *@cliexstart{show buffer metadata}
478  * ethernet-input: current_data current_length flags error
479  * vnet_buffer_t: l2_hdr_offset l3_hdr_offset
480  * vnet_buffer2_t: no changes
481  *@cliexend
482 ?*/
483 
484 /* *INDENT-OFF* */
486 {
487  .path = "show buffer metadata",
488  .short_help = "show buffer metadata",
489  .function = show_metadata_command_fn,
490 };
491 /* *INDENT-OFF* */
492 
493 /*
494  * fd.io coding-style-patch-verification: ON
495  *
496  * Local Variables:
497  * eval: (c-set-style "gnu")
498  * End:
499  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
vl_api_mdata_enable_disable_t::enable_disable
bool enable_disable
Definition: mdata.api:45
clib_spinlock_init
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
mdata_main_t::msg_id_base
u16 msg_id_base
API message ID base.
Definition: mdata.h:41
mdata_main_t::vlib_main
vlib_main_t * vlib_main
Definition: mdata.h:53
mdata_enable_disable_command_fn
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:171
api.h
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
vnet_buffer_opaque2_t
Definition: buffer.h:440
mdata_main_t::modify_lock
clib_spinlock_t modify_lock
Spinlock to protect modified metadata by node.
Definition: mdata.h:47
clib_spinlock_lock_if_init
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:106
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
mdata_trace_callback
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
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
mdata_none
static mdata_t mdata_none
Definition: mdata.c:40
show_metadata_command_fn
static clib_error_t * show_metadata_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: mdata.c:455
mdata_main_t
Definition: mdata.h:38
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
api.h
vnet_buffer2
#define vnet_buffer2(b)
Definition: buffer.h:499
unformat_input_t
struct _unformat_input_t unformat_input_t
VLIB_NODE_RUNTIME_PERF_RESET
@ VLIB_NODE_RUNTIME_PERF_RESET
Definition: main.h:70
vlib_frame_t
Definition: node.h:372
mdata_t
Definition: mdata.h:30
clib_memcpy_fast
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
mdata_init
static clib_error_t * mdata_init(vlib_main_t *vm)
Definition: mdata.c:243
error
Definition: cJSON.c:88
vl_api_mdata_enable_disable_t_handler
static void vl_api_mdata_enable_disable_t_handler(vl_api_mdata_enable_disable_t *mp)
Definition: mdata.c:228
mdata_main
mdata_main_t mdata_main
Definition: mdata.c:34
clib_callback_data_enable_disable
#define clib_callback_data_enable_disable(set_, fp_, ena_)
Enable/Disable the specified callback.
Definition: callback_data.h:189
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:451
vlib_node_runtime_perf_callback_args_t::node
vlib_node_runtime_t * node
Definition: main.h:76
vlib_thread_main_t::n_vlib_mains
u32 n_vlib_mains
Definition: threads.h:283
foreach_opaque_metadata_field
#define foreach_opaque_metadata_field
Definition: mdata.c:280
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
foreach_primary_metadata_field
#define foreach_primary_metadata_field
Definition: mdata.c:268
mdata_enable_disable_command
static vlib_cli_command_t mdata_enable_disable_command
(constructor) VLIB_CLI_COMMAND (mdata_enable_disable_command)
Definition: mdata.c:218
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
mdata_t::mdata
u8 mdata[128]
buffer metadata, cast to vlib_buffer_t as needed
Definition: mdata.h:35
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
mdata_main_t::vnet_main
vnet_main_t * vnet_main
Definition: mdata.h:54
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
callback_data.h
Callback multiplex scheme.
vlib_node_runtime_perf_callback_data_t
Definition: main.h:89
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:213
vlib_get_node
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
vlib_node_runtime_perf_callback_args_t::vm
struct vlib_main_t * vm
Definition: main.h:75
plugin.h
vlib_main_t::vlib_node_runtime_perf_callbacks
vlib_node_runtime_perf_callback_set_t vlib_node_runtime_perf_callbacks
Definition: main.h:134
data
u8 data[128]
Definition: ipsec_types.api:92
mdata_main_t::before_per_thread
mdata_t ** before_per_thread
Per-thread buffer metadata before calling node fcn.
Definition: mdata.h:44
vlib_node_runtime_perf_callback_args_t::frame
vlib_frame_t * frame
Definition: main.h:77
vnet_buffer_opaque_t
Definition: buffer.h:149
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
vlib_node_runtime_perf_callback_args_t::call_type
vlib_node_runtime_perf_call_type_t call_type
Definition: main.h:80
vec_validate_init_empty
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header,...
Definition: vec.h:570
format_buffer_metadata_changes
static u8 * format_buffer_metadata_changes(u8 *s, va_list *args)
Definition: mdata.c:364
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vlib_thread_main_t
Definition: threads.h:264
mdata_t::node_index
u32 node_index
Node index, ~0 means no data from this run.
Definition: mdata.h:33
api_helper_macros.h
vlib_get_main_by_index
static vlib_main_t * vlib_get_main_by_index(u32 thread_index)
Definition: global_funcs.h:29
vlib_main_t
Definition: main.h:102
vlib_get_n_threads
static u32 vlib_get_n_threads()
Definition: global_funcs.h:23
vlib_node_t
Definition: node.h:247
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
mdata_main_t::modifies
mdata_t * modifies
Modified metadata by node.
Definition: mdata.h:50
show_metadata_command
static vlib_cli_command_t show_metadata_command
(constructor) VLIB_CLI_COMMAND (show_metadata_command)
Definition: mdata.c:485
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
mdata_enable_disable
int mdata_enable_disable(mdata_main_t *mmp, int enable_disable)
Definition: mdata.c:138
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
foreach_opaque2_metadata_field
#define foreach_opaque2_metadata_field
Definition: mdata.c:353
vlib_node_runtime_t
Definition: node.h:454
clib_spinlock_unlock_if_init
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:129
mdata.h
vlib_cli_command_t
Definition: cli.h:92
vlib_node_runtime_perf_callback_args_t
Definition: main.h:73
from
from
Definition: nat44_ei_hairpinning.c:415
vlib_get_thread_main
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:56
VLIB_NODE_RUNTIME_PERF_AFTER
@ VLIB_NODE_RUNTIME_PERF_AFTER
Definition: main.h:69
vl_api_mdata_enable_disable_t
API to enable / disable mdata on an interface.
Definition: mdata.api:37
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111