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