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