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/lib-trace/trace_util.h>
31 #include <ioam/lib-trace/trace_config.h>
32 #include <ioam/encap/ip6_ioam_trace.h>
33 #include <ioam/udp-ping/udp_ping.h>
34 #include <ioam/udp-ping/udp_ping_packet.h>
35 #include <ioam/udp-ping/udp_ping_util.h>
37 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
38 * and nanoseconds respectively.
40 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
48 extern ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
49 extern ip6_main_t ip6_main;
51 #define foreach_ip6_hop_by_hop_ioam_trace_stats \
52 _(PROCESSED, "Pkts with ip6 hop-by-hop trace options") \
53 _(PROFILE_MISS, "Pkts with ip6 hop-by-hop trace options but no profile set") \
54 _(UPDATED, "Pkts with trace updated") \
55 _(FULL, "Pkts with trace options but no space") \
56 _(LOOPBACK, "Pkts with trace options Loopback") \
57 _(LOOPBACK_REPLY, "Pkts with trace options Loopback Reply")
59 static char *ip6_hop_by_hop_ioam_trace_stats_strings[] = {
60 #define _(sym,string) string,
61 foreach_ip6_hop_by_hop_ioam_trace_stats
67 #define _(sym,str) IP6_IOAM_TRACE_##sym,
68 foreach_ip6_hop_by_hop_ioam_trace_stats
70 IP6_IOAM_TRACE_N_STATS,
71 } ip6_ioam_trace_stats_t;
77 u64 counters[ARRAY_LEN (ip6_hop_by_hop_ioam_trace_stats_strings)];
80 vlib_main_t *vlib_main;
81 vnet_main_t *vnet_main;
82 } ip6_hop_by_hop_ioam_trace_main_t;
84 ip6_hop_by_hop_ioam_trace_main_t ip6_hop_by_hop_ioam_trace_main;
87 ip6_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
89 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
91 hm->counters[counter_index] += increment;
96 format_ioam_data_list_element (u8 * s, va_list * args)
98 u32 *elt = va_arg (*args, u32 *);
99 u8 *trace_type_p = va_arg (*args, u8 *);
100 u8 trace_type = *trace_type_p;
103 if (trace_type & BIT_TTL_NODEID)
105 u32 ttl_node_id_host_byte_order = clib_net_to_host_u32 (*elt);
106 s = format (s, "ttl 0x%x node id 0x%x ",
107 ttl_node_id_host_byte_order >> 24,
108 ttl_node_id_host_byte_order & 0x00FFFFFF);
113 if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
115 u32 ingress_host_byte_order = clib_net_to_host_u32 (*elt);
116 s = format (s, "ingress 0x%x egress 0x%x ",
117 ingress_host_byte_order >> 16,
118 ingress_host_byte_order & 0xFFFF);
122 if (trace_type & BIT_TIMESTAMP)
124 u32 ts_in_host_byte_order = clib_net_to_host_u32 (*elt);
125 s = format (s, "ts 0x%x \n", ts_in_host_byte_order);
129 if (trace_type & BIT_APPDATA)
131 u32 appdata_in_host_byte_order = clib_net_to_host_u32 (*elt);
132 s = format (s, "app 0x%x ", appdata_in_host_byte_order);
141 ip6_ioam_trace_get_sizeof_handler (u32 * result)
144 u8 trace_data_size = 0;
145 trace_profile *profile = NULL;
149 profile = trace_profile_find ();
151 if (PREDICT_FALSE (!profile))
153 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
157 trace_data_size = fetch_trace_data_size (profile->trace_type);
158 if (PREDICT_FALSE (trace_data_size == 0))
159 return VNET_API_ERROR_INVALID_VALUE;
161 if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
162 return VNET_API_ERROR_INVALID_VALUE;
165 sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
174 ip6_hop_by_hop_ioam_trace_rewrite_handler (u8 * rewrite_string,
177 ioam_trace_option_t *trace_option = NULL;
178 u8 trace_data_size = 0;
179 u8 trace_option_elts = 0;
180 trace_profile *profile = NULL;
183 profile = trace_profile_find ();
185 if (PREDICT_FALSE (!profile))
187 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
191 if (PREDICT_FALSE (!rewrite_string))
194 trace_option_elts = profile->num_elts;
195 trace_data_size = fetch_trace_data_size (profile->trace_type);
196 trace_option = (ioam_trace_option_t *) rewrite_string;
197 trace_option->hdr.type = HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST |
198 HBH_OPTION_TYPE_DATA_CHANGE_ENROUTE;
199 trace_option->hdr.length = 2 /*ioam_trace_type,data_list_elts_left */ +
200 trace_option_elts * trace_data_size;
201 trace_option->trace_hdr.ioam_trace_type =
202 profile->trace_type & TRACE_TYPE_MASK;
203 trace_option->trace_hdr.data_list_elts_left = trace_option_elts;
205 sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
211 ip6_hbh_ioam_loopback_handler (vlib_buffer_t * b, ip6_header_t * ip,
212 ioam_trace_option_t * trace)
215 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
217 vlib_frame_t *nf = 0;
219 vlib_node_t *next_node;
221 ip6_hop_by_hop_header_t *hbh;
222 ioam_trace_option_t *opt;
225 b0 = vlib_buffer_copy (hm->vlib_main, b);
229 buf_index = vlib_get_buffer_index (hm->vlib_main, b0);
230 next_node = vlib_get_node_by_name (hm->vlib_main, (u8 *) "ip6-lookup");
231 nf = vlib_get_frame_to_node (hm->vlib_main, next_node->index);
233 to_next = vlib_frame_vector_args (nf);
235 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
236 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
238 ip6 = vlib_buffer_get_current (b0);
239 hbh = (ip6_hop_by_hop_header_t *) (ip6 + 1);
240 opt = (ioam_trace_option_t *)
241 ip6_hbh_get_option (hbh, HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST);
243 udp = (udp_ping_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
244 udp_ping_create_reply_from_probe_ip6 (ip6, hbh, udp);
245 ip6_hbh_ioam_trace_set_bit (opt, BIT_LOOPBACK_REPLY);
247 *to_next = buf_index;
251 vlib_put_frame_to_node (hm->vlib_main, next_node->index, nf);
252 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK, 1);
256 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
257 ip6_hop_by_hop_option_t * opt)
259 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
261 ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
262 u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
263 ip_adjacency_t *adj = adj_get (adj_index);
267 trace_profile *profile = NULL;
270 profile = trace_profile_find ();
272 if (PREDICT_FALSE (!profile))
274 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
278 /* Don't trace loopback reply packets */
279 if (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY)
281 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK_REPLY,
288 if (PREDICT_TRUE (trace->trace_hdr.data_list_elts_left))
290 trace->trace_hdr.data_list_elts_left--;
291 /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
292 * to skip to this node's location.
295 trace->trace_hdr.data_list_elts_left *
296 fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
297 elt = &trace->trace_hdr.elts[elt_index];
298 if (trace->trace_hdr.ioam_trace_type & BIT_TTL_NODEID)
301 clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
305 if (trace->trace_hdr.ioam_trace_type & BIT_ING_INTERFACE)
308 (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
309 (adj->rewrite_header.sw_if_index & 0xFFFF);
310 *elt = clib_host_to_net_u32 (*elt);
314 if (trace->trace_hdr.ioam_trace_type & BIT_TIMESTAMP)
316 /* Send least significant 32 bits */
318 (f64) (((f64) hm->unix_time_0) +
319 (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
321 time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
322 *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
326 if (trace->trace_hdr.ioam_trace_type & BIT_APPDATA)
328 /* $$$ set elt0->app_data */
329 *elt = clib_host_to_net_u32 (profile->app_data);
334 if (PREDICT_FALSE (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK))
336 /* if loopback flag set then copy the packet
337 * and send it back to source */
338 ip6_hbh_ioam_loopback_handler (b, ip, trace);
341 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
345 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
351 ip6_hbh_ioam_trace_data_list_trace_handler (u8 * s,
352 ip6_hop_by_hop_option_t * opt)
354 ioam_trace_option_t *trace;
355 u8 trace_data_size_in_words = 0;
359 trace = (ioam_trace_option_t *) opt;
361 format (s, " Trace Type 0x%x , %d elts left\n",
362 trace->trace_hdr.ioam_trace_type,
363 trace->trace_hdr.data_list_elts_left);
364 trace_data_size_in_words =
365 fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
366 elt = &trace->trace_hdr.elts[0];
368 ((u8 *) (&trace->trace_hdr.elts[0]) + trace->hdr.length - 2
369 /* -2 accounts for ioam_trace_type,elts_left */ ))
371 s = format (s, " [%d] %U\n", elt_index,
372 format_ioam_data_list_element,
373 elt, &trace->trace_hdr.ioam_trace_type);
375 elt += trace_data_size_in_words;
381 static clib_error_t *
382 ip6_show_ioam_trace_cmd_fn (vlib_main_t * vm,
383 unformat_input_t * input,
384 vlib_cli_command_t * cmd)
386 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
390 for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
393 format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
397 vlib_cli_output (vm, "%v", s);
404 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
405 .path = "show ioam trace",
406 .short_help = "iOAM trace statistics",
407 .function = ip6_show_ioam_trace_cmd_fn,
412 VLIB_PLUGIN_REGISTER () = {
413 .version = VPP_BUILD_VER,
414 .description = "Inbound Operations, Administration, and Maintenance (OAM)",
418 static clib_error_t *
419 ip6_hop_by_hop_ioam_trace_init (vlib_main_t * vm)
421 ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
424 hm->vnet_main = vnet_get_main ();
425 clib_memset (hm->counters, 0, sizeof (hm->counters));
428 if (ip6_hbh_register_option
429 (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
430 ip6_hbh_ioam_trace_data_list_handler,
431 ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
432 return (clib_error_create
433 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
436 if (ip6_hbh_add_register_option (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
437 sizeof (ioam_trace_option_t),
438 ip6_hop_by_hop_ioam_trace_rewrite_handler)
440 return (clib_error_create
441 ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
448 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_trace_init) =
450 .runs_after = VLIB_INITS ("ip_main_init", "ip6_lookup_init",
451 "ip6_hop_by_hop_ioam_init"),
456 ip6_trace_profile_cleanup (void)
458 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
460 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = 0;
468 ip6_trace_profile_setup (void)
471 ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
473 trace_profile *profile = NULL;
476 profile = trace_profile_find ();
478 if (PREDICT_FALSE (!profile))
480 ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
485 if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
488 hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = trace_size;
494 * fd.io coding-style-patch-verification: ON
497 * eval: (c-set-style "gnu")