2 *------------------------------------------------------------------
3 * af_packet.c - linux kernel packet interface
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <fcntl.h> /* for open */
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/uio.h> /* for iovec */
26 #include <netinet/in.h>
28 #include <vlib/vlib.h>
29 #include <vlib/unix/unix.h>
30 #include <vnet/ip/ip.h>
31 #include <vnet/ethernet/ethernet.h>
33 #include <vnet/devices/af_packet/af_packet.h>
37 * @brief CLI for Host Interface Device Driver.
39 * This file contains the source code for CLI for the host interface.
43 af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
44 vlib_cli_command_t * cmd)
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;
52 clib_memset (arg, 0, sizeof (*arg));
54 /* Get a line of input. */
55 if (!unformat_user (input, unformat_line_input, line_input))
58 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
60 if (unformat (line_input, "name %s", &arg->host_if_name))
62 else if (unformat (line_input, "rx-size %u", &arg->rx_frame_size))
64 else if (unformat (line_input, "tx-size %u", &arg->tx_frame_size))
66 else if (unformat (line_input, "rx-per-block %u",
67 &arg->rx_frames_per_block))
69 else if (unformat (line_input, "tx-per-block %u",
70 &arg->tx_frames_per_block))
72 else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address,
74 arg->hw_addr = hwaddr;
77 error = clib_error_return (0, "unknown input `%U'",
78 format_unformat_error, line_input);
83 if (arg->host_if_name == NULL)
85 error = clib_error_return (0, "missing host interface name");
89 r = af_packet_create_if (arg);
91 if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
93 error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
97 if (r == VNET_API_ERROR_INVALID_INTERFACE)
99 error = clib_error_return (0, "Invalid interface name");
103 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
105 error = clib_error_return (0, "Interface already exists");
109 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
113 vec_free (arg->host_if_name);
114 unformat_free (line_input);
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
125 * '<em>show interface</em>' command to display host interface details.
127 * This command has the following optional parameters:
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.
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}
138 * Once the host interface is created, enable the interface using:
139 * @cliexcmd{set interface state host-vpp1 up}
141 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
142 .path = "create host-interface",
143 .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]",
144 .function = af_packet_create_command_fn,
147 static clib_error_t *
148 af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
149 vlib_cli_command_t * cmd)
151 unformat_input_t _line_input, *line_input = &_line_input;
152 u8 *host_if_name = NULL;
153 clib_error_t *error = NULL;
155 /* Get a line of input. */
156 if (!unformat_user (input, unformat_line_input, line_input))
159 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
161 if (unformat (line_input, "name %s", &host_if_name))
165 error = clib_error_return (0, "unknown input `%U'",
166 format_unformat_error, line_input);
171 if (host_if_name == NULL)
173 error = clib_error_return (0, "missing host interface name");
177 af_packet_delete_if (host_if_name);
180 vec_free (host_if_name);
181 unformat_free (line_input);
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.
193 * Example of how to delete a host interface named host-vpp1:
194 * @cliexcmd{delete host-interface name vpp1}
196 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
197 .path = "delete host-interface",
198 .short_help = "delete host-interface name <ifname>",
199 .function = af_packet_delete_command_fn,
202 static clib_error_t *
203 af_packet_set_l4_cksum_offload_command_fn (vlib_main_t * vm,
204 unformat_input_t * input,
205 vlib_cli_command_t * cmd)
207 unformat_input_t _line_input, *line_input = &_line_input;
209 clib_error_t *error = NULL;
210 vnet_main_t *vnm = vnet_get_main ();
213 if (!unformat_user (input, unformat_line_input, line_input))
216 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
218 if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm,
221 else if (unformat (line_input, "on"))
223 else if (unformat (line_input, "off"))
227 error = clib_error_return (0, "unknown input '%U'",
228 format_unformat_error, line_input);
233 if (af_packet_set_l4_cksum_offload (sw_if_index, set) < 0)
234 error = clib_error_return (0, "not an af_packet interface");
237 unformat_free (line_input);
242 * Set TCP/UDP offload checksum calculation. Use interface
243 * name to identify the interface to set TCP/UDP offload checksum
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}
251 VLIB_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,
258 af_packet_cli_init (vlib_main_t * vm)
263 VLIB_INIT_FUNCTION (af_packet_cli_init);
266 * fd.io coding-style-patch-verification: ON
269 * eval: (c-set-style "gnu")