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 | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 28 | #include <sched.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 29 | |
| 30 | #include <linux/netlink.h> |
| 31 | #include <linux/rtnetlink.h> |
| 32 | |
| 33 | #include <vlib/vlib.h> |
| 34 | #include <vlib/unix/unix.h> |
| 35 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 36 | #include <vnet/ip/ip4_packet.h> |
| 37 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 38 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 39 | #include <vnet/devices/virtio/virtio.h> |
Damjan Marion | c99b4cd | 2017-12-04 15:25:58 +0100 | [diff] [blame] | 40 | #include <vnet/devices/tap/tap.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 41 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 42 | tap_main_t tap_main; |
| 43 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 44 | #define _IOCTL(fd,a,...) \ |
| 45 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 46 | { \ |
| 47 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
| 48 | goto error; \ |
| 49 | } |
| 50 | |
| 51 | static u32 |
| 52 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 53 | u32 flags) |
| 54 | { |
| 55 | /* nothing for now */ |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 56 | //TODO On MTU change call vnet_netlink_set_if_mtu |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 60 | static int |
| 61 | open_netns_fd (char *netns) |
| 62 | { |
| 63 | u8 *s = 0; |
| 64 | int fd; |
| 65 | |
| 66 | if (strncmp (netns, "pid:", 4) == 0) |
| 67 | s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0); |
| 68 | else if (netns[0] == '/') |
| 69 | s = format (0, "%s%c", netns, 0); |
| 70 | else |
| 71 | s = format (0, "/var/run/netns/%s%c", netns, 0); |
| 72 | |
| 73 | fd = open ((char *) s, O_RDONLY); |
| 74 | vec_free (s); |
| 75 | return fd; |
| 76 | } |
| 77 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 78 | #define TAP_MAX_INSTANCE 1024 |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 79 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 80 | void |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 81 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 82 | { |
| 83 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 84 | vlib_thread_main_t *thm = vlib_get_thread_main (); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 85 | virtio_main_t *vim = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 86 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 87 | vnet_sw_interface_t *sw; |
| 88 | vnet_hw_interface_t *hw; |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 89 | int i; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 90 | int old_netns_fd = -1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 91 | struct ifreq ifr; |
| 92 | size_t hdrsz; |
| 93 | struct vhost_memory *vhost_mem = 0; |
| 94 | virtio_if_t *vif = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 95 | clib_error_t *err = 0; |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 96 | int fd = -1; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 97 | |
| 98 | if (args->id != ~0) |
| 99 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 100 | if (clib_bitmap_get (tm->tap_ids, args->id)) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 101 | { |
| 102 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 103 | args->error = clib_error_return (0, "interface already exists"); |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 109 | args->id = clib_bitmap_first_clear (tm->tap_ids); |
| 110 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 111 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 112 | if (args->id > TAP_MAX_INSTANCE) |
| 113 | { |
| 114 | args->rv = VNET_API_ERROR_UNSPECIFIED; |
| 115 | args->error = clib_error_return (0, "cannot find free interface id"); |
| 116 | return; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 117 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 118 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 119 | clib_memset (&ifr, 0, sizeof (ifr)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 120 | pool_get (vim->interfaces, vif); |
| 121 | vif->dev_instance = vif - vim->interfaces; |
| 122 | vif->tap_fd = -1; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 123 | vif->id = args->id; |
| 124 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 125 | if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 126 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 127 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 128 | args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 129 | goto error; |
| 130 | } |
| 131 | |
| 132 | _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features); |
| 133 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 134 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 135 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 136 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 137 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 138 | "VIRTIO_NET_F_MRG_RXBUF feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 139 | goto error; |
| 140 | } |
| 141 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 142 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 143 | 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 144 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 145 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 146 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 147 | "VIRTIO_RING_F_INDIRECT_DESC feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 148 | goto error; |
| 149 | } |
| 150 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 151 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 152 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 153 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 154 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 155 | "VIRTIO_F_VERSION_1 features"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 156 | goto error; |
| 157 | } |
| 158 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 159 | vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF); |
| 160 | vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
| 161 | vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 162 | |
| 163 | virtio_set_net_hdr_size (vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 164 | |
| 165 | _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); |
| 166 | |
| 167 | if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 168 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 169 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 170 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 171 | goto error; |
| 172 | } |
| 173 | |
| 174 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 175 | _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 176 | vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 177 | |
Mohsin Kazmi | 2a6861f | 2019-04-15 13:17:55 +0200 | [diff] [blame^] | 178 | if (!args->host_if_name) |
| 179 | args->host_if_name = (u8 *) ifr.ifr_ifrn.ifrn_name; |
| 180 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 181 | unsigned int offload = 0; |
| 182 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 183 | if (args->tap_flags & TAP_FLAG_GSO) |
| 184 | { |
| 185 | offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 186 | vif->gso_enabled = 1; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | vif->gso_enabled = 0; |
| 191 | } |
| 192 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 193 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 194 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 195 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 196 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 197 | /* if namespace is specified, all further netlink messages should be excuted |
| 198 | after we change our net namespace */ |
| 199 | if (args->host_namespace) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 200 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 201 | old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); |
| 202 | if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1) |
| 203 | { |
| 204 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 205 | args->error = clib_error_return_unix (0, "open_netns_fd '%s'", |
| 206 | args->host_namespace); |
| 207 | goto error; |
| 208 | } |
| 209 | args->error = vnet_netlink_set_link_netns (vif->ifindex, fd, |
| 210 | (char *) args->host_if_name); |
| 211 | if (args->error) |
| 212 | { |
| 213 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 214 | goto error; |
| 215 | } |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 216 | if (setns (fd, CLONE_NEWNET) == -1) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 217 | { |
| 218 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 219 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 220 | args->host_namespace); |
| 221 | goto error; |
| 222 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 223 | if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0) |
| 224 | { |
| 225 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 226 | args->error = clib_error_return_unix (0, "if_nametoindex '%s'", |
| 227 | args->host_if_name); |
| 228 | goto error; |
| 229 | } |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | if (args->host_if_name) |
| 234 | { |
| 235 | args->error = vnet_netlink_set_link_name (vif->ifindex, |
| 236 | (char *) |
| 237 | args->host_if_name); |
| 238 | if (args->error) |
| 239 | { |
| 240 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 241 | goto error; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (!ethernet_mac_address_is_zero (args->host_mac_addr)) |
| 247 | { |
| 248 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 249 | args->host_mac_addr); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 250 | if (args->error) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 251 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 252 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 253 | goto error; |
| 254 | } |
| 255 | } |
| 256 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 257 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 258 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 259 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 260 | (char *) args->host_bridge); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 261 | if (args->error) |
| 262 | { |
| 263 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 264 | goto error; |
| 265 | } |
| 266 | } |
| 267 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 268 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 269 | if (args->host_ip4_prefix_len) |
| 270 | { |
| 271 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 272 | &args->host_ip4_addr, |
| 273 | args->host_ip4_prefix_len); |
| 274 | if (args->error) |
| 275 | { |
| 276 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 277 | goto error; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (args->host_ip6_prefix_len) |
| 282 | { |
| 283 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 284 | &args->host_ip6_addr, |
| 285 | args->host_ip6_prefix_len); |
| 286 | if (args->error) |
| 287 | { |
| 288 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 289 | goto error; |
| 290 | } |
| 291 | } |
| 292 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 293 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 294 | if (args->error) |
| 295 | { |
| 296 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 297 | goto error; |
| 298 | } |
| 299 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 300 | if (args->host_ip4_gw_set) |
| 301 | { |
| 302 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 303 | if (args->error) |
| 304 | { |
| 305 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 306 | goto error; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (args->host_ip6_gw_set) |
| 311 | { |
| 312 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 313 | if (args->error) |
| 314 | { |
| 315 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 316 | goto error; |
| 317 | } |
| 318 | } |
| 319 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 320 | /* switch back to old net namespace */ |
| 321 | if (args->host_namespace) |
| 322 | { |
| 323 | if (setns (old_netns_fd, CLONE_NEWNET) == -1) |
| 324 | { |
| 325 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 326 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 327 | args->host_namespace); |
| 328 | goto error; |
| 329 | } |
| 330 | } |
| 331 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 332 | /* Set vhost memory table */ |
| 333 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 334 | vhost_mem = clib_mem_alloc (i); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 335 | clib_memset (vhost_mem, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 336 | vhost_mem->nregions = 1; |
| 337 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 338 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 339 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 340 | if ((args->error = |
| 341 | virtio_vring_init (vm, vif, RX_QUEUE (0), args->rx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 342 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 343 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 344 | goto error; |
| 345 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 346 | vif->num_rxqs = 1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 347 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 348 | if ((args->error = |
| 349 | virtio_vring_init (vm, vif, TX_QUEUE (0), args->tx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 350 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 351 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 352 | goto error; |
| 353 | } |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 354 | vif->num_txqs = 1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 355 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 356 | if (!args->mac_addr_set) |
Benoît Ganne | fe750c2 | 2019-03-25 11:41:34 +0100 | [diff] [blame] | 357 | ethernet_mac_address_generate (args->mac_addr); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 358 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 359 | clib_memcpy (vif->mac_addr, args->mac_addr, 6); |
| 360 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 361 | vif->host_if_name = args->host_if_name; |
| 362 | args->host_if_name = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 363 | vif->net_ns = args->host_namespace; |
| 364 | args->host_namespace = 0; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 365 | vif->host_bridge = args->host_bridge; |
| 366 | args->host_bridge = 0; |
| 367 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6); |
| 368 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 369 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 370 | if (args->host_ip4_prefix_len) |
| 371 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 372 | if (args->host_ip6_prefix_len) |
| 373 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 374 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 375 | vif->type = VIRTIO_IF_TYPE_TAP; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 376 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 377 | vif->dev_instance, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 378 | vif->mac_addr, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 379 | &vif->hw_if_index, |
| 380 | virtio_eth_flag_change); |
| 381 | if (args->error) |
| 382 | { |
| 383 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 384 | goto error; |
| 385 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 386 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 387 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 388 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 389 | vif->sw_if_index = sw->sw_if_index; |
| 390 | args->sw_if_index = vif->sw_if_index; |
| 391 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 392 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 393 | if (args->tap_flags & TAP_FLAG_GSO) |
| 394 | { |
| 395 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 396 | vnm->interface_main.gso_interface_count++; |
| 397 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 398 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 399 | virtio_input_node.index); |
| 400 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 401 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 402 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 403 | vif->per_interface_next_index = ~0; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 404 | virtio_vring_set_numa_node (vm, vif, RX_QUEUE (0)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 405 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 406 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 407 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 408 | vif->cxq_vring = NULL; |
| 409 | |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 410 | if (thm->n_vlib_mains > 1) |
| 411 | clib_spinlock_init (&vif->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 412 | goto done; |
| 413 | |
| 414 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 415 | if (err) |
| 416 | { |
| 417 | ASSERT (args->error == 0); |
| 418 | args->error = err; |
| 419 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 420 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 421 | if (vif->tap_fd != -1) |
| 422 | close (vif->tap_fd); |
| 423 | if (vif->fd != -1) |
| 424 | close (vif->fd); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 425 | vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif, |
| 426 | RX_QUEUE (i)); |
| 427 | vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif, |
| 428 | TX_QUEUE (i)); |
| 429 | vec_free (vif->rxq_vrings); |
| 430 | vec_free (vif->txq_vrings); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 431 | clib_memset (vif, 0, sizeof (virtio_if_t)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 432 | pool_put (vim->interfaces, vif); |
| 433 | |
| 434 | done: |
| 435 | if (vhost_mem) |
| 436 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 437 | if (old_netns_fd != -1) |
| 438 | close (old_netns_fd); |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 439 | if (fd != -1) |
| 440 | close (fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | int |
| 444 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 445 | { |
| 446 | vnet_main_t *vnm = vnet_get_main (); |
| 447 | virtio_main_t *mm = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 448 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 449 | int i; |
| 450 | virtio_if_t *vif; |
| 451 | vnet_hw_interface_t *hw; |
| 452 | |
| 453 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 454 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 455 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 456 | |
| 457 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 458 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 459 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 460 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 461 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 462 | /* decrement if this was a GSO interface */ |
| 463 | if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) |
| 464 | vnm->interface_main.gso_interface_count--; |
| 465 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 466 | /* bring down the interface */ |
| 467 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 468 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 469 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, RX_QUEUE (0)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 470 | |
| 471 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 472 | vif->hw_if_index = ~0; |
| 473 | |
| 474 | if (vif->tap_fd != -1) |
| 475 | close (vif->tap_fd); |
| 476 | if (vif->fd != -1) |
| 477 | close (vif->fd); |
| 478 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 479 | vec_foreach_index (i, vif->rxq_vrings) virtio_vring_free_rx (vm, vif, |
| 480 | RX_QUEUE (i)); |
| 481 | vec_foreach_index (i, vif->txq_vrings) virtio_vring_free_tx (vm, vif, |
| 482 | TX_QUEUE (i)); |
| 483 | vec_free (vif->rxq_vrings); |
| 484 | vec_free (vif->txq_vrings); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 485 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 486 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 487 | clib_spinlock_free (&vif->lockp); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 488 | clib_memset (vif, 0, sizeof (*vif)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 489 | pool_put (mm->interfaces, vif); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | int |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 495 | tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable) |
| 496 | { |
| 497 | vnet_main_t *vnm = vnet_get_main (); |
| 498 | virtio_main_t *mm = &virtio_main; |
| 499 | virtio_if_t *vif; |
| 500 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 501 | clib_error_t *err = 0; |
| 502 | |
| 503 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 504 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 505 | |
| 506 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 507 | |
| 508 | const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 509 | const unsigned int gso_off = 0; |
| 510 | unsigned int offload = enable_disable ? gso_on : gso_off; |
| 511 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 512 | vif->gso_enabled = enable_disable ? 1 : 0; |
| 513 | if (enable_disable) |
| 514 | { |
| 515 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0) |
| 516 | { |
| 517 | vnm->interface_main.gso_interface_count++; |
| 518 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 519 | } |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0) |
| 524 | { |
| 525 | vnm->interface_main.gso_interface_count--; |
| 526 | hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | error: |
| 531 | if (err) |
| 532 | { |
| 533 | clib_warning ("Error %s gso on sw_if_index %d", |
| 534 | enable_disable ? "enabling" : "disabling", sw_if_index); |
| 535 | return VNET_API_ERROR_SYSCALL_ERROR_3; |
| 536 | } |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | int |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 541 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 542 | { |
| 543 | vnet_main_t *vnm = vnet_get_main (); |
| 544 | virtio_main_t *mm = &virtio_main; |
| 545 | virtio_if_t *vif; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 546 | virtio_vring_t *vring; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 547 | vnet_hw_interface_t *hi; |
| 548 | tap_interface_details_t *r_tapids = NULL; |
| 549 | tap_interface_details_t *tapid = NULL; |
| 550 | |
| 551 | /* *INDENT-OFF* */ |
| 552 | pool_foreach (vif, mm->interfaces, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 553 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 554 | continue; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 555 | vec_add2(r_tapids, tapid, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 556 | clib_memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 557 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 558 | tapid->sw_if_index = vif->sw_if_index; |
| 559 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 560 | clib_memcpy(tapid->dev_name, hi->name, |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 561 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 562 | strlen ((const char *) hi->name))); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 563 | vring = vec_elt_at_index (vif->rxq_vrings, RX_QUEUE_ACCESS(0)); |
| 564 | tapid->rx_ring_sz = vring->size; |
| 565 | vring = vec_elt_at_index (vif->txq_vrings, TX_QUEUE_ACCESS(0)); |
| 566 | tapid->tx_ring_sz = vring->size; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 567 | clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6); |
| 568 | if (vif->host_if_name) |
| 569 | { |
| 570 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 571 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
| 572 | strlen ((const char *) vif->host_if_name))); |
| 573 | } |
| 574 | if (vif->net_ns) |
| 575 | { |
| 576 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 577 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
| 578 | strlen ((const char *) vif->net_ns))); |
| 579 | } |
| 580 | if (vif->host_bridge) |
| 581 | { |
| 582 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 583 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
| 584 | strlen ((const char *) vif->host_bridge))); |
| 585 | } |
| 586 | if (vif->host_ip4_prefix_len) |
| 587 | clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4); |
| 588 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 589 | if (vif->host_ip6_prefix_len) |
| 590 | clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16); |
| 591 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 592 | ); |
| 593 | /* *INDENT-ON* */ |
| 594 | |
| 595 | *out_tapids = r_tapids; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static clib_error_t * |
| 601 | tap_init (vlib_main_t * vm) |
| 602 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 603 | tap_main_t *tm = &tap_main; |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 604 | clib_error_t *error = 0; |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 605 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 606 | tm->log_default = vlib_log_register_class ("tap", 0); |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 607 | vlib_log_debug (tm->log_default, "initialized"); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 608 | |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 609 | return error; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | VLIB_INIT_FUNCTION (tap_init); |
| 613 | |
| 614 | /* |
| 615 | * fd.io coding-style-patch-verification: ON |
| 616 | * |
| 617 | * Local Variables: |
| 618 | * eval: (c-set-style "gnu") |
| 619 | * End: |
| 620 | */ |