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/ip/ip6.h>
21 #include <vnet/ip/ip6_hop_by_hop.h>
22 #include <vnet/ip/ip6_hop_by_hop_packet.h>
24 #include <vppinfra/hash.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/elog.h>
27 #include <vnet/plugin/plugin.h>
29 #include <ioam/lib-trace/trace_util.h>
31 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
32 * and nanoseconds respectively.
34 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
43 typedef CLIB_PACKED(struct {
44 ip6_hop_by_hop_option_t hdr;
46 u8 data_list_elts_left;
47 u32 elts[0]; /* Variable type. So keep it generic */
48 }) ioam_trace_option_t;
52 extern ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
53 extern ip6_main_t ip6_main;
55 #define foreach_ip6_hop_by_hop_ioam_trace_stats \
56 _(PROCESSED, "Pkts with ip6 hop-by-hop trace options") \
57 _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
58 _(UPDATED, "Pkts with trace updated") \
59 _(FULL, "Pkts with trace options but no space")
61 static char *ip6_hop_by_hop_ioam_trace_stats_strings[] = {
62 #define _(sym,string) string,
63 foreach_ip6_hop_by_hop_ioam_trace_stats
69 #define _(sym,str) IP6_IOAM_TRACE_##sym,
70 foreach_ip6_hop_by_hop_ioam_trace_stats
72 IP6_IOAM_TRACE_N_STATS,
73 } ip6_ioam_trace_stats_t;
79 u64 counters[ARRAY_LEN (ip6_hop_by_hop_ioam_trace_stats_strings)];
82 vlib_main_t *vlib_main;
83 vnet_main_t *vnet_main;
84 } ip6_hop_by_hop_ioam_trace_main_t;
86 ip6_hop_by_hop_ioam_trace_main_t ip6_hop_by_hop_ioam_trace_main;
89 ip6_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
91 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
93 hm->counters[counter_index] += increment;
98 format_ioam_data_list_element (u8 * s, va_list * args)
100 u32 *elt = va_arg (*args, u32 *);
101 u8 *trace_type_p = va_arg (*args, u8 *);
102 u8 trace_type = *trace_type_p;
105 if (trace_type & BIT_TTL_NODEID)
107 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
108 s = format (s, "ttl 0x%x node id 0x%x ",
109 ttl_node_id_host_byte_order >> 24,
110 ttl_node_id_host_byte_order & 0x00FFFFFF);
115 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
117 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
118 s = format (s, "ingress 0x%x egress 0x%x ",
119 ingress_host_byte_order >> 16,
120 ingress_host_byte_order & 0xFFFF);
124 if (trace_type & BIT_TIMESTAMP)
126 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
127 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
131 if (trace_type & BIT_APPDATA)
133 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
134 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
143 ip6_ioam_trace_get_sizeof_handler (u32 * result)
146 u8 trace_data_size = 0;
147 trace_profile *profile = NULL;
151 profile = trace_profile_find ();
153 if (PREDICT_FALSE (!profile))
155 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
159 trace_data_size = fetch_trace_data_size (profile->trace_type);
160 if (PREDICT_FALSE (trace_data_size == 0))
161 return VNET_API_ERROR_INVALID_VALUE;
163 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
164 return VNET_API_ERROR_INVALID_VALUE;
167 sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
176 ip6_hop_by_hop_ioam_trace_rewrite_handler (u8 * rewrite_string,
179 ioam_trace_option_t *trace_option = NULL;
180 u8 trace_data_size = 0;
181 u8 trace_option_elts = 0;
182 trace_profile *profile = NULL;
185 profile = trace_profile_find ();
187 if (PREDICT_FALSE (!profile))
189 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
193 if (PREDICT_FALSE (!rewrite_string))
196 trace_option_elts = profile->num_elts;
197 trace_data_size = fetch_trace_data_size (profile->trace_type);
198 trace_option = (ioam_trace_option_t *) rewrite_string;
199 trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
200 HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
201 trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
202 trace_option_elts * trace_data_size;
203 trace_option->ioam_trace_type = profile->trace_type & TRACE_TYPE_MASK;
204 trace_option->data_list_elts_left = trace_option_elts;
206 sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
213 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
214 ip6_hop_by_hop_option_t * opt)
216 ip6_main_t *im = &ip6_main;
217 ip_lookup_main_t *lm = &im->lookup_main;
218 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
220 ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
221 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
222 ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
226 trace_profile *profile = NULL;
229 profile = trace_profile_find ();
231 if (PREDICT_FALSE (!profile))
233 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
240 if (PREDICT_TRUE (trace->data_list_elts_left))
242 trace->data_list_elts_left--;
243 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
244 * to skip to this node's location.
247 trace->data_list_elts_left *
248 fetch_trace_data_size (trace->ioam_trace_type) / 4;
249 elt = &trace->elts[elt_index];
250 if (trace->ioam_trace_type & BIT_TTL_NODEID)
253 clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
257 if (trace->ioam_trace_type & BIT_ING_INTERFACE)
260 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
261 (adj->rewrite_header.sw_if_index & 0xFFFF);
262 *elt = clib_host_to_net_u32 (*elt);
266 if (trace->ioam_trace_type & BIT_TIMESTAMP)
268 /* Send least significant 32 bits */
270 (f64) (((f64) hm->unix_time_0) +
271 (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
273 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
274 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
278 if (trace->ioam_trace_type & BIT_APPDATA)
280 /* $$$ set elt0->app_data */
281 *elt = clib_host_to_net_u32 (profile->app_data);
284 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
288 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
294 ip6_hbh_ioam_trace_data_list_trace_handler (u8 * s,
295 ip6_hop_by_hop_option_t * opt)
297 ioam_trace_option_t *trace;
298 u8 trace_data_size_in_words = 0;
302 trace = (ioam_trace_option_t *) opt;
304 format (s, " Trace Type 0x%x , %d elts left\n", trace->ioam_trace_type,
305 trace->data_list_elts_left);
306 trace_data_size_in_words =
307 fetch_trace_data_size (trace->ioam_trace_type) / 4;
308 elt = &trace->elts[0];
309 while ((u8 *) elt < ((u8 *) (&trace->elts[0]) + trace->hdr.length - 2
310 /* -2 accounts for ioam_trace_type,elts_left */ ))
312 s = format (s, " [%d] %U\n", elt_index,
313 format_ioam_data_list_element,
314 elt, &trace->ioam_trace_type);
316 elt += trace_data_size_in_words;
322 static clib_error_t *
323 ip6_show_ioam_trace_cmd_fn (vlib_main_t * vm,
324 unformat_input_t * input,
325 vlib_cli_command_t * cmd)
327 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
331 for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
334 format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
338 vlib_cli_output (vm, "%v", s);
345 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
346 .path = "show ioam trace",
347 .short_help = "iOAM trace statistics",
348 .function = ip6_show_ioam_trace_cmd_fn,
353 * This routine exists to convince the vlib plugin framework that
354 * we haven't accidentally copied a random .dll into the plugin directory.
356 * Also collects global variable pointers passed from the vpp engine
359 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
365 static clib_error_t *
366 ip6_hop_by_hop_ioam_trace_init (vlib_main_t * vm)
368 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
371 if ((error = vlib_call_init_function (vm, ip_main_init)))
374 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
377 if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
381 hm->vnet_main = vnet_get_main ();
382 memset (hm->counters, 0, sizeof (hm->counters));
385 if (ip6_hbh_register_option
386 (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
387 ip6_hbh_ioam_trace_data_list_handler,
388 ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
389 return (clib_error_create
390 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
393 if (ip6_hbh_add_register_option (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
394 sizeof (ioam_trace_option_t),
395 ip6_hop_by_hop_ioam_trace_rewrite_handler)
397 return (clib_error_create
398 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
405 ip6_trace_profile_cleanup (void)
407 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
409 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = 0;
417 ip6_trace_profile_setup (void)
420 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
422 trace_profile *profile = NULL;
425 profile = trace_profile_find ();
427 if (PREDICT_FALSE (!profile))
429 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
434 if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
437 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = trace_size;
443 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_trace_init);
446 * fd.io coding-style-patch-verification: ON
449 * eval: (c-set-style "gnu")