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
42 typedef CLIB_PACKED(struct {
46 u8 data_list_elts_left;
49 u32 elts[0]; /* Variable type. So keep it generic */
50 }) nsh_md2_ioam_trace_option_t;
54 #define foreach_nsh_md2_ioam_trace_stats \
55 _(SUCCESS, "Pkts updated with TRACE records") \
56 _(FAILED, "Errors in TRACE due to lack of TRACE records")
58 static char *nsh_md2_ioam_trace_stats_strings[] = {
59 #define _(sym,string) string,
60 foreach_nsh_md2_ioam_trace_stats
66 #define _(sym,str) NSH_MD2_IOAM_TRACE_##sym,
67 foreach_nsh_md2_ioam_trace_stats
69 NSH_MD2_IOAM_TRACE_N_STATS,
70 } nsh_md2_ioam_trace_stats_t;
76 u64 counters[ARRAY_LEN (nsh_md2_ioam_trace_stats_strings)];
79 vlib_main_t *vlib_main;
80 vnet_main_t *vnet_main;
81 } nsh_md2_ioam_trace_main_t;
83 nsh_md2_ioam_trace_main_t nsh_md2_ioam_trace_main;
86 * Find a trace profile
89 extern u8 *nsh_trace_main;
90 always_inline trace_profile *
91 nsh_trace_profile_find (void)
93 trace_main_t *sm = (trace_main_t *) nsh_trace_main;
95 return (&(sm->profile));
100 nsh_md2_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
102 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
104 hm->counters[counter_index] += increment;
109 format_ioam_data_list_element (u8 * s, va_list * args)
111 u32 *elt = va_arg (*args, u32 *);
112 u8 *trace_type_p = va_arg (*args, u8 *);
113 u8 trace_type = *trace_type_p;
116 if (trace_type & BIT_TTL_NODEID)
118 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
119 s = format (s, "ttl 0x%x node id 0x%x ",
120 ttl_node_id_host_byte_order >> 24,
121 ttl_node_id_host_byte_order & 0x00FFFFFF);
126 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
128 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
129 s = format (s, "ingress 0x%x egress 0x%x ",
130 ingress_host_byte_order >> 16,
131 ingress_host_byte_order & 0xFFFF);
135 if (trace_type & BIT_TIMESTAMP)
137 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
138 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
142 if (trace_type & BIT_APPDATA)
144 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
145 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
155 nsh_md2_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
157 nsh_md2_ioam_trace_option_t *trace_option = NULL;
158 u8 trace_data_size = 0;
159 u8 trace_option_elts = 0;
160 trace_profile *profile = NULL;
162 profile = nsh_trace_profile_find ();
164 if (PREDICT_FALSE (!profile))
169 if (PREDICT_FALSE (!rewrite_string))
172 trace_option_elts = profile->num_elts;
173 trace_data_size = fetch_trace_data_size (profile->trace_type);
175 trace_option = (nsh_md2_ioam_trace_option_t *) rewrite_string;
176 trace_option->class = clib_host_to_net_u16 (0x9);
177 trace_option->type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
178 trace_option->length = (trace_option_elts * trace_data_size) + 4;
179 trace_option->data_list_elts_left = trace_option_elts;
180 trace_option->ioam_trace_type =
181 clib_host_to_net_u16 (profile->trace_type & TRACE_TYPE_MASK);
184 sizeof (nsh_md2_ioam_trace_option_t) +
185 (trace_option_elts * trace_data_size);
192 nsh_md2_ioam_trace_data_list_handler (vlib_buffer_t * b,
193 nsh_tlv_header_t * opt)
196 nsh_md2_ioam_trace_option_t *trace =
197 (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
201 trace_profile *profile = NULL;
202 nsh_md2_ioam_main_t *hm = &nsh_md2_ioam_main;
203 nsh_main_t *gm = &nsh_main;
204 u16 ioam_trace_type = 0;
206 profile = nsh_trace_profile_find ();
208 if (PREDICT_FALSE (!profile))
214 ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
217 if (PREDICT_TRUE (trace->data_list_elts_left))
219 trace->data_list_elts_left--;
220 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
221 * to skip to this node's location.
224 trace->data_list_elts_left *
225 fetch_trace_data_size (ioam_trace_type) / 4;
226 elt = &trace->elts[elt_index];
227 if (ioam_trace_type & BIT_TTL_NODEID)
229 ip4_header_t *ip0 = vlib_buffer_get_current (b);
230 *elt = clib_host_to_net_u32 (((ip0->ttl - 1) << 24) |
235 if (ioam_trace_type & BIT_ING_INTERFACE)
237 u16 tx_if = vnet_buffer (b)->sw_if_index[VLIB_TX];
240 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 | tx_if;
241 *elt = clib_host_to_net_u32 (*elt);
246 if (ioam_trace_type & BIT_TIMESTAMP)
248 /* Send least significant 32 bits */
250 (f64) (((f64) hm->unix_time_0) +
251 (vlib_time_now (gm->vlib_main) - hm->vlib_time_0));
253 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
254 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
258 if (ioam_trace_type & BIT_APPDATA)
260 /* $$$ set elt0->app_data */
261 *elt = clib_host_to_net_u32 (profile->app_data);
264 nsh_md2_ioam_trace_stats_increment_counter
265 (NSH_MD2_IOAM_TRACE_SUCCESS, 1);
269 nsh_md2_ioam_trace_stats_increment_counter
270 (NSH_MD2_IOAM_TRACE_FAILED, 1);
278 nsh_md2_ioam_trace_data_list_trace_handler (u8 * s, nsh_tlv_header_t * opt)
280 nsh_md2_ioam_trace_option_t *trace;
281 u8 trace_data_size_in_words = 0;
284 u16 ioam_trace_type = 0;
286 trace = (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
287 ioam_trace_type = clib_net_to_host_u16 (trace->ioam_trace_type);
288 trace_data_size_in_words = fetch_trace_data_size (ioam_trace_type) / 4;
289 elt = &trace->elts[0];
291 format (s, " Trace Type 0x%x , %d elts left\n", ioam_trace_type,
292 trace->data_list_elts_left);
293 while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->length - 4
294 /* -2 accounts for ioam_trace_type,elts_left */ ))
296 s = format (s, " [%d] %U\n", elt_index,
297 format_ioam_data_list_element, elt, &ioam_trace_type);
299 elt += trace_data_size_in_words;
305 nsh_md2_ioam_trace_swap_handler (vlib_buffer_t * b,
306 nsh_tlv_header_t * old_opt,
307 nsh_tlv_header_t * new_opt)
310 clib_memcpy_fast (new_opt, old_opt,
311 new_opt->length + sizeof (nsh_tlv_header_t));
312 return nsh_md2_ioam_trace_data_list_handler (b, new_opt);
315 static clib_error_t *
316 nsh_md2_ioam_show_ioam_trace_cmd_fn (vlib_main_t * vm,
317 unformat_input_t * input,
318 vlib_cli_command_t * cmd)
320 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
324 for (i = 0; i < NSH_MD2_IOAM_TRACE_N_STATS; i++)
326 s = format (s, " %s - %lu\n", nsh_md2_ioam_trace_stats_strings[i],
330 vlib_cli_output (vm, "%v", s);
337 VLIB_CLI_COMMAND (nsh_md2_ioam_show_ioam_trace_cmd, static) = {
338 .path = "show ioam nsh-lisp-gpe trace",
339 .short_help = "iOAM trace statistics",
340 .function = nsh_md2_ioam_show_ioam_trace_cmd_fn,
346 nsh_md2_ioam_trace_pop_handler (vlib_buffer_t * b, nsh_tlv_header_t * opt)
348 return nsh_md2_ioam_trace_data_list_handler (b, opt);
351 static clib_error_t *
352 nsh_md2_ioam_trace_init (vlib_main_t * vm)
354 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
355 nsh_md2_ioam_main_t *gm = &nsh_md2_ioam_main;
358 hm->vnet_main = vnet_get_main ();
359 gm->unix_time_0 = (u32) time (0); /* Store starting time */
360 gm->vlib_time_0 = vlib_time_now (vm);
362 clib_memset (hm->counters, 0, sizeof (hm->counters));
364 if (nsh_md2_register_option
365 (clib_host_to_net_u16 (0x9),
366 NSH_MD2_IOAM_OPTION_TYPE_TRACE,
367 NSH_MD2_IOAM_TRACE_SIZE_DUMMY,
368 nsh_md2_ioam_trace_rewrite_handler,
369 nsh_md2_ioam_trace_data_list_handler,
370 nsh_md2_ioam_trace_swap_handler,
371 nsh_md2_ioam_trace_pop_handler,
372 nsh_md2_ioam_trace_data_list_trace_handler) < 0)
373 return (clib_error_create
374 ("registration of NSH_MD2_IOAM_OPTION_TYPE_TRACE failed"));
380 VLIB_INIT_FUNCTION (nsh_md2_ioam_trace_init) =
382 .runs_after = VLIB_INITS ("nsh_init", "nsh_md2_ioam_init"),
387 nsh_md2_ioam_trace_profile_cleanup (void)
389 nsh_main_t *hm = &nsh_main;
391 hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = 0;
398 nsh_md2_ioam_trace_get_sizeof_handler (u32 * result)
401 u8 trace_data_size = 0;
402 trace_profile *profile = NULL;
406 profile = nsh_trace_profile_find ();
408 if (PREDICT_FALSE (!profile))
413 trace_data_size = fetch_trace_data_size (profile->trace_type);
414 if (PREDICT_FALSE (trace_data_size == 0))
415 return VNET_API_ERROR_INVALID_VALUE;
417 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
418 return VNET_API_ERROR_INVALID_VALUE;
421 sizeof (nsh_md2_ioam_trace_option_t) +
422 profile->num_elts * trace_data_size;
430 nsh_md2_ioam_trace_profile_setup (void)
433 nsh_main_t *hm = &nsh_main;
435 trace_profile *profile = NULL;
438 profile = nsh_trace_profile_find ();
440 if (PREDICT_FALSE (!profile))
446 if (nsh_md2_ioam_trace_get_sizeof_handler (&trace_size) < 0)
449 hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = trace_size;
457 * fd.io coding-style-patch-verification: ON
460 * eval: (c-set-style "gnu")