blob: d20ef61a09d43bbe428bf1faeeebdc7d16cf4361 [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
Mohsin Kazmi2b6479c2022-04-05 12:03:47 +000061 // Default flags
Mohsin Kazmi788676b2022-04-05 12:43:13 +000062 arg->flags = AF_PACKET_IF_FLAGS_QDISC_BYPASS | AF_PACKET_IF_FLAGS_CKSUM_GSO;
Mohsin Kazmi2b6479c2022-04-05 12:03:47 +000063
Damjan Marion83243a02016-02-29 13:09:30 +010064 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +020065 if (!unformat_user (input, unformat_line_input, line_input))
Damjan Marion83243a02016-02-29 13:09:30 +010066 return 0;
67
68 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
69 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020070 if (unformat (line_input, "name %s", &arg->host_if_name))
Damjan Marion83243a02016-02-29 13:09:30 +010071 ;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020072 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 ;
Mohsin Kazmi5a7aa512022-03-25 14:27:45 +000082 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 ;
Mohsin Kazmi2b6479c2022-04-05 12:03:47 +000086 else if (unformat (line_input, "qdisc-bypass-disable"))
87 arg->flags &= ~AF_PACKET_IF_FLAGS_QDISC_BYPASS;
Mohsin Kazmi788676b2022-04-05 12:43:13 +000088 else if (unformat (line_input, "cksum-gso-disable"))
89 arg->flags &= ~AF_PACKET_IF_FLAGS_CKSUM_GSO;
Mohsin Kazmicae84fa2021-10-08 15:10:49 +000090 else if (unformat (line_input, "mode ip"))
91 arg->mode = AF_PACKET_IF_MODE_IP;
Mohsin Kazmi8b90d892022-09-08 17:21:20 +000092 else if (unformat (line_input, "v2"))
93 arg->is_v2 = 1;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020094 else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address,
95 hwaddr))
96 arg->hw_addr = hwaddr;
Damjan Marion83243a02016-02-29 13:09:30 +010097 else
Billy McFalla9a20e72017-02-15 11:39:12 -050098 {
99 error = clib_error_return (0, "unknown input `%U'",
100 format_unformat_error, line_input);
101 goto done;
102 }
Damjan Marion83243a02016-02-29 13:09:30 +0100103 }
Damjan Marion83243a02016-02-29 13:09:30 +0100104
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200105 if (arg->host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -0500106 {
107 error = clib_error_return (0, "missing host interface name");
108 goto done;
109 }
Damjan Marion83243a02016-02-29 13:09:30 +0100110
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200111 r = af_packet_create_if (arg);
Damjan Marion83243a02016-02-29 13:09:30 +0100112
113 if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
Billy McFalla9a20e72017-02-15 11:39:12 -0500114 {
115 error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
116 goto done;
117 }
Damjan Marion83243a02016-02-29 13:09:30 +0100118
119 if (r == VNET_API_ERROR_INVALID_INTERFACE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500120 {
121 error = clib_error_return (0, "Invalid interface name");
122 goto done;
123 }
Damjan Marion83243a02016-02-29 13:09:30 +0100124
125 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
Billy McFalla9a20e72017-02-15 11:39:12 -0500126 {
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700127 error = clib_error_return (0, "Interface already exists");
Billy McFalla9a20e72017-02-15 11:39:12 -0500128 goto done;
129 }
Damjan Marion83243a02016-02-29 13:09:30 +0100130
Damjan Marion00a9dca2016-08-17 17:05:46 +0200131 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200132 arg->sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -0500133
134done:
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200135 vec_free (arg->host_if_name);
Billy McFalla9a20e72017-02-15 11:39:12 -0500136 unformat_free (line_input);
137
138 return error;
Damjan Marion83243a02016-02-29 13:09:30 +0100139}
140
Billy McFalla1b99da2017-01-06 12:40:14 -0500141/*?
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
Dave Barach13ad1f02017-03-26 19:36:18 -0400147 * '<em>show interface</em>' command to display host interface details.
Billy McFalla1b99da2017-01-06 12:40:14 -0500148 *
Billy McFall2d0b6e32017-01-11 08:44:52 -0500149 * 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 *
Billy McFalla1b99da2017-01-06 12:40:14 -0500154 * @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?*/
Damjan Marion83243a02016-02-29 13:09:30 +0100163VLIB_CLI_COMMAND (af_packet_create_command, static) = {
164 .path = "create host-interface",
Mohsin Kazmi8b90d892022-09-08 17:21:20 +0000165 .short_help = "create host-interface [v2] name <ifname> [num-rx-queues <n>] "
Mohsin Kazmi2b6479c2022-04-05 12:03:47 +0000166 "[num-tx-queues <n>] [hw-addr <mac-addr>] [mode ip] "
Mohsin Kazmi788676b2022-04-05 12:43:13 +0000167 "[qdisc-bypass-disable] [cksum-gso-disable]",
Damjan Marion83243a02016-02-29 13:09:30 +0100168 .function = af_packet_create_command_fn,
169};
170
Peter Leidba76f22016-04-08 08:16:31 -0700171static clib_error_t *
172af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200173 vlib_cli_command_t * cmd)
Peter Leidba76f22016-04-08 08:16:31 -0700174{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200175 unformat_input_t _line_input, *line_input = &_line_input;
176 u8 *host_if_name = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -0500177 clib_error_t *error = NULL;
Peter Leidba76f22016-04-08 08:16:31 -0700178
179 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200180 if (!unformat_user (input, unformat_line_input, line_input))
Peter Leidba76f22016-04-08 08:16:31 -0700181 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))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200186 ;
Peter Leidba76f22016-04-08 08:16:31 -0700187 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500188 {
189 error = clib_error_return (0, "unknown input `%U'",
190 format_unformat_error, line_input);
191 goto done;
192 }
Peter Leidba76f22016-04-08 08:16:31 -0700193 }
Peter Leidba76f22016-04-08 08:16:31 -0700194
195 if (host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -0500196 {
197 error = clib_error_return (0, "missing host interface name");
198 goto done;
199 }
Peter Leidba76f22016-04-08 08:16:31 -0700200
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200201 af_packet_delete_if (host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700202
Billy McFalla9a20e72017-02-15 11:39:12 -0500203done:
204 vec_free (host_if_name);
205 unformat_free (line_input);
206
207 return error;
Peter Leidba76f22016-04-08 08:16:31 -0700208}
209
Billy McFalla1b99da2017-01-06 12:40:14 -0500210/*?
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?*/
Peter Leidba76f22016-04-08 08:16:31 -0700220VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
221 .path = "delete host-interface",
Billy McFalla1b99da2017-01-06 12:40:14 -0500222 .short_help = "delete host-interface name <ifname>",
Peter Leidba76f22016-04-08 08:16:31 -0700223 .function = af_packet_delete_command_fn,
224};
225
Jakub Grajciar92b02752017-10-20 13:37:28 +0200226static clib_error_t *
227af_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 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200242 if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
243 &sw_if_index))
Jakub Grajciar92b02752017-10-20 13:37:28 +0200244 ;
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
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200257 if (af_packet_set_l4_cksum_offload (sw_if_index, set) < 0)
Jakub Grajciar64ae7782017-11-14 13:45:04 +0100258 error = clib_error_return (0, "not an af_packet interface");
Jakub Grajciar92b02752017-10-20 13:37:28 +0200259
260done:
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?*/
Jakub Grajciar92b02752017-10-20 13:37:28 +0200275VLIB_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};
Jakub Grajciar92b02752017-10-20 13:37:28 +0200280
Damjan Marion83243a02016-02-29 13:09:30 +0100281clib_error_t *
282af_packet_cli_init (vlib_main_t * vm)
283{
284 return 0;
285}
286
287VLIB_INIT_FUNCTION (af_packet_cli_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200288
289/*
290 * fd.io coding-style-patch-verification: ON
291 *
292 * Local Variables:
293 * eval: (c-set-style "gnu")
294 * End:
295 */