root / net.c @ 6c9f58ba
History | View | Annotate | Download (94.4 kB)
1 | 63a01ef8 | aliguori | /*
|
---|---|---|---|
2 | 63a01ef8 | aliguori | * QEMU System Emulator
|
3 | 63a01ef8 | aliguori | *
|
4 | 63a01ef8 | aliguori | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 63a01ef8 | aliguori | *
|
6 | 63a01ef8 | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 63a01ef8 | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
8 | 63a01ef8 | aliguori | * in the Software without restriction, including without limitation the rights
|
9 | 63a01ef8 | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 63a01ef8 | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
11 | 63a01ef8 | aliguori | * furnished to do so, subject to the following conditions:
|
12 | 63a01ef8 | aliguori | *
|
13 | 63a01ef8 | aliguori | * The above copyright notice and this permission notice shall be included in
|
14 | 63a01ef8 | aliguori | * all copies or substantial portions of the Software.
|
15 | 63a01ef8 | aliguori | *
|
16 | 63a01ef8 | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 63a01ef8 | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 63a01ef8 | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 63a01ef8 | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 63a01ef8 | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 63a01ef8 | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 63a01ef8 | aliguori | * THE SOFTWARE.
|
23 | 63a01ef8 | aliguori | */
|
24 | 63a01ef8 | aliguori | #include <unistd.h> |
25 | 63a01ef8 | aliguori | #include <fcntl.h> |
26 | 63a01ef8 | aliguori | #include <signal.h> |
27 | 63a01ef8 | aliguori | #include <time.h> |
28 | 63a01ef8 | aliguori | #include <errno.h> |
29 | 63a01ef8 | aliguori | #include <sys/time.h> |
30 | 63a01ef8 | aliguori | #include <zlib.h> |
31 | 63a01ef8 | aliguori | |
32 | 71e72a19 | Juan Quintela | /* Needed early for CONFIG_BSD etc. */
|
33 | d40cdb10 | blueswir1 | #include "config-host.h" |
34 | d40cdb10 | blueswir1 | |
35 | 63a01ef8 | aliguori | #ifndef _WIN32
|
36 | 63a01ef8 | aliguori | #include <sys/times.h> |
37 | 63a01ef8 | aliguori | #include <sys/wait.h> |
38 | 63a01ef8 | aliguori | #include <termios.h> |
39 | 63a01ef8 | aliguori | #include <sys/mman.h> |
40 | 63a01ef8 | aliguori | #include <sys/ioctl.h> |
41 | 24646c7e | blueswir1 | #include <sys/resource.h> |
42 | 63a01ef8 | aliguori | #include <sys/socket.h> |
43 | 63a01ef8 | aliguori | #include <netinet/in.h> |
44 | 24646c7e | blueswir1 | #include <net/if.h> |
45 | 24646c7e | blueswir1 | #ifdef __NetBSD__
|
46 | 24646c7e | blueswir1 | #include <net/if_tap.h> |
47 | 24646c7e | blueswir1 | #endif
|
48 | 24646c7e | blueswir1 | #ifdef __linux__
|
49 | 76682299 | Mark McLoughlin | #include "tap-linux.h" |
50 | 24646c7e | blueswir1 | #endif
|
51 | 24646c7e | blueswir1 | #include <arpa/inet.h> |
52 | 63a01ef8 | aliguori | #include <dirent.h> |
53 | 63a01ef8 | aliguori | #include <netdb.h> |
54 | 63a01ef8 | aliguori | #include <sys/select.h> |
55 | 71e72a19 | Juan Quintela | #ifdef CONFIG_BSD
|
56 | 63a01ef8 | aliguori | #include <sys/stat.h> |
57 | c5e97233 | blueswir1 | #if defined(__FreeBSD__) || defined(__DragonFly__)
|
58 | 63a01ef8 | aliguori | #include <libutil.h> |
59 | 24646c7e | blueswir1 | #else
|
60 | 24646c7e | blueswir1 | #include <util.h> |
61 | 63a01ef8 | aliguori | #endif
|
62 | 63a01ef8 | aliguori | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
63 | 63a01ef8 | aliguori | #include <freebsd/stdlib.h> |
64 | 63a01ef8 | aliguori | #else
|
65 | 63a01ef8 | aliguori | #ifdef __linux__
|
66 | 63a01ef8 | aliguori | #include <pty.h> |
67 | 63a01ef8 | aliguori | #include <malloc.h> |
68 | 63a01ef8 | aliguori | #include <linux/rtc.h> |
69 | 63a01ef8 | aliguori | |
70 | 63a01ef8 | aliguori | /* For the benefit of older linux systems which don't supply it,
|
71 | 63a01ef8 | aliguori | we use a local copy of hpet.h. */
|
72 | 63a01ef8 | aliguori | /* #include <linux/hpet.h> */
|
73 | 63a01ef8 | aliguori | #include "hpet.h" |
74 | 63a01ef8 | aliguori | |
75 | 63a01ef8 | aliguori | #include <linux/ppdev.h> |
76 | 63a01ef8 | aliguori | #include <linux/parport.h> |
77 | 63a01ef8 | aliguori | #endif
|
78 | 63a01ef8 | aliguori | #ifdef __sun__
|
79 | 63a01ef8 | aliguori | #include <sys/stat.h> |
80 | 63a01ef8 | aliguori | #include <sys/ethernet.h> |
81 | 63a01ef8 | aliguori | #include <sys/sockio.h> |
82 | 63a01ef8 | aliguori | #include <netinet/arp.h> |
83 | 63a01ef8 | aliguori | #include <netinet/in.h> |
84 | 63a01ef8 | aliguori | #include <netinet/in_systm.h> |
85 | 63a01ef8 | aliguori | #include <netinet/ip.h> |
86 | 63a01ef8 | aliguori | #include <netinet/ip_icmp.h> // must come after ip.h |
87 | 63a01ef8 | aliguori | #include <netinet/udp.h> |
88 | 63a01ef8 | aliguori | #include <netinet/tcp.h> |
89 | 63a01ef8 | aliguori | #include <net/if.h> |
90 | 63a01ef8 | aliguori | #include <syslog.h> |
91 | 63a01ef8 | aliguori | #include <stropts.h> |
92 | 63a01ef8 | aliguori | #endif
|
93 | 63a01ef8 | aliguori | #endif
|
94 | 63a01ef8 | aliguori | #endif
|
95 | 63a01ef8 | aliguori | |
96 | 63a01ef8 | aliguori | #if defined(__OpenBSD__)
|
97 | 63a01ef8 | aliguori | #include <util.h> |
98 | 63a01ef8 | aliguori | #endif
|
99 | 63a01ef8 | aliguori | |
100 | 63a01ef8 | aliguori | #if defined(CONFIG_VDE)
|
101 | 63a01ef8 | aliguori | #include <libvdeplug.h> |
102 | 63a01ef8 | aliguori | #endif
|
103 | 63a01ef8 | aliguori | |
104 | 511d2b14 | blueswir1 | #include "qemu-common.h" |
105 | 511d2b14 | blueswir1 | #include "net.h" |
106 | 511d2b14 | blueswir1 | #include "monitor.h" |
107 | 511d2b14 | blueswir1 | #include "sysemu.h" |
108 | 511d2b14 | blueswir1 | #include "qemu-timer.h" |
109 | 511d2b14 | blueswir1 | #include "qemu-char.h" |
110 | 511d2b14 | blueswir1 | #include "audio/audio.h" |
111 | 511d2b14 | blueswir1 | #include "qemu_socket.h" |
112 | bb9ea79e | aliguori | #include "qemu-log.h" |
113 | f83c6e10 | Mark McLoughlin | #include "qemu-config.h" |
114 | 511d2b14 | blueswir1 | |
115 | d918f23e | Jan Kiszka | #include "slirp/libslirp.h" |
116 | 511d2b14 | blueswir1 | |
117 | 5610c3aa | Mark McLoughlin | static QTAILQ_HEAD(, VLANState) vlans;
|
118 | 577c4af9 | Mark McLoughlin | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
|
119 | 63a01ef8 | aliguori | |
120 | 63a01ef8 | aliguori | /***********************************************************/
|
121 | 63a01ef8 | aliguori | /* network device redirectors */
|
122 | 63a01ef8 | aliguori | |
123 | 63a01ef8 | aliguori | #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
|
124 | 63a01ef8 | aliguori | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
125 | 63a01ef8 | aliguori | { |
126 | 63a01ef8 | aliguori | int len, i, j, c;
|
127 | 63a01ef8 | aliguori | |
128 | 63a01ef8 | aliguori | for(i=0;i<size;i+=16) { |
129 | 63a01ef8 | aliguori | len = size - i; |
130 | 63a01ef8 | aliguori | if (len > 16) |
131 | 63a01ef8 | aliguori | len = 16;
|
132 | 63a01ef8 | aliguori | fprintf(f, "%08x ", i);
|
133 | 63a01ef8 | aliguori | for(j=0;j<16;j++) { |
134 | 63a01ef8 | aliguori | if (j < len)
|
135 | 63a01ef8 | aliguori | fprintf(f, " %02x", buf[i+j]);
|
136 | 63a01ef8 | aliguori | else
|
137 | 63a01ef8 | aliguori | fprintf(f, " ");
|
138 | 63a01ef8 | aliguori | } |
139 | 63a01ef8 | aliguori | fprintf(f, " ");
|
140 | 63a01ef8 | aliguori | for(j=0;j<len;j++) { |
141 | 63a01ef8 | aliguori | c = buf[i+j]; |
142 | 63a01ef8 | aliguori | if (c < ' ' || c > '~') |
143 | 63a01ef8 | aliguori | c = '.';
|
144 | 63a01ef8 | aliguori | fprintf(f, "%c", c);
|
145 | 63a01ef8 | aliguori | } |
146 | 63a01ef8 | aliguori | fprintf(f, "\n");
|
147 | 63a01ef8 | aliguori | } |
148 | 63a01ef8 | aliguori | } |
149 | 63a01ef8 | aliguori | #endif
|
150 | 63a01ef8 | aliguori | |
151 | 63a01ef8 | aliguori | static int parse_macaddr(uint8_t *macaddr, const char *p) |
152 | 63a01ef8 | aliguori | { |
153 | 63a01ef8 | aliguori | int i;
|
154 | 63a01ef8 | aliguori | char *last_char;
|
155 | 63a01ef8 | aliguori | long int offset; |
156 | 63a01ef8 | aliguori | |
157 | 63a01ef8 | aliguori | errno = 0;
|
158 | 63a01ef8 | aliguori | offset = strtol(p, &last_char, 0);
|
159 | 63a01ef8 | aliguori | if (0 == errno && '\0' == *last_char && |
160 | 63a01ef8 | aliguori | offset >= 0 && offset <= 0xFFFFFF) { |
161 | 63a01ef8 | aliguori | macaddr[3] = (offset & 0xFF0000) >> 16; |
162 | 63a01ef8 | aliguori | macaddr[4] = (offset & 0xFF00) >> 8; |
163 | 63a01ef8 | aliguori | macaddr[5] = offset & 0xFF; |
164 | 63a01ef8 | aliguori | return 0; |
165 | 63a01ef8 | aliguori | } else {
|
166 | 63a01ef8 | aliguori | for(i = 0; i < 6; i++) { |
167 | 63a01ef8 | aliguori | macaddr[i] = strtol(p, (char **)&p, 16); |
168 | 63a01ef8 | aliguori | if (i == 5) { |
169 | 63a01ef8 | aliguori | if (*p != '\0') |
170 | 63a01ef8 | aliguori | return -1; |
171 | 63a01ef8 | aliguori | } else {
|
172 | 63a01ef8 | aliguori | if (*p != ':' && *p != '-') |
173 | 63a01ef8 | aliguori | return -1; |
174 | 63a01ef8 | aliguori | p++; |
175 | 63a01ef8 | aliguori | } |
176 | 63a01ef8 | aliguori | } |
177 | 63a01ef8 | aliguori | return 0; |
178 | 63a01ef8 | aliguori | } |
179 | 63a01ef8 | aliguori | |
180 | 63a01ef8 | aliguori | return -1; |
181 | 63a01ef8 | aliguori | } |
182 | 63a01ef8 | aliguori | |
183 | 63a01ef8 | aliguori | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
184 | 63a01ef8 | aliguori | { |
185 | 63a01ef8 | aliguori | const char *p, *p1; |
186 | 63a01ef8 | aliguori | int len;
|
187 | 63a01ef8 | aliguori | p = *pp; |
188 | 63a01ef8 | aliguori | p1 = strchr(p, sep); |
189 | 63a01ef8 | aliguori | if (!p1)
|
190 | 63a01ef8 | aliguori | return -1; |
191 | 63a01ef8 | aliguori | len = p1 - p; |
192 | 63a01ef8 | aliguori | p1++; |
193 | 63a01ef8 | aliguori | if (buf_size > 0) { |
194 | 63a01ef8 | aliguori | if (len > buf_size - 1) |
195 | 63a01ef8 | aliguori | len = buf_size - 1;
|
196 | 63a01ef8 | aliguori | memcpy(buf, p, len); |
197 | 63a01ef8 | aliguori | buf[len] = '\0';
|
198 | 63a01ef8 | aliguori | } |
199 | 63a01ef8 | aliguori | *pp = p1; |
200 | 63a01ef8 | aliguori | return 0; |
201 | 63a01ef8 | aliguori | } |
202 | 63a01ef8 | aliguori | |
203 | 63a01ef8 | aliguori | int parse_host_src_port(struct sockaddr_in *haddr, |
204 | 63a01ef8 | aliguori | struct sockaddr_in *saddr,
|
205 | 63a01ef8 | aliguori | const char *input_str) |
206 | 63a01ef8 | aliguori | { |
207 | 63a01ef8 | aliguori | char *str = strdup(input_str);
|
208 | 63a01ef8 | aliguori | char *host_str = str;
|
209 | 63a01ef8 | aliguori | char *src_str;
|
210 | 63a01ef8 | aliguori | const char *src_str2; |
211 | 63a01ef8 | aliguori | char *ptr;
|
212 | 63a01ef8 | aliguori | |
213 | 63a01ef8 | aliguori | /*
|
214 | 63a01ef8 | aliguori | * Chop off any extra arguments at the end of the string which
|
215 | 63a01ef8 | aliguori | * would start with a comma, then fill in the src port information
|
216 | 63a01ef8 | aliguori | * if it was provided else use the "any address" and "any port".
|
217 | 63a01ef8 | aliguori | */
|
218 | 63a01ef8 | aliguori | if ((ptr = strchr(str,','))) |
219 | 63a01ef8 | aliguori | *ptr = '\0';
|
220 | 63a01ef8 | aliguori | |
221 | 63a01ef8 | aliguori | if ((src_str = strchr(input_str,'@'))) { |
222 | 63a01ef8 | aliguori | *src_str = '\0';
|
223 | 63a01ef8 | aliguori | src_str++; |
224 | 63a01ef8 | aliguori | } |
225 | 63a01ef8 | aliguori | |
226 | 63a01ef8 | aliguori | if (parse_host_port(haddr, host_str) < 0) |
227 | 63a01ef8 | aliguori | goto fail;
|
228 | 63a01ef8 | aliguori | |
229 | 63a01ef8 | aliguori | src_str2 = src_str; |
230 | 63a01ef8 | aliguori | if (!src_str || *src_str == '\0') |
231 | 63a01ef8 | aliguori | src_str2 = ":0";
|
232 | 63a01ef8 | aliguori | |
233 | 63a01ef8 | aliguori | if (parse_host_port(saddr, src_str2) < 0) |
234 | 63a01ef8 | aliguori | goto fail;
|
235 | 63a01ef8 | aliguori | |
236 | 63a01ef8 | aliguori | free(str); |
237 | 63a01ef8 | aliguori | return(0); |
238 | 63a01ef8 | aliguori | |
239 | 63a01ef8 | aliguori | fail:
|
240 | 63a01ef8 | aliguori | free(str); |
241 | 63a01ef8 | aliguori | return -1; |
242 | 63a01ef8 | aliguori | } |
243 | 63a01ef8 | aliguori | |
244 | 63a01ef8 | aliguori | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
245 | 63a01ef8 | aliguori | { |
246 | 63a01ef8 | aliguori | char buf[512]; |
247 | 63a01ef8 | aliguori | struct hostent *he;
|
248 | 63a01ef8 | aliguori | const char *p, *r; |
249 | 63a01ef8 | aliguori | int port;
|
250 | 63a01ef8 | aliguori | |
251 | 63a01ef8 | aliguori | p = str; |
252 | 63a01ef8 | aliguori | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
253 | 63a01ef8 | aliguori | return -1; |
254 | 63a01ef8 | aliguori | saddr->sin_family = AF_INET; |
255 | 63a01ef8 | aliguori | if (buf[0] == '\0') { |
256 | 63a01ef8 | aliguori | saddr->sin_addr.s_addr = 0;
|
257 | 63a01ef8 | aliguori | } else {
|
258 | cd390083 | blueswir1 | if (qemu_isdigit(buf[0])) { |
259 | 63a01ef8 | aliguori | if (!inet_aton(buf, &saddr->sin_addr))
|
260 | 63a01ef8 | aliguori | return -1; |
261 | 63a01ef8 | aliguori | } else {
|
262 | 63a01ef8 | aliguori | if ((he = gethostbyname(buf)) == NULL) |
263 | 63a01ef8 | aliguori | return - 1; |
264 | 63a01ef8 | aliguori | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
265 | 63a01ef8 | aliguori | } |
266 | 63a01ef8 | aliguori | } |
267 | 63a01ef8 | aliguori | port = strtol(p, (char **)&r, 0); |
268 | 63a01ef8 | aliguori | if (r == p)
|
269 | 63a01ef8 | aliguori | return -1; |
270 | 63a01ef8 | aliguori | saddr->sin_port = htons(port); |
271 | 63a01ef8 | aliguori | return 0; |
272 | 63a01ef8 | aliguori | } |
273 | 63a01ef8 | aliguori | |
274 | 7cb7434b | aliguori | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
275 | 7cb7434b | aliguori | { |
276 | 7cb7434b | aliguori | snprintf(vc->info_str, sizeof(vc->info_str),
|
277 | 4dda4063 | aliguori | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
|
278 | 4dda4063 | aliguori | vc->model, |
279 | 7cb7434b | aliguori | macaddr[0], macaddr[1], macaddr[2], |
280 | 7cb7434b | aliguori | macaddr[3], macaddr[4], macaddr[5]); |
281 | 7cb7434b | aliguori | } |
282 | 7cb7434b | aliguori | |
283 | 76d32cba | Gerd Hoffmann | void qemu_macaddr_default_if_unset(MACAddr *macaddr)
|
284 | 76d32cba | Gerd Hoffmann | { |
285 | 76d32cba | Gerd Hoffmann | static int index = 0; |
286 | 76d32cba | Gerd Hoffmann | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
287 | 76d32cba | Gerd Hoffmann | |
288 | 76d32cba | Gerd Hoffmann | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
289 | 76d32cba | Gerd Hoffmann | return;
|
290 | 76d32cba | Gerd Hoffmann | macaddr->a[0] = 0x52; |
291 | 76d32cba | Gerd Hoffmann | macaddr->a[1] = 0x54; |
292 | 76d32cba | Gerd Hoffmann | macaddr->a[2] = 0x00; |
293 | 76d32cba | Gerd Hoffmann | macaddr->a[3] = 0x12; |
294 | 76d32cba | Gerd Hoffmann | macaddr->a[4] = 0x34; |
295 | 76d32cba | Gerd Hoffmann | macaddr->a[5] = 0x56 + index++; |
296 | 76d32cba | Gerd Hoffmann | } |
297 | 76d32cba | Gerd Hoffmann | |
298 | 676cff29 | aliguori | static char *assign_name(VLANClientState *vc1, const char *model) |
299 | 676cff29 | aliguori | { |
300 | 676cff29 | aliguori | VLANState *vlan; |
301 | 676cff29 | aliguori | char buf[256]; |
302 | 676cff29 | aliguori | int id = 0; |
303 | 676cff29 | aliguori | |
304 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
305 | 676cff29 | aliguori | VLANClientState *vc; |
306 | 676cff29 | aliguori | |
307 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
308 | 5610c3aa | Mark McLoughlin | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
309 | 676cff29 | aliguori | id++; |
310 | 5610c3aa | Mark McLoughlin | } |
311 | 5610c3aa | Mark McLoughlin | } |
312 | 676cff29 | aliguori | } |
313 | 676cff29 | aliguori | |
314 | 676cff29 | aliguori | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
315 | 676cff29 | aliguori | |
316 | 02374aa0 | Mark McLoughlin | return qemu_strdup(buf);
|
317 | 676cff29 | aliguori | } |
318 | 676cff29 | aliguori | |
319 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
320 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
321 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
322 | 9a6ecb30 | Mark McLoughlin | size_t size, |
323 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
324 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
325 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
326 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
327 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
328 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
329 | 9a6ecb30 | Mark McLoughlin | |
330 | bb6e6364 | Mark McLoughlin | VLANClientState *qemu_new_vlan_client(net_client_type type, |
331 | bb6e6364 | Mark McLoughlin | VLANState *vlan, |
332 | 283c7c63 | Mark McLoughlin | VLANClientState *peer, |
333 | bf38c1a0 | aliguori | const char *model, |
334 | 7a9f6e4a | aliguori | const char *name, |
335 | cda9046b | Mark McLoughlin | NetCanReceive *can_receive, |
336 | cda9046b | Mark McLoughlin | NetReceive *receive, |
337 | 70783b9c | Mark McLoughlin | NetReceive *receive_raw, |
338 | cda9046b | Mark McLoughlin | NetReceiveIOV *receive_iov, |
339 | b946a153 | aliguori | NetCleanup *cleanup, |
340 | 63a01ef8 | aliguori | void *opaque)
|
341 | 63a01ef8 | aliguori | { |
342 | 5610c3aa | Mark McLoughlin | VLANClientState *vc; |
343 | 5610c3aa | Mark McLoughlin | |
344 | 63a01ef8 | aliguori | vc = qemu_mallocz(sizeof(VLANClientState));
|
345 | 5610c3aa | Mark McLoughlin | |
346 | bb6e6364 | Mark McLoughlin | vc->type = type; |
347 | 02374aa0 | Mark McLoughlin | vc->model = qemu_strdup(model); |
348 | 7a9f6e4a | aliguori | if (name)
|
349 | 02374aa0 | Mark McLoughlin | vc->name = qemu_strdup(name); |
350 | 7a9f6e4a | aliguori | else
|
351 | 7a9f6e4a | aliguori | vc->name = assign_name(vc, model); |
352 | cda9046b | Mark McLoughlin | vc->can_receive = can_receive; |
353 | cda9046b | Mark McLoughlin | vc->receive = receive; |
354 | 70783b9c | Mark McLoughlin | vc->receive_raw = receive_raw; |
355 | cda9046b | Mark McLoughlin | vc->receive_iov = receive_iov; |
356 | b946a153 | aliguori | vc->cleanup = cleanup; |
357 | 63a01ef8 | aliguori | vc->opaque = opaque; |
358 | 5610c3aa | Mark McLoughlin | |
359 | d80b9fc6 | Mark McLoughlin | if (vlan) {
|
360 | 283c7c63 | Mark McLoughlin | assert(!peer); |
361 | d80b9fc6 | Mark McLoughlin | vc->vlan = vlan; |
362 | d80b9fc6 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
363 | 577c4af9 | Mark McLoughlin | } else {
|
364 | 283c7c63 | Mark McLoughlin | if (peer) {
|
365 | 283c7c63 | Mark McLoughlin | vc->peer = peer; |
366 | 283c7c63 | Mark McLoughlin | peer->peer = vc; |
367 | 283c7c63 | Mark McLoughlin | } |
368 | 577c4af9 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
369 | 9a6ecb30 | Mark McLoughlin | |
370 | 9a6ecb30 | Mark McLoughlin | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
371 | 9a6ecb30 | Mark McLoughlin | qemu_deliver_packet_iov, |
372 | 9a6ecb30 | Mark McLoughlin | vc); |
373 | d80b9fc6 | Mark McLoughlin | } |
374 | 63a01ef8 | aliguori | |
375 | 63a01ef8 | aliguori | return vc;
|
376 | 63a01ef8 | aliguori | } |
377 | 63a01ef8 | aliguori | |
378 | 63a01ef8 | aliguori | void qemu_del_vlan_client(VLANClientState *vc)
|
379 | 63a01ef8 | aliguori | { |
380 | d80b9fc6 | Mark McLoughlin | if (vc->vlan) {
|
381 | d80b9fc6 | Mark McLoughlin | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
382 | 577c4af9 | Mark McLoughlin | } else {
|
383 | 9a6ecb30 | Mark McLoughlin | if (vc->send_queue) {
|
384 | 9a6ecb30 | Mark McLoughlin | qemu_del_net_queue(vc->send_queue); |
385 | 9a6ecb30 | Mark McLoughlin | } |
386 | 577c4af9 | Mark McLoughlin | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
387 | 283c7c63 | Mark McLoughlin | if (vc->peer) {
|
388 | 283c7c63 | Mark McLoughlin | vc->peer->peer = NULL;
|
389 | 283c7c63 | Mark McLoughlin | } |
390 | d80b9fc6 | Mark McLoughlin | } |
391 | 63a01ef8 | aliguori | |
392 | 5610c3aa | Mark McLoughlin | if (vc->cleanup) {
|
393 | 5610c3aa | Mark McLoughlin | vc->cleanup(vc); |
394 | 5610c3aa | Mark McLoughlin | } |
395 | 5610c3aa | Mark McLoughlin | |
396 | 5610c3aa | Mark McLoughlin | qemu_free(vc->name); |
397 | 5610c3aa | Mark McLoughlin | qemu_free(vc->model); |
398 | 5610c3aa | Mark McLoughlin | qemu_free(vc); |
399 | 63a01ef8 | aliguori | } |
400 | 63a01ef8 | aliguori | |
401 | 8b13c4a7 | aliguori | VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
|
402 | 8b13c4a7 | aliguori | { |
403 | 5610c3aa | Mark McLoughlin | VLANClientState *vc; |
404 | 8b13c4a7 | aliguori | |
405 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
406 | 5610c3aa | Mark McLoughlin | if (vc->opaque == opaque) {
|
407 | 5610c3aa | Mark McLoughlin | return vc;
|
408 | 5610c3aa | Mark McLoughlin | } |
409 | 5610c3aa | Mark McLoughlin | } |
410 | 8b13c4a7 | aliguori | |
411 | 8b13c4a7 | aliguori | return NULL; |
412 | 8b13c4a7 | aliguori | } |
413 | 8b13c4a7 | aliguori | |
414 | 1a609520 | Jan Kiszka | static VLANClientState *
|
415 | 1a609520 | Jan Kiszka | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
|
416 | 1a609520 | Jan Kiszka | const char *client_str) |
417 | 1a609520 | Jan Kiszka | { |
418 | 1a609520 | Jan Kiszka | VLANState *vlan; |
419 | 1a609520 | Jan Kiszka | VLANClientState *vc; |
420 | 1a609520 | Jan Kiszka | |
421 | 1a609520 | Jan Kiszka | vlan = qemu_find_vlan(vlan_id, 0);
|
422 | 1a609520 | Jan Kiszka | if (!vlan) {
|
423 | 1a609520 | Jan Kiszka | monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
|
424 | 1a609520 | Jan Kiszka | return NULL; |
425 | 1a609520 | Jan Kiszka | } |
426 | 1a609520 | Jan Kiszka | |
427 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
428 | 1a609520 | Jan Kiszka | if (!strcmp(vc->name, client_str)) {
|
429 | 1a609520 | Jan Kiszka | break;
|
430 | 1a609520 | Jan Kiszka | } |
431 | 1a609520 | Jan Kiszka | } |
432 | 1a609520 | Jan Kiszka | if (!vc) {
|
433 | 1a609520 | Jan Kiszka | monitor_printf(mon, "can't find device %s on VLAN %d\n",
|
434 | 1a609520 | Jan Kiszka | client_str, vlan_id); |
435 | 1a609520 | Jan Kiszka | } |
436 | 1a609520 | Jan Kiszka | |
437 | 1a609520 | Jan Kiszka | return vc;
|
438 | 1a609520 | Jan Kiszka | } |
439 | 1a609520 | Jan Kiszka | |
440 | 2e1e0641 | Mark McLoughlin | int qemu_can_send_packet(VLANClientState *sender)
|
441 | 63a01ef8 | aliguori | { |
442 | 2e1e0641 | Mark McLoughlin | VLANState *vlan = sender->vlan; |
443 | 63a01ef8 | aliguori | VLANClientState *vc; |
444 | 63a01ef8 | aliguori | |
445 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
446 | 9a6ecb30 | Mark McLoughlin | if (!sender->peer->can_receive ||
|
447 | 9a6ecb30 | Mark McLoughlin | sender->peer->can_receive(sender->peer)) { |
448 | 9a6ecb30 | Mark McLoughlin | return 1; |
449 | 9a6ecb30 | Mark McLoughlin | } else {
|
450 | 9a6ecb30 | Mark McLoughlin | return 0; |
451 | 9a6ecb30 | Mark McLoughlin | } |
452 | 9a6ecb30 | Mark McLoughlin | } |
453 | 9a6ecb30 | Mark McLoughlin | |
454 | d80b9fc6 | Mark McLoughlin | if (!sender->vlan) {
|
455 | d80b9fc6 | Mark McLoughlin | return 1; |
456 | d80b9fc6 | Mark McLoughlin | } |
457 | d80b9fc6 | Mark McLoughlin | |
458 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
459 | 2e1e0641 | Mark McLoughlin | if (vc == sender) {
|
460 | 2e1e0641 | Mark McLoughlin | continue;
|
461 | 2e1e0641 | Mark McLoughlin | } |
462 | 2e1e0641 | Mark McLoughlin | |
463 | cda9046b | Mark McLoughlin | /* no can_receive() handler, they can always receive */
|
464 | e3f5ec2b | Mark McLoughlin | if (!vc->can_receive || vc->can_receive(vc)) {
|
465 | 2e1e0641 | Mark McLoughlin | return 1; |
466 | 63a01ef8 | aliguori | } |
467 | 63a01ef8 | aliguori | } |
468 | 63a01ef8 | aliguori | return 0; |
469 | 63a01ef8 | aliguori | } |
470 | 63a01ef8 | aliguori | |
471 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
472 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
473 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
474 | 9a6ecb30 | Mark McLoughlin | size_t size, |
475 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
476 | 9a6ecb30 | Mark McLoughlin | { |
477 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
478 | 9a6ecb30 | Mark McLoughlin | |
479 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
480 | 9a6ecb30 | Mark McLoughlin | return size;
|
481 | 9a6ecb30 | Mark McLoughlin | } |
482 | 9a6ecb30 | Mark McLoughlin | |
483 | ca77d175 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw)
|
484 | ca77d175 | Mark McLoughlin | return vc->receive_raw(vc, data, size);
|
485 | ca77d175 | Mark McLoughlin | else
|
486 | ca77d175 | Mark McLoughlin | return vc->receive(vc, data, size);
|
487 | 9a6ecb30 | Mark McLoughlin | } |
488 | 9a6ecb30 | Mark McLoughlin | |
489 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
|
490 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
491 | f7105843 | Mark McLoughlin | const uint8_t *buf,
|
492 | f7105843 | Mark McLoughlin | size_t size, |
493 | f7105843 | Mark McLoughlin | void *opaque)
|
494 | 63a01ef8 | aliguori | { |
495 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
496 | 63a01ef8 | aliguori | VLANClientState *vc; |
497 | 3e021d40 | Mark McLoughlin | int ret = -1; |
498 | 63a01ef8 | aliguori | |
499 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
500 | 3e021d40 | Mark McLoughlin | ssize_t len; |
501 | 3e021d40 | Mark McLoughlin | |
502 | 3e021d40 | Mark McLoughlin | if (vc == sender) {
|
503 | 3e021d40 | Mark McLoughlin | continue;
|
504 | 764a4d1d | aliguori | } |
505 | 3e021d40 | Mark McLoughlin | |
506 | 3e021d40 | Mark McLoughlin | if (vc->link_down) {
|
507 | 3e021d40 | Mark McLoughlin | ret = size; |
508 | 3e021d40 | Mark McLoughlin | continue;
|
509 | 3e021d40 | Mark McLoughlin | } |
510 | 3e021d40 | Mark McLoughlin | |
511 | ca77d175 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw)
|
512 | ca77d175 | Mark McLoughlin | len = vc->receive_raw(vc, buf, size); |
513 | ca77d175 | Mark McLoughlin | else
|
514 | ca77d175 | Mark McLoughlin | len = vc->receive(vc, buf, size); |
515 | 3e021d40 | Mark McLoughlin | |
516 | 3e021d40 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
517 | 764a4d1d | aliguori | } |
518 | 3e021d40 | Mark McLoughlin | |
519 | 3e021d40 | Mark McLoughlin | return ret;
|
520 | 764a4d1d | aliguori | } |
521 | 764a4d1d | aliguori | |
522 | 8cad5516 | Mark McLoughlin | void qemu_purge_queued_packets(VLANClientState *vc)
|
523 | 8cad5516 | Mark McLoughlin | { |
524 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
525 | 9a6ecb30 | Mark McLoughlin | |
526 | 9a6ecb30 | Mark McLoughlin | if (!vc->peer && !vc->vlan) {
|
527 | d80b9fc6 | Mark McLoughlin | return;
|
528 | 9a6ecb30 | Mark McLoughlin | } |
529 | d80b9fc6 | Mark McLoughlin | |
530 | 9a6ecb30 | Mark McLoughlin | if (vc->peer) {
|
531 | 9a6ecb30 | Mark McLoughlin | queue = vc->peer->send_queue; |
532 | 9a6ecb30 | Mark McLoughlin | } else {
|
533 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
534 | 9a6ecb30 | Mark McLoughlin | } |
535 | 9a6ecb30 | Mark McLoughlin | |
536 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_purge(queue, vc); |
537 | 8cad5516 | Mark McLoughlin | } |
538 | 8cad5516 | Mark McLoughlin | |
539 | f3b6c7fc | Mark McLoughlin | void qemu_flush_queued_packets(VLANClientState *vc)
|
540 | e94667b9 | Mark McLoughlin | { |
541 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
542 | 9a6ecb30 | Mark McLoughlin | |
543 | 9a6ecb30 | Mark McLoughlin | if (vc->vlan) {
|
544 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
545 | 9a6ecb30 | Mark McLoughlin | } else {
|
546 | 9a6ecb30 | Mark McLoughlin | queue = vc->send_queue; |
547 | 9a6ecb30 | Mark McLoughlin | } |
548 | d80b9fc6 | Mark McLoughlin | |
549 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_flush(queue); |
550 | e94667b9 | Mark McLoughlin | } |
551 | e94667b9 | Mark McLoughlin | |
552 | ca77d175 | Mark McLoughlin | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
|
553 | ca77d175 | Mark McLoughlin | unsigned flags,
|
554 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
555 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
556 | 764a4d1d | aliguori | { |
557 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
558 | 436e5e53 | aliguori | |
559 | 63a01ef8 | aliguori | #ifdef DEBUG_NET
|
560 | d80b9fc6 | Mark McLoughlin | printf("qemu_send_packet_async:\n");
|
561 | 63a01ef8 | aliguori | hex_dump(stdout, buf, size); |
562 | 63a01ef8 | aliguori | #endif
|
563 | f3b6c7fc | Mark McLoughlin | |
564 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
565 | 9a6ecb30 | Mark McLoughlin | return size;
|
566 | 9a6ecb30 | Mark McLoughlin | } |
567 | 9a6ecb30 | Mark McLoughlin | |
568 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
569 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
570 | 9a6ecb30 | Mark McLoughlin | } else {
|
571 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
572 | 9a6ecb30 | Mark McLoughlin | } |
573 | 9a6ecb30 | Mark McLoughlin | |
574 | ca77d175 | Mark McLoughlin | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
|
575 | ca77d175 | Mark McLoughlin | } |
576 | ca77d175 | Mark McLoughlin | |
577 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_async(VLANClientState *sender, |
578 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
579 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
580 | ca77d175 | Mark McLoughlin | { |
581 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
|
582 | ca77d175 | Mark McLoughlin | buf, size, sent_cb); |
583 | f3b6c7fc | Mark McLoughlin | } |
584 | f3b6c7fc | Mark McLoughlin | |
585 | f3b6c7fc | Mark McLoughlin | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
586 | f3b6c7fc | Mark McLoughlin | { |
587 | f3b6c7fc | Mark McLoughlin | qemu_send_packet_async(vc, buf, size, NULL);
|
588 | 63a01ef8 | aliguori | } |
589 | 63a01ef8 | aliguori | |
590 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
591 | ca77d175 | Mark McLoughlin | { |
592 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
|
593 | ca77d175 | Mark McLoughlin | buf, size, NULL);
|
594 | ca77d175 | Mark McLoughlin | } |
595 | ca77d175 | Mark McLoughlin | |
596 | fbe78f4f | aliguori | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
597 | fbe78f4f | aliguori | int iovcnt)
|
598 | fbe78f4f | aliguori | { |
599 | fbe78f4f | aliguori | uint8_t buffer[4096];
|
600 | fbe78f4f | aliguori | size_t offset = 0;
|
601 | fbe78f4f | aliguori | int i;
|
602 | fbe78f4f | aliguori | |
603 | fbe78f4f | aliguori | for (i = 0; i < iovcnt; i++) { |
604 | fbe78f4f | aliguori | size_t len; |
605 | fbe78f4f | aliguori | |
606 | fbe78f4f | aliguori | len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
|
607 | fbe78f4f | aliguori | memcpy(buffer + offset, iov[i].iov_base, len); |
608 | fbe78f4f | aliguori | offset += len; |
609 | fbe78f4f | aliguori | } |
610 | fbe78f4f | aliguori | |
611 | f3b6c7fc | Mark McLoughlin | return vc->receive(vc, buffer, offset);
|
612 | fbe78f4f | aliguori | } |
613 | fbe78f4f | aliguori | |
614 | e0e7877a | aliguori | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
615 | e0e7877a | aliguori | { |
616 | e0e7877a | aliguori | size_t offset = 0;
|
617 | e0e7877a | aliguori | int i;
|
618 | e0e7877a | aliguori | |
619 | e0e7877a | aliguori | for (i = 0; i < iovcnt; i++) |
620 | e0e7877a | aliguori | offset += iov[i].iov_len; |
621 | e0e7877a | aliguori | return offset;
|
622 | e0e7877a | aliguori | } |
623 | e0e7877a | aliguori | |
624 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
625 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
626 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
627 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
628 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
629 | 9a6ecb30 | Mark McLoughlin | { |
630 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
631 | 9a6ecb30 | Mark McLoughlin | |
632 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
633 | 9a6ecb30 | Mark McLoughlin | return calc_iov_length(iov, iovcnt);
|
634 | 9a6ecb30 | Mark McLoughlin | } |
635 | 9a6ecb30 | Mark McLoughlin | |
636 | 9a6ecb30 | Mark McLoughlin | if (vc->receive_iov) {
|
637 | 9a6ecb30 | Mark McLoughlin | return vc->receive_iov(vc, iov, iovcnt);
|
638 | 9a6ecb30 | Mark McLoughlin | } else {
|
639 | 9a6ecb30 | Mark McLoughlin | return vc_sendv_compat(vc, iov, iovcnt);
|
640 | 9a6ecb30 | Mark McLoughlin | } |
641 | 9a6ecb30 | Mark McLoughlin | } |
642 | 9a6ecb30 | Mark McLoughlin | |
643 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
|
644 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
645 | f7105843 | Mark McLoughlin | const struct iovec *iov, |
646 | f7105843 | Mark McLoughlin | int iovcnt,
|
647 | f7105843 | Mark McLoughlin | void *opaque)
|
648 | fbe78f4f | aliguori | { |
649 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
650 | fbe78f4f | aliguori | VLANClientState *vc; |
651 | f7105843 | Mark McLoughlin | ssize_t ret = -1;
|
652 | e94667b9 | Mark McLoughlin | |
653 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
654 | e94667b9 | Mark McLoughlin | ssize_t len; |
655 | e94667b9 | Mark McLoughlin | |
656 | e94667b9 | Mark McLoughlin | if (vc == sender) {
|
657 | e94667b9 | Mark McLoughlin | continue;
|
658 | e94667b9 | Mark McLoughlin | } |
659 | e94667b9 | Mark McLoughlin | |
660 | e94667b9 | Mark McLoughlin | if (vc->link_down) {
|
661 | e94667b9 | Mark McLoughlin | ret = calc_iov_length(iov, iovcnt); |
662 | e94667b9 | Mark McLoughlin | continue;
|
663 | e94667b9 | Mark McLoughlin | } |
664 | e94667b9 | Mark McLoughlin | |
665 | ca77d175 | Mark McLoughlin | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
666 | ca77d175 | Mark McLoughlin | |
667 | e94667b9 | Mark McLoughlin | if (vc->receive_iov) {
|
668 | e94667b9 | Mark McLoughlin | len = vc->receive_iov(vc, iov, iovcnt); |
669 | e94667b9 | Mark McLoughlin | } else {
|
670 | e94667b9 | Mark McLoughlin | len = vc_sendv_compat(vc, iov, iovcnt); |
671 | e94667b9 | Mark McLoughlin | } |
672 | e94667b9 | Mark McLoughlin | |
673 | e94667b9 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
674 | e94667b9 | Mark McLoughlin | } |
675 | e94667b9 | Mark McLoughlin | |
676 | e94667b9 | Mark McLoughlin | return ret;
|
677 | e94667b9 | Mark McLoughlin | } |
678 | e94667b9 | Mark McLoughlin | |
679 | f3b6c7fc | Mark McLoughlin | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
680 | f3b6c7fc | Mark McLoughlin | const struct iovec *iov, int iovcnt, |
681 | f3b6c7fc | Mark McLoughlin | NetPacketSent *sent_cb) |
682 | e94667b9 | Mark McLoughlin | { |
683 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
684 | 9a6ecb30 | Mark McLoughlin | |
685 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
686 | e94667b9 | Mark McLoughlin | return calc_iov_length(iov, iovcnt);
|
687 | e94667b9 | Mark McLoughlin | } |
688 | e94667b9 | Mark McLoughlin | |
689 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
690 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
691 | 9a6ecb30 | Mark McLoughlin | } else {
|
692 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
693 | 9a6ecb30 | Mark McLoughlin | } |
694 | 9a6ecb30 | Mark McLoughlin | |
695 | c0b8e49c | Mark McLoughlin | return qemu_net_queue_send_iov(queue, sender,
|
696 | c0b8e49c | Mark McLoughlin | QEMU_NET_PACKET_FLAG_NONE, |
697 | c0b8e49c | Mark McLoughlin | iov, iovcnt, sent_cb); |
698 | fbe78f4f | aliguori | } |
699 | fbe78f4f | aliguori | |
700 | f3b6c7fc | Mark McLoughlin | ssize_t |
701 | f3b6c7fc | Mark McLoughlin | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
702 | f3b6c7fc | Mark McLoughlin | { |
703 | f3b6c7fc | Mark McLoughlin | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
704 | f3b6c7fc | Mark McLoughlin | } |
705 | f3b6c7fc | Mark McLoughlin | |
706 | 63a01ef8 | aliguori | #if defined(CONFIG_SLIRP)
|
707 | 63a01ef8 | aliguori | |
708 | 63a01ef8 | aliguori | /* slirp network adapter */
|
709 | 63a01ef8 | aliguori | |
710 | c92ef6a2 | Jan Kiszka | #define SLIRP_CFG_HOSTFWD 1 |
711 | c92ef6a2 | Jan Kiszka | #define SLIRP_CFG_LEGACY 2 |
712 | ad196a9d | Jan Kiszka | |
713 | b8e8af38 | Jan Kiszka | struct slirp_config_str {
|
714 | b8e8af38 | Jan Kiszka | struct slirp_config_str *next;
|
715 | ad196a9d | Jan Kiszka | int flags;
|
716 | ad196a9d | Jan Kiszka | char str[1024]; |
717 | c92ef6a2 | Jan Kiszka | int legacy_format;
|
718 | b8e8af38 | Jan Kiszka | }; |
719 | b8e8af38 | Jan Kiszka | |
720 | 9f8bd042 | Jan Kiszka | typedef struct SlirpState { |
721 | 72cf2d4f | Blue Swirl | QTAILQ_ENTRY(SlirpState) entry; |
722 | 9f8bd042 | Jan Kiszka | VLANClientState *vc; |
723 | 9f8bd042 | Jan Kiszka | Slirp *slirp; |
724 | 28432466 | Jan Kiszka | #ifndef _WIN32
|
725 | 28432466 | Jan Kiszka | char smb_dir[128]; |
726 | 28432466 | Jan Kiszka | #endif
|
727 | 9f8bd042 | Jan Kiszka | } SlirpState; |
728 | 9f8bd042 | Jan Kiszka | |
729 | ad196a9d | Jan Kiszka | static struct slirp_config_str *slirp_configs; |
730 | ad196a9d | Jan Kiszka | const char *legacy_tftp_prefix; |
731 | ad196a9d | Jan Kiszka | const char *legacy_bootp_filename; |
732 | 72cf2d4f | Blue Swirl | static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
|
733 | 72cf2d4f | Blue Swirl | QTAILQ_HEAD_INITIALIZER(slirp_stacks); |
734 | 63a01ef8 | aliguori | |
735 | fb12577c | Markus Armbruster | static int slirp_hostfwd(SlirpState *s, const char *redir_str, |
736 | 0752706d | Markus Armbruster | int legacy_format);
|
737 | fb12577c | Markus Armbruster | static int slirp_guestfwd(SlirpState *s, const char *config_str, |
738 | 3c6a0580 | Jan Kiszka | int legacy_format);
|
739 | ad196a9d | Jan Kiszka | |
740 | c5b76b38 | Blue Swirl | #ifndef _WIN32
|
741 | ad196a9d | Jan Kiszka | static const char *legacy_smb_export; |
742 | ad196a9d | Jan Kiszka | |
743 | fb12577c | Markus Armbruster | static int slirp_smb(SlirpState *s, const char *exported_dir, |
744 | 0752706d | Markus Armbruster | struct in_addr vserver_addr);
|
745 | 28432466 | Jan Kiszka | static void slirp_smb_cleanup(SlirpState *s); |
746 | 28432466 | Jan Kiszka | #else
|
747 | 28432466 | Jan Kiszka | static inline void slirp_smb_cleanup(SlirpState *s) { } |
748 | c5b76b38 | Blue Swirl | #endif
|
749 | b8e8af38 | Jan Kiszka | |
750 | 9f8bd042 | Jan Kiszka | int slirp_can_output(void *opaque) |
751 | 63a01ef8 | aliguori | { |
752 | 9f8bd042 | Jan Kiszka | SlirpState *s = opaque; |
753 | 9f8bd042 | Jan Kiszka | |
754 | 9f8bd042 | Jan Kiszka | return qemu_can_send_packet(s->vc);
|
755 | 63a01ef8 | aliguori | } |
756 | 63a01ef8 | aliguori | |
757 | 9f8bd042 | Jan Kiszka | void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len) |
758 | 63a01ef8 | aliguori | { |
759 | 9f8bd042 | Jan Kiszka | SlirpState *s = opaque; |
760 | 9f8bd042 | Jan Kiszka | |
761 | 63a01ef8 | aliguori | #ifdef DEBUG_SLIRP
|
762 | 63a01ef8 | aliguori | printf("slirp output:\n");
|
763 | 63a01ef8 | aliguori | hex_dump(stdout, pkt, pkt_len); |
764 | 63a01ef8 | aliguori | #endif
|
765 | 9f8bd042 | Jan Kiszka | qemu_send_packet(s->vc, pkt, pkt_len); |
766 | 63a01ef8 | aliguori | } |
767 | 63a01ef8 | aliguori | |
768 | 4f1c942b | Mark McLoughlin | static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
769 | 63a01ef8 | aliguori | { |
770 | 9f8bd042 | Jan Kiszka | SlirpState *s = vc->opaque; |
771 | 9f8bd042 | Jan Kiszka | |
772 | 63a01ef8 | aliguori | #ifdef DEBUG_SLIRP
|
773 | 63a01ef8 | aliguori | printf("slirp input:\n");
|
774 | 63a01ef8 | aliguori | hex_dump(stdout, buf, size); |
775 | 63a01ef8 | aliguori | #endif
|
776 | 9f8bd042 | Jan Kiszka | slirp_input(s->slirp, buf, size); |
777 | 4f1c942b | Mark McLoughlin | return size;
|
778 | 63a01ef8 | aliguori | } |
779 | 63a01ef8 | aliguori | |
780 | 8d6249a7 | aliguori | static void net_slirp_cleanup(VLANClientState *vc) |
781 | 8d6249a7 | aliguori | { |
782 | ad0d8c4c | Jan Kiszka | SlirpState *s = vc->opaque; |
783 | ad0d8c4c | Jan Kiszka | |
784 | ad0d8c4c | Jan Kiszka | slirp_cleanup(s->slirp); |
785 | 28432466 | Jan Kiszka | slirp_smb_cleanup(s); |
786 | 72cf2d4f | Blue Swirl | QTAILQ_REMOVE(&slirp_stacks, s, entry); |
787 | ad0d8c4c | Jan Kiszka | qemu_free(s); |
788 | 8d6249a7 | aliguori | } |
789 | 8d6249a7 | aliguori | |
790 | fb12577c | Markus Armbruster | static int net_slirp_init(VLANState *vlan, const char *model, |
791 | c92ef6a2 | Jan Kiszka | const char *name, int restricted, |
792 | c92ef6a2 | Jan Kiszka | const char *vnetwork, const char *vhost, |
793 | c92ef6a2 | Jan Kiszka | const char *vhostname, const char *tftp_export, |
794 | c92ef6a2 | Jan Kiszka | const char *bootfile, const char *vdhcp_start, |
795 | c92ef6a2 | Jan Kiszka | const char *vnameserver, const char *smb_export, |
796 | c92ef6a2 | Jan Kiszka | const char *vsmbserver) |
797 | 63a01ef8 | aliguori | { |
798 | ad0d8c4c | Jan Kiszka | /* default settings according to historic slirp */
|
799 | 8389e7f4 | Anthony Liguori | struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */ |
800 | 8389e7f4 | Anthony Liguori | struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */ |
801 | ad0d8c4c | Jan Kiszka | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ |
802 | ad0d8c4c | Jan Kiszka | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ |
803 | ad0d8c4c | Jan Kiszka | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ |
804 | c92ef6a2 | Jan Kiszka | #ifndef _WIN32
|
805 | ad0d8c4c | Jan Kiszka | struct in_addr smbsrv = { .s_addr = 0 }; |
806 | c92ef6a2 | Jan Kiszka | #endif
|
807 | ad0d8c4c | Jan Kiszka | SlirpState *s; |
808 | ad0d8c4c | Jan Kiszka | char buf[20]; |
809 | ad0d8c4c | Jan Kiszka | uint32_t addr; |
810 | ad0d8c4c | Jan Kiszka | int shift;
|
811 | ad0d8c4c | Jan Kiszka | char *end;
|
812 | 3a179c66 | Markus Armbruster | struct slirp_config_str *config;
|
813 | ad0d8c4c | Jan Kiszka | |
814 | ad0d8c4c | Jan Kiszka | if (!tftp_export) {
|
815 | ad0d8c4c | Jan Kiszka | tftp_export = legacy_tftp_prefix; |
816 | ad0d8c4c | Jan Kiszka | } |
817 | ad0d8c4c | Jan Kiszka | if (!bootfile) {
|
818 | ad0d8c4c | Jan Kiszka | bootfile = legacy_bootp_filename; |
819 | ad0d8c4c | Jan Kiszka | } |
820 | c92ef6a2 | Jan Kiszka | |
821 | ad0d8c4c | Jan Kiszka | if (vnetwork) {
|
822 | ad0d8c4c | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { |
823 | ad0d8c4c | Jan Kiszka | if (!inet_aton(vnetwork, &net)) {
|
824 | ad0d8c4c | Jan Kiszka | return -1; |
825 | ad0d8c4c | Jan Kiszka | } |
826 | ad0d8c4c | Jan Kiszka | addr = ntohl(net.s_addr); |
827 | ad0d8c4c | Jan Kiszka | if (!(addr & 0x80000000)) { |
828 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xff000000); /* class A */ |
829 | ad0d8c4c | Jan Kiszka | } else if ((addr & 0xfff00000) == 0xac100000) { |
830 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ |
831 | ad0d8c4c | Jan Kiszka | } else if ((addr & 0xc0000000) == 0x80000000) { |
832 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xffff0000); /* class B */ |
833 | ad0d8c4c | Jan Kiszka | } else if ((addr & 0xffff0000) == 0xc0a80000) { |
834 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ |
835 | ad0d8c4c | Jan Kiszka | } else if ((addr & 0xffff0000) == 0xc6120000) { |
836 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ |
837 | ad0d8c4c | Jan Kiszka | } else if ((addr & 0xe0000000) == 0xe0000000) { |
838 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xffffff00); /* class C */ |
839 | c92ef6a2 | Jan Kiszka | } else {
|
840 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ |
841 | ad0d8c4c | Jan Kiszka | } |
842 | ad0d8c4c | Jan Kiszka | } else {
|
843 | ad0d8c4c | Jan Kiszka | if (!inet_aton(buf, &net)) {
|
844 | ad0d8c4c | Jan Kiszka | return -1; |
845 | ad0d8c4c | Jan Kiszka | } |
846 | ad0d8c4c | Jan Kiszka | shift = strtol(vnetwork, &end, 10);
|
847 | ad0d8c4c | Jan Kiszka | if (*end != '\0') { |
848 | ad0d8c4c | Jan Kiszka | if (!inet_aton(vnetwork, &mask)) {
|
849 | c92ef6a2 | Jan Kiszka | return -1; |
850 | c92ef6a2 | Jan Kiszka | } |
851 | ad0d8c4c | Jan Kiszka | } else if (shift < 4 || shift > 32) { |
852 | ad0d8c4c | Jan Kiszka | return -1; |
853 | ad0d8c4c | Jan Kiszka | } else {
|
854 | ad0d8c4c | Jan Kiszka | mask.s_addr = htonl(0xffffffff << (32 - shift)); |
855 | c92ef6a2 | Jan Kiszka | } |
856 | c92ef6a2 | Jan Kiszka | } |
857 | ad0d8c4c | Jan Kiszka | net.s_addr &= mask.s_addr; |
858 | ad0d8c4c | Jan Kiszka | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
|
859 | ad0d8c4c | Jan Kiszka | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
|
860 | ad0d8c4c | Jan Kiszka | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
|
861 | ad0d8c4c | Jan Kiszka | } |
862 | c92ef6a2 | Jan Kiszka | |
863 | ad0d8c4c | Jan Kiszka | if (vhost && !inet_aton(vhost, &host)) {
|
864 | ad0d8c4c | Jan Kiszka | return -1; |
865 | ad0d8c4c | Jan Kiszka | } |
866 | ad0d8c4c | Jan Kiszka | if ((host.s_addr & mask.s_addr) != net.s_addr) {
|
867 | ad0d8c4c | Jan Kiszka | return -1; |
868 | ad0d8c4c | Jan Kiszka | } |
869 | c92ef6a2 | Jan Kiszka | |
870 | ad0d8c4c | Jan Kiszka | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
|
871 | ad0d8c4c | Jan Kiszka | return -1; |
872 | ad0d8c4c | Jan Kiszka | } |
873 | ad0d8c4c | Jan Kiszka | if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
|
874 | ad0d8c4c | Jan Kiszka | dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { |
875 | ad0d8c4c | Jan Kiszka | return -1; |
876 | ad0d8c4c | Jan Kiszka | } |
877 | c92ef6a2 | Jan Kiszka | |
878 | ad0d8c4c | Jan Kiszka | if (vnameserver && !inet_aton(vnameserver, &dns)) {
|
879 | ad0d8c4c | Jan Kiszka | return -1; |
880 | ad0d8c4c | Jan Kiszka | } |
881 | ad0d8c4c | Jan Kiszka | if ((dns.s_addr & mask.s_addr) != net.s_addr ||
|
882 | ad0d8c4c | Jan Kiszka | dns.s_addr == host.s_addr) { |
883 | ad0d8c4c | Jan Kiszka | return -1; |
884 | ad0d8c4c | Jan Kiszka | } |
885 | c92ef6a2 | Jan Kiszka | |
886 | c92ef6a2 | Jan Kiszka | #ifndef _WIN32
|
887 | ad0d8c4c | Jan Kiszka | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
|
888 | ad0d8c4c | Jan Kiszka | return -1; |
889 | ad0d8c4c | Jan Kiszka | } |
890 | c92ef6a2 | Jan Kiszka | #endif
|
891 | c92ef6a2 | Jan Kiszka | |
892 | ad0d8c4c | Jan Kiszka | s = qemu_mallocz(sizeof(SlirpState));
|
893 | ad0d8c4c | Jan Kiszka | s->slirp = slirp_init(restricted, net, mask, host, vhostname, |
894 | ad0d8c4c | Jan Kiszka | tftp_export, bootfile, dhcp, dns, s); |
895 | 72cf2d4f | Blue Swirl | QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry); |
896 | b8e8af38 | Jan Kiszka | |
897 | 3a179c66 | Markus Armbruster | for (config = slirp_configs; config; config = config->next) {
|
898 | ad0d8c4c | Jan Kiszka | if (config->flags & SLIRP_CFG_HOSTFWD) {
|
899 | fb12577c | Markus Armbruster | if (slirp_hostfwd(s, config->str,
|
900 | 0752706d | Markus Armbruster | config->flags & SLIRP_CFG_LEGACY) < 0)
|
901 | 0752706d | Markus Armbruster | return -1; |
902 | ad0d8c4c | Jan Kiszka | } else {
|
903 | fb12577c | Markus Armbruster | if (slirp_guestfwd(s, config->str,
|
904 | 0752706d | Markus Armbruster | config->flags & SLIRP_CFG_LEGACY) < 0)
|
905 | 0752706d | Markus Armbruster | return -1; |
906 | b8e8af38 | Jan Kiszka | } |
907 | ad0d8c4c | Jan Kiszka | } |
908 | b8e8af38 | Jan Kiszka | #ifndef _WIN32
|
909 | ad0d8c4c | Jan Kiszka | if (!smb_export) {
|
910 | ad0d8c4c | Jan Kiszka | smb_export = legacy_smb_export; |
911 | ad0d8c4c | Jan Kiszka | } |
912 | ad0d8c4c | Jan Kiszka | if (smb_export) {
|
913 | fb12577c | Markus Armbruster | if (slirp_smb(s, smb_export, smbsrv) < 0) |
914 | 0752706d | Markus Armbruster | return -1; |
915 | 63a01ef8 | aliguori | } |
916 | ad0d8c4c | Jan Kiszka | #endif
|
917 | b8e8af38 | Jan Kiszka | |
918 | bb6e6364 | Mark McLoughlin | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SLIRP, |
919 | bb6e6364 | Mark McLoughlin | vlan, NULL, model, name, NULL, |
920 | 70783b9c | Mark McLoughlin | slirp_receive, NULL, NULL, |
921 | 9f8bd042 | Jan Kiszka | net_slirp_cleanup, s); |
922 | fc57bc57 | Jan Kiszka | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
923 | fc57bc57 | Jan Kiszka | "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n'); |
924 | 63a01ef8 | aliguori | return 0; |
925 | 63a01ef8 | aliguori | } |
926 | 63a01ef8 | aliguori | |
927 | f13b572c | Jan Kiszka | static SlirpState *slirp_lookup(Monitor *mon, const char *vlan, |
928 | f13b572c | Jan Kiszka | const char *stack) |
929 | f13b572c | Jan Kiszka | { |
930 | f13b572c | Jan Kiszka | VLANClientState *vc; |
931 | f13b572c | Jan Kiszka | |
932 | f13b572c | Jan Kiszka | if (vlan) {
|
933 | f13b572c | Jan Kiszka | vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack); |
934 | f13b572c | Jan Kiszka | if (!vc) {
|
935 | f13b572c | Jan Kiszka | return NULL; |
936 | f13b572c | Jan Kiszka | } |
937 | f13b572c | Jan Kiszka | if (strcmp(vc->model, "user")) { |
938 | f13b572c | Jan Kiszka | monitor_printf(mon, "invalid device specified\n");
|
939 | f13b572c | Jan Kiszka | return NULL; |
940 | f13b572c | Jan Kiszka | } |
941 | f13b572c | Jan Kiszka | return vc->opaque;
|
942 | f13b572c | Jan Kiszka | } else {
|
943 | 72cf2d4f | Blue Swirl | if (QTAILQ_EMPTY(&slirp_stacks)) {
|
944 | f13b572c | Jan Kiszka | monitor_printf(mon, "user mode network stack not in use\n");
|
945 | f13b572c | Jan Kiszka | return NULL; |
946 | f13b572c | Jan Kiszka | } |
947 | 72cf2d4f | Blue Swirl | return QTAILQ_FIRST(&slirp_stacks);
|
948 | f13b572c | Jan Kiszka | } |
949 | f13b572c | Jan Kiszka | } |
950 | f13b572c | Jan Kiszka | |
951 | 1d4daa91 | Luiz Capitulino | void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict) |
952 | c1261d8d | Alexander Graf | { |
953 | 3c6a0580 | Jan Kiszka | struct in_addr host_addr = { .s_addr = INADDR_ANY };
|
954 | c1261d8d | Alexander Graf | int host_port;
|
955 | c1261d8d | Alexander Graf | char buf[256] = ""; |
956 | f13b572c | Jan Kiszka | const char *src_str, *p; |
957 | f13b572c | Jan Kiszka | SlirpState *s; |
958 | c1261d8d | Alexander Graf | int is_udp = 0; |
959 | 9c12a6f2 | Jan Kiszka | int err;
|
960 | 1d4daa91 | Luiz Capitulino | const char *arg1 = qdict_get_str(qdict, "arg1"); |
961 | 1d4daa91 | Luiz Capitulino | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
962 | 1d4daa91 | Luiz Capitulino | const char *arg3 = qdict_get_try_str(qdict, "arg3"); |
963 | c1261d8d | Alexander Graf | |
964 | f13b572c | Jan Kiszka | if (arg2) {
|
965 | f13b572c | Jan Kiszka | s = slirp_lookup(mon, arg1, arg2); |
966 | f13b572c | Jan Kiszka | src_str = arg3; |
967 | f13b572c | Jan Kiszka | } else {
|
968 | f13b572c | Jan Kiszka | s = slirp_lookup(mon, NULL, NULL); |
969 | f13b572c | Jan Kiszka | src_str = arg1; |
970 | f13b572c | Jan Kiszka | } |
971 | f13b572c | Jan Kiszka | if (!s) {
|
972 | c1261d8d | Alexander Graf | return;
|
973 | f3546deb | Jan Kiszka | } |
974 | c1261d8d | Alexander Graf | |
975 | 3c6a0580 | Jan Kiszka | if (!src_str || !src_str[0]) |
976 | c1261d8d | Alexander Graf | goto fail_syntax;
|
977 | c1261d8d | Alexander Graf | |
978 | f13b572c | Jan Kiszka | p = src_str; |
979 | c1261d8d | Alexander Graf | get_str_sep(buf, sizeof(buf), &p, ':'); |
980 | c1261d8d | Alexander Graf | |
981 | c1261d8d | Alexander Graf | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
982 | c1261d8d | Alexander Graf | is_udp = 0;
|
983 | c1261d8d | Alexander Graf | } else if (!strcmp(buf, "udp")) { |
984 | c1261d8d | Alexander Graf | is_udp = 1;
|
985 | c1261d8d | Alexander Graf | } else {
|
986 | c1261d8d | Alexander Graf | goto fail_syntax;
|
987 | c1261d8d | Alexander Graf | } |
988 | c1261d8d | Alexander Graf | |
989 | 3c6a0580 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
990 | 3c6a0580 | Jan Kiszka | goto fail_syntax;
|
991 | 3c6a0580 | Jan Kiszka | } |
992 | 3c6a0580 | Jan Kiszka | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { |
993 | 3c6a0580 | Jan Kiszka | goto fail_syntax;
|
994 | 3c6a0580 | Jan Kiszka | } |
995 | 3c6a0580 | Jan Kiszka | |
996 | c1261d8d | Alexander Graf | host_port = atoi(p); |
997 | c1261d8d | Alexander Graf | |
998 | 72cf2d4f | Blue Swirl | err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp, |
999 | 9f8bd042 | Jan Kiszka | host_addr, host_port); |
1000 | c1261d8d | Alexander Graf | |
1001 | 9c12a6f2 | Jan Kiszka | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
|
1002 | 9c12a6f2 | Jan Kiszka | err ? "removed" : "not found"); |
1003 | c1261d8d | Alexander Graf | return;
|
1004 | c1261d8d | Alexander Graf | |
1005 | c1261d8d | Alexander Graf | fail_syntax:
|
1006 | c1261d8d | Alexander Graf | monitor_printf(mon, "invalid format\n");
|
1007 | c1261d8d | Alexander Graf | } |
1008 | c1261d8d | Alexander Graf | |
1009 | fb12577c | Markus Armbruster | static int slirp_hostfwd(SlirpState *s, const char *redir_str, |
1010 | 0752706d | Markus Armbruster | int legacy_format)
|
1011 | 63a01ef8 | aliguori | { |
1012 | 3c6a0580 | Jan Kiszka | struct in_addr host_addr = { .s_addr = INADDR_ANY };
|
1013 | c92ef6a2 | Jan Kiszka | struct in_addr guest_addr = { .s_addr = 0 }; |
1014 | 63a01ef8 | aliguori | int host_port, guest_port;
|
1015 | b8e8af38 | Jan Kiszka | const char *p; |
1016 | c92ef6a2 | Jan Kiszka | char buf[256]; |
1017 | b8e8af38 | Jan Kiszka | int is_udp;
|
1018 | c92ef6a2 | Jan Kiszka | char *end;
|
1019 | 1c6ed9f3 | Alexander Graf | |
1020 | 63a01ef8 | aliguori | p = redir_str; |
1021 | f13b572c | Jan Kiszka | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
1022 | d4ebe193 | aliguori | goto fail_syntax;
|
1023 | b8e8af38 | Jan Kiszka | } |
1024 | d4ebe193 | aliguori | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
1025 | 63a01ef8 | aliguori | is_udp = 0;
|
1026 | 63a01ef8 | aliguori | } else if (!strcmp(buf, "udp")) { |
1027 | 63a01ef8 | aliguori | is_udp = 1;
|
1028 | 63a01ef8 | aliguori | } else {
|
1029 | d4ebe193 | aliguori | goto fail_syntax;
|
1030 | 63a01ef8 | aliguori | } |
1031 | 63a01ef8 | aliguori | |
1032 | 3c6a0580 | Jan Kiszka | if (!legacy_format) {
|
1033 | 3c6a0580 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
1034 | 3c6a0580 | Jan Kiszka | goto fail_syntax;
|
1035 | 3c6a0580 | Jan Kiszka | } |
1036 | 3c6a0580 | Jan Kiszka | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { |
1037 | 3c6a0580 | Jan Kiszka | goto fail_syntax;
|
1038 | 3c6a0580 | Jan Kiszka | } |
1039 | 3c6a0580 | Jan Kiszka | } |
1040 | 3c6a0580 | Jan Kiszka | |
1041 | 3c6a0580 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) { |
1042 | d4ebe193 | aliguori | goto fail_syntax;
|
1043 | b8e8af38 | Jan Kiszka | } |
1044 | c92ef6a2 | Jan Kiszka | host_port = strtol(buf, &end, 0);
|
1045 | c92ef6a2 | Jan Kiszka | if (*end != '\0' || host_port < 1 || host_port > 65535) { |
1046 | d4ebe193 | aliguori | goto fail_syntax;
|
1047 | b8e8af38 | Jan Kiszka | } |
1048 | 63a01ef8 | aliguori | |
1049 | b8e8af38 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
1050 | d4ebe193 | aliguori | goto fail_syntax;
|
1051 | b8e8af38 | Jan Kiszka | } |
1052 | c92ef6a2 | Jan Kiszka | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { |
1053 | d4ebe193 | aliguori | goto fail_syntax;
|
1054 | b8e8af38 | Jan Kiszka | } |
1055 | 63a01ef8 | aliguori | |
1056 | c92ef6a2 | Jan Kiszka | guest_port = strtol(p, &end, 0);
|
1057 | c92ef6a2 | Jan Kiszka | if (*end != '\0' || guest_port < 1 || guest_port > 65535) { |
1058 | d4ebe193 | aliguori | goto fail_syntax;
|
1059 | b8e8af38 | Jan Kiszka | } |
1060 | 63a01ef8 | aliguori | |
1061 | 9f8bd042 | Jan Kiszka | if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
|
1062 | 9f8bd042 | Jan Kiszka | guest_port) < 0) {
|
1063 | fb12577c | Markus Armbruster | qemu_error("could not set up host forwarding rule '%s'\n",
|
1064 | fb12577c | Markus Armbruster | redir_str); |
1065 | 0752706d | Markus Armbruster | return -1; |
1066 | 63a01ef8 | aliguori | } |
1067 | 0752706d | Markus Armbruster | return 0; |
1068 | d4ebe193 | aliguori | |
1069 | d4ebe193 | aliguori | fail_syntax:
|
1070 | fb12577c | Markus Armbruster | qemu_error("invalid host forwarding rule '%s'\n", redir_str);
|
1071 | 0752706d | Markus Armbruster | return -1; |
1072 | 63a01ef8 | aliguori | } |
1073 | 63a01ef8 | aliguori | |
1074 | 1d4daa91 | Luiz Capitulino | void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict) |
1075 | b8e8af38 | Jan Kiszka | { |
1076 | f13b572c | Jan Kiszka | const char *redir_str; |
1077 | f13b572c | Jan Kiszka | SlirpState *s; |
1078 | 1d4daa91 | Luiz Capitulino | const char *arg1 = qdict_get_str(qdict, "arg1"); |
1079 | 1d4daa91 | Luiz Capitulino | const char *arg2 = qdict_get_try_str(qdict, "arg2"); |
1080 | 1d4daa91 | Luiz Capitulino | const char *arg3 = qdict_get_try_str(qdict, "arg3"); |
1081 | f13b572c | Jan Kiszka | |
1082 | f13b572c | Jan Kiszka | if (arg2) {
|
1083 | f13b572c | Jan Kiszka | s = slirp_lookup(mon, arg1, arg2); |
1084 | f13b572c | Jan Kiszka | redir_str = arg3; |
1085 | f13b572c | Jan Kiszka | } else {
|
1086 | f13b572c | Jan Kiszka | s = slirp_lookup(mon, NULL, NULL); |
1087 | f13b572c | Jan Kiszka | redir_str = arg1; |
1088 | f13b572c | Jan Kiszka | } |
1089 | f13b572c | Jan Kiszka | if (s) {
|
1090 | fb12577c | Markus Armbruster | slirp_hostfwd(s, redir_str, 0);
|
1091 | b8e8af38 | Jan Kiszka | } |
1092 | b8e8af38 | Jan Kiszka | |
1093 | f3546deb | Jan Kiszka | } |
1094 | f3546deb | Jan Kiszka | |
1095 | 0752706d | Markus Armbruster | int net_slirp_redir(const char *redir_str) |
1096 | f3546deb | Jan Kiszka | { |
1097 | f3546deb | Jan Kiszka | struct slirp_config_str *config;
|
1098 | f3546deb | Jan Kiszka | |
1099 | 72cf2d4f | Blue Swirl | if (QTAILQ_EMPTY(&slirp_stacks)) {
|
1100 | f3546deb | Jan Kiszka | config = qemu_malloc(sizeof(*config));
|
1101 | f3546deb | Jan Kiszka | pstrcpy(config->str, sizeof(config->str), redir_str);
|
1102 | 3c6a0580 | Jan Kiszka | config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY; |
1103 | f3546deb | Jan Kiszka | config->next = slirp_configs; |
1104 | f3546deb | Jan Kiszka | slirp_configs = config; |
1105 | 0752706d | Markus Armbruster | return 0; |
1106 | b8e8af38 | Jan Kiszka | } |
1107 | b8e8af38 | Jan Kiszka | |
1108 | fb12577c | Markus Armbruster | return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1); |
1109 | b8e8af38 | Jan Kiszka | } |
1110 | b8e8af38 | Jan Kiszka | |
1111 | 63a01ef8 | aliguori | #ifndef _WIN32
|
1112 | 63a01ef8 | aliguori | |
1113 | 63a01ef8 | aliguori | /* automatic user mode samba server configuration */
|
1114 | 28432466 | Jan Kiszka | static void slirp_smb_cleanup(SlirpState *s) |
1115 | 63a01ef8 | aliguori | { |
1116 | 28432466 | Jan Kiszka | char cmd[128]; |
1117 | 09c18925 | Jan Kiszka | |
1118 | 28432466 | Jan Kiszka | if (s->smb_dir[0] != '\0') { |
1119 | 28432466 | Jan Kiszka | snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir); |
1120 | 28432466 | Jan Kiszka | system(cmd); |
1121 | 28432466 | Jan Kiszka | s->smb_dir[0] = '\0'; |
1122 | 28432466 | Jan Kiszka | } |
1123 | 63a01ef8 | aliguori | } |
1124 | 63a01ef8 | aliguori | |
1125 | fb12577c | Markus Armbruster | static int slirp_smb(SlirpState* s, const char *exported_dir, |
1126 | 0752706d | Markus Armbruster | struct in_addr vserver_addr)
|
1127 | 63a01ef8 | aliguori | { |
1128 | 28432466 | Jan Kiszka | static int instance; |
1129 | 28432466 | Jan Kiszka | char smb_conf[128]; |
1130 | 28432466 | Jan Kiszka | char smb_cmdline[128]; |
1131 | 63a01ef8 | aliguori | FILE *f; |
1132 | 63a01ef8 | aliguori | |
1133 | 28432466 | Jan Kiszka | snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d", |
1134 | 28432466 | Jan Kiszka | (long)getpid(), instance++);
|
1135 | 28432466 | Jan Kiszka | if (mkdir(s->smb_dir, 0700) < 0) { |
1136 | fb12577c | Markus Armbruster | qemu_error("could not create samba server dir '%s'\n", s->smb_dir);
|
1137 | 0752706d | Markus Armbruster | return -1; |
1138 | 63a01ef8 | aliguori | } |
1139 | 28432466 | Jan Kiszka | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf"); |
1140 | 63a01ef8 | aliguori | |
1141 | 63a01ef8 | aliguori | f = fopen(smb_conf, "w");
|
1142 | 63a01ef8 | aliguori | if (!f) {
|
1143 | 28432466 | Jan Kiszka | slirp_smb_cleanup(s); |
1144 | fb12577c | Markus Armbruster | qemu_error("could not create samba server configuration file '%s'\n",
|
1145 | fb12577c | Markus Armbruster | smb_conf); |
1146 | 0752706d | Markus Armbruster | return -1; |
1147 | 63a01ef8 | aliguori | } |
1148 | 63a01ef8 | aliguori | fprintf(f, |
1149 | 63a01ef8 | aliguori | "[global]\n"
|
1150 | 63a01ef8 | aliguori | "private dir=%s\n"
|
1151 | 63a01ef8 | aliguori | "smb ports=0\n"
|
1152 | 63a01ef8 | aliguori | "socket address=127.0.0.1\n"
|
1153 | 63a01ef8 | aliguori | "pid directory=%s\n"
|
1154 | 63a01ef8 | aliguori | "lock directory=%s\n"
|
1155 | 63a01ef8 | aliguori | "log file=%s/log.smbd\n"
|
1156 | 63a01ef8 | aliguori | "smb passwd file=%s/smbpasswd\n"
|
1157 | 63a01ef8 | aliguori | "security = share\n"
|
1158 | 63a01ef8 | aliguori | "[qemu]\n"
|
1159 | 63a01ef8 | aliguori | "path=%s\n"
|
1160 | 63a01ef8 | aliguori | "read only=no\n"
|
1161 | 63a01ef8 | aliguori | "guest ok=yes\n",
|
1162 | 28432466 | Jan Kiszka | s->smb_dir, |
1163 | 28432466 | Jan Kiszka | s->smb_dir, |
1164 | 28432466 | Jan Kiszka | s->smb_dir, |
1165 | 28432466 | Jan Kiszka | s->smb_dir, |
1166 | 28432466 | Jan Kiszka | s->smb_dir, |
1167 | 63a01ef8 | aliguori | exported_dir |
1168 | 63a01ef8 | aliguori | ); |
1169 | 63a01ef8 | aliguori | fclose(f); |
1170 | 63a01ef8 | aliguori | |
1171 | 63a01ef8 | aliguori | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
1172 | 63a01ef8 | aliguori | SMBD_COMMAND, smb_conf); |
1173 | 63a01ef8 | aliguori | |
1174 | bb53fc53 | Jan Kiszka | if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) { |
1175 | 28432466 | Jan Kiszka | slirp_smb_cleanup(s); |
1176 | fb12577c | Markus Armbruster | qemu_error("conflicting/invalid smbserver address\n");
|
1177 | 0752706d | Markus Armbruster | return -1; |
1178 | c92ef6a2 | Jan Kiszka | } |
1179 | 0752706d | Markus Armbruster | return 0; |
1180 | 63a01ef8 | aliguori | } |
1181 | 63a01ef8 | aliguori | |
1182 | ad196a9d | Jan Kiszka | /* automatic user mode samba server configuration (legacy interface) */
|
1183 | 0752706d | Markus Armbruster | int net_slirp_smb(const char *exported_dir) |
1184 | b8e8af38 | Jan Kiszka | { |
1185 | c92ef6a2 | Jan Kiszka | struct in_addr vserver_addr = { .s_addr = 0 }; |
1186 | c92ef6a2 | Jan Kiszka | |
1187 | ad196a9d | Jan Kiszka | if (legacy_smb_export) {
|
1188 | b8e8af38 | Jan Kiszka | fprintf(stderr, "-smb given twice\n");
|
1189 | 0752706d | Markus Armbruster | return -1; |
1190 | b8e8af38 | Jan Kiszka | } |
1191 | ad196a9d | Jan Kiszka | legacy_smb_export = exported_dir; |
1192 | 72cf2d4f | Blue Swirl | if (!QTAILQ_EMPTY(&slirp_stacks)) {
|
1193 | fb12577c | Markus Armbruster | return slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir,
|
1194 | 0752706d | Markus Armbruster | vserver_addr); |
1195 | b8e8af38 | Jan Kiszka | } |
1196 | 0752706d | Markus Armbruster | return 0; |
1197 | b8e8af38 | Jan Kiszka | } |
1198 | b8e8af38 | Jan Kiszka | |
1199 | 63a01ef8 | aliguori | #endif /* !defined(_WIN32) */ |
1200 | b8e8af38 | Jan Kiszka | |
1201 | c92ef6a2 | Jan Kiszka | struct GuestFwd {
|
1202 | 8ca9217d | aliguori | CharDriverState *hd; |
1203 | c92ef6a2 | Jan Kiszka | struct in_addr server;
|
1204 | 8ca9217d | aliguori | int port;
|
1205 | 9f8bd042 | Jan Kiszka | Slirp *slirp; |
1206 | 511d2b14 | blueswir1 | }; |
1207 | 8ca9217d | aliguori | |
1208 | c92ef6a2 | Jan Kiszka | static int guestfwd_can_read(void *opaque) |
1209 | 8ca9217d | aliguori | { |
1210 | c92ef6a2 | Jan Kiszka | struct GuestFwd *fwd = opaque;
|
1211 | 9f8bd042 | Jan Kiszka | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
|
1212 | 8ca9217d | aliguori | } |
1213 | 8ca9217d | aliguori | |
1214 | c92ef6a2 | Jan Kiszka | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) |
1215 | 8ca9217d | aliguori | { |
1216 | c92ef6a2 | Jan Kiszka | struct GuestFwd *fwd = opaque;
|
1217 | 9f8bd042 | Jan Kiszka | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); |
1218 | 8ca9217d | aliguori | } |
1219 | 8ca9217d | aliguori | |
1220 | fb12577c | Markus Armbruster | static int slirp_guestfwd(SlirpState *s, const char *config_str, |
1221 | 0752706d | Markus Armbruster | int legacy_format)
|
1222 | ad196a9d | Jan Kiszka | { |
1223 | c92ef6a2 | Jan Kiszka | struct in_addr server = { .s_addr = 0 }; |
1224 | c92ef6a2 | Jan Kiszka | struct GuestFwd *fwd;
|
1225 | c92ef6a2 | Jan Kiszka | const char *p; |
1226 | c92ef6a2 | Jan Kiszka | char buf[128]; |
1227 | c92ef6a2 | Jan Kiszka | char *end;
|
1228 | ad196a9d | Jan Kiszka | int port;
|
1229 | ad196a9d | Jan Kiszka | |
1230 | c92ef6a2 | Jan Kiszka | p = config_str; |
1231 | c92ef6a2 | Jan Kiszka | if (legacy_format) {
|
1232 | c92ef6a2 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
1233 | c92ef6a2 | Jan Kiszka | goto fail_syntax;
|
1234 | c92ef6a2 | Jan Kiszka | } |
1235 | c92ef6a2 | Jan Kiszka | } else {
|
1236 | c92ef6a2 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
1237 | c92ef6a2 | Jan Kiszka | goto fail_syntax;
|
1238 | c92ef6a2 | Jan Kiszka | } |
1239 | c92ef6a2 | Jan Kiszka | if (strcmp(buf, "tcp") && buf[0] != '\0') { |
1240 | c92ef6a2 | Jan Kiszka | goto fail_syntax;
|
1241 | c92ef6a2 | Jan Kiszka | } |
1242 | c92ef6a2 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
1243 | c92ef6a2 | Jan Kiszka | goto fail_syntax;
|
1244 | c92ef6a2 | Jan Kiszka | } |
1245 | c92ef6a2 | Jan Kiszka | if (buf[0] != '\0' && !inet_aton(buf, &server)) { |
1246 | c92ef6a2 | Jan Kiszka | goto fail_syntax;
|
1247 | c92ef6a2 | Jan Kiszka | } |
1248 | c92ef6a2 | Jan Kiszka | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { |
1249 | c92ef6a2 | Jan Kiszka | goto fail_syntax;
|
1250 | c92ef6a2 | Jan Kiszka | } |
1251 | c92ef6a2 | Jan Kiszka | } |
1252 | c92ef6a2 | Jan Kiszka | port = strtol(buf, &end, 10);
|
1253 | c92ef6a2 | Jan Kiszka | if (*end != '\0' || port < 1 || port > 65535) { |
1254 | c92ef6a2 | Jan Kiszka | goto fail_syntax;
|
1255 | ad196a9d | Jan Kiszka | } |
1256 | ad196a9d | Jan Kiszka | |
1257 | c92ef6a2 | Jan Kiszka | fwd = qemu_malloc(sizeof(struct GuestFwd)); |
1258 | c92ef6a2 | Jan Kiszka | snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port); |
1259 | c92ef6a2 | Jan Kiszka | fwd->hd = qemu_chr_open(buf, p, NULL);
|
1260 | c92ef6a2 | Jan Kiszka | if (!fwd->hd) {
|
1261 | fb12577c | Markus Armbruster | qemu_error("could not open guest forwarding device '%s'\n", buf);
|
1262 | c92ef6a2 | Jan Kiszka | qemu_free(fwd); |
1263 | 0752706d | Markus Armbruster | return -1; |
1264 | ad196a9d | Jan Kiszka | } |
1265 | ad196a9d | Jan Kiszka | |
1266 | bb53fc53 | Jan Kiszka | if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) { |
1267 | fb12577c | Markus Armbruster | qemu_error("conflicting/invalid host:port in guest forwarding "
|
1268 | fb12577c | Markus Armbruster | "rule '%s'\n", config_str);
|
1269 | c92ef6a2 | Jan Kiszka | qemu_free(fwd); |
1270 | 0752706d | Markus Armbruster | return -1; |
1271 | c92ef6a2 | Jan Kiszka | } |
1272 | bb53fc53 | Jan Kiszka | fwd->server = server; |
1273 | bb53fc53 | Jan Kiszka | fwd->port = port; |
1274 | bb53fc53 | Jan Kiszka | fwd->slirp = s->slirp; |
1275 | bb53fc53 | Jan Kiszka | |
1276 | c92ef6a2 | Jan Kiszka | qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read, |
1277 | c92ef6a2 | Jan Kiszka | NULL, fwd);
|
1278 | 0752706d | Markus Armbruster | return 0; |
1279 | c92ef6a2 | Jan Kiszka | |
1280 | c92ef6a2 | Jan Kiszka | fail_syntax:
|
1281 | fb12577c | Markus Armbruster | qemu_error("invalid guest forwarding rule '%s'\n", config_str);
|
1282 | 0752706d | Markus Armbruster | return -1; |
1283 | ad196a9d | Jan Kiszka | } |
1284 | ad196a9d | Jan Kiszka | |
1285 | 6dbe553f | Jan Kiszka | void do_info_usernet(Monitor *mon)
|
1286 | 6dbe553f | Jan Kiszka | { |
1287 | b1c99fcd | Jan Kiszka | SlirpState *s; |
1288 | 9f8bd042 | Jan Kiszka | |
1289 | 72cf2d4f | Blue Swirl | QTAILQ_FOREACH(s, &slirp_stacks, entry) { |
1290 | b1c99fcd | Jan Kiszka | monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
|
1291 | b1c99fcd | Jan Kiszka | slirp_connection_info(s->slirp, mon); |
1292 | 9f8bd042 | Jan Kiszka | } |
1293 | 6dbe553f | Jan Kiszka | } |
1294 | 6dbe553f | Jan Kiszka | |
1295 | 63a01ef8 | aliguori | #endif /* CONFIG_SLIRP */ |
1296 | 63a01ef8 | aliguori | |
1297 | 24e32363 | Mark McLoughlin | #if defined(_WIN32)
|
1298 | 6c9f58ba | Sridhar Samudrala | int tap_has_ufo(VLANClientState *vc)
|
1299 | 6c9f58ba | Sridhar Samudrala | { |
1300 | 6c9f58ba | Sridhar Samudrala | return 0; |
1301 | 6c9f58ba | Sridhar Samudrala | } |
1302 | 24e32363 | Mark McLoughlin | int tap_has_vnet_hdr(VLANClientState *vc)
|
1303 | 24e32363 | Mark McLoughlin | { |
1304 | 24e32363 | Mark McLoughlin | return 0; |
1305 | 24e32363 | Mark McLoughlin | } |
1306 | 24e32363 | Mark McLoughlin | void tap_using_vnet_hdr(VLANClientState *vc, int using_vnet_hdr) |
1307 | 24e32363 | Mark McLoughlin | { |
1308 | 24e32363 | Mark McLoughlin | } |
1309 | 6c9f58ba | Sridhar Samudrala | void tap_set_offload(VLANClientState *vc, int csum, int tso4, |
1310 | 6c9f58ba | Sridhar Samudrala | int tso6, int ecn, int ufo) |
1311 | b1c28b46 | Mark McLoughlin | { |
1312 | b1c28b46 | Mark McLoughlin | } |
1313 | 24e32363 | Mark McLoughlin | #else /* !defined(_WIN32) */ |
1314 | 63a01ef8 | aliguori | |
1315 | 8e0f8e5b | Mark McLoughlin | /* Maximum GSO packet size (64k) plus plenty of room for
|
1316 | 8e0f8e5b | Mark McLoughlin | * the ethernet and virtio_net headers
|
1317 | 8e0f8e5b | Mark McLoughlin | */
|
1318 | 8e0f8e5b | Mark McLoughlin | #define TAP_BUFSIZE (4096 + 65536) |
1319 | 8e0f8e5b | Mark McLoughlin | |
1320 | 63a01ef8 | aliguori | typedef struct TAPState { |
1321 | 63a01ef8 | aliguori | VLANClientState *vc; |
1322 | 63a01ef8 | aliguori | int fd;
|
1323 | 63a01ef8 | aliguori | char down_script[1024]; |
1324 | 973cbd37 | aliguori | char down_script_arg[128]; |
1325 | 8e0f8e5b | Mark McLoughlin | uint8_t buf[TAP_BUFSIZE]; |
1326 | b664e367 | Mark McLoughlin | unsigned int read_poll : 1; |
1327 | 1f7babf6 | Mark McLoughlin | unsigned int write_poll : 1; |
1328 | 8e0f8e5b | Mark McLoughlin | unsigned int has_vnet_hdr : 1; |
1329 | 24e32363 | Mark McLoughlin | unsigned int using_vnet_hdr : 1; |
1330 | 6c9f58ba | Sridhar Samudrala | unsigned int has_ufo: 1; |
1331 | 63a01ef8 | aliguori | } TAPState; |
1332 | 63a01ef8 | aliguori | |
1333 | b946a153 | aliguori | static int launch_script(const char *setup_script, const char *ifname, int fd); |
1334 | b946a153 | aliguori | |
1335 | b664e367 | Mark McLoughlin | static int tap_can_send(void *opaque); |
1336 | b664e367 | Mark McLoughlin | static void tap_send(void *opaque); |
1337 | 1f7babf6 | Mark McLoughlin | static void tap_writable(void *opaque); |
1338 | b664e367 | Mark McLoughlin | |
1339 | b664e367 | Mark McLoughlin | static void tap_update_fd_handler(TAPState *s) |
1340 | b664e367 | Mark McLoughlin | { |
1341 | b664e367 | Mark McLoughlin | qemu_set_fd_handler2(s->fd, |
1342 | b664e367 | Mark McLoughlin | s->read_poll ? tap_can_send : NULL,
|
1343 | b664e367 | Mark McLoughlin | s->read_poll ? tap_send : NULL,
|
1344 | 1f7babf6 | Mark McLoughlin | s->write_poll ? tap_writable : NULL,
|
1345 | b664e367 | Mark McLoughlin | s); |
1346 | b664e367 | Mark McLoughlin | } |
1347 | b664e367 | Mark McLoughlin | |
1348 | b664e367 | Mark McLoughlin | static void tap_read_poll(TAPState *s, int enable) |
1349 | b664e367 | Mark McLoughlin | { |
1350 | b664e367 | Mark McLoughlin | s->read_poll = !!enable; |
1351 | b664e367 | Mark McLoughlin | tap_update_fd_handler(s); |
1352 | b664e367 | Mark McLoughlin | } |
1353 | b664e367 | Mark McLoughlin | |
1354 | 1f7babf6 | Mark McLoughlin | static void tap_write_poll(TAPState *s, int enable) |
1355 | 1f7babf6 | Mark McLoughlin | { |
1356 | 1f7babf6 | Mark McLoughlin | s->write_poll = !!enable; |
1357 | 1f7babf6 | Mark McLoughlin | tap_update_fd_handler(s); |
1358 | 1f7babf6 | Mark McLoughlin | } |
1359 | 1f7babf6 | Mark McLoughlin | |
1360 | 1f7babf6 | Mark McLoughlin | static void tap_writable(void *opaque) |
1361 | 1f7babf6 | Mark McLoughlin | { |
1362 | 1f7babf6 | Mark McLoughlin | TAPState *s = opaque; |
1363 | 1f7babf6 | Mark McLoughlin | |
1364 | 1f7babf6 | Mark McLoughlin | tap_write_poll(s, 0);
|
1365 | 1f7babf6 | Mark McLoughlin | |
1366 | 1f7babf6 | Mark McLoughlin | qemu_flush_queued_packets(s->vc); |
1367 | 1f7babf6 | Mark McLoughlin | } |
1368 | 1f7babf6 | Mark McLoughlin | |
1369 | 38c75be3 | Mark McLoughlin | static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt) |
1370 | b535b7b2 | aliguori | { |
1371 | b535b7b2 | aliguori | ssize_t len; |
1372 | b535b7b2 | aliguori | |
1373 | b535b7b2 | aliguori | do {
|
1374 | b535b7b2 | aliguori | len = writev(s->fd, iov, iovcnt); |
1375 | 1f7babf6 | Mark McLoughlin | } while (len == -1 && errno == EINTR); |
1376 | 1f7babf6 | Mark McLoughlin | |
1377 | 1f7babf6 | Mark McLoughlin | if (len == -1 && errno == EAGAIN) { |
1378 | 1f7babf6 | Mark McLoughlin | tap_write_poll(s, 1);
|
1379 | 1f7babf6 | Mark McLoughlin | return 0; |
1380 | 1f7babf6 | Mark McLoughlin | } |
1381 | b535b7b2 | aliguori | |
1382 | b535b7b2 | aliguori | return len;
|
1383 | b535b7b2 | aliguori | } |
1384 | b535b7b2 | aliguori | |
1385 | 38c75be3 | Mark McLoughlin | static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov, |
1386 | 38c75be3 | Mark McLoughlin | int iovcnt)
|
1387 | 38c75be3 | Mark McLoughlin | { |
1388 | 38c75be3 | Mark McLoughlin | TAPState *s = vc->opaque; |
1389 | 8e0f8e5b | Mark McLoughlin | const struct iovec *iovp = iov; |
1390 | 8e0f8e5b | Mark McLoughlin | struct iovec iov_copy[iovcnt + 1]; |
1391 | 8e0f8e5b | Mark McLoughlin | struct virtio_net_hdr hdr = { 0, }; |
1392 | 38c75be3 | Mark McLoughlin | |
1393 | 24e32363 | Mark McLoughlin | if (s->has_vnet_hdr && !s->using_vnet_hdr) {
|
1394 | 8e0f8e5b | Mark McLoughlin | iov_copy[0].iov_base = &hdr;
|
1395 | 8e0f8e5b | Mark McLoughlin | iov_copy[0].iov_len = sizeof(hdr); |
1396 | 8e0f8e5b | Mark McLoughlin | memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov)); |
1397 | 8e0f8e5b | Mark McLoughlin | iovp = iov_copy; |
1398 | 8e0f8e5b | Mark McLoughlin | iovcnt++; |
1399 | 8e0f8e5b | Mark McLoughlin | } |
1400 | 8e0f8e5b | Mark McLoughlin | |
1401 | 8e0f8e5b | Mark McLoughlin | return tap_write_packet(s, iovp, iovcnt);
|
1402 | 38c75be3 | Mark McLoughlin | } |
1403 | 38c75be3 | Mark McLoughlin | |
1404 | 2596774a | Mark McLoughlin | static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size) |
1405 | 63a01ef8 | aliguori | { |
1406 | e3f5ec2b | Mark McLoughlin | TAPState *s = vc->opaque; |
1407 | 8e0f8e5b | Mark McLoughlin | struct iovec iov[2]; |
1408 | 38c75be3 | Mark McLoughlin | int iovcnt = 0; |
1409 | 8e0f8e5b | Mark McLoughlin | struct virtio_net_hdr hdr = { 0, }; |
1410 | 8e0f8e5b | Mark McLoughlin | |
1411 | 2596774a | Mark McLoughlin | if (s->has_vnet_hdr) {
|
1412 | 8e0f8e5b | Mark McLoughlin | iov[iovcnt].iov_base = &hdr; |
1413 | 8e0f8e5b | Mark McLoughlin | iov[iovcnt].iov_len = sizeof(hdr);
|
1414 | 8e0f8e5b | Mark McLoughlin | iovcnt++; |
1415 | 8e0f8e5b | Mark McLoughlin | } |
1416 | 4f1c942b | Mark McLoughlin | |
1417 | 38c75be3 | Mark McLoughlin | iov[iovcnt].iov_base = (char *)buf;
|
1418 | 38c75be3 | Mark McLoughlin | iov[iovcnt].iov_len = size; |
1419 | 38c75be3 | Mark McLoughlin | iovcnt++; |
1420 | 4f1c942b | Mark McLoughlin | |
1421 | 38c75be3 | Mark McLoughlin | return tap_write_packet(s, iov, iovcnt);
|
1422 | 63a01ef8 | aliguori | } |
1423 | 63a01ef8 | aliguori | |
1424 | 2596774a | Mark McLoughlin | static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
1425 | 2596774a | Mark McLoughlin | { |
1426 | 2596774a | Mark McLoughlin | TAPState *s = vc->opaque; |
1427 | 2596774a | Mark McLoughlin | struct iovec iov[1]; |
1428 | 2596774a | Mark McLoughlin | |
1429 | 2596774a | Mark McLoughlin | if (s->has_vnet_hdr && !s->using_vnet_hdr) {
|
1430 | 2596774a | Mark McLoughlin | return tap_receive_raw(vc, buf, size);
|
1431 | 2596774a | Mark McLoughlin | } |
1432 | 2596774a | Mark McLoughlin | |
1433 | 2596774a | Mark McLoughlin | iov[0].iov_base = (char *)buf; |
1434 | 2596774a | Mark McLoughlin | iov[0].iov_len = size;
|
1435 | 2596774a | Mark McLoughlin | |
1436 | 2596774a | Mark McLoughlin | return tap_write_packet(s, iov, 1); |
1437 | 2596774a | Mark McLoughlin | } |
1438 | 2596774a | Mark McLoughlin | |
1439 | 3471b757 | Mark McLoughlin | static int tap_can_send(void *opaque) |
1440 | 3471b757 | Mark McLoughlin | { |
1441 | 3471b757 | Mark McLoughlin | TAPState *s = opaque; |
1442 | 3471b757 | Mark McLoughlin | |
1443 | 3471b757 | Mark McLoughlin | return qemu_can_send_packet(s->vc);
|
1444 | 3471b757 | Mark McLoughlin | } |
1445 | 3471b757 | Mark McLoughlin | |
1446 | 63a01ef8 | aliguori | #ifdef __sun__
|
1447 | 5a6d8815 | Mark McLoughlin | static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
1448 | 5a6d8815 | Mark McLoughlin | { |
1449 | 63a01ef8 | aliguori | struct strbuf sbuf;
|
1450 | 63a01ef8 | aliguori | int f = 0; |
1451 | 5a6d8815 | Mark McLoughlin | |
1452 | 5a6d8815 | Mark McLoughlin | sbuf.maxlen = maxlen; |
1453 | 3f4cb3d3 | blueswir1 | sbuf.buf = (char *)buf;
|
1454 | 5a6d8815 | Mark McLoughlin | |
1455 | 5a6d8815 | Mark McLoughlin | return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1; |
1456 | 5a6d8815 | Mark McLoughlin | } |
1457 | 63a01ef8 | aliguori | #else
|
1458 | 5a6d8815 | Mark McLoughlin | static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
1459 | 5a6d8815 | Mark McLoughlin | { |
1460 | 5a6d8815 | Mark McLoughlin | return read(tapfd, buf, maxlen);
|
1461 | 5a6d8815 | Mark McLoughlin | } |
1462 | 63a01ef8 | aliguori | #endif
|
1463 | 5a6d8815 | Mark McLoughlin | |
1464 | 783527a9 | Mark McLoughlin | static void tap_send_completed(VLANClientState *vc, ssize_t len) |
1465 | e19eb224 | Mark McLoughlin | { |
1466 | e19eb224 | Mark McLoughlin | TAPState *s = vc->opaque; |
1467 | b664e367 | Mark McLoughlin | tap_read_poll(s, 1);
|
1468 | e19eb224 | Mark McLoughlin | } |
1469 | e19eb224 | Mark McLoughlin | |
1470 | 5a6d8815 | Mark McLoughlin | static void tap_send(void *opaque) |
1471 | 5a6d8815 | Mark McLoughlin | { |
1472 | 5a6d8815 | Mark McLoughlin | TAPState *s = opaque; |
1473 | 5a6d8815 | Mark McLoughlin | int size;
|
1474 | 5a6d8815 | Mark McLoughlin | |
1475 | e19eb224 | Mark McLoughlin | do {
|
1476 | 8e0f8e5b | Mark McLoughlin | uint8_t *buf = s->buf; |
1477 | 8e0f8e5b | Mark McLoughlin | |
1478 | e19eb224 | Mark McLoughlin | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
|
1479 | e19eb224 | Mark McLoughlin | if (size <= 0) { |
1480 | e19eb224 | Mark McLoughlin | break;
|
1481 | e19eb224 | Mark McLoughlin | } |
1482 | e19eb224 | Mark McLoughlin | |
1483 | 24e32363 | Mark McLoughlin | if (s->has_vnet_hdr && !s->using_vnet_hdr) {
|
1484 | 8e0f8e5b | Mark McLoughlin | buf += sizeof(struct virtio_net_hdr); |
1485 | 8e0f8e5b | Mark McLoughlin | size -= sizeof(struct virtio_net_hdr); |
1486 | 8e0f8e5b | Mark McLoughlin | } |
1487 | 8e0f8e5b | Mark McLoughlin | |
1488 | 8e0f8e5b | Mark McLoughlin | size = qemu_send_packet_async(s->vc, buf, size, tap_send_completed); |
1489 | e19eb224 | Mark McLoughlin | if (size == 0) { |
1490 | b664e367 | Mark McLoughlin | tap_read_poll(s, 0);
|
1491 | e19eb224 | Mark McLoughlin | } |
1492 | e19eb224 | Mark McLoughlin | } while (size > 0); |
1493 | 63a01ef8 | aliguori | } |
1494 | 63a01ef8 | aliguori | |
1495 | fc5b81d1 | Mark McLoughlin | /* sndbuf should be set to a value lower than the tx queue
|
1496 | fc5b81d1 | Mark McLoughlin | * capacity of any destination network interface.
|
1497 | fc5b81d1 | Mark McLoughlin | * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
|
1498 | fc5b81d1 | Mark McLoughlin | * a good default, given a 1500 byte MTU.
|
1499 | fc5b81d1 | Mark McLoughlin | */
|
1500 | fc5b81d1 | Mark McLoughlin | #define TAP_DEFAULT_SNDBUF 1024*1024 |
1501 | fc5b81d1 | Mark McLoughlin | |
1502 | 8a1c5235 | Mark McLoughlin | static int tap_set_sndbuf(TAPState *s, QemuOpts *opts) |
1503 | fc5b81d1 | Mark McLoughlin | { |
1504 | 8a1c5235 | Mark McLoughlin | int sndbuf;
|
1505 | fc5b81d1 | Mark McLoughlin | |
1506 | 8a1c5235 | Mark McLoughlin | sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF);
|
1507 | fc5b81d1 | Mark McLoughlin | if (!sndbuf) {
|
1508 | fc5b81d1 | Mark McLoughlin | sndbuf = INT_MAX; |
1509 | fc5b81d1 | Mark McLoughlin | } |
1510 | fc5b81d1 | Mark McLoughlin | |
1511 | 8a1c5235 | Mark McLoughlin | if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) { |
1512 | fb12577c | Markus Armbruster | qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno));
|
1513 | 0752706d | Markus Armbruster | return -1; |
1514 | 0df0ff6d | Mark McLoughlin | } |
1515 | 0752706d | Markus Armbruster | return 0; |
1516 | fc5b81d1 | Mark McLoughlin | } |
1517 | 0df0ff6d | Mark McLoughlin | |
1518 | 6c9f58ba | Sridhar Samudrala | int tap_has_ufo(VLANClientState *vc)
|
1519 | 6c9f58ba | Sridhar Samudrala | { |
1520 | 6c9f58ba | Sridhar Samudrala | TAPState *s = vc->opaque; |
1521 | 6c9f58ba | Sridhar Samudrala | |
1522 | 6c9f58ba | Sridhar Samudrala | assert(vc->type == NET_CLIENT_TYPE_TAP); |
1523 | 6c9f58ba | Sridhar Samudrala | |
1524 | 6c9f58ba | Sridhar Samudrala | return s->has_ufo;
|
1525 | 6c9f58ba | Sridhar Samudrala | } |
1526 | 6c9f58ba | Sridhar Samudrala | |
1527 | 24e32363 | Mark McLoughlin | int tap_has_vnet_hdr(VLANClientState *vc)
|
1528 | 24e32363 | Mark McLoughlin | { |
1529 | 24e32363 | Mark McLoughlin | TAPState *s = vc->opaque; |
1530 | 24e32363 | Mark McLoughlin | |
1531 | 24e32363 | Mark McLoughlin | assert(vc->type == NET_CLIENT_TYPE_TAP); |
1532 | 24e32363 | Mark McLoughlin | |
1533 | 24e32363 | Mark McLoughlin | return s->has_vnet_hdr;
|
1534 | 24e32363 | Mark McLoughlin | } |
1535 | 24e32363 | Mark McLoughlin | |
1536 | 24e32363 | Mark McLoughlin | void tap_using_vnet_hdr(VLANClientState *vc, int using_vnet_hdr) |
1537 | 24e32363 | Mark McLoughlin | { |
1538 | 24e32363 | Mark McLoughlin | TAPState *s = vc->opaque; |
1539 | 24e32363 | Mark McLoughlin | |
1540 | 24e32363 | Mark McLoughlin | using_vnet_hdr = using_vnet_hdr != 0;
|
1541 | 24e32363 | Mark McLoughlin | |
1542 | 24e32363 | Mark McLoughlin | assert(vc->type == NET_CLIENT_TYPE_TAP); |
1543 | 24e32363 | Mark McLoughlin | assert(s->has_vnet_hdr == using_vnet_hdr); |
1544 | 24e32363 | Mark McLoughlin | |
1545 | 24e32363 | Mark McLoughlin | s->using_vnet_hdr = using_vnet_hdr; |
1546 | 24e32363 | Mark McLoughlin | } |
1547 | 24e32363 | Mark McLoughlin | |
1548 | 8e0f8e5b | Mark McLoughlin | static int tap_probe_vnet_hdr(int fd) |
1549 | 8e0f8e5b | Mark McLoughlin | { |
1550 | 8e0f8e5b | Mark McLoughlin | struct ifreq ifr;
|
1551 | 8e0f8e5b | Mark McLoughlin | |
1552 | 8e0f8e5b | Mark McLoughlin | if (ioctl(fd, TUNGETIFF, &ifr) != 0) { |
1553 | 8e0f8e5b | Mark McLoughlin | qemu_error("TUNGETIFF ioctl() failed: %s\n", strerror(errno));
|
1554 | 8e0f8e5b | Mark McLoughlin | return 0; |
1555 | 8e0f8e5b | Mark McLoughlin | } |
1556 | 8e0f8e5b | Mark McLoughlin | |
1557 | 8e0f8e5b | Mark McLoughlin | return ifr.ifr_flags & IFF_VNET_HDR;
|
1558 | 8e0f8e5b | Mark McLoughlin | } |
1559 | 8e0f8e5b | Mark McLoughlin | |
1560 | 6c9f58ba | Sridhar Samudrala | void tap_set_offload(VLANClientState *vc, int csum, int tso4, |
1561 | 6c9f58ba | Sridhar Samudrala | int tso6, int ecn, int ufo) |
1562 | b1c28b46 | Mark McLoughlin | { |
1563 | b1c28b46 | Mark McLoughlin | TAPState *s = vc->opaque; |
1564 | b1c28b46 | Mark McLoughlin | unsigned int offload = 0; |
1565 | b1c28b46 | Mark McLoughlin | |
1566 | b1c28b46 | Mark McLoughlin | if (csum) {
|
1567 | b1c28b46 | Mark McLoughlin | offload |= TUN_F_CSUM; |
1568 | b1c28b46 | Mark McLoughlin | if (tso4)
|
1569 | b1c28b46 | Mark McLoughlin | offload |= TUN_F_TSO4; |
1570 | b1c28b46 | Mark McLoughlin | if (tso6)
|
1571 | b1c28b46 | Mark McLoughlin | offload |= TUN_F_TSO6; |
1572 | b1c28b46 | Mark McLoughlin | if ((tso4 || tso6) && ecn)
|
1573 | b1c28b46 | Mark McLoughlin | offload |= TUN_F_TSO_ECN; |
1574 | 6c9f58ba | Sridhar Samudrala | if (ufo)
|
1575 | 6c9f58ba | Sridhar Samudrala | offload |= TUN_F_UFO; |
1576 | b1c28b46 | Mark McLoughlin | } |
1577 | b1c28b46 | Mark McLoughlin | |
1578 | b1c28b46 | Mark McLoughlin | if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0) { |
1579 | 6c9f58ba | Sridhar Samudrala | offload &= ~TUN_F_UFO; |
1580 | 6c9f58ba | Sridhar Samudrala | if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0) { |
1581 | 6c9f58ba | Sridhar Samudrala | fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
|
1582 | 6c9f58ba | Sridhar Samudrala | strerror(errno)); |
1583 | 6c9f58ba | Sridhar Samudrala | } |
1584 | b1c28b46 | Mark McLoughlin | } |
1585 | b1c28b46 | Mark McLoughlin | } |
1586 | b1c28b46 | Mark McLoughlin | |
1587 | b946a153 | aliguori | static void tap_cleanup(VLANClientState *vc) |
1588 | b946a153 | aliguori | { |
1589 | b946a153 | aliguori | TAPState *s = vc->opaque; |
1590 | b946a153 | aliguori | |
1591 | b9adce2c | Mark McLoughlin | qemu_purge_queued_packets(vc); |
1592 | b9adce2c | Mark McLoughlin | |
1593 | b946a153 | aliguori | if (s->down_script[0]) |
1594 | b946a153 | aliguori | launch_script(s->down_script, s->down_script_arg, s->fd); |
1595 | b946a153 | aliguori | |
1596 | b664e367 | Mark McLoughlin | tap_read_poll(s, 0);
|
1597 | 1f7babf6 | Mark McLoughlin | tap_write_poll(s, 0);
|
1598 | b946a153 | aliguori | close(s->fd); |
1599 | b946a153 | aliguori | qemu_free(s); |
1600 | b946a153 | aliguori | } |
1601 | b946a153 | aliguori | |
1602 | 63a01ef8 | aliguori | /* fd support */
|
1603 | 63a01ef8 | aliguori | |
1604 | 7a9f6e4a | aliguori | static TAPState *net_tap_fd_init(VLANState *vlan,
|
1605 | 7a9f6e4a | aliguori | const char *model, |
1606 | 7a9f6e4a | aliguori | const char *name, |
1607 | 8e0f8e5b | Mark McLoughlin | int fd,
|
1608 | 8e0f8e5b | Mark McLoughlin | int vnet_hdr)
|
1609 | 63a01ef8 | aliguori | { |
1610 | 63a01ef8 | aliguori | TAPState *s; |
1611 | 6c9f58ba | Sridhar Samudrala | unsigned int offload; |
1612 | 63a01ef8 | aliguori | |
1613 | 63a01ef8 | aliguori | s = qemu_mallocz(sizeof(TAPState));
|
1614 | 63a01ef8 | aliguori | s->fd = fd; |
1615 | 8e0f8e5b | Mark McLoughlin | s->has_vnet_hdr = vnet_hdr != 0;
|
1616 | 24e32363 | Mark McLoughlin | s->using_vnet_hdr = 0;
|
1617 | bb6e6364 | Mark McLoughlin | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_TAP, |
1618 | bb6e6364 | Mark McLoughlin | vlan, NULL, model, name, NULL, |
1619 | 2596774a | Mark McLoughlin | tap_receive, tap_receive_raw, |
1620 | 2596774a | Mark McLoughlin | tap_receive_iov, tap_cleanup, s); |
1621 | 6c9f58ba | Sridhar Samudrala | s->has_ufo = 0;
|
1622 | 6c9f58ba | Sridhar Samudrala | /* Check if tap supports UFO */
|
1623 | 6c9f58ba | Sridhar Samudrala | offload = TUN_F_CSUM | TUN_F_UFO; |
1624 | 6c9f58ba | Sridhar Samudrala | if (ioctl(s->fd, TUNSETOFFLOAD, offload) == 0) |
1625 | 6c9f58ba | Sridhar Samudrala | s->has_ufo = 1;
|
1626 | 6c9f58ba | Sridhar Samudrala | tap_set_offload(s->vc, 0, 0, 0, 0, 0); |
1627 | b664e367 | Mark McLoughlin | tap_read_poll(s, 1);
|
1628 | 63a01ef8 | aliguori | return s;
|
1629 | 63a01ef8 | aliguori | } |
1630 | 63a01ef8 | aliguori | |
1631 | 71e72a19 | Juan Quintela | #if defined (CONFIG_BSD) || defined (__FreeBSD_kernel__)
|
1632 | baf74c95 | Mark McLoughlin | static int tap_open(char *ifname, int ifname_size, |
1633 | baf74c95 | Mark McLoughlin | int *vnet_hdr, int vnet_hdr_required) |
1634 | 63a01ef8 | aliguori | { |
1635 | 63a01ef8 | aliguori | int fd;
|
1636 | 63a01ef8 | aliguori | char *dev;
|
1637 | 63a01ef8 | aliguori | struct stat s;
|
1638 | 63a01ef8 | aliguori | |
1639 | 63a01ef8 | aliguori | TFR(fd = open("/dev/tap", O_RDWR));
|
1640 | 63a01ef8 | aliguori | if (fd < 0) { |
1641 | 63a01ef8 | aliguori | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
1642 | 63a01ef8 | aliguori | return -1; |
1643 | 63a01ef8 | aliguori | } |
1644 | 63a01ef8 | aliguori | |
1645 | 63a01ef8 | aliguori | fstat(fd, &s); |
1646 | 63a01ef8 | aliguori | dev = devname(s.st_rdev, S_IFCHR); |
1647 | 63a01ef8 | aliguori | pstrcpy(ifname, ifname_size, dev); |
1648 | 63a01ef8 | aliguori | |
1649 | 63a01ef8 | aliguori | fcntl(fd, F_SETFL, O_NONBLOCK); |
1650 | 63a01ef8 | aliguori | return fd;
|
1651 | 63a01ef8 | aliguori | } |
1652 | 63a01ef8 | aliguori | #elif defined(__sun__)
|
1653 | 63a01ef8 | aliguori | #define TUNNEWPPA (('T'<<16) | 0x0001) |
1654 | 63a01ef8 | aliguori | /*
|
1655 | 63a01ef8 | aliguori | * Allocate TAP device, returns opened fd.
|
1656 | 63a01ef8 | aliguori | * Stores dev name in the first arg(must be large enough).
|
1657 | 63a01ef8 | aliguori | */
|
1658 | 3f4cb3d3 | blueswir1 | static int tap_alloc(char *dev, size_t dev_size) |
1659 | 63a01ef8 | aliguori | { |
1660 | 63a01ef8 | aliguori | int tap_fd, if_fd, ppa = -1; |
1661 | 63a01ef8 | aliguori | static int ip_fd = 0; |
1662 | 63a01ef8 | aliguori | char *ptr;
|
1663 | 63a01ef8 | aliguori | |
1664 | 63a01ef8 | aliguori | static int arp_fd = 0; |
1665 | 63a01ef8 | aliguori | int ip_muxid, arp_muxid;
|
1666 | 63a01ef8 | aliguori | struct strioctl strioc_if, strioc_ppa;
|
1667 | 63a01ef8 | aliguori | int link_type = I_PLINK;;
|
1668 | 63a01ef8 | aliguori | struct lifreq ifr;
|
1669 | 63a01ef8 | aliguori | char actual_name[32] = ""; |
1670 | 63a01ef8 | aliguori | |
1671 | 63a01ef8 | aliguori | memset(&ifr, 0x0, sizeof(ifr)); |
1672 | 63a01ef8 | aliguori | |
1673 | 63a01ef8 | aliguori | if( *dev ){
|
1674 | 63a01ef8 | aliguori | ptr = dev; |
1675 | 47398b9c | blueswir1 | while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++; |
1676 | 63a01ef8 | aliguori | ppa = atoi(ptr); |
1677 | 63a01ef8 | aliguori | } |
1678 | 63a01ef8 | aliguori | |
1679 | 63a01ef8 | aliguori | /* Check if IP device was opened */
|
1680 | 63a01ef8 | aliguori | if( ip_fd )
|
1681 | 63a01ef8 | aliguori | close(ip_fd); |
1682 | 63a01ef8 | aliguori | |
1683 | 63a01ef8 | aliguori | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
1684 | 63a01ef8 | aliguori | if (ip_fd < 0) { |
1685 | 63a01ef8 | aliguori | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
1686 | 63a01ef8 | aliguori | return -1; |
1687 | 63a01ef8 | aliguori | } |
1688 | 63a01ef8 | aliguori | |
1689 | 63a01ef8 | aliguori | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
1690 | 63a01ef8 | aliguori | if (tap_fd < 0) { |
1691 | 63a01ef8 | aliguori | syslog(LOG_ERR, "Can't open /dev/tap");
|
1692 | 63a01ef8 | aliguori | return -1; |
1693 | 63a01ef8 | aliguori | } |
1694 | 63a01ef8 | aliguori | |
1695 | 63a01ef8 | aliguori | /* Assign a new PPA and get its unit number. */
|
1696 | 63a01ef8 | aliguori | strioc_ppa.ic_cmd = TUNNEWPPA; |
1697 | 63a01ef8 | aliguori | strioc_ppa.ic_timout = 0;
|
1698 | 63a01ef8 | aliguori | strioc_ppa.ic_len = sizeof(ppa);
|
1699 | 63a01ef8 | aliguori | strioc_ppa.ic_dp = (char *)&ppa;
|
1700 | 63a01ef8 | aliguori | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
1701 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't assign new interface");
|
1702 | 63a01ef8 | aliguori | |
1703 | 63a01ef8 | aliguori | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
1704 | 63a01ef8 | aliguori | if (if_fd < 0) { |
1705 | 63a01ef8 | aliguori | syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
1706 | 63a01ef8 | aliguori | return -1; |
1707 | 63a01ef8 | aliguori | } |
1708 | 63a01ef8 | aliguori | if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
1709 | 63a01ef8 | aliguori | syslog(LOG_ERR, "Can't push IP module");
|
1710 | 63a01ef8 | aliguori | return -1; |
1711 | 63a01ef8 | aliguori | } |
1712 | 63a01ef8 | aliguori | |
1713 | 63a01ef8 | aliguori | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
1714 | 63a01ef8 | aliguori | syslog(LOG_ERR, "Can't get flags\n");
|
1715 | 63a01ef8 | aliguori | |
1716 | 63a01ef8 | aliguori | snprintf (actual_name, 32, "tap%d", ppa); |
1717 | 63a01ef8 | aliguori | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
|
1718 | 63a01ef8 | aliguori | |
1719 | 63a01ef8 | aliguori | ifr.lifr_ppa = ppa; |
1720 | 63a01ef8 | aliguori | /* Assign ppa according to the unit number returned by tun device */
|
1721 | 63a01ef8 | aliguori | |
1722 | 63a01ef8 | aliguori | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
1723 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
1724 | 63a01ef8 | aliguori | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
1725 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't get flags\n");
|
1726 | 63a01ef8 | aliguori | /* Push arp module to if_fd */
|
1727 | 63a01ef8 | aliguori | if (ioctl (if_fd, I_PUSH, "arp") < 0) |
1728 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't push ARP module (2)");
|
1729 | 63a01ef8 | aliguori | |
1730 | 63a01ef8 | aliguori | /* Push arp module to ip_fd */
|
1731 | 63a01ef8 | aliguori | if (ioctl (ip_fd, I_POP, NULL) < 0) |
1732 | 63a01ef8 | aliguori | syslog (LOG_ERR, "I_POP failed\n");
|
1733 | 63a01ef8 | aliguori | if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
1734 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
1735 | 63a01ef8 | aliguori | /* Open arp_fd */
|
1736 | 63a01ef8 | aliguori | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
1737 | 63a01ef8 | aliguori | if (arp_fd < 0) |
1738 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
1739 | 63a01ef8 | aliguori | |
1740 | 63a01ef8 | aliguori | /* Set ifname to arp */
|
1741 | 63a01ef8 | aliguori | strioc_if.ic_cmd = SIOCSLIFNAME; |
1742 | 63a01ef8 | aliguori | strioc_if.ic_timout = 0;
|
1743 | 63a01ef8 | aliguori | strioc_if.ic_len = sizeof(ifr);
|
1744 | 63a01ef8 | aliguori | strioc_if.ic_dp = (char *)𝔦
|
1745 | 63a01ef8 | aliguori | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
1746 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't set ifname to arp\n");
|
1747 | 63a01ef8 | aliguori | } |
1748 | 63a01ef8 | aliguori | |
1749 | 63a01ef8 | aliguori | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
1750 | 63a01ef8 | aliguori | syslog(LOG_ERR, "Can't link TAP device to IP");
|
1751 | 63a01ef8 | aliguori | return -1; |
1752 | 63a01ef8 | aliguori | } |
1753 | 63a01ef8 | aliguori | |
1754 | 63a01ef8 | aliguori | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
1755 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't link TAP device to ARP");
|
1756 | 63a01ef8 | aliguori | |
1757 | 63a01ef8 | aliguori | close (if_fd); |
1758 | 63a01ef8 | aliguori | |
1759 | 63a01ef8 | aliguori | memset(&ifr, 0x0, sizeof(ifr)); |
1760 | 63a01ef8 | aliguori | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
|
1761 | 63a01ef8 | aliguori | ifr.lifr_ip_muxid = ip_muxid; |
1762 | 63a01ef8 | aliguori | ifr.lifr_arp_muxid = arp_muxid; |
1763 | 63a01ef8 | aliguori | |
1764 | 63a01ef8 | aliguori | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
1765 | 63a01ef8 | aliguori | { |
1766 | 63a01ef8 | aliguori | ioctl (ip_fd, I_PUNLINK , arp_muxid); |
1767 | 63a01ef8 | aliguori | ioctl (ip_fd, I_PUNLINK, ip_muxid); |
1768 | 63a01ef8 | aliguori | syslog (LOG_ERR, "Can't set multiplexor id");
|
1769 | 63a01ef8 | aliguori | } |
1770 | 63a01ef8 | aliguori | |
1771 | 63a01ef8 | aliguori | snprintf(dev, dev_size, "tap%d", ppa);
|
1772 | 63a01ef8 | aliguori | return tap_fd;
|
1773 | 63a01ef8 | aliguori | } |
1774 | 63a01ef8 | aliguori | |
1775 | baf74c95 | Mark McLoughlin | static int tap_open(char *ifname, int ifname_size, |
1776 | baf74c95 | Mark McLoughlin | int *vnet_hdr, int vnet_hdr_required) |
1777 | 63a01ef8 | aliguori | { |
1778 | 63a01ef8 | aliguori | char dev[10]=""; |
1779 | 63a01ef8 | aliguori | int fd;
|
1780 | 63a01ef8 | aliguori | if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ |
1781 | 63a01ef8 | aliguori | fprintf(stderr, "Cannot allocate TAP device\n");
|
1782 | 63a01ef8 | aliguori | return -1; |
1783 | 63a01ef8 | aliguori | } |
1784 | 63a01ef8 | aliguori | pstrcpy(ifname, ifname_size, dev); |
1785 | 63a01ef8 | aliguori | fcntl(fd, F_SETFL, O_NONBLOCK); |
1786 | 63a01ef8 | aliguori | return fd;
|
1787 | 63a01ef8 | aliguori | } |
1788 | b29fe3ed | malc | #elif defined (_AIX)
|
1789 | baf74c95 | Mark McLoughlin | static int tap_open(char *ifname, int ifname_size, |
1790 | baf74c95 | Mark McLoughlin | int *vnet_hdr, int vnet_hdr_required) |
1791 | b29fe3ed | malc | { |
1792 | b29fe3ed | malc | fprintf (stderr, "no tap on AIX\n");
|
1793 | b29fe3ed | malc | return -1; |
1794 | b29fe3ed | malc | } |
1795 | 63a01ef8 | aliguori | #else
|
1796 | baf74c95 | Mark McLoughlin | static int tap_open(char *ifname, int ifname_size, |
1797 | baf74c95 | Mark McLoughlin | int *vnet_hdr, int vnet_hdr_required) |
1798 | 63a01ef8 | aliguori | { |
1799 | 63a01ef8 | aliguori | struct ifreq ifr;
|
1800 | 63a01ef8 | aliguori | int fd, ret;
|
1801 | 63a01ef8 | aliguori | |
1802 | 63a01ef8 | aliguori | TFR(fd = open("/dev/net/tun", O_RDWR));
|
1803 | 63a01ef8 | aliguori | if (fd < 0) { |
1804 | 63a01ef8 | aliguori | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
1805 | 63a01ef8 | aliguori | return -1; |
1806 | 63a01ef8 | aliguori | } |
1807 | 63a01ef8 | aliguori | memset(&ifr, 0, sizeof(ifr)); |
1808 | 63a01ef8 | aliguori | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
1809 | 8e0f8e5b | Mark McLoughlin | |
1810 | baf74c95 | Mark McLoughlin | if (*vnet_hdr) {
|
1811 | 8e0f8e5b | Mark McLoughlin | unsigned int features; |
1812 | 8e0f8e5b | Mark McLoughlin | |
1813 | 8e0f8e5b | Mark McLoughlin | if (ioctl(fd, TUNGETFEATURES, &features) == 0 && |
1814 | 8e0f8e5b | Mark McLoughlin | features & IFF_VNET_HDR) { |
1815 | 8e0f8e5b | Mark McLoughlin | *vnet_hdr = 1;
|
1816 | 8e0f8e5b | Mark McLoughlin | ifr.ifr_flags |= IFF_VNET_HDR; |
1817 | 8e0f8e5b | Mark McLoughlin | } |
1818 | baf74c95 | Mark McLoughlin | |
1819 | baf74c95 | Mark McLoughlin | if (vnet_hdr_required && !*vnet_hdr) {
|
1820 | baf74c95 | Mark McLoughlin | qemu_error("vnet_hdr=1 requested, but no kernel "
|
1821 | baf74c95 | Mark McLoughlin | "support for IFF_VNET_HDR available");
|
1822 | baf74c95 | Mark McLoughlin | close(fd); |
1823 | baf74c95 | Mark McLoughlin | return -1; |
1824 | baf74c95 | Mark McLoughlin | } |
1825 | 8e0f8e5b | Mark McLoughlin | } |
1826 | 8e0f8e5b | Mark McLoughlin | |
1827 | 63a01ef8 | aliguori | if (ifname[0] != '\0') |
1828 | 63a01ef8 | aliguori | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
1829 | 63a01ef8 | aliguori | else
|
1830 | 63a01ef8 | aliguori | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
1831 | 63a01ef8 | aliguori | ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
1832 | 63a01ef8 | aliguori | if (ret != 0) { |
1833 | 63a01ef8 | aliguori | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
1834 | 63a01ef8 | aliguori | close(fd); |
1835 | 63a01ef8 | aliguori | return -1; |
1836 | 63a01ef8 | aliguori | } |
1837 | 63a01ef8 | aliguori | pstrcpy(ifname, ifname_size, ifr.ifr_name); |
1838 | 63a01ef8 | aliguori | fcntl(fd, F_SETFL, O_NONBLOCK); |
1839 | 63a01ef8 | aliguori | return fd;
|
1840 | 63a01ef8 | aliguori | } |
1841 | 63a01ef8 | aliguori | #endif
|
1842 | 63a01ef8 | aliguori | |
1843 | 63a01ef8 | aliguori | static int launch_script(const char *setup_script, const char *ifname, int fd) |
1844 | 63a01ef8 | aliguori | { |
1845 | 7c3370d4 | Jan Kiszka | sigset_t oldmask, mask; |
1846 | 63a01ef8 | aliguori | int pid, status;
|
1847 | 63a01ef8 | aliguori | char *args[3]; |
1848 | 63a01ef8 | aliguori | char **parg;
|
1849 | 63a01ef8 | aliguori | |
1850 | 7c3370d4 | Jan Kiszka | sigemptyset(&mask); |
1851 | 7c3370d4 | Jan Kiszka | sigaddset(&mask, SIGCHLD); |
1852 | 7c3370d4 | Jan Kiszka | sigprocmask(SIG_BLOCK, &mask, &oldmask); |
1853 | 7c3370d4 | Jan Kiszka | |
1854 | 7c3370d4 | Jan Kiszka | /* try to launch network script */
|
1855 | 7c3370d4 | Jan Kiszka | pid = fork(); |
1856 | 7c3370d4 | Jan Kiszka | if (pid == 0) { |
1857 | 7c3370d4 | Jan Kiszka | int open_max = sysconf(_SC_OPEN_MAX), i;
|
1858 | 7c3370d4 | Jan Kiszka | |
1859 | 7c3370d4 | Jan Kiszka | for (i = 0; i < open_max; i++) { |
1860 | 7c3370d4 | Jan Kiszka | if (i != STDIN_FILENO &&
|
1861 | 7c3370d4 | Jan Kiszka | i != STDOUT_FILENO && |
1862 | 7c3370d4 | Jan Kiszka | i != STDERR_FILENO && |
1863 | 7c3370d4 | Jan Kiszka | i != fd) { |
1864 | 7c3370d4 | Jan Kiszka | close(i); |
1865 | 63a01ef8 | aliguori | } |
1866 | 63a01ef8 | aliguori | } |
1867 | 7c3370d4 | Jan Kiszka | parg = args; |
1868 | 7c3370d4 | Jan Kiszka | *parg++ = (char *)setup_script;
|
1869 | 7c3370d4 | Jan Kiszka | *parg++ = (char *)ifname;
|
1870 | 7c3370d4 | Jan Kiszka | *parg++ = NULL;
|
1871 | 7c3370d4 | Jan Kiszka | execv(setup_script, args); |
1872 | 7c3370d4 | Jan Kiszka | _exit(1);
|
1873 | 7c3370d4 | Jan Kiszka | } else if (pid > 0) { |
1874 | 7c3370d4 | Jan Kiszka | while (waitpid(pid, &status, 0) != pid) { |
1875 | 7c3370d4 | Jan Kiszka | /* loop */
|
1876 | 7c3370d4 | Jan Kiszka | } |
1877 | 7c3370d4 | Jan Kiszka | sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
1878 | 7c3370d4 | Jan Kiszka | |
1879 | 7c3370d4 | Jan Kiszka | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
1880 | 7c3370d4 | Jan Kiszka | return 0; |
1881 | 7c3370d4 | Jan Kiszka | } |
1882 | 7c3370d4 | Jan Kiszka | } |
1883 | 7c3370d4 | Jan Kiszka | fprintf(stderr, "%s: could not launch network script\n", setup_script);
|
1884 | 7c3370d4 | Jan Kiszka | return -1; |
1885 | 63a01ef8 | aliguori | } |
1886 | 63a01ef8 | aliguori | |
1887 | 424a7f96 | Mark McLoughlin | static int net_tap_init(QemuOpts *opts, int *vnet_hdr) |
1888 | 63a01ef8 | aliguori | { |
1889 | baf74c95 | Mark McLoughlin | int fd, vnet_hdr_required;
|
1890 | 424a7f96 | Mark McLoughlin | char ifname[128] = {0,}; |
1891 | 424a7f96 | Mark McLoughlin | const char *setup_script; |
1892 | 63a01ef8 | aliguori | |
1893 | 424a7f96 | Mark McLoughlin | if (qemu_opt_get(opts, "ifname")) { |
1894 | 424a7f96 | Mark McLoughlin | pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname")); |
1895 | 424a7f96 | Mark McLoughlin | } |
1896 | 63a01ef8 | aliguori | |
1897 | baf74c95 | Mark McLoughlin | *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1); |
1898 | baf74c95 | Mark McLoughlin | if (qemu_opt_get(opts, "vnet_hdr")) { |
1899 | baf74c95 | Mark McLoughlin | vnet_hdr_required = *vnet_hdr; |
1900 | baf74c95 | Mark McLoughlin | } else {
|
1901 | baf74c95 | Mark McLoughlin | vnet_hdr_required = 0;
|
1902 | baf74c95 | Mark McLoughlin | } |
1903 | baf74c95 | Mark McLoughlin | |
1904 | baf74c95 | Mark McLoughlin | TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
|
1905 | 424a7f96 | Mark McLoughlin | if (fd < 0) { |
1906 | 424a7f96 | Mark McLoughlin | return -1; |
1907 | 63a01ef8 | aliguori | } |
1908 | 424a7f96 | Mark McLoughlin | |
1909 | 424a7f96 | Mark McLoughlin | setup_script = qemu_opt_get(opts, "script");
|
1910 | 424a7f96 | Mark McLoughlin | if (setup_script &&
|
1911 | 424a7f96 | Mark McLoughlin | setup_script[0] != '\0' && |
1912 | 424a7f96 | Mark McLoughlin | strcmp(setup_script, "no") != 0 && |
1913 | 424a7f96 | Mark McLoughlin | launch_script(setup_script, ifname, fd)) { |
1914 | 424a7f96 | Mark McLoughlin | close(fd); |
1915 | 424a7f96 | Mark McLoughlin | return -1; |
1916 | 973cbd37 | aliguori | } |
1917 | 424a7f96 | Mark McLoughlin | |
1918 | 424a7f96 | Mark McLoughlin | qemu_opt_set(opts, "ifname", ifname);
|
1919 | 424a7f96 | Mark McLoughlin | |
1920 | 424a7f96 | Mark McLoughlin | return fd;
|
1921 | 63a01ef8 | aliguori | } |
1922 | 63a01ef8 | aliguori | |
1923 | 63a01ef8 | aliguori | #endif /* !_WIN32 */ |
1924 | 63a01ef8 | aliguori | |
1925 | 63a01ef8 | aliguori | #if defined(CONFIG_VDE)
|
1926 | 63a01ef8 | aliguori | typedef struct VDEState { |
1927 | 63a01ef8 | aliguori | VLANClientState *vc; |
1928 | 63a01ef8 | aliguori | VDECONN *vde; |
1929 | 63a01ef8 | aliguori | } VDEState; |
1930 | 63a01ef8 | aliguori | |
1931 | 63a01ef8 | aliguori | static void vde_to_qemu(void *opaque) |
1932 | 63a01ef8 | aliguori | { |
1933 | 63a01ef8 | aliguori | VDEState *s = opaque; |
1934 | 63a01ef8 | aliguori | uint8_t buf[4096];
|
1935 | 63a01ef8 | aliguori | int size;
|
1936 | 63a01ef8 | aliguori | |
1937 | cc63bb0f | Paul Brook | size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0); |
1938 | 63a01ef8 | aliguori | if (size > 0) { |
1939 | 63a01ef8 | aliguori | qemu_send_packet(s->vc, buf, size); |
1940 | 63a01ef8 | aliguori | } |
1941 | 63a01ef8 | aliguori | } |
1942 | 63a01ef8 | aliguori | |
1943 | 4f1c942b | Mark McLoughlin | static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
1944 | 63a01ef8 | aliguori | { |
1945 | e3f5ec2b | Mark McLoughlin | VDEState *s = vc->opaque; |
1946 | 068daedd | Anthony Liguori | ssize_t ret; |
1947 | 4f1c942b | Mark McLoughlin | |
1948 | 4f1c942b | Mark McLoughlin | do {
|
1949 | 4f1c942b | Mark McLoughlin | ret = vde_send(s->vde, (const char *)buf, size, 0); |
1950 | 4f1c942b | Mark McLoughlin | } while (ret < 0 && errno == EINTR); |
1951 | 4f1c942b | Mark McLoughlin | |
1952 | 4f1c942b | Mark McLoughlin | return ret;
|
1953 | 63a01ef8 | aliguori | } |
1954 | 63a01ef8 | aliguori | |
1955 | b946a153 | aliguori | static void vde_cleanup(VLANClientState *vc) |
1956 | b946a153 | aliguori | { |
1957 | b946a153 | aliguori | VDEState *s = vc->opaque; |
1958 | b946a153 | aliguori | qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL); |
1959 | b946a153 | aliguori | vde_close(s->vde); |
1960 | b946a153 | aliguori | qemu_free(s); |
1961 | b946a153 | aliguori | } |
1962 | b946a153 | aliguori | |
1963 | 7a9f6e4a | aliguori | static int net_vde_init(VLANState *vlan, const char *model, |
1964 | 7a9f6e4a | aliguori | const char *name, const char *sock, |
1965 | bf38c1a0 | aliguori | int port, const char *group, int mode) |
1966 | 63a01ef8 | aliguori | { |
1967 | 63a01ef8 | aliguori | VDEState *s; |
1968 | dd51058d | Mark McLoughlin | char *init_group = (char *)group; |
1969 | dd51058d | Mark McLoughlin | char *init_sock = (char *)sock; |
1970 | 63a01ef8 | aliguori | |
1971 | 63a01ef8 | aliguori | struct vde_open_args args = {
|
1972 | 63a01ef8 | aliguori | .port = port, |
1973 | 63a01ef8 | aliguori | .group = init_group, |
1974 | 63a01ef8 | aliguori | .mode = mode, |
1975 | 63a01ef8 | aliguori | }; |
1976 | 63a01ef8 | aliguori | |
1977 | 63a01ef8 | aliguori | s = qemu_mallocz(sizeof(VDEState));
|
1978 | cc63bb0f | Paul Brook | s->vde = vde_open(init_sock, (char *)"QEMU", &args); |
1979 | 63a01ef8 | aliguori | if (!s->vde){
|
1980 | 63a01ef8 | aliguori | free(s); |
1981 | 63a01ef8 | aliguori | return -1; |
1982 | 63a01ef8 | aliguori | } |
1983 | bb6e6364 | Mark McLoughlin | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_VDE, |
1984 | bb6e6364 | Mark McLoughlin | vlan, NULL, model, name, NULL, |
1985 | 70783b9c | Mark McLoughlin | vde_receive, NULL, NULL, |
1986 | 283c7c63 | Mark McLoughlin | vde_cleanup, s); |
1987 | 63a01ef8 | aliguori | qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
|
1988 | 7cb7434b | aliguori | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d", |
1989 | 63a01ef8 | aliguori | sock, vde_datafd(s->vde)); |
1990 | 63a01ef8 | aliguori | return 0; |
1991 | 63a01ef8 | aliguori | } |
1992 | 63a01ef8 | aliguori | #endif
|
1993 | 63a01ef8 | aliguori | |
1994 | 63a01ef8 | aliguori | /* network connection */
|
1995 | 63a01ef8 | aliguori | typedef struct NetSocketState { |
1996 | 63a01ef8 | aliguori | VLANClientState *vc; |
1997 | 63a01ef8 | aliguori | int fd;
|
1998 | 63a01ef8 | aliguori | int state; /* 0 = getting length, 1 = getting data */ |
1999 | abcd2baa | aliguori | unsigned int index; |
2000 | abcd2baa | aliguori | unsigned int packet_len; |
2001 | 63a01ef8 | aliguori | uint8_t buf[4096];
|
2002 | 63a01ef8 | aliguori | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
2003 | 63a01ef8 | aliguori | } NetSocketState; |
2004 | 63a01ef8 | aliguori | |
2005 | 63a01ef8 | aliguori | typedef struct NetSocketListenState { |
2006 | 63a01ef8 | aliguori | VLANState *vlan; |
2007 | bf38c1a0 | aliguori | char *model;
|
2008 | 7a9f6e4a | aliguori | char *name;
|
2009 | 63a01ef8 | aliguori | int fd;
|
2010 | 63a01ef8 | aliguori | } NetSocketListenState; |
2011 | 63a01ef8 | aliguori | |
2012 | 63a01ef8 | aliguori | /* XXX: we consider we can send the whole packet without blocking */
|
2013 | 4f1c942b | Mark McLoughlin | static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
2014 | 63a01ef8 | aliguori | { |
2015 | e3f5ec2b | Mark McLoughlin | NetSocketState *s = vc->opaque; |
2016 | 63a01ef8 | aliguori | uint32_t len; |
2017 | 63a01ef8 | aliguori | len = htonl(size); |
2018 | 63a01ef8 | aliguori | |
2019 | 63a01ef8 | aliguori | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
2020 | 4f1c942b | Mark McLoughlin | return send_all(s->fd, buf, size);
|
2021 | 63a01ef8 | aliguori | } |
2022 | 63a01ef8 | aliguori | |
2023 | 4f1c942b | Mark McLoughlin | static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size) |
2024 | 63a01ef8 | aliguori | { |
2025 | e3f5ec2b | Mark McLoughlin | NetSocketState *s = vc->opaque; |
2026 | 4f1c942b | Mark McLoughlin | |
2027 | 70503264 | Stefan Weil | return sendto(s->fd, (const void *)buf, size, 0, |
2028 | 4f1c942b | Mark McLoughlin | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
2029 | 63a01ef8 | aliguori | } |
2030 | 63a01ef8 | aliguori | |
2031 | 63a01ef8 | aliguori | static void net_socket_send(void *opaque) |
2032 | 63a01ef8 | aliguori | { |
2033 | 63a01ef8 | aliguori | NetSocketState *s = opaque; |
2034 | abcd2baa | aliguori | int size, err;
|
2035 | abcd2baa | aliguori | unsigned l;
|
2036 | 63a01ef8 | aliguori | uint8_t buf1[4096];
|
2037 | 63a01ef8 | aliguori | const uint8_t *buf;
|
2038 | 63a01ef8 | aliguori | |
2039 | c5b76b38 | Blue Swirl | size = recv(s->fd, (void *)buf1, sizeof(buf1), 0); |
2040 | 63a01ef8 | aliguori | if (size < 0) { |
2041 | 63a01ef8 | aliguori | err = socket_error(); |
2042 | 63a01ef8 | aliguori | if (err != EWOULDBLOCK)
|
2043 | 63a01ef8 | aliguori | goto eoc;
|
2044 | 63a01ef8 | aliguori | } else if (size == 0) { |
2045 | 63a01ef8 | aliguori | /* end of connection */
|
2046 | 63a01ef8 | aliguori | eoc:
|
2047 | 63a01ef8 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
2048 | 63a01ef8 | aliguori | closesocket(s->fd); |
2049 | 63a01ef8 | aliguori | return;
|
2050 | 63a01ef8 | aliguori | } |
2051 | 63a01ef8 | aliguori | buf = buf1; |
2052 | 63a01ef8 | aliguori | while (size > 0) { |
2053 | 63a01ef8 | aliguori | /* reassemble a packet from the network */
|
2054 | 63a01ef8 | aliguori | switch(s->state) {
|
2055 | 63a01ef8 | aliguori | case 0: |
2056 | 63a01ef8 | aliguori | l = 4 - s->index;
|
2057 | 63a01ef8 | aliguori | if (l > size)
|
2058 | 63a01ef8 | aliguori | l = size; |
2059 | 63a01ef8 | aliguori | memcpy(s->buf + s->index, buf, l); |
2060 | 63a01ef8 | aliguori | buf += l; |
2061 | 63a01ef8 | aliguori | size -= l; |
2062 | 63a01ef8 | aliguori | s->index += l; |
2063 | 63a01ef8 | aliguori | if (s->index == 4) { |
2064 | 63a01ef8 | aliguori | /* got length */
|
2065 | 63a01ef8 | aliguori | s->packet_len = ntohl(*(uint32_t *)s->buf); |
2066 | 63a01ef8 | aliguori | s->index = 0;
|
2067 | 63a01ef8 | aliguori | s->state = 1;
|
2068 | 63a01ef8 | aliguori | } |
2069 | 63a01ef8 | aliguori | break;
|
2070 | 63a01ef8 | aliguori | case 1: |
2071 | 63a01ef8 | aliguori | l = s->packet_len - s->index; |
2072 | 63a01ef8 | aliguori | if (l > size)
|
2073 | 63a01ef8 | aliguori | l = size; |
2074 | abcd2baa | aliguori | if (s->index + l <= sizeof(s->buf)) { |
2075 | abcd2baa | aliguori | memcpy(s->buf + s->index, buf, l); |
2076 | abcd2baa | aliguori | } else {
|
2077 | abcd2baa | aliguori | fprintf(stderr, "serious error: oversized packet received,"
|
2078 | abcd2baa | aliguori | "connection terminated.\n");
|
2079 | abcd2baa | aliguori | s->state = 0;
|
2080 | abcd2baa | aliguori | goto eoc;
|
2081 | abcd2baa | aliguori | } |
2082 | abcd2baa | aliguori | |
2083 | 63a01ef8 | aliguori | s->index += l; |
2084 | 63a01ef8 | aliguori | buf += l; |
2085 | 63a01ef8 | aliguori | size -= l; |
2086 | 63a01ef8 | aliguori | if (s->index >= s->packet_len) {
|
2087 | 63a01ef8 | aliguori | qemu_send_packet(s->vc, s->buf, s->packet_len); |
2088 | 63a01ef8 | aliguori | s->index = 0;
|
2089 | 63a01ef8 | aliguori | s->state = 0;
|
2090 | 63a01ef8 | aliguori | } |
2091 | 63a01ef8 | aliguori | break;
|
2092 | 63a01ef8 | aliguori | } |
2093 | 63a01ef8 | aliguori | } |
2094 | 63a01ef8 | aliguori | } |
2095 | 63a01ef8 | aliguori | |
2096 | 63a01ef8 | aliguori | static void net_socket_send_dgram(void *opaque) |
2097 | 63a01ef8 | aliguori | { |
2098 | 63a01ef8 | aliguori | NetSocketState *s = opaque; |
2099 | 63a01ef8 | aliguori | int size;
|
2100 | 63a01ef8 | aliguori | |
2101 | c5b76b38 | Blue Swirl | size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); |
2102 | 63a01ef8 | aliguori | if (size < 0) |
2103 | 63a01ef8 | aliguori | return;
|
2104 | 63a01ef8 | aliguori | if (size == 0) { |
2105 | 63a01ef8 | aliguori | /* end of connection */
|
2106 | 63a01ef8 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
2107 | 63a01ef8 | aliguori | return;
|
2108 | 63a01ef8 | aliguori | } |
2109 | 63a01ef8 | aliguori | qemu_send_packet(s->vc, s->buf, size); |
2110 | 63a01ef8 | aliguori | } |
2111 | 63a01ef8 | aliguori | |
2112 | 63a01ef8 | aliguori | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
2113 | 63a01ef8 | aliguori | { |
2114 | 63a01ef8 | aliguori | struct ip_mreq imr;
|
2115 | 63a01ef8 | aliguori | int fd;
|
2116 | 63a01ef8 | aliguori | int val, ret;
|
2117 | 63a01ef8 | aliguori | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
2118 | 63a01ef8 | aliguori | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
2119 | 63a01ef8 | aliguori | inet_ntoa(mcastaddr->sin_addr), |
2120 | 63a01ef8 | aliguori | (int)ntohl(mcastaddr->sin_addr.s_addr));
|
2121 | 63a01ef8 | aliguori | return -1; |
2122 | 63a01ef8 | aliguori | |
2123 | 63a01ef8 | aliguori | } |
2124 | 63a01ef8 | aliguori | fd = socket(PF_INET, SOCK_DGRAM, 0);
|
2125 | 63a01ef8 | aliguori | if (fd < 0) { |
2126 | 63a01ef8 | aliguori | perror("socket(PF_INET, SOCK_DGRAM)");
|
2127 | 63a01ef8 | aliguori | return -1; |
2128 | 63a01ef8 | aliguori | } |
2129 | 63a01ef8 | aliguori | |
2130 | 63a01ef8 | aliguori | val = 1;
|
2131 | 63a01ef8 | aliguori | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
2132 | 63a01ef8 | aliguori | (const char *)&val, sizeof(val)); |
2133 | 63a01ef8 | aliguori | if (ret < 0) { |
2134 | 63a01ef8 | aliguori | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
2135 | 63a01ef8 | aliguori | goto fail;
|
2136 | 63a01ef8 | aliguori | } |
2137 | 63a01ef8 | aliguori | |
2138 | 63a01ef8 | aliguori | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
2139 | 63a01ef8 | aliguori | if (ret < 0) { |
2140 | 63a01ef8 | aliguori | perror("bind");
|
2141 | 63a01ef8 | aliguori | goto fail;
|
2142 | 63a01ef8 | aliguori | } |
2143 | 63a01ef8 | aliguori | |
2144 | 63a01ef8 | aliguori | /* Add host to multicast group */
|
2145 | 63a01ef8 | aliguori | imr.imr_multiaddr = mcastaddr->sin_addr; |
2146 | 63a01ef8 | aliguori | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
2147 | 63a01ef8 | aliguori | |
2148 | 63a01ef8 | aliguori | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
2149 | 63a01ef8 | aliguori | (const char *)&imr, sizeof(struct ip_mreq)); |
2150 | 63a01ef8 | aliguori | if (ret < 0) { |
2151 | 63a01ef8 | aliguori | perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
2152 | 63a01ef8 | aliguori | goto fail;
|
2153 | 63a01ef8 | aliguori | } |
2154 | 63a01ef8 | aliguori | |
2155 | 63a01ef8 | aliguori | /* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
2156 | 63a01ef8 | aliguori | val = 1;
|
2157 | 63a01ef8 | aliguori | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
2158 | 63a01ef8 | aliguori | (const char *)&val, sizeof(val)); |
2159 | 63a01ef8 | aliguori | if (ret < 0) { |
2160 | 63a01ef8 | aliguori | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
2161 | 63a01ef8 | aliguori | goto fail;
|
2162 | 63a01ef8 | aliguori | } |
2163 | 63a01ef8 | aliguori | |
2164 | 63a01ef8 | aliguori | socket_set_nonblock(fd); |
2165 | 63a01ef8 | aliguori | return fd;
|
2166 | 63a01ef8 | aliguori | fail:
|
2167 | 63a01ef8 | aliguori | if (fd >= 0) |
2168 | 63a01ef8 | aliguori | closesocket(fd); |
2169 | 63a01ef8 | aliguori | return -1; |
2170 | 63a01ef8 | aliguori | } |
2171 | 63a01ef8 | aliguori | |
2172 | b946a153 | aliguori | static void net_socket_cleanup(VLANClientState *vc) |
2173 | b946a153 | aliguori | { |
2174 | b946a153 | aliguori | NetSocketState *s = vc->opaque; |
2175 | b946a153 | aliguori | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
2176 | b946a153 | aliguori | close(s->fd); |
2177 | b946a153 | aliguori | qemu_free(s); |
2178 | b946a153 | aliguori | } |
2179 | b946a153 | aliguori | |
2180 | 7a9f6e4a | aliguori | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
|
2181 | 7a9f6e4a | aliguori | const char *model, |
2182 | 7a9f6e4a | aliguori | const char *name, |
2183 | bf38c1a0 | aliguori | int fd, int is_connected) |
2184 | 63a01ef8 | aliguori | { |
2185 | 63a01ef8 | aliguori | struct sockaddr_in saddr;
|
2186 | 63a01ef8 | aliguori | int newfd;
|
2187 | 63a01ef8 | aliguori | socklen_t saddr_len; |
2188 | 63a01ef8 | aliguori | NetSocketState *s; |
2189 | 63a01ef8 | aliguori | |
2190 | 63a01ef8 | aliguori | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
2191 | 63a01ef8 | aliguori | * Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
2192 | 63a01ef8 | aliguori | * by ONLY ONE process: we must "clone" this dgram socket --jjo
|
2193 | 63a01ef8 | aliguori | */
|
2194 | 63a01ef8 | aliguori | |
2195 | 63a01ef8 | aliguori | if (is_connected) {
|
2196 | 63a01ef8 | aliguori | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
2197 | 63a01ef8 | aliguori | /* must be bound */
|
2198 | 63a01ef8 | aliguori | if (saddr.sin_addr.s_addr==0) { |
2199 | 63a01ef8 | aliguori | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
2200 | 63a01ef8 | aliguori | fd); |
2201 | 63a01ef8 | aliguori | return NULL; |
2202 | 63a01ef8 | aliguori | } |
2203 | 63a01ef8 | aliguori | /* clone dgram socket */
|
2204 | 63a01ef8 | aliguori | newfd = net_socket_mcast_create(&saddr); |
2205 | 63a01ef8 | aliguori | if (newfd < 0) { |
2206 | 63a01ef8 | aliguori | /* error already reported by net_socket_mcast_create() */
|
2207 | 63a01ef8 | aliguori | close(fd); |
2208 | 63a01ef8 | aliguori | return NULL; |
2209 | 63a01ef8 | aliguori | } |
2210 | 63a01ef8 | aliguori | /* clone newfd to fd, close newfd */
|
2211 | 63a01ef8 | aliguori | dup2(newfd, fd); |
2212 | 63a01ef8 | aliguori | close(newfd); |
2213 | 63a01ef8 | aliguori | |
2214 | 63a01ef8 | aliguori | } else {
|
2215 | 63a01ef8 | aliguori | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
2216 | 63a01ef8 | aliguori | fd, strerror(errno)); |
2217 | 63a01ef8 | aliguori | return NULL; |
2218 | 63a01ef8 | aliguori | } |
2219 | 63a01ef8 | aliguori | } |
2220 | 63a01ef8 | aliguori | |
2221 | 63a01ef8 | aliguori | s = qemu_mallocz(sizeof(NetSocketState));
|
2222 | 63a01ef8 | aliguori | s->fd = fd; |
2223 | 63a01ef8 | aliguori | |
2224 | bb6e6364 | Mark McLoughlin | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
2225 | bb6e6364 | Mark McLoughlin | vlan, NULL, model, name, NULL, |
2226 | 70783b9c | Mark McLoughlin | net_socket_receive_dgram, NULL, NULL, |
2227 | 283c7c63 | Mark McLoughlin | net_socket_cleanup, s); |
2228 | 63a01ef8 | aliguori | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
2229 | 63a01ef8 | aliguori | |
2230 | 63a01ef8 | aliguori | /* mcast: save bound address as dst */
|
2231 | 63a01ef8 | aliguori | if (is_connected) s->dgram_dst=saddr;
|
2232 | 63a01ef8 | aliguori | |
2233 | 63a01ef8 | aliguori | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
2234 | 63a01ef8 | aliguori | "socket: fd=%d (%s mcast=%s:%d)",
|
2235 | 63a01ef8 | aliguori | fd, is_connected? "cloned" : "", |
2236 | 63a01ef8 | aliguori | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
2237 | 63a01ef8 | aliguori | return s;
|
2238 | 63a01ef8 | aliguori | } |
2239 | 63a01ef8 | aliguori | |
2240 | 63a01ef8 | aliguori | static void net_socket_connect(void *opaque) |
2241 | 63a01ef8 | aliguori | { |
2242 | 63a01ef8 | aliguori | NetSocketState *s = opaque; |
2243 | 63a01ef8 | aliguori | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
2244 | 63a01ef8 | aliguori | } |
2245 | 63a01ef8 | aliguori | |
2246 | 7a9f6e4a | aliguori | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
|
2247 | 7a9f6e4a | aliguori | const char *model, |
2248 | 7a9f6e4a | aliguori | const char *name, |
2249 | bf38c1a0 | aliguori | int fd, int is_connected) |
2250 | 63a01ef8 | aliguori | { |
2251 | 63a01ef8 | aliguori | NetSocketState *s; |
2252 | 63a01ef8 | aliguori | s = qemu_mallocz(sizeof(NetSocketState));
|
2253 | 63a01ef8 | aliguori | s->fd = fd; |
2254 | bb6e6364 | Mark McLoughlin | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET, |
2255 | bb6e6364 | Mark McLoughlin | vlan, NULL, model, name, NULL, |
2256 | 70783b9c | Mark McLoughlin | net_socket_receive, NULL, NULL, |
2257 | 283c7c63 | Mark McLoughlin | net_socket_cleanup, s); |
2258 | 63a01ef8 | aliguori | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
2259 | 63a01ef8 | aliguori | "socket: fd=%d", fd);
|
2260 | 63a01ef8 | aliguori | if (is_connected) {
|
2261 | 63a01ef8 | aliguori | net_socket_connect(s); |
2262 | 63a01ef8 | aliguori | } else {
|
2263 | 63a01ef8 | aliguori | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
2264 | 63a01ef8 | aliguori | } |
2265 | 63a01ef8 | aliguori | return s;
|
2266 | 63a01ef8 | aliguori | } |
2267 | 63a01ef8 | aliguori | |
2268 | 7a9f6e4a | aliguori | static NetSocketState *net_socket_fd_init(VLANState *vlan,
|
2269 | 7a9f6e4a | aliguori | const char *model, const char *name, |
2270 | bf38c1a0 | aliguori | int fd, int is_connected) |
2271 | 63a01ef8 | aliguori | { |
2272 | acedcfbf | Michael S. Tsirkin | int so_type = -1, optlen=sizeof(so_type); |
2273 | 63a01ef8 | aliguori | |
2274 | 63a01ef8 | aliguori | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
2275 | 63a01ef8 | aliguori | (socklen_t *)&optlen)< 0) {
|
2276 | 63a01ef8 | aliguori | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
2277 | 63a01ef8 | aliguori | return NULL; |
2278 | 63a01ef8 | aliguori | } |
2279 | 63a01ef8 | aliguori | switch(so_type) {
|
2280 | 63a01ef8 | aliguori | case SOCK_DGRAM:
|
2281 | 7a9f6e4a | aliguori | return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
|
2282 | 63a01ef8 | aliguori | case SOCK_STREAM:
|
2283 | 7a9f6e4a | aliguori | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
|
2284 | 63a01ef8 | aliguori | default:
|
2285 | 63a01ef8 | aliguori | /* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
2286 | 63a01ef8 | aliguori | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
2287 | 7a9f6e4a | aliguori | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
|
2288 | 63a01ef8 | aliguori | } |
2289 | 63a01ef8 | aliguori | return NULL; |
2290 | 63a01ef8 | aliguori | } |
2291 | 63a01ef8 | aliguori | |
2292 | 63a01ef8 | aliguori | static void net_socket_accept(void *opaque) |
2293 | 63a01ef8 | aliguori | { |
2294 | 63a01ef8 | aliguori | NetSocketListenState *s = opaque; |
2295 | 63a01ef8 | aliguori | NetSocketState *s1; |
2296 | 63a01ef8 | aliguori | struct sockaddr_in saddr;
|
2297 | 63a01ef8 | aliguori | socklen_t len; |
2298 | 63a01ef8 | aliguori | int fd;
|
2299 | 63a01ef8 | aliguori | |
2300 | 63a01ef8 | aliguori | for(;;) {
|
2301 | 63a01ef8 | aliguori | len = sizeof(saddr);
|
2302 | 63a01ef8 | aliguori | fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
2303 | 63a01ef8 | aliguori | if (fd < 0 && errno != EINTR) { |
2304 | 63a01ef8 | aliguori | return;
|
2305 | 63a01ef8 | aliguori | } else if (fd >= 0) { |
2306 | 63a01ef8 | aliguori | break;
|
2307 | 63a01ef8 | aliguori | } |
2308 | 63a01ef8 | aliguori | } |
2309 | 7a9f6e4a | aliguori | s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
|
2310 | 63a01ef8 | aliguori | if (!s1) {
|
2311 | 63a01ef8 | aliguori | closesocket(fd); |
2312 | 63a01ef8 | aliguori | } else {
|
2313 | 63a01ef8 | aliguori | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
2314 | 63a01ef8 | aliguori | "socket: connection from %s:%d",
|
2315 | 63a01ef8 | aliguori | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
2316 | 63a01ef8 | aliguori | } |
2317 | 63a01ef8 | aliguori | } |
2318 | 63a01ef8 | aliguori | |
2319 | 7a9f6e4a | aliguori | static int net_socket_listen_init(VLANState *vlan, |
2320 | 7a9f6e4a | aliguori | const char *model, |
2321 | 7a9f6e4a | aliguori | const char *name, |
2322 | bf38c1a0 | aliguori | const char *host_str) |
2323 | 63a01ef8 | aliguori | { |
2324 | 63a01ef8 | aliguori | NetSocketListenState *s; |
2325 | 63a01ef8 | aliguori | int fd, val, ret;
|
2326 | 63a01ef8 | aliguori | struct sockaddr_in saddr;
|
2327 | 63a01ef8 | aliguori | |
2328 | 63a01ef8 | aliguori | if (parse_host_port(&saddr, host_str) < 0) |
2329 | 63a01ef8 | aliguori | return -1; |
2330 | 63a01ef8 | aliguori | |
2331 | 63a01ef8 | aliguori | s = qemu_mallocz(sizeof(NetSocketListenState));
|
2332 | 63a01ef8 | aliguori | |
2333 | 63a01ef8 | aliguori | fd = socket(PF_INET, SOCK_STREAM, 0);
|
2334 | 63a01ef8 | aliguori | if (fd < 0) { |
2335 | 63a01ef8 | aliguori | perror("socket");
|
2336 | 63a01ef8 | aliguori | return -1; |
2337 | 63a01ef8 | aliguori | } |
2338 | 63a01ef8 | aliguori | socket_set_nonblock(fd); |
2339 | 63a01ef8 | aliguori | |
2340 | 63a01ef8 | aliguori | /* allow fast reuse */
|
2341 | 63a01ef8 | aliguori | val = 1;
|
2342 | 63a01ef8 | aliguori | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
2343 | 63a01ef8 | aliguori | |
2344 | 63a01ef8 | aliguori | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
2345 | 63a01ef8 | aliguori | if (ret < 0) { |
2346 | 63a01ef8 | aliguori | perror("bind");
|
2347 | 63a01ef8 | aliguori | return -1; |
2348 | 63a01ef8 | aliguori | } |
2349 | 63a01ef8 | aliguori | ret = listen(fd, 0);
|
2350 | 63a01ef8 | aliguori | if (ret < 0) { |
2351 | 63a01ef8 | aliguori | perror("listen");
|
2352 | 63a01ef8 | aliguori | return -1; |
2353 | 63a01ef8 | aliguori | } |
2354 | 63a01ef8 | aliguori | s->vlan = vlan; |
2355 | 02374aa0 | Mark McLoughlin | s->model = qemu_strdup(model); |
2356 | 02374aa0 | Mark McLoughlin | s->name = name ? qemu_strdup(name) : NULL;
|
2357 | 63a01ef8 | aliguori | s->fd = fd; |
2358 | 63a01ef8 | aliguori | qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
2359 | 63a01ef8 | aliguori | return 0; |
2360 | 63a01ef8 | aliguori | } |
2361 | 63a01ef8 | aliguori | |
2362 | 7a9f6e4a | aliguori | static int net_socket_connect_init(VLANState *vlan, |
2363 | 7a9f6e4a | aliguori | const char *model, |
2364 | 7a9f6e4a | aliguori | const char *name, |
2365 | bf38c1a0 | aliguori | const char *host_str) |
2366 | 63a01ef8 | aliguori | { |
2367 | 63a01ef8 | aliguori | NetSocketState *s; |
2368 | 63a01ef8 | aliguori | int fd, connected, ret, err;
|
2369 | 63a01ef8 | aliguori | struct sockaddr_in saddr;
|
2370 | 63a01ef8 | aliguori | |
2371 | 63a01ef8 | aliguori | if (parse_host_port(&saddr, host_str) < 0) |
2372 | 63a01ef8 | aliguori | return -1; |
2373 | 63a01ef8 | aliguori | |
2374 | 63a01ef8 | aliguori | fd = socket(PF_INET, SOCK_STREAM, 0);
|
2375 | 63a01ef8 | aliguori | if (fd < 0) { |
2376 | 63a01ef8 | aliguori | perror("socket");
|
2377 | 63a01ef8 | aliguori | return -1; |
2378 | 63a01ef8 | aliguori | } |
2379 | 63a01ef8 | aliguori | socket_set_nonblock(fd); |
2380 | 63a01ef8 | aliguori | |
2381 | 63a01ef8 | aliguori | connected = 0;
|
2382 | 63a01ef8 | aliguori | for(;;) {
|
2383 | 63a01ef8 | aliguori | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
2384 | 63a01ef8 | aliguori | if (ret < 0) { |
2385 | 63a01ef8 | aliguori | err = socket_error(); |
2386 | 63a01ef8 | aliguori | if (err == EINTR || err == EWOULDBLOCK) {
|
2387 | 63a01ef8 | aliguori | } else if (err == EINPROGRESS) { |
2388 | 63a01ef8 | aliguori | break;
|
2389 | 63a01ef8 | aliguori | #ifdef _WIN32
|
2390 | 63a01ef8 | aliguori | } else if (err == WSAEALREADY) { |
2391 | 63a01ef8 | aliguori | break;
|
2392 | 63a01ef8 | aliguori | #endif
|
2393 | 63a01ef8 | aliguori | } else {
|
2394 | 63a01ef8 | aliguori | perror("connect");
|
2395 | 63a01ef8 | aliguori | closesocket(fd); |
2396 | 63a01ef8 | aliguori | return -1; |
2397 | 63a01ef8 | aliguori | } |
2398 | 63a01ef8 | aliguori | } else {
|
2399 | 63a01ef8 | aliguori | connected = 1;
|
2400 | 63a01ef8 | aliguori | break;
|
2401 | 63a01ef8 | aliguori | } |
2402 | 63a01ef8 | aliguori | } |
2403 | 7a9f6e4a | aliguori | s = net_socket_fd_init(vlan, model, name, fd, connected); |
2404 | 63a01ef8 | aliguori | if (!s)
|
2405 | 63a01ef8 | aliguori | return -1; |
2406 | 63a01ef8 | aliguori | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
2407 | 63a01ef8 | aliguori | "socket: connect to %s:%d",
|
2408 | 63a01ef8 | aliguori | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
2409 | 63a01ef8 | aliguori | return 0; |
2410 | 63a01ef8 | aliguori | } |
2411 | 63a01ef8 | aliguori | |
2412 | 7a9f6e4a | aliguori | static int net_socket_mcast_init(VLANState *vlan, |
2413 | 7a9f6e4a | aliguori | const char *model, |
2414 | 7a9f6e4a | aliguori | const char *name, |
2415 | bf38c1a0 | aliguori | const char *host_str) |
2416 | 63a01ef8 | aliguori | { |
2417 | 63a01ef8 | aliguori | NetSocketState *s; |
2418 | 63a01ef8 | aliguori | int fd;
|
2419 | 63a01ef8 | aliguori | struct sockaddr_in saddr;
|
2420 | 63a01ef8 | aliguori | |
2421 | 63a01ef8 | aliguori | if (parse_host_port(&saddr, host_str) < 0) |
2422 | 63a01ef8 | aliguori | return -1; |
2423 | 63a01ef8 | aliguori | |
2424 | 63a01ef8 | aliguori | |
2425 | 63a01ef8 | aliguori | fd = net_socket_mcast_create(&saddr); |
2426 | 63a01ef8 | aliguori | if (fd < 0) |
2427 | 63a01ef8 | aliguori | return -1; |
2428 | 63a01ef8 | aliguori | |
2429 | 7a9f6e4a | aliguori | s = net_socket_fd_init(vlan, model, name, fd, 0);
|
2430 | 63a01ef8 | aliguori | if (!s)
|
2431 | 63a01ef8 | aliguori | return -1; |
2432 | 63a01ef8 | aliguori | |
2433 | 63a01ef8 | aliguori | s->dgram_dst = saddr; |
2434 | 63a01ef8 | aliguori | |
2435 | 63a01ef8 | aliguori | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
2436 | 63a01ef8 | aliguori | "socket: mcast=%s:%d",
|
2437 | 63a01ef8 | aliguori | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
2438 | 63a01ef8 | aliguori | return 0; |
2439 | 63a01ef8 | aliguori | |
2440 | 63a01ef8 | aliguori | } |
2441 | 63a01ef8 | aliguori | |
2442 | bb9ea79e | aliguori | typedef struct DumpState { |
2443 | bb9ea79e | aliguori | VLANClientState *pcap_vc; |
2444 | bb9ea79e | aliguori | int fd;
|
2445 | bb9ea79e | aliguori | int pcap_caplen;
|
2446 | bb9ea79e | aliguori | } DumpState; |
2447 | bb9ea79e | aliguori | |
2448 | bb9ea79e | aliguori | #define PCAP_MAGIC 0xa1b2c3d4 |
2449 | bb9ea79e | aliguori | |
2450 | bb9ea79e | aliguori | struct pcap_file_hdr {
|
2451 | bb9ea79e | aliguori | uint32_t magic; |
2452 | bb9ea79e | aliguori | uint16_t version_major; |
2453 | bb9ea79e | aliguori | uint16_t version_minor; |
2454 | bb9ea79e | aliguori | int32_t thiszone; |
2455 | bb9ea79e | aliguori | uint32_t sigfigs; |
2456 | bb9ea79e | aliguori | uint32_t snaplen; |
2457 | bb9ea79e | aliguori | uint32_t linktype; |
2458 | bb9ea79e | aliguori | }; |
2459 | bb9ea79e | aliguori | |
2460 | bb9ea79e | aliguori | struct pcap_sf_pkthdr {
|
2461 | bb9ea79e | aliguori | struct {
|
2462 | bb9ea79e | aliguori | int32_t tv_sec; |
2463 | bb9ea79e | aliguori | int32_t tv_usec; |
2464 | bb9ea79e | aliguori | } ts; |
2465 | bb9ea79e | aliguori | uint32_t caplen; |
2466 | bb9ea79e | aliguori | uint32_t len; |
2467 | bb9ea79e | aliguori | }; |
2468 | bb9ea79e | aliguori | |
2469 | 4f1c942b | Mark McLoughlin | static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
2470 | bb9ea79e | aliguori | { |
2471 | e3f5ec2b | Mark McLoughlin | DumpState *s = vc->opaque; |
2472 | bb9ea79e | aliguori | struct pcap_sf_pkthdr hdr;
|
2473 | bb9ea79e | aliguori | int64_t ts; |
2474 | bb9ea79e | aliguori | int caplen;
|
2475 | bb9ea79e | aliguori | |
2476 | bb9ea79e | aliguori | /* Early return in case of previous error. */
|
2477 | bb9ea79e | aliguori | if (s->fd < 0) { |
2478 | 4f1c942b | Mark McLoughlin | return size;
|
2479 | bb9ea79e | aliguori | } |
2480 | bb9ea79e | aliguori | |
2481 | 6ee093c9 | Juan Quintela | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec());
|
2482 | bb9ea79e | aliguori | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
2483 | bb9ea79e | aliguori | |
2484 | 37cb6fc3 | Jan Kiszka | hdr.ts.tv_sec = ts / 1000000;
|
2485 | bb9ea79e | aliguori | hdr.ts.tv_usec = ts % 1000000;
|
2486 | bb9ea79e | aliguori | hdr.caplen = caplen; |
2487 | bb9ea79e | aliguori | hdr.len = size; |
2488 | bb9ea79e | aliguori | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || |
2489 | bb9ea79e | aliguori | write(s->fd, buf, caplen) != caplen) { |
2490 | bb9ea79e | aliguori | qemu_log("-net dump write error - stop dump\n");
|
2491 | bb9ea79e | aliguori | close(s->fd); |
2492 | bb9ea79e | aliguori | s->fd = -1;
|
2493 | bb9ea79e | aliguori | } |
2494 | 4f1c942b | Mark McLoughlin | |
2495 | 4f1c942b | Mark McLoughlin | return size;
|
2496 | bb9ea79e | aliguori | } |
2497 | bb9ea79e | aliguori | |
2498 | bb9ea79e | aliguori | static void net_dump_cleanup(VLANClientState *vc) |
2499 | bb9ea79e | aliguori | { |
2500 | bb9ea79e | aliguori | DumpState *s = vc->opaque; |
2501 | bb9ea79e | aliguori | |
2502 | bb9ea79e | aliguori | close(s->fd); |
2503 | bb9ea79e | aliguori | qemu_free(s); |
2504 | bb9ea79e | aliguori | } |
2505 | bb9ea79e | aliguori | |
2506 | fb12577c | Markus Armbruster | static int net_dump_init(VLANState *vlan, const char *device, |
2507 | bb9ea79e | aliguori | const char *name, const char *filename, int len) |
2508 | bb9ea79e | aliguori | { |
2509 | bb9ea79e | aliguori | struct pcap_file_hdr hdr;
|
2510 | bb9ea79e | aliguori | DumpState *s; |
2511 | bb9ea79e | aliguori | |
2512 | bb9ea79e | aliguori | s = qemu_malloc(sizeof(DumpState));
|
2513 | bb9ea79e | aliguori | |
2514 | 024431b3 | Filip Navara | s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
|
2515 | bb9ea79e | aliguori | if (s->fd < 0) { |
2516 | fb12577c | Markus Armbruster | qemu_error("-net dump: can't open %s\n", filename);
|
2517 | bb9ea79e | aliguori | return -1; |
2518 | bb9ea79e | aliguori | } |
2519 | bb9ea79e | aliguori | |
2520 | bb9ea79e | aliguori | s->pcap_caplen = len; |
2521 | bb9ea79e | aliguori | |
2522 | bb9ea79e | aliguori | hdr.magic = PCAP_MAGIC; |
2523 | bb9ea79e | aliguori | hdr.version_major = 2;
|
2524 | bb9ea79e | aliguori | hdr.version_minor = 4;
|
2525 | bb9ea79e | aliguori | hdr.thiszone = 0;
|
2526 | bb9ea79e | aliguori | hdr.sigfigs = 0;
|
2527 | bb9ea79e | aliguori | hdr.snaplen = s->pcap_caplen; |
2528 | bb9ea79e | aliguori | hdr.linktype = 1;
|
2529 | bb9ea79e | aliguori | |
2530 | bb9ea79e | aliguori | if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { |
2531 | fb12577c | Markus Armbruster | qemu_error("-net dump write error: %s\n", strerror(errno));
|
2532 | bb9ea79e | aliguori | close(s->fd); |
2533 | bb9ea79e | aliguori | qemu_free(s); |
2534 | bb9ea79e | aliguori | return -1; |
2535 | bb9ea79e | aliguori | } |
2536 | bb9ea79e | aliguori | |
2537 | bb6e6364 | Mark McLoughlin | s->pcap_vc = qemu_new_vlan_client(NET_CLIENT_TYPE_DUMP, |
2538 | bb6e6364 | Mark McLoughlin | vlan, NULL, device, name, NULL, |
2539 | 70783b9c | Mark McLoughlin | dump_receive, NULL, NULL, |
2540 | bb9ea79e | aliguori | net_dump_cleanup, s); |
2541 | bb9ea79e | aliguori | snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
|
2542 | bb9ea79e | aliguori | "dump to %s (len=%d)", filename, len);
|
2543 | bb9ea79e | aliguori | return 0; |
2544 | bb9ea79e | aliguori | } |
2545 | bb9ea79e | aliguori | |
2546 | 63a01ef8 | aliguori | /* find or alloc a new VLAN */
|
2547 | 1a609520 | Jan Kiszka | VLANState *qemu_find_vlan(int id, int allocate) |
2548 | 63a01ef8 | aliguori | { |
2549 | 5610c3aa | Mark McLoughlin | VLANState *vlan; |
2550 | 5610c3aa | Mark McLoughlin | |
2551 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
2552 | 5610c3aa | Mark McLoughlin | if (vlan->id == id) {
|
2553 | 63a01ef8 | aliguori | return vlan;
|
2554 | 5610c3aa | Mark McLoughlin | } |
2555 | 63a01ef8 | aliguori | } |
2556 | 5610c3aa | Mark McLoughlin | |
2557 | 1a609520 | Jan Kiszka | if (!allocate) {
|
2558 | 1a609520 | Jan Kiszka | return NULL; |
2559 | 1a609520 | Jan Kiszka | } |
2560 | 5610c3aa | Mark McLoughlin | |
2561 | 63a01ef8 | aliguori | vlan = qemu_mallocz(sizeof(VLANState));
|
2562 | 63a01ef8 | aliguori | vlan->id = id; |
2563 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlan->clients); |
2564 | f7105843 | Mark McLoughlin | |
2565 | f7105843 | Mark McLoughlin | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
2566 | f7105843 | Mark McLoughlin | qemu_vlan_deliver_packet_iov, |
2567 | f7105843 | Mark McLoughlin | vlan); |
2568 | 5610c3aa | Mark McLoughlin | |
2569 | 5610c3aa | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
2570 | 5610c3aa | Mark McLoughlin | |
2571 | 63a01ef8 | aliguori | return vlan;
|
2572 | 63a01ef8 | aliguori | } |
2573 | 63a01ef8 | aliguori | |
2574 | 2ef924b4 | Gerd Hoffmann | VLANClientState *qemu_find_netdev(const char *id) |
2575 | 5869c4d5 | Mark McLoughlin | { |
2576 | 5869c4d5 | Mark McLoughlin | VLANClientState *vc; |
2577 | 5869c4d5 | Mark McLoughlin | |
2578 | 5869c4d5 | Mark McLoughlin | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
2579 | 5869c4d5 | Mark McLoughlin | if (!strcmp(vc->name, id)) {
|
2580 | 5869c4d5 | Mark McLoughlin | return vc;
|
2581 | 5869c4d5 | Mark McLoughlin | } |
2582 | 5869c4d5 | Mark McLoughlin | } |
2583 | 5869c4d5 | Mark McLoughlin | |
2584 | 5869c4d5 | Mark McLoughlin | return NULL; |
2585 | 5869c4d5 | Mark McLoughlin | } |
2586 | 5869c4d5 | Mark McLoughlin | |
2587 | 7697079b | aliguori | static int nic_get_free_idx(void) |
2588 | 7697079b | aliguori | { |
2589 | 7697079b | aliguori | int index;
|
2590 | 7697079b | aliguori | |
2591 | 7697079b | aliguori | for (index = 0; index < MAX_NICS; index++) |
2592 | 7697079b | aliguori | if (!nd_table[index].used)
|
2593 | 7697079b | aliguori | return index;
|
2594 | 7697079b | aliguori | return -1; |
2595 | 7697079b | aliguori | } |
2596 | 7697079b | aliguori | |
2597 | 07caea31 | Markus Armbruster | int qemu_show_nic_models(const char *arg, const char *const *models) |
2598 | 07caea31 | Markus Armbruster | { |
2599 | 07caea31 | Markus Armbruster | int i;
|
2600 | 07caea31 | Markus Armbruster | |
2601 | 07caea31 | Markus Armbruster | if (!arg || strcmp(arg, "?")) |
2602 | 07caea31 | Markus Armbruster | return 0; |
2603 | 07caea31 | Markus Armbruster | |
2604 | 07caea31 | Markus Armbruster | fprintf(stderr, "qemu: Supported NIC models: ");
|
2605 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) |
2606 | 07caea31 | Markus Armbruster | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
2607 | 07caea31 | Markus Armbruster | return 1; |
2608 | 07caea31 | Markus Armbruster | } |
2609 | 07caea31 | Markus Armbruster | |
2610 | d07f22c5 | aliguori | void qemu_check_nic_model(NICInfo *nd, const char *model) |
2611 | d07f22c5 | aliguori | { |
2612 | d07f22c5 | aliguori | const char *models[2]; |
2613 | d07f22c5 | aliguori | |
2614 | d07f22c5 | aliguori | models[0] = model;
|
2615 | d07f22c5 | aliguori | models[1] = NULL; |
2616 | d07f22c5 | aliguori | |
2617 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, models))
|
2618 | 07caea31 | Markus Armbruster | exit(0);
|
2619 | 07caea31 | Markus Armbruster | if (qemu_find_nic_model(nd, models, model) < 0) |
2620 | 07caea31 | Markus Armbruster | exit(1);
|
2621 | d07f22c5 | aliguori | } |
2622 | d07f22c5 | aliguori | |
2623 | 07caea31 | Markus Armbruster | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
2624 | 07caea31 | Markus Armbruster | const char *default_model) |
2625 | d07f22c5 | aliguori | { |
2626 | 07caea31 | Markus Armbruster | int i;
|
2627 | d07f22c5 | aliguori | |
2628 | d07f22c5 | aliguori | if (!nd->model)
|
2629 | 32a8e14a | Mark McLoughlin | nd->model = qemu_strdup(default_model); |
2630 | d07f22c5 | aliguori | |
2631 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) { |
2632 | 07caea31 | Markus Armbruster | if (strcmp(nd->model, models[i]) == 0) |
2633 | 07caea31 | Markus Armbruster | return i;
|
2634 | d07f22c5 | aliguori | } |
2635 | d07f22c5 | aliguori | |
2636 | 07caea31 | Markus Armbruster | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
|
2637 | 07caea31 | Markus Armbruster | return -1; |
2638 | d07f22c5 | aliguori | } |
2639 | d07f22c5 | aliguori | |
2640 | c1d6eed7 | Mark McLoughlin | static int net_handle_fd_param(Monitor *mon, const char *param) |
2641 | c1d6eed7 | Mark McLoughlin | { |
2642 | c1d6eed7 | Mark McLoughlin | if (!qemu_isdigit(param[0])) { |
2643 | c1d6eed7 | Mark McLoughlin | int fd;
|
2644 | c1d6eed7 | Mark McLoughlin | |
2645 | c1d6eed7 | Mark McLoughlin | fd = monitor_get_fd(mon, param); |
2646 | c1d6eed7 | Mark McLoughlin | if (fd == -1) { |
2647 | fb12577c | Markus Armbruster | qemu_error("No file descriptor named %s found", param);
|
2648 | c1d6eed7 | Mark McLoughlin | return -1; |
2649 | c1d6eed7 | Mark McLoughlin | } |
2650 | c1d6eed7 | Mark McLoughlin | |
2651 | c1d6eed7 | Mark McLoughlin | return fd;
|
2652 | c1d6eed7 | Mark McLoughlin | } else {
|
2653 | c1d6eed7 | Mark McLoughlin | return strtol(param, NULL, 0); |
2654 | c1d6eed7 | Mark McLoughlin | } |
2655 | c1d6eed7 | Mark McLoughlin | } |
2656 | c1d6eed7 | Mark McLoughlin | |
2657 | f6b134ac | Mark McLoughlin | static int net_init_nic(QemuOpts *opts, |
2658 | f6b134ac | Mark McLoughlin | Monitor *mon, |
2659 | f6b134ac | Mark McLoughlin | const char *name, |
2660 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
2661 | f83c6e10 | Mark McLoughlin | { |
2662 | f83c6e10 | Mark McLoughlin | int idx;
|
2663 | f83c6e10 | Mark McLoughlin | NICInfo *nd; |
2664 | 5869c4d5 | Mark McLoughlin | const char *netdev; |
2665 | f83c6e10 | Mark McLoughlin | |
2666 | f83c6e10 | Mark McLoughlin | idx = nic_get_free_idx(); |
2667 | f83c6e10 | Mark McLoughlin | if (idx == -1 || nb_nics >= MAX_NICS) { |
2668 | f83c6e10 | Mark McLoughlin | qemu_error("Too Many NICs\n");
|
2669 | f83c6e10 | Mark McLoughlin | return -1; |
2670 | f83c6e10 | Mark McLoughlin | } |
2671 | f83c6e10 | Mark McLoughlin | |
2672 | f83c6e10 | Mark McLoughlin | nd = &nd_table[idx]; |
2673 | f83c6e10 | Mark McLoughlin | |
2674 | f83c6e10 | Mark McLoughlin | memset(nd, 0, sizeof(*nd)); |
2675 | f83c6e10 | Mark McLoughlin | |
2676 | 5869c4d5 | Mark McLoughlin | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
2677 | 5869c4d5 | Mark McLoughlin | nd->netdev = qemu_find_netdev(netdev); |
2678 | 5869c4d5 | Mark McLoughlin | if (!nd->netdev) {
|
2679 | 5869c4d5 | Mark McLoughlin | qemu_error("netdev '%s' not found\n", netdev);
|
2680 | 5869c4d5 | Mark McLoughlin | return -1; |
2681 | 5869c4d5 | Mark McLoughlin | } |
2682 | 5869c4d5 | Mark McLoughlin | } else {
|
2683 | 5869c4d5 | Mark McLoughlin | assert(vlan); |
2684 | 5869c4d5 | Mark McLoughlin | nd->vlan = vlan; |
2685 | 5869c4d5 | Mark McLoughlin | } |
2686 | 6d952ebe | Mark McLoughlin | if (name) {
|
2687 | 6d952ebe | Mark McLoughlin | nd->name = qemu_strdup(name); |
2688 | 6d952ebe | Mark McLoughlin | } |
2689 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "model")) { |
2690 | f83c6e10 | Mark McLoughlin | nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
|
2691 | f83c6e10 | Mark McLoughlin | } |
2692 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "addr")) { |
2693 | f83c6e10 | Mark McLoughlin | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
|
2694 | f83c6e10 | Mark McLoughlin | } |
2695 | f83c6e10 | Mark McLoughlin | |
2696 | f83c6e10 | Mark McLoughlin | nd->macaddr[0] = 0x52; |
2697 | f83c6e10 | Mark McLoughlin | nd->macaddr[1] = 0x54; |
2698 | f83c6e10 | Mark McLoughlin | nd->macaddr[2] = 0x00; |
2699 | f83c6e10 | Mark McLoughlin | nd->macaddr[3] = 0x12; |
2700 | f83c6e10 | Mark McLoughlin | nd->macaddr[4] = 0x34; |
2701 | f83c6e10 | Mark McLoughlin | nd->macaddr[5] = 0x56 + idx; |
2702 | f83c6e10 | Mark McLoughlin | |
2703 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "macaddr") && |
2704 | f83c6e10 | Mark McLoughlin | parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
2705 | f83c6e10 | Mark McLoughlin | qemu_error("invalid syntax for ethernet address\n");
|
2706 | f83c6e10 | Mark McLoughlin | return -1; |
2707 | f83c6e10 | Mark McLoughlin | } |
2708 | f83c6e10 | Mark McLoughlin | |
2709 | f83c6e10 | Mark McLoughlin | nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
|
2710 | f83c6e10 | Mark McLoughlin | if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
|
2711 | f83c6e10 | Mark McLoughlin | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
2712 | f83c6e10 | Mark McLoughlin | qemu_error("invalid # of vectors: %d\n", nd->nvectors);
|
2713 | f83c6e10 | Mark McLoughlin | return -1; |
2714 | f83c6e10 | Mark McLoughlin | } |
2715 | f83c6e10 | Mark McLoughlin | |
2716 | f83c6e10 | Mark McLoughlin | nd->used = 1;
|
2717 | 5869c4d5 | Mark McLoughlin | if (vlan) {
|
2718 | 5869c4d5 | Mark McLoughlin | nd->vlan->nb_guest_devs++; |
2719 | 5869c4d5 | Mark McLoughlin | } |
2720 | f83c6e10 | Mark McLoughlin | nb_nics++; |
2721 | f83c6e10 | Mark McLoughlin | |
2722 | f83c6e10 | Mark McLoughlin | return idx;
|
2723 | f83c6e10 | Mark McLoughlin | } |
2724 | f83c6e10 | Mark McLoughlin | |
2725 | a3a766e7 | Juan Quintela | #if defined(CONFIG_SLIRP)
|
2726 | ec302ffd | Mark McLoughlin | static int net_init_slirp_configs(const char *name, const char *value, void *opaque) |
2727 | ec302ffd | Mark McLoughlin | { |
2728 | ec302ffd | Mark McLoughlin | struct slirp_config_str *config;
|
2729 | ec302ffd | Mark McLoughlin | |
2730 | ec302ffd | Mark McLoughlin | if (strcmp(name, "hostfwd") != 0 && strcmp(name, "guestfwd") != 0) { |
2731 | ec302ffd | Mark McLoughlin | return 0; |
2732 | ec302ffd | Mark McLoughlin | } |
2733 | ec302ffd | Mark McLoughlin | |
2734 | ec302ffd | Mark McLoughlin | config = qemu_mallocz(sizeof(*config));
|
2735 | ec302ffd | Mark McLoughlin | |
2736 | ec302ffd | Mark McLoughlin | pstrcpy(config->str, sizeof(config->str), value);
|
2737 | ec302ffd | Mark McLoughlin | |
2738 | ec302ffd | Mark McLoughlin | if (!strcmp(name, "hostfwd")) { |
2739 | ec302ffd | Mark McLoughlin | config->flags = SLIRP_CFG_HOSTFWD; |
2740 | ec302ffd | Mark McLoughlin | } |
2741 | ec302ffd | Mark McLoughlin | |
2742 | ec302ffd | Mark McLoughlin | config->next = slirp_configs; |
2743 | ec302ffd | Mark McLoughlin | slirp_configs = config; |
2744 | ec302ffd | Mark McLoughlin | |
2745 | ec302ffd | Mark McLoughlin | return 0; |
2746 | ec302ffd | Mark McLoughlin | } |
2747 | ec302ffd | Mark McLoughlin | |
2748 | f6b134ac | Mark McLoughlin | static int net_init_slirp(QemuOpts *opts, |
2749 | f6b134ac | Mark McLoughlin | Monitor *mon, |
2750 | f6b134ac | Mark McLoughlin | const char *name, |
2751 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
2752 | ec302ffd | Mark McLoughlin | { |
2753 | ec302ffd | Mark McLoughlin | struct slirp_config_str *config;
|
2754 | ec302ffd | Mark McLoughlin | const char *vhost; |
2755 | ec302ffd | Mark McLoughlin | const char *vhostname; |
2756 | ec302ffd | Mark McLoughlin | const char *vdhcp_start; |
2757 | ec302ffd | Mark McLoughlin | const char *vnamesrv; |
2758 | ec302ffd | Mark McLoughlin | const char *tftp_export; |
2759 | ec302ffd | Mark McLoughlin | const char *bootfile; |
2760 | ec302ffd | Mark McLoughlin | const char *smb_export; |
2761 | ec302ffd | Mark McLoughlin | const char *vsmbsrv; |
2762 | ec302ffd | Mark McLoughlin | char *vnet = NULL; |
2763 | ec302ffd | Mark McLoughlin | int restricted = 0; |
2764 | ec302ffd | Mark McLoughlin | int ret;
|
2765 | ec302ffd | Mark McLoughlin | |
2766 | ec302ffd | Mark McLoughlin | vhost = qemu_opt_get(opts, "host");
|
2767 | ec302ffd | Mark McLoughlin | vhostname = qemu_opt_get(opts, "hostname");
|
2768 | ec302ffd | Mark McLoughlin | vdhcp_start = qemu_opt_get(opts, "dhcpstart");
|
2769 | ec302ffd | Mark McLoughlin | vnamesrv = qemu_opt_get(opts, "dns");
|
2770 | ec302ffd | Mark McLoughlin | tftp_export = qemu_opt_get(opts, "tftp");
|
2771 | ec302ffd | Mark McLoughlin | bootfile = qemu_opt_get(opts, "bootfile");
|
2772 | ec302ffd | Mark McLoughlin | smb_export = qemu_opt_get(opts, "smb");
|
2773 | ec302ffd | Mark McLoughlin | vsmbsrv = qemu_opt_get(opts, "smbserver");
|
2774 | ec302ffd | Mark McLoughlin | |
2775 | ec302ffd | Mark McLoughlin | if (qemu_opt_get(opts, "ip")) { |
2776 | ec302ffd | Mark McLoughlin | const char *ip = qemu_opt_get(opts, "ip"); |
2777 | ec302ffd | Mark McLoughlin | int l = strlen(ip) + strlen("/24") + 1; |
2778 | ec302ffd | Mark McLoughlin | |
2779 | ec302ffd | Mark McLoughlin | vnet = qemu_malloc(l); |
2780 | ec302ffd | Mark McLoughlin | |
2781 | ec302ffd | Mark McLoughlin | /* emulate legacy ip= parameter */
|
2782 | ec302ffd | Mark McLoughlin | pstrcpy(vnet, l, ip); |
2783 | ec302ffd | Mark McLoughlin | pstrcat(vnet, l, "/24");
|
2784 | ec302ffd | Mark McLoughlin | } |
2785 | ec302ffd | Mark McLoughlin | |
2786 | ec302ffd | Mark McLoughlin | if (qemu_opt_get(opts, "net")) { |
2787 | ec302ffd | Mark McLoughlin | if (vnet) {
|
2788 | ec302ffd | Mark McLoughlin | qemu_free(vnet); |
2789 | ec302ffd | Mark McLoughlin | } |
2790 | ec302ffd | Mark McLoughlin | vnet = qemu_strdup(qemu_opt_get(opts, "net"));
|
2791 | ec302ffd | Mark McLoughlin | } |
2792 | ec302ffd | Mark McLoughlin | |
2793 | ec302ffd | Mark McLoughlin | if (qemu_opt_get(opts, "restrict") && |
2794 | ec302ffd | Mark McLoughlin | qemu_opt_get(opts, "restrict")[0] == 'y') { |
2795 | ec302ffd | Mark McLoughlin | restricted = 1;
|
2796 | ec302ffd | Mark McLoughlin | } |
2797 | ec302ffd | Mark McLoughlin | |
2798 | ec302ffd | Mark McLoughlin | qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0); |
2799 | ec302ffd | Mark McLoughlin | |
2800 | ec302ffd | Mark McLoughlin | ret = net_slirp_init(vlan, "user", name, restricted, vnet, vhost,
|
2801 | ec302ffd | Mark McLoughlin | vhostname, tftp_export, bootfile, vdhcp_start, |
2802 | ec302ffd | Mark McLoughlin | vnamesrv, smb_export, vsmbsrv); |
2803 | ec302ffd | Mark McLoughlin | |
2804 | ec302ffd | Mark McLoughlin | while (slirp_configs) {
|
2805 | ec302ffd | Mark McLoughlin | config = slirp_configs; |
2806 | ec302ffd | Mark McLoughlin | slirp_configs = config->next; |
2807 | ec302ffd | Mark McLoughlin | qemu_free(config); |
2808 | ec302ffd | Mark McLoughlin | } |
2809 | ec302ffd | Mark McLoughlin | |
2810 | f6b134ac | Mark McLoughlin | if (ret != -1 && vlan) { |
2811 | ec302ffd | Mark McLoughlin | vlan->nb_host_devs++; |
2812 | ec302ffd | Mark McLoughlin | } |
2813 | ec302ffd | Mark McLoughlin | |
2814 | ec302ffd | Mark McLoughlin | qemu_free(vnet); |
2815 | ec302ffd | Mark McLoughlin | |
2816 | ec302ffd | Mark McLoughlin | return ret;
|
2817 | ec302ffd | Mark McLoughlin | } |
2818 | a3a766e7 | Juan Quintela | #endif /* CONFIG_SLIRP */ |
2819 | ec302ffd | Mark McLoughlin | |
2820 | 8a1c5235 | Mark McLoughlin | #ifdef _WIN32
|
2821 | f6b134ac | Mark McLoughlin | static int net_init_tap_win32(QemuOpts *opts, |
2822 | f6b134ac | Mark McLoughlin | Monitor *mon, |
2823 | f6b134ac | Mark McLoughlin | const char *name, |
2824 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
2825 | 8a1c5235 | Mark McLoughlin | { |
2826 | 8a1c5235 | Mark McLoughlin | const char *ifname; |
2827 | 8a1c5235 | Mark McLoughlin | |
2828 | 8a1c5235 | Mark McLoughlin | ifname = qemu_opt_get(opts, "ifname");
|
2829 | 8a1c5235 | Mark McLoughlin | |
2830 | 8a1c5235 | Mark McLoughlin | if (!ifname) {
|
2831 | 8a1c5235 | Mark McLoughlin | qemu_error("tap: no interface name\n");
|
2832 | 8a1c5235 | Mark McLoughlin | return -1; |
2833 | 8a1c5235 | Mark McLoughlin | } |
2834 | 8a1c5235 | Mark McLoughlin | |
2835 | 8a1c5235 | Mark McLoughlin | if (tap_win32_init(vlan, "tap", name, ifname) == -1) { |
2836 | 8a1c5235 | Mark McLoughlin | return -1; |
2837 | 8a1c5235 | Mark McLoughlin | } |
2838 | 8a1c5235 | Mark McLoughlin | |
2839 | f6b134ac | Mark McLoughlin | if (vlan) {
|
2840 | f6b134ac | Mark McLoughlin | vlan->nb_host_devs++; |
2841 | f6b134ac | Mark McLoughlin | } |
2842 | 8a1c5235 | Mark McLoughlin | |
2843 | 8a1c5235 | Mark McLoughlin | return 0; |
2844 | 8a1c5235 | Mark McLoughlin | } |
2845 | 8a1c5235 | Mark McLoughlin | #elif !defined(_AIX)
|
2846 | f6b134ac | Mark McLoughlin | static int net_init_tap(QemuOpts *opts, |
2847 | f6b134ac | Mark McLoughlin | Monitor *mon, |
2848 | f6b134ac | Mark McLoughlin | const char *name, |
2849 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
2850 | 8a1c5235 | Mark McLoughlin | { |
2851 | 8a1c5235 | Mark McLoughlin | TAPState *s; |
2852 | 424a7f96 | Mark McLoughlin | int fd, vnet_hdr;
|
2853 | 8a1c5235 | Mark McLoughlin | |
2854 | 8a1c5235 | Mark McLoughlin | if (qemu_opt_get(opts, "fd")) { |
2855 | 8a1c5235 | Mark McLoughlin | if (qemu_opt_get(opts, "ifname") || |
2856 | 8a1c5235 | Mark McLoughlin | qemu_opt_get(opts, "script") ||
|
2857 | baf74c95 | Mark McLoughlin | qemu_opt_get(opts, "downscript") ||
|
2858 | baf74c95 | Mark McLoughlin | qemu_opt_get(opts, "vnet_hdr")) {
|
2859 | baf74c95 | Mark McLoughlin | qemu_error("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=\n");
|
2860 | 8a1c5235 | Mark McLoughlin | return -1; |
2861 | 8a1c5235 | Mark McLoughlin | } |
2862 | 8a1c5235 | Mark McLoughlin | |
2863 | 8a1c5235 | Mark McLoughlin | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
|
2864 | 8a1c5235 | Mark McLoughlin | if (fd == -1) { |
2865 | 8a1c5235 | Mark McLoughlin | return -1; |
2866 | 8a1c5235 | Mark McLoughlin | } |
2867 | 8a1c5235 | Mark McLoughlin | |
2868 | 8a1c5235 | Mark McLoughlin | fcntl(fd, F_SETFL, O_NONBLOCK); |
2869 | 8a1c5235 | Mark McLoughlin | |
2870 | 424a7f96 | Mark McLoughlin | vnet_hdr = tap_probe_vnet_hdr(fd); |
2871 | 8a1c5235 | Mark McLoughlin | } else {
|
2872 | 424a7f96 | Mark McLoughlin | if (!qemu_opt_get(opts, "script")) { |
2873 | 424a7f96 | Mark McLoughlin | qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
|
2874 | 8a1c5235 | Mark McLoughlin | } |
2875 | 424a7f96 | Mark McLoughlin | |
2876 | 424a7f96 | Mark McLoughlin | if (!qemu_opt_get(opts, "downscript")) { |
2877 | 424a7f96 | Mark McLoughlin | qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
|
2878 | 8a1c5235 | Mark McLoughlin | } |
2879 | 8a1c5235 | Mark McLoughlin | |
2880 | 424a7f96 | Mark McLoughlin | fd = net_tap_init(opts, &vnet_hdr); |
2881 | 8a1c5235 | Mark McLoughlin | } |
2882 | 8a1c5235 | Mark McLoughlin | |
2883 | 424a7f96 | Mark McLoughlin | s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
|
2884 | 8a1c5235 | Mark McLoughlin | if (!s) {
|
2885 | 424a7f96 | Mark McLoughlin | close(fd); |
2886 | 8a1c5235 | Mark McLoughlin | return -1; |
2887 | 8a1c5235 | Mark McLoughlin | } |
2888 | 8a1c5235 | Mark McLoughlin | |
2889 | 8a1c5235 | Mark McLoughlin | if (tap_set_sndbuf(s, opts) < 0) { |
2890 | 8a1c5235 | Mark McLoughlin | return -1; |
2891 | 8a1c5235 | Mark McLoughlin | } |
2892 | 8a1c5235 | Mark McLoughlin | |
2893 | 424a7f96 | Mark McLoughlin | if (qemu_opt_get(opts, "fd")) { |
2894 | 424a7f96 | Mark McLoughlin | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); |
2895 | 424a7f96 | Mark McLoughlin | } else {
|
2896 | 424a7f96 | Mark McLoughlin | const char *ifname, *script, *downscript; |
2897 | 424a7f96 | Mark McLoughlin | |
2898 | 424a7f96 | Mark McLoughlin | ifname = qemu_opt_get(opts, "ifname");
|
2899 | 424a7f96 | Mark McLoughlin | script = qemu_opt_get(opts, "script");
|
2900 | 424a7f96 | Mark McLoughlin | downscript = qemu_opt_get(opts, "downscript");
|
2901 | 424a7f96 | Mark McLoughlin | |
2902 | 424a7f96 | Mark McLoughlin | snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
2903 | 424a7f96 | Mark McLoughlin | "ifname=%s,script=%s,downscript=%s",
|
2904 | 424a7f96 | Mark McLoughlin | ifname, script, downscript); |
2905 | 424a7f96 | Mark McLoughlin | |
2906 | 424a7f96 | Mark McLoughlin | if (strcmp(downscript, "no") != 0) { |
2907 | 424a7f96 | Mark McLoughlin | snprintf(s->down_script, sizeof(s->down_script), "%s", downscript); |
2908 | 424a7f96 | Mark McLoughlin | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); |
2909 | 424a7f96 | Mark McLoughlin | } |
2910 | 424a7f96 | Mark McLoughlin | } |
2911 | 424a7f96 | Mark McLoughlin | |
2912 | f6b134ac | Mark McLoughlin | if (vlan) {
|
2913 | f6b134ac | Mark McLoughlin | vlan->nb_host_devs++; |
2914 | f6b134ac | Mark McLoughlin | } |
2915 | 8a1c5235 | Mark McLoughlin | |
2916 | 8a1c5235 | Mark McLoughlin | return 0; |
2917 | 8a1c5235 | Mark McLoughlin | } |
2918 | 8a1c5235 | Mark McLoughlin | #endif
|
2919 | 8a1c5235 | Mark McLoughlin | |
2920 | f6b134ac | Mark McLoughlin | static int net_init_socket(QemuOpts *opts, |
2921 | f6b134ac | Mark McLoughlin | Monitor *mon, |
2922 | f6b134ac | Mark McLoughlin | const char *name, |
2923 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
2924 | 88ce16ca | Mark McLoughlin | { |
2925 | 88ce16ca | Mark McLoughlin | if (qemu_opt_get(opts, "fd")) { |
2926 | 88ce16ca | Mark McLoughlin | int fd;
|
2927 | 88ce16ca | Mark McLoughlin | |
2928 | 88ce16ca | Mark McLoughlin | if (qemu_opt_get(opts, "listen") || |
2929 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "connect") ||
|
2930 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "mcast")) {
|
2931 | 88ce16ca | Mark McLoughlin | qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
|
2932 | 88ce16ca | Mark McLoughlin | return -1; |
2933 | 88ce16ca | Mark McLoughlin | } |
2934 | 88ce16ca | Mark McLoughlin | |
2935 | 88ce16ca | Mark McLoughlin | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
|
2936 | 88ce16ca | Mark McLoughlin | if (fd == -1) { |
2937 | 88ce16ca | Mark McLoughlin | return -1; |
2938 | 88ce16ca | Mark McLoughlin | } |
2939 | 88ce16ca | Mark McLoughlin | |
2940 | 88ce16ca | Mark McLoughlin | if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { |
2941 | 88ce16ca | Mark McLoughlin | close(fd); |
2942 | 88ce16ca | Mark McLoughlin | return -1; |
2943 | 88ce16ca | Mark McLoughlin | } |
2944 | 88ce16ca | Mark McLoughlin | } else if (qemu_opt_get(opts, "listen")) { |
2945 | 88ce16ca | Mark McLoughlin | const char *listen; |
2946 | 88ce16ca | Mark McLoughlin | |
2947 | 88ce16ca | Mark McLoughlin | if (qemu_opt_get(opts, "fd") || |
2948 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "connect") ||
|
2949 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "mcast")) {
|
2950 | 88ce16ca | Mark McLoughlin | qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
|
2951 | 88ce16ca | Mark McLoughlin | return -1; |
2952 | 88ce16ca | Mark McLoughlin | } |
2953 | 88ce16ca | Mark McLoughlin | |
2954 | 88ce16ca | Mark McLoughlin | listen = qemu_opt_get(opts, "listen");
|
2955 | 88ce16ca | Mark McLoughlin | |
2956 | 88ce16ca | Mark McLoughlin | if (net_socket_listen_init(vlan, "socket", name, listen) == -1) { |
2957 | 88ce16ca | Mark McLoughlin | return -1; |
2958 | 88ce16ca | Mark McLoughlin | } |
2959 | 88ce16ca | Mark McLoughlin | } else if (qemu_opt_get(opts, "connect")) { |
2960 | 88ce16ca | Mark McLoughlin | const char *connect; |
2961 | 88ce16ca | Mark McLoughlin | |
2962 | 88ce16ca | Mark McLoughlin | if (qemu_opt_get(opts, "fd") || |
2963 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "listen") ||
|
2964 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "mcast")) {
|
2965 | 88ce16ca | Mark McLoughlin | qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
|
2966 | 88ce16ca | Mark McLoughlin | return -1; |
2967 | 88ce16ca | Mark McLoughlin | } |
2968 | 88ce16ca | Mark McLoughlin | |
2969 | 88ce16ca | Mark McLoughlin | connect = qemu_opt_get(opts, "connect");
|
2970 | 88ce16ca | Mark McLoughlin | |
2971 | 88ce16ca | Mark McLoughlin | if (net_socket_connect_init(vlan, "socket", name, connect) == -1) { |
2972 | 88ce16ca | Mark McLoughlin | return -1; |
2973 | 88ce16ca | Mark McLoughlin | } |
2974 | 88ce16ca | Mark McLoughlin | } else if (qemu_opt_get(opts, "mcast")) { |
2975 | 88ce16ca | Mark McLoughlin | const char *mcast; |
2976 | 88ce16ca | Mark McLoughlin | |
2977 | 88ce16ca | Mark McLoughlin | if (qemu_opt_get(opts, "fd") || |
2978 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "connect") ||
|
2979 | 88ce16ca | Mark McLoughlin | qemu_opt_get(opts, "listen")) {
|
2980 | 88ce16ca | Mark McLoughlin | qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
|
2981 | 88ce16ca | Mark McLoughlin | return -1; |
2982 | 88ce16ca | Mark McLoughlin | } |
2983 | 88ce16ca | Mark McLoughlin | |
2984 | 88ce16ca | Mark McLoughlin | mcast = qemu_opt_get(opts, "mcast");
|
2985 | 88ce16ca | Mark McLoughlin | |
2986 | 88ce16ca | Mark McLoughlin | if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) { |
2987 | 88ce16ca | Mark McLoughlin | return -1; |
2988 | 88ce16ca | Mark McLoughlin | } |
2989 | 88ce16ca | Mark McLoughlin | } else {
|
2990 | 88ce16ca | Mark McLoughlin | qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
|
2991 | 88ce16ca | Mark McLoughlin | return -1; |
2992 | 88ce16ca | Mark McLoughlin | } |
2993 | 88ce16ca | Mark McLoughlin | |
2994 | f6b134ac | Mark McLoughlin | if (vlan) {
|
2995 | f6b134ac | Mark McLoughlin | vlan->nb_host_devs++; |
2996 | f6b134ac | Mark McLoughlin | } |
2997 | 88ce16ca | Mark McLoughlin | |
2998 | 88ce16ca | Mark McLoughlin | return 0; |
2999 | 88ce16ca | Mark McLoughlin | } |
3000 | 88ce16ca | Mark McLoughlin | |
3001 | dd51058d | Mark McLoughlin | #ifdef CONFIG_VDE
|
3002 | f6b134ac | Mark McLoughlin | static int net_init_vde(QemuOpts *opts, |
3003 | f6b134ac | Mark McLoughlin | Monitor *mon, |
3004 | f6b134ac | Mark McLoughlin | const char *name, |
3005 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
3006 | dd51058d | Mark McLoughlin | { |
3007 | dd51058d | Mark McLoughlin | const char *sock; |
3008 | dd51058d | Mark McLoughlin | const char *group; |
3009 | dd51058d | Mark McLoughlin | int port, mode;
|
3010 | dd51058d | Mark McLoughlin | |
3011 | dd51058d | Mark McLoughlin | sock = qemu_opt_get(opts, "sock");
|
3012 | dd51058d | Mark McLoughlin | group = qemu_opt_get(opts, "group");
|
3013 | dd51058d | Mark McLoughlin | |
3014 | dd51058d | Mark McLoughlin | port = qemu_opt_get_number(opts, "port", 0); |
3015 | dd51058d | Mark McLoughlin | mode = qemu_opt_get_number(opts, "mode", 0700); |
3016 | dd51058d | Mark McLoughlin | |
3017 | dd51058d | Mark McLoughlin | if (net_vde_init(vlan, "vde", name, sock, port, group, mode) == -1) { |
3018 | dd51058d | Mark McLoughlin | return -1; |
3019 | dd51058d | Mark McLoughlin | } |
3020 | dd51058d | Mark McLoughlin | |
3021 | f6b134ac | Mark McLoughlin | if (vlan) {
|
3022 | f6b134ac | Mark McLoughlin | vlan->nb_host_devs++; |
3023 | f6b134ac | Mark McLoughlin | } |
3024 | dd51058d | Mark McLoughlin | |
3025 | dd51058d | Mark McLoughlin | return 0; |
3026 | dd51058d | Mark McLoughlin | } |
3027 | dd51058d | Mark McLoughlin | #endif
|
3028 | dd51058d | Mark McLoughlin | |
3029 | f6b134ac | Mark McLoughlin | static int net_init_dump(QemuOpts *opts, |
3030 | f6b134ac | Mark McLoughlin | Monitor *mon, |
3031 | f6b134ac | Mark McLoughlin | const char *name, |
3032 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
3033 | ed2955c2 | Mark McLoughlin | { |
3034 | ed2955c2 | Mark McLoughlin | int len;
|
3035 | ed2955c2 | Mark McLoughlin | const char *file; |
3036 | ed2955c2 | Mark McLoughlin | char def_file[128]; |
3037 | ed2955c2 | Mark McLoughlin | |
3038 | f6b134ac | Mark McLoughlin | assert(vlan); |
3039 | ed2955c2 | Mark McLoughlin | |
3040 | ed2955c2 | Mark McLoughlin | file = qemu_opt_get(opts, "file");
|
3041 | ed2955c2 | Mark McLoughlin | if (!file) {
|
3042 | ed2955c2 | Mark McLoughlin | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); |
3043 | ed2955c2 | Mark McLoughlin | file = def_file; |
3044 | ed2955c2 | Mark McLoughlin | } |
3045 | ed2955c2 | Mark McLoughlin | |
3046 | ed2955c2 | Mark McLoughlin | len = qemu_opt_get_size(opts, "len", 65536); |
3047 | ed2955c2 | Mark McLoughlin | |
3048 | ed2955c2 | Mark McLoughlin | return net_dump_init(vlan, "dump", name, file, len); |
3049 | ed2955c2 | Mark McLoughlin | } |
3050 | ed2955c2 | Mark McLoughlin | |
3051 | f83c6e10 | Mark McLoughlin | #define NET_COMMON_PARAMS_DESC \
|
3052 | f83c6e10 | Mark McLoughlin | { \ |
3053 | f83c6e10 | Mark McLoughlin | .name = "type", \
|
3054 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
3055 | f83c6e10 | Mark McLoughlin | .help = "net client type (nic, tap etc.)", \
|
3056 | f83c6e10 | Mark McLoughlin | }, { \ |
3057 | f83c6e10 | Mark McLoughlin | .name = "vlan", \
|
3058 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, \ |
3059 | f83c6e10 | Mark McLoughlin | .help = "vlan number", \
|
3060 | f83c6e10 | Mark McLoughlin | }, { \ |
3061 | f83c6e10 | Mark McLoughlin | .name = "name", \
|
3062 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
3063 | f83c6e10 | Mark McLoughlin | .help = "identifier for monitor commands", \
|
3064 | f83c6e10 | Mark McLoughlin | } |
3065 | f83c6e10 | Mark McLoughlin | |
3066 | 6d952ebe | Mark McLoughlin | typedef int (*net_client_init_func)(QemuOpts *opts, |
3067 | 6d952ebe | Mark McLoughlin | Monitor *mon, |
3068 | f6b134ac | Mark McLoughlin | const char *name, |
3069 | f6b134ac | Mark McLoughlin | VLANState *vlan); |
3070 | f83c6e10 | Mark McLoughlin | |
3071 | f83c6e10 | Mark McLoughlin | /* magic number, but compiler will warn if too small */
|
3072 | f83c6e10 | Mark McLoughlin | #define NET_MAX_DESC 20 |
3073 | f83c6e10 | Mark McLoughlin | |
3074 | f83c6e10 | Mark McLoughlin | static struct { |
3075 | f83c6e10 | Mark McLoughlin | const char *type; |
3076 | f83c6e10 | Mark McLoughlin | net_client_init_func init; |
3077 | f83c6e10 | Mark McLoughlin | QemuOptDesc desc[NET_MAX_DESC]; |
3078 | f83c6e10 | Mark McLoughlin | } net_client_types[] = { |
3079 | f83c6e10 | Mark McLoughlin | { |
3080 | f83c6e10 | Mark McLoughlin | .type = "none",
|
3081 | f83c6e10 | Mark McLoughlin | .desc = { |
3082 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3083 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
3084 | f83c6e10 | Mark McLoughlin | }, |
3085 | f83c6e10 | Mark McLoughlin | }, { |
3086 | f83c6e10 | Mark McLoughlin | .type = "nic",
|
3087 | f83c6e10 | Mark McLoughlin | .init = net_init_nic, |
3088 | f83c6e10 | Mark McLoughlin | .desc = { |
3089 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3090 | f83c6e10 | Mark McLoughlin | { |
3091 | 5869c4d5 | Mark McLoughlin | .name = "netdev",
|
3092 | 5869c4d5 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3093 | 5869c4d5 | Mark McLoughlin | .help = "id of -netdev to connect to",
|
3094 | 5869c4d5 | Mark McLoughlin | }, |
3095 | 5869c4d5 | Mark McLoughlin | { |
3096 | f83c6e10 | Mark McLoughlin | .name = "macaddr",
|
3097 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3098 | f83c6e10 | Mark McLoughlin | .help = "MAC address",
|
3099 | f83c6e10 | Mark McLoughlin | }, { |
3100 | f83c6e10 | Mark McLoughlin | .name = "model",
|
3101 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3102 | f83c6e10 | Mark McLoughlin | .help = "device model (e1000, rtl8139, virtio etc.)",
|
3103 | f83c6e10 | Mark McLoughlin | }, { |
3104 | f83c6e10 | Mark McLoughlin | .name = "addr",
|
3105 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3106 | f83c6e10 | Mark McLoughlin | .help = "PCI device address",
|
3107 | f83c6e10 | Mark McLoughlin | }, { |
3108 | f83c6e10 | Mark McLoughlin | .name = "vectors",
|
3109 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
3110 | f83c6e10 | Mark McLoughlin | .help = "number of MSI-x vectors, 0 to disable MSI-X",
|
3111 | f83c6e10 | Mark McLoughlin | }, |
3112 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
3113 | f83c6e10 | Mark McLoughlin | }, |
3114 | ec302ffd | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
3115 | ec302ffd | Mark McLoughlin | }, { |
3116 | ec302ffd | Mark McLoughlin | .type = "user",
|
3117 | ec302ffd | Mark McLoughlin | .init = net_init_slirp, |
3118 | ec302ffd | Mark McLoughlin | .desc = { |
3119 | ec302ffd | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3120 | ec302ffd | Mark McLoughlin | { |
3121 | ec302ffd | Mark McLoughlin | .name = "hostname",
|
3122 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3123 | ec302ffd | Mark McLoughlin | .help = "client hostname reported by the builtin DHCP server",
|
3124 | ec302ffd | Mark McLoughlin | }, { |
3125 | ec302ffd | Mark McLoughlin | .name = "restrict",
|
3126 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3127 | ec302ffd | Mark McLoughlin | .help = "isolate the guest from the host (y|yes|n|no)",
|
3128 | ec302ffd | Mark McLoughlin | }, { |
3129 | ec302ffd | Mark McLoughlin | .name = "ip",
|
3130 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3131 | ec302ffd | Mark McLoughlin | .help = "legacy parameter, use net= instead",
|
3132 | ec302ffd | Mark McLoughlin | }, { |
3133 | ec302ffd | Mark McLoughlin | .name = "net",
|
3134 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3135 | ec302ffd | Mark McLoughlin | .help = "IP address and optional netmask",
|
3136 | ec302ffd | Mark McLoughlin | }, { |
3137 | ec302ffd | Mark McLoughlin | .name = "host",
|
3138 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3139 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the host",
|
3140 | ec302ffd | Mark McLoughlin | }, { |
3141 | ec302ffd | Mark McLoughlin | .name = "tftp",
|
3142 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3143 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in TFTP server",
|
3144 | ec302ffd | Mark McLoughlin | }, { |
3145 | ec302ffd | Mark McLoughlin | .name = "bootfile",
|
3146 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3147 | ec302ffd | Mark McLoughlin | .help = "BOOTP filename, for use with tftp=",
|
3148 | ec302ffd | Mark McLoughlin | }, { |
3149 | ec302ffd | Mark McLoughlin | .name = "dhcpstart",
|
3150 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3151 | ec302ffd | Mark McLoughlin | .help = "the first of the 16 IPs the built-in DHCP server can assign",
|
3152 | ec302ffd | Mark McLoughlin | }, { |
3153 | ec302ffd | Mark McLoughlin | .name = "dns",
|
3154 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3155 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the virtual nameserver",
|
3156 | ec302ffd | Mark McLoughlin | }, { |
3157 | ec302ffd | Mark McLoughlin | .name = "smb",
|
3158 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3159 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in SMB server",
|
3160 | ec302ffd | Mark McLoughlin | }, { |
3161 | ec302ffd | Mark McLoughlin | .name = "smbserver",
|
3162 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3163 | ec302ffd | Mark McLoughlin | .help = "IP address of the built-in SMB server",
|
3164 | ec302ffd | Mark McLoughlin | }, { |
3165 | ec302ffd | Mark McLoughlin | .name = "hostfwd",
|
3166 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3167 | ec302ffd | Mark McLoughlin | .help = "guest port number to forward incoming TCP or UDP connections",
|
3168 | ec302ffd | Mark McLoughlin | }, { |
3169 | ec302ffd | Mark McLoughlin | .name = "guestfwd",
|
3170 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3171 | ec302ffd | Mark McLoughlin | .help = "IP address and port to forward guest TCP connections",
|
3172 | ec302ffd | Mark McLoughlin | }, |
3173 | ec302ffd | Mark McLoughlin | { /* end of list */ }
|
3174 | ec302ffd | Mark McLoughlin | }, |
3175 | ec302ffd | Mark McLoughlin | #endif
|
3176 | 8a1c5235 | Mark McLoughlin | #ifdef _WIN32
|
3177 | 8a1c5235 | Mark McLoughlin | }, { |
3178 | 8a1c5235 | Mark McLoughlin | .type = "tap",
|
3179 | 8a1c5235 | Mark McLoughlin | .init = net_init_tap_win32, |
3180 | 8a1c5235 | Mark McLoughlin | .desc = { |
3181 | 8a1c5235 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3182 | 8a1c5235 | Mark McLoughlin | { |
3183 | 8a1c5235 | Mark McLoughlin | .name = "ifname",
|
3184 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3185 | 8a1c5235 | Mark McLoughlin | .help = "interface name",
|
3186 | 8a1c5235 | Mark McLoughlin | }, |
3187 | 8a1c5235 | Mark McLoughlin | { /* end of list */ }
|
3188 | 8a1c5235 | Mark McLoughlin | }, |
3189 | 8a1c5235 | Mark McLoughlin | #elif !defined(_AIX)
|
3190 | 8a1c5235 | Mark McLoughlin | }, { |
3191 | 8a1c5235 | Mark McLoughlin | .type = "tap",
|
3192 | 8a1c5235 | Mark McLoughlin | .init = net_init_tap, |
3193 | 8a1c5235 | Mark McLoughlin | .desc = { |
3194 | 8a1c5235 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3195 | 8a1c5235 | Mark McLoughlin | { |
3196 | 8a1c5235 | Mark McLoughlin | .name = "fd",
|
3197 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3198 | 8a1c5235 | Mark McLoughlin | .help = "file descriptor of an already opened tap",
|
3199 | 8a1c5235 | Mark McLoughlin | }, { |
3200 | 8a1c5235 | Mark McLoughlin | .name = "ifname",
|
3201 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3202 | 8a1c5235 | Mark McLoughlin | .help = "interface name",
|
3203 | 8a1c5235 | Mark McLoughlin | }, { |
3204 | 8a1c5235 | Mark McLoughlin | .name = "script",
|
3205 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3206 | 8a1c5235 | Mark McLoughlin | .help = "script to initialize the interface",
|
3207 | 8a1c5235 | Mark McLoughlin | }, { |
3208 | 8a1c5235 | Mark McLoughlin | .name = "downscript",
|
3209 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3210 | 8a1c5235 | Mark McLoughlin | .help = "script to shut down the interface",
|
3211 | 8a1c5235 | Mark McLoughlin | }, { |
3212 | 8a1c5235 | Mark McLoughlin | .name = "sndbuf",
|
3213 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
3214 | 8a1c5235 | Mark McLoughlin | .help = "send buffer limit"
|
3215 | baf74c95 | Mark McLoughlin | }, { |
3216 | baf74c95 | Mark McLoughlin | .name = "vnet_hdr",
|
3217 | baf74c95 | Mark McLoughlin | .type = QEMU_OPT_BOOL, |
3218 | baf74c95 | Mark McLoughlin | .help = "enable the IFF_VNET_HDR flag on the tap interface"
|
3219 | 8a1c5235 | Mark McLoughlin | }, |
3220 | 8a1c5235 | Mark McLoughlin | { /* end of list */ }
|
3221 | 8a1c5235 | Mark McLoughlin | }, |
3222 | 8a1c5235 | Mark McLoughlin | #endif
|
3223 | 88ce16ca | Mark McLoughlin | }, { |
3224 | 88ce16ca | Mark McLoughlin | .type = "socket",
|
3225 | 88ce16ca | Mark McLoughlin | .init = net_init_socket, |
3226 | 88ce16ca | Mark McLoughlin | .desc = { |
3227 | 88ce16ca | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3228 | 88ce16ca | Mark McLoughlin | { |
3229 | 88ce16ca | Mark McLoughlin | .name = "fd",
|
3230 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3231 | 88ce16ca | Mark McLoughlin | .help = "file descriptor of an already opened socket",
|
3232 | 88ce16ca | Mark McLoughlin | }, { |
3233 | 88ce16ca | Mark McLoughlin | .name = "listen",
|
3234 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3235 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to listen on",
|
3236 | 88ce16ca | Mark McLoughlin | }, { |
3237 | 88ce16ca | Mark McLoughlin | .name = "connect",
|
3238 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3239 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to connect to",
|
3240 | 88ce16ca | Mark McLoughlin | }, { |
3241 | 88ce16ca | Mark McLoughlin | .name = "mcast",
|
3242 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3243 | 88ce16ca | Mark McLoughlin | .help = "UDP multicast address and port number",
|
3244 | 88ce16ca | Mark McLoughlin | }, |
3245 | 88ce16ca | Mark McLoughlin | { /* end of list */ }
|
3246 | 88ce16ca | Mark McLoughlin | }, |
3247 | dd51058d | Mark McLoughlin | #ifdef CONFIG_VDE
|
3248 | dd51058d | Mark McLoughlin | }, { |
3249 | dd51058d | Mark McLoughlin | .type = "vde",
|
3250 | dd51058d | Mark McLoughlin | .init = net_init_vde, |
3251 | dd51058d | Mark McLoughlin | .desc = { |
3252 | dd51058d | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3253 | dd51058d | Mark McLoughlin | { |
3254 | dd51058d | Mark McLoughlin | .name = "sock",
|
3255 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3256 | dd51058d | Mark McLoughlin | .help = "socket path",
|
3257 | dd51058d | Mark McLoughlin | }, { |
3258 | dd51058d | Mark McLoughlin | .name = "port",
|
3259 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
3260 | dd51058d | Mark McLoughlin | .help = "port number",
|
3261 | dd51058d | Mark McLoughlin | }, { |
3262 | dd51058d | Mark McLoughlin | .name = "group",
|
3263 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3264 | dd51058d | Mark McLoughlin | .help = "group owner of socket",
|
3265 | dd51058d | Mark McLoughlin | }, { |
3266 | dd51058d | Mark McLoughlin | .name = "mode",
|
3267 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
3268 | dd51058d | Mark McLoughlin | .help = "permissions for socket",
|
3269 | dd51058d | Mark McLoughlin | }, |
3270 | dd51058d | Mark McLoughlin | { /* end of list */ }
|
3271 | dd51058d | Mark McLoughlin | }, |
3272 | dd51058d | Mark McLoughlin | #endif
|
3273 | ed2955c2 | Mark McLoughlin | }, { |
3274 | ed2955c2 | Mark McLoughlin | .type = "dump",
|
3275 | ed2955c2 | Mark McLoughlin | .init = net_init_dump, |
3276 | ed2955c2 | Mark McLoughlin | .desc = { |
3277 | ed2955c2 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
3278 | ed2955c2 | Mark McLoughlin | { |
3279 | ed2955c2 | Mark McLoughlin | .name = "len",
|
3280 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
3281 | ed2955c2 | Mark McLoughlin | .help = "per-packet size limit (64k default)",
|
3282 | ed2955c2 | Mark McLoughlin | }, { |
3283 | ed2955c2 | Mark McLoughlin | .name = "file",
|
3284 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
3285 | ed2955c2 | Mark McLoughlin | .help = "dump file path (default is qemu-vlan0.pcap)",
|
3286 | ed2955c2 | Mark McLoughlin | }, |
3287 | ed2955c2 | Mark McLoughlin | { /* end of list */ }
|
3288 | ed2955c2 | Mark McLoughlin | }, |
3289 | f83c6e10 | Mark McLoughlin | }, |
3290 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
3291 | f83c6e10 | Mark McLoughlin | }; |
3292 | f83c6e10 | Mark McLoughlin | |
3293 | f6b134ac | Mark McLoughlin | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
3294 | f83c6e10 | Mark McLoughlin | { |
3295 | 6d952ebe | Mark McLoughlin | const char *name; |
3296 | f83c6e10 | Mark McLoughlin | const char *type; |
3297 | f83c6e10 | Mark McLoughlin | int i;
|
3298 | f83c6e10 | Mark McLoughlin | |
3299 | f83c6e10 | Mark McLoughlin | type = qemu_opt_get(opts, "type");
|
3300 | f83c6e10 | Mark McLoughlin | if (!type) {
|
3301 | f83c6e10 | Mark McLoughlin | qemu_error("No type specified for -net\n");
|
3302 | f83c6e10 | Mark McLoughlin | return -1; |
3303 | f83c6e10 | Mark McLoughlin | } |
3304 | f83c6e10 | Mark McLoughlin | |
3305 | f6b134ac | Mark McLoughlin | if (is_netdev) {
|
3306 | f6b134ac | Mark McLoughlin | if (strcmp(type, "tap") != 0 && |
3307 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
3308 | f6b134ac | Mark McLoughlin | strcmp(type, "user") != 0 && |
3309 | f6b134ac | Mark McLoughlin | #endif
|
3310 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_VDE
|
3311 | f6b134ac | Mark McLoughlin | strcmp(type, "vde") != 0 && |
3312 | f6b134ac | Mark McLoughlin | #endif
|
3313 | f6b134ac | Mark McLoughlin | strcmp(type, "socket") != 0) { |
3314 | f6b134ac | Mark McLoughlin | qemu_error("The '%s' network backend type is not valid with -netdev\n",
|
3315 | f6b134ac | Mark McLoughlin | type); |
3316 | f6b134ac | Mark McLoughlin | return -1; |
3317 | f6b134ac | Mark McLoughlin | } |
3318 | f6b134ac | Mark McLoughlin | |
3319 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "vlan")) { |
3320 | f6b134ac | Mark McLoughlin | qemu_error("The 'vlan' parameter is not valid with -netdev\n");
|
3321 | f6b134ac | Mark McLoughlin | return -1; |
3322 | f6b134ac | Mark McLoughlin | } |
3323 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "name")) { |
3324 | f6b134ac | Mark McLoughlin | qemu_error("The 'name' parameter is not valid with -netdev\n");
|
3325 | f6b134ac | Mark McLoughlin | return -1; |
3326 | f6b134ac | Mark McLoughlin | } |
3327 | f6b134ac | Mark McLoughlin | if (!qemu_opts_id(opts)) {
|
3328 | f6b134ac | Mark McLoughlin | qemu_error("The id= parameter is required with -netdev\n");
|
3329 | f6b134ac | Mark McLoughlin | return -1; |
3330 | f6b134ac | Mark McLoughlin | } |
3331 | f6b134ac | Mark McLoughlin | } |
3332 | f6b134ac | Mark McLoughlin | |
3333 | 6d952ebe | Mark McLoughlin | name = qemu_opts_id(opts); |
3334 | 6d952ebe | Mark McLoughlin | if (!name) {
|
3335 | 6d952ebe | Mark McLoughlin | name = qemu_opt_get(opts, "name");
|
3336 | 6d952ebe | Mark McLoughlin | } |
3337 | 6d952ebe | Mark McLoughlin | |
3338 | f83c6e10 | Mark McLoughlin | for (i = 0; net_client_types[i].type != NULL; i++) { |
3339 | f83c6e10 | Mark McLoughlin | if (!strcmp(net_client_types[i].type, type)) {
|
3340 | f6b134ac | Mark McLoughlin | VLANState *vlan = NULL;
|
3341 | f6b134ac | Mark McLoughlin | |
3342 | f83c6e10 | Mark McLoughlin | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
3343 | f83c6e10 | Mark McLoughlin | return -1; |
3344 | f83c6e10 | Mark McLoughlin | } |
3345 | f83c6e10 | Mark McLoughlin | |
3346 | 5869c4d5 | Mark McLoughlin | /* Do not add to a vlan if it's a -netdev or a nic with a
|
3347 | 5869c4d5 | Mark McLoughlin | * netdev= parameter. */
|
3348 | 5869c4d5 | Mark McLoughlin | if (!(is_netdev ||
|
3349 | 5869c4d5 | Mark McLoughlin | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
3350 | f6b134ac | Mark McLoughlin | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
3351 | f6b134ac | Mark McLoughlin | } |
3352 | f6b134ac | Mark McLoughlin | |
3353 | f83c6e10 | Mark McLoughlin | if (net_client_types[i].init) {
|
3354 | f6b134ac | Mark McLoughlin | return net_client_types[i].init(opts, mon, name, vlan);
|
3355 | f83c6e10 | Mark McLoughlin | } else {
|
3356 | f83c6e10 | Mark McLoughlin | return 0; |
3357 | f83c6e10 | Mark McLoughlin | } |
3358 | f83c6e10 | Mark McLoughlin | } |
3359 | f83c6e10 | Mark McLoughlin | } |
3360 | f83c6e10 | Mark McLoughlin | |
3361 | f83c6e10 | Mark McLoughlin | qemu_error("Invalid -net type '%s'\n", type);
|
3362 | f83c6e10 | Mark McLoughlin | return -1; |
3363 | f83c6e10 | Mark McLoughlin | } |
3364 | f83c6e10 | Mark McLoughlin | |
3365 | 8b13c4a7 | aliguori | void net_client_uninit(NICInfo *nd)
|
3366 | 8b13c4a7 | aliguori | { |
3367 | d80b9fc6 | Mark McLoughlin | if (nd->vlan) {
|
3368 | d80b9fc6 | Mark McLoughlin | nd->vlan->nb_guest_devs--; |
3369 | d80b9fc6 | Mark McLoughlin | } |
3370 | 8b13c4a7 | aliguori | nb_nics--; |
3371 | a9796703 | Glauber Costa | |
3372 | 9203f520 | Mark McLoughlin | qemu_free(nd->model); |
3373 | 9203f520 | Mark McLoughlin | qemu_free(nd->name); |
3374 | 9203f520 | Mark McLoughlin | qemu_free(nd->devaddr); |
3375 | a9796703 | Glauber Costa | |
3376 | d2cffe30 | Mark McLoughlin | nd->used = 0;
|
3377 | 8b13c4a7 | aliguori | } |
3378 | 8b13c4a7 | aliguori | |
3379 | 6f338c34 | aliguori | static int net_host_check_device(const char *device) |
3380 | 6f338c34 | aliguori | { |
3381 | 6f338c34 | aliguori | int i;
|
3382 | bb9ea79e | aliguori | const char *valid_param_list[] = { "tap", "socket", "dump" |
3383 | 6f338c34 | aliguori | #ifdef CONFIG_SLIRP
|
3384 | 6f338c34 | aliguori | ,"user"
|
3385 | 6f338c34 | aliguori | #endif
|
3386 | 6f338c34 | aliguori | #ifdef CONFIG_VDE
|
3387 | 6f338c34 | aliguori | ,"vde"
|
3388 | 6f338c34 | aliguori | #endif
|
3389 | 6f338c34 | aliguori | }; |
3390 | 6f338c34 | aliguori | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
3391 | 6f338c34 | aliguori | if (!strncmp(valid_param_list[i], device,
|
3392 | 6f338c34 | aliguori | strlen(valid_param_list[i]))) |
3393 | 6f338c34 | aliguori | return 1; |
3394 | 6f338c34 | aliguori | } |
3395 | 6f338c34 | aliguori | |
3396 | 6f338c34 | aliguori | return 0; |
3397 | 6f338c34 | aliguori | } |
3398 | 6f338c34 | aliguori | |
3399 | f18c16de | Luiz Capitulino | void net_host_device_add(Monitor *mon, const QDict *qdict) |
3400 | 6f338c34 | aliguori | { |
3401 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
3402 | 7f1c9d20 | Mark McLoughlin | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
3403 | 7f1c9d20 | Mark McLoughlin | QemuOpts *opts; |
3404 | f18c16de | Luiz Capitulino | |
3405 | 6f338c34 | aliguori | if (!net_host_check_device(device)) {
|
3406 | 376253ec | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
3407 | 6f338c34 | aliguori | return;
|
3408 | 6f338c34 | aliguori | } |
3409 | 7f1c9d20 | Mark McLoughlin | |
3410 | 7f1c9d20 | Mark McLoughlin | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); |
3411 | 7f1c9d20 | Mark McLoughlin | if (!opts) {
|
3412 | 7f1c9d20 | Mark McLoughlin | monitor_printf(mon, "parsing network options '%s' failed\n",
|
3413 | 7f1c9d20 | Mark McLoughlin | opts_str ? opts_str : "");
|
3414 | 7f1c9d20 | Mark McLoughlin | return;
|
3415 | 7f1c9d20 | Mark McLoughlin | } |
3416 | 7f1c9d20 | Mark McLoughlin | |
3417 | 7f1c9d20 | Mark McLoughlin | qemu_opt_set(opts, "type", device);
|
3418 | 7f1c9d20 | Mark McLoughlin | |
3419 | f6b134ac | Mark McLoughlin | if (net_client_init(mon, opts, 0) < 0) { |
3420 | 5c8be678 | aliguori | monitor_printf(mon, "adding host network device %s failed\n", device);
|
3421 | 5c8be678 | aliguori | } |
3422 | 6f338c34 | aliguori | } |
3423 | 6f338c34 | aliguori | |
3424 | f18c16de | Luiz Capitulino | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
3425 | 6f338c34 | aliguori | { |
3426 | 6f338c34 | aliguori | VLANClientState *vc; |
3427 | f18c16de | Luiz Capitulino | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
3428 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
3429 | 6f338c34 | aliguori | |
3430 | 1a609520 | Jan Kiszka | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
3431 | 6f338c34 | aliguori | if (!vc) {
|
3432 | 6f338c34 | aliguori | return;
|
3433 | 6f338c34 | aliguori | } |
3434 | e8f1f9db | aliguori | if (!net_host_check_device(vc->model)) {
|
3435 | e8f1f9db | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
3436 | e8f1f9db | aliguori | return;
|
3437 | e8f1f9db | aliguori | } |
3438 | 6f338c34 | aliguori | qemu_del_vlan_client(vc); |
3439 | 6f338c34 | aliguori | } |
3440 | 6f338c34 | aliguori | |
3441 | 406c8df3 | Glauber Costa | void net_set_boot_mask(int net_boot_mask) |
3442 | 406c8df3 | Glauber Costa | { |
3443 | 406c8df3 | Glauber Costa | int i;
|
3444 | 406c8df3 | Glauber Costa | |
3445 | 406c8df3 | Glauber Costa | /* Only the first four NICs may be bootable */
|
3446 | 406c8df3 | Glauber Costa | net_boot_mask = net_boot_mask & 0xF;
|
3447 | 406c8df3 | Glauber Costa | |
3448 | 406c8df3 | Glauber Costa | for (i = 0; i < nb_nics; i++) { |
3449 | 406c8df3 | Glauber Costa | if (net_boot_mask & (1 << i)) { |
3450 | 406c8df3 | Glauber Costa | nd_table[i].bootable = 1;
|
3451 | 406c8df3 | Glauber Costa | net_boot_mask &= ~(1 << i);
|
3452 | 406c8df3 | Glauber Costa | } |
3453 | 406c8df3 | Glauber Costa | } |
3454 | 406c8df3 | Glauber Costa | |
3455 | 406c8df3 | Glauber Costa | if (net_boot_mask) {
|
3456 | 406c8df3 | Glauber Costa | fprintf(stderr, "Cannot boot from non-existent NIC\n");
|
3457 | 406c8df3 | Glauber Costa | exit(1);
|
3458 | 406c8df3 | Glauber Costa | } |
3459 | 406c8df3 | Glauber Costa | } |
3460 | 406c8df3 | Glauber Costa | |
3461 | 376253ec | aliguori | void do_info_network(Monitor *mon)
|
3462 | 63a01ef8 | aliguori | { |
3463 | 63a01ef8 | aliguori | VLANState *vlan; |
3464 | 63a01ef8 | aliguori | |
3465 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
3466 | 5610c3aa | Mark McLoughlin | VLANClientState *vc; |
3467 | 5610c3aa | Mark McLoughlin | |
3468 | 376253ec | aliguori | monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
|
3469 | 5610c3aa | Mark McLoughlin | |
3470 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
3471 | 376253ec | aliguori | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
|
3472 | 5610c3aa | Mark McLoughlin | } |
3473 | 63a01ef8 | aliguori | } |
3474 | 63a01ef8 | aliguori | } |
3475 | 63a01ef8 | aliguori | |
3476 | f18c16de | Luiz Capitulino | void do_set_link(Monitor *mon, const QDict *qdict) |
3477 | 436e5e53 | aliguori | { |
3478 | 436e5e53 | aliguori | VLANState *vlan; |
3479 | 436e5e53 | aliguori | VLANClientState *vc = NULL;
|
3480 | f18c16de | Luiz Capitulino | const char *name = qdict_get_str(qdict, "name"); |
3481 | f18c16de | Luiz Capitulino | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); |
3482 | 436e5e53 | aliguori | |
3483 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
3484 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
3485 | 5610c3aa | Mark McLoughlin | if (strcmp(vc->name, name) == 0) { |
3486 | dd5de373 | edgar_igl | goto done;
|
3487 | 5610c3aa | Mark McLoughlin | } |
3488 | 5610c3aa | Mark McLoughlin | } |
3489 | 5610c3aa | Mark McLoughlin | } |
3490 | dd5de373 | edgar_igl | done:
|
3491 | 436e5e53 | aliguori | |
3492 | 436e5e53 | aliguori | if (!vc) {
|
3493 | 7dc3fa09 | Stefan Weil | monitor_printf(mon, "could not find network device '%s'\n", name);
|
3494 | c3cf0d3f | Luiz Capitulino | return;
|
3495 | 436e5e53 | aliguori | } |
3496 | 436e5e53 | aliguori | |
3497 | 436e5e53 | aliguori | if (strcmp(up_or_down, "up") == 0) |
3498 | 436e5e53 | aliguori | vc->link_down = 0;
|
3499 | 436e5e53 | aliguori | else if (strcmp(up_or_down, "down") == 0) |
3500 | 436e5e53 | aliguori | vc->link_down = 1;
|
3501 | 436e5e53 | aliguori | else
|
3502 | 376253ec | aliguori | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
|
3503 | 376253ec | aliguori | "valid\n", up_or_down);
|
3504 | 436e5e53 | aliguori | |
3505 | 34b25ca7 | aliguori | if (vc->link_status_changed)
|
3506 | 34b25ca7 | aliguori | vc->link_status_changed(vc); |
3507 | 436e5e53 | aliguori | } |
3508 | 436e5e53 | aliguori | |
3509 | 63a01ef8 | aliguori | void net_cleanup(void) |
3510 | 63a01ef8 | aliguori | { |
3511 | 63a01ef8 | aliguori | VLANState *vlan; |
3512 | 577c4af9 | Mark McLoughlin | VLANClientState *vc, *next_vc; |
3513 | 63a01ef8 | aliguori | |
3514 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
3515 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
3516 | b946a153 | aliguori | qemu_del_vlan_client(vc); |
3517 | 63a01ef8 | aliguori | } |
3518 | 63a01ef8 | aliguori | } |
3519 | 577c4af9 | Mark McLoughlin | |
3520 | 577c4af9 | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
3521 | 577c4af9 | Mark McLoughlin | qemu_del_vlan_client(vc); |
3522 | 577c4af9 | Mark McLoughlin | } |
3523 | 63a01ef8 | aliguori | } |
3524 | 63a01ef8 | aliguori | |
3525 | dc1c9fe8 | Mark McLoughlin | static void net_check_clients(void) |
3526 | 63a01ef8 | aliguori | { |
3527 | 63a01ef8 | aliguori | VLANState *vlan; |
3528 | 63a01ef8 | aliguori | |
3529 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
3530 | 63a01ef8 | aliguori | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
3531 | 63a01ef8 | aliguori | continue;
|
3532 | 63a01ef8 | aliguori | if (vlan->nb_guest_devs == 0) |
3533 | 63a01ef8 | aliguori | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
3534 | 63a01ef8 | aliguori | if (vlan->nb_host_devs == 0) |
3535 | 63a01ef8 | aliguori | fprintf(stderr, |
3536 | 63a01ef8 | aliguori | "Warning: vlan %d is not connected to host network\n",
|
3537 | 63a01ef8 | aliguori | vlan->id); |
3538 | 63a01ef8 | aliguori | } |
3539 | 63a01ef8 | aliguori | } |
3540 | dc1c9fe8 | Mark McLoughlin | |
3541 | dc1c9fe8 | Mark McLoughlin | static int net_init_client(QemuOpts *opts, void *dummy) |
3542 | dc1c9fe8 | Mark McLoughlin | { |
3543 | c1671a08 | Mark McLoughlin | if (net_client_init(NULL, opts, 0) < 0) |
3544 | c1671a08 | Mark McLoughlin | return -1; |
3545 | c1671a08 | Mark McLoughlin | return 0; |
3546 | f6b134ac | Mark McLoughlin | } |
3547 | f6b134ac | Mark McLoughlin | |
3548 | f6b134ac | Mark McLoughlin | static int net_init_netdev(QemuOpts *opts, void *dummy) |
3549 | f6b134ac | Mark McLoughlin | { |
3550 | f6b134ac | Mark McLoughlin | return net_client_init(NULL, opts, 1); |
3551 | dc1c9fe8 | Mark McLoughlin | } |
3552 | dc1c9fe8 | Mark McLoughlin | |
3553 | dc1c9fe8 | Mark McLoughlin | int net_init_clients(void) |
3554 | dc1c9fe8 | Mark McLoughlin | { |
3555 | dc1c9fe8 | Mark McLoughlin | if (QTAILQ_EMPTY(&qemu_net_opts.head)) {
|
3556 | dc1c9fe8 | Mark McLoughlin | /* if no clients, we use a default config */
|
3557 | dc1c9fe8 | Mark McLoughlin | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); |
3558 | dc1c9fe8 | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
3559 | dc1c9fe8 | Mark McLoughlin | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); |
3560 | dc1c9fe8 | Mark McLoughlin | #endif
|
3561 | dc1c9fe8 | Mark McLoughlin | } |
3562 | dc1c9fe8 | Mark McLoughlin | |
3563 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlans); |
3564 | 577c4af9 | Mark McLoughlin | QTAILQ_INIT(&non_vlan_clients); |
3565 | 5610c3aa | Mark McLoughlin | |
3566 | f6b134ac | Mark McLoughlin | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
3567 | f6b134ac | Mark McLoughlin | return -1; |
3568 | f6b134ac | Mark McLoughlin | |
3569 | dc1c9fe8 | Mark McLoughlin | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
3570 | dc1c9fe8 | Mark McLoughlin | return -1; |
3571 | dc1c9fe8 | Mark McLoughlin | } |
3572 | dc1c9fe8 | Mark McLoughlin | |
3573 | dc1c9fe8 | Mark McLoughlin | net_check_clients(); |
3574 | dc1c9fe8 | Mark McLoughlin | |
3575 | dc1c9fe8 | Mark McLoughlin | return 0; |
3576 | dc1c9fe8 | Mark McLoughlin | } |
3577 | dc1c9fe8 | Mark McLoughlin | |
3578 | 7f161aae | Mark McLoughlin | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
3579 | dc1c9fe8 | Mark McLoughlin | { |
3580 | a3a766e7 | Juan Quintela | #if defined(CONFIG_SLIRP)
|
3581 | dc1c9fe8 | Mark McLoughlin | /* handle legacy -net channel,port:chr */
|
3582 | 7f161aae | Mark McLoughlin | if (!strcmp(opts_list->name, "net") && |
3583 | 7f161aae | Mark McLoughlin | !strncmp(optarg, "channel,", strlen("channel,"))) { |
3584 | dc1c9fe8 | Mark McLoughlin | int ret;
|
3585 | dc1c9fe8 | Mark McLoughlin | |
3586 | dc1c9fe8 | Mark McLoughlin | optarg += strlen("channel,");
|
3587 | dc1c9fe8 | Mark McLoughlin | |
3588 | dc1c9fe8 | Mark McLoughlin | if (QTAILQ_EMPTY(&slirp_stacks)) {
|
3589 | dc1c9fe8 | Mark McLoughlin | struct slirp_config_str *config;
|
3590 | dc1c9fe8 | Mark McLoughlin | |
3591 | dc1c9fe8 | Mark McLoughlin | config = qemu_malloc(sizeof(*config));
|
3592 | dc1c9fe8 | Mark McLoughlin | pstrcpy(config->str, sizeof(config->str), optarg);
|
3593 | dc1c9fe8 | Mark McLoughlin | config->flags = SLIRP_CFG_LEGACY; |
3594 | dc1c9fe8 | Mark McLoughlin | config->next = slirp_configs; |
3595 | dc1c9fe8 | Mark McLoughlin | slirp_configs = config; |
3596 | dc1c9fe8 | Mark McLoughlin | ret = 0;
|
3597 | dc1c9fe8 | Mark McLoughlin | } else {
|
3598 | dc1c9fe8 | Mark McLoughlin | ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1);
|
3599 | dc1c9fe8 | Mark McLoughlin | } |
3600 | dc1c9fe8 | Mark McLoughlin | |
3601 | dc1c9fe8 | Mark McLoughlin | return ret;
|
3602 | dc1c9fe8 | Mark McLoughlin | } |
3603 | a3a766e7 | Juan Quintela | #endif
|
3604 | 7f161aae | Mark McLoughlin | if (!qemu_opts_parse(opts_list, optarg, "type")) { |
3605 | dc1c9fe8 | Mark McLoughlin | return -1; |
3606 | dc1c9fe8 | Mark McLoughlin | } |
3607 | dc1c9fe8 | Mark McLoughlin | |
3608 | dc1c9fe8 | Mark McLoughlin | return 0; |
3609 | dc1c9fe8 | Mark McLoughlin | } |