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