blob: 1922d8abd19cdb4e45d6b1fd9a067b2fb9a506de [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"
Mark McLoughlin1df49e02009-11-25 18:48:58 +000035#include "qemu-common.h"
blueswir1511d2b12009-03-07 15:32:56 +000036#include "qemu_socket.h"
Luiz Capitulino4b371562011-11-23 13:11:55 -020037#include "qmp-commands.h"
Amit Shah75422b02010-02-25 17:24:43 +053038#include "hw/qdev.h"
Benjamin Poirierce053662011-02-23 19:57:21 -050039#include "iov.h"
blueswir1511d2b12009-03-07 15:32:56 +000040
Stefan Weil2944e4e2012-02-04 09:24:46 +010041/* Net bridge is currently not supported for W32. */
42#if !defined(_WIN32)
43# define CONFIG_NET_BRIDGE
44#endif
45
Mark McLoughlin5610c3a2009-10-08 19:58:23 +010046static QTAILQ_HEAD(, VLANState) vlans;
Mark McLoughlin577c4af2009-10-08 19:58:28 +010047static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
aliguori63a01ef2008-10-31 19:10:00 +000048
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010049int default_net = 1;
50
aliguori63a01ef2008-10-31 19:10:00 +000051/***********************************************************/
52/* network device redirectors */
53
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000054#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000055static void hex_dump(FILE *f, const uint8_t *buf, int size)
56{
57 int len, i, j, c;
58
59 for(i=0;i<size;i+=16) {
60 len = size - i;
61 if (len > 16)
62 len = 16;
63 fprintf(f, "%08x ", i);
64 for(j=0;j<16;j++) {
65 if (j < len)
66 fprintf(f, " %02x", buf[i+j]);
67 else
68 fprintf(f, " ");
69 }
70 fprintf(f, " ");
71 for(j=0;j<len;j++) {
72 c = buf[i+j];
73 if (c < ' ' || c > '~')
74 c = '.';
75 fprintf(f, "%c", c);
76 }
77 fprintf(f, "\n");
78 }
79}
80#endif
81
aliguori63a01ef2008-10-31 19:10:00 +000082static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
83{
84 const char *p, *p1;
85 int len;
86 p = *pp;
87 p1 = strchr(p, sep);
88 if (!p1)
89 return -1;
90 len = p1 - p;
91 p1++;
92 if (buf_size > 0) {
93 if (len > buf_size - 1)
94 len = buf_size - 1;
95 memcpy(buf, p, len);
96 buf[len] = '\0';
97 }
98 *pp = p1;
99 return 0;
100}
101
aliguori63a01ef2008-10-31 19:10:00 +0000102int parse_host_port(struct sockaddr_in *saddr, const char *str)
103{
104 char buf[512];
105 struct hostent *he;
106 const char *p, *r;
107 int port;
108
109 p = str;
110 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
111 return -1;
112 saddr->sin_family = AF_INET;
113 if (buf[0] == '\0') {
114 saddr->sin_addr.s_addr = 0;
115 } else {
blueswir1cd390082008-11-16 13:53:32 +0000116 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000117 if (!inet_aton(buf, &saddr->sin_addr))
118 return -1;
119 } else {
120 if ((he = gethostbyname(buf)) == NULL)
121 return - 1;
122 saddr->sin_addr = *(struct in_addr *)he->h_addr;
123 }
124 }
125 port = strtol(p, (char **)&r, 0);
126 if (r == p)
127 return -1;
128 saddr->sin_port = htons(port);
129 return 0;
130}
131
aliguori7cb7434b2009-01-07 17:46:21 +0000132void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
133{
134 snprintf(vc->info_str, sizeof(vc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000135 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
136 vc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000137 macaddr[0], macaddr[1], macaddr[2],
138 macaddr[3], macaddr[4], macaddr[5]);
139}
140
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200141void qemu_macaddr_default_if_unset(MACAddr *macaddr)
142{
143 static int index = 0;
144 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
145
146 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
147 return;
148 macaddr->a[0] = 0x52;
149 macaddr->a[1] = 0x54;
150 macaddr->a[2] = 0x00;
151 macaddr->a[3] = 0x12;
152 macaddr->a[4] = 0x34;
153 macaddr->a[5] = 0x56 + index++;
154}
155
aliguori676cff22009-01-07 17:43:44 +0000156static char *assign_name(VLANClientState *vc1, const char *model)
157{
158 VLANState *vlan;
Markus Armbruster53e51d82011-06-16 18:45:36 +0200159 VLANClientState *vc;
aliguori676cff22009-01-07 17:43:44 +0000160 char buf[256];
161 int id = 0;
162
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100163 QTAILQ_FOREACH(vlan, &vlans, next) {
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100164 QTAILQ_FOREACH(vc, &vlan->clients, next) {
165 if (vc != vc1 && strcmp(vc->model, model) == 0) {
aliguori676cff22009-01-07 17:43:44 +0000166 id++;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100167 }
168 }
aliguori676cff22009-01-07 17:43:44 +0000169 }
170
Markus Armbruster53e51d82011-06-16 18:45:36 +0200171 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
172 if (vc != vc1 && strcmp(vc->model, model) == 0) {
173 id++;
174 }
175 }
176
aliguori676cff22009-01-07 17:43:44 +0000177 snprintf(buf, sizeof(buf), "%s.%d", model, id);
178
Anthony Liguori7267c092011-08-20 22:09:37 -0500179 return g_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000180}
181
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100182static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100183 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100184 const uint8_t *data,
185 size_t size,
186 void *opaque);
187static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100188 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100189 const struct iovec *iov,
190 int iovcnt,
191 void *opaque);
192
Mark McLoughlin45460d12009-11-25 18:49:02 +0000193VLANClientState *qemu_new_net_client(NetClientInfo *info,
194 VLANState *vlan,
195 VLANClientState *peer,
196 const char *model,
197 const char *name)
aliguori63a01ef2008-10-31 19:10:00 +0000198{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100199 VLANClientState *vc;
200
Mark McLoughlin45460d12009-11-25 18:49:02 +0000201 assert(info->size >= sizeof(VLANClientState));
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100202
Anthony Liguori7267c092011-08-20 22:09:37 -0500203 vc = g_malloc0(info->size);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000204
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000205 vc->info = info;
Anthony Liguori7267c092011-08-20 22:09:37 -0500206 vc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000207 if (name) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500208 vc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000209 } else {
aliguori7a9f6e42009-01-07 17:48:51 +0000210 vc->name = assign_name(vc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000211 }
aliguori63a01ef2008-10-31 19:10:00 +0000212
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100213 if (vlan) {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100214 assert(!peer);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100215 vc->vlan = vlan;
216 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100217 } else {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100218 if (peer) {
Markus Armbruster27f3f8a2010-02-26 15:50:51 +0100219 assert(!peer->peer);
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100220 vc->peer = peer;
221 peer->peer = vc;
222 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100223 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100224
225 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
226 qemu_deliver_packet_iov,
227 vc);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100228 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100229
aliguori63a01ef2008-10-31 19:10:00 +0000230 return vc;
231}
232
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000233NICState *qemu_new_nic(NetClientInfo *info,
234 NICConf *conf,
235 const char *model,
236 const char *name,
237 void *opaque)
238{
239 VLANClientState *nc;
240 NICState *nic;
241
242 assert(info->type == NET_CLIENT_TYPE_NIC);
243 assert(info->size >= sizeof(NICState));
244
245 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
246
247 nic = DO_UPCAST(NICState, nc, nc);
248 nic->conf = conf;
249 nic->opaque = opaque;
250
251 return nic;
252}
253
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200254static void qemu_cleanup_vlan_client(VLANClientState *vc)
aliguori63a01ef2008-10-31 19:10:00 +0000255{
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100256 if (vc->vlan) {
257 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100258 } else {
259 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100260 }
aliguori63a01ef2008-10-31 19:10:00 +0000261
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000262 if (vc->info->cleanup) {
263 vc->info->cleanup(vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100264 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200265}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100266
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200267static void qemu_free_vlan_client(VLANClientState *vc)
268{
269 if (!vc->vlan) {
270 if (vc->send_queue) {
271 qemu_del_net_queue(vc->send_queue);
272 }
273 if (vc->peer) {
274 vc->peer->peer = NULL;
275 }
276 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500277 g_free(vc->name);
278 g_free(vc->model);
279 g_free(vc);
aliguori63a01ef2008-10-31 19:10:00 +0000280}
281
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200282void qemu_del_vlan_client(VLANClientState *vc)
283{
284 /* If there is a peer NIC, delete and cleanup client, but do not free. */
285 if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
286 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
287 if (nic->peer_deleted) {
288 return;
289 }
290 nic->peer_deleted = true;
291 /* Let NIC know peer is gone. */
292 vc->peer->link_down = true;
293 if (vc->peer->info->link_status_changed) {
294 vc->peer->info->link_status_changed(vc->peer);
295 }
296 qemu_cleanup_vlan_client(vc);
297 return;
298 }
299
300 /* If this is a peer NIC and peer has already been deleted, free it now. */
301 if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
302 NICState *nic = DO_UPCAST(NICState, nc, vc);
303 if (nic->peer_deleted) {
304 qemu_free_vlan_client(vc->peer);
305 }
306 }
307
308 qemu_cleanup_vlan_client(vc);
309 qemu_free_vlan_client(vc);
310}
311
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000312VLANClientState *
Jan Kiszka1a609522009-06-24 14:42:31 +0200313qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
314 const char *client_str)
315{
316 VLANState *vlan;
317 VLANClientState *vc;
318
319 vlan = qemu_find_vlan(vlan_id, 0);
320 if (!vlan) {
321 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
322 return NULL;
323 }
324
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100325 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka1a609522009-06-24 14:42:31 +0200326 if (!strcmp(vc->name, client_str)) {
327 break;
328 }
329 }
330 if (!vc) {
331 monitor_printf(mon, "can't find device %s on VLAN %d\n",
332 client_str, vlan_id);
333 }
334
335 return vc;
336}
337
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000338void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
339{
340 VLANClientState *nc;
341 VLANState *vlan;
342
343 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
344 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
345 func(DO_UPCAST(NICState, nc, nc), opaque);
346 }
347 }
348
349 QTAILQ_FOREACH(vlan, &vlans, next) {
350 QTAILQ_FOREACH(nc, &vlan->clients, next) {
351 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
352 func(DO_UPCAST(NICState, nc, nc), opaque);
353 }
354 }
355 }
356}
357
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100358int qemu_can_send_packet(VLANClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000359{
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100360 VLANState *vlan = sender->vlan;
aliguori63a01ef2008-10-31 19:10:00 +0000361 VLANClientState *vc;
362
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100363 if (sender->peer) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000364 if (sender->peer->receive_disabled) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100365 return 0;
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000366 } else if (sender->peer->info->can_receive &&
367 !sender->peer->info->can_receive(sender->peer)) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000368 return 0;
369 } else {
370 return 1;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100371 }
372 }
373
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100374 if (!sender->vlan) {
375 return 1;
376 }
377
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100378 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100379 if (vc == sender) {
380 continue;
381 }
382
Mark McLoughlincda90462009-05-18 13:13:16 +0100383 /* no can_receive() handler, they can always receive */
Vincent Palatin60c07d92011-03-02 17:25:02 -0500384 if (vc->info->can_receive && !vc->info->can_receive(vc)) {
385 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000386 }
387 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500388 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000389}
390
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100391static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100392 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100393 const uint8_t *data,
394 size_t size,
395 void *opaque)
396{
397 VLANClientState *vc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000398 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100399
400 if (vc->link_down) {
401 return size;
402 }
403
Mark McLoughlin893379e2009-10-27 18:16:36 +0000404 if (vc->receive_disabled) {
405 return 0;
406 }
407
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000408 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
409 ret = vc->info->receive_raw(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000410 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000411 ret = vc->info->receive(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000412 }
413
414 if (ret == 0) {
415 vc->receive_disabled = 1;
416 };
417
418 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100419}
420
Mark McLoughlinf7105842009-10-08 19:58:31 +0100421static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100422 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100423 const uint8_t *buf,
424 size_t size,
425 void *opaque)
aliguori63a01ef2008-10-31 19:10:00 +0000426{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100427 VLANState *vlan = opaque;
aliguori63a01ef2008-10-31 19:10:00 +0000428 VLANClientState *vc;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000429 ssize_t ret = -1;
aliguori63a01ef2008-10-31 19:10:00 +0000430
Mark McLoughlinf7105842009-10-08 19:58:31 +0100431 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100432 ssize_t len;
433
434 if (vc == sender) {
435 continue;
aliguori764a4d12009-04-21 19:56:41 +0000436 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100437
438 if (vc->link_down) {
439 ret = size;
440 continue;
441 }
442
Mark McLoughlin893379e2009-10-27 18:16:36 +0000443 if (vc->receive_disabled) {
444 ret = 0;
445 continue;
446 }
447
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000448 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
449 len = vc->info->receive_raw(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000450 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000451 len = vc->info->receive(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000452 }
453
454 if (len == 0) {
455 vc->receive_disabled = 1;
456 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100457
458 ret = (ret >= 0) ? ret : len;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000459
aliguori764a4d12009-04-21 19:56:41 +0000460 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100461
462 return ret;
aliguori764a4d12009-04-21 19:56:41 +0000463}
464
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100465void qemu_purge_queued_packets(VLANClientState *vc)
466{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100467 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100468
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100469 if (!vc->peer && !vc->vlan) {
470 return;
471 }
472
473 if (vc->peer) {
474 queue = vc->peer->send_queue;
475 } else {
476 queue = vc->vlan->send_queue;
477 }
478
479 qemu_net_queue_purge(queue, vc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100480}
481
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100482void qemu_flush_queued_packets(VLANClientState *vc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100483{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100484 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100485
Mark McLoughlin893379e2009-10-27 18:16:36 +0000486 vc->receive_disabled = 0;
487
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100488 if (vc->vlan) {
489 queue = vc->vlan->send_queue;
490 } else {
491 queue = vc->send_queue;
492 }
493
494 qemu_net_queue_flush(queue);
Mark McLoughline94667b2009-04-29 11:48:12 +0100495}
496
Mark McLoughlinca77d172009-10-22 17:43:41 +0100497static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
498 unsigned flags,
499 const uint8_t *buf, int size,
500 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000501{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100502 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100503
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100504#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100505 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100506 hex_dump(stdout, buf, size);
507#endif
508
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100509 if (sender->link_down || (!sender->peer && !sender->vlan)) {
510 return size;
511 }
512
513 if (sender->peer) {
514 queue = sender->peer->send_queue;
515 } else {
516 queue = sender->vlan->send_queue;
517 }
518
Mark McLoughlinca77d172009-10-22 17:43:41 +0100519 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
520}
521
522ssize_t qemu_send_packet_async(VLANClientState *sender,
523 const uint8_t *buf, int size,
524 NetPacketSent *sent_cb)
525{
526 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
527 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100528}
529
530void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
531{
532 qemu_send_packet_async(vc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000533}
534
Mark McLoughlinca77d172009-10-22 17:43:41 +0100535ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
536{
537 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
538 buf, size, NULL);
539}
540
aliguorifbe78f42008-12-17 19:13:11 +0000541static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
542 int iovcnt)
543{
544 uint8_t buffer[4096];
Benjamin Poirierce053662011-02-23 19:57:21 -0500545 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000546
Benjamin Poirierce053662011-02-23 19:57:21 -0500547 offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000548
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000549 return vc->info->receive(vc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000550}
551
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100552static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100553 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100554 const struct iovec *iov,
555 int iovcnt,
556 void *opaque)
557{
558 VLANClientState *vc = opaque;
559
560 if (vc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500561 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100562 }
563
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000564 if (vc->info->receive_iov) {
565 return vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100566 } else {
567 return vc_sendv_compat(vc, iov, iovcnt);
568 }
569}
570
Mark McLoughlinf7105842009-10-08 19:58:31 +0100571static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100572 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100573 const struct iovec *iov,
574 int iovcnt,
575 void *opaque)
Mark McLoughline94667b2009-04-29 11:48:12 +0100576{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100577 VLANState *vlan = opaque;
Mark McLoughline94667b2009-04-29 11:48:12 +0100578 VLANClientState *vc;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100579 ssize_t ret = -1;
Mark McLoughline94667b2009-04-29 11:48:12 +0100580
Mark McLoughlinf7105842009-10-08 19:58:31 +0100581 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughline94667b2009-04-29 11:48:12 +0100582 ssize_t len;
583
584 if (vc == sender) {
585 continue;
586 }
587
588 if (vc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500589 ret = iov_size(iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100590 continue;
591 }
592
Mark McLoughlinca77d172009-10-22 17:43:41 +0100593 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
594
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000595 if (vc->info->receive_iov) {
596 len = vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100597 } else {
598 len = vc_sendv_compat(vc, iov, iovcnt);
599 }
600
601 ret = (ret >= 0) ? ret : len;
602 }
603
Mark McLoughline94667b2009-04-29 11:48:12 +0100604 return ret;
605}
606
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100607ssize_t qemu_sendv_packet_async(VLANClientState *sender,
608 const struct iovec *iov, int iovcnt,
609 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000610{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100611 NetQueue *queue;
612
613 if (sender->link_down || (!sender->peer && !sender->vlan)) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500614 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000615 }
616
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100617 if (sender->peer) {
618 queue = sender->peer->send_queue;
619 } else {
620 queue = sender->vlan->send_queue;
621 }
622
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100623 return qemu_net_queue_send_iov(queue, sender,
624 QEMU_NET_PACKET_FLAG_NONE,
625 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000626}
627
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100628ssize_t
629qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
630{
631 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
632}
633
aliguori63a01ef2008-10-31 19:10:00 +0000634/* find or alloc a new VLAN */
Jan Kiszka1a609522009-06-24 14:42:31 +0200635VLANState *qemu_find_vlan(int id, int allocate)
aliguori63a01ef2008-10-31 19:10:00 +0000636{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100637 VLANState *vlan;
638
639 QTAILQ_FOREACH(vlan, &vlans, next) {
640 if (vlan->id == id) {
aliguori63a01ef2008-10-31 19:10:00 +0000641 return vlan;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100642 }
aliguori63a01ef2008-10-31 19:10:00 +0000643 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100644
Jan Kiszka1a609522009-06-24 14:42:31 +0200645 if (!allocate) {
646 return NULL;
647 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100648
Anthony Liguori7267c092011-08-20 22:09:37 -0500649 vlan = g_malloc0(sizeof(VLANState));
aliguori63a01ef2008-10-31 19:10:00 +0000650 vlan->id = id;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100651 QTAILQ_INIT(&vlan->clients);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100652
653 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
654 qemu_vlan_deliver_packet_iov,
655 vlan);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100656
657 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
658
aliguori63a01ef2008-10-31 19:10:00 +0000659 return vlan;
660}
661
Gerd Hoffmann2ef924b2009-10-21 15:25:24 +0200662VLANClientState *qemu_find_netdev(const char *id)
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100663{
664 VLANClientState *vc;
665
666 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200667 if (vc->info->type == NET_CLIENT_TYPE_NIC)
668 continue;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100669 if (!strcmp(vc->name, id)) {
670 return vc;
671 }
672 }
673
674 return NULL;
675}
676
aliguori76970792009-02-11 15:20:03 +0000677static int nic_get_free_idx(void)
678{
679 int index;
680
681 for (index = 0; index < MAX_NICS; index++)
682 if (!nd_table[index].used)
683 return index;
684 return -1;
685}
686
Markus Armbruster07caea32009-09-25 03:53:51 +0200687int qemu_show_nic_models(const char *arg, const char *const *models)
688{
689 int i;
690
691 if (!arg || strcmp(arg, "?"))
692 return 0;
693
694 fprintf(stderr, "qemu: Supported NIC models: ");
695 for (i = 0 ; models[i]; i++)
696 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
697 return 1;
698}
699
aliguorid07f22c2009-01-13 19:03:57 +0000700void qemu_check_nic_model(NICInfo *nd, const char *model)
701{
702 const char *models[2];
703
704 models[0] = model;
705 models[1] = NULL;
706
Markus Armbruster07caea32009-09-25 03:53:51 +0200707 if (qemu_show_nic_models(nd->model, models))
708 exit(0);
709 if (qemu_find_nic_model(nd, models, model) < 0)
710 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000711}
712
Markus Armbruster07caea32009-09-25 03:53:51 +0200713int qemu_find_nic_model(NICInfo *nd, const char * const *models,
714 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000715{
Markus Armbruster07caea32009-09-25 03:53:51 +0200716 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000717
718 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500719 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000720
Markus Armbruster07caea32009-09-25 03:53:51 +0200721 for (i = 0 ; models[i]; i++) {
722 if (strcmp(nd->model, models[i]) == 0)
723 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000724 }
725
Markus Armbruster6daf1942011-06-22 14:03:54 +0200726 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200727 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000728}
729
Mark McLoughlin5281d752009-10-22 17:49:07 +0100730int net_handle_fd_param(Monitor *mon, const char *param)
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100731{
Jason Wangf7c31d62010-10-25 13:39:59 +0800732 int fd;
733
734 if (!qemu_isdigit(param[0]) && mon) {
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100735
736 fd = monitor_get_fd(mon, param);
737 if (fd == -1) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100738 error_report("No file descriptor named %s found", param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100739 return -1;
740 }
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100741 } else {
Stefan Berger443916d2011-09-28 06:41:32 -0400742 fd = qemu_parse_fd(param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100743 }
Jason Wangf7c31d62010-10-25 13:39:59 +0800744
745 return fd;
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100746}
747
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100748static int net_init_nic(QemuOpts *opts,
749 Monitor *mon,
750 const char *name,
751 VLANState *vlan)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100752{
753 int idx;
754 NICInfo *nd;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100755 const char *netdev;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100756
757 idx = nic_get_free_idx();
758 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100759 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100760 return -1;
761 }
762
763 nd = &nd_table[idx];
764
765 memset(nd, 0, sizeof(*nd));
766
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100767 if ((netdev = qemu_opt_get(opts, "netdev"))) {
768 nd->netdev = qemu_find_netdev(netdev);
769 if (!nd->netdev) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100770 error_report("netdev '%s' not found", netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100771 return -1;
772 }
773 } else {
774 assert(vlan);
775 nd->vlan = vlan;
776 }
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100777 if (name) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500778 nd->name = g_strdup(name);
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100779 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100780 if (qemu_opt_get(opts, "model")) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500781 nd->model = g_strdup(qemu_opt_get(opts, "model"));
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100782 }
783 if (qemu_opt_get(opts, "addr")) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500784 nd->devaddr = g_strdup(qemu_opt_get(opts, "addr"));
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100785 }
786
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100787 if (qemu_opt_get(opts, "macaddr") &&
Jan Kiszka6eed1852011-07-20 12:20:22 +0200788 net_parse_macaddr(nd->macaddr.a, qemu_opt_get(opts, "macaddr")) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100789 error_report("invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100790 return -1;
791 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200792 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100793
Amit Shah75422b02010-02-25 17:24:43 +0530794 nd->nvectors = qemu_opt_get_number(opts, "vectors",
795 DEV_NVECTORS_UNSPECIFIED);
796 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100797 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100798 error_report("invalid # of vectors: %d", nd->nvectors);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100799 return -1;
800 }
801
802 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100803 nb_nics++;
804
805 return idx;
806}
807
808#define NET_COMMON_PARAMS_DESC \
809 { \
810 .name = "type", \
811 .type = QEMU_OPT_STRING, \
812 .help = "net client type (nic, tap etc.)", \
813 }, { \
814 .name = "vlan", \
815 .type = QEMU_OPT_NUMBER, \
816 .help = "vlan number", \
817 }, { \
818 .name = "name", \
819 .type = QEMU_OPT_STRING, \
820 .help = "identifier for monitor commands", \
821 }
822
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100823typedef int (*net_client_init_func)(QemuOpts *opts,
824 Monitor *mon,
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100825 const char *name,
826 VLANState *vlan);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100827
828/* magic number, but compiler will warn if too small */
829#define NET_MAX_DESC 20
830
Blue Swirl238431a2010-02-21 16:01:30 +0000831static const struct {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100832 const char *type;
833 net_client_init_func init;
834 QemuOptDesc desc[NET_MAX_DESC];
Jan Kiszka6f7b3b12011-07-20 12:20:20 +0200835} net_client_types[NET_CLIENT_TYPE_MAX] = {
836 [NET_CLIENT_TYPE_NONE] = {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100837 .type = "none",
838 .desc = {
839 NET_COMMON_PARAMS_DESC,
840 { /* end of list */ }
841 },
Jan Kiszka6f7b3b12011-07-20 12:20:20 +0200842 },
843 [NET_CLIENT_TYPE_NIC] = {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100844 .type = "nic",
845 .init = net_init_nic,
846 .desc = {
847 NET_COMMON_PARAMS_DESC,
848 {
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100849 .name = "netdev",
850 .type = QEMU_OPT_STRING,
851 .help = "id of -netdev to connect to",
852 },
853 {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100854 .name = "macaddr",
855 .type = QEMU_OPT_STRING,
856 .help = "MAC address",
857 }, {
858 .name = "model",
859 .type = QEMU_OPT_STRING,
860 .help = "device model (e1000, rtl8139, virtio etc.)",
861 }, {
862 .name = "addr",
863 .type = QEMU_OPT_STRING,
864 .help = "PCI device address",
865 }, {
866 .name = "vectors",
867 .type = QEMU_OPT_NUMBER,
868 .help = "number of MSI-x vectors, 0 to disable MSI-X",
869 },
870 { /* end of list */ }
871 },
Jan Kiszka6f7b3b12011-07-20 12:20:20 +0200872 },
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100873#ifdef CONFIG_SLIRP
Jan Kiszka6f7b3b12011-07-20 12:20:20 +0200874 [NET_CLIENT_TYPE_USER] = {
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100875 .type = "user",
876 .init = net_init_slirp,
877 .desc = {
878 NET_COMMON_PARAMS_DESC,
879 {
880 .name = "hostname",
881 .type = QEMU_OPT_STRING,
882 .help = "client hostname reported by the builtin DHCP server",
883 }, {
884 .name = "restrict",
885 .type = QEMU_OPT_STRING,
886 .help = "isolate the guest from the host (y|yes|n|no)",
887 }, {
888 .name = "ip",
889 .type = QEMU_OPT_STRING,
890 .help = "legacy parameter, use net= instead",
891 }, {
892 .name = "net",
893 .type = QEMU_OPT_STRING,
894 .help = "IP address and optional netmask",
895 }, {
896 .name = "host",
897 .type = QEMU_OPT_STRING,
898 .help = "guest-visible address of the host",
899 }, {
900 .name = "tftp",
901 .type = QEMU_OPT_STRING,
902 .help = "root directory of the built-in TFTP server",
903 }, {
904 .name = "bootfile",
905 .type = QEMU_OPT_STRING,
906 .help = "BOOTP filename, for use with tftp=",
907 }, {
908 .name = "dhcpstart",
909 .type = QEMU_OPT_STRING,
910 .help = "the first of the 16 IPs the built-in DHCP server can assign",
911 }, {
912 .name = "dns",
913 .type = QEMU_OPT_STRING,
914 .help = "guest-visible address of the virtual nameserver",
915 }, {
916 .name = "smb",
917 .type = QEMU_OPT_STRING,
918 .help = "root directory of the built-in SMB server",
919 }, {
920 .name = "smbserver",
921 .type = QEMU_OPT_STRING,
922 .help = "IP address of the built-in SMB server",
923 }, {
924 .name = "hostfwd",
925 .type = QEMU_OPT_STRING,
926 .help = "guest port number to forward incoming TCP or UDP connections",
927 }, {
928 .name = "guestfwd",
929 .type = QEMU_OPT_STRING,
930 .help = "IP address and port to forward guest TCP connections",
931 },
932 { /* end of list */ }
933 },
Jan Kiszka6f7b3b12011-07-20 12:20:20 +0200934 },
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100935#endif
Jan Kiszka6f7b3b12011-07-20 12:20:20 +0200936 [NET_CLIENT_TYPE_TAP] = {
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100937 .type = "tap",
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100938 .init = net_init_tap,
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100939 .desc = {
940 NET_COMMON_PARAMS_DESC,
941 {
942 .name = "ifname",
943 .type = QEMU_OPT_STRING,
944 .help = "interface name",
945 },
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100946#ifndef _WIN32
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100947 {
948 .name = "fd",
949 .type = QEMU_OPT_STRING,
950 .help = "file descriptor of an already opened tap",
951 }, {
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100952 .name = "script",
953 .type = QEMU_OPT_STRING,
954 .help = "script to initialize the interface",
955 }, {
956 .name = "downscript",
957 .type = QEMU_OPT_STRING,
958 .help = "script to shut down the interface",
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100959 }, {
Stefan Weil2944e4e2012-02-04 09:24:46 +0100960#ifdef CONFIG_NET_BRIDGE
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500961 .name = "helper",
962 .type = QEMU_OPT_STRING,
963 .help = "command to execute to configure bridge",
964 }, {
Stefan Weil2944e4e2012-02-04 09:24:46 +0100965#endif
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100966 .name = "sndbuf",
967 .type = QEMU_OPT_SIZE,
968 .help = "send buffer limit"
Mark McLoughlinbaf74c92009-10-22 17:43:37 +0100969 }, {
970 .name = "vnet_hdr",
971 .type = QEMU_OPT_BOOL,
972 .help = "enable the IFF_VNET_HDR flag on the tap interface"
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200973 }, {
974 .name = "vhost",
975 .type = QEMU_OPT_BOOL,
976 .help = "enable vhost-net network accelerator",
977 }, {
978 .name = "vhostfd",
979 .type = QEMU_OPT_STRING,
980 .help = "file descriptor of an already opened vhost net device",
Jason Wang96c94b22011-02-25 16:11:27 +0800981 }, {
982 .name = "vhostforce",
983 .type = QEMU_OPT_BOOL,
984 .help = "force vhost on for non-MSIX virtio guests",
985 },
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100986#endif /* _WIN32 */
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100987 { /* end of list */ }
988 },
Jan Kiszka6f7b3b12011-07-20 12:20:20 +0200989 },
990 [NET_CLIENT_TYPE_SOCKET] = {
Mark McLoughlin88ce16c2009-10-06 12:17:09 +0100991 .type = "socket",
992 .init = net_init_socket,
993 .desc = {
994 NET_COMMON_PARAMS_DESC,
995 {
996 .name = "fd",
997 .type = QEMU_OPT_STRING,
998 .help = "file descriptor of an already opened socket",
999 }, {
1000 .name = "listen",
1001 .type = QEMU_OPT_STRING,
1002 .help = "port number, and optional hostname, to listen on",
1003 }, {
1004 .name = "connect",
1005 .type = QEMU_OPT_STRING,
1006 .help = "port number, and optional hostname, to connect to",
1007 }, {
1008 .name = "mcast",
1009 .type = QEMU_OPT_STRING,
1010 .help = "UDP multicast address and port number",
Mike Ryan3a75e742010-12-01 11:16:47 -08001011 }, {
1012 .name = "localaddr",
1013 .type = QEMU_OPT_STRING,
Benjamin0e0e7fa2012-01-11 09:20:54 +09001014 .help = "source address and port for multicast and udp packets",
1015 }, {
1016 .name = "udp",
1017 .type = QEMU_OPT_STRING,
1018 .help = "UDP unicast address and port number",
Mark McLoughlin88ce16c2009-10-06 12:17:09 +01001019 },
1020 { /* end of list */ }
1021 },
Jan Kiszka6f7b3b12011-07-20 12:20:20 +02001022 },
Mark McLoughlindd510582009-10-06 12:17:10 +01001023#ifdef CONFIG_VDE
Jan Kiszka6f7b3b12011-07-20 12:20:20 +02001024 [NET_CLIENT_TYPE_VDE] = {
Mark McLoughlindd510582009-10-06 12:17:10 +01001025 .type = "vde",
1026 .init = net_init_vde,
1027 .desc = {
1028 NET_COMMON_PARAMS_DESC,
1029 {
1030 .name = "sock",
1031 .type = QEMU_OPT_STRING,
1032 .help = "socket path",
1033 }, {
1034 .name = "port",
1035 .type = QEMU_OPT_NUMBER,
1036 .help = "port number",
1037 }, {
1038 .name = "group",
1039 .type = QEMU_OPT_STRING,
1040 .help = "group owner of socket",
1041 }, {
1042 .name = "mode",
1043 .type = QEMU_OPT_NUMBER,
1044 .help = "permissions for socket",
1045 },
1046 { /* end of list */ }
1047 },
Jan Kiszka6f7b3b12011-07-20 12:20:20 +02001048 },
Mark McLoughlindd510582009-10-06 12:17:10 +01001049#endif
Jan Kiszka6f7b3b12011-07-20 12:20:20 +02001050 [NET_CLIENT_TYPE_DUMP] = {
Mark McLoughlined2955c2009-10-06 12:17:11 +01001051 .type = "dump",
1052 .init = net_init_dump,
1053 .desc = {
1054 NET_COMMON_PARAMS_DESC,
1055 {
1056 .name = "len",
1057 .type = QEMU_OPT_SIZE,
1058 .help = "per-packet size limit (64k default)",
1059 }, {
1060 .name = "file",
1061 .type = QEMU_OPT_STRING,
1062 .help = "dump file path (default is qemu-vlan0.pcap)",
1063 },
1064 { /* end of list */ }
1065 },
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001066 },
Stefan Weil2944e4e2012-02-04 09:24:46 +01001067#ifdef CONFIG_NET_BRIDGE
Corey Bryanta7c36ee2012-01-26 09:42:27 -05001068 [NET_CLIENT_TYPE_BRIDGE] = {
1069 .type = "bridge",
1070 .init = net_init_bridge,
1071 .desc = {
1072 NET_COMMON_PARAMS_DESC,
1073 {
1074 .name = "br",
1075 .type = QEMU_OPT_STRING,
1076 .help = "bridge name",
1077 }, {
1078 .name = "helper",
1079 .type = QEMU_OPT_STRING,
1080 .help = "command to execute to configure bridge",
1081 },
1082 { /* end of list */ }
1083 },
1084 },
Stefan Weil2944e4e2012-02-04 09:24:46 +01001085#endif /* CONFIG_NET_BRIDGE */
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001086};
1087
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001088int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001089{
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01001090 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001091 const char *type;
1092 int i;
1093
1094 type = qemu_opt_get(opts, "type");
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001095 if (!type) {
1096 qerror_report(QERR_MISSING_PARAMETER, "type");
1097 return -1;
1098 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001099
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001100 if (is_netdev) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001101 if (strcmp(type, "tap") != 0 &&
Stefan Weil2944e4e2012-02-04 09:24:46 +01001102#ifdef CONFIG_NET_BRIDGE
1103 strcmp(type, "bridge") != 0 &&
1104#endif
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001105#ifdef CONFIG_SLIRP
1106 strcmp(type, "user") != 0 &&
1107#endif
1108#ifdef CONFIG_VDE
1109 strcmp(type, "vde") != 0 &&
1110#endif
Stefan Weil2944e4e2012-02-04 09:24:46 +01001111 strcmp(type, "socket") != 0) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001112 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1113 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001114 return -1;
1115 }
1116
1117 if (qemu_opt_get(opts, "vlan")) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001118 qerror_report(QERR_INVALID_PARAMETER, "vlan");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001119 return -1;
1120 }
1121 if (qemu_opt_get(opts, "name")) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001122 qerror_report(QERR_INVALID_PARAMETER, "name");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001123 return -1;
1124 }
1125 if (!qemu_opts_id(opts)) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001126 qerror_report(QERR_MISSING_PARAMETER, "id");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001127 return -1;
1128 }
1129 }
1130
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01001131 name = qemu_opts_id(opts);
1132 if (!name) {
1133 name = qemu_opt_get(opts, "name");
1134 }
1135
Jan Kiszka6f7b3b12011-07-20 12:20:20 +02001136 for (i = 0; i < NET_CLIENT_TYPE_MAX; i++) {
1137 if (net_client_types[i].type != NULL &&
1138 !strcmp(net_client_types[i].type, type)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001139 VLANState *vlan = NULL;
Amit Shah50e32ea2010-06-08 21:13:58 +05301140 int ret;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001141
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001142 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1143 return -1;
1144 }
1145
Mark McLoughlin5869c4d2009-10-08 19:58:29 +01001146 /* Do not add to a vlan if it's a -netdev or a nic with a
1147 * netdev= parameter. */
1148 if (!(is_netdev ||
1149 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001150 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1151 }
1152
Amit Shah03c71552010-06-15 13:30:39 +05301153 ret = 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001154 if (net_client_types[i].init) {
Amit Shah50e32ea2010-06-08 21:13:58 +05301155 ret = net_client_types[i].init(opts, mon, name, vlan);
1156 if (ret < 0) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001157 /* TODO push error reporting into init() methods */
1158 qerror_report(QERR_DEVICE_INIT_FAILED, type);
1159 return -1;
1160 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001161 }
Amit Shah50e32ea2010-06-08 21:13:58 +05301162 return ret;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001163 }
1164 }
1165
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001166 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1167 "a network client type");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001168 return -1;
1169}
1170
aliguori6f338c32009-02-11 15:21:54 +00001171static int net_host_check_device(const char *device)
1172{
1173 int i;
aliguoribb9ea792009-04-21 19:56:28 +00001174 const char *valid_param_list[] = { "tap", "socket", "dump"
Stefan Weil2944e4e2012-02-04 09:24:46 +01001175#ifdef CONFIG_NET_BRIDGE
1176 , "bridge"
1177#endif
aliguori6f338c32009-02-11 15:21:54 +00001178#ifdef CONFIG_SLIRP
1179 ,"user"
1180#endif
1181#ifdef CONFIG_VDE
1182 ,"vde"
1183#endif
1184 };
1185 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1186 if (!strncmp(valid_param_list[i], device,
1187 strlen(valid_param_list[i])))
1188 return 1;
1189 }
1190
1191 return 0;
1192}
1193
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001194void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001195{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001196 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001197 const char *opts_str = qdict_get_try_str(qdict, "opts");
1198 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001199
aliguori6f338c32009-02-11 15:21:54 +00001200 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001201 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001202 return;
1203 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001204
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001205 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001206 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001207 return;
1208 }
1209
1210 qemu_opt_set(opts, "type", device);
1211
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001212 if (net_client_init(mon, opts, 0) < 0) {
aliguori5c8be672009-04-21 19:56:32 +00001213 monitor_printf(mon, "adding host network device %s failed\n", device);
1214 }
aliguori6f338c32009-02-11 15:21:54 +00001215}
1216
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001217void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001218{
aliguori6f338c32009-02-11 15:21:54 +00001219 VLANClientState *vc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001220 int vlan_id = qdict_get_int(qdict, "vlan_id");
1221 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001222
Jan Kiszka1a609522009-06-24 14:42:31 +02001223 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
aliguori6f338c32009-02-11 15:21:54 +00001224 if (!vc) {
aliguori6f338c32009-02-11 15:21:54 +00001225 return;
1226 }
aliguorie8f1f9d2009-04-21 19:56:08 +00001227 if (!net_host_check_device(vc->model)) {
1228 monitor_printf(mon, "invalid host network device %s\n", device);
1229 return;
1230 }
aliguori6f338c32009-02-11 15:21:54 +00001231 qemu_del_vlan_client(vc);
1232}
1233
Markus Armbrusterae82d322010-03-25 17:22:40 +01001234int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1235{
1236 QemuOpts *opts;
1237 int res;
1238
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001239 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001240 if (!opts) {
1241 return -1;
1242 }
1243
1244 res = net_client_init(mon, opts, 1);
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001245 if (res < 0) {
1246 qemu_opts_del(opts);
1247 }
1248
Markus Armbrusterae82d322010-03-25 17:22:40 +01001249 return res;
1250}
1251
Markus Armbrusterae82d322010-03-25 17:22:40 +01001252int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1253{
1254 const char *id = qdict_get_str(qdict, "id");
1255 VLANClientState *vc;
1256
1257 vc = qemu_find_netdev(id);
Markus Armbruster85dde9a2011-06-16 18:45:37 +02001258 if (!vc) {
Markus Armbrusterae82d322010-03-25 17:22:40 +01001259 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1260 return -1;
1261 }
Markus Armbrusterae82d322010-03-25 17:22:40 +01001262 qemu_del_vlan_client(vc);
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001263 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
Markus Armbrusterae82d322010-03-25 17:22:40 +01001264 return 0;
1265}
1266
Jan Kiszka44e798d2011-07-20 12:20:21 +02001267static void print_net_client(Monitor *mon, VLANClientState *vc)
1268{
1269 monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
1270 net_client_types[vc->info->type].type, vc->info_str);
1271}
1272
aliguori376253e2009-03-05 23:01:23 +00001273void do_info_network(Monitor *mon)
aliguori63a01ef2008-10-31 19:10:00 +00001274{
1275 VLANState *vlan;
Jan Kiszka19061e62011-07-20 12:20:19 +02001276 VLANClientState *vc, *peer;
1277 net_client_type type;
aliguori63a01ef2008-10-31 19:10:00 +00001278
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001279 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori376253e2009-03-05 23:01:23 +00001280 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001281
1282 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka44e798d2011-07-20 12:20:21 +02001283 monitor_printf(mon, " ");
1284 print_net_client(mon, vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001285 }
aliguori63a01ef2008-10-31 19:10:00 +00001286 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001287 monitor_printf(mon, "Devices not on any VLAN:\n");
1288 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
Jan Kiszka19061e62011-07-20 12:20:19 +02001289 peer = vc->peer;
1290 type = vc->info->type;
1291 if (!peer || type == NET_CLIENT_TYPE_NIC) {
Jan Kiszka44e798d2011-07-20 12:20:21 +02001292 monitor_printf(mon, " ");
1293 print_net_client(mon, vc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001294 } /* else it's a netdev connected to a NIC, printed with the NIC */
1295 if (peer && type == NET_CLIENT_TYPE_NIC) {
Jan Kiszka44e798d2011-07-20 12:20:21 +02001296 monitor_printf(mon, " \\ ");
1297 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001298 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001299 }
aliguori63a01ef2008-10-31 19:10:00 +00001300}
1301
Luiz Capitulino4b371562011-11-23 13:11:55 -02001302void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001303{
1304 VLANState *vlan;
1305 VLANClientState *vc = NULL;
1306
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001307 QTAILQ_FOREACH(vlan, &vlans, next) {
1308 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1309 if (strcmp(vc->name, name) == 0) {
edgar_igldd5de372009-01-09 00:48:28 +00001310 goto done;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001311 }
1312 }
1313 }
Markus Armbruster85dde9a2011-06-16 18:45:37 +02001314 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1315 if (!strcmp(vc->name, name)) {
1316 goto done;
1317 }
1318 }
edgar_igldd5de372009-01-09 00:48:28 +00001319done:
aliguori436e5e52009-01-08 19:44:06 +00001320
1321 if (!vc) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001322 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1323 return;
aliguori436e5e52009-01-08 19:44:06 +00001324 }
1325
Markus Armbrusterc9b26a42010-03-26 09:07:10 +01001326 vc->link_down = !up;
aliguori436e5e52009-01-08 19:44:06 +00001327
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001328 if (vc->info->link_status_changed) {
1329 vc->info->link_status_changed(vc);
1330 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001331
1332 /* Notify peer. Don't update peer link status: this makes it possible to
1333 * disconnect from host network without notifying the guest.
1334 * FIXME: is disconnected link status change operation useful?
1335 *
1336 * Current behaviour is compatible with qemu vlans where there could be
1337 * multiple clients that can still communicate with each other in
1338 * disconnected mode. For now maintain this compatibility. */
1339 if (vc->peer && vc->peer->info->link_status_changed) {
1340 vc->peer->info->link_status_changed(vc->peer);
1341 }
aliguori436e5e52009-01-08 19:44:06 +00001342}
1343
aliguori63a01ef2008-10-31 19:10:00 +00001344void net_cleanup(void)
1345{
1346 VLANState *vlan;
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001347 VLANClientState *vc, *next_vc;
aliguori63a01ef2008-10-31 19:10:00 +00001348
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001349 QTAILQ_FOREACH(vlan, &vlans, next) {
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001350 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
aliguorib946a152009-04-17 17:11:08 +00001351 qemu_del_vlan_client(vc);
aliguori63a01ef2008-10-31 19:10:00 +00001352 }
1353 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001354
1355 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1356 qemu_del_vlan_client(vc);
1357 }
aliguori63a01ef2008-10-31 19:10:00 +00001358}
1359
Markus Armbruster668680f2010-02-11 14:44:58 +01001360void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001361{
1362 VLANState *vlan;
Markus Armbruster62112d12010-02-11 14:44:59 +01001363 VLANClientState *vc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001364 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001365
Peter Maydell641f6ea2011-05-20 16:50:00 +01001366 /* Don't warn about the default network setup that you get if
1367 * no command line -net or -netdev options are specified. There
1368 * are two cases that we would otherwise complain about:
1369 * (1) board doesn't support a NIC but the implicit "-net nic"
1370 * requested one
1371 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1372 * sets up a nic that isn't connected to anything.
1373 */
1374 if (default_net) {
1375 return;
1376 }
1377
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001378 QTAILQ_FOREACH(vlan, &vlans, next) {
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001379 int has_nic = 0, has_host_dev = 0;
1380
Markus Armbruster62112d12010-02-11 14:44:59 +01001381 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1382 switch (vc->info->type) {
1383 case NET_CLIENT_TYPE_NIC:
1384 has_nic = 1;
1385 break;
Jan Kiszka6f7b3b12011-07-20 12:20:20 +02001386 case NET_CLIENT_TYPE_USER:
Markus Armbruster62112d12010-02-11 14:44:59 +01001387 case NET_CLIENT_TYPE_TAP:
1388 case NET_CLIENT_TYPE_SOCKET:
1389 case NET_CLIENT_TYPE_VDE:
1390 has_host_dev = 1;
1391 break;
1392 default: ;
1393 }
1394 }
1395 if (has_host_dev && !has_nic)
aliguori63a01ef2008-10-31 19:10:00 +00001396 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
Markus Armbruster62112d12010-02-11 14:44:59 +01001397 if (has_nic && !has_host_dev)
aliguori63a01ef2008-10-31 19:10:00 +00001398 fprintf(stderr,
1399 "Warning: vlan %d is not connected to host network\n",
1400 vlan->id);
1401 }
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001402 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1403 if (!vc->peer) {
1404 fprintf(stderr, "Warning: %s %s has no peer\n",
1405 vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1406 vc->name);
1407 }
1408 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001409
1410 /* Check that all NICs requested via -net nic actually got created.
1411 * NICs created via -device don't need to be checked here because
1412 * they are always instantiated.
1413 */
1414 for (i = 0; i < MAX_NICS; i++) {
1415 NICInfo *nd = &nd_table[i];
1416 if (nd->used && !nd->instantiated) {
1417 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1418 "was not created (not supported by this machine?)\n",
1419 nd->name ? nd->name : "anonymous",
1420 nd->model ? nd->model : "unspecified");
1421 }
1422 }
aliguori63a01ef2008-10-31 19:10:00 +00001423}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001424
1425static int net_init_client(QemuOpts *opts, void *dummy)
1426{
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001427 if (net_client_init(NULL, opts, 0) < 0)
1428 return -1;
1429 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001430}
1431
1432static int net_init_netdev(QemuOpts *opts, void *dummy)
1433{
1434 return net_client_init(NULL, opts, 1);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001435}
1436
1437int net_init_clients(void)
1438{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001439 QemuOptsList *net = qemu_find_opts("net");
1440
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001441 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001442 /* if no clients, we use a default config */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001443 qemu_opts_set(net, NULL, "type", "nic");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001444#ifdef CONFIG_SLIRP
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001445 qemu_opts_set(net, NULL, "type", "user");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001446#endif
1447 }
1448
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001449 QTAILQ_INIT(&vlans);
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001450 QTAILQ_INIT(&non_vlan_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001451
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001452 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001453 return -1;
1454
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001455 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001456 return -1;
1457 }
1458
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001459 return 0;
1460}
1461
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001462int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001463{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001464#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001465 int ret;
1466 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001467 return ret;
1468 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001469#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001470
Markus Armbruster8212c642010-02-10 19:52:18 +01001471 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001472 return -1;
1473 }
1474
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001475 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001476 return 0;
1477}
Jason Wang7fc8d912012-03-05 11:08:50 +08001478
1479/* From FreeBSD */
1480/* XXX: optimize */
1481unsigned compute_mcast_idx(const uint8_t *ep)
1482{
1483 uint32_t crc;
1484 int carry, i, j;
1485 uint8_t b;
1486
1487 crc = 0xffffffff;
1488 for (i = 0; i < 6; i++) {
1489 b = *ep++;
1490 for (j = 0; j < 8; j++) {
1491 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1492 crc <<= 1;
1493 b >>= 1;
1494 if (carry) {
1495 crc = ((crc ^ POLYNOMIAL) | carry);
1496 }
1497 }
1498 }
1499 return crc >> 26;
1500}