drivers: use SPDX tag for Intel copyright files
[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 <errno.h>
11 #include <string.h>
12 #include <limits.h>
13
14 #include "vhost_kernel_tap.h"
15 #include "../virtio_logs.h"
16
17 int
18 vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq)
19 {
20         unsigned int tap_features;
21         int sndbuf = INT_MAX;
22         struct ifreq ifr;
23         int tapfd;
24         unsigned int offload =
25                         TUN_F_CSUM |
26                         TUN_F_TSO4 |
27                         TUN_F_TSO6 |
28                         TUN_F_TSO_ECN |
29                         TUN_F_UFO;
30
31         /* TODO:
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
35          */
36         tapfd = open(PATH_NET_TUN, O_RDWR);
37         if (tapfd < 0) {
38                 PMD_DRV_LOG(ERR, "fail to open %s: %s",
39                             PATH_NET_TUN, strerror(errno));
40                 return -1;
41         }
42
43         /* Construct ifr */
44         memset(&ifr, 0, sizeof(ifr));
45         ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
46
47         if (ioctl(tapfd, TUNGETFEATURES, &tap_features) == -1) {
48                 PMD_DRV_LOG(ERR, "TUNGETFEATURES failed: %s", strerror(errno));
49                 goto error;
50         }
51         if (tap_features & IFF_ONE_QUEUE)
52                 ifr.ifr_flags |= IFF_ONE_QUEUE;
53
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.
57          */
58         if (tap_features & IFF_VNET_HDR) {
59                 ifr.ifr_flags |= IFF_VNET_HDR;
60         } else {
61                 PMD_DRV_LOG(ERR, "TAP does not support IFF_VNET_HDR");
62                 goto error;
63         }
64
65         if (req_mq)
66                 ifr.ifr_flags |= IFF_MULTI_QUEUE;
67
68         if (*p_ifname)
69                 strncpy(ifr.ifr_name, *p_ifname, IFNAMSIZ - 1);
70         else
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));
74                 goto error;
75         }
76
77         fcntl(tapfd, F_SETFL, O_NONBLOCK);
78
79         if (ioctl(tapfd, TUNSETVNETHDRSZ, &hdr_size) < 0) {
80                 PMD_DRV_LOG(ERR, "TUNSETVNETHDRSZ failed: %s", strerror(errno));
81                 goto error;
82         }
83
84         if (ioctl(tapfd, TUNSETSNDBUF, &sndbuf) < 0) {
85                 PMD_DRV_LOG(ERR, "TUNSETSNDBUF failed: %s", strerror(errno));
86                 goto error;
87         }
88
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.
92          */
93         if (ioctl(tapfd, TUNSETOFFLOAD, offload) != 0)
94                 PMD_DRV_LOG(ERR, "TUNSETOFFLOAD ioctl() failed: %s",
95                            strerror(errno));
96
97         if (!(*p_ifname))
98                 *p_ifname = strdup(ifr.ifr_name);
99
100         return tapfd;
101 error:
102         close(tapfd);
103         return -1;
104 }