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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <sys/types.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>
30 #include <linux/netlink.h>
31 #include <linux/rtnetlink.h>
33 #include <vlib/vlib.h>
34 #include <vlib/unix/unix.h>
35 #include <vnet/ethernet/ethernet.h>
36 #include <vnet/ip/ip4_packet.h>
37 #include <vnet/ip/ip6_packet.h>
38 #include <vnet/devices/netlink.h>
39 #include <vnet/devices/virtio/virtio.h>
40 #include <vnet/devices/tap/tap.h>
44 #define _IOCTL(fd,a,...) \
45 if (ioctl (fd, a, __VA_ARGS__) < 0) \
47 err = clib_error_return_unix (0, "ioctl(" #a ")"); \
52 virtio_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
56 //TODO On MTU change call vnet_netlink_set_if_mtu
61 open_netns_fd (char *netns)
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);
71 s = format (0, "/var/run/netns/%s%c", netns, 0);
73 fd = open ((char *) s, O_RDONLY);
78 #define TAP_MAX_INSTANCE 1024
81 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
83 vnet_main_t *vnm = vnet_get_main ();
84 vlib_thread_main_t *thm = vlib_get_thread_main ();
85 virtio_main_t *vim = &virtio_main;
86 tap_main_t *tm = &tap_main;
87 vnet_sw_interface_t *sw;
88 vnet_hw_interface_t *hw;
90 int old_netns_fd = -1;
93 struct vhost_memory *vhost_mem = 0;
95 clib_error_t *err = 0;
99 if (clib_bitmap_get (tm->tap_ids, args->id))
101 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
102 args->error = clib_error_return (0, "interface already exists");
108 args->id = clib_bitmap_first_clear (tm->tap_ids);
111 if (args->id > TAP_MAX_INSTANCE)
113 args->rv = VNET_API_ERROR_UNSPECIFIED;
114 args->error = clib_error_return (0, "cannot find free interface id");
118 clib_memset (&ifr, 0, sizeof (ifr));
119 pool_get (vim->interfaces, vif);
120 vif->dev_instance = vif - vim->interfaces;
124 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
126 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
127 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
131 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
133 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
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");
141 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
143 args->rv = VNET_API_ERROR_UNSUPPORTED;
144 args->error = clib_error_return (0, "vhost-net backend doesn't support "
145 "VIRTIO_RING_F_INDIRECT_DESC feature");
149 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
151 args->rv = VNET_API_ERROR_UNSUPPORTED;
152 args->error = clib_error_return (0, "vhost-net backend doesn't support "
153 "VIRTIO_F_VERSION_1 features");
157 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
158 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
159 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
161 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
163 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
165 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
166 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
170 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
171 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
172 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
174 unsigned int offload = 0;
175 hdrsz = sizeof (struct virtio_net_hdr_v1);
176 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
177 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
178 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
180 /* if namespace is specified, all further netlink messages should be excuted
181 after we change our net namespace */
182 if (args->host_namespace)
186 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
187 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
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);
194 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
195 (char *) args->host_if_name);
198 args->rv = VNET_API_ERROR_NETLINK_ERROR;
201 rc = setns (fd, CLONE_NEWNET);
205 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
206 args->error = clib_error_return_unix (0, "setns '%s'",
207 args->host_namespace);
210 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
212 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
213 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
220 if (args->host_if_name)
222 args->error = vnet_netlink_set_link_name (vif->ifindex,
227 args->rv = VNET_API_ERROR_NETLINK_ERROR;
233 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
235 args->error = vnet_netlink_set_link_addr (vif->ifindex,
236 args->host_mac_addr);
239 args->rv = VNET_API_ERROR_NETLINK_ERROR;
244 if (args->host_bridge)
246 args->error = vnet_netlink_set_link_master (vif->ifindex,
247 (char *) args->host_bridge);
250 args->rv = VNET_API_ERROR_NETLINK_ERROR;
256 if (args->host_ip4_prefix_len)
258 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
259 &args->host_ip4_addr,
260 args->host_ip4_prefix_len);
263 args->rv = VNET_API_ERROR_NETLINK_ERROR;
268 if (args->host_ip6_prefix_len)
270 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
271 &args->host_ip6_addr,
272 args->host_ip6_prefix_len);
275 args->rv = VNET_API_ERROR_NETLINK_ERROR;
280 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
283 args->rv = VNET_API_ERROR_NETLINK_ERROR;
287 if (args->host_ip4_gw_set)
289 args->error = vnet_netlink_add_ip4_route (0, 0, &args->host_ip4_gw);
292 args->rv = VNET_API_ERROR_NETLINK_ERROR;
297 if (args->host_ip6_gw_set)
299 args->error = vnet_netlink_add_ip6_route (0, 0, &args->host_ip6_gw);
302 args->rv = VNET_API_ERROR_NETLINK_ERROR;
307 /* switch back to old net namespace */
308 if (args->host_namespace)
310 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
312 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
313 args->error = clib_error_return_unix (0, "setns '%s'",
314 args->host_namespace);
319 /* Set vhost memory table */
320 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
321 vhost_mem = clib_mem_alloc (i);
322 clib_memset (vhost_mem, 0, i);
323 vhost_mem->nregions = 1;
324 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
325 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
327 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
329 args->rv = VNET_API_ERROR_INIT_FAILED;
333 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
335 args->rv = VNET_API_ERROR_INIT_FAILED;
339 if (!args->mac_addr_set)
341 f64 now = vlib_time_now (vm);
343 rnd = (u32) (now * 1e6);
344 rnd = random_u32 (&rnd);
346 memcpy (args->mac_addr + 2, &rnd, sizeof (rnd));
347 args->mac_addr[0] = 2;
348 args->mac_addr[1] = 0xfe;
350 vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256;
351 vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256;
352 vif->host_if_name = args->host_if_name;
353 args->host_if_name = 0;
354 vif->net_ns = args->host_namespace;
355 args->host_namespace = 0;
356 vif->host_bridge = args->host_bridge;
357 args->host_bridge = 0;
358 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
359 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
360 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
361 if (args->host_ip4_prefix_len)
362 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
363 if (args->host_ip6_prefix_len)
364 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
366 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
370 virtio_eth_flag_change);
373 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
377 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 1);
378 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
379 vif->sw_if_index = sw->sw_if_index;
380 args->sw_if_index = vif->sw_if_index;
381 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
382 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
383 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
384 virtio_input_node.index);
385 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
386 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
387 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
388 vif->per_interface_next_index = ~0;
389 vif->type = VIRTIO_IF_TYPE_TAP;
390 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
391 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
392 VNET_HW_INTERFACE_FLAG_LINK_UP);
393 if (thm->n_vlib_mains > 1)
394 clib_spinlock_init (&vif->lockp);
400 ASSERT (args->error == 0);
402 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
404 if (vif->tap_fd != -1)
408 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
409 vec_free (vif->vrings);
410 clib_memset (vif, 0, sizeof (virtio_if_t));
411 pool_put (vim->interfaces, vif);
415 clib_mem_free (vhost_mem);
416 if (old_netns_fd != -1)
417 close (old_netns_fd);
421 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
423 vnet_main_t *vnm = vnet_get_main ();
424 virtio_main_t *mm = &virtio_main;
425 tap_main_t *tm = &tap_main;
428 vnet_hw_interface_t *hw;
430 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
431 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
432 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
434 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
436 /* bring down the interface */
437 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
438 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
439 vnet_hw_interface_unassign_rx_thread (vnm, vif->hw_if_index, 0);
441 ethernet_delete_interface (vnm, vif->hw_if_index);
442 vif->hw_if_index = ~0;
444 if (vif->tap_fd != -1)
449 vec_foreach_index (i, vif->vrings) virtio_vring_free (vm, vif, i);
450 vec_free (vif->vrings);
452 tm->tap_ids = clib_bitmap_set (tm->tap_ids, vif->id, 0);
453 clib_spinlock_free (&vif->lockp);
454 clib_memset (vif, 0, sizeof (*vif));
455 pool_put (mm->interfaces, vif);
461 tap_dump_ifs (tap_interface_details_t ** out_tapids)
463 vnet_main_t *vnm = vnet_get_main ();
464 virtio_main_t *mm = &virtio_main;
466 vnet_hw_interface_t *hi;
467 tap_interface_details_t *r_tapids = NULL;
468 tap_interface_details_t *tapid = NULL;
471 pool_foreach (vif, mm->interfaces,
472 vec_add2(r_tapids, tapid, 1);
473 clib_memset (tapid, 0, sizeof (*tapid));
475 tapid->sw_if_index = vif->sw_if_index;
476 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
477 clib_memcpy(tapid->dev_name, hi->name,
478 MIN (ARRAY_LEN (tapid->dev_name) - 1,
479 strlen ((const char *) hi->name)));
480 tapid->rx_ring_sz = vif->rx_ring_sz;
481 tapid->tx_ring_sz = vif->tx_ring_sz;
482 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
483 if (vif->host_if_name)
485 clib_memcpy(tapid->host_if_name, vif->host_if_name,
486 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
487 strlen ((const char *) vif->host_if_name)));
491 clib_memcpy(tapid->host_namespace, vif->net_ns,
492 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
493 strlen ((const char *) vif->net_ns)));
495 if (vif->host_bridge)
497 clib_memcpy(tapid->host_bridge, vif->host_bridge,
498 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
499 strlen ((const char *) vif->host_bridge)));
501 if (vif->host_ip4_prefix_len)
502 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
503 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
504 if (vif->host_ip6_prefix_len)
505 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
506 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
510 *out_tapids = r_tapids;
515 static clib_error_t *
516 tap_init (vlib_main_t * vm)
518 tap_main_t *tm = &tap_main;
519 clib_error_t *error = 0;
521 tm->log_default = vlib_log_register_class ("tap", 0);
522 vlib_log_debug (tm->log_default, "initialized");
527 VLIB_INIT_FUNCTION (tap_init);
530 * fd.io coding-style-patch-verification: ON
533 * eval: (c-set-style "gnu")