root / net.c @ 658788c5
History | View | Annotate | Download (36 kB)
1 |
/*
|
---|---|
2 |
* QEMU System Emulator
|
3 |
*
|
4 |
* Copyright (c) 2003-2008 Fabrice Bellard
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include "net.h" |
25 |
|
26 |
#include "config-host.h" |
27 |
|
28 |
#include "net/tap.h" |
29 |
#include "net/socket.h" |
30 |
#include "net/dump.h" |
31 |
#include "net/slirp.h" |
32 |
#include "net/vde.h" |
33 |
#include "net/util.h" |
34 |
#include "monitor.h" |
35 |
#include "sysemu.h" |
36 |
#include "qemu-common.h" |
37 |
#include "qemu_socket.h" |
38 |
|
39 |
static QTAILQ_HEAD(, VLANState) vlans;
|
40 |
static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
|
41 |
|
42 |
/***********************************************************/
|
43 |
/* network device redirectors */
|
44 |
|
45 |
#if defined(DEBUG_NET)
|
46 |
static void hex_dump(FILE *f, const uint8_t *buf, int size) |
47 |
{ |
48 |
int len, i, j, c;
|
49 |
|
50 |
for(i=0;i<size;i+=16) { |
51 |
len = size - i; |
52 |
if (len > 16) |
53 |
len = 16;
|
54 |
fprintf(f, "%08x ", i);
|
55 |
for(j=0;j<16;j++) { |
56 |
if (j < len)
|
57 |
fprintf(f, " %02x", buf[i+j]);
|
58 |
else
|
59 |
fprintf(f, " ");
|
60 |
} |
61 |
fprintf(f, " ");
|
62 |
for(j=0;j<len;j++) { |
63 |
c = buf[i+j]; |
64 |
if (c < ' ' || c > '~') |
65 |
c = '.';
|
66 |
fprintf(f, "%c", c);
|
67 |
} |
68 |
fprintf(f, "\n");
|
69 |
} |
70 |
} |
71 |
#endif
|
72 |
|
73 |
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
74 |
{ |
75 |
const char *p, *p1; |
76 |
int len;
|
77 |
p = *pp; |
78 |
p1 = strchr(p, sep); |
79 |
if (!p1)
|
80 |
return -1; |
81 |
len = p1 - p; |
82 |
p1++; |
83 |
if (buf_size > 0) { |
84 |
if (len > buf_size - 1) |
85 |
len = buf_size - 1;
|
86 |
memcpy(buf, p, len); |
87 |
buf[len] = '\0';
|
88 |
} |
89 |
*pp = p1; |
90 |
return 0; |
91 |
} |
92 |
|
93 |
int parse_host_src_port(struct sockaddr_in *haddr, |
94 |
struct sockaddr_in *saddr,
|
95 |
const char *input_str) |
96 |
{ |
97 |
char *str = strdup(input_str);
|
98 |
char *host_str = str;
|
99 |
char *src_str;
|
100 |
const char *src_str2; |
101 |
char *ptr;
|
102 |
|
103 |
/*
|
104 |
* Chop off any extra arguments at the end of the string which
|
105 |
* would start with a comma, then fill in the src port information
|
106 |
* if it was provided else use the "any address" and "any port".
|
107 |
*/
|
108 |
if ((ptr = strchr(str,','))) |
109 |
*ptr = '\0';
|
110 |
|
111 |
if ((src_str = strchr(input_str,'@'))) { |
112 |
*src_str = '\0';
|
113 |
src_str++; |
114 |
} |
115 |
|
116 |
if (parse_host_port(haddr, host_str) < 0) |
117 |
goto fail;
|
118 |
|
119 |
src_str2 = src_str; |
120 |
if (!src_str || *src_str == '\0') |
121 |
src_str2 = ":0";
|
122 |
|
123 |
if (parse_host_port(saddr, src_str2) < 0) |
124 |
goto fail;
|
125 |
|
126 |
free(str); |
127 |
return(0); |
128 |
|
129 |
fail:
|
130 |
free(str); |
131 |
return -1; |
132 |
} |
133 |
|
134 |
int parse_host_port(struct sockaddr_in *saddr, const char *str) |
135 |
{ |
136 |
char buf[512]; |
137 |
struct hostent *he;
|
138 |
const char *p, *r; |
139 |
int port;
|
140 |
|
141 |
p = str; |
142 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
143 |
return -1; |
144 |
saddr->sin_family = AF_INET; |
145 |
if (buf[0] == '\0') { |
146 |
saddr->sin_addr.s_addr = 0;
|
147 |
} else {
|
148 |
if (qemu_isdigit(buf[0])) { |
149 |
if (!inet_aton(buf, &saddr->sin_addr))
|
150 |
return -1; |
151 |
} else {
|
152 |
if ((he = gethostbyname(buf)) == NULL) |
153 |
return - 1; |
154 |
saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
155 |
} |
156 |
} |
157 |
port = strtol(p, (char **)&r, 0); |
158 |
if (r == p)
|
159 |
return -1; |
160 |
saddr->sin_port = htons(port); |
161 |
return 0; |
162 |
} |
163 |
|
164 |
void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
165 |
{ |
166 |
snprintf(vc->info_str, sizeof(vc->info_str),
|
167 |
"model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
|
168 |
vc->model, |
169 |
macaddr[0], macaddr[1], macaddr[2], |
170 |
macaddr[3], macaddr[4], macaddr[5]); |
171 |
} |
172 |
|
173 |
void qemu_macaddr_default_if_unset(MACAddr *macaddr)
|
174 |
{ |
175 |
static int index = 0; |
176 |
static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
177 |
|
178 |
if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
179 |
return;
|
180 |
macaddr->a[0] = 0x52; |
181 |
macaddr->a[1] = 0x54; |
182 |
macaddr->a[2] = 0x00; |
183 |
macaddr->a[3] = 0x12; |
184 |
macaddr->a[4] = 0x34; |
185 |
macaddr->a[5] = 0x56 + index++; |
186 |
} |
187 |
|
188 |
static char *assign_name(VLANClientState *vc1, const char *model) |
189 |
{ |
190 |
VLANState *vlan; |
191 |
char buf[256]; |
192 |
int id = 0; |
193 |
|
194 |
QTAILQ_FOREACH(vlan, &vlans, next) { |
195 |
VLANClientState *vc; |
196 |
|
197 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
198 |
if (vc != vc1 && strcmp(vc->model, model) == 0) { |
199 |
id++; |
200 |
} |
201 |
} |
202 |
} |
203 |
|
204 |
snprintf(buf, sizeof(buf), "%s.%d", model, id); |
205 |
|
206 |
return qemu_strdup(buf);
|
207 |
} |
208 |
|
209 |
static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
210 |
unsigned flags,
|
211 |
const uint8_t *data,
|
212 |
size_t size, |
213 |
void *opaque);
|
214 |
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
215 |
unsigned flags,
|
216 |
const struct iovec *iov, |
217 |
int iovcnt,
|
218 |
void *opaque);
|
219 |
|
220 |
VLANClientState *qemu_new_net_client(NetClientInfo *info, |
221 |
VLANState *vlan, |
222 |
VLANClientState *peer, |
223 |
const char *model, |
224 |
const char *name) |
225 |
{ |
226 |
VLANClientState *vc; |
227 |
|
228 |
assert(info->size >= sizeof(VLANClientState));
|
229 |
|
230 |
vc = qemu_mallocz(info->size); |
231 |
|
232 |
vc->type = info->type; |
233 |
vc->model = qemu_strdup(model); |
234 |
if (name) {
|
235 |
vc->name = qemu_strdup(name); |
236 |
} else {
|
237 |
vc->name = assign_name(vc, model); |
238 |
} |
239 |
vc->can_receive = info->can_receive; |
240 |
vc->receive = info->receive; |
241 |
vc->receive_raw = info->receive_raw; |
242 |
vc->receive_iov = info->receive_iov; |
243 |
vc->cleanup = info->cleanup; |
244 |
vc->link_status_changed = info->link_status_changed; |
245 |
|
246 |
if (vlan) {
|
247 |
assert(!peer); |
248 |
vc->vlan = vlan; |
249 |
QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
250 |
} else {
|
251 |
if (peer) {
|
252 |
vc->peer = peer; |
253 |
peer->peer = vc; |
254 |
} |
255 |
QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
256 |
|
257 |
vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
258 |
qemu_deliver_packet_iov, |
259 |
vc); |
260 |
} |
261 |
|
262 |
return vc;
|
263 |
} |
264 |
|
265 |
NICState *qemu_new_nic(NetClientInfo *info, |
266 |
NICConf *conf, |
267 |
const char *model, |
268 |
const char *name, |
269 |
void *opaque)
|
270 |
{ |
271 |
VLANClientState *nc; |
272 |
NICState *nic; |
273 |
|
274 |
assert(info->type == NET_CLIENT_TYPE_NIC); |
275 |
assert(info->size >= sizeof(NICState));
|
276 |
|
277 |
nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); |
278 |
|
279 |
nic = DO_UPCAST(NICState, nc, nc); |
280 |
nic->conf = conf; |
281 |
nic->opaque = opaque; |
282 |
|
283 |
return nic;
|
284 |
} |
285 |
|
286 |
VLANClientState *qemu_new_vlan_client(net_client_type type, |
287 |
VLANState *vlan, |
288 |
VLANClientState *peer, |
289 |
const char *model, |
290 |
const char *name, |
291 |
NetCanReceive *can_receive, |
292 |
NetReceive *receive, |
293 |
NetReceive *receive_raw, |
294 |
NetReceiveIOV *receive_iov, |
295 |
NetCleanup *cleanup, |
296 |
void *opaque)
|
297 |
{ |
298 |
VLANClientState *ret; |
299 |
NetClientInfo info; |
300 |
|
301 |
info.type = type; |
302 |
info.size = sizeof(VLANClientState);
|
303 |
info.can_receive = can_receive; |
304 |
info.receive = receive; |
305 |
info.receive_raw = receive_raw; |
306 |
info.receive_iov = receive_iov; |
307 |
info.cleanup = cleanup; |
308 |
info.link_status_changed = NULL;
|
309 |
|
310 |
ret = qemu_new_net_client(&info, vlan, peer, model, name); |
311 |
|
312 |
ret->opaque = opaque; |
313 |
|
314 |
return ret;
|
315 |
} |
316 |
|
317 |
void qemu_del_vlan_client(VLANClientState *vc)
|
318 |
{ |
319 |
if (vc->vlan) {
|
320 |
QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
321 |
} else {
|
322 |
if (vc->send_queue) {
|
323 |
qemu_del_net_queue(vc->send_queue); |
324 |
} |
325 |
QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
326 |
if (vc->peer) {
|
327 |
vc->peer->peer = NULL;
|
328 |
} |
329 |
} |
330 |
|
331 |
if (vc->cleanup) {
|
332 |
vc->cleanup(vc); |
333 |
} |
334 |
|
335 |
qemu_free(vc->name); |
336 |
qemu_free(vc->model); |
337 |
qemu_free(vc); |
338 |
} |
339 |
|
340 |
VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
|
341 |
{ |
342 |
VLANClientState *vc; |
343 |
|
344 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
345 |
if (vc->opaque == opaque) {
|
346 |
return vc;
|
347 |
} |
348 |
} |
349 |
|
350 |
return NULL; |
351 |
} |
352 |
|
353 |
VLANClientState * |
354 |
qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
|
355 |
const char *client_str) |
356 |
{ |
357 |
VLANState *vlan; |
358 |
VLANClientState *vc; |
359 |
|
360 |
vlan = qemu_find_vlan(vlan_id, 0);
|
361 |
if (!vlan) {
|
362 |
monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
|
363 |
return NULL; |
364 |
} |
365 |
|
366 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
367 |
if (!strcmp(vc->name, client_str)) {
|
368 |
break;
|
369 |
} |
370 |
} |
371 |
if (!vc) {
|
372 |
monitor_printf(mon, "can't find device %s on VLAN %d\n",
|
373 |
client_str, vlan_id); |
374 |
} |
375 |
|
376 |
return vc;
|
377 |
} |
378 |
|
379 |
int qemu_can_send_packet(VLANClientState *sender)
|
380 |
{ |
381 |
VLANState *vlan = sender->vlan; |
382 |
VLANClientState *vc; |
383 |
|
384 |
if (sender->peer) {
|
385 |
if (sender->peer->receive_disabled) {
|
386 |
return 0; |
387 |
} else if (sender->peer->can_receive && |
388 |
!sender->peer->can_receive(sender->peer)) { |
389 |
return 0; |
390 |
} else {
|
391 |
return 1; |
392 |
} |
393 |
} |
394 |
|
395 |
if (!sender->vlan) {
|
396 |
return 1; |
397 |
} |
398 |
|
399 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
400 |
if (vc == sender) {
|
401 |
continue;
|
402 |
} |
403 |
|
404 |
/* no can_receive() handler, they can always receive */
|
405 |
if (!vc->can_receive || vc->can_receive(vc)) {
|
406 |
return 1; |
407 |
} |
408 |
} |
409 |
return 0; |
410 |
} |
411 |
|
412 |
static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
413 |
unsigned flags,
|
414 |
const uint8_t *data,
|
415 |
size_t size, |
416 |
void *opaque)
|
417 |
{ |
418 |
VLANClientState *vc = opaque; |
419 |
ssize_t ret; |
420 |
|
421 |
if (vc->link_down) {
|
422 |
return size;
|
423 |
} |
424 |
|
425 |
if (vc->receive_disabled) {
|
426 |
return 0; |
427 |
} |
428 |
|
429 |
if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
|
430 |
ret = vc->receive_raw(vc, data, size); |
431 |
} else {
|
432 |
ret = vc->receive(vc, data, size); |
433 |
} |
434 |
|
435 |
if (ret == 0) { |
436 |
vc->receive_disabled = 1;
|
437 |
}; |
438 |
|
439 |
return ret;
|
440 |
} |
441 |
|
442 |
static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
|
443 |
unsigned flags,
|
444 |
const uint8_t *buf,
|
445 |
size_t size, |
446 |
void *opaque)
|
447 |
{ |
448 |
VLANState *vlan = opaque; |
449 |
VLANClientState *vc; |
450 |
ssize_t ret = -1;
|
451 |
|
452 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
453 |
ssize_t len; |
454 |
|
455 |
if (vc == sender) {
|
456 |
continue;
|
457 |
} |
458 |
|
459 |
if (vc->link_down) {
|
460 |
ret = size; |
461 |
continue;
|
462 |
} |
463 |
|
464 |
if (vc->receive_disabled) {
|
465 |
ret = 0;
|
466 |
continue;
|
467 |
} |
468 |
|
469 |
if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
|
470 |
len = vc->receive_raw(vc, buf, size); |
471 |
} else {
|
472 |
len = vc->receive(vc, buf, size); |
473 |
} |
474 |
|
475 |
if (len == 0) { |
476 |
vc->receive_disabled = 1;
|
477 |
} |
478 |
|
479 |
ret = (ret >= 0) ? ret : len;
|
480 |
|
481 |
} |
482 |
|
483 |
return ret;
|
484 |
} |
485 |
|
486 |
void qemu_purge_queued_packets(VLANClientState *vc)
|
487 |
{ |
488 |
NetQueue *queue; |
489 |
|
490 |
if (!vc->peer && !vc->vlan) {
|
491 |
return;
|
492 |
} |
493 |
|
494 |
if (vc->peer) {
|
495 |
queue = vc->peer->send_queue; |
496 |
} else {
|
497 |
queue = vc->vlan->send_queue; |
498 |
} |
499 |
|
500 |
qemu_net_queue_purge(queue, vc); |
501 |
} |
502 |
|
503 |
void qemu_flush_queued_packets(VLANClientState *vc)
|
504 |
{ |
505 |
NetQueue *queue; |
506 |
|
507 |
vc->receive_disabled = 0;
|
508 |
|
509 |
if (vc->vlan) {
|
510 |
queue = vc->vlan->send_queue; |
511 |
} else {
|
512 |
queue = vc->send_queue; |
513 |
} |
514 |
|
515 |
qemu_net_queue_flush(queue); |
516 |
} |
517 |
|
518 |
static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
|
519 |
unsigned flags,
|
520 |
const uint8_t *buf, int size, |
521 |
NetPacketSent *sent_cb) |
522 |
{ |
523 |
NetQueue *queue; |
524 |
|
525 |
#ifdef DEBUG_NET
|
526 |
printf("qemu_send_packet_async:\n");
|
527 |
hex_dump(stdout, buf, size); |
528 |
#endif
|
529 |
|
530 |
if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
531 |
return size;
|
532 |
} |
533 |
|
534 |
if (sender->peer) {
|
535 |
queue = sender->peer->send_queue; |
536 |
} else {
|
537 |
queue = sender->vlan->send_queue; |
538 |
} |
539 |
|
540 |
return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
|
541 |
} |
542 |
|
543 |
ssize_t qemu_send_packet_async(VLANClientState *sender, |
544 |
const uint8_t *buf, int size, |
545 |
NetPacketSent *sent_cb) |
546 |
{ |
547 |
return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
|
548 |
buf, size, sent_cb); |
549 |
} |
550 |
|
551 |
void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
552 |
{ |
553 |
qemu_send_packet_async(vc, buf, size, NULL);
|
554 |
} |
555 |
|
556 |
ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
557 |
{ |
558 |
return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
|
559 |
buf, size, NULL);
|
560 |
} |
561 |
|
562 |
static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
563 |
int iovcnt)
|
564 |
{ |
565 |
uint8_t buffer[4096];
|
566 |
size_t offset = 0;
|
567 |
int i;
|
568 |
|
569 |
for (i = 0; i < iovcnt; i++) { |
570 |
size_t len; |
571 |
|
572 |
len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
|
573 |
memcpy(buffer + offset, iov[i].iov_base, len); |
574 |
offset += len; |
575 |
} |
576 |
|
577 |
return vc->receive(vc, buffer, offset);
|
578 |
} |
579 |
|
580 |
static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
581 |
{ |
582 |
size_t offset = 0;
|
583 |
int i;
|
584 |
|
585 |
for (i = 0; i < iovcnt; i++) |
586 |
offset += iov[i].iov_len; |
587 |
return offset;
|
588 |
} |
589 |
|
590 |
static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
591 |
unsigned flags,
|
592 |
const struct iovec *iov, |
593 |
int iovcnt,
|
594 |
void *opaque)
|
595 |
{ |
596 |
VLANClientState *vc = opaque; |
597 |
|
598 |
if (vc->link_down) {
|
599 |
return calc_iov_length(iov, iovcnt);
|
600 |
} |
601 |
|
602 |
if (vc->receive_iov) {
|
603 |
return vc->receive_iov(vc, iov, iovcnt);
|
604 |
} else {
|
605 |
return vc_sendv_compat(vc, iov, iovcnt);
|
606 |
} |
607 |
} |
608 |
|
609 |
static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
|
610 |
unsigned flags,
|
611 |
const struct iovec *iov, |
612 |
int iovcnt,
|
613 |
void *opaque)
|
614 |
{ |
615 |
VLANState *vlan = opaque; |
616 |
VLANClientState *vc; |
617 |
ssize_t ret = -1;
|
618 |
|
619 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
620 |
ssize_t len; |
621 |
|
622 |
if (vc == sender) {
|
623 |
continue;
|
624 |
} |
625 |
|
626 |
if (vc->link_down) {
|
627 |
ret = calc_iov_length(iov, iovcnt); |
628 |
continue;
|
629 |
} |
630 |
|
631 |
assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
632 |
|
633 |
if (vc->receive_iov) {
|
634 |
len = vc->receive_iov(vc, iov, iovcnt); |
635 |
} else {
|
636 |
len = vc_sendv_compat(vc, iov, iovcnt); |
637 |
} |
638 |
|
639 |
ret = (ret >= 0) ? ret : len;
|
640 |
} |
641 |
|
642 |
return ret;
|
643 |
} |
644 |
|
645 |
ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
646 |
const struct iovec *iov, int iovcnt, |
647 |
NetPacketSent *sent_cb) |
648 |
{ |
649 |
NetQueue *queue; |
650 |
|
651 |
if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
652 |
return calc_iov_length(iov, iovcnt);
|
653 |
} |
654 |
|
655 |
if (sender->peer) {
|
656 |
queue = sender->peer->send_queue; |
657 |
} else {
|
658 |
queue = sender->vlan->send_queue; |
659 |
} |
660 |
|
661 |
return qemu_net_queue_send_iov(queue, sender,
|
662 |
QEMU_NET_PACKET_FLAG_NONE, |
663 |
iov, iovcnt, sent_cb); |
664 |
} |
665 |
|
666 |
ssize_t |
667 |
qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
668 |
{ |
669 |
return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
670 |
} |
671 |
|
672 |
/* find or alloc a new VLAN */
|
673 |
VLANState *qemu_find_vlan(int id, int allocate) |
674 |
{ |
675 |
VLANState *vlan; |
676 |
|
677 |
QTAILQ_FOREACH(vlan, &vlans, next) { |
678 |
if (vlan->id == id) {
|
679 |
return vlan;
|
680 |
} |
681 |
} |
682 |
|
683 |
if (!allocate) {
|
684 |
return NULL; |
685 |
} |
686 |
|
687 |
vlan = qemu_mallocz(sizeof(VLANState));
|
688 |
vlan->id = id; |
689 |
QTAILQ_INIT(&vlan->clients); |
690 |
|
691 |
vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
692 |
qemu_vlan_deliver_packet_iov, |
693 |
vlan); |
694 |
|
695 |
QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
696 |
|
697 |
return vlan;
|
698 |
} |
699 |
|
700 |
VLANClientState *qemu_find_netdev(const char *id) |
701 |
{ |
702 |
VLANClientState *vc; |
703 |
|
704 |
QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
705 |
if (!strcmp(vc->name, id)) {
|
706 |
return vc;
|
707 |
} |
708 |
} |
709 |
|
710 |
return NULL; |
711 |
} |
712 |
|
713 |
static int nic_get_free_idx(void) |
714 |
{ |
715 |
int index;
|
716 |
|
717 |
for (index = 0; index < MAX_NICS; index++) |
718 |
if (!nd_table[index].used)
|
719 |
return index;
|
720 |
return -1; |
721 |
} |
722 |
|
723 |
int qemu_show_nic_models(const char *arg, const char *const *models) |
724 |
{ |
725 |
int i;
|
726 |
|
727 |
if (!arg || strcmp(arg, "?")) |
728 |
return 0; |
729 |
|
730 |
fprintf(stderr, "qemu: Supported NIC models: ");
|
731 |
for (i = 0 ; models[i]; i++) |
732 |
fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
733 |
return 1; |
734 |
} |
735 |
|
736 |
void qemu_check_nic_model(NICInfo *nd, const char *model) |
737 |
{ |
738 |
const char *models[2]; |
739 |
|
740 |
models[0] = model;
|
741 |
models[1] = NULL; |
742 |
|
743 |
if (qemu_show_nic_models(nd->model, models))
|
744 |
exit(0);
|
745 |
if (qemu_find_nic_model(nd, models, model) < 0) |
746 |
exit(1);
|
747 |
} |
748 |
|
749 |
int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
750 |
const char *default_model) |
751 |
{ |
752 |
int i;
|
753 |
|
754 |
if (!nd->model)
|
755 |
nd->model = qemu_strdup(default_model); |
756 |
|
757 |
for (i = 0 ; models[i]; i++) { |
758 |
if (strcmp(nd->model, models[i]) == 0) |
759 |
return i;
|
760 |
} |
761 |
|
762 |
qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
|
763 |
return -1; |
764 |
} |
765 |
|
766 |
int net_handle_fd_param(Monitor *mon, const char *param) |
767 |
{ |
768 |
if (!qemu_isdigit(param[0])) { |
769 |
int fd;
|
770 |
|
771 |
fd = monitor_get_fd(mon, param); |
772 |
if (fd == -1) { |
773 |
qemu_error("No file descriptor named %s found", param);
|
774 |
return -1; |
775 |
} |
776 |
|
777 |
return fd;
|
778 |
} else {
|
779 |
return strtol(param, NULL, 0); |
780 |
} |
781 |
} |
782 |
|
783 |
static int net_init_nic(QemuOpts *opts, |
784 |
Monitor *mon, |
785 |
const char *name, |
786 |
VLANState *vlan) |
787 |
{ |
788 |
int idx;
|
789 |
NICInfo *nd; |
790 |
const char *netdev; |
791 |
|
792 |
idx = nic_get_free_idx(); |
793 |
if (idx == -1 || nb_nics >= MAX_NICS) { |
794 |
qemu_error("Too Many NICs\n");
|
795 |
return -1; |
796 |
} |
797 |
|
798 |
nd = &nd_table[idx]; |
799 |
|
800 |
memset(nd, 0, sizeof(*nd)); |
801 |
|
802 |
if ((netdev = qemu_opt_get(opts, "netdev"))) { |
803 |
nd->netdev = qemu_find_netdev(netdev); |
804 |
if (!nd->netdev) {
|
805 |
qemu_error("netdev '%s' not found\n", netdev);
|
806 |
return -1; |
807 |
} |
808 |
} else {
|
809 |
assert(vlan); |
810 |
nd->vlan = vlan; |
811 |
} |
812 |
if (name) {
|
813 |
nd->name = qemu_strdup(name); |
814 |
} |
815 |
if (qemu_opt_get(opts, "model")) { |
816 |
nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
|
817 |
} |
818 |
if (qemu_opt_get(opts, "addr")) { |
819 |
nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
|
820 |
} |
821 |
|
822 |
nd->macaddr[0] = 0x52; |
823 |
nd->macaddr[1] = 0x54; |
824 |
nd->macaddr[2] = 0x00; |
825 |
nd->macaddr[3] = 0x12; |
826 |
nd->macaddr[4] = 0x34; |
827 |
nd->macaddr[5] = 0x56 + idx; |
828 |
|
829 |
if (qemu_opt_get(opts, "macaddr") && |
830 |
net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
831 |
qemu_error("invalid syntax for ethernet address\n");
|
832 |
return -1; |
833 |
} |
834 |
|
835 |
nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
|
836 |
if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
|
837 |
(nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
838 |
qemu_error("invalid # of vectors: %d\n", nd->nvectors);
|
839 |
return -1; |
840 |
} |
841 |
|
842 |
nd->used = 1;
|
843 |
if (vlan) {
|
844 |
nd->vlan->nb_guest_devs++; |
845 |
} |
846 |
nb_nics++; |
847 |
|
848 |
return idx;
|
849 |
} |
850 |
|
851 |
#define NET_COMMON_PARAMS_DESC \
|
852 |
{ \ |
853 |
.name = "type", \
|
854 |
.type = QEMU_OPT_STRING, \ |
855 |
.help = "net client type (nic, tap etc.)", \
|
856 |
}, { \ |
857 |
.name = "vlan", \
|
858 |
.type = QEMU_OPT_NUMBER, \ |
859 |
.help = "vlan number", \
|
860 |
}, { \ |
861 |
.name = "name", \
|
862 |
.type = QEMU_OPT_STRING, \ |
863 |
.help = "identifier for monitor commands", \
|
864 |
} |
865 |
|
866 |
typedef int (*net_client_init_func)(QemuOpts *opts, |
867 |
Monitor *mon, |
868 |
const char *name, |
869 |
VLANState *vlan); |
870 |
|
871 |
/* magic number, but compiler will warn if too small */
|
872 |
#define NET_MAX_DESC 20 |
873 |
|
874 |
static struct { |
875 |
const char *type; |
876 |
net_client_init_func init; |
877 |
QemuOptDesc desc[NET_MAX_DESC]; |
878 |
} net_client_types[] = { |
879 |
{ |
880 |
.type = "none",
|
881 |
.desc = { |
882 |
NET_COMMON_PARAMS_DESC, |
883 |
{ /* end of list */ }
|
884 |
}, |
885 |
}, { |
886 |
.type = "nic",
|
887 |
.init = net_init_nic, |
888 |
.desc = { |
889 |
NET_COMMON_PARAMS_DESC, |
890 |
{ |
891 |
.name = "netdev",
|
892 |
.type = QEMU_OPT_STRING, |
893 |
.help = "id of -netdev to connect to",
|
894 |
}, |
895 |
{ |
896 |
.name = "macaddr",
|
897 |
.type = QEMU_OPT_STRING, |
898 |
.help = "MAC address",
|
899 |
}, { |
900 |
.name = "model",
|
901 |
.type = QEMU_OPT_STRING, |
902 |
.help = "device model (e1000, rtl8139, virtio etc.)",
|
903 |
}, { |
904 |
.name = "addr",
|
905 |
.type = QEMU_OPT_STRING, |
906 |
.help = "PCI device address",
|
907 |
}, { |
908 |
.name = "vectors",
|
909 |
.type = QEMU_OPT_NUMBER, |
910 |
.help = "number of MSI-x vectors, 0 to disable MSI-X",
|
911 |
}, |
912 |
{ /* end of list */ }
|
913 |
}, |
914 |
#ifdef CONFIG_SLIRP
|
915 |
}, { |
916 |
.type = "user",
|
917 |
.init = net_init_slirp, |
918 |
.desc = { |
919 |
NET_COMMON_PARAMS_DESC, |
920 |
{ |
921 |
.name = "hostname",
|
922 |
.type = QEMU_OPT_STRING, |
923 |
.help = "client hostname reported by the builtin DHCP server",
|
924 |
}, { |
925 |
.name = "restrict",
|
926 |
.type = QEMU_OPT_STRING, |
927 |
.help = "isolate the guest from the host (y|yes|n|no)",
|
928 |
}, { |
929 |
.name = "ip",
|
930 |
.type = QEMU_OPT_STRING, |
931 |
.help = "legacy parameter, use net= instead",
|
932 |
}, { |
933 |
.name = "net",
|
934 |
.type = QEMU_OPT_STRING, |
935 |
.help = "IP address and optional netmask",
|
936 |
}, { |
937 |
.name = "host",
|
938 |
.type = QEMU_OPT_STRING, |
939 |
.help = "guest-visible address of the host",
|
940 |
}, { |
941 |
.name = "tftp",
|
942 |
.type = QEMU_OPT_STRING, |
943 |
.help = "root directory of the built-in TFTP server",
|
944 |
}, { |
945 |
.name = "bootfile",
|
946 |
.type = QEMU_OPT_STRING, |
947 |
.help = "BOOTP filename, for use with tftp=",
|
948 |
}, { |
949 |
.name = "dhcpstart",
|
950 |
.type = QEMU_OPT_STRING, |
951 |
.help = "the first of the 16 IPs the built-in DHCP server can assign",
|
952 |
}, { |
953 |
.name = "dns",
|
954 |
.type = QEMU_OPT_STRING, |
955 |
.help = "guest-visible address of the virtual nameserver",
|
956 |
}, { |
957 |
.name = "smb",
|
958 |
.type = QEMU_OPT_STRING, |
959 |
.help = "root directory of the built-in SMB server",
|
960 |
}, { |
961 |
.name = "smbserver",
|
962 |
.type = QEMU_OPT_STRING, |
963 |
.help = "IP address of the built-in SMB server",
|
964 |
}, { |
965 |
.name = "hostfwd",
|
966 |
.type = QEMU_OPT_STRING, |
967 |
.help = "guest port number to forward incoming TCP or UDP connections",
|
968 |
}, { |
969 |
.name = "guestfwd",
|
970 |
.type = QEMU_OPT_STRING, |
971 |
.help = "IP address and port to forward guest TCP connections",
|
972 |
}, |
973 |
{ /* end of list */ }
|
974 |
}, |
975 |
#endif
|
976 |
}, { |
977 |
.type = "tap",
|
978 |
.init = net_init_tap, |
979 |
.desc = { |
980 |
NET_COMMON_PARAMS_DESC, |
981 |
{ |
982 |
.name = "ifname",
|
983 |
.type = QEMU_OPT_STRING, |
984 |
.help = "interface name",
|
985 |
}, |
986 |
#ifndef _WIN32
|
987 |
{ |
988 |
.name = "fd",
|
989 |
.type = QEMU_OPT_STRING, |
990 |
.help = "file descriptor of an already opened tap",
|
991 |
}, { |
992 |
.name = "script",
|
993 |
.type = QEMU_OPT_STRING, |
994 |
.help = "script to initialize the interface",
|
995 |
}, { |
996 |
.name = "downscript",
|
997 |
.type = QEMU_OPT_STRING, |
998 |
.help = "script to shut down the interface",
|
999 |
}, { |
1000 |
.name = "sndbuf",
|
1001 |
.type = QEMU_OPT_SIZE, |
1002 |
.help = "send buffer limit"
|
1003 |
}, { |
1004 |
.name = "vnet_hdr",
|
1005 |
.type = QEMU_OPT_BOOL, |
1006 |
.help = "enable the IFF_VNET_HDR flag on the tap interface"
|
1007 |
}, |
1008 |
#endif /* _WIN32 */ |
1009 |
{ /* end of list */ }
|
1010 |
}, |
1011 |
}, { |
1012 |
.type = "socket",
|
1013 |
.init = net_init_socket, |
1014 |
.desc = { |
1015 |
NET_COMMON_PARAMS_DESC, |
1016 |
{ |
1017 |
.name = "fd",
|
1018 |
.type = QEMU_OPT_STRING, |
1019 |
.help = "file descriptor of an already opened socket",
|
1020 |
}, { |
1021 |
.name = "listen",
|
1022 |
.type = QEMU_OPT_STRING, |
1023 |
.help = "port number, and optional hostname, to listen on",
|
1024 |
}, { |
1025 |
.name = "connect",
|
1026 |
.type = QEMU_OPT_STRING, |
1027 |
.help = "port number, and optional hostname, to connect to",
|
1028 |
}, { |
1029 |
.name = "mcast",
|
1030 |
.type = QEMU_OPT_STRING, |
1031 |
.help = "UDP multicast address and port number",
|
1032 |
}, |
1033 |
{ /* end of list */ }
|
1034 |
}, |
1035 |
#ifdef CONFIG_VDE
|
1036 |
}, { |
1037 |
.type = "vde",
|
1038 |
.init = net_init_vde, |
1039 |
.desc = { |
1040 |
NET_COMMON_PARAMS_DESC, |
1041 |
{ |
1042 |
.name = "sock",
|
1043 |
.type = QEMU_OPT_STRING, |
1044 |
.help = "socket path",
|
1045 |
}, { |
1046 |
.name = "port",
|
1047 |
.type = QEMU_OPT_NUMBER, |
1048 |
.help = "port number",
|
1049 |
}, { |
1050 |
.name = "group",
|
1051 |
.type = QEMU_OPT_STRING, |
1052 |
.help = "group owner of socket",
|
1053 |
}, { |
1054 |
.name = "mode",
|
1055 |
.type = QEMU_OPT_NUMBER, |
1056 |
.help = "permissions for socket",
|
1057 |
}, |
1058 |
{ /* end of list */ }
|
1059 |
}, |
1060 |
#endif
|
1061 |
}, { |
1062 |
.type = "dump",
|
1063 |
.init = net_init_dump, |
1064 |
.desc = { |
1065 |
NET_COMMON_PARAMS_DESC, |
1066 |
{ |
1067 |
.name = "len",
|
1068 |
.type = QEMU_OPT_SIZE, |
1069 |
.help = "per-packet size limit (64k default)",
|
1070 |
}, { |
1071 |
.name = "file",
|
1072 |
.type = QEMU_OPT_STRING, |
1073 |
.help = "dump file path (default is qemu-vlan0.pcap)",
|
1074 |
}, |
1075 |
{ /* end of list */ }
|
1076 |
}, |
1077 |
}, |
1078 |
{ /* end of list */ }
|
1079 |
}; |
1080 |
|
1081 |
int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
1082 |
{ |
1083 |
const char *name; |
1084 |
const char *type; |
1085 |
int i;
|
1086 |
|
1087 |
type = qemu_opt_get(opts, "type");
|
1088 |
if (!type) {
|
1089 |
qemu_error("No type specified for -net\n");
|
1090 |
return -1; |
1091 |
} |
1092 |
|
1093 |
if (is_netdev) {
|
1094 |
if (strcmp(type, "tap") != 0 && |
1095 |
#ifdef CONFIG_SLIRP
|
1096 |
strcmp(type, "user") != 0 && |
1097 |
#endif
|
1098 |
#ifdef CONFIG_VDE
|
1099 |
strcmp(type, "vde") != 0 && |
1100 |
#endif
|
1101 |
strcmp(type, "socket") != 0) { |
1102 |
qemu_error("The '%s' network backend type is not valid with -netdev\n",
|
1103 |
type); |
1104 |
return -1; |
1105 |
} |
1106 |
|
1107 |
if (qemu_opt_get(opts, "vlan")) { |
1108 |
qemu_error("The 'vlan' parameter is not valid with -netdev\n");
|
1109 |
return -1; |
1110 |
} |
1111 |
if (qemu_opt_get(opts, "name")) { |
1112 |
qemu_error("The 'name' parameter is not valid with -netdev\n");
|
1113 |
return -1; |
1114 |
} |
1115 |
if (!qemu_opts_id(opts)) {
|
1116 |
qemu_error("The id= parameter is required with -netdev\n");
|
1117 |
return -1; |
1118 |
} |
1119 |
} |
1120 |
|
1121 |
name = qemu_opts_id(opts); |
1122 |
if (!name) {
|
1123 |
name = qemu_opt_get(opts, "name");
|
1124 |
} |
1125 |
|
1126 |
for (i = 0; net_client_types[i].type != NULL; i++) { |
1127 |
if (!strcmp(net_client_types[i].type, type)) {
|
1128 |
VLANState *vlan = NULL;
|
1129 |
|
1130 |
if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
1131 |
return -1; |
1132 |
} |
1133 |
|
1134 |
/* Do not add to a vlan if it's a -netdev or a nic with a
|
1135 |
* netdev= parameter. */
|
1136 |
if (!(is_netdev ||
|
1137 |
(strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
1138 |
vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
1139 |
} |
1140 |
|
1141 |
if (net_client_types[i].init) {
|
1142 |
return net_client_types[i].init(opts, mon, name, vlan);
|
1143 |
} else {
|
1144 |
return 0; |
1145 |
} |
1146 |
} |
1147 |
} |
1148 |
|
1149 |
qemu_error("Invalid -net type '%s'\n", type);
|
1150 |
return -1; |
1151 |
} |
1152 |
|
1153 |
void net_client_uninit(NICInfo *nd)
|
1154 |
{ |
1155 |
if (nd->vlan) {
|
1156 |
nd->vlan->nb_guest_devs--; |
1157 |
} |
1158 |
nb_nics--; |
1159 |
|
1160 |
qemu_free(nd->model); |
1161 |
qemu_free(nd->name); |
1162 |
qemu_free(nd->devaddr); |
1163 |
|
1164 |
nd->used = 0;
|
1165 |
} |
1166 |
|
1167 |
static int net_host_check_device(const char *device) |
1168 |
{ |
1169 |
int i;
|
1170 |
const char *valid_param_list[] = { "tap", "socket", "dump" |
1171 |
#ifdef CONFIG_SLIRP
|
1172 |
,"user"
|
1173 |
#endif
|
1174 |
#ifdef CONFIG_VDE
|
1175 |
,"vde"
|
1176 |
#endif
|
1177 |
}; |
1178 |
for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
1179 |
if (!strncmp(valid_param_list[i], device,
|
1180 |
strlen(valid_param_list[i]))) |
1181 |
return 1; |
1182 |
} |
1183 |
|
1184 |
return 0; |
1185 |
} |
1186 |
|
1187 |
void net_host_device_add(Monitor *mon, const QDict *qdict) |
1188 |
{ |
1189 |
const char *device = qdict_get_str(qdict, "device"); |
1190 |
const char *opts_str = qdict_get_try_str(qdict, "opts"); |
1191 |
QemuOpts *opts; |
1192 |
|
1193 |
if (!net_host_check_device(device)) {
|
1194 |
monitor_printf(mon, "invalid host network device %s\n", device);
|
1195 |
return;
|
1196 |
} |
1197 |
|
1198 |
opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); |
1199 |
if (!opts) {
|
1200 |
monitor_printf(mon, "parsing network options '%s' failed\n",
|
1201 |
opts_str ? opts_str : "");
|
1202 |
return;
|
1203 |
} |
1204 |
|
1205 |
qemu_opt_set(opts, "type", device);
|
1206 |
|
1207 |
if (net_client_init(mon, opts, 0) < 0) { |
1208 |
monitor_printf(mon, "adding host network device %s failed\n", device);
|
1209 |
} |
1210 |
} |
1211 |
|
1212 |
void net_host_device_remove(Monitor *mon, const QDict *qdict) |
1213 |
{ |
1214 |
VLANClientState *vc; |
1215 |
int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1216 |
const char *device = qdict_get_str(qdict, "device"); |
1217 |
|
1218 |
vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
1219 |
if (!vc) {
|
1220 |
return;
|
1221 |
} |
1222 |
if (!net_host_check_device(vc->model)) {
|
1223 |
monitor_printf(mon, "invalid host network device %s\n", device);
|
1224 |
return;
|
1225 |
} |
1226 |
qemu_del_vlan_client(vc); |
1227 |
} |
1228 |
|
1229 |
void net_set_boot_mask(int net_boot_mask) |
1230 |
{ |
1231 |
int i;
|
1232 |
|
1233 |
/* Only the first four NICs may be bootable */
|
1234 |
net_boot_mask = net_boot_mask & 0xF;
|
1235 |
|
1236 |
for (i = 0; i < nb_nics; i++) { |
1237 |
if (net_boot_mask & (1 << i)) { |
1238 |
nd_table[i].bootable = 1;
|
1239 |
net_boot_mask &= ~(1 << i);
|
1240 |
} |
1241 |
} |
1242 |
|
1243 |
if (net_boot_mask) {
|
1244 |
fprintf(stderr, "Cannot boot from non-existent NIC\n");
|
1245 |
exit(1);
|
1246 |
} |
1247 |
} |
1248 |
|
1249 |
void do_info_network(Monitor *mon)
|
1250 |
{ |
1251 |
VLANState *vlan; |
1252 |
|
1253 |
QTAILQ_FOREACH(vlan, &vlans, next) { |
1254 |
VLANClientState *vc; |
1255 |
|
1256 |
monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
|
1257 |
|
1258 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1259 |
monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
|
1260 |
} |
1261 |
} |
1262 |
} |
1263 |
|
1264 |
void do_set_link(Monitor *mon, const QDict *qdict) |
1265 |
{ |
1266 |
VLANState *vlan; |
1267 |
VLANClientState *vc = NULL;
|
1268 |
const char *name = qdict_get_str(qdict, "name"); |
1269 |
const char *up_or_down = qdict_get_str(qdict, "up_or_down"); |
1270 |
|
1271 |
QTAILQ_FOREACH(vlan, &vlans, next) { |
1272 |
QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1273 |
if (strcmp(vc->name, name) == 0) { |
1274 |
goto done;
|
1275 |
} |
1276 |
} |
1277 |
} |
1278 |
done:
|
1279 |
|
1280 |
if (!vc) {
|
1281 |
monitor_printf(mon, "could not find network device '%s'\n", name);
|
1282 |
return;
|
1283 |
} |
1284 |
|
1285 |
if (strcmp(up_or_down, "up") == 0) |
1286 |
vc->link_down = 0;
|
1287 |
else if (strcmp(up_or_down, "down") == 0) |
1288 |
vc->link_down = 1;
|
1289 |
else
|
1290 |
monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
|
1291 |
"valid\n", up_or_down);
|
1292 |
|
1293 |
if (vc->link_status_changed)
|
1294 |
vc->link_status_changed(vc); |
1295 |
} |
1296 |
|
1297 |
void net_cleanup(void) |
1298 |
{ |
1299 |
VLANState *vlan; |
1300 |
VLANClientState *vc, *next_vc; |
1301 |
|
1302 |
QTAILQ_FOREACH(vlan, &vlans, next) { |
1303 |
QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
1304 |
qemu_del_vlan_client(vc); |
1305 |
} |
1306 |
} |
1307 |
|
1308 |
QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
1309 |
qemu_del_vlan_client(vc); |
1310 |
} |
1311 |
} |
1312 |
|
1313 |
static void net_check_clients(void) |
1314 |
{ |
1315 |
VLANState *vlan; |
1316 |
|
1317 |
QTAILQ_FOREACH(vlan, &vlans, next) { |
1318 |
if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
1319 |
continue;
|
1320 |
if (vlan->nb_guest_devs == 0) |
1321 |
fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
1322 |
if (vlan->nb_host_devs == 0) |
1323 |
fprintf(stderr, |
1324 |
"Warning: vlan %d is not connected to host network\n",
|
1325 |
vlan->id); |
1326 |
} |
1327 |
} |
1328 |
|
1329 |
static int net_init_client(QemuOpts *opts, void *dummy) |
1330 |
{ |
1331 |
if (net_client_init(NULL, opts, 0) < 0) |
1332 |
return -1; |
1333 |
return 0; |
1334 |
} |
1335 |
|
1336 |
static int net_init_netdev(QemuOpts *opts, void *dummy) |
1337 |
{ |
1338 |
return net_client_init(NULL, opts, 1); |
1339 |
} |
1340 |
|
1341 |
int net_init_clients(void) |
1342 |
{ |
1343 |
if (QTAILQ_EMPTY(&qemu_net_opts.head)) {
|
1344 |
/* if no clients, we use a default config */
|
1345 |
qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); |
1346 |
#ifdef CONFIG_SLIRP
|
1347 |
qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); |
1348 |
#endif
|
1349 |
} |
1350 |
|
1351 |
QTAILQ_INIT(&vlans); |
1352 |
QTAILQ_INIT(&non_vlan_clients); |
1353 |
|
1354 |
if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) |
1355 |
return -1; |
1356 |
|
1357 |
if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { |
1358 |
return -1; |
1359 |
} |
1360 |
|
1361 |
net_check_clients(); |
1362 |
|
1363 |
return 0; |
1364 |
} |
1365 |
|
1366 |
int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
1367 |
{ |
1368 |
#if defined(CONFIG_SLIRP)
|
1369 |
int ret;
|
1370 |
if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
|
1371 |
return ret;
|
1372 |
} |
1373 |
#endif
|
1374 |
|
1375 |
if (!qemu_opts_parse(opts_list, optarg, "type")) { |
1376 |
return -1; |
1377 |
} |
1378 |
|
1379 |
return 0; |
1380 |
} |