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