FD.io VPP  v17.01.1-3-gc6833f8
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 
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 
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 {
39  u32 as_u32[2];
40 } time_u64_t;
41 
42 /* *INDENT-OFF* */
43 typedef CLIB_PACKED(struct {
45  u8 ioam_trace_type;
46  u8 data_list_elts_left;
47  u32 elts[0]; /* Variable type. So keep it generic */
48 }) ioam_trace_option_t;
49 /* *INDENT-ON* */
50 
51 
53 extern ip6_main_t ip6_main;
54 
55 #define foreach_ip6_hop_by_hop_ioam_trace_stats \
56  _(PROCESSED, "Pkts with ip6 hop-by-hop trace options") \
57  _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
58  _(UPDATED, "Pkts with trace updated") \
59  _(FULL, "Pkts with trace options but no space")
60 
61 static char *ip6_hop_by_hop_ioam_trace_stats_strings[] = {
62 #define _(sym,string) string,
64 #undef _
65 };
66 
67 typedef enum
68 {
69 #define _(sym,str) IP6_IOAM_TRACE_##sym,
71 #undef _
74 
75 
76 typedef struct
77 {
78  /* stats */
79  u64 counters[ARRAY_LEN (ip6_hop_by_hop_ioam_trace_stats_strings)];
80 
81  /* convenience */
85 
87 
88 always_inline void
90 {
92 
93  hm->counters[counter_index] += increment;
94 }
95 
96 
97 static u8 *
98 format_ioam_data_list_element (u8 * s, va_list * args)
99 {
100  u32 *elt = va_arg (*args, u32 *);
101  u8 *trace_type_p = va_arg (*args, u8 *);
102  u8 trace_type = *trace_type_p;
103 
104 
105  if (trace_type & BIT_TTL_NODEID)
106  {
107  u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
108  s = format (s, "ttl 0x%x node id 0x%x ",
109  ttl_node_id_host_byte_order >> 24,
110  ttl_node_id_host_byte_order & 0x00FFFFFF);
111 
112  elt++;
113  }
114 
115  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
116  {
117  u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
118  s = format (s, "ingress 0x%x egress 0x%x ",
119  ingress_host_byte_order >> 16,
120  ingress_host_byte_order & 0xFFFF);
121  elt++;
122  }
123 
124  if (trace_type & BIT_TIMESTAMP)
125  {
126  u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
127  s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
128  elt++;
129  }
130 
131  if (trace_type & BIT_APPDATA)
132  {
133  u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
134  s = format (s, "app 0x%x ", appdata_in_host_byte_order);
135  elt++;
136  }
137 
138  return s;
139 }
140 
141 
142 int
144 {
145  u16 size = 0;
146  u8 trace_data_size = 0;
147  trace_profile *profile = NULL;
148 
149  *result = 0;
150 
151  profile = trace_profile_find ();
152 
153  if (PREDICT_FALSE (!profile))
154  {
155  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
156  return (-1);
157  }
158 
159  trace_data_size = fetch_trace_data_size (profile->trace_type);
160  if (PREDICT_FALSE (trace_data_size == 0))
161  return VNET_API_ERROR_INVALID_VALUE;
162 
163  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
164  return VNET_API_ERROR_INVALID_VALUE;
165 
166  size +=
167  sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
168  *result = size;
169 
170  return 0;
171 }
172 
173 
174 
175 int
177  u8 * rewrite_size)
178 {
179  ioam_trace_option_t *trace_option = NULL;
180  u8 trace_data_size = 0;
181  u8 trace_option_elts = 0;
182  trace_profile *profile = NULL;
183 
184 
185  profile = trace_profile_find ();
186 
187  if (PREDICT_FALSE (!profile))
188  {
189  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
190  return (-1);
191  }
192 
193  if (PREDICT_FALSE (!rewrite_string))
194  return -1;
195 
196  trace_option_elts = profile->num_elts;
197  trace_data_size = fetch_trace_data_size (profile->trace_type);
198  trace_option = (ioam_trace_option_t *) rewrite_string;
199  trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
201  trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
202  trace_option_elts * trace_data_size;
203  trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
204  trace_option->data_list_elts_left = trace_option_elts;
205  *rewrite_size =
206  sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
207 
208  return 0;
209 }
210 
211 
212 int
215 {
216  ip6_main_t *im = &ip6_main;
217  ip_lookup_main_t *lm = &im->lookup_main;
219  u8 elt_index = 0;
220  ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
221  u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
222  ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
223  time_u64_t time_u64;
224  u32 *elt;
225  int rv = 0;
226  trace_profile *profile = NULL;
227 
228 
229  profile = trace_profile_find ();
230 
231  if (PREDICT_FALSE (!profile))
232  {
233  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
234  return (-1);
235  }
236 
237 
238  time_u64.as_u64 = 0;
239 
240  if (PREDICT_TRUE (trace->data_list_elts_left))
241  {
242  trace->data_list_elts_left--;
243  /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
244  * to skip to this node's location.
245  */
246  elt_index =
247  trace->data_list_elts_left *
248  fetch_trace_data_size (trace->ioam_trace_type) / 4;
249  elt = &trace->elts[elt_index];
250  if (trace->ioam_trace_type & BIT_TTL_NODEID)
251  {
252  *elt =
253  clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
254  elt++;
255  }
256 
257  if (trace->ioam_trace_type & BIT_ING_INTERFACE)
258  {
259  *elt =
260  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
261  (adj->rewrite_header.sw_if_index & 0xFFFF);
262  *elt = clib_host_to_net_u32 (*elt);
263  elt++;
264  }
265 
266  if (trace->ioam_trace_type & BIT_TIMESTAMP)
267  {
268  /* Send least significant 32 bits */
269  f64 time_f64 =
270  (f64) (((f64) hm->unix_time_0) +
271  (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
272 
273  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
274  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
275  elt++;
276  }
277 
278  if (trace->ioam_trace_type & BIT_APPDATA)
279  {
280  /* $$$ set elt0->app_data */
281  *elt = clib_host_to_net_u32 (profile->app_data);
282  elt++;
283  }
284  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
285  }
286  else
287  {
288  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
289  }
290  return (rv);
291 }
292 
293 u8 *
296 {
297  ioam_trace_option_t *trace;
298  u8 trace_data_size_in_words = 0;
299  u32 *elt;
300  int elt_index = 0;
301 
302  trace = (ioam_trace_option_t *) opt;
303  s =
304  format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
305  trace->data_list_elts_left);
306  trace_data_size_in_words =
307  fetch_trace_data_size (trace->ioam_trace_type) / 4;
308  elt = &trace->elts[0];
309  while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
310  /* -2 accounts for ioam_trace_type,elts_left */ ))
311  {
312  s = format (s, " [%d] %U\n", elt_index,
314  elt, &trace->ioam_trace_type);
315  elt_index++;
316  elt += trace_data_size_in_words;
317  }
318  return (s);
319 }
320 
321 
322 static clib_error_t *
324  unformat_input_t * input,
325  vlib_cli_command_t * cmd)
326 {
328  u8 *s = 0;
329  int i = 0;
330 
331  for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
332  {
333  s =
334  format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
335  hm->counters[i]);
336  }
337 
338  vlib_cli_output (vm, "%v", s);
339  vec_free (s);
340  return 0;
341 }
342 
343 
344 /* *INDENT-OFF* */
345 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
346  .path = "show ioam trace",
347  .short_help = "iOAM trace statistics",
348  .function = ip6_show_ioam_trace_cmd_fn,
349 };
350 /* *INDENT-ON* */
351 
352 /*
353  * This routine exists to convince the vlib plugin framework that
354  * we haven't accidentally copied a random .dll into the plugin directory.
355  *
356  * Also collects global variable pointers passed from the vpp engine
357  */
358 clib_error_t *
360  int from_early_init)
361 {
362  return 0;
363 }
364 
365 static clib_error_t *
367 {
369  clib_error_t *error;
370 
371  if ((error = vlib_call_init_function (vm, ip_main_init)))
372  return (error);
373 
374  if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
375  return error;
376 
378  return (error);
379 
380  hm->vlib_main = vm;
381  hm->vnet_main = vnet_get_main ();
382  memset (hm->counters, 0, sizeof (hm->counters));
383 
384 
389  return (clib_error_create
390  ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
391 
392 
394  sizeof (ioam_trace_option_t),
396  < 0)
397  return (clib_error_create
398  ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
399 
400 
401  return (0);
402 }
403 
404 int
406 {
408 
410 
411  return 0;
412 
413 }
414 
415 
416 int
418 {
419  u32 trace_size = 0;
421 
422  trace_profile *profile = NULL;
423 
424 
425  profile = trace_profile_find ();
426 
427  if (PREDICT_FALSE (!profile))
428  {
429  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
430  return (-1);
431  }
432 
433 
434  if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
435  return (-1);
436 
438 
439  return (0);
440 }
441 
442 
444 
445 /*
446  * fd.io coding-style-patch-verification: ON
447  *
448  * Local Variables:
449  * eval: (c-set-style "gnu")
450  * End:
451  */
u32 as_u32[2]
ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: memory_vlib.c:1115
#define PREDICT_TRUE(x)
Definition: clib.h:98
#define foreach_ip6_hop_by_hop_ioam_trace_stats
#define NULL
Definition: clib.h:55
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
IP unicast adjacency.
Definition: lookup.h:188
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:2772
typedef CLIB_PACKED(struct{ip6_hop_by_hop_option_t hdr;u8 ioam_trace_type;u8 data_list_elts_left;u32 elts[0];})
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#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 vlib_call_init_function(vm, x)
Definition: init.h:161
u8 options_size[MAX_IP6_HBH_OPTION]
ip6_ioam_trace_stats_t
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define clib_error_create(args...)
Definition: error.h:108
static u8 fetch_trace_data_size(u8 trace_type)
Definition: trace_util.h:212
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)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
int ip6_trace_profile_cleanup(void)
clib_error_t * ip_main_init(vlib_main_t *vm)
Definition: ip_init.c:45
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static void ip6_ioam_trace_stats_increment_counter(u32 counter_index, u64 increment)
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
clib_error_t * vlib_plugin_register(vlib_main_t *vm, vnet_plugin_handoff_t *h, int from_early_init)
#define BIT_ING_INTERFACE
Definition: trace_util.h:95
#define BIT_TTL_NODEID
Definition: trace_util.h:94
#define TRACE_TYPE_MASK
Definition: trace_util.h:99
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define BIT_APPDATA
Definition: trace_util.h:98
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:361
ip6_main_t ip6_main
Definition: ip6_forward.c:2828
ip_lookup_main_t lookup_main
Definition: ip6.h:135
u64 size
Definition: vhost-user.h:74
#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
#define HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE
static clib_error_t * ip6_hop_by_hop_ioam_trace_init(vlib_main_t *vm)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static trace_profile * trace_profile_find(void)
Definition: trace_util.h:78
int ip6_hbh_add_register_option(u8 option, u8 size, int rewrite_options(u8 *rewrite_string, u8 *rewrite_size))
struct _unformat_input_t unformat_input_t
int ip6_ioam_trace_get_sizeof_handler(u32 *result)
static clib_error_t * ip6_lookup_init(vlib_main_t *vm)
Definition: ip6_forward.c:2831
static f64 trace_tsp_mul[4]
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:97
static ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:415
u64 counters[ARRAY_LEN(ip6_hop_by_hop_ioam_trace_stats_strings)]