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 <linux/if_ether.h> |
| 21 | #include <linux/if_packet.h> |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 22 | #include <sys/ioctl.h> |
| 23 | #include <net/if.h> |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 24 | #include <dirent.h> |
Ray Kinsella | 7bfa119 | 2017-05-15 11:52:43 +0100 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <fcntl.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 28 | |
Damjan Marion | 01914ce | 2017-09-14 19:04:50 +0200 | [diff] [blame] | 29 | #include <vppinfra/linux/sysfs.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 30 | #include <vlib/vlib.h> |
| 31 | #include <vlib/unix/unix.h> |
| 32 | #include <vnet/ip/ip.h> |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 33 | #include <vnet/devices/netlink.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 34 | #include <vnet/ethernet/ethernet.h> |
Mohammed Hawari | 85c1943 | 2020-12-18 16:29:45 +0100 | [diff] [blame] | 35 | #include <vnet/interface/rx_queue_funcs.h> |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 36 | #include <vnet/interface/tx_queue_funcs.h> |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 37 | |
| 38 | #include <vnet/devices/af_packet/af_packet.h> |
| 39 | |
Dave Wallace | 71612d6 | 2017-10-24 01:32:41 -0400 | [diff] [blame] | 40 | af_packet_main_t af_packet_main; |
| 41 | |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 42 | VNET_HW_INTERFACE_CLASS (af_packet_ip_device_hw_interface_class, static) = { |
| 43 | .name = "af-packet-ip-device", |
| 44 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
| 45 | }; |
| 46 | |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 47 | #define AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK 1024 |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 48 | #define AF_PACKET_DEFAULT_TX_FRAME_SIZE (2048 * 33) // GSO packet of 64KB |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 49 | #define AF_PACKET_TX_BLOCK_NR 1 |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 50 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 51 | #define AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK 32 |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 52 | #define AF_PACKET_DEFAULT_RX_FRAME_SIZE 2048 |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 53 | #define AF_PACKET_RX_BLOCK_NR 160 |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 54 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 55 | /*defined in net/if.h but clashes with dpdk headers */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 56 | unsigned int if_nametoindex (const char *ifname); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 57 | |
Damjan Marion | 88a9c0e | 2022-01-06 21:14:08 +0100 | [diff] [blame] | 58 | static clib_error_t * |
Damjan Marion | 1cd0e5d | 2022-01-17 14:49:17 +0100 | [diff] [blame] | 59 | af_packet_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi, |
| 60 | u32 frame_size) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 61 | { |
Damjan Marion | 88a9c0e | 2022-01-06 21:14:08 +0100 | [diff] [blame] | 62 | clib_error_t *error, *rv; |
Ray Kinsella | 7bfa119 | 2017-05-15 11:52:43 +0100 | [diff] [blame] | 63 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 88a9c0e | 2022-01-06 21:14:08 +0100 | [diff] [blame] | 64 | af_packet_if_t *apif = pool_elt_at_index (apm->interfaces, hi->dev_instance); |
Ray Kinsella | 7bfa119 | 2017-05-15 11:52:43 +0100 | [diff] [blame] | 65 | |
Damjan Marion | 1cd0e5d | 2022-01-17 14:49:17 +0100 | [diff] [blame] | 66 | error = vnet_netlink_set_link_mtu (apif->host_if_index, |
| 67 | frame_size + hi->frame_overhead); |
Damjan Marion | 88a9c0e | 2022-01-06 21:14:08 +0100 | [diff] [blame] | 68 | |
| 69 | if (error) |
Ray Kinsella | 7bfa119 | 2017-05-15 11:52:43 +0100 | [diff] [blame] | 70 | { |
Damjan Marion | 88a9c0e | 2022-01-06 21:14:08 +0100 | [diff] [blame] | 71 | vlib_log_err (apm->log_class, "netlink failed to change MTU: %U", |
| 72 | format_clib_error, error); |
| 73 | rv = vnet_error (VNET_ERR_SYSCALL_ERROR_1, "netlink error: %U", |
| 74 | format_clib_error, error); |
| 75 | clib_error_free (error); |
| 76 | return rv; |
Ray Kinsella | 7bfa119 | 2017-05-15 11:52:43 +0100 | [diff] [blame] | 77 | } |
Damjan Marion | 88a9c0e | 2022-01-06 21:14:08 +0100 | [diff] [blame] | 78 | else |
Damjan Marion | 1cd0e5d | 2022-01-17 14:49:17 +0100 | [diff] [blame] | 79 | apif->host_mtu = frame_size + hi->frame_overhead; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 83 | static int |
| 84 | af_packet_read_mtu (af_packet_if_t *apif) |
| 85 | { |
| 86 | af_packet_main_t *apm = &af_packet_main; |
| 87 | clib_error_t *error; |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 88 | error = vnet_netlink_get_link_mtu (apif->host_if_index, &apif->host_mtu); |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 89 | if (error) |
| 90 | { |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 91 | vlib_log_err (apm->log_class, "netlink failed to get MTU: %U", |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 92 | format_clib_error, error); |
| 93 | clib_error_free (error); |
| 94 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 99 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 100 | af_packet_fd_read_ready (clib_file_t * uf) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 101 | { |
Damjan Marion | eb743fa | 2017-03-20 16:34:15 +0100 | [diff] [blame] | 102 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 103 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 104 | /* Schedule the rx node */ |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 105 | vnet_hw_if_rx_queue_set_int_pending (vnm, uf->private_data); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 110 | is_bridge (const u8 * host_if_name) |
| 111 | { |
| 112 | u8 *s; |
| 113 | DIR *dir = NULL; |
| 114 | |
| 115 | s = format (0, "/sys/class/net/%s/bridge%c", host_if_name, 0); |
| 116 | dir = opendir ((char *) s); |
| 117 | vec_free (s); |
| 118 | |
| 119 | if (dir) |
| 120 | { |
| 121 | closedir (dir); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | return -1; |
| 126 | } |
| 127 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 128 | static void |
| 129 | af_packet_set_rx_queues (vlib_main_t *vm, af_packet_if_t *apif) |
| 130 | { |
| 131 | vnet_main_t *vnm = vnet_get_main (); |
| 132 | af_packet_queue_t *rx_queue; |
| 133 | |
| 134 | vnet_hw_if_set_input_node (vnm, apif->hw_if_index, |
| 135 | af_packet_input_node.index); |
| 136 | |
| 137 | vec_foreach (rx_queue, apif->rx_queues) |
| 138 | { |
| 139 | rx_queue->queue_index = vnet_hw_if_register_rx_queue ( |
| 140 | vnm, apif->hw_if_index, rx_queue->queue_id, VNET_HW_IF_RXQ_THREAD_ANY); |
| 141 | |
| 142 | { |
| 143 | clib_file_t template = { 0 }; |
| 144 | template.read_function = af_packet_fd_read_ready; |
| 145 | template.file_descriptor = rx_queue->fd; |
| 146 | template.private_data = rx_queue->queue_index; |
| 147 | template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED; |
| 148 | template.description = |
| 149 | format (0, "%U queue %u", format_af_packet_device_name, |
| 150 | apif->dev_instance, rx_queue->queue_id); |
| 151 | rx_queue->clib_file_index = clib_file_add (&file_main, &template); |
| 152 | } |
| 153 | vnet_hw_if_set_rx_queue_file_index (vnm, rx_queue->queue_index, |
| 154 | rx_queue->clib_file_index); |
| 155 | vnet_hw_if_set_rx_queue_mode (vnm, rx_queue->queue_index, |
| 156 | VNET_HW_IF_RX_MODE_INTERRUPT); |
| 157 | rx_queue->mode = VNET_HW_IF_RX_MODE_INTERRUPT; |
| 158 | } |
| 159 | vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index); |
| 160 | } |
| 161 | |
| 162 | static void |
| 163 | af_packet_set_tx_queues (vlib_main_t *vm, af_packet_if_t *apif) |
| 164 | { |
| 165 | vnet_main_t *vnm = vnet_get_main (); |
| 166 | af_packet_main_t *apm = &af_packet_main; |
| 167 | af_packet_queue_t *tx_queue; |
| 168 | |
| 169 | vec_foreach (tx_queue, apif->tx_queues) |
| 170 | { |
| 171 | tx_queue->queue_index = vnet_hw_if_register_tx_queue ( |
| 172 | vnm, apif->hw_if_index, tx_queue->queue_id); |
| 173 | } |
| 174 | |
| 175 | if (apif->num_txqs == 0) |
| 176 | { |
| 177 | vlib_log_err (apm->log_class, "Interface %U has 0 txq", |
| 178 | format_vnet_hw_if_index_name, vnm, apif->hw_if_index); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | for (u32 j = 0; j < vlib_get_n_threads (); j++) |
| 183 | { |
| 184 | u32 qi = apif->tx_queues[j % apif->num_txqs].queue_index; |
| 185 | vnet_hw_if_tx_queue_assign_thread (vnm, qi, j); |
| 186 | } |
| 187 | |
| 188 | vnet_hw_if_update_runtime_data (vnm, apif->hw_if_index); |
| 189 | } |
| 190 | |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 191 | static int |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 192 | create_packet_v3_sock (int host_if_index, tpacket_req3_t *rx_req, |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 193 | tpacket_req3_t *tx_req, int *fd, af_packet_ring_t *ring, |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 194 | u32 fanout_id, af_packet_if_flags_t *flags) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 195 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 196 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 197 | struct sockaddr_ll sll; |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 198 | socklen_t req_sz = sizeof (tpacket_req3_t); |
| 199 | int ret; |
| 200 | int ver = TPACKET_V3; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 201 | u32 ring_sz = 0; |
| 202 | |
| 203 | if (rx_req) |
| 204 | ring_sz += rx_req->tp_block_size * rx_req->tp_block_nr; |
| 205 | |
| 206 | if (tx_req) |
| 207 | ring_sz += tx_req->tp_block_size * tx_req->tp_block_nr; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 208 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 209 | if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 210 | { |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 211 | vlib_log_err (apm->log_class, |
| 212 | "Failed to create AF_PACKET socket: %s (errno %d)", |
| 213 | strerror (errno), errno); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 214 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 215 | goto error; |
| 216 | } |
Damjan Marion | c517020 | 2017-10-11 14:15:47 +0200 | [diff] [blame] | 217 | |
Chaoyu Jin | afb1930 | 2018-03-13 07:37:41 -0700 | [diff] [blame] | 218 | /* bind before rx ring is cfged so we don't receive packets from other interfaces */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 219 | clib_memset (&sll, 0, sizeof (sll)); |
Chaoyu Jin | afb1930 | 2018-03-13 07:37:41 -0700 | [diff] [blame] | 220 | sll.sll_family = PF_PACKET; |
| 221 | sll.sll_protocol = htons (ETH_P_ALL); |
| 222 | sll.sll_ifindex = host_if_index; |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 223 | if (bind (*fd, (struct sockaddr *) &sll, sizeof (sll)) < 0) |
Chaoyu Jin | afb1930 | 2018-03-13 07:37:41 -0700 | [diff] [blame] | 224 | { |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 225 | vlib_log_err (apm->log_class, |
| 226 | "Failed to bind rx packet socket: %s (errno %d)", |
| 227 | strerror (errno), errno); |
Chaoyu Jin | afb1930 | 2018-03-13 07:37:41 -0700 | [diff] [blame] | 228 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 229 | goto error; |
| 230 | } |
| 231 | |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 232 | if (setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver)) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 233 | { |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 234 | vlib_log_err (apm->log_class, |
| 235 | "Failed to set rx packet interface version: %s (errno %d)", |
| 236 | strerror (errno), errno); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 237 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 238 | goto error; |
| 239 | } |
| 240 | |
Damjan Marion | c517020 | 2017-10-11 14:15:47 +0200 | [diff] [blame] | 241 | int opt = 1; |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 242 | if (setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt, sizeof (opt)) < 0) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 243 | { |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 244 | vlib_log_err ( |
Mohsin Kazmi | 219cbcb | 2022-03-18 16:58:31 +0000 | [diff] [blame] | 245 | apm->log_class, |
| 246 | "Failed to set packet tx ring error handling option: %s (errno %d)", |
| 247 | strerror (errno), errno); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 248 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 249 | goto error; |
| 250 | } |
| 251 | |
Mohsin Kazmi | 788676b | 2022-04-05 12:43:13 +0000 | [diff] [blame] | 252 | if (*flags & AF_PACKET_IF_FLAGS_CKSUM_GSO) |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 253 | { |
Mohsin Kazmi | 788676b | 2022-04-05 12:43:13 +0000 | [diff] [blame] | 254 | |
| 255 | int opt2 = 1; |
| 256 | if (setsockopt (*fd, SOL_PACKET, PACKET_VNET_HDR, &opt2, sizeof (opt2)) < |
| 257 | 0) |
| 258 | { |
| 259 | // remove the flag |
| 260 | *flags &= ~AF_PACKET_IF_FLAGS_CKSUM_GSO; |
| 261 | vlib_log_debug (apm->log_class, |
| 262 | "Failed to set packet vnet hdr error handling " |
| 263 | "option: %s (errno %d)", |
| 264 | strerror (errno), errno); |
| 265 | } |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 266 | } |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 267 | |
Florin Coras | 2dc942e | 2021-11-22 21:34:56 -0800 | [diff] [blame] | 268 | #if defined(PACKET_QDISC_BYPASS) |
Mohsin Kazmi | 2b6479c | 2022-04-05 12:03:47 +0000 | [diff] [blame] | 269 | if (*flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS) |
| 270 | /* Introduced with Linux 3.14 so the ifdef should eventually be removed */ |
| 271 | if (setsockopt (*fd, SOL_PACKET, PACKET_QDISC_BYPASS, &opt, sizeof (opt)) < |
| 272 | 0) |
| 273 | { |
| 274 | // remove the flag |
| 275 | *flags &= ~AF_PACKET_IF_FLAGS_QDISC_BYPASS; |
| 276 | vlib_log_debug (apm->log_class, |
| 277 | "Failed to set qdisc bypass error " |
| 278 | "handling option: %s (errno %d)", |
| 279 | strerror (errno), errno); |
| 280 | } |
Florin Coras | 2dc942e | 2021-11-22 21:34:56 -0800 | [diff] [blame] | 281 | #endif |
Mohammed Hawari | eed6fc9 | 2021-09-06 11:48:17 +0200 | [diff] [blame] | 282 | |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 283 | if (rx_req) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 284 | { |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 285 | if (*flags & AF_PACKET_IF_FLAGS_FANOUT) |
| 286 | { |
| 287 | int fanout = ((fanout_id & 0xffff) | ((PACKET_FANOUT_HASH) << 16)); |
| 288 | if (setsockopt (*fd, SOL_PACKET, PACKET_FANOUT, &fanout, |
| 289 | sizeof (fanout)) < 0) |
| 290 | { |
| 291 | // remove the flag |
| 292 | *flags &= ~AF_PACKET_IF_FLAGS_FANOUT; |
| 293 | vlib_log_err (apm->log_class, |
| 294 | "Failed to set fanout options: %s (errno %d)", |
| 295 | strerror (errno), errno); |
| 296 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 297 | goto error; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz) < 0) |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 302 | { |
| 303 | vlib_log_err (apm->log_class, |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 304 | "Failed to set packet rx ring options: %s (errno %d)", |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 305 | strerror (errno), errno); |
| 306 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 307 | goto error; |
| 308 | } |
| 309 | } |
| 310 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 311 | if (tx_req) |
| 312 | if (setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz) < 0) |
| 313 | { |
| 314 | vlib_log_err (apm->log_class, |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 315 | "Failed to set packet tx ring options: %s (errno %d)", |
| 316 | strerror (errno), errno); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 317 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 318 | goto error; |
| 319 | } |
| 320 | |
| 321 | ring->ring_start_addr = mmap (NULL, ring_sz, PROT_READ | PROT_WRITE, |
| 322 | MAP_SHARED | MAP_LOCKED, *fd, 0); |
| 323 | if (ring->ring_start_addr == MAP_FAILED) |
| 324 | { |
| 325 | vlib_log_err (apm->log_class, "mmap failure: %s (errno %d)", |
| 326 | strerror (errno), errno); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 327 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 328 | goto error; |
| 329 | } |
| 330 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 331 | ring->ring_size = ring_sz; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 332 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 333 | return 0; |
| 334 | error: |
Dave Barach | 16ad6ae | 2016-07-28 17:55:30 -0400 | [diff] [blame] | 335 | if (*fd >= 0) |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 336 | { |
| 337 | close (*fd); |
| 338 | *fd = -1; |
| 339 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | int |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 344 | af_packet_queue_init (vlib_main_t *vm, af_packet_if_t *apif, |
| 345 | af_packet_create_if_arg_t *arg, |
| 346 | af_packet_queue_t *rx_queue, af_packet_queue_t *tx_queue, |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 347 | u8 queue_id) |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 348 | { |
| 349 | af_packet_main_t *apm = &af_packet_main; |
| 350 | tpacket_req3_t *rx_req = 0; |
| 351 | tpacket_req3_t *tx_req = 0; |
| 352 | int ret, fd = -1; |
| 353 | af_packet_ring_t ring = { 0 }; |
| 354 | u8 *ring_addr = 0; |
| 355 | u32 rx_frames_per_block, tx_frames_per_block; |
| 356 | u32 rx_frame_size, tx_frame_size; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 357 | u32 i = 0; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 358 | |
| 359 | if (rx_queue) |
| 360 | { |
| 361 | rx_frames_per_block = arg->rx_frames_per_block ? |
| 362 | arg->rx_frames_per_block : |
| 363 | AF_PACKET_DEFAULT_RX_FRAMES_PER_BLOCK; |
| 364 | |
| 365 | rx_frame_size = arg->rx_frame_size ? arg->rx_frame_size : |
| 366 | AF_PACKET_DEFAULT_RX_FRAME_SIZE; |
| 367 | vec_validate (rx_queue->rx_req, 0); |
| 368 | rx_queue->rx_req->tp_block_size = rx_frame_size * rx_frames_per_block; |
| 369 | rx_queue->rx_req->tp_frame_size = rx_frame_size; |
| 370 | rx_queue->rx_req->tp_block_nr = AF_PACKET_RX_BLOCK_NR; |
| 371 | rx_queue->rx_req->tp_frame_nr = |
| 372 | AF_PACKET_RX_BLOCK_NR * rx_frames_per_block; |
| 373 | rx_queue->rx_req->tp_retire_blk_tov = 1; // 1 ms block timout |
| 374 | rx_queue->rx_req->tp_feature_req_word = 0; |
| 375 | rx_queue->rx_req->tp_sizeof_priv = 0; |
| 376 | rx_req = rx_queue->rx_req; |
| 377 | } |
| 378 | |
| 379 | if (tx_queue) |
| 380 | { |
| 381 | tx_frames_per_block = arg->tx_frames_per_block ? |
| 382 | arg->tx_frames_per_block : |
| 383 | AF_PACKET_DEFAULT_TX_FRAMES_PER_BLOCK; |
| 384 | tx_frame_size = arg->tx_frame_size ? arg->tx_frame_size : |
| 385 | AF_PACKET_DEFAULT_TX_FRAME_SIZE; |
| 386 | |
| 387 | vec_validate (tx_queue->tx_req, 0); |
| 388 | tx_queue->tx_req->tp_block_size = tx_frame_size * tx_frames_per_block; |
| 389 | tx_queue->tx_req->tp_frame_size = tx_frame_size; |
| 390 | tx_queue->tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR; |
| 391 | tx_queue->tx_req->tp_frame_nr = |
| 392 | AF_PACKET_TX_BLOCK_NR * tx_frames_per_block; |
| 393 | tx_queue->tx_req->tp_retire_blk_tov = 0; |
| 394 | tx_queue->tx_req->tp_sizeof_priv = 0; |
| 395 | tx_queue->tx_req->tp_feature_req_word = 0; |
| 396 | tx_req = tx_queue->tx_req; |
| 397 | } |
| 398 | |
Mohsin Kazmi | 3ce1d14 | 2022-04-05 10:46:39 +0000 | [diff] [blame] | 399 | if (rx_queue || tx_queue) |
| 400 | { |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 401 | ret = create_packet_v3_sock (apif->host_if_index, rx_req, tx_req, &fd, |
| 402 | &ring, apif->dev_instance, &arg->flags); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 403 | |
Mohsin Kazmi | 3ce1d14 | 2022-04-05 10:46:39 +0000 | [diff] [blame] | 404 | if (ret != 0) |
| 405 | goto error; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 406 | |
Mohsin Kazmi | 3ce1d14 | 2022-04-05 10:46:39 +0000 | [diff] [blame] | 407 | vec_add1 (apif->rings, ring); |
| 408 | ring_addr = ring.ring_start_addr; |
| 409 | } |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 410 | |
| 411 | if (rx_queue) |
| 412 | { |
| 413 | rx_queue->fd = fd; |
| 414 | vec_validate (rx_queue->rx_ring, rx_queue->rx_req->tp_block_nr - 1); |
| 415 | vec_foreach_index (i, rx_queue->rx_ring) |
| 416 | { |
| 417 | rx_queue->rx_ring[i] = |
| 418 | ring_addr + i * rx_queue->rx_req->tp_block_size; |
| 419 | } |
| 420 | |
| 421 | rx_queue->next_rx_block = 0; |
| 422 | rx_queue->queue_id = queue_id; |
| 423 | rx_queue->is_rx_pending = 0; |
| 424 | ring_addr = ring_addr + rx_queue->rx_req->tp_block_size * |
| 425 | rx_queue->rx_req->tp_block_nr; |
| 426 | } |
| 427 | |
| 428 | if (tx_queue) |
| 429 | { |
| 430 | tx_queue->fd = fd; |
| 431 | vec_validate (tx_queue->tx_ring, tx_queue->tx_req->tp_block_nr - 1); |
| 432 | vec_foreach_index (i, tx_queue->tx_ring) |
| 433 | { |
| 434 | tx_queue->tx_ring[i] = |
| 435 | ring_addr + i * tx_queue->tx_req->tp_block_size; |
| 436 | } |
| 437 | |
| 438 | tx_queue->next_tx_frame = 0; |
| 439 | tx_queue->queue_id = queue_id; |
| 440 | clib_spinlock_init (&tx_queue->lockp); |
| 441 | } |
| 442 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 443 | return 0; |
| 444 | error: |
| 445 | vlib_log_err (apm->log_class, "Failed to set queue %u error", queue_id); |
Mohsin Kazmi | 3ce1d14 | 2022-04-05 10:46:39 +0000 | [diff] [blame] | 446 | if (rx_queue) |
| 447 | vec_free (rx_queue->rx_req); |
| 448 | if (tx_queue) |
| 449 | vec_free (tx_queue->tx_req); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | int |
| 454 | af_packet_device_init (vlib_main_t *vm, af_packet_if_t *apif, |
| 455 | af_packet_create_if_arg_t *args) |
| 456 | { |
| 457 | af_packet_main_t *apm = &af_packet_main; |
| 458 | af_packet_queue_t *rx_queue = 0; |
| 459 | af_packet_queue_t *tx_queue = 0; |
| 460 | u16 nq = clib_min (args->num_rxqs, args->num_txqs); |
| 461 | u16 i = 0; |
| 462 | int ret = 0; |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 463 | |
| 464 | // enable fanout feature for multi-rxqs |
| 465 | if (args->num_rxqs > 1) |
| 466 | args->flags |= AF_PACKET_IF_FLAGS_FANOUT; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 467 | |
| 468 | vec_validate (apif->rx_queues, args->num_rxqs - 1); |
| 469 | vec_validate (apif->tx_queues, args->num_txqs - 1); |
| 470 | |
| 471 | for (; i < nq; i++) |
| 472 | { |
| 473 | rx_queue = vec_elt_at_index (apif->rx_queues, i); |
| 474 | tx_queue = vec_elt_at_index (apif->tx_queues, i); |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 475 | ret = af_packet_queue_init (vm, apif, args, rx_queue, tx_queue, i); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 476 | if (ret != 0) |
| 477 | goto error; |
| 478 | } |
| 479 | |
| 480 | if (args->num_rxqs > args->num_txqs) |
| 481 | { |
| 482 | for (; i < args->num_rxqs; i++) |
| 483 | { |
| 484 | rx_queue = vec_elt_at_index (apif->rx_queues, i); |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 485 | ret = af_packet_queue_init (vm, apif, args, rx_queue, 0, i); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 486 | if (ret != 0) |
| 487 | goto error; |
| 488 | } |
| 489 | } |
| 490 | else if (args->num_txqs > args->num_rxqs) |
| 491 | { |
| 492 | for (; i < args->num_txqs; i++) |
| 493 | { |
| 494 | tx_queue = vec_elt_at_index (apif->tx_queues, i); |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 495 | ret = af_packet_queue_init (vm, apif, args, 0, tx_queue, i); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 496 | if (ret != 0) |
| 497 | goto error; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | apif->num_rxqs = args->num_rxqs; |
| 502 | apif->num_txqs = args->num_txqs; |
| 503 | |
| 504 | return 0; |
| 505 | error: |
| 506 | vlib_log_err (apm->log_class, "Failed to init device error"); |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | int |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 511 | af_packet_create_if (af_packet_create_if_arg_t *arg) |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 512 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 513 | af_packet_main_t *apm = &af_packet_main; |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 514 | vlib_main_t *vm = vlib_get_main (); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 515 | int fd2 = -1; |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 516 | struct ifreq ifr; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 517 | af_packet_if_t *apif = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 518 | u8 hw_addr[6]; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 519 | vnet_sw_interface_t *sw; |
| 520 | vnet_main_t *vnm = vnet_get_main (); |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 521 | vnet_hw_if_caps_t caps = VNET_HW_IF_CAP_INT_MODE; |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 522 | uword *p; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 523 | uword if_index; |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 524 | u8 *host_if_name_dup = 0; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 525 | int host_if_index = -1; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 526 | int ret = 0; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 527 | |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 528 | p = mhash_get (&apm->if_index_by_host_if_name, arg->host_if_name); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 529 | if (p) |
| 530 | { |
Mohsin Kazmi | 43fc688 | 2018-03-22 23:45:23 +0100 | [diff] [blame] | 531 | apif = vec_elt_at_index (apm->interfaces, p[0]); |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 532 | arg->sw_if_index = apif->sw_if_index; |
Mohsin Kazmi | 43fc688 | 2018-03-22 23:45:23 +0100 | [diff] [blame] | 533 | return VNET_API_ERROR_IF_ALREADY_EXISTS; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 536 | host_if_name_dup = vec_dup (arg->host_if_name); |
| 537 | |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 538 | /* |
| 539 | * make sure host side of interface is 'UP' before binding AF_PACKET |
| 540 | * socket on it. |
| 541 | */ |
| 542 | if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0) |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 543 | { |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 544 | vlib_log_debug (apm->log_class, |
| 545 | "Failed to create AF_UNIX socket: %s (errno %d)", |
| 546 | strerror (errno), errno); |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 547 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 548 | goto error; |
| 549 | } |
| 550 | |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 551 | clib_memcpy (ifr.ifr_name, (const char *) arg->host_if_name, |
| 552 | vec_len (arg->host_if_name)); |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 553 | if (ioctl (fd2, SIOCGIFINDEX, &ifr) < 0) |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 554 | { |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 555 | vlib_log_debug ( |
| 556 | apm->log_class, |
| 557 | "Failed to retrieve the interface (%s) index: %s (errno %d)", |
| 558 | arg->host_if_name, strerror (errno), errno); |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 559 | ret = VNET_API_ERROR_INVALID_INTERFACE; |
| 560 | goto error; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 563 | host_if_index = ifr.ifr_ifindex; |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 564 | if (ioctl (fd2, SIOCGIFFLAGS, &ifr) < 0) |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 565 | { |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 566 | vlib_log_debug (apm->log_class, |
| 567 | "Failed to get the active flag: %s (errno %d)", |
| 568 | strerror (errno), errno); |
| 569 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 570 | goto error; |
| 571 | } |
| 572 | |
| 573 | if (!(ifr.ifr_flags & IFF_UP)) |
| 574 | { |
| 575 | ifr.ifr_flags |= IFF_UP; |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 576 | if (ioctl (fd2, SIOCSIFFLAGS, &ifr) < 0) |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 577 | { |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 578 | vlib_log_debug (apm->log_class, |
| 579 | "Failed to set the active flag: %s (errno %d)", |
| 580 | strerror (errno), errno); |
| 581 | ret = VNET_API_ERROR_SYSCALL_ERROR_1; |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 582 | goto error; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | if (fd2 > -1) |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 587 | { |
| 588 | close (fd2); |
| 589 | fd2 = -1; |
| 590 | } |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 591 | |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 592 | ret = is_bridge (arg->host_if_name); |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 593 | if (ret == 0) /* is a bridge, ignore state */ |
| 594 | host_if_index = -1; |
| 595 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 596 | /* So far everything looks good, let's create interface */ |
Damjan Marion | 048ee2e | 2016-03-16 22:59:21 +0100 | [diff] [blame] | 597 | pool_get (apm->interfaces, apif); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 598 | if_index = apif - apm->interfaces; |
| 599 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 600 | apif->dev_instance = if_index; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 601 | apif->host_if_index = host_if_index; |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 602 | apif->host_if_name = host_if_name_dup; |
Dave Barach | 13f3c45 | 2016-03-29 11:56:41 -0400 | [diff] [blame] | 603 | apif->per_interface_next_index = ~0; |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 604 | apif->mode = arg->mode; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 605 | |
| 606 | ret = af_packet_device_init (vm, apif, arg); |
| 607 | if (ret != 0) |
| 608 | goto error; |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 609 | |
Nathan Skrzypczak | ffc6bdc | 2021-02-01 17:13:59 +0100 | [diff] [blame] | 610 | ret = af_packet_read_mtu (apif); |
| 611 | if (ret != 0) |
| 612 | goto error; |
| 613 | |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 614 | |
Nathan Skrzypczak | b225b0a | 2021-10-26 16:11:38 +0200 | [diff] [blame] | 615 | if (apif->mode != AF_PACKET_IF_MODE_IP) |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 616 | { |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 617 | vnet_eth_interface_registration_t eir = {}; |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 618 | /*use configured or generate random MAC address */ |
| 619 | if (arg->hw_addr) |
| 620 | clib_memcpy (hw_addr, arg->hw_addr, 6); |
| 621 | else |
| 622 | { |
| 623 | f64 now = vlib_time_now (vm); |
| 624 | u32 rnd; |
| 625 | rnd = (u32) (now * 1e6); |
| 626 | rnd = random_u32 (&rnd); |
| 627 | |
| 628 | clib_memcpy (hw_addr + 2, &rnd, sizeof (rnd)); |
| 629 | hw_addr[0] = 2; |
| 630 | hw_addr[1] = 0xfe; |
| 631 | } |
| 632 | |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 633 | eir.dev_class_index = af_packet_device_class.index; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 634 | eir.dev_instance = apif->dev_instance; |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 635 | eir.address = hw_addr; |
Damjan Marion | 1cd0e5d | 2022-01-17 14:49:17 +0100 | [diff] [blame] | 636 | eir.cb.set_max_frame_size = af_packet_eth_set_max_frame_size; |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 637 | apif->hw_if_index = vnet_eth_register_interface (vnm, &eir); |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 638 | } |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 639 | else |
| 640 | { |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 641 | apif->hw_if_index = vnet_register_interface ( |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 642 | vnm, af_packet_device_class.index, apif->dev_instance, |
| 643 | af_packet_ip_device_hw_interface_class.index, apif->dev_instance); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 644 | } |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 645 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 646 | sw = vnet_get_hw_sw_interface (vnm, apif->hw_if_index); |
| 647 | apif->sw_if_index = sw->sw_if_index; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 648 | |
| 649 | af_packet_set_rx_queues (vm, apif); |
| 650 | af_packet_set_tx_queues (vm, apif); |
Damjan Marion | 4403690 | 2017-04-28 12:29:15 +0200 | [diff] [blame] | 651 | |
Mohsin Kazmi | c73984a | 2022-04-05 13:08:53 +0000 | [diff] [blame^] | 652 | if (arg->flags & AF_PACKET_IF_FLAGS_FANOUT) |
| 653 | apif->is_fanout_enabled = 1; |
| 654 | |
Mohsin Kazmi | 2b6479c | 2022-04-05 12:03:47 +0000 | [diff] [blame] | 655 | apif->is_qdisc_bypass_enabled = |
| 656 | (arg->flags & AF_PACKET_IF_FLAGS_QDISC_BYPASS); |
| 657 | |
Mohsin Kazmi | 788676b | 2022-04-05 12:43:13 +0000 | [diff] [blame] | 658 | if (arg->flags & AF_PACKET_IF_FLAGS_CKSUM_GSO) |
| 659 | apif->is_cksum_gso_enabled = 1; |
| 660 | |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 661 | if (apif->is_cksum_gso_enabled) |
| 662 | caps |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_IP4_CKSUM | |
| 663 | VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM; |
| 664 | |
| 665 | vnet_hw_if_set_caps (vnm, apif->hw_if_index, caps); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 666 | vnet_hw_interface_set_flags (vnm, apif->hw_if_index, |
| 667 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 668 | |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 669 | mhash_set_mem (&apm->if_index_by_host_if_name, host_if_name_dup, &if_index, |
| 670 | 0); |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 671 | arg->sw_if_index = apif->sw_if_index; |
Mohsin KAZMI | cf751ec | 2017-01-18 11:59:45 +0100 | [diff] [blame] | 672 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 673 | return 0; |
| 674 | |
| 675 | error: |
Mohsin Kazmi | a50a14c | 2018-04-25 15:58:05 +0200 | [diff] [blame] | 676 | if (fd2 > -1) |
jackiechen1985 | 7fa4160 | 2019-05-07 18:59:13 +0800 | [diff] [blame] | 677 | { |
| 678 | close (fd2); |
| 679 | fd2 = -1; |
| 680 | } |
Ivan Kelly | bfe737a | 2016-10-07 18:02:43 +0200 | [diff] [blame] | 681 | vec_free (host_if_name_dup); |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 682 | memset (apif, 0, sizeof (*apif)); |
| 683 | pool_put (apm->interfaces, apif); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 684 | return ret; |
| 685 | } |
| 686 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 687 | static int |
| 688 | af_packet_rx_queue_free (af_packet_if_t *apif, af_packet_queue_t *rx_queue) |
| 689 | { |
| 690 | clib_file_del_by_index (&file_main, rx_queue->clib_file_index); |
| 691 | close (rx_queue->fd); |
| 692 | rx_queue->fd = -1; |
| 693 | rx_queue->rx_ring = NULL; |
| 694 | vec_free (rx_queue->rx_req); |
| 695 | rx_queue->rx_req = NULL; |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int |
| 700 | af_packet_tx_queue_free (af_packet_if_t *apif, af_packet_queue_t *tx_queue) |
| 701 | { |
| 702 | close (tx_queue->fd); |
| 703 | tx_queue->fd = -1; |
| 704 | clib_spinlock_free (&tx_queue->lockp); |
| 705 | tx_queue->tx_ring = NULL; |
| 706 | vec_free (tx_queue->tx_req); |
| 707 | tx_queue->tx_req = NULL; |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static int |
| 712 | af_packet_ring_free (af_packet_if_t *apif, af_packet_ring_t *ring) |
| 713 | { |
| 714 | af_packet_main_t *apm = &af_packet_main; |
| 715 | |
| 716 | if (ring) |
| 717 | { |
| 718 | // FIXME: unmap the memory |
| 719 | if (munmap (ring->ring_start_addr, ring->ring_size)) |
| 720 | vlib_log_warn (apm->log_class, |
| 721 | "Host interface %s could not free ring %p of size %u", |
| 722 | apif->host_if_name, ring->ring_start_addr, |
| 723 | ring->ring_size); |
| 724 | else |
| 725 | ring->ring_start_addr = 0; |
| 726 | } |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 731 | int |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 732 | af_packet_delete_if (u8 *host_if_name) |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 733 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 734 | vnet_main_t *vnm = vnet_get_main (); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 735 | af_packet_main_t *apm = &af_packet_main; |
| 736 | af_packet_if_t *apif; |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 737 | af_packet_queue_t *rx_queue; |
| 738 | af_packet_queue_t *tx_queue; |
| 739 | af_packet_ring_t *ring; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 740 | uword *p; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 741 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 742 | p = mhash_get (&apm->if_index_by_host_if_name, host_if_name); |
| 743 | if (p == NULL) |
| 744 | { |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 745 | vlib_log_warn (apm->log_class, "Host interface %s does not exist", |
| 746 | host_if_name); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 747 | return VNET_API_ERROR_SYSCALL_ERROR_1; |
| 748 | } |
| 749 | apif = pool_elt_at_index (apm->interfaces, p[0]); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 750 | |
| 751 | /* bring down the interface */ |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 752 | vnet_hw_interface_set_flags (vnm, apif->hw_if_index, 0); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 753 | |
| 754 | /* clean up */ |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 755 | vec_foreach (rx_queue, apif->rx_queues) |
| 756 | af_packet_rx_queue_free (apif, rx_queue); |
| 757 | vec_foreach (tx_queue, apif->tx_queues) |
| 758 | af_packet_tx_queue_free (apif, tx_queue); |
| 759 | vec_foreach (ring, apif->rings) |
| 760 | af_packet_ring_free (apif, ring); |
Eyal Bari | f298ecf | 2016-09-19 18:47:39 +0300 | [diff] [blame] | 761 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 762 | vec_free (apif->rx_queues); |
| 763 | apif->rx_queues = NULL; |
| 764 | vec_free (apif->tx_queues); |
| 765 | apif->tx_queues = NULL; |
| 766 | vec_free (apif->rings); |
| 767 | apif->rings = NULL; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 768 | |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 769 | vec_free (apif->host_if_name); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 770 | apif->host_if_name = NULL; |
Ray Kinsella | c855b73 | 2017-04-21 12:24:43 +0100 | [diff] [blame] | 771 | apif->host_if_index = -1; |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 772 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 773 | mhash_unset (&apm->if_index_by_host_if_name, host_if_name, p); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 774 | |
Nathan Skrzypczak | b225b0a | 2021-10-26 16:11:38 +0200 | [diff] [blame] | 775 | if (apif->mode != AF_PACKET_IF_MODE_IP) |
Mohsin Kazmi | cae84fa | 2021-10-08 15:10:49 +0000 | [diff] [blame] | 776 | ethernet_delete_interface (vnm, apif->hw_if_index); |
| 777 | else |
| 778 | vnet_delete_hw_interface (vnm, apif->hw_if_index); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 779 | |
Mohsin Kazmi | 5a7aa51 | 2022-03-25 14:27:45 +0000 | [diff] [blame] | 780 | memset (apif, 0, sizeof (*apif)); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 781 | pool_put (apm->interfaces, apif); |
Peter Lei | dba76f2 | 2016-04-08 08:16:31 -0700 | [diff] [blame] | 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 786 | int |
Nathan Skrzypczak | 7d0e30b | 2021-06-23 11:28:39 +0200 | [diff] [blame] | 787 | af_packet_set_l4_cksum_offload (u32 sw_if_index, u8 set) |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 788 | { |
Mohsin Kazmi | c1fd17b | 2022-03-22 21:40:04 +0000 | [diff] [blame] | 789 | // deprecated ... |
Jakub Grajciar | 92b0275 | 2017-10-20 13:37:28 +0200 | [diff] [blame] | 790 | return 0; |
| 791 | } |
| 792 | |
Mohsin Kazmi | 04e0bb2 | 2018-05-28 18:55:37 +0200 | [diff] [blame] | 793 | int |
| 794 | af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs) |
| 795 | { |
| 796 | af_packet_main_t *apm = &af_packet_main; |
| 797 | af_packet_if_t *apif; |
| 798 | af_packet_if_detail_t *r_af_packet_ifs = NULL; |
| 799 | af_packet_if_detail_t *af_packet_if = NULL; |
| 800 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 801 | pool_foreach (apif, apm->interfaces) |
| 802 | { |
Jakub Grajciar | 3b2db90 | 2019-08-26 11:25:52 +0200 | [diff] [blame] | 803 | vec_add2 (r_af_packet_ifs, af_packet_if, 1); |
| 804 | af_packet_if->sw_if_index = apif->sw_if_index; |
| 805 | if (apif->host_if_name) |
| 806 | { |
| 807 | clib_memcpy (af_packet_if->host_if_name, apif->host_if_name, |
| 808 | MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1, |
| 809 | strlen ((const char *) apif->host_if_name))); |
| 810 | } |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 811 | } |
Mohsin Kazmi | 04e0bb2 | 2018-05-28 18:55:37 +0200 | [diff] [blame] | 812 | |
| 813 | *out_af_packet_ifs = r_af_packet_ifs; |
| 814 | |
| 815 | return 0; |
| 816 | } |
| 817 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 818 | static clib_error_t * |
| 819 | af_packet_init (vlib_main_t * vm) |
| 820 | { |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 821 | af_packet_main_t *apm = &af_packet_main; |
Damjan Marion | 553f6bd | 2016-09-07 11:54:22 +0200 | [diff] [blame] | 822 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 823 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 824 | clib_memset (apm, 0, sizeof (af_packet_main_t)); |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 825 | |
| 826 | mhash_init_vec_string (&apm->if_index_by_host_if_name, sizeof (uword)); |
| 827 | |
Damjan Marion | 553f6bd | 2016-09-07 11:54:22 +0200 | [diff] [blame] | 828 | vec_validate_aligned (apm->rx_buffers, tm->n_vlib_mains - 1, |
| 829 | CLIB_CACHE_LINE_BYTES); |
| 830 | |
Mohsin Kazmi | acba9f7 | 2018-05-17 15:42:27 +0200 | [diff] [blame] | 831 | apm->log_class = vlib_log_register_class ("af_packet", 0); |
| 832 | vlib_log_debug (apm->log_class, "initialized"); |
| 833 | |
Damjan Marion | 83243a0 | 2016-02-29 13:09:30 +0100 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
| 837 | VLIB_INIT_FUNCTION (af_packet_init); |
Damjan Marion | 00a9dca | 2016-08-17 17:05:46 +0200 | [diff] [blame] | 838 | |
| 839 | /* |
| 840 | * fd.io coding-style-patch-verification: ON |
| 841 | * |
| 842 | * Local Variables: |
| 843 | * eval: (c-set-style "gnu") |
| 844 | * End: |
| 845 | */ |