4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
39 #include <net/if_arp.h>
44 #include <rte_ether.h>
46 #include "vhost_kernel_tap.h"
47 #include "../virtio_logs.h"
48 #include "../virtio_pci.h"
51 vhost_kernel_tap_set_offload(int fd, uint64_t features)
53 unsigned int offload = 0;
55 if (features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
56 offload |= TUN_F_CSUM;
57 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO4))
58 offload |= TUN_F_TSO4;
59 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO6))
60 offload |= TUN_F_TSO6;
61 if (features & ((1ULL << VIRTIO_NET_F_GUEST_TSO4) |
62 (1ULL << VIRTIO_NET_F_GUEST_TSO6)) &&
63 (features & (1ULL << VIRTIO_NET_F_GUEST_ECN)))
64 offload |= TUN_F_TSO_ECN;
65 if (features & (1ULL << VIRTIO_NET_F_GUEST_UFO))
70 /* Check if our kernel supports TUNSETOFFLOAD */
71 if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
72 PMD_DRV_LOG(ERR, "Kernel does't support TUNSETOFFLOAD\n");
76 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
77 offload &= ~TUN_F_UFO;
78 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
79 PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s\n",
90 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
91 const char *mac, uint64_t features)
93 unsigned int tap_features;
99 * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
100 * 2. get number of memory regions from vhost module parameter
101 * max_mem_regions, supported in newer version linux kernel
103 tapfd = open(PATH_NET_TUN, O_RDWR);
105 PMD_DRV_LOG(ERR, "fail to open %s: %s",
106 PATH_NET_TUN, strerror(errno));
111 memset(&ifr, 0, sizeof(ifr));
112 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
114 if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
115 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
118 if (tap_features & IFF_ONE_QUEUE)
119 ifr.ifr_flags |= IFF_ONE_QUEUE;
121 /* Let tap instead of vhost-net handle vnet header, as the latter does
122 * not support offloading. And in this case, we should not set feature
123 * bit VHOST_NET_F_VIRTIO_NET_HDR.
125 if (tap_features & IFF_VNET_HDR) {
126 ifr.ifr_flags |= IFF_VNET_HDR;
128 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
133 ifr.ifr_flags |= IFF_MULTI_QUEUE;
136 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
138 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
139 if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
140 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
144 fcntl(tapfd, F_SETFL, O_NONBLOCK);
146 if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
147 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
151 if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
152 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
156 vhost_kernel_tap_set_offload(tapfd, features);
158 memset(&ifr, 0, sizeof(ifr));
159 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
160 memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
161 if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
162 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
167 *p_ifname = strdup(ifr.ifr_name);