blob: 3dd3c8ee848d7312370340fb4139811aec9d5ce4 [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
Damjan Marion83243a02016-02-29 13:09:30 +010057 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +020058 if (!unformat_user (input, unformat_line_input, line_input))
Damjan Marion83243a02016-02-29 13:09:30 +010059 return 0;
60
61 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
62 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020063 if (unformat (line_input, "name %s", &arg->host_if_name))
Damjan Marion83243a02016-02-29 13:09:30 +010064 ;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020065 else if (unformat (line_input, "rx-size %u", &arg->rx_frame_size))
66 ;
67 else if (unformat (line_input, "tx-size %u", &arg->tx_frame_size))
68 ;
69 else if (unformat (line_input, "rx-per-block %u",
70 &arg->rx_frames_per_block))
71 ;
72 else if (unformat (line_input, "tx-per-block %u",
73 &arg->tx_frames_per_block))
74 ;
Mohsin Kazmicae84fa2021-10-08 15:10:49 +000075 else if (unformat (line_input, "mode ip"))
76 arg->mode = AF_PACKET_IF_MODE_IP;
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020077 else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address,
78 hwaddr))
79 arg->hw_addr = hwaddr;
Damjan Marion83243a02016-02-29 13:09:30 +010080 else
Billy McFalla9a20e72017-02-15 11:39:12 -050081 {
82 error = clib_error_return (0, "unknown input `%U'",
83 format_unformat_error, line_input);
84 goto done;
85 }
Damjan Marion83243a02016-02-29 13:09:30 +010086 }
Damjan Marion83243a02016-02-29 13:09:30 +010087
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020088 if (arg->host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -050089 {
90 error = clib_error_return (0, "missing host interface name");
91 goto done;
92 }
Damjan Marion83243a02016-02-29 13:09:30 +010093
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +020094 r = af_packet_create_if (arg);
Damjan Marion83243a02016-02-29 13:09:30 +010095
96 if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
Billy McFalla9a20e72017-02-15 11:39:12 -050097 {
98 error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
99 goto done;
100 }
Damjan Marion83243a02016-02-29 13:09:30 +0100101
102 if (r == VNET_API_ERROR_INVALID_INTERFACE)
Billy McFalla9a20e72017-02-15 11:39:12 -0500103 {
104 error = clib_error_return (0, "Invalid interface name");
105 goto done;
106 }
Damjan Marion83243a02016-02-29 13:09:30 +0100107
108 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
Billy McFalla9a20e72017-02-15 11:39:12 -0500109 {
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700110 error = clib_error_return (0, "Interface already exists");
Billy McFalla9a20e72017-02-15 11:39:12 -0500111 goto done;
112 }
Damjan Marion83243a02016-02-29 13:09:30 +0100113
Damjan Marion00a9dca2016-08-17 17:05:46 +0200114 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200115 arg->sw_if_index);
Billy McFalla9a20e72017-02-15 11:39:12 -0500116
117done:
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200118 vec_free (arg->host_if_name);
Billy McFalla9a20e72017-02-15 11:39:12 -0500119 unformat_free (line_input);
120
121 return error;
Damjan Marion83243a02016-02-29 13:09:30 +0100122}
123
Billy McFalla1b99da2017-01-06 12:40:14 -0500124/*?
125 * Create a host interface that will attach to a linux AF_PACKET
126 * interface, one side of a veth pair. The veth pair must already
127 * exist. Once created, a new host interface will exist in VPP
128 * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
129 * is the name of the specified veth pair. Use the
Dave Barach13ad1f02017-03-26 19:36:18 -0400130 * '<em>show interface</em>' command to display host interface details.
Billy McFalla1b99da2017-01-06 12:40:14 -0500131 *
Billy McFall2d0b6e32017-01-11 08:44:52 -0500132 * This command has the following optional parameters:
133 *
134 * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either
135 * X:X:X:X:X:X unix or X.X.X cisco format.
136 *
Billy McFalla1b99da2017-01-06 12:40:14 -0500137 * @cliexpar
138 * Example of how to create a host interface tied to one side of an
139 * existing linux veth pair named vpp1:
140 * @cliexstart{create host-interface name vpp1}
141 * host-vpp1
142 * @cliexend
143 * Once the host interface is created, enable the interface using:
144 * @cliexcmd{set interface state host-vpp1 up}
145?*/
Damjan Marion83243a02016-02-29 13:09:30 +0100146VLIB_CLI_COMMAND (af_packet_create_command, static) = {
147 .path = "create host-interface",
Mohsin Kazmicae84fa2021-10-08 15:10:49 +0000148 .short_help =
149 "create host-interface name <ifname> [hw-addr <mac-addr>] [mode ip]",
Damjan Marion83243a02016-02-29 13:09:30 +0100150 .function = af_packet_create_command_fn,
151};
152
Peter Leidba76f22016-04-08 08:16:31 -0700153static clib_error_t *
154af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
Damjan Marion00a9dca2016-08-17 17:05:46 +0200155 vlib_cli_command_t * cmd)
Peter Leidba76f22016-04-08 08:16:31 -0700156{
Damjan Marion00a9dca2016-08-17 17:05:46 +0200157 unformat_input_t _line_input, *line_input = &_line_input;
158 u8 *host_if_name = NULL;
Billy McFalla9a20e72017-02-15 11:39:12 -0500159 clib_error_t *error = NULL;
Peter Leidba76f22016-04-08 08:16:31 -0700160
161 /* Get a line of input. */
Damjan Marion00a9dca2016-08-17 17:05:46 +0200162 if (!unformat_user (input, unformat_line_input, line_input))
Peter Leidba76f22016-04-08 08:16:31 -0700163 return 0;
164
165 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
166 {
167 if (unformat (line_input, "name %s", &host_if_name))
Damjan Marion00a9dca2016-08-17 17:05:46 +0200168 ;
Peter Leidba76f22016-04-08 08:16:31 -0700169 else
Billy McFalla9a20e72017-02-15 11:39:12 -0500170 {
171 error = clib_error_return (0, "unknown input `%U'",
172 format_unformat_error, line_input);
173 goto done;
174 }
Peter Leidba76f22016-04-08 08:16:31 -0700175 }
Peter Leidba76f22016-04-08 08:16:31 -0700176
177 if (host_if_name == NULL)
Billy McFalla9a20e72017-02-15 11:39:12 -0500178 {
179 error = clib_error_return (0, "missing host interface name");
180 goto done;
181 }
Peter Leidba76f22016-04-08 08:16:31 -0700182
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200183 af_packet_delete_if (host_if_name);
Peter Leidba76f22016-04-08 08:16:31 -0700184
Billy McFalla9a20e72017-02-15 11:39:12 -0500185done:
186 vec_free (host_if_name);
187 unformat_free (line_input);
188
189 return error;
Peter Leidba76f22016-04-08 08:16:31 -0700190}
191
Billy McFalla1b99da2017-01-06 12:40:14 -0500192/*?
193 * Delete a host interface. Use the linux interface name to identify
194 * the host interface to be deleted. In VPP, host interfaces are
195 * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
196 * is the name of the linux interface.
197 *
198 * @cliexpar
199 * Example of how to delete a host interface named host-vpp1:
200 * @cliexcmd{delete host-interface name vpp1}
201?*/
Peter Leidba76f22016-04-08 08:16:31 -0700202VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
203 .path = "delete host-interface",
Billy McFalla1b99da2017-01-06 12:40:14 -0500204 .short_help = "delete host-interface name <ifname>",
Peter Leidba76f22016-04-08 08:16:31 -0700205 .function = af_packet_delete_command_fn,
206};
207
Jakub Grajciar92b02752017-10-20 13:37:28 +0200208static clib_error_t *
209af_packet_set_l4_cksum_offload_command_fn (vlib_main_t * vm,
210 unformat_input_t * input,
211 vlib_cli_command_t * cmd)
212{
213 unformat_input_t _line_input, *line_input = &_line_input;
214 u8 set = 0;
215 clib_error_t *error = NULL;
216 vnet_main_t *vnm = vnet_get_main ();
217 u32 sw_if_index;
218
219 if (!unformat_user (input, unformat_line_input, line_input))
220 return 0;
221
222 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
223 {
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200224 if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
225 &sw_if_index))
Jakub Grajciar92b02752017-10-20 13:37:28 +0200226 ;
227 else if (unformat (line_input, "on"))
228 set = 1;
229 else if (unformat (line_input, "off"))
230 set = 0;
231 else
232 {
233 error = clib_error_return (0, "unknown input '%U'",
234 format_unformat_error, line_input);
235 goto done;
236 }
237 }
238
Nathan Skrzypczak7d0e30b2021-06-23 11:28:39 +0200239 if (af_packet_set_l4_cksum_offload (sw_if_index, set) < 0)
Jakub Grajciar64ae7782017-11-14 13:45:04 +0100240 error = clib_error_return (0, "not an af_packet interface");
Jakub Grajciar92b02752017-10-20 13:37:28 +0200241
242done:
243 unformat_free (line_input);
244 return error;
245}
246
247/*?
248 * Set TCP/UDP offload checksum calculation. Use interface
249 * name to identify the interface to set TCP/UDP offload checksum
250 * calculation.
251 *
252 * @cliexpar
253 * Example of how to set TCP/UDP offload checksum calculation on host-vpp0:
254 * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 off}
255 * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 on}
256?*/
Jakub Grajciar92b02752017-10-20 13:37:28 +0200257VLIB_CLI_COMMAND (af_packet_set_l4_cksum_offload_command, static) = {
258 .path = "set host-interface l4-cksum-offload",
259 .short_help = "set host-interface l4-cksum-offload <host-if-name> <on|off>",
260 .function = af_packet_set_l4_cksum_offload_command_fn,
261};
Jakub Grajciar92b02752017-10-20 13:37:28 +0200262
Damjan Marion83243a02016-02-29 13:09:30 +0100263clib_error_t *
264af_packet_cli_init (vlib_main_t * vm)
265{
266 return 0;
267}
268
269VLIB_INIT_FUNCTION (af_packet_cli_init);
Damjan Marion00a9dca2016-08-17 17:05:46 +0200270
271/*
272 * fd.io coding-style-patch-verification: ON
273 *
274 * Local Variables:
275 * eval: (c-set-style "gnu")
276 * End:
277 */