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 u8 *host_if_name = NULL;
52 clib_error_t *error = NULL;
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", &host_if_name))
64 (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
68 error = clib_error_return (0, "unknown input `%U'",
69 format_unformat_error, line_input);
74 if (host_if_name == NULL)
76 error = clib_error_return (0, "missing host interface name");
80 r = af_packet_create_if (vm, host_if_name, hw_addr_ptr, &sw_if_index);
82 if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
84 error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno);
88 if (r == VNET_API_ERROR_INVALID_INTERFACE)
90 error = clib_error_return (0, "Invalid interface name");
94 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
96 error = clib_error_return (0, "Interface elready exists");
100 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (),
104 vec_free (host_if_name);
105 unformat_free (line_input);
111 * Create a host interface that will attach to a linux AF_PACKET
112 * interface, one side of a veth pair. The veth pair must already
113 * exist. Once created, a new host interface will exist in VPP
114 * with the name '<em>host-<ifname></em>', where '<em><ifname></em>'
115 * is the name of the specified veth pair. Use the
116 * '<em>show interface</em>' command to display host interface details.
118 * This command has the following optional parameters:
120 * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either
121 * X:X:X:X:X:X unix or X.X.X cisco format.
124 * Example of how to create a host interface tied to one side of an
125 * existing linux veth pair named vpp1:
126 * @cliexstart{create host-interface name vpp1}
129 * Once the host interface is created, enable the interface using:
130 * @cliexcmd{set interface state host-vpp1 up}
133 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
134 .path = "create host-interface",
135 .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]",
136 .function = af_packet_create_command_fn,
140 static clib_error_t *
141 af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
142 vlib_cli_command_t * cmd)
144 unformat_input_t _line_input, *line_input = &_line_input;
145 u8 *host_if_name = NULL;
146 clib_error_t *error = NULL;
148 /* Get a line of input. */
149 if (!unformat_user (input, unformat_line_input, line_input))
152 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
154 if (unformat (line_input, "name %s", &host_if_name))
158 error = clib_error_return (0, "unknown input `%U'",
159 format_unformat_error, line_input);
164 if (host_if_name == NULL)
166 error = clib_error_return (0, "missing host interface name");
170 af_packet_delete_if (vm, host_if_name);
173 vec_free (host_if_name);
174 unformat_free (line_input);
180 * Delete a host interface. Use the linux interface name to identify
181 * the host interface to be deleted. In VPP, host interfaces are
182 * named as '<em>host-<ifname></em>', where '<em><ifname></em>'
183 * is the name of the linux interface.
186 * Example of how to delete a host interface named host-vpp1:
187 * @cliexcmd{delete host-interface name vpp1}
190 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
191 .path = "delete host-interface",
192 .short_help = "delete host-interface name <ifname>",
193 .function = af_packet_delete_command_fn,
198 af_packet_cli_init (vlib_main_t * vm)
203 VLIB_INIT_FUNCTION (af_packet_cli_init);
206 * fd.io coding-style-patch-verification: ON
209 * eval: (c-set-style "gnu")