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> |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 34 | #include <vlib/log.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 35 | #include <vlib/unix/unix.h> |
| 36 | #include <vnet/ethernet/ethernet.h> |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 37 | #include <vnet/ip/ip4_packet.h> |
| 38 | #include <vnet/ip/ip6_packet.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 39 | #include <vnet/devices/netlink.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 40 | #include <vnet/devices/virtio/virtio.h> |
Damjan Marion | c99b4cd | 2017-12-04 15:25:58 +0100 | [diff] [blame] | 41 | #include <vnet/devices/tap/tap.h> |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 42 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 43 | tap_main_t tap_main; |
| 44 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 45 | #define _IOCTL(fd,a,...) \ |
| 46 | if (ioctl (fd, a, __VA_ARGS__) < 0) \ |
| 47 | { \ |
| 48 | err = clib_error_return_unix (0, "ioctl(" #a ")"); \ |
| 49 | goto error; \ |
| 50 | } |
| 51 | |
| 52 | static u32 |
| 53 | virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, |
| 54 | u32 flags) |
| 55 | { |
| 56 | /* nothing for now */ |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 57 | //TODO On MTU change call vnet_netlink_set_if_mtu |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 61 | static int |
| 62 | open_netns_fd (char *netns) |
| 63 | { |
| 64 | u8 *s = 0; |
| 65 | int fd; |
| 66 | |
| 67 | if (strncmp (netns, "pid:", 4) == 0) |
| 68 | s = format (0, "/proc/%u/ns/net%c", atoi (netns + 4), 0); |
| 69 | else if (netns[0] == '/') |
| 70 | s = format (0, "%s%c", netns, 0); |
| 71 | else |
| 72 | s = format (0, "/var/run/netns/%s%c", netns, 0); |
| 73 | |
| 74 | fd = open ((char *) s, O_RDONLY); |
| 75 | vec_free (s); |
| 76 | return fd; |
| 77 | } |
| 78 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame^] | 79 | #define TAP_MAX_INSTANCE 1024 |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 80 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 81 | void |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 82 | tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args) |
| 83 | { |
| 84 | vnet_main_t *vnm = vnet_get_main (); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 85 | vlib_thread_main_t *thm = vlib_get_thread_main (); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 86 | virtio_main_t *vim = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 87 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 88 | vnet_sw_interface_t *sw; |
| 89 | vnet_hw_interface_t *hw; |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 90 | int i; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 91 | int old_netns_fd = -1; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 92 | struct ifreq ifr; |
| 93 | size_t hdrsz; |
| 94 | struct vhost_memory *vhost_mem = 0; |
| 95 | virtio_if_t *vif = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 96 | clib_error_t *err = 0; |
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 | |
| 119 | memset (&ifr, 0, sizeof (ifr)); |
| 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 | |
| 134 | if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0) |
| 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 | |
| 142 | if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0) |
| 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 | |
| 150 | if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0) |
| 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 | |
| 158 | vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF; |
| 159 | vif->features |= 1ULL << VIRTIO_F_VERSION_1; |
| 160 | vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC; |
| 161 | |
| 162 | _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features); |
| 163 | |
| 164 | if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) |
| 165 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 166 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 167 | args->error = clib_error_return_unix (0, "open '/dev/net/tun'"); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | 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] | 172 | _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 173 | vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 174 | |
| 175 | unsigned int offload = 0; |
| 176 | hdrsz = sizeof (struct virtio_net_hdr_v1); |
| 177 | _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload); |
| 178 | _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz); |
| 179 | _IOCTL (vif->fd, VHOST_SET_OWNER, 0); |
| 180 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 181 | /* if namespace is specified, all further netlink messages should be excuted |
| 182 | after we change our net namespace */ |
| 183 | if (args->host_namespace) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 184 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 185 | int fd; |
| 186 | old_netns_fd = open ("/proc/self/ns/net", O_RDONLY); |
| 187 | if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1) |
| 188 | { |
| 189 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 190 | args->error = clib_error_return_unix (0, "open_netns_fd '%s'", |
| 191 | args->host_namespace); |
| 192 | goto error; |
| 193 | } |
| 194 | args->error = vnet_netlink_set_link_netns (vif->ifindex, fd, |
| 195 | (char *) args->host_if_name); |
| 196 | if (args->error) |
| 197 | { |
| 198 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 199 | goto error; |
| 200 | } |
| 201 | if (setns (fd, CLONE_NEWNET) == -1) |
| 202 | { |
| 203 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 204 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 205 | args->host_namespace); |
| 206 | goto error; |
| 207 | } |
| 208 | close (fd); |
| 209 | if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0) |
| 210 | { |
| 211 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 212 | args->error = clib_error_return_unix (0, "if_nametoindex '%s'", |
| 213 | args->host_if_name); |
| 214 | goto error; |
| 215 | } |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | if (args->host_if_name) |
| 220 | { |
| 221 | args->error = vnet_netlink_set_link_name (vif->ifindex, |
| 222 | (char *) |
| 223 | args->host_if_name); |
| 224 | if (args->error) |
| 225 | { |
| 226 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 227 | goto error; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (!ethernet_mac_address_is_zero (args->host_mac_addr)) |
| 233 | { |
| 234 | args->error = vnet_netlink_set_link_addr (vif->ifindex, |
| 235 | args->host_mac_addr); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 236 | if (args->error) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 237 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 238 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 239 | goto error; |
| 240 | } |
| 241 | } |
| 242 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 243 | if (args->host_bridge) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 244 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 245 | args->error = vnet_netlink_set_link_master (vif->ifindex, |
| 246 | (char *) args->host_bridge); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 247 | if (args->error) |
| 248 | { |
| 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 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 255 | if (args->host_ip4_prefix_len) |
| 256 | { |
| 257 | args->error = vnet_netlink_add_ip4_addr (vif->ifindex, |
| 258 | &args->host_ip4_addr, |
| 259 | args->host_ip4_prefix_len); |
| 260 | if (args->error) |
| 261 | { |
| 262 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 263 | goto error; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | if (args->host_ip6_prefix_len) |
| 268 | { |
| 269 | args->error = vnet_netlink_add_ip6_addr (vif->ifindex, |
| 270 | &args->host_ip6_addr, |
| 271 | args->host_ip6_prefix_len); |
| 272 | if (args->error) |
| 273 | { |
| 274 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 275 | goto error; |
| 276 | } |
| 277 | } |
| 278 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 279 | args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ ); |
| 280 | if (args->error) |
| 281 | { |
| 282 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 283 | goto error; |
| 284 | } |
| 285 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 286 | if (args->host_ip4_gw_set) |
| 287 | { |
| 288 | args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw); |
| 289 | if (args->error) |
| 290 | { |
| 291 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 292 | goto error; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | if (args->host_ip6_gw_set) |
| 297 | { |
| 298 | args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw); |
| 299 | if (args->error) |
| 300 | { |
| 301 | args->rv = VNET_API_ERROR_NETLINK_ERROR; |
| 302 | goto error; |
| 303 | } |
| 304 | } |
| 305 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 306 | /* switch back to old net namespace */ |
| 307 | if (args->host_namespace) |
| 308 | { |
| 309 | if (setns (old_netns_fd, CLONE_NEWNET) == -1) |
| 310 | { |
| 311 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_2; |
| 312 | args->error = clib_error_return_unix (0, "setns '%s'", |
| 313 | args->host_namespace); |
| 314 | goto error; |
| 315 | } |
| 316 | } |
| 317 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 318 | /* Set vhost memory table */ |
| 319 | i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region); |
| 320 | vhost_mem = clib_mem_alloc (i); |
| 321 | memset (vhost_mem, 0, i); |
| 322 | vhost_mem->nregions = 1; |
| 323 | vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096; |
| 324 | _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem); |
| 325 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 326 | 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] | 327 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 328 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 329 | goto error; |
| 330 | } |
| 331 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 332 | 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] | 333 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 334 | args->rv = VNET_API_ERROR_INIT_FAILED; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 335 | goto error; |
| 336 | } |
| 337 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 338 | if (!args->mac_addr_set) |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 339 | { |
| 340 | f64 now = vlib_time_now (vm); |
| 341 | u32 rnd; |
| 342 | rnd = (u32) (now * 1e6); |
| 343 | rnd = random_u32 (&rnd); |
| 344 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 345 | memcpy (args->mac_addr + 2, &rnd, sizeof (rnd)); |
| 346 | args->mac_addr[0] = 2; |
| 347 | args->mac_addr[1] = 0xfe; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 348 | } |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 349 | vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256; |
| 350 | vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 351 | vif->host_if_name = args->host_if_name; |
| 352 | args->host_if_name = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 353 | vif->net_ns = args->host_namespace; |
| 354 | args->host_namespace = 0; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 355 | vif->host_bridge = args->host_bridge; |
| 356 | args->host_bridge = 0; |
| 357 | clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6); |
| 358 | vif->host_ip4_prefix_len = args->host_ip4_prefix_len; |
| 359 | vif->host_ip6_prefix_len = args->host_ip6_prefix_len; |
| 360 | if (args->host_ip4_prefix_len) |
| 361 | clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4); |
| 362 | if (args->host_ip6_prefix_len) |
| 363 | clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16); |
| 364 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 365 | args->error = ethernet_register_interface (vnm, virtio_device_class.index, |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 366 | vif->dev_instance, |
| 367 | args->mac_addr, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 368 | &vif->hw_if_index, |
| 369 | virtio_eth_flag_change); |
| 370 | if (args->error) |
| 371 | { |
| 372 | args->rv = VNET_API_ERROR_INVALID_REGISTRATION; |
| 373 | goto error; |
| 374 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 375 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame^] | 376 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 377 | sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index); |
| 378 | vif->sw_if_index = sw->sw_if_index; |
| 379 | args->sw_if_index = vif->sw_if_index; |
| 380 | hw = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 381 | hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE; |
| 382 | vnet_hw_interface_set_input_node (vnm, vif->hw_if_index, |
| 383 | virtio_input_node.index); |
| 384 | vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0); |
| 385 | vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0, |
| 386 | VNET_HW_INTERFACE_RX_MODE_DEFAULT); |
| 387 | vif->per_interface_next_index = ~0; |
| 388 | vif->type = VIRTIO_IF_TYPE_TAP; |
| 389 | vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP; |
| 390 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, |
| 391 | VNET_HW_INTERFACE_FLAG_LINK_UP); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 392 | if (thm->n_vlib_mains > 1) |
| 393 | clib_spinlock_init (&vif->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 394 | goto done; |
| 395 | |
| 396 | error: |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 397 | if (err) |
| 398 | { |
| 399 | ASSERT (args->error == 0); |
| 400 | args->error = err; |
| 401 | args->rv = VNET_API_ERROR_SYSCALL_ERROR_3; |
| 402 | } |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 403 | if (vif->tap_fd != -1) |
| 404 | close (vif->tap_fd); |
| 405 | if (vif->fd != -1) |
| 406 | close (vif->fd); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 407 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame^] | 408 | vec_free (vif->vrings); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 409 | memset (vif, 0, sizeof (virtio_if_t)); |
| 410 | pool_put (vim->interfaces, vif); |
| 411 | |
| 412 | done: |
| 413 | if (vhost_mem) |
| 414 | clib_mem_free (vhost_mem); |
Damjan Marion | 4e671d2 | 2017-12-09 21:19:01 +0100 | [diff] [blame] | 415 | if (old_netns_fd != -1) |
| 416 | close (old_netns_fd); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | int |
| 420 | tap_delete_if (vlib_main_t * vm, u32 sw_if_index) |
| 421 | { |
| 422 | vnet_main_t *vnm = vnet_get_main (); |
| 423 | virtio_main_t *mm = &virtio_main; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 424 | tap_main_t *tm = &tap_main; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 425 | int i; |
| 426 | virtio_if_t *vif; |
| 427 | vnet_hw_interface_t *hw; |
| 428 | |
| 429 | hw = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 430 | if (hw == NULL || virtio_device_class.index != hw->dev_class_index) |
| 431 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 432 | |
| 433 | vif = pool_elt_at_index (mm->interfaces, hw->dev_instance); |
| 434 | |
| 435 | /* bring down the interface */ |
| 436 | vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0); |
| 437 | vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0); |
Steven | 0b85673 | 2018-02-28 11:00:34 -0800 | [diff] [blame] | 438 | vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 439 | |
| 440 | ethernet_delete_interface (vnm, vif->hw_if_index); |
| 441 | vif->hw_if_index = ~0; |
| 442 | |
| 443 | if (vif->tap_fd != -1) |
| 444 | close (vif->tap_fd); |
| 445 | if (vif->fd != -1) |
| 446 | close (vif->fd); |
| 447 | |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 448 | vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 449 | vec_free (vif->vrings); |
| 450 | |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame^] | 451 | tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0); |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 452 | clib_spinlock_free (&vif->lockp); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 453 | memset (vif, 0, sizeof (*vif)); |
| 454 | pool_put (mm->interfaces, vif); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | int |
| 460 | tap_dump_ifs (tap_interface_details_t ** out_tapids) |
| 461 | { |
| 462 | vnet_main_t *vnm = vnet_get_main (); |
| 463 | virtio_main_t *mm = &virtio_main; |
| 464 | virtio_if_t *vif; |
| 465 | vnet_hw_interface_t *hi; |
| 466 | tap_interface_details_t *r_tapids = NULL; |
| 467 | tap_interface_details_t *tapid = NULL; |
| 468 | |
| 469 | /* *INDENT-OFF* */ |
| 470 | pool_foreach (vif, mm->interfaces, |
| 471 | vec_add2(r_tapids, tapid, 1); |
| 472 | memset (tapid, 0, sizeof (*tapid)); |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 473 | tapid->id = vif->id; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 474 | tapid->sw_if_index = vif->sw_if_index; |
| 475 | hi = vnet_get_hw_interface (vnm, vif->hw_if_index); |
| 476 | clib_memcpy(tapid->dev_name, hi->name, |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 477 | MIN (ARRAY_LEN (tapid->dev_name) - 1, |
| 478 | strlen ((const char *) hi->name))); |
| 479 | tapid->rx_ring_sz = vif->rx_ring_sz; |
| 480 | tapid->tx_ring_sz = vif->tx_ring_sz; |
| 481 | clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6); |
| 482 | if (vif->host_if_name) |
| 483 | { |
| 484 | clib_memcpy(tapid->host_if_name, vif->host_if_name, |
| 485 | MIN (ARRAY_LEN (tapid->host_if_name) - 1, |
| 486 | strlen ((const char *) vif->host_if_name))); |
| 487 | } |
| 488 | if (vif->net_ns) |
| 489 | { |
| 490 | clib_memcpy(tapid->host_namespace, vif->net_ns, |
| 491 | MIN (ARRAY_LEN (tapid->host_namespace) - 1, |
| 492 | strlen ((const char *) vif->net_ns))); |
| 493 | } |
| 494 | if (vif->host_bridge) |
| 495 | { |
| 496 | clib_memcpy(tapid->host_bridge, vif->host_bridge, |
| 497 | MIN (ARRAY_LEN (tapid->host_bridge) - 1, |
| 498 | strlen ((const char *) vif->host_bridge))); |
| 499 | } |
| 500 | if (vif->host_ip4_prefix_len) |
| 501 | clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4); |
| 502 | tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len; |
| 503 | if (vif->host_ip6_prefix_len) |
| 504 | clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16); |
| 505 | tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 506 | ); |
| 507 | /* *INDENT-ON* */ |
| 508 | |
| 509 | *out_tapids = r_tapids; |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 514 | #define vlib_log_info(...) vlib_log(VLIB_LOG_LEVEL_INFO, __VA_ARGS__) |
| 515 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 516 | static clib_error_t * |
| 517 | tap_init (vlib_main_t * vm) |
| 518 | { |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 519 | tap_main_t *tm = &tap_main; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 520 | clib_error_t *error; |
| 521 | error = vlib_call_init_function (vm, vlib_log_init); |
| 522 | if (error) |
| 523 | return error; |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame^] | 524 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 525 | tm->log_default = vlib_log_register_class ("tap", 0); |
| 526 | vlib_log_info (tm->log_default, "initialized"); |
Neale Ranns | cbe8d65 | 2018-04-27 04:42:47 -0700 | [diff] [blame^] | 527 | |
| 528 | return NULL; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | VLIB_INIT_FUNCTION (tap_init); |
| 532 | |
| 533 | /* |
| 534 | * fd.io coding-style-patch-verification: ON |
| 535 | * |
| 536 | * Local Variables: |
| 537 | * eval: (c-set-style "gnu") |
| 538 | * End: |
| 539 | */ |