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; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 52 | clib_error_t *error = NULL; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 53 | |
| 54 | /* Get a line of input. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 55 | if (!unformat_user (input, unformat_line_input, line_input)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 56 | return 0; |
| 57 | |
| 58 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 59 | { |
| 60 | if (unformat (line_input, "name %s", &host_if_name)) |
| 61 | ; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 62 | else |
| 63 | if (unformat |
| 64 | (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr)) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 65 | hw_addr_ptr = hwaddr; |
| 66 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 67 | { |
| 68 | error = clib_error_return (0, "unknown input `%U'", |
| 69 | format_unformat_error, line_input); |
| 70 | goto done; |
| 71 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 72 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 73 | |
| 74 | if (host_if_name == NULL) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 75 | { |
| 76 | error = clib_error_return (0, "missing host interface name"); |
| 77 | goto done; |
| 78 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 79 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 80 | r = af_packet_create_if (vm, host_if_name, hw_addr_ptr, &sw_if_index); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 81 | |
| 82 | if (r == VNET_API_ERROR_SYSCALL_ERROR_1) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 83 | { |
| 84 | error = clib_error_return (0, "%s (errno %d)", strerror (errno), errno); |
| 85 | goto done; |
| 86 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 87 | |
| 88 | if (r == VNET_API_ERROR_INVALID_INTERFACE) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 89 | { |
| 90 | error = clib_error_return (0, "Invalid interface name"); |
| 91 | goto done; |
| 92 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 93 | |
| 94 | if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 95 | { |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 96 | error = clib_error_return (0, "Interface already exists"); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 97 | goto done; |
| 98 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 99 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 100 | vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), |
| 101 | sw_if_index); |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 102 | |
| 103 | done: |
| 104 | vec_free (host_if_name); |
| 105 | unformat_free (line_input); |
| 106 | |
| 107 | return error; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 110 | /*? |
| 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 |
Dave Barach | 13ad1f0 | 2017-03-26 19:36:18 -0400 | [diff] [blame] | 116 | * '<em>show interface</em>' command to display host interface details. |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 117 | * |
Billy McFall | 2d0b6e3 | 2017-01-11 08:44:52 -0500 | [diff] [blame] | 118 | * This command has the following optional parameters: |
| 119 | * |
| 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. |
| 122 | * |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 123 | * @cliexpar |
| 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} |
| 127 | * host-vpp1 |
| 128 | * @cliexend |
| 129 | * Once the host interface is created, enable the interface using: |
| 130 | * @cliexcmd{set interface state host-vpp1 up} |
| 131 | ?*/ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 132 | /* *INDENT-OFF* */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 133 | VLIB_CLI_COMMAND (af_packet_create_command, static) = { |
| 134 | .path = "create host-interface", |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 135 | .short_help = "create host-interface name <ifname> [hw-addr <mac-addr>]", |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 136 | .function = af_packet_create_command_fn, |
| 137 | }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 138 | /* *INDENT-ON* */ |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 139 | |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 140 | static clib_error_t * |
| 141 | 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] | 142 | vlib_cli_command_t * cmd) |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 143 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 144 | unformat_input_t _line_input, *line_input = &_line_input; |
| 145 | u8 *host_if_name = NULL; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 146 | clib_error_t *error = NULL; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 147 | |
| 148 | /* Get a line of input. */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 149 | if (!unformat_user (input, unformat_line_input, line_input)) |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 150 | return 0; |
| 151 | |
| 152 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 153 | { |
| 154 | if (unformat (line_input, "name %s", &host_if_name)) |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 155 | ; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 156 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 157 | { |
| 158 | error = clib_error_return (0, "unknown input `%U'", |
| 159 | format_unformat_error, line_input); |
| 160 | goto done; |
| 161 | } |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 162 | } |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 163 | |
| 164 | if (host_if_name == NULL) |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 165 | { |
| 166 | error = clib_error_return (0, "missing host interface name"); |
| 167 | goto done; |
| 168 | } |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 169 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 170 | af_packet_delete_if (vm, host_if_name); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 171 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 172 | done: |
| 173 | vec_free (host_if_name); |
| 174 | unformat_free (line_input); |
| 175 | |
| 176 | return error; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 179 | /*? |
| 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. |
| 184 | * |
| 185 | * @cliexpar |
| 186 | * Example of how to delete a host interface named host-vpp1: |
| 187 | * @cliexcmd{delete host-interface name vpp1} |
| 188 | ?*/ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 189 | /* *INDENT-OFF* */ |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 190 | VLIB_CLI_COMMAND (af_packet_delete_command, static) = { |
| 191 | .path = "delete host-interface", |
Billy McFall | a1b99da | 2017-01-06 12:40:14 -0500 | [diff] [blame] | 192 | .short_help = "delete host-interface name <ifname>", |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 193 | .function = af_packet_delete_command_fn, |
| 194 | }; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 195 | /* *INDENT-ON* */ |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 196 | |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 197 | static clib_error_t * |
| 198 | af_packet_set_l4_cksum_offload_command_fn (vlib_main_t * vm, |
| 199 | unformat_input_t * input, |
| 200 | vlib_cli_command_t * cmd) |
| 201 | { |
| 202 | unformat_input_t _line_input, *line_input = &_line_input; |
| 203 | u8 set = 0; |
| 204 | clib_error_t *error = NULL; |
| 205 | vnet_main_t *vnm = vnet_get_main (); |
| 206 | u32 sw_if_index; |
| 207 | |
| 208 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 209 | return 0; |
| 210 | |
| 211 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 212 | { |
| 213 | if (unformat |
| 214 | (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) |
| 215 | ; |
| 216 | else if (unformat (line_input, "on")) |
| 217 | set = 1; |
| 218 | else if (unformat (line_input, "off")) |
| 219 | set = 0; |
| 220 | else |
| 221 | { |
| 222 | error = clib_error_return (0, "unknown input '%U'", |
| 223 | format_unformat_error, line_input); |
| 224 | goto done; |
| 225 | } |
| 226 | } |
| 227 | |
Jakub Grajciar | 64ae778 | 2017-11-14 13:45:04 +0100 | [diff] [blame] | 228 | if (af_packet_set_l4_cksum_offload (vm, sw_if_index, set) < 0) |
| 229 | error = clib_error_return (0, "not an af_packet interface"); |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 230 | |
| 231 | done: |
| 232 | unformat_free (line_input); |
| 233 | return error; |
| 234 | } |
| 235 | |
| 236 | /*? |
| 237 | * Set TCP/UDP offload checksum calculation. Use interface |
| 238 | * name to identify the interface to set TCP/UDP offload checksum |
| 239 | * calculation. |
| 240 | * |
| 241 | * @cliexpar |
| 242 | * Example of how to set TCP/UDP offload checksum calculation on host-vpp0: |
| 243 | * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 off} |
| 244 | * @cliexcmd{set host-interface l4-cksum-offload host-vpp0 on} |
| 245 | ?*/ |
| 246 | /* *INDENT-OFF* */ |
| 247 | VLIB_CLI_COMMAND (af_packet_set_l4_cksum_offload_command, static) = { |
| 248 | .path = "set host-interface l4-cksum-offload", |
| 249 | .short_help = "set host-interface l4-cksum-offload <host-if-name> <on|off>", |
| 250 | .function = af_packet_set_l4_cksum_offload_command_fn, |
| 251 | }; |
| 252 | /* *INDENT-ON* */ |
| 253 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 254 | clib_error_t * |
| 255 | af_packet_cli_init (vlib_main_t * vm) |
| 256 | { |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | VLIB_INIT_FUNCTION (af_packet_cli_init); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 261 | |
| 262 | /* |
| 263 | * fd.io coding-style-patch-verification: ON |
| 264 | * |
| 265 | * Local Variables: |
| 266 | * eval: (c-set-style "gnu") |
| 267 | * End: |
| 268 | */ |