API:replaced all REPLY_MACRO's with api_helper_macros.h
[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 static clib_error_t *
190 set_ioam_cache_command_fn (vlib_main_t * vm,
191                            unformat_input_t * input, vlib_cli_command_t * cmd)
192 {
193   ioam_cache_main_t *em = &ioam_cache_main;
194   u8 is_disable = 0;
195
196   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
197     {
198       if (unformat (input, "disable"))
199         is_disable = 1;
200       else
201         break;
202     }
203   ioam_cache_ip6_enable_disable (em, is_disable);
204
205   return 0;
206 }
207
208 /* *INDENT_OFF* */
209 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
210 {
211 .path = "set ioam ip6 cache",.short_help =
212     "set ioam ip6 cache [disable]",.function = set_ioam_cache_command_fn};
213 /* *INDENT_ON* */
214
215 #define IOAM_TS_WAIT_FOR_RESPONSES 3
216 static clib_error_t *
217 set_ioam_tunnel_select_command_fn (vlib_main_t * vm,
218                                    unformat_input_t * input,
219                                    vlib_cli_command_t * cmd)
220 {
221   ioam_cache_main_t *em = &ioam_cache_main;
222   u8 is_disable = 0;
223   u8 one_way = 0;
224   u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
225   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
226     {
227       if (unformat (input, "disable"))
228         is_disable = 1;
229       else if (unformat (input, "rtt"))
230         one_way = 0;
231       else if (unformat (input, "oneway"))
232         one_way = 1;
233       else if (unformat (input, "wait_for_responses %d", &no_of_responses))
234         ;
235       else
236         break;
237     }
238
239   ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
240                                          is_disable);
241
242   return 0;
243 }
244
245 /* *INDENT_OFF* */
246 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
247 {
248 .path = "set ioam ip6 sr-tunnel-select",.short_help =
249     "set ioam ip6 sr-tunnel-select [disable] [oneway|rtt] [wait_for_responses <n|default 3>]",.function
250     = set_ioam_tunnel_select_command_fn};
251 /* *INDENT_ON* */
252
253 static void
254 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
255 {
256   ioam_cache_main_t *cm = &ioam_cache_main;
257   ioam_cache_entry_t *entry = 0;
258   ioam_cache_ts_entry_t *ts_entry = 0;
259   int no_of_threads = vec_len (vlib_worker_threads);
260   int i;
261
262   pool_foreach (entry, cm->ioam_rewrite_pool, (
263                                                 {
264                                                 vlib_cli_output (vm, "%U",
265                                                                  format_ioam_cache_entry,
266                                                                  entry);
267                                                 }));
268
269   if (cm->ts_stats)
270     for (i = 0; i < no_of_threads; i++)
271       {
272         vlib_cli_output (vm, "Number of entries in thread-%d selection pool: %lu\n \
273                           (pool found to be full: %lu times)", i,
274                          cm->ts_stats[i].inuse, cm->ts_stats[i].add_failed);
275
276         if (verbose == 1)
277           vlib_worker_thread_barrier_sync (vm);
278         pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
279                                                        {
280                                                        vlib_cli_output (vm,
281                                                                         "%U",
282                                                                         format_ioam_cache_ts_entry,
283                                                                         ts_entry,
284                                                                         (u32)
285                                                                         i);
286                                                        }
287                       ));
288         vlib_worker_thread_barrier_release (vm);
289       }
290
291 }
292
293 static clib_error_t *
294 show_ioam_cache_command_fn (vlib_main_t * vm,
295                             unformat_input_t * input,
296                             vlib_cli_command_t * cmd)
297 {
298   u8 verbose = 0;
299
300   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
301     {
302       if (unformat (input, "verbose"))
303         verbose = 1;
304     }
305   ioam_cache_table_print (vm, verbose);
306
307
308   return 0;
309 }
310
311 /* *INDENT_OFF* */
312 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
313 {
314 .path = "show ioam ip6 cache",.short_help =
315     "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
316 /* *INDENT_ON* */
317
318 static clib_error_t *
319 ioam_cache_init (vlib_main_t * vm)
320 {
321   ioam_cache_main_t *em = &ioam_cache_main;
322   clib_error_t *error = 0;
323   u8 *name;
324   u32 cache_node_index = ioam_cache_node.index;
325   u32 ts_node_index = ioam_cache_ts_node.index;
326   vlib_node_t *ip6_hbyh_node = NULL, *ip6_hbh_pop_node = NULL, *error_node =
327     NULL;
328
329   name = format (0, "ioam_cache_%08x%c", api_version, 0);
330
331   memset (&ioam_cache_main, 0, sizeof (ioam_cache_main));
332   /* Ask for a correctly-sized block of API message decode slots */
333   em->msg_id_base = vl_msg_api_get_msg_ids
334     ((char *) name, VL_MSG_FIRST_AVAILABLE);
335
336   error = ioam_cache_plugin_api_hookup (vm);
337   /* Hook this node to ip6-hop-by-hop */
338   ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
339   em->cache_hbh_slot =
340     vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
341   em->ts_hbh_slot =
342     vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
343
344   ip6_hbh_pop_node = vlib_get_node_by_name (vm, (u8 *) "ip6-pop-hop-by-hop");
345   em->ip6_hbh_pop_node_index = ip6_hbh_pop_node->index;
346
347   error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
348   em->error_node_index = error_node->index;
349   em->vlib_main = vm;
350
351   vec_free (name);
352
353   return error;
354 }
355
356 VLIB_INIT_FUNCTION (ioam_cache_init);
357
358 /*
359  * fd.io coding-style-patch-verification: ON
360  *
361  * Local Variables:
362  * eval: (c-set-style "gnu")
363  * End:
364  */