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