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