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