2 * oddbuf.c - awkward chained buffer geometry test tool
4 * Copyright (c) 2019 by Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <oddbuf/oddbuf.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
27 /* define message IDs */
28 #include <oddbuf/oddbuf_msg_enum.h>
30 /* define message structures */
32 #include <oddbuf/oddbuf_all_api_h.h>
35 /* define generated endian-swappers */
37 #include <oddbuf/oddbuf_all_api_h.h>
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #include <oddbuf/oddbuf_all_api_h.h>
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <oddbuf/oddbuf_all_api_h.h>
51 #define REPLY_MSG_ID_BASE omp->msg_id_base
52 #include <vlibapi/api_helper_macros.h>
54 oddbuf_main_t oddbuf_main;
56 /* List of message types that this plugin understands */
58 #define foreach_oddbuf_plugin_api_msg \
59 _(ODDBUF_ENABLE_DISABLE, oddbuf_enable_disable)
61 /* Action function shared between message handler and debug CLI */
64 oddbuf_enable_disable (oddbuf_main_t * omp, u32 sw_if_index,
67 vnet_sw_interface_t *sw;
71 if (pool_is_free_index (omp->vnet_main->interface_main.sw_interfaces,
73 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
75 /* Not a physical port? */
76 sw = vnet_get_sw_interface (omp->vnet_main, sw_if_index);
77 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
78 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
80 vnet_feature_enable_disable ("device-input", "oddbuf",
81 sw_if_index, enable_disable, 0, 0);
87 oddbuf_enable_disable_command_fn (vlib_main_t * vm,
88 unformat_input_t * input,
89 vlib_cli_command_t * cmd)
91 oddbuf_main_t *omp = &oddbuf_main;
93 int enable_disable = 1;
97 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
99 if (unformat (input, "disable"))
101 else if (unformat (input, "%U", unformat_vnet_sw_interface,
102 omp->vnet_main, &sw_if_index))
108 if (sw_if_index == ~0)
109 return clib_error_return (0, "Please specify an interface...");
111 rv = oddbuf_enable_disable (omp, sw_if_index, enable_disable);
118 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
119 return clib_error_return
120 (0, "Invalid interface, only works on physical ports");
123 case VNET_API_ERROR_UNIMPLEMENTED:
124 return clib_error_return (0,
125 "Device driver doesn't support redirection");
129 return clib_error_return (0, "oddbuf_enable_disable returned %d", rv);
135 VLIB_CLI_COMMAND (oddbuf_enable_disable_command, static) =
137 .path = "oddbuf enable-disable",
139 "oddbuf enable-disable <interface-name> [disable]",
140 .function = oddbuf_enable_disable_command_fn,
144 /* API message handler */
145 static void vl_api_oddbuf_enable_disable_t_handler
146 (vl_api_oddbuf_enable_disable_t * mp)
148 vl_api_oddbuf_enable_disable_reply_t *rmp;
149 oddbuf_main_t *omp = &oddbuf_main;
153 VALIDATE_SW_IF_INDEX (mp);
155 sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
156 rv = oddbuf_enable_disable (omp, sw_if_index, (int) (mp->enable_disable));
158 BAD_SW_IF_INDEX_LABEL;
159 REPLY_MACRO (VL_API_ODDBUF_ENABLE_DISABLE_REPLY);
162 /* Set up the API message handling tables */
163 static clib_error_t *
164 oddbuf_plugin_api_hookup (vlib_main_t * vm)
166 oddbuf_main_t *omp = &oddbuf_main;
168 vl_msg_api_set_handlers((VL_API_##N + omp->msg_id_base), \
170 vl_api_##n##_t_handler, \
172 vl_api_##n##_t_endian, \
173 vl_api_##n##_t_print, \
174 sizeof(vl_api_##n##_t), 1);
175 foreach_oddbuf_plugin_api_msg;
181 #define vl_msg_name_crc_list
182 #include <oddbuf/oddbuf_all_api_h.h>
183 #undef vl_msg_name_crc_list
186 setup_message_id_table (oddbuf_main_t * omp, api_main_t * am)
188 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + omp->msg_id_base);
189 foreach_vl_msg_name_crc_oddbuf;
193 static clib_error_t *
194 oddbuf_init (vlib_main_t * vm)
196 oddbuf_main_t *om = &oddbuf_main;
197 clib_error_t *error = 0;
201 om->vnet_main = vnet_get_main ();
203 name = format (0, "oddbuf_%08x%c", api_version, 0);
205 /* Ask for a correctly-sized block of API message decode slots */
206 om->msg_id_base = vl_msg_api_get_msg_ids
207 ((char *) name, VL_MSG_FIRST_AVAILABLE);
209 error = oddbuf_plugin_api_hookup (vm);
211 /* Add our API messages to the global name_crc hash table */
212 setup_message_id_table (om, &api_main);
216 om->second_chunk_offset = 1;
217 om->first_chunk_offset = 0;
224 VLIB_INIT_FUNCTION (oddbuf_init);
227 VNET_FEATURE_INIT (oddbuf, static) =
229 .arc_name = "device-input",
230 .node_name = "oddbuf",
231 .runs_before = VNET_FEATURES ("ethernet-input"),
236 VLIB_PLUGIN_REGISTER () =
238 .version = VPP_BUILD_VER,
239 .description = "Awkward chained buffer geometry generator",
240 .default_disabled = 1,
245 static clib_error_t *
246 oddbuf_config_command_fn (vlib_main_t * vm,
247 unformat_input_t * input, vlib_cli_command_t * cmd)
249 oddbuf_main_t *omp = &oddbuf_main;
250 unformat_input_t _line_input, *line_input = &_line_input;
252 /* Get a line of input. */
253 if (!unformat_user (input, unformat_line_input, line_input))
256 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
258 if (unformat (line_input, "n_to_copy %d", &omp->n_to_copy))
260 else if (unformat (line_input, "offset %d", &omp->second_chunk_offset))
262 else if (unformat (line_input, "first_offset %d",
263 &omp->first_chunk_offset))
269 unformat_free (line_input);
275 VLIB_CLI_COMMAND (oddbuf_config_command, static) =
277 .path = "oddbuf configure",
279 "oddbuf configure n_to_copy <nn> offset <nn> first_offset <nn>",
280 .function = oddbuf_config_command_fn,
287 * fd.io coding-style-patch-verification: ON
290 * eval: (c-set-style "gnu")