2 * oddbuf.c - skeleton vpp engine plug-in
4 * Copyright (c) <current-year> <your-organization>
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;
152 rv = oddbuf_enable_disable (omp, ntohl (mp->sw_if_index),
153 (int) (mp->enable_disable));
155 REPLY_MACRO (VL_API_ODDBUF_ENABLE_DISABLE_REPLY);
158 /* Set up the API message handling tables */
159 static clib_error_t *
160 oddbuf_plugin_api_hookup (vlib_main_t * vm)
162 oddbuf_main_t *omp = &oddbuf_main;
164 vl_msg_api_set_handlers((VL_API_##N + omp->msg_id_base), \
166 vl_api_##n##_t_handler, \
168 vl_api_##n##_t_endian, \
169 vl_api_##n##_t_print, \
170 sizeof(vl_api_##n##_t), 1);
171 foreach_oddbuf_plugin_api_msg;
177 #define vl_msg_name_crc_list
178 #include <oddbuf/oddbuf_all_api_h.h>
179 #undef vl_msg_name_crc_list
182 setup_message_id_table (oddbuf_main_t * omp, api_main_t * am)
184 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + omp->msg_id_base);
185 foreach_vl_msg_name_crc_oddbuf;
189 static clib_error_t *
190 oddbuf_init (vlib_main_t * vm)
192 oddbuf_main_t *om = &oddbuf_main;
193 clib_error_t *error = 0;
197 om->vnet_main = vnet_get_main ();
199 name = format (0, "oddbuf_%08x%c", api_version, 0);
201 /* Ask for a correctly-sized block of API message decode slots */
202 om->msg_id_base = vl_msg_api_get_msg_ids
203 ((char *) name, VL_MSG_FIRST_AVAILABLE);
205 error = oddbuf_plugin_api_hookup (vm);
207 /* Add our API messages to the global name_crc hash table */
208 setup_message_id_table (om, &api_main);
212 om->second_chunk_offset = 1;
213 om->first_chunk_offset = 0;
220 VLIB_INIT_FUNCTION (oddbuf_init);
223 VNET_FEATURE_INIT (oddbuf, static) =
225 .arc_name = "device-input",
226 .node_name = "oddbuf",
227 .runs_before = VNET_FEATURES ("ethernet-input"),
232 VLIB_PLUGIN_REGISTER () =
234 .version = VPP_BUILD_VER,
235 .description = "Awkward chained buffer geometry generator",
236 .default_disabled = 1,
241 static clib_error_t *
242 oddbuf_config_command_fn (vlib_main_t * vm,
243 unformat_input_t * input, vlib_cli_command_t * cmd)
245 oddbuf_main_t *omp = &oddbuf_main;
246 unformat_input_t _line_input, *line_input = &_line_input;
248 /* Get a line of input. */
249 if (!unformat_user (input, unformat_line_input, line_input))
252 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
254 if (unformat (line_input, "n_to_copy %d", &omp->n_to_copy))
256 else if (unformat (line_input, "offset %d", &omp->second_chunk_offset))
258 else if (unformat (line_input, "first_offset %d",
259 &omp->first_chunk_offset))
265 unformat_free (line_input);
271 VLIB_CLI_COMMAND (oddbuf_config_command, static) =
273 .path = "oddbuf configure",
275 "oddbuf configure n_to_copy <nn> offset <nn> first_offset <nn>",
276 .function = oddbuf_config_command_fn,
283 * fd.io coding-style-patch-verification: ON
286 * eval: (c-set-style "gnu")