blob: b2dfaa8085c1273c0bff39f62c6edf26c4afaf41 [file] [log] [blame]
aliguori63a01ef2008-10-31 19:10:00 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Mark McLoughlin1df49e02009-11-25 18:48:58 +000024#include "net.h"
aliguori63a01ef2008-10-31 19:10:00 +000025
blueswir1d40cdb12009-03-07 16:52:02 +000026#include "config-host.h"
27
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010028#include "net/tap.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000029#include "net/socket.h"
Mark McLoughlin1abecf72009-11-25 18:48:57 +000030#include "net/dump.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000031#include "net/slirp.h"
Mark McLoughlin5c361cc2009-11-25 18:48:55 +000032#include "net/vde.h"
Mark McLoughlinf1d078c2009-11-25 18:49:27 +000033#include "net/util.h"
blueswir1511d2b12009-03-07 15:32:56 +000034#include "monitor.h"
35#include "sysemu.h"
Mark McLoughlin1df49e02009-11-25 18:48:58 +000036#include "qemu-common.h"
blueswir1511d2b12009-03-07 15:32:56 +000037#include "qemu_socket.h"
Amit Shah75422b02010-02-25 17:24:43 +053038#include "hw/qdev.h"
Benjamin Poirierce053662011-02-23 19:57:21 -050039#include "iov.h"
blueswir1511d2b12009-03-07 15:32:56 +000040
Mark McLoughlin5610c3a2009-10-08 19:58:23 +010041static QTAILQ_HEAD(, VLANState) vlans;
Mark McLoughlin577c4af2009-10-08 19:58:28 +010042static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
aliguori63a01ef2008-10-31 19:10:00 +000043
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010044int default_net = 1;
45
aliguori63a01ef2008-10-31 19:10:00 +000046/***********************************************************/
47/* network device redirectors */
48
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000049#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000050static void hex_dump(FILE *f, const uint8_t *buf, int size)
51{
52 int len, i, j, c;
53
54 for(i=0;i<size;i+=16) {
55 len = size - i;
56 if (len > 16)
57 len = 16;
58 fprintf(f, "%08x ", i);
59 for(j=0;j<16;j++) {
60 if (j < len)
61 fprintf(f, " %02x", buf[i+j]);
62 else
63 fprintf(f, " ");
64 }
65 fprintf(f, " ");
66 for(j=0;j<len;j++) {
67 c = buf[i+j];
68 if (c < ' ' || c > '~')
69 c = '.';
70 fprintf(f, "%c", c);
71 }
72 fprintf(f, "\n");
73 }
74}
75#endif
76
aliguori63a01ef2008-10-31 19:10:00 +000077static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
78{
79 const char *p, *p1;
80 int len;
81 p = *pp;
82 p1 = strchr(p, sep);
83 if (!p1)
84 return -1;
85 len = p1 - p;
86 p1++;
87 if (buf_size > 0) {
88 if (len > buf_size - 1)
89 len = buf_size - 1;
90 memcpy(buf, p, len);
91 buf[len] = '\0';
92 }
93 *pp = p1;
94 return 0;
95}
96
aliguori63a01ef2008-10-31 19:10:00 +000097int parse_host_port(struct sockaddr_in *saddr, const char *str)
98{
99 char buf[512];
100 struct hostent *he;
101 const char *p, *r;
102 int port;
103
104 p = str;
105 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
106 return -1;
107 saddr->sin_family = AF_INET;
108 if (buf[0] == '\0') {
109 saddr->sin_addr.s_addr = 0;
110 } else {
blueswir1cd390082008-11-16 13:53:32 +0000111 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000112 if (!inet_aton(buf, &saddr->sin_addr))
113 return -1;
114 } else {
115 if ((he = gethostbyname(buf)) == NULL)
116 return - 1;
117 saddr->sin_addr = *(struct in_addr *)he->h_addr;
118 }
119 }
120 port = strtol(p, (char **)&r, 0);
121 if (r == p)
122 return -1;
123 saddr->sin_port = htons(port);
124 return 0;
125}
126
aliguori7cb7434b2009-01-07 17:46:21 +0000127void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
128{
129 snprintf(vc->info_str, sizeof(vc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000130 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
131 vc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000132 macaddr[0], macaddr[1], macaddr[2],
133 macaddr[3], macaddr[4], macaddr[5]);
134}
135
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200136void qemu_macaddr_default_if_unset(MACAddr *macaddr)
137{
138 static int index = 0;
139 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
140
141 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
142 return;
143 macaddr->a[0] = 0x52;
144 macaddr->a[1] = 0x54;
145 macaddr->a[2] = 0x00;
146 macaddr->a[3] = 0x12;
147 macaddr->a[4] = 0x34;
148 macaddr->a[5] = 0x56 + index++;
149}
150
aliguori676cff22009-01-07 17:43:44 +0000151static char *assign_name(VLANClientState *vc1, const char *model)
152{
153 VLANState *vlan;
154 char buf[256];
155 int id = 0;
156
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100157 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori676cff22009-01-07 17:43:44 +0000158 VLANClientState *vc;
159
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100160 QTAILQ_FOREACH(vc, &vlan->clients, next) {
161 if (vc != vc1 && strcmp(vc->model, model) == 0) {
aliguori676cff22009-01-07 17:43:44 +0000162 id++;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100163 }
164 }
aliguori676cff22009-01-07 17:43:44 +0000165 }
166
167 snprintf(buf, sizeof(buf), "%s.%d", model, id);
168
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100169 return qemu_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000170}
171
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100172static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100173 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100174 const uint8_t *data,
175 size_t size,
176 void *opaque);
177static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100178 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100179 const struct iovec *iov,
180 int iovcnt,
181 void *opaque);
182
Mark McLoughlin45460d12009-11-25 18:49:02 +0000183VLANClientState *qemu_new_net_client(NetClientInfo *info,
184 VLANState *vlan,
185 VLANClientState *peer,
186 const char *model,
187 const char *name)
aliguori63a01ef2008-10-31 19:10:00 +0000188{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100189 VLANClientState *vc;
190
Mark McLoughlin45460d12009-11-25 18:49:02 +0000191 assert(info->size >= sizeof(VLANClientState));
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100192
Mark McLoughlin45460d12009-11-25 18:49:02 +0000193 vc = qemu_mallocz(info->size);
194
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000195 vc->info = info;
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100196 vc->model = qemu_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000197 if (name) {
Mark McLoughlin02374aa2009-10-06 12:16:55 +0100198 vc->name = qemu_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000199 } else {
aliguori7a9f6e42009-01-07 17:48:51 +0000200 vc->name = assign_name(vc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000201 }
aliguori63a01ef2008-10-31 19:10:00 +0000202
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100203 if (vlan) {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100204 assert(!peer);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100205 vc->vlan = vlan;
206 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100207 } else {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100208 if (peer) {
Markus Armbruster27f3f8a2010-02-26 15:50:51 +0100209 assert(!peer->peer);
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100210 vc->peer = peer;
211 peer->peer = vc;
212 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100213 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100214
215 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
216 qemu_deliver_packet_iov,
217 vc);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100218 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100219
aliguori63a01ef2008-10-31 19:10:00 +0000220 return vc;
221}
222
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000223NICState *qemu_new_nic(NetClientInfo *info,
224 NICConf *conf,
225 const char *model,
226 const char *name,
227 void *opaque)
228{
229 VLANClientState *nc;
230 NICState *nic;
231
232 assert(info->type == NET_CLIENT_TYPE_NIC);
233 assert(info->size >= sizeof(NICState));
234
235 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
236
237 nic = DO_UPCAST(NICState, nc, nc);
238 nic->conf = conf;
239 nic->opaque = opaque;
240
241 return nic;
242}
243
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200244static void qemu_cleanup_vlan_client(VLANClientState *vc)
aliguori63a01ef2008-10-31 19:10:00 +0000245{
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100246 if (vc->vlan) {
247 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100248 } else {
249 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100250 }
aliguori63a01ef2008-10-31 19:10:00 +0000251
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000252 if (vc->info->cleanup) {
253 vc->info->cleanup(vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100254 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200255}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100256
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200257static void qemu_free_vlan_client(VLANClientState *vc)
258{
259 if (!vc->vlan) {
260 if (vc->send_queue) {
261 qemu_del_net_queue(vc->send_queue);
262 }
263 if (vc->peer) {
264 vc->peer->peer = NULL;
265 }
266 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100267 qemu_free(vc->name);
268 qemu_free(vc->model);
269 qemu_free(vc);
aliguori63a01ef2008-10-31 19:10:00 +0000270}
271
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200272void qemu_del_vlan_client(VLANClientState *vc)
273{
274 /* If there is a peer NIC, delete and cleanup client, but do not free. */
275 if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
276 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
277 if (nic->peer_deleted) {
278 return;
279 }
280 nic->peer_deleted = true;
281 /* Let NIC know peer is gone. */
282 vc->peer->link_down = true;
283 if (vc->peer->info->link_status_changed) {
284 vc->peer->info->link_status_changed(vc->peer);
285 }
286 qemu_cleanup_vlan_client(vc);
287 return;
288 }
289
290 /* If this is a peer NIC and peer has already been deleted, free it now. */
291 if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
292 NICState *nic = DO_UPCAST(NICState, nc, vc);
293 if (nic->peer_deleted) {
294 qemu_free_vlan_client(vc->peer);
295 }
296 }
297
298 qemu_cleanup_vlan_client(vc);
299 qemu_free_vlan_client(vc);
300}
301
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000302VLANClientState *
Jan Kiszka1a609522009-06-24 14:42:31 +0200303qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
304 const char *client_str)
305{
306 VLANState *vlan;
307 VLANClientState *vc;
308
309 vlan = qemu_find_vlan(vlan_id, 0);
310 if (!vlan) {
311 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
312 return NULL;
313 }
314
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100315 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka1a609522009-06-24 14:42:31 +0200316 if (!strcmp(vc->name, client_str)) {
317 break;
318 }
319 }
320 if (!vc) {
321 monitor_printf(mon, "can't find device %s on VLAN %d\n",
322 client_str, vlan_id);
323 }
324
325 return vc;
326}
327
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000328void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
329{
330 VLANClientState *nc;
331 VLANState *vlan;
332
333 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
334 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
335 func(DO_UPCAST(NICState, nc, nc), opaque);
336 }
337 }
338
339 QTAILQ_FOREACH(vlan, &vlans, next) {
340 QTAILQ_FOREACH(nc, &vlan->clients, next) {
341 if (nc->info->type == NET_CLIENT_TYPE_NIC) {
342 func(DO_UPCAST(NICState, nc, nc), opaque);
343 }
344 }
345 }
346}
347
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100348int qemu_can_send_packet(VLANClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000349{
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100350 VLANState *vlan = sender->vlan;
aliguori63a01ef2008-10-31 19:10:00 +0000351 VLANClientState *vc;
352
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100353 if (sender->peer) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000354 if (sender->peer->receive_disabled) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100355 return 0;
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000356 } else if (sender->peer->info->can_receive &&
357 !sender->peer->info->can_receive(sender->peer)) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000358 return 0;
359 } else {
360 return 1;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100361 }
362 }
363
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100364 if (!sender->vlan) {
365 return 1;
366 }
367
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100368 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100369 if (vc == sender) {
370 continue;
371 }
372
Mark McLoughlincda90462009-05-18 13:13:16 +0100373 /* no can_receive() handler, they can always receive */
Vincent Palatin60c07d92011-03-02 17:25:02 -0500374 if (vc->info->can_receive && !vc->info->can_receive(vc)) {
375 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000376 }
377 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500378 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000379}
380
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100381static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100382 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100383 const uint8_t *data,
384 size_t size,
385 void *opaque)
386{
387 VLANClientState *vc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000388 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100389
390 if (vc->link_down) {
391 return size;
392 }
393
Mark McLoughlin893379e2009-10-27 18:16:36 +0000394 if (vc->receive_disabled) {
395 return 0;
396 }
397
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000398 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
399 ret = vc->info->receive_raw(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000400 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000401 ret = vc->info->receive(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000402 }
403
404 if (ret == 0) {
405 vc->receive_disabled = 1;
406 };
407
408 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100409}
410
Mark McLoughlinf7105842009-10-08 19:58:31 +0100411static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100412 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100413 const uint8_t *buf,
414 size_t size,
415 void *opaque)
aliguori63a01ef2008-10-31 19:10:00 +0000416{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100417 VLANState *vlan = opaque;
aliguori63a01ef2008-10-31 19:10:00 +0000418 VLANClientState *vc;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000419 ssize_t ret = -1;
aliguori63a01ef2008-10-31 19:10:00 +0000420
Mark McLoughlinf7105842009-10-08 19:58:31 +0100421 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100422 ssize_t len;
423
424 if (vc == sender) {
425 continue;
aliguori764a4d12009-04-21 19:56:41 +0000426 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100427
428 if (vc->link_down) {
429 ret = size;
430 continue;
431 }
432
Mark McLoughlin893379e2009-10-27 18:16:36 +0000433 if (vc->receive_disabled) {
434 ret = 0;
435 continue;
436 }
437
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000438 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
439 len = vc->info->receive_raw(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000440 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000441 len = vc->info->receive(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000442 }
443
444 if (len == 0) {
445 vc->receive_disabled = 1;
446 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100447
448 ret = (ret >= 0) ? ret : len;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000449
aliguori764a4d12009-04-21 19:56:41 +0000450 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100451
452 return ret;
aliguori764a4d12009-04-21 19:56:41 +0000453}
454
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100455void qemu_purge_queued_packets(VLANClientState *vc)
456{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100457 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100458
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100459 if (!vc->peer && !vc->vlan) {
460 return;
461 }
462
463 if (vc->peer) {
464 queue = vc->peer->send_queue;
465 } else {
466 queue = vc->vlan->send_queue;
467 }
468
469 qemu_net_queue_purge(queue, vc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100470}
471
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100472void qemu_flush_queued_packets(VLANClientState *vc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100473{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100474 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100475
Mark McLoughlin893379e2009-10-27 18:16:36 +0000476 vc->receive_disabled = 0;
477
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100478 if (vc->vlan) {
479 queue = vc->vlan->send_queue;
480 } else {
481 queue = vc->send_queue;
482 }
483
484 qemu_net_queue_flush(queue);
Mark McLoughline94667b2009-04-29 11:48:12 +0100485}
486
Mark McLoughlinca77d172009-10-22 17:43:41 +0100487static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
488 unsigned flags,
489 const uint8_t *buf, int size,
490 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000491{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100492 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100493
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100494#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100495 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100496 hex_dump(stdout, buf, size);
497#endif
498
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100499 if (sender->link_down || (!sender->peer && !sender->vlan)) {
500 return size;
501 }
502
503 if (sender->peer) {
504 queue = sender->peer->send_queue;
505 } else {
506 queue = sender->vlan->send_queue;
507 }
508
Mark McLoughlinca77d172009-10-22 17:43:41 +0100509 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
510}
511
512ssize_t qemu_send_packet_async(VLANClientState *sender,
513 const uint8_t *buf, int size,
514 NetPacketSent *sent_cb)
515{
516 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
517 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100518}
519
520void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
521{
522 qemu_send_packet_async(vc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000523}
524
Mark McLoughlinca77d172009-10-22 17:43:41 +0100525ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
526{
527 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
528 buf, size, NULL);
529}
530
aliguorifbe78f42008-12-17 19:13:11 +0000531static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
532 int iovcnt)
533{
534 uint8_t buffer[4096];
Benjamin Poirierce053662011-02-23 19:57:21 -0500535 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000536
Benjamin Poirierce053662011-02-23 19:57:21 -0500537 offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000538
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000539 return vc->info->receive(vc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000540}
541
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100542static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100543 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100544 const struct iovec *iov,
545 int iovcnt,
546 void *opaque)
547{
548 VLANClientState *vc = opaque;
549
550 if (vc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500551 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100552 }
553
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000554 if (vc->info->receive_iov) {
555 return vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100556 } else {
557 return vc_sendv_compat(vc, iov, iovcnt);
558 }
559}
560
Mark McLoughlinf7105842009-10-08 19:58:31 +0100561static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100562 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100563 const struct iovec *iov,
564 int iovcnt,
565 void *opaque)
Mark McLoughline94667b2009-04-29 11:48:12 +0100566{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100567 VLANState *vlan = opaque;
Mark McLoughline94667b2009-04-29 11:48:12 +0100568 VLANClientState *vc;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100569 ssize_t ret = -1;
Mark McLoughline94667b2009-04-29 11:48:12 +0100570
Mark McLoughlinf7105842009-10-08 19:58:31 +0100571 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughline94667b2009-04-29 11:48:12 +0100572 ssize_t len;
573
574 if (vc == sender) {
575 continue;
576 }
577
578 if (vc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500579 ret = iov_size(iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100580 continue;
581 }
582
Mark McLoughlinca77d172009-10-22 17:43:41 +0100583 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
584
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000585 if (vc->info->receive_iov) {
586 len = vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100587 } else {
588 len = vc_sendv_compat(vc, iov, iovcnt);
589 }
590
591 ret = (ret >= 0) ? ret : len;
592 }
593
Mark McLoughline94667b2009-04-29 11:48:12 +0100594 return ret;
595}
596
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100597ssize_t qemu_sendv_packet_async(VLANClientState *sender,
598 const struct iovec *iov, int iovcnt,
599 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000600{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100601 NetQueue *queue;
602
603 if (sender->link_down || (!sender->peer && !sender->vlan)) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500604 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000605 }
606
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100607 if (sender->peer) {
608 queue = sender->peer->send_queue;
609 } else {
610 queue = sender->vlan->send_queue;
611 }
612
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100613 return qemu_net_queue_send_iov(queue, sender,
614 QEMU_NET_PACKET_FLAG_NONE,
615 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000616}
617
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100618ssize_t
619qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
620{
621 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
622}
623
aliguori63a01ef2008-10-31 19:10:00 +0000624/* find or alloc a new VLAN */
Jan Kiszka1a609522009-06-24 14:42:31 +0200625VLANState *qemu_find_vlan(int id, int allocate)
aliguori63a01ef2008-10-31 19:10:00 +0000626{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100627 VLANState *vlan;
628
629 QTAILQ_FOREACH(vlan, &vlans, next) {
630 if (vlan->id == id) {
aliguori63a01ef2008-10-31 19:10:00 +0000631 return vlan;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100632 }
aliguori63a01ef2008-10-31 19:10:00 +0000633 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100634
Jan Kiszka1a609522009-06-24 14:42:31 +0200635 if (!allocate) {
636 return NULL;
637 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100638
aliguori63a01ef2008-10-31 19:10:00 +0000639 vlan = qemu_mallocz(sizeof(VLANState));
aliguori63a01ef2008-10-31 19:10:00 +0000640 vlan->id = id;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100641 QTAILQ_INIT(&vlan->clients);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100642
643 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
644 qemu_vlan_deliver_packet_iov,
645 vlan);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100646
647 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
648
aliguori63a01ef2008-10-31 19:10:00 +0000649 return vlan;
650}
651
Gerd Hoffmann2ef924b2009-10-21 15:25:24 +0200652VLANClientState *qemu_find_netdev(const char *id)
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100653{
654 VLANClientState *vc;
655
656 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
657 if (!strcmp(vc->name, id)) {
658 return vc;
659 }
660 }
661
662 return NULL;
663}
664
aliguori76970792009-02-11 15:20:03 +0000665static int nic_get_free_idx(void)
666{
667 int index;
668
669 for (index = 0; index < MAX_NICS; index++)
670 if (!nd_table[index].used)
671 return index;
672 return -1;
673}
674
Markus Armbruster07caea32009-09-25 03:53:51 +0200675int qemu_show_nic_models(const char *arg, const char *const *models)
676{
677 int i;
678
679 if (!arg || strcmp(arg, "?"))
680 return 0;
681
682 fprintf(stderr, "qemu: Supported NIC models: ");
683 for (i = 0 ; models[i]; i++)
684 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
685 return 1;
686}
687
aliguorid07f22c2009-01-13 19:03:57 +0000688void qemu_check_nic_model(NICInfo *nd, const char *model)
689{
690 const char *models[2];
691
692 models[0] = model;
693 models[1] = NULL;
694
Markus Armbruster07caea32009-09-25 03:53:51 +0200695 if (qemu_show_nic_models(nd->model, models))
696 exit(0);
697 if (qemu_find_nic_model(nd, models, model) < 0)
698 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000699}
700
Markus Armbruster07caea32009-09-25 03:53:51 +0200701int qemu_find_nic_model(NICInfo *nd, const char * const *models,
702 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000703{
Markus Armbruster07caea32009-09-25 03:53:51 +0200704 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000705
706 if (!nd->model)
Mark McLoughlin32a8e142009-10-06 12:16:51 +0100707 nd->model = qemu_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000708
Markus Armbruster07caea32009-09-25 03:53:51 +0200709 for (i = 0 ; models[i]; i++) {
710 if (strcmp(nd->model, models[i]) == 0)
711 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000712 }
713
Markus Armbruster1ecda022010-02-18 17:25:24 +0100714 error_report("qemu: Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200715 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000716}
717
Mark McLoughlin5281d752009-10-22 17:49:07 +0100718int net_handle_fd_param(Monitor *mon, const char *param)
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100719{
Jason Wangf7c31d62010-10-25 13:39:59 +0800720 int fd;
721
722 if (!qemu_isdigit(param[0]) && mon) {
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100723
724 fd = monitor_get_fd(mon, param);
725 if (fd == -1) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100726 error_report("No file descriptor named %s found", param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100727 return -1;
728 }
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100729 } else {
Jason Wangf7c31d62010-10-25 13:39:59 +0800730 char *endptr = NULL;
731
732 fd = strtol(param, &endptr, 10);
733 if (*endptr || (fd == 0 && param == endptr)) {
734 return -1;
735 }
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100736 }
Jason Wangf7c31d62010-10-25 13:39:59 +0800737
738 return fd;
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100739}
740
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100741static int net_init_nic(QemuOpts *opts,
742 Monitor *mon,
743 const char *name,
744 VLANState *vlan)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100745{
746 int idx;
747 NICInfo *nd;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100748 const char *netdev;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100749
750 idx = nic_get_free_idx();
751 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100752 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100753 return -1;
754 }
755
756 nd = &nd_table[idx];
757
758 memset(nd, 0, sizeof(*nd));
759
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100760 if ((netdev = qemu_opt_get(opts, "netdev"))) {
761 nd->netdev = qemu_find_netdev(netdev);
762 if (!nd->netdev) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100763 error_report("netdev '%s' not found", netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100764 return -1;
765 }
766 } else {
767 assert(vlan);
768 nd->vlan = vlan;
769 }
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100770 if (name) {
771 nd->name = qemu_strdup(name);
772 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100773 if (qemu_opt_get(opts, "model")) {
774 nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
775 }
776 if (qemu_opt_get(opts, "addr")) {
777 nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
778 }
779
780 nd->macaddr[0] = 0x52;
781 nd->macaddr[1] = 0x54;
782 nd->macaddr[2] = 0x00;
783 nd->macaddr[3] = 0x12;
784 nd->macaddr[4] = 0x34;
785 nd->macaddr[5] = 0x56 + idx;
786
787 if (qemu_opt_get(opts, "macaddr") &&
Mark McLoughlinf1d078c2009-11-25 18:49:27 +0000788 net_parse_macaddr(nd->macaddr, 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 }
792
Amit Shah75422b02010-02-25 17:24:43 +0530793 nd->nvectors = qemu_opt_get_number(opts, "vectors",
794 DEV_NVECTORS_UNSPECIFIED);
795 if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100796 (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100797 error_report("invalid # of vectors: %d", nd->nvectors);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100798 return -1;
799 }
800
801 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100802 nb_nics++;
803
804 return idx;
805}
806
807#define NET_COMMON_PARAMS_DESC \
808 { \
809 .name = "type", \
810 .type = QEMU_OPT_STRING, \
811 .help = "net client type (nic, tap etc.)", \
812 }, { \
813 .name = "vlan", \
814 .type = QEMU_OPT_NUMBER, \
815 .help = "vlan number", \
816 }, { \
817 .name = "name", \
818 .type = QEMU_OPT_STRING, \
819 .help = "identifier for monitor commands", \
820 }
821
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100822typedef int (*net_client_init_func)(QemuOpts *opts,
823 Monitor *mon,
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100824 const char *name,
825 VLANState *vlan);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100826
827/* magic number, but compiler will warn if too small */
828#define NET_MAX_DESC 20
829
Blue Swirl238431a2010-02-21 16:01:30 +0000830static const struct {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100831 const char *type;
832 net_client_init_func init;
833 QemuOptDesc desc[NET_MAX_DESC];
834} net_client_types[] = {
835 {
836 .type = "none",
837 .desc = {
838 NET_COMMON_PARAMS_DESC,
839 { /* end of list */ }
840 },
841 }, {
842 .type = "nic",
843 .init = net_init_nic,
844 .desc = {
845 NET_COMMON_PARAMS_DESC,
846 {
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100847 .name = "netdev",
848 .type = QEMU_OPT_STRING,
849 .help = "id of -netdev to connect to",
850 },
851 {
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100852 .name = "macaddr",
853 .type = QEMU_OPT_STRING,
854 .help = "MAC address",
855 }, {
856 .name = "model",
857 .type = QEMU_OPT_STRING,
858 .help = "device model (e1000, rtl8139, virtio etc.)",
859 }, {
860 .name = "addr",
861 .type = QEMU_OPT_STRING,
862 .help = "PCI device address",
863 }, {
864 .name = "vectors",
865 .type = QEMU_OPT_NUMBER,
866 .help = "number of MSI-x vectors, 0 to disable MSI-X",
867 },
868 { /* end of list */ }
869 },
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100870#ifdef CONFIG_SLIRP
871 }, {
872 .type = "user",
873 .init = net_init_slirp,
874 .desc = {
875 NET_COMMON_PARAMS_DESC,
876 {
877 .name = "hostname",
878 .type = QEMU_OPT_STRING,
879 .help = "client hostname reported by the builtin DHCP server",
880 }, {
881 .name = "restrict",
882 .type = QEMU_OPT_STRING,
883 .help = "isolate the guest from the host (y|yes|n|no)",
884 }, {
885 .name = "ip",
886 .type = QEMU_OPT_STRING,
887 .help = "legacy parameter, use net= instead",
888 }, {
889 .name = "net",
890 .type = QEMU_OPT_STRING,
891 .help = "IP address and optional netmask",
892 }, {
893 .name = "host",
894 .type = QEMU_OPT_STRING,
895 .help = "guest-visible address of the host",
896 }, {
897 .name = "tftp",
898 .type = QEMU_OPT_STRING,
899 .help = "root directory of the built-in TFTP server",
900 }, {
901 .name = "bootfile",
902 .type = QEMU_OPT_STRING,
903 .help = "BOOTP filename, for use with tftp=",
904 }, {
905 .name = "dhcpstart",
906 .type = QEMU_OPT_STRING,
907 .help = "the first of the 16 IPs the built-in DHCP server can assign",
908 }, {
909 .name = "dns",
910 .type = QEMU_OPT_STRING,
911 .help = "guest-visible address of the virtual nameserver",
912 }, {
913 .name = "smb",
914 .type = QEMU_OPT_STRING,
915 .help = "root directory of the built-in SMB server",
916 }, {
917 .name = "smbserver",
918 .type = QEMU_OPT_STRING,
919 .help = "IP address of the built-in SMB server",
920 }, {
921 .name = "hostfwd",
922 .type = QEMU_OPT_STRING,
923 .help = "guest port number to forward incoming TCP or UDP connections",
924 }, {
925 .name = "guestfwd",
926 .type = QEMU_OPT_STRING,
927 .help = "IP address and port to forward guest TCP connections",
928 },
929 { /* end of list */ }
930 },
931#endif
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100932 }, {
933 .type = "tap",
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100934 .init = net_init_tap,
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100935 .desc = {
936 NET_COMMON_PARAMS_DESC,
937 {
938 .name = "ifname",
939 .type = QEMU_OPT_STRING,
940 .help = "interface name",
941 },
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100942#ifndef _WIN32
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100943 {
944 .name = "fd",
945 .type = QEMU_OPT_STRING,
946 .help = "file descriptor of an already opened tap",
947 }, {
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100948 .name = "script",
949 .type = QEMU_OPT_STRING,
950 .help = "script to initialize the interface",
951 }, {
952 .name = "downscript",
953 .type = QEMU_OPT_STRING,
954 .help = "script to shut down the interface",
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100955 }, {
956 .name = "sndbuf",
957 .type = QEMU_OPT_SIZE,
958 .help = "send buffer limit"
Mark McLoughlinbaf74c92009-10-22 17:43:37 +0100959 }, {
960 .name = "vnet_hdr",
961 .type = QEMU_OPT_BOOL,
962 .help = "enable the IFF_VNET_HDR flag on the tap interface"
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200963 }, {
964 .name = "vhost",
965 .type = QEMU_OPT_BOOL,
966 .help = "enable vhost-net network accelerator",
967 }, {
968 .name = "vhostfd",
969 .type = QEMU_OPT_STRING,
970 .help = "file descriptor of an already opened vhost net device",
Jason Wang96c94b22011-02-25 16:11:27 +0800971 }, {
972 .name = "vhostforce",
973 .type = QEMU_OPT_BOOL,
974 .help = "force vhost on for non-MSIX virtio guests",
975 },
Mark McLoughlina8ed73f2009-10-22 17:49:05 +0100976#endif /* _WIN32 */
Mark McLoughlin8a1c5232009-10-06 12:17:08 +0100977 { /* end of list */ }
978 },
Mark McLoughlin88ce16c2009-10-06 12:17:09 +0100979 }, {
980 .type = "socket",
981 .init = net_init_socket,
982 .desc = {
983 NET_COMMON_PARAMS_DESC,
984 {
985 .name = "fd",
986 .type = QEMU_OPT_STRING,
987 .help = "file descriptor of an already opened socket",
988 }, {
989 .name = "listen",
990 .type = QEMU_OPT_STRING,
991 .help = "port number, and optional hostname, to listen on",
992 }, {
993 .name = "connect",
994 .type = QEMU_OPT_STRING,
995 .help = "port number, and optional hostname, to connect to",
996 }, {
997 .name = "mcast",
998 .type = QEMU_OPT_STRING,
999 .help = "UDP multicast address and port number",
Mike Ryan3a75e742010-12-01 11:16:47 -08001000 }, {
1001 .name = "localaddr",
1002 .type = QEMU_OPT_STRING,
1003 .help = "source address for multicast packets",
Mark McLoughlin88ce16c2009-10-06 12:17:09 +01001004 },
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");
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001060 if (!type) {
1061 qerror_report(QERR_MISSING_PARAMETER, "type");
1062 return -1;
1063 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001064
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001065 if (is_netdev) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001066 if (strcmp(type, "tap") != 0 &&
1067#ifdef CONFIG_SLIRP
1068 strcmp(type, "user") != 0 &&
1069#endif
1070#ifdef CONFIG_VDE
1071 strcmp(type, "vde") != 0 &&
1072#endif
1073 strcmp(type, "socket") != 0) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001074 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1075 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001076 return -1;
1077 }
1078
1079 if (qemu_opt_get(opts, "vlan")) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001080 qerror_report(QERR_INVALID_PARAMETER, "vlan");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001081 return -1;
1082 }
1083 if (qemu_opt_get(opts, "name")) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001084 qerror_report(QERR_INVALID_PARAMETER, "name");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001085 return -1;
1086 }
1087 if (!qemu_opts_id(opts)) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001088 qerror_report(QERR_MISSING_PARAMETER, "id");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001089 return -1;
1090 }
1091 }
1092
Mark McLoughlin6d952eb2009-10-08 19:58:21 +01001093 name = qemu_opts_id(opts);
1094 if (!name) {
1095 name = qemu_opt_get(opts, "name");
1096 }
1097
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001098 for (i = 0; net_client_types[i].type != NULL; i++) {
1099 if (!strcmp(net_client_types[i].type, type)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001100 VLANState *vlan = NULL;
Amit Shah50e32ea2010-06-08 21:13:58 +05301101 int ret;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001102
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001103 if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) {
1104 return -1;
1105 }
1106
Mark McLoughlin5869c4d2009-10-08 19:58:29 +01001107 /* Do not add to a vlan if it's a -netdev or a nic with a
1108 * netdev= parameter. */
1109 if (!(is_netdev ||
1110 (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001111 vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1);
1112 }
1113
Amit Shah03c71552010-06-15 13:30:39 +05301114 ret = 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001115 if (net_client_types[i].init) {
Amit Shah50e32ea2010-06-08 21:13:58 +05301116 ret = net_client_types[i].init(opts, mon, name, vlan);
1117 if (ret < 0) {
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001118 /* TODO push error reporting into init() methods */
1119 qerror_report(QERR_DEVICE_INIT_FAILED, type);
1120 return -1;
1121 }
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001122 }
Amit Shah50e32ea2010-06-08 21:13:58 +05301123 return ret;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001124 }
1125 }
1126
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001127 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
1128 "a network client type");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001129 return -1;
1130}
1131
aliguori6f338c32009-02-11 15:21:54 +00001132static int net_host_check_device(const char *device)
1133{
1134 int i;
aliguoribb9ea792009-04-21 19:56:28 +00001135 const char *valid_param_list[] = { "tap", "socket", "dump"
aliguori6f338c32009-02-11 15:21:54 +00001136#ifdef CONFIG_SLIRP
1137 ,"user"
1138#endif
1139#ifdef CONFIG_VDE
1140 ,"vde"
1141#endif
1142 };
1143 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1144 if (!strncmp(valid_param_list[i], device,
1145 strlen(valid_param_list[i])))
1146 return 1;
1147 }
1148
1149 return 0;
1150}
1151
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001152void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001153{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001154 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001155 const char *opts_str = qdict_get_try_str(qdict, "opts");
1156 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001157
aliguori6f338c32009-02-11 15:21:54 +00001158 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001159 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001160 return;
1161 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001162
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001163 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001164 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001165 return;
1166 }
1167
1168 qemu_opt_set(opts, "type", device);
1169
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001170 if (net_client_init(mon, opts, 0) < 0) {
aliguori5c8be672009-04-21 19:56:32 +00001171 monitor_printf(mon, "adding host network device %s failed\n", device);
1172 }
aliguori6f338c32009-02-11 15:21:54 +00001173}
1174
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001175void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001176{
aliguori6f338c32009-02-11 15:21:54 +00001177 VLANClientState *vc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001178 int vlan_id = qdict_get_int(qdict, "vlan_id");
1179 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001180
Jan Kiszka1a609522009-06-24 14:42:31 +02001181 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
aliguori6f338c32009-02-11 15:21:54 +00001182 if (!vc) {
aliguori6f338c32009-02-11 15:21:54 +00001183 return;
1184 }
aliguorie8f1f9d2009-04-21 19:56:08 +00001185 if (!net_host_check_device(vc->model)) {
1186 monitor_printf(mon, "invalid host network device %s\n", device);
1187 return;
1188 }
aliguori6f338c32009-02-11 15:21:54 +00001189 qemu_del_vlan_client(vc);
1190}
1191
Markus Armbrusterae82d322010-03-25 17:22:40 +01001192int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
1193{
1194 QemuOpts *opts;
1195 int res;
1196
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001197 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001198 if (!opts) {
1199 return -1;
1200 }
1201
1202 res = net_client_init(mon, opts, 1);
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001203 if (res < 0) {
1204 qemu_opts_del(opts);
1205 }
1206
Markus Armbrusterae82d322010-03-25 17:22:40 +01001207 return res;
1208}
1209
Markus Armbrusterae82d322010-03-25 17:22:40 +01001210int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data)
1211{
1212 const char *id = qdict_get_str(qdict, "id");
1213 VLANClientState *vc;
1214
1215 vc = qemu_find_netdev(id);
1216 if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) {
1217 qerror_report(QERR_DEVICE_NOT_FOUND, id);
1218 return -1;
1219 }
Markus Armbrusterae82d322010-03-25 17:22:40 +01001220 qemu_del_vlan_client(vc);
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001221 qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
Markus Armbrusterae82d322010-03-25 17:22:40 +01001222 return 0;
1223}
1224
aliguori376253e2009-03-05 23:01:23 +00001225void do_info_network(Monitor *mon)
aliguori63a01ef2008-10-31 19:10:00 +00001226{
1227 VLANState *vlan;
Markus Armbrustera0104e02010-02-11 14:45:01 +01001228 VLANClientState *vc;
aliguori63a01ef2008-10-31 19:10:00 +00001229
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001230 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori376253e2009-03-05 23:01:23 +00001231 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001232
1233 QTAILQ_FOREACH(vc, &vlan->clients, next) {
aliguori376253e2009-03-05 23:01:23 +00001234 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001235 }
aliguori63a01ef2008-10-31 19:10:00 +00001236 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001237 monitor_printf(mon, "Devices not on any VLAN:\n");
1238 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1239 monitor_printf(mon, " %s: %s", vc->name, vc->info_str);
1240 if (vc->peer) {
1241 monitor_printf(mon, " peer=%s", vc->peer->name);
1242 }
1243 monitor_printf(mon, "\n");
1244 }
aliguori63a01ef2008-10-31 19:10:00 +00001245}
1246
Markus Armbruster5369e3c2010-03-26 09:07:11 +01001247int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data)
aliguori436e5e52009-01-08 19:44:06 +00001248{
1249 VLANState *vlan;
1250 VLANClientState *vc = NULL;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001251 const char *name = qdict_get_str(qdict, "name");
Markus Armbrusterc9b26a42010-03-26 09:07:10 +01001252 int up = qdict_get_bool(qdict, "up");
aliguori436e5e52009-01-08 19:44:06 +00001253
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001254 QTAILQ_FOREACH(vlan, &vlans, next) {
1255 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1256 if (strcmp(vc->name, name) == 0) {
edgar_igldd5de372009-01-09 00:48:28 +00001257 goto done;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001258 }
1259 }
1260 }
Markus Armbruster2583ba92010-02-11 14:45:02 +01001261 vc = qemu_find_netdev(name);
edgar_igldd5de372009-01-09 00:48:28 +00001262done:
aliguori436e5e52009-01-08 19:44:06 +00001263
1264 if (!vc) {
Markus Armbruster5369e3c2010-03-26 09:07:11 +01001265 qerror_report(QERR_DEVICE_NOT_FOUND, name);
1266 return -1;
aliguori436e5e52009-01-08 19:44:06 +00001267 }
1268
Markus Armbrusterc9b26a42010-03-26 09:07:10 +01001269 vc->link_down = !up;
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 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001274
1275 /* Notify peer. Don't update peer link status: this makes it possible to
1276 * disconnect from host network without notifying the guest.
1277 * FIXME: is disconnected link status change operation useful?
1278 *
1279 * Current behaviour is compatible with qemu vlans where there could be
1280 * multiple clients that can still communicate with each other in
1281 * disconnected mode. For now maintain this compatibility. */
1282 if (vc->peer && vc->peer->info->link_status_changed) {
1283 vc->peer->info->link_status_changed(vc->peer);
1284 }
Markus Armbruster5369e3c2010-03-26 09:07:11 +01001285 return 0;
aliguori436e5e52009-01-08 19:44:06 +00001286}
1287
aliguori63a01ef2008-10-31 19:10:00 +00001288void net_cleanup(void)
1289{
1290 VLANState *vlan;
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001291 VLANClientState *vc, *next_vc;
aliguori63a01ef2008-10-31 19:10:00 +00001292
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001293 QTAILQ_FOREACH(vlan, &vlans, next) {
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001294 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
aliguorib946a152009-04-17 17:11:08 +00001295 qemu_del_vlan_client(vc);
aliguori63a01ef2008-10-31 19:10:00 +00001296 }
1297 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001298
1299 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1300 qemu_del_vlan_client(vc);
1301 }
aliguori63a01ef2008-10-31 19:10:00 +00001302}
1303
Markus Armbruster668680f2010-02-11 14:44:58 +01001304void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001305{
1306 VLANState *vlan;
Markus Armbruster62112d12010-02-11 14:44:59 +01001307 VLANClientState *vc;
aliguori63a01ef2008-10-31 19:10:00 +00001308
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001309 QTAILQ_FOREACH(vlan, &vlans, next) {
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001310 int has_nic = 0, has_host_dev = 0;
1311
Markus Armbruster62112d12010-02-11 14:44:59 +01001312 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1313 switch (vc->info->type) {
1314 case NET_CLIENT_TYPE_NIC:
1315 has_nic = 1;
1316 break;
1317 case NET_CLIENT_TYPE_SLIRP:
1318 case NET_CLIENT_TYPE_TAP:
1319 case NET_CLIENT_TYPE_SOCKET:
1320 case NET_CLIENT_TYPE_VDE:
1321 has_host_dev = 1;
1322 break;
1323 default: ;
1324 }
1325 }
1326 if (has_host_dev && !has_nic)
aliguori63a01ef2008-10-31 19:10:00 +00001327 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
Markus Armbruster62112d12010-02-11 14:44:59 +01001328 if (has_nic && !has_host_dev)
aliguori63a01ef2008-10-31 19:10:00 +00001329 fprintf(stderr,
1330 "Warning: vlan %d is not connected to host network\n",
1331 vlan->id);
1332 }
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001333 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1334 if (!vc->peer) {
1335 fprintf(stderr, "Warning: %s %s has no peer\n",
1336 vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev",
1337 vc->name);
1338 }
1339 }
aliguori63a01ef2008-10-31 19:10:00 +00001340}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001341
1342static int net_init_client(QemuOpts *opts, void *dummy)
1343{
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001344 if (net_client_init(NULL, opts, 0) < 0)
1345 return -1;
1346 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001347}
1348
1349static int net_init_netdev(QemuOpts *opts, void *dummy)
1350{
1351 return net_client_init(NULL, opts, 1);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001352}
1353
1354int net_init_clients(void)
1355{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001356 QemuOptsList *net = qemu_find_opts("net");
1357
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001358 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001359 /* if no clients, we use a default config */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001360 qemu_opts_set(net, NULL, "type", "nic");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001361#ifdef CONFIG_SLIRP
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001362 qemu_opts_set(net, NULL, "type", "user");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001363#endif
1364 }
1365
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001366 QTAILQ_INIT(&vlans);
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001367 QTAILQ_INIT(&non_vlan_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001368
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001369 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001370 return -1;
1371
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001372 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001373 return -1;
1374 }
1375
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001376 return 0;
1377}
1378
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001379int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001380{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001381#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001382 int ret;
1383 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001384 return ret;
1385 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001386#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001387
Markus Armbruster8212c642010-02-10 19:52:18 +01001388 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001389 return -1;
1390 }
1391
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001392 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001393 return 0;
1394}