net: Use iov helper functions
[qemu.git] / net.c
blob436c24c59851b49f1397482c5d3bcded8c2af759
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "net.h"
26 #include "config-host.h"
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 #include "iov.h"
41 static QTAILQ_HEAD(, VLANState) vlans;
42 static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
44 int default_net = 1;
46 /***********************************************************/
47 /* network device redirectors */
49 #if defined(DEBUG_NET)
50 static void hex_dump(FILE *f, const uint8_t *buf, int size)
52 int len, i, j, c;
54 for(i=0;i<size;i+=16) {
55 len = size - i;
56 if (len > 16)
57 len = 16;
58 fprintf(f, "%08x ", i);
59 for(j=0;j<16;j++) {
60 if (j < len)
61 fprintf(f, " %02x", buf[i+j]);
62 else
63 fprintf(f, " ");
65 fprintf(f, " ");
66 for(j=0;j<len;j++) {
67 c = buf[i+j];
68 if (c < ' ' || c > '~')
69 c = '.';
70 fprintf(f, "%c", c);
72 fprintf(f, "\n");
75 #endif
77 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
79 const char *p, *p1;
80 int len;
81 p = *pp;
82 p1 = strchr(p, sep);
83 if (!p1)
84 return -1;
85 len = p1 - p;
86 p1++;
87 if (buf_size > 0) {
88 if (len > buf_size - 1)
89 len = buf_size - 1;
90 memcpy(buf, p, len);
91 buf[len] = '\0';
93 *pp = p1;
94 return 0;
97 int parse_host_src_port(struct sockaddr_in *haddr,
98 struct sockaddr_in *saddr,
99 const char *input_str)
101 char *str = qemu_strdup(input_str);
102 char *host_str = str;
103 char *src_str;
104 const char *src_str2;
105 char *ptr;
108 * Chop off any extra arguments at the end of the string which
109 * would start with a comma, then fill in the src port information
110 * if it was provided else use the "any address" and "any port".
112 if ((ptr = strchr(str,',')))
113 *ptr = '\0';
115 if ((src_str = strchr(input_str,'@'))) {
116 *src_str = '\0';
117 src_str++;
120 if (parse_host_port(haddr, host_str) < 0)
121 goto fail;
123 src_str2 = src_str;
124 if (!src_str || *src_str == '\0')
125 src_str2 = ":0";
127 if (parse_host_port(saddr, src_str2) < 0)
128 goto fail;
130 free(str);
131 return(0);
133 fail:
134 free(str);
135 return -1;
138 int parse_host_port(struct sockaddr_in *saddr, const char *str)
140 char buf[512];
141 struct hostent *he;
142 const char *p, *r;
143 int port;
145 p = str;
146 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
147 return -1;
148 saddr->sin_family = AF_INET;
149 if (buf[0] == '\0') {
150 saddr->sin_addr.s_addr = 0;
151 } else {
152 if (qemu_isdigit(buf[0])) {
153 if (!inet_aton(buf, &saddr->sin_addr))
154 return -1;
155 } else {
156 if ((he = gethostbyname(buf)) == NULL)
157 return - 1;
158 saddr->sin_addr = *(struct in_addr *)he->h_addr;
161 port = strtol(p, (char **)&r, 0);
162 if (r == p)
163 return -1;
164 saddr->sin_port = htons(port);
165 return 0;
168 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
170 snprintf(vc->info_str, sizeof(vc->info_str),
171 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
172 vc->model,
173 macaddr[0], macaddr[1], macaddr[2],
174 macaddr[3], macaddr[4], macaddr[5]);
177 void qemu_macaddr_default_if_unset(MACAddr *macaddr)
179 static int index = 0;
180 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
182 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
183 return;
184 macaddr->a[0] = 0x52;
185 macaddr->a[1] = 0x54;
186 macaddr->a[2] = 0x00;
187 macaddr->a[3] = 0x12;
188 macaddr->a[4] = 0x34;
189 macaddr->a[5] = 0x56 + index++;
192 static char *assign_name(VLANClientState *vc1, const char *model)
194 VLANState *vlan;
195 char buf[256];
196 int id = 0;
198 QTAILQ_FOREACH(vlan, &vlans, next) {
199 VLANClientState *vc;
201 QTAILQ_FOREACH(vc, &vlan->clients, next) {
202 if (vc != vc1 && strcmp(vc->model, model) == 0) {
203 id++;
208 snprintf(buf, sizeof(buf), "%s.%d", model, id);
210 return qemu_strdup(buf);
213 static ssize_t qemu_deliver_packet(VLANClientState *sender,
214 unsigned flags,
215 const uint8_t *data,
216 size_t size,
217 void *opaque);
218 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
219 unsigned flags,
220 const struct iovec *iov,
221 int iovcnt,
222 void *opaque);
224 VLANClientState *qemu_new_net_client(NetClientInfo *info,
225 VLANState *vlan,
226 VLANClientState *peer,
227 const char *model,
228 const char *name)
230 VLANClientState *vc;
232 assert(info->size >= sizeof(VLANClientState));
234 vc = qemu_mallocz(info->size);
236 vc->info = info;
237 vc->model = qemu_strdup(model);
238 if (name) {
239 vc->name = qemu_strdup(name);
240 } else {
241 vc->name = assign_name(vc, model);
244 if (vlan) {
245 assert(!peer);
246 vc->vlan = vlan;
247 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
248 } else {
249 if (peer) {
250 assert(!peer->peer);
251 vc->peer = peer;
252 peer->peer = vc;
254 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
256 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
257 qemu_deliver_packet_iov,
258 vc);
261 return vc;
264 NICState *qemu_new_nic(NetClientInfo *info,
265 NICConf *conf,
266 const char *model,
267 const char *name,
268 void *opaque)
270 VLANClientState *nc;
271 NICState *nic;
273 assert(info->type == NET_CLIENT_TYPE_NIC);
274 assert(info->size >= sizeof(NICState));
276 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
278 nic = DO_UPCAST(NICState, nc, nc);
279 nic->conf = conf;
280 nic->opaque = opaque;
282 return nic;
285 static void qemu_cleanup_vlan_client(VLANClientState *vc)
287 if (vc->vlan) {
288 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
289 } else {
290 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
293 if (vc->info->cleanup) {
294 vc->info->cleanup(vc);
298 static void qemu_free_vlan_client(VLANClientState *vc)
300 if (!vc->vlan) {
301 if (vc->send_queue) {
302 qemu_del_net_queue(vc->send_queue);
304 if (vc->peer) {
305 vc->peer->peer = NULL;
308 qemu_free(vc->name);
309 qemu_free(vc->model);
310 qemu_free(vc);
313 void qemu_del_vlan_client(VLANClientState *vc)
315 /* If there is a peer NIC, delete and cleanup client, but do not free. */
316 if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
317 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
318 if (nic->peer_deleted) {
319 return;
321 nic->peer_deleted = true;
322 /* Let NIC know peer is gone. */
323 vc->peer->link_down = true;
324 if (vc->peer->info->link_status_changed) {
325 vc->peer->info->link_status_changed(vc->peer);
327 qemu_cleanup_vlan_client(vc);
328 return;
331 /* If this is a peer NIC and peer has already been deleted, free it now. */
332 if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
333 NICState *nic = DO_UPCAST(NICState, nc, vc);
334 if (nic->peer_deleted) {
335 qemu_free_vlan_client(vc->peer);
339 qemu_cleanup_vlan_client(vc);
340 qemu_free_vlan_client(vc);
343 VLANClientState *
344 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
345 const char *client_str)
347 VLANState *vlan;
348 VLANClientState *vc;
350 vlan = qemu_find_vlan(vlan_id, 0);
351 if (!vlan) {
352 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
353 return NULL;
356 QTAILQ_FOREACH(vc, &vlan->clients, next) {
357 if (!strcmp(vc->name, client_str)) {
358 break;
361 if (!vc) {
362 monitor_printf(mon, "can't find device %s on VLAN %d\n",
363 client_str, vlan_id);
366 return vc;
369 void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
371 VLANClientState *nc;
372 VLANState *vlan;
374 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
375 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
376 func(DO_UPCAST(NICState, nc, nc), opaque);
380 QTAILQ_FOREACH(vlan, &vlans, next) {
381 QTAILQ_FOREACH(nc, &vlan->clients, next) {
382 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
383 func(DO_UPCAST(NICState, nc, nc), opaque);
389 int qemu_can_send_packet(VLANClientState *sender)
391 VLANState *vlan = sender->vlan;
392 VLANClientState *vc;
394 if (sender->peer) {
395 if (sender->peer->receive_disabled) {
396 return 0;
397 } else if (sender->peer->info->can_receive &&
398 !sender->peer->info->can_receive(sender->peer)) {
399 return 0;
400 } else {
401 return 1;
405 if (!sender->vlan) {
406 return 1;
409 QTAILQ_FOREACH(vc, &vlan->clients, next) {
410 if (vc == sender) {
411 continue;
414 /* no can_receive() handler, they can always receive */
415 if (vc->info->can_receive && !vc->info->can_receive(vc)) {
416 return 0;
419 return 1;
422 static ssize_t qemu_deliver_packet(VLANClientState *sender,
423 unsigned flags,
424 const uint8_t *data,
425 size_t size,
426 void *opaque)
428 VLANClientState *vc = opaque;
429 ssize_t ret;
431 if (vc->link_down) {
432 return size;
435 if (vc->receive_disabled) {
436 return 0;
439 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
440 ret = vc->info->receive_raw(vc, data, size);
441 } else {
442 ret = vc->info->receive(vc, data, size);
445 if (ret == 0) {
446 vc->receive_disabled = 1;
449 return ret;
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)
458 VLANState *vlan = opaque;
459 VLANClientState *vc;
460 ssize_t ret = -1;
462 QTAILQ_FOREACH(vc, &vlan->clients, next) {
463 ssize_t len;
465 if (vc == sender) {
466 continue;
469 if (vc->link_down) {
470 ret = size;
471 continue;
474 if (vc->receive_disabled) {
475 ret = 0;
476 continue;
479 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
480 len = vc->info->receive_raw(vc, buf, size);
481 } else {
482 len = vc->info->receive(vc, buf, size);
485 if (len == 0) {
486 vc->receive_disabled = 1;
489 ret = (ret >= 0) ? ret : len;
493 return ret;
496 void qemu_purge_queued_packets(VLANClientState *vc)
498 NetQueue *queue;
500 if (!vc->peer && !vc->vlan) {
501 return;
504 if (vc->peer) {
505 queue = vc->peer->send_queue;
506 } else {
507 queue = vc->vlan->send_queue;
510 qemu_net_queue_purge(queue, vc);
513 void qemu_flush_queued_packets(VLANClientState *vc)
515 NetQueue *queue;
517 vc->receive_disabled = 0;
519 if (vc->vlan) {
520 queue = vc->vlan->send_queue;
521 } else {
522 queue = vc->send_queue;
525 qemu_net_queue_flush(queue);
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)
533 NetQueue *queue;
535 #ifdef DEBUG_NET
536 printf("qemu_send_packet_async:\n");
537 hex_dump(stdout, buf, size);
538 #endif
540 if (sender->link_down || (!sender->peer && !sender->vlan)) {
541 return size;
544 if (sender->peer) {
545 queue = sender->peer->send_queue;
546 } else {
547 queue = sender->vlan->send_queue;
550 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
553 ssize_t qemu_send_packet_async(VLANClientState *sender,
554 const uint8_t *buf, int size,
555 NetPacketSent *sent_cb)
557 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
558 buf, size, sent_cb);
561 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
563 qemu_send_packet_async(vc, buf, size, NULL);
566 ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
568 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
569 buf, size, NULL);
572 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
573 int iovcnt)
575 uint8_t buffer[4096];
576 size_t offset;
578 offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer));
580 return vc->info->receive(vc, buffer, offset);
583 static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
584 unsigned flags,
585 const struct iovec *iov,
586 int iovcnt,
587 void *opaque)
589 VLANClientState *vc = opaque;
591 if (vc->link_down) {
592 return iov_size(iov, iovcnt);
595 if (vc->info->receive_iov) {
596 return vc->info->receive_iov(vc, iov, iovcnt);
597 } else {
598 return vc_sendv_compat(vc, iov, iovcnt);
602 static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
603 unsigned flags,
604 const struct iovec *iov,
605 int iovcnt,
606 void *opaque)
608 VLANState *vlan = opaque;
609 VLANClientState *vc;
610 ssize_t ret = -1;
612 QTAILQ_FOREACH(vc, &vlan->clients, next) {
613 ssize_t len;
615 if (vc == sender) {
616 continue;
619 if (vc->link_down) {
620 ret = iov_size(iov, iovcnt);
621 continue;
624 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
626 if (vc->info->receive_iov) {
627 len = vc->info->receive_iov(vc, iov, iovcnt);
628 } else {
629 len = vc_sendv_compat(vc, iov, iovcnt);
632 ret = (ret >= 0) ? ret : len;
635 return ret;
638 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
639 const struct iovec *iov, int iovcnt,
640 NetPacketSent *sent_cb)
642 NetQueue *queue;
644 if (sender->link_down || (!sender->peer && !sender->vlan)) {
645 return iov_size(iov, iovcnt);
648 if (sender->peer) {
649 queue = sender->peer->send_queue;
650 } else {
651 queue = sender->vlan->send_queue;
654 return qemu_net_queue_send_iov(queue, sender,
655 QEMU_NET_PACKET_FLAG_NONE,
656 iov, iovcnt, sent_cb);
659 ssize_t
660 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
662 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
665 /* find or alloc a new VLAN */
666 VLANState *qemu_find_vlan(int id, int allocate)
668 VLANState *vlan;
670 QTAILQ_FOREACH(vlan, &vlans, next) {
671 if (vlan->id == id) {
672 return vlan;
676 if (!allocate) {
677 return NULL;
680 vlan = qemu_mallocz(sizeof(VLANState));
681 vlan->id = id;
682 QTAILQ_INIT(&vlan->clients);
684 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
685 qemu_vlan_deliver_packet_iov,
686 vlan);
688 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
690 return vlan;
693 VLANClientState *qemu_find_netdev(const char *id)
695 VLANClientState *vc;
697 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
698 if (!strcmp(vc->name, id)) {
699 return vc;
703 return NULL;
706 static int nic_get_free_idx(void)
708 int index;
710 for (index = 0; index < MAX_NICS; index++)
711 if (!nd_table[index].used)
712 return index;
713 return -1;
716 int qemu_show_nic_models(const char *arg, const char *const *models)
718 int i;
720 if (!arg || strcmp(arg, "?"))
721 return 0;
723 fprintf(stderr, "qemu: Supported NIC models: ");
724 for (i = 0 ; models[i]; i++)
725 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
726 return 1;
729 void qemu_check_nic_model(NICInfo *nd, const char *model)
731 const char *models[2];
733 models[0] = model;
734 models[1] = NULL;
736 if (qemu_show_nic_models(nd->model, models))
737 exit(0);
738 if (qemu_find_nic_model(nd, models, model) < 0)
739 exit(1);
742 int qemu_find_nic_model(NICInfo *nd, const char * const *models,
743 const char *default_model)
745 int i;
747 if (!nd->model)
748 nd->model = qemu_strdup(default_model);
750 for (i = 0 ; models[i]; i++) {
751 if (strcmp(nd->model, models[i]) == 0)
752 return i;
755 error_report("qemu: Unsupported NIC model: %s", nd->model);
756 return -1;
759 int net_handle_fd_param(Monitor *mon, const char *param)
761 int fd;
763 if (!qemu_isdigit(param[0]) && mon) {
765 fd = monitor_get_fd(mon, param);
766 if (fd == -1) {
767 error_report("No file descriptor named %s found", param);
768 return -1;
770 } else {
771 char *endptr = NULL;
773 fd = strtol(param, &endptr, 10);
774 if (*endptr || (fd == 0 && param == endptr)) {
775 return -1;
779 return fd;
782 static int net_init_nic(QemuOpts *opts,
783 Monitor *mon,
784 const char *name,
785 VLANState *vlan)
787 int idx;
788 NICInfo *nd;
789 const char *netdev;
791 idx = nic_get_free_idx();
792 if (idx == -1 || nb_nics >= MAX_NICS) {
793 error_report("Too Many NICs");
794 return -1;
797 nd = &nd_table[idx];
799 memset(nd, 0, sizeof(*nd));
801 if ((netdev = qemu_opt_get(opts, "netdev"))) {
802 nd->netdev = qemu_find_netdev(netdev);
803 if (!nd->netdev) {
804 error_report("netdev '%s' not found", netdev);
805 return -1;
807 } else {
808 assert(vlan);
809 nd->vlan = vlan;
811 if (name) {
812 nd->name = qemu_strdup(name);
814 if (qemu_opt_get(opts, "model")) {
815 nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
817 if (qemu_opt_get(opts, "addr")) {
818 nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
821 nd->macaddr[0] = 0x52;
822 nd->macaddr[1] = 0x54;
823 nd->macaddr[2] = 0x00;
824 nd->macaddr[3] = 0x12;
825 nd->macaddr[4] = 0x34;
826 nd->macaddr[5] = 0x56 + idx;
828 if (qemu_opt_get(opts, "macaddr") &&
829 net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
830 error_report("invalid syntax for ethernet address");
831 return -1;
834 nd->nvectors = qemu_opt_get_number(opts, "vectors",
835 DEV_NVECTORS_UNSPECIFIED);
836 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
837 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
838 error_report("invalid # of vectors: %d", nd->nvectors);
839 return -1;
842 nd->used = 1;
843 nb_nics++;
845 return idx;
848 #define NET_COMMON_PARAMS_DESC \
850 .name = "type", \
851 .type = QEMU_OPT_STRING, \
852 .help = "net client type (nic, tap etc.)", \
853 }, { \
854 .name = "vlan", \
855 .type = QEMU_OPT_NUMBER, \
856 .help = "vlan number", \
857 }, { \
858 .name = "name", \
859 .type = QEMU_OPT_STRING, \
860 .help = "identifier for monitor commands", \
863 typedef int (*net_client_init_func)(QemuOpts *opts,
864 Monitor *mon,
865 const char *name,
866 VLANState *vlan);
868 /* magic number, but compiler will warn if too small */
869 #define NET_MAX_DESC 20
871 static const struct {
872 const char *type;
873 net_client_init_func init;
874 QemuOptDesc desc[NET_MAX_DESC];
875 } net_client_types[] = {
877 .type = "none",
878 .desc = {
879 NET_COMMON_PARAMS_DESC,
880 { /* end of list */ }
882 }, {
883 .type = "nic",
884 .init = net_init_nic,
885 .desc = {
886 NET_COMMON_PARAMS_DESC,
888 .name = "netdev",
889 .type = QEMU_OPT_STRING,
890 .help = "id of -netdev to connect to",
893 .name = "macaddr",
894 .type = QEMU_OPT_STRING,
895 .help = "MAC address",
896 }, {
897 .name = "model",
898 .type = QEMU_OPT_STRING,
899 .help = "device model (e1000, rtl8139, virtio etc.)",
900 }, {
901 .name = "addr",
902 .type = QEMU_OPT_STRING,
903 .help = "PCI device address",
904 }, {
905 .name = "vectors",
906 .type = QEMU_OPT_NUMBER,
907 .help = "number of MSI-x vectors, 0 to disable MSI-X",
909 { /* end of list */ }
911 #ifdef CONFIG_SLIRP
912 }, {
913 .type = "user",
914 .init = net_init_slirp,
915 .desc = {
916 NET_COMMON_PARAMS_DESC,
918 .name = "hostname",
919 .type = QEMU_OPT_STRING,
920 .help = "client hostname reported by the builtin DHCP server",
921 }, {
922 .name = "restrict",
923 .type = QEMU_OPT_STRING,
924 .help = "isolate the guest from the host (y|yes|n|no)",
925 }, {
926 .name = "ip",
927 .type = QEMU_OPT_STRING,
928 .help = "legacy parameter, use net= instead",
929 }, {
930 .name = "net",
931 .type = QEMU_OPT_STRING,
932 .help = "IP address and optional netmask",
933 }, {
934 .name = "host",
935 .type = QEMU_OPT_STRING,
936 .help = "guest-visible address of the host",
937 }, {
938 .name = "tftp",
939 .type = QEMU_OPT_STRING,
940 .help = "root directory of the built-in TFTP server",
941 }, {
942 .name = "bootfile",
943 .type = QEMU_OPT_STRING,
944 .help = "BOOTP filename, for use with tftp=",
945 }, {
946 .name = "dhcpstart",
947 .type = QEMU_OPT_STRING,
948 .help = "the first of the 16 IPs the built-in DHCP server can assign",
949 }, {
950 .name = "dns",
951 .type = QEMU_OPT_STRING,
952 .help = "guest-visible address of the virtual nameserver",
953 }, {
954 .name = "smb",
955 .type = QEMU_OPT_STRING,
956 .help = "root directory of the built-in SMB server",
957 }, {
958 .name = "smbserver",
959 .type = QEMU_OPT_STRING,
960 .help = "IP address of the built-in SMB server",
961 }, {
962 .name = "hostfwd",
963 .type = QEMU_OPT_STRING,
964 .help = "guest port number to forward incoming TCP or UDP connections",
965 }, {
966 .name = "guestfwd",
967 .type = QEMU_OPT_STRING,
968 .help = "IP address and port to forward guest TCP connections",
970 { /* end of list */ }
972 #endif
973 }, {
974 .type = "tap",
975 .init = net_init_tap,
976 .desc = {
977 NET_COMMON_PARAMS_DESC,
979 .name = "ifname",
980 .type = QEMU_OPT_STRING,
981 .help = "interface name",
983 #ifndef _WIN32
985 .name = "fd",
986 .type = QEMU_OPT_STRING,
987 .help = "file descriptor of an already opened tap",
988 }, {
989 .name = "script",
990 .type = QEMU_OPT_STRING,
991 .help = "script to initialize the interface",
992 }, {
993 .name = "downscript",
994 .type = QEMU_OPT_STRING,
995 .help = "script to shut down the interface",
996 }, {
997 .name = "sndbuf",
998 .type = QEMU_OPT_SIZE,
999 .help = "send buffer limit"
1000 }, {
1001 .name = "vnet_hdr",
1002 .type = QEMU_OPT_BOOL,
1003 .help = "enable the IFF_VNET_HDR flag on the tap interface"
1004 }, {
1005 .name = "vhost",
1006 .type = QEMU_OPT_BOOL,
1007 .help = "enable vhost-net network accelerator",
1008 }, {
1009 .name = "vhostfd",
1010 .type = QEMU_OPT_STRING,
1011 .help = "file descriptor of an already opened vhost net device",
1013 #endif /* _WIN32 */
1014 { /* end of list */ }
1016 }, {
1017 .type = "socket",
1018 .init = net_init_socket,
1019 .desc = {
1020 NET_COMMON_PARAMS_DESC,
1022 .name = "fd",
1023 .type = QEMU_OPT_STRING,
1024 .help = "file descriptor of an already opened socket",
1025 }, {
1026 .name = "listen",
1027 .type = QEMU_OPT_STRING,
1028 .help = "port number, and optional hostname, to listen on",
1029 }, {
1030 .name = "connect",
1031 .type = QEMU_OPT_STRING,
1032 .help = "port number, and optional hostname, to connect to",
1033 }, {
1034 .name = "mcast",
1035 .type = QEMU_OPT_STRING,
1036 .help = "UDP multicast address and port number",
1037 }, {
1038 .name = "localaddr",
1039 .type = QEMU_OPT_STRING,
1040 .help = "source address for multicast packets",
1042 { /* end of list */ }
1044 #ifdef CONFIG_VDE
1045 }, {
1046 .type = "vde",
1047 .init = net_init_vde,
1048 .desc = {
1049 NET_COMMON_PARAMS_DESC,
1051 .name = "sock",
1052 .type = QEMU_OPT_STRING,
1053 .help = "socket path",
1054 }, {
1055 .name = "port",
1056 .type = QEMU_OPT_NUMBER,
1057 .help = "port number",
1058 }, {
1059 .name = "group",
1060 .type = QEMU_OPT_STRING,
1061 .help = "group owner of socket",
1062 }, {
1063 .name = "mode",
1064 .type = QEMU_OPT_NUMBER,
1065 .help = "permissions for socket",
1067 { /* end of list */ }
1069 #endif
1070 }, {
1071 .type = "dump",
1072 .init = net_init_dump,
1073 .desc = {
1074 NET_COMMON_PARAMS_DESC,
1076 .name = "len",
1077 .type = QEMU_OPT_SIZE,
1078 .help = "per-packet size limit (64k default)",
1079 }, {
1080 .name = "file",
1081 .type = QEMU_OPT_STRING,
1082 .help = "dump file path (default is qemu-vlan0.pcap)",
1084 { /* end of list */ }
1087 { /* end of list */ }
1090 int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
1092 const char *name;
1093 const char *type;
1094 int i;
1096 type = qemu_opt_get(opts, "type");
1097 if (!type) {
1098 qerror_report(QERR_MISSING_PARAMETER, "type");
1099 return -1;
1102 if (is_netdev) {
1103 if (strcmp(type, "tap") != 0 &&
1104 #ifdef CONFIG_SLIRP
1105 strcmp(type, "user") != 0 &&
1106 #endif
1107 #ifdef CONFIG_VDE
1108 strcmp(type, "vde") != 0 &&
1109 #endif
1110 strcmp(type, "socket") != 0) {
1111 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1112 "a netdev backend type");
1113 return -1;
1116 if (qemu_opt_get(opts, "vlan")) {
1117 qerror_report(QERR_INVALID_PARAMETER, "vlan");
1118 return -1;
1120 if (qemu_opt_get(opts, "name")) {
1121 qerror_report(QERR_INVALID_PARAMETER, "name");
1122 return -1;
1124 if (!qemu_opts_id(opts)) {
1125 qerror_report(QERR_MISSING_PARAMETER, "id");
1126 return -1;
1130 name = qemu_opts_id(opts);
1131 if (!name) {
1132 name = qemu_opt_get(opts, "name");
1135 for (i = 0; net_client_types[i].type != NULL; i++) {
1136 if (!strcmp(net_client_types[i].type, type)) {
1137 VLANState *vlan = NULL;
1138 int ret;
1140 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1141 return -1;
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);
1151 ret = 0;
1152 if (net_client_types[i].init) {
1153 ret = net_client_types[i].init(opts, mon, name, vlan);
1154 if (ret < 0) {
1155 /* TODO push error reporting into init() methods */
1156 qerror_report(QERR_DEVICE_INIT_FAILED, type);
1157 return -1;
1160 return ret;
1164 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1165 "a network client type");
1166 return -1;
1169 static int net_host_check_device(const char *device)
1171 int i;
1172 const char *valid_param_list[] = { "tap", "socket", "dump"
1173 #ifdef CONFIG_SLIRP
1174 ,"user"
1175 #endif
1176 #ifdef CONFIG_VDE
1177 ,"vde"
1178 #endif
1180 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1181 if (!strncmp(valid_param_list[i], device,
1182 strlen(valid_param_list[i])))
1183 return 1;
1186 return 0;
1189 void net_host_device_add(Monitor *mon, const QDict *qdict)
1191 const char *device = qdict_get_str(qdict, "device");
1192 const char *opts_str = qdict_get_try_str(qdict, "opts");
1193 QemuOpts *opts;
1195 if (!net_host_check_device(device)) {
1196 monitor_printf(mon, "invalid host network device %s\n", device);
1197 return;
1200 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
1201 if (!opts) {
1202 return;
1205 qemu_opt_set(opts, "type", device);
1207 if (net_client_init(mon, opts, 0) < 0) {
1208 monitor_printf(mon, "adding host network device %s failed\n", device);
1212 void net_host_device_remove(Monitor *mon, const QDict *qdict)
1214 VLANClientState *vc;
1215 int vlan_id = qdict_get_int(qdict, "vlan_id");
1216 const char *device = qdict_get_str(qdict, "device");
1218 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
1219 if (!vc) {
1220 return;
1222 if (!net_host_check_device(vc->model)) {
1223 monitor_printf(mon, "invalid host network device %s\n", device);
1224 return;
1226 qemu_del_vlan_client(vc);
1229 int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1231 QemuOpts *opts;
1232 int res;
1234 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
1235 if (!opts) {
1236 return -1;
1239 res = net_client_init(mon, opts, 1);
1240 if (res < 0) {
1241 qemu_opts_del(opts);
1244 return res;
1247 int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1249 const char *id = qdict_get_str(qdict, "id");
1250 VLANClientState *vc;
1252 vc = qemu_find_netdev(id);
1253 if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) {
1254 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1255 return -1;
1257 qemu_del_vlan_client(vc);
1258 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
1259 return 0;
1262 void do_info_network(Monitor *mon)
1264 VLANState *vlan;
1265 VLANClientState *vc;
1267 QTAILQ_FOREACH(vlan, &vlans, next) {
1268 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1270 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1271 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
1274 monitor_printf(mon, "Devices not on any VLAN:\n");
1275 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1276 monitor_printf(mon, " %s: %s", vc->name, vc->info_str);
1277 if (vc->peer) {
1278 monitor_printf(mon, " peer=%s", vc->peer->name);
1280 monitor_printf(mon, "\n");
1284 int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
1286 VLANState *vlan;
1287 VLANClientState *vc = NULL;
1288 const char *name = qdict_get_str(qdict, "name");
1289 int up = qdict_get_bool(qdict, "up");
1291 QTAILQ_FOREACH(vlan, &vlans, next) {
1292 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1293 if (strcmp(vc->name, name) == 0) {
1294 goto done;
1298 vc = qemu_find_netdev(name);
1299 done:
1301 if (!vc) {
1302 qerror_report(QERR_DEVICE_NOT_FOUND, name);
1303 return -1;
1306 vc->link_down = !up;
1308 if (vc->info->link_status_changed) {
1309 vc->info->link_status_changed(vc);
1312 /* Notify peer. Don't update peer link status: this makes it possible to
1313 * disconnect from host network without notifying the guest.
1314 * FIXME: is disconnected link status change operation useful?
1316 * Current behaviour is compatible with qemu vlans where there could be
1317 * multiple clients that can still communicate with each other in
1318 * disconnected mode. For now maintain this compatibility. */
1319 if (vc->peer && vc->peer->info->link_status_changed) {
1320 vc->peer->info->link_status_changed(vc->peer);
1322 return 0;
1325 void net_cleanup(void)
1327 VLANState *vlan;
1328 VLANClientState *vc, *next_vc;
1330 QTAILQ_FOREACH(vlan, &vlans, next) {
1331 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
1332 qemu_del_vlan_client(vc);
1336 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1337 qemu_del_vlan_client(vc);
1341 void net_check_clients(void)
1343 VLANState *vlan;
1344 VLANClientState *vc;
1345 int has_nic = 0, has_host_dev = 0;
1347 QTAILQ_FOREACH(vlan, &vlans, next) {
1348 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1349 switch (vc->info->type) {
1350 case NET_CLIENT_TYPE_NIC:
1351 has_nic = 1;
1352 break;
1353 case NET_CLIENT_TYPE_SLIRP:
1354 case NET_CLIENT_TYPE_TAP:
1355 case NET_CLIENT_TYPE_SOCKET:
1356 case NET_CLIENT_TYPE_VDE:
1357 has_host_dev = 1;
1358 break;
1359 default: ;
1362 if (has_host_dev && !has_nic)
1363 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1364 if (has_nic && !has_host_dev)
1365 fprintf(stderr,
1366 "Warning: vlan %d is not connected to host network\n",
1367 vlan->id);
1369 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1370 if (!vc->peer) {
1371 fprintf(stderr, "Warning: %s %s has no peer\n",
1372 vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1373 vc->name);
1378 static int net_init_client(QemuOpts *opts, void *dummy)
1380 if (net_client_init(NULL, opts, 0) < 0)
1381 return -1;
1382 return 0;
1385 static int net_init_netdev(QemuOpts *opts, void *dummy)
1387 return net_client_init(NULL, opts, 1);
1390 int net_init_clients(void)
1392 QemuOptsList *net = qemu_find_opts("net");
1394 if (default_net) {
1395 /* if no clients, we use a default config */
1396 qemu_opts_set(net, NULL, "type", "nic");
1397 #ifdef CONFIG_SLIRP
1398 qemu_opts_set(net, NULL, "type", "user");
1399 #endif
1402 QTAILQ_INIT(&vlans);
1403 QTAILQ_INIT(&non_vlan_clients);
1405 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
1406 return -1;
1408 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
1409 return -1;
1412 return 0;
1415 int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1417 #if defined(CONFIG_SLIRP)
1418 int ret;
1419 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
1420 return ret;
1422 #endif
1424 if (!qemu_opts_parse(opts_list, optarg, 1)) {
1425 return -1;
1428 default_net = 0;
1429 return 0;