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 <vnet/ip/ip6_hop_by_hop.h>
29 #include "ioam_cache.h"
31 /* define message IDs */
32 #include <ioam/ip6/ioam_cache_msg_enum.h>
34 /* define message structures */
36 #include <ioam/ip6/ioam_cache_all_api_h.h>
39 /* define generated endian-swappers */
41 #include <ioam/ip6/ioam_cache_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <ioam/ip6/ioam_cache_all_api_h.h>
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <ioam/ip6/ioam_cache_all_api_h.h>
55 #define REPLY_MSG_ID_BASE cm->msg_id_base
56 #include <vlibapi/api_helper_macros.h>
58 ioam_cache_main_t ioam_cache_main;
60 /* List of message types that this plugin understands */
61 #define foreach_ioam_cache_plugin_api_msg \
62 _(IOAM_CACHE_IP6_ENABLE_DISABLE, ioam_cache_ip6_enable_disable)
65 ioam_e2e_id_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
67 ioam_e2e_id_option_t *e2e = (ioam_e2e_id_option_t *) opt;
72 format (s, "IP6_HOP_BY_HOP E2E ID = %U\n", format_ip6_address,
81 ioam_e2e_cache_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
83 ioam_e2e_cache_option_t *e2e = (ioam_e2e_cache_option_t *) opt;
88 format (s, "IP6_HOP_BY_HOP E2E CACHE = pool:%d idx:%d\n",
89 e2e->pool_id, e2e->pool_index);
96 /* Action function shared between message handler and debug CLI */
98 ioam_cache_ip6_enable_disable (ioam_cache_main_t * em,
99 ip6_address_t * sr_localsid, u8 is_disable)
101 vlib_main_t *vm = em->vlib_main;
105 ioam_cache_table_init (vm);
106 em->sr_localsid_cache.as_u64[0] = sr_localsid->as_u64[0];
107 em->sr_localsid_cache.as_u64[1] = sr_localsid->as_u64[1];
108 ip6_hbh_set_next_override (em->cache_hbh_slot);
109 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
110 0, ioam_e2e_id_trace_handler);
111 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
112 0, ioam_e2e_cache_trace_handler);
117 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
118 ioam_cache_table_destroy (vm);
119 em->sr_localsid_cache.as_u64[0] = 0;
120 em->sr_localsid_cache.as_u64[1] = 0;
121 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
122 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
128 /* Action function shared between message handler and debug CLI */
130 ioam_tunnel_select_ip6_enable_disable (ioam_cache_main_t * em,
133 ip6_address_t * sr_localsid,
136 vlib_main_t *vm = em->vlib_main;
140 ioam_cache_ts_table_init (vm);
141 em->criteria_oneway = criteria;
142 em->wait_for_responses = no_of_responses;
143 em->sr_localsid_ts.as_u64[0] = sr_localsid->as_u64[0];
144 em->sr_localsid_ts.as_u64[1] = sr_localsid->as_u64[1];
145 ip6_hbh_set_next_override (em->ts_hbh_slot);
146 ip6_ioam_ts_cache_set_rewrite ();
147 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
148 0, ioam_e2e_id_trace_handler);
149 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
150 0, ioam_e2e_cache_trace_handler);
152 /* Turn on the cleanup process */
153 // vlib_process_signal_event (vm, em->cleanup_process_node_index, 1, 0);
157 ioam_cache_ts_timer_node_enable (vm, 0);
158 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
159 em->sr_localsid_ts.as_u64[0] = 0;
160 em->sr_localsid_ts.as_u64[1] = 0;
161 ioam_cache_ts_table_destroy (vm);
162 ip6_ioam_ts_cache_cleanup_rewrite ();
163 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
164 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
170 /* API message handler */
171 static void vl_api_ioam_cache_ip6_enable_disable_t_handler
172 (vl_api_ioam_cache_ip6_enable_disable_t * mp)
174 vl_api_ioam_cache_ip6_enable_disable_reply_t *rmp;
175 ioam_cache_main_t *cm = &ioam_cache_main;
176 ip6_address_t sr_localsid;
179 sr_localsid.as_u64[0] = 0;
180 sr_localsid.as_u64[1] = 0;
182 ioam_cache_ip6_enable_disable (cm, &sr_localsid, (int) (mp->is_disable));
183 REPLY_MACRO (VL_API_IOAM_CACHE_IP6_ENABLE_DISABLE_REPLY);
186 /* Set up the API message handling tables */
187 static clib_error_t *
188 ioam_cache_plugin_api_hookup (vlib_main_t * vm)
190 ioam_cache_main_t *sm = &ioam_cache_main;
192 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
194 vl_api_##n##_t_handler, \
196 vl_api_##n##_t_endian, \
197 vl_api_##n##_t_print, \
198 sizeof(vl_api_##n##_t), 1);
199 foreach_ioam_cache_plugin_api_msg;
205 #define vl_msg_name_crc_list
206 #include <ioam/ip6/ioam_cache_all_api_h.h>
207 #undef vl_msg_name_crc_list
210 setup_message_id_table (ioam_cache_main_t * sm, api_main_t * am)
212 #define _(id,n,crc) \
213 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
214 foreach_vl_msg_name_crc_ioam_cache;
218 static clib_error_t *
219 set_ioam_cache_command_fn (vlib_main_t * vm,
220 unformat_input_t * input, vlib_cli_command_t * cmd)
222 ioam_cache_main_t *em = &ioam_cache_main;
224 ip6_address_t sr_localsid;
227 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
229 if (unformat (input, "disable"))
231 else if (!address_set
232 && unformat (input, "sr_localsid %U", unformat_ip6_address,
239 if (is_disable == 0 && !address_set)
240 return clib_error_return (0, "Error: SRv6 LocalSID address is mandatory");
242 ioam_cache_ip6_enable_disable (em, &sr_localsid, is_disable);
248 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
250 .path = "set ioam ip6 cache",.short_help =
251 "set ioam ip6 cache sr_localsid <ip6 address> [disable]",.function =
252 set_ioam_cache_command_fn};
255 #define IOAM_TS_WAIT_FOR_RESPONSES 3
256 static clib_error_t *
257 set_ioam_tunnel_select_command_fn (vlib_main_t * vm,
258 unformat_input_t * input,
259 vlib_cli_command_t * cmd)
261 ioam_cache_main_t *em = &ioam_cache_main;
264 ip6_address_t sr_localsid;
266 u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
268 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
270 if (unformat (input, "disable"))
272 else if (unformat (input, "rtt"))
274 else if (unformat (input, "oneway"))
276 else if (unformat (input, "wait_for_responses %d", &no_of_responses))
278 else if (!address_set
279 && unformat (input, "sr_localsid %U", unformat_ip6_address,
285 if (is_disable == 0 && !address_set)
286 return clib_error_return (0,
287 "Error: SRv6 LocalSID address is mandatory to receive response.");
289 ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
290 &sr_localsid, is_disable);
296 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
298 .path = "set ioam ip6 sr-tunnel-select",.short_help =
299 "set ioam ip6 sr-tunnel-select [disable] [oneway|rtt] [wait_for_responses <n|default 3>] \
300 [sr_localsid <ip6 address>]",.function = set_ioam_tunnel_select_command_fn};
304 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
306 ioam_cache_main_t *cm = &ioam_cache_main;
307 ioam_cache_entry_t *entry = 0;
308 ioam_cache_ts_entry_t *ts_entry = 0;
309 int no_of_threads = vec_len (vlib_worker_threads);
312 pool_foreach (entry, cm->ioam_rewrite_pool, (
314 vlib_cli_output (vm, "%U",
315 format_ioam_cache_entry,
320 for (i = 0; i < no_of_threads; i++)
322 vlib_cli_output (vm, "Number of entries in thread-%d selection pool: %lu\n \
323 (pool found to be full: %lu times)", i,
324 cm->ts_stats[i].inuse, cm->ts_stats[i].add_failed);
327 vlib_worker_thread_barrier_sync (vm);
328 pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
332 format_ioam_cache_ts_entry,
338 vlib_worker_thread_barrier_release (vm);
343 static clib_error_t *
344 show_ioam_cache_command_fn (vlib_main_t * vm,
345 unformat_input_t * input,
346 vlib_cli_command_t * cmd)
350 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
352 if (unformat (input, "verbose"))
355 return clib_error_return (0, "unknown input `%U'",
356 format_unformat_error, input);
358 ioam_cache_table_print (vm, verbose);
365 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
367 .path = "show ioam ip6 cache",.short_help =
368 "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
371 static clib_error_t *
372 ioam_cache_init (vlib_main_t * vm)
374 ioam_cache_main_t *em = &ioam_cache_main;
375 clib_error_t *error = 0;
377 u32 cache_node_index = ioam_cache_node.index;
378 u32 ts_node_index = ioam_cache_ts_node.index;
379 vlib_node_t *ip6_hbyh_node = NULL, *ip6_hbh_pop_node = NULL, *error_node =
382 name = format (0, "ioam_cache_%08x%c", api_version, 0);
384 memset (&ioam_cache_main, 0, sizeof (ioam_cache_main));
385 /* Ask for a correctly-sized block of API message decode slots */
386 em->msg_id_base = vl_msg_api_get_msg_ids
387 ((char *) name, VL_MSG_FIRST_AVAILABLE);
389 error = ioam_cache_plugin_api_hookup (vm);
391 /* Add our API messages to the global name_crc hash table */
392 setup_message_id_table (em, &api_main);
394 /* Hook this node to ip6-hop-by-hop */
395 ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
397 vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
399 vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
401 ip6_hbh_pop_node = vlib_get_node_by_name (vm, (u8 *) "ip6-pop-hop-by-hop");
402 em->ip6_hbh_pop_node_index = ip6_hbh_pop_node->index;
404 error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
405 em->error_node_index = error_node->index;
413 VLIB_INIT_FUNCTION (ioam_cache_init);
416 * fd.io coding-style-patch-verification: ON
419 * eval: (c-set-style "gnu")