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