FD.io VPP  v19.08-27-gf4dcae4
Vector Packet Processing
oddbuf.c
Go to the documentation of this file.
1 /*
2  * oddbuf.c - skeleton vpp engine plug-in
3  *
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:
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
33 #undef vl_typedefs
34 
35 /* define generated endian-swappers */
36 #define vl_endianfun
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
44 #undef vl_printfun
45 
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
49 #undef vl_api_version
50 
51 #define REPLY_MSG_ID_BASE omp->msg_id_base
53 
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 
63 int
65  int enable_disable)
66 {
68  int rv = 0;
69 
70  /* Utterly wrong? */
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);
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 
86 static clib_error_t *
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 
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* */
135 VLIB_CLI_COMMAND (oddbuf_enable_disable_command, static) =
136 {
137  .path = "oddbuf enable-disable",
138  .short_help =
139  "oddbuf enable-disable <interface-name> [disable]",
141 };
142 /* *INDENT-ON* */
143 
144 /* API message handler */
147 {
148  vl_api_oddbuf_enable_disable_reply_t *rmp;
149  oddbuf_main_t *omp = &oddbuf_main;
150  int rv;
151 
152  rv = oddbuf_enable_disable (omp, ntohl (mp->sw_if_index),
153  (int) (mp->enable_disable));
154 
155  REPLY_MACRO (VL_API_ODDBUF_ENABLE_DISABLE_REPLY);
156 }
157 
158 /* Set up the API message handling tables */
159 static clib_error_t *
161 {
162  oddbuf_main_t *omp = &oddbuf_main;
163 #define _(N,n) \
164  vl_msg_api_set_handlers((VL_API_##N + omp->msg_id_base), \
165  #n, \
166  vl_api_##n##_t_handler, \
167  vl_noop_handler, \
168  vl_api_##n##_t_endian, \
169  vl_api_##n##_t_print, \
170  sizeof(vl_api_##n##_t), 1);
172 #undef _
173 
174  return 0;
175 }
176 
177 #define vl_msg_name_crc_list
178 #include <oddbuf/oddbuf_all_api_h.h>
179 #undef vl_msg_name_crc_list
180 
181 static void
183 {
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;
186 #undef _
187 }
188 
189 static clib_error_t *
191 {
192  oddbuf_main_t *om = &oddbuf_main;
193  clib_error_t *error = 0;
194  u8 *name;
195 
196  om->vlib_main = vm;
197  om->vnet_main = vnet_get_main ();
198 
199  name = format (0, "oddbuf_%08x%c", api_version, 0);
200 
201  /* Ask for a correctly-sized block of API message decode slots */
203  ((char *) name, VL_MSG_FIRST_AVAILABLE);
204 
205  error = oddbuf_plugin_api_hookup (vm);
206 
207  /* Add our API messages to the global name_crc hash table */
209 
210  /* Basic setup */
211  om->n_to_copy = 1;
212  om->second_chunk_offset = 1;
213  om->first_chunk_offset = 0;
214 
215  vec_free (name);
216 
217  return error;
218 }
219 
221 
222 /* *INDENT-OFF* */
223 VNET_FEATURE_INIT (oddbuf, static) =
224 {
225  .arc_name = "device-input",
226  .node_name = "oddbuf",
227  .runs_before = VNET_FEATURES ("ethernet-input"),
228 };
229 /* *INDENT-ON */
230 
231 /* *INDENT-OFF* */
233 {
234  .version = VPP_BUILD_VER,
235  .description = "Awkward chained buffer geometry generator",
236  .default_disabled = 1,
237 };
238 /* *INDENT-ON* */
239 
240 
241 static clib_error_t *
243  unformat_input_t * input, vlib_cli_command_t * cmd)
244 {
245  oddbuf_main_t *omp = &oddbuf_main;
246  unformat_input_t _line_input, *line_input = &_line_input;
247 
248  /* Get a line of input. */
249  if (!unformat_user (input, unformat_line_input, line_input))
250  return 0;
251 
252  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
253  {
254  if (unformat (line_input, "n_to_copy %d", &omp->n_to_copy))
255  ;
256  else if (unformat (line_input, "offset %d", &omp->second_chunk_offset))
257  ;
258  else if (unformat (line_input, "first_offset %d",
259  &omp->first_chunk_offset))
260  ;
261  else
262  break;
263  }
264 
265  unformat_free (line_input);
266 
267  return 0;
268 }
269 
270 /* *INDENT-OFF* */
271 VLIB_CLI_COMMAND (oddbuf_config_command, static) =
272 {
273  .path = "oddbuf configure",
274  .short_help =
275  "oddbuf configure n_to_copy <nn> offset <nn> first_offset <nn>",
276  .function = oddbuf_config_command_fn,
277 };
278 /* *INDENT-ON* */
279 
280 
281 
282 /*
283  * fd.io coding-style-patch-verification: ON
284  *
285  * Local Variables:
286  * eval: (c-set-style "gnu")
287  * End:
288  */
u16 msg_id_base
Definition: oddbuf.h:32
vlib_main_t * vlib_main
Definition: oddbuf.h:43
static void vl_api_oddbuf_enable_disable_t_handler(vl_api_oddbuf_enable_disable_t *mp)
Definition: oddbuf.c:146
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:56
static clib_error_t * oddbuf_enable_disable_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: oddbuf.c:87
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
static clib_error_t * oddbuf_init(vlib_main_t *vm)
Definition: oddbuf.c:190
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
static clib_error_t * oddbuf_plugin_api_hookup(vlib_main_t *vm)
Definition: oddbuf.c:160
unsigned char u8
Definition: types.h:56
vl_api_interface_index_t sw_if_index
Definition: gre.api:50
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
vnet_main_t * vnet_main
Definition: oddbuf.h:44
API to enable / disable oddbuf on an interface.
Definition: oddbuf.api:39
static clib_error_t * oddbuf_config_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: oddbuf.c:242
#define clib_error_return(e, args...)
Definition: error.h:99
unsigned int u32
Definition: types.h:88
unformat_function_t unformat_line_input
Definition: format.h:283
#define foreach_oddbuf_plugin_api_msg
Definition: oddbuf.c:58
int oddbuf_enable_disable(oddbuf_main_t *omp, u32 sw_if_index, int enable_disable)
Definition: oddbuf.c:64
struct _unformat_input_t unformat_input_t
#define REPLY_MACRO(t)
int second_chunk_offset
Definition: oddbuf.h:39
VNET_FEATURE_INIT(oddbuf, static)
u8 name[64]
Definition: memclnt.api:152
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:203
int first_chunk_offset
Definition: oddbuf.h:40
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
vlib_main_t * vm
Definition: buffer.c:312
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
#define VNET_FEATURES(...)
Definition: feature.h:435
vl_api_interface_index_t sw_if_index
Definition: oddbuf.api:50
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
vnet_sw_interface_t * sw_interfaces
Definition: interface.h:833
vnet_sw_interface_type_t type
Definition: interface.h:697
VLIB_PLUGIN_REGISTER()
static void setup_message_id_table(oddbuf_main_t *omp, api_main_t *am)
Definition: oddbuf.c:182
api_main_t api_main
Definition: api_shared.c:35
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: feature.c:274
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
oddbuf_main_t oddbuf_main
Definition: oddbuf.c:54
int n_to_copy
Definition: oddbuf.h:38
u16 vl_msg_api_get_msg_ids(const char *name, int n)
Definition: api_shared.c:957