9e90ff9a9207b233cc111487ce430b9483545e1b
[vpp.git] / src / plugins / ioam / ip6 / ioam_cache.c
1 /*
2  * Copyright (c) 2017 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 /*
16  *------------------------------------------------------------------
17  * ioam_cache.c - ioam ip6 API / debug CLI handling
18  *------------------------------------------------------------------
19  */
20
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <ioam/ip6/ioam_cache.h>
24
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
28 #include <vnet/ip/ip6_hop_by_hop.h>
29
30 #include "ioam_cache.h"
31
32 /* define message IDs */
33 #include <ioam/ip6/ioam_cache_msg_enum.h>
34
35 /* define message structures */
36 #define vl_typedefs
37 #include <ioam/ip6/ioam_cache_all_api_h.h>
38 #undef vl_typedefs
39
40 /* define generated endian-swappers */
41 #define vl_endianfun
42 #include <ioam/ip6/ioam_cache_all_api_h.h>
43 #undef vl_endianfun
44
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #define vl_printfun
48 #include <ioam/ip6/ioam_cache_all_api_h.h>
49 #undef vl_printfun
50
51 /* Get the API version number */
52 #define vl_api_version(n,v) static u32 api_version=(v);
53 #include <ioam/ip6/ioam_cache_all_api_h.h>
54 #undef vl_api_version
55
56 /*
57  * A handy macro to set up a message reply.
58  * Assumes that the following variables are available:
59  * mp - pointer to request message
60  * rmp - pointer to reply message type
61  * rv - return value
62  */
63
64 #define REPLY_MACRO(t)                                          \
65 do {                                                            \
66     unix_shared_memory_queue_t * q =                            \
67     vl_api_client_index_to_input_queue (mp->client_index);      \
68     if (!q)                                                     \
69         return;                                                 \
70                                                                 \
71     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
72     rmp->_vl_msg_id = ntohs((t)+cm->msg_id_base);               \
73     rmp->context = mp->context;                                 \
74     rmp->retval = ntohl(rv);                                    \
75                                                                 \
76     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
77 } while(0);
78
79
80 /* List of message types that this plugin understands */
81
82 #define foreach_ioam_cache_plugin_api_msg                        \
83 _(IOAM_CACHE_IP6_ENABLE_DISABLE, ioam_cache_ip6_enable_disable)
84
85 static u8 *
86 ioam_e2e_id_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
87 {
88   ioam_e2e_id_option_t *e2e = (ioam_e2e_id_option_t *) opt;
89
90   if (e2e)
91     {
92       s =
93         format (s, "IP6_HOP_BY_HOP E2E ID = %U\n", format_ip6_address,
94                 &(e2e->id));
95     }
96
97
98   return s;
99 }
100
101 static u8 *
102 ioam_e2e_cache_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
103 {
104   ioam_e2e_cache_option_t *e2e = (ioam_e2e_cache_option_t *) opt;
105
106   if (e2e)
107     {
108       s =
109         format (s, "IP6_HOP_BY_HOP E2E CACHE = pool:%d idx:%d\n",
110                 e2e->pool_id, e2e->pool_index);
111     }
112
113
114   return s;
115 }
116
117 /* Action function shared between message handler and debug CLI */
118 int
119 ioam_cache_ip6_enable_disable (ioam_cache_main_t * em, u8 is_disable)
120 {
121   vlib_main_t *vm = em->vlib_main;
122
123   if (is_disable == 0)
124     {
125       ioam_cache_table_init (vm);
126       ip6_hbh_set_next_override (em->cache_hbh_slot);
127       ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
128                                0, ioam_e2e_id_trace_handler);
129       ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
130                                0, ioam_e2e_cache_trace_handler);
131
132     }
133   else
134     {
135       ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
136       ioam_cache_table_destroy (vm);
137       ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
138       ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
139     }
140
141   return 0;
142 }
143
144 /* Action function shared between message handler and debug CLI */
145 int
146 ioam_tunnel_select_ip6_enable_disable (ioam_cache_main_t * em,
147                                        u8 criteria,
148                                        u8 no_of_responses, u8 is_disable)
149 {
150   vlib_main_t *vm = em->vlib_main;
151
152   if (is_disable == 0)
153     {
154       ioam_cache_ts_table_init (vm);
155       em->criteria_oneway = criteria;
156       em->wait_for_responses = no_of_responses;
157       ip6_hbh_set_next_override (em->ts_hbh_slot);
158       ip6_ioam_ts_cache_set_rewrite ();
159       ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
160                                0, ioam_e2e_id_trace_handler);
161       ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
162                                0, ioam_e2e_cache_trace_handler);
163
164       /* Turn on the cleanup process */
165       //      vlib_process_signal_event (vm, em->cleanup_process_node_index, 1, 0);
166     }
167   else
168     {
169       ioam_cache_ts_timer_node_enable (vm, 0);
170       ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
171       ioam_cache_ts_table_destroy (vm);
172       ip6_ioam_ts_cache_cleanup_rewrite ();
173       ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
174       ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
175     }
176
177   return 0;
178 }
179
180 /* API message handler */
181 static void vl_api_ioam_cache_ip6_enable_disable_t_handler
182   (vl_api_ioam_cache_ip6_enable_disable_t * mp)
183 {
184   vl_api_ioam_cache_ip6_enable_disable_reply_t *rmp;
185   ioam_cache_main_t *cm = &ioam_cache_main;
186   int rv;
187
188   rv = ioam_cache_ip6_enable_disable (cm, (int) (mp->is_disable));
189   REPLY_MACRO (VL_API_IOAM_CACHE_IP6_ENABLE_DISABLE_REPLY);
190 }
191
192 /* Set up the API message handling tables */
193 static clib_error_t *
194 ioam_cache_plugin_api_hookup (vlib_main_t * vm)
195 {
196   ioam_cache_main_t *sm = &ioam_cache_main;
197 #define _(N,n)                                                  \
198     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
199                            #n,                                  \
200                            vl_api_##n##_t_handler,              \
201                            vl_noop_handler,                     \
202                            vl_api_##n##_t_endian,               \
203                            vl_api_##n##_t_print,                \
204                            sizeof(vl_api_##n##_t), 1);
205   foreach_ioam_cache_plugin_api_msg;
206 #undef _
207
208   return 0;
209 }
210
211 static clib_error_t *
212 set_ioam_cache_command_fn (vlib_main_t * vm,
213                            unformat_input_t * input, vlib_cli_command_t * cmd)
214 {
215   ioam_cache_main_t *em = &ioam_cache_main;
216   u8 is_disable = 0;
217
218   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
219     {
220       if (unformat (input, "disable"))
221         is_disable = 1;
222       else
223         break;
224     }
225   ioam_cache_ip6_enable_disable (em, is_disable);
226
227   return 0;
228 }
229
230 /* *INDENT_OFF* */
231 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
232 {
233 .path = "set ioam ip6 cache",.short_help =
234     "set ioam ip6 cache [disable]",.function = set_ioam_cache_command_fn};
235 /* *INDENT_ON* */
236
237 #define IOAM_TS_WAIT_FOR_RESPONSES 3
238 static clib_error_t *
239 set_ioam_tunnel_select_command_fn (vlib_main_t * vm,
240                                    unformat_input_t * input,
241                                    vlib_cli_command_t * cmd)
242 {
243   ioam_cache_main_t *em = &ioam_cache_main;
244   u8 is_disable = 0;
245   u8 one_way = 0;
246   u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
247   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
248     {
249       if (unformat (input, "disable"))
250         is_disable = 1;
251       else if (unformat (input, "rtt"))
252         one_way = 0;
253       else if (unformat (input, "oneway"))
254         one_way = 1;
255       else if (unformat (input, "wait_for_responses %d", &no_of_responses))
256         ;
257       else
258         break;
259     }
260
261   ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
262                                          is_disable);
263
264   return 0;
265 }
266
267 /* *INDENT_OFF* */
268 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
269 {
270 .path = "set ioam ip6 sr-tunnel-select",.short_help =
271     "set ioam ip6 sr-tunnel-select [disable] [oneway|rtt] [wait_for_responses <n|default 3>]",.function
272     = set_ioam_tunnel_select_command_fn};
273 /* *INDENT_ON* */
274
275 static void
276 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
277 {
278   ioam_cache_main_t *cm = &ioam_cache_main;
279   ioam_cache_entry_t *entry = 0;
280   ioam_cache_ts_entry_t *ts_entry = 0;
281   int no_of_threads = vec_len (vlib_worker_threads);
282   int i;
283
284   pool_foreach (entry, cm->ioam_rewrite_pool, (
285                                                 {
286                                                 vlib_cli_output (vm, "%U",
287                                                                  format_ioam_cache_entry,
288                                                                  entry);
289                                                 }));
290
291   if (cm->ts_stats)
292     for (i = 0; i < no_of_threads; i++)
293       {
294         vlib_cli_output (vm, "Number of entries in thread-%d selection pool: %lu\n \
295                           (pool found to be full: %lu times)", i,
296                          cm->ts_stats[i].inuse, cm->ts_stats[i].add_failed);
297
298         if (verbose == 1)
299           vlib_worker_thread_barrier_sync (vm);
300         pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
301                                                        {
302                                                        vlib_cli_output (vm,
303                                                                         "%U",
304                                                                         format_ioam_cache_ts_entry,
305                                                                         ts_entry,
306                                                                         (u32)
307                                                                         i);
308                                                        }
309                       ));
310         vlib_worker_thread_barrier_release (vm);
311       }
312
313 }
314
315 static clib_error_t *
316 show_ioam_cache_command_fn (vlib_main_t * vm,
317                             unformat_input_t * input,
318                             vlib_cli_command_t * cmd)
319 {
320   u8 verbose = 0;
321
322   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
323     {
324       if (unformat (input, "verbose"))
325         verbose = 1;
326     }
327   ioam_cache_table_print (vm, verbose);
328
329
330   return 0;
331 }
332
333 /* *INDENT_OFF* */
334 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
335 {
336 .path = "show ioam ip6 cache",.short_help =
337     "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
338 /* *INDENT_ON* */
339
340 static clib_error_t *
341 ioam_cache_init (vlib_main_t * vm)
342 {
343   ioam_cache_main_t *em = &ioam_cache_main;
344   clib_error_t *error = 0;
345   u8 *name;
346   u32 cache_node_index = ioam_cache_node.index;
347   u32 ts_node_index = ioam_cache_ts_node.index;
348   vlib_node_t *ip6_hbyh_node = NULL, *ip6_hbh_pop_node = NULL, *error_node =
349     NULL;
350
351   name = format (0, "ioam_cache_%08x%c", api_version, 0);
352
353   memset (&ioam_cache_main, 0, sizeof (ioam_cache_main));
354   /* Ask for a correctly-sized block of API message decode slots */
355   em->msg_id_base = vl_msg_api_get_msg_ids
356     ((char *) name, VL_MSG_FIRST_AVAILABLE);
357
358   error = ioam_cache_plugin_api_hookup (vm);
359   /* Hook this node to ip6-hop-by-hop */
360   ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
361   em->cache_hbh_slot =
362     vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
363   em->ts_hbh_slot =
364     vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
365
366   ip6_hbh_pop_node = vlib_get_node_by_name (vm, (u8 *) "ip6-pop-hop-by-hop");
367   em->ip6_hbh_pop_node_index = ip6_hbh_pop_node->index;
368
369   error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
370   em->error_node_index = error_node->index;
371   em->vlib_main = vm;
372
373   vec_free (name);
374
375   return error;
376 }
377
378 VLIB_INIT_FUNCTION (ioam_cache_init);
379
380 /*
381  * fd.io coding-style-patch-verification: ON
382  *
383  * Local Variables:
384  * eval: (c-set-style "gnu")
385  * End:
386  */