2fbfecba1268564e91900fda11b32162a4bc2df7
[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 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         /* Check if our kernel supports TUNSETOFFLOAD */
41         if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) {
42                 PMD_DRV_LOG(ERR, "Kernel does't support TUNSETOFFLOAD\n");
43                 return -ENOTSUP;
44         }
45
46         if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
47                 offload &= ~TUN_F_UFO;
48                 if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) {
49                         PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s\n",
50                                 strerror(errno));
51                         return -1;
52                 }
53         }
54
55         return 0;
56 }
57
58 int
59 vhost_kernel_tap_set_queue(int fd, bool attach)
60 {
61         struct ifreq ifr = {
62                 .ifr_flags = attach ? IFF_ATTACH_QUEUE : IFF_DETACH_QUEUE,
63         };
64
65         return ioctl(fd, TUNSETQUEUE, &ifr);
66 }
67
68 int
69 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
70                          const char *mac, uint64_t features)
71 {
72         unsigned int tap_features;
73         char *tap_name = NULL;
74         int sndbuf = INT_MAX;
75         struct ifreq ifr;
76         int tapfd;
77
78         /* TODO:
79          * 1. verify we can get/set vnet_hdr_len, tap_probe_vnet_hdr_len
80          * 2. get number of memory regions from vhost module parameter
81          * max_mem_regions, supported in newer version linux kernel
82          */
83         tapfd = open(PATH_NET_TUN, O_RDWR);
84         if (tapfd < 0) {
85                 PMD_DRV_LOG(ERR, "fail to open %s: %s",
86                             PATH_NET_TUN, strerror(errno));
87                 return -1;
88         }
89
90         /* Construct ifr */
91         memset(&ifr, 0, sizeof(ifr));
92         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
93
94         if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
95                 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
96                 goto error;
97         }
98         if (tap_features & IFF_ONE_QUEUE)
99                 ifr.ifr_flags |= IFF_ONE_QUEUE;
100
101         /* Let tap instead of vhost-net handle vnet header, as the latter does
102          * not support offloading. And in this case, we should not set feature
103          * bit VHOST_NET_F_VIRTIO_NET_HDR.
104          */
105         if (tap_features & IFF_VNET_HDR) {
106                 ifr.ifr_flags |= IFF_VNET_HDR;
107         } else {
108                 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
109                 goto error;
110         }
111
112         if (req_mq)
113                 ifr.ifr_flags |= IFF_MULTI_QUEUE;
114
115         if (*p_ifname)
116                 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
117         else
118                 strncpy(ifr.ifr_name, "tap%d", IFNAMSIZ - 1);
119         if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) == -1) {
120                 PMD_DRV_LOG(ERR, "TUNSETIFF failed: %s", strerror(errno));
121                 goto error;
122         }
123
124         tap_name = strdup(ifr.ifr_name);
125         if (!tap_name) {
126                 PMD_DRV_LOG(ERR, "strdup ifname failed: %s", strerror(errno));
127                 goto error;
128         }
129
130         fcntl(tapfd, F_SETFL, O_NONBLOCK);
131
132         if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
133                 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
134                 goto error;
135         }
136
137         if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
138                 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
139                 goto error;
140         }
141
142         vhost_kernel_tap_set_offload(tapfd, features);
143
144         memset(&ifr, 0, sizeof(ifr));
145         ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
146         memcpy(ifr.ifr_hwaddr.sa_data, mac, RTE_ETHER_ADDR_LEN);
147         if (ioctl(tapfd, SIOCSIFHWADDR, (void *)&ifr) == -1) {
148                 PMD_DRV_LOG(ERR, "SIOCSIFHWADDR failed: %s", strerror(errno));
149                 goto error;
150         }
151
152         free(*p_ifname);
153         *p_ifname = tap_name;
154
155         return tapfd;
156 error:
157         free(tap_name);
158         close(tapfd);
159         return -1;
160 }