1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
14 #include "vhost_kernel_tap.h"
15 #include "../virtio_logs.h"
18 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq)
20 unsigned int tap_features;
24 unsigned int offload =
32 * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
33 * 2. get number of memory regions from vhost module parameter
34 * max_mem_regions, supported in newer version linux kernel
36 tapfd = open(PATH_NET_TUN, O_RDWR);
38 PMD_DRV_LOG(ERR, "fail to open %s: %s",
39 PATH_NET_TUN, strerror(errno));
44 memset(&ifr, 0, sizeof(ifr));
45 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
47 if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
48 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
51 if (tap_features & IFF_ONE_QUEUE)
52 ifr.ifr_flags |= IFF_ONE_QUEUE;
54 /* Let tap instead of vhost-net handle vnet header, as the latter does
55 * not support offloading. And in this case, we should not set feature
56 * bit VHOST_NET_F_VIRTIO_NET_HDR.
58 if (tap_features & IFF_VNET_HDR) {
59 ifr.ifr_flags |= IFF_VNET_HDR;
61 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
66 ifr.ifr_flags |= IFF_MULTI_QUEUE;
69 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
71 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
72 if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
73 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
77 fcntl(tapfd, F_SETFL, O_NONBLOCK);
79 if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
80 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
84 if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
85 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
89 /* TODO: before set the offload capabilities, we'd better (1) check
90 * negotiated features to see if necessary to offload; (2) query tap
91 * to see if it supports the offload capabilities.
93 if (ioctl(tapfd, TUNSETOFFLOAD, offload) != 0)
94 PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s",
98 *p_ifname = strdup(ifr.ifr_name);