FD.io VPP  v17.01.1-3-gc6833f8
Vector Packet Processing
vxlan_gpe_ioam_trace.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
19 
22 
23 #include <vppinfra/hash.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
26 
29 
30 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
31  * and nanoseconds respectively.
32  */
33 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
34 
35 typedef union
36 {
37  u64 as_u64;
38  u32 as_u32[2];
39 } time_u64_t;
40 
41 
42 /* *INDENT-OFF* */
43 typedef CLIB_PACKED(struct {
45  u8 ioam_trace_type;
46  u8 data_list_elts_left;
47  u32 elts[0]; /* Variable type. So keep it generic */
48 }) vxlan_gpe_ioam_trace_option_t;
49 /* *INDENT-ON* */
50 
51 
52 #define foreach_vxlan_gpe_ioam_trace_stats \
53  _(SUCCESS, "Pkts updated with TRACE records") \
54  _(FAILED, "Errors in TRACE due to lack of TRACE records")
55 
56 static char *vxlan_gpe_ioam_trace_stats_strings[] = {
57 #define _(sym,string) string,
59 #undef _
60 };
61 
62 typedef enum
63 {
64 #define _(sym,str) VXLAN_GPE_IOAM_TRACE_##sym,
66 #undef _
69 
70 
71 typedef struct
72 {
73  /* stats */
74  u64 counters[ARRAY_LEN (vxlan_gpe_ioam_trace_stats_strings)];
75 
76  /* convenience */
80 
82 
83 int
85  u8 size,
86  int rewrite_options (u8 * rewrite_string,
87  u8 * rewrite_size))
88 {
90 
91  ASSERT (option < ARRAY_LEN (hm->add_options));
92 
93  /* Already registered */
94  if (hm->add_options[option])
95  return (-1);
96 
97  hm->add_options[option] = rewrite_options;
98  hm->options_size[option] = size;
99 
100  return (0);
101 }
102 
103 int
105 {
107 
108  ASSERT (option < ARRAY_LEN (hm->add_options));
109 
110  /* Not registered */
111  if (!hm->add_options[option])
112  return (-1);
113 
114  hm->add_options[option] = NULL;
115  hm->options_size[option] = 0;
116  return (0);
117 }
118 
119 
120 int
122  int options (vlib_buffer_t * b,
124  u8 is_ipv4, u8 use_adj),
125  u8 * trace (u8 * s,
127 {
129 
130  ASSERT (option < ARRAY_LEN (im->options));
131 
132  /* Already registered */
133  if (im->options[option])
134  return (-1);
135 
136  im->options[option] = options;
137  im->trace[option] = trace;
138 
139  return (0);
140 }
141 
142 int
144 {
146 
147  ASSERT (option < ARRAY_LEN (hm->options));
148 
149  /* Not registered */
150  if (!hm->options[option])
151  return (-1);
152 
153  hm->options[option] = NULL;
154  hm->trace[option] = NULL;
155 
156  return (0);
157 }
158 
159 
160 always_inline void
162  u64 increment)
163 {
165 
166  hm->counters[counter_index] += increment;
167 }
168 
169 
170 static u8 *
171 format_ioam_data_list_element (u8 * s, va_list * args)
172 {
173  u32 *elt = va_arg (*args, u32 *);
174  u8 *trace_type_p = va_arg (*args, u8 *);
175  u8 trace_type = *trace_type_p;
176 
177 
178  if (trace_type & BIT_TTL_NODEID)
179  {
180  u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
181  s = format (s, "ttl 0x%x node id 0x%x ",
182  ttl_node_id_host_byte_order >> 24,
183  ttl_node_id_host_byte_order & 0x00FFFFFF);
184 
185  elt++;
186  }
187 
188  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
189  {
190  u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
191  s = format (s, "ingress 0x%x egress 0x%x ",
192  ingress_host_byte_order >> 16,
193  ingress_host_byte_order & 0xFFFF);
194  elt++;
195  }
196 
197  if (trace_type & BIT_TIMESTAMP)
198  {
199  u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
200  s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
201  elt++;
202  }
203 
204  if (trace_type & BIT_APPDATA)
205  {
206  u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
207  s = format (s, "app 0x%x ", appdata_in_host_byte_order);
208  elt++;
209  }
210 
211  return s;
212 }
213 
214 
215 
216 int
217 vxlan_gpe_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
218 {
219  vxlan_gpe_ioam_trace_option_t *trace_option = NULL;
220  u8 trace_data_size = 0;
221  u8 trace_option_elts = 0;
222  trace_profile *profile = NULL;
223 
224 
225  profile = trace_profile_find ();
226 
227  if (PREDICT_FALSE (!profile))
228  {
229  return (-1);
230  }
231 
232  if (PREDICT_FALSE (!rewrite_string))
233  return -1;
234 
235  trace_option_elts = profile->num_elts;
236  trace_data_size = fetch_trace_data_size (profile->trace_type);
237  trace_option = (vxlan_gpe_ioam_trace_option_t *) rewrite_string;
238  trace_option->hdr.type = VXLAN_GPE_OPTION_TYPE_IOAM_TRACE;
239  trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
240  trace_option_elts * trace_data_size;
241  trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
242  trace_option->data_list_elts_left = trace_option_elts;
243  *rewrite_size =
244  sizeof (vxlan_gpe_ioam_trace_option_t) +
245  (trace_option_elts * trace_data_size);
246 
247  return 0;
248 }
249 
250 
251 int
254  u8 is_ipv4, u8 use_adj)
255 {
256  u8 elt_index = 0;
257  vxlan_gpe_ioam_trace_option_t *trace =
258  (vxlan_gpe_ioam_trace_option_t *) opt;
259  time_u64_t time_u64;
260  u32 *elt;
261  int rv = 0;
262  trace_profile *profile = NULL;
264 
265 
266  profile = trace_profile_find ();
267 
268  if (PREDICT_FALSE (!profile))
269  {
270  return (-1);
271  }
272 
273 
274  time_u64.as_u64 = 0;
275 
276  if (PREDICT_TRUE (trace->data_list_elts_left))
277  {
278  trace->data_list_elts_left--;
279  /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
280  * to skip to this node's location.
281  */
282  elt_index =
283  trace->data_list_elts_left *
284  fetch_trace_data_size (trace->ioam_trace_type) / 4;
285  elt = &trace->elts[elt_index];
286  if (is_ipv4)
287  {
288  if (trace->ioam_trace_type & BIT_TTL_NODEID)
289  {
291  /* The transit case is the only case where the TTL decrement happens
292  * before iOAM processing. For now, use the use_adj flag as an overload.
293  * We can probably use a separate flag instead of overloading the use_adj flag.
294  */
295  *elt = clib_host_to_net_u32 (((ip0->ttl - 1 + use_adj) << 24) |
296  profile->node_id);
297  elt++;
298  }
299 
300  if (trace->ioam_trace_type & BIT_ING_INTERFACE)
301  {
302  u16 tx_if = 0;
303  u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
304  ip4_main_t *im4 = &ip4_main;
305  ip_lookup_main_t *lm = &im4->lookup_main;
306  if (use_adj)
307  {
308  ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
309  tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
310  }
311 
312  *elt =
313  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
314  tx_if;
315  *elt = clib_host_to_net_u32 (*elt);
316  elt++;
317  }
318  }
319  else
320  {
321  if (trace->ioam_trace_type & BIT_TTL_NODEID)
322  {
324  *elt = clib_host_to_net_u32 ((ip0->hop_limit << 24) |
325  profile->node_id);
326  elt++;
327  }
328  if (trace->ioam_trace_type & BIT_ING_INTERFACE)
329  {
330  u16 tx_if = 0;
331  u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
332  ip6_main_t *im6 = &ip6_main;
333  ip_lookup_main_t *lm = &im6->lookup_main;
334  if (use_adj)
335  {
336  ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
337  tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
338  }
339 
340  *elt =
341  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
342  tx_if;
343  *elt = clib_host_to_net_u32 (*elt);
344  elt++;
345  }
346  }
347 
348  if (trace->ioam_trace_type & BIT_TIMESTAMP)
349  {
350  /* Send least significant 32 bits */
351  f64 time_f64 =
352  (f64) (((f64) hm->unix_time_0) +
353  (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
354 
355  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
356  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
357  elt++;
358  }
359 
360  if (trace->ioam_trace_type & BIT_APPDATA)
361  {
362  /* $$$ set elt0->app_data */
363  *elt = clib_host_to_net_u32 (profile->app_data);
364  elt++;
365  }
367  (VXLAN_GPE_IOAM_TRACE_SUCCESS, 1);
368  }
369  else
370  {
372  (VXLAN_GPE_IOAM_TRACE_FAILED, 1);
373  }
374  return (rv);
375 }
376 
377 u8 *
380 {
381  vxlan_gpe_ioam_trace_option_t *trace;
382  u8 trace_data_size_in_words = 0;
383  u32 *elt;
384  int elt_index = 0;
385 
386  trace = (vxlan_gpe_ioam_trace_option_t *) opt;
387  s =
388  format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
389  trace->data_list_elts_left);
390  trace_data_size_in_words =
391  fetch_trace_data_size (trace->ioam_trace_type) / 4;
392  elt = &trace->elts[0];
393  while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
394  /* -2 accounts for ioam_trace_type,elts_left */ ))
395  {
396  s = format (s, " [%d] %U\n", elt_index,
398  elt, &trace->ioam_trace_type);
399  elt_index++;
400  elt += trace_data_size_in_words;
401  }
402  return (s);
403 }
404 
405 
406 static clib_error_t *
408  unformat_input_t * input,
409  vlib_cli_command_t * cmd)
410 {
412  u8 *s = 0;
413  int i = 0;
414 
415  for (i = 0; i < VXLAN_GPE_IOAM_TRACE_N_STATS; i++)
416  {
417  s = format (s, " %s - %lu\n", vxlan_gpe_ioam_trace_stats_strings[i],
418  hm->counters[i]);
419  }
420 
421  vlib_cli_output (vm, "%v", s);
422  vec_free (s);
423  return 0;
424 }
425 
426 
427 /* *INDENT-OFF* */
428 VLIB_CLI_COMMAND (vxlan_gpe_show_ioam_trace_cmd, static) = {
429  .path = "show ioam vxlan-gpe trace",
430  .short_help = "iOAM trace statistics",
432 };
433 /* *INDENT-ON* */
434 
435 
436 static clib_error_t *
438 {
440  clib_error_t *error;
441 
442  if ((error = vlib_call_init_function (vm, ip_main_init)))
443  return (error);
444 
445  if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
446  return error;
447 
448  if ((error = vlib_call_init_function (vm, vxlan_gpe_init)))
449  return (error);
450 
451  hm->vlib_main = vm;
452  hm->vnet_main = vnet_get_main ();
453  memset (hm->counters, 0, sizeof (hm->counters));
454 
459  return (clib_error_create
460  ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE failed"));
461 
462 
465  sizeof (vxlan_gpe_ioam_trace_option_t),
467  return (clib_error_create
468  ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE for rewrite failed"));
469 
470 
471  return (0);
472 }
473 
475 
476 int
478 {
480 
482 
483  return 0;
484 
485 }
486 
487 static int
489 {
490  u16 size = 0;
491  u8 trace_data_size = 0;
492  trace_profile *profile = NULL;
493 
494  *result = 0;
495 
496  profile = trace_profile_find ();
497 
498  if (PREDICT_FALSE (!profile))
499  {
500  return (-1);
501  }
502 
503  trace_data_size = fetch_trace_data_size (profile->trace_type);
504  if (PREDICT_FALSE (trace_data_size == 0))
505  return VNET_API_ERROR_INVALID_VALUE;
506 
507  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
508  return VNET_API_ERROR_INVALID_VALUE;
509 
510  size +=
511  sizeof (vxlan_gpe_ioam_trace_option_t) +
512  profile->num_elts * trace_data_size;
513  *result = size;
514 
515  return 0;
516 }
517 
518 
519 int
521 {
522  u32 trace_size = 0;
524 
525  trace_profile *profile = NULL;
526 
527 
528  profile = trace_profile_find ();
529 
530  if (PREDICT_FALSE (!profile))
531  {
532  return (-1);
533  }
534 
535 
536  if (vxlan_gpe_ioam_trace_get_sizeof_handler (&trace_size) < 0)
537  return (-1);
538 
540 
541  return (0);
542 }
543 
544 
545 
546 /*
547  * fd.io coding-style-patch-verification: ON
548  *
549  * Local Variables:
550  * eval: (c-set-style "gnu")
551  * End:
552  */
u32 as_u32[2]
static void vxlan_gpe_ioam_trace_stats_increment_counter(u32 counter_index, u64 increment)
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
clib_error_t * vxlan_gpe_init(vlib_main_t *vm)
Feature init function for VXLAN GPE.
Definition: vxlan_gpe.c:629
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: memory_vlib.c:1115
vlib_main_t * vlib_main
State convenience vlib_main_t.
u64 as_u64
Definition: bihash_doc.h:63
#define PREDICT_TRUE(x)
Definition: clib.h:98
static int vxlan_gpe_ioam_trace_get_sizeof_handler(u32 *result)
#define NULL
Definition: clib.h:55
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
IP unicast adjacency.
Definition: lookup.h:188
VXLAN GPE definitions.
ip_lookup_main_t lookup_main
Definition: ip4.h:97
typedef CLIB_PACKED(struct{vxlan_gpe_ioam_option_t hdr;u8 ioam_trace_type;u8 data_list_elts_left;u32 elts[0];})
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
static clib_error_t * vxlan_gpe_ioam_trace_init(vlib_main_t *vm)
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define always_inline
Definition: clib.h:84
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:194
static u32 counter_index(vlib_main_t *vm, vlib_error_t e)
vxlan_gpe_ioam_main_t vxlan_gpe_ioam_main
int vxlan_gpe_ioam_trace_rewrite_handler(u8 *rewrite_string, u8 *rewrite_size)
unsigned long u64
Definition: types.h:89
int vxlan_gpe_trace_profile_setup(void)
int vxlan_gpe_ioam_register_option(u8 option, int options(vlib_buffer_t *b, vxlan_gpe_ioam_option_t *opt, u8 is_ipv4, u8 use_adj), u8 *trace(u8 *s, vxlan_gpe_ioam_option_t *opt))
#define vlib_call_init_function(vm, x)
Definition: init.h:161
int vxlan_gpe_ioam_add_register_option(u8 option, u8 size, int rewrite_options(u8 *rewrite_string, u8 *rewrite_size))
VXLAN GPE packet header structure.
u64 counters[ARRAY_LEN(vxlan_gpe_ioam_trace_stats_strings)]
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define clib_error_create(args...)
Definition: error.h:108
static u8 fetch_trace_data_size(u8 trace_type)
Definition: trace_util.h:212
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
#define VXLAN_GPE_OPTION_TYPE_IOAM_TRACE
#define foreach_vxlan_gpe_ioam_trace_stats
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
static clib_error_t * vxlan_gpe_show_ioam_trace_cmd_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
vxlan_gpe_ioam_trace_main_t vxlan_gpe_ioam_trace_main
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
#define ARRAY_LEN(x)
Definition: clib.h:59
#define BIT_ING_INTERFACE
Definition: trace_util.h:95
#define BIT_TTL_NODEID
Definition: trace_util.h:94
#define TRACE_TYPE_MASK
Definition: trace_util.h:99
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define BIT_APPDATA
Definition: trace_util.h:98
int(* add_options[256])(u8 *rewrite_string, u8 *rewrite_size)
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:361
ip6_main_t ip6_main
Definition: ip6_forward.c:2828
ip_lookup_main_t lookup_main
Definition: ip6.h:135
IPv4 main type.
Definition: ip4.h:95
int vxlan_gpe_ioam_unregister_option(u8 option)
u64 size
Definition: vhost-user.h:74
int vxlan_gpe_trace_profile_cleanup(void)
int(* options[256])(vlib_buffer_t *b, vxlan_gpe_ioam_option_t *opt, u8 is_ipv4, u8 use_adj)
vxlan_gpe_ioam_trace_stats_t
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
u8 * vxlan_gpe_ioam_trace_data_list_trace_handler(u8 *s, vxlan_gpe_ioam_option_t *opt)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
int vxlan_gpe_ioam_trace_data_list_handler(vlib_buffer_t *b, vxlan_gpe_ioam_option_t *opt, u8 is_ipv4, u8 use_adj)
static f64 trace_tsp_mul[4]
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1099
int vxlan_gpe_add_unregister_option(u8 option)
static trace_profile * trace_profile_find(void)
Definition: trace_util.h:78
struct _unformat_input_t unformat_input_t
static clib_error_t * ip6_lookup_init(vlib_main_t *vm)
Definition: ip6_forward.c:2831
Definition: defs.h:46
#define BIT_TIMESTAMP
Definition: trace_util.h:97
static ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:415
u8 *(* trace[256])(u8 *s, vxlan_gpe_ioam_option_t *opt)
static u8 * format_ioam_data_list_element(u8 *s, va_list *args)