blob: 6ef93e615c8db9d2de230fc1b988213d763ba4f7 [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"
38
Mark McLoughlin5610c3a2009-10-08 19:58:23 +010039static QTAILQ_HEAD(, VLANState) vlans;
Mark McLoughlin577c4af2009-10-08 19:58:28 +010040static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
aliguori63a01ef2008-10-31 19:10:00 +000041
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010042int default_net = 1;
43
aliguori63a01ef2008-10-31 19:10:00 +000044/***********************************************************/
45/* network device redirectors */
46
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000047#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000048static void hex_dump(FILE *f, const uint8_t *buf, int size)
49{
50 int len, i, j, c;
51
52 for(i=0;i<size;i+=16) {
53 len = size - i;
54 if (len > 16)
55 len = 16;
56 fprintf(f, "%08x ", i);
57 for(j=0;j<16;j++) {
58 if (j < len)
59 fprintf(f, " %02x", buf[i+j]);
60 else
61 fprintf(f, " ");
62 }
63 fprintf(f, " ");
64 for(j=0;j<len;j++) {
65 c = buf[i+j];
66 if (c < ' ' || c > '~')
67 c = '.';
68 fprintf(f, "%c", c);
69 }
70 fprintf(f, "\n");
71 }
72}
73#endif
74
aliguori63a01ef2008-10-31 19:10:00 +000075static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
76{
77 const char *p, *p1;
78 int len;
79 p = *pp;
80 p1 = strchr(p, sep);
81 if (!p1)
82 return -1;
83 len = p1 - p;
84 p1++;
85 if (buf_size > 0) {
86 if (len > buf_size - 1)
87 len = buf_size - 1;
88 memcpy(buf, p, len);
89 buf[len] = '\0';
90 }
91 *pp = p1;
92 return 0;
93}
94
95int parse_host_src_port(struct sockaddr_in *haddr,
96 struct sockaddr_in *saddr,
97 const char *input_str)
98{
99 char *str = strdup(input_str);
100 char *host_str = str;
101 char *src_str;
102 const char *src_str2;
103 char *ptr;
104
105 /*
106 * Chop off any extra arguments at the end of the string which
107 * would start with a comma, then fill in the src port information
108 * if it was provided else use the "any address" and "any port".
109 */
110 if ((ptr = strchr(str,',')))
111 *ptr = '\0';
112
113 if ((src_str = strchr(input_str,'@'))) {
114 *src_str = '\0';
115 src_str++;
116 }
117
118 if (parse_host_port(haddr, host_str) < 0)
119 goto fail;
120
121 src_str2 = src_str;
122 if (!src_str || *src_str == '\0')
123 src_str2 = ":0";
124
125 if (parse_host_port(saddr, src_str2) < 0)
126 goto fail;
127
128 free(str);
129 return(0);
130
131fail:
132 free(str);
133 return -1;
134}
135
136int parse_host_port(struct sockaddr_in *saddr, const char *str)
137{
138 char buf[512];
139 struct hostent *he;
140 const char *p, *r;
141 int port;
142
143 p = str;
144 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
145 return -1;
146 saddr->sin_family = AF_INET;
147 if (buf[0] == '\0') {
148 saddr->sin_addr.s_addr = 0;
149 } else {
blueswir1cd390082008-11-16 13:53:32 +0000150 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000151 if (!inet_aton(buf, &saddr->sin_addr))
152 return -1;
153 } else {
154 if ((he = gethostbyname(buf)) == NULL)
155 return - 1;
156 saddr->sin_addr = *(struct in_addr *)he->h_addr;
157 }
158 }
159 port = strtol(p, (char **)&r, 0);
160 if (r == p)
161 return -1;
162 saddr->sin_port = htons(port);
163 return 0;
164}
165
aliguori7cb7434b2009-01-07 17:46:21 +0000166void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
167{
168 snprintf(vc->info_str, sizeof(vc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000169 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
170 vc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000171 macaddr[0], macaddr[1], macaddr[2],
172 macaddr[3], macaddr[4], macaddr[5]);
173}
174
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200175void qemu_macaddr_default_if_unset(MACAddr *macaddr)
176{
177 static int index = 0;
178 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
179
180 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
181 return;
182 macaddr->a[0] = 0x52;
183 macaddr->a[1] = 0x54;
184 macaddr->a[2] = 0x00;
185 macaddr->a[3] = 0x12;
186 macaddr->a[4] = 0x34;
187 macaddr->a[5] = 0x56 + index++;
188}
189
aliguori676cff22009-01-07 17:43:44 +0000190static char *assign_name(VLANClientState *vc1, const char *model)
191{
192 VLANState *vlan;
193 char buf[256];
194 int id = 0;
195
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100196 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori676cff22009-01-07 17:43:44 +0000197 VLANClientState *vc;
198
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100199 QTAILQ_FOREACH(vc, &vlan->clients, next) {
200 if (vc != vc1 && strcmp(vc->model, model) == 0) {
aliguori676cff22009-01-07 17:43:44 +0000201 id++;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100202 }
203 }
aliguori676cff22009-01-07 17:43:44 +0000204 }
205
206 snprintf(buf, sizeof(buf), "%s.%d", model, id);
207
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100208 return qemu_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000209}
210
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100211static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100212 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100213 const uint8_t *data,
214 size_t size,
215 void *opaque);
216static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100217 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100218 const struct iovec *iov,
219 int iovcnt,
220 void *opaque);
221
Mark McLoughlin45460d12009-11-25 18:49:02 +0000222VLANClientState *qemu_new_net_client(NetClientInfo *info,
223 VLANState *vlan,
224 VLANClientState *peer,
225 const char *model,
226 const char *name)
aliguori63a01ef2008-10-31 19:10:00 +0000227{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100228 VLANClientState *vc;
229
Mark McLoughlin45460d12009-11-25 18:49:02 +0000230 assert(info->size >= sizeof(VLANClientState));
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100231
Mark McLoughlin45460d12009-11-25 18:49:02 +0000232 vc = qemu_mallocz(info->size);
233
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000234 vc->info = info;
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100235 vc->model = qemu_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000236 if (name) {
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100237 vc->name = qemu_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000238 } else {
aliguori7a9f6e42009-01-07 17:48:51 +0000239 vc->name = assign_name(vc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000240 }
aliguori63a01ef2008-10-31 19:10:00 +0000241
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100242 if (vlan) {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100243 assert(!peer);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100244 vc->vlan = vlan;
245 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100246 } else {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100247 if (peer) {
248 vc->peer = peer;
249 peer->peer = vc;
250 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100251 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100252
253 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
254 qemu_deliver_packet_iov,
255 vc);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100256 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100257
aliguori63a01ef2008-10-31 19:10:00 +0000258 return vc;
259}
260
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000261NICState *qemu_new_nic(NetClientInfo *info,
262 NICConf *conf,
263 const char *model,
264 const char *name,
265 void *opaque)
266{
267 VLANClientState *nc;
268 NICState *nic;
269
270 assert(info->type == NET_CLIENT_TYPE_NIC);
271 assert(info->size >= sizeof(NICState));
272
273 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
274
275 nic = DO_UPCAST(NICState, nc, nc);
276 nic->conf = conf;
277 nic->opaque = opaque;
278
279 return nic;
280}
281
aliguori63a01ef2008-10-31 19:10:00 +0000282void qemu_del_vlan_client(VLANClientState *vc)
283{
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100284 if (vc->vlan) {
285 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100286 } else {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100287 if (vc->send_queue) {
288 qemu_del_net_queue(vc->send_queue);
289 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100290 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100291 if (vc->peer) {
292 vc->peer->peer = NULL;
293 }
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100294 }
aliguori63a01ef2008-10-31 19:10:00 +0000295
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000296 if (vc->info->cleanup) {
297 vc->info->cleanup(vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100298 }
299
300 qemu_free(vc->name);
301 qemu_free(vc->model);
302 qemu_free(vc);
aliguori63a01ef2008-10-31 19:10:00 +0000303}
304
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000305VLANClientState *
Jan Kiszka1a609522009-06-24 14:42:31 +0200306qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
307 const char *client_str)
308{
309 VLANState *vlan;
310 VLANClientState *vc;
311
312 vlan = qemu_find_vlan(vlan_id, 0);
313 if (!vlan) {
314 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
315 return NULL;
316 }
317
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100318 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka1a609522009-06-24 14:42:31 +0200319 if (!strcmp(vc->name, client_str)) {
320 break;
321 }
322 }
323 if (!vc) {
324 monitor_printf(mon, "can't find device %s on VLAN %d\n",
325 client_str, vlan_id);
326 }
327
328 return vc;
329}
330
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000331void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
332{
333 VLANClientState *nc;
334 VLANState *vlan;
335
336 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
337 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
338 func(DO_UPCAST(NICState, nc, nc), opaque);
339 }
340 }
341
342 QTAILQ_FOREACH(vlan, &vlans, next) {
343 QTAILQ_FOREACH(nc, &vlan->clients, next) {
344 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
345 func(DO_UPCAST(NICState, nc, nc), opaque);
346 }
347 }
348 }
349}
350
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100351int qemu_can_send_packet(VLANClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000352{
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100353 VLANState *vlan = sender->vlan;
aliguori63a01ef2008-10-31 19:10:00 +0000354 VLANClientState *vc;
355
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100356 if (sender->peer) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000357 if (sender->peer->receive_disabled) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100358 return 0;
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000359 } else if (sender->peer->info->can_receive &&
360 !sender->peer->info->can_receive(sender->peer)) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000361 return 0;
362 } else {
363 return 1;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100364 }
365 }
366
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100367 if (!sender->vlan) {
368 return 1;
369 }
370
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100371 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100372 if (vc == sender) {
373 continue;
374 }
375
Mark McLoughlincda90462009-05-18 13:13:16 +0100376 /* no can_receive() handler, they can always receive */
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000377 if (!vc->info->can_receive || vc->info->can_receive(vc)) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100378 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000379 }
380 }
381 return 0;
382}
383
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100384static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100385 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100386 const uint8_t *data,
387 size_t size,
388 void *opaque)
389{
390 VLANClientState *vc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000391 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100392
393 if (vc->link_down) {
394 return size;
395 }
396
Mark McLoughlin893379e2009-10-27 18:16:36 +0000397 if (vc->receive_disabled) {
398 return 0;
399 }
400
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000401 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
402 ret = vc->info->receive_raw(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000403 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000404 ret = vc->info->receive(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000405 }
406
407 if (ret == 0) {
408 vc->receive_disabled = 1;
409 };
410
411 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100412}
413
Mark McLoughlinf7105842009-10-08 19:58:31 +0100414static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100415 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100416 const uint8_t *buf,
417 size_t size,
418 void *opaque)
aliguori63a01ef2008-10-31 19:10:00 +0000419{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100420 VLANState *vlan = opaque;
aliguori63a01ef2008-10-31 19:10:00 +0000421 VLANClientState *vc;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000422 ssize_t ret = -1;
aliguori63a01ef2008-10-31 19:10:00 +0000423
Mark McLoughlinf7105842009-10-08 19:58:31 +0100424 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100425 ssize_t len;
426
427 if (vc == sender) {
428 continue;
aliguori764a4d12009-04-21 19:56:41 +0000429 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100430
431 if (vc->link_down) {
432 ret = size;
433 continue;
434 }
435
Mark McLoughlin893379e2009-10-27 18:16:36 +0000436 if (vc->receive_disabled) {
437 ret = 0;
438 continue;
439 }
440
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000441 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
442 len = vc->info->receive_raw(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000443 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000444 len = vc->info->receive(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000445 }
446
447 if (len == 0) {
448 vc->receive_disabled = 1;
449 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100450
451 ret = (ret >= 0) ? ret : len;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000452
aliguori764a4d12009-04-21 19:56:41 +0000453 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100454
455 return ret;
aliguori764a4d12009-04-21 19:56:41 +0000456}
457
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100458void qemu_purge_queued_packets(VLANClientState *vc)
459{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100460 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100461
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100462 if (!vc->peer && !vc->vlan) {
463 return;
464 }
465
466 if (vc->peer) {
467 queue = vc->peer->send_queue;
468 } else {
469 queue = vc->vlan->send_queue;
470 }
471
472 qemu_net_queue_purge(queue, vc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100473}
474
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100475void qemu_flush_queued_packets(VLANClientState *vc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100476{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100477 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100478
Mark McLoughlin893379e2009-10-27 18:16:36 +0000479 vc->receive_disabled = 0;
480
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100481 if (vc->vlan) {
482 queue = vc->vlan->send_queue;
483 } else {
484 queue = vc->send_queue;
485 }
486
487 qemu_net_queue_flush(queue);
Mark McLoughline94667b2009-04-29 11:48:12 +0100488}
489
Mark McLoughlinca77d172009-10-22 17:43:41 +0100490static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
491 unsigned flags,
492 const uint8_t *buf, int size,
493 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000494{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100495 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100496
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100497#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100498 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100499 hex_dump(stdout, buf, size);
500#endif
501
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100502 if (sender->link_down || (!sender->peer && !sender->vlan)) {
503 return size;
504 }
505
506 if (sender->peer) {
507 queue = sender->peer->send_queue;
508 } else {
509 queue = sender->vlan->send_queue;
510 }
511
Mark McLoughlinca77d172009-10-22 17:43:41 +0100512 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
513}
514
515ssize_t qemu_send_packet_async(VLANClientState *sender,
516 const uint8_t *buf, int size,
517 NetPacketSent *sent_cb)
518{
519 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
520 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100521}
522
523void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
524{
525 qemu_send_packet_async(vc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000526}
527
Mark McLoughlinca77d172009-10-22 17:43:41 +0100528ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
529{
530 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
531 buf, size, NULL);
532}
533
aliguorifbe78f42008-12-17 19:13:11 +0000534static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
535 int iovcnt)
536{
537 uint8_t buffer[4096];
538 size_t offset = 0;
539 int i;
540
541 for (i = 0; i < iovcnt; i++) {
542 size_t len;
543
544 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
545 memcpy(buffer + offset, iov[i].iov_base, len);
546 offset += len;
547 }
548
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000549 return vc->info->receive(vc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000550}
551
aliguorie0e78772009-01-26 15:37:44 +0000552static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
553{
554 size_t offset = 0;
555 int i;
556
557 for (i = 0; i < iovcnt; i++)
558 offset += iov[i].iov_len;
559 return offset;
560}
561
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100562static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100563 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100564 const struct iovec *iov,
565 int iovcnt,
566 void *opaque)
567{
568 VLANClientState *vc = opaque;
569
570 if (vc->link_down) {
571 return calc_iov_length(iov, iovcnt);
572 }
573
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000574 if (vc->info->receive_iov) {
575 return vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100576 } else {
577 return vc_sendv_compat(vc, iov, iovcnt);
578 }
579}
580
Mark McLoughlinf7105842009-10-08 19:58:31 +0100581static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100582 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100583 const struct iovec *iov,
584 int iovcnt,
585 void *opaque)
Mark McLoughline94667b2009-04-29 11:48:12 +0100586{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100587 VLANState *vlan = opaque;
Mark McLoughline94667b2009-04-29 11:48:12 +0100588 VLANClientState *vc;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100589 ssize_t ret = -1;
Mark McLoughline94667b2009-04-29 11:48:12 +0100590
Mark McLoughlinf7105842009-10-08 19:58:31 +0100591 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughline94667b2009-04-29 11:48:12 +0100592 ssize_t len;
593
594 if (vc == sender) {
595 continue;
596 }
597
598 if (vc->link_down) {
599 ret = calc_iov_length(iov, iovcnt);
600 continue;
601 }
602
Mark McLoughlinca77d172009-10-22 17:43:41 +0100603 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
604
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000605 if (vc->info->receive_iov) {
606 len = vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100607 } else {
608 len = vc_sendv_compat(vc, iov, iovcnt);
609 }
610
611 ret = (ret >= 0) ? ret : len;
612 }
613
Mark McLoughline94667b2009-04-29 11:48:12 +0100614 return ret;
615}
616
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100617ssize_t qemu_sendv_packet_async(VLANClientState *sender,
618 const struct iovec *iov, int iovcnt,
619 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000620{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100621 NetQueue *queue;
622
623 if (sender->link_down || (!sender->peer && !sender->vlan)) {
aliguorie0e78772009-01-26 15:37:44 +0000624 return calc_iov_length(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000625 }
626
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100627 if (sender->peer) {
628 queue = sender->peer->send_queue;
629 } else {
630 queue = sender->vlan->send_queue;
631 }
632
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100633 return qemu_net_queue_send_iov(queue, sender,
634 QEMU_NET_PACKET_FLAG_NONE,
635 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000636}
637
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100638ssize_t
639qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
640{
641 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
642}
643
aliguori63a01ef2008-10-31 19:10:00 +0000644/* find or alloc a new VLAN */
Jan Kiszka1a609522009-06-24 14:42:31 +0200645VLANState *qemu_find_vlan(int id, int allocate)
aliguori63a01ef2008-10-31 19:10:00 +0000646{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100647 VLANState *vlan;
648
649 QTAILQ_FOREACH(vlan, &vlans, next) {
650 if (vlan->id == id) {
aliguori63a01ef2008-10-31 19:10:00 +0000651 return vlan;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100652 }
aliguori63a01ef2008-10-31 19:10:00 +0000653 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100654
Jan Kiszka1a609522009-06-24 14:42:31 +0200655 if (!allocate) {
656 return NULL;
657 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100658
aliguori63a01ef2008-10-31 19:10:00 +0000659 vlan = qemu_mallocz(sizeof(VLANState));
aliguori63a01ef2008-10-31 19:10:00 +0000660 vlan->id = id;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100661 QTAILQ_INIT(&vlan->clients);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100662
663 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
664 qemu_vlan_deliver_packet_iov,
665 vlan);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100666
667 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
668
aliguori63a01ef2008-10-31 19:10:00 +0000669 return vlan;
670}
671
Gerd Hoffmann2ef924b2009-10-21 15:25:24 +0200672VLANClientState *qemu_find_netdev(const char *id)
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100673{
674 VLANClientState *vc;
675
676 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
677 if (!strcmp(vc->name, id)) {
678 return vc;
679 }
680 }
681
682 return NULL;
683}
684
aliguori76970792009-02-11 15:20:03 +0000685static int nic_get_free_idx(void)
686{
687 int index;
688
689 for (index = 0; index < MAX_NICS; index++)
690 if (!nd_table[index].used)
691 return index;
692 return -1;
693}
694
Markus Armbruster07caea32009-09-25 03:53:51 +0200695int qemu_show_nic_models(const char *arg, const char *const *models)
696{
697 int i;
698
699 if (!arg || strcmp(arg, "?"))
700 return 0;
701
702 fprintf(stderr, "qemu: Supported NIC models: ");
703 for (i = 0 ; models[i]; i++)
704 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
705 return 1;
706}
707
aliguorid07f22c2009-01-13 19:03:57 +0000708void qemu_check_nic_model(NICInfo *nd, const char *model)
709{
710 const char *models[2];
711
712 models[0] = model;
713 models[1] = NULL;
714
Markus Armbruster07caea32009-09-25 03:53:51 +0200715 if (qemu_show_nic_models(nd->model, models))
716 exit(0);
717 if (qemu_find_nic_model(nd, models, model) < 0)
718 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000719}
720
Markus Armbruster07caea32009-09-25 03:53:51 +0200721int qemu_find_nic_model(NICInfo *nd, const char * const *models,
722 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000723{
Markus Armbruster07caea32009-09-25 03:53:51 +0200724 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000725
726 if (!nd->model)
Mark McLoughlin32a8e142009-10-06 12:16:51 +0100727 nd->model = qemu_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000728
Markus Armbruster07caea32009-09-25 03:53:51 +0200729 for (i = 0 ; models[i]; i++) {
730 if (strcmp(nd->model, models[i]) == 0)
731 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000732 }
733
Markus Armbruster07caea32009-09-25 03:53:51 +0200734 qemu_error("qemu: Unsupported NIC model: %s\n", nd->model);
735 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000736}
737
Mark McLoughlin5281d752009-10-22 17:49:07 +0100738int net_handle_fd_param(Monitor *mon, const char *param)
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100739{
740 if (!qemu_isdigit(param[0])) {
741 int fd;
742
743 fd = monitor_get_fd(mon, param);
744 if (fd == -1) {
Markus Armbrusterfb125772009-10-06 12:16:58 +0100745 qemu_error("No file descriptor named %s found", param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100746 return -1;
747 }
748
749 return fd;
750 } else {
751 return strtol(param, NULL, 0);
752 }
753}
754
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100755static int net_init_nic(QemuOpts *opts,
756 Monitor *mon,
757 const char *name,
758 VLANState *vlan)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100759{
760 int idx;
761 NICInfo *nd;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100762 const char *netdev;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100763
764 idx = nic_get_free_idx();
765 if (idx == -1 || nb_nics >= MAX_NICS) {
766 qemu_error("Too Many NICs\n");
767 return -1;
768 }
769
770 nd = &nd_table[idx];
771
772 memset(nd, 0, sizeof(*nd));
773
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100774 if ((netdev = qemu_opt_get(opts, "netdev"))) {
775 nd->netdev = qemu_find_netdev(netdev);
776 if (!nd->netdev) {
777 qemu_error("netdev '%s' not found\n", netdev);
778 return -1;
779 }
780 } else {
781 assert(vlan);
782 nd->vlan = vlan;
783 }
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100784 if (name) {
785 nd->name = qemu_strdup(name);
786 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100787 if (qemu_opt_get(opts, "model")) {
788 nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
789 }
790 if (qemu_opt_get(opts, "addr")) {
791 nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
792 }
793
794 nd->macaddr[0] = 0x52;
795 nd->macaddr[1] = 0x54;
796 nd->macaddr[2] = 0x00;
797 nd->macaddr[3] = 0x12;
798 nd->macaddr[4] = 0x34;
799 nd->macaddr[5] = 0x56 + idx;
800
801 if (qemu_opt_get(opts, "macaddr") &&
Mark McLoughlinf1d078c2009-11-25 18:49:27 +0000802 net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100803 qemu_error("invalid syntax for ethernet address\n");
804 return -1;
805 }
806
807 nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED);
808 if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED &&
809 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
810 qemu_error("invalid # of vectors: %d\n", nd->nvectors);
811 return -1;
812 }
813
814 nd->used = 1;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100815 if (vlan) {
816 nd->vlan->nb_guest_devs++;
817 }
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
846static struct {
847 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"
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100979 },
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100980#endif /* _WIN32 */
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100981 { /* end of list */ }
982 },
Mark McLoughlin88ce16c2009-10-06 12:17:09 +0100983 }, {
984 .type = "socket",
985 .init = net_init_socket,
986 .desc = {
987 NET_COMMON_PARAMS_DESC,
988 {
989 .name = "fd",
990 .type = QEMU_OPT_STRING,
991 .help = "file descriptor of an already opened socket",
992 }, {
993 .name = "listen",
994 .type = QEMU_OPT_STRING,
995 .help = "port number, and optional hostname, to listen on",
996 }, {
997 .name = "connect",
998 .type = QEMU_OPT_STRING,
999 .help = "port number, and optional hostname, to connect to",
1000 }, {
1001 .name = "mcast",
1002 .type = QEMU_OPT_STRING,
1003 .help = "UDP multicast address and port number",
1004 },
1005 { /* end of list */ }
1006 },
Mark McLoughlindd510582009-10-06 12:17:10 +01001007#ifdef CONFIG_VDE
1008 }, {
1009 .type = "vde",
1010 .init = net_init_vde,
1011 .desc = {
1012 NET_COMMON_PARAMS_DESC,
1013 {
1014 .name = "sock",
1015 .type = QEMU_OPT_STRING,
1016 .help = "socket path",
1017 }, {
1018 .name = "port",
1019 .type = QEMU_OPT_NUMBER,
1020 .help = "port number",
1021 }, {
1022 .name = "group",
1023 .type = QEMU_OPT_STRING,
1024 .help = "group owner of socket",
1025 }, {
1026 .name = "mode",
1027 .type = QEMU_OPT_NUMBER,
1028 .help = "permissions for socket",
1029 },
1030 { /* end of list */ }
1031 },
1032#endif
Mark McLoughlined2955c2009-10-06 12:17:11 +01001033 }, {
1034 .type = "dump",
1035 .init = net_init_dump,
1036 .desc = {
1037 NET_COMMON_PARAMS_DESC,
1038 {
1039 .name = "len",
1040 .type = QEMU_OPT_SIZE,
1041 .help = "per-packet size limit (64k default)",
1042 }, {
1043 .name = "file",
1044 .type = QEMU_OPT_STRING,
1045 .help = "dump file path (default is qemu-vlan0.pcap)",
1046 },
1047 { /* end of list */ }
1048 },
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001049 },
1050 { /* end of list */ }
1051};
1052
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001053int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001054{
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01001055 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001056 const char *type;
1057 int i;
1058
1059 type = qemu_opt_get(opts, "type");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001060
Mark McLoughlin0f2fbf42009-11-25 18:49:33 +00001061 if (!is_netdev) {
1062 if (!type) {
1063 qemu_error("No type specified for -net\n");
1064 return -1;
1065 }
1066 } else {
1067 if (!type) {
1068 qemu_error("No type specified for -netdev\n");
1069 return -1;
1070 }
1071
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001072 if (strcmp(type, "tap") != 0 &&
1073#ifdef CONFIG_SLIRP
1074 strcmp(type, "user") != 0 &&
1075#endif
1076#ifdef CONFIG_VDE
1077 strcmp(type, "vde") != 0 &&
1078#endif
1079 strcmp(type, "socket") != 0) {
1080 qemu_error("The '%s' network backend type is not valid with -netdev\n",
1081 type);
1082 return -1;
1083 }
1084
1085 if (qemu_opt_get(opts, "vlan")) {
1086 qemu_error("The 'vlan' parameter is not valid with -netdev\n");
1087 return -1;
1088 }
1089 if (qemu_opt_get(opts, "name")) {
1090 qemu_error("The 'name' parameter is not valid with -netdev\n");
1091 return -1;
1092 }
1093 if (!qemu_opts_id(opts)) {
1094 qemu_error("The id= parameter is required with -netdev\n");
1095 return -1;
1096 }
1097 }
1098
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01001099 name = qemu_opts_id(opts);
1100 if (!name) {
1101 name = qemu_opt_get(opts, "name");
1102 }
1103
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001104 for (i = 0; net_client_types[i].type != NULL; i++) {
1105 if (!strcmp(net_client_types[i].type, type)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001106 VLANState *vlan = NULL;
1107
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001108 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1109 return -1;
1110 }
1111
Mark McLoughlin5869c4d2009-10-08 19:58:29 +01001112 /* Do not add to a vlan if it's a -netdev or a nic with a
1113 * netdev= parameter. */
1114 if (!(is_netdev ||
1115 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001116 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1117 }
1118
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001119 if (net_client_types[i].init) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001120 return net_client_types[i].init(opts, mon, name, vlan);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001121 } else {
1122 return 0;
1123 }
1124 }
1125 }
1126
1127 qemu_error("Invalid -net type '%s'\n", type);
1128 return -1;
1129}
1130
aliguori8b13c4a2009-02-11 15:20:51 +00001131void net_client_uninit(NICInfo *nd)
1132{
Mark McLoughlind80b9fc2009-10-08 19:58:24 +01001133 if (nd->vlan) {
1134 nd->vlan->nb_guest_devs--;
1135 }
aliguori8b13c4a2009-02-11 15:20:51 +00001136 nb_nics--;
Glauber Costaa9796702009-09-17 16:53:39 -04001137
Mark McLoughlin9203f522009-10-06 12:16:53 +01001138 qemu_free(nd->model);
1139 qemu_free(nd->name);
1140 qemu_free(nd->devaddr);
Glauber Costaa9796702009-09-17 16:53:39 -04001141
Mark McLoughlind2cffe32009-10-06 12:16:54 +01001142 nd->used = 0;
aliguori8b13c4a2009-02-11 15:20:51 +00001143}
1144
aliguori6f338c32009-02-11 15:21:54 +00001145static int net_host_check_device(const char *device)
1146{
1147 int i;
aliguoribb9ea792009-04-21 19:56:28 +00001148 const char *valid_param_list[] = { "tap", "socket", "dump"
aliguori6f338c32009-02-11 15:21:54 +00001149#ifdef CONFIG_SLIRP
1150 ,"user"
1151#endif
1152#ifdef CONFIG_VDE
1153 ,"vde"
1154#endif
1155 };
1156 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1157 if (!strncmp(valid_param_list[i], device,
1158 strlen(valid_param_list[i])))
1159 return 1;
1160 }
1161
1162 return 0;
1163}
1164
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001165void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001166{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001167 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001168 const char *opts_str = qdict_get_try_str(qdict, "opts");
1169 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001170
aliguori6f338c32009-02-11 15:21:54 +00001171 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001172 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001173 return;
1174 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001175
1176 opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL);
1177 if (!opts) {
1178 monitor_printf(mon, "parsing network options '%s' failed\n",
1179 opts_str ? opts_str : "");
1180 return;
1181 }
1182
1183 qemu_opt_set(opts, "type", device);
1184
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001185 if (net_client_init(mon, opts, 0) < 0) {
aliguori5c8be672009-04-21 19:56:32 +00001186 monitor_printf(mon, "adding host network device %s failed\n", device);
1187 }
aliguori6f338c32009-02-11 15:21:54 +00001188}
1189
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001190void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001191{
aliguori6f338c32009-02-11 15:21:54 +00001192 VLANClientState *vc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001193 int vlan_id = qdict_get_int(qdict, "vlan_id");
1194 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001195
Jan Kiszka1a609522009-06-24 14:42:31 +02001196 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
aliguori6f338c32009-02-11 15:21:54 +00001197 if (!vc) {
aliguori6f338c32009-02-11 15:21:54 +00001198 return;
1199 }
aliguorie8f1f9d2009-04-21 19:56:08 +00001200 if (!net_host_check_device(vc->model)) {
1201 monitor_printf(mon, "invalid host network device %s\n", device);
1202 return;
1203 }
aliguori6f338c32009-02-11 15:21:54 +00001204 qemu_del_vlan_client(vc);
1205}
1206
Glauber Costa406c8df2009-06-17 09:05:30 -04001207void net_set_boot_mask(int net_boot_mask)
1208{
1209 int i;
1210
1211 /* Only the first four NICs may be bootable */
1212 net_boot_mask = net_boot_mask & 0xF;
1213
1214 for (i = 0; i < nb_nics; i++) {
1215 if (net_boot_mask & (1 << i)) {
1216 nd_table[i].bootable = 1;
1217 net_boot_mask &= ~(1 << i);
1218 }
1219 }
1220
1221 if (net_boot_mask) {
1222 fprintf(stderr, "Cannot boot from non-existent NIC\n");
1223 exit(1);
1224 }
1225}
1226
aliguori376253e2009-03-05 23:01:23 +00001227void do_info_network(Monitor *mon)
aliguori63a01ef2008-10-31 19:10:00 +00001228{
1229 VLANState *vlan;
aliguori63a01ef2008-10-31 19:10:00 +00001230
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001231 QTAILQ_FOREACH(vlan, &vlans, next) {
1232 VLANClientState *vc;
1233
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001235
1236 QTAILQ_FOREACH(vc, &vlan->clients, next) {
aliguori376253e2009-03-05 23:01:23 +00001237 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001238 }
aliguori63a01ef2008-10-31 19:10:00 +00001239 }
1240}
1241
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001242void do_set_link(Monitor *mon, const QDict *qdict)
aliguori436e5e52009-01-08 19:44:06 +00001243{
1244 VLANState *vlan;
1245 VLANClientState *vc = NULL;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001246 const char *name = qdict_get_str(qdict, "name");
1247 const char *up_or_down = qdict_get_str(qdict, "up_or_down");
aliguori436e5e52009-01-08 19:44:06 +00001248
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001249 QTAILQ_FOREACH(vlan, &vlans, next) {
1250 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1251 if (strcmp(vc->name, name) == 0) {
edgar_igldd5de372009-01-09 00:48:28 +00001252 goto done;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001253 }
1254 }
1255 }
edgar_igldd5de372009-01-09 00:48:28 +00001256done:
aliguori436e5e52009-01-08 19:44:06 +00001257
1258 if (!vc) {
Stefan Weil7dc3fa02009-08-08 23:33:26 +02001259 monitor_printf(mon, "could not find network device '%s'\n", name);
Luiz Capitulinoc3cf0d32009-08-03 13:56:59 -03001260 return;
aliguori436e5e52009-01-08 19:44:06 +00001261 }
1262
1263 if (strcmp(up_or_down, "up") == 0)
1264 vc->link_down = 0;
1265 else if (strcmp(up_or_down, "down") == 0)
1266 vc->link_down = 1;
1267 else
aliguori376253e2009-03-05 23:01:23 +00001268 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
1269 "valid\n", up_or_down);
aliguori436e5e52009-01-08 19:44:06 +00001270
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001271 if (vc->info->link_status_changed) {
1272 vc->info->link_status_changed(vc);
1273 }
aliguori436e5e52009-01-08 19:44:06 +00001274}
1275
aliguori63a01ef2008-10-31 19:10:00 +00001276void net_cleanup(void)
1277{
1278 VLANState *vlan;
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001279 VLANClientState *vc, *next_vc;
aliguori63a01ef2008-10-31 19:10:00 +00001280
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001281 QTAILQ_FOREACH(vlan, &vlans, next) {
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001282 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
aliguorib946a152009-04-17 17:11:08 +00001283 qemu_del_vlan_client(vc);
aliguori63a01ef2008-10-31 19:10:00 +00001284 }
1285 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001286
1287 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1288 qemu_del_vlan_client(vc);
1289 }
aliguori63a01ef2008-10-31 19:10:00 +00001290}
1291
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001292static void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001293{
1294 VLANState *vlan;
1295
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001296 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori63a01ef2008-10-31 19:10:00 +00001297 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1298 continue;
1299 if (vlan->nb_guest_devs == 0)
1300 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1301 if (vlan->nb_host_devs == 0)
1302 fprintf(stderr,
1303 "Warning: vlan %d is not connected to host network\n",
1304 vlan->id);
1305 }
1306}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001307
1308static int net_init_client(QemuOpts *opts, void *dummy)
1309{
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001310 if (net_client_init(NULL, opts, 0) < 0)
1311 return -1;
1312 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001313}
1314
1315static int net_init_netdev(QemuOpts *opts, void *dummy)
1316{
1317 return net_client_init(NULL, opts, 1);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001318}
1319
1320int net_init_clients(void)
1321{
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001322 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001323 /* if no clients, we use a default config */
1324 qemu_opts_set(&qemu_net_opts, NULL, "type", "nic");
1325#ifdef CONFIG_SLIRP
1326 qemu_opts_set(&qemu_net_opts, NULL, "type", "user");
1327#endif
1328 }
1329
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001330 QTAILQ_INIT(&vlans);
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001331 QTAILQ_INIT(&non_vlan_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001332
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001333 if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1)
1334 return -1;
1335
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001336 if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) {
1337 return -1;
1338 }
1339
1340 net_check_clients();
1341
1342 return 0;
1343}
1344
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001345int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001346{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001347#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001348 int ret;
1349 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001350 return ret;
1351 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001352#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001353
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001354 if (!qemu_opts_parse(opts_list, optarg, "type")) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001355 return -1;
1356 }
1357
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001358 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001359 return 0;
1360}