ip: Replace Sematics for Interface IP addresses
[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 /* *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]",
115   .function = oddbuf_enable_disable_command_fn,
116 };
117 /* *INDENT-ON* */
118
119 /* API message handler */
120 static void vl_api_oddbuf_enable_disable_t_handler
121   (vl_api_oddbuf_enable_disable_t * mp)
122 {
123   vl_api_oddbuf_enable_disable_reply_t *rmp;
124   oddbuf_main_t *omp = &oddbuf_main;
125   u32 sw_if_index;
126   int rv;
127
128   VALIDATE_SW_IF_INDEX (mp);
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
133   BAD_SW_IF_INDEX_LABEL;
134   REPLY_MACRO (VL_API_ODDBUF_ENABLE_DISABLE_REPLY);
135 }
136
137 #include <oddbuf/oddbuf.api.c>
138 static clib_error_t *
139 oddbuf_init (vlib_main_t * vm)
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 */
148   om->msg_id_base = setup_message_id_table ();
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
158 VLIB_INIT_FUNCTION (oddbuf_init);
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* */
170 VLIB_PLUGIN_REGISTER () =
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 *
180 oddbuf_config_command_fn (vlib_main_t * vm,
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  */