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