net/virtio-user: check negotiated features before set
[dpdk.git] / drivers / net / virtio / virtio_user / vhost_kernel_tap.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <net/if.h>
10 #include <net/if_arp.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <limits.h>
14
15 #include <rte_ether.h>
16
17 #include "vhost_kernel_tap.h"
18 #include "../virtio_logs.h"
19 #include "../virtio_pci.h"
20
21 static int
22 vhost_kernel_tap_set_offload(int fd, uint64_t features)
23 {
24         unsigned int offload = 0;
25
26         if (features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
27                 offload |= TUN_F_CSUM;
28                 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO4))
29                         offload |= TUN_F_TSO4;
30                 if (features & (1ULL << VIRTIO_NET_F_GUEST_TSO6))
31                         offload |= TUN_F_TSO6;
32                 if (features & ((1ULL << VIRTIO_NET_F_GUEST_TSO4) |
33                         (1ULL << VIRTIO_NET_F_GUEST_TSO6)) &&
34                         (features & (1ULL << VIRTIO_NET_F_GUEST_ECN)))
35                         offload |= TUN_F_TSO_ECN;
36                 if (features & (1ULL << VIRTIO_NET_F_GUEST_UFO))
37                         offload |= TUN_F_UFO;
38         }
39
40         if (offload != 0) {
41                 /* Check if our kernel supports TUNSETOFFLOAD */
42                 if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
43                         PMD_DRV_LOG(ERR, "Kernel does't support TUNSETOFFLOAD\n");
44                         return -ENOTSUP;
45                 }
46
47                 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
48                         offload &= ~TUN_F_UFO;
49                         if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
50                                 PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s\n",
51                                         strerror(errno));
52                                 return -1;
53                         }
54                 }
55         }
56
57         return 0;
58 }
59
60 int
61 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
62                          const char *mac, uint64_t features)
63 {
64         unsigned int tap_features;
65         int sndbuf = INT_MAX;
66         struct ifreq ifr;
67         int tapfd;
68
69         /* TODO:
70          * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
71          * 2. get number of memory regions from vhost module parameter
72          * max_mem_regions, supported in newer version linux kernel
73          */
74         tapfd = open(PATH_NET_TUN, O_RDWR);
75         if (tapfd < 0) {
76                 PMD_DRV_LOG(ERR, "fail to open %s: %s",
77                             PATH_NET_TUN, strerror(errno));
78                 return -1;
79         }
80
81         /* Construct ifr */
82         memset(&ifr, 0, sizeof(ifr));
83         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
84
85         if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
86                 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
87                 goto error;
88         }
89         if (tap_features & IFF_ONE_QUEUE)
90                 ifr.ifr_flags |= IFF_ONE_QUEUE;
91
92         /* Let tap instead of vhost-net handle vnet header, as the latter does
93          * not support offloading. And in this case, we should not set feature
94          * bit VHOST_NET_F_VIRTIO_NET_HDR.
95          */
96         if (tap_features & IFF_VNET_HDR) {
97                 ifr.ifr_flags |= IFF_VNET_HDR;
98         } else {
99                 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
100                 goto error;
101         }
102
103         if (req_mq)
104                 ifr.ifr_flags |= IFF_MULTI_QUEUE;
105
106         if (*p_ifname)
107                 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
108         else
109                 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
110         if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
111                 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
112                 goto error;
113         }
114
115         fcntl(tapfd, F_SETFL, O_NONBLOCK);
116
117         if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
118                 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
119                 goto error;
120         }
121
122         if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
123                 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
124                 goto error;
125         }
126
127         vhost_kernel_tap_set_offload(tapfd, features);
128
129         memset(&ifr, 0, sizeof(ifr));
130         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
131         memcpy(ifr.ifr_hwaddr.sa_data, mac, ETHER_ADDR_LEN);
132         if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
133                 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
134                 goto error;
135         }
136
137         if (!(*p_ifname))
138                 *p_ifname = strdup(ifr.ifr_name);
139
140         return tapfd;
141 error:
142         close(tapfd);
143         return -1;
144 }