blob: 3ede738cac8be5829cdd0a3350279be3c016cb95 [file] [log] [blame]
aliguori63a01ef2008-10-31 19:10:00 +00001/*
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 */
Mark McLoughlin1df49e02009-11-25 18:48:58 +000024#include "net.h"
aliguori63a01ef2008-10-31 19:10:00 +000025
blueswir1d40cdb12009-03-07 16:52:02 +000026#include "config-host.h"
27
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010028#include "net/tap.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000029#include "net/socket.h"
Mark McLoughlin1abecf72009-11-25 18:48:57 +000030#include "net/dump.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000031#include "net/slirp.h"
Mark McLoughlin5c361cc2009-11-25 18:48:55 +000032#include "net/vde.h"
Mark McLoughlinf1d078c2009-11-25 18:49:27 +000033#include "net/util.h"
blueswir1511d2b12009-03-07 15:32:56 +000034#include "monitor.h"
35#include "sysemu.h"
Mark McLoughlin1df49e02009-11-25 18:48:58 +000036#include "qemu-common.h"
blueswir1511d2b12009-03-07 15:32:56 +000037#include "qemu_socket.h"
Amit Shah75422b02010-02-25 17:24:43 +053038#include "hw/qdev.h"
blueswir1511d2b12009-03-07 15:32:56 +000039
Mark McLoughlin5610c3a2009-10-08 19:58:23 +010040static QTAILQ_HEAD(, VLANState) vlans;
Mark McLoughlin577c4af2009-10-08 19:58:28 +010041static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
aliguori63a01ef2008-10-31 19:10:00 +000042
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010043int default_net = 1;
44
aliguori63a01ef2008-10-31 19:10:00 +000045/***********************************************************/
46/* network device redirectors */
47
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000048#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000049static void hex_dump(FILE *f, const uint8_t *buf, int size)
50{
51 int len, i, j, c;
52
53 for(i=0;i<size;i+=16) {
54 len = size - i;
55 if (len > 16)
56 len = 16;
57 fprintf(f, "%08x ", i);
58 for(j=0;j<16;j++) {
59 if (j < len)
60 fprintf(f, " %02x", buf[i+j]);
61 else
62 fprintf(f, " ");
63 }
64 fprintf(f, " ");
65 for(j=0;j<len;j++) {
66 c = buf[i+j];
67 if (c < ' ' || c > '~')
68 c = '.';
69 fprintf(f, "%c", c);
70 }
71 fprintf(f, "\n");
72 }
73}
74#endif
75
aliguori63a01ef2008-10-31 19:10:00 +000076static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
77{
78 const char *p, *p1;
79 int len;
80 p = *pp;
81 p1 = strchr(p, sep);
82 if (!p1)
83 return -1;
84 len = p1 - p;
85 p1++;
86 if (buf_size > 0) {
87 if (len > buf_size - 1)
88 len = buf_size - 1;
89 memcpy(buf, p, len);
90 buf[len] = '\0';
91 }
92 *pp = p1;
93 return 0;
94}
95
96int parse_host_src_port(struct sockaddr_in *haddr,
97 struct sockaddr_in *saddr,
98 const char *input_str)
99{
Jim Meyering6265eb22010-02-08 19:28:38 +0100100 char *str = qemu_strdup(input_str);
aliguori63a01ef2008-10-31 19:10:00 +0000101 char *host_str = str;
102 char *src_str;
103 const char *src_str2;
104 char *ptr;
105
106 /*
107 * Chop off any extra arguments at the end of the string which
108 * would start with a comma, then fill in the src port information
109 * if it was provided else use the "any address" and "any port".
110 */
111 if ((ptr = strchr(str,',')))
112 *ptr = '\0';
113
114 if ((src_str = strchr(input_str,'@'))) {
115 *src_str = '\0';
116 src_str++;
117 }
118
119 if (parse_host_port(haddr, host_str) < 0)
120 goto fail;
121
122 src_str2 = src_str;
123 if (!src_str || *src_str == '\0')
124 src_str2 = ":0";
125
126 if (parse_host_port(saddr, src_str2) < 0)
127 goto fail;
128
129 free(str);
130 return(0);
131
132fail:
133 free(str);
134 return -1;
135}
136
137int parse_host_port(struct sockaddr_in *saddr, const char *str)
138{
139 char buf[512];
140 struct hostent *he;
141 const char *p, *r;
142 int port;
143
144 p = str;
145 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
146 return -1;
147 saddr->sin_family = AF_INET;
148 if (buf[0] == '\0') {
149 saddr->sin_addr.s_addr = 0;
150 } else {
blueswir1cd390082008-11-16 13:53:32 +0000151 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000152 if (!inet_aton(buf, &saddr->sin_addr))
153 return -1;
154 } else {
155 if ((he = gethostbyname(buf)) == NULL)
156 return - 1;
157 saddr->sin_addr = *(struct in_addr *)he->h_addr;
158 }
159 }
160 port = strtol(p, (char **)&r, 0);
161 if (r == p)
162 return -1;
163 saddr->sin_port = htons(port);
164 return 0;
165}
166
aliguori7cb74342009-01-07 17:46:21 +0000167void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
168{
169 snprintf(vc->info_str, sizeof(vc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000170 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
171 vc->model,
aliguori7cb74342009-01-07 17:46:21 +0000172 macaddr[0], macaddr[1], macaddr[2],
173 macaddr[3], macaddr[4], macaddr[5]);
174}
175
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200176void qemu_macaddr_default_if_unset(MACAddr *macaddr)
177{
178 static int index = 0;
179 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
180
181 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
182 return;
183 macaddr->a[0] = 0x52;
184 macaddr->a[1] = 0x54;
185 macaddr->a[2] = 0x00;
186 macaddr->a[3] = 0x12;
187 macaddr->a[4] = 0x34;
188 macaddr->a[5] = 0x56 + index++;
189}
190
aliguori676cff22009-01-07 17:43:44 +0000191static char *assign_name(VLANClientState *vc1, const char *model)
192{
193 VLANState *vlan;
194 char buf[256];
195 int id = 0;
196
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100197 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori676cff22009-01-07 17:43:44 +0000198 VLANClientState *vc;
199
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100200 QTAILQ_FOREACH(vc, &vlan->clients, next) {
201 if (vc != vc1 && strcmp(vc->model, model) == 0) {
aliguori676cff22009-01-07 17:43:44 +0000202 id++;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100203 }
204 }
aliguori676cff22009-01-07 17:43:44 +0000205 }
206
207 snprintf(buf, sizeof(buf), "%s.%d", model, id);
208
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100209 return qemu_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000210}
211
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100212static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100213 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100214 const uint8_t *data,
215 size_t size,
216 void *opaque);
217static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100218 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100219 const struct iovec *iov,
220 int iovcnt,
221 void *opaque);
222
Mark McLoughlin45460d12009-11-25 18:49:02 +0000223VLANClientState *qemu_new_net_client(NetClientInfo *info,
224 VLANState *vlan,
225 VLANClientState *peer,
226 const char *model,
227 const char *name)
aliguori63a01ef2008-10-31 19:10:00 +0000228{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100229 VLANClientState *vc;
230
Mark McLoughlin45460d12009-11-25 18:49:02 +0000231 assert(info->size >= sizeof(VLANClientState));
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100232
Mark McLoughlin45460d12009-11-25 18:49:02 +0000233 vc = qemu_mallocz(info->size);
234
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000235 vc->info = info;
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100236 vc->model = qemu_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000237 if (name) {
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100238 vc->name = qemu_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000239 } else {
aliguori7a9f6e42009-01-07 17:48:51 +0000240 vc->name = assign_name(vc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000241 }
aliguori63a01ef2008-10-31 19:10:00 +0000242
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100243 if (vlan) {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100244 assert(!peer);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100245 vc->vlan = vlan;
246 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100247 } else {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100248 if (peer) {
Markus Armbruster27f3f8a2010-02-26 15:50:51 +0100249 assert(!peer->peer);
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100250 vc->peer = peer;
251 peer->peer = vc;
252 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100253 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100254
255 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
256 qemu_deliver_packet_iov,
257 vc);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100258 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100259
aliguori63a01ef2008-10-31 19:10:00 +0000260 return vc;
261}
262
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000263NICState *qemu_new_nic(NetClientInfo *info,
264 NICConf *conf,
265 const char *model,
266 const char *name,
267 void *opaque)
268{
269 VLANClientState *nc;
270 NICState *nic;
271
272 assert(info->type == NET_CLIENT_TYPE_NIC);
273 assert(info->size >= sizeof(NICState));
274
275 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
276
277 nic = DO_UPCAST(NICState, nc, nc);
278 nic->conf = conf;
279 nic->opaque = opaque;
280
281 return nic;
282}
283
aliguori63a01ef2008-10-31 19:10:00 +0000284void qemu_del_vlan_client(VLANClientState *vc)
285{
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100286 if (vc->vlan) {
287 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100288 } else {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100289 if (vc->send_queue) {
290 qemu_del_net_queue(vc->send_queue);
291 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100292 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100293 if (vc->peer) {
294 vc->peer->peer = NULL;
295 }
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100296 }
aliguori63a01ef2008-10-31 19:10:00 +0000297
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000298 if (vc->info->cleanup) {
299 vc->info->cleanup(vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100300 }
301
302 qemu_free(vc->name);
303 qemu_free(vc->model);
304 qemu_free(vc);
aliguori63a01ef2008-10-31 19:10:00 +0000305}
306
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000307VLANClientState *
Jan Kiszka1a609522009-06-24 14:42:31 +0200308qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
309 const char *client_str)
310{
311 VLANState *vlan;
312 VLANClientState *vc;
313
314 vlan = qemu_find_vlan(vlan_id, 0);
315 if (!vlan) {
316 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
317 return NULL;
318 }
319
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100320 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka1a609522009-06-24 14:42:31 +0200321 if (!strcmp(vc->name, client_str)) {
322 break;
323 }
324 }
325 if (!vc) {
326 monitor_printf(mon, "can't find device %s on VLAN %d\n",
327 client_str, vlan_id);
328 }
329
330 return vc;
331}
332
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000333void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
334{
335 VLANClientState *nc;
336 VLANState *vlan;
337
338 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
339 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
340 func(DO_UPCAST(NICState, nc, nc), opaque);
341 }
342 }
343
344 QTAILQ_FOREACH(vlan, &vlans, next) {
345 QTAILQ_FOREACH(nc, &vlan->clients, next) {
346 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
347 func(DO_UPCAST(NICState, nc, nc), opaque);
348 }
349 }
350 }
351}
352
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100353int qemu_can_send_packet(VLANClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000354{
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100355 VLANState *vlan = sender->vlan;
aliguori63a01ef2008-10-31 19:10:00 +0000356 VLANClientState *vc;
357
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100358 if (sender->peer) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000359 if (sender->peer->receive_disabled) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100360 return 0;
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000361 } else if (sender->peer->info->can_receive &&
362 !sender->peer->info->can_receive(sender->peer)) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000363 return 0;
364 } else {
365 return 1;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100366 }
367 }
368
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100369 if (!sender->vlan) {
370 return 1;
371 }
372
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100373 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100374 if (vc == sender) {
375 continue;
376 }
377
Mark McLoughlincda90462009-05-18 13:13:16 +0100378 /* no can_receive() handler, they can always receive */
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000379 if (!vc->info->can_receive || vc->info->can_receive(vc)) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100380 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000381 }
382 }
383 return 0;
384}
385
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100386static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100387 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100388 const uint8_t *data,
389 size_t size,
390 void *opaque)
391{
392 VLANClientState *vc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000393 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100394
395 if (vc->link_down) {
396 return size;
397 }
398
Mark McLoughlin893379e2009-10-27 18:16:36 +0000399 if (vc->receive_disabled) {
400 return 0;
401 }
402
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000403 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
404 ret = vc->info->receive_raw(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000405 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000406 ret = vc->info->receive(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000407 }
408
409 if (ret == 0) {
410 vc->receive_disabled = 1;
411 };
412
413 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100414}
415
Mark McLoughlinf7105842009-10-08 19:58:31 +0100416static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100417 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100418 const uint8_t *buf,
419 size_t size,
420 void *opaque)
aliguori63a01ef2008-10-31 19:10:00 +0000421{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100422 VLANState *vlan = opaque;
aliguori63a01ef2008-10-31 19:10:00 +0000423 VLANClientState *vc;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000424 ssize_t ret = -1;
aliguori63a01ef2008-10-31 19:10:00 +0000425
Mark McLoughlinf7105842009-10-08 19:58:31 +0100426 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100427 ssize_t len;
428
429 if (vc == sender) {
430 continue;
aliguori764a4d12009-04-21 19:56:41 +0000431 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100432
433 if (vc->link_down) {
434 ret = size;
435 continue;
436 }
437
Mark McLoughlin893379e2009-10-27 18:16:36 +0000438 if (vc->receive_disabled) {
439 ret = 0;
440 continue;
441 }
442
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000443 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
444 len = vc->info->receive_raw(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000445 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000446 len = vc->info->receive(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000447 }
448
449 if (len == 0) {
450 vc->receive_disabled = 1;
451 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100452
453 ret = (ret >= 0) ? ret : len;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000454
aliguori764a4d12009-04-21 19:56:41 +0000455 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100456
457 return ret;
aliguori764a4d12009-04-21 19:56:41 +0000458}
459
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100460void qemu_purge_queued_packets(VLANClientState *vc)
461{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100462 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100463
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100464 if (!vc->peer && !vc->vlan) {
465 return;
466 }
467
468 if (vc->peer) {
469 queue = vc->peer->send_queue;
470 } else {
471 queue = vc->vlan->send_queue;
472 }
473
474 qemu_net_queue_purge(queue, vc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100475}
476
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100477void qemu_flush_queued_packets(VLANClientState *vc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100478{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100479 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100480
Mark McLoughlin893379e2009-10-27 18:16:36 +0000481 vc->receive_disabled = 0;
482
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100483 if (vc->vlan) {
484 queue = vc->vlan->send_queue;
485 } else {
486 queue = vc->send_queue;
487 }
488
489 qemu_net_queue_flush(queue);
Mark McLoughline94667b2009-04-29 11:48:12 +0100490}
491
Mark McLoughlinca77d172009-10-22 17:43:41 +0100492static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
493 unsigned flags,
494 const uint8_t *buf, int size,
495 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000496{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100497 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100498
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100499#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100500 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100501 hex_dump(stdout, buf, size);
502#endif
503
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100504 if (sender->link_down || (!sender->peer && !sender->vlan)) {
505 return size;
506 }
507
508 if (sender->peer) {
509 queue = sender->peer->send_queue;
510 } else {
511 queue = sender->vlan->send_queue;
512 }
513
Mark McLoughlinca77d172009-10-22 17:43:41 +0100514 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
515}
516
517ssize_t qemu_send_packet_async(VLANClientState *sender,
518 const uint8_t *buf, int size,
519 NetPacketSent *sent_cb)
520{
521 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
522 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100523}
524
525void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
526{
527 qemu_send_packet_async(vc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000528}
529
Mark McLoughlinca77d172009-10-22 17:43:41 +0100530ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
531{
532 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
533 buf, size, NULL);
534}
535
aliguorifbe78f42008-12-17 19:13:11 +0000536static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
537 int iovcnt)
538{
539 uint8_t buffer[4096];
540 size_t offset = 0;
541 int i;
542
543 for (i = 0; i < iovcnt; i++) {
544 size_t len;
545
546 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
547 memcpy(buffer + offset, iov[i].iov_base, len);
548 offset += len;
549 }
550
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000551 return vc->info->receive(vc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000552}
553
aliguorie0e78772009-01-26 15:37:44 +0000554static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
555{
556 size_t offset = 0;
557 int i;
558
559 for (i = 0; i < iovcnt; i++)
560 offset += iov[i].iov_len;
561 return offset;
562}
563
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100564static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100565 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100566 const struct iovec *iov,
567 int iovcnt,
568 void *opaque)
569{
570 VLANClientState *vc = opaque;
571
572 if (vc->link_down) {
573 return calc_iov_length(iov, iovcnt);
574 }
575
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000576 if (vc->info->receive_iov) {
577 return vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100578 } else {
579 return vc_sendv_compat(vc, iov, iovcnt);
580 }
581}
582
Mark McLoughlinf7105842009-10-08 19:58:31 +0100583static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100584 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100585 const struct iovec *iov,
586 int iovcnt,
587 void *opaque)
Mark McLoughline94667b2009-04-29 11:48:12 +0100588{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100589 VLANState *vlan = opaque;
Mark McLoughline94667b2009-04-29 11:48:12 +0100590 VLANClientState *vc;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100591 ssize_t ret = -1;
Mark McLoughline94667b2009-04-29 11:48:12 +0100592
Mark McLoughlinf7105842009-10-08 19:58:31 +0100593 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughline94667b2009-04-29 11:48:12 +0100594 ssize_t len;
595
596 if (vc == sender) {
597 continue;
598 }
599
600 if (vc->link_down) {
601 ret = calc_iov_length(iov, iovcnt);
602 continue;
603 }
604
Mark McLoughlinca77d172009-10-22 17:43:41 +0100605 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
606
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000607 if (vc->info->receive_iov) {
608 len = vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100609 } else {
610 len = vc_sendv_compat(vc, iov, iovcnt);
611 }
612
613 ret = (ret >= 0) ? ret : len;
614 }
615
Mark McLoughline94667b2009-04-29 11:48:12 +0100616 return ret;
617}
618
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100619ssize_t qemu_sendv_packet_async(VLANClientState *sender,
620 const struct iovec *iov, int iovcnt,
621 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000622{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100623 NetQueue *queue;
624
625 if (sender->link_down || (!sender->peer && !sender->vlan)) {
aliguorie0e78772009-01-26 15:37:44 +0000626 return calc_iov_length(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000627 }
628
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100629 if (sender->peer) {
630 queue = sender->peer->send_queue;
631 } else {
632 queue = sender->vlan->send_queue;
633 }
634
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100635 return qemu_net_queue_send_iov(queue, sender,
636 QEMU_NET_PACKET_FLAG_NONE,
637 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000638}
639
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100640ssize_t
641qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
642{
643 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
644}
645
aliguori63a01ef2008-10-31 19:10:00 +0000646/* find or alloc a new VLAN */
Jan Kiszka1a609522009-06-24 14:42:31 +0200647VLANState *qemu_find_vlan(int id, int allocate)
aliguori63a01ef2008-10-31 19:10:00 +0000648{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100649 VLANState *vlan;
650
651 QTAILQ_FOREACH(vlan, &vlans, next) {
652 if (vlan->id == id) {
aliguori63a01ef2008-10-31 19:10:00 +0000653 return vlan;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100654 }
aliguori63a01ef2008-10-31 19:10:00 +0000655 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100656
Jan Kiszka1a609522009-06-24 14:42:31 +0200657 if (!allocate) {
658 return NULL;
659 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100660
aliguori63a01ef2008-10-31 19:10:00 +0000661 vlan = qemu_mallocz(sizeof(VLANState));
aliguori63a01ef2008-10-31 19:10:00 +0000662 vlan->id = id;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100663 QTAILQ_INIT(&vlan->clients);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100664
665 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
666 qemu_vlan_deliver_packet_iov,
667 vlan);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100668
669 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
670
aliguori63a01ef2008-10-31 19:10:00 +0000671 return vlan;
672}
673
Gerd Hoffmann2ef924b2009-10-21 15:25:24 +0200674VLANClientState *qemu_find_netdev(const char *id)
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100675{
676 VLANClientState *vc;
677
678 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
679 if (!strcmp(vc->name, id)) {
680 return vc;
681 }
682 }
683
684 return NULL;
685}
686
aliguori76970792009-02-11 15:20:03 +0000687static int nic_get_free_idx(void)
688{
689 int index;
690
691 for (index = 0; index < MAX_NICS; index++)
692 if (!nd_table[index].used)
693 return index;
694 return -1;
695}
696
Markus Armbruster07caea32009-09-25 03:53:51 +0200697int qemu_show_nic_models(const char *arg, const char *const *models)
698{
699 int i;
700
701 if (!arg || strcmp(arg, "?"))
702 return 0;
703
704 fprintf(stderr, "qemu: Supported NIC models: ");
705 for (i = 0 ; models[i]; i++)
706 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
707 return 1;
708}
709
aliguorid07f22c2009-01-13 19:03:57 +0000710void qemu_check_nic_model(NICInfo *nd, const char *model)
711{
712 const char *models[2];
713
714 models[0] = model;
715 models[1] = NULL;
716
Markus Armbruster07caea32009-09-25 03:53:51 +0200717 if (qemu_show_nic_models(nd->model, models))
718 exit(0);
719 if (qemu_find_nic_model(nd, models, model) < 0)
720 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000721}
722
Markus Armbruster07caea32009-09-25 03:53:51 +0200723int qemu_find_nic_model(NICInfo *nd, const char * const *models,
724 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000725{
Markus Armbruster07caea32009-09-25 03:53:51 +0200726 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000727
728 if (!nd->model)
Mark McLoughlin32a8e142009-10-06 12:16:51 +0100729 nd->model = qemu_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000730
Markus Armbruster07caea32009-09-25 03:53:51 +0200731 for (i = 0 ; models[i]; i++) {
732 if (strcmp(nd->model, models[i]) == 0)
733 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000734 }
735
Markus Armbruster1ecda022010-02-18 17:25:24 +0100736 error_report("qemu: Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200737 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000738}
739
Mark McLoughlin5281d752009-10-22 17:49:07 +0100740int net_handle_fd_param(Monitor *mon, const char *param)
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100741{
742 if (!qemu_isdigit(param[0])) {
743 int fd;
744
745 fd = monitor_get_fd(mon, param);
746 if (fd == -1) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100747 error_report("No file descriptor named %s found", param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100748 return -1;
749 }
750
751 return fd;
752 } else {
753 return strtol(param, NULL, 0);
754 }
755}
756
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100757static int net_init_nic(QemuOpts *opts,
758 Monitor *mon,
759 const char *name,
760 VLANState *vlan)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100761{
762 int idx;
763 NICInfo *nd;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100764 const char *netdev;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100765
766 idx = nic_get_free_idx();
767 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100768 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100769 return -1;
770 }
771
772 nd = &nd_table[idx];
773
774 memset(nd, 0, sizeof(*nd));
775
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100776 if ((netdev = qemu_opt_get(opts, "netdev"))) {
777 nd->netdev = qemu_find_netdev(netdev);
778 if (!nd->netdev) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100779 error_report("netdev '%s' not found", netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100780 return -1;
781 }
782 } else {
783 assert(vlan);
784 nd->vlan = vlan;
785 }
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100786 if (name) {
787 nd->name = qemu_strdup(name);
788 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100789 if (qemu_opt_get(opts, "model")) {
790 nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
791 }
792 if (qemu_opt_get(opts, "addr")) {
793 nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
794 }
795
796 nd->macaddr[0] = 0x52;
797 nd->macaddr[1] = 0x54;
798 nd->macaddr[2] = 0x00;
799 nd->macaddr[3] = 0x12;
800 nd->macaddr[4] = 0x34;
801 nd->macaddr[5] = 0x56 + idx;
802
803 if (qemu_opt_get(opts, "macaddr") &&
Mark McLoughlinf1d078c2009-11-25 18:49:27 +0000804 net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100805 error_report("invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100806 return -1;
807 }
808
Amit Shah75422b02010-02-25 17:24:43 +0530809 nd->nvectors = qemu_opt_get_number(opts, "vectors",
810 DEV_NVECTORS_UNSPECIFIED);
811 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100812 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100813 error_report("invalid # of vectors: %d", nd->nvectors);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100814 return -1;
815 }
816
817 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100818 nb_nics++;
819
820 return idx;
821}
822
823#define NET_COMMON_PARAMS_DESC \
824 { \
825 .name = "type", \
826 .type = QEMU_OPT_STRING, \
827 .help = "net client type (nic, tap etc.)", \
828 }, { \
829 .name = "vlan", \
830 .type = QEMU_OPT_NUMBER, \
831 .help = "vlan number", \
832 }, { \
833 .name = "name", \
834 .type = QEMU_OPT_STRING, \
835 .help = "identifier for monitor commands", \
836 }
837
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100838typedef int (*net_client_init_func)(QemuOpts *opts,
839 Monitor *mon,
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100840 const char *name,
841 VLANState *vlan);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100842
843/* magic number, but compiler will warn if too small */
844#define NET_MAX_DESC 20
845
Blue Swirl238431a2010-02-21 16:01:30 +0000846static const struct {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100847 const char *type;
848 net_client_init_func init;
849 QemuOptDesc desc[NET_MAX_DESC];
850} net_client_types[] = {
851 {
852 .type = "none",
853 .desc = {
854 NET_COMMON_PARAMS_DESC,
855 { /* end of list */ }
856 },
857 }, {
858 .type = "nic",
859 .init = net_init_nic,
860 .desc = {
861 NET_COMMON_PARAMS_DESC,
862 {
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100863 .name = "netdev",
864 .type = QEMU_OPT_STRING,
865 .help = "id of -netdev to connect to",
866 },
867 {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100868 .name = "macaddr",
869 .type = QEMU_OPT_STRING,
870 .help = "MAC address",
871 }, {
872 .name = "model",
873 .type = QEMU_OPT_STRING,
874 .help = "device model (e1000, rtl8139, virtio etc.)",
875 }, {
876 .name = "addr",
877 .type = QEMU_OPT_STRING,
878 .help = "PCI device address",
879 }, {
880 .name = "vectors",
881 .type = QEMU_OPT_NUMBER,
882 .help = "number of MSI-x vectors, 0 to disable MSI-X",
883 },
884 { /* end of list */ }
885 },
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100886#ifdef CONFIG_SLIRP
887 }, {
888 .type = "user",
889 .init = net_init_slirp,
890 .desc = {
891 NET_COMMON_PARAMS_DESC,
892 {
893 .name = "hostname",
894 .type = QEMU_OPT_STRING,
895 .help = "client hostname reported by the builtin DHCP server",
896 }, {
897 .name = "restrict",
898 .type = QEMU_OPT_STRING,
899 .help = "isolate the guest from the host (y|yes|n|no)",
900 }, {
901 .name = "ip",
902 .type = QEMU_OPT_STRING,
903 .help = "legacy parameter, use net= instead",
904 }, {
905 .name = "net",
906 .type = QEMU_OPT_STRING,
907 .help = "IP address and optional netmask",
908 }, {
909 .name = "host",
910 .type = QEMU_OPT_STRING,
911 .help = "guest-visible address of the host",
912 }, {
913 .name = "tftp",
914 .type = QEMU_OPT_STRING,
915 .help = "root directory of the built-in TFTP server",
916 }, {
917 .name = "bootfile",
918 .type = QEMU_OPT_STRING,
919 .help = "BOOTP filename, for use with tftp=",
920 }, {
921 .name = "dhcpstart",
922 .type = QEMU_OPT_STRING,
923 .help = "the first of the 16 IPs the built-in DHCP server can assign",
924 }, {
925 .name = "dns",
926 .type = QEMU_OPT_STRING,
927 .help = "guest-visible address of the virtual nameserver",
928 }, {
929 .name = "smb",
930 .type = QEMU_OPT_STRING,
931 .help = "root directory of the built-in SMB server",
932 }, {
933 .name = "smbserver",
934 .type = QEMU_OPT_STRING,
935 .help = "IP address of the built-in SMB server",
936 }, {
937 .name = "hostfwd",
938 .type = QEMU_OPT_STRING,
939 .help = "guest port number to forward incoming TCP or UDP connections",
940 }, {
941 .name = "guestfwd",
942 .type = QEMU_OPT_STRING,
943 .help = "IP address and port to forward guest TCP connections",
944 },
945 { /* end of list */ }
946 },
947#endif
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100948 }, {
949 .type = "tap",
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100950 .init = net_init_tap,
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100951 .desc = {
952 NET_COMMON_PARAMS_DESC,
953 {
954 .name = "ifname",
955 .type = QEMU_OPT_STRING,
956 .help = "interface name",
957 },
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100958#ifndef _WIN32
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100959 {
960 .name = "fd",
961 .type = QEMU_OPT_STRING,
962 .help = "file descriptor of an already opened tap",
963 }, {
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100964 .name = "script",
965 .type = QEMU_OPT_STRING,
966 .help = "script to initialize the interface",
967 }, {
968 .name = "downscript",
969 .type = QEMU_OPT_STRING,
970 .help = "script to shut down the interface",
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100971 }, {
972 .name = "sndbuf",
973 .type = QEMU_OPT_SIZE,
974 .help = "send buffer limit"
Mark McLoughlinbaf74c92009-10-22 17:43:37 +0100975 }, {
976 .name = "vnet_hdr",
977 .type = QEMU_OPT_BOOL,
978 .help = "enable the IFF_VNET_HDR flag on the tap interface"
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200979 }, {
980 .name = "vhost",
981 .type = QEMU_OPT_BOOL,
982 .help = "enable vhost-net network accelerator",
983 }, {
984 .name = "vhostfd",
985 .type = QEMU_OPT_STRING,
986 .help = "file descriptor of an already opened vhost net device",
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100987 },
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100988#endif /* _WIN32 */
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100989 { /* end of list */ }
990 },
Mark McLoughlin88ce16c2009-10-06 12:17:09 +0100991 }, {
992 .type = "socket",
993 .init = net_init_socket,
994 .desc = {
995 NET_COMMON_PARAMS_DESC,
996 {
997 .name = "fd",
998 .type = QEMU_OPT_STRING,
999 .help = "file descriptor of an already opened socket",
1000 }, {
1001 .name = "listen",
1002 .type = QEMU_OPT_STRING,
1003 .help = "port number, and optional hostname, to listen on",
1004 }, {
1005 .name = "connect",
1006 .type = QEMU_OPT_STRING,
1007 .help = "port number, and optional hostname, to connect to",
1008 }, {
1009 .name = "mcast",
1010 .type = QEMU_OPT_STRING,
1011 .help = "UDP multicast address and port number",
1012 },
1013 { /* end of list */ }
1014 },
Mark McLoughlindd510582009-10-06 12:17:10 +01001015#ifdef CONFIG_VDE
1016 }, {
1017 .type = "vde",
1018 .init = net_init_vde,
1019 .desc = {
1020 NET_COMMON_PARAMS_DESC,
1021 {
1022 .name = "sock",
1023 .type = QEMU_OPT_STRING,
1024 .help = "socket path",
1025 }, {
1026 .name = "port",
1027 .type = QEMU_OPT_NUMBER,
1028 .help = "port number",
1029 }, {
1030 .name = "group",
1031 .type = QEMU_OPT_STRING,
1032 .help = "group owner of socket",
1033 }, {
1034 .name = "mode",
1035 .type = QEMU_OPT_NUMBER,
1036 .help = "permissions for socket",
1037 },
1038 { /* end of list */ }
1039 },
1040#endif
Mark McLoughlined2955c2009-10-06 12:17:11 +01001041 }, {
1042 .type = "dump",
1043 .init = net_init_dump,
1044 .desc = {
1045 NET_COMMON_PARAMS_DESC,
1046 {
1047 .name = "len",
1048 .type = QEMU_OPT_SIZE,
1049 .help = "per-packet size limit (64k default)",
1050 }, {
1051 .name = "file",
1052 .type = QEMU_OPT_STRING,
1053 .help = "dump file path (default is qemu-vlan0.pcap)",
1054 },
1055 { /* end of list */ }
1056 },
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001057 },
1058 { /* end of list */ }
1059};
1060
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001061int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001062{
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01001063 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001064 const char *type;
1065 int i;
1066
1067 type = qemu_opt_get(opts, "type");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001068
Mark McLoughlin0f2fbf42009-11-25 18:49:33 +00001069 if (!is_netdev) {
1070 if (!type) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001071 error_report("No type specified for -net");
Mark McLoughlin0f2fbf42009-11-25 18:49:33 +00001072 return -1;
1073 }
1074 } else {
1075 if (!type) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001076 error_report("No type specified for -netdev");
Mark McLoughlin0f2fbf42009-11-25 18:49:33 +00001077 return -1;
1078 }
1079
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001080 if (strcmp(type, "tap") != 0 &&
1081#ifdef CONFIG_SLIRP
1082 strcmp(type, "user") != 0 &&
1083#endif
1084#ifdef CONFIG_VDE
1085 strcmp(type, "vde") != 0 &&
1086#endif
1087 strcmp(type, "socket") != 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001088 error_report("The '%s' network backend type is not valid with -netdev",
1089 type);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001090 return -1;
1091 }
1092
1093 if (qemu_opt_get(opts, "vlan")) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001094 error_report("The 'vlan' parameter is not valid with -netdev");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001095 return -1;
1096 }
1097 if (qemu_opt_get(opts, "name")) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001098 error_report("The 'name' parameter is not valid with -netdev");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001099 return -1;
1100 }
1101 if (!qemu_opts_id(opts)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001102 error_report("The id= parameter is required with -netdev");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001103 return -1;
1104 }
1105 }
1106
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01001107 name = qemu_opts_id(opts);
1108 if (!name) {
1109 name = qemu_opt_get(opts, "name");
1110 }
1111
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001112 for (i = 0; net_client_types[i].type != NULL; i++) {
1113 if (!strcmp(net_client_types[i].type, type)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001114 VLANState *vlan = NULL;
1115
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001116 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1117 return -1;
1118 }
1119
Mark McLoughlin5869c4d2009-10-08 19:58:29 +01001120 /* Do not add to a vlan if it's a -netdev or a nic with a
1121 * netdev= parameter. */
1122 if (!(is_netdev ||
1123 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001124 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1125 }
1126
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001127 if (net_client_types[i].init) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001128 return net_client_types[i].init(opts, mon, name, vlan);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001129 } else {
1130 return 0;
1131 }
1132 }
1133 }
1134
Markus Armbruster1ecda022010-02-18 17:25:24 +01001135 error_report("Invalid -net type '%s'", type);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001136 return -1;
1137}
1138
aliguori6f338c32009-02-11 15:21:54 +00001139static int net_host_check_device(const char *device)
1140{
1141 int i;
aliguoribb9ea792009-04-21 19:56:28 +00001142 const char *valid_param_list[] = { "tap", "socket", "dump"
aliguori6f338c32009-02-11 15:21:54 +00001143#ifdef CONFIG_SLIRP
1144 ,"user"
1145#endif
1146#ifdef CONFIG_VDE
1147 ,"vde"
1148#endif
1149 };
1150 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1151 if (!strncmp(valid_param_list[i], device,
1152 strlen(valid_param_list[i])))
1153 return 1;
1154 }
1155
1156 return 0;
1157}
1158
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001159void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001160{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001161 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001162 const char *opts_str = qdict_get_try_str(qdict, "opts");
1163 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001164
aliguori6f338c32009-02-11 15:21:54 +00001165 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001166 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001167 return;
1168 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001169
Markus Armbruster8212c642010-02-10 19:52:18 +01001170 opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001171 if (!opts) {
1172 monitor_printf(mon, "parsing network options '%s' failed\n",
1173 opts_str ? opts_str : "");
1174 return;
1175 }
1176
1177 qemu_opt_set(opts, "type", device);
1178
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001179 if (net_client_init(mon, opts, 0) < 0) {
aliguori5c8be672009-04-21 19:56:32 +00001180 monitor_printf(mon, "adding host network device %s failed\n", device);
1181 }
aliguori6f338c32009-02-11 15:21:54 +00001182}
1183
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001184void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001185{
aliguori6f338c32009-02-11 15:21:54 +00001186 VLANClientState *vc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001187 int vlan_id = qdict_get_int(qdict, "vlan_id");
1188 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001189
Jan Kiszka1a609522009-06-24 14:42:31 +02001190 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
aliguori6f338c32009-02-11 15:21:54 +00001191 if (!vc) {
aliguori6f338c32009-02-11 15:21:54 +00001192 return;
1193 }
aliguorie8f1f9d2009-04-21 19:56:08 +00001194 if (!net_host_check_device(vc->model)) {
1195 monitor_printf(mon, "invalid host network device %s\n", device);
1196 return;
1197 }
aliguori6f338c32009-02-11 15:21:54 +00001198 qemu_del_vlan_client(vc);
1199}
1200
Glauber Costa406c8df2009-06-17 09:05:30 -04001201void net_set_boot_mask(int net_boot_mask)
1202{
1203 int i;
1204
1205 /* Only the first four NICs may be bootable */
1206 net_boot_mask = net_boot_mask & 0xF;
1207
1208 for (i = 0; i < nb_nics; i++) {
1209 if (net_boot_mask & (1 << i)) {
1210 nd_table[i].bootable = 1;
1211 net_boot_mask &= ~(1 << i);
1212 }
1213 }
1214
1215 if (net_boot_mask) {
1216 fprintf(stderr, "Cannot boot from non-existent NIC\n");
1217 exit(1);
1218 }
1219}
1220
aliguori376253e2009-03-05 23:01:23 +00001221void do_info_network(Monitor *mon)
aliguori63a01ef2008-10-31 19:10:00 +00001222{
1223 VLANState *vlan;
Markus Armbrustera0104e02010-02-11 14:45:01 +01001224 VLANClientState *vc;
aliguori63a01ef2008-10-31 19:10:00 +00001225
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001226 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori376253e2009-03-05 23:01:23 +00001227 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001228
1229 QTAILQ_FOREACH(vc, &vlan->clients, next) {
aliguori376253e2009-03-05 23:01:23 +00001230 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001231 }
aliguori63a01ef2008-10-31 19:10:00 +00001232 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001233 monitor_printf(mon, "Devices not on any VLAN:\n");
1234 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1235 monitor_printf(mon, " %s: %s", vc->name, vc->info_str);
1236 if (vc->peer) {
1237 monitor_printf(mon, " peer=%s", vc->peer->name);
1238 }
1239 monitor_printf(mon, "\n");
1240 }
aliguori63a01ef2008-10-31 19:10:00 +00001241}
1242
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001243void do_set_link(Monitor *mon, const QDict *qdict)
aliguori436e5e52009-01-08 19:44:06 +00001244{
1245 VLANState *vlan;
1246 VLANClientState *vc = NULL;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001247 const char *name = qdict_get_str(qdict, "name");
1248 const char *up_or_down = qdict_get_str(qdict, "up_or_down");
aliguori436e5e52009-01-08 19:44:06 +00001249
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001250 QTAILQ_FOREACH(vlan, &vlans, next) {
1251 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1252 if (strcmp(vc->name, name) == 0) {
edgar_igldd5de372009-01-09 00:48:28 +00001253 goto done;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001254 }
1255 }
1256 }
Markus Armbruster2583ba92010-02-11 14:45:02 +01001257 vc = qemu_find_netdev(name);
edgar_igldd5de372009-01-09 00:48:28 +00001258done:
aliguori436e5e52009-01-08 19:44:06 +00001259
1260 if (!vc) {
Stefan Weil7dc3fa02009-08-08 23:33:26 +02001261 monitor_printf(mon, "could not find network device '%s'\n", name);
Luiz Capitulinoc3cf0d32009-08-03 13:56:59 -03001262 return;
aliguori436e5e52009-01-08 19:44:06 +00001263 }
1264
1265 if (strcmp(up_or_down, "up") == 0)
1266 vc->link_down = 0;
1267 else if (strcmp(up_or_down, "down") == 0)
1268 vc->link_down = 1;
1269 else
aliguori376253e2009-03-05 23:01:23 +00001270 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
1271 "valid\n", up_or_down);
aliguori436e5e52009-01-08 19:44:06 +00001272
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001273 if (vc->info->link_status_changed) {
1274 vc->info->link_status_changed(vc);
1275 }
aliguori436e5e52009-01-08 19:44:06 +00001276}
1277
aliguori63a01ef2008-10-31 19:10:00 +00001278void net_cleanup(void)
1279{
1280 VLANState *vlan;
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001281 VLANClientState *vc, *next_vc;
aliguori63a01ef2008-10-31 19:10:00 +00001282
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001283 QTAILQ_FOREACH(vlan, &vlans, next) {
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001284 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
aliguorib946a152009-04-17 17:11:08 +00001285 qemu_del_vlan_client(vc);
aliguori63a01ef2008-10-31 19:10:00 +00001286 }
1287 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001288
1289 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1290 qemu_del_vlan_client(vc);
1291 }
aliguori63a01ef2008-10-31 19:10:00 +00001292}
1293
Markus Armbruster668680f2010-02-11 14:44:58 +01001294void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001295{
1296 VLANState *vlan;
Markus Armbruster62112d12010-02-11 14:44:59 +01001297 VLANClientState *vc;
Blue Swirl64e69d52010-02-20 08:20:18 +00001298 int has_nic = 0, has_host_dev = 0;
aliguori63a01ef2008-10-31 19:10:00 +00001299
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001300 QTAILQ_FOREACH(vlan, &vlans, next) {
Markus Armbruster62112d12010-02-11 14:44:59 +01001301 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1302 switch (vc->info->type) {
1303 case NET_CLIENT_TYPE_NIC:
1304 has_nic = 1;
1305 break;
1306 case NET_CLIENT_TYPE_SLIRP:
1307 case NET_CLIENT_TYPE_TAP:
1308 case NET_CLIENT_TYPE_SOCKET:
1309 case NET_CLIENT_TYPE_VDE:
1310 has_host_dev = 1;
1311 break;
1312 default: ;
1313 }
1314 }
1315 if (has_host_dev && !has_nic)
aliguori63a01ef2008-10-31 19:10:00 +00001316 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
Markus Armbruster62112d12010-02-11 14:44:59 +01001317 if (has_nic && !has_host_dev)
aliguori63a01ef2008-10-31 19:10:00 +00001318 fprintf(stderr,
1319 "Warning: vlan %d is not connected to host network\n",
1320 vlan->id);
1321 }
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001322 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1323 if (!vc->peer) {
1324 fprintf(stderr, "Warning: %s %s has no peer\n",
1325 vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1326 vc->name);
1327 }
1328 }
aliguori63a01ef2008-10-31 19:10:00 +00001329}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001330
1331static int net_init_client(QemuOpts *opts, void *dummy)
1332{
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001333 if (net_client_init(NULL, opts, 0) < 0)
1334 return -1;
1335 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001336}
1337
1338static int net_init_netdev(QemuOpts *opts, void *dummy)
1339{
1340 return net_client_init(NULL, opts, 1);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001341}
1342
1343int net_init_clients(void)
1344{
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001345 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001346 /* if no clients, we use a default config */
1347 qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
1348#ifdef CONFIG_SLIRP
1349 qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
1350#endif
1351 }
1352
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001353 QTAILQ_INIT(&vlans);
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001354 QTAILQ_INIT(&non_vlan_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001355
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001356 if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1)
1357 return -1;
1358
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001359 if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) {
1360 return -1;
1361 }
1362
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001363 return 0;
1364}
1365
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001366int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001367{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001368#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001369 int ret;
1370 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001371 return ret;
1372 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001373#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001374
Markus Armbruster8212c642010-02-10 19:52:18 +01001375 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001376 return -1;
1377 }
1378
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001379 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001380 return 0;
1381}