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