devices: add cli support to enable disable qdisc bypass
[vpp.git] / src / plugins / af_packet / cli.c
1 /*
2  *------------------------------------------------------------------
3  * af_packet.c - linux kernel packet interface
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <fcntl.h>              /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/uio.h>            /* for iovec */
26 #include <netinet/in.h>
27
28 #include <vlib/vlib.h>
29 #include <vlib/unix/unix.h>
30 #include <vnet/ip/ip.h>
31 #include <vnet/ethernet/ethernet.h>
32
33 #include <af_packet/af_packet.h>
34
35 /**
36  * @file
37  * @brief CLI for Host Interface Device Driver.
38  *
39  * This file contains the source code for CLI for the host interface.
40  */
41
42 static clib_error_t *
43 af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
44                              vlib_cli_command_t * cmd)
45 {
46   unformat_input_t _line_input, *line_input = &_line_input;
47   af_packet_create_if_arg_t _arg, *arg = &_arg;
48   clib_error_t *error = NULL;
49   u8 hwaddr[6];
50   int r;
51
52   clib_memset (arg, 0, sizeof (*arg));
53
54   // Default mode
55   arg->mode = AF_PACKET_IF_MODE_ETHERNET;
56
57   // Default number of rx/tx queue(s)
58   arg->num_rxqs = 1;
59   arg->num_txqs = 1;
60
61   // Default flags
62   arg->flags = AF_PACKET_IF_FLAGS_QDISC_BYPASS | AF_PACKET_IF_FLAGS_CKSUM_GSO;
63
64   /* Get a line of input. */
65   if (!unformat_user (input, unformat_line_input, line_input))
66     return 0;
67
68   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
69     {
70       if (unformat (line_input, "name %s", &arg->host_if_name))
71         ;
72       else if (unformat (line_input, "rx-size %u", &arg->rx_frame_size))
73         ;
74       else if (unformat (line_input, "tx-size %u", &arg->tx_frame_size))
75         ;
76       else if (unformat (line_input, "rx-per-block %u",
77                          &arg->rx_frames_per_block))
78         ;
79       else if (unformat (line_input, "tx-per-block %u",
80                          &arg->tx_frames_per_block))
81         ;
82       else if (unformat (line_input, "num-rx-queues %u", &arg->num_rxqs))
83         ;
84       else if (unformat (line_input, "num-tx-queues %u", &arg->num_txqs))
85         ;
86       else if (unformat (line_input, "qdisc-bypass-disable"))
87         arg->flags &= ~AF_PACKET_IF_FLAGS_QDISC_BYPASS;
88       else if (unformat (line_input, "cksum-gso-disable"))
89         arg->flags &= ~AF_PACKET_IF_FLAGS_CKSUM_GSO;
90       else if (unformat (line_input, "mode ip"))
91         arg->mode = AF_PACKET_IF_MODE_IP;
92       else if (unformat (line_input, "v2"))
93         arg->is_v2 = 1;
94       else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address,
95                          hwaddr))
96         arg->hw_addr = hwaddr;
97       else
98         {
99           error = clib_error_return (0, "unknown input `%U'",
100                                      format_unformat_error, line_input);
101           goto done;
102         }
103     }
104
105   if (arg->host_if_name == NULL)
106     {
107       error = clib_error_return (0, "missing host interface name");
108       goto done;
109     }
110
111   r = af_packet_create_if (arg);
112
113   if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
114     {
115       error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
116       goto done;
117     }
118
119   if (r == VNET_API_ERROR_INVALID_INTERFACE)
120     {
121       error = clib_error_return (0, "Invalid interface name");
122       goto done;
123     }
124
125   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
126     {
127       error = clib_error_return (0, "Interface already exists");
128       goto done;
129     }
130
131   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
132                    arg->sw_if_index);
133
134 done:
135   vec_free (arg->host_if_name);
136   unformat_free (line_input);
137
138   return error;
139 }
140
141 /*?
142  * Create a host interface that will attach to a linux AF_PACKET
143  * interface, one side of a veth pair. The veth pair must already
144  * exist. Once created, a new host interface will exist in VPP
145  * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
146  * is the name of the specified veth pair. Use the
147  * '<em>show interface</em>' command to display host interface details.
148  *
149  * This command has the following optional parameters:
150  *
151  * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either
152  * X:X:X:X:X:X unix or X.X.X cisco format.
153  *
154  * @cliexpar
155  * Example of how to create a host interface tied to one side of an
156  * existing linux veth pair named vpp1:
157  * @cliexstart{create host-interface name vpp1}
158  * host-vpp1
159  * @cliexend
160  * Once the host interface is created, enable the interface using:
161  * @cliexcmd{set interface state host-vpp1 up}
162 ?*/
163 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
164   .path = "create host-interface",
165   .short_help = "create host-interface [v2] name <ifname> [num-rx-queues <n>] "
166                 "[num-tx-queues <n>] [hw-addr <mac-addr>] [mode ip] "
167                 "[qdisc-bypass-disable] [cksum-gso-disable]",
168   .function = af_packet_create_command_fn,
169 };
170
171 static clib_error_t *
172 af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
173                              vlib_cli_command_t * cmd)
174 {
175   unformat_input_t _line_input, *line_input = &_line_input;
176   u8 *host_if_name = NULL;
177   clib_error_t *error = NULL;
178
179   /* Get a line of input. */
180   if (!unformat_user (input, unformat_line_input, line_input))
181     return 0;
182
183   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
184     {
185       if (unformat (line_input, "name %s", &host_if_name))
186         ;
187       else
188         {
189           error = clib_error_return (0, "unknown input `%U'",
190                                      format_unformat_error, line_input);
191           goto done;
192         }
193     }
194
195   if (host_if_name == NULL)
196     {
197       error = clib_error_return (0, "missing host interface name");
198       goto done;
199     }
200
201   af_packet_delete_if (host_if_name);
202
203 done:
204   vec_free (host_if_name);
205   unformat_free (line_input);
206
207   return error;
208 }
209
210 /*?
211  * Delete a host interface. Use the linux interface name to identify
212  * the host interface to be deleted. In VPP, host interfaces are
213  * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
214  * is the name of the linux interface.
215  *
216  * @cliexpar
217  * Example of how to delete a host interface named host-vpp1:
218  * @cliexcmd{delete host-interface name vpp1}
219 ?*/
220 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
221   .path = "delete host-interface",
222   .short_help = "delete host-interface name <ifname>",
223   .function = af_packet_delete_command_fn,
224 };
225
226 static clib_error_t *
227 af_packet_set_l4_cksum_offload_command_fn (vlib_main_t * vm,
228                                            unformat_input_t * input,
229                                            vlib_cli_command_t * cmd)
230 {
231   unformat_input_t _line_input, *line_input = &_line_input;
232   u8 set = 0;
233   clib_error_t *error = NULL;
234   vnet_main_t *vnm = vnet_get_main ();
235   u32 sw_if_index;
236
237   if (!unformat_user (input, unformat_line_input, line_input))
238     return 0;
239
240   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
241     {
242       if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
243                     &sw_if_index))
244         ;
245       else if (unformat (line_input, "on"))
246         set = 1;
247       else if (unformat (line_input, "off"))
248         set = 0;
249       else
250         {
251           error = clib_error_return (0, "unknown input '%U'",
252                                      format_unformat_error, line_input);
253           goto done;
254         }
255     }
256
257   if (af_packet_set_l4_cksum_offload (sw_if_index, set) < 0)
258     error = clib_error_return (0, "not an af_packet interface");
259
260 done:
261   unformat_free (line_input);
262   return error;
263 }
264
265 /*?
266  * Set TCP/UDP offload checksum calculation. Use interface
267  * name to identify the interface to set TCP/UDP offload checksum
268  * calculation.
269  *
270  * @cliexpar
271  * Example of how to set TCP/UDP offload checksum calculation on host-vpp0:
272  * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 off}
273  * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 on}
274 ?*/
275 VLIB_CLI_COMMAND (af_packet_set_l4_cksum_offload_command, static) = {
276   .path = "set host-interface l4-cksum-offload",
277   .short_help = "set host-interface l4-cksum-offload <host-if-name> <on|off>",
278   .function = af_packet_set_l4_cksum_offload_command_fn,
279 };
280
281 static clib_error_t *
282 af_packet_enable_disable_qdisc_bypass_command_fn (vlib_main_t *vm,
283                                                   unformat_input_t *input,
284                                                   vlib_cli_command_t *cmd)
285 {
286   unformat_input_t _line_input, *line_input = &_line_input;
287   u8 enable_disable = 0;
288   clib_error_t *error = NULL;
289   vnet_main_t *vnm = vnet_get_main ();
290   u32 sw_if_index;
291
292   if (!unformat_user (input, unformat_line_input, line_input))
293     return 0;
294
295   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
296     {
297       if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
298                     &sw_if_index))
299         ;
300       else if (unformat (line_input, "enable"))
301         enable_disable = 1;
302       else if (unformat (line_input, "disable"))
303         enable_disable = 0;
304       else
305         {
306           error = clib_error_return (0, "unknown input '%U'",
307                                      format_unformat_error, line_input);
308           goto done;
309         }
310     }
311
312   if (af_packet_enable_disable_qdisc_bypass (sw_if_index, enable_disable) < 0)
313     error = clib_error_return (0, "not an af_packet interface");
314
315 done:
316   unformat_free (line_input);
317   return error;
318 }
319
320 VLIB_CLI_COMMAND (af_packet_enable_disable_qdisc_bypass_command, static) = {
321   .path = "set host-interface qdisc-bypass",
322   .short_help =
323     "set host-interface qdisc-bypass <host-if-name> <enable|disable>",
324   .function = af_packet_enable_disable_qdisc_bypass_command_fn,
325 };
326
327 clib_error_t *
328 af_packet_cli_init (vlib_main_t * vm)
329 {
330   return 0;
331 }
332
333 VLIB_INIT_FUNCTION (af_packet_cli_init);
334
335 /*
336  * fd.io coding-style-patch-verification: ON
337  *
338  * Local Variables:
339  * eval: (c-set-style "gnu")
340  * End:
341  */