3ec3ea821133b94bfbfc2362c435b820ddc47faa
[vpp.git] / src / plugins / ioam / encap / ip6_ioam_trace.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
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>
20
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>
24
25 #include <vppinfra/hash.h>
26 #include <vppinfra/error.h>
27 #include <vppinfra/elog.h>
28 #include <vnet/plugin/plugin.h>
29
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>
36
37 /* Timestamp precision multipliers for seconds, milliseconds, microseconds
38  * and nanoseconds respectively.
39  */
40 static f64 trace_tsp_mul[4] = { 1, 1e3, 1e6, 1e9 };
41
42 typedef union
43 {
44   u64 as_u64;
45   u32 as_u32[2];
46 } time_u64_t;
47
48 extern ip6_hop_by_hop_ioam_main_t ip6_hop_by_hop_ioam_main;
49 extern ip6_main_t ip6_main;
50
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")
58
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
62 #undef _
63 };
64
65 typedef enum
66 {
67 #define _(sym,str) IP6_IOAM_TRACE_##sym,
68   foreach_ip6_hop_by_hop_ioam_trace_stats
69 #undef _
70     IP6_IOAM_TRACE_N_STATS,
71 } ip6_ioam_trace_stats_t;
72
73
74 typedef struct
75 {
76   /* stats */
77   u64 counters[ARRAY_LEN (ip6_hop_by_hop_ioam_trace_stats_strings)];
78
79   /* convenience */
80   vlib_main_t *vlib_main;
81   vnet_main_t *vnet_main;
82 } ip6_hop_by_hop_ioam_trace_main_t;
83
84 ip6_hop_by_hop_ioam_trace_main_t ip6_hop_by_hop_ioam_trace_main;
85
86 always_inline void
87 ip6_ioam_trace_stats_increment_counter (u32 counter_index, u64 increment)
88 {
89   ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
90
91   hm->counters[counter_index] += increment;
92 }
93
94
95 static u8 *
96 format_ioam_data_list_element (u8 * s, va_list * args)
97 {
98   u32 *elt = va_arg (*args, u32 *);
99   u8 *trace_type_p = va_arg (*args, u8 *);
100   u8 trace_type = *trace_type_p;
101
102
103   if (trace_type & BIT_TTL_NODEID)
104     {
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);
109
110       elt++;
111     }
112
113   if (trace_type & BIT_ING_INTERFACE && trace_type & BIT_ING_INTERFACE)
114     {
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);
119       elt++;
120     }
121
122   if (trace_type & BIT_TIMESTAMP)
123     {
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);
126       elt++;
127     }
128
129   if (trace_type & BIT_APPDATA)
130     {
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);
133       elt++;
134     }
135
136   return s;
137 }
138
139
140 int
141 ip6_ioam_trace_get_sizeof_handler (u32 * result)
142 {
143   u16 size = 0;
144   u8 trace_data_size = 0;
145   trace_profile *profile = NULL;
146
147   *result = 0;
148
149   profile = trace_profile_find ();
150
151   if (PREDICT_FALSE (!profile))
152     {
153       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
154       return (-1);
155     }
156
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;
160
161   if (PREDICT_FALSE (profile->num_elts * trace_data_size > 254))
162     return VNET_API_ERROR_INVALID_VALUE;
163
164   size +=
165     sizeof (ioam_trace_option_t) + (profile->num_elts * trace_data_size);
166   *result = size;
167
168   return 0;
169 }
170
171
172
173 int
174 ip6_hop_by_hop_ioam_trace_rewrite_handler (u8 * rewrite_string,
175                                            u8 * rewrite_size)
176 {
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;
181
182
183   profile = trace_profile_find ();
184
185   if (PREDICT_FALSE (!profile))
186     {
187       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
188       return (-1);
189     }
190
191   if (PREDICT_FALSE (!rewrite_string))
192     return -1;
193
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;
204   *rewrite_size =
205     sizeof (ioam_trace_option_t) + (trace_option_elts * trace_data_size);
206
207   return 0;
208 }
209
210 always_inline void
211 ip6_hbh_ioam_loopback_handler (vlib_buffer_t * b, ip6_header_t * ip,
212                                ioam_trace_option_t * trace)
213 {
214   u32 buf_index;
215   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
216   vlib_buffer_t *b0;
217   vlib_frame_t *nf = 0;
218   u32 *to_next;
219   vlib_node_t *next_node;
220   ip6_header_t *ip6;
221   ip6_hop_by_hop_header_t *hbh;
222   ioam_trace_option_t *opt;
223   udp_ping_t *udp;
224
225   next_node = vlib_get_node_by_name (hm->vlib_main, (u8 *) "ip6-lookup");
226   nf = vlib_get_frame_to_node (hm->vlib_main, next_node->index);
227   nf->n_vectors = 0;
228   to_next = vlib_frame_vector_args (nf);
229
230   b0 = vlib_buffer_copy (hm->vlib_main, b);
231   buf_index = vlib_get_buffer_index (hm->vlib_main, b0);
232
233   vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
234   vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
235
236   ip6 = vlib_buffer_get_current (b0);
237   hbh = (ip6_hop_by_hop_header_t *) (ip6 + 1);
238   opt = (ioam_trace_option_t *)
239     ip6_hbh_get_option (hbh, HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST);
240
241   udp = (udp_ping_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
242   udp_ping_create_reply_from_probe_ip6 (ip6, hbh, udp);
243   ip6_hbh_ioam_trace_set_bit (opt, BIT_LOOPBACK_REPLY);
244
245   *to_next = buf_index;
246   nf->n_vectors++;
247   to_next++;
248
249   vlib_put_frame_to_node (hm->vlib_main, next_node->index, nf);
250   ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK, 1);
251 }
252
253 int
254 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
255                                       ip6_hop_by_hop_option_t * opt)
256 {
257   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
258   u8 elt_index = 0;
259   ioam_trace_option_t *trace = (ioam_trace_option_t *) opt;
260   u32 adj_index = vnet_buffer (b)->ip.adj_index[VLIB_TX];
261   ip_adjacency_t *adj = adj_get (adj_index);
262   time_u64_t time_u64;
263   u32 *elt;
264   int rv = 0;
265   trace_profile *profile = NULL;
266
267
268   profile = trace_profile_find ();
269
270   if (PREDICT_FALSE (!profile))
271     {
272       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
273       return (-1);
274     }
275
276   /* Don't trace loopback reply packets */
277   if (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK_REPLY)
278     {
279       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK_REPLY,
280                                               1);
281       return rv;
282     }
283
284   time_u64.as_u64 = 0;
285
286   if (PREDICT_TRUE (trace->trace_hdr.data_list_elts_left))
287     {
288       trace->trace_hdr.data_list_elts_left--;
289       /* fetch_trace_data_size returns in bytes. Convert it to 4-bytes
290        * to skip to this node's location.
291        */
292       elt_index =
293         trace->trace_hdr.data_list_elts_left *
294         fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
295       elt = &trace->trace_hdr.elts[elt_index];
296       if (trace->trace_hdr.ioam_trace_type & BIT_TTL_NODEID)
297         {
298           *elt =
299             clib_host_to_net_u32 ((ip->hop_limit << 24) | profile->node_id);
300           elt++;
301         }
302
303       if (trace->trace_hdr.ioam_trace_type & BIT_ING_INTERFACE)
304         {
305           *elt =
306             (vnet_buffer (b)->sw_if_index[VLIB_RX] & 0xFFFF) << 16 |
307             (adj->rewrite_header.sw_if_index & 0xFFFF);
308           *elt = clib_host_to_net_u32 (*elt);
309           elt++;
310         }
311
312       if (trace->trace_hdr.ioam_trace_type & BIT_TIMESTAMP)
313         {
314           /* Send least significant 32 bits */
315           f64 time_f64 =
316             (f64) (((f64) hm->unix_time_0) +
317                    (vlib_time_now (hm->vlib_main) - hm->vlib_time_0));
318
319           time_u64.as_u64 = time_f64 * trace_tsp_mul[profile->trace_tsp];
320           *elt = clib_host_to_net_u32 (time_u64.as_u32[0]);
321           elt++;
322         }
323
324       if (trace->trace_hdr.ioam_trace_type & BIT_APPDATA)
325         {
326           /* $$$ set elt0->app_data */
327           *elt = clib_host_to_net_u32 (profile->app_data);
328           elt++;
329         }
330
331
332       if (PREDICT_FALSE (trace->trace_hdr.ioam_trace_type & BIT_LOOPBACK))
333         {
334           /* if loopback flag set then copy the packet
335            * and send it back to source */
336           ip6_hbh_ioam_loopback_handler (b, ip, trace);
337         }
338
339       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_UPDATED, 1);
340     }
341   else
342     {
343       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_FULL, 1);
344     }
345   return (rv);
346 }
347
348 u8 *
349 ip6_hbh_ioam_trace_data_list_trace_handler (u8 * s,
350                                             ip6_hop_by_hop_option_t * opt)
351 {
352   ioam_trace_option_t *trace;
353   u8 trace_data_size_in_words = 0;
354   u32 *elt;
355   int elt_index = 0;
356
357   trace = (ioam_trace_option_t *) opt;
358   s =
359     format (s, "  Trace Type 0x%x , %d elts left\n",
360             trace->trace_hdr.ioam_trace_type,
361             trace->trace_hdr.data_list_elts_left);
362   trace_data_size_in_words =
363     fetch_trace_data_size (trace->trace_hdr.ioam_trace_type) / 4;
364   elt = &trace->trace_hdr.elts[0];
365   while ((u8 *) elt <
366          ((u8 *) (&trace->trace_hdr.elts[0]) + trace->hdr.length - 2
367           /* -2 accounts for ioam_trace_type,elts_left */ ))
368     {
369       s = format (s, "    [%d] %U\n", elt_index,
370                   format_ioam_data_list_element,
371                   elt, &trace->trace_hdr.ioam_trace_type);
372       elt_index++;
373       elt += trace_data_size_in_words;
374     }
375   return (s);
376 }
377
378
379 static clib_error_t *
380 ip6_show_ioam_trace_cmd_fn (vlib_main_t * vm,
381                             unformat_input_t * input,
382                             vlib_cli_command_t * cmd)
383 {
384   ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
385   u8 *s = 0;
386   int i = 0;
387
388   for (i = 0; i < IP6_IOAM_TRACE_N_STATS; i++)
389     {
390       s =
391         format (s, " %s - %lu\n", ip6_hop_by_hop_ioam_trace_stats_strings[i],
392                 hm->counters[i]);
393     }
394
395   vlib_cli_output (vm, "%v", s);
396   vec_free (s);
397   return 0;
398 }
399
400
401 /* *INDENT-OFF* */
402 VLIB_CLI_COMMAND (ip6_show_ioam_trace_cmd, static) = {
403   .path = "show ioam trace",
404   .short_help = "iOAM trace statistics",
405   .function = ip6_show_ioam_trace_cmd_fn,
406 };
407 /* *INDENT-ON* */
408
409 /* *INDENT-OFF* */
410 VLIB_PLUGIN_REGISTER () = {
411     .version = VPP_BUILD_VER,
412     .description = "Inbound OAM",
413 };
414 /* *INDENT-ON* */
415
416 static clib_error_t *
417 ip6_hop_by_hop_ioam_trace_init (vlib_main_t * vm)
418 {
419   ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
420   clib_error_t *error;
421
422   if ((error = vlib_call_init_function (vm, ip_main_init)))
423     return (error);
424
425   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
426     return error;
427
428   if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
429     return (error);
430
431   hm->vlib_main = vm;
432   hm->vnet_main = vnet_get_main ();
433   memset (hm->counters, 0, sizeof (hm->counters));
434
435
436   if (ip6_hbh_register_option
437       (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
438        ip6_hbh_ioam_trace_data_list_handler,
439        ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
440     return (clib_error_create
441             ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
442
443
444   if (ip6_hbh_add_register_option (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
445                                    sizeof (ioam_trace_option_t),
446                                    ip6_hop_by_hop_ioam_trace_rewrite_handler)
447       < 0)
448     return (clib_error_create
449             ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
450
451
452   return (0);
453 }
454
455 int
456 ip6_trace_profile_cleanup (void)
457 {
458   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
459
460   hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = 0;
461
462   return 0;
463
464 }
465
466
467 int
468 ip6_trace_profile_setup (void)
469 {
470   u32 trace_size = 0;
471   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
472
473   trace_profile *profile = NULL;
474
475
476   profile = trace_profile_find ();
477
478   if (PREDICT_FALSE (!profile))
479     {
480       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
481       return (-1);
482     }
483
484
485   if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
486     return (-1);
487
488   hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = trace_size;
489
490   return (0);
491 }
492
493
494 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_trace_init);
495
496 /*
497  * fd.io coding-style-patch-verification: ON
498  *
499  * Local Variables:
500  * eval: (c-set-style "gnu")
501  * End:
502  */