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 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <net/if.h> |
| 22 | #include <linux/if_tun.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <linux/virtio_net.h> |
| 25 | #include <linux/vhost.h> |
| 26 | #include <sys/eventfd.h> |
| 27 | |
| 28 | #include <linux/netlink.h> |
| 29 | #include <linux/rtnetlink.h> |
| 30 | |
| 31 | #include <vlib/vlib.h> |
| 32 | #include <vlib/unix/unix.h> |
| 33 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 34 | #include <vnet/ip/ip4_packet.h> |
| 35 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 36 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 37 | #include <vnet/devices/virtio/virtio.h> |
Damjan Marion | c99b4cd | 2017-12-04 15:25:58 +0100 | [diff] [blame] | 38 | #include <vnet/devices/tap/tap.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 39 | |
| 40 | #define _IOCTL(fd,a,...) \ |
| 41 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 42 | { \ |
| 43 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
| 44 | goto error; \ |
| 45 | } |
| 46 | |
| 47 | static u32 |
| 48 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 49 | u32 flags) |
| 50 | { |
| 51 | /* nothing for now */ |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 52 | //TODO On MTU change call vnet_netlink_set_if_mtu |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 56 | void |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 57 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 58 | { |
| 59 | vnet_main_t *vnm = vnet_get_main (); |
| 60 | virtio_main_t *vim = &virtio_main; |
| 61 | vnet_sw_interface_t *sw; |
| 62 | vnet_hw_interface_t *hw; |
Steven | 37eba0d | 2017-12-02 20:17:27 -0800 | [diff] [blame] | 63 | int i, fd = -1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 64 | struct ifreq ifr; |
| 65 | size_t hdrsz; |
| 66 | struct vhost_memory *vhost_mem = 0; |
| 67 | virtio_if_t *vif = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 68 | clib_error_t *err = 0; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 69 | |
| 70 | memset (&ifr, 0, sizeof (ifr)); |
| 71 | pool_get (vim->interfaces, vif); |
| 72 | vif->dev_instance = vif - vim->interfaces; |
| 73 | vif->tap_fd = -1; |
| 74 | |
| 75 | if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 76 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 77 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 78 | args->error = clib_error_return_unix (0, "open '/dev/vhost-net'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 79 | goto error; |
| 80 | } |
| 81 | |
| 82 | _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features); |
| 83 | |
| 84 | if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 85 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 86 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 87 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 88 | "VIRTIO_NET_F_MRG_RXBUF feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 89 | goto error; |
| 90 | } |
| 91 | |
| 92 | if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0) |
| 93 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 94 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 95 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 96 | "VIRTIO_RING_F_INDIRECT_DESC feature"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 97 | goto error; |
| 98 | } |
| 99 | |
| 100 | if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0) |
| 101 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 102 | args->rv = VNET_API_ERROR_UNSUPPORTED; |
| 103 | args->error = clib_error_return (0, "vhost-net backend doesn't support " |
| 104 | "VIRTIO_F_VERSION_1 features"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 105 | goto error; |
| 106 | } |
| 107 | |
| 108 | vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF; |
| 109 | vif->features |= 1ULL << VIRTIO_F_VERSION_1; |
| 110 | vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC; |
| 111 | |
| 112 | _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); |
| 113 | |
| 114 | if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 115 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 116 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 117 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR; |
Steven | f953dfc | 2017-11-30 16:56:54 -0800 | [diff] [blame] | 122 | strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE - 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 123 | _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); |
| 124 | |
| 125 | vif->ifindex = if_nametoindex ((char *) args->name); |
| 126 | |
| 127 | unsigned int offload = 0; |
| 128 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
| 129 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 130 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 131 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 132 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 133 | if (args->host_bridge) |
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 | int master_ifindex = if_nametoindex ((char *) args->host_bridge); |
| 136 | args->error = vnet_netlink_set_if_master (vif->ifindex, master_ifindex); |
| 137 | if (args->error) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 138 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 139 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 140 | goto error; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (args->host_namespace) |
| 145 | { |
| 146 | args->error = vnet_netlink_set_if_namespace (vif->ifindex, |
| 147 | (char *) |
| 148 | args->host_namespace); |
| 149 | if (args->error) |
| 150 | { |
| 151 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 152 | goto error; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (args->host_ip4_prefix_len) |
| 157 | { |
| 158 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 159 | &args->host_ip4_addr, |
| 160 | args->host_ip4_prefix_len); |
| 161 | if (args->error) |
| 162 | { |
| 163 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 164 | goto error; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (args->host_ip6_prefix_len) |
| 169 | { |
| 170 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 171 | &args->host_ip6_addr, |
| 172 | args->host_ip6_prefix_len); |
| 173 | if (args->error) |
| 174 | { |
| 175 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 176 | goto error; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* Set vhost memory table */ |
| 181 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 182 | vhost_mem = clib_mem_alloc (i); |
| 183 | memset (vhost_mem, 0, i); |
| 184 | vhost_mem->nregions = 1; |
| 185 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 186 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 187 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 188 | 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] | 189 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 190 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 191 | goto error; |
| 192 | } |
| 193 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 194 | 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] | 195 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 196 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 197 | goto error; |
| 198 | } |
| 199 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 200 | /* set host side up */ |
| 201 | if ((fd = socket (AF_INET, SOCK_STREAM, 0)) > 0) |
| 202 | { |
| 203 | memset (&ifr, 0, sizeof (struct ifreq)); |
| 204 | strncpy (ifr.ifr_name, (char *) args->name, sizeof (ifr.ifr_name) - 1); |
| 205 | _IOCTL (fd, SIOCGIFFLAGS, (void *) &ifr); |
| 206 | ifr.ifr_flags |= IFF_UP | IFF_RUNNING; |
| 207 | _IOCTL (fd, SIOCSIFFLAGS, (void *) &ifr); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 210 | if (!args->hw_addr_set) |
| 211 | { |
| 212 | f64 now = vlib_time_now (vm); |
| 213 | u32 rnd; |
| 214 | rnd = (u32) (now * 1e6); |
| 215 | rnd = random_u32 (&rnd); |
| 216 | |
| 217 | memcpy (args->hw_addr + 2, &rnd, sizeof (rnd)); |
| 218 | args->hw_addr[0] = 2; |
| 219 | args->hw_addr[1] = 0xfe; |
| 220 | } |
| 221 | vif->name = args->name; |
| 222 | args->name = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 223 | vif->net_ns = args->host_namespace; |
| 224 | args->host_namespace = 0; |
| 225 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
| 226 | vif->dev_instance, args->hw_addr, |
| 227 | &vif->hw_if_index, |
| 228 | virtio_eth_flag_change); |
| 229 | if (args->error) |
| 230 | { |
| 231 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 232 | goto error; |
| 233 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 234 | |
| 235 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 236 | vif->sw_if_index = sw->sw_if_index; |
| 237 | args->sw_if_index = vif->sw_if_index; |
| 238 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 239 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 240 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 241 | virtio_input_node.index); |
| 242 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 243 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 244 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 245 | vif->per_interface_next_index = ~0; |
| 246 | vif->type = VIRTIO_IF_TYPE_TAP; |
| 247 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 248 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 249 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 250 | goto done; |
| 251 | |
| 252 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 253 | if (err) |
| 254 | { |
| 255 | ASSERT (args->error == 0); |
| 256 | args->error = err; |
| 257 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 258 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 259 | if (vif->tap_fd != -1) |
| 260 | close (vif->tap_fd); |
| 261 | if (vif->fd != -1) |
| 262 | close (vif->fd); |
| 263 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i); |
| 264 | memset (vif, 0, sizeof (virtio_if_t)); |
| 265 | pool_put (vim->interfaces, vif); |
| 266 | |
| 267 | done: |
| 268 | if (vhost_mem) |
| 269 | clib_mem_free (vhost_mem); |
Steven | 37eba0d | 2017-12-02 20:17:27 -0800 | [diff] [blame] | 270 | if (fd != -1) |
| 271 | close (fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | int |
| 275 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 276 | { |
| 277 | vnet_main_t *vnm = vnet_get_main (); |
| 278 | virtio_main_t *mm = &virtio_main; |
| 279 | int i; |
| 280 | virtio_if_t *vif; |
| 281 | vnet_hw_interface_t *hw; |
| 282 | |
| 283 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 284 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 285 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 286 | |
| 287 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 288 | |
| 289 | /* bring down the interface */ |
| 290 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 291 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
| 292 | |
| 293 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 294 | vif->hw_if_index = ~0; |
| 295 | |
| 296 | if (vif->tap_fd != -1) |
| 297 | close (vif->tap_fd); |
| 298 | if (vif->fd != -1) |
| 299 | close (vif->fd); |
| 300 | |
| 301 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i); |
| 302 | vec_free (vif->vrings); |
| 303 | |
| 304 | memset (vif, 0, sizeof (*vif)); |
| 305 | pool_put (mm->interfaces, vif); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 312 | { |
| 313 | vnet_main_t *vnm = vnet_get_main (); |
| 314 | virtio_main_t *mm = &virtio_main; |
| 315 | virtio_if_t *vif; |
| 316 | vnet_hw_interface_t *hi; |
| 317 | tap_interface_details_t *r_tapids = NULL; |
| 318 | tap_interface_details_t *tapid = NULL; |
| 319 | |
| 320 | /* *INDENT-OFF* */ |
| 321 | pool_foreach (vif, mm->interfaces, |
| 322 | vec_add2(r_tapids, tapid, 1); |
| 323 | memset (tapid, 0, sizeof (*tapid)); |
| 324 | tapid->sw_if_index = vif->sw_if_index; |
| 325 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 326 | clib_memcpy(tapid->dev_name, hi->name, |
| 327 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 328 | strlen ((const char *) hi->name))); |
| 329 | ); |
| 330 | /* *INDENT-ON* */ |
| 331 | |
| 332 | *out_tapids = r_tapids; |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static clib_error_t * |
| 338 | tap_init (vlib_main_t * vm) |
| 339 | { |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | VLIB_INIT_FUNCTION (tap_init); |
| 345 | |
| 346 | /* |
| 347 | * fd.io coding-style-patch-verification: ON |
| 348 | * |
| 349 | * Local Variables: |
| 350 | * eval: (c-set-style "gnu") |
| 351 | * End: |
| 352 | */ |