Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 1 | /* |
| 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 McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 35 | /** |
| 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 Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 42 | static clib_error_t * |
| 43 | af_packet_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 44 | vlib_cli_command_t * cmd) |
| 45 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 46 | unformat_input_t _line_input, *line_input = &_line_input; |
| 47 | u8 *host_if_name = NULL; |
| 48 | u8 hwaddr[6]; |
| 49 | u8 *hw_addr_ptr = 0; |
Pierre Pfister | 78ea9c2 | 2016-05-23 12:51:54 +0100 | [diff] [blame] | 50 | u32 sw_if_index; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 51 | int r; |
| 52 | |
| 53 | /* Get a line of input. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 54 | if (!unformat_user (input, unformat_line_input, line_input)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 55 | return 0; |
| 56 | |
| 57 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 58 | { |
| 59 | if (unformat (line_input, "name %s", &host_if_name)) |
| 60 | ; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 61 | else |
| 62 | if (unformat |
| 63 | (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 64 | hw_addr_ptr = hwaddr; |
| 65 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 66 | return clib_error_return (0, "unknown input `%U'", |
| 67 | format_unformat_error, input); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 68 | } |
| 69 | unformat_free (line_input); |
| 70 | |
| 71 | if (host_if_name == NULL) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 72 | return clib_error_return (0, "missing host interface name"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 73 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 74 | r = af_packet_create_if (vm, host_if_name, hw_addr_ptr, &sw_if_index); |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 75 | vec_free (host_if_name); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 76 | |
| 77 | if (r == VNET_API_ERROR_SYSCALL_ERROR_1) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 78 | return clib_error_return (0, "%s (errno %d)", strerror (errno), errno); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 79 | |
| 80 | if (r == VNET_API_ERROR_INVALID_INTERFACE) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 81 | return clib_error_return (0, "Invalid interface name"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 82 | |
| 83 | if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 84 | return clib_error_return (0, "Interface elready exists"); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 85 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 86 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), |
| 87 | sw_if_index); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 91 | /*? |
| 92 | * Create a host interface that will attach to a linux AF_PACKET |
| 93 | * interface, one side of a veth pair. The veth pair must already |
| 94 | * exist. Once created, a new host interface will exist in VPP |
| 95 | * with the name '<em>host-<ifname></em>', where '<em><ifname></em>' |
| 96 | * is the name of the specified veth pair. Use the |
| 97 | * '<em>show interfaces</em>' command to display host interface details. |
| 98 | * |
Billy McFall | 2d0b6e3 | 2017-01-11 08:44:52 -0500 | [diff] [blame] | 99 | * This command has the following optional parameters: |
| 100 | * |
| 101 | * - <b>hw-addr <mac-addr></b> - Optional ethernet address, can be in either |
| 102 | * X:X:X:X:X:X unix or X.X.X cisco format. |
| 103 | * |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 104 | * @cliexpar |
| 105 | * Example of how to create a host interface tied to one side of an |
| 106 | * existing linux veth pair named vpp1: |
| 107 | * @cliexstart{create host-interface name vpp1} |
| 108 | * host-vpp1 |
| 109 | * @cliexend |
| 110 | * Once the host interface is created, enable the interface using: |
| 111 | * @cliexcmd{set interface state host-vpp1 up} |
| 112 | ?*/ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 113 | /* *INDENT-OFF* */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 114 | VLIB_CLI_COMMAND (af_packet_create_command, static) = { |
| 115 | .path = "create host-interface", |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 116 | .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]", |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 117 | .function = af_packet_create_command_fn, |
| 118 | }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 119 | /* *INDENT-ON* */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 120 | |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 121 | static clib_error_t * |
| 122 | af_packet_delete_command_fn (vlib_main_t * vm, unformat_input_t * input, |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 123 | vlib_cli_command_t * cmd) |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 124 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 125 | unformat_input_t _line_input, *line_input = &_line_input; |
| 126 | u8 *host_if_name = NULL; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 127 | |
| 128 | /* Get a line of input. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 129 | if (!unformat_user (input, unformat_line_input, line_input)) |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 130 | return 0; |
| 131 | |
| 132 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 133 | { |
| 134 | if (unformat (line_input, "name %s", &host_if_name)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 135 | ; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 136 | else |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 137 | return clib_error_return (0, "unknown input `%U'", |
| 138 | format_unformat_error, input); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 139 | } |
| 140 | unformat_free (line_input); |
| 141 | |
| 142 | if (host_if_name == NULL) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 143 | return clib_error_return (0, "missing host interface name"); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 144 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 145 | af_packet_delete_if (vm, host_if_name); |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 146 | vec_free (host_if_name); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 151 | /*? |
| 152 | * Delete a host interface. Use the linux interface name to identify |
| 153 | * the host interface to be deleted. In VPP, host interfaces are |
| 154 | * named as '<em>host-<ifname></em>', where '<em><ifname></em>' |
| 155 | * is the name of the linux interface. |
| 156 | * |
| 157 | * @cliexpar |
| 158 | * Example of how to delete a host interface named host-vpp1: |
| 159 | * @cliexcmd{delete host-interface name vpp1} |
| 160 | ?*/ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 161 | /* *INDENT-OFF* */ |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 162 | VLIB_CLI_COMMAND (af_packet_delete_command, static) = { |
| 163 | .path = "delete host-interface", |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 164 | .short_help = "delete host-interface name <ifname>", |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 165 | .function = af_packet_delete_command_fn, |
| 166 | }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 167 | /* *INDENT-ON* */ |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 168 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 169 | clib_error_t * |
| 170 | af_packet_cli_init (vlib_main_t * vm) |
| 171 | { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | VLIB_INIT_FUNCTION (af_packet_cli_init); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 176 | |
| 177 | /* |
| 178 | * fd.io coding-style-patch-verification: ON |
| 179 | * |
| 180 | * Local Variables: |
| 181 | * eval: (c-set-style "gnu") |
| 182 | * End: |
| 183 | */ |