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