]>
Commit | Line | Data |
---|---|---|
c28b1c10 MM |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * Copyright (c) 2009 Red Hat, Inc. | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include "net/tap.h" | |
27 | #include "net/tap-linux.h" | |
28 | ||
29 | #include <net/if.h> | |
30 | #include <sys/ioctl.h> | |
31 | ||
32 | #include "sysemu.h" | |
33 | #include "qemu-common.h" | |
34 | ||
35 | int tap_open(char *ifname, int ifname_size, int *vnet_hdr, int vnet_hdr_required) | |
36 | { | |
37 | struct ifreq ifr; | |
38 | int fd, ret; | |
39 | ||
40 | TFR(fd = open("/dev/net/tun", O_RDWR)); | |
41 | if (fd < 0) { | |
42 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); | |
43 | return -1; | |
44 | } | |
45 | memset(&ifr, 0, sizeof(ifr)); | |
46 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; | |
47 | ||
48 | if (*vnet_hdr) { | |
49 | unsigned int features; | |
50 | ||
51 | if (ioctl(fd, TUNGETFEATURES, &features) == 0 && | |
52 | features & IFF_VNET_HDR) { | |
53 | *vnet_hdr = 1; | |
54 | ifr.ifr_flags |= IFF_VNET_HDR; | |
6720b35b PR |
55 | } else { |
56 | *vnet_hdr = 0; | |
c28b1c10 MM |
57 | } |
58 | ||
59 | if (vnet_hdr_required && !*vnet_hdr) { | |
60 | qemu_error("vnet_hdr=1 requested, but no kernel " | |
61 | "support for IFF_VNET_HDR available"); | |
62 | close(fd); | |
63 | return -1; | |
64 | } | |
65 | } | |
66 | ||
67 | if (ifname[0] != '\0') | |
68 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); | |
69 | else | |
70 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); | |
71 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); | |
72 | if (ret != 0) { | |
73 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); | |
74 | close(fd); | |
75 | return -1; | |
76 | } | |
77 | pstrcpy(ifname, ifname_size, ifr.ifr_name); | |
78 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
79 | return fd; | |
80 | } | |
15ac913b MM |
81 | |
82 | /* sndbuf should be set to a value lower than the tx queue | |
83 | * capacity of any destination network interface. | |
84 | * Ethernet NICs generally have txqueuelen=1000, so 1Mb is | |
85 | * a good default, given a 1500 byte MTU. | |
86 | */ | |
87 | #define TAP_DEFAULT_SNDBUF 1024*1024 | |
88 | ||
89 | int tap_set_sndbuf(int fd, QemuOpts *opts) | |
90 | { | |
91 | int sndbuf; | |
92 | ||
93 | sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF); | |
94 | if (!sndbuf) { | |
95 | sndbuf = INT_MAX; | |
96 | } | |
97 | ||
98 | if (ioctl(fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) { | |
99 | qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno)); | |
100 | return -1; | |
101 | } | |
102 | return 0; | |
103 | } | |
dc69004c MM |
104 | |
105 | int tap_probe_vnet_hdr(int fd) | |
106 | { | |
107 | struct ifreq ifr; | |
108 | ||
109 | if (ioctl(fd, TUNGETIFF, &ifr) != 0) { | |
110 | qemu_error("TUNGETIFF ioctl() failed: %s\n", strerror(errno)); | |
111 | return 0; | |
112 | } | |
113 | ||
114 | return ifr.ifr_flags & IFF_VNET_HDR; | |
115 | } | |
1faac1f7 | 116 | |
9c282718 MM |
117 | int tap_probe_has_ufo(int fd) |
118 | { | |
119 | unsigned offload; | |
120 | ||
121 | offload = TUN_F_CSUM | TUN_F_UFO; | |
122 | ||
123 | if (ioctl(fd, TUNSETOFFLOAD, offload) < 0) | |
124 | return 0; | |
125 | ||
126 | return 1; | |
127 | } | |
128 | ||
1faac1f7 MM |
129 | void tap_fd_set_offload(int fd, int csum, int tso4, |
130 | int tso6, int ecn, int ufo) | |
131 | { | |
132 | unsigned int offload = 0; | |
133 | ||
2e50326c PR |
134 | /* Check if our kernel supports TUNSETOFFLOAD */ |
135 | if (ioctl(fd, TUNSETOFFLOAD, 0) != 0 && errno == EINVAL) { | |
136 | return; | |
137 | } | |
138 | ||
1faac1f7 MM |
139 | if (csum) { |
140 | offload |= TUN_F_CSUM; | |
141 | if (tso4) | |
142 | offload |= TUN_F_TSO4; | |
143 | if (tso6) | |
144 | offload |= TUN_F_TSO6; | |
145 | if ((tso4 || tso6) && ecn) | |
146 | offload |= TUN_F_TSO_ECN; | |
147 | if (ufo) | |
148 | offload |= TUN_F_UFO; | |
149 | } | |
150 | ||
151 | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { | |
152 | offload &= ~TUN_F_UFO; | |
153 | if (ioctl(fd, TUNSETOFFLOAD, offload) != 0) { | |
154 | fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n", | |
155 | strerror(errno)); | |
156 | } | |
157 | } | |
158 | } |