1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
10 #include <net/if_arp.h>
15 #include <rte_ether.h>
17 #include "vhost_kernel_tap.h"
18 #include "../virtio_logs.h"
21 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
24 unsigned int tap_features;
28 unsigned int offload =
36 * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
37 * 2. get number of memory regions from vhost module parameter
38 * max_mem_regions, supported in newer version linux kernel
40 tapfd = open(PATH_NET_TUN, O_RDWR);
42 PMD_DRV_LOG(ERR, "fail to open %s: %s",
43 PATH_NET_TUN, strerror(errno));
48 memset(&ifr, 0, sizeof(ifr));
49 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
51 if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
52 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
55 if (tap_features & IFF_ONE_QUEUE)
56 ifr.ifr_flags |= IFF_ONE_QUEUE;
58 /* Let tap instead of vhost-net handle vnet header, as the latter does
59 * not support offloading. And in this case, we should not set feature
60 * bit VHOST_NET_F_VIRTIO_NET_HDR.
62 if (tap_features & IFF_VNET_HDR) {
63 ifr.ifr_flags |= IFF_VNET_HDR;
65 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
70 ifr.ifr_flags |= IFF_MULTI_QUEUE;
73 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
75 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
76 if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
77 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
81 fcntl(tapfd, F_SETFL, O_NONBLOCK);
83 if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
84 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
88 if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
89 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
93 /* TODO: before set the offload capabilities, we'd better (1) check
94 * negotiated features to see if necessary to offload; (2) query tap
95 * to see if it supports the offload capabilities.
97 if (ioctl(tapfd, TUNSETOFFLOAD, offload) != 0)
98 PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s",
101 memset(&ifr, 0, sizeof(ifr));
102 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
103 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
104 if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
105 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
110 *p_ifname = strdup(ifr.ifr_name);