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