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; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 96 | |
| 97 | if (args->id != ~0) |
| 98 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 99 | if (clib_bitmap_get (tm->tap_ids, args->id)) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 100 | { |
| 101 | args->rv = VNET_API_ERROR_INVALID_INTERFACE; |
| 102 | args->error = clib_error_return (0, "interface already exists"); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | else |
| 107 | { |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 108 | args->id = clib_bitmap_first_clear (tm->tap_ids); |
| 109 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 110 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 111 | if (args->id > TAP_MAX_INSTANCE) |
| 112 | { |
| 113 | args->rv = VNET_API_ERROR_UNSPECIFIED; |
| 114 | args->error = clib_error_return (0, "cannot find free interface id"); |
| 115 | return; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 116 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 117 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 118 | clib_memset (&ifr, 0, sizeof (ifr)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 119 | pool_get (vim->interfaces, vif); |
| 120 | vif->dev_instance = vif - vim->interfaces; |
| 121 | vif->tap_fd = -1; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 122 | vif->id = args->id; |
| 123 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 124 | if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 125 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 126 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 127 | args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features); |
| 132 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 133 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 134 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 135 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 136 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 137 | "VIRTIO_NET_F_MRG_RXBUF feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 138 | goto error; |
| 139 | } |
| 140 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 141 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC)) == |
| 142 | 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 143 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 144 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 145 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 146 | "VIRTIO_RING_F_INDIRECT_DESC feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 147 | goto error; |
| 148 | } |
| 149 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 150 | if ((vif->remote_features & VIRTIO_FEATURE (VIRTIO_F_VERSION_1)) == 0) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 151 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 152 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 153 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 154 | "VIRTIO_F_VERSION_1 features"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 155 | goto error; |
| 156 | } |
| 157 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 158 | vif->features |= VIRTIO_FEATURE (VIRTIO_NET_F_MRG_RXBUF); |
| 159 | vif->features |= VIRTIO_FEATURE (VIRTIO_F_VERSION_1); |
| 160 | vif->features |= VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC); |
| 161 | |
| 162 | virtio_set_net_hdr_size (vif); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 163 | |
| 164 | _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); |
| 165 | |
| 166 | if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 167 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 168 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 169 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 170 | goto error; |
| 171 | } |
| 172 | |
| 173 | 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] | 174 | _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 175 | vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 176 | |
| 177 | unsigned int offload = 0; |
| 178 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
| 179 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 180 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 181 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 182 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 183 | /* if namespace is specified, all further netlink messages should be excuted |
| 184 | after we change our net namespace */ |
| 185 | if (args->host_namespace) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 186 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 187 | int fd; |
Steven | 27ca298 | 2018-12-21 12:42:34 -0800 | [diff] [blame] | 188 | int rc; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 189 | old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); |
| 190 | if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1) |
| 191 | { |
| 192 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 193 | args->error = clib_error_return_unix (0, "open_netns_fd '%s'", |
| 194 | args->host_namespace); |
| 195 | goto error; |
| 196 | } |
| 197 | args->error = vnet_netlink_set_link_netns (vif->ifindex, fd, |
| 198 | (char *) args->host_if_name); |
| 199 | if (args->error) |
| 200 | { |
| 201 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 202 | goto error; |
| 203 | } |
Steven | 27ca298 | 2018-12-21 12:42:34 -0800 | [diff] [blame] | 204 | rc = setns (fd, CLONE_NEWNET); |
| 205 | close (fd); |
| 206 | if (rc == -1) |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 207 | { |
| 208 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 209 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 210 | args->host_namespace); |
| 211 | goto error; |
| 212 | } |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 213 | if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0) |
| 214 | { |
| 215 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 216 | args->error = clib_error_return_unix (0, "if_nametoindex '%s'", |
| 217 | args->host_if_name); |
| 218 | goto error; |
| 219 | } |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | if (args->host_if_name) |
| 224 | { |
| 225 | args->error = vnet_netlink_set_link_name (vif->ifindex, |
| 226 | (char *) |
| 227 | args->host_if_name); |
| 228 | if (args->error) |
| 229 | { |
| 230 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 231 | goto error; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (!ethernet_mac_address_is_zero (args->host_mac_addr)) |
| 237 | { |
| 238 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 239 | args->host_mac_addr); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 240 | if (args->error) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 241 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 242 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 243 | goto error; |
| 244 | } |
| 245 | } |
| 246 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 247 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 248 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 249 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 250 | (char *) args->host_bridge); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 251 | if (args->error) |
| 252 | { |
| 253 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 254 | goto error; |
| 255 | } |
| 256 | } |
| 257 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 258 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 259 | if (args->host_ip4_prefix_len) |
| 260 | { |
| 261 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 262 | &args->host_ip4_addr, |
| 263 | args->host_ip4_prefix_len); |
| 264 | if (args->error) |
| 265 | { |
| 266 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 267 | goto error; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (args->host_ip6_prefix_len) |
| 272 | { |
| 273 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 274 | &args->host_ip6_addr, |
| 275 | args->host_ip6_prefix_len); |
| 276 | if (args->error) |
| 277 | { |
| 278 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 279 | goto error; |
| 280 | } |
| 281 | } |
| 282 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 283 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 284 | if (args->error) |
| 285 | { |
| 286 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 287 | goto error; |
| 288 | } |
| 289 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 290 | if (args->host_ip4_gw_set) |
| 291 | { |
| 292 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 293 | if (args->error) |
| 294 | { |
| 295 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 296 | goto error; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (args->host_ip6_gw_set) |
| 301 | { |
| 302 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 303 | if (args->error) |
| 304 | { |
| 305 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 306 | goto error; |
| 307 | } |
| 308 | } |
| 309 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 310 | /* switch back to old net namespace */ |
| 311 | if (args->host_namespace) |
| 312 | { |
| 313 | if (setns (old_netns_fd, CLONE_NEWNET) == -1) |
| 314 | { |
| 315 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 316 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 317 | args->host_namespace); |
| 318 | goto error; |
| 319 | } |
| 320 | } |
| 321 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 322 | /* Set vhost memory table */ |
| 323 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 324 | vhost_mem = clib_mem_alloc (i); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 325 | clib_memset (vhost_mem, 0, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 326 | vhost_mem->nregions = 1; |
| 327 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 328 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 329 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 330 | 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] | 331 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 332 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 333 | goto error; |
| 334 | } |
| 335 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 336 | 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] | 337 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 338 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 339 | goto error; |
| 340 | } |
| 341 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 342 | if (!args->mac_addr_set) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 343 | { |
| 344 | f64 now = vlib_time_now (vm); |
| 345 | u32 rnd; |
| 346 | rnd = (u32) (now * 1e6); |
| 347 | rnd = random_u32 (&rnd); |
| 348 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 349 | memcpy (args->mac_addr + 2, &rnd, sizeof (rnd)); |
| 350 | args->mac_addr[0] = 2; |
| 351 | args->mac_addr[1] = 0xfe; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 352 | } |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 353 | vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256; |
| 354 | 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] | 355 | clib_memcpy (vif->mac_addr, args->mac_addr, 6); |
| 356 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 357 | vif->host_if_name = args->host_if_name; |
| 358 | args->host_if_name = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 359 | vif->net_ns = args->host_namespace; |
| 360 | args->host_namespace = 0; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 361 | vif->host_bridge = args->host_bridge; |
| 362 | args->host_bridge = 0; |
| 363 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6); |
| 364 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 365 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 366 | if (args->host_ip4_prefix_len) |
| 367 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 368 | if (args->host_ip6_prefix_len) |
| 369 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 370 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 371 | vif->type = VIRTIO_IF_TYPE_TAP; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 372 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 373 | vif->dev_instance, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 374 | vif->mac_addr, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 375 | &vif->hw_if_index, |
| 376 | virtio_eth_flag_change); |
| 377 | if (args->error) |
| 378 | { |
| 379 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 380 | goto error; |
| 381 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 382 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 383 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 384 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 385 | vif->sw_if_index = sw->sw_if_index; |
| 386 | args->sw_if_index = vif->sw_if_index; |
| 387 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 388 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 389 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 390 | virtio_input_node.index); |
| 391 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 392 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 393 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 394 | vif->per_interface_next_index = ~0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 395 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 396 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 397 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 398 | if (thm->n_vlib_mains > 1) |
| 399 | clib_spinlock_init (&vif->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 400 | goto done; |
| 401 | |
| 402 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 403 | if (err) |
| 404 | { |
| 405 | ASSERT (args->error == 0); |
| 406 | args->error = err; |
| 407 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 408 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 409 | if (vif->tap_fd != -1) |
| 410 | close (vif->tap_fd); |
| 411 | if (vif->fd != -1) |
| 412 | close (vif->fd); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 413 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 414 | vec_free (vif->vrings); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 415 | clib_memset (vif, 0, sizeof (virtio_if_t)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 416 | pool_put (vim->interfaces, vif); |
| 417 | |
| 418 | done: |
| 419 | if (vhost_mem) |
| 420 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 421 | if (old_netns_fd != -1) |
| 422 | close (old_netns_fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | int |
| 426 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 427 | { |
| 428 | vnet_main_t *vnm = vnet_get_main (); |
| 429 | virtio_main_t *mm = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 430 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 431 | int i; |
| 432 | virtio_if_t *vif; |
| 433 | vnet_hw_interface_t *hw; |
| 434 | |
| 435 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 436 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 437 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 438 | |
| 439 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 440 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 441 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 442 | return VNET_API_ERROR_INVALID_INTERFACE; |
| 443 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 444 | /* bring down the interface */ |
| 445 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 446 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
Steven | 0b85673 | 2018-02-28 11:00:34 -0800 | [diff] [blame] | 447 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 448 | |
| 449 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 450 | vif->hw_if_index = ~0; |
| 451 | |
| 452 | if (vif->tap_fd != -1) |
| 453 | close (vif->tap_fd); |
| 454 | if (vif->fd != -1) |
| 455 | close (vif->fd); |
| 456 | |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 457 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 458 | vec_free (vif->vrings); |
| 459 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 460 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 461 | clib_spinlock_free (&vif->lockp); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 462 | clib_memset (vif, 0, sizeof (*vif)); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 463 | pool_put (mm->interfaces, vif); |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | int |
| 469 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 470 | { |
| 471 | vnet_main_t *vnm = vnet_get_main (); |
| 472 | virtio_main_t *mm = &virtio_main; |
| 473 | virtio_if_t *vif; |
| 474 | vnet_hw_interface_t *hi; |
| 475 | tap_interface_details_t *r_tapids = NULL; |
| 476 | tap_interface_details_t *tapid = NULL; |
| 477 | |
| 478 | /* *INDENT-OFF* */ |
| 479 | pool_foreach (vif, mm->interfaces, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 480 | if (vif->type != VIRTIO_IF_TYPE_TAP) |
| 481 | continue; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 482 | vec_add2(r_tapids, tapid, 1); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 483 | clib_memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 484 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 485 | tapid->sw_if_index = vif->sw_if_index; |
| 486 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 487 | clib_memcpy(tapid->dev_name, hi->name, |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 488 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 489 | strlen ((const char *) hi->name))); |
| 490 | tapid->rx_ring_sz = vif->rx_ring_sz; |
| 491 | tapid->tx_ring_sz = vif->tx_ring_sz; |
| 492 | clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6); |
| 493 | if (vif->host_if_name) |
| 494 | { |
| 495 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 496 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
| 497 | strlen ((const char *) vif->host_if_name))); |
| 498 | } |
| 499 | if (vif->net_ns) |
| 500 | { |
| 501 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 502 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
| 503 | strlen ((const char *) vif->net_ns))); |
| 504 | } |
| 505 | if (vif->host_bridge) |
| 506 | { |
| 507 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 508 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
| 509 | strlen ((const char *) vif->host_bridge))); |
| 510 | } |
| 511 | if (vif->host_ip4_prefix_len) |
| 512 | clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4); |
| 513 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 514 | if (vif->host_ip6_prefix_len) |
| 515 | clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16); |
| 516 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 517 | ); |
| 518 | /* *INDENT-ON* */ |
| 519 | |
| 520 | *out_tapids = r_tapids; |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static clib_error_t * |
| 526 | tap_init (vlib_main_t * vm) |
| 527 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 528 | tap_main_t *tm = &tap_main; |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 529 | clib_error_t *error = 0; |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 530 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 531 | tm->log_default = vlib_log_register_class ("tap", 0); |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 532 | vlib_log_debug (tm->log_default, "initialized"); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame] | 533 | |
Mohsin Kazmi | a23b615 | 2018-05-17 17:21:39 +0200 | [diff] [blame] | 534 | return error; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | VLIB_INIT_FUNCTION (tap_init); |
| 538 | |
| 539 | /* |
| 540 | * fd.io coding-style-patch-verification: ON |
| 541 | * |
| 542 | * Local Variables: |
| 543 | * eval: (c-set-style "gnu") |
| 544 | * End: |
| 545 | */ |