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>
19 #include <vpp/app/version.h>
21 #include <vnet/ip/ip6.h>
22 #include <vnet/ip/ip6_hop_by_hop.h>
23 #include <vnet/ip/ip6_hop_by_hop_packet.h>
25 #include <vppinfra/hash.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/elog.h>
28 #include <vnet/plugin/plugin.h>
30 #include <ioam/encap/ip6_ioam_trace.h>
32 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
33 * and nanoseconds respectively.
35 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
43 extern ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
44 extern ip6_main_t ip6_main;
46 #define foreach_ip6_hop_by_hop_ioam_trace_stats \
47 _(PROCESSED, "Pkts with ip6 hop-by-hop trace options") \
48 _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
49 _(UPDATED, "Pkts with trace updated") \
50 _(FULL, "Pkts with trace options but no space")
52 static char *ip6_hop_by_hop_ioam_trace_stats_strings[] = {
53 #define _(sym,string) string,
54 foreach_ip6_hop_by_hop_ioam_trace_stats
60 #define _(sym,str) IP6_IOAM_TRACE_##sym,
61 foreach_ip6_hop_by_hop_ioam_trace_stats
63 IP6_IOAM_TRACE_N_STATS,
64 } ip6_ioam_trace_stats_t;
70 u64 counters[ARRAY_LEN (ip6_hop_by_hop_ioam_trace_stats_strings)];
73 vlib_main_t *vlib_main;
74 vnet_main_t *vnet_main;
75 } ip6_hop_by_hop_ioam_trace_main_t;
77 ip6_hop_by_hop_ioam_trace_main_t ip6_hop_by_hop_ioam_trace_main;
80 ip6_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
82 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
84 hm->counters[counter_index] += increment;
89 format_ioam_data_list_element (u8 * s, va_list * args)
91 u32 *elt = va_arg (*args, u32 *);
92 u8 *trace_type_p = va_arg (*args, u8 *);
93 u8 trace_type = *trace_type_p;
96 if (trace_type & BIT_TTL_NODEID)
98 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
99 s = format (s, "ttl 0x%x node id 0x%x ",
100 ttl_node_id_host_byte_order >> 24,
101 ttl_node_id_host_byte_order & 0x00FFFFFF);
106 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
108 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
109 s = format (s, "ingress 0x%x egress 0x%x ",
110 ingress_host_byte_order >> 16,
111 ingress_host_byte_order & 0xFFFF);
115 if (trace_type & BIT_TIMESTAMP)
117 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
118 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
122 if (trace_type & BIT_APPDATA)
124 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
125 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
134 ip6_ioam_trace_get_sizeof_handler (u32 * result)
137 u8 trace_data_size = 0;
138 trace_profile *profile = NULL;
142 profile = trace_profile_find ();
144 if (PREDICT_FALSE (!profile))
146 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
150 trace_data_size = fetch_trace_data_size (profile->trace_type);
151 if (PREDICT_FALSE (trace_data_size == 0))
152 return VNET_API_ERROR_INVALID_VALUE;
154 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
155 return VNET_API_ERROR_INVALID_VALUE;
158 sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
167 ip6_hop_by_hop_ioam_trace_rewrite_handler (u8 * rewrite_string,
170 ioam_trace_option_t *trace_option = NULL;
171 u8 trace_data_size = 0;
172 u8 trace_option_elts = 0;
173 trace_profile *profile = NULL;
176 profile = trace_profile_find ();
178 if (PREDICT_FALSE (!profile))
180 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
184 if (PREDICT_FALSE (!rewrite_string))
187 trace_option_elts = profile->num_elts;
188 trace_data_size = fetch_trace_data_size (profile->trace_type);
189 trace_option = (ioam_trace_option_t *) rewrite_string;
190 trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
191 HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
192 trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
193 trace_option_elts * trace_data_size;
194 trace_option->trace_hdr.ioam_trace_type =
195 profile->trace_type & TRACE_TYPE_MASK;
196 trace_option->trace_hdr.data_list_elts_left = trace_option_elts;
198 sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
205 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
206 ip6_hop_by_hop_option_t * opt)
208 ip6_main_t *im = &ip6_main;
209 ip_lookup_main_t *lm = &im->lookup_main;
210 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
212 ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
213 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
214 ip_adjacency_t *adj = ip_get_adjacency (lm, adj_index);
218 trace_profile *profile = NULL;
221 profile = trace_profile_find ();
223 if (PREDICT_FALSE (!profile))
225 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
232 if (PREDICT_TRUE (trace->trace_hdr.data_list_elts_left))
234 trace->trace_hdr.data_list_elts_left--;
235 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
236 * to skip to this node's location.
239 trace->trace_hdr.data_list_elts_left *
240 fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
241 elt = &trace->trace_hdr.elts[elt_index];
242 if (trace->trace_hdr.ioam_trace_type & BIT_TTL_NODEID)
245 clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
249 if (trace->trace_hdr.ioam_trace_type & BIT_ING_INTERFACE)
252 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
253 (adj->rewrite_header.sw_if_index & 0xFFFF);
254 *elt = clib_host_to_net_u32 (*elt);
258 if (trace->trace_hdr.ioam_trace_type & BIT_TIMESTAMP)
260 /* Send least significant 32 bits */
262 (f64) (((f64) hm->unix_time_0) +
263 (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
265 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
266 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
270 if (trace->trace_hdr.ioam_trace_type & BIT_APPDATA)
272 /* $$$ set elt0->app_data */
273 *elt = clib_host_to_net_u32 (profile->app_data);
276 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
280 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
286 ip6_hbh_ioam_trace_data_list_trace_handler (u8 * s,
287 ip6_hop_by_hop_option_t * opt)
289 ioam_trace_option_t *trace;
290 u8 trace_data_size_in_words = 0;
294 trace = (ioam_trace_option_t *) opt;
296 format (s, " Trace Type 0x%x , %d elts left\n",
297 trace->trace_hdr.ioam_trace_type,
298 trace->trace_hdr.data_list_elts_left);
299 trace_data_size_in_words =
300 fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
301 elt = &trace->trace_hdr.elts[0];
303 ((u8 *) (&trace->trace_hdr.elts[0]) + trace->hdr.length - 2
304 /* -2 accounts for ioam_trace_type,elts_left */ ))
306 s = format (s, " [%d] %U\n", elt_index,
307 format_ioam_data_list_element,
308 elt, &trace->trace_hdr.ioam_trace_type);
310 elt += trace_data_size_in_words;
316 static clib_error_t *
317 ip6_show_ioam_trace_cmd_fn (vlib_main_t * vm,
318 unformat_input_t * input,
319 vlib_cli_command_t * cmd)
321 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
325 for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
328 format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
332 vlib_cli_output (vm, "%v", s);
339 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
340 .path = "show ioam trace",
341 .short_help = "iOAM trace statistics",
342 .function = ip6_show_ioam_trace_cmd_fn,
347 VLIB_PLUGIN_REGISTER () = {
348 .version = VPP_BUILD_VER,
352 static clib_error_t *
353 ip6_hop_by_hop_ioam_trace_init (vlib_main_t * vm)
355 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
358 if ((error = vlib_call_init_function (vm, ip_main_init)))
361 if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
364 if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
368 hm->vnet_main = vnet_get_main ();
369 memset (hm->counters, 0, sizeof (hm->counters));
372 if (ip6_hbh_register_option
373 (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
374 ip6_hbh_ioam_trace_data_list_handler,
375 ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
376 return (clib_error_create
377 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
380 if (ip6_hbh_add_register_option (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
381 sizeof (ioam_trace_option_t),
382 ip6_hop_by_hop_ioam_trace_rewrite_handler)
384 return (clib_error_create
385 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
392 ip6_trace_profile_cleanup (void)
394 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
396 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = 0;
404 ip6_trace_profile_setup (void)
407 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
409 trace_profile *profile = NULL;
412 profile = trace_profile_find ();
414 if (PREDICT_FALSE (!profile))
416 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
421 if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
424 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = trace_size;
430 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_trace_init);
433 * fd.io coding-style-patch-verification: ON
436 * eval: (c-set-style "gnu")