blob: 11fb769dff7084177e3db57e8a5fc4d995611a1a [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 */
blueswir1d40cdb12009-03-07 16:52:02 +000024#include "config-host.h"
25
Paolo Bonzini1422e322012-10-24 08:43:34 +020026#include "net/net.h"
Paolo Bonzinifd9400b2012-10-24 11:27:28 +020027#include "clients.h"
28#include "hub.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020029#include "net/slirp.h"
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +040030#include "net/eth.h"
Paolo Bonzinifd9400b2012-10-24 11:27:28 +020031#include "util.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020032
Paolo Bonzini83c90892012-12-17 18:19:49 +010033#include "monitor/monitor.h"
Mark McLoughlin1df49e02009-11-25 18:48:58 +000034#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/sockets.h"
36#include "qemu/config-file.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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010040#include "qemu/main-loop.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020041#include "qapi-visit.h"
42#include "qapi/opts-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010043#include "qapi/dealloc-visitor.h"
zhanghailiange1d64c02014-08-26 16:06:17 +080044#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000045
Stefan Weil2944e4e2012-02-04 09:24:46 +010046/* Net bridge is currently not supported for W32. */
47#if !defined(_WIN32)
48# define CONFIG_NET_BRIDGE
49#endif
50
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030051static VMChangeStateEntry *net_change_state_entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010052static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000053
Hani Benhabiles84007e82014-05-27 23:39:33 +010054const char *host_net_devices[] = {
55 "tap",
56 "socket",
57 "dump",
58#ifdef CONFIG_NET_BRIDGE
59 "bridge",
60#endif
61#ifdef CONFIG_SLIRP
62 "user",
63#endif
64#ifdef CONFIG_VDE
65 "vde",
66#endif
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030067 "vhost-user",
Hani Benhabiles84007e82014-05-27 23:39:33 +010068 NULL,
69};
70
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010071int default_net = 1;
72
aliguori63a01ef2008-10-31 19:10:00 +000073/***********************************************************/
74/* network device redirectors */
75
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000076#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000077static void hex_dump(FILE *f, const uint8_t *buf, int size)
78{
79 int len, i, j, c;
80
81 for(i=0;i<size;i+=16) {
82 len = size - i;
83 if (len > 16)
84 len = 16;
85 fprintf(f, "%08x ", i);
86 for(j=0;j<16;j++) {
87 if (j < len)
88 fprintf(f, " %02x", buf[i+j]);
89 else
90 fprintf(f, " ");
91 }
92 fprintf(f, " ");
93 for(j=0;j<len;j++) {
94 c = buf[i+j];
95 if (c < ' ' || c > '~')
96 c = '.';
97 fprintf(f, "%c", c);
98 }
99 fprintf(f, "\n");
100 }
101}
102#endif
103
aliguori63a01ef2008-10-31 19:10:00 +0000104static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
105{
106 const char *p, *p1;
107 int len;
108 p = *pp;
109 p1 = strchr(p, sep);
110 if (!p1)
111 return -1;
112 len = p1 - p;
113 p1++;
114 if (buf_size > 0) {
115 if (len > buf_size - 1)
116 len = buf_size - 1;
117 memcpy(buf, p, len);
118 buf[len] = '\0';
119 }
120 *pp = p1;
121 return 0;
122}
123
aliguori63a01ef2008-10-31 19:10:00 +0000124int parse_host_port(struct sockaddr_in *saddr, const char *str)
125{
126 char buf[512];
127 struct hostent *he;
128 const char *p, *r;
129 int port;
130
131 p = str;
132 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
133 return -1;
134 saddr->sin_family = AF_INET;
135 if (buf[0] == '\0') {
136 saddr->sin_addr.s_addr = 0;
137 } else {
blueswir1cd390082008-11-16 13:53:32 +0000138 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000139 if (!inet_aton(buf, &saddr->sin_addr))
140 return -1;
141 } else {
142 if ((he = gethostbyname(buf)) == NULL)
143 return - 1;
144 saddr->sin_addr = *(struct in_addr *)he->h_addr;
145 }
146 }
147 port = strtol(p, (char **)&r, 0);
148 if (r == p)
149 return -1;
150 saddr->sin_port = htons(port);
151 return 0;
152}
153
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100154void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000155{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100156 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000157 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100158 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000159 macaddr[0], macaddr[1], macaddr[2],
160 macaddr[3], macaddr[4], macaddr[5]);
161}
162
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200163void qemu_macaddr_default_if_unset(MACAddr *macaddr)
164{
165 static int index = 0;
166 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
167
168 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
169 return;
170 macaddr->a[0] = 0x52;
171 macaddr->a[1] = 0x54;
172 macaddr->a[2] = 0x00;
173 macaddr->a[3] = 0x12;
174 macaddr->a[4] = 0x34;
175 macaddr->a[5] = 0x56 + index++;
176}
177
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100178/**
179 * Generate a name for net client
180 *
Amos Kongc9635302013-04-15 18:55:19 +0800181 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100182 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100183static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000184{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100185 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000186 int id = 0;
187
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100188 QTAILQ_FOREACH(nc, &net_clients, next) {
189 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100190 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100191 }
Amos Kongc9635302013-04-15 18:55:19 +0800192 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200193 id++;
194 }
195 }
196
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100197 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000198}
199
Jason Wangf7860452013-01-30 19:12:27 +0800200static void qemu_net_client_destructor(NetClientState *nc)
201{
202 g_free(nc);
203}
204
Jason Wang18a15412013-01-30 19:12:26 +0800205static void qemu_net_client_setup(NetClientState *nc,
206 NetClientInfo *info,
207 NetClientState *peer,
208 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800209 const char *name,
210 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000211{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100212 nc->info = info;
213 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000214 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100215 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000216 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100217 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000218 }
aliguori63a01ef2008-10-31 19:10:00 +0000219
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100220 if (peer) {
221 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100222 nc->peer = peer;
223 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100224 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100225 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100226
Jan Kiszka067404b2013-08-02 21:47:08 +0200227 nc->incoming_queue = qemu_new_net_queue(nc);
Jason Wangf7860452013-01-30 19:12:27 +0800228 nc->destructor = destructor;
Jason Wang18a15412013-01-30 19:12:26 +0800229}
230
231NetClientState *qemu_new_net_client(NetClientInfo *info,
232 NetClientState *peer,
233 const char *model,
234 const char *name)
235{
236 NetClientState *nc;
237
238 assert(info->size >= sizeof(NetClientState));
239
240 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800241 qemu_net_client_setup(nc, info, peer, model, name,
242 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800243
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100244 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000245}
246
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000247NICState *qemu_new_nic(NetClientInfo *info,
248 NICConf *conf,
249 const char *model,
250 const char *name,
251 void *opaque)
252{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800253 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000254 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200255 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000256
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200257 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000258 assert(info->size >= sizeof(NICState));
259
Jason Wangf6b26cf2013-02-22 23:15:06 +0800260 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
261 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000262 nic->conf = conf;
263 nic->opaque = opaque;
264
Jason Wangf6b26cf2013-02-22 23:15:06 +0800265 for (i = 0; i < queues; i++) {
266 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800267 NULL);
268 nic->ncs[i].queue_index = i;
269 }
270
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000271 return nic;
272}
273
Jason Wang1ceef9f2013-01-30 19:12:28 +0800274NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
275{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800276 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800277}
278
Jason Wangb356f762013-01-30 19:12:22 +0800279NetClientState *qemu_get_queue(NICState *nic)
280{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800281 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800282}
283
Jason Wangcc1f0f42013-01-30 19:12:23 +0800284NICState *qemu_get_nic(NetClientState *nc)
285{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800286 NetClientState *nc0 = nc - nc->queue_index;
287
Jason Wangf6b26cf2013-02-22 23:15:06 +0800288 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800289}
290
291void *qemu_get_nic_opaque(NetClientState *nc)
292{
293 NICState *nic = qemu_get_nic(nc);
294
295 return nic->opaque;
296}
297
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100298static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000299{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100300 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000301
Andreas Färbercc2a9042013-02-12 23:16:06 +0100302 if (nc->info->cleanup) {
303 nc->info->cleanup(nc);
304 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200305}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100306
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100307static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200308{
Jan Kiszka067404b2013-08-02 21:47:08 +0200309 if (nc->incoming_queue) {
310 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200311 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100312 if (nc->peer) {
313 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100314 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100315 g_free(nc->name);
316 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800317 if (nc->destructor) {
318 nc->destructor(nc);
319 }
aliguori63a01ef2008-10-31 19:10:00 +0000320}
321
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100322void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200323{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800324 NetClientState *ncs[MAX_QUEUE_NUM];
325 int queues, i;
326
327 /* If the NetClientState belongs to a multiqueue backend, we will change all
328 * other NetClientStates also.
329 */
330 queues = qemu_find_net_clients_except(nc->name, ncs,
331 NET_CLIENT_OPTIONS_KIND_NIC,
332 MAX_QUEUE_NUM);
333 assert(queues != 0);
334
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200335 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100336 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800337 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200338 if (nic->peer_deleted) {
339 return;
340 }
341 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800342
343 for (i = 0; i < queues; i++) {
344 ncs[i]->peer->link_down = true;
345 }
346
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100347 if (nc->peer->info->link_status_changed) {
348 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200349 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800350
351 for (i = 0; i < queues; i++) {
352 qemu_cleanup_net_client(ncs[i]);
353 }
354
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200355 return;
356 }
357
Jason Wang948ecf22013-01-30 19:12:24 +0800358 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
359
Jason Wang1ceef9f2013-01-30 19:12:28 +0800360 for (i = 0; i < queues; i++) {
361 qemu_cleanup_net_client(ncs[i]);
362 qemu_free_net_client(ncs[i]);
363 }
Jason Wang948ecf22013-01-30 19:12:24 +0800364}
365
366void qemu_del_nic(NICState *nic)
367{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200368 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800369
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200370 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800371 if (nic->peer_deleted) {
372 for (i = 0; i < queues; i++) {
373 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200374 }
375 }
376
Jason Wang1ceef9f2013-01-30 19:12:28 +0800377 for (i = queues - 1; i >= 0; i--) {
378 NetClientState *nc = qemu_get_subqueue(nic, i);
379
380 qemu_cleanup_net_client(nc);
381 qemu_free_net_client(nc);
382 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800383
384 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200385}
386
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000387void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
388{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100389 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000390
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100391 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200392 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800393 if (nc->queue_index == 0) {
394 func(qemu_get_nic(nc), opaque);
395 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000396 }
397 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000398}
399
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100400bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100401{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100402 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100403 return false;
404 }
405
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100406 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100407}
408
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100409bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100410{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100411 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100412 return false;
413 }
414
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100415 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100416}
417
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100418bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100419{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100420 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100421 return false;
422 }
423
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100424 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100425}
426
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100427void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100428{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100429 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100430 return;
431 }
432
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100433 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100434}
435
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100436void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100437 int ecn, int ufo)
438{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100439 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100440 return;
441 }
442
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100443 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100444}
445
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100446void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100447{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100448 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100449 return;
450 }
451
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100452 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100453}
454
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100455int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000456{
zhanghailiange1d64c02014-08-26 16:06:17 +0800457 int vm_running = runstate_is_running();
458
459 if (!vm_running) {
460 return 0;
461 }
462
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100463 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100464 return 1;
465 }
466
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100467 if (sender->peer->receive_disabled) {
468 return 0;
469 } else if (sender->peer->info->can_receive &&
470 !sender->peer->info->can_receive(sender->peer)) {
471 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000472 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500473 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000474}
475
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100476ssize_t qemu_deliver_packet(NetClientState *sender,
477 unsigned flags,
478 const uint8_t *data,
479 size_t size,
480 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100481{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100482 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000483 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100484
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100485 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100486 return size;
487 }
488
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100489 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000490 return 0;
491 }
492
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100493 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
494 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000495 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100496 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000497 }
498
499 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100500 nc->receive_disabled = 1;
Igor Ryzhovb9259652014-04-16 17:43:07 +0400501 }
Mark McLoughlin893379e2009-10-27 18:16:36 +0000502
503 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100504}
505
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100506void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000507{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100508 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100509 return;
510 }
511
Jan Kiszka067404b2013-08-02 21:47:08 +0200512 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100513}
514
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300515static
516void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100517{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100518 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100519
Luigi Rizzo199ee602013-02-05 17:53:31 +0100520 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
521 if (net_hub_flush(nc->peer)) {
522 qemu_notify_event();
523 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100524 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200525 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200526 /* We emptied the queue successfully, signal to the IO thread to repoll
527 * the file descriptor (for tap, for example).
528 */
529 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300530 } else if (purge) {
531 /* Unable to empty the queue, purge remaining packets */
532 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200533 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100534}
535
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300536void qemu_flush_queued_packets(NetClientState *nc)
537{
538 qemu_flush_or_purge_queued_packets(nc, false);
539}
540
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100541static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100542 unsigned flags,
543 const uint8_t *buf, int size,
544 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000545{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100546 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100547
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100548#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100549 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100550 hex_dump(stdout, buf, size);
551#endif
552
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100553 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100554 return size;
555 }
556
Jan Kiszka067404b2013-08-02 21:47:08 +0200557 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100558
Mark McLoughlinca77d172009-10-22 17:43:41 +0100559 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
560}
561
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100562ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100563 const uint8_t *buf, int size,
564 NetPacketSent *sent_cb)
565{
566 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
567 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100568}
569
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100570void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100571{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100572 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000573}
574
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100575ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100576{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100577 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100578 buf, size, NULL);
579}
580
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100581static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000582 int iovcnt)
583{
Scott Feldmand32fcad2013-03-18 11:43:44 -0700584 uint8_t buffer[NET_BUFSIZE];
Benjamin Poirierce053662011-02-23 19:57:21 -0500585 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000586
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400587 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000588
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100589 return nc->info->receive(nc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000590}
591
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100592ssize_t qemu_deliver_packet_iov(NetClientState *sender,
593 unsigned flags,
594 const struct iovec *iov,
595 int iovcnt,
596 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100597{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100598 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100599 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100600
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100601 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500602 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100603 }
604
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100605 if (nc->receive_disabled) {
606 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100607 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100608
609 if (nc->info->receive_iov) {
610 ret = nc->info->receive_iov(nc, iov, iovcnt);
611 } else {
612 ret = nc_sendv_compat(nc, iov, iovcnt);
613 }
614
615 if (ret == 0) {
616 nc->receive_disabled = 1;
617 }
618
619 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100620}
621
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100622ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100623 const struct iovec *iov, int iovcnt,
624 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000625{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100626 NetQueue *queue;
627
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100628 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500629 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000630 }
631
Jan Kiszka067404b2013-08-02 21:47:08 +0200632 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100633
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100634 return qemu_net_queue_send_iov(queue, sender,
635 QEMU_NET_PACKET_FLAG_NONE,
636 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000637}
638
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100639ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100640qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100641{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100642 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100643}
644
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100645NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000646{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100647 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100648
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100649 QTAILQ_FOREACH(nc, &net_clients, next) {
650 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200651 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100652 if (!strcmp(nc->name, id)) {
653 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100654 }
655 }
656
657 return NULL;
658}
659
Jason Wang6c51ae72013-01-30 19:12:25 +0800660int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
661 NetClientOptionsKind type, int max)
662{
663 NetClientState *nc;
664 int ret = 0;
665
666 QTAILQ_FOREACH(nc, &net_clients, next) {
667 if (nc->info->type == type) {
668 continue;
669 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100670 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800671 if (ret < max) {
672 ncs[ret] = nc;
673 }
674 ret++;
675 }
676 }
677
678 return ret;
679}
680
aliguori76970792009-02-11 15:20:03 +0000681static int nic_get_free_idx(void)
682{
683 int index;
684
685 for (index = 0; index < MAX_NICS; index++)
686 if (!nd_table[index].used)
687 return index;
688 return -1;
689}
690
Markus Armbruster07caea32009-09-25 03:53:51 +0200691int qemu_show_nic_models(const char *arg, const char *const *models)
692{
693 int i;
694
Peter Maydellc8057f92012-08-02 13:45:54 +0100695 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200696 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100697 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200698
699 fprintf(stderr, "qemu: Supported NIC models: ");
700 for (i = 0 ; models[i]; i++)
701 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
702 return 1;
703}
704
aliguorid07f22c2009-01-13 19:03:57 +0000705void qemu_check_nic_model(NICInfo *nd, const char *model)
706{
707 const char *models[2];
708
709 models[0] = model;
710 models[1] = NULL;
711
Markus Armbruster07caea32009-09-25 03:53:51 +0200712 if (qemu_show_nic_models(nd->model, models))
713 exit(0);
714 if (qemu_find_nic_model(nd, models, model) < 0)
715 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000716}
717
Markus Armbruster07caea32009-09-25 03:53:51 +0200718int qemu_find_nic_model(NICInfo *nd, const char * const *models,
719 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000720{
Markus Armbruster07caea32009-09-25 03:53:51 +0200721 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000722
723 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500724 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000725
Markus Armbruster07caea32009-09-25 03:53:51 +0200726 for (i = 0 ; models[i]; i++) {
727 if (strcmp(nd->model, models[i]) == 0)
728 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000729 }
730
Markus Armbruster6daf1942011-06-22 14:03:54 +0200731 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200732 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000733}
734
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200735static int net_init_nic(const NetClientOptions *opts, const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100736 NetClientState *peer)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100737{
738 int idx;
739 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200740 const NetLegacyNicOptions *nic;
741
742 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
743 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100744
745 idx = nic_get_free_idx();
746 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100747 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100748 return -1;
749 }
750
751 nd = &nd_table[idx];
752
753 memset(nd, 0, sizeof(*nd));
754
Laszlo Ersek2456f362012-07-17 16:17:14 +0200755 if (nic->has_netdev) {
756 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100757 if (!nd->netdev) {
Laszlo Ersek2456f362012-07-17 16:17:14 +0200758 error_report("netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100759 return -1;
760 }
761 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100762 assert(peer);
763 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100764 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100765 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200766 if (nic->has_model) {
767 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100768 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200769 if (nic->has_addr) {
770 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100771 }
772
Laszlo Ersek2456f362012-07-17 16:17:14 +0200773 if (nic->has_macaddr &&
774 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100775 error_report("invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100776 return -1;
777 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400778 if (nic->has_macaddr &&
779 is_multicast_ether_addr(nd->macaddr.a)) {
780 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
781 return -1;
782 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200783 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100784
Laszlo Ersek2456f362012-07-17 16:17:14 +0200785 if (nic->has_vectors) {
786 if (nic->vectors > 0x7ffffff) {
787 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
788 return -1;
789 }
790 nd->nvectors = nic->vectors;
791 } else {
792 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100793 }
794
795 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100796 nb_nics++;
797
798 return idx;
799}
800
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100801
Laszlo Ersek6687b792012-07-17 16:17:13 +0200802static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200803 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200804 const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100805 NetClientState *peer) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100806 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100807#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100808 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100809#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100810 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
811 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100812#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100813 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100814#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100815#ifdef CONFIG_NETMAP
816 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
817#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100818 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100819#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100820 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200821#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100822 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300823#ifdef CONFIG_VHOST_NET_USED
824 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
825#endif
Gonglei015a33b2014-07-01 20:58:08 +0800826#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100827 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
828#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100829};
830
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100831
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200832static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200833{
834 union {
835 const Netdev *netdev;
836 const NetLegacy *net;
837 } u;
838 const NetClientOptions *opts;
839 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100840
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100841 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200842 u.netdev = object;
843 opts = u.netdev->opts;
844 name = u.netdev->id;
845
846 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100847#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200848 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100849#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200850 case NET_CLIENT_OPTIONS_KIND_TAP:
851 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100852#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200853 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100854#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100855#ifdef CONFIG_NETMAP
856 case NET_CLIENT_OPTIONS_KIND_NETMAP:
857#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200858#ifdef CONFIG_NET_BRIDGE
859 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
860#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100861 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300862#ifdef CONFIG_VHOST_NET_USED
863 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
864#endif
Gonglei015a33b2014-07-01 20:58:08 +0800865#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100866 case NET_CLIENT_OPTIONS_KIND_L2TPV3:
867#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200868 break;
869
870 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300871 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
872 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100873 return -1;
874 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200875 } else {
876 u.net = object;
877 opts = u.net->opts;
878 /* missing optional values have been initialized to "all bits zero" */
879 name = u.net->has_id ? u.net->id : u.net->name;
880 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100881
Laszlo Ersek6687b792012-07-17 16:17:13 +0200882 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100883 NetClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200884
885 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
886 * parameter. */
887 if (!is_netdev &&
888 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
889 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100890 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100891 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200892
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100893 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200894 /* TODO push error reporting into init() methods */
895 error_set(errp, QERR_DEVICE_INIT_FAILED,
896 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100897 return -1;
898 }
899 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200900 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100901}
902
Laszlo Ersek6687b792012-07-17 16:17:13 +0200903
904static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
905{
906 if (is_netdev) {
907 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
908 } else {
909 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
910 }
911}
912
913
914int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
915{
916 void *object = NULL;
917 Error *err = NULL;
918 int ret = -1;
919
920 {
921 OptsVisitor *ov = opts_visitor_new(opts);
922
923 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
924 opts_visitor_cleanup(ov);
925 }
926
927 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200928 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +0200929 }
930
931 if (object) {
932 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
933
934 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
935 qapi_dealloc_visitor_cleanup(dv);
936 }
937
938 error_propagate(errp, err);
939 return ret;
940}
941
942
aliguori6f338c32009-02-11 15:21:54 +0000943static int net_host_check_device(const char *device)
944{
945 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +0100946 for (i = 0; host_net_devices[i]; i++) {
947 if (!strncmp(host_net_devices[i], device,
948 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +0000949 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +0100950 }
aliguori6f338c32009-02-11 15:21:54 +0000951 }
952
953 return 0;
954}
955
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100956void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000957{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300958 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100959 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300960 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100961 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300962
aliguori6f338c32009-02-11 15:21:54 +0000963 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +0000964 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +0000965 return;
966 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100967
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200968 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100969 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100970 return;
971 }
972
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100973 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100974
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300975 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100976 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +0100977 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +0000978 monitor_printf(mon, "adding host network device %s failed\n", device);
979 }
aliguori6f338c32009-02-11 15:21:54 +0000980}
981
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100982void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000983{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100984 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300985 int vlan_id = qdict_get_int(qdict, "vlan_id");
986 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +0000987
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100988 nc = net_hub_find_client_by_name(vlan_id, device);
989 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +0100990 error_report("Host network device '%s' on hub '%d' not found",
991 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +0000992 return;
993 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100994 if (!net_host_check_device(nc->model)) {
Hani Benhabiles86e11772014-04-01 00:05:14 +0100995 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +0000996 return;
997 }
Jason Wang64a55d62015-02-02 15:06:37 +0800998
999 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001000 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001001}
1002
Luiz Capitulino928059a2012-04-18 17:34:15 -03001003void netdev_add(QemuOpts *opts, Error **errp)
1004{
1005 net_client_init(opts, 1, errp);
1006}
1007
1008int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001009{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001010 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001011 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001012 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001013
Luiz Capitulino928059a2012-04-18 17:34:15 -03001014 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001015 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001016 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001017 }
1018
Luiz Capitulino928059a2012-04-18 17:34:15 -03001019 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001020 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001021 goto exit_err;
1022 }
1023
1024 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001025 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001026 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001027 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001028 }
1029
Luiz Capitulino928059a2012-04-18 17:34:15 -03001030 return 0;
1031
1032exit_err:
1033 qerror_report_err(local_err);
1034 error_free(local_err);
1035 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001036}
1037
Luiz Capitulino5f964152012-04-16 14:36:32 -03001038void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001039{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001040 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001041 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001042
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001043 nc = qemu_find_netdev(id);
1044 if (!nc) {
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
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001049 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1050 if (!opts) {
1051 error_setg(errp, "Device '%s' is not a netdev", id);
1052 return;
1053 }
1054
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001055 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001056 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001057}
1058
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001059void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001060{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001061 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1062 nc->queue_index,
1063 NetClientOptionsKind_lookup[nc->info->type],
1064 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +02001065}
1066
Amos Kongb1be4282013-06-14 15:45:52 +08001067RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1068 Error **errp)
1069{
1070 NetClientState *nc;
1071 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1072
1073 QTAILQ_FOREACH(nc, &net_clients, next) {
1074 RxFilterInfoList *entry;
1075 RxFilterInfo *info;
1076
1077 if (has_name && strcmp(nc->name, name) != 0) {
1078 continue;
1079 }
1080
1081 /* only query rx-filter information of NIC */
1082 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1083 if (has_name) {
1084 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001085 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001086 }
1087 continue;
1088 }
1089
1090 if (nc->info->query_rx_filter) {
1091 info = nc->info->query_rx_filter(nc);
1092 entry = g_malloc0(sizeof(*entry));
1093 entry->value = info;
1094
1095 if (!filter_list) {
1096 filter_list = entry;
1097 } else {
1098 last_entry->next = entry;
1099 }
1100 last_entry = entry;
1101 } else if (has_name) {
1102 error_setg(errp, "net client(%s) doesn't support"
1103 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001104 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001105 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001106
1107 if (has_name) {
1108 break;
1109 }
Amos Kongb1be4282013-06-14 15:45:52 +08001110 }
1111
Markus Armbruster9083da12014-04-24 15:44:18 +02001112 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001113 error_setg(errp, "invalid net client name: %s", name);
1114 }
1115
1116 return filter_list;
1117}
1118
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001119void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001120{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001121 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001122 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001123
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001124 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001125
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001126 QTAILQ_FOREACH(nc, &net_clients, next) {
1127 peer = nc->peer;
1128 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001129
1130 /* Skip if already printed in hub info */
1131 if (net_hub_id_for_client(nc, NULL) == 0) {
1132 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001133 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001134
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001135 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001136 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001137 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001138 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001139 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001140 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001141 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001142 }
aliguori63a01ef2008-10-31 19:10:00 +00001143}
1144
Luiz Capitulino4b371562011-11-23 13:11:55 -02001145void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001146{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001147 NetClientState *ncs[MAX_QUEUE_NUM];
1148 NetClientState *nc;
1149 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001150
Jason Wang1ceef9f2013-01-30 19:12:28 +08001151 queues = qemu_find_net_clients_except(name, ncs,
1152 NET_CLIENT_OPTIONS_KIND_MAX,
1153 MAX_QUEUE_NUM);
1154
1155 if (queues == 0) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001156 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1157 return;
aliguori436e5e52009-01-08 19:44:06 +00001158 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001159 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001160
Jason Wang1ceef9f2013-01-30 19:12:28 +08001161 for (i = 0; i < queues; i++) {
1162 ncs[i]->link_down = !up;
1163 }
aliguori436e5e52009-01-08 19:44:06 +00001164
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001165 if (nc->info->link_status_changed) {
1166 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001167 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001168
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001169 if (nc->peer) {
1170 /* Change peer link only if the peer is NIC and then notify peer.
1171 * If the peer is a HUBPORT or a backend, we do not change the
1172 * link status.
1173 *
1174 * This behavior is compatible with qemu vlans where there could be
1175 * multiple clients that can still communicate with each other in
1176 * disconnected mode. For now maintain this compatibility.
1177 */
1178 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1179 for (i = 0; i < queues; i++) {
1180 ncs[i]->peer->link_down = !up;
1181 }
1182 }
1183 if (nc->peer->info->link_status_changed) {
1184 nc->peer->info->link_status_changed(nc->peer);
1185 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001186 }
aliguori436e5e52009-01-08 19:44:06 +00001187}
1188
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001189static void net_vm_change_state_handler(void *opaque, int running,
1190 RunState state)
1191{
1192 /* Complete all queued packets, to guarantee we don't modify
1193 * state later when VM is not running.
1194 */
1195 if (!running) {
1196 NetClientState *nc;
1197 NetClientState *tmp;
1198
1199 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1200 qemu_flush_or_purge_queued_packets(nc, true);
1201 }
1202 }
1203}
1204
aliguori63a01ef2008-10-31 19:10:00 +00001205void net_cleanup(void)
1206{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001207 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001208
Jason Wang1ceef9f2013-01-30 19:12:28 +08001209 /* We may del multiple entries during qemu_del_net_client(),
1210 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1211 */
1212 while (!QTAILQ_EMPTY(&net_clients)) {
1213 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001214 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1215 qemu_del_nic(qemu_get_nic(nc));
1216 } else {
1217 qemu_del_net_client(nc);
1218 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001219 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001220
1221 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001222}
1223
Markus Armbruster668680f2010-02-11 14:44:58 +01001224void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001225{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001226 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001227 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001228
Peter Maydell641f6ea2011-05-20 16:50:00 +01001229 /* Don't warn about the default network setup that you get if
1230 * no command line -net or -netdev options are specified. There
1231 * are two cases that we would otherwise complain about:
1232 * (1) board doesn't support a NIC but the implicit "-net nic"
1233 * requested one
1234 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1235 * sets up a nic that isn't connected to anything.
1236 */
1237 if (default_net) {
1238 return;
1239 }
1240
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001241 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001242
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001243 QTAILQ_FOREACH(nc, &net_clients, next) {
1244 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001245 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001246 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1247 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001248 }
1249 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001250
1251 /* Check that all NICs requested via -net nic actually got created.
1252 * NICs created via -device don't need to be checked here because
1253 * they are always instantiated.
1254 */
1255 for (i = 0; i < MAX_NICS; i++) {
1256 NICInfo *nd = &nd_table[i];
1257 if (nd->used && !nd->instantiated) {
1258 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1259 "was not created (not supported by this machine?)\n",
1260 nd->name ? nd->name : "anonymous",
1261 nd->model ? nd->model : "unspecified");
1262 }
1263 }
aliguori63a01ef2008-10-31 19:10:00 +00001264}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001265
1266static int net_init_client(QemuOpts *opts, void *dummy)
1267{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001268 Error *local_err = NULL;
1269
1270 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001271 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001272 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001273 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001274 }
1275
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001276 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001277}
1278
1279static int net_init_netdev(QemuOpts *opts, void *dummy)
1280{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001281 Error *local_err = NULL;
1282 int ret;
1283
1284 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001285 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001286 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001287 return -1;
1288 }
1289
1290 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001291}
1292
1293int net_init_clients(void)
1294{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001295 QemuOptsList *net = qemu_find_opts("net");
1296
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001297 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001298 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001299 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001300#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001301 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001302#endif
1303 }
1304
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001305 net_change_state_entry =
1306 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1307
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001308 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001309
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001310 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001311 return -1;
1312
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001313 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001314 return -1;
1315 }
1316
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001317 return 0;
1318}
1319
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001320int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001321{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001322#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001323 int ret;
1324 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001325 return ret;
1326 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001327#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001328
Markus Armbruster8212c642010-02-10 19:52:18 +01001329 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001330 return -1;
1331 }
1332
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001333 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001334 return 0;
1335}
Jason Wang7fc8d912012-03-05 11:08:50 +08001336
1337/* From FreeBSD */
1338/* XXX: optimize */
1339unsigned compute_mcast_idx(const uint8_t *ep)
1340{
1341 uint32_t crc;
1342 int carry, i, j;
1343 uint8_t b;
1344
1345 crc = 0xffffffff;
1346 for (i = 0; i < 6; i++) {
1347 b = *ep++;
1348 for (j = 0; j < 8; j++) {
1349 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1350 crc <<= 1;
1351 b >>= 1;
1352 if (carry) {
1353 crc = ((crc ^ POLYNOMIAL) | carry);
1354 }
1355 }
1356 }
1357 return crc >> 26;
1358}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001359
1360QemuOptsList qemu_netdev_opts = {
1361 .name = "netdev",
1362 .implied_opt_name = "type",
1363 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1364 .desc = {
1365 /*
1366 * no elements => accept any params
1367 * validation will happen later
1368 */
1369 { /* end of list */ }
1370 },
1371};
1372
1373QemuOptsList qemu_net_opts = {
1374 .name = "net",
1375 .implied_opt_name = "type",
1376 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1377 .desc = {
1378 /*
1379 * no elements => accept any params
1380 * validation will happen later
1381 */
1382 { /* end of list */ }
1383 },
1384};