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