nat: Include platform specific headers on FreeBSD
[vpp.git] / src / plugins / oddbuf / oddbuf.c
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
32 #include <vlibapi/api_helper_macros.h>
33
34 oddbuf_main_t oddbuf_main;
35
36 /* Action function shared between message handler and debug CLI */
37
38 int
39 oddbuf_enable_disable (oddbuf_main_t * omp, u32 sw_if_index,
40                        int enable_disable)
41 {
42   vnet_sw_interface_t *sw;
43   int rv = 0;
44
45   /* Utterly wrong? */
46   if (pool_is_free_index (omp->vnet_main->interface_main.sw_interfaces,
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);
52   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
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 *
62 oddbuf_enable_disable_command_fn (vlib_main_t * vm,
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
72   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
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 VLIB_CLI_COMMAND (oddbuf_enable_disable_command, static) =
110 {
111   .path = "oddbuf enable-disable",
112   .short_help =
113   "oddbuf enable-disable <interface-name> [disable]",
114   .function = oddbuf_enable_disable_command_fn,
115 };
116
117 /* API message handler */
118 static void vl_api_oddbuf_enable_disable_t_handler
119   (vl_api_oddbuf_enable_disable_t * mp)
120 {
121   vl_api_oddbuf_enable_disable_reply_t *rmp;
122   oddbuf_main_t *omp = &oddbuf_main;
123   u32 sw_if_index;
124   int rv;
125
126   VALIDATE_SW_IF_INDEX (mp);
127
128   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
129   rv = oddbuf_enable_disable (omp, sw_if_index, (int) (mp->enable_disable));
130
131   BAD_SW_IF_INDEX_LABEL;
132   REPLY_MACRO (VL_API_ODDBUF_ENABLE_DISABLE_REPLY);
133 }
134
135 #include <oddbuf/oddbuf.api.c>
136 static clib_error_t *
137 oddbuf_init (vlib_main_t * vm)
138 {
139   oddbuf_main_t *om = &oddbuf_main;
140   clib_error_t *error = 0;
141
142   om->vlib_main = vm;
143   om->vnet_main = vnet_get_main ();
144
145   /* Ask for a correctly-sized block of API message decode slots */
146   om->msg_id_base = setup_message_id_table ();
147
148   /* Basic setup */
149   om->n_to_copy = 1;
150   om->second_chunk_offset = 1;
151   om->first_chunk_offset = 0;
152
153   return error;
154 }
155
156 VLIB_INIT_FUNCTION (oddbuf_init);
157
158 VNET_FEATURE_INIT (oddbuf, static) =
159 {
160   .arc_name = "device-input",
161   .node_name = "oddbuf",
162   .runs_before = VNET_FEATURES ("ethernet-input"),
163 };
164
165 VLIB_PLUGIN_REGISTER () =
166 {
167   .version = VPP_BUILD_VER,
168   .description = "Awkward chained buffer geometry generator",
169   .default_disabled = 1,
170 };
171
172
173 static clib_error_t *
174 oddbuf_config_command_fn (vlib_main_t * vm,
175                           unformat_input_t * input, vlib_cli_command_t * cmd)
176 {
177   oddbuf_main_t *omp = &oddbuf_main;
178   unformat_input_t _line_input, *line_input = &_line_input;
179
180   /* Get a line of input. */
181   if (!unformat_user (input, unformat_line_input, line_input))
182     return 0;
183
184   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
185     {
186       if (unformat (line_input, "n_to_copy %d", &omp->n_to_copy))
187         ;
188       else if (unformat (line_input, "offset %d", &omp->second_chunk_offset))
189         ;
190       else if (unformat (line_input, "first_offset %d",
191                          &omp->first_chunk_offset))
192         ;
193       else
194         break;
195     }
196
197   unformat_free (line_input);
198
199   return 0;
200 }
201
202 VLIB_CLI_COMMAND (oddbuf_config_command, static) =
203 {
204   .path = "oddbuf configure",
205   .short_help =
206   "oddbuf configure n_to_copy <nn> offset <nn> first_offset <nn>",
207   .function = oddbuf_config_command_fn,
208 };
209
210
211
212 /*
213  * fd.io coding-style-patch-verification: ON
214  *
215  * Local Variables:
216  * eval: (c-set-style "gnu")
217  * End:
218  */