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>
36 af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
37 vlib_cli_command_t * cmd)
39 unformat_input_t _line_input, * line_input = &_line_input;
40 u8 * host_if_name = NULL;
45 /* Get a line of input. */
46 if (! unformat_user (input, unformat_line_input, line_input))
49 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
51 if (unformat (line_input, "name %s", &host_if_name))
53 else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
56 return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
58 unformat_free (line_input);
60 if (host_if_name == NULL)
61 return clib_error_return (0, "missing host interface name");
63 r = af_packet_create_if(vm, host_if_name, hw_addr_ptr);
65 if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
66 return clib_error_return(0, "%s (errno %d)", strerror (errno), errno);
68 if (r == VNET_API_ERROR_INVALID_INTERFACE)
69 return clib_error_return(0, "Invalid interface name");
71 if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
72 return clib_error_return(0, "Interface elready exists");
77 VLIB_CLI_COMMAND (af_packet_create_command, static) = {
78 .path = "create host-interface",
79 .short_help = "create host-interface name <interface name> [hw-addr <mac>]",
80 .function = af_packet_create_command_fn,
84 af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
85 vlib_cli_command_t * cmd)
87 unformat_input_t _line_input, * line_input = &_line_input;
88 u8 * host_if_name = NULL;
90 /* Get a line of input. */
91 if (! unformat_user (input, unformat_line_input, line_input))
94 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
96 if (unformat (line_input, "name %s", &host_if_name))
99 return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
101 unformat_free (line_input);
103 if (host_if_name == NULL)
104 return clib_error_return (0, "missing host interface name");
106 af_packet_delete_if(vm, host_if_name);
111 VLIB_CLI_COMMAND (af_packet_delete_command, static) = {
112 .path = "delete host-interface",
113 .short_help = "delete host-interface name <interface name>",
114 .function = af_packet_delete_command_fn,
118 af_packet_cli_init (vlib_main_t * vm)
123 VLIB_INIT_FUNCTION (af_packet_cli_init);