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 <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
20 #include <vnet/vxlan-gpe/vxlan_gpe.h>
21 #include <vnet/vxlan-gpe/vxlan_gpe_packet.h>
23 #include <vppinfra/hash.h>
24 #include <vppinfra/error.h>
25 #include <vppinfra/elog.h>
27 #include <ioam/lib-trace/trace_util.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 };
43 typedef CLIB_PACKED(struct {
44 vxlan_gpe_ioam_option_t hdr;
46 u8 data_list_elts_left;
47 u32 elts[0]; /* Variable type. So keep it generic */
48 }) vxlan_gpe_ioam_trace_option_t;
52 #define foreach_vxlan_gpe_ioam_trace_stats \
53 _(SUCCESS, "Pkts updated with TRACE records") \
54 _(FAILED, "Errors in TRACE due to lack of TRACE records")
56 static char *vxlan_gpe_ioam_trace_stats_strings[] = {
57 #define _(sym,string) string,
58 foreach_vxlan_gpe_ioam_trace_stats
64 #define _(sym,str) VXLAN_GPE_IOAM_TRACE_##sym,
65 foreach_vxlan_gpe_ioam_trace_stats
67 VXLAN_GPE_IOAM_TRACE_N_STATS,
68 } vxlan_gpe_ioam_trace_stats_t;
74 u64 counters[ARRAY_LEN (vxlan_gpe_ioam_trace_stats_strings)];
77 vlib_main_t *vlib_main;
78 vnet_main_t *vnet_main;
79 } vxlan_gpe_ioam_trace_main_t;
81 vxlan_gpe_ioam_trace_main_t vxlan_gpe_ioam_trace_main;
84 vxlan_gpe_ioam_add_register_option (u8 option,
86 int rewrite_options (u8 * rewrite_string,
89 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
91 ASSERT (option < ARRAY_LEN (hm->add_options));
93 /* Already registered */
94 if (hm->add_options[option])
97 hm->add_options[option] = rewrite_options;
98 hm->options_size[option] = size;
104 vxlan_gpe_add_unregister_option (u8 option)
106 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
108 ASSERT (option < ARRAY_LEN (hm->add_options));
111 if (!hm->add_options[option])
114 hm->add_options[option] = NULL;
115 hm->options_size[option] = 0;
121 vxlan_gpe_ioam_register_option (u8 option,
122 int options (vlib_buffer_t * b,
123 vxlan_gpe_ioam_option_t * opt,
124 u8 is_ipv4, u8 use_adj),
126 vxlan_gpe_ioam_option_t * opt))
128 vxlan_gpe_ioam_main_t *im = &vxlan_gpe_ioam_main;
130 ASSERT (option < ARRAY_LEN (im->options));
132 /* Already registered */
133 if (im->options[option])
136 im->options[option] = options;
137 im->trace[option] = trace;
143 vxlan_gpe_ioam_unregister_option (u8 option)
145 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
147 ASSERT (option < ARRAY_LEN (hm->options));
150 if (!hm->options[option])
153 hm->options[option] = NULL;
154 hm->trace[option] = NULL;
161 vxlan_gpe_ioam_trace_stats_increment_counter (u32 counter_index,
164 vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
166 hm->counters[counter_index] += increment;
171 format_ioam_data_list_element (u8 * s, va_list * args)
173 u32 *elt = va_arg (*args, u32 *);
174 u8 *trace_type_p = va_arg (*args, u8 *);
175 u8 trace_type = *trace_type_p;
178 if (trace_type & BIT_TTL_NODEID)
180 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
181 s = format (s, "ttl 0x%x node id 0x%x ",
182 ttl_node_id_host_byte_order >> 24,
183 ttl_node_id_host_byte_order & 0x00FFFFFF);
188 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
190 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
191 s = format (s, "ingress 0x%x egress 0x%x ",
192 ingress_host_byte_order >> 16,
193 ingress_host_byte_order & 0xFFFF);
197 if (trace_type & BIT_TIMESTAMP)
199 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
200 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
204 if (trace_type & BIT_APPDATA)
206 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
207 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
217 vxlan_gpe_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
219 vxlan_gpe_ioam_trace_option_t *trace_option = NULL;
220 u8 trace_data_size = 0;
221 u8 trace_option_elts = 0;
222 trace_profile *profile = NULL;
225 profile = trace_profile_find ();
227 if (PREDICT_FALSE (!profile))
232 if (PREDICT_FALSE (!rewrite_string))
235 trace_option_elts = profile->num_elts;
236 trace_data_size = fetch_trace_data_size (profile->trace_type);
237 trace_option = (vxlan_gpe_ioam_trace_option_t *) rewrite_string;
238 trace_option->hdr.type = VXLAN_GPE_OPTION_TYPE_IOAM_TRACE;
239 trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
240 trace_option_elts * trace_data_size;
241 trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
242 trace_option->data_list_elts_left = trace_option_elts;
244 sizeof (vxlan_gpe_ioam_trace_option_t) +
245 (trace_option_elts * trace_data_size);
252 vxlan_gpe_ioam_trace_data_list_handler (vlib_buffer_t * b,
253 vxlan_gpe_ioam_option_t * opt,
254 u8 is_ipv4, u8 use_adj)
257 vxlan_gpe_ioam_trace_option_t *trace =
258 (vxlan_gpe_ioam_trace_option_t *) opt;
262 trace_profile *profile = NULL;
263 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
266 profile = trace_profile_find ();
268 if (PREDICT_FALSE (!profile))
276 if (PREDICT_TRUE (trace->data_list_elts_left))
278 trace->data_list_elts_left--;
279 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
280 * to skip to this node's location.
283 trace->data_list_elts_left *
284 fetch_trace_data_size (trace->ioam_trace_type) / 4;
285 elt = &trace->elts[elt_index];
288 if (trace->ioam_trace_type & BIT_TTL_NODEID)
290 ip4_header_t *ip0 = vlib_buffer_get_current (b);
291 /* The transit case is the only case where the TTL decrement happens
292 * before iOAM processing. For now, use the use_adj flag as an overload.
293 * We can probably use a separate flag instead of overloading the use_adj flag.
295 *elt = clib_host_to_net_u32 (((ip0->ttl - 1 + use_adj) << 24) |
300 if (trace->ioam_trace_type & BIT_ING_INTERFACE)
303 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
304 ip4_main_t *im4 = &ip4_main;
305 ip_lookup_main_t *lm = &im4->lookup_main;
308 ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
309 tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
313 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
315 *elt = clib_host_to_net_u32 (*elt);
321 if (trace->ioam_trace_type & BIT_TTL_NODEID)
323 ip6_header_t *ip0 = vlib_buffer_get_current (b);
324 *elt = clib_host_to_net_u32 ((ip0->hop_limit << 24) |
328 if (trace->ioam_trace_type & BIT_ING_INTERFACE)
331 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
332 ip6_main_t *im6 = &ip6_main;
333 ip_lookup_main_t *lm = &im6->lookup_main;
336 ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
337 tx_if = adj->rewrite_header.sw_if_index & 0xFFFF;
341 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
343 *elt = clib_host_to_net_u32 (*elt);
348 if (trace->ioam_trace_type & BIT_TIMESTAMP)
350 /* Send least significant 32 bits */
352 (f64) (((f64) hm->unix_time_0) +
353 (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
355 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
356 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
360 if (trace->ioam_trace_type & BIT_APPDATA)
362 /* $$$ set elt0->app_data */
363 *elt = clib_host_to_net_u32 (profile->app_data);
366 vxlan_gpe_ioam_trace_stats_increment_counter
367 (VXLAN_GPE_IOAM_TRACE_SUCCESS, 1);
371 vxlan_gpe_ioam_trace_stats_increment_counter
372 (VXLAN_GPE_IOAM_TRACE_FAILED, 1);
378 vxlan_gpe_ioam_trace_data_list_trace_handler (u8 * s,
379 vxlan_gpe_ioam_option_t * opt)
381 vxlan_gpe_ioam_trace_option_t *trace;
382 u8 trace_data_size_in_words = 0;
386 trace = (vxlan_gpe_ioam_trace_option_t *) opt;
388 format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
389 trace->data_list_elts_left);
390 trace_data_size_in_words =
391 fetch_trace_data_size (trace->ioam_trace_type) / 4;
392 elt = &trace->elts[0];
393 while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
394 /* -2 accounts for ioam_trace_type,elts_left */ ))
396 s = format (s, " [%d] %U\n", elt_index,
397 format_ioam_data_list_element,
398 elt, &trace->ioam_trace_type);
400 elt += trace_data_size_in_words;
406 static clib_error_t *
407 vxlan_gpe_show_ioam_trace_cmd_fn (vlib_main_t * vm,
408 unformat_input_t * input,
409 vlib_cli_command_t * cmd)
411 vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
415 for (i = 0; i < VXLAN_GPE_IOAM_TRACE_N_STATS; i++)
417 s = format (s, " %s - %lu\n", vxlan_gpe_ioam_trace_stats_strings[i],
421 vlib_cli_output (vm, "%v", s);
428 VLIB_CLI_COMMAND (vxlan_gpe_show_ioam_trace_cmd, static) = {
429 .path = "show ioam vxlan-gpe trace",
430 .short_help = "iOAM trace statistics",
431 .function = vxlan_gpe_show_ioam_trace_cmd_fn,
436 static clib_error_t *
437 vxlan_gpe_ioam_trace_init (vlib_main_t * vm)
439 vxlan_gpe_ioam_trace_main_t *hm = &vxlan_gpe_ioam_trace_main;
442 if ((error = vlib_call_init_function (vm, ip_main_init)))
445 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
448 if ((error = vlib_call_init_function (vm, vxlan_gpe_init)))
452 hm->vnet_main = vnet_get_main ();
453 memset (hm->counters, 0, sizeof (hm->counters));
455 if (vxlan_gpe_ioam_register_option
456 (VXLAN_GPE_OPTION_TYPE_IOAM_TRACE,
457 vxlan_gpe_ioam_trace_data_list_handler,
458 vxlan_gpe_ioam_trace_data_list_trace_handler) < 0)
459 return (clib_error_create
460 ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE failed"));
463 if (vxlan_gpe_ioam_add_register_option
464 (VXLAN_GPE_OPTION_TYPE_IOAM_TRACE,
465 sizeof (vxlan_gpe_ioam_trace_option_t),
466 vxlan_gpe_ioam_trace_rewrite_handler) < 0)
467 return (clib_error_create
468 ("registration of VXLAN_GPE_OPTION_TYPE_IOAM_TRACE for rewrite failed"));
474 VLIB_INIT_FUNCTION (vxlan_gpe_ioam_trace_init);
477 vxlan_gpe_trace_profile_cleanup (void)
479 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
481 hm->options_size[VXLAN_GPE_OPTION_TYPE_IOAM_TRACE] = 0;
488 vxlan_gpe_ioam_trace_get_sizeof_handler (u32 * result)
491 u8 trace_data_size = 0;
492 trace_profile *profile = NULL;
496 profile = trace_profile_find ();
498 if (PREDICT_FALSE (!profile))
503 trace_data_size = fetch_trace_data_size (profile->trace_type);
504 if (PREDICT_FALSE (trace_data_size == 0))
505 return VNET_API_ERROR_INVALID_VALUE;
507 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
508 return VNET_API_ERROR_INVALID_VALUE;
511 sizeof (vxlan_gpe_ioam_trace_option_t) +
512 profile->num_elts * trace_data_size;
520 vxlan_gpe_trace_profile_setup (void)
523 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
525 trace_profile *profile = NULL;
528 profile = trace_profile_find ();
530 if (PREDICT_FALSE (!profile))
536 if (vxlan_gpe_ioam_trace_get_sizeof_handler (&trace_size) < 0)
539 hm->options_size[VXLAN_GPE_OPTION_TYPE_IOAM_TRACE] = trace_size;
547 * fd.io coding-style-patch-verification: ON
550 * eval: (c-set-style "gnu")