FD.io VPP  v17.04-9-g99c0734
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 
30 
31 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
32  * and nanoseconds respectively.
33  */
34 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
35 
36 typedef union
37 {
38  u64 as_u64;
39  u32 as_u32[2];
40 } time_u64_t;
41 
42 
43 /* *INDENT-OFF* */
44 typedef CLIB_PACKED(struct {
46  u8 ioam_trace_type;
47  u8 data_list_elts_left;
48  u32 elts[0]; /* Variable type. So keep it generic */
49 }) vxlan_gpe_ioam_trace_option_t;
50 /* *INDENT-ON* */
51 
52 
53 #define foreach_vxlan_gpe_ioam_trace_stats \
54  _(SUCCESS, "Pkts updated with TRACE records") \
55  _(FAILED, "Errors in TRACE due to lack of TRACE records")
56 
57 static char *vxlan_gpe_ioam_trace_stats_strings[] = {
58 #define _(sym,string) string,
60 #undef _
61 };
62 
63 typedef enum
64 {
65 #define _(sym,str) VXLAN_GPE_IOAM_TRACE_##sym,
67 #undef _
70 
71 
72 typedef struct
73 {
74  /* stats */
75  u64 counters[ARRAY_LEN (vxlan_gpe_ioam_trace_stats_strings)];
76 
77  /* convenience */
81 
83 
84 int
86  u8 size,
87  int rewrite_options (u8 * rewrite_string,
88  u8 * rewrite_size))
89 {
91 
92  ASSERT (option < ARRAY_LEN (hm->add_options));
93 
94  /* Already registered */
95  if (hm->add_options[option])
96  return (-1);
97 
98  hm->add_options[option] = rewrite_options;
99  hm->options_size[option] = size;
100 
101  return (0);
102 }
103 
104 int
106 {
108 
109  ASSERT (option < ARRAY_LEN (hm->add_options));
110 
111  /* Not registered */
112  if (!hm->add_options[option])
113  return (-1);
114 
115  hm->add_options[option] = NULL;
116  hm->options_size[option] = 0;
117  return (0);
118 }
119 
120 
121 int
123  int options (vlib_buffer_t * b,
125  u8 is_ipv4, u8 use_adj),
126  u8 * trace (u8 * s,
128 {
130 
131  ASSERT (option < ARRAY_LEN (im->options));
132 
133  /* Already registered */
134  if (im->options[option])
135  return (-1);
136 
137  im->options[option] = options;
138  im->trace[option] = trace;
139 
140  return (0);
141 }
142 
143 int
145 {
147 
148  ASSERT (option < ARRAY_LEN (hm->options));
149 
150  /* Not registered */
151  if (!hm->options[option])
152  return (-1);
153 
154  hm->options[option] = NULL;
155  hm->trace[option] = NULL;
156 
157  return (0);
158 }
159 
160 
161 always_inline void
163  u64 increment)
164 {
166 
167  hm->counters[counter_index] += increment;
168 }
169 
170 
171 static u8 *
172 format_ioam_data_list_element (u8 * s, va_list * args)
173 {
174  u32 *elt = va_arg (*args, u32 *);
175  u8 *trace_type_p = va_arg (*args, u8 *);
176  u8 trace_type = *trace_type_p;
177 
178 
179  if (trace_type & BIT_TTL_NODEID)
180  {
181  u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
182  s = format (s, "ttl 0x%x node id 0x%x ",
183  ttl_node_id_host_byte_order >> 24,
184  ttl_node_id_host_byte_order & 0x00FFFFFF);
185 
186  elt++;
187  }
188 
189  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
190  {
191  u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
192  s = format (s, "ingress 0x%x egress 0x%x ",
193  ingress_host_byte_order >> 16,
194  ingress_host_byte_order & 0xFFFF);
195  elt++;
196  }
197 
198  if (trace_type & BIT_TIMESTAMP)
199  {
200  u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
201  s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
202  elt++;
203  }
204 
205  if (trace_type & BIT_APPDATA)
206  {
207  u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
208  s = format (s, "app 0x%x ", appdata_in_host_byte_order);
209  elt++;
210  }
211 
212  return s;
213 }
214 
215 
216 
217 int
218 vxlan_gpe_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
219 {
220  vxlan_gpe_ioam_trace_option_t *trace_option = NULL;
221  u8 trace_data_size = 0;
222  u8 trace_option_elts = 0;
223  trace_profile *profile = NULL;
224 
225 
226  profile = trace_profile_find ();
227 
228  if (PREDICT_FALSE (!profile))
229  {
230  return (-1);
231  }
232 
233  if (PREDICT_FALSE (!rewrite_string))
234  return -1;
235 
236  trace_option_elts = profile->num_elts;
237  trace_data_size = fetch_trace_data_size (profile->trace_type);
238  trace_option = (vxlan_gpe_ioam_trace_option_t *) rewrite_string;
239  trace_option->hdr.type = VXLAN_GPE_OPTION_TYPE_IOAM_TRACE;
240  trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
241  trace_option_elts * trace_data_size;
242  trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
243  trace_option->data_list_elts_left = trace_option_elts;
244  *rewrite_size =
245  sizeof (vxlan_gpe_ioam_trace_option_t) +
246  (trace_option_elts * trace_data_size);
247 
248  return 0;
249 }
250 
251 
252 int
255  u8 is_ipv4, u8 use_adj)
256 {
257  u8 elt_index = 0;
258  vxlan_gpe_ioam_trace_option_t *trace =
259  (vxlan_gpe_ioam_trace_option_t *) opt;
260  time_u64_t time_u64;
261  u32 *elt;
262  int rv = 0;
263  trace_profile *profile = NULL;
265 
266 
267  profile = trace_profile_find ();
268 
269  if (PREDICT_FALSE (!profile))
270  {
271  return (-1);
272  }
273 
274 
275  time_u64.as_u64 = 0;
276 
277  if (PREDICT_TRUE (trace->data_list_elts_left))
278  {
279  trace->data_list_elts_left--;
280  /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
281  * to skip to this node's location.
282  */
283  elt_index =
284  trace->data_list_elts_left *
285  fetch_trace_data_size (trace->ioam_trace_type) / 4;
286  elt = &trace->elts[elt_index];
287  if (is_ipv4)
288  {
289  if (trace->ioam_trace_type & BIT_TTL_NODEID)
290  {
292  /* The transit case is the only case where the TTL decrement happens
293  * before iOAM processing. For now, use the use_adj flag as an overload.
294  * We can probably use a separate flag instead of overloading the use_adj flag.
295  */
296  *elt = clib_host_to_net_u32 (((ip0->ttl - 1 + use_adj) << 24) |
297  profile->node_id);
298  elt++;
299  }
300 
301  if (trace->ioam_trace_type & BIT_ING_INTERFACE)
302  {
303  u16 tx_if = 0;
304  u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
305  ip4_main_t *im4 = &ip4_main;
306  ip_lookup_main_t *lm = &im4->lookup_main;
307  if (use_adj)
308  {
309  ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
310  tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
311  }
312 
313  *elt =
314  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
315  tx_if;
316  *elt = clib_host_to_net_u32 (*elt);
317  elt++;
318  }
319  }
320  else
321  {
322  if (trace->ioam_trace_type & BIT_TTL_NODEID)
323  {
325  *elt = clib_host_to_net_u32 ((ip0->hop_limit << 24) |
326  profile->node_id);
327  elt++;
328  }
329  if (trace->ioam_trace_type & BIT_ING_INTERFACE)
330  {
331  u16 tx_if = 0;
332  u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
333  ip6_main_t *im6 = &ip6_main;
334  ip_lookup_main_t *lm = &im6->lookup_main;
335  if (use_adj)
336  {
337  ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
338  tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
339  }
340 
341  *elt =
342  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
343  tx_if;
344  *elt = clib_host_to_net_u32 (*elt);
345  elt++;
346  }
347  }
348 
349  if (trace->ioam_trace_type & BIT_TIMESTAMP)
350  {
351  /* Send least significant 32 bits */
352  f64 time_f64 =
353  (f64) (((f64) hm->unix_time_0) +
354  (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
355 
356  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
357  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
358  elt++;
359  }
360 
361  if (trace->ioam_trace_type & BIT_APPDATA)
362  {
363  /* $$$ set elt0->app_data */
364  *elt = clib_host_to_net_u32 (profile->app_data);
365  elt++;
366  }
368  (VXLAN_GPE_IOAM_TRACE_SUCCESS, 1);
369  }
370  else
371  {
373  (VXLAN_GPE_IOAM_TRACE_FAILED, 1);
374  }
375  return (rv);
376 }
377 
378 u8 *
381 {
382  vxlan_gpe_ioam_trace_option_t *trace;
383  u8 trace_data_size_in_words = 0;
384  u32 *elt;
385  int elt_index = 0;
386 
387  trace = (vxlan_gpe_ioam_trace_option_t *) opt;
388  s =
389  format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
390  trace->data_list_elts_left);
391  trace_data_size_in_words =
392  fetch_trace_data_size (trace->ioam_trace_type) / 4;
393  elt = &trace->elts[0];
394  while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
395  /* -2 accounts for ioam_trace_type,elts_left */ ))
396  {
397  s = format (s, " [%d] %U\n", elt_index,
399  elt, &trace->ioam_trace_type);
400  elt_index++;
401  elt += trace_data_size_in_words;
402  }
403  return (s);
404 }
405 
406 
407 static clib_error_t *
409  unformat_input_t * input,
410  vlib_cli_command_t * cmd)
411 {
413  u8 *s = 0;
414  int i = 0;
415 
416  for (i = 0; i < VXLAN_GPE_IOAM_TRACE_N_STATS; i++)
417  {
418  s = format (s, " %s - %lu\n", vxlan_gpe_ioam_trace_stats_strings[i],
419  hm->counters[i]);
420  }
421 
422  vlib_cli_output (vm, "%v", s);
423  vec_free (s);
424  return 0;
425 }
426 
427 
428 /* *INDENT-OFF* */
429 VLIB_CLI_COMMAND (vxlan_gpe_show_ioam_trace_cmd, static) = {
430  .path = "show ioam vxlan-gpe trace",
431  .short_help = "iOAM trace statistics",
433 };
434 /* *INDENT-ON* */
435 
436 
437 static clib_error_t *
439 {
441  clib_error_t *error;
442 
443  if ((error = vlib_call_init_function (vm, ip_main_init)))
444  return (error);
445 
446  if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
447  return error;
448 
449  if ((error = vlib_call_init_function (vm, vxlan_gpe_init)))
450  return (error);
451 
452  hm->vlib_main = vm;
453  hm->vnet_main = vnet_get_main ();
454  memset (hm->counters, 0, sizeof (hm->counters));
455 
460  return (clib_error_create
461  ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE failed"));
462 
463 
466  sizeof (vxlan_gpe_ioam_trace_option_t),
468  return (clib_error_create
469  ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE for rewrite failed"));
470 
471 
472  return (0);
473 }
474 
476 
477 int
479 {
481 
483 
484  return 0;
485 
486 }
487 
488 static int
490 {
491  u16 size = 0;
492  u8 trace_data_size = 0;
493  trace_profile *profile = NULL;
494 
495  *result = 0;
496 
497  profile = trace_profile_find ();
498 
499  if (PREDICT_FALSE (!profile))
500  {
501  return (-1);
502  }
503 
504  trace_data_size = fetch_trace_data_size (profile->trace_type);
505  if (PREDICT_FALSE (trace_data_size == 0))
506  return VNET_API_ERROR_INVALID_VALUE;
507 
508  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
509  return VNET_API_ERROR_INVALID_VALUE;
510 
511  size +=
512  sizeof (vxlan_gpe_ioam_trace_option_t) +
513  profile->num_elts * trace_data_size;
514  *result = size;
515 
516  return 0;
517 }
518 
519 
520 int
522 {
523  u32 trace_size = 0;
525 
526  trace_profile *profile = NULL;
527 
528 
529  profile = trace_profile_find ();
530 
531  if (PREDICT_FALSE (!profile))
532  {
533  return (-1);
534  }
535 
536 
537  if (vxlan_gpe_ioam_trace_get_sizeof_handler (&trace_size) < 0)
538  return (-1);
539 
541 
542  return (0);
543 }
544 
545 
546 
547 /*
548  * fd.io coding-style-patch-verification: ON
549  *
550  * Local Variables:
551  * eval: (c-set-style "gnu")
552  * End:
553  */
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:659
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: memory_vlib.c:1172
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
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:185
IP unicast adjacency.
Definition: lookup.h:193
VXLAN GPE definitions.
static u8 fetch_trace_data_size(u16 trace_type)
Definition: trace_util.h:209
static trace_profile * trace_profile_find(void)
Definition: trace_config.h:24
ip_lookup_main_t lookup_main
Definition: ip4.h:109
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
typedef CLIB_PACKED(struct{vxlan_gpe_ioam_option_t hdr;u8 ioam_trace_type;u8 data_list_elts_left;u32 elts[0];})
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 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
#define clib_error_create(args...)
Definition: error.h:108
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:162
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.
struct _unformat_input_t unformat_input_t
u64 counters[ARRAY_LEN(vxlan_gpe_ioam_trace_stats_strings)]
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:188
#define PREDICT_FALSE(x)
Definition: clib.h:97
#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)
vlib_main_t * vm
Definition: buffer.c:276
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:340
#define ARRAY_LEN(x)
Definition: clib.h:59
#define BIT_ING_INTERFACE
Definition: trace_util.h:90
#define BIT_TTL_NODEID
Definition: trace_util.h:89
#define TRACE_TYPE_MASK
Definition: trace_util.h:96
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define BIT_APPDATA
Definition: trace_util.h:93
int(* add_options[256])(u8 *rewrite_string, u8 *rewrite_size)
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
ip6_main_t ip6_main
Definition: ip6_forward.c:2846
ip_lookup_main_t lookup_main
Definition: ip6.h:151
IPv4 main type.
Definition: ip4.h:107
int vxlan_gpe_ioam_unregister_option(u8 option)
u64 size
Definition: vhost-user.h:77
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)
#define vnet_buffer(b)
Definition: buffer.h:294
static f64 trace_tsp_mul[4]
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1117
int vxlan_gpe_add_unregister_option(u8 option)
static clib_error_t * ip6_lookup_init(vlib_main_t *vm)
Definition: ip6_forward.c:2849
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:577
Definition: defs.h:46
#define BIT_TIMESTAMP
Definition: trace_util.h:92
static ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:377
u8 *(* trace[256])(u8 *s, vxlan_gpe_ioam_option_t *opt)
static u8 * format_ioam_data_list_element(u8 *s, va_list *args)