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);
80 tap_create_if (vlib_main_t * vm, tap_create_if_args_t * args)
82 vnet_main_t *vnm = vnet_get_main ();
83 virtio_main_t *vim = &virtio_main;
84 tap_main_t *tm = &tap_main;
85 vnet_sw_interface_t *sw;
86 vnet_hw_interface_t *hw;
88 int old_netns_fd = -1;
91 struct vhost_memory *vhost_mem = 0;
93 clib_error_t *err = 0;
98 p = hash_get (tm->dev_instance_by_interface_id, args->id);
101 args->rv = VNET_API_ERROR_INVALID_INTERFACE;
102 args->error = clib_error_return (0, "interface already exists");
111 args->id = tm->last_used_interface_id++;
112 p = hash_get (tm->dev_instance_by_interface_id, args->id);
119 args->rv = VNET_API_ERROR_UNSPECIFIED;
121 clib_error_return (0, "cannot find free interface id");
126 memset (&ifr, 0, sizeof (ifr));
127 pool_get (vim->interfaces, vif);
128 vif->dev_instance = vif - vim->interfaces;
132 hash_set (tm->dev_instance_by_interface_id, vif->id, vif->dev_instance);
134 if ((vif->fd = open ("/dev/vhost-net", O_RDWR | O_NONBLOCK)) < 0)
136 args->rv = VNET_API_ERROR_SYSCALL_ERROR_1;
137 args->error = clib_error_return_unix (0, "open '/dev/vhost-net'");
141 _IOCTL (vif->fd, VHOST_GET_FEATURES, &vif->remote_features);
143 if ((vif->remote_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) == 0)
145 args->rv = VNET_API_ERROR_UNSUPPORTED;
146 args->error = clib_error_return (0, "vhost-net backend doesn't support "
147 "VIRTIO_NET_F_MRG_RXBUF feature");
151 if ((vif->remote_features & (1ULL << VIRTIO_RING_F_INDIRECT_DESC)) == 0)
153 args->rv = VNET_API_ERROR_UNSUPPORTED;
154 args->error = clib_error_return (0, "vhost-net backend doesn't support "
155 "VIRTIO_RING_F_INDIRECT_DESC feature");
159 if ((vif->remote_features & (1ULL << VIRTIO_F_VERSION_1)) == 0)
161 args->rv = VNET_API_ERROR_UNSUPPORTED;
162 args->error = clib_error_return (0, "vhost-net backend doesn't support "
163 "VIRTIO_F_VERSION_1 features");
167 vif->features |= 1ULL << VIRTIO_NET_F_MRG_RXBUF;
168 vif->features |= 1ULL << VIRTIO_F_VERSION_1;
169 vif->features |= 1ULL << VIRTIO_RING_F_INDIRECT_DESC;
171 _IOCTL (vif->fd, VHOST_SET_FEATURES, &vif->features);
173 if ((vif->tap_fd = open ("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0)
175 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
176 args->error = clib_error_return_unix (0, "open '/dev/net/tun'");
180 ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR;
181 _IOCTL (vif->tap_fd, TUNSETIFF, (void *) &ifr);
182 vif->ifindex = if_nametoindex (ifr.ifr_ifrn.ifrn_name);
184 unsigned int offload = 0;
185 hdrsz = sizeof (struct virtio_net_hdr_v1);
186 _IOCTL (vif->tap_fd, TUNSETOFFLOAD, offload);
187 _IOCTL (vif->tap_fd, TUNSETVNETHDRSZ, &hdrsz);
188 _IOCTL (vif->fd, VHOST_SET_OWNER, 0);
190 /* if namespace is specified, all further netlink messages should be excuted
191 after we change our net namespace */
192 if (args->host_namespace)
195 old_netns_fd = open ("/proc/self/ns/net", O_RDONLY);
196 if ((fd = open_netns_fd ((char *) args->host_namespace)) == -1)
198 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
199 args->error = clib_error_return_unix (0, "open_netns_fd '%s'",
200 args->host_namespace);
203 args->error = vnet_netlink_set_link_netns (vif->ifindex, fd,
204 (char *) args->host_if_name);
207 args->rv = VNET_API_ERROR_NETLINK_ERROR;
210 if (setns (fd, CLONE_NEWNET) == -1)
212 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
213 args->error = clib_error_return_unix (0, "setns '%s'",
214 args->host_namespace);
218 if ((vif->ifindex = if_nametoindex ((char *) args->host_if_name)) == 0)
220 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
221 args->error = clib_error_return_unix (0, "if_nametoindex '%s'",
228 if (args->host_if_name)
230 args->error = vnet_netlink_set_link_name (vif->ifindex,
235 args->rv = VNET_API_ERROR_NETLINK_ERROR;
241 if (!ethernet_mac_address_is_zero (args->host_mac_addr))
243 args->error = vnet_netlink_set_link_addr (vif->ifindex,
244 args->host_mac_addr);
247 args->rv = VNET_API_ERROR_NETLINK_ERROR;
252 if (args->host_bridge)
254 args->error = vnet_netlink_set_link_master (vif->ifindex,
255 (char *) args->host_bridge);
258 args->rv = VNET_API_ERROR_NETLINK_ERROR;
264 if (args->host_ip4_prefix_len)
266 args->error = vnet_netlink_add_ip4_addr (vif->ifindex,
267 &args->host_ip4_addr,
268 args->host_ip4_prefix_len);
271 args->rv = VNET_API_ERROR_NETLINK_ERROR;
276 if (args->host_ip6_prefix_len)
278 args->error = vnet_netlink_add_ip6_addr (vif->ifindex,
279 &args->host_ip6_addr,
280 args->host_ip6_prefix_len);
283 args->rv = VNET_API_ERROR_NETLINK_ERROR;
288 args->error = vnet_netlink_set_link_state (vif->ifindex, 1 /* UP */ );
291 args->rv = VNET_API_ERROR_NETLINK_ERROR;
295 /* switch back to old net namespace */
296 if (args->host_namespace)
298 if (setns (old_netns_fd, CLONE_NEWNET) == -1)
300 args->rv = VNET_API_ERROR_SYSCALL_ERROR_2;
301 args->error = clib_error_return_unix (0, "setns '%s'",
302 args->host_namespace);
307 /* Set vhost memory table */
308 i = sizeof (struct vhost_memory) + sizeof (struct vhost_memory_region);
309 vhost_mem = clib_mem_alloc (i);
310 memset (vhost_mem, 0, i);
311 vhost_mem->nregions = 1;
312 vhost_mem->regions[0].memory_size = (1ULL << 47) - 4096;
313 _IOCTL (vif->fd, VHOST_SET_MEM_TABLE, vhost_mem);
315 if ((args->error = virtio_vring_init (vm, vif, 0, args->rx_ring_sz)))
317 args->rv = VNET_API_ERROR_INIT_FAILED;
321 if ((args->error = virtio_vring_init (vm, vif, 1, args->tx_ring_sz)))
323 args->rv = VNET_API_ERROR_INIT_FAILED;
327 if (!args->mac_addr_set)
329 f64 now = vlib_time_now (vm);
331 rnd = (u32) (now * 1e6);
332 rnd = random_u32 (&rnd);
334 memcpy (args->mac_addr + 2, &rnd, sizeof (rnd));
335 args->mac_addr[0] = 2;
336 args->mac_addr[1] = 0xfe;
338 vif->rx_ring_sz = args->rx_ring_sz != 0 ? args->rx_ring_sz : 256;
339 vif->tx_ring_sz = args->tx_ring_sz != 0 ? args->tx_ring_sz : 256;
340 vif->host_if_name = args->host_if_name;
341 args->host_if_name = 0;
342 vif->net_ns = args->host_namespace;
343 args->host_namespace = 0;
344 vif->host_bridge = args->host_bridge;
345 args->host_bridge = 0;
346 clib_memcpy (vif->host_mac_addr, args->host_mac_addr, 6);
347 vif->host_ip4_prefix_len = args->host_ip4_prefix_len;
348 vif->host_ip6_prefix_len = args->host_ip6_prefix_len;
349 if (args->host_ip4_prefix_len)
350 clib_memcpy (&vif->host_ip4_addr, &args->host_ip4_addr, 4);
351 if (args->host_ip6_prefix_len)
352 clib_memcpy (&vif->host_ip6_addr, &args->host_ip6_addr, 16);
354 args->error = ethernet_register_interface (vnm, virtio_device_class.index,
358 virtio_eth_flag_change);
361 args->rv = VNET_API_ERROR_INVALID_REGISTRATION;
365 sw = vnet_get_hw_sw_interface (vnm, vif->hw_if_index);
366 vif->sw_if_index = sw->sw_if_index;
367 args->sw_if_index = vif->sw_if_index;
368 hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
369 hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE;
370 vnet_hw_interface_set_input_node (vnm, vif->hw_if_index,
371 virtio_input_node.index);
372 vnet_hw_interface_assign_rx_thread (vnm, vif->hw_if_index, 0, ~0);
373 vnet_hw_interface_set_rx_mode (vnm, vif->hw_if_index, 0,
374 VNET_HW_INTERFACE_RX_MODE_DEFAULT);
375 vif->per_interface_next_index = ~0;
376 vif->type = VIRTIO_IF_TYPE_TAP;
377 vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
378 vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
379 VNET_HW_INTERFACE_FLAG_LINK_UP);
385 ASSERT (args->error == 0);
387 args->rv = VNET_API_ERROR_SYSCALL_ERROR_3;
389 if (vif->tap_fd != -1)
393 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
394 memset (vif, 0, sizeof (virtio_if_t));
395 pool_put (vim->interfaces, vif);
399 clib_mem_free (vhost_mem);
400 if (old_netns_fd != -1)
401 close (old_netns_fd);
405 tap_delete_if (vlib_main_t * vm, u32 sw_if_index)
407 vnet_main_t *vnm = vnet_get_main ();
408 virtio_main_t *mm = &virtio_main;
409 tap_main_t *tm = &tap_main;
412 vnet_hw_interface_t *hw;
414 hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
415 if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
416 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
418 vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
420 /* bring down the interface */
421 vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
422 vnet_sw_interface_set_flags (vnm, vif->sw_if_index, 0);
424 ethernet_delete_interface (vnm, vif->hw_if_index);
425 vif->hw_if_index = ~0;
427 if (vif->tap_fd != -1)
432 vec_foreach_index (i, vif->vrings) virtio_vring_free (vif, i);
433 vec_free (vif->vrings);
435 hash_unset (tm->dev_instance_by_interface_id, vif->id);
436 memset (vif, 0, sizeof (*vif));
437 pool_put (mm->interfaces, vif);
443 tap_dump_ifs (tap_interface_details_t ** out_tapids)
445 vnet_main_t *vnm = vnet_get_main ();
446 virtio_main_t *mm = &virtio_main;
448 vnet_hw_interface_t *hi;
449 tap_interface_details_t *r_tapids = NULL;
450 tap_interface_details_t *tapid = NULL;
453 pool_foreach (vif, mm->interfaces,
454 vec_add2(r_tapids, tapid, 1);
455 memset (tapid, 0, sizeof (*tapid));
457 tapid->sw_if_index = vif->sw_if_index;
458 hi = vnet_get_hw_interface (vnm, vif->hw_if_index);
459 clib_memcpy(tapid->dev_name, hi->name,
460 MIN (ARRAY_LEN (tapid->dev_name) - 1,
461 strlen ((const char *) hi->name)));
462 tapid->rx_ring_sz = vif->rx_ring_sz;
463 tapid->tx_ring_sz = vif->tx_ring_sz;
464 clib_memcpy(tapid->host_mac_addr, vif->host_mac_addr, 6);
465 if (vif->host_if_name)
467 clib_memcpy(tapid->host_if_name, vif->host_if_name,
468 MIN (ARRAY_LEN (tapid->host_if_name) - 1,
469 strlen ((const char *) vif->host_if_name)));
473 clib_memcpy(tapid->host_namespace, vif->net_ns,
474 MIN (ARRAY_LEN (tapid->host_namespace) - 1,
475 strlen ((const char *) vif->net_ns)));
477 if (vif->host_bridge)
479 clib_memcpy(tapid->host_bridge, vif->host_bridge,
480 MIN (ARRAY_LEN (tapid->host_bridge) - 1,
481 strlen ((const char *) vif->host_bridge)));
483 if (vif->host_ip4_prefix_len)
484 clib_memcpy(tapid->host_ip4_addr, &vif->host_ip4_addr, 4);
485 tapid->host_ip4_prefix_len = vif->host_ip4_prefix_len;
486 if (vif->host_ip6_prefix_len)
487 clib_memcpy(tapid->host_ip6_addr, &vif->host_ip6_addr, 16);
488 tapid->host_ip6_prefix_len = vif->host_ip6_prefix_len;
492 *out_tapids = r_tapids;
497 static clib_error_t *
498 tap_init (vlib_main_t * vm)
500 tap_main_t *tm = &tap_main;
501 tm->dev_instance_by_interface_id = hash_create (0, sizeof (uword));
505 VLIB_INIT_FUNCTION (tap_init);
508 * fd.io coding-style-patch-verification: ON
511 * eval: (c-set-style "gnu")