FD.io VPP  v16.12-rc0-308-g931be3a
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 
29 
30 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
31  * and nanoseconds respectively.
32  */
33 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
34 
35 typedef union
36 {
38  u32 as_u32[2];
39 } time_u64_t;
40 
41 
43 extern ip6_main_t ip6_main;
44 
45 #define foreach_ip6_hop_by_hop_ioam_trace_stats \
46  _(PROCESSED, "Pkts with ip6 hop-by-hop trace options") \
47  _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
48  _(UPDATED, "Pkts with trace updated") \
49  _(FULL, "Pkts with trace options but no space")
50 
52 #define _(sym,string) string,
54 #undef _
55 };
56 
57 typedef enum
58 {
59 #define _(sym,str) IP6_IOAM_TRACE_##sym,
61 #undef _
64 
65 
66 typedef struct
67 {
68  /* stats */
70 
71  /* convenience */
75 
77 
78 always_inline void
80 {
82 
83  hm->counters[counter_index] += increment;
84 }
85 
86 
87 static u8 *
88 format_ioam_data_list_element (u8 * s, va_list * args)
89 {
90  u32 *elt = va_arg (*args, u32 *);
91  u8 *trace_type_p = va_arg (*args, u8 *);
92  u8 trace_type = *trace_type_p;
93 
94 
95  if (trace_type & BIT_TTL_NODEID)
96  {
97  u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
98  s = format (s, "ttl 0x%x node id 0x%x ",
99  ttl_node_id_host_byte_order >> 24,
100  ttl_node_id_host_byte_order & 0x00FFFFFF);
101 
102  elt++;
103  }
104 
105  if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
106  {
107  u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
108  s = format (s, "ingress 0x%x egress 0x%x ",
109  ingress_host_byte_order >> 16,
110  ingress_host_byte_order & 0xFFFF);
111  elt++;
112  }
113 
114  if (trace_type & BIT_TIMESTAMP)
115  {
116  u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
117  s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
118  elt++;
119  }
120 
121  if (trace_type & BIT_APPDATA)
122  {
123  u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
124  s = format (s, "app 0x%x ", appdata_in_host_byte_order);
125  elt++;
126  }
127 
128  return s;
129 }
130 
131 
132 int
134 {
135  u16 size = 0;
136  u8 trace_data_size = 0;
137  trace_profile *profile = NULL;
138 
139  *result = 0;
140 
141  profile = trace_profile_find ();
142 
143  if (PREDICT_FALSE (!profile))
144  {
145  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
146  return (-1);
147  }
148 
149  trace_data_size = fetch_trace_data_size (profile->trace_type);
150  if (PREDICT_FALSE (trace_data_size == 0))
151  return VNET_API_ERROR_INVALID_VALUE;
152 
153  if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
154  return VNET_API_ERROR_INVALID_VALUE;
155 
156  size +=
157  sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
158  *result = size;
159 
160  return 0;
161 }
162 
163 
164 
165 int
167  u8 * rewrite_size)
168 {
169  ioam_trace_option_t *trace_option = NULL;
170  u8 trace_data_size = 0;
171  u8 trace_option_elts = 0;
172  trace_profile *profile = NULL;
173 
174 
175  profile = trace_profile_find ();
176 
177  if (PREDICT_FALSE (!profile))
178  {
179  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
180  return (-1);
181  }
182 
183  if (PREDICT_FALSE (!rewrite_string))
184  return -1;
185 
186  trace_option_elts = profile->num_elts;
187  trace_data_size = fetch_trace_data_size (profile->trace_type);
188  trace_option = (ioam_trace_option_t *) rewrite_string;
189  trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
191  trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
192  trace_option_elts * trace_data_size;
193  trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
194  trace_option->data_list_elts_left = trace_option_elts;
195  *rewrite_size =
196  sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
197 
198  return 0;
199 }
200 
201 
202 int
205 {
206  ip6_main_t *im = &ip6_main;
207  ip_lookup_main_t *lm = &im->lookup_main;
209  u8 elt_index = 0;
210  ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
211  u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
212  ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
213  time_u64_t time_u64;
214  u32 *elt;
215  int rv = 0;
216  trace_profile *profile = NULL;
217 
218 
219  profile = trace_profile_find ();
220 
221  if (PREDICT_FALSE (!profile))
222  {
223  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
224  return (-1);
225  }
226 
227 
228  time_u64.as_u64 = 0;
229 
230  if (PREDICT_TRUE (trace->data_list_elts_left))
231  {
232  trace->data_list_elts_left--;
233  /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
234  * to skip to this node's location.
235  */
236  elt_index =
237  trace->data_list_elts_left *
238  fetch_trace_data_size (trace->ioam_trace_type) / 4;
239  elt = &trace->elts[elt_index];
240  if (trace->ioam_trace_type & BIT_TTL_NODEID)
241  {
242  *elt =
243  clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
244  elt++;
245  }
246 
247  if (trace->ioam_trace_type & BIT_ING_INTERFACE)
248  {
249  *elt =
250  (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
251  (adj->rewrite_header.sw_if_index & 0xFFFF);
252  *elt = clib_host_to_net_u32 (*elt);
253  elt++;
254  }
255 
256  if (trace->ioam_trace_type & BIT_TIMESTAMP)
257  {
258  /* Send least significant 32 bits */
259  f64 time_f64 =
260  (f64) (((f64) hm->unix_time_0) +
261  (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
262 
263  time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
264  *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
265  elt++;
266  }
267 
268  if (trace->ioam_trace_type & BIT_APPDATA)
269  {
270  /* $$$ set elt0->app_data */
271  *elt = clib_host_to_net_u32 (profile->app_data);
272  elt++;
273  }
274  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
275  }
276  else
277  {
278  ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
279  }
280  return (rv);
281 }
282 
283 u8 *
286 {
287  ioam_trace_option_t *trace;
288  u8 trace_data_size_in_words = 0;
289  u32 *elt;
290  int elt_index = 0;
291 
292  trace = (ioam_trace_option_t *) opt;
293 #if 0
294  s =
295  format (s, " Trace Type 0x%x , %d elts left ts msb(s) 0x%x\n",
296  trace->ioam_trace_type, trace->data_list_elts_left,
297  t->timestamp_msbs);
298 #endif
299  s =
300  format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
301  trace->data_list_elts_left);
302  trace_data_size_in_words =
303  fetch_trace_data_size (trace->ioam_trace_type) / 4;
304  elt = &trace->elts[0];
305  while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
306  /* -2 accounts for ioam_trace_type,elts_left */ ))
307  {
308  s = format (s, " [%d] %U\n", elt_index,
310  elt, &trace->ioam_trace_type);
311  elt_index++;
312  elt += trace_data_size_in_words;
313  }
314  return (s);
315 }
316 
317 
318 static clib_error_t *
320  unformat_input_t * input,
321  vlib_cli_command_t * cmd)
322 {
324  u8 *s = 0;
325  int i = 0;
326 
327  for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
328  {
329  s =
330  format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
331  hm->counters[i]);
332  }
333 
334  vlib_cli_output (vm, "%v", s);
335  vec_free (s);
336  return 0;
337 }
338 
339 
340 /* *INDENT-OFF* */
341 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
342  .path = "show ioam trace",
343  .short_help = "iOAM trace statistics",
344  .function = ip6_show_ioam_trace_cmd_fn,
345 };
346 /* *INDENT-ON* */
347 
348 
349 static clib_error_t *
351 {
353  clib_error_t *error;
354 
355  if ((error = vlib_call_init_function (vm, ip_main_init)))
356  return (error);
357 
358  if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
359  return error;
360 
361  if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
362  return (error);
363 
364  hm->vlib_main = vm;
365  hm->vnet_main = vnet_get_main ();
366  memset (hm->counters, 0, sizeof (hm->counters));
367 
368 
373  return (clib_error_create
374  ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
375 
376 
378  sizeof (ioam_trace_option_t),
380  < 0)
381  return (clib_error_create
382  ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
383 
384 
385  return (0);
386 }
387 
389 
390 /*
391  * fd.io coding-style-patch-verification: ON
392  *
393  * Local Variables:
394  * eval: (c-set-style "gnu")
395  * End:
396  */
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
u32 as_u32[2]
static vlib_cli_command_t trace
(constructor) VLIB_CLI_COMMAND (trace)
Definition: memory_vlib.c:1211
#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:174
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:2602
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
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
ip6_ioam_trace_stats_t
ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main
int ioam_trace_get_sizeof_handler(u32 *result)
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define clib_error_create(args...)
Definition: error.h:108
ip6_main_t ip6_main
Definition: ip6_forward.c:2655
static u8 fetch_trace_data_size(u8 trace_type)
Definition: trace_util.h:223
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:575
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 char * ip6_hop_by_hop_ioam_trace_stats_strings[]
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
#define BIT_ING_INTERFACE
Definition: trace_util.h:97
#define BIT_TTL_NODEID
Definition: trace_util.h:96
#define TRACE_TYPE_MASK
Definition: trace_util.h:101
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define BIT_APPDATA
Definition: trace_util.h:100
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:333
ip_lookup_main_t lookup_main
Definition: ip6.h:132
u32 size
Definition: vhost-user.h:76
static trace_profile * trace_profile_find(void)
Definition: trace_util.h:80
#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
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
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
static clib_error_t * ip6_lookup_init(vlib_main_t *vm)
Definition: ip6_forward.c:2658
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:99
static ip_adjacency_t * ip_get_adjacency(ip_lookup_main_t *lm, u32 adj_index)
Definition: lookup.h:385
u64 counters[ARRAY_LEN(ip6_hop_by_hop_ioam_trace_stats_strings)]