blob: ee75e0e2ea0d291adf051fa3e3f649053ed213bd [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"
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +010033#include "net/hub.h"
Mark McLoughlinf1d078c2009-11-25 18:49:27 +000034#include "net/util.h"
blueswir1511d2b12009-03-07 15:32:56 +000035#include "monitor.h"
Mark McLoughlin1df49e02009-11-25 18:48:58 +000036#include "qemu-common.h"
blueswir1511d2b12009-03-07 15:32:56 +000037#include "qemu_socket.h"
Luiz Capitulino4b371562011-11-23 13:11:55 -020038#include "qmp-commands.h"
Amit Shah75422b02010-02-25 17:24:43 +053039#include "hw/qdev.h"
Benjamin Poirierce053662011-02-23 19:57:21 -050040#include "iov.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020041#include "qapi-visit.h"
42#include "qapi/opts-visitor.h"
43#include "qapi/qapi-dealloc-visitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000044
Stefan Weil2944e4e2012-02-04 09:24:46 +010045/* Net bridge is currently not supported for W32. */
46#if !defined(_WIN32)
47# define CONFIG_NET_BRIDGE
48#endif
49
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
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100159/**
160 * Generate a name for net client
161 *
162 * Only net clients created with the legacy -net option need this. Naming is
163 * mandatory for net clients created with -netdev.
164 */
aliguori676cff22009-01-07 17:43:44 +0000165static char *assign_name(VLANClientState *vc1, const char *model)
166{
Markus Armbruster53e51d82011-06-16 18:45:36 +0200167 VLANClientState *vc;
aliguori676cff22009-01-07 17:43:44 +0000168 char buf[256];
169 int id = 0;
170
Markus Armbruster53e51d82011-06-16 18:45:36 +0200171 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100172 if (vc == vc1) {
173 continue;
174 }
175 /* For compatibility only bump id for net clients on a vlan */
176 if (strcmp(vc->model, model) == 0 &&
177 net_hub_id_for_client(vc, NULL) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200178 id++;
179 }
180 }
181
aliguori676cff22009-01-07 17:43:44 +0000182 snprintf(buf, sizeof(buf), "%s.%d", model, id);
183
Anthony Liguori7267c092011-08-20 22:09:37 -0500184 return g_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000185}
186
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100187static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100188 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100189 const uint8_t *data,
190 size_t size,
191 void *opaque);
192static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100193 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100194 const struct iovec *iov,
195 int iovcnt,
196 void *opaque);
197
Mark McLoughlin45460d12009-11-25 18:49:02 +0000198VLANClientState *qemu_new_net_client(NetClientInfo *info,
Mark McLoughlin45460d12009-11-25 18:49:02 +0000199 VLANClientState *peer,
200 const char *model,
201 const char *name)
aliguori63a01ef2008-10-31 19:10:00 +0000202{
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100203 VLANClientState *vc;
204
Mark McLoughlin45460d12009-11-25 18:49:02 +0000205 assert(info->size >= sizeof(VLANClientState));
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100206
Anthony Liguori7267c092011-08-20 22:09:37 -0500207 vc = g_malloc0(info->size);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000208
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000209 vc->info = info;
Anthony Liguori7267c092011-08-20 22:09:37 -0500210 vc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000211 if (name) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500212 vc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000213 } else {
aliguori7a9f6e42009-01-07 17:48:51 +0000214 vc->name = assign_name(vc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000215 }
aliguori63a01ef2008-10-31 19:10:00 +0000216
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100217 if (peer) {
218 assert(!peer->peer);
219 vc->peer = peer;
220 peer->peer = vc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100221 }
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100222 QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next);
223
224 vc->send_queue = qemu_new_net_queue(qemu_deliver_packet,
225 qemu_deliver_packet_iov,
226 vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100227
aliguori63a01ef2008-10-31 19:10:00 +0000228 return vc;
229}
230
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000231NICState *qemu_new_nic(NetClientInfo *info,
232 NICConf *conf,
233 const char *model,
234 const char *name,
235 void *opaque)
236{
237 VLANClientState *nc;
238 NICState *nic;
239
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200240 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000241 assert(info->size >= sizeof(NICState));
242
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100243 nc = qemu_new_net_client(info, conf->peer, model, name);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000244
245 nic = DO_UPCAST(NICState, nc, nc);
246 nic->conf = conf;
247 nic->opaque = opaque;
248
249 return nic;
250}
251
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200252static void qemu_cleanup_vlan_client(VLANClientState *vc)
aliguori63a01ef2008-10-31 19:10:00 +0000253{
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100254 QTAILQ_REMOVE(&non_vlan_clients, vc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000255
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000256 if (vc->info->cleanup) {
257 vc->info->cleanup(vc);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100258 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200259}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100260
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200261static void qemu_free_vlan_client(VLANClientState *vc)
262{
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100263 if (vc->send_queue) {
264 qemu_del_net_queue(vc->send_queue);
265 }
266 if (vc->peer) {
267 vc->peer->peer = NULL;
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200268 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500269 g_free(vc->name);
270 g_free(vc->model);
271 g_free(vc);
aliguori63a01ef2008-10-31 19:10:00 +0000272}
273
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200274void qemu_del_vlan_client(VLANClientState *vc)
275{
276 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100277 if (vc->peer && vc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200278 NICState *nic = DO_UPCAST(NICState, nc, vc->peer);
279 if (nic->peer_deleted) {
280 return;
281 }
282 nic->peer_deleted = true;
283 /* Let NIC know peer is gone. */
284 vc->peer->link_down = true;
285 if (vc->peer->info->link_status_changed) {
286 vc->peer->info->link_status_changed(vc->peer);
287 }
288 qemu_cleanup_vlan_client(vc);
289 return;
290 }
291
292 /* If this is a peer NIC and peer has already been deleted, free it now. */
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100293 if (vc->peer && vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200294 NICState *nic = DO_UPCAST(NICState, nc, vc);
295 if (nic->peer_deleted) {
296 qemu_free_vlan_client(vc->peer);
297 }
298 }
299
300 qemu_cleanup_vlan_client(vc);
301 qemu_free_vlan_client(vc);
302}
303
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000304void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
305{
306 VLANClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000307
308 QTAILQ_FOREACH(nc, &non_vlan_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200309 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000310 func(DO_UPCAST(NICState, nc, nc), opaque);
311 }
312 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000313}
314
Mark McLoughlin2e1e0642009-04-29 09:36:43 +0100315int qemu_can_send_packet(VLANClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000316{
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100317 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100318 return 1;
319 }
320
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100321 if (sender->peer->receive_disabled) {
322 return 0;
323 } else if (sender->peer->info->can_receive &&
324 !sender->peer->info->can_receive(sender->peer)) {
325 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000326 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500327 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000328}
329
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100330static ssize_t qemu_deliver_packet(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100331 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100332 const uint8_t *data,
333 size_t size,
334 void *opaque)
335{
336 VLANClientState *vc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000337 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100338
339 if (vc->link_down) {
340 return size;
341 }
342
Mark McLoughlin893379e2009-10-27 18:16:36 +0000343 if (vc->receive_disabled) {
344 return 0;
345 }
346
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000347 if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
348 ret = vc->info->receive_raw(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000349 } else {
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000350 ret = vc->info->receive(vc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000351 }
352
353 if (ret == 0) {
354 vc->receive_disabled = 1;
355 };
356
357 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100358}
359
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100360void qemu_purge_queued_packets(VLANClientState *vc)
361{
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100362 if (!vc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100363 return;
364 }
365
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100366 qemu_net_queue_purge(vc->peer->send_queue, vc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100367}
368
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100369void qemu_flush_queued_packets(VLANClientState *vc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100370{
Mark McLoughlin893379e2009-10-27 18:16:36 +0000371 vc->receive_disabled = 0;
372
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100373 qemu_net_queue_flush(vc->send_queue);
Mark McLoughline94667b2009-04-29 11:48:12 +0100374}
375
Mark McLoughlinca77d172009-10-22 17:43:41 +0100376static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
377 unsigned flags,
378 const uint8_t *buf, int size,
379 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000380{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100381 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100382
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100383#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100384 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100385 hex_dump(stdout, buf, size);
386#endif
387
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100388 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100389 return size;
390 }
391
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100392 queue = sender->peer->send_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100393
Mark McLoughlinca77d172009-10-22 17:43:41 +0100394 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
395}
396
397ssize_t qemu_send_packet_async(VLANClientState *sender,
398 const uint8_t *buf, int size,
399 NetPacketSent *sent_cb)
400{
401 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
402 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100403}
404
405void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
406{
407 qemu_send_packet_async(vc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000408}
409
Mark McLoughlinca77d172009-10-22 17:43:41 +0100410ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size)
411{
412 return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
413 buf, size, NULL);
414}
415
aliguorifbe78f42008-12-17 19:13:11 +0000416static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
417 int iovcnt)
418{
419 uint8_t buffer[4096];
Benjamin Poirierce053662011-02-23 19:57:21 -0500420 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000421
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400422 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000423
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000424 return vc->info->receive(vc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000425}
426
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100427static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100428 unsigned flags,
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100429 const struct iovec *iov,
430 int iovcnt,
431 void *opaque)
432{
433 VLANClientState *vc = opaque;
434
435 if (vc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500436 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100437 }
438
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000439 if (vc->info->receive_iov) {
440 return vc->info->receive_iov(vc, iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100441 } else {
442 return vc_sendv_compat(vc, iov, iovcnt);
443 }
444}
445
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100446ssize_t qemu_sendv_packet_async(VLANClientState *sender,
447 const struct iovec *iov, int iovcnt,
448 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000449{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100450 NetQueue *queue;
451
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100452 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500453 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000454 }
455
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100456 queue = sender->peer->send_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100457
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100458 return qemu_net_queue_send_iov(queue, sender,
459 QEMU_NET_PACKET_FLAG_NONE,
460 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000461}
462
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100463ssize_t
464qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
465{
466 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
467}
468
Gerd Hoffmann2ef924b2009-10-21 15:25:24 +0200469VLANClientState *qemu_find_netdev(const char *id)
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100470{
471 VLANClientState *vc;
472
473 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200474 if (vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200475 continue;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100476 if (!strcmp(vc->name, id)) {
477 return vc;
478 }
479 }
480
481 return NULL;
482}
483
aliguori76970792009-02-11 15:20:03 +0000484static int nic_get_free_idx(void)
485{
486 int index;
487
488 for (index = 0; index < MAX_NICS; index++)
489 if (!nd_table[index].used)
490 return index;
491 return -1;
492}
493
Markus Armbruster07caea32009-09-25 03:53:51 +0200494int qemu_show_nic_models(const char *arg, const char *const *models)
495{
496 int i;
497
498 if (!arg || strcmp(arg, "?"))
499 return 0;
500
501 fprintf(stderr, "qemu: Supported NIC models: ");
502 for (i = 0 ; models[i]; i++)
503 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
504 return 1;
505}
506
aliguorid07f22c2009-01-13 19:03:57 +0000507void qemu_check_nic_model(NICInfo *nd, const char *model)
508{
509 const char *models[2];
510
511 models[0] = model;
512 models[1] = NULL;
513
Markus Armbruster07caea32009-09-25 03:53:51 +0200514 if (qemu_show_nic_models(nd->model, models))
515 exit(0);
516 if (qemu_find_nic_model(nd, models, model) < 0)
517 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000518}
519
Markus Armbruster07caea32009-09-25 03:53:51 +0200520int qemu_find_nic_model(NICInfo *nd, const char * const *models,
521 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000522{
Markus Armbruster07caea32009-09-25 03:53:51 +0200523 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000524
525 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500526 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000527
Markus Armbruster07caea32009-09-25 03:53:51 +0200528 for (i = 0 ; models[i]; i++) {
529 if (strcmp(nd->model, models[i]) == 0)
530 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000531 }
532
Markus Armbruster6daf1942011-06-22 14:03:54 +0200533 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200534 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000535}
536
Mark McLoughlin5281d752009-10-22 17:49:07 +0100537int net_handle_fd_param(Monitor *mon, const char *param)
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100538{
Jason Wangf7c31d62010-10-25 13:39:59 +0800539 int fd;
540
541 if (!qemu_isdigit(param[0]) && mon) {
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100542
543 fd = monitor_get_fd(mon, param);
544 if (fd == -1) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100545 error_report("No file descriptor named %s found", param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100546 return -1;
547 }
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100548 } else {
Stefan Berger443916d2011-09-28 06:41:32 -0400549 fd = qemu_parse_fd(param);
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100550 }
Jason Wangf7c31d62010-10-25 13:39:59 +0800551
552 return fd;
Mark McLoughlinc1d6eed2009-07-22 09:11:42 +0100553}
554
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200555static int net_init_nic(const NetClientOptions *opts, const char *name,
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100556 VLANClientState *peer)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100557{
558 int idx;
559 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200560 const NetLegacyNicOptions *nic;
561
562 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
563 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100564
565 idx = nic_get_free_idx();
566 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100567 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100568 return -1;
569 }
570
571 nd = &nd_table[idx];
572
573 memset(nd, 0, sizeof(*nd));
574
Laszlo Ersek2456f362012-07-17 16:17:14 +0200575 if (nic->has_netdev) {
576 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100577 if (!nd->netdev) {
Laszlo Ersek2456f362012-07-17 16:17:14 +0200578 error_report("netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100579 return -1;
580 }
581 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100582 assert(peer);
583 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100584 }
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100585 if (name) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500586 nd->name = g_strdup(name);
Mark McLoughlin6d952eb2009-10-08 19:58:21 +0100587 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200588 if (nic->has_model) {
589 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100590 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200591 if (nic->has_addr) {
592 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100593 }
594
Laszlo Ersek2456f362012-07-17 16:17:14 +0200595 if (nic->has_macaddr &&
596 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100597 error_report("invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100598 return -1;
599 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200600 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100601
Laszlo Ersek2456f362012-07-17 16:17:14 +0200602 if (nic->has_vectors) {
603 if (nic->vectors > 0x7ffffff) {
604 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
605 return -1;
606 }
607 nd->nvectors = nic->vectors;
608 } else {
609 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100610 }
611
612 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100613 nb_nics++;
614
615 return idx;
616}
617
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100618
Laszlo Ersek6687b792012-07-17 16:17:13 +0200619static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200620 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200621 const char *name,
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100622 VLANClientState *peer) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100623 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100624#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100625 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100626#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100627 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
628 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100629#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100630 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100631#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100632 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100633#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100634 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200635#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100636 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100637};
638
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100639
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200640static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200641{
642 union {
643 const Netdev *netdev;
644 const NetLegacy *net;
645 } u;
646 const NetClientOptions *opts;
647 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100648
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100649 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200650 u.netdev = object;
651 opts = u.netdev->opts;
652 name = u.netdev->id;
653
654 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100655#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200656 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100657#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200658 case NET_CLIENT_OPTIONS_KIND_TAP:
659 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100660#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200661 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100662#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200663#ifdef CONFIG_NET_BRIDGE
664 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
665#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100666 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Laszlo Ersek6687b792012-07-17 16:17:13 +0200667 break;
668
669 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300670 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
671 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100672 return -1;
673 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200674 } else {
675 u.net = object;
676 opts = u.net->opts;
677 /* missing optional values have been initialized to "all bits zero" */
678 name = u.net->has_id ? u.net->id : u.net->name;
679 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100680
Laszlo Ersek6687b792012-07-17 16:17:13 +0200681 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100682 VLANClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200683
684 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
685 * parameter. */
686 if (!is_netdev &&
687 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
688 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100689 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100690 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200691
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100692 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200693 /* TODO push error reporting into init() methods */
694 error_set(errp, QERR_DEVICE_INIT_FAILED,
695 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100696 return -1;
697 }
698 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200699 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100700}
701
Laszlo Ersek6687b792012-07-17 16:17:13 +0200702
703static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
704{
705 if (is_netdev) {
706 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
707 } else {
708 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
709 }
710}
711
712
713int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
714{
715 void *object = NULL;
716 Error *err = NULL;
717 int ret = -1;
718
719 {
720 OptsVisitor *ov = opts_visitor_new(opts);
721
722 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
723 opts_visitor_cleanup(ov);
724 }
725
726 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200727 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +0200728 }
729
730 if (object) {
731 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
732
733 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
734 qapi_dealloc_visitor_cleanup(dv);
735 }
736
737 error_propagate(errp, err);
738 return ret;
739}
740
741
aliguori6f338c32009-02-11 15:21:54 +0000742static int net_host_check_device(const char *device)
743{
744 int i;
aliguoribb9ea792009-04-21 19:56:28 +0000745 const char *valid_param_list[] = { "tap", "socket", "dump"
Stefan Weil2944e4e2012-02-04 09:24:46 +0100746#ifdef CONFIG_NET_BRIDGE
747 , "bridge"
748#endif
aliguori6f338c32009-02-11 15:21:54 +0000749#ifdef CONFIG_SLIRP
750 ,"user"
751#endif
752#ifdef CONFIG_VDE
753 ,"vde"
754#endif
755 };
756 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
757 if (!strncmp(valid_param_list[i], device,
758 strlen(valid_param_list[i])))
759 return 1;
760 }
761
762 return 0;
763}
764
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300765void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000766{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300767 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100768 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300769 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100770 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300771
aliguori6f338c32009-02-11 15:21:54 +0000772 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +0000773 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +0000774 return;
775 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100776
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200777 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100778 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100779 return;
780 }
781
782 qemu_opt_set(opts, "type", device);
783
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300784 net_client_init(opts, 0, &local_err);
785 if (error_is_set(&local_err)) {
786 qerror_report_err(local_err);
787 error_free(local_err);
aliguori5c8be672009-04-21 19:56:32 +0000788 monitor_printf(mon, "adding host network device %s failed\n", device);
789 }
aliguori6f338c32009-02-11 15:21:54 +0000790}
791
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300792void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000793{
aliguori6f338c32009-02-11 15:21:54 +0000794 VLANClientState *vc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300795 int vlan_id = qdict_get_int(qdict, "vlan_id");
796 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +0000797
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100798 vc = net_hub_find_client_by_name(vlan_id, device);
aliguori6f338c32009-02-11 15:21:54 +0000799 if (!vc) {
aliguori6f338c32009-02-11 15:21:54 +0000800 return;
801 }
aliguorie8f1f9d2009-04-21 19:56:08 +0000802 if (!net_host_check_device(vc->model)) {
803 monitor_printf(mon, "invalid host network device %s\n", device);
804 return;
805 }
aliguori6f338c32009-02-11 15:21:54 +0000806 qemu_del_vlan_client(vc);
807}
808
Luiz Capitulino928059a2012-04-18 17:34:15 -0300809void netdev_add(QemuOpts *opts, Error **errp)
810{
811 net_client_init(opts, 1, errp);
812}
813
814int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +0100815{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300816 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -0300817 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100818 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100819
Luiz Capitulino928059a2012-04-18 17:34:15 -0300820 opts_list = qemu_find_opts_err("netdev", &local_err);
821 if (error_is_set(&local_err)) {
822 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100823 }
824
Luiz Capitulino928059a2012-04-18 17:34:15 -0300825 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
826 if (error_is_set(&local_err)) {
827 goto exit_err;
828 }
829
830 netdev_add(opts, &local_err);
831 if (error_is_set(&local_err)) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +0900832 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -0300833 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +0900834 }
835
Luiz Capitulino928059a2012-04-18 17:34:15 -0300836 return 0;
837
838exit_err:
839 qerror_report_err(local_err);
840 error_free(local_err);
841 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100842}
843
Luiz Capitulino5f964152012-04-16 14:36:32 -0300844void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +0100845{
Markus Armbrusterae82d322010-03-25 17:22:40 +0100846 VLANClientState *vc;
847
848 vc = qemu_find_netdev(id);
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200849 if (!vc) {
Luiz Capitulino5f964152012-04-16 14:36:32 -0300850 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
851 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100852 }
Luiz Capitulino5f964152012-04-16 14:36:32 -0300853
Markus Armbrusterae82d322010-03-25 17:22:40 +0100854 qemu_del_vlan_client(vc);
Luiz Capitulino5f964152012-04-16 14:36:32 -0300855 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("netdev", errp), id));
Markus Armbrusterae82d322010-03-25 17:22:40 +0100856}
857
Jan Kiszka44e798d2011-07-20 12:20:21 +0200858static void print_net_client(Monitor *mon, VLANClientState *vc)
859{
860 monitor_printf(mon, "%s: type=%s,%s\n", vc->name,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200861 NetClientOptionsKind_lookup[vc->info->type], vc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +0200862}
863
aliguori376253e2009-03-05 23:01:23 +0000864void do_info_network(Monitor *mon)
aliguori63a01ef2008-10-31 19:10:00 +0000865{
Jan Kiszka19061e62011-07-20 12:20:19 +0200866 VLANClientState *vc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200867 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +0000868
Markus Armbrustera0104e02010-02-11 14:45:01 +0100869 monitor_printf(mon, "Devices not on any VLAN:\n");
870 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
Jan Kiszka19061e62011-07-20 12:20:19 +0200871 peer = vc->peer;
872 type = vc->info->type;
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200873 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jan Kiszka44e798d2011-07-20 12:20:21 +0200874 monitor_printf(mon, " ");
875 print_net_client(mon, vc);
Jan Kiszka19061e62011-07-20 12:20:19 +0200876 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200877 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jan Kiszka44e798d2011-07-20 12:20:21 +0200878 monitor_printf(mon, " \\ ");
879 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +0100880 }
Markus Armbrustera0104e02010-02-11 14:45:01 +0100881 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100882 net_hub_info(mon);
aliguori63a01ef2008-10-31 19:10:00 +0000883}
884
Luiz Capitulino4b371562011-11-23 13:11:55 -0200885void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +0000886{
aliguori436e5e52009-01-08 19:44:06 +0000887 VLANClientState *vc = NULL;
888
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200889 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
890 if (!strcmp(vc->name, name)) {
891 goto done;
892 }
893 }
edgar_igldd5de372009-01-09 00:48:28 +0000894done:
aliguori436e5e52009-01-08 19:44:06 +0000895
896 if (!vc) {
Luiz Capitulino4b371562011-11-23 13:11:55 -0200897 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
898 return;
aliguori436e5e52009-01-08 19:44:06 +0000899 }
900
Markus Armbrusterc9b26a42010-03-26 09:07:10 +0100901 vc->link_down = !up;
aliguori436e5e52009-01-08 19:44:06 +0000902
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000903 if (vc->info->link_status_changed) {
904 vc->info->link_status_changed(vc);
905 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +0200906
907 /* Notify peer. Don't update peer link status: this makes it possible to
908 * disconnect from host network without notifying the guest.
909 * FIXME: is disconnected link status change operation useful?
910 *
911 * Current behaviour is compatible with qemu vlans where there could be
912 * multiple clients that can still communicate with each other in
913 * disconnected mode. For now maintain this compatibility. */
914 if (vc->peer && vc->peer->info->link_status_changed) {
915 vc->peer->info->link_status_changed(vc->peer);
916 }
aliguori436e5e52009-01-08 19:44:06 +0000917}
918
aliguori63a01ef2008-10-31 19:10:00 +0000919void net_cleanup(void)
920{
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100921 VLANClientState *vc, *next_vc;
aliguori63a01ef2008-10-31 19:10:00 +0000922
Mark McLoughlin577c4af2009-10-08 19:58:28 +0100923 QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) {
924 qemu_del_vlan_client(vc);
925 }
aliguori63a01ef2008-10-31 19:10:00 +0000926}
927
Markus Armbruster668680f2010-02-11 14:44:58 +0100928void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +0000929{
Markus Armbruster62112d12010-02-11 14:44:59 +0100930 VLANClientState *vc;
Peter Maydell48e2faf2011-05-20 16:50:01 +0100931 int i;
aliguori63a01ef2008-10-31 19:10:00 +0000932
Peter Maydell641f6ea2011-05-20 16:50:00 +0100933 /* Don't warn about the default network setup that you get if
934 * no command line -net or -netdev options are specified. There
935 * are two cases that we would otherwise complain about:
936 * (1) board doesn't support a NIC but the implicit "-net nic"
937 * requested one
938 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
939 * sets up a nic that isn't connected to anything.
940 */
941 if (default_net) {
942 return;
943 }
944
Stefan Hajnoczi81017642012-07-24 16:35:07 +0100945 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +0100946
Markus Armbrusterefe32fd2010-02-11 14:45:00 +0100947 QTAILQ_FOREACH(vc, &non_vlan_clients, next) {
948 if (!vc->peer) {
949 fprintf(stderr, "Warning: %s %s has no peer\n",
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200950 vc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ? "nic" : "netdev",
Markus Armbrusterefe32fd2010-02-11 14:45:00 +0100951 vc->name);
952 }
953 }
Peter Maydell48e2faf2011-05-20 16:50:01 +0100954
955 /* Check that all NICs requested via -net nic actually got created.
956 * NICs created via -device don't need to be checked here because
957 * they are always instantiated.
958 */
959 for (i = 0; i < MAX_NICS; i++) {
960 NICInfo *nd = &nd_table[i];
961 if (nd->used && !nd->instantiated) {
962 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
963 "was not created (not supported by this machine?)\n",
964 nd->name ? nd->name : "anonymous",
965 nd->model ? nd->model : "unspecified");
966 }
967 }
aliguori63a01ef2008-10-31 19:10:00 +0000968}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +0100969
970static int net_init_client(QemuOpts *opts, void *dummy)
971{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300972 Error *local_err = NULL;
973
974 net_client_init(opts, 0, &local_err);
975 if (error_is_set(&local_err)) {
976 qerror_report_err(local_err);
977 error_free(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +0100978 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300979 }
980
Mark McLoughlinc1671a02009-10-12 09:52:00 +0100981 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100982}
983
984static int net_init_netdev(QemuOpts *opts, void *dummy)
985{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300986 Error *local_err = NULL;
987 int ret;
988
989 ret = net_client_init(opts, 1, &local_err);
990 if (error_is_set(&local_err)) {
991 qerror_report_err(local_err);
992 error_free(local_err);
993 return -1;
994 }
995
996 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +0100997}
998
999int net_init_clients(void)
1000{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001001 QemuOptsList *net = qemu_find_opts("net");
1002
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001003 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001004 /* if no clients, we use a default config */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001005 qemu_opts_set(net, NULL, "type", "nic");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001006#ifdef CONFIG_SLIRP
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001007 qemu_opts_set(net, NULL, "type", "user");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001008#endif
1009 }
1010
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001011 QTAILQ_INIT(&non_vlan_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001012
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001013 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001014 return -1;
1015
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001016 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001017 return -1;
1018 }
1019
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001020 return 0;
1021}
1022
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001023int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001024{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001025#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001026 int ret;
1027 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001028 return ret;
1029 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001030#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001031
Markus Armbruster8212c642010-02-10 19:52:18 +01001032 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001033 return -1;
1034 }
1035
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001036 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001037 return 0;
1038}
Jason Wang7fc8d912012-03-05 11:08:50 +08001039
1040/* From FreeBSD */
1041/* XXX: optimize */
1042unsigned compute_mcast_idx(const uint8_t *ep)
1043{
1044 uint32_t crc;
1045 int carry, i, j;
1046 uint8_t b;
1047
1048 crc = 0xffffffff;
1049 for (i = 0; i < 6; i++) {
1050 b = *ep++;
1051 for (j = 0; j < 8; j++) {
1052 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1053 crc <<= 1;
1054 b >>= 1;
1055 if (carry) {
1056 crc = ((crc ^ POLYNOMIAL) | carry);
1057 }
1058 }
1059 }
1060 return crc >> 26;
1061}