blob: c90d5251f9f06e795b62c8416122e34d0ef19854 [file] [log] [blame]
Damjan Marion83243a02016-02-29 13:09:30 +01001/*
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
Billy McFalla1b99da2017-01-06 12:40:14 -050035/**
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
Damjan Marion83243a02016-02-29 13:09:30 +010042static clib_error_t *
43af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
44 vlib_cli_command_t * cmd)
45{
Damjan Marion00a9dca2016-08-17 17:05:46 +020046 unformat_input_t _line_input, *line_input = &_line_input;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020047 af_packet_create_if_arg_t _arg, *arg = &_arg;
Billy McFalla9a20e72017-02-15 11:39:12 -050048 clib_error_t *error = NULL;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020049 u8 hwaddr[6];
50 int r;
51
52 clib_memset (arg, 0, sizeof (*arg));
Damjan Marion83243a02016-02-29 13:09:30 +010053
Mohsin Kazmicae84fa2021-10-08 15:10:49 +000054 // Default mode
55 arg->mode = AF_PACKET_IF_MODE_ETHERNET;
56
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +000057 // Default number of rx/tx queue(s)
58 arg->num_rxqs = 1;
59 arg->num_txqs = 1;
60
Damjan Marion83243a02016-02-29 13:09:30 +010061 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +020062 if (!unformat_user (input, unformat_line_input, line_input))
Damjan Marion83243a02016-02-29 13:09:30 +010063 return 0;
64
65 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
66 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020067 if (unformat (line_input, "name %s", &arg->host_if_name))
Damjan Marion83243a02016-02-29 13:09:30 +010068 ;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020069 else if (unformat (line_input, "rx-size %u", &arg->rx_frame_size))
70 ;
71 else if (unformat (line_input, "tx-size %u", &arg->tx_frame_size))
72 ;
73 else if (unformat (line_input, "rx-per-block %u",
74 &arg->rx_frames_per_block))
75 ;
76 else if (unformat (line_input, "tx-per-block %u",
77 &arg->tx_frames_per_block))
78 ;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +000079 else if (unformat (line_input, "num-rx-queues %u", &arg->num_rxqs))
80 ;
81 else if (unformat (line_input, "num-tx-queues %u", &arg->num_txqs))
82 ;
Mohsin Kazmicae84fa2021-10-08 15:10:49 +000083 else if (unformat (line_input, "mode ip"))
84 arg->mode = AF_PACKET_IF_MODE_IP;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020085 else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address,
86 hwaddr))
87 arg->hw_addr = hwaddr;
Damjan Marion83243a02016-02-29 13:09:30 +010088 else
Billy McFalla9a20e72017-02-15 11:39:12 -050089 {
90 error = clib_error_return (0, "unknown input `%U'",
91 format_unformat_error, line_input);
92 goto done;
93 }
Damjan Marion83243a02016-02-29 13:09:30 +010094 }
Damjan Marion83243a02016-02-29 13:09:30 +010095
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020096 if (arg->host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -050097 {
98 error = clib_error_return (0, "missing host interface name");
99 goto done;
100 }
Damjan Marion83243a02016-02-29 13:09:30 +0100101
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200102 r = af_packet_create_if (arg);
Damjan Marion83243a02016-02-29 13:09:30 +0100103
104 if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500105 {
106 error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
107 goto done;
108 }
Damjan Marion83243a02016-02-29 13:09:30 +0100109
110 if (r == VNET_API_ERROR_INVALID_INTERFACE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500111 {
112 error = clib_error_return (0, "Invalid interface name");
113 goto done;
114 }
Damjan Marion83243a02016-02-29 13:09:30 +0100115
116 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
Billy McFalla9a20e72017-02-15 11:39:12 -0500117 {
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700118 error = clib_error_return (0, "Interface already exists");
Billy McFalla9a20e72017-02-15 11:39:12 -0500119 goto done;
120 }
Damjan Marion83243a02016-02-29 13:09:30 +0100121
Damjan Marion00a9dca2016-08-17 17:05:46 +0200122 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200123 arg->sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -0500124
125done:
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200126 vec_free (arg->host_if_name);
Billy McFalla9a20e72017-02-15 11:39:12 -0500127 unformat_free (line_input);
128
129 return error;
Damjan Marion83243a02016-02-29 13:09:30 +0100130}
131
Billy McFalla1b99da2017-01-06 12:40:14 -0500132/*?
133 * Create a host interface that will attach to a linux AF_PACKET
134 * interface, one side of a veth pair. The veth pair must already
135 * exist. Once created, a new host interface will exist in VPP
136 * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
137 * is the name of the specified veth pair. Use the
Dave Barach13ad1f02017-03-26 19:36:18 -0400138 * '<em>show interface</em>' command to display host interface details.
Billy McFalla1b99da2017-01-06 12:40:14 -0500139 *
Billy McFall2d0b6e32017-01-11 08:44:52 -0500140 * This command has the following optional parameters:
141 *
142 * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either
143 * X:X:X:X:X:X unix or X.X.X cisco format.
144 *
Billy McFalla1b99da2017-01-06 12:40:14 -0500145 * @cliexpar
146 * Example of how to create a host interface tied to one side of an
147 * existing linux veth pair named vpp1:
148 * @cliexstart{create host-interface name vpp1}
149 * host-vpp1
150 * @cliexend
151 * Once the host interface is created, enable the interface using:
152 * @cliexcmd{set interface state host-vpp1 up}
153?*/
Damjan Marion83243a02016-02-29 13:09:30 +0100154VLIB_CLI_COMMAND (af_packet_create_command, static) = {
155 .path = "create host-interface",
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +0000156 .short_help = "create host-interface name <ifname> [num-rx-queues <n>] "
157 "[num-tx-queues <n>] [hw-addr <mac-addr>] [mode ip]",
Damjan Marion83243a02016-02-29 13:09:30 +0100158 .function = af_packet_create_command_fn,
159};
160
Peter Leidba76f22016-04-08 08:16:31 -0700161static clib_error_t *
162af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200163 vlib_cli_command_t * cmd)
Peter Leidba76f22016-04-08 08:16:31 -0700164{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200165 unformat_input_t _line_input, *line_input = &_line_input;
166 u8 *host_if_name = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -0500167 clib_error_t *error = NULL;
Peter Leidba76f22016-04-08 08:16:31 -0700168
169 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200170 if (!unformat_user (input, unformat_line_input, line_input))
Peter Leidba76f22016-04-08 08:16:31 -0700171 return 0;
172
173 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
174 {
175 if (unformat (line_input, "name %s", &host_if_name))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200176 ;
Peter Leidba76f22016-04-08 08:16:31 -0700177 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500178 {
179 error = clib_error_return (0, "unknown input `%U'",
180 format_unformat_error, line_input);
181 goto done;
182 }
Peter Leidba76f22016-04-08 08:16:31 -0700183 }
Peter Leidba76f22016-04-08 08:16:31 -0700184
185 if (host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -0500186 {
187 error = clib_error_return (0, "missing host interface name");
188 goto done;
189 }
Peter Leidba76f22016-04-08 08:16:31 -0700190
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200191 af_packet_delete_if (host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700192
Billy McFalla9a20e72017-02-15 11:39:12 -0500193done:
194 vec_free (host_if_name);
195 unformat_free (line_input);
196
197 return error;
Peter Leidba76f22016-04-08 08:16:31 -0700198}
199
Billy McFalla1b99da2017-01-06 12:40:14 -0500200/*?
201 * Delete a host interface. Use the linux interface name to identify
202 * the host interface to be deleted. In VPP, host interfaces are
203 * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
204 * is the name of the linux interface.
205 *
206 * @cliexpar
207 * Example of how to delete a host interface named host-vpp1:
208 * @cliexcmd{delete host-interface name vpp1}
209?*/
Peter Leidba76f22016-04-08 08:16:31 -0700210VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
211 .path = "delete host-interface",
Billy McFalla1b99da2017-01-06 12:40:14 -0500212 .short_help = "delete host-interface name <ifname>",
Peter Leidba76f22016-04-08 08:16:31 -0700213 .function = af_packet_delete_command_fn,
214};
215
Jakub Grajciar92b02752017-10-20 13:37:28 +0200216static clib_error_t *
217af_packet_set_l4_cksum_offload_command_fn (vlib_main_t * vm,
218 unformat_input_t * input,
219 vlib_cli_command_t * cmd)
220{
221 unformat_input_t _line_input, *line_input = &_line_input;
222 u8 set = 0;
223 clib_error_t *error = NULL;
224 vnet_main_t *vnm = vnet_get_main ();
225 u32 sw_if_index;
226
227 if (!unformat_user (input, unformat_line_input, line_input))
228 return 0;
229
230 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
231 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200232 if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
233 &sw_if_index))
Jakub Grajciar92b02752017-10-20 13:37:28 +0200234 ;
235 else if (unformat (line_input, "on"))
236 set = 1;
237 else if (unformat (line_input, "off"))
238 set = 0;
239 else
240 {
241 error = clib_error_return (0, "unknown input '%U'",
242 format_unformat_error, line_input);
243 goto done;
244 }
245 }
246
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200247 if (af_packet_set_l4_cksum_offload (sw_if_index, set) < 0)
Jakub Grajciar64ae7782017-11-14 13:45:04 +0100248 error = clib_error_return (0, "not an af_packet interface");
Jakub Grajciar92b02752017-10-20 13:37:28 +0200249
250done:
251 unformat_free (line_input);
252 return error;
253}
254
255/*?
256 * Set TCP/UDP offload checksum calculation. Use interface
257 * name to identify the interface to set TCP/UDP offload checksum
258 * calculation.
259 *
260 * @cliexpar
261 * Example of how to set TCP/UDP offload checksum calculation on host-vpp0:
262 * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 off}
263 * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 on}
264?*/
Jakub Grajciar92b02752017-10-20 13:37:28 +0200265VLIB_CLI_COMMAND (af_packet_set_l4_cksum_offload_command, static) = {
266 .path = "set host-interface l4-cksum-offload",
267 .short_help = "set host-interface l4-cksum-offload <host-if-name> <on|off>",
268 .function = af_packet_set_l4_cksum_offload_command_fn,
269};
Jakub Grajciar92b02752017-10-20 13:37:28 +0200270
Damjan Marion83243a02016-02-29 13:09:30 +0100271clib_error_t *
272af_packet_cli_init (vlib_main_t * vm)
273{
274 return 0;
275}
276
277VLIB_INIT_FUNCTION (af_packet_cli_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200278
279/*
280 * fd.io coding-style-patch-verification: ON
281 *
282 * Local Variables:
283 * eval: (c-set-style "gnu")
284 * End:
285 */