FD.io VPP  v21.06
Vector Packet Processing
nsh_md2_ioam_trace.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 <vppinfra/error.h>
18 
19 #include <vppinfra/hash.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/elog.h>
22 
25 #include <nsh/nsh_packet.h>
26 
27 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
28  * and nanoseconds respectively.
29  */
30 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
31 
32 #define NSH_MD2_IOAM_TRACE_SIZE_DUMMY 20
33 
34 typedef union
35 {
36  u64 as_u64;
37  u32 as_u32[2];
38 } time_u64_t;
39 
40 
41 /* *INDENT-OFF* */
42 typedef CLIB_PACKED(struct {
43  u16 class;
44  u8 type;
45  u8 length;
46  u8 data_list_elts_left;
47  u16 ioam_trace_type;
48  u8 reserve;
49  u32 elts[0]; /* Variable type. So keep it generic */
50 }) nsh_md2_ioam_trace_option_t;
51 /* *INDENT-ON* */
52 
53 
54 #define foreach_nsh_md2_ioam_trace_stats \
55  _(SUCCESS, "Pkts updated with TRACE records") \
56  _(FAILED, "Errors in TRACE due to lack of TRACE records")
57 
58 static char *nsh_md2_ioam_trace_stats_strings[] = {
59 #define _(sym,string) string,
61 #undef _
62 };
63 
64 typedef enum
65 {
66 #define _(sym,str) NSH_MD2_IOAM_TRACE_##sym,
68 #undef _
71 
72 
73 typedef struct
74 {
75  /* stats */
76  u64 counters[ARRAY_LEN (nsh_md2_ioam_trace_stats_strings)];
77 
78  /* convenience */
82 
84 
85 /*
86  * Find a trace profile
87  */
88 
89 extern u8 *nsh_trace_main;
92 {
93  trace_main_t *sm = (trace_main_t *) nsh_trace_main;
94 
95  return (&(sm->profile));
96 }
97 
98 
99 always_inline void
101 {
103 
104  hm->counters[counter_index] += increment;
105 }
106 
107 
108 static u8 *
109 format_ioam_data_list_element (u8 * s, va_list * args)
110 {
111  u32 *elt = va_arg (*args, u32 *);
112  u8 *trace_type_p = va_arg (*args, u8 *);
113  u8 trace_type = *trace_type_p;
114 
115 
116  if (trace_type & BIT_TTL_NODEID)
117  {
118  u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
119  s = format (s, "ttl 0x%x node id 0x%x ",
120  ttl_node_id_host_byte_order >> 24,
121  ttl_node_id_host_byte_order & 0x00FFFFFF);
122 
123  elt++;
124  }
125 
126  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
127  {
128  u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
129  s = format (s, "ingress 0x%x egress 0x%x ",
130  ingress_host_byte_order >> 16,
131  ingress_host_byte_order & 0xFFFF);
132  elt++;
133  }
134 
135  if (trace_type & BIT_TIMESTAMP)
136  {
137  u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
138  s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
139  elt++;
140  }
141 
142  if (trace_type & BIT_APPDATA)
143  {
144  u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
145  s = format (s, "app 0x%x ", appdata_in_host_byte_order);
146  elt++;
147  }
148 
149  return s;
150 }
151 
152 
153 
154 int
155 nsh_md2_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
156 {
157  nsh_md2_ioam_trace_option_t *trace_option = NULL;
158  u8 trace_data_size = 0;
159  u8 trace_option_elts = 0;
160  trace_profile *profile = NULL;
161 
162  profile = nsh_trace_profile_find ();
163 
164  if (PREDICT_FALSE (!profile))
165  {
166  return (-1);
167  }
168 
169  if (PREDICT_FALSE (!rewrite_string))
170  return -1;
171 
172  trace_option_elts = profile->num_elts;
173  trace_data_size = fetch_trace_data_size (profile->trace_type);
174 
175  trace_option = (nsh_md2_ioam_trace_option_t *) rewrite_string;
176  trace_option->class = clib_host_to_net_u16 (0x9);
177  trace_option->type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
178  trace_option->length = (trace_option_elts * trace_data_size) + 4;
179  trace_option->data_list_elts_left = trace_option_elts;
180  trace_option->ioam_trace_type =
181  clib_host_to_net_u16 (profile->trace_type & TRACE_TYPE_MASK);
182 
183  *rewrite_size =
184  sizeof (nsh_md2_ioam_trace_option_t) +
185  (trace_option_elts * trace_data_size);
186 
187  return 0;
188 }
189 
190 
191 int
193  nsh_tlv_header_t * opt)
194 {
195  u8 elt_index = 0;
196  nsh_md2_ioam_trace_option_t *trace =
197  (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
198  time_u64_t time_u64;
199  u32 *elt;
200  int rv = 0;
201  trace_profile *profile = NULL;
203  nsh_main_t *gm = &nsh_main;
204  u16 ioam_trace_type = 0;
205 
206  profile = nsh_trace_profile_find ();
207 
208  if (PREDICT_FALSE (!profile))
209  {
210  return (-1);
211  }
212 
213 
214  ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
215  time_u64.as_u64 = 0;
216 
217  if (PREDICT_TRUE (trace->data_list_elts_left))
218  {
219  trace->data_list_elts_left--;
220  /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
221  * to skip to this node's location.
222  */
223  elt_index =
224  trace->data_list_elts_left *
225  fetch_trace_data_size (ioam_trace_type) / 4;
226  elt = &trace->elts[elt_index];
227  if (ioam_trace_type & BIT_TTL_NODEID)
228  {
230  *elt = clib_host_to_net_u32 (((ip0->ttl - 1) << 24) |
231  profile->node_id);
232  elt++;
233  }
234 
235  if (ioam_trace_type & BIT_ING_INTERFACE)
236  {
237  u16 tx_if = vnet_buffer (b)->sw_if_index[VLIB_TX];
238 
239  *elt =
240  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 | tx_if;
241  *elt = clib_host_to_net_u32 (*elt);
242  elt++;
243  }
244 
245 
246  if (ioam_trace_type & BIT_TIMESTAMP)
247  {
248  /* Send least significant 32 bits */
249  f64 time_f64 =
250  (f64) (((f64) hm->unix_time_0) +
251  (vlib_time_now (gm->vlib_main) - hm->vlib_time_0));
252 
253  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
254  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
255  elt++;
256  }
257 
258  if (ioam_trace_type & BIT_APPDATA)
259  {
260  /* $$$ set elt0->app_data */
261  *elt = clib_host_to_net_u32 (profile->app_data);
262  elt++;
263  }
265  (NSH_MD2_IOAM_TRACE_SUCCESS, 1);
266  }
267  else
268  {
270  (NSH_MD2_IOAM_TRACE_FAILED, 1);
271  }
272  return (rv);
273 }
274 
275 
276 
277 u8 *
278 nsh_md2_ioam_trace_data_list_trace_handler (u8 * s, nsh_tlv_header_t * opt)
279 {
280  nsh_md2_ioam_trace_option_t *trace;
281  u8 trace_data_size_in_words = 0;
282  u32 *elt;
283  int elt_index = 0;
284  u16 ioam_trace_type = 0;
285 
286  trace = (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
287  ioam_trace_type = clib_net_to_host_u16 (trace->ioam_trace_type);
288  trace_data_size_in_words = fetch_trace_data_size (ioam_trace_type) / 4;
289  elt = &trace->elts[0];
290  s =
291  format (s, " Trace Type 0x%x , %d elts left\n", ioam_trace_type,
292  trace->data_list_elts_left);
293  while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->length - 4
294  /* -2 accounts for ioam_trace_type,elts_left */ ))
295  {
296  s = format (s, " [%d] %U\n", elt_index,
297  format_ioam_data_list_element, elt, &ioam_trace_type);
298  elt_index++;
299  elt += trace_data_size_in_words;
300  }
301  return (s);
302 }
303 
304 int
306  nsh_tlv_header_t * old_opt,
307  nsh_tlv_header_t * new_opt)
308 {
309 
310  clib_memcpy_fast (new_opt, old_opt,
311  new_opt->length + sizeof (nsh_tlv_header_t));
312  return nsh_md2_ioam_trace_data_list_handler (b, new_opt);
313 }
314 
315 static clib_error_t *
317  unformat_input_t * input,
318  vlib_cli_command_t * cmd)
319 {
321  u8 *s = 0;
322  int i = 0;
323 
324  for (i = 0; i < NSH_MD2_IOAM_TRACE_N_STATS; i++)
325  {
326  s = format (s, " %s - %lu\n", nsh_md2_ioam_trace_stats_strings[i],
327  hm->counters[i]);
328  }
329 
330  vlib_cli_output (vm, "%v", s);
331  vec_free (s);
332  return 0;
333 }
334 
335 
336 /* *INDENT-OFF* */
337 VLIB_CLI_COMMAND (nsh_md2_ioam_show_ioam_trace_cmd, static) = {
338  .path = "show ioam nsh-lisp-gpe trace",
339  .short_help = "iOAM trace statistics",
341 };
342 /* *INDENT-ON* */
343 
344 
345 int
346 nsh_md2_ioam_trace_pop_handler (vlib_buffer_t * b, nsh_tlv_header_t * opt)
347 {
348  return nsh_md2_ioam_trace_data_list_handler (b, opt);
349 }
350 
351 static clib_error_t *
353 {
356 
357  hm->vlib_main = vm;
358  hm->vnet_main = vnet_get_main ();
359  gm->unix_time_0 = (u32) time (0); /* Store starting time */
360  gm->vlib_time_0 = vlib_time_now (vm);
361 
362  clib_memset (hm->counters, 0, sizeof (hm->counters));
363 
365  (clib_host_to_net_u16 (0x9),
373  return (clib_error_create
374  ("registration of NSH_MD2_IOAM_OPTION_TYPE_TRACE failed"));
375 
376  return (0);
377 }
378 
379 /* *INDENT-OFF* */
381 {
382  .runs_after = VLIB_INITS ("nsh_init", "nsh_md2_ioam_init"),
383 };
384 /* *INDENT-ON* */
385 
386 int
388 {
389  nsh_main_t *hm = &nsh_main;
390 
392 
393  return 0;
394 
395 }
396 
397 static int
399 {
400  u16 size = 0;
401  u8 trace_data_size = 0;
402  trace_profile *profile = NULL;
403 
404  *result = 0;
405 
406  profile = nsh_trace_profile_find ();
407 
408  if (PREDICT_FALSE (!profile))
409  {
410  return (-1);
411  }
412 
413  trace_data_size = fetch_trace_data_size (profile->trace_type);
414  if (PREDICT_FALSE (trace_data_size == 0))
415  return VNET_API_ERROR_INVALID_VALUE;
416 
417  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
418  return VNET_API_ERROR_INVALID_VALUE;
419 
420  size +=
421  sizeof (nsh_md2_ioam_trace_option_t) +
422  profile->num_elts * trace_data_size;
423  *result = size;
424 
425  return 0;
426 }
427 
428 
429 int
431 {
432  u32 trace_size = 0;
433  nsh_main_t *hm = &nsh_main;
434 
435  trace_profile *profile = NULL;
436 
437 
438  profile = nsh_trace_profile_find ();
439 
440  if (PREDICT_FALSE (!profile))
441  {
442  return (-1);
443  }
444 
445 
446  if (nsh_md2_ioam_trace_get_sizeof_handler (&trace_size) < 0)
447  return (-1);
448 
450 
451  return (0);
452 }
453 
454 
455 
456 /*
457  * fd.io coding-style-patch-verification: ON
458  *
459  * Local Variables:
460  * eval: (c-set-style "gnu")
461  * End:
462  */
u32 as_u32[2]
int nsh_md2_ioam_trace_profile_cleanup(void)
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:899
u64 as_u64
Definition: bihash_doc.h:63
#define PREDICT_TRUE(x)
Definition: clib.h:125
typedef CLIB_PACKED(struct { u16 class;u8 type;u8 length;u8 data_list_elts_left;u16 ioam_trace_type;u8 reserve;u32 elts[0];})
unsigned long u64
Definition: types.h:89
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
static u8 fetch_trace_data_size(u16 trace_type)
Definition: trace_util.h:209
nsh_md2_ioam_trace_stats_t
int nsh_md2_ioam_trace_data_list_handler(vlib_buffer_t *b, nsh_tlv_header_t *opt)
unsigned char u8
Definition: types.h:56
vlib_buffer_t ** b
int nsh_md2_ioam_trace_pop_handler(vlib_buffer_t *b, nsh_tlv_header_t *opt)
double f64
Definition: types.h:142
unsigned int u32
Definition: types.h:88
int nsh_md2_ioam_trace_rewrite_handler(u8 *rewrite_string, u8 *rewrite_size)
static trace_profile * nsh_trace_profile_find(void)
int nsh_md2_ioam_trace_swap_handler(vlib_buffer_t *b, nsh_tlv_header_t *old_opt, nsh_tlv_header_t *new_opt)
u8 options_size[MAX_MD2_OPTIONS]
Definition: nsh.h:145
trace_profile profile
Definition: trace_util.h:55
static f64 trace_tsp_mul[4]
#define NSH_MD2_IOAM_TRACE_SIZE_DUMMY
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
description fragment has unexpected format
Definition: map.api:433
vnet_main_t * vnet_get_main(void)
#define clib_error_create(args...)
Definition: error.h:96
int __clib_unused rv
Definition: application.c:491
vl_api_fib_path_type_t type
Definition: fib_types.api:123
nsh_md2_ioam_main_t nsh_md2_ioam_main
Definition: nsh_md2_ioam.c:42
#define gm
Definition: dlmalloc.c:1219
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
u32 size
Definition: vhost_user.h:125
static u8 * format_ioam_data_list_element(u8 *s, va_list *args)
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
#define PREDICT_FALSE(x)
Definition: clib.h:124
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
static u32 counter_index(vlib_main_t *vm, vlib_error_t e)
Definition: drop.c:67
static clib_error_t * nsh_md2_ioam_show_ioam_trace_cmd_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
#define foreach_nsh_md2_ioam_trace_stats
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
u8 * nsh_md2_ioam_trace_data_list_trace_handler(u8 *s, nsh_tlv_header_t *opt)
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
static clib_error_t * nsh_md2_ioam_trace_init(vlib_main_t *vm)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
nsh_main_t nsh_main
Definition: nsh.c:28
#define ARRAY_LEN(x)
Definition: clib.h:70
#define BIT_ING_INTERFACE
Definition: trace_util.h:90
static void nsh_md2_ioam_trace_stats_increment_counter(u32 counter_index, u64 increment)
#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:163
#define BIT_APPDATA
Definition: trace_util.h:93
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
app_rx_mq_elt_t * elt
Definition: application.c:488
#define always_inline
Definition: rdma_mlx5dv.h:23
char const int length
Definition: cJSON.h:163
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
static int nsh_md2_ioam_trace_get_sizeof_handler(u32 *result)
vlib_main_t * vlib_main
Definition: nsh.h:167
u64 counters[ARRAY_LEN(nsh_md2_ioam_trace_stats_strings)]
Definition: defs.h:47
VLIB buffer representation.
Definition: buffer.h:111
u8 * nsh_trace_main
#define vnet_buffer(b)
Definition: buffer.h:437
int nsh_md2_register_option(u16 class, u8 type, u8 option_size, int add_options(u8 *opt, u8 *opt_size), int options(vlib_buffer_t *b, nsh_tlv_header_t *opt), int swap_options(vlib_buffer_t *b, nsh_tlv_header_t *old_opt, nsh_tlv_header_t *new_opt), int pop_options(vlib_buffer_t *b, nsh_tlv_header_t *opt), u8 *trace(u8 *s, nsh_tlv_header_t *opt))
Definition: nsh.c:32
int nsh_md2_ioam_trace_profile_setup(void)
#define VLIB_INITS(...)
Definition: init.h:352
Definition: defs.h:46
#define BIT_TIMESTAMP
Definition: trace_util.h:92
nsh_md2_ioam_trace_main_t nsh_md2_ioam_trace_main
#define NSH_MD2_IOAM_OPTION_TYPE_TRACE
Definition: nsh.h:245