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