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.api_enum.h>
33 #include <ioam/ip6/ioam_cache.api_types.h>
35 #define REPLY_MSG_ID_BASE cm->msg_id_base
36 #include <vlibapi/api_helper_macros.h>
38 ioam_cache_main_t ioam_cache_main;
41 ioam_e2e_id_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
43 ioam_e2e_id_option_t *e2e = (ioam_e2e_id_option_t *) opt;
48 format (s, "IP6_HOP_BY_HOP E2E ID = %U\n", format_ip6_address,
57 ioam_e2e_cache_trace_handler (u8 * s, ip6_hop_by_hop_option_t * opt)
59 ioam_e2e_cache_option_t *e2e = (ioam_e2e_cache_option_t *) opt;
64 format (s, "IP6_HOP_BY_HOP E2E CACHE = pool:%d idx:%d\n",
65 e2e->pool_id, e2e->pool_index);
72 /* Action function shared between message handler and debug CLI */
74 ioam_cache_ip6_enable_disable (ioam_cache_main_t * em,
75 ip6_address_t * sr_localsid, u8 is_disable)
77 vlib_main_t *vm = em->vlib_main;
81 ioam_cache_table_init (vm);
82 em->sr_localsid_cache.as_u64[0] = sr_localsid->as_u64[0];
83 em->sr_localsid_cache.as_u64[1] = sr_localsid->as_u64[1];
84 ip6_hbh_set_next_override (em->cache_hbh_slot);
85 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
86 0, ioam_e2e_id_trace_handler);
87 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
88 0, ioam_e2e_cache_trace_handler);
93 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
94 ioam_cache_table_destroy (vm);
95 em->sr_localsid_cache.as_u64[0] = 0;
96 em->sr_localsid_cache.as_u64[1] = 0;
97 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
98 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
104 /* Action function shared between message handler and debug CLI */
106 ioam_tunnel_select_ip6_enable_disable (ioam_cache_main_t * em,
109 ip6_address_t * sr_localsid,
112 vlib_main_t *vm = em->vlib_main;
116 ioam_cache_ts_table_init (vm);
117 em->criteria_oneway = criteria;
118 em->wait_for_responses = no_of_responses;
119 em->sr_localsid_ts.as_u64[0] = sr_localsid->as_u64[0];
120 em->sr_localsid_ts.as_u64[1] = sr_localsid->as_u64[1];
121 ip6_hbh_set_next_override (em->ts_hbh_slot);
122 ip6_ioam_ts_cache_set_rewrite ();
123 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID,
124 0, ioam_e2e_id_trace_handler);
125 ip6_hbh_register_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID,
126 0, ioam_e2e_cache_trace_handler);
128 /* Turn on the cleanup process */
129 // vlib_process_signal_event (vm, em->cleanup_process_node_index, 1, 0);
133 ioam_cache_ts_timer_node_enable (vm, 0);
134 ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
135 em->sr_localsid_ts.as_u64[0] = 0;
136 em->sr_localsid_ts.as_u64[1] = 0;
137 ioam_cache_ts_table_destroy (vm);
138 ip6_ioam_ts_cache_cleanup_rewrite ();
139 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_EDGE_TO_EDGE_ID);
140 ip6_hbh_unregister_option (HBH_OPTION_TYPE_IOAM_E2E_CACHE_ID);
146 /* API message handler */
147 static void vl_api_ioam_cache_ip6_enable_disable_t_handler
148 (vl_api_ioam_cache_ip6_enable_disable_t * mp)
150 vl_api_ioam_cache_ip6_enable_disable_reply_t *rmp;
151 ioam_cache_main_t *cm = &ioam_cache_main;
152 ip6_address_t sr_localsid;
155 sr_localsid.as_u64[0] = 0;
156 sr_localsid.as_u64[1] = 0;
158 ioam_cache_ip6_enable_disable (cm, &sr_localsid, (int) (mp->is_disable));
159 REPLY_MACRO (VL_API_IOAM_CACHE_IP6_ENABLE_DISABLE_REPLY);
162 static clib_error_t *
163 set_ioam_cache_command_fn (vlib_main_t * vm,
164 unformat_input_t * input, vlib_cli_command_t * cmd)
166 ioam_cache_main_t *em = &ioam_cache_main;
168 ip6_address_t sr_localsid;
171 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
173 if (unformat (input, "disable"))
175 else if (!address_set
176 && unformat (input, "sr_localsid %U", unformat_ip6_address,
183 if (is_disable == 0 && !address_set)
184 return clib_error_return (0, "Error: SRv6 LocalSID address is mandatory");
186 ioam_cache_ip6_enable_disable (em, &sr_localsid, is_disable);
192 VLIB_CLI_COMMAND (set_ioam_cache_command, static) =
194 .path = "set ioam ip6 cache",.short_help =
195 "set ioam ip6 cache sr_localsid <ip6 address> [disable]",.function =
196 set_ioam_cache_command_fn};
199 #define IOAM_TS_WAIT_FOR_RESPONSES 3
200 static clib_error_t *
201 set_ioam_tunnel_select_command_fn (vlib_main_t * vm,
202 unformat_input_t * input,
203 vlib_cli_command_t * cmd)
205 ioam_cache_main_t *em = &ioam_cache_main;
208 ip6_address_t sr_localsid;
210 u8 no_of_responses = IOAM_TS_WAIT_FOR_RESPONSES;
212 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
214 if (unformat (input, "disable"))
216 else if (unformat (input, "rtt"))
218 else if (unformat (input, "oneway"))
220 else if (unformat (input, "wait_for_responses %d", &no_of_responses))
222 else if (!address_set
223 && unformat (input, "sr_localsid %U", unformat_ip6_address,
229 if (is_disable == 0 && !address_set)
230 return clib_error_return (0,
231 "Error: SRv6 LocalSID address is mandatory to receive response.");
233 ioam_tunnel_select_ip6_enable_disable (em, one_way, no_of_responses,
234 &sr_localsid, is_disable);
240 VLIB_CLI_COMMAND (set_ioam_cache_ts_command, static) =
242 .path = "set ioam ip6 sr-tunnel-select",.short_help =
243 "set ioam ip6 sr-tunnel-select [disable] [oneway|rtt] [wait_for_responses <n|default 3>] \
244 [sr_localsid <ip6 address>]",.function = set_ioam_tunnel_select_command_fn};
248 ioam_cache_table_print (vlib_main_t * vm, u8 verbose)
250 ioam_cache_main_t *cm = &ioam_cache_main;
251 ioam_cache_entry_t *entry = 0;
252 ioam_cache_ts_entry_t *ts_entry = 0;
253 int no_of_threads = vec_len (vlib_worker_threads);
256 pool_foreach (entry, cm->ioam_rewrite_pool, (
258 vlib_cli_output (vm, "%U",
259 format_ioam_cache_entry,
264 for (i = 0; i < no_of_threads; i++)
266 vlib_cli_output (vm, "Number of entries in thread-%d selection pool: %lu\n \
267 (pool found to be full: %lu times)", i,
268 cm->ts_stats[i].inuse, cm->ts_stats[i].add_failed);
271 vlib_worker_thread_barrier_sync (vm);
272 pool_foreach (ts_entry, cm->ioam_ts_pool[i], (
276 format_ioam_cache_ts_entry,
282 vlib_worker_thread_barrier_release (vm);
287 static clib_error_t *
288 show_ioam_cache_command_fn (vlib_main_t * vm,
289 unformat_input_t * input,
290 vlib_cli_command_t * cmd)
294 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
296 if (unformat (input, "verbose"))
299 return clib_error_return (0, "unknown input `%U'",
300 format_unformat_error, input);
302 ioam_cache_table_print (vm, verbose);
309 VLIB_CLI_COMMAND (show_ioam_cache_command, static) =
311 .path = "show ioam ip6 cache",.short_help =
312 "show ioam ip6 cache [verbose]",.function = show_ioam_cache_command_fn};
315 #include <ioam/ip6/ioam_cache.api.c>
316 static clib_error_t *
317 ioam_cache_init (vlib_main_t * vm)
320 ioam_cache_main_t *em = &ioam_cache_main;
321 u32 cache_node_index = ioam_cache_node.index;
322 u32 ts_node_index = ioam_cache_ts_node.index;
323 vlib_node_t *ip6_hbyh_node = NULL, *ip6_hbh_pop_node = NULL, *error_node =
326 clib_memset (&ioam_cache_main, 0, sizeof (ioam_cache_main));
327 /* Ask for a correctly-sized block of API message decode slots */
328 em->msg_id_base = setup_message_id_table ();
330 /* Hook this node to ip6-hop-by-hop */
331 ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
333 vlib_node_add_next (vm, ip6_hbyh_node->index, cache_node_index);
335 vlib_node_add_next (vm, ip6_hbyh_node->index, ts_node_index);
337 ip6_hbh_pop_node = vlib_get_node_by_name (vm, (u8 *) "ip6-pop-hop-by-hop");
338 em->ip6_hbh_pop_node_index = ip6_hbh_pop_node->index;
340 error_node = vlib_get_node_by_name (vm, (u8 *) "error-drop");
341 em->error_node_index = error_node->index;
344 node = vlib_get_node_by_name (vm, (u8 *) "ip6-add-from-cache-hop-by-hop");
345 em->ip6_add_from_cache_hbh_node_index = node->index;
347 node = vlib_get_node_by_name (vm, (u8 *) "ip6-add-syn-hop-by-hop");
348 em->ip6_reset_ts_hbh_node_index = node->index;
353 VLIB_INIT_FUNCTION (ioam_cache_init);
356 * fd.io coding-style-patch-verification: ON
359 * eval: (c-set-style "gnu")