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 #define vl_msg_name_crc_list
190 #include <ioam/ip6/ioam_cache_all_api_h.h>
191 #undef vl_msg_name_crc_list
194 setup_message_id_table (ioam_cache_main_t * sm, api_main_t * am)
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;
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)
206 ioam_cache_main_t *em = &ioam_cache_main;
209 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
211 if (unformat (input, "disable"))
216 ioam_cache_ip6_enable_disable (em, is_disable);
222 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
224 .path = "set ioam ip6 cache",.short_help =
225 "set ioam ip6 cache [disable]",.function = set_ioam_cache_command_fn};
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)
234 ioam_cache_main_t *em = &ioam_cache_main;
237 u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
238 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
240 if (unformat (input, "disable"))
242 else if (unformat (input, "rtt"))
244 else if (unformat (input, "oneway"))
246 else if (unformat (input, "wait_for_responses %d", &no_of_responses))
252 ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
259 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
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};
267 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
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);
275 pool_foreach (entry, cm->ioam_rewrite_pool, (
277 vlib_cli_output (vm, "%U",
278 format_ioam_cache_entry,
283 for (i = 0; i < no_of_threads; i++)
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);
290 vlib_worker_thread_barrier_sync (vm);
291 pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
295 format_ioam_cache_ts_entry,
301 vlib_worker_thread_barrier_release (vm);
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)
313 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
315 if (unformat (input, "verbose"))
318 ioam_cache_table_print (vm, verbose);
325 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
327 .path = "show ioam ip6 cache",.short_help =
328 "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
331 static clib_error_t *
332 ioam_cache_init (vlib_main_t * vm)
334 ioam_cache_main_t *em = &ioam_cache_main;
335 clib_error_t *error = 0;
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 =
342 name = format (0, "ioam_cache_%08x%c", api_version, 0);
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);
349 error = ioam_cache_plugin_api_hookup (vm);
351 /* Add our API messages to the global name_crc hash table */
352 setup_message_id_table (em, &api_main);
354 /* Hook this node to ip6-hop-by-hop */
355 ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
357 vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
359 vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
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;
364 error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
365 em->error_node_index = error_node->index;
373 VLIB_INIT_FUNCTION (ioam_cache_init);
376 * fd.io coding-style-patch-verification: ON
379 * eval: (c-set-style "gnu")