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