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