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 <vnet/pg/pg.h>
18 #include <vppinfra/error.h>
20 #include <vppinfra/hash.h>
21 #include <vppinfra/error.h>
22 #include <vppinfra/elog.h>
24 #include <ioam/lib-trace/trace_util.h>
25 #include <nsh/nsh-md2-ioam/nsh_md2_ioam.h>
26 #include <nsh/nsh_packet.h>
28 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
29 * and nanoseconds respectively.
31 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
33 #define NSH_MD2_IOAM_TRACE_SIZE_DUMMY 20
43 typedef CLIB_PACKED(struct {
47 u8 data_list_elts_left;
50 u32 elts[0]; /* Variable type. So keep it generic */
51 }) nsh_md2_ioam_trace_option_t;
55 #define foreach_nsh_md2_ioam_trace_stats \
56 _(SUCCESS, "Pkts updated with TRACE records") \
57 _(FAILED, "Errors in TRACE due to lack of TRACE records")
59 static char *nsh_md2_ioam_trace_stats_strings[] = {
60 #define _(sym,string) string,
61 foreach_nsh_md2_ioam_trace_stats
67 #define _(sym,str) NSH_MD2_IOAM_TRACE_##sym,
68 foreach_nsh_md2_ioam_trace_stats
70 NSH_MD2_IOAM_TRACE_N_STATS,
71 } nsh_md2_ioam_trace_stats_t;
77 u64 counters[ARRAY_LEN (nsh_md2_ioam_trace_stats_strings)];
80 vlib_main_t *vlib_main;
81 vnet_main_t *vnet_main;
82 } nsh_md2_ioam_trace_main_t;
84 nsh_md2_ioam_trace_main_t nsh_md2_ioam_trace_main;
87 * Find a trace profile
90 extern u8 *nsh_trace_main;
91 always_inline trace_profile *
92 nsh_trace_profile_find (void)
94 trace_main_t *sm = (trace_main_t *) nsh_trace_main;
96 return (&(sm->profile));
101 nsh_md2_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
103 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
105 hm->counters[counter_index] += increment;
110 format_ioam_data_list_element (u8 * s, va_list * args)
112 u32 *elt = va_arg (*args, u32 *);
113 u8 *trace_type_p = va_arg (*args, u8 *);
114 u8 trace_type = *trace_type_p;
117 if (trace_type & BIT_TTL_NODEID)
119 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
120 s = format (s, "ttl 0x%x node id 0x%x ",
121 ttl_node_id_host_byte_order >> 24,
122 ttl_node_id_host_byte_order & 0x00FFFFFF);
127 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
129 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
130 s = format (s, "ingress 0x%x egress 0x%x ",
131 ingress_host_byte_order >> 16,
132 ingress_host_byte_order & 0xFFFF);
136 if (trace_type & BIT_TIMESTAMP)
138 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
139 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
143 if (trace_type & BIT_APPDATA)
145 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
146 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
156 nsh_md2_ioam_trace_rewrite_handler (u8 * rewrite_string, u8 * rewrite_size)
158 nsh_md2_ioam_trace_option_t *trace_option = NULL;
159 u8 trace_data_size = 0;
160 u8 trace_option_elts = 0;
161 trace_profile *profile = NULL;
163 profile = nsh_trace_profile_find ();
165 if (PREDICT_FALSE (!profile))
170 if (PREDICT_FALSE (!rewrite_string))
173 trace_option_elts = profile->num_elts;
174 trace_data_size = fetch_trace_data_size (profile->trace_type);
176 trace_option = (nsh_md2_ioam_trace_option_t *) rewrite_string;
177 trace_option->class = clib_host_to_net_u16 (0x9);
178 trace_option->type = NSH_MD2_IOAM_OPTION_TYPE_TRACE;
179 trace_option->length = (trace_option_elts * trace_data_size) + 4;
180 trace_option->data_list_elts_left = trace_option_elts;
181 trace_option->ioam_trace_type =
182 clib_host_to_net_u16 (profile->trace_type & TRACE_TYPE_MASK);
185 sizeof (nsh_md2_ioam_trace_option_t) +
186 (trace_option_elts * trace_data_size);
193 nsh_md2_ioam_trace_data_list_handler (vlib_buffer_t * b,
194 nsh_tlv_header_t * opt)
197 nsh_md2_ioam_trace_option_t *trace =
198 (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
202 trace_profile *profile = NULL;
203 nsh_md2_ioam_main_t *hm = &nsh_md2_ioam_main;
204 nsh_main_t *gm = &nsh_main;
205 u16 ioam_trace_type = 0;
207 profile = nsh_trace_profile_find ();
209 if (PREDICT_FALSE (!profile))
215 ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
218 if (PREDICT_TRUE (trace->data_list_elts_left))
220 trace->data_list_elts_left--;
221 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
222 * to skip to this node's location.
225 trace->data_list_elts_left *
226 fetch_trace_data_size (ioam_trace_type) / 4;
227 elt = &trace->elts[elt_index];
228 if (ioam_trace_type & BIT_TTL_NODEID)
230 ip4_header_t *ip0 = vlib_buffer_get_current (b);
231 *elt = clib_host_to_net_u32 (((ip0->ttl - 1) << 24) |
236 if (ioam_trace_type & BIT_ING_INTERFACE)
238 u16 tx_if = vnet_buffer (b)->sw_if_index[VLIB_TX];
241 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 | tx_if;
242 *elt = clib_host_to_net_u32 (*elt);
247 if (ioam_trace_type & BIT_TIMESTAMP)
249 /* Send least significant 32 bits */
251 (f64) (((f64) hm->unix_time_0) +
252 (vlib_time_now (gm->vlib_main) - hm->vlib_time_0));
254 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
255 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
259 if (ioam_trace_type & BIT_APPDATA)
261 /* $$$ set elt0->app_data */
262 *elt = clib_host_to_net_u32 (profile->app_data);
265 nsh_md2_ioam_trace_stats_increment_counter
266 (NSH_MD2_IOAM_TRACE_SUCCESS, 1);
270 nsh_md2_ioam_trace_stats_increment_counter
271 (NSH_MD2_IOAM_TRACE_FAILED, 1);
279 nsh_md2_ioam_trace_data_list_trace_handler (u8 * s, nsh_tlv_header_t * opt)
281 nsh_md2_ioam_trace_option_t *trace;
282 u8 trace_data_size_in_words = 0;
285 u16 ioam_trace_type = 0;
287 trace = (nsh_md2_ioam_trace_option_t *) ((u8 *) opt);
288 ioam_trace_type = clib_net_to_host_u16 (trace->ioam_trace_type);
289 trace_data_size_in_words = fetch_trace_data_size (ioam_trace_type) / 4;
290 elt = &trace->elts[0];
292 format (s, " Trace Type 0x%x , %d elts left\n", ioam_trace_type,
293 trace->data_list_elts_left);
294 while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->length - 4
295 /* -2 accounts for ioam_trace_type,elts_left */ ))
297 s = format (s, " [%d] %U\n", elt_index,
298 format_ioam_data_list_element, elt, &ioam_trace_type);
300 elt += trace_data_size_in_words;
306 nsh_md2_ioam_trace_swap_handler (vlib_buffer_t * b,
307 nsh_tlv_header_t * old_opt,
308 nsh_tlv_header_t * new_opt)
311 clib_memcpy_fast (new_opt, old_opt,
312 new_opt->length + sizeof (nsh_tlv_header_t));
313 return nsh_md2_ioam_trace_data_list_handler (b, new_opt);
316 static clib_error_t *
317 nsh_md2_ioam_show_ioam_trace_cmd_fn (vlib_main_t * vm,
318 unformat_input_t * input,
319 vlib_cli_command_t * cmd)
321 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
325 for (i = 0; i < NSH_MD2_IOAM_TRACE_N_STATS; i++)
327 s = format (s, " %s - %lu\n", nsh_md2_ioam_trace_stats_strings[i],
331 vlib_cli_output (vm, "%v", s);
338 VLIB_CLI_COMMAND (nsh_md2_ioam_show_ioam_trace_cmd, static) = {
339 .path = "show ioam nsh-lisp-gpe trace",
340 .short_help = "iOAM trace statistics",
341 .function = nsh_md2_ioam_show_ioam_trace_cmd_fn,
347 nsh_md2_ioam_trace_pop_handler (vlib_buffer_t * b, nsh_tlv_header_t * opt)
349 return nsh_md2_ioam_trace_data_list_handler (b, opt);
352 static clib_error_t *
353 nsh_md2_ioam_trace_init (vlib_main_t * vm)
355 nsh_md2_ioam_trace_main_t *hm = &nsh_md2_ioam_trace_main;
356 nsh_md2_ioam_main_t *gm = &nsh_md2_ioam_main;
359 if ((error = vlib_call_init_function (vm, nsh_init)))
362 if ((error = vlib_call_init_function (vm, nsh_md2_ioam_init)))
366 hm->vnet_main = vnet_get_main ();
367 gm->unix_time_0 = (u32) time (0); /* Store starting time */
368 gm->vlib_time_0 = vlib_time_now (vm);
370 clib_memset (hm->counters, 0, sizeof (hm->counters));
372 if (nsh_md2_register_option
373 (clib_host_to_net_u16 (0x9),
374 NSH_MD2_IOAM_OPTION_TYPE_TRACE,
375 NSH_MD2_IOAM_TRACE_SIZE_DUMMY,
376 nsh_md2_ioam_trace_rewrite_handler,
377 nsh_md2_ioam_trace_data_list_handler,
378 nsh_md2_ioam_trace_swap_handler,
379 nsh_md2_ioam_trace_pop_handler,
380 nsh_md2_ioam_trace_data_list_trace_handler) < 0)
381 return (clib_error_create
382 ("registration of NSH_MD2_IOAM_OPTION_TYPE_TRACE failed"));
387 VLIB_INIT_FUNCTION (nsh_md2_ioam_trace_init);
390 nsh_md2_ioam_trace_profile_cleanup (void)
392 nsh_main_t *hm = &nsh_main;
394 hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = 0;
401 nsh_md2_ioam_trace_get_sizeof_handler (u32 * result)
404 u8 trace_data_size = 0;
405 trace_profile *profile = NULL;
409 profile = nsh_trace_profile_find ();
411 if (PREDICT_FALSE (!profile))
416 trace_data_size = fetch_trace_data_size (profile->trace_type);
417 if (PREDICT_FALSE (trace_data_size == 0))
418 return VNET_API_ERROR_INVALID_VALUE;
420 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
421 return VNET_API_ERROR_INVALID_VALUE;
424 sizeof (nsh_md2_ioam_trace_option_t) +
425 profile->num_elts * trace_data_size;
433 nsh_md2_ioam_trace_profile_setup (void)
436 nsh_main_t *hm = &nsh_main;
438 trace_profile *profile = NULL;
441 profile = nsh_trace_profile_find ();
443 if (PREDICT_FALSE (!profile))
449 if (nsh_md2_ioam_trace_get_sizeof_handler (&trace_size) < 0)
452 hm->options_size[NSH_MD2_IOAM_OPTION_TYPE_TRACE] = trace_size;
460 * fd.io coding-style-patch-verification: ON
463 * eval: (c-set-style "gnu")