]> git.proxmox.com Git - qemu.git/blame - net.c
net: remove qemu_new_vlan_client()
[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
45460d1a 232 vc->type = info->type;
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
MM
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;
5610c3aa 245
d80b9fc6 246 if (vlan) {
283c7c63 247 assert(!peer);
d80b9fc6
MM
248 vc->vlan = vlan;
249 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
577c4af9 250 } else {
283c7c63
MM
251 if (peer) {
252 vc->peer = peer;
253 peer->peer = vc;
254 }
577c4af9 255 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
9a6ecb30
MM
256
257 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
258 qemu_deliver_packet_iov,
259 vc);
d80b9fc6 260 }
63a01ef8 261
63a01ef8
AL
262 return vc;
263}
264
ebef2c09
MM
265NICState *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
63a01ef8
AL
286void qemu_del_vlan_client(VLANClientState *vc)
287{
d80b9fc6
MM
288 if (vc->vlan) {
289 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
577c4af9 290 } else {
9a6ecb30
MM
291 if (vc->send_queue) {
292 qemu_del_net_queue(vc->send_queue);
293 }
577c4af9 294 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
283c7c63
MM
295 if (vc->peer) {
296 vc->peer->peer = NULL;
297 }
d80b9fc6 298 }
63a01ef8 299
5610c3aa
MM
300 if (vc->cleanup) {
301 vc->cleanup(vc);
302 }
303
304 qemu_free(vc->name);
305 qemu_free(vc->model);
306 qemu_free(vc);
63a01ef8
AL
307}
308
68ac40d2 309VLANClientState *
1a609520
JK
310qemu_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
5610c3aa 322 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1a609520
JK
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
2e1e0641 335int qemu_can_send_packet(VLANClientState *sender)
63a01ef8 336{
2e1e0641 337 VLANState *vlan = sender->vlan;
63a01ef8
AL
338 VLANClientState *vc;
339
9a6ecb30 340 if (sender->peer) {
893379ef
MM
341 if (sender->peer->receive_disabled) {
342 return 0;
343 } else if (sender->peer->can_receive &&
344 !sender->peer->can_receive(sender->peer)) {
9a6ecb30 345 return 0;
893379ef
MM
346 } else {
347 return 1;
9a6ecb30
MM
348 }
349 }
350
d80b9fc6
MM
351 if (!sender->vlan) {
352 return 1;
353 }
354
5610c3aa 355 QTAILQ_FOREACH(vc, &vlan->clients, next) {
2e1e0641
MM
356 if (vc == sender) {
357 continue;
358 }
359
cda9046b 360 /* no can_receive() handler, they can always receive */
e3f5ec2b 361 if (!vc->can_receive || vc->can_receive(vc)) {
2e1e0641 362 return 1;
63a01ef8
AL
363 }
364 }
365 return 0;
366}
367
9a6ecb30 368static ssize_t qemu_deliver_packet(VLANClientState *sender,
c0b8e49c 369 unsigned flags,
9a6ecb30
MM
370 const uint8_t *data,
371 size_t size,
372 void *opaque)
373{
374 VLANClientState *vc = opaque;
893379ef 375 ssize_t ret;
9a6ecb30
MM
376
377 if (vc->link_down) {
378 return size;
379 }
380
893379ef
MM
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;
9a6ecb30
MM
396}
397
f7105843 398static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
c0b8e49c 399 unsigned flags,
f7105843
MM
400 const uint8_t *buf,
401 size_t size,
402 void *opaque)
63a01ef8 403{
f7105843 404 VLANState *vlan = opaque;
63a01ef8 405 VLANClientState *vc;
893379ef 406 ssize_t ret = -1;
63a01ef8 407
f7105843 408 QTAILQ_FOREACH(vc, &vlan->clients, next) {
3e021d40
MM
409 ssize_t len;
410
411 if (vc == sender) {
412 continue;
764a4d1d 413 }
3e021d40
MM
414
415 if (vc->link_down) {
416 ret = size;
417 continue;
418 }
419
893379ef
MM
420 if (vc->receive_disabled) {
421 ret = 0;
422 continue;
423 }
424
425 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->receive_raw) {
ca77d175 426 len = vc->receive_raw(vc, buf, size);
893379ef 427 } else {
ca77d175 428 len = vc->receive(vc, buf, size);
893379ef
MM
429 }
430
431 if (len == 0) {
432 vc->receive_disabled = 1;
433 }
3e021d40
MM
434
435 ret = (ret >= 0) ? ret : len;
893379ef 436
764a4d1d 437 }
3e021d40
MM
438
439 return ret;
764a4d1d
AL
440}
441
8cad5516
MM
442void qemu_purge_queued_packets(VLANClientState *vc)
443{
9a6ecb30
MM
444 NetQueue *queue;
445
446 if (!vc->peer && !vc->vlan) {
d80b9fc6 447 return;
9a6ecb30 448 }
d80b9fc6 449
9a6ecb30
MM
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);
8cad5516
MM
457}
458
f3b6c7fc 459void qemu_flush_queued_packets(VLANClientState *vc)
e94667b9 460{
9a6ecb30
MM
461 NetQueue *queue;
462
893379ef
MM
463 vc->receive_disabled = 0;
464
9a6ecb30
MM
465 if (vc->vlan) {
466 queue = vc->vlan->send_queue;
467 } else {
468 queue = vc->send_queue;
469 }
d80b9fc6 470
9a6ecb30 471 qemu_net_queue_flush(queue);
e94667b9
MM
472}
473
ca77d175
MM
474static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
475 unsigned flags,
476 const uint8_t *buf, int size,
477 NetPacketSent *sent_cb)
764a4d1d 478{
9a6ecb30 479 NetQueue *queue;
436e5e53 480
63a01ef8 481#ifdef DEBUG_NET
d80b9fc6 482 printf("qemu_send_packet_async:\n");
63a01ef8
AL
483 hex_dump(stdout, buf, size);
484#endif
f3b6c7fc 485
9a6ecb30
MM
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
ca77d175
MM
496 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
497}
498
499ssize_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);
f3b6c7fc
MM
505}
506
507void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
508{
509 qemu_send_packet_async(vc, buf, size, NULL);
63a01ef8
AL
510}
511
ca77d175
MM
512ssize_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
fbe78f4f
AL
518static 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
f3b6c7fc 533 return vc->receive(vc, buffer, offset);
fbe78f4f
AL
534}
535
e0e7877a
AL
536static 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
9a6ecb30 546static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
c0b8e49c 547 unsigned flags,
9a6ecb30
MM
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
f7105843 565static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
c0b8e49c 566 unsigned flags,
f7105843
MM
567 const struct iovec *iov,
568 int iovcnt,
569 void *opaque)
fbe78f4f 570{
f7105843 571 VLANState *vlan = opaque;
fbe78f4f 572 VLANClientState *vc;
f7105843 573 ssize_t ret = -1;
e94667b9 574
f7105843 575 QTAILQ_FOREACH(vc, &vlan->clients, next) {
e94667b9
MM
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
ca77d175
MM
587 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
588
e94667b9
MM
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
e94667b9
MM
598 return ret;
599}
600
f3b6c7fc
MM
601ssize_t qemu_sendv_packet_async(VLANClientState *sender,
602 const struct iovec *iov, int iovcnt,
603 NetPacketSent *sent_cb)
e94667b9 604{
9a6ecb30
MM
605 NetQueue *queue;
606
607 if (sender->link_down || (!sender->peer && !sender->vlan)) {
e94667b9
MM
608 return calc_iov_length(iov, iovcnt);
609 }
610
9a6ecb30
MM
611 if (sender->peer) {
612 queue = sender->peer->send_queue;
613 } else {
614 queue = sender->vlan->send_queue;
615 }
616
c0b8e49c
MM
617 return qemu_net_queue_send_iov(queue, sender,
618 QEMU_NET_PACKET_FLAG_NONE,
619 iov, iovcnt, sent_cb);
fbe78f4f
AL
620}
621
f3b6c7fc
MM
622ssize_t
623qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
624{
625 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
626}
627
63a01ef8 628/* find or alloc a new VLAN */
1a609520 629VLANState *qemu_find_vlan(int id, int allocate)
63a01ef8 630{
5610c3aa
MM
631 VLANState *vlan;
632
633 QTAILQ_FOREACH(vlan, &vlans, next) {
634 if (vlan->id == id) {
63a01ef8 635 return vlan;
5610c3aa 636 }
63a01ef8 637 }
5610c3aa 638
1a609520
JK
639 if (!allocate) {
640 return NULL;
641 }
5610c3aa 642
63a01ef8 643 vlan = qemu_mallocz(sizeof(VLANState));
63a01ef8 644 vlan->id = id;
5610c3aa 645 QTAILQ_INIT(&vlan->clients);
f7105843
MM
646
647 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
648 qemu_vlan_deliver_packet_iov,
649 vlan);
5610c3aa
MM
650
651 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
652
63a01ef8
AL
653 return vlan;
654}
655
2ef924b4 656VLANClientState *qemu_find_netdev(const char *id)
5869c4d5
MM
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
7697079b
AL
669static 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
07caea31
MA
679int 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
d07f22c5
AL
692void 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
07caea31
MA
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);
d07f22c5
AL
703}
704
07caea31
MA
705int qemu_find_nic_model(NICInfo *nd, const char * const *models,
706 const char *default_model)
d07f22c5 707{
07caea31 708 int i;
d07f22c5
AL
709
710 if (!nd->model)
32a8e14a 711 nd->model = qemu_strdup(default_model);
d07f22c5 712
07caea31
MA
713 for (i = 0 ; models[i]; i++) {
714 if (strcmp(nd->model, models[i]) == 0)
715 return i;
d07f22c5
AL
716 }
717
07caea31
MA
718 qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
719 return -1;
d07f22c5
AL
720}
721
5281d757 722int net_handle_fd_param(Monitor *mon, const char *param)
c1d6eed7
MM
723{
724 if (!qemu_isdigit(param[0])) {
725 int fd;
726
727 fd = monitor_get_fd(mon, param);
728 if (fd == -1) {
fb12577c 729 qemu_error("No file descriptor named %s found", param);
c1d6eed7
MM
730 return -1;
731 }
732
733 return fd;
734 } else {
735 return strtol(param, NULL, 0);
736 }
737}
738
f6b134ac
MM
739static int net_init_nic(QemuOpts *opts,
740 Monitor *mon,
741 const char *name,
742 VLANState *vlan)
f83c6e10
MM
743{
744 int idx;
745 NICInfo *nd;
5869c4d5 746 const char *netdev;
f83c6e10
MM
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
5869c4d5
MM
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 }
6d952ebe
MM
768 if (name) {
769 nd->name = qemu_strdup(name);
770 }
f83c6e10
MM
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") &&
f1d078c3 786 net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
f83c6e10
MM
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;
5869c4d5
MM
799 if (vlan) {
800 nd->vlan->nb_guest_devs++;
801 }
f83c6e10
MM
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
6d952ebe
MM
822typedef int (*net_client_init_func)(QemuOpts *opts,
823 Monitor *mon,
f6b134ac
MM
824 const char *name,
825 VLANState *vlan);
f83c6e10
MM
826
827/* magic number, but compiler will warn if too small */
828#define NET_MAX_DESC 20
829
830static 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,
5869c4d5
MM
846 {
847 .name = "netdev",
848 .type = QEMU_OPT_STRING,
849 .help = "id of -netdev to connect to",
850 },
f83c6e10
MM
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 },
ec302ffd
MM
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 },
8a1c5235 931#endif
8a1c5235
MM
932 }, {
933 .type = "tap",
a8ed73f7 934 .init = net_init_tap,
8a1c5235
MM
935 .desc = {
936 NET_COMMON_PARAMS_DESC,
937 {
938 .name = "ifname",
939 .type = QEMU_OPT_STRING,
940 .help = "interface name",
941 },
a8ed73f7 942#ifndef _WIN32
8a1c5235
MM
943 {
944 .name = "fd",
945 .type = QEMU_OPT_STRING,
946 .help = "file descriptor of an already opened tap",
8a1c5235
MM
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",
8a1c5235
MM
955 }, {
956 .name = "sndbuf",
957 .type = QEMU_OPT_SIZE,
958 .help = "send buffer limit"
baf74c95
MM
959 }, {
960 .name = "vnet_hdr",
961 .type = QEMU_OPT_BOOL,
962 .help = "enable the IFF_VNET_HDR flag on the tap interface"
8a1c5235 963 },
a8ed73f7 964#endif /* _WIN32 */
8a1c5235
MM
965 { /* end of list */ }
966 },
88ce16ca
MM
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 },
dd51058d
MM
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
ed2955c2
MM
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 },
f83c6e10
MM
1033 },
1034 { /* end of list */ }
1035};
1036
f6b134ac 1037int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
f83c6e10 1038{
6d952ebe 1039 const char *name;
f83c6e10
MM
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
f6b134ac
MM
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
6d952ebe
MM
1077 name = qemu_opts_id(opts);
1078 if (!name) {
1079 name = qemu_opt_get(opts, "name");
1080 }
1081
f83c6e10
MM
1082 for (i = 0; net_client_types[i].type != NULL; i++) {
1083 if (!strcmp(net_client_types[i].type, type)) {
f6b134ac
MM
1084 VLANState *vlan = NULL;
1085
f83c6e10
MM
1086 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1087 return -1;
1088 }
1089
5869c4d5
MM
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")))) {
f6b134ac
MM
1094 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1095 }
1096
f83c6e10 1097 if (net_client_types[i].init) {
f6b134ac 1098 return net_client_types[i].init(opts, mon, name, vlan);
f83c6e10
MM
1099 } else {
1100 return 0;
1101 }
1102 }
1103 }
1104
1105 qemu_error("Invalid -net type '%s'\n", type);
1106 return -1;
1107}
1108
8b13c4a7
AL
1109void net_client_uninit(NICInfo *nd)
1110{
d80b9fc6
MM
1111 if (nd->vlan) {
1112 nd->vlan->nb_guest_devs--;
1113 }
8b13c4a7 1114 nb_nics--;
a9796703 1115
9203f520
MM
1116 qemu_free(nd->model);
1117 qemu_free(nd->name);
1118 qemu_free(nd->devaddr);
a9796703 1119
d2cffe30 1120 nd->used = 0;
8b13c4a7
AL
1121}
1122
6f338c34
AL
1123static int net_host_check_device(const char *device)
1124{
1125 int i;
bb9ea79e 1126 const char *valid_param_list[] = { "tap", "socket", "dump"
6f338c34
AL
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
f18c16de 1143void net_host_device_add(Monitor *mon, const QDict *qdict)
6f338c34 1144{
f18c16de 1145 const char *device = qdict_get_str(qdict, "device");
7f1c9d20
MM
1146 const char *opts_str = qdict_get_try_str(qdict, "opts");
1147 QemuOpts *opts;
f18c16de 1148
6f338c34 1149 if (!net_host_check_device(device)) {
376253ec 1150 monitor_printf(mon, "invalid host network device %s\n", device);
6f338c34
AL
1151 return;
1152 }
7f1c9d20
MM
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
f6b134ac 1163 if (net_client_init(mon, opts, 0) < 0) {
5c8be678
AL
1164 monitor_printf(mon, "adding host network device %s failed\n", device);
1165 }
6f338c34
AL
1166}
1167
f18c16de 1168void net_host_device_remove(Monitor *mon, const QDict *qdict)
6f338c34 1169{
6f338c34 1170 VLANClientState *vc;
f18c16de
LC
1171 int vlan_id = qdict_get_int(qdict, "vlan_id");
1172 const char *device = qdict_get_str(qdict, "device");
6f338c34 1173
1a609520 1174 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
6f338c34 1175 if (!vc) {
6f338c34
AL
1176 return;
1177 }
e8f1f9db
AL
1178 if (!net_host_check_device(vc->model)) {
1179 monitor_printf(mon, "invalid host network device %s\n", device);
1180 return;
1181 }
6f338c34
AL
1182 qemu_del_vlan_client(vc);
1183}
1184
406c8df3
GC
1185void 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
376253ec 1205void do_info_network(Monitor *mon)
63a01ef8
AL
1206{
1207 VLANState *vlan;
63a01ef8 1208
5610c3aa
MM
1209 QTAILQ_FOREACH(vlan, &vlans, next) {
1210 VLANClientState *vc;
1211
376253ec 1212 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
5610c3aa
MM
1213
1214 QTAILQ_FOREACH(vc, &vlan->clients, next) {
376253ec 1215 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
5610c3aa 1216 }
63a01ef8
AL
1217 }
1218}
1219
f18c16de 1220void do_set_link(Monitor *mon, const QDict *qdict)
436e5e53
AL
1221{
1222 VLANState *vlan;
1223 VLANClientState *vc = NULL;
f18c16de
LC
1224 const char *name = qdict_get_str(qdict, "name");
1225 const char *up_or_down = qdict_get_str(qdict, "up_or_down");
436e5e53 1226
5610c3aa
MM
1227 QTAILQ_FOREACH(vlan, &vlans, next) {
1228 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1229 if (strcmp(vc->name, name) == 0) {
dd5de373 1230 goto done;
5610c3aa
MM
1231 }
1232 }
1233 }
dd5de373 1234done:
436e5e53
AL
1235
1236 if (!vc) {
7dc3fa09 1237 monitor_printf(mon, "could not find network device '%s'\n", name);
c3cf0d3f 1238 return;
436e5e53
AL
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
376253ec
AL
1246 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
1247 "valid\n", up_or_down);
436e5e53 1248
34b25ca7
AL
1249 if (vc->link_status_changed)
1250 vc->link_status_changed(vc);
436e5e53
AL
1251}
1252
63a01ef8
AL
1253void net_cleanup(void)
1254{
1255 VLANState *vlan;
577c4af9 1256 VLANClientState *vc, *next_vc;
63a01ef8 1257
5610c3aa 1258 QTAILQ_FOREACH(vlan, &vlans, next) {
5610c3aa 1259 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
b946a153 1260 qemu_del_vlan_client(vc);
63a01ef8
AL
1261 }
1262 }
577c4af9
MM
1263
1264 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1265 qemu_del_vlan_client(vc);
1266 }
63a01ef8
AL
1267}
1268
dc1c9fe8 1269static void net_check_clients(void)
63a01ef8
AL
1270{
1271 VLANState *vlan;
1272
5610c3aa 1273 QTAILQ_FOREACH(vlan, &vlans, next) {
63a01ef8
AL
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}
dc1c9fe8
MM
1284
1285static int net_init_client(QemuOpts *opts, void *dummy)
1286{
c1671a08
MM
1287 if (net_client_init(NULL, opts, 0) < 0)
1288 return -1;
1289 return 0;
f6b134ac
MM
1290}
1291
1292static int net_init_netdev(QemuOpts *opts, void *dummy)
1293{
1294 return net_client_init(NULL, opts, 1);
dc1c9fe8
MM
1295}
1296
1297int 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
5610c3aa 1307 QTAILQ_INIT(&vlans);
577c4af9 1308 QTAILQ_INIT(&non_vlan_clients);
5610c3aa 1309
f6b134ac
MM
1310 if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1)
1311 return -1;
1312
dc1c9fe8
MM
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
7f161aae 1322int net_client_parse(QemuOptsList *opts_list, const char *optarg)
dc1c9fe8 1323{
a3a766e7 1324#if defined(CONFIG_SLIRP)
68ac40d2
MM
1325 int ret;
1326 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
dc1c9fe8
MM
1327 return ret;
1328 }
a3a766e7 1329#endif
68ac40d2 1330
7f161aae 1331 if (!qemu_opts_parse(opts_list, optarg, "type")) {
dc1c9fe8
MM
1332 return -1;
1333 }
1334
1335 return 0;
1336}