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 | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 34 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 35 | #include <vnet/devices/virtio/virtio.h> |
| 36 | #include <vnet/devices/virtio/tap.h> |
| 37 | |
| 38 | #define _IOCTL(fd,a,...) \ |
| 39 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 40 | { \ |
| 41 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
| 42 | goto error; \ |
| 43 | } |
| 44 | |
| 45 | static u32 |
| 46 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 47 | u32 flags) |
| 48 | { |
| 49 | /* nothing for now */ |
| 50 | return 0; |
| 51 | } |
| 52 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 53 | int |
| 54 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 55 | { |
| 56 | vnet_main_t *vnm = vnet_get_main (); |
| 57 | virtio_main_t *vim = &virtio_main; |
| 58 | vnet_sw_interface_t *sw; |
| 59 | vnet_hw_interface_t *hw; |
| 60 | int i; |
| 61 | clib_error_t *err = 0; |
| 62 | struct ifreq ifr; |
| 63 | size_t hdrsz; |
| 64 | struct vhost_memory *vhost_mem = 0; |
| 65 | virtio_if_t *vif = 0; |
| 66 | int rv = 0; |
| 67 | |
| 68 | memset (&ifr, 0, sizeof (ifr)); |
| 69 | pool_get (vim->interfaces, vif); |
| 70 | vif->dev_instance = vif - vim->interfaces; |
| 71 | vif->tap_fd = -1; |
| 72 | |
| 73 | if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0) |
| 74 | { |
| 75 | rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 76 | goto error; |
| 77 | } |
| 78 | |
| 79 | _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features); |
| 80 | |
| 81 | if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 82 | { |
| 83 | rv = VNET_API_ERROR_UNSUPPORTED; |
| 84 | goto error; |
| 85 | } |
| 86 | |
| 87 | if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0) |
| 88 | { |
| 89 | rv = VNET_API_ERROR_UNSUPPORTED; |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0) |
| 94 | { |
| 95 | rv = VNET_API_ERROR_UNSUPPORTED; |
| 96 | goto error; |
| 97 | } |
| 98 | |
| 99 | vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF; |
| 100 | vif->features |= 1ULL << VIRTIO_F_VERSION_1; |
| 101 | vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC; |
| 102 | |
| 103 | _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); |
| 104 | |
| 105 | if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 106 | { |
| 107 | rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 108 | goto error; |
| 109 | } |
| 110 | |
| 111 | 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] | 112 | strncpy (ifr.ifr_ifrn.ifrn_name, (char *) args->name, IF_NAMESIZE - 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 113 | _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); |
| 114 | |
| 115 | vif->ifindex = if_nametoindex ((char *) args->name); |
| 116 | |
| 117 | unsigned int offload = 0; |
| 118 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
| 119 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 120 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 121 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 122 | |
| 123 | if (args->net_ns) |
| 124 | { |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 125 | err = vnet_netlink_set_if_namespace (vif->ifindex, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 126 | (char *) args->net_ns); |
| 127 | if (err) |
| 128 | { |
| 129 | rv = VNET_API_ERROR_NAMESPACE_CREATE; |
| 130 | goto error; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Set vhost memory table */ |
| 135 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 136 | vhost_mem = clib_mem_alloc (i); |
| 137 | memset (vhost_mem, 0, i); |
| 138 | vhost_mem->nregions = 1; |
| 139 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 140 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 141 | |
| 142 | if ((err = virtio_vring_init (vm, vif, 0, args->rx_ring_sz))) |
| 143 | { |
| 144 | rv = VNET_API_ERROR_VIRTIO_INIT; |
| 145 | goto error; |
| 146 | } |
| 147 | |
| 148 | if ((err = virtio_vring_init (vm, vif, 1, args->tx_ring_sz))) |
| 149 | { |
| 150 | rv = VNET_API_ERROR_VIRTIO_INIT; |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | if (!args->hw_addr_set) |
| 155 | { |
| 156 | f64 now = vlib_time_now (vm); |
| 157 | u32 rnd; |
| 158 | rnd = (u32) (now * 1e6); |
| 159 | rnd = random_u32 (&rnd); |
| 160 | |
| 161 | memcpy (args->hw_addr + 2, &rnd, sizeof (rnd)); |
| 162 | args->hw_addr[0] = 2; |
| 163 | args->hw_addr[1] = 0xfe; |
| 164 | } |
| 165 | vif->name = args->name; |
| 166 | args->name = 0; |
| 167 | vif->net_ns = args->net_ns; |
| 168 | args->net_ns = 0; |
| 169 | err = ethernet_register_interface (vnm, virtio_device_class.index, |
| 170 | vif->dev_instance, args->hw_addr, |
| 171 | &vif->hw_if_index, |
| 172 | virtio_eth_flag_change); |
| 173 | if (err) |
| 174 | rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 175 | |
| 176 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 177 | vif->sw_if_index = sw->sw_if_index; |
| 178 | args->sw_if_index = vif->sw_if_index; |
| 179 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 180 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 181 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 182 | virtio_input_node.index); |
| 183 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 184 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 185 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 186 | vif->per_interface_next_index = ~0; |
| 187 | vif->type = VIRTIO_IF_TYPE_TAP; |
| 188 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 189 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 190 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
| 191 | goto done; |
| 192 | |
| 193 | error: |
| 194 | if (vif->tap_fd != -1) |
| 195 | close (vif->tap_fd); |
| 196 | if (vif->fd != -1) |
| 197 | close (vif->fd); |
| 198 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i); |
| 199 | memset (vif, 0, sizeof (virtio_if_t)); |
| 200 | pool_put (vim->interfaces, vif); |
| 201 | |
| 202 | done: |
| 203 | if (vhost_mem) |
| 204 | clib_mem_free (vhost_mem); |
| 205 | |
| 206 | return rv; |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 211 | { |
| 212 | vnet_main_t *vnm = vnet_get_main (); |
| 213 | virtio_main_t *mm = &virtio_main; |
| 214 | int i; |
| 215 | virtio_if_t *vif; |
| 216 | vnet_hw_interface_t *hw; |
| 217 | |
| 218 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 219 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 220 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 221 | |
| 222 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 223 | |
| 224 | /* bring down the interface */ |
| 225 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 226 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
| 227 | |
| 228 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 229 | vif->hw_if_index = ~0; |
| 230 | |
| 231 | if (vif->tap_fd != -1) |
| 232 | close (vif->tap_fd); |
| 233 | if (vif->fd != -1) |
| 234 | close (vif->fd); |
| 235 | |
| 236 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i); |
| 237 | vec_free (vif->vrings); |
| 238 | |
| 239 | memset (vif, 0, sizeof (*vif)); |
| 240 | pool_put (mm->interfaces, vif); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int |
| 246 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 247 | { |
| 248 | vnet_main_t *vnm = vnet_get_main (); |
| 249 | virtio_main_t *mm = &virtio_main; |
| 250 | virtio_if_t *vif; |
| 251 | vnet_hw_interface_t *hi; |
| 252 | tap_interface_details_t *r_tapids = NULL; |
| 253 | tap_interface_details_t *tapid = NULL; |
| 254 | |
| 255 | /* *INDENT-OFF* */ |
| 256 | pool_foreach (vif, mm->interfaces, |
| 257 | vec_add2(r_tapids, tapid, 1); |
| 258 | memset (tapid, 0, sizeof (*tapid)); |
| 259 | tapid->sw_if_index = vif->sw_if_index; |
| 260 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 261 | clib_memcpy(tapid->dev_name, hi->name, |
| 262 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 263 | strlen ((const char *) hi->name))); |
| 264 | ); |
| 265 | /* *INDENT-ON* */ |
| 266 | |
| 267 | *out_tapids = r_tapids; |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static clib_error_t * |
| 273 | tap_init (vlib_main_t * vm) |
| 274 | { |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | VLIB_INIT_FUNCTION (tap_init); |
| 280 | |
| 281 | /* |
| 282 | * fd.io coding-style-patch-verification: ON |
| 283 | * |
| 284 | * Local Variables: |
| 285 | * eval: (c-set-style "gnu") |
| 286 | * End: |
| 287 | */ |