blob: d9aaeb5341ba058ea44466a6c868a748dd1ac34d [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
Scott Feldman890ee6a2015-03-13 21:09:25 -0700154char *qemu_mac_strdup_printf(const uint8_t *macaddr)
155{
156 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
157 macaddr[0], macaddr[1], macaddr[2],
158 macaddr[3], macaddr[4], macaddr[5]);
159}
160
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100161void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000162{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100163 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000164 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100165 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000166 macaddr[0], macaddr[1], macaddr[2],
167 macaddr[3], macaddr[4], macaddr[5]);
168}
169
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200170void qemu_macaddr_default_if_unset(MACAddr *macaddr)
171{
172 static int index = 0;
173 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
174
175 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
176 return;
177 macaddr->a[0] = 0x52;
178 macaddr->a[1] = 0x54;
179 macaddr->a[2] = 0x00;
180 macaddr->a[3] = 0x12;
181 macaddr->a[4] = 0x34;
182 macaddr->a[5] = 0x56 + index++;
183}
184
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100185/**
186 * Generate a name for net client
187 *
Amos Kongc9635302013-04-15 18:55:19 +0800188 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100189 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100190static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000191{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100192 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000193 int id = 0;
194
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100195 QTAILQ_FOREACH(nc, &net_clients, next) {
196 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100197 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100198 }
Amos Kongc9635302013-04-15 18:55:19 +0800199 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200200 id++;
201 }
202 }
203
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100204 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000205}
206
Jason Wangf7860452013-01-30 19:12:27 +0800207static void qemu_net_client_destructor(NetClientState *nc)
208{
209 g_free(nc);
210}
211
Jason Wang18a15412013-01-30 19:12:26 +0800212static void qemu_net_client_setup(NetClientState *nc,
213 NetClientInfo *info,
214 NetClientState *peer,
215 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800216 const char *name,
217 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000218{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100219 nc->info = info;
220 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000221 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100222 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000223 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100224 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000225 }
aliguori63a01ef2008-10-31 19:10:00 +0000226
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100227 if (peer) {
228 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100229 nc->peer = peer;
230 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100231 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100232 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100233
Jan Kiszka067404b2013-08-02 21:47:08 +0200234 nc->incoming_queue = qemu_new_net_queue(nc);
Jason Wangf7860452013-01-30 19:12:27 +0800235 nc->destructor = destructor;
Jason Wang18a15412013-01-30 19:12:26 +0800236}
237
238NetClientState *qemu_new_net_client(NetClientInfo *info,
239 NetClientState *peer,
240 const char *model,
241 const char *name)
242{
243 NetClientState *nc;
244
245 assert(info->size >= sizeof(NetClientState));
246
247 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800248 qemu_net_client_setup(nc, info, peer, model, name,
249 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800250
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100251 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000252}
253
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000254NICState *qemu_new_nic(NetClientInfo *info,
255 NICConf *conf,
256 const char *model,
257 const char *name,
258 void *opaque)
259{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800260 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000261 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200262 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000263
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200264 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000265 assert(info->size >= sizeof(NICState));
266
Jason Wangf6b26cf2013-02-22 23:15:06 +0800267 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
268 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000269 nic->conf = conf;
270 nic->opaque = opaque;
271
Jason Wangf6b26cf2013-02-22 23:15:06 +0800272 for (i = 0; i < queues; i++) {
273 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800274 NULL);
275 nic->ncs[i].queue_index = i;
276 }
277
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000278 return nic;
279}
280
Jason Wang1ceef9f2013-01-30 19:12:28 +0800281NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
282{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800283 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800284}
285
Jason Wangb356f762013-01-30 19:12:22 +0800286NetClientState *qemu_get_queue(NICState *nic)
287{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800288 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800289}
290
Jason Wangcc1f0f42013-01-30 19:12:23 +0800291NICState *qemu_get_nic(NetClientState *nc)
292{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800293 NetClientState *nc0 = nc - nc->queue_index;
294
Jason Wangf6b26cf2013-02-22 23:15:06 +0800295 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800296}
297
298void *qemu_get_nic_opaque(NetClientState *nc)
299{
300 NICState *nic = qemu_get_nic(nc);
301
302 return nic->opaque;
303}
304
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100305static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000306{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100307 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000308
Andreas Färbercc2a9042013-02-12 23:16:06 +0100309 if (nc->info->cleanup) {
310 nc->info->cleanup(nc);
311 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200312}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100313
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100314static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200315{
Jan Kiszka067404b2013-08-02 21:47:08 +0200316 if (nc->incoming_queue) {
317 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200318 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100319 if (nc->peer) {
320 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100321 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100322 g_free(nc->name);
323 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800324 if (nc->destructor) {
325 nc->destructor(nc);
326 }
aliguori63a01ef2008-10-31 19:10:00 +0000327}
328
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100329void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200330{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800331 NetClientState *ncs[MAX_QUEUE_NUM];
332 int queues, i;
333
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100334 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
335
Jason Wang1ceef9f2013-01-30 19:12:28 +0800336 /* If the NetClientState belongs to a multiqueue backend, we will change all
337 * other NetClientStates also.
338 */
339 queues = qemu_find_net_clients_except(nc->name, ncs,
340 NET_CLIENT_OPTIONS_KIND_NIC,
341 MAX_QUEUE_NUM);
342 assert(queues != 0);
343
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200344 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100345 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800346 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200347 if (nic->peer_deleted) {
348 return;
349 }
350 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800351
352 for (i = 0; i < queues; i++) {
353 ncs[i]->peer->link_down = true;
354 }
355
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100356 if (nc->peer->info->link_status_changed) {
357 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200358 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800359
360 for (i = 0; i < queues; i++) {
361 qemu_cleanup_net_client(ncs[i]);
362 }
363
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200364 return;
365 }
366
Jason Wang1ceef9f2013-01-30 19:12:28 +0800367 for (i = 0; i < queues; i++) {
368 qemu_cleanup_net_client(ncs[i]);
369 qemu_free_net_client(ncs[i]);
370 }
Jason Wang948ecf22013-01-30 19:12:24 +0800371}
372
373void qemu_del_nic(NICState *nic)
374{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200375 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800376
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200377 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800378 if (nic->peer_deleted) {
379 for (i = 0; i < queues; i++) {
380 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200381 }
382 }
383
Jason Wang1ceef9f2013-01-30 19:12:28 +0800384 for (i = queues - 1; i >= 0; i--) {
385 NetClientState *nc = qemu_get_subqueue(nic, i);
386
387 qemu_cleanup_net_client(nc);
388 qemu_free_net_client(nc);
389 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800390
391 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200392}
393
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000394void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
395{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100396 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000397
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100398 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200399 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800400 if (nc->queue_index == 0) {
401 func(qemu_get_nic(nc), opaque);
402 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000403 }
404 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000405}
406
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100407bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100408{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100409 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100410 return false;
411 }
412
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100413 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100414}
415
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100416bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100417{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100418 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100419 return false;
420 }
421
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100422 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100423}
424
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100425bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100426{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100427 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100428 return false;
429 }
430
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100431 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100432}
433
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100434void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100435{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100436 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100437 return;
438 }
439
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100440 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100441}
442
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100443void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100444 int ecn, int ufo)
445{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100446 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100447 return;
448 }
449
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100450 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100451}
452
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100453void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100454{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100455 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100456 return;
457 }
458
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100459 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100460}
461
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100462int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000463{
zhanghailiange1d64c02014-08-26 16:06:17 +0800464 int vm_running = runstate_is_running();
465
466 if (!vm_running) {
467 return 0;
468 }
469
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100470 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100471 return 1;
472 }
473
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100474 if (sender->peer->receive_disabled) {
475 return 0;
476 } else if (sender->peer->info->can_receive &&
477 !sender->peer->info->can_receive(sender->peer)) {
478 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000479 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500480 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000481}
482
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100483ssize_t qemu_deliver_packet(NetClientState *sender,
484 unsigned flags,
485 const uint8_t *data,
486 size_t size,
487 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100488{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100489 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000490 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100491
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100492 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100493 return size;
494 }
495
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100496 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000497 return 0;
498 }
499
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100500 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
501 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000502 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100503 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000504 }
505
506 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100507 nc->receive_disabled = 1;
Igor Ryzhovb9259652014-04-16 17:43:07 +0400508 }
Mark McLoughlin893379e2009-10-27 18:16:36 +0000509
510 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100511}
512
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100513void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000514{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100515 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100516 return;
517 }
518
Jan Kiszka067404b2013-08-02 21:47:08 +0200519 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100520}
521
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300522static
523void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100524{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100525 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100526
Luigi Rizzo199ee602013-02-05 17:53:31 +0100527 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
528 if (net_hub_flush(nc->peer)) {
529 qemu_notify_event();
530 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100531 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200532 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200533 /* We emptied the queue successfully, signal to the IO thread to repoll
534 * the file descriptor (for tap, for example).
535 */
536 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300537 } else if (purge) {
538 /* Unable to empty the queue, purge remaining packets */
539 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200540 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100541}
542
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300543void qemu_flush_queued_packets(NetClientState *nc)
544{
545 qemu_flush_or_purge_queued_packets(nc, false);
546}
547
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100548static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100549 unsigned flags,
550 const uint8_t *buf, int size,
551 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000552{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100553 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100554
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100555#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100556 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100557 hex_dump(stdout, buf, size);
558#endif
559
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100560 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100561 return size;
562 }
563
Jan Kiszka067404b2013-08-02 21:47:08 +0200564 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100565
Mark McLoughlinca77d172009-10-22 17:43:41 +0100566 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
567}
568
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100569ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100570 const uint8_t *buf, int size,
571 NetPacketSent *sent_cb)
572{
573 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
574 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100575}
576
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100577void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100578{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100579 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000580}
581
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100582ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100583{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100584 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100585 buf, size, NULL);
586}
587
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100588static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000589 int iovcnt)
590{
Scott Feldmand32fcad2013-03-18 11:43:44 -0700591 uint8_t buffer[NET_BUFSIZE];
Benjamin Poirierce053662011-02-23 19:57:21 -0500592 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000593
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400594 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000595
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100596 return nc->info->receive(nc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000597}
598
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100599ssize_t qemu_deliver_packet_iov(NetClientState *sender,
600 unsigned flags,
601 const struct iovec *iov,
602 int iovcnt,
603 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100604{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100605 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100606 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100607
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100608 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500609 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100610 }
611
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100612 if (nc->receive_disabled) {
613 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100614 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100615
616 if (nc->info->receive_iov) {
617 ret = nc->info->receive_iov(nc, iov, iovcnt);
618 } else {
619 ret = nc_sendv_compat(nc, iov, iovcnt);
620 }
621
622 if (ret == 0) {
623 nc->receive_disabled = 1;
624 }
625
626 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100627}
628
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100629ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100630 const struct iovec *iov, int iovcnt,
631 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000632{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100633 NetQueue *queue;
634
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100635 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500636 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000637 }
638
Jan Kiszka067404b2013-08-02 21:47:08 +0200639 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100640
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100641 return qemu_net_queue_send_iov(queue, sender,
642 QEMU_NET_PACKET_FLAG_NONE,
643 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000644}
645
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100646ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100647qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100648{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100649 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100650}
651
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100652NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000653{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100654 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100655
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100656 QTAILQ_FOREACH(nc, &net_clients, next) {
657 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200658 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100659 if (!strcmp(nc->name, id)) {
660 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100661 }
662 }
663
664 return NULL;
665}
666
Jason Wang6c51ae72013-01-30 19:12:25 +0800667int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
668 NetClientOptionsKind type, int max)
669{
670 NetClientState *nc;
671 int ret = 0;
672
673 QTAILQ_FOREACH(nc, &net_clients, next) {
674 if (nc->info->type == type) {
675 continue;
676 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100677 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800678 if (ret < max) {
679 ncs[ret] = nc;
680 }
681 ret++;
682 }
683 }
684
685 return ret;
686}
687
aliguori76970792009-02-11 15:20:03 +0000688static int nic_get_free_idx(void)
689{
690 int index;
691
692 for (index = 0; index < MAX_NICS; index++)
693 if (!nd_table[index].used)
694 return index;
695 return -1;
696}
697
Markus Armbruster07caea32009-09-25 03:53:51 +0200698int qemu_show_nic_models(const char *arg, const char *const *models)
699{
700 int i;
701
Peter Maydellc8057f92012-08-02 13:45:54 +0100702 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200703 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100704 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200705
706 fprintf(stderr, "qemu: Supported NIC models: ");
707 for (i = 0 ; models[i]; i++)
708 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
709 return 1;
710}
711
aliguorid07f22c2009-01-13 19:03:57 +0000712void qemu_check_nic_model(NICInfo *nd, const char *model)
713{
714 const char *models[2];
715
716 models[0] = model;
717 models[1] = NULL;
718
Markus Armbruster07caea32009-09-25 03:53:51 +0200719 if (qemu_show_nic_models(nd->model, models))
720 exit(0);
721 if (qemu_find_nic_model(nd, models, model) < 0)
722 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000723}
724
Markus Armbruster07caea32009-09-25 03:53:51 +0200725int qemu_find_nic_model(NICInfo *nd, const char * const *models,
726 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000727{
Markus Armbruster07caea32009-09-25 03:53:51 +0200728 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000729
730 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500731 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000732
Markus Armbruster07caea32009-09-25 03:53:51 +0200733 for (i = 0 ; models[i]; i++) {
734 if (strcmp(nd->model, models[i]) == 0)
735 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000736 }
737
Markus Armbruster6daf1942011-06-22 14:03:54 +0200738 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200739 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000740}
741
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200742static int net_init_nic(const NetClientOptions *opts, const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100743 NetClientState *peer)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100744{
745 int idx;
746 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200747 const NetLegacyNicOptions *nic;
748
749 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
750 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100751
752 idx = nic_get_free_idx();
753 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100754 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100755 return -1;
756 }
757
758 nd = &nd_table[idx];
759
760 memset(nd, 0, sizeof(*nd));
761
Laszlo Ersek2456f362012-07-17 16:17:14 +0200762 if (nic->has_netdev) {
763 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100764 if (!nd->netdev) {
Laszlo Ersek2456f362012-07-17 16:17:14 +0200765 error_report("netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100766 return -1;
767 }
768 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100769 assert(peer);
770 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100771 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100772 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200773 if (nic->has_model) {
774 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100775 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200776 if (nic->has_addr) {
777 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100778 }
779
Laszlo Ersek2456f362012-07-17 16:17:14 +0200780 if (nic->has_macaddr &&
781 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100782 error_report("invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100783 return -1;
784 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400785 if (nic->has_macaddr &&
786 is_multicast_ether_addr(nd->macaddr.a)) {
787 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
788 return -1;
789 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200790 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100791
Laszlo Ersek2456f362012-07-17 16:17:14 +0200792 if (nic->has_vectors) {
793 if (nic->vectors > 0x7ffffff) {
794 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
795 return -1;
796 }
797 nd->nvectors = nic->vectors;
798 } else {
799 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100800 }
801
802 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100803 nb_nics++;
804
805 return idx;
806}
807
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100808
Laszlo Ersek6687b792012-07-17 16:17:13 +0200809static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200810 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200811 const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100812 NetClientState *peer) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100813 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100814#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100815 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100816#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100817 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
818 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100819#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100820 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100821#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100822#ifdef CONFIG_NETMAP
823 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
824#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100825 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100826#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100827 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200828#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100829 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300830#ifdef CONFIG_VHOST_NET_USED
831 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
832#endif
Gonglei015a33b2014-07-01 20:58:08 +0800833#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100834 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
835#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100836};
837
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100838
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200839static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200840{
841 union {
842 const Netdev *netdev;
843 const NetLegacy *net;
844 } u;
845 const NetClientOptions *opts;
846 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100847
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100848 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200849 u.netdev = object;
850 opts = u.netdev->opts;
851 name = u.netdev->id;
852
853 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100854#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200855 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100856#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200857 case NET_CLIENT_OPTIONS_KIND_TAP:
858 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100859#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200860 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100861#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100862#ifdef CONFIG_NETMAP
863 case NET_CLIENT_OPTIONS_KIND_NETMAP:
864#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200865#ifdef CONFIG_NET_BRIDGE
866 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
867#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100868 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300869#ifdef CONFIG_VHOST_NET_USED
870 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
871#endif
Gonglei015a33b2014-07-01 20:58:08 +0800872#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100873 case NET_CLIENT_OPTIONS_KIND_L2TPV3:
874#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200875 break;
876
877 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300878 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
879 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100880 return -1;
881 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200882 } else {
883 u.net = object;
884 opts = u.net->opts;
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200885 if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
886 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
887 "a net type");
888 return -1;
889 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200890 /* missing optional values have been initialized to "all bits zero" */
891 name = u.net->has_id ? u.net->id : u.net->name;
892 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100893
Laszlo Ersek6687b792012-07-17 16:17:13 +0200894 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100895 NetClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200896
897 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
898 * parameter. */
899 if (!is_netdev &&
900 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
901 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100902 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100903 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200904
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100905 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200906 /* TODO push error reporting into init() methods */
907 error_set(errp, QERR_DEVICE_INIT_FAILED,
908 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100909 return -1;
910 }
911 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200912 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100913}
914
Laszlo Ersek6687b792012-07-17 16:17:13 +0200915
916static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
917{
918 if (is_netdev) {
919 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
920 } else {
921 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
922 }
923}
924
925
926int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
927{
928 void *object = NULL;
929 Error *err = NULL;
930 int ret = -1;
931
932 {
933 OptsVisitor *ov = opts_visitor_new(opts);
934
935 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
936 opts_visitor_cleanup(ov);
937 }
938
939 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200940 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +0200941 }
942
943 if (object) {
944 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
945
946 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
947 qapi_dealloc_visitor_cleanup(dv);
948 }
949
950 error_propagate(errp, err);
951 return ret;
952}
953
954
aliguori6f338c32009-02-11 15:21:54 +0000955static int net_host_check_device(const char *device)
956{
957 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +0100958 for (i = 0; host_net_devices[i]; i++) {
959 if (!strncmp(host_net_devices[i], device,
960 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +0000961 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +0100962 }
aliguori6f338c32009-02-11 15:21:54 +0000963 }
964
965 return 0;
966}
967
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100968void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000969{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300970 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100971 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300972 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100973 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300974
aliguori6f338c32009-02-11 15:21:54 +0000975 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +0000976 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +0000977 return;
978 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100979
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200980 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100981 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100982 return;
983 }
984
Markus Armbrusterf43e47d2015-02-12 17:52:20 +0100985 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100986
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300987 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100988 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +0100989 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +0000990 monitor_printf(mon, "adding host network device %s failed\n", device);
991 }
aliguori6f338c32009-02-11 15:21:54 +0000992}
993
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100994void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000995{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100996 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300997 int vlan_id = qdict_get_int(qdict, "vlan_id");
998 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +0000999
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001000 nc = net_hub_find_client_by_name(vlan_id, device);
1001 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001002 error_report("Host network device '%s' on hub '%d' not found",
1003 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001004 return;
1005 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001006 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001007 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001008 return;
1009 }
Jason Wang64a55d62015-02-02 15:06:37 +08001010
1011 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001012 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001013}
1014
Luiz Capitulino928059a2012-04-18 17:34:15 -03001015void netdev_add(QemuOpts *opts, Error **errp)
1016{
1017 net_client_init(opts, 1, errp);
1018}
1019
1020int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001021{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001022 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001023 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001024 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001025
Luiz Capitulino928059a2012-04-18 17:34:15 -03001026 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001027 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001028 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001029 }
1030
Luiz Capitulino928059a2012-04-18 17:34:15 -03001031 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001032 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001033 goto exit_err;
1034 }
1035
1036 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001037 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001038 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001039 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001040 }
1041
Luiz Capitulino928059a2012-04-18 17:34:15 -03001042 return 0;
1043
1044exit_err:
1045 qerror_report_err(local_err);
1046 error_free(local_err);
1047 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001048}
1049
Luiz Capitulino5f964152012-04-16 14:36:32 -03001050void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001051{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001052 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001053 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001054
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001055 nc = qemu_find_netdev(id);
1056 if (!nc) {
Luiz Capitulino5f964152012-04-16 14:36:32 -03001057 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
1058 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001059 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001060
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001061 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1062 if (!opts) {
1063 error_setg(errp, "Device '%s' is not a netdev", id);
1064 return;
1065 }
1066
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001067 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001068 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001069}
1070
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001071void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001072{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001073 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1074 nc->queue_index,
1075 NetClientOptionsKind_lookup[nc->info->type],
1076 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +02001077}
1078
Amos Kongb1be4282013-06-14 15:45:52 +08001079RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1080 Error **errp)
1081{
1082 NetClientState *nc;
1083 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1084
1085 QTAILQ_FOREACH(nc, &net_clients, next) {
1086 RxFilterInfoList *entry;
1087 RxFilterInfo *info;
1088
1089 if (has_name && strcmp(nc->name, name) != 0) {
1090 continue;
1091 }
1092
1093 /* only query rx-filter information of NIC */
1094 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1095 if (has_name) {
1096 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001097 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001098 }
1099 continue;
1100 }
1101
1102 if (nc->info->query_rx_filter) {
1103 info = nc->info->query_rx_filter(nc);
1104 entry = g_malloc0(sizeof(*entry));
1105 entry->value = info;
1106
1107 if (!filter_list) {
1108 filter_list = entry;
1109 } else {
1110 last_entry->next = entry;
1111 }
1112 last_entry = entry;
1113 } else if (has_name) {
1114 error_setg(errp, "net client(%s) doesn't support"
1115 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001116 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001117 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001118
1119 if (has_name) {
1120 break;
1121 }
Amos Kongb1be4282013-06-14 15:45:52 +08001122 }
1123
Markus Armbruster9083da12014-04-24 15:44:18 +02001124 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001125 error_setg(errp, "invalid net client name: %s", name);
1126 }
1127
1128 return filter_list;
1129}
1130
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001131void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001132{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001133 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001134 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001135
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001136 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001137
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001138 QTAILQ_FOREACH(nc, &net_clients, next) {
1139 peer = nc->peer;
1140 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001141
1142 /* Skip if already printed in hub info */
1143 if (net_hub_id_for_client(nc, NULL) == 0) {
1144 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001145 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001146
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001147 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001148 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001149 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001150 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001151 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001152 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001153 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001154 }
aliguori63a01ef2008-10-31 19:10:00 +00001155}
1156
Luiz Capitulino4b371562011-11-23 13:11:55 -02001157void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001158{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001159 NetClientState *ncs[MAX_QUEUE_NUM];
1160 NetClientState *nc;
1161 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001162
Jason Wang1ceef9f2013-01-30 19:12:28 +08001163 queues = qemu_find_net_clients_except(name, ncs,
1164 NET_CLIENT_OPTIONS_KIND_MAX,
1165 MAX_QUEUE_NUM);
1166
1167 if (queues == 0) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001168 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1169 return;
aliguori436e5e52009-01-08 19:44:06 +00001170 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001171 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001172
Jason Wang1ceef9f2013-01-30 19:12:28 +08001173 for (i = 0; i < queues; i++) {
1174 ncs[i]->link_down = !up;
1175 }
aliguori436e5e52009-01-08 19:44:06 +00001176
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001177 if (nc->info->link_status_changed) {
1178 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001179 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001180
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001181 if (nc->peer) {
1182 /* Change peer link only if the peer is NIC and then notify peer.
1183 * If the peer is a HUBPORT or a backend, we do not change the
1184 * link status.
1185 *
1186 * This behavior is compatible with qemu vlans where there could be
1187 * multiple clients that can still communicate with each other in
1188 * disconnected mode. For now maintain this compatibility.
1189 */
1190 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1191 for (i = 0; i < queues; i++) {
1192 ncs[i]->peer->link_down = !up;
1193 }
1194 }
1195 if (nc->peer->info->link_status_changed) {
1196 nc->peer->info->link_status_changed(nc->peer);
1197 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001198 }
aliguori436e5e52009-01-08 19:44:06 +00001199}
1200
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001201static void net_vm_change_state_handler(void *opaque, int running,
1202 RunState state)
1203{
1204 /* Complete all queued packets, to guarantee we don't modify
1205 * state later when VM is not running.
1206 */
1207 if (!running) {
1208 NetClientState *nc;
1209 NetClientState *tmp;
1210
1211 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1212 qemu_flush_or_purge_queued_packets(nc, true);
1213 }
1214 }
1215}
1216
aliguori63a01ef2008-10-31 19:10:00 +00001217void net_cleanup(void)
1218{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001219 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001220
Jason Wang1ceef9f2013-01-30 19:12:28 +08001221 /* We may del multiple entries during qemu_del_net_client(),
1222 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1223 */
1224 while (!QTAILQ_EMPTY(&net_clients)) {
1225 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001226 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1227 qemu_del_nic(qemu_get_nic(nc));
1228 } else {
1229 qemu_del_net_client(nc);
1230 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001231 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001232
1233 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001234}
1235
Markus Armbruster668680f2010-02-11 14:44:58 +01001236void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001237{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001238 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001239 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001240
Peter Maydell641f6ea2011-05-20 16:50:00 +01001241 /* Don't warn about the default network setup that you get if
1242 * no command line -net or -netdev options are specified. There
1243 * are two cases that we would otherwise complain about:
1244 * (1) board doesn't support a NIC but the implicit "-net nic"
1245 * requested one
1246 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1247 * sets up a nic that isn't connected to anything.
1248 */
1249 if (default_net) {
1250 return;
1251 }
1252
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001253 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001254
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001255 QTAILQ_FOREACH(nc, &net_clients, next) {
1256 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001257 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001258 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1259 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001260 }
1261 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001262
1263 /* Check that all NICs requested via -net nic actually got created.
1264 * NICs created via -device don't need to be checked here because
1265 * they are always instantiated.
1266 */
1267 for (i = 0; i < MAX_NICS; i++) {
1268 NICInfo *nd = &nd_table[i];
1269 if (nd->used && !nd->instantiated) {
1270 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1271 "was not created (not supported by this machine?)\n",
1272 nd->name ? nd->name : "anonymous",
1273 nd->model ? nd->model : "unspecified");
1274 }
1275 }
aliguori63a01ef2008-10-31 19:10:00 +00001276}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001277
1278static int net_init_client(QemuOpts *opts, void *dummy)
1279{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001280 Error *local_err = NULL;
1281
1282 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001283 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001284 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001285 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001286 }
1287
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001288 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001289}
1290
1291static int net_init_netdev(QemuOpts *opts, void *dummy)
1292{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001293 Error *local_err = NULL;
1294 int ret;
1295
1296 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001297 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001298 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001299 return -1;
1300 }
1301
1302 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001303}
1304
1305int net_init_clients(void)
1306{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001307 QemuOptsList *net = qemu_find_opts("net");
1308
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001309 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001310 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001311 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001312#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001313 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001314#endif
1315 }
1316
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001317 net_change_state_entry =
1318 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1319
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001320 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001321
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001322 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001323 return -1;
1324
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001325 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001326 return -1;
1327 }
1328
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001329 return 0;
1330}
1331
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001332int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001333{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001334#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001335 int ret;
1336 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001337 return ret;
1338 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001339#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001340
Markus Armbruster8212c642010-02-10 19:52:18 +01001341 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001342 return -1;
1343 }
1344
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001345 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001346 return 0;
1347}
Jason Wang7fc8d912012-03-05 11:08:50 +08001348
1349/* From FreeBSD */
1350/* XXX: optimize */
1351unsigned compute_mcast_idx(const uint8_t *ep)
1352{
1353 uint32_t crc;
1354 int carry, i, j;
1355 uint8_t b;
1356
1357 crc = 0xffffffff;
1358 for (i = 0; i < 6; i++) {
1359 b = *ep++;
1360 for (j = 0; j < 8; j++) {
1361 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1362 crc <<= 1;
1363 b >>= 1;
1364 if (carry) {
1365 crc = ((crc ^ POLYNOMIAL) | carry);
1366 }
1367 }
1368 }
1369 return crc >> 26;
1370}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001371
1372QemuOptsList qemu_netdev_opts = {
1373 .name = "netdev",
1374 .implied_opt_name = "type",
1375 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1376 .desc = {
1377 /*
1378 * no elements => accept any params
1379 * validation will happen later
1380 */
1381 { /* end of list */ }
1382 },
1383};
1384
1385QemuOptsList qemu_net_opts = {
1386 .name = "net",
1387 .implied_opt_name = "type",
1388 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1389 .desc = {
1390 /*
1391 * no elements => accept any params
1392 * validation will happen later
1393 */
1394 { /* end of list */ }
1395 },
1396};