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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 *------------------------------------------------------------------
17 * ioam_cache.c - ioam ip6 API / debug CLI handling
18 *------------------------------------------------------------------
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <ioam/ip6/ioam_cache.h>
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
28 #include <vnet/ip/ip6_hop_by_hop.h>
30 #include "ioam_cache.h"
32 /* define message IDs */
33 #include <ioam/ip6/ioam_cache_msg_enum.h>
35 /* define message structures */
37 #include <ioam/ip6/ioam_cache_all_api_h.h>
40 /* define generated endian-swappers */
42 #include <ioam/ip6/ioam_cache_all_api_h.h>
45 /* instantiate all the print functions we know about */
46 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
48 #include <ioam/ip6/ioam_cache_all_api_h.h>
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>
56 #define REPLY_MSG_ID_BASE cm->msg_id_base
57 #include <vlibapi/api_helper_macros.h>
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)
64 ioam_e2e_id_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
66 ioam_e2e_id_option_t *e2e = (ioam_e2e_id_option_t *) opt;
71 format (s, "IP6_HOP_BY_HOP E2E ID = %U\n", format_ip6_address,
80 ioam_e2e_cache_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
82 ioam_e2e_cache_option_t *e2e = (ioam_e2e_cache_option_t *) opt;
87 format (s, "IP6_HOP_BY_HOP E2E CACHE = pool:%d idx:%d\n",
88 e2e->pool_id, e2e->pool_index);
95 /* Action function shared between message handler and debug CLI */
97 ioam_cache_ip6_enable_disable (ioam_cache_main_t * em, u8 is_disable)
99 vlib_main_t *vm = em->vlib_main;
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);
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);
122 /* Action function shared between message handler and debug CLI */
124 ioam_tunnel_select_ip6_enable_disable (ioam_cache_main_t * em,
126 u8 no_of_responses, u8 is_disable)
128 vlib_main_t *vm = em->vlib_main;
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);
142 /* Turn on the cleanup process */
143 // vlib_process_signal_event (vm, em->cleanup_process_node_index, 1, 0);
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);
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)
162 vl_api_ioam_cache_ip6_enable_disable_reply_t *rmp;
163 ioam_cache_main_t *cm = &ioam_cache_main;
166 rv = ioam_cache_ip6_enable_disable (cm, (int) (mp->is_disable));
167 REPLY_MACRO (VL_API_IOAM_CACHE_IP6_ENABLE_DISABLE_REPLY);
170 /* Set up the API message handling tables */
171 static clib_error_t *
172 ioam_cache_plugin_api_hookup (vlib_main_t * vm)
174 ioam_cache_main_t *sm = &ioam_cache_main;
176 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
178 vl_api_##n##_t_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;
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)
193 ioam_cache_main_t *em = &ioam_cache_main;
196 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
198 if (unformat (input, "disable"))
203 ioam_cache_ip6_enable_disable (em, is_disable);
209 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
211 .path = "set ioam ip6 cache",.short_help =
212 "set ioam ip6 cache [disable]",.function = set_ioam_cache_command_fn};
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)
221 ioam_cache_main_t *em = &ioam_cache_main;
224 u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
225 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
227 if (unformat (input, "disable"))
229 else if (unformat (input, "rtt"))
231 else if (unformat (input, "oneway"))
233 else if (unformat (input, "wait_for_responses %d", &no_of_responses))
239 ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
246 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
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};
254 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
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);
262 pool_foreach (entry, cm->ioam_rewrite_pool, (
264 vlib_cli_output (vm, "%U",
265 format_ioam_cache_entry,
270 for (i = 0; i < no_of_threads; i++)
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);
277 vlib_worker_thread_barrier_sync (vm);
278 pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
282 format_ioam_cache_ts_entry,
288 vlib_worker_thread_barrier_release (vm);
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)
300 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
302 if (unformat (input, "verbose"))
305 ioam_cache_table_print (vm, verbose);
312 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
314 .path = "show ioam ip6 cache",.short_help =
315 "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
318 static clib_error_t *
319 ioam_cache_init (vlib_main_t * vm)
321 ioam_cache_main_t *em = &ioam_cache_main;
322 clib_error_t *error = 0;
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 =
329 name = format (0, "ioam_cache_%08x%c", api_version, 0);
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);
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");
340 vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
342 vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
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;
347 error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
348 em->error_node_index = error_node->index;
356 VLIB_INIT_FUNCTION (ioam_cache_init);
359 * fd.io coding-style-patch-verification: ON
362 * eval: (c-set-style "gnu")