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