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