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