blob: 32ca50eaf008a34b4b5d77a0b9c81a83d027dbb8 [file] [log] [blame]
aliguori63a01ef2008-10-31 19:10:00 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Mark McLoughlin1df49e02009-11-25 18:48:58 +000024#include "net.h"
aliguori63a01ef2008-10-31 19:10:00 +000025
blueswir1d40cdb12009-03-07 16:52:02 +000026#include "config-host.h"
27
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010028#include "net/tap.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000029#include "net/socket.h"
Mark McLoughlin1abecf72009-11-25 18:48:57 +000030#include "net/dump.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000031#include "net/slirp.h"
Mark McLoughlin5c361cc2009-11-25 18:48:55 +000032#include "net/vde.h"
Mark McLoughlinf1d078c2009-11-25 18:49:27 +000033#include "net/util.h"
blueswir1511d2b12009-03-07 15:32:56 +000034#include "monitor.h"
Mark McLoughlin1df49e02009-11-25 18:48:58 +000035#include "qemu-common.h"
blueswir1511d2b12009-03-07 15:32:56 +000036#include "qemu_socket.h"
Luiz Capitulino4b371562011-11-23 13:11:55 -020037#include "qmp-commands.h"
Amit Shah75422b02010-02-25 17:24:43 +053038#include "hw/qdev.h"
Benjamin Poirierce053662011-02-23 19:57:21 -050039#include "iov.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020040#include "qapi-visit.h"
41#include "qapi/opts-visitor.h"
42#include "qapi/qapi-dealloc-visitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000043
Stefan Weil2944e4e2012-02-04 09:24:46 +010044/* Net bridge is currently not supported for W32. */
45#if !defined(_WIN32)
46# define CONFIG_NET_BRIDGE
47#endif
48
Mark McLoughlin5610c3a2009-10-08 19:58:23 +010049static QTAILQ_HEAD(, VLANState) vlans;
Mark McLoughlin577c4af2009-10-08 19:58:28 +010050static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
aliguori63a01ef2008-10-31 19:10:00 +000051
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010052int default_net = 1;
53
aliguori63a01ef2008-10-31 19:10:00 +000054/***********************************************************/
55/* network device redirectors */
56
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000057#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000058static void hex_dump(FILE *f, const uint8_t *buf, int size)
59{
60 int len, i, j, c;
61
62 for(i=0;i<size;i+=16) {
63 len = size - i;
64 if (len > 16)
65 len = 16;
66 fprintf(f, "%08x ", i);
67 for(j=0;j<16;j++) {
68 if (j < len)
69 fprintf(f, " %02x", buf[i+j]);
70 else
71 fprintf(f, " ");
72 }
73 fprintf(f, " ");
74 for(j=0;j<len;j++) {
75 c = buf[i+j];
76 if (c < ' ' || c > '~')
77 c = '.';
78 fprintf(f, "%c", c);
79 }
80 fprintf(f, "\n");
81 }
82}
83#endif
84
aliguori63a01ef2008-10-31 19:10:00 +000085static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
86{
87 const char *p, *p1;
88 int len;
89 p = *pp;
90 p1 = strchr(p, sep);
91 if (!p1)
92 return -1;
93 len = p1 - p;
94 p1++;
95 if (buf_size > 0) {
96 if (len > buf_size - 1)
97 len = buf_size - 1;
98 memcpy(buf, p, len);
99 buf[len] = '\0';
100 }
101 *pp = p1;
102 return 0;
103}
104
aliguori63a01ef2008-10-31 19:10:00 +0000105int parse_host_port(struct sockaddr_in *saddr, const char *str)
106{
107 char buf[512];
108 struct hostent *he;
109 const char *p, *r;
110 int port;
111
112 p = str;
113 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
114 return -1;
115 saddr->sin_family = AF_INET;
116 if (buf[0] == '\0') {
117 saddr->sin_addr.s_addr = 0;
118 } else {
blueswir1cd390082008-11-16 13:53:32 +0000119 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000120 if (!inet_aton(buf, &saddr->sin_addr))
121 return -1;
122 } else {
123 if ((he = gethostbyname(buf)) == NULL)
124 return - 1;
125 saddr->sin_addr = *(struct in_addr *)he->h_addr;
126 }
127 }
128 port = strtol(p, (char **)&r, 0);
129 if (r == p)
130 return -1;
131 saddr->sin_port = htons(port);
132 return 0;
133}
134
aliguori7cb7434b2009-01-07 17:46:21 +0000135void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
136{
137 snprintf(vc->info_str, sizeof(vc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000138 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
139 vc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000140 macaddr[0], macaddr[1], macaddr[2],
141 macaddr[3], macaddr[4], macaddr[5]);
142}
143
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200144void qemu_macaddr_default_if_unset(MACAddr *macaddr)
145{
146 static int index = 0;
147 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
148
149 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
150 return;
151 macaddr->a[0] = 0x52;
152 macaddr->a[1] = 0x54;
153 macaddr->a[2] = 0x00;
154 macaddr->a[3] = 0x12;
155 macaddr->a[4] = 0x34;
156 macaddr->a[5] = 0x56 + index++;
157}
158
aliguori676cff22009-01-07 17:43:44 +0000159static char *assign_name(VLANClientState *vc1, const char *model)
160{
161 VLANState *vlan;
Markus Armbruster53e51d82011-06-16 18:45:36 +0200162 VLANClientState *vc;
aliguori676cff22009-01-07 17:43:44 +0000163 char buf[256];
164 int id = 0;
165
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100166 QTAILQ_FOREACH(vlan, &vlans, next) {
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100167 QTAILQ_FOREACH(vc, &vlan->clients, next) {
168 if (vc != vc1 && strcmp(vc->model, model) == 0) {
aliguori676cff22009-01-07 17:43:44 +0000169 id++;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100170 }
171 }
aliguori676cff22009-01-07 17:43:44 +0000172 }
173
Markus Armbruster53e51d82011-06-16 18:45:36 +0200174 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
175 if (vc != vc1 && strcmp(vc->model, model) == 0) {
176 id++;
177 }
178 }
179
aliguori676cff22009-01-07 17:43:44 +0000180 snprintf(buf, sizeof(buf), "%s.%d", model, id);
181
Anthony Liguori7267c092011-08-20 22:09:37 -0500182 return g_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000183}
184
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100185static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100186 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100187 const uint8_t *data,
188 size_t size,
189 void *opaque);
190static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100191 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100192 const struct iovec *iov,
193 int iovcnt,
194 void *opaque);
195
Mark McLoughlin45460d12009-11-25 18:49:02 +0000196VLANClientState *qemu_new_net_client(NetClientInfo *info,
197 VLANState *vlan,
198 VLANClientState *peer,
199 const char *model,
200 const char *name)
aliguori63a01ef2008-10-31 19:10:00 +0000201{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100202 VLANClientState *vc;
203
Mark McLoughlin45460d12009-11-25 18:49:02 +0000204 assert(info->size >= sizeof(VLANClientState));
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100205
Anthony Liguori7267c092011-08-20 22:09:37 -0500206 vc = g_malloc0(info->size);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000207
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000208 vc->info = info;
Anthony Liguori7267c092011-08-20 22:09:37 -0500209 vc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000210 if (name) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500211 vc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000212 } else {
aliguori7a9f6e42009-01-07 17:48:51 +0000213 vc->name = assign_name(vc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000214 }
aliguori63a01ef2008-10-31 19:10:00 +0000215
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100216 if (vlan) {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100217 assert(!peer);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100218 vc->vlan = vlan;
219 QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100220 } else {
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100221 if (peer) {
Markus Armbruster27f3f8a2010-02-26 15:50:51 +0100222 assert(!peer->peer);
Mark McLoughlin283c7c62009-10-08 19:58:30 +0100223 vc->peer = peer;
224 peer->peer = vc;
225 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100226 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100227
228 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
229 qemu_deliver_packet_iov,
230 vc);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100231 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100232
aliguori63a01ef2008-10-31 19:10:00 +0000233 return vc;
234}
235
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000236NICState *qemu_new_nic(NetClientInfo *info,
237 NICConf *conf,
238 const char *model,
239 const char *name,
240 void *opaque)
241{
242 VLANClientState *nc;
243 NICState *nic;
244
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200245 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000246 assert(info->size >= sizeof(NICState));
247
248 nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name);
249
250 nic = DO_UPCAST(NICState, nc, nc);
251 nic->conf = conf;
252 nic->opaque = opaque;
253
254 return nic;
255}
256
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200257static void qemu_cleanup_vlan_client(VLANClientState *vc)
aliguori63a01ef2008-10-31 19:10:00 +0000258{
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100259 if (vc->vlan) {
260 QTAILQ_REMOVE(&vc->vlan->clients, vc, next);
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100261 } else {
262 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100263 }
aliguori63a01ef2008-10-31 19:10:00 +0000264
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000265 if (vc->info->cleanup) {
266 vc->info->cleanup(vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100267 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200268}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100269
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200270static void qemu_free_vlan_client(VLANClientState *vc)
271{
272 if (!vc->vlan) {
273 if (vc->send_queue) {
274 qemu_del_net_queue(vc->send_queue);
275 }
276 if (vc->peer) {
277 vc->peer->peer = NULL;
278 }
279 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500280 g_free(vc->name);
281 g_free(vc->model);
282 g_free(vc);
aliguori63a01ef2008-10-31 19:10:00 +0000283}
284
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200285void qemu_del_vlan_client(VLANClientState *vc)
286{
287 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200288 if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200289 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
290 if (nic->peer_deleted) {
291 return;
292 }
293 nic->peer_deleted = true;
294 /* Let NIC know peer is gone. */
295 vc->peer->link_down = true;
296 if (vc->peer->info->link_status_changed) {
297 vc->peer->info->link_status_changed(vc->peer);
298 }
299 qemu_cleanup_vlan_client(vc);
300 return;
301 }
302
303 /* If this is a peer NIC and peer has already been deleted, free it now. */
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200304 if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200305 NICState *nic = DO_UPCAST(NICState, nc, vc);
306 if (nic->peer_deleted) {
307 qemu_free_vlan_client(vc->peer);
308 }
309 }
310
311 qemu_cleanup_vlan_client(vc);
312 qemu_free_vlan_client(vc);
313}
314
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000315VLANClientState *
Jan Kiszka1a609522009-06-24 14:42:31 +0200316qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
317 const char *client_str)
318{
319 VLANState *vlan;
320 VLANClientState *vc;
321
322 vlan = qemu_find_vlan(vlan_id, 0);
323 if (!vlan) {
324 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
325 return NULL;
326 }
327
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100328 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka1a609522009-06-24 14:42:31 +0200329 if (!strcmp(vc->name, client_str)) {
330 break;
331 }
332 }
333 if (!vc) {
334 monitor_printf(mon, "can't find device %s on VLAN %d\n",
335 client_str, vlan_id);
336 }
337
338 return vc;
339}
340
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000341void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
342{
343 VLANClientState *nc;
344 VLANState *vlan;
345
346 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200347 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000348 func(DO_UPCAST(NICState, nc, nc), opaque);
349 }
350 }
351
352 QTAILQ_FOREACH(vlan, &vlans, next) {
353 QTAILQ_FOREACH(nc, &vlan->clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200354 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000355 func(DO_UPCAST(NICState, nc, nc), opaque);
356 }
357 }
358 }
359}
360
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100361int qemu_can_send_packet(VLANClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000362{
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100363 VLANState *vlan = sender->vlan;
aliguori63a01ef2008-10-31 19:10:00 +0000364 VLANClientState *vc;
365
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100366 if (sender->peer) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000367 if (sender->peer->receive_disabled) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100368 return 0;
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000369 } else if (sender->peer->info->can_receive &&
370 !sender->peer->info->can_receive(sender->peer)) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000371 return 0;
372 } else {
373 return 1;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100374 }
375 }
376
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100377 if (!sender->vlan) {
378 return 1;
379 }
380
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100381 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100382 if (vc == sender) {
383 continue;
384 }
385
Mark McLoughlincda90462009-05-18 13:13:16 +0100386 /* no can_receive() handler, they can always receive */
Vincent Palatin60c07d92011-03-02 17:25:02 -0500387 if (vc->info->can_receive && !vc->info->can_receive(vc)) {
388 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000389 }
390 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500391 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000392}
393
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100394static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100395 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100396 const uint8_t *data,
397 size_t size,
398 void *opaque)
399{
400 VLANClientState *vc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000401 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100402
403 if (vc->link_down) {
404 return size;
405 }
406
Mark McLoughlin893379e2009-10-27 18:16:36 +0000407 if (vc->receive_disabled) {
408 return 0;
409 }
410
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000411 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
412 ret = vc->info->receive_raw(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000413 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000414 ret = vc->info->receive(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000415 }
416
417 if (ret == 0) {
418 vc->receive_disabled = 1;
419 };
420
421 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100422}
423
Mark McLoughlinf7105842009-10-08 19:58:31 +0100424static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100425 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100426 const uint8_t *buf,
427 size_t size,
428 void *opaque)
aliguori63a01ef2008-10-31 19:10:00 +0000429{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100430 VLANState *vlan = opaque;
aliguori63a01ef2008-10-31 19:10:00 +0000431 VLANClientState *vc;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000432 ssize_t ret = -1;
aliguori63a01ef2008-10-31 19:10:00 +0000433
Mark McLoughlinf7105842009-10-08 19:58:31 +0100434 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100435 ssize_t len;
436
437 if (vc == sender) {
438 continue;
aliguori764a4d12009-04-21 19:56:41 +0000439 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100440
441 if (vc->link_down) {
442 ret = size;
443 continue;
444 }
445
Mark McLoughlin893379e2009-10-27 18:16:36 +0000446 if (vc->receive_disabled) {
447 ret = 0;
448 continue;
449 }
450
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000451 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
452 len = vc->info->receive_raw(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000453 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000454 len = vc->info->receive(vc, buf, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000455 }
456
457 if (len == 0) {
458 vc->receive_disabled = 1;
459 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100460
461 ret = (ret >= 0) ? ret : len;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000462
aliguori764a4d12009-04-21 19:56:41 +0000463 }
Mark McLoughlin3e021d42009-04-29 11:34:52 +0100464
465 return ret;
aliguori764a4d12009-04-21 19:56:41 +0000466}
467
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100468void qemu_purge_queued_packets(VLANClientState *vc)
469{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100470 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100471
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100472 if (!vc->peer && !vc->vlan) {
473 return;
474 }
475
476 if (vc->peer) {
477 queue = vc->peer->send_queue;
478 } else {
479 queue = vc->vlan->send_queue;
480 }
481
482 qemu_net_queue_purge(queue, vc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100483}
484
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100485void qemu_flush_queued_packets(VLANClientState *vc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100486{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100487 NetQueue *queue;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100488
Mark McLoughlin893379e2009-10-27 18:16:36 +0000489 vc->receive_disabled = 0;
490
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100491 if (vc->vlan) {
492 queue = vc->vlan->send_queue;
493 } else {
494 queue = vc->send_queue;
495 }
496
497 qemu_net_queue_flush(queue);
Mark McLoughline94667b2009-04-29 11:48:12 +0100498}
499
Mark McLoughlinca77d172009-10-22 17:43:41 +0100500static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
501 unsigned flags,
502 const uint8_t *buf, int size,
503 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000504{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100505 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100506
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100507#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100508 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100509 hex_dump(stdout, buf, size);
510#endif
511
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100512 if (sender->link_down || (!sender->peer && !sender->vlan)) {
513 return size;
514 }
515
516 if (sender->peer) {
517 queue = sender->peer->send_queue;
518 } else {
519 queue = sender->vlan->send_queue;
520 }
521
Mark McLoughlinca77d172009-10-22 17:43:41 +0100522 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
523}
524
525ssize_t qemu_send_packet_async(VLANClientState *sender,
526 const uint8_t *buf, int size,
527 NetPacketSent *sent_cb)
528{
529 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
530 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100531}
532
533void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
534{
535 qemu_send_packet_async(vc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000536}
537
Mark McLoughlinca77d172009-10-22 17:43:41 +0100538ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
539{
540 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
541 buf, size, NULL);
542}
543
aliguorifbe78f42008-12-17 19:13:11 +0000544static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
545 int iovcnt)
546{
547 uint8_t buffer[4096];
Benjamin Poirierce053662011-02-23 19:57:21 -0500548 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000549
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400550 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000551
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000552 return vc->info->receive(vc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000553}
554
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100555static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100556 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100557 const struct iovec *iov,
558 int iovcnt,
559 void *opaque)
560{
561 VLANClientState *vc = opaque;
562
563 if (vc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500564 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100565 }
566
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000567 if (vc->info->receive_iov) {
568 return vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100569 } else {
570 return vc_sendv_compat(vc, iov, iovcnt);
571 }
572}
573
Mark McLoughlinf7105842009-10-08 19:58:31 +0100574static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100575 unsigned flags,
Mark McLoughlinf7105842009-10-08 19:58:31 +0100576 const struct iovec *iov,
577 int iovcnt,
578 void *opaque)
Mark McLoughline94667b2009-04-29 11:48:12 +0100579{
Mark McLoughlinf7105842009-10-08 19:58:31 +0100580 VLANState *vlan = opaque;
Mark McLoughline94667b2009-04-29 11:48:12 +0100581 VLANClientState *vc;
Mark McLoughlinf7105842009-10-08 19:58:31 +0100582 ssize_t ret = -1;
Mark McLoughline94667b2009-04-29 11:48:12 +0100583
Mark McLoughlinf7105842009-10-08 19:58:31 +0100584 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Mark McLoughline94667b2009-04-29 11:48:12 +0100585 ssize_t len;
586
587 if (vc == sender) {
588 continue;
589 }
590
591 if (vc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500592 ret = iov_size(iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100593 continue;
594 }
595
Mark McLoughlinca77d172009-10-22 17:43:41 +0100596 assert(!(flags & QEMU_NET_PACKET_FLAG_RAW));
597
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000598 if (vc->info->receive_iov) {
599 len = vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughline94667b2009-04-29 11:48:12 +0100600 } else {
601 len = vc_sendv_compat(vc, iov, iovcnt);
602 }
603
604 ret = (ret >= 0) ? ret : len;
605 }
606
Mark McLoughline94667b2009-04-29 11:48:12 +0100607 return ret;
608}
609
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100610ssize_t qemu_sendv_packet_async(VLANClientState *sender,
611 const struct iovec *iov, int iovcnt,
612 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000613{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100614 NetQueue *queue;
615
616 if (sender->link_down || (!sender->peer && !sender->vlan)) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500617 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000618 }
619
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100620 if (sender->peer) {
621 queue = sender->peer->send_queue;
622 } else {
623 queue = sender->vlan->send_queue;
624 }
625
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100626 return qemu_net_queue_send_iov(queue, sender,
627 QEMU_NET_PACKET_FLAG_NONE,
628 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000629}
630
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100631ssize_t
632qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
633{
634 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
635}
636
aliguori63a01ef2008-10-31 19:10:00 +0000637/* find or alloc a new VLAN */
Jan Kiszka1a609522009-06-24 14:42:31 +0200638VLANState *qemu_find_vlan(int id, int allocate)
aliguori63a01ef2008-10-31 19:10:00 +0000639{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100640 VLANState *vlan;
641
642 QTAILQ_FOREACH(vlan, &vlans, next) {
643 if (vlan->id == id) {
aliguori63a01ef2008-10-31 19:10:00 +0000644 return vlan;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100645 }
aliguori63a01ef2008-10-31 19:10:00 +0000646 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100647
Jan Kiszka1a609522009-06-24 14:42:31 +0200648 if (!allocate) {
649 return NULL;
650 }
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100651
Anthony Liguori7267c092011-08-20 22:09:37 -0500652 vlan = g_malloc0(sizeof(VLANState));
aliguori63a01ef2008-10-31 19:10:00 +0000653 vlan->id = id;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100654 QTAILQ_INIT(&vlan->clients);
Mark McLoughlinf7105842009-10-08 19:58:31 +0100655
656 vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet,
657 qemu_vlan_deliver_packet_iov,
658 vlan);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100659
660 QTAILQ_INSERT_TAIL(&vlans, vlan, next);
661
aliguori63a01ef2008-10-31 19:10:00 +0000662 return vlan;
663}
664
Gerd Hoffmann2ef924b2009-10-21 15:25:24 +0200665VLANClientState *qemu_find_netdev(const char *id)
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100666{
667 VLANClientState *vc;
668
669 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200670 if (vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200671 continue;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100672 if (!strcmp(vc->name, id)) {
673 return vc;
674 }
675 }
676
677 return NULL;
678}
679
aliguori76970792009-02-11 15:20:03 +0000680static int nic_get_free_idx(void)
681{
682 int index;
683
684 for (index = 0; index < MAX_NICS; index++)
685 if (!nd_table[index].used)
686 return index;
687 return -1;
688}
689
Markus Armbruster07caea32009-09-25 03:53:51 +0200690int qemu_show_nic_models(const char *arg, const char *const *models)
691{
692 int i;
693
Peter Maydellc8057f92012-08-02 13:45:54 +0100694 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200695 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100696 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200697
698 fprintf(stderr, "qemu: Supported NIC models: ");
699 for (i = 0 ; models[i]; i++)
700 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
701 return 1;
702}
703
aliguorid07f22c2009-01-13 19:03:57 +0000704void qemu_check_nic_model(NICInfo *nd, const char *model)
705{
706 const char *models[2];
707
708 models[0] = model;
709 models[1] = NULL;
710
Markus Armbruster07caea32009-09-25 03:53:51 +0200711 if (qemu_show_nic_models(nd->model, models))
712 exit(0);
713 if (qemu_find_nic_model(nd, models, model) < 0)
714 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000715}
716
Markus Armbruster07caea32009-09-25 03:53:51 +0200717int qemu_find_nic_model(NICInfo *nd, const char * const *models,
718 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000719{
Markus Armbruster07caea32009-09-25 03:53:51 +0200720 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000721
722 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500723 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000724
Markus Armbruster07caea32009-09-25 03:53:51 +0200725 for (i = 0 ; models[i]; i++) {
726 if (strcmp(nd->model, models[i]) == 0)
727 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000728 }
729
Markus Armbruster6daf1942011-06-22 14:03:54 +0200730 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200731 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000732}
733
Mark McLoughlin5281d752009-10-22 17:49:07 +0100734int net_handle_fd_param(Monitor *mon, const char *param)
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100735{
Jason Wangf7c31d62010-10-25 13:39:59 +0800736 int fd;
737
738 if (!qemu_isdigit(param[0]) && mon) {
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100739
740 fd = monitor_get_fd(mon, param);
741 if (fd == -1) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100742 error_report("No file descriptor named %s found", param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100743 return -1;
744 }
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100745 } else {
Stefan Berger443916d2011-09-28 06:41:32 -0400746 fd = qemu_parse_fd(param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100747 }
Jason Wangf7c31d62010-10-25 13:39:59 +0800748
749 return fd;
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100750}
751
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200752static int net_init_nic(const NetClientOptions *opts, const char *name,
753 VLANState *vlan)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100754{
755 int idx;
756 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200757 const NetLegacyNicOptions *nic;
758
759 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
760 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100761
762 idx = nic_get_free_idx();
763 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100764 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100765 return -1;
766 }
767
768 nd = &nd_table[idx];
769
770 memset(nd, 0, sizeof(*nd));
771
Laszlo Ersek2456f362012-07-17 16:17:14 +0200772 if (nic->has_netdev) {
773 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100774 if (!nd->netdev) {
Laszlo Ersek2456f362012-07-17 16:17:14 +0200775 error_report("netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100776 return -1;
777 }
778 } else {
779 assert(vlan);
780 nd->vlan = vlan;
781 }
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100782 if (name) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500783 nd->name = g_strdup(name);
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100784 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200785 if (nic->has_model) {
786 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100787 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200788 if (nic->has_addr) {
789 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100790 }
791
Laszlo Ersek2456f362012-07-17 16:17:14 +0200792 if (nic->has_macaddr &&
793 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100794 error_report("invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100795 return -1;
796 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200797 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100798
Laszlo Ersek2456f362012-07-17 16:17:14 +0200799 if (nic->has_vectors) {
800 if (nic->vectors > 0x7ffffff) {
801 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
802 return -1;
803 }
804 nd->nvectors = nic->vectors;
805 } else {
806 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100807 }
808
809 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100810 nb_nics++;
811
812 return idx;
813}
814
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100815
Laszlo Ersek6687b792012-07-17 16:17:13 +0200816static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200817 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200818 const char *name,
819 VLANState *vlan) = {
820 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100821#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200822 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100823#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200824 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
825 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100826#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200827 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100828#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200829 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100830#ifdef CONFIG_NET_BRIDGE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200831 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
832#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100833};
834
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100835
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200836static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200837{
838 union {
839 const Netdev *netdev;
840 const NetLegacy *net;
841 } u;
842 const NetClientOptions *opts;
843 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100844
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100845 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200846 u.netdev = object;
847 opts = u.netdev->opts;
848 name = u.netdev->id;
849
850 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100851#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200852 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100853#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200854 case NET_CLIENT_OPTIONS_KIND_TAP:
855 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100856#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200857 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100858#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200859#ifdef CONFIG_NET_BRIDGE
860 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
861#endif
862 break;
863
864 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300865 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
866 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100867 return -1;
868 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200869 } else {
870 u.net = object;
871 opts = u.net->opts;
872 /* missing optional values have been initialized to "all bits zero" */
873 name = u.net->has_id ? u.net->id : u.net->name;
874 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100875
Laszlo Ersek6687b792012-07-17 16:17:13 +0200876 if (net_client_init_fun[opts->kind]) {
877 VLANState *vlan = NULL;
878
879 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
880 * parameter. */
881 if (!is_netdev &&
882 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
883 !opts->nic->has_netdev)) {
884 vlan = qemu_find_vlan(u.net->has_vlan ? u.net->vlan : 0, true);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100885 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200886
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200887 if (net_client_init_fun[opts->kind](opts, name, vlan) < 0) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200888 /* TODO push error reporting into init() methods */
889 error_set(errp, QERR_DEVICE_INIT_FAILED,
890 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100891 return -1;
892 }
893 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200894 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100895}
896
Laszlo Ersek6687b792012-07-17 16:17:13 +0200897
898static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
899{
900 if (is_netdev) {
901 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
902 } else {
903 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
904 }
905}
906
907
908int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
909{
910 void *object = NULL;
911 Error *err = NULL;
912 int ret = -1;
913
914 {
915 OptsVisitor *ov = opts_visitor_new(opts);
916
917 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
918 opts_visitor_cleanup(ov);
919 }
920
921 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200922 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +0200923 }
924
925 if (object) {
926 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
927
928 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
929 qapi_dealloc_visitor_cleanup(dv);
930 }
931
932 error_propagate(errp, err);
933 return ret;
934}
935
936
aliguori6f338c32009-02-11 15:21:54 +0000937static int net_host_check_device(const char *device)
938{
939 int i;
aliguoribb9ea792009-04-21 19:56:28 +0000940 const char *valid_param_list[] = { "tap", "socket", "dump"
Stefan Weil2944e4e2012-02-04 09:24:46 +0100941#ifdef CONFIG_NET_BRIDGE
942 , "bridge"
943#endif
aliguori6f338c32009-02-11 15:21:54 +0000944#ifdef CONFIG_SLIRP
945 ,"user"
946#endif
947#ifdef CONFIG_VDE
948 ,"vde"
949#endif
950 };
951 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
952 if (!strncmp(valid_param_list[i], device,
953 strlen(valid_param_list[i])))
954 return 1;
955 }
956
957 return 0;
958}
959
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300960void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000961{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300962 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100963 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300964 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100965 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300966
aliguori6f338c32009-02-11 15:21:54 +0000967 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +0000968 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +0000969 return;
970 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100971
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200972 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100973 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100974 return;
975 }
976
977 qemu_opt_set(opts, "type", device);
978
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300979 net_client_init(opts, 0, &local_err);
980 if (error_is_set(&local_err)) {
981 qerror_report_err(local_err);
982 error_free(local_err);
aliguori5c8be672009-04-21 19:56:32 +0000983 monitor_printf(mon, "adding host network device %s failed\n", device);
984 }
aliguori6f338c32009-02-11 15:21:54 +0000985}
986
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300987void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000988{
aliguori6f338c32009-02-11 15:21:54 +0000989 VLANClientState *vc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300990 int vlan_id = qdict_get_int(qdict, "vlan_id");
991 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +0000992
Jan Kiszka1a609522009-06-24 14:42:31 +0200993 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
aliguori6f338c32009-02-11 15:21:54 +0000994 if (!vc) {
aliguori6f338c32009-02-11 15:21:54 +0000995 return;
996 }
aliguorie8f1f9d2009-04-21 19:56:08 +0000997 if (!net_host_check_device(vc->model)) {
998 monitor_printf(mon, "invalid host network device %s\n", device);
999 return;
1000 }
aliguori6f338c32009-02-11 15:21:54 +00001001 qemu_del_vlan_client(vc);
1002}
1003
Luiz Capitulino928059a2012-04-18 17:34:15 -03001004void netdev_add(QemuOpts *opts, Error **errp)
1005{
1006 net_client_init(opts, 1, errp);
1007}
1008
1009int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001010{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001011 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001012 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001013 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001014
Luiz Capitulino928059a2012-04-18 17:34:15 -03001015 opts_list = qemu_find_opts_err("netdev", &local_err);
1016 if (error_is_set(&local_err)) {
1017 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001018 }
1019
Luiz Capitulino928059a2012-04-18 17:34:15 -03001020 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
1021 if (error_is_set(&local_err)) {
1022 goto exit_err;
1023 }
1024
1025 netdev_add(opts, &local_err);
1026 if (error_is_set(&local_err)) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001027 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001028 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001029 }
1030
Luiz Capitulino928059a2012-04-18 17:34:15 -03001031 return 0;
1032
1033exit_err:
1034 qerror_report_err(local_err);
1035 error_free(local_err);
1036 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001037}
1038
Luiz Capitulino5f964152012-04-16 14:36:32 -03001039void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001040{
Markus Armbrusterae82d322010-03-25 17:22:40 +01001041 VLANClientState *vc;
1042
1043 vc = qemu_find_netdev(id);
Markus Armbruster85dde9a2011-06-16 18:45:37 +02001044 if (!vc) {
Luiz Capitulino5f964152012-04-16 14:36:32 -03001045 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
1046 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001047 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001048
Markus Armbrusterae82d322010-03-25 17:22:40 +01001049 qemu_del_vlan_client(vc);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001050 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp), id));
Markus Armbrusterae82d322010-03-25 17:22:40 +01001051}
1052
Jan Kiszka44e798d2011-07-20 12:20:21 +02001053static void print_net_client(Monitor *mon, VLANClientState *vc)
1054{
1055 monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
Laszlo Ersek6687b792012-07-17 16:17:13 +02001056 NetClientOptionsKind_lookup[vc->info->type], vc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +02001057}
1058
aliguori376253e2009-03-05 23:01:23 +00001059void do_info_network(Monitor *mon)
aliguori63a01ef2008-10-31 19:10:00 +00001060{
1061 VLANState *vlan;
Jan Kiszka19061e62011-07-20 12:20:19 +02001062 VLANClientState *vc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001063 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001064
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001065 QTAILQ_FOREACH(vlan, &vlans, next) {
aliguori376253e2009-03-05 23:01:23 +00001066 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001067
1068 QTAILQ_FOREACH(vc, &vlan->clients, next) {
Jan Kiszka44e798d2011-07-20 12:20:21 +02001069 monitor_printf(mon, " ");
1070 print_net_client(mon, vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001071 }
aliguori63a01ef2008-10-31 19:10:00 +00001072 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001073 monitor_printf(mon, "Devices not on any VLAN:\n");
1074 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
Jan Kiszka19061e62011-07-20 12:20:19 +02001075 peer = vc->peer;
1076 type = vc->info->type;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001077 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jan Kiszka44e798d2011-07-20 12:20:21 +02001078 monitor_printf(mon, " ");
1079 print_net_client(mon, vc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001080 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001081 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jan Kiszka44e798d2011-07-20 12:20:21 +02001082 monitor_printf(mon, " \\ ");
1083 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001084 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001085 }
aliguori63a01ef2008-10-31 19:10:00 +00001086}
1087
Luiz Capitulino4b371562011-11-23 13:11:55 -02001088void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001089{
1090 VLANState *vlan;
1091 VLANClientState *vc = NULL;
1092
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001093 QTAILQ_FOREACH(vlan, &vlans, next) {
1094 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1095 if (strcmp(vc->name, name) == 0) {
edgar_igldd5de372009-01-09 00:48:28 +00001096 goto done;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001097 }
1098 }
1099 }
Markus Armbruster85dde9a2011-06-16 18:45:37 +02001100 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1101 if (!strcmp(vc->name, name)) {
1102 goto done;
1103 }
1104 }
edgar_igldd5de372009-01-09 00:48:28 +00001105done:
aliguori436e5e52009-01-08 19:44:06 +00001106
1107 if (!vc) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001108 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1109 return;
aliguori436e5e52009-01-08 19:44:06 +00001110 }
1111
Markus Armbrusterc9b26a42010-03-26 09:07:10 +01001112 vc->link_down = !up;
aliguori436e5e52009-01-08 19:44:06 +00001113
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001114 if (vc->info->link_status_changed) {
1115 vc->info->link_status_changed(vc);
1116 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001117
1118 /* Notify peer. Don't update peer link status: this makes it possible to
1119 * disconnect from host network without notifying the guest.
1120 * FIXME: is disconnected link status change operation useful?
1121 *
1122 * Current behaviour is compatible with qemu vlans where there could be
1123 * multiple clients that can still communicate with each other in
1124 * disconnected mode. For now maintain this compatibility. */
1125 if (vc->peer && vc->peer->info->link_status_changed) {
1126 vc->peer->info->link_status_changed(vc->peer);
1127 }
aliguori436e5e52009-01-08 19:44:06 +00001128}
1129
aliguori63a01ef2008-10-31 19:10:00 +00001130void net_cleanup(void)
1131{
1132 VLANState *vlan;
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001133 VLANClientState *vc, *next_vc;
aliguori63a01ef2008-10-31 19:10:00 +00001134
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001135 QTAILQ_FOREACH(vlan, &vlans, next) {
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001136 QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) {
aliguorib946a152009-04-17 17:11:08 +00001137 qemu_del_vlan_client(vc);
aliguori63a01ef2008-10-31 19:10:00 +00001138 }
1139 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001140
1141 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
1142 qemu_del_vlan_client(vc);
1143 }
aliguori63a01ef2008-10-31 19:10:00 +00001144}
1145
Markus Armbruster668680f2010-02-11 14:44:58 +01001146void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001147{
1148 VLANState *vlan;
Markus Armbruster62112d12010-02-11 14:44:59 +01001149 VLANClientState *vc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001150 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001151
Peter Maydell641f6ea2011-05-20 16:50:00 +01001152 /* Don't warn about the default network setup that you get if
1153 * no command line -net or -netdev options are specified. There
1154 * are two cases that we would otherwise complain about:
1155 * (1) board doesn't support a NIC but the implicit "-net nic"
1156 * requested one
1157 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1158 * sets up a nic that isn't connected to anything.
1159 */
1160 if (default_net) {
1161 return;
1162 }
1163
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001164 QTAILQ_FOREACH(vlan, &vlans, next) {
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001165 int has_nic = 0, has_host_dev = 0;
1166
Markus Armbruster62112d12010-02-11 14:44:59 +01001167 QTAILQ_FOREACH(vc, &vlan->clients, next) {
1168 switch (vc->info->type) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001169 case NET_CLIENT_OPTIONS_KIND_NIC:
Markus Armbruster62112d12010-02-11 14:44:59 +01001170 has_nic = 1;
1171 break;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001172 case NET_CLIENT_OPTIONS_KIND_USER:
1173 case NET_CLIENT_OPTIONS_KIND_TAP:
1174 case NET_CLIENT_OPTIONS_KIND_SOCKET:
1175 case NET_CLIENT_OPTIONS_KIND_VDE:
Markus Armbruster62112d12010-02-11 14:44:59 +01001176 has_host_dev = 1;
1177 break;
1178 default: ;
1179 }
1180 }
1181 if (has_host_dev && !has_nic)
aliguori63a01ef2008-10-31 19:10:00 +00001182 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
Markus Armbruster62112d12010-02-11 14:44:59 +01001183 if (has_nic && !has_host_dev)
aliguori63a01ef2008-10-31 19:10:00 +00001184 fprintf(stderr,
1185 "Warning: vlan %d is not connected to host network\n",
1186 vlan->id);
1187 }
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001188 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
1189 if (!vc->peer) {
1190 fprintf(stderr, "Warning: %s %s has no peer\n",
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001191 vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ? "nic" : "netdev",
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001192 vc->name);
1193 }
1194 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001195
1196 /* Check that all NICs requested via -net nic actually got created.
1197 * NICs created via -device don't need to be checked here because
1198 * they are always instantiated.
1199 */
1200 for (i = 0; i < MAX_NICS; i++) {
1201 NICInfo *nd = &nd_table[i];
1202 if (nd->used && !nd->instantiated) {
1203 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1204 "was not created (not supported by this machine?)\n",
1205 nd->name ? nd->name : "anonymous",
1206 nd->model ? nd->model : "unspecified");
1207 }
1208 }
aliguori63a01ef2008-10-31 19:10:00 +00001209}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001210
1211static int net_init_client(QemuOpts *opts, void *dummy)
1212{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001213 Error *local_err = NULL;
1214
1215 net_client_init(opts, 0, &local_err);
1216 if (error_is_set(&local_err)) {
1217 qerror_report_err(local_err);
1218 error_free(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001219 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001220 }
1221
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001222 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001223}
1224
1225static int net_init_netdev(QemuOpts *opts, void *dummy)
1226{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001227 Error *local_err = NULL;
1228 int ret;
1229
1230 ret = net_client_init(opts, 1, &local_err);
1231 if (error_is_set(&local_err)) {
1232 qerror_report_err(local_err);
1233 error_free(local_err);
1234 return -1;
1235 }
1236
1237 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001238}
1239
1240int net_init_clients(void)
1241{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001242 QemuOptsList *net = qemu_find_opts("net");
1243
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001244 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001245 /* if no clients, we use a default config */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001246 qemu_opts_set(net, NULL, "type", "nic");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001247#ifdef CONFIG_SLIRP
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001248 qemu_opts_set(net, NULL, "type", "user");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001249#endif
1250 }
1251
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001252 QTAILQ_INIT(&vlans);
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001253 QTAILQ_INIT(&non_vlan_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001254
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001255 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001256 return -1;
1257
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001258 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001259 return -1;
1260 }
1261
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001262 return 0;
1263}
1264
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001265int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001266{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001267#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001268 int ret;
1269 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001270 return ret;
1271 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001272#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001273
Markus Armbruster8212c642010-02-10 19:52:18 +01001274 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001275 return -1;
1276 }
1277
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001278 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001279 return 0;
1280}
Jason Wang7fc8d912012-03-05 11:08:50 +08001281
1282/* From FreeBSD */
1283/* XXX: optimize */
1284unsigned compute_mcast_idx(const uint8_t *ep)
1285{
1286 uint32_t crc;
1287 int carry, i, j;
1288 uint8_t b;
1289
1290 crc = 0xffffffff;
1291 for (i = 0; i < 6; i++) {
1292 b = *ep++;
1293 for (j = 0; j < 8; j++) {
1294 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1295 crc <<= 1;
1296 b >>= 1;
1297 if (carry) {
1298 crc = ((crc ^ POLYNOMIAL) | carry);
1299 }
1300 }
1301 }
1302 return crc >> 26;
1303}