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