ip: fix udp/tcp checksum corner cases
[vpp.git] / src / plugins / oddbuf / oddbuf.c
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
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
54 oddbuf_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
63 int
64 oddbuf_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
86 static clib_error_t *
87 oddbuf_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* */
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]",
140   .function = oddbuf_enable_disable_command_fn,
141 };
142 /* *INDENT-ON* */
143
144 /* API message handler */
145 static 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   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 *
160 oddbuf_plugin_api_hookup (vlib_main_t * vm)
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);
171   foreach_oddbuf_plugin_api_msg;
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
182 setup_message_id_table (oddbuf_main_t * omp, api_main_t * am)
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 *
190 oddbuf_init (vlib_main_t * vm)
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 */
202   om->msg_id_base = vl_msg_api_get_msg_ids
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 */
208   setup_message_id_table (om, &api_main);
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
220 VLIB_INIT_FUNCTION (oddbuf_init);
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* */
232 VLIB_PLUGIN_REGISTER () =
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 *
242 oddbuf_config_command_fn (vlib_main_t * vm,
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  */