2 * Copyright (c) 2017 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 <vppinfra/hash.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/elog.h>
23 #include <ioam/lib-trace/trace_util.h>
24 #include <nsh/nsh-md2-ioam/nsh_md2_ioam.h>
25 #include <nsh/nsh_packet.h>
27 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
28 * and nanoseconds respectively.
30 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
32 #define NSH_MD2_IOAM_TRACE_SIZE_DUMMY 20
41 typedef CLIB_PACKED(struct {
45 u8 data_list_elts_left;
48 u32 elts[0]; /* Variable type. So keep it generic */
49 }) nsh_md2_ioam_trace_option_t;
52 #define foreach_nsh_md2_ioam_trace_stats \
53 _(SUCCESS, "Pkts updated with TRACE records") \
54 _(FAILED, "Errors in TRACE due to lack of TRACE records")
56 static char *nsh_md2_ioam_trace_stats_strings[] = {
57 #define _(sym,string) string,
58 foreach_nsh_md2_ioam_trace_stats
64 #define _(sym,str) NSH_MD2_IOAM_TRACE_##sym,
65 foreach_nsh_md2_ioam_trace_stats
67 NSH_MD2_IOAM_TRACE_N_STATS,
68 } nsh_md2_ioam_trace_stats_t;
74 u64 counters[ARRAY_LEN (nsh_md2_ioam_trace_stats_strings)];
77 vlib_main_t *vlib_main;
78 vnet_main_t *vnet_main;
79 } nsh_md2_ioam_trace_main_t;
81 nsh_md2_ioam_trace_main_t nsh_md2_ioam_trace_main;
84 * Find a trace profile
87 extern u8 *nsh_trace_main;
88 always_inline trace_profile *
89 nsh_trace_profile_find (void)
91 trace_main_t *sm = (trace_main_t *) nsh_trace_main;
93 return (&(sm->profile));
98 nsh_md2_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
100 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
102 hm->counters[counter_index] += increment;
107 format_ioam_data_list_element (u8 * s, va_list * args)
109 u32 *elt = va_arg (*args, u32 *);
110 u8 *trace_type_p = va_arg (*args, u8 *);
111 u8 trace_type = *trace_type_p;
114 if (trace_type & BIT_TTL_NODEID)
116 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
117 s = format (s, "ttl 0x%x node id 0x%x ",
118 ttl_node_id_host_byte_order >> 24,
119 ttl_node_id_host_byte_order & 0x00FFFFFF);
124 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
126 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
127 s = format (s, "ingress 0x%x egress 0x%x ",
128 ingress_host_byte_order >> 16,
129 ingress_host_byte_order & 0xFFFF);
133 if (trace_type & BIT_TIMESTAMP)
135 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
136 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
140 if (trace_type & BIT_APPDATA)
142 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
143 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
153 nsh_md2_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
155 nsh_md2_ioam_trace_option_t *trace_option = NULL;
156 u8 trace_data_size = 0;
157 u8 trace_option_elts = 0;
158 trace_profile *profile = NULL;
160 profile = nsh_trace_profile_find ();
162 if (PREDICT_FALSE (!profile))
167 if (PREDICT_FALSE (!rewrite_string))
170 trace_option_elts = profile->num_elts;
171 trace_data_size = fetch_trace_data_size (profile->trace_type);
173 trace_option = (nsh_md2_ioam_trace_option_t *) rewrite_string;
174 trace_option->class = clib_host_to_net_u16 (0x9);
175 trace_option->type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
176 trace_option->length = (trace_option_elts * trace_data_size) + 4;
177 trace_option->data_list_elts_left = trace_option_elts;
178 trace_option->ioam_trace_type =
179 clib_host_to_net_u16 (profile->trace_type & TRACE_TYPE_MASK);
182 sizeof (nsh_md2_ioam_trace_option_t) +
183 (trace_option_elts * trace_data_size);
190 nsh_md2_ioam_trace_data_list_handler (vlib_buffer_t * b,
191 nsh_tlv_header_t * opt)
194 nsh_md2_ioam_trace_option_t *trace =
195 (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
199 trace_profile *profile = NULL;
200 nsh_md2_ioam_main_t *hm = &nsh_md2_ioam_main;
201 nsh_main_t *gm = &nsh_main;
202 u16 ioam_trace_type = 0;
204 profile = nsh_trace_profile_find ();
206 if (PREDICT_FALSE (!profile))
212 ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
215 if (PREDICT_TRUE (trace->data_list_elts_left))
217 trace->data_list_elts_left--;
218 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
219 * to skip to this node's location.
222 trace->data_list_elts_left *
223 fetch_trace_data_size (ioam_trace_type) / 4;
224 elt = &trace->elts[elt_index];
225 if (ioam_trace_type & BIT_TTL_NODEID)
227 ip4_header_t *ip0 = vlib_buffer_get_current (b);
228 *elt = clib_host_to_net_u32 (((ip0->ttl - 1) << 24) |
233 if (ioam_trace_type & BIT_ING_INTERFACE)
235 u16 tx_if = vnet_buffer (b)->sw_if_index[VLIB_TX];
238 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 | tx_if;
239 *elt = clib_host_to_net_u32 (*elt);
244 if (ioam_trace_type & BIT_TIMESTAMP)
246 /* Send least significant 32 bits */
248 (f64) (((f64) hm->unix_time_0) +
249 (vlib_time_now (gm->vlib_main) - hm->vlib_time_0));
251 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
252 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
256 if (ioam_trace_type & BIT_APPDATA)
258 /* $$$ set elt0->app_data */
259 *elt = clib_host_to_net_u32 (profile->app_data);
262 nsh_md2_ioam_trace_stats_increment_counter
263 (NSH_MD2_IOAM_TRACE_SUCCESS, 1);
267 nsh_md2_ioam_trace_stats_increment_counter
268 (NSH_MD2_IOAM_TRACE_FAILED, 1);
276 nsh_md2_ioam_trace_data_list_trace_handler (u8 * s, nsh_tlv_header_t * opt)
278 nsh_md2_ioam_trace_option_t *trace;
279 u8 trace_data_size_in_words = 0;
282 u16 ioam_trace_type = 0;
284 trace = (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
285 ioam_trace_type = clib_net_to_host_u16 (trace->ioam_trace_type);
286 trace_data_size_in_words = fetch_trace_data_size (ioam_trace_type) / 4;
287 elt = &trace->elts[0];
289 format (s, " Trace Type 0x%x , %d elts left\n", ioam_trace_type,
290 trace->data_list_elts_left);
291 while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->length - 4
292 /* -2 accounts for ioam_trace_type,elts_left */ ))
294 s = format (s, " [%d] %U\n", elt_index,
295 format_ioam_data_list_element, elt, &ioam_trace_type);
297 elt += trace_data_size_in_words;
303 nsh_md2_ioam_trace_swap_handler (vlib_buffer_t * b,
304 nsh_tlv_header_t * old_opt,
305 nsh_tlv_header_t * new_opt)
308 clib_memcpy_fast (new_opt, old_opt,
309 new_opt->length + sizeof (nsh_tlv_header_t));
310 return nsh_md2_ioam_trace_data_list_handler (b, new_opt);
313 static clib_error_t *
314 nsh_md2_ioam_show_ioam_trace_cmd_fn (vlib_main_t * vm,
315 unformat_input_t * input,
316 vlib_cli_command_t * cmd)
318 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
322 for (i = 0; i < NSH_MD2_IOAM_TRACE_N_STATS; i++)
324 s = format (s, " %s - %lu\n", nsh_md2_ioam_trace_stats_strings[i],
328 vlib_cli_output (vm, "%v", s);
334 VLIB_CLI_COMMAND (nsh_md2_ioam_show_ioam_trace_cmd, static) = {
335 .path = "show ioam nsh-lisp-gpe trace",
336 .short_help = "iOAM trace statistics",
337 .function = nsh_md2_ioam_show_ioam_trace_cmd_fn,
342 nsh_md2_ioam_trace_pop_handler (vlib_buffer_t * b, nsh_tlv_header_t * opt)
344 return nsh_md2_ioam_trace_data_list_handler (b, opt);
347 static clib_error_t *
348 nsh_md2_ioam_trace_init (vlib_main_t * vm)
350 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
351 nsh_md2_ioam_main_t *gm = &nsh_md2_ioam_main;
354 hm->vnet_main = vnet_get_main ();
355 gm->unix_time_0 = (u32) time (0); /* Store starting time */
356 gm->vlib_time_0 = vlib_time_now (vm);
358 clib_memset (hm->counters, 0, sizeof (hm->counters));
360 if (nsh_md2_register_option
361 (clib_host_to_net_u16 (0x9),
362 NSH_MD2_IOAM_OPTION_TYPE_TRACE,
363 NSH_MD2_IOAM_TRACE_SIZE_DUMMY,
364 nsh_md2_ioam_trace_rewrite_handler,
365 nsh_md2_ioam_trace_data_list_handler,
366 nsh_md2_ioam_trace_swap_handler,
367 nsh_md2_ioam_trace_pop_handler,
368 nsh_md2_ioam_trace_data_list_trace_handler) < 0)
369 return (clib_error_create
370 ("registration of NSH_MD2_IOAM_OPTION_TYPE_TRACE failed"));
375 VLIB_INIT_FUNCTION (nsh_md2_ioam_trace_init) =
377 .runs_after = VLIB_INITS ("nsh_init", "nsh_md2_ioam_init"),
381 nsh_md2_ioam_trace_profile_cleanup (void)
383 nsh_main_t *hm = &nsh_main;
385 hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = 0;
392 nsh_md2_ioam_trace_get_sizeof_handler (u32 * result)
395 u8 trace_data_size = 0;
396 trace_profile *profile = NULL;
400 profile = nsh_trace_profile_find ();
402 if (PREDICT_FALSE (!profile))
407 trace_data_size = fetch_trace_data_size (profile->trace_type);
408 if (PREDICT_FALSE (trace_data_size == 0))
409 return VNET_API_ERROR_INVALID_VALUE;
411 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
412 return VNET_API_ERROR_INVALID_VALUE;
415 sizeof (nsh_md2_ioam_trace_option_t) +
416 profile->num_elts * trace_data_size;
424 nsh_md2_ioam_trace_profile_setup (void)
427 nsh_main_t *hm = &nsh_main;
429 trace_profile *profile = NULL;
432 profile = nsh_trace_profile_find ();
434 if (PREDICT_FALSE (!profile))
440 if (nsh_md2_ioam_trace_get_sizeof_handler (&trace_size) < 0)
443 hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = trace_size;
451 * fd.io coding-style-patch-verification: ON
454 * eval: (c-set-style "gnu")