FD.io VPP  v21.01.1
Vector Packet Processing
ip6_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 <vppinfra/error.h>
18 #include <vpp/app/version.h>
19 
20 #include <vnet/ip/ip6.h>
21 #include <vnet/ip/ip6_hop_by_hop.h>
23 
24 #include <vppinfra/hash.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/elog.h>
27 #include <vnet/plugin/plugin.h>
28 
32 #include <ioam/udp-ping/udp_ping.h>
35 
36 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
37  * and nanoseconds respectively.
38  */
39 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
40 
41 typedef union
42 {
44  u32 as_u32[2];
45 } time_u64_t;
46 
48 extern ip6_main_t ip6_main;
49 
50 #define foreach_ip6_hop_by_hop_ioam_trace_stats \
51  _(PROCESSED, "Pkts with ip6 hop-by-hop trace options") \
52  _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
53  _(UPDATED, "Pkts with trace updated") \
54  _(FULL, "Pkts with trace options but no space") \
55  _(LOOPBACK, "Pkts with trace options Loopback") \
56  _(LOOPBACK_REPLY, "Pkts with trace options Loopback Reply")
57 
59 #define _(sym,string) string,
61 #undef _
62 };
63 
64 typedef enum
65 {
66 #define _(sym,str) IP6_IOAM_TRACE_##sym,
68 #undef _
71 
72 
73 typedef struct
74 {
75  /* stats */
77 
78  /* convenience */
82 
84 
85 always_inline void
87 {
89 
90  hm->counters[counter_index] += increment;
91 }
92 
93 
94 static u8 *
95 format_ioam_data_list_element (u8 * s, va_list * args)
96 {
97  u32 *elt = va_arg (*args, u32 *);
98  u8 *trace_type_p = va_arg (*args, u8 *);
99  u8 trace_type = *trace_type_p;
100 
101 
102  if (trace_type & BIT_TTL_NODEID)
103  {
104  u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
105  s = format (s, "ttl 0x%x node id 0x%x ",
106  ttl_node_id_host_byte_order >> 24,
107  ttl_node_id_host_byte_order & 0x00FFFFFF);
108 
109  elt++;
110  }
111 
112  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
113  {
114  u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
115  s = format (s, "ingress 0x%x egress 0x%x ",
116  ingress_host_byte_order >> 16,
117  ingress_host_byte_order & 0xFFFF);
118  elt++;
119  }
120 
121  if (trace_type & BIT_TIMESTAMP)
122  {
123  u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
124  s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
125  elt++;
126  }
127 
128  if (trace_type & BIT_APPDATA)
129  {
130  u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
131  s = format (s, "app 0x%x ", appdata_in_host_byte_order);
132  elt++;
133  }
134 
135  return s;
136 }
137 
138 
139 int
141 {
142  u16 size = 0;
143  u8 trace_data_size = 0;
144  trace_profile *profile = NULL;
145 
146  *result = 0;
147 
148  profile = trace_profile_find ();
149 
150  if (PREDICT_FALSE (!profile))
151  {
152  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
153  return (-1);
154  }
155 
156  trace_data_size = fetch_trace_data_size (profile->trace_type);
157  if (PREDICT_FALSE (trace_data_size == 0))
158  return VNET_API_ERROR_INVALID_VALUE;
159 
160  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
161  return VNET_API_ERROR_INVALID_VALUE;
162 
163  size +=
164  sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
165  *result = size;
166 
167  return 0;
168 }
169 
170 
171 
172 int
174  u8 * rewrite_size)
175 {
176  ioam_trace_option_t *trace_option = NULL;
177  u8 trace_data_size = 0;
178  u8 trace_option_elts = 0;
179  trace_profile *profile = NULL;
180 
181 
182  profile = trace_profile_find ();
183 
184  if (PREDICT_FALSE (!profile))
185  {
186  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
187  return (-1);
188  }
189 
190  if (PREDICT_FALSE (!rewrite_string))
191  return -1;
192 
193  trace_option_elts = profile->num_elts;
194  trace_data_size = fetch_trace_data_size (profile->trace_type);
195  trace_option = (ioam_trace_option_t *) rewrite_string;
196  trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
198  trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
199  trace_option_elts * trace_data_size;
200  trace_option->trace_hdr.ioam_trace_type =
201  profile->trace_type & TRACE_TYPE_MASK;
202  trace_option->trace_hdr.data_list_elts_left = trace_option_elts;
203  *rewrite_size =
204  sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
205 
206  return 0;
207 }
208 
209 always_inline void
211  ioam_trace_option_t * trace)
212 {
213  u32 buf_index;
215  vlib_buffer_t *b0;
216  vlib_frame_t *nf = 0;
217  u32 *to_next;
218  vlib_node_t *next_node;
219  ip6_header_t *ip6;
221  ioam_trace_option_t *opt;
222  udp_ping_t *udp;
223 
224  b0 = vlib_buffer_copy (hm->vlib_main, b);
225  if (b0 == NULL)
226  return;
227 
228  buf_index = vlib_get_buffer_index (hm->vlib_main, b0);
229  next_node = vlib_get_node_by_name (hm->vlib_main, (u8 *) "ip6-lookup");
230  nf = vlib_get_frame_to_node (hm->vlib_main, next_node->index);
231  nf->n_vectors = 0;
232  to_next = vlib_frame_vector_args (nf);
233 
234  vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
235  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
236 
237  ip6 = vlib_buffer_get_current (b0);
238  hbh = (ip6_hop_by_hop_header_t *) (ip6 + 1);
239  opt = (ioam_trace_option_t *)
241 
242  udp = (udp_ping_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
243  udp_ping_create_reply_from_probe_ip6 (ip6, hbh, udp);
245 
246  *to_next = buf_index;
247  nf->n_vectors++;
248  to_next++;
249 
250  vlib_put_frame_to_node (hm->vlib_main, next_node->index, nf);
251  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK, 1);
252 }
253 
254 int
257 {
259  u8 elt_index = 0;
260  ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
261  u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
262  ip_adjacency_t *adj = adj_get (adj_index);
263  time_u64_t time_u64;
264  u32 *elt;
265  int rv = 0;
266  trace_profile *profile = NULL;
267 
268 
269  profile = trace_profile_find ();
270 
271  if (PREDICT_FALSE (!profile))
272  {
273  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
274  return (-1);
275  }
276 
277  /* Don't trace loopback reply packets */
278  if (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY)
279  {
280  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK_REPLY,
281  1);
282  return rv;
283  }
284 
285  time_u64.as_u64 = 0;
286 
287  if (PREDICT_TRUE (trace->trace_hdr.data_list_elts_left))
288  {
289  trace->trace_hdr.data_list_elts_left--;
290  /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
291  * to skip to this node's location.
292  */
293  elt_index =
294  trace->trace_hdr.data_list_elts_left *
295  fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
296  elt = &trace->trace_hdr.elts[elt_index];
297  if (trace->trace_hdr.ioam_trace_type & BIT_TTL_NODEID)
298  {
299  *elt =
300  clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
301  elt++;
302  }
303 
304  if (trace->trace_hdr.ioam_trace_type & BIT_ING_INTERFACE)
305  {
306  *elt =
307  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
308  (adj->rewrite_header.sw_if_index & 0xFFFF);
309  *elt = clib_host_to_net_u32 (*elt);
310  elt++;
311  }
312 
313  if (trace->trace_hdr.ioam_trace_type & BIT_TIMESTAMP)
314  {
315  /* Send least significant 32 bits */
316  f64 time_f64 =
317  (f64) (((f64) hm->unix_time_0) +
318  (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
319 
320  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
321  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
322  elt++;
323  }
324 
325  if (trace->trace_hdr.ioam_trace_type & BIT_APPDATA)
326  {
327  /* $$$ set elt0->app_data */
328  *elt = clib_host_to_net_u32 (profile->app_data);
329  elt++;
330  }
331 
332 
333  if (PREDICT_FALSE (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK))
334  {
335  /* if loopback flag set then copy the packet
336  * and send it back to source */
337  ip6_hbh_ioam_loopback_handler (b, ip, trace);
338  }
339 
340  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
341  }
342  else
343  {
344  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
345  }
346  return (rv);
347 }
348 
349 u8 *
352 {
353  ioam_trace_option_t *trace;
354  u8 trace_data_size_in_words = 0;
355  u32 *elt;
356  int elt_index = 0;
357 
358  trace = (ioam_trace_option_t *) opt;
359  s =
360  format (s, " Trace Type 0x%x , %d elts left\n",
361  trace->trace_hdr.ioam_trace_type,
362  trace->trace_hdr.data_list_elts_left);
363  trace_data_size_in_words =
364  fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
365  elt = &trace->trace_hdr.elts[0];
366  while ((u8 *) elt <
367  ((u8 *) (&trace->trace_hdr.elts[0]) + trace->hdr.length - 2
368  /* -2 accounts for ioam_trace_type,elts_left */ ))
369  {
370  s = format (s, " [%d] %U\n", elt_index,
372  elt, &trace->trace_hdr.ioam_trace_type);
373  elt_index++;
374  elt += trace_data_size_in_words;
375  }
376  return (s);
377 }
378 
379 
380 static clib_error_t *
382  unformat_input_t * input,
383  vlib_cli_command_t * cmd)
384 {
386  u8 *s = 0;
387  int i = 0;
388 
389  for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
390  {
391  s =
392  format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
393  hm->counters[i]);
394  }
395 
396  vlib_cli_output (vm, "%v", s);
397  vec_free (s);
398  return 0;
399 }
400 
401 
402 /* *INDENT-OFF* */
403 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
404  .path = "show ioam trace",
405  .short_help = "iOAM trace statistics",
406  .function = ip6_show_ioam_trace_cmd_fn,
407 };
408 /* *INDENT-ON* */
409 
410 /* *INDENT-OFF* */
412  .version = VPP_BUILD_VER,
413  .description = "Inbound Operations, Administration, and Maintenance (OAM)",
414 };
415 /* *INDENT-ON* */
416 
417 static clib_error_t *
419 {
421 
422  hm->vlib_main = vm;
423  hm->vnet_main = vnet_get_main ();
424  clib_memset (hm->counters, 0, sizeof (hm->counters));
425 
426 
431  return (clib_error_create
432  ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
433 
434 
436  sizeof (ioam_trace_option_t),
438  < 0)
439  return (clib_error_create
440  ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
441 
442 
443  return (0);
444 }
445 
446 /* *INDENT-OFF* */
448 {
449  .runs_after = VLIB_INITS ("ip_main_init", "ip6_lookup_init",
450  "ip6_hop_by_hop_ioam_init"),
451 };
452 /* *INDENT-ON* */
453 
454 int
456 {
458 
460 
461  return 0;
462 
463 }
464 
465 
466 int
468 {
469  u32 trace_size = 0;
471 
472  trace_profile *profile = NULL;
473 
474 
475  profile = trace_profile_find ();
476 
477  if (PREDICT_FALSE (!profile))
478  {
479  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
480  return (-1);
481  }
482 
483 
484  if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
485  return (-1);
486 
488 
489  return (0);
490 }
491 
492 /*
493  * fd.io coding-style-patch-verification: ON
494  *
495  * Local Variables:
496  * eval: (c-set-style "gnu")
497  * End:
498  */
u32 as_u32[2]
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: vlib_api_cli.c:899
static void ip6_hbh_ioam_trace_set_bit(ioam_trace_option_t *trace, u8 trace_bit)
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define PREDICT_TRUE(x)
Definition: clib.h:122
#define foreach_ip6_hop_by_hop_ioam_trace_stats
unsigned long u64
Definition: types.h:89
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:280
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:334
IP unicast adjacency.
Definition: adj.h:235
int ip6_hbh_register_option(u8 option, int options(vlib_buffer_t *b, ip6_header_t *ip, ip6_hop_by_hop_option_t *opt), u8 *trace(u8 *s, ip6_hop_by_hop_option_t *opt))
Definition: ip6_forward.c:2729
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
vlib_main_t * vm
Definition: in2out_ed.c:1580
unsigned char u8
Definition: types.h:56
static vlib_buffer_t * vlib_buffer_copy(vlib_main_t *vm, vlib_buffer_t *b)
double f64
Definition: types.h:142
static ip_adjacency_t * adj_get(adj_index_t adj_index)
Get a pointer to an adjacency object from its index.
Definition: adj.h:467
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
int ip6_trace_profile_setup(void)
int ip6_hop_by_hop_ioam_trace_rewrite_handler(u8 *rewrite_string, u8 *rewrite_size)
vl_api_ip6_address_t ip6
Definition: one.api:424
ip6_hop_by_hop_ioam_trace_main_t ip6_hop_by_hop_ioam_trace_main
description fragment has unexpected format
Definition: map.api:433
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
const cJSON *const b
Definition: cJSON.h:255
unsigned int u32
Definition: types.h:88
#define clib_error_create(args...)
Definition: error.h:96
u8 options_size[MAX_IP6_HBH_OPTION]
VLIB_PLUGIN_REGISTER()
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:293
ip6_ioam_trace_stats_t
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main
u32 size
Definition: vhost_user.h:106
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:216
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:233
#define PREDICT_FALSE(x)
Definition: clib.h:121
#define always_inline
Definition: ipsec.h:28
ip6_main_t ip6_main
Definition: ip6_forward.c:2785
static u8 * format_ioam_data_list_element(u8 *s, va_list *args)
int ip6_hbh_ioam_trace_data_list_handler(vlib_buffer_t *b, ip6_header_t *ip, ip6_hop_by_hop_option_t *opt)
static u32 counter_index(vlib_main_t *vm, vlib_error_t e)
Definition: drop.c:67
int ip6_trace_profile_cleanup(void)
#define BIT_LOOPBACK
Definition: trace_util.h:94
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
static void ip6_hbh_ioam_loopback_handler(vlib_buffer_t *b, ip6_header_t *ip, ioam_trace_option_t *trace)
u16 n_vectors
Definition: node.h:397
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
static char * ip6_hop_by_hop_ioam_trace_stats_strings[]
static void ip6_ioam_trace_stats_increment_counter(u32 counter_index, u64 increment)
static void udp_ping_create_reply_from_probe_ip6(ip6_header_t *ip, ip6_hop_by_hop_header_t *hbh, udp_ping_t *udp)
Create and send ipv6 udp-ping response packet.
Definition: udp_ping_util.h:48
static clib_error_t * ip6_show_ioam_trace_cmd_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
#define ARRAY_LEN(x)
Definition: clib.h:67
#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:158
#define BIT_APPDATA
Definition: trace_util.h:93
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
#define BIT_LOOPBACK_REPLY
Definition: trace_util.h:95
#define HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST
Definition: defs.h:47
vl_api_address_t ip
Definition: l2.api:501
VLIB buffer representation.
Definition: buffer.h:102
static ip6_hop_by_hop_option_t * ip6_hbh_get_option(ip6_hop_by_hop_header_t *hbh0, u8 option_to_search)
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
#define HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE
static clib_error_t * ip6_hop_by_hop_ioam_trace_init(vlib_main_t *vm)
#define vnet_buffer(b)
Definition: buffer.h:417
int ip6_hbh_add_register_option(u8 option, u8 size, int rewrite_options(u8 *rewrite_string, u8 *rewrite_size))
int ip6_ioam_trace_get_sizeof_handler(u32 *result)
static f64 trace_tsp_mul[4]
u8 * ip6_hbh_ioam_trace_data_list_trace_handler(u8 *s, ip6_hop_by_hop_option_t *opt)
#define VLIB_INITS(...)
Definition: init.h:357
Definition: defs.h:46
#define BIT_TIMESTAMP
Definition: trace_util.h:92
u64 counters[ARRAY_LEN(ip6_hop_by_hop_ioam_trace_stats_strings)]