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