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