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