FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
oddbuf.c
Go to the documentation of this file.
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.api_enum.h>
29 #include <oddbuf/oddbuf.api_types.h>
30 
31 #define REPLY_MSG_ID_BASE omp->msg_id_base
33 
35 
36 /* Action function shared between message handler and debug CLI */
37 
38 int
40  int enable_disable)
41 {
43  int rv = 0;
44 
45  /* Utterly wrong? */
47  sw_if_index))
48  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
49 
50  /* Not a physical port? */
51  sw = vnet_get_sw_interface (omp->vnet_main, sw_if_index);
53  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
54 
55  vnet_feature_enable_disable ("device-input", "oddbuf",
56  sw_if_index, enable_disable, 0, 0);
57 
58  return rv;
59 }
60 
61 static clib_error_t *
63  unformat_input_t * input,
64  vlib_cli_command_t * cmd)
65 {
66  oddbuf_main_t *omp = &oddbuf_main;
67  u32 sw_if_index = ~0;
68  int enable_disable = 1;
69 
70  int rv;
71 
73  {
74  if (unformat (input, "disable"))
75  enable_disable = 0;
76  else if (unformat (input, "%U", unformat_vnet_sw_interface,
77  omp->vnet_main, &sw_if_index))
78  ;
79  else
80  break;
81  }
82 
83  if (sw_if_index == ~0)
84  return clib_error_return (0, "Please specify an interface...");
85 
86  rv = oddbuf_enable_disable (omp, sw_if_index, enable_disable);
87 
88  switch (rv)
89  {
90  case 0:
91  break;
92 
93  case VNET_API_ERROR_INVALID_SW_IF_INDEX:
94  return clib_error_return
95  (0, "Invalid interface, only works on physical ports");
96  break;
97 
98  case VNET_API_ERROR_UNIMPLEMENTED:
99  return clib_error_return (0,
100  "Device driver doesn't support redirection");
101  break;
102 
103  default:
104  return clib_error_return (0, "oddbuf_enable_disable returned %d", rv);
105  }
106  return 0;
107 }
108 
109 /* *INDENT-OFF* */
110 VLIB_CLI_COMMAND (oddbuf_enable_disable_command, static) =
111 {
112  .path = "oddbuf enable-disable",
113  .short_help =
114  "oddbuf enable-disable <interface-name> [disable]",
116 };
117 /* *INDENT-ON* */
118 
119 /* API message handler */
122 {
123  vl_api_oddbuf_enable_disable_reply_t *rmp;
124  oddbuf_main_t *omp = &oddbuf_main;
126  int rv;
127 
129 
130  sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
131  rv = oddbuf_enable_disable (omp, sw_if_index, (int) (mp->enable_disable));
132 
134  REPLY_MACRO (VL_API_ODDBUF_ENABLE_DISABLE_REPLY);
135 }
136 
137 #include <oddbuf/oddbuf.api.c>
138 static clib_error_t *
140 {
141  oddbuf_main_t *om = &oddbuf_main;
142  clib_error_t *error = 0;
143 
144  om->vlib_main = vm;
145  om->vnet_main = vnet_get_main ();
146 
147  /* Ask for a correctly-sized block of API message decode slots */
149 
150  /* Basic setup */
151  om->n_to_copy = 1;
152  om->second_chunk_offset = 1;
153  om->first_chunk_offset = 0;
154 
155  return error;
156 }
157 
159 
160 /* *INDENT-OFF* */
161 VNET_FEATURE_INIT (oddbuf, static) =
162 {
163  .arc_name = "device-input",
164  .node_name = "oddbuf",
165  .runs_before = VNET_FEATURES ("ethernet-input"),
166 };
167 /* *INDENT-ON */
168 
169 /* *INDENT-OFF* */
171 {
172  .version = VPP_BUILD_VER,
173  .description = "Awkward chained buffer geometry generator",
174  .default_disabled = 1,
175 };
176 /* *INDENT-ON* */
177 
178 
179 static clib_error_t *
181  unformat_input_t * input, vlib_cli_command_t * cmd)
182 {
183  oddbuf_main_t *omp = &oddbuf_main;
184  unformat_input_t _line_input, *line_input = &_line_input;
185 
186  /* Get a line of input. */
187  if (!unformat_user (input, unformat_line_input, line_input))
188  return 0;
189 
190  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
191  {
192  if (unformat (line_input, "n_to_copy %d", &omp->n_to_copy))
193  ;
194  else if (unformat (line_input, "offset %d", &omp->second_chunk_offset))
195  ;
196  else if (unformat (line_input, "first_offset %d",
197  &omp->first_chunk_offset))
198  ;
199  else
200  break;
201  }
202 
203  unformat_free (line_input);
204 
205  return 0;
206 }
207 
208 /* *INDENT-OFF* */
209 VLIB_CLI_COMMAND (oddbuf_config_command, static) =
210 {
211  .path = "oddbuf configure",
212  .short_help =
213  "oddbuf configure n_to_copy <nn> offset <nn> first_offset <nn>",
214  .function = oddbuf_config_command_fn,
215 };
216 /* *INDENT-ON* */
217 
218 
219 
220 /*
221  * fd.io coding-style-patch-verification: ON
222  *
223  * Local Variables:
224  * eval: (c-set-style "gnu")
225  * End:
226  */
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:121
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:59
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:62
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:139
static vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
vlib_main_t * vm
Definition: in2out_ed.c:1582
unformat_function_t unformat_vnet_sw_interface
#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:180
#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
int oddbuf_enable_disable(oddbuf_main_t *omp, u32 sw_if_index, int enable_disable)
Definition: oddbuf.c:39
struct _unformat_input_t unformat_input_t
#define REPLY_MACRO(t)
int second_chunk_offset
Definition: oddbuf.h:39
VNET_FEATURE_INIT(oddbuf, static)
#define BAD_SW_IF_INDEX_LABEL
int first_chunk_offset
Definition: oddbuf.h:40
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:299
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
#define VNET_FEATURES(...)
Definition: feature.h:470
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:872
vnet_sw_interface_type_t type
Definition: interface.h:736
VLIB_PLUGIN_REGISTER()
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:2804
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:33
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:303
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
#define VALIDATE_SW_IF_INDEX(mp)
oddbuf_main_t oddbuf_main
Definition: oddbuf.c:34
int n_to_copy
Definition: oddbuf.h:38