oddbuf.c revision a287a30d
1/*
2 * oddbuf.c - awkward chained buffer geometry test tool
3 *
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:
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vnet/vnet.h>
19#include <vnet/plugin/plugin.h>
20#include <oddbuf/oddbuf.h>
21
22#include <vlibapi/api.h>
23#include <vlibmemory/api.h>
24#include <vpp/app/version.h>
25#include <stdbool.h>
26
27/* define message IDs */
28#include <oddbuf/oddbuf_msg_enum.h>
29
30/* define message structures */
31#define vl_typedefs
32#include <oddbuf/oddbuf_all_api_h.h>
33#undef vl_typedefs
34
35/* define generated endian-swappers */
36#define vl_endianfun
37#include <oddbuf/oddbuf_all_api_h.h>
38#undef vl_endianfun
39
40/* instantiate all the print functions we know about */
41#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42#define vl_printfun
43#include <oddbuf/oddbuf_all_api_h.h>
44#undef vl_printfun
45
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>
49#undef vl_api_version
50
51#define REPLY_MSG_ID_BASE omp->msg_id_base
52#include <vlibapi/api_helper_macros.h>
53
54oddbuf_main_t oddbuf_main;
55
56/* List of message types that this plugin understands */
57
58#define foreach_oddbuf_plugin_api_msg                           \
59_(ODDBUF_ENABLE_DISABLE, oddbuf_enable_disable)
60
61/* Action function shared between message handler and debug CLI */
62
63int
64oddbuf_enable_disable (oddbuf_main_t * omp, u32 sw_if_index,
65		       int enable_disable)
66{
67  vnet_sw_interface_t *sw;
68  int rv = 0;
69
70  /* Utterly wrong? */
71  if (pool_is_free_index (omp->vnet_main->interface_main.sw_interfaces,
72			  sw_if_index))
73    return VNET_API_ERROR_INVALID_SW_IF_INDEX;
74
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;
79
80  vnet_feature_enable_disable ("device-input", "oddbuf",
81			       sw_if_index, enable_disable, 0, 0);
82
83  return rv;
84}
85
86static clib_error_t *
87oddbuf_enable_disable_command_fn (vlib_main_t * vm,
88				  unformat_input_t * input,
89				  vlib_cli_command_t * cmd)
90{
91  oddbuf_main_t *omp = &oddbuf_main;
92  u32 sw_if_index = ~0;
93  int enable_disable = 1;
94
95  int rv;
96
97  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
98    {
99      if (unformat (input, "disable"))
100	enable_disable = 0;
101      else if (unformat (input, "%U", unformat_vnet_sw_interface,
102			 omp->vnet_main, &sw_if_index))
103	;
104      else
105	break;
106    }
107
108  if (sw_if_index == ~0)
109    return clib_error_return (0, "Please specify an interface...");
110
111  rv = oddbuf_enable_disable (omp, sw_if_index, enable_disable);
112
113  switch (rv)
114    {
115    case 0:
116      break;
117
118    case VNET_API_ERROR_INVALID_SW_IF_INDEX:
119      return clib_error_return
120	(0, "Invalid interface, only works on physical ports");
121      break;
122
123    case VNET_API_ERROR_UNIMPLEMENTED:
124      return clib_error_return (0,
125				"Device driver doesn't support redirection");
126      break;
127
128    default:
129      return clib_error_return (0, "oddbuf_enable_disable returned %d", rv);
130    }
131  return 0;
132}
133
134/* *INDENT-OFF* */
135VLIB_CLI_COMMAND (oddbuf_enable_disable_command, static) =
136{
137  .path = "oddbuf enable-disable",
138  .short_help =
139  "oddbuf enable-disable <interface-name> [disable]",
140  .function = oddbuf_enable_disable_command_fn,
141};
142/* *INDENT-ON* */
143
144/* API message handler */
145static void vl_api_oddbuf_enable_disable_t_handler
146  (vl_api_oddbuf_enable_disable_t * mp)
147{
148  vl_api_oddbuf_enable_disable_reply_t *rmp;
149  oddbuf_main_t *omp = &oddbuf_main;
150  u32 sw_if_index;
151  int rv;
152
153  VALIDATE_SW_IF_INDEX (mp);
154
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));
157
158  BAD_SW_IF_INDEX_LABEL;
159  REPLY_MACRO (VL_API_ODDBUF_ENABLE_DISABLE_REPLY);
160}
161
162/* Set up the API message handling tables */
163static clib_error_t *
164oddbuf_plugin_api_hookup (vlib_main_t * vm)
165{
166  oddbuf_main_t *omp = &oddbuf_main;
167#define _(N,n)                                                  \
168    vl_msg_api_set_handlers((VL_API_##N + omp->msg_id_base),     \
169                           #n,					\
170                           vl_api_##n##_t_handler,              \
171                           vl_noop_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;
176#undef _
177
178  return 0;
179}
180
181#define vl_msg_name_crc_list
182#include <oddbuf/oddbuf_all_api_h.h>
183#undef vl_msg_name_crc_list
184
185static void
186setup_message_id_table (oddbuf_main_t * omp, api_main_t * am)
187{
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;
190#undef _
191}
192
193static clib_error_t *
194oddbuf_init (vlib_main_t * vm)
195{
196  oddbuf_main_t *om = &oddbuf_main;
197  clib_error_t *error = 0;
198  u8 *name;
199
200  om->vlib_main = vm;
201  om->vnet_main = vnet_get_main ();
202
203  name = format (0, "oddbuf_%08x%c", api_version, 0);
204
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);
208
209  error = oddbuf_plugin_api_hookup (vm);
210
211  /* Add our API messages to the global name_crc hash table */
212  setup_message_id_table (om, &api_main);
213
214  /* Basic setup */
215  om->n_to_copy = 1;
216  om->second_chunk_offset = 1;
217  om->first_chunk_offset = 0;
218
219  vec_free (name);
220
221  return error;
222}
223
224VLIB_INIT_FUNCTION (oddbuf_init);
225
226/* *INDENT-OFF* */
227VNET_FEATURE_INIT (oddbuf, static) =
228{
229  .arc_name = "device-input",
230  .node_name = "oddbuf",
231  .runs_before = VNET_FEATURES ("ethernet-input"),
232};
233/* *INDENT-ON */
234
235/* *INDENT-OFF* */
236VLIB_PLUGIN_REGISTER () =
237{
238  .version = VPP_BUILD_VER,
239  .description = "Awkward chained buffer geometry generator",
240  .default_disabled = 1,
241};
242/* *INDENT-ON* */
243
244
245static clib_error_t *
246oddbuf_config_command_fn (vlib_main_t * vm,
247			  unformat_input_t * input, vlib_cli_command_t * cmd)
248{
249  oddbuf_main_t *omp = &oddbuf_main;
250  unformat_input_t _line_input, *line_input = &_line_input;
251
252  /* Get a line of input. */
253  if (!unformat_user (input, unformat_line_input, line_input))
254    return 0;
255
256  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
257    {
258      if (unformat (line_input, "n_to_copy %d", &omp->n_to_copy))
259	;
260      else if (unformat (line_input, "offset %d", &omp->second_chunk_offset))
261	;
262      else if (unformat (line_input, "first_offset %d",
263			 &omp->first_chunk_offset))
264	;
265      else
266	break;
267    }
268
269  unformat_free (line_input);
270
271  return 0;
272}
273
274/* *INDENT-OFF* */
275VLIB_CLI_COMMAND (oddbuf_config_command, static) =
276{
277  .path = "oddbuf configure",
278  .short_help =
279  "oddbuf configure n_to_copy <nn> offset <nn> first_offset <nn>",
280  .function = oddbuf_config_command_fn,
281};
282/* *INDENT-ON* */
283
284
285
286/*
287 * fd.io coding-style-patch-verification: ON
288 *
289 * Local Variables:
290 * eval: (c-set-style "gnu")
291 * End:
292 */
293