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>
57 * A handy macro to set up a message reply.
58 * Assumes that the following variables are available:
59 * mp - pointer to request message
60 * rmp - pointer to reply message type
64 #define REPLY_MACRO(t) \
66 unix_shared_memory_queue_t * q = \
67 vl_api_client_index_to_input_queue (mp->client_index); \
71 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
72 rmp->_vl_msg_id = ntohs((t)+cm->msg_id_base); \
73 rmp->context = mp->context; \
74 rmp->retval = ntohl(rv); \
76 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
80 /* List of message types that this plugin understands */
82 #define foreach_ioam_cache_plugin_api_msg \
83 _(IOAM_CACHE_IP6_ENABLE_DISABLE, ioam_cache_ip6_enable_disable)
86 ioam_e2e_id_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
88 ioam_e2e_id_option_t *e2e = (ioam_e2e_id_option_t *) opt;
93 format (s, "IP6_HOP_BY_HOP E2E ID = %U\n", format_ip6_address,
102 ioam_e2e_cache_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
104 ioam_e2e_cache_option_t *e2e = (ioam_e2e_cache_option_t *) opt;
109 format (s, "IP6_HOP_BY_HOP E2E CACHE = pool:%d idx:%d\n",
110 e2e->pool_id, e2e->pool_index);
117 /* Action function shared between message handler and debug CLI */
119 ioam_cache_ip6_enable_disable (ioam_cache_main_t * em, u8 is_disable)
121 vlib_main_t *vm = em->vlib_main;
125 ioam_cache_table_init (vm);
126 ip6_hbh_set_next_override (em->cache_hbh_slot);
127 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
128 0, ioam_e2e_id_trace_handler);
129 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
130 0, ioam_e2e_cache_trace_handler);
135 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
136 ioam_cache_table_destroy (vm);
137 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
138 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
144 /* Action function shared between message handler and debug CLI */
146 ioam_tunnel_select_ip6_enable_disable (ioam_cache_main_t * em,
148 u8 no_of_responses, u8 is_disable)
150 vlib_main_t *vm = em->vlib_main;
154 ioam_cache_ts_table_init (vm);
155 em->criteria_oneway = criteria;
156 em->wait_for_responses = no_of_responses;
157 ip6_hbh_set_next_override (em->ts_hbh_slot);
158 ip6_ioam_ts_cache_set_rewrite ();
159 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
160 0, ioam_e2e_id_trace_handler);
161 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
162 0, ioam_e2e_cache_trace_handler);
164 /* Turn on the cleanup process */
165 // vlib_process_signal_event (vm, em->cleanup_process_node_index, 1, 0);
169 ioam_cache_ts_timer_node_enable (vm, 0);
170 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
171 ioam_cache_ts_table_destroy (vm);
172 ip6_ioam_ts_cache_cleanup_rewrite ();
173 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
174 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
180 /* API message handler */
181 static void vl_api_ioam_cache_ip6_enable_disable_t_handler
182 (vl_api_ioam_cache_ip6_enable_disable_t * mp)
184 vl_api_ioam_cache_ip6_enable_disable_reply_t *rmp;
185 ioam_cache_main_t *cm = &ioam_cache_main;
188 rv = ioam_cache_ip6_enable_disable (cm, (int) (mp->is_disable));
189 REPLY_MACRO (VL_API_IOAM_CACHE_IP6_ENABLE_DISABLE_REPLY);
192 /* Set up the API message handling tables */
193 static clib_error_t *
194 ioam_cache_plugin_api_hookup (vlib_main_t * vm)
196 ioam_cache_main_t *sm = &ioam_cache_main;
198 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
200 vl_api_##n##_t_handler, \
202 vl_api_##n##_t_endian, \
203 vl_api_##n##_t_print, \
204 sizeof(vl_api_##n##_t), 1);
205 foreach_ioam_cache_plugin_api_msg;
211 static clib_error_t *
212 set_ioam_cache_command_fn (vlib_main_t * vm,
213 unformat_input_t * input, vlib_cli_command_t * cmd)
215 ioam_cache_main_t *em = &ioam_cache_main;
218 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
220 if (unformat (input, "disable"))
225 ioam_cache_ip6_enable_disable (em, is_disable);
231 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
233 .path = "set ioam ip6 cache",.short_help =
234 "set ioam ip6 cache [disable]",.function = set_ioam_cache_command_fn};
237 #define IOAM_TS_WAIT_FOR_RESPONSES 3
238 static clib_error_t *
239 set_ioam_tunnel_select_command_fn (vlib_main_t * vm,
240 unformat_input_t * input,
241 vlib_cli_command_t * cmd)
243 ioam_cache_main_t *em = &ioam_cache_main;
246 u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
247 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
249 if (unformat (input, "disable"))
251 else if (unformat (input, "rtt"))
253 else if (unformat (input, "oneway"))
255 else if (unformat (input, "wait_for_responses %d", &no_of_responses))
261 ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
268 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
270 .path = "set ioam ip6 sr-tunnel-select",.short_help =
271 "set ioam ip6 sr-tunnel-select [disable] [oneway|rtt] [wait_for_responses <n|default 3>]",.function
272 = set_ioam_tunnel_select_command_fn};
276 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
278 ioam_cache_main_t *cm = &ioam_cache_main;
279 ioam_cache_entry_t *entry = 0;
280 ioam_cache_ts_entry_t *ts_entry = 0;
281 int no_of_threads = vec_len (vlib_worker_threads);
284 pool_foreach (entry, cm->ioam_rewrite_pool, (
286 vlib_cli_output (vm, "%U",
287 format_ioam_cache_entry,
292 for (i = 0; i < no_of_threads; i++)
294 vlib_cli_output (vm, "Number of entries in thread-%d selection pool: %lu\n \
295 (pool found to be full: %lu times)", i,
296 cm->ts_stats[i].inuse, cm->ts_stats[i].add_failed);
299 vlib_worker_thread_barrier_sync (vm);
300 pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
304 format_ioam_cache_ts_entry,
310 vlib_worker_thread_barrier_release (vm);
315 static clib_error_t *
316 show_ioam_cache_command_fn (vlib_main_t * vm,
317 unformat_input_t * input,
318 vlib_cli_command_t * cmd)
322 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
324 if (unformat (input, "verbose"))
327 ioam_cache_table_print (vm, verbose);
334 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
336 .path = "show ioam ip6 cache",.short_help =
337 "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
340 static clib_error_t *
341 ioam_cache_init (vlib_main_t * vm)
343 ioam_cache_main_t *em = &ioam_cache_main;
344 clib_error_t *error = 0;
346 u32 cache_node_index = ioam_cache_node.index;
347 u32 ts_node_index = ioam_cache_ts_node.index;
348 vlib_node_t *ip6_hbyh_node = NULL, *ip6_hbh_pop_node = NULL, *error_node =
351 name = format (0, "ioam_cache_%08x%c", api_version, 0);
353 memset (&ioam_cache_main, 0, sizeof (ioam_cache_main));
354 /* Ask for a correctly-sized block of API message decode slots */
355 em->msg_id_base = vl_msg_api_get_msg_ids
356 ((char *) name, VL_MSG_FIRST_AVAILABLE);
358 error = ioam_cache_plugin_api_hookup (vm);
359 /* Hook this node to ip6-hop-by-hop */
360 ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
362 vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
364 vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
366 ip6_hbh_pop_node = vlib_get_node_by_name (vm, (u8 *) "ip6-pop-hop-by-hop");
367 em->ip6_hbh_pop_node_index = ip6_hbh_pop_node->index;
369 error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
370 em->error_node_index = error_node->index;
378 VLIB_INIT_FUNCTION (ioam_cache_init);
381 * fd.io coding-style-patch-verification: ON
384 * eval: (c-set-style "gnu")