IOAM Coverity fix
[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 buf_index;
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   udp_ping_t *udp;
222
223   next_node = vlib_get_node_by_name (hm->vlib_main, (u8 *) "ip6-lookup");
224   nf = vlib_get_frame_to_node (hm->vlib_main, next_node->index);
225   nf->n_vectors = 0;
226   to_next = vlib_frame_vector_args (nf);
227
228   b0 = vlib_buffer_copy (hm->vlib_main, b);
229   buf_index = vlib_get_buffer_index (hm->vlib_main, b0);
230
231   vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
232   vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
233
234   ip6 = vlib_buffer_get_current (b0);
235   hbh = (ip6_hop_by_hop_header_t *) (ip6 + 1);
236   opt = (ioam_trace_option_t *)
237     ip6_hbh_get_option (hbh, HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST);
238
239   udp = (udp_ping_t *) ((u8 *) hbh + ((hbh->length + 1) << 3));
240   udp_ping_create_reply_from_probe_ip6 (ip6, hbh, udp);
241   ip6_hbh_ioam_trace_set_bit (opt, BIT_LOOPBACK_REPLY);
242
243   *to_next = buf_index;
244   nf->n_vectors++;
245   to_next++;
246
247   vlib_put_frame_to_node (hm->vlib_main, next_node->index, nf);
248   ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_LOOPBACK, 1);
249 }
250
251 int
252 ip6_hbh_ioam_trace_data_list_handler (vlib_buffer_t * b, ip6_header_t * ip,
253                                       ip6_hop_by_hop_option_t * opt)
254 {
255   ip6_main_t *im = &ip6_main;
256   ip_lookup_main_t *lm = &im->lookup_main;
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 = ip_get_adjacency (lm, 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 };
413 /* *INDENT-ON* */
414
415 static clib_error_t *
416 ip6_hop_by_hop_ioam_trace_init (vlib_main_t * vm)
417 {
418   ip6_hop_by_hop_ioam_trace_main_t *hm = &ip6_hop_by_hop_ioam_trace_main;
419   clib_error_t *error;
420
421   if ((error = vlib_call_init_function (vm, ip_main_init)))
422     return (error);
423
424   if ((error = vlib_call_init_function (vm, ip6_lookup_init)))
425     return error;
426
427   if ((error = vlib_call_init_function (vm, ip6_hop_by_hop_ioam_init)))
428     return (error);
429
430   hm->vlib_main = vm;
431   hm->vnet_main = vnet_get_main ();
432   memset (hm->counters, 0, sizeof (hm->counters));
433
434
435   if (ip6_hbh_register_option
436       (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
437        ip6_hbh_ioam_trace_data_list_handler,
438        ip6_hbh_ioam_trace_data_list_trace_handler) < 0)
439     return (clib_error_create
440             ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST failed"));
441
442
443   if (ip6_hbh_add_register_option (HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST,
444                                    sizeof (ioam_trace_option_t),
445                                    ip6_hop_by_hop_ioam_trace_rewrite_handler)
446       < 0)
447     return (clib_error_create
448             ("registration of HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST for rewrite failed"));
449
450
451   return (0);
452 }
453
454 int
455 ip6_trace_profile_cleanup (void)
456 {
457   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
458
459   hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = 0;
460
461   return 0;
462
463 }
464
465
466 int
467 ip6_trace_profile_setup (void)
468 {
469   u32 trace_size = 0;
470   ip6_hop_by_hop_ioam_main_t *hm = &ip6_hop_by_hop_ioam_main;
471
472   trace_profile *profile = NULL;
473
474
475   profile = trace_profile_find ();
476
477   if (PREDICT_FALSE (!profile))
478     {
479       ip6_ioam_trace_stats_increment_counter (IP6_IOAM_TRACE_PROFILE_MISS, 1);
480       return (-1);
481     }
482
483
484   if (ip6_ioam_trace_get_sizeof_handler (&trace_size) < 0)
485     return (-1);
486
487   hm->options_size[HBH_OPTION_TYPE_IOAM_TRACE_DATA_LIST] = trace_size;
488
489   return (0);
490 }
491
492
493 VLIB_INIT_FUNCTION (ip6_hop_by_hop_ioam_trace_init);
494
495 /*
496  * fd.io coding-style-patch-verification: ON
497  *
498  * Local Variables:
499  * eval: (c-set-style "gnu")
500  * End:
501  */