e730659bfcd821bc71fdf9afed88bf4f73445a82
[vpp.git] / src / vnet / devices / 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 <vnet/devices/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, "hw-addr %U", unformat_ethernet_address,
93                          hwaddr))
94         arg->hw_addr = hwaddr;
95       else
96         {
97           error = clib_error_return (0, "unknown input `%U'",
98                                      format_unformat_error, line_input);
99           goto done;
100         }
101     }
102
103   if (arg->host_if_name == NULL)
104     {
105       error = clib_error_return (0, "missing host interface name");
106       goto done;
107     }
108
109   r = af_packet_create_if (arg);
110
111   if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
112     {
113       error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
114       goto done;
115     }
116
117   if (r == VNET_API_ERROR_INVALID_INTERFACE)
118     {
119       error = clib_error_return (0, "Invalid interface name");
120       goto done;
121     }
122
123   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
124     {
125       error = clib_error_return (0, "Interface already exists");
126       goto done;
127     }
128
129   vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
130                    arg->sw_if_index);
131
132 done:
133   vec_free (arg->host_if_name);
134   unformat_free (line_input);
135
136   return error;
137 }
138
139 /*?
140  * Create a host interface that will attach to a linux AF_PACKET
141  * interface, one side of a veth pair. The veth pair must already
142  * exist. Once created, a new host interface will exist in VPP
143  * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
144  * is the name of the specified veth pair. Use the
145  * '<em>show interface</em>' command to display host interface details.
146  *
147  * This command has the following optional parameters:
148  *
149  * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either
150  * X:X:X:X:X:X unix or X.X.X cisco format.
151  *
152  * @cliexpar
153  * Example of how to create a host interface tied to one side of an
154  * existing linux veth pair named vpp1:
155  * @cliexstart{create host-interface name vpp1}
156  * host-vpp1
157  * @cliexend
158  * Once the host interface is created, enable the interface using:
159  * @cliexcmd{set interface state host-vpp1 up}
160 ?*/
161 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
162   .path = "create host-interface",
163   .short_help = "create host-interface name <ifname> [num-rx-queues <n>] "
164                 "[num-tx-queues <n>] [hw-addr <mac-addr>] [mode ip] "
165                 "[qdisc-bypass-disable] [cksum-gso-disable]",
166   .function = af_packet_create_command_fn,
167 };
168
169 static clib_error_t *
170 af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
171                              vlib_cli_command_t * cmd)
172 {
173   unformat_input_t _line_input, *line_input = &_line_input;
174   u8 *host_if_name = NULL;
175   clib_error_t *error = NULL;
176
177   /* Get a line of input. */
178   if (!unformat_user (input, unformat_line_input, line_input))
179     return 0;
180
181   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
182     {
183       if (unformat (line_input, "name %s", &host_if_name))
184         ;
185       else
186         {
187           error = clib_error_return (0, "unknown input `%U'",
188                                      format_unformat_error, line_input);
189           goto done;
190         }
191     }
192
193   if (host_if_name == NULL)
194     {
195       error = clib_error_return (0, "missing host interface name");
196       goto done;
197     }
198
199   af_packet_delete_if (host_if_name);
200
201 done:
202   vec_free (host_if_name);
203   unformat_free (line_input);
204
205   return error;
206 }
207
208 /*?
209  * Delete a host interface. Use the linux interface name to identify
210  * the host interface to be deleted. In VPP, host interfaces are
211  * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
212  * is the name of the linux interface.
213  *
214  * @cliexpar
215  * Example of how to delete a host interface named host-vpp1:
216  * @cliexcmd{delete host-interface name vpp1}
217 ?*/
218 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
219   .path = "delete host-interface",
220   .short_help = "delete host-interface name <ifname>",
221   .function = af_packet_delete_command_fn,
222 };
223
224 static clib_error_t *
225 af_packet_set_l4_cksum_offload_command_fn (vlib_main_t * vm,
226                                            unformat_input_t * input,
227                                            vlib_cli_command_t * cmd)
228 {
229   unformat_input_t _line_input, *line_input = &_line_input;
230   u8 set = 0;
231   clib_error_t *error = NULL;
232   vnet_main_t *vnm = vnet_get_main ();
233   u32 sw_if_index;
234
235   if (!unformat_user (input, unformat_line_input, line_input))
236     return 0;
237
238   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
239     {
240       if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
241                     &sw_if_index))
242         ;
243       else if (unformat (line_input, "on"))
244         set = 1;
245       else if (unformat (line_input, "off"))
246         set = 0;
247       else
248         {
249           error = clib_error_return (0, "unknown input '%U'",
250                                      format_unformat_error, line_input);
251           goto done;
252         }
253     }
254
255   if (af_packet_set_l4_cksum_offload (sw_if_index, set) < 0)
256     error = clib_error_return (0, "not an af_packet interface");
257
258 done:
259   unformat_free (line_input);
260   return error;
261 }
262
263 /*?
264  * Set TCP/UDP offload checksum calculation. Use interface
265  * name to identify the interface to set TCP/UDP offload checksum
266  * calculation.
267  *
268  * @cliexpar
269  * Example of how to set TCP/UDP offload checksum calculation on host-vpp0:
270  * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 off}
271  * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 on}
272 ?*/
273 VLIB_CLI_COMMAND (af_packet_set_l4_cksum_offload_command, static) = {
274   .path = "set host-interface l4-cksum-offload",
275   .short_help = "set host-interface l4-cksum-offload <host-if-name> <on|off>",
276   .function = af_packet_set_l4_cksum_offload_command_fn,
277 };
278
279 clib_error_t *
280 af_packet_cli_init (vlib_main_t * vm)
281 {
282   return 0;
283 }
284
285 VLIB_INIT_FUNCTION (af_packet_cli_init);
286
287 /*
288  * fd.io coding-style-patch-verification: ON
289  *
290  * Local Variables:
291  * eval: (c-set-style "gnu")
292  * End:
293  */