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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vlib/vlib.h>
16 #include <vnet/vnet.h>
17 #include <vppinfra/error.h>
19 #include <vnet/vxlan-gpe/vxlan_gpe.h>
20 #include <vnet/vxlan-gpe/vxlan_gpe_packet.h>
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/elog.h>
26 #include <ioam/lib-trace/trace_util.h>
27 #include <ioam/lib-trace/trace_config.h>
28 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
30 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
31 * and nanoseconds respectively.
33 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
42 typedef CLIB_PACKED(struct {
43 vxlan_gpe_ioam_option_t hdr;
45 u8 data_list_elts_left;
46 u32 elts[0]; /* Variable type. So keep it generic */
47 }) vxlan_gpe_ioam_trace_option_t;
50 #define foreach_vxlan_gpe_ioam_trace_stats \
51 _(SUCCESS, "Pkts updated with TRACE records") \
52 _(FAILED, "Errors in TRACE due to lack of TRACE records")
54 static char *vxlan_gpe_ioam_trace_stats_strings[] = {
55 #define _(sym,string) string,
56 foreach_vxlan_gpe_ioam_trace_stats
62 #define _(sym,str) VXLAN_GPE_IOAM_TRACE_##sym,
63 foreach_vxlan_gpe_ioam_trace_stats
65 VXLAN_GPE_IOAM_TRACE_N_STATS,
66 } vxlan_gpe_ioam_trace_stats_t;
72 u64 counters[ARRAY_LEN (vxlan_gpe_ioam_trace_stats_strings)];
75 vlib_main_t *vlib_main;
76 vnet_main_t *vnet_main;
77 } vxlan_gpe_ioam_trace_main_t;
79 vxlan_gpe_ioam_trace_main_t vxlan_gpe_ioam_trace_main;
82 vxlan_gpe_ioam_add_register_option (u8 option,
84 int rewrite_options (u8 * rewrite_string,
87 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
89 ASSERT ((u32) option < ARRAY_LEN (hm->add_options));
91 /* Already registered */
92 if (hm->add_options[option])
95 hm->add_options[option] = rewrite_options;
96 hm->options_size[option] = size;
102 vxlan_gpe_add_unregister_option (u8 option)
104 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
106 ASSERT ((u32) option < ARRAY_LEN (hm->add_options));
109 if (!hm->add_options[option])
112 hm->add_options[option] = NULL;
113 hm->options_size[option] = 0;
119 vxlan_gpe_ioam_register_option (u8 option,
120 int options (vlib_buffer_t * b,
121 vxlan_gpe_ioam_option_t * opt,
122 u8 is_ipv4, u8 use_adj),
124 vxlan_gpe_ioam_option_t * opt))
126 vxlan_gpe_ioam_main_t *im = &vxlan_gpe_ioam_main;
128 ASSERT ((u32) option < ARRAY_LEN (im->options));
130 /* Already registered */
131 if (im->options[option])
134 im->options[option] = options;
135 im->trace[option] = trace;
141 vxlan_gpe_ioam_unregister_option (u8 option)
143 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
145 ASSERT ((u32) option < ARRAY_LEN (hm->options));
148 if (!hm->options[option])
151 hm->options[option] = NULL;
152 hm->trace[option] = NULL;
159 vxlan_gpe_ioam_trace_stats_increment_counter (u32 counter_index,
162 vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
164 hm->counters[counter_index] += increment;
169 format_ioam_data_list_element (u8 * s, va_list * args)
171 u32 *elt = va_arg (*args, u32 *);
172 u8 *trace_type_p = va_arg (*args, u8 *);
173 u8 trace_type = *trace_type_p;
176 if (trace_type & BIT_TTL_NODEID)
178 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
179 s = format (s, "ttl 0x%x node id 0x%x ",
180 ttl_node_id_host_byte_order >> 24,
181 ttl_node_id_host_byte_order & 0x00FFFFFF);
186 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
188 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
189 s = format (s, "ingress 0x%x egress 0x%x ",
190 ingress_host_byte_order >> 16,
191 ingress_host_byte_order & 0xFFFF);
195 if (trace_type & BIT_TIMESTAMP)
197 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
198 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
202 if (trace_type & BIT_APPDATA)
204 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
205 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
215 vxlan_gpe_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
217 vxlan_gpe_ioam_trace_option_t *trace_option = NULL;
218 u8 trace_data_size = 0;
219 u8 trace_option_elts = 0;
220 trace_profile *profile = NULL;
223 profile = trace_profile_find ();
225 if (PREDICT_FALSE (!profile))
230 if (PREDICT_FALSE (!rewrite_string))
233 trace_option_elts = profile->num_elts;
234 trace_data_size = fetch_trace_data_size (profile->trace_type);
235 trace_option = (vxlan_gpe_ioam_trace_option_t *) rewrite_string;
236 trace_option->hdr.type = VXLAN_GPE_OPTION_TYPE_IOAM_TRACE;
237 trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
238 trace_option_elts * trace_data_size;
239 trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
240 trace_option->data_list_elts_left = trace_option_elts;
242 sizeof (vxlan_gpe_ioam_trace_option_t) +
243 (trace_option_elts * trace_data_size);
250 vxlan_gpe_ioam_trace_data_list_handler (vlib_buffer_t * b,
251 vxlan_gpe_ioam_option_t * opt,
252 u8 is_ipv4, u8 use_adj)
255 vxlan_gpe_ioam_trace_option_t *trace =
256 (vxlan_gpe_ioam_trace_option_t *) opt;
260 trace_profile *profile = NULL;
261 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
264 profile = trace_profile_find ();
266 if (PREDICT_FALSE (!profile))
274 if (PREDICT_TRUE (trace->data_list_elts_left))
276 trace->data_list_elts_left--;
277 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
278 * to skip to this node's location.
281 trace->data_list_elts_left *
282 fetch_trace_data_size (trace->ioam_trace_type) / 4;
283 elt = &trace->elts[elt_index];
286 if (trace->ioam_trace_type & BIT_TTL_NODEID)
288 ip4_header_t *ip0 = vlib_buffer_get_current (b);
289 /* The transit case is the only case where the TTL decrement happens
290 * before iOAM processing. For now, use the use_adj flag as an overload.
291 * We can probably use a separate flag instead of overloading the use_adj flag.
293 *elt = clib_host_to_net_u32 (((ip0->ttl - 1 + use_adj) << 24) |
298 if (trace->ioam_trace_type & BIT_ING_INTERFACE)
301 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
305 ip_adjacency_t *adj = adj_get (adj_index);
306 tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
310 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
312 *elt = clib_host_to_net_u32 (*elt);
318 if (trace->ioam_trace_type & BIT_TTL_NODEID)
320 ip6_header_t *ip0 = vlib_buffer_get_current (b);
321 *elt = clib_host_to_net_u32 ((ip0->hop_limit << 24) |
325 if (trace->ioam_trace_type & BIT_ING_INTERFACE)
328 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
332 ip_adjacency_t *adj = adj_get (adj_index);
333 tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
337 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
339 *elt = clib_host_to_net_u32 (*elt);
344 if (trace->ioam_trace_type & BIT_TIMESTAMP)
346 /* Send least significant 32 bits */
348 (f64) (((f64) hm->unix_time_0) +
349 (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
351 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
352 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
356 if (trace->ioam_trace_type & BIT_APPDATA)
358 /* $$$ set elt0->app_data */
359 *elt = clib_host_to_net_u32 (profile->app_data);
362 vxlan_gpe_ioam_trace_stats_increment_counter
363 (VXLAN_GPE_IOAM_TRACE_SUCCESS, 1);
367 vxlan_gpe_ioam_trace_stats_increment_counter
368 (VXLAN_GPE_IOAM_TRACE_FAILED, 1);
374 vxlan_gpe_ioam_trace_data_list_trace_handler (u8 * s,
375 vxlan_gpe_ioam_option_t * opt)
377 vxlan_gpe_ioam_trace_option_t *trace;
378 u8 trace_data_size_in_words = 0;
382 trace = (vxlan_gpe_ioam_trace_option_t *) opt;
384 format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
385 trace->data_list_elts_left);
386 trace_data_size_in_words =
387 fetch_trace_data_size (trace->ioam_trace_type) / 4;
388 elt = &trace->elts[0];
389 while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
390 /* -2 accounts for ioam_trace_type,elts_left */ ))
392 s = format (s, " [%d] %U\n", elt_index,
393 format_ioam_data_list_element,
394 elt, &trace->ioam_trace_type);
396 elt += trace_data_size_in_words;
402 static clib_error_t *
403 vxlan_gpe_show_ioam_trace_cmd_fn (vlib_main_t * vm,
404 unformat_input_t * input,
405 vlib_cli_command_t * cmd)
407 vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
411 for (i = 0; i < VXLAN_GPE_IOAM_TRACE_N_STATS; i++)
413 s = format (s, " %s - %lu\n", vxlan_gpe_ioam_trace_stats_strings[i],
417 vlib_cli_output (vm, "%v", s);
423 VLIB_CLI_COMMAND (vxlan_gpe_show_ioam_trace_cmd, static) = {
424 .path = "show ioam vxlan-gpe trace",
425 .short_help = "iOAM trace statistics",
426 .function = vxlan_gpe_show_ioam_trace_cmd_fn,
430 static clib_error_t *
431 vxlan_gpe_ioam_trace_init (vlib_main_t * vm)
433 vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
436 hm->vnet_main = vnet_get_main ();
437 clib_memset (hm->counters, 0, sizeof (hm->counters));
439 if (vxlan_gpe_ioam_register_option
440 (VXLAN_GPE_OPTION_TYPE_IOAM_TRACE,
441 vxlan_gpe_ioam_trace_data_list_handler,
442 vxlan_gpe_ioam_trace_data_list_trace_handler) < 0)
443 return (clib_error_create
444 ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE failed"));
447 if (vxlan_gpe_ioam_add_register_option
448 (VXLAN_GPE_OPTION_TYPE_IOAM_TRACE,
449 sizeof (vxlan_gpe_ioam_trace_option_t),
450 vxlan_gpe_ioam_trace_rewrite_handler) < 0)
451 return (clib_error_create
452 ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE for rewrite failed"));
458 VLIB_INIT_FUNCTION (vxlan_gpe_ioam_trace_init) =
460 .runs_after = VLIB_INITS("ip_main_init", "ip6_lookup_init",
466 vxlan_gpe_trace_profile_cleanup (void)
468 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
470 hm->options_size[VXLAN_GPE_OPTION_TYPE_IOAM_TRACE] = 0;
477 vxlan_gpe_ioam_trace_get_sizeof_handler (u32 * result)
480 u8 trace_data_size = 0;
481 trace_profile *profile = NULL;
485 profile = trace_profile_find ();
487 if (PREDICT_FALSE (!profile))
492 trace_data_size = fetch_trace_data_size (profile->trace_type);
493 if (PREDICT_FALSE (trace_data_size == 0))
494 return VNET_API_ERROR_INVALID_VALUE;
496 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
497 return VNET_API_ERROR_INVALID_VALUE;
500 sizeof (vxlan_gpe_ioam_trace_option_t) +
501 profile->num_elts * trace_data_size;
509 vxlan_gpe_trace_profile_setup (void)
512 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
514 trace_profile *profile = NULL;
517 profile = trace_profile_find ();
519 if (PREDICT_FALSE (!profile))
525 if (vxlan_gpe_ioam_trace_get_sizeof_handler (&trace_size) < 0)
528 hm->options_size[VXLAN_GPE_OPTION_TYPE_IOAM_TRACE] = trace_size;
536 * fd.io coding-style-patch-verification: ON
539 * eval: (c-set-style "gnu")