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 | |
| 178 | unsigned int offload = 0; |
| 179 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 180 | if (args->tap_flags & TAP_FLAG_GSO) |
| 181 | { |
| 182 | offload = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 183 | vif->gso_enabled = 1; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | vif->gso_enabled = 0; |
| 188 | } |
| 189 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 190 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 191 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 192 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 193 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 194 | /* if namespace is specified, all further netlink messages should be excuted |
| 195 | after we change our net namespace */ |
| 196 | if (args->host_namespace) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 197 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 198 | old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); |
| 199 | if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1) |
| 200 | { |
| 201 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 202 | args->error = clib_error_return_unix (0, "open_netns_fd '%s'", |
| 203 | args->host_namespace); |
| 204 | goto error; |
| 205 | } |
| 206 | args->error = vnet_netlink_set_link_netns (vif->ifindex, fd, |
| 207 | (char *) args->host_if_name); |
| 208 | if (args->error) |
| 209 | { |
| 210 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 211 | goto error; |
| 212 | } |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 213 | if (setns (fd, CLONE_NEWNET) == -1) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 214 | { |
| 215 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 216 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 217 | args->host_namespace); |
| 218 | goto error; |
| 219 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 220 | if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0) |
| 221 | { |
| 222 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 223 | args->error = clib_error_return_unix (0, "if_nametoindex '%s'", |
| 224 | args->host_if_name); |
| 225 | goto error; |
| 226 | } |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | if (args->host_if_name) |
| 231 | { |
| 232 | args->error = vnet_netlink_set_link_name (vif->ifindex, |
| 233 | (char *) |
| 234 | args->host_if_name); |
| 235 | if (args->error) |
| 236 | { |
| 237 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 238 | goto error; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (!ethernet_mac_address_is_zero (args->host_mac_addr)) |
| 244 | { |
| 245 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 246 | args->host_mac_addr); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 247 | if (args->error) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 248 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 249 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 250 | goto error; |
| 251 | } |
| 252 | } |
| 253 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 254 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 255 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 256 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 257 | (char *) args->host_bridge); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 258 | if (args->error) |
| 259 | { |
| 260 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 261 | goto error; |
| 262 | } |
| 263 | } |
| 264 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 265 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 266 | if (args->host_ip4_prefix_len) |
| 267 | { |
| 268 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 269 | &args->host_ip4_addr, |
| 270 | args->host_ip4_prefix_len); |
| 271 | if (args->error) |
| 272 | { |
| 273 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 274 | goto error; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (args->host_ip6_prefix_len) |
| 279 | { |
| 280 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 281 | &args->host_ip6_addr, |
| 282 | args->host_ip6_prefix_len); |
| 283 | if (args->error) |
| 284 | { |
| 285 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 286 | goto error; |
| 287 | } |
| 288 | } |
| 289 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 290 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 291 | if (args->error) |
| 292 | { |
| 293 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 294 | goto error; |
| 295 | } |
| 296 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 297 | if (args->host_ip4_gw_set) |
| 298 | { |
| 299 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 300 | if (args->error) |
| 301 | { |
| 302 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 303 | goto error; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (args->host_ip6_gw_set) |
| 308 | { |
| 309 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 310 | if (args->error) |
| 311 | { |
| 312 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 313 | goto error; |
| 314 | } |
| 315 | } |
| 316 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 317 | /* switch back to old net namespace */ |
| 318 | if (args->host_namespace) |
| 319 | { |
| 320 | if (setns (old_netns_fd, CLONE_NEWNET) == -1) |
| 321 | { |
| 322 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 323 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 324 | args->host_namespace); |
| 325 | goto error; |
| 326 | } |
| 327 | } |
| 328 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 329 | /* Set vhost memory table */ |
| 330 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 331 | vhost_mem = clib_mem_alloc (i); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 332 | clib_memset (vhost_mem, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 333 | vhost_mem->nregions = 1; |
| 334 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 335 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 336 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 337 | if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 338 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 339 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 340 | goto error; |
| 341 | } |
| 342 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 343 | if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz))) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 344 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 345 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 346 | goto error; |
| 347 | } |
| 348 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 349 | if (!args->mac_addr_set) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 350 | { |
| 351 | f64 now = vlib_time_now (vm); |
| 352 | u32 rnd; |
| 353 | rnd = (u32) (now * 1e6); |
| 354 | rnd = random_u32 (&rnd); |
| 355 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 356 | memcpy (args->mac_addr + 2, &rnd, sizeof (rnd)); |
| 357 | args->mac_addr[0] = 2; |
| 358 | args->mac_addr[1] = 0xfe; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 359 | } |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 360 | vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256; |
| 361 | vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 362 | clib_memcpy (vif->mac_addr, args->mac_addr, 6); |
| 363 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 364 | vif->host_if_name = args->host_if_name; |
| 365 | args->host_if_name = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 366 | vif->net_ns = args->host_namespace; |
| 367 | args->host_namespace = 0; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 368 | vif->host_bridge = args->host_bridge; |
| 369 | args->host_bridge = 0; |
| 370 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6); |
| 371 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 372 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 373 | if (args->host_ip4_prefix_len) |
| 374 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 375 | if (args->host_ip6_prefix_len) |
| 376 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 377 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 378 | vif->type = VIRTIO_IF_TYPE_TAP; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 379 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 380 | vif->dev_instance, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 381 | vif->mac_addr, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 382 | &vif->hw_if_index, |
| 383 | virtio_eth_flag_change); |
| 384 | if (args->error) |
| 385 | { |
| 386 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 387 | goto error; |
| 388 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 389 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 390 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 391 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 392 | vif->sw_if_index = sw->sw_if_index; |
| 393 | args->sw_if_index = vif->sw_if_index; |
| 394 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 395 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 396 | if (args->tap_flags & TAP_FLAG_GSO) |
| 397 | { |
| 398 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 399 | vnm->interface_main.gso_interface_count++; |
| 400 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 401 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 402 | virtio_input_node.index); |
| 403 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 404 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 405 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 406 | vif->per_interface_next_index = ~0; |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 407 | virtio_vring_set_numa_node (vm, vif, 0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 408 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 409 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 410 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 411 | if (thm->n_vlib_mains > 1) |
| 412 | clib_spinlock_init (&vif->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 413 | goto done; |
| 414 | |
| 415 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 416 | if (err) |
| 417 | { |
| 418 | ASSERT (args->error == 0); |
| 419 | args->error = err; |
| 420 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 421 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 422 | if (vif->tap_fd != -1) |
| 423 | close (vif->tap_fd); |
| 424 | if (vif->fd != -1) |
| 425 | close (vif->fd); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 426 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 427 | vec_free (vif->vrings); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 428 | clib_memset (vif, 0, sizeof (virtio_if_t)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 429 | pool_put (vim->interfaces, vif); |
| 430 | |
| 431 | done: |
| 432 | if (vhost_mem) |
| 433 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 434 | if (old_netns_fd != -1) |
| 435 | close (old_netns_fd); |
Steven Luong | 4a310d2 | 2019-02-21 14:55:52 -0800 | [diff] [blame] | 436 | if (fd != -1) |
| 437 | close (fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | int |
| 441 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 442 | { |
| 443 | vnet_main_t *vnm = vnet_get_main (); |
| 444 | virtio_main_t *mm = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 445 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 446 | int i; |
| 447 | virtio_if_t *vif; |
| 448 | vnet_hw_interface_t *hw; |
| 449 | |
| 450 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 451 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 452 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 453 | |
| 454 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 455 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 456 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 457 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 458 | |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 459 | /* decrement if this was a GSO interface */ |
| 460 | if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) |
| 461 | vnm->interface_main.gso_interface_count--; |
| 462 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 463 | /* bring down the interface */ |
| 464 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 465 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
Steven | 0b85673 | 2018-02-28 11:00:34 -0800 | [diff] [blame] | 466 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 467 | |
| 468 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 469 | vif->hw_if_index = ~0; |
| 470 | |
| 471 | if (vif->tap_fd != -1) |
| 472 | close (vif->tap_fd); |
| 473 | if (vif->fd != -1) |
| 474 | close (vif->fd); |
| 475 | |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 476 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 477 | vec_free (vif->vrings); |
| 478 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 479 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 480 | clib_spinlock_free (&vif->lockp); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 481 | clib_memset (vif, 0, sizeof (*vif)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 482 | pool_put (mm->interfaces, vif); |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | int |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 488 | tap_gso_enable_disable (vlib_main_t * vm, u32 sw_if_index, int enable_disable) |
| 489 | { |
| 490 | vnet_main_t *vnm = vnet_get_main (); |
| 491 | virtio_main_t *mm = &virtio_main; |
| 492 | virtio_if_t *vif; |
| 493 | vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 494 | clib_error_t *err = 0; |
| 495 | |
| 496 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 497 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 498 | |
| 499 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 500 | |
| 501 | const unsigned int gso_on = TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6; |
| 502 | const unsigned int gso_off = 0; |
| 503 | unsigned int offload = enable_disable ? gso_on : gso_off; |
| 504 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 505 | vif->gso_enabled = enable_disable ? 1 : 0; |
| 506 | if (enable_disable) |
| 507 | { |
| 508 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) == 0) |
| 509 | { |
| 510 | vnm->interface_main.gso_interface_count++; |
| 511 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 512 | } |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | if ((hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO) != 0) |
| 517 | { |
| 518 | vnm->interface_main.gso_interface_count--; |
| 519 | hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | error: |
| 524 | if (err) |
| 525 | { |
| 526 | clib_warning ("Error %s gso on sw_if_index %d", |
| 527 | enable_disable ? "enabling" : "disabling", sw_if_index); |
| 528 | return VNET_API_ERROR_SYSCALL_ERROR_3; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | int |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 534 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 535 | { |
| 536 | vnet_main_t *vnm = vnet_get_main (); |
| 537 | virtio_main_t *mm = &virtio_main; |
| 538 | virtio_if_t *vif; |
| 539 | vnet_hw_interface_t *hi; |
| 540 | tap_interface_details_t *r_tapids = NULL; |
| 541 | tap_interface_details_t *tapid = NULL; |
| 542 | |
| 543 | /* *INDENT-OFF* */ |
| 544 | pool_foreach (vif, mm->interfaces, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 545 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 546 | continue; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 547 | vec_add2(r_tapids, tapid, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 548 | clib_memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 549 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 550 | tapid->sw_if_index = vif->sw_if_index; |
| 551 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 552 | clib_memcpy(tapid->dev_name, hi->name, |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 553 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 554 | strlen ((const char *) hi->name))); |
| 555 | tapid->rx_ring_sz = vif->rx_ring_sz; |
| 556 | tapid->tx_ring_sz = vif->tx_ring_sz; |
| 557 | clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6); |
| 558 | if (vif->host_if_name) |
| 559 | { |
| 560 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 561 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
| 562 | strlen ((const char *) vif->host_if_name))); |
| 563 | } |
| 564 | if (vif->net_ns) |
| 565 | { |
| 566 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 567 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
| 568 | strlen ((const char *) vif->net_ns))); |
| 569 | } |
| 570 | if (vif->host_bridge) |
| 571 | { |
| 572 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 573 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
| 574 | strlen ((const char *) vif->host_bridge))); |
| 575 | } |
| 576 | if (vif->host_ip4_prefix_len) |
| 577 | clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4); |
| 578 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 579 | if (vif->host_ip6_prefix_len) |
| 580 | clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16); |
| 581 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 582 | ); |
| 583 | /* *INDENT-ON* */ |
| 584 | |
| 585 | *out_tapids = r_tapids; |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static clib_error_t * |
| 591 | tap_init (vlib_main_t * vm) |
| 592 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 593 | tap_main_t *tm = &tap_main; |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 594 | clib_error_t *error = 0; |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 595 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 596 | tm->log_default = vlib_log_register_class ("tap", 0); |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 597 | vlib_log_debug (tm->log_default, "initialized"); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 598 | |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 599 | return error; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | VLIB_INIT_FUNCTION (tap_init); |
| 603 | |
| 604 | /* |
| 605 | * fd.io coding-style-patch-verification: ON |
| 606 | * |
| 607 | * Local Variables: |
| 608 | * eval: (c-set-style "gnu") |
| 609 | * End: |
| 610 | */ |