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 <vppinfra/error.h>
18 #include <vpp/app/version.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>
30 #include <ioam/lib-trace/trace_config.h>
31 #include <ioam/encap/ip6_ioam_trace.h>
32 #include <ioam/udp-ping/udp_ping.h>
33 #include <ioam/udp-ping/udp_ping_packet.h>
34 #include <ioam/udp-ping/udp_ping_util.h>
36 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
37 * and nanoseconds respectively.
39 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
47 extern ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
48 extern ip6_main_t ip6_main;
50 #define foreach_ip6_hop_by_hop_ioam_trace_stats \
51 _(PROCESSED, "Pkts with ip6 hop-by-hop trace options") \
52 _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
53 _(UPDATED, "Pkts with trace updated") \
54 _(FULL, "Pkts with trace options but no space") \
55 _(LOOPBACK, "Pkts with trace options Loopback") \
56 _(LOOPBACK_REPLY, "Pkts with trace options Loopback Reply")
58 static char *ip6_hop_by_hop_ioam_trace_stats_strings[] = {
59 #define _(sym,string) string,
60 foreach_ip6_hop_by_hop_ioam_trace_stats
66 #define _(sym,str) IP6_IOAM_TRACE_##sym,
67 foreach_ip6_hop_by_hop_ioam_trace_stats
69 IP6_IOAM_TRACE_N_STATS,
70 } ip6_ioam_trace_stats_t;
76 u64 counters[ARRAY_LEN (ip6_hop_by_hop_ioam_trace_stats_strings)];
79 vlib_main_t *vlib_main;
80 vnet_main_t *vnet_main;
81 } ip6_hop_by_hop_ioam_trace_main_t;
83 ip6_hop_by_hop_ioam_trace_main_t ip6_hop_by_hop_ioam_trace_main;
86 ip6_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
88 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
90 hm->counters[counter_index] += increment;
95 format_ioam_data_list_element (u8 * s, va_list * args)
97 u32 *elt = va_arg (*args, u32 *);
98 u8 *trace_type_p = va_arg (*args, u8 *);
99 u8 trace_type = *trace_type_p;
102 if (trace_type & BIT_TTL_NODEID)
104 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
105 s = format (s, "ttl 0x%x node id 0x%x ",
106 ttl_node_id_host_byte_order >> 24,
107 ttl_node_id_host_byte_order & 0x00FFFFFF);
112 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
114 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
115 s = format (s, "ingress 0x%x egress 0x%x ",
116 ingress_host_byte_order >> 16,
117 ingress_host_byte_order & 0xFFFF);
121 if (trace_type & BIT_TIMESTAMP)
123 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
124 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
128 if (trace_type & BIT_APPDATA)
130 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
131 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
140 ip6_ioam_trace_get_sizeof_handler (u32 * result)
143 u8 trace_data_size = 0;
144 trace_profile *profile = NULL;
148 profile = trace_profile_find ();
150 if (PREDICT_FALSE (!profile))
152 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
156 trace_data_size = fetch_trace_data_size (profile->trace_type);
157 if (PREDICT_FALSE (trace_data_size == 0))
158 return VNET_API_ERROR_INVALID_VALUE;
160 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
161 return VNET_API_ERROR_INVALID_VALUE;
164 sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
173 ip6_hop_by_hop_ioam_trace_rewrite_handler (u8 * rewrite_string,
176 ioam_trace_option_t *trace_option = NULL;
177 u8 trace_data_size = 0;
178 u8 trace_option_elts = 0;
179 trace_profile *profile = NULL;
182 profile = trace_profile_find ();
184 if (PREDICT_FALSE (!profile))
186 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
190 if (PREDICT_FALSE (!rewrite_string))
193 trace_option_elts = profile->num_elts;
194 trace_data_size = fetch_trace_data_size (profile->trace_type);
195 trace_option = (ioam_trace_option_t *) rewrite_string;
196 trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
197 HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
198 trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
199 trace_option_elts * trace_data_size;
200 trace_option->trace_hdr.ioam_trace_type =
201 profile->trace_type & TRACE_TYPE_MASK;
202 trace_option->trace_hdr.data_list_elts_left = trace_option_elts;
204 sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
210 ip6_hbh_ioam_loopback_handler (vlib_buffer_t * b, ip6_header_t * ip,
211 ioam_trace_option_t * trace)
214 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
216 vlib_frame_t *nf = 0;
218 vlib_node_t *next_node;
220 ip6_hop_by_hop_header_t *hbh;
221 ioam_trace_option_t *opt;
224 b0 = vlib_buffer_copy (hm->vlib_main, b);
228 buf_index = vlib_get_buffer_index (hm->vlib_main, b0);
229 next_node = vlib_get_node_by_name (hm->vlib_main, (u8 *) "ip6-lookup");
230 nf = vlib_get_frame_to_node (hm->vlib_main, next_node->index);
232 to_next = vlib_frame_vector_args (nf);
234 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
235 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
237 ip6 = vlib_buffer_get_current (b0);
238 hbh = (ip6_hop_by_hop_header_t *) (ip6 + 1);
239 opt = (ioam_trace_option_t *)
240 ip6_hbh_get_option (hbh, HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST);
242 udp = (udp_ping_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
243 udp_ping_create_reply_from_probe_ip6 (ip6, hbh, udp);
244 ip6_hbh_ioam_trace_set_bit (opt, BIT_LOOPBACK_REPLY);
246 *to_next = buf_index;
250 vlib_put_frame_to_node (hm->vlib_main, next_node->index, nf);
251 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK, 1);
255 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
256 ip6_hop_by_hop_option_t * opt)
258 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
260 ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
261 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
262 ip_adjacency_t *adj = adj_get (adj_index);
266 trace_profile *profile = NULL;
269 profile = trace_profile_find ();
271 if (PREDICT_FALSE (!profile))
273 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
277 /* Don't trace loopback reply packets */
278 if (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY)
280 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK_REPLY,
287 if (PREDICT_TRUE (trace->trace_hdr.data_list_elts_left))
289 trace->trace_hdr.data_list_elts_left--;
290 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
291 * to skip to this node's location.
294 trace->trace_hdr.data_list_elts_left *
295 fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
296 elt = &trace->trace_hdr.elts[elt_index];
297 if (trace->trace_hdr.ioam_trace_type & BIT_TTL_NODEID)
300 clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
304 if (trace->trace_hdr.ioam_trace_type & BIT_ING_INTERFACE)
307 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
308 (adj->rewrite_header.sw_if_index & 0xFFFF);
309 *elt = clib_host_to_net_u32 (*elt);
313 if (trace->trace_hdr.ioam_trace_type & BIT_TIMESTAMP)
315 /* Send least significant 32 bits */
317 (f64) (((f64) hm->unix_time_0) +
318 (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
320 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
321 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
325 if (trace->trace_hdr.ioam_trace_type & BIT_APPDATA)
327 /* $$$ set elt0->app_data */
328 *elt = clib_host_to_net_u32 (profile->app_data);
333 if (PREDICT_FALSE (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK))
335 /* if loopback flag set then copy the packet
336 * and send it back to source */
337 ip6_hbh_ioam_loopback_handler (b, ip, trace);
340 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
344 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
350 ip6_hbh_ioam_trace_data_list_trace_handler (u8 * s,
351 ip6_hop_by_hop_option_t * opt)
353 ioam_trace_option_t *trace;
354 u8 trace_data_size_in_words = 0;
358 trace = (ioam_trace_option_t *) opt;
360 format (s, " Trace Type 0x%x , %d elts left\n",
361 trace->trace_hdr.ioam_trace_type,
362 trace->trace_hdr.data_list_elts_left);
363 trace_data_size_in_words =
364 fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
365 elt = &trace->trace_hdr.elts[0];
367 ((u8 *) (&trace->trace_hdr.elts[0]) + trace->hdr.length - 2
368 /* -2 accounts for ioam_trace_type,elts_left */ ))
370 s = format (s, " [%d] %U\n", elt_index,
371 format_ioam_data_list_element,
372 elt, &trace->trace_hdr.ioam_trace_type);
374 elt += trace_data_size_in_words;
380 static clib_error_t *
381 ip6_show_ioam_trace_cmd_fn (vlib_main_t * vm,
382 unformat_input_t * input,
383 vlib_cli_command_t * cmd)
385 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
389 for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
392 format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
396 vlib_cli_output (vm, "%v", s);
403 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
404 .path = "show ioam trace",
405 .short_help = "iOAM trace statistics",
406 .function = ip6_show_ioam_trace_cmd_fn,
411 VLIB_PLUGIN_REGISTER () = {
412 .version = VPP_BUILD_VER,
413 .description = "Inbound Operations, Administration, and Maintenance (OAM)",
417 static clib_error_t *
418 ip6_hop_by_hop_ioam_trace_init (vlib_main_t * vm)
420 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
423 hm->vnet_main = vnet_get_main ();
424 clib_memset (hm->counters, 0, sizeof (hm->counters));
427 if (ip6_hbh_register_option
428 (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
429 ip6_hbh_ioam_trace_data_list_handler,
430 ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
431 return (clib_error_create
432 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
435 if (ip6_hbh_add_register_option (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
436 sizeof (ioam_trace_option_t),
437 ip6_hop_by_hop_ioam_trace_rewrite_handler)
439 return (clib_error_create
440 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
447 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_trace_init) =
449 .runs_after = VLIB_INITS ("ip_main_init", "ip6_lookup_init",
450 "ip6_hop_by_hop_ioam_init"),
455 ip6_trace_profile_cleanup (void)
457 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
459 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = 0;
467 ip6_trace_profile_setup (void)
470 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
472 trace_profile *profile = NULL;
475 profile = trace_profile_find ();
477 if (PREDICT_FALSE (!profile))
479 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
484 if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
487 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = trace_size;
493 * fd.io coding-style-patch-verification: ON
496 * eval: (c-set-style "gnu")