blob: bae4f6181b0fc938d7747df3c3675648dc645512 [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
54 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +020055 if (!unformat_user (input, unformat_line_input, line_input))
Damjan Marion83243a02016-02-29 13:09:30 +010056 return 0;
57
58 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
59 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020060 if (unformat (line_input, "name %s", &arg->host_if_name))
Damjan Marion83243a02016-02-29 13:09:30 +010061 ;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020062 else if (unformat (line_input, "rx-size %u", &arg->rx_frame_size))
63 ;
64 else if (unformat (line_input, "tx-size %u", &arg->tx_frame_size))
65 ;
66 else if (unformat (line_input, "rx-per-block %u",
67 &arg->rx_frames_per_block))
68 ;
69 else if (unformat (line_input, "tx-per-block %u",
70 &arg->tx_frames_per_block))
71 ;
72 else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address,
73 hwaddr))
74 arg->hw_addr = hwaddr;
Damjan Marion83243a02016-02-29 13:09:30 +010075 else
Billy McFalla9a20e72017-02-15 11:39:12 -050076 {
77 error = clib_error_return (0, "unknown input `%U'",
78 format_unformat_error, line_input);
79 goto done;
80 }
Damjan Marion83243a02016-02-29 13:09:30 +010081 }
Damjan Marion83243a02016-02-29 13:09:30 +010082
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020083 if (arg->host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -050084 {
85 error = clib_error_return (0, "missing host interface name");
86 goto done;
87 }
Damjan Marion83243a02016-02-29 13:09:30 +010088
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020089 r = af_packet_create_if (arg);
Damjan Marion83243a02016-02-29 13:09:30 +010090
91 if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
Billy McFalla9a20e72017-02-15 11:39:12 -050092 {
93 error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
94 goto done;
95 }
Damjan Marion83243a02016-02-29 13:09:30 +010096
97 if (r == VNET_API_ERROR_INVALID_INTERFACE)
Billy McFalla9a20e72017-02-15 11:39:12 -050098 {
99 error = clib_error_return (0, "Invalid interface name");
100 goto done;
101 }
Damjan Marion83243a02016-02-29 13:09:30 +0100102
103 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
Billy McFalla9a20e72017-02-15 11:39:12 -0500104 {
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700105 error = clib_error_return (0, "Interface already exists");
Billy McFalla9a20e72017-02-15 11:39:12 -0500106 goto done;
107 }
Damjan Marion83243a02016-02-29 13:09:30 +0100108
Damjan Marion00a9dca2016-08-17 17:05:46 +0200109 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200110 arg->sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -0500111
112done:
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200113 vec_free (arg->host_if_name);
Billy McFalla9a20e72017-02-15 11:39:12 -0500114 unformat_free (line_input);
115
116 return error;
Damjan Marion83243a02016-02-29 13:09:30 +0100117}
118
Billy McFalla1b99da2017-01-06 12:40:14 -0500119/*?
120 * Create a host interface that will attach to a linux AF_PACKET
121 * interface, one side of a veth pair. The veth pair must already
122 * exist. Once created, a new host interface will exist in VPP
123 * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
124 * is the name of the specified veth pair. Use the
Dave Barach13ad1f02017-03-26 19:36:18 -0400125 * '<em>show interface</em>' command to display host interface details.
Billy McFalla1b99da2017-01-06 12:40:14 -0500126 *
Billy McFall2d0b6e32017-01-11 08:44:52 -0500127 * This command has the following optional parameters:
128 *
129 * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either
130 * X:X:X:X:X:X unix or X.X.X cisco format.
131 *
Billy McFalla1b99da2017-01-06 12:40:14 -0500132 * @cliexpar
133 * Example of how to create a host interface tied to one side of an
134 * existing linux veth pair named vpp1:
135 * @cliexstart{create host-interface name vpp1}
136 * host-vpp1
137 * @cliexend
138 * Once the host interface is created, enable the interface using:
139 * @cliexcmd{set interface state host-vpp1 up}
140?*/
Damjan Marion83243a02016-02-29 13:09:30 +0100141VLIB_CLI_COMMAND (af_packet_create_command, static) = {
142 .path = "create host-interface",
Billy McFalla1b99da2017-01-06 12:40:14 -0500143 .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]",
Damjan Marion83243a02016-02-29 13:09:30 +0100144 .function = af_packet_create_command_fn,
145};
146
Peter Leidba76f22016-04-08 08:16:31 -0700147static clib_error_t *
148af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200149 vlib_cli_command_t * cmd)
Peter Leidba76f22016-04-08 08:16:31 -0700150{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200151 unformat_input_t _line_input, *line_input = &_line_input;
152 u8 *host_if_name = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -0500153 clib_error_t *error = NULL;
Peter Leidba76f22016-04-08 08:16:31 -0700154
155 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200156 if (!unformat_user (input, unformat_line_input, line_input))
Peter Leidba76f22016-04-08 08:16:31 -0700157 return 0;
158
159 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
160 {
161 if (unformat (line_input, "name %s", &host_if_name))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200162 ;
Peter Leidba76f22016-04-08 08:16:31 -0700163 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500164 {
165 error = clib_error_return (0, "unknown input `%U'",
166 format_unformat_error, line_input);
167 goto done;
168 }
Peter Leidba76f22016-04-08 08:16:31 -0700169 }
Peter Leidba76f22016-04-08 08:16:31 -0700170
171 if (host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -0500172 {
173 error = clib_error_return (0, "missing host interface name");
174 goto done;
175 }
Peter Leidba76f22016-04-08 08:16:31 -0700176
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200177 af_packet_delete_if (host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700178
Billy McFalla9a20e72017-02-15 11:39:12 -0500179done:
180 vec_free (host_if_name);
181 unformat_free (line_input);
182
183 return error;
Peter Leidba76f22016-04-08 08:16:31 -0700184}
185
Billy McFalla1b99da2017-01-06 12:40:14 -0500186/*?
187 * Delete a host interface. Use the linux interface name to identify
188 * the host interface to be deleted. In VPP, host interfaces are
189 * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
190 * is the name of the linux interface.
191 *
192 * @cliexpar
193 * Example of how to delete a host interface named host-vpp1:
194 * @cliexcmd{delete host-interface name vpp1}
195?*/
Peter Leidba76f22016-04-08 08:16:31 -0700196VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
197 .path = "delete host-interface",
Billy McFalla1b99da2017-01-06 12:40:14 -0500198 .short_help = "delete host-interface name <ifname>",
Peter Leidba76f22016-04-08 08:16:31 -0700199 .function = af_packet_delete_command_fn,
200};
201
Jakub Grajciar92b02752017-10-20 13:37:28 +0200202static clib_error_t *
203af_packet_set_l4_cksum_offload_command_fn (vlib_main_t * vm,
204 unformat_input_t * input,
205 vlib_cli_command_t * cmd)
206{
207 unformat_input_t _line_input, *line_input = &_line_input;
208 u8 set = 0;
209 clib_error_t *error = NULL;
210 vnet_main_t *vnm = vnet_get_main ();
211 u32 sw_if_index;
212
213 if (!unformat_user (input, unformat_line_input, line_input))
214 return 0;
215
216 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
217 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200218 if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
219 &sw_if_index))
Jakub Grajciar92b02752017-10-20 13:37:28 +0200220 ;
221 else if (unformat (line_input, "on"))
222 set = 1;
223 else if (unformat (line_input, "off"))
224 set = 0;
225 else
226 {
227 error = clib_error_return (0, "unknown input '%U'",
228 format_unformat_error, line_input);
229 goto done;
230 }
231 }
232
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200233 if (af_packet_set_l4_cksum_offload (sw_if_index, set) < 0)
Jakub Grajciar64ae7782017-11-14 13:45:04 +0100234 error = clib_error_return (0, "not an af_packet interface");
Jakub Grajciar92b02752017-10-20 13:37:28 +0200235
236done:
237 unformat_free (line_input);
238 return error;
239}
240
241/*?
242 * Set TCP/UDP offload checksum calculation. Use interface
243 * name to identify the interface to set TCP/UDP offload checksum
244 * calculation.
245 *
246 * @cliexpar
247 * Example of how to set TCP/UDP offload checksum calculation on host-vpp0:
248 * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 off}
249 * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 on}
250?*/
Jakub Grajciar92b02752017-10-20 13:37:28 +0200251VLIB_CLI_COMMAND (af_packet_set_l4_cksum_offload_command, static) = {
252 .path = "set host-interface l4-cksum-offload",
253 .short_help = "set host-interface l4-cksum-offload <host-if-name> <on|off>",
254 .function = af_packet_set_l4_cksum_offload_command_fn,
255};
Jakub Grajciar92b02752017-10-20 13:37:28 +0200256
Damjan Marion83243a02016-02-29 13:09:30 +0100257clib_error_t *
258af_packet_cli_init (vlib_main_t * vm)
259{
260 return 0;
261}
262
263VLIB_INIT_FUNCTION (af_packet_cli_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200264
265/*
266 * fd.io coding-style-patch-verification: ON
267 *
268 * Local Variables:
269 * eval: (c-set-style "gnu")
270 * End:
271 */