Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 18 | #define _GNU_SOURCE |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
Alexander Chernavin | a6c34a1 | 2020-09-04 09:24:20 -0400 | [diff] [blame] | 21 | #include <sys/socket.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <net/if.h> |
| 24 | #include <linux/if_tun.h> |
| 25 | #include <sys/ioctl.h> |
Alexander Chernavin | a6c34a1 | 2020-09-04 09:24:20 -0400 | [diff] [blame] | 26 | #include <linux/ethtool.h> |
| 27 | #include <linux/sockios.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 28 | #include <sys/eventfd.h> |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 29 | #include <net/if_arp.h> |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 30 | #include <limits.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 31 | |
| 32 | #include <linux/netlink.h> |
| 33 | #include <linux/rtnetlink.h> |
| 34 | |
| 35 | #include <vlib/vlib.h> |
Lijian.Zhang | ba0da57 | 2019-08-21 17:51:16 +0800 | [diff] [blame] | 36 | #include <vlib/physmem.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 37 | #include <vlib/unix/unix.h> |
Nathan Skrzypczak | 4cef6de | 2021-07-19 18:21:43 +0200 | [diff] [blame] | 38 | #include <vppinfra/linux/netns.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 39 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 40 | #include <vnet/ip/ip4_packet.h> |
| 41 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 42 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 43 | #include <vnet/devices/virtio/virtio.h> |
Damjan Marion | c99b4cd | 2017-12-04 15:25:58 +0100 | [diff] [blame] | 44 | #include <vnet/devices/tap/tap.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 45 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 46 | tap_main_t tap_main; |
| 47 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 48 | #define tap_log_err(dev, f, ...) \ |
| 49 | vlib_log (VLIB_LOG_LEVEL_ERR, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) |
| 50 | #define tap_log_dbg(dev, f, ...) \ |
| 51 | vlib_log (VLIB_LOG_LEVEL_DEBUG, tap_main.log_default, "tap%u: " f, dev->dev_instance, ## __VA_ARGS__) |
| 52 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 53 | #define _IOCTL(fd,a,...) \ |
| 54 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 55 | { \ |
| 56 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 57 | tap_log_err (vif, "%U", format_clib_error, err); \ |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 58 | goto error; \ |
| 59 | } |
| 60 | |
Mohsin Kazmi | 0d05c0d | 2021-11-09 17:44:10 +0000 | [diff] [blame] | 61 | VNET_HW_INTERFACE_CLASS (tun_device_hw_interface_class, static) = { |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 62 | .name = "tun-device", |
| 63 | .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, |
Mohsin Kazmi | 0d05c0d | 2021-11-09 17:44:10 +0000 | [diff] [blame] | 64 | .tx_hash_fn_type = VNET_HASH_FN_TYPE_IP, |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 65 | }; |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 66 | |
Neale Ranns | 0cc23b7 | 2021-05-20 16:09:40 +0000 | [diff] [blame] | 67 | #define TUN_MAX_PACKET_BYTES 65355 |
| 68 | #define TUN_MIN_PACKET_BYTES 64 |
| 69 | #define TUN_DEFAULT_PACKET_BYTES 1500 |
| 70 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 71 | static u32 |
| 72 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 73 | u32 flags) |
| 74 | { |
| 75 | /* nothing for now */ |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 76 | //TODO On MTU change call vnet_netlink_set_if_mtu |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Artem Glazychev | 66593a6 | 2022-02-18 12:23:20 +0700 | [diff] [blame^] | 80 | static clib_error_t * |
| 81 | virtio_eth_set_max_frame_size (vnet_main_t *vnm, vnet_hw_interface_t *hi, |
| 82 | u32 frame_size) |
| 83 | { |
| 84 | /* nothing for now */ |
| 85 | return 0; |
| 86 | } |
| 87 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 88 | #define TAP_MAX_INSTANCE 1024 |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 89 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 90 | static void |
| 91 | tap_free (vlib_main_t * vm, virtio_if_t * vif) |
| 92 | { |
| 93 | virtio_main_t *mm = &virtio_main; |
| 94 | tap_main_t *tm = &tap_main; |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 95 | clib_error_t *err = 0; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 96 | int i; |
| 97 | |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 98 | virtio_pre_input_node_disable (vm, vif); |
| 99 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 100 | /* *INDENT-OFF* */ |
| 101 | vec_foreach_index (i, vif->vhost_fds) if (vif->vhost_fds[i] != -1) |
| 102 | close (vif->vhost_fds[i]); |
| 103 | vec_foreach_index (i, vif->rxq_vrings) |
| 104 | virtio_vring_free_rx (vm, vif, RX_QUEUE (i)); |
| 105 | vec_foreach_index (i, vif->txq_vrings) |
| 106 | virtio_vring_free_tx (vm, vif, TX_QUEUE (i)); |
| 107 | /* *INDENT-ON* */ |
| 108 | |
Mohsin Kazmi | 108b15b | 2020-10-28 19:35:53 +0100 | [diff] [blame] | 109 | if (vif->tap_fds) |
| 110 | { |
| 111 | _IOCTL (vif->tap_fds[0], TUNSETPERSIST, (void *) (uintptr_t) 0); |
| 112 | tap_log_dbg (vif, "TUNSETPERSIST: unset"); |
| 113 | } |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 114 | error: |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 115 | vec_foreach_index (i, vif->tap_fds) close (vif->tap_fds[i]); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 116 | |
Mohsin Kazmi | efd967f | 2021-10-01 12:34:42 +0200 | [diff] [blame] | 117 | vec_free (vif->tap_fds); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 118 | vec_free (vif->vhost_fds); |
| 119 | vec_free (vif->rxq_vrings); |
| 120 | vec_free (vif->txq_vrings); |
| 121 | vec_free (vif->host_if_name); |
| 122 | vec_free (vif->net_ns); |
| 123 | vec_free (vif->host_bridge); |
| 124 | clib_error_free (vif->error); |
| 125 | |
| 126 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0); |
| 127 | clib_memset (vif, 0, sizeof (*vif)); |
| 128 | pool_put (mm->interfaces, vif); |
| 129 | } |
| 130 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 131 | void |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 132 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 133 | { |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 134 | vlib_thread_main_t *thm = vlib_get_thread_main (); |
Lijian.Zhang | ba0da57 | 2019-08-21 17:51:16 +0800 | [diff] [blame] | 135 | vlib_physmem_main_t *vpm = &vm->physmem_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 136 | vnet_main_t *vnm = vnet_get_main (); |
| 137 | virtio_main_t *vim = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 138 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 139 | vnet_sw_interface_t *sw; |
| 140 | vnet_hw_interface_t *hw; |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 141 | vnet_hw_if_caps_change_t cc; |
Mohsin Kazmi | 4834a66 | 2020-07-06 18:03:41 +0000 | [diff] [blame] | 142 | int i, num_vhost_queues; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 143 | int old_netns_fd = -1; |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 144 | struct ifreq ifr = {.ifr_flags = IFF_NO_PI | IFF_VNET_HDR }; |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 145 | struct ifreq get_ifr = {.ifr_flags = 0 }; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 146 | size_t hdrsz; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 147 | vhost_memory_t *vhost_mem = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 148 | virtio_if_t *vif = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 149 | clib_error_t *err = 0; |
Damjan Marion | 39807d0 | 2019-11-08 13:52:28 +0100 | [diff] [blame] | 150 | unsigned int tap_features; |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 151 | int tfd = -1, qfd = -1, vfd = -1, nfd = -1; |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 152 | char *host_if_name = 0; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 153 | unsigned int offload = 0; |
Mohsin Kazmi | 4834a66 | 2020-07-06 18:03:41 +0000 | [diff] [blame] | 154 | int sndbuf = 0; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 155 | |
| 156 | if (args->id != ~0) |
| 157 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 158 | if (clib_bitmap_get (tm->tap_ids, args->id)) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 159 | { |
| 160 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 161 | args->error = clib_error_return (0, "interface already exists"); |
| 162 | return; |
| 163 | } |
| 164 | } |
| 165 | else |
| 166 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 167 | args->id = clib_bitmap_first_clear (tm->tap_ids); |
| 168 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 169 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 170 | if (args->id > TAP_MAX_INSTANCE) |
| 171 | { |
| 172 | args->rv = VNET_API_ERROR_UNSPECIFIED; |
| 173 | args->error = clib_error_return (0, "cannot find free interface id"); |
| 174 | return; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 175 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 176 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 177 | pool_get_zero (vim->interfaces, vif); |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 178 | |
| 179 | if (args->tap_flags & TAP_FLAG_TUN) |
| 180 | { |
| 181 | vif->type = VIRTIO_IF_TYPE_TUN; |
| 182 | ifr.ifr_flags |= IFF_TUN; |
Mohsin Kazmi | 4834a66 | 2020-07-06 18:03:41 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * From kernel 4.20, xdp support has been added in tun_sendmsg. |
| 186 | * If sndbuf == INT_MAX, vhost batches the packet and processes |
| 187 | * them using xdp data path for tun driver. It assumes packets |
| 188 | * are ethernet frames (It needs to be fixed). |
| 189 | * To avoid xdp data path in tun driver, sndbuf value should |
| 190 | * be < INT_MAX. |
| 191 | */ |
| 192 | sndbuf = INT_MAX - 1; |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 193 | } |
| 194 | else |
| 195 | { |
| 196 | vif->type = VIRTIO_IF_TYPE_TAP; |
| 197 | ifr.ifr_flags |= IFF_TAP; |
Mohsin Kazmi | 4834a66 | 2020-07-06 18:03:41 +0000 | [diff] [blame] | 198 | sndbuf = INT_MAX; |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 201 | vif->dev_instance = vif - vim->interfaces; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 202 | vif->id = args->id; |
Nathan Skrzypczak | 40edaf6 | 2021-12-15 18:45:59 +0100 | [diff] [blame] | 203 | vif->num_txqs = clib_max (args->num_tx_queues, thm->n_vlib_mains); |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 204 | vif->num_rxqs = clib_max (args->num_rx_queues, 1); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 205 | |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 206 | if (args->tap_flags & TAP_FLAG_ATTACH) |
| 207 | { |
Duncan Eastoe | 764a878 | 2021-09-08 19:11:33 +0100 | [diff] [blame] | 208 | if (args->host_if_name == NULL) |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 209 | { |
| 210 | args->rv = VNET_API_ERROR_NO_MATCHING_INTERFACE; |
| 211 | err = clib_error_return (0, "host_if_name is not provided"); |
| 212 | goto error; |
| 213 | } |
Nathan Skrzypczak | 979545e | 2021-09-22 17:46:02 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* if namespace is specified, all further netlink messages should be executed |
| 217 | * after we change our net namespace */ |
| 218 | if (args->host_namespace) |
| 219 | { |
| 220 | old_netns_fd = clib_netns_open (NULL /* self */); |
| 221 | if ((nfd = clib_netns_open (args->host_namespace)) == -1) |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 222 | { |
Nathan Skrzypczak | 979545e | 2021-09-22 17:46:02 +0200 | [diff] [blame] | 223 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 224 | args->error = clib_error_return_unix (0, "clib_netns_open '%s'", |
| 225 | args->host_namespace); |
| 226 | goto error; |
| 227 | } |
| 228 | if (clib_setns (nfd) == -1) |
| 229 | { |
| 230 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 231 | args->error = |
| 232 | clib_error_return_unix (0, "setns '%s'", args->host_namespace); |
| 233 | goto error; |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 236 | |
Duncan Eastoe | 764a878 | 2021-09-08 19:11:33 +0100 | [diff] [blame] | 237 | if (args->host_if_name != NULL) |
| 238 | { |
| 239 | host_if_name = (char *) args->host_if_name; |
| 240 | clib_memcpy (ifr.ifr_name, host_if_name, |
| 241 | clib_min (IFNAMSIZ, vec_len (host_if_name))); |
| 242 | } |
| 243 | |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 244 | if ((tfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 245 | { |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 246 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 247 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
| 248 | goto error; |
| 249 | } |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 250 | vec_add1 (vif->tap_fds, tfd); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 251 | tap_log_dbg (vif, "open tap fd %d", tfd); |
| 252 | |
| 253 | _IOCTL (tfd, TUNGETFEATURES, &tap_features); |
| 254 | tap_log_dbg (vif, "TUNGETFEATURES: features 0x%lx", tap_features); |
| 255 | if ((tap_features & IFF_VNET_HDR) == 0) |
| 256 | { |
| 257 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 258 | args->error = clib_error_return (0, "vhost-net backend not available"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 259 | goto error; |
| 260 | } |
| 261 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 262 | if ((tap_features & IFF_MULTI_QUEUE) == 0) |
| 263 | { |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 264 | if (vif->num_rxqs > 1) |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 265 | { |
| 266 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 267 | args->error = clib_error_return (0, "multiqueue not supported"); |
| 268 | goto error; |
| 269 | } |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 270 | vif->num_rxqs = vif->num_txqs = 1; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 271 | } |
| 272 | else |
| 273 | ifr.ifr_flags |= IFF_MULTI_QUEUE; |
| 274 | |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 275 | hdrsz = sizeof (virtio_net_hdr_v1_t); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 276 | if (args->tap_flags & TAP_FLAG_GSO) |
| 277 | { |
| 278 | offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 279 | vif->gso_enabled = 1; |
| 280 | } |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 281 | else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD) |
| 282 | { |
| 283 | offload = TUN_F_CSUM; |
| 284 | vif->csum_offload_enabled = 1; |
| 285 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 286 | |
| 287 | _IOCTL (tfd, TUNSETIFF, (void *) &ifr); |
| 288 | tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", tfd, |
| 289 | ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags); |
| 290 | |
| 291 | vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); |
| 292 | tap_log_dbg (vif, "ifindex %d", vif->ifindex); |
| 293 | |
| 294 | if (!args->host_if_name) |
| 295 | host_if_name = ifr.ifr_ifrn.ifrn_name; |
| 296 | else |
| 297 | host_if_name = (char *) args->host_if_name; |
| 298 | |
Mohsin Kazmi | b49bc1a | 2020-02-14 17:51:04 +0000 | [diff] [blame] | 299 | /* |
| 300 | * unset the persistence when attaching to existing |
| 301 | * interface |
| 302 | */ |
| 303 | if (args->tap_flags & TAP_FLAG_ATTACH) |
| 304 | { |
| 305 | _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 0); |
| 306 | tap_log_dbg (vif, "TUNSETPERSIST: unset"); |
| 307 | } |
| 308 | |
| 309 | /* set the persistence */ |
| 310 | if (args->tap_flags & TAP_FLAG_PERSIST) |
| 311 | { |
| 312 | _IOCTL (tfd, TUNSETPERSIST, (void *) (uintptr_t) 1); |
| 313 | tap_log_dbg (vif, "TUNSETPERSIST: set"); |
| 314 | |
| 315 | /* verify persistence is set, read the flags */ |
| 316 | _IOCTL (tfd, TUNGETIFF, (void *) &get_ifr); |
| 317 | tap_log_dbg (vif, "TUNGETIFF: flags 0x%lx", get_ifr.ifr_flags); |
| 318 | if ((get_ifr.ifr_flags & IFF_PERSIST) == 0) |
| 319 | { |
| 320 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 321 | args->error = clib_error_return (0, "persistence not supported"); |
| 322 | goto error; |
| 323 | } |
| 324 | } |
| 325 | |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 326 | /* create additional queues on the linux side. |
| 327 | * we create as many linux queue pairs as we have rx queues |
| 328 | */ |
| 329 | for (i = 1; i < vif->num_rxqs; i++) |
| 330 | { |
| 331 | if ((qfd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 332 | { |
| 333 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 334 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
| 335 | goto error; |
| 336 | } |
| 337 | _IOCTL (qfd, TUNSETIFF, (void *) &ifr); |
| 338 | tap_log_dbg (vif, "TUNSETIFF fd %d name %s flags 0x%x", qfd, |
| 339 | ifr.ifr_ifrn.ifrn_name, ifr.ifr_flags); |
| 340 | vec_add1 (vif->tap_fds, qfd); |
| 341 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 342 | |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 343 | for (i = 0; i < vif->num_rxqs; i++) |
| 344 | { |
| 345 | tap_log_dbg (vif, "TUNSETVNETHDRSZ: fd %d vnet_hdr_sz %u", |
| 346 | vif->tap_fds[i], hdrsz); |
| 347 | _IOCTL (vif->tap_fds[i], TUNSETVNETHDRSZ, &hdrsz); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 348 | |
Mohsin Kazmi | 4834a66 | 2020-07-06 18:03:41 +0000 | [diff] [blame] | 349 | tap_log_dbg (vif, "TUNSETSNDBUF: fd %d sndbuf %d", vif->tap_fds[i], |
| 350 | sndbuf); |
| 351 | _IOCTL (vif->tap_fds[i], TUNSETSNDBUF, &sndbuf); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 352 | |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 353 | tap_log_dbg (vif, "TUNSETOFFLOAD: fd %d offload 0x%lx", vif->tap_fds[i], |
| 354 | offload); |
| 355 | _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload); |
| 356 | |
| 357 | if (fcntl (vif->tap_fds[i], F_SETFL, O_NONBLOCK) < 0) |
| 358 | { |
| 359 | err = clib_error_return_unix (0, "fcntl(tfd, F_SETFL, O_NONBLOCK)"); |
| 360 | tap_log_err (vif, "set nonblocking: %U", format_clib_error, err); |
| 361 | goto error; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* open as many vhost-net fds as required and set ownership */ |
| 366 | num_vhost_queues = clib_max (vif->num_rxqs, vif->num_txqs); |
| 367 | for (i = 0; i < num_vhost_queues; i++) |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 368 | { |
| 369 | if ((vfd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 370 | { |
| 371 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 372 | args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); |
| 373 | goto error; |
| 374 | } |
| 375 | vec_add1 (vif->vhost_fds, vfd); |
| 376 | virtio_log_debug (vif, "open vhost-net fd %d qpair %u", vfd, i); |
| 377 | _IOCTL (vfd, VHOST_SET_OWNER, 0); |
| 378 | virtio_log_debug (vif, "VHOST_SET_OWNER: fd %u", vfd); |
| 379 | } |
| 380 | |
| 381 | _IOCTL (vif->vhost_fds[0], VHOST_GET_FEATURES, &vif->remote_features); |
| 382 | virtio_log_debug (vif, "VHOST_GET_FEATURES: features 0x%lx", |
| 383 | vif->remote_features); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 384 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 385 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 386 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 387 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 388 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 389 | "VIRTIO_NET_F_MRG_RXBUF feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 390 | goto error; |
| 391 | } |
| 392 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 393 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 394 | 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 395 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 396 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 397 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 398 | "VIRTIO_RING_F_INDIRECT_DESC feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 399 | goto error; |
| 400 | } |
| 401 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 402 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 403 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 404 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 405 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 406 | "VIRTIO_F_VERSION_1 features"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 407 | goto error; |
| 408 | } |
| 409 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 410 | vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF); |
| 411 | vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
| 412 | vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 413 | |
| 414 | virtio_set_net_hdr_size (vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 415 | |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 416 | if (vif->type == VIRTIO_IF_TYPE_TAP) |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 417 | { |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 418 | if (ethernet_mac_address_is_zero (args->host_mac_addr.bytes)) |
| 419 | ethernet_mac_address_generate (args->host_mac_addr.bytes); |
| 420 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 421 | args->host_mac_addr.bytes); |
| 422 | if (args->error) |
| 423 | { |
| 424 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 425 | goto error; |
| 426 | } |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 427 | |
Mohsin Kazmi | add4a41 | 2020-06-26 13:47:21 +0000 | [diff] [blame] | 428 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 429 | { |
Mohsin Kazmi | add4a41 | 2020-06-26 13:47:21 +0000 | [diff] [blame] | 430 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 431 | (char *) |
| 432 | args->host_bridge); |
| 433 | if (args->error) |
| 434 | { |
| 435 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 436 | goto error; |
| 437 | } |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
| 441 | if (args->host_ip4_prefix_len) |
| 442 | { |
| 443 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 444 | &args->host_ip4_addr, |
| 445 | args->host_ip4_prefix_len); |
| 446 | if (args->error) |
| 447 | { |
| 448 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 449 | goto error; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if (args->host_ip6_prefix_len) |
| 454 | { |
| 455 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 456 | &args->host_ip6_addr, |
| 457 | args->host_ip6_prefix_len); |
| 458 | if (args->error) |
| 459 | { |
| 460 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 461 | goto error; |
| 462 | } |
| 463 | } |
| 464 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 465 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 466 | if (args->error) |
| 467 | { |
| 468 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 469 | goto error; |
| 470 | } |
| 471 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 472 | if (args->host_ip4_gw_set) |
| 473 | { |
| 474 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 475 | if (args->error) |
| 476 | { |
| 477 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 478 | goto error; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (args->host_ip6_gw_set) |
| 483 | { |
| 484 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 485 | if (args->error) |
| 486 | { |
| 487 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 488 | goto error; |
| 489 | } |
| 490 | } |
| 491 | |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 492 | if (args->host_mtu_set) |
| 493 | { |
| 494 | args->error = |
| 495 | vnet_netlink_set_link_mtu (vif->ifindex, args->host_mtu_size); |
| 496 | if (args->error) |
| 497 | { |
| 498 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 499 | goto error; |
| 500 | } |
| 501 | } |
| 502 | else if (tm->host_mtu_size != 0) |
| 503 | { |
| 504 | args->error = |
| 505 | vnet_netlink_set_link_mtu (vif->ifindex, tm->host_mtu_size); |
| 506 | if (args->error) |
| 507 | { |
| 508 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 509 | goto error; |
| 510 | } |
| 511 | args->host_mtu_set = 1; |
| 512 | args->host_mtu_size = tm->host_mtu_size; |
| 513 | } |
| 514 | |
Mohsin Kazmi | 91592c0 | 2020-01-30 16:08:08 +0100 | [diff] [blame] | 515 | /* switch back to old net namespace */ |
| 516 | if (args->host_namespace) |
| 517 | { |
Nathan Skrzypczak | 4cef6de | 2021-07-19 18:21:43 +0200 | [diff] [blame] | 518 | if (clib_setns (old_netns_fd) == -1) |
Mohsin Kazmi | 91592c0 | 2020-01-30 16:08:08 +0100 | [diff] [blame] | 519 | { |
| 520 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 521 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 522 | args->host_namespace); |
| 523 | goto error; |
| 524 | } |
| 525 | } |
| 526 | |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 527 | for (i = 0; i < num_vhost_queues; i++) |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 528 | { |
| 529 | if (i < vif->num_rxqs && (args->error = |
| 530 | virtio_vring_init (vm, vif, RX_QUEUE (i), |
| 531 | args->rx_ring_sz))) |
| 532 | { |
| 533 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 534 | goto error; |
| 535 | } |
| 536 | |
| 537 | if (i < vif->num_txqs && (args->error = |
| 538 | virtio_vring_init (vm, vif, TX_QUEUE (i), |
| 539 | args->tx_ring_sz))) |
| 540 | { |
| 541 | args->rv = VNET_API_ERROR_INIT_FAILED; |
| 542 | goto error; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /* setup features and memtable */ |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 547 | i = sizeof (vhost_memory_t) + sizeof (vhost_memory_region_t); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 548 | vhost_mem = clib_mem_alloc (i); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 549 | clib_memset (vhost_mem, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 550 | vhost_mem->nregions = 1; |
Lijian.Zhang | ba0da57 | 2019-08-21 17:51:16 +0800 | [diff] [blame] | 551 | vhost_mem->regions[0].memory_size = vpm->max_size; |
| 552 | vhost_mem->regions[0].guest_phys_addr = vpm->base_addr; |
| 553 | vhost_mem->regions[0].userspace_addr = |
| 554 | vhost_mem->regions[0].guest_phys_addr; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 555 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 556 | for (i = 0; i < vhost_mem->nregions; i++) |
| 557 | virtio_log_debug (vif, "memtable region %u memory_size 0x%lx " |
| 558 | "guest_phys_addr 0x%lx userspace_addr 0x%lx", i, |
| 559 | vhost_mem->regions[0].memory_size, |
| 560 | vhost_mem->regions[0].guest_phys_addr, |
| 561 | vhost_mem->regions[0].userspace_addr); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 562 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 563 | |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 564 | for (i = 0; i < num_vhost_queues; i++) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 565 | { |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 566 | int fd = vif->vhost_fds[i]; |
| 567 | _IOCTL (fd, VHOST_SET_FEATURES, &vif->features); |
| 568 | virtio_log_debug (vif, "VHOST_SET_FEATURES: fd %u features 0x%lx", |
| 569 | fd, vif->features); |
| 570 | _IOCTL (fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 571 | virtio_log_debug (vif, "VHOST_SET_MEM_TABLE: fd %u", fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 572 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 573 | |
| 574 | /* finish initializing queue pair */ |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 575 | for (i = 0; i < num_vhost_queues * 2; i++) |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 576 | { |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 577 | vhost_vring_addr_t addr = { 0 }; |
| 578 | vhost_vring_state_t state = { 0 }; |
| 579 | vhost_vring_file_t file = { 0 }; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 580 | virtio_vring_t *vring; |
| 581 | u16 qp = i >> 1; |
| 582 | int fd = vif->vhost_fds[qp]; |
| 583 | |
| 584 | if (i & 1) |
| 585 | { |
| 586 | if (qp >= vif->num_txqs) |
| 587 | continue; |
| 588 | vring = vec_elt_at_index (vif->txq_vrings, qp); |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | if (qp >= vif->num_rxqs) |
| 593 | continue; |
| 594 | vring = vec_elt_at_index (vif->rxq_vrings, qp); |
| 595 | } |
| 596 | |
| 597 | addr.index = state.index = file.index = vring->queue_id & 1; |
| 598 | state.num = vring->size; |
| 599 | virtio_log_debug (vif, "VHOST_SET_VRING_NUM fd %d index %u num %u", fd, |
| 600 | state.index, state.num); |
| 601 | _IOCTL (fd, VHOST_SET_VRING_NUM, &state); |
| 602 | |
| 603 | addr.flags = 0; |
| 604 | addr.desc_user_addr = pointer_to_uword (vring->desc); |
| 605 | addr.avail_user_addr = pointer_to_uword (vring->avail); |
| 606 | addr.used_user_addr = pointer_to_uword (vring->used); |
| 607 | |
| 608 | virtio_log_debug (vif, "VHOST_SET_VRING_ADDR fd %d index %u flags 0x%x " |
| 609 | "desc_user_addr 0x%lx avail_user_addr 0x%lx " |
| 610 | "used_user_addr 0x%lx", fd, addr.index, |
| 611 | addr.flags, addr.desc_user_addr, addr.avail_user_addr, |
| 612 | addr.used_user_addr); |
| 613 | _IOCTL (fd, VHOST_SET_VRING_ADDR, &addr); |
| 614 | |
| 615 | file.fd = vring->call_fd; |
| 616 | virtio_log_debug (vif, "VHOST_SET_VRING_CALL fd %d index %u call_fd %d", |
| 617 | fd, file.index, file.fd); |
| 618 | _IOCTL (fd, VHOST_SET_VRING_CALL, &file); |
| 619 | |
| 620 | file.fd = vring->kick_fd; |
| 621 | virtio_log_debug (vif, "VHOST_SET_VRING_KICK fd %d index %u kick_fd %d", |
| 622 | fd, file.index, file.fd); |
| 623 | _IOCTL (fd, VHOST_SET_VRING_KICK, &file); |
| 624 | |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 625 | file.fd = vif->tap_fds[qp % vif->num_rxqs]; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 626 | virtio_log_debug (vif, "VHOST_NET_SET_BACKEND fd %d index %u tap_fd %d", |
| 627 | fd, file.index, file.fd); |
| 628 | _IOCTL (fd, VHOST_NET_SET_BACKEND, &file); |
| 629 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 630 | |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 631 | if (vif->type == VIRTIO_IF_TYPE_TAP) |
| 632 | { |
| 633 | if (!args->mac_addr_set) |
| 634 | ethernet_mac_address_generate (args->mac_addr.bytes); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 635 | |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 636 | clib_memcpy (vif->mac_addr, args->mac_addr.bytes, 6); |
Matthew Smith | 2b716b1 | 2021-10-22 09:53:44 -0500 | [diff] [blame] | 637 | if (args->host_bridge) |
| 638 | vif->host_bridge = format (0, "%s%c", args->host_bridge, 0); |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 639 | } |
Mohsin Kazmi | 19b697f | 2019-07-29 13:21:17 +0200 | [diff] [blame] | 640 | vif->host_if_name = format (0, "%s%c", host_if_name, 0); |
Matthew Smith | 2b716b1 | 2021-10-22 09:53:44 -0500 | [diff] [blame] | 641 | if (args->host_namespace) |
| 642 | vif->net_ns = format (0, "%s%c", args->host_namespace, 0); |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 643 | vif->host_mtu_size = args->host_mtu_size; |
Mohsin Kazmi | 86f281a | 2020-06-30 15:28:01 +0200 | [diff] [blame] | 644 | vif->tap_flags = args->tap_flags; |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 645 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr.bytes, 6); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 646 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 647 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 648 | if (args->host_ip4_prefix_len) |
| 649 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 650 | if (args->host_ip6_prefix_len) |
| 651 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 652 | |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 653 | if (vif->type != VIRTIO_IF_TYPE_TUN) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 654 | { |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 655 | vnet_eth_interface_registration_t eir = {}; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 656 | |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 657 | eir.dev_class_index = virtio_device_class.index; |
| 658 | eir.dev_instance = vif->dev_instance; |
| 659 | eir.address = vif->mac_addr; |
| 660 | eir.cb.flag_change = virtio_eth_flag_change; |
Artem Glazychev | 66593a6 | 2022-02-18 12:23:20 +0700 | [diff] [blame^] | 661 | eir.cb.set_max_frame_size = virtio_eth_set_max_frame_size; |
Damjan Marion | 5c954c4 | 2022-01-06 20:36:14 +0100 | [diff] [blame] | 662 | vif->hw_if_index = vnet_eth_register_interface (vnm, &eir); |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 663 | } |
| 664 | else |
| 665 | { |
| 666 | vif->hw_if_index = vnet_register_interface |
| 667 | (vnm, virtio_device_class.index, |
| 668 | vif->dev_instance /* device instance */ , |
| 669 | tun_device_hw_interface_class.index, vif->dev_instance); |
| 670 | |
| 671 | } |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 672 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 673 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 674 | vif->sw_if_index = sw->sw_if_index; |
| 675 | args->sw_if_index = vif->sw_if_index; |
Mohsin Kazmi | c5d5327 | 2019-07-01 10:26:43 +0200 | [diff] [blame] | 676 | args->rv = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 677 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 678 | cc.mask = VNET_HW_IF_CAP_INT_MODE | VNET_HW_IF_CAP_TCP_GSO | |
| 679 | VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM; |
| 680 | cc.val = VNET_HW_IF_CAP_INT_MODE; |
| 681 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 682 | if (args->tap_flags & TAP_FLAG_GSO) |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 683 | cc.val |= VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_TX_TCP_CKSUM | |
| 684 | VNET_HW_IF_CAP_TX_UDP_CKSUM; |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 685 | else if (args->tap_flags & TAP_FLAG_CSUM_OFFLOAD) |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 686 | cc.val |= VNET_HW_IF_CAP_TX_TCP_CKSUM | VNET_HW_IF_CAP_TX_UDP_CKSUM; |
| 687 | |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 688 | if ((args->tap_flags & TAP_FLAG_GSO) |
| 689 | && (args->tap_flags & TAP_FLAG_GRO_COALESCE)) |
| 690 | { |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 691 | virtio_set_packet_coalesce (vif); |
| 692 | } |
Neale Ranns | 0cc23b7 | 2021-05-20 16:09:40 +0000 | [diff] [blame] | 693 | if (vif->type == VIRTIO_IF_TYPE_TUN) |
| 694 | { |
Damjan Marion | 1cd0e5d | 2022-01-17 14:49:17 +0100 | [diff] [blame] | 695 | hw->min_frame_size = TUN_MIN_PACKET_BYTES; |
| 696 | vnet_hw_interface_set_mtu ( |
| 697 | vnm, hw->sw_if_index, |
| 698 | args->host_mtu_size ? args->host_mtu_size : TUN_DEFAULT_PACKET_BYTES); |
Neale Ranns | 0cc23b7 | 2021-05-20 16:09:40 +0000 | [diff] [blame] | 699 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 700 | |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 701 | vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 702 | virtio_pre_input_node_enable (vm, vif); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 703 | virtio_vring_set_rx_queues (vm, vif); |
Mohsin Kazmi | b7e4e6d | 2021-12-13 18:32:42 +0000 | [diff] [blame] | 704 | virtio_vring_set_tx_queues (vm, vif); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 705 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 706 | vif->per_interface_next_index = ~0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 707 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 708 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Matthew Smith | bd50ed1 | 2020-07-24 13:38:03 -0500 | [diff] [blame] | 709 | /* |
| 710 | * Host tun/tap driver link carrier state is "up" at creation. The |
| 711 | * driver never changes this unless the backend (VPP) changes it using |
| 712 | * TUNSETCARRIER ioctl(). See tap_set_carrier(). |
| 713 | */ |
| 714 | vif->host_carrier_up = 1; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 715 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 716 | goto done; |
| 717 | |
| 718 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 719 | if (err) |
| 720 | { |
| 721 | ASSERT (args->error == 0); |
| 722 | args->error = err; |
| 723 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 724 | } |
Benoît Ganne | c30d87e | 2019-07-15 17:16:49 +0200 | [diff] [blame] | 725 | |
Mohsin Kazmi | 91592c0 | 2020-01-30 16:08:08 +0100 | [diff] [blame] | 726 | tap_log_err (vif, "%U", format_clib_error, args->error); |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 727 | tap_free (vm, vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 728 | done: |
| 729 | if (vhost_mem) |
| 730 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 731 | if (old_netns_fd != -1) |
Nathan Skrzypczak | 979545e | 2021-09-22 17:46:02 +0200 | [diff] [blame] | 732 | { |
| 733 | /* in case we errored with a switched netns */ |
| 734 | clib_setns (old_netns_fd); |
| 735 | close (old_netns_fd); |
| 736 | } |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 737 | if (nfd != -1) |
| 738 | close (nfd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | int |
| 742 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 743 | { |
| 744 | vnet_main_t *vnm = vnet_get_main (); |
| 745 | virtio_main_t *mm = &virtio_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 746 | virtio_if_t *vif; |
| 747 | vnet_hw_interface_t *hw; |
| 748 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 749 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 750 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 751 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 752 | |
| 753 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 754 | |
Matthew Smith | 3d18fbf | 2020-04-29 11:17:08 -0500 | [diff] [blame] | 755 | if ((vif->type != VIRTIO_IF_TYPE_TAP) && (vif->type != VIRTIO_IF_TYPE_TUN)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 756 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 757 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 758 | /* bring down the interface */ |
| 759 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 760 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
| 761 | |
Matthew Smith | 3d18fbf | 2020-04-29 11:17:08 -0500 | [diff] [blame] | 762 | if (vif->type == VIRTIO_IF_TYPE_TAP) |
| 763 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 764 | else /* VIRTIO_IF_TYPE_TUN */ |
| 765 | vnet_delete_hw_interface (vnm, vif->hw_if_index); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 766 | vif->hw_if_index = ~0; |
| 767 | |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 768 | tap_free (vm, vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 769 | |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | int |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 774 | tap_csum_offload_enable_disable (vlib_main_t * vm, u32 sw_if_index, |
| 775 | int enable_disable) |
| 776 | { |
| 777 | vnet_main_t *vnm = vnet_get_main (); |
| 778 | virtio_main_t *mm = &virtio_main; |
| 779 | virtio_if_t *vif; |
| 780 | vnet_hw_interface_t *hw; |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 781 | vnet_hw_if_caps_change_t cc; |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 782 | clib_error_t *err = 0; |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 783 | int i = 0; |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 784 | |
| 785 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
| 786 | |
| 787 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 788 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 789 | |
| 790 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 791 | |
| 792 | const unsigned int csum_offload_on = TUN_F_CSUM; |
| 793 | const unsigned int csum_offload_off = 0; |
| 794 | unsigned int offload = enable_disable ? csum_offload_on : csum_offload_off; |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 795 | vec_foreach_index (i, vif->tap_fds) |
| 796 | _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload); |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 797 | vif->gso_enabled = 0; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 798 | vif->packet_coalesce = 0; |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 799 | |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 800 | cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM; |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 801 | if (enable_disable) |
| 802 | { |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 803 | cc.val = VNET_HW_IF_CAP_L4_TX_CKSUM; |
| 804 | vif->csum_offload_enabled = 1; |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 805 | } |
| 806 | else |
| 807 | { |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 808 | cc.val = 0; |
| 809 | vif->csum_offload_enabled = 0; |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 810 | } |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 811 | vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); |
Mohsin Kazmi | ba0061f | 2019-12-18 17:08:54 +0100 | [diff] [blame] | 812 | |
| 813 | error: |
| 814 | if (err) |
| 815 | { |
| 816 | clib_warning ("Error %s checksum offload on sw_if_index %d", |
| 817 | enable_disable ? "enabling" : "disabling", sw_if_index); |
| 818 | return VNET_API_ERROR_SYSCALL_ERROR_3; |
| 819 | } |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | int |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 824 | tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable, |
| 825 | int is_packet_coalesce) |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 826 | { |
| 827 | vnet_main_t *vnm = vnet_get_main (); |
| 828 | virtio_main_t *mm = &virtio_main; |
| 829 | virtio_if_t *vif; |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 830 | vnet_hw_interface_t *hw; |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 831 | vnet_hw_if_caps_change_t cc; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 832 | clib_error_t *err = 0; |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 833 | int i = 0; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 834 | |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 835 | hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index); |
| 836 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 837 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 838 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 839 | |
| 840 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 841 | |
| 842 | const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 843 | const unsigned int gso_off = 0; |
| 844 | unsigned int offload = enable_disable ? gso_on : gso_off; |
Aloys Augustin | 2857e78 | 2020-03-16 17:29:52 +0100 | [diff] [blame] | 845 | vec_foreach_index (i, vif->tap_fds) |
| 846 | _IOCTL (vif->tap_fds[i], TUNSETOFFLOAD, offload); |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 847 | |
| 848 | cc.mask = VNET_HW_IF_CAP_TCP_GSO | VNET_HW_IF_CAP_L4_TX_CKSUM; |
| 849 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 850 | if (enable_disable) |
| 851 | { |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 852 | cc.val = cc.mask; |
| 853 | vif->gso_enabled = 1; |
| 854 | vif->csum_offload_enabled = 1; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 855 | if (is_packet_coalesce) |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 856 | virtio_set_packet_coalesce (vif); |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 857 | } |
| 858 | else |
| 859 | { |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 860 | cc.val = 0; |
| 861 | vif->gso_enabled = 0; |
| 862 | vif->csum_offload_enabled = 0; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 863 | vif->packet_coalesce = 0; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 864 | } |
Damjan Marion | c48ec5d | 2022-01-10 19:38:57 +0000 | [diff] [blame] | 865 | vnet_hw_if_change_caps (vnm, vif->hw_if_index, &cc); |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 866 | |
| 867 | error: |
| 868 | if (err) |
| 869 | { |
| 870 | clib_warning ("Error %s gso on sw_if_index %d", |
| 871 | enable_disable ? "enabling" : "disabling", sw_if_index); |
| 872 | return VNET_API_ERROR_SYSCALL_ERROR_3; |
| 873 | } |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | int |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 878 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 879 | { |
| 880 | vnet_main_t *vnm = vnet_get_main (); |
| 881 | virtio_main_t *mm = &virtio_main; |
| 882 | virtio_if_t *vif; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 883 | virtio_vring_t *vring; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 884 | vnet_hw_interface_t *hi; |
| 885 | tap_interface_details_t *r_tapids = NULL; |
| 886 | tap_interface_details_t *tapid = NULL; |
| 887 | |
| 888 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 889 | pool_foreach (vif, mm->interfaces) { |
Mohsin Kazmi | 86f281a | 2020-06-30 15:28:01 +0200 | [diff] [blame] | 890 | if ((vif->type != VIRTIO_IF_TYPE_TAP) |
| 891 | && (vif->type != VIRTIO_IF_TYPE_TUN)) |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 892 | continue; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 893 | vec_add2(r_tapids, tapid, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 894 | clib_memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 895 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 896 | tapid->sw_if_index = vif->sw_if_index; |
| 897 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 898 | clib_memcpy(tapid->dev_name, hi->name, |
Vladimir Isaev | 84f3d9f | 2020-09-18 14:43:29 +0300 | [diff] [blame] | 899 | MIN (ARRAY_LEN (tapid->dev_name) - 1, vec_len (hi->name))); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 900 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0)); |
| 901 | tapid->rx_ring_sz = vring->size; |
| 902 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0)); |
| 903 | tapid->tx_ring_sz = vring->size; |
Mohsin Kazmi | 86f281a | 2020-06-30 15:28:01 +0200 | [diff] [blame] | 904 | tapid->tap_flags = vif->tap_flags; |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 905 | clib_memcpy(&tapid->host_mac_addr, vif->host_mac_addr, 6); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 906 | if (vif->host_if_name) |
| 907 | { |
| 908 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 909 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
Mohsin Kazmi | 03b76a2 | 2020-10-08 17:44:36 +0200 | [diff] [blame] | 910 | vec_len (vif->host_if_name))); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 911 | } |
| 912 | if (vif->net_ns) |
| 913 | { |
| 914 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 915 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
Mohsin Kazmi | 03b76a2 | 2020-10-08 17:44:36 +0200 | [diff] [blame] | 916 | vec_len (vif->net_ns))); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 917 | } |
| 918 | if (vif->host_bridge) |
| 919 | { |
| 920 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 921 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
Mohsin Kazmi | 03b76a2 | 2020-10-08 17:44:36 +0200 | [diff] [blame] | 922 | vec_len (vif->host_bridge))); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 923 | } |
| 924 | if (vif->host_ip4_prefix_len) |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 925 | clib_memcpy(tapid->host_ip4_addr.as_u8, &vif->host_ip4_addr, 4); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 926 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 927 | if (vif->host_ip6_prefix_len) |
Jakub Grajciar | 5de4fb7 | 2019-09-03 10:40:01 +0200 | [diff] [blame] | 928 | clib_memcpy(tapid->host_ip6_addr.as_u8, &vif->host_ip6_addr, 16); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 929 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 930 | tapid->host_mtu_size = vif->host_mtu_size; |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 931 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 932 | /* *INDENT-ON* */ |
| 933 | |
| 934 | *out_tapids = r_tapids; |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
Matthew Smith | bd50ed1 | 2020-07-24 13:38:03 -0500 | [diff] [blame] | 939 | /* |
| 940 | * Set host tap/tun interface carrier state so it will appear to host |
| 941 | * applications that the interface's link state changed. |
| 942 | * |
| 943 | * If the kernel we're building against does not have support for the |
| 944 | * TUNSETCARRIER ioctl command, do nothing. |
| 945 | */ |
| 946 | int |
| 947 | tap_set_carrier (u32 hw_if_index, u32 carrier_up) |
| 948 | { |
| 949 | int ret = 0; |
| 950 | #ifdef TUNSETCARRIER |
| 951 | vnet_main_t *vnm = vnet_get_main (); |
| 952 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 953 | virtio_main_t *mm = &virtio_main; |
| 954 | virtio_if_t *vif; |
| 955 | int *fd; |
| 956 | |
| 957 | vif = pool_elt_at_index (mm->interfaces, hi->dev_instance); |
| 958 | vec_foreach (fd, vif->tap_fds) |
| 959 | { |
| 960 | ret = ioctl (*fd, TUNSETCARRIER, &carrier_up); |
| 961 | if (ret < 0) |
| 962 | { |
| 963 | clib_warning ("ioctl (TUNSETCARRIER) returned %d", ret); |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | if (!ret) |
| 968 | vif->host_carrier_up = (carrier_up != 0); |
| 969 | #endif |
| 970 | |
| 971 | return ret; |
| 972 | } |
| 973 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 974 | static clib_error_t * |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 975 | tap_mtu_config (vlib_main_t * vm, unformat_input_t * input) |
| 976 | { |
| 977 | tap_main_t *tm = &tap_main; |
| 978 | |
| 979 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 980 | { |
| 981 | if (unformat (input, "host-mtu %d", &tm->host_mtu_size)) |
| 982 | ; |
| 983 | else |
| 984 | return clib_error_return (0, "unknown input `%U'", |
| 985 | format_unformat_error, input); |
| 986 | } |
| 987 | |
| 988 | return 0; |
| 989 | } |
| 990 | |
Alexander Chernavin | a6c34a1 | 2020-09-04 09:24:20 -0400 | [diff] [blame] | 991 | /* |
| 992 | * Set host tap/tun interface speed in Mbps. |
| 993 | */ |
| 994 | int |
| 995 | tap_set_speed (u32 hw_if_index, u32 speed) |
| 996 | { |
| 997 | vnet_main_t *vnm = vnet_get_main (); |
| 998 | vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index); |
| 999 | virtio_main_t *mm = &virtio_main; |
| 1000 | virtio_if_t *vif; |
| 1001 | int old_netns_fd = -1; |
| 1002 | int nfd = -1; |
| 1003 | int ctl_fd = -1; |
| 1004 | struct ifreq ifr; |
| 1005 | struct ethtool_cmd ecmd; |
| 1006 | int ret = -1; |
| 1007 | |
| 1008 | vif = pool_elt_at_index (mm->interfaces, hi->dev_instance); |
| 1009 | |
| 1010 | if (vif->net_ns) |
| 1011 | { |
Nathan Skrzypczak | 4cef6de | 2021-07-19 18:21:43 +0200 | [diff] [blame] | 1012 | old_netns_fd = clib_netns_open (NULL /* self */); |
| 1013 | if ((nfd = clib_netns_open (vif->net_ns)) == -1) |
Alexander Chernavin | a6c34a1 | 2020-09-04 09:24:20 -0400 | [diff] [blame] | 1014 | { |
| 1015 | clib_warning ("Cannot open netns"); |
| 1016 | goto done; |
| 1017 | } |
Nathan Skrzypczak | 4cef6de | 2021-07-19 18:21:43 +0200 | [diff] [blame] | 1018 | if (clib_setns (nfd) == -1) |
Alexander Chernavin | a6c34a1 | 2020-09-04 09:24:20 -0400 | [diff] [blame] | 1019 | { |
| 1020 | clib_warning ("Cannot set ns"); |
| 1021 | goto done; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | if ((ctl_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1) |
| 1026 | { |
| 1027 | clib_warning ("Cannot open control socket"); |
| 1028 | goto done; |
| 1029 | } |
| 1030 | |
| 1031 | ecmd.cmd = ETHTOOL_GSET; |
| 1032 | clib_memset (&ifr, 0, sizeof (ifr)); |
| 1033 | clib_memcpy (ifr.ifr_name, vif->host_if_name, |
| 1034 | strlen ((const char *) vif->host_if_name)); |
| 1035 | ifr.ifr_data = (void *) &ecmd; |
| 1036 | if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0) |
| 1037 | { |
| 1038 | clib_warning ("Cannot get device settings"); |
| 1039 | goto done; |
| 1040 | } |
| 1041 | |
| 1042 | if (ethtool_cmd_speed (&ecmd) != speed) |
| 1043 | { |
| 1044 | ecmd.cmd = ETHTOOL_SSET; |
| 1045 | ethtool_cmd_speed_set (&ecmd, speed); |
| 1046 | if ((ret = ioctl (ctl_fd, SIOCETHTOOL, &ifr)) < 0) |
| 1047 | { |
| 1048 | clib_warning ("Cannot set device settings"); |
| 1049 | goto done; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | done: |
| 1054 | if (old_netns_fd != -1) |
| 1055 | { |
Nathan Skrzypczak | 4cef6de | 2021-07-19 18:21:43 +0200 | [diff] [blame] | 1056 | if (clib_setns (old_netns_fd) == -1) |
Alexander Chernavin | a6c34a1 | 2020-09-04 09:24:20 -0400 | [diff] [blame] | 1057 | { |
| 1058 | clib_warning ("Cannot set old ns"); |
| 1059 | } |
| 1060 | close (old_netns_fd); |
| 1061 | } |
| 1062 | if (nfd != -1) |
| 1063 | close (nfd); |
| 1064 | if (ctl_fd != -1) |
| 1065 | close (ctl_fd); |
| 1066 | |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 1070 | /* tap { host-mtu <size> } configuration. */ |
| 1071 | VLIB_CONFIG_FUNCTION (tap_mtu_config, "tap"); |
| 1072 | |
| 1073 | static clib_error_t * |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1074 | tap_init (vlib_main_t * vm) |
| 1075 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 1076 | tap_main_t *tm = &tap_main; |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 1077 | clib_error_t *error = 0; |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 1078 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1079 | tm->log_default = vlib_log_register_class ("tap", 0); |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 1080 | vlib_log_debug (tm->log_default, "initialized"); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 1081 | |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 1082 | tm->host_mtu_size = 0; |
| 1083 | |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 1084 | return error; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | VLIB_INIT_FUNCTION (tap_init); |
| 1088 | |
| 1089 | /* |
| 1090 | * fd.io coding-style-patch-verification: ON |
| 1091 | * |
| 1092 | * Local Variables: |
| 1093 | * eval: (c-set-style "gnu") |
| 1094 | * End: |
| 1095 | */ |