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,
98 ip6_address_t * sr_localsid, u8 is_disable)
100 vlib_main_t *vm = em->vlib_main;
104 ioam_cache_table_init (vm);
105 em->sr_localsid_cache.as_u64[0] = sr_localsid->as_u64[0];
106 em->sr_localsid_cache.as_u64[1] = sr_localsid->as_u64[1];
107 ip6_hbh_set_next_override (em->cache_hbh_slot);
108 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
109 0, ioam_e2e_id_trace_handler);
110 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
111 0, ioam_e2e_cache_trace_handler);
116 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
117 ioam_cache_table_destroy (vm);
118 em->sr_localsid_cache.as_u64[0] = 0;
119 em->sr_localsid_cache.as_u64[1] = 0;
120 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
121 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
127 /* Action function shared between message handler and debug CLI */
129 ioam_tunnel_select_ip6_enable_disable (ioam_cache_main_t * em,
132 ip6_address_t * sr_localsid,
135 vlib_main_t *vm = em->vlib_main;
139 ioam_cache_ts_table_init (vm);
140 em->criteria_oneway = criteria;
141 em->wait_for_responses = no_of_responses;
142 em->sr_localsid_ts.as_u64[0] = sr_localsid->as_u64[0];
143 em->sr_localsid_ts.as_u64[1] = sr_localsid->as_u64[1];
144 ip6_hbh_set_next_override (em->ts_hbh_slot);
145 ip6_ioam_ts_cache_set_rewrite ();
146 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
147 0, ioam_e2e_id_trace_handler);
148 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
149 0, ioam_e2e_cache_trace_handler);
151 /* Turn on the cleanup process */
152 // vlib_process_signal_event (vm, em->cleanup_process_node_index, 1, 0);
156 ioam_cache_ts_timer_node_enable (vm, 0);
157 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
158 em->sr_localsid_ts.as_u64[0] = 0;
159 em->sr_localsid_ts.as_u64[1] = 0;
160 ioam_cache_ts_table_destroy (vm);
161 ip6_ioam_ts_cache_cleanup_rewrite ();
162 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
163 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
169 /* API message handler */
170 static void vl_api_ioam_cache_ip6_enable_disable_t_handler
171 (vl_api_ioam_cache_ip6_enable_disable_t * mp)
173 vl_api_ioam_cache_ip6_enable_disable_reply_t *rmp;
174 ioam_cache_main_t *cm = &ioam_cache_main;
175 ip6_address_t sr_localsid;
178 sr_localsid.as_u64[0] = 0;
179 sr_localsid.as_u64[1] = 0;
181 ioam_cache_ip6_enable_disable (cm, &sr_localsid, (int) (mp->is_disable));
182 REPLY_MACRO (VL_API_IOAM_CACHE_IP6_ENABLE_DISABLE_REPLY);
185 /* Set up the API message handling tables */
186 static clib_error_t *
187 ioam_cache_plugin_api_hookup (vlib_main_t * vm)
189 ioam_cache_main_t *sm = &ioam_cache_main;
191 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
193 vl_api_##n##_t_handler, \
195 vl_api_##n##_t_endian, \
196 vl_api_##n##_t_print, \
197 sizeof(vl_api_##n##_t), 1);
198 foreach_ioam_cache_plugin_api_msg;
204 #define vl_msg_name_crc_list
205 #include <ioam/ip6/ioam_cache_all_api_h.h>
206 #undef vl_msg_name_crc_list
209 setup_message_id_table (ioam_cache_main_t * sm, api_main_t * am)
211 #define _(id,n,crc) \
212 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
213 foreach_vl_msg_name_crc_ioam_cache;
217 static clib_error_t *
218 set_ioam_cache_command_fn (vlib_main_t * vm,
219 unformat_input_t * input, vlib_cli_command_t * cmd)
221 ioam_cache_main_t *em = &ioam_cache_main;
223 ip6_address_t sr_localsid;
226 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
228 if (unformat (input, "disable"))
230 else if (!address_set
231 && unformat (input, "sr_localsid %U", unformat_ip6_address,
238 if (is_disable == 0 && !address_set)
239 return clib_error_return (0, "Error: SRv6 LocalSID address is mandatory");
241 ioam_cache_ip6_enable_disable (em, &sr_localsid, is_disable);
247 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
249 .path = "set ioam ip6 cache",.short_help =
250 "set ioam ip6 cache sr_localsid <ip6 address> [disable]",.function =
251 set_ioam_cache_command_fn};
254 #define IOAM_TS_WAIT_FOR_RESPONSES 3
255 static clib_error_t *
256 set_ioam_tunnel_select_command_fn (vlib_main_t * vm,
257 unformat_input_t * input,
258 vlib_cli_command_t * cmd)
260 ioam_cache_main_t *em = &ioam_cache_main;
263 ip6_address_t sr_localsid;
265 u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
267 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
269 if (unformat (input, "disable"))
271 else if (unformat (input, "rtt"))
273 else if (unformat (input, "oneway"))
275 else if (unformat (input, "wait_for_responses %d", &no_of_responses))
277 else if (!address_set
278 && unformat (input, "sr_localsid %U", unformat_ip6_address,
284 if (is_disable == 0 && !address_set)
285 return clib_error_return (0,
286 "Error: SRv6 LocalSID address is mandatory to receive response.");
288 ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
289 &sr_localsid, is_disable);
295 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
297 .path = "set ioam ip6 sr-tunnel-select",.short_help =
298 "set ioam ip6 sr-tunnel-select [disable] [oneway|rtt] [wait_for_responses <n|default 3>] \
299 [sr_localsid <ip6 address>]",.function = set_ioam_tunnel_select_command_fn};
303 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
305 ioam_cache_main_t *cm = &ioam_cache_main;
306 ioam_cache_entry_t *entry = 0;
307 ioam_cache_ts_entry_t *ts_entry = 0;
308 int no_of_threads = vec_len (vlib_worker_threads);
311 pool_foreach (entry, cm->ioam_rewrite_pool, (
313 vlib_cli_output (vm, "%U",
314 format_ioam_cache_entry,
319 for (i = 0; i < no_of_threads; i++)
321 vlib_cli_output (vm, "Number of entries in thread-%d selection pool: %lu\n \
322 (pool found to be full: %lu times)", i,
323 cm->ts_stats[i].inuse, cm->ts_stats[i].add_failed);
326 vlib_worker_thread_barrier_sync (vm);
327 pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
331 format_ioam_cache_ts_entry,
337 vlib_worker_thread_barrier_release (vm);
342 static clib_error_t *
343 show_ioam_cache_command_fn (vlib_main_t * vm,
344 unformat_input_t * input,
345 vlib_cli_command_t * cmd)
349 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
351 if (unformat (input, "verbose"))
354 ioam_cache_table_print (vm, verbose);
361 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
363 .path = "show ioam ip6 cache",.short_help =
364 "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
367 static clib_error_t *
368 ioam_cache_init (vlib_main_t * vm)
370 ioam_cache_main_t *em = &ioam_cache_main;
371 clib_error_t *error = 0;
373 u32 cache_node_index = ioam_cache_node.index;
374 u32 ts_node_index = ioam_cache_ts_node.index;
375 vlib_node_t *ip6_hbyh_node = NULL, *ip6_hbh_pop_node = NULL, *error_node =
378 name = format (0, "ioam_cache_%08x%c", api_version, 0);
380 memset (&ioam_cache_main, 0, sizeof (ioam_cache_main));
381 /* Ask for a correctly-sized block of API message decode slots */
382 em->msg_id_base = vl_msg_api_get_msg_ids
383 ((char *) name, VL_MSG_FIRST_AVAILABLE);
385 error = ioam_cache_plugin_api_hookup (vm);
387 /* Add our API messages to the global name_crc hash table */
388 setup_message_id_table (em, &api_main);
390 /* Hook this node to ip6-hop-by-hop */
391 ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
393 vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
395 vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
397 ip6_hbh_pop_node = vlib_get_node_by_name (vm, (u8 *) "ip6-pop-hop-by-hop");
398 em->ip6_hbh_pop_node_index = ip6_hbh_pop_node->index;
400 error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
401 em->error_node_index = error_node->index;
409 VLIB_INIT_FUNCTION (ioam_cache_init);
412 * fd.io coding-style-patch-verification: ON
415 * eval: (c-set-style "gnu")