FD.io VPP  v19.04.4-rc0-5-ge88582fac
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 <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
19 
20 #include <vppinfra/hash.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/elog.h>
23 
26 #include <nsh/nsh_packet.h>
27 
28 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
29  * and nanoseconds respectively.
30  */
31 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
32 
33 #define NSH_MD2_IOAM_TRACE_SIZE_DUMMY 20
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 {
44  u16 class;
45  u8 type;
46  u8 length;
47  u8 data_list_elts_left;
48  u16 ioam_trace_type;
49  u8 reserve;
50  u32 elts[0]; /* Variable type. So keep it generic */
51 }) nsh_md2_ioam_trace_option_t;
52 /* *INDENT-ON* */
53 
54 
55 #define foreach_nsh_md2_ioam_trace_stats \
56  _(SUCCESS, "Pkts updated with TRACE records") \
57  _(FAILED, "Errors in TRACE due to lack of TRACE records")
58 
59 static char *nsh_md2_ioam_trace_stats_strings[] = {
60 #define _(sym,string) string,
62 #undef _
63 };
64 
65 typedef enum
66 {
67 #define _(sym,str) NSH_MD2_IOAM_TRACE_##sym,
69 #undef _
72 
73 
74 typedef struct
75 {
76  /* stats */
77  u64 counters[ARRAY_LEN (nsh_md2_ioam_trace_stats_strings)];
78 
79  /* convenience */
83 
85 
86 /*
87  * Find a trace profile
88  */
89 
90 extern u8 *nsh_trace_main;
93 {
94  trace_main_t *sm = (trace_main_t *) nsh_trace_main;
95 
96  return (&(sm->profile));
97 }
98 
99 
100 always_inline void
102 {
104 
105  hm->counters[counter_index] += increment;
106 }
107 
108 
109 static u8 *
110 format_ioam_data_list_element (u8 * s, va_list * args)
111 {
112  u32 *elt = va_arg (*args, u32 *);
113  u8 *trace_type_p = va_arg (*args, u8 *);
114  u8 trace_type = *trace_type_p;
115 
116 
117  if (trace_type & BIT_TTL_NODEID)
118  {
119  u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
120  s = format (s, "ttl 0x%x node id 0x%x ",
121  ttl_node_id_host_byte_order >> 24,
122  ttl_node_id_host_byte_order & 0x00FFFFFF);
123 
124  elt++;
125  }
126 
127  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
128  {
129  u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
130  s = format (s, "ingress 0x%x egress 0x%x ",
131  ingress_host_byte_order >> 16,
132  ingress_host_byte_order & 0xFFFF);
133  elt++;
134  }
135 
136  if (trace_type & BIT_TIMESTAMP)
137  {
138  u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
139  s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
140  elt++;
141  }
142 
143  if (trace_type & BIT_APPDATA)
144  {
145  u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
146  s = format (s, "app 0x%x ", appdata_in_host_byte_order);
147  elt++;
148  }
149 
150  return s;
151 }
152 
153 
154 
155 int
156 nsh_md2_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
157 {
158  nsh_md2_ioam_trace_option_t *trace_option = NULL;
159  u8 trace_data_size = 0;
160  u8 trace_option_elts = 0;
161  trace_profile *profile = NULL;
162 
163  profile = nsh_trace_profile_find ();
164 
165  if (PREDICT_FALSE (!profile))
166  {
167  return (-1);
168  }
169 
170  if (PREDICT_FALSE (!rewrite_string))
171  return -1;
172 
173  trace_option_elts = profile->num_elts;
174  trace_data_size = fetch_trace_data_size (profile->trace_type);
175 
176  trace_option = (nsh_md2_ioam_trace_option_t *) rewrite_string;
177  trace_option->class = clib_host_to_net_u16 (0x9);
178  trace_option->type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
179  trace_option->length = (trace_option_elts * trace_data_size) + 4;
180  trace_option->data_list_elts_left = trace_option_elts;
181  trace_option->ioam_trace_type =
182  clib_host_to_net_u16 (profile->trace_type & TRACE_TYPE_MASK);
183 
184  *rewrite_size =
185  sizeof (nsh_md2_ioam_trace_option_t) +
186  (trace_option_elts * trace_data_size);
187 
188  return 0;
189 }
190 
191 
192 int
194  nsh_tlv_header_t * opt)
195 {
196  u8 elt_index = 0;
197  nsh_md2_ioam_trace_option_t *trace =
198  (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
199  time_u64_t time_u64;
200  u32 *elt;
201  int rv = 0;
202  trace_profile *profile = NULL;
204  nsh_main_t *gm = &nsh_main;
205  u16 ioam_trace_type = 0;
206 
207  profile = nsh_trace_profile_find ();
208 
209  if (PREDICT_FALSE (!profile))
210  {
211  return (-1);
212  }
213 
214 
215  ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
216  time_u64.as_u64 = 0;
217 
218  if (PREDICT_TRUE (trace->data_list_elts_left))
219  {
220  trace->data_list_elts_left--;
221  /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
222  * to skip to this node's location.
223  */
224  elt_index =
225  trace->data_list_elts_left *
226  fetch_trace_data_size (ioam_trace_type) / 4;
227  elt = &trace->elts[elt_index];
228  if (ioam_trace_type & BIT_TTL_NODEID)
229  {
231  *elt = clib_host_to_net_u32 (((ip0->ttl - 1) << 24) |
232  profile->node_id);
233  elt++;
234  }
235 
236  if (ioam_trace_type & BIT_ING_INTERFACE)
237  {
238  u16 tx_if = vnet_buffer (b)->sw_if_index[VLIB_TX];
239 
240  *elt =
241  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 | tx_if;
242  *elt = clib_host_to_net_u32 (*elt);
243  elt++;
244  }
245 
246 
247  if (ioam_trace_type & BIT_TIMESTAMP)
248  {
249  /* Send least significant 32 bits */
250  f64 time_f64 =
251  (f64) (((f64) hm->unix_time_0) +
252  (vlib_time_now (gm->vlib_main) - hm->vlib_time_0));
253 
254  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
255  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
256  elt++;
257  }
258 
259  if (ioam_trace_type & BIT_APPDATA)
260  {
261  /* $$$ set elt0->app_data */
262  *elt = clib_host_to_net_u32 (profile->app_data);
263  elt++;
264  }
266  (NSH_MD2_IOAM_TRACE_SUCCESS, 1);
267  }
268  else
269  {
271  (NSH_MD2_IOAM_TRACE_FAILED, 1);
272  }
273  return (rv);
274 }
275 
276 
277 
278 u8 *
279 nsh_md2_ioam_trace_data_list_trace_handler (u8 * s, nsh_tlv_header_t * opt)
280 {
281  nsh_md2_ioam_trace_option_t *trace;
282  u8 trace_data_size_in_words = 0;
283  u32 *elt;
284  int elt_index = 0;
285  u16 ioam_trace_type = 0;
286 
287  trace = (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
288  ioam_trace_type = clib_net_to_host_u16 (trace->ioam_trace_type);
289  trace_data_size_in_words = fetch_trace_data_size (ioam_trace_type) / 4;
290  elt = &trace->elts[0];
291  s =
292  format (s, " Trace Type 0x%x , %d elts left\n", ioam_trace_type,
293  trace->data_list_elts_left);
294  while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->length - 4
295  /* -2 accounts for ioam_trace_type,elts_left */ ))
296  {
297  s = format (s, " [%d] %U\n", elt_index,
298  format_ioam_data_list_element, elt, &ioam_trace_type);
299  elt_index++;
300  elt += trace_data_size_in_words;
301  }
302  return (s);
303 }
304 
305 int
307  nsh_tlv_header_t * old_opt,
308  nsh_tlv_header_t * new_opt)
309 {
310 
311  clib_memcpy_fast (new_opt, old_opt,
312  new_opt->length + sizeof (nsh_tlv_header_t));
313  return nsh_md2_ioam_trace_data_list_handler (b, new_opt);
314 }
315 
316 static clib_error_t *
318  unformat_input_t * input,
319  vlib_cli_command_t * cmd)
320 {
322  u8 *s = 0;
323  int i = 0;
324 
325  for (i = 0; i < NSH_MD2_IOAM_TRACE_N_STATS; i++)
326  {
327  s = format (s, " %s - %lu\n", nsh_md2_ioam_trace_stats_strings[i],
328  hm->counters[i]);
329  }
330 
331  vlib_cli_output (vm, "%v", s);
332  vec_free (s);
333  return 0;
334 }
335 
336 
337 /* *INDENT-OFF* */
338 VLIB_CLI_COMMAND (nsh_md2_ioam_show_ioam_trace_cmd, static) = {
339  .path = "show ioam nsh-lisp-gpe trace",
340  .short_help = "iOAM trace statistics",
342 };
343 /* *INDENT-ON* */
344 
345 
346 int
347 nsh_md2_ioam_trace_pop_handler (vlib_buffer_t * b, nsh_tlv_header_t * opt)
348 {
349  return nsh_md2_ioam_trace_data_list_handler (b, opt);
350 }
351 
352 static clib_error_t *
354 {
357  clib_error_t *error;
358 
359  if ((error = vlib_call_init_function (vm, nsh_init)))
360  return (error);
361 
362  if ((error = vlib_call_init_function (vm, nsh_md2_ioam_init)))
363  return (error);
364 
365  hm->vlib_main = vm;
366  hm->vnet_main = vnet_get_main ();
367  gm->unix_time_0 = (u32) time (0); /* Store starting time */
368  gm->vlib_time_0 = vlib_time_now (vm);
369 
370  clib_memset (hm->counters, 0, sizeof (hm->counters));
371 
373  (clib_host_to_net_u16 (0x9),
381  return (clib_error_create
382  ("registration of NSH_MD2_IOAM_OPTION_TYPE_TRACE failed"));
383 
384  return (0);
385 }
386 
388 
389 int
391 {
392  nsh_main_t *hm = &nsh_main;
393 
395 
396  return 0;
397 
398 }
399 
400 static int
402 {
403  u16 size = 0;
404  u8 trace_data_size = 0;
405  trace_profile *profile = NULL;
406 
407  *result = 0;
408 
409  profile = nsh_trace_profile_find ();
410 
411  if (PREDICT_FALSE (!profile))
412  {
413  return (-1);
414  }
415 
416  trace_data_size = fetch_trace_data_size (profile->trace_type);
417  if (PREDICT_FALSE (trace_data_size == 0))
418  return VNET_API_ERROR_INVALID_VALUE;
419 
420  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
421  return VNET_API_ERROR_INVALID_VALUE;
422 
423  size +=
424  sizeof (nsh_md2_ioam_trace_option_t) +
425  profile->num_elts * trace_data_size;
426  *result = size;
427 
428  return 0;
429 }
430 
431 
432 int
434 {
435  u32 trace_size = 0;
436  nsh_main_t *hm = &nsh_main;
437 
438  trace_profile *profile = NULL;
439 
440 
441  profile = nsh_trace_profile_find ();
442 
443  if (PREDICT_FALSE (!profile))
444  {
445  return (-1);
446  }
447 
448 
449  if (nsh_md2_ioam_trace_get_sizeof_handler (&trace_size) < 0)
450  return (-1);
451 
453 
454  return (0);
455 }
456 
457 
458 
459 /*
460  * fd.io coding-style-patch-verification: ON
461  *
462  * Local Variables:
463  * eval: (c-set-style "gnu")
464  * End:
465  */
u32 as_u32[2]
int nsh_md2_ioam_trace_profile_cleanup(void)
static clib_error_t * nsh_md2_ioam_init(vlib_main_t *vm)
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:862
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
u64 as_u64
Definition: bihash_doc.h:63
#define PREDICT_TRUE(x)
Definition: clib.h:112
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
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define NULL
Definition: clib.h:58
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
static u8 fetch_trace_data_size(u16 trace_type)
Definition: trace_util.h:209
nsh_md2_ioam_trace_stats_t
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
int nsh_md2_ioam_trace_data_list_handler(vlib_buffer_t *b, nsh_tlv_header_t *opt)
unsigned char u8
Definition: types.h:56
int nsh_md2_ioam_trace_pop_handler(vlib_buffer_t *b, nsh_tlv_header_t *opt)
double f64
Definition: types.h:142
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:163
#define always_inline
Definition: clib.h:98
unsigned int u32
Definition: types.h:88
#define clib_error_create(args...)
Definition: error.h:96
#define vlib_call_init_function(vm, x)
Definition: init.h:260
uword size
nsh_md2_ioam_main_t nsh_md2_ioam_main
Definition: nsh_md2_ioam.c:42
#define gm
Definition: dlmalloc.c:1217
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
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:229
#define PREDICT_FALSE(x)
Definition: clib.h:111
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...
clib_error_t * nsh_init(vlib_main_t *vm)
Definition: nsh.c:185
u8 * nsh_md2_ioam_trace_data_list_trace_handler(u8 *s, nsh_tlv_header_t *opt)
vlib_main_t * vm
Definition: buffer.c:312
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:341
nsh_main_t nsh_main
Definition: nsh.c:28
#define ARRAY_LEN(x)
Definition: clib.h:62
#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:155
#define BIT_APPDATA
Definition: trace_util.h:93
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:102
u8 * nsh_trace_main
#define vnet_buffer(b)
Definition: buffer.h:369
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
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
int nsh_md2_ioam_trace_profile_setup(void)
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