blob: 8e5b6f636390807b0eec946021561b09c5e42868 [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"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010035#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010036#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/sockets.h"
38#include "qemu/config-file.h"
Luiz Capitulino4b371562011-11-23 13:11:55 -020039#include "qmp-commands.h"
Amit Shah75422b02010-02-25 17:24:43 +053040#include "hw/qdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010042#include "qemu/main-loop.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020043#include "qapi-visit.h"
44#include "qapi/opts-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010045#include "qapi/dealloc-visitor.h"
zhanghailiange1d64c02014-08-26 16:06:17 +080046#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000047
Stefan Weil2944e4e2012-02-04 09:24:46 +010048/* Net bridge is currently not supported for W32. */
49#if !defined(_WIN32)
50# define CONFIG_NET_BRIDGE
51#endif
52
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030053static VMChangeStateEntry *net_change_state_entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010054static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000055
Hani Benhabiles84007e82014-05-27 23:39:33 +010056const char *host_net_devices[] = {
57 "tap",
58 "socket",
59 "dump",
60#ifdef CONFIG_NET_BRIDGE
61 "bridge",
62#endif
Stefan Hajnoczi027a2472015-05-27 17:16:48 +010063#ifdef CONFIG_NETMAP
64 "netmap",
65#endif
Hani Benhabiles84007e82014-05-27 23:39:33 +010066#ifdef CONFIG_SLIRP
67 "user",
68#endif
69#ifdef CONFIG_VDE
70 "vde",
71#endif
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030072 "vhost-user",
Hani Benhabiles84007e82014-05-27 23:39:33 +010073 NULL,
74};
75
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010076int default_net = 1;
77
aliguori63a01ef2008-10-31 19:10:00 +000078/***********************************************************/
79/* network device redirectors */
80
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000081#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000082static void hex_dump(FILE *f, const uint8_t *buf, int size)
83{
84 int len, i, j, c;
85
86 for(i=0;i<size;i+=16) {
87 len = size - i;
88 if (len > 16)
89 len = 16;
90 fprintf(f, "%08x ", i);
91 for(j=0;j<16;j++) {
92 if (j < len)
93 fprintf(f, " %02x", buf[i+j]);
94 else
95 fprintf(f, " ");
96 }
97 fprintf(f, " ");
98 for(j=0;j<len;j++) {
99 c = buf[i+j];
100 if (c < ' ' || c > '~')
101 c = '.';
102 fprintf(f, "%c", c);
103 }
104 fprintf(f, "\n");
105 }
106}
107#endif
108
aliguori63a01ef2008-10-31 19:10:00 +0000109static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
110{
111 const char *p, *p1;
112 int len;
113 p = *pp;
114 p1 = strchr(p, sep);
115 if (!p1)
116 return -1;
117 len = p1 - p;
118 p1++;
119 if (buf_size > 0) {
120 if (len > buf_size - 1)
121 len = buf_size - 1;
122 memcpy(buf, p, len);
123 buf[len] = '\0';
124 }
125 *pp = p1;
126 return 0;
127}
128
aliguori63a01ef2008-10-31 19:10:00 +0000129int parse_host_port(struct sockaddr_in *saddr, const char *str)
130{
131 char buf[512];
132 struct hostent *he;
133 const char *p, *r;
134 int port;
135
136 p = str;
137 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
138 return -1;
139 saddr->sin_family = AF_INET;
140 if (buf[0] == '\0') {
141 saddr->sin_addr.s_addr = 0;
142 } else {
blueswir1cd390082008-11-16 13:53:32 +0000143 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000144 if (!inet_aton(buf, &saddr->sin_addr))
145 return -1;
146 } else {
147 if ((he = gethostbyname(buf)) == NULL)
148 return - 1;
149 saddr->sin_addr = *(struct in_addr *)he->h_addr;
150 }
151 }
152 port = strtol(p, (char **)&r, 0);
153 if (r == p)
154 return -1;
155 saddr->sin_port = htons(port);
156 return 0;
157}
158
Scott Feldman890ee6a2015-03-13 21:09:25 -0700159char *qemu_mac_strdup_printf(const uint8_t *macaddr)
160{
161 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
162 macaddr[0], macaddr[1], macaddr[2],
163 macaddr[3], macaddr[4], macaddr[5]);
164}
165
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100166void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000167{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100168 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000169 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100170 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000171 macaddr[0], macaddr[1], macaddr[2],
172 macaddr[3], macaddr[4], macaddr[5]);
173}
174
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800175static int mac_table[256] = {0};
176
177static void qemu_macaddr_set_used(MACAddr *macaddr)
178{
179 int index;
180
181 for (index = 0x56; index < 0xFF; index++) {
182 if (macaddr->a[5] == index) {
183 mac_table[index]++;
184 }
185 }
186}
187
188static void qemu_macaddr_set_free(MACAddr *macaddr)
189{
190 int index;
191 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
192
193 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
194 return;
195 }
196 for (index = 0x56; index < 0xFF; index++) {
197 if (macaddr->a[5] == index) {
198 mac_table[index]--;
199 }
200 }
201}
202
203static int qemu_macaddr_get_free(void)
204{
205 int index;
206
207 for (index = 0x56; index < 0xFF; index++) {
208 if (mac_table[index] == 0) {
209 return index;
210 }
211 }
212
213 return -1;
214}
215
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200216void qemu_macaddr_default_if_unset(MACAddr *macaddr)
217{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200218 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800219 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200220
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800221 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
222 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
223 return;
224 } else {
225 qemu_macaddr_set_used(macaddr);
226 return;
227 }
228 }
229
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200230 macaddr->a[0] = 0x52;
231 macaddr->a[1] = 0x54;
232 macaddr->a[2] = 0x00;
233 macaddr->a[3] = 0x12;
234 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800235 macaddr->a[5] = qemu_macaddr_get_free();
236 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200237}
238
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100239/**
240 * Generate a name for net client
241 *
Amos Kongc9635302013-04-15 18:55:19 +0800242 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100243 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100244static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000245{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100246 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000247 int id = 0;
248
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100249 QTAILQ_FOREACH(nc, &net_clients, next) {
250 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100251 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100252 }
Amos Kongc9635302013-04-15 18:55:19 +0800253 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200254 id++;
255 }
256 }
257
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100258 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000259}
260
Jason Wangf7860452013-01-30 19:12:27 +0800261static void qemu_net_client_destructor(NetClientState *nc)
262{
263 g_free(nc);
264}
265
Jason Wang18a15412013-01-30 19:12:26 +0800266static void qemu_net_client_setup(NetClientState *nc,
267 NetClientInfo *info,
268 NetClientState *peer,
269 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800270 const char *name,
271 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000272{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100273 nc->info = info;
274 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000275 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100276 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000277 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100278 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000279 }
aliguori63a01ef2008-10-31 19:10:00 +0000280
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100281 if (peer) {
282 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100283 nc->peer = peer;
284 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100285 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100286 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100287
Jan Kiszka067404b2013-08-02 21:47:08 +0200288 nc->incoming_queue = qemu_new_net_queue(nc);
Jason Wangf7860452013-01-30 19:12:27 +0800289 nc->destructor = destructor;
Jason Wang18a15412013-01-30 19:12:26 +0800290}
291
292NetClientState *qemu_new_net_client(NetClientInfo *info,
293 NetClientState *peer,
294 const char *model,
295 const char *name)
296{
297 NetClientState *nc;
298
299 assert(info->size >= sizeof(NetClientState));
300
301 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800302 qemu_net_client_setup(nc, info, peer, model, name,
303 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800304
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100305 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000306}
307
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000308NICState *qemu_new_nic(NetClientInfo *info,
309 NICConf *conf,
310 const char *model,
311 const char *name,
312 void *opaque)
313{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800314 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000315 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200316 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000317
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200318 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000319 assert(info->size >= sizeof(NICState));
320
Jason Wangf6b26cf2013-02-22 23:15:06 +0800321 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
322 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000323 nic->conf = conf;
324 nic->opaque = opaque;
325
Jason Wangf6b26cf2013-02-22 23:15:06 +0800326 for (i = 0; i < queues; i++) {
327 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800328 NULL);
329 nic->ncs[i].queue_index = i;
330 }
331
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000332 return nic;
333}
334
Jason Wang1ceef9f2013-01-30 19:12:28 +0800335NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
336{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800337 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800338}
339
Jason Wangb356f762013-01-30 19:12:22 +0800340NetClientState *qemu_get_queue(NICState *nic)
341{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800342 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800343}
344
Jason Wangcc1f0f42013-01-30 19:12:23 +0800345NICState *qemu_get_nic(NetClientState *nc)
346{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800347 NetClientState *nc0 = nc - nc->queue_index;
348
Jason Wangf6b26cf2013-02-22 23:15:06 +0800349 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800350}
351
352void *qemu_get_nic_opaque(NetClientState *nc)
353{
354 NICState *nic = qemu_get_nic(nc);
355
356 return nic->opaque;
357}
358
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100359static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000360{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100361 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000362
Andreas Färbercc2a9042013-02-12 23:16:06 +0100363 if (nc->info->cleanup) {
364 nc->info->cleanup(nc);
365 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200366}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100367
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100368static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200369{
Jan Kiszka067404b2013-08-02 21:47:08 +0200370 if (nc->incoming_queue) {
371 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200372 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100373 if (nc->peer) {
374 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100375 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100376 g_free(nc->name);
377 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800378 if (nc->destructor) {
379 nc->destructor(nc);
380 }
aliguori63a01ef2008-10-31 19:10:00 +0000381}
382
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100383void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200384{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800385 NetClientState *ncs[MAX_QUEUE_NUM];
386 int queues, i;
387
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100388 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
389
Jason Wang1ceef9f2013-01-30 19:12:28 +0800390 /* If the NetClientState belongs to a multiqueue backend, we will change all
391 * other NetClientStates also.
392 */
393 queues = qemu_find_net_clients_except(nc->name, ncs,
394 NET_CLIENT_OPTIONS_KIND_NIC,
395 MAX_QUEUE_NUM);
396 assert(queues != 0);
397
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200398 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100399 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800400 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200401 if (nic->peer_deleted) {
402 return;
403 }
404 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800405
406 for (i = 0; i < queues; i++) {
407 ncs[i]->peer->link_down = true;
408 }
409
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100410 if (nc->peer->info->link_status_changed) {
411 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200412 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800413
414 for (i = 0; i < queues; i++) {
415 qemu_cleanup_net_client(ncs[i]);
416 }
417
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200418 return;
419 }
420
Jason Wang1ceef9f2013-01-30 19:12:28 +0800421 for (i = 0; i < queues; i++) {
422 qemu_cleanup_net_client(ncs[i]);
423 qemu_free_net_client(ncs[i]);
424 }
Jason Wang948ecf22013-01-30 19:12:24 +0800425}
426
427void qemu_del_nic(NICState *nic)
428{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200429 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800430
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800431 qemu_macaddr_set_free(&nic->conf->macaddr);
432
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200433 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800434 if (nic->peer_deleted) {
435 for (i = 0; i < queues; i++) {
436 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200437 }
438 }
439
Jason Wang1ceef9f2013-01-30 19:12:28 +0800440 for (i = queues - 1; i >= 0; i--) {
441 NetClientState *nc = qemu_get_subqueue(nic, i);
442
443 qemu_cleanup_net_client(nc);
444 qemu_free_net_client(nc);
445 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800446
447 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200448}
449
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000450void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
451{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100452 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000453
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100454 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200455 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800456 if (nc->queue_index == 0) {
457 func(qemu_get_nic(nc), opaque);
458 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000459 }
460 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000461}
462
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100463bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100464{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100465 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100466 return false;
467 }
468
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100469 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100470}
471
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100472bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100473{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100474 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100475 return false;
476 }
477
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100478 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100479}
480
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100481bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100482{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100483 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100484 return false;
485 }
486
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100487 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100488}
489
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100490void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100491{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100492 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100493 return;
494 }
495
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100496 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100497}
498
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100499void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100500 int ecn, int ufo)
501{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100502 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100503 return;
504 }
505
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100506 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100507}
508
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100509void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100510{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100511 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100512 return;
513 }
514
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100515 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100516}
517
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200518int qemu_set_vnet_le(NetClientState *nc, bool is_le)
519{
520 if (!nc || !nc->info->set_vnet_le) {
521 return -ENOSYS;
522 }
523
524 return nc->info->set_vnet_le(nc, is_le);
525}
526
527int qemu_set_vnet_be(NetClientState *nc, bool is_be)
528{
529 if (!nc || !nc->info->set_vnet_be) {
530 return -ENOSYS;
531 }
532
533 return nc->info->set_vnet_be(nc, is_be);
534}
535
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100536int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000537{
zhanghailiange1d64c02014-08-26 16:06:17 +0800538 int vm_running = runstate_is_running();
539
540 if (!vm_running) {
541 return 0;
542 }
543
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100544 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100545 return 1;
546 }
547
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100548 if (sender->peer->receive_disabled) {
549 return 0;
550 } else if (sender->peer->info->can_receive &&
551 !sender->peer->info->can_receive(sender->peer)) {
552 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000553 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500554 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000555}
556
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100557ssize_t qemu_deliver_packet(NetClientState *sender,
558 unsigned flags,
559 const uint8_t *data,
560 size_t size,
561 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100562{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100563 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000564 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100565
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100566 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100567 return size;
568 }
569
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100570 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000571 return 0;
572 }
573
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100574 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
575 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000576 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100577 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000578 }
579
580 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100581 nc->receive_disabled = 1;
Igor Ryzhovb9259652014-04-16 17:43:07 +0400582 }
Mark McLoughlin893379e2009-10-27 18:16:36 +0000583
584 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100585}
586
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100587void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000588{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100589 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100590 return;
591 }
592
Jan Kiszka067404b2013-08-02 21:47:08 +0200593 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100594}
595
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300596static
597void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100598{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100599 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100600
Luigi Rizzo199ee602013-02-05 17:53:31 +0100601 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
602 if (net_hub_flush(nc->peer)) {
603 qemu_notify_event();
604 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100605 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200606 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200607 /* We emptied the queue successfully, signal to the IO thread to repoll
608 * the file descriptor (for tap, for example).
609 */
610 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300611 } else if (purge) {
612 /* Unable to empty the queue, purge remaining packets */
613 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200614 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100615}
616
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300617void qemu_flush_queued_packets(NetClientState *nc)
618{
619 qemu_flush_or_purge_queued_packets(nc, false);
620}
621
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100622static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100623 unsigned flags,
624 const uint8_t *buf, int size,
625 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000626{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100627 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100628
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100629#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100630 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100631 hex_dump(stdout, buf, size);
632#endif
633
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100634 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100635 return size;
636 }
637
Jan Kiszka067404b2013-08-02 21:47:08 +0200638 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100639
Mark McLoughlinca77d172009-10-22 17:43:41 +0100640 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
641}
642
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100643ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100644 const uint8_t *buf, int size,
645 NetPacketSent *sent_cb)
646{
647 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
648 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100649}
650
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100651void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100652{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100653 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000654}
655
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100656ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100657{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100658 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100659 buf, size, NULL);
660}
661
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100662static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000663 int iovcnt)
664{
Scott Feldmand32fcad2013-03-18 11:43:44 -0700665 uint8_t buffer[NET_BUFSIZE];
Benjamin Poirierce053662011-02-23 19:57:21 -0500666 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000667
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400668 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000669
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100670 return nc->info->receive(nc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000671}
672
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100673ssize_t qemu_deliver_packet_iov(NetClientState *sender,
674 unsigned flags,
675 const struct iovec *iov,
676 int iovcnt,
677 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100678{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100679 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100680 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100681
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100682 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500683 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100684 }
685
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100686 if (nc->receive_disabled) {
687 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100688 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100689
690 if (nc->info->receive_iov) {
691 ret = nc->info->receive_iov(nc, iov, iovcnt);
692 } else {
693 ret = nc_sendv_compat(nc, iov, iovcnt);
694 }
695
696 if (ret == 0) {
697 nc->receive_disabled = 1;
698 }
699
700 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100701}
702
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100703ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100704 const struct iovec *iov, int iovcnt,
705 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000706{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100707 NetQueue *queue;
708
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100709 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500710 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000711 }
712
Jan Kiszka067404b2013-08-02 21:47:08 +0200713 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100714
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100715 return qemu_net_queue_send_iov(queue, sender,
716 QEMU_NET_PACKET_FLAG_NONE,
717 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000718}
719
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100720ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100721qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100722{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100723 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100724}
725
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100726NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000727{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100728 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100729
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100730 QTAILQ_FOREACH(nc, &net_clients, next) {
731 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200732 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100733 if (!strcmp(nc->name, id)) {
734 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100735 }
736 }
737
738 return NULL;
739}
740
Jason Wang6c51ae72013-01-30 19:12:25 +0800741int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
742 NetClientOptionsKind type, int max)
743{
744 NetClientState *nc;
745 int ret = 0;
746
747 QTAILQ_FOREACH(nc, &net_clients, next) {
748 if (nc->info->type == type) {
749 continue;
750 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100751 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800752 if (ret < max) {
753 ncs[ret] = nc;
754 }
755 ret++;
756 }
757 }
758
759 return ret;
760}
761
aliguori76970792009-02-11 15:20:03 +0000762static int nic_get_free_idx(void)
763{
764 int index;
765
766 for (index = 0; index < MAX_NICS; index++)
767 if (!nd_table[index].used)
768 return index;
769 return -1;
770}
771
Markus Armbruster07caea32009-09-25 03:53:51 +0200772int qemu_show_nic_models(const char *arg, const char *const *models)
773{
774 int i;
775
Peter Maydellc8057f92012-08-02 13:45:54 +0100776 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200777 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100778 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200779
780 fprintf(stderr, "qemu: Supported NIC models: ");
781 for (i = 0 ; models[i]; i++)
782 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
783 return 1;
784}
785
aliguorid07f22c2009-01-13 19:03:57 +0000786void qemu_check_nic_model(NICInfo *nd, const char *model)
787{
788 const char *models[2];
789
790 models[0] = model;
791 models[1] = NULL;
792
Markus Armbruster07caea32009-09-25 03:53:51 +0200793 if (qemu_show_nic_models(nd->model, models))
794 exit(0);
795 if (qemu_find_nic_model(nd, models, model) < 0)
796 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000797}
798
Markus Armbruster07caea32009-09-25 03:53:51 +0200799int qemu_find_nic_model(NICInfo *nd, const char * const *models,
800 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000801{
Markus Armbruster07caea32009-09-25 03:53:51 +0200802 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000803
804 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500805 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000806
Markus Armbruster07caea32009-09-25 03:53:51 +0200807 for (i = 0 ; models[i]; i++) {
808 if (strcmp(nd->model, models[i]) == 0)
809 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000810 }
811
Markus Armbruster6daf1942011-06-22 14:03:54 +0200812 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200813 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000814}
815
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200816static int net_init_nic(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200817 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100818{
819 int idx;
820 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200821 const NetLegacyNicOptions *nic;
822
823 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
824 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100825
826 idx = nic_get_free_idx();
827 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200828 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100829 return -1;
830 }
831
832 nd = &nd_table[idx];
833
834 memset(nd, 0, sizeof(*nd));
835
Laszlo Ersek2456f362012-07-17 16:17:14 +0200836 if (nic->has_netdev) {
837 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100838 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200839 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100840 return -1;
841 }
842 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100843 assert(peer);
844 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100845 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100846 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200847 if (nic->has_model) {
848 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100849 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200850 if (nic->has_addr) {
851 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100852 }
853
Laszlo Ersek2456f362012-07-17 16:17:14 +0200854 if (nic->has_macaddr &&
855 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200856 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100857 return -1;
858 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400859 if (nic->has_macaddr &&
860 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200861 error_setg(errp,
862 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400863 return -1;
864 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200865 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100866
Laszlo Ersek2456f362012-07-17 16:17:14 +0200867 if (nic->has_vectors) {
868 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200869 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200870 return -1;
871 }
872 nd->nvectors = nic->vectors;
873 } else {
874 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100875 }
876
877 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100878 nb_nics++;
879
880 return idx;
881}
882
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100883
Laszlo Ersek6687b792012-07-17 16:17:13 +0200884static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200885 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200886 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200887 NetClientState *peer, Error **errp) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100888 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100889#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100890 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100891#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100892 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
893 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100894#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100895 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100896#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100897#ifdef CONFIG_NETMAP
898 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
899#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100900 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100901#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100902 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200903#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100904 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300905#ifdef CONFIG_VHOST_NET_USED
906 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
907#endif
Gonglei015a33b2014-07-01 20:58:08 +0800908#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100909 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
910#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100911};
912
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100913
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200914static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200915{
916 union {
917 const Netdev *netdev;
918 const NetLegacy *net;
919 } u;
920 const NetClientOptions *opts;
921 const char *name;
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100922 NetClientState *peer = NULL;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100923
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100924 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200925 u.netdev = object;
926 opts = u.netdev->opts;
927 name = u.netdev->id;
928
Stefan Hajnoczi13226292015-05-27 17:16:49 +0100929 if (opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP ||
930 opts->kind == NET_CLIENT_OPTIONS_KIND_NIC ||
931 !net_client_init_fun[opts->kind]) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100932 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
933 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100934 return -1;
935 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200936 } else {
937 u.net = object;
938 opts = u.net->opts;
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200939 if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100940 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
941 "a net type");
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200942 return -1;
943 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200944 /* missing optional values have been initialized to "all bits zero" */
945 name = u.net->has_id ? u.net->id : u.net->name;
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +0100946
947 if (opts->kind == NET_CLIENT_OPTIONS_KIND_NONE) {
948 return 0; /* nothing to do */
949 }
950
951 if (!net_client_init_fun[opts->kind]) {
952 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
953 "a net backend type (maybe it is not compiled "
954 "into this binary)");
955 return -1;
956 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200957 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100958
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100959 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
960 * parameter. */
961 if (!is_netdev &&
962 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
963 !opts->nic->has_netdev)) {
964 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
965 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200966
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100967 if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
968 /* FIXME drop when all init functions store an Error */
969 if (errp && !*errp) {
970 error_setg(errp, QERR_DEVICE_INIT_FAILED,
971 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100972 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100973 return -1;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100974 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200975 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100976}
977
Laszlo Ersek6687b792012-07-17 16:17:13 +0200978
979static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
980{
981 if (is_netdev) {
982 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
983 } else {
984 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
985 }
986}
987
988
989int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
990{
991 void *object = NULL;
992 Error *err = NULL;
993 int ret = -1;
994
995 {
996 OptsVisitor *ov = opts_visitor_new(opts);
997
998 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
999 opts_visitor_cleanup(ov);
1000 }
1001
1002 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +02001003 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001004 }
1005
1006 if (object) {
1007 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
1008
1009 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
1010 qapi_dealloc_visitor_cleanup(dv);
1011 }
1012
1013 error_propagate(errp, err);
1014 return ret;
1015}
1016
1017
aliguori6f338c32009-02-11 15:21:54 +00001018static int net_host_check_device(const char *device)
1019{
1020 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001021 for (i = 0; host_net_devices[i]; i++) {
1022 if (!strncmp(host_net_devices[i], device,
1023 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +00001024 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001025 }
aliguori6f338c32009-02-11 15:21:54 +00001026 }
1027
1028 return 0;
1029}
1030
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001031void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001032{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001033 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001034 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001035 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001036 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001037
aliguori6f338c32009-02-11 15:21:54 +00001038 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001039 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001040 return;
1041 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001042
Markus Armbruster70b94332015-02-13 12:50:26 +01001043 opts = qemu_opts_parse_noisily(qemu_find_opts("net"),
1044 opts_str ? opts_str : "", false);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001045 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001046 return;
1047 }
1048
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001049 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001050
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001051 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001052 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001053 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +00001054 monitor_printf(mon, "adding host network device %s failed\n", device);
1055 }
aliguori6f338c32009-02-11 15:21:54 +00001056}
1057
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001058void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001059{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001060 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001061 int vlan_id = qdict_get_int(qdict, "vlan_id");
1062 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001063
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001064 nc = net_hub_find_client_by_name(vlan_id, device);
1065 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001066 error_report("Host network device '%s' on hub '%d' not found",
1067 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001068 return;
1069 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001070 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001071 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001072 return;
1073 }
Jason Wang64a55d62015-02-02 15:06:37 +08001074
1075 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001076 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001077}
1078
Luiz Capitulino928059a2012-04-18 17:34:15 -03001079void netdev_add(QemuOpts *opts, Error **errp)
1080{
1081 net_client_init(opts, 1, errp);
1082}
1083
Markus Armbruster485febc2015-03-13 17:25:50 +01001084void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001085{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001086 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001087 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001088 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001089
Luiz Capitulino928059a2012-04-18 17:34:15 -03001090 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001091 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001092 goto out;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001093 }
1094
Luiz Capitulino928059a2012-04-18 17:34:15 -03001095 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001096 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001097 goto out;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001098 }
1099
1100 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001101 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001102 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +01001103 goto out;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001104 }
1105
Markus Armbruster485febc2015-03-13 17:25:50 +01001106out:
1107 error_propagate(errp, local_err);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001108}
1109
Luiz Capitulino5f964152012-04-16 14:36:32 -03001110void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001111{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001112 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001113 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001114
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001115 nc = qemu_find_netdev(id);
1116 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001117 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1118 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001119 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001120 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001121
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001122 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1123 if (!opts) {
1124 error_setg(errp, "Device '%s' is not a netdev", id);
1125 return;
1126 }
1127
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001128 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001129 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001130}
1131
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001132void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001133{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001134 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1135 nc->queue_index,
1136 NetClientOptionsKind_lookup[nc->info->type],
1137 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +02001138}
1139
Amos Kongb1be4282013-06-14 15:45:52 +08001140RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1141 Error **errp)
1142{
1143 NetClientState *nc;
1144 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1145
1146 QTAILQ_FOREACH(nc, &net_clients, next) {
1147 RxFilterInfoList *entry;
1148 RxFilterInfo *info;
1149
1150 if (has_name && strcmp(nc->name, name) != 0) {
1151 continue;
1152 }
1153
1154 /* only query rx-filter information of NIC */
1155 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1156 if (has_name) {
1157 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001158 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001159 }
1160 continue;
1161 }
1162
1163 if (nc->info->query_rx_filter) {
1164 info = nc->info->query_rx_filter(nc);
1165 entry = g_malloc0(sizeof(*entry));
1166 entry->value = info;
1167
1168 if (!filter_list) {
1169 filter_list = entry;
1170 } else {
1171 last_entry->next = entry;
1172 }
1173 last_entry = entry;
1174 } else if (has_name) {
1175 error_setg(errp, "net client(%s) doesn't support"
1176 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001177 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001178 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001179
1180 if (has_name) {
1181 break;
1182 }
Amos Kongb1be4282013-06-14 15:45:52 +08001183 }
1184
Markus Armbruster9083da12014-04-24 15:44:18 +02001185 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001186 error_setg(errp, "invalid net client name: %s", name);
1187 }
1188
1189 return filter_list;
1190}
1191
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001192void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001193{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001194 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001195 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001196
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001197 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001198
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001199 QTAILQ_FOREACH(nc, &net_clients, next) {
1200 peer = nc->peer;
1201 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001202
1203 /* Skip if already printed in hub info */
1204 if (net_hub_id_for_client(nc, NULL) == 0) {
1205 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001206 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001207
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001208 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001209 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001210 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001211 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001212 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001213 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001214 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001215 }
aliguori63a01ef2008-10-31 19:10:00 +00001216}
1217
Luiz Capitulino4b371562011-11-23 13:11:55 -02001218void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001219{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001220 NetClientState *ncs[MAX_QUEUE_NUM];
1221 NetClientState *nc;
1222 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001223
Jason Wang1ceef9f2013-01-30 19:12:28 +08001224 queues = qemu_find_net_clients_except(name, ncs,
1225 NET_CLIENT_OPTIONS_KIND_MAX,
1226 MAX_QUEUE_NUM);
1227
1228 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001229 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1230 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001231 return;
aliguori436e5e52009-01-08 19:44:06 +00001232 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001233 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001234
Jason Wang1ceef9f2013-01-30 19:12:28 +08001235 for (i = 0; i < queues; i++) {
1236 ncs[i]->link_down = !up;
1237 }
aliguori436e5e52009-01-08 19:44:06 +00001238
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001239 if (nc->info->link_status_changed) {
1240 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001241 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001242
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001243 if (nc->peer) {
1244 /* Change peer link only if the peer is NIC and then notify peer.
1245 * If the peer is a HUBPORT or a backend, we do not change the
1246 * link status.
1247 *
1248 * This behavior is compatible with qemu vlans where there could be
1249 * multiple clients that can still communicate with each other in
1250 * disconnected mode. For now maintain this compatibility.
1251 */
1252 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1253 for (i = 0; i < queues; i++) {
1254 ncs[i]->peer->link_down = !up;
1255 }
1256 }
1257 if (nc->peer->info->link_status_changed) {
1258 nc->peer->info->link_status_changed(nc->peer);
1259 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001260 }
aliguori436e5e52009-01-08 19:44:06 +00001261}
1262
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001263static void net_vm_change_state_handler(void *opaque, int running,
1264 RunState state)
1265{
1266 /* Complete all queued packets, to guarantee we don't modify
1267 * state later when VM is not running.
1268 */
1269 if (!running) {
1270 NetClientState *nc;
1271 NetClientState *tmp;
1272
1273 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1274 qemu_flush_or_purge_queued_packets(nc, true);
1275 }
1276 }
1277}
1278
aliguori63a01ef2008-10-31 19:10:00 +00001279void net_cleanup(void)
1280{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001281 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001282
Jason Wang1ceef9f2013-01-30 19:12:28 +08001283 /* We may del multiple entries during qemu_del_net_client(),
1284 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1285 */
1286 while (!QTAILQ_EMPTY(&net_clients)) {
1287 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001288 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1289 qemu_del_nic(qemu_get_nic(nc));
1290 } else {
1291 qemu_del_net_client(nc);
1292 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001293 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001294
1295 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001296}
1297
Markus Armbruster668680f2010-02-11 14:44:58 +01001298void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001299{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001300 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001301 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001302
Peter Maydell641f6ea2011-05-20 16:50:00 +01001303 /* Don't warn about the default network setup that you get if
1304 * no command line -net or -netdev options are specified. There
1305 * are two cases that we would otherwise complain about:
1306 * (1) board doesn't support a NIC but the implicit "-net nic"
1307 * requested one
1308 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1309 * sets up a nic that isn't connected to anything.
1310 */
1311 if (default_net) {
1312 return;
1313 }
1314
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001315 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001316
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001317 QTAILQ_FOREACH(nc, &net_clients, next) {
1318 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001319 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001320 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1321 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001322 }
1323 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001324
1325 /* Check that all NICs requested via -net nic actually got created.
1326 * NICs created via -device don't need to be checked here because
1327 * they are always instantiated.
1328 */
1329 for (i = 0; i < MAX_NICS; i++) {
1330 NICInfo *nd = &nd_table[i];
1331 if (nd->used && !nd->instantiated) {
1332 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1333 "was not created (not supported by this machine?)\n",
1334 nd->name ? nd->name : "anonymous",
1335 nd->model ? nd->model : "unspecified");
1336 }
1337 }
aliguori63a01ef2008-10-31 19:10:00 +00001338}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001339
Markus Armbruster28d0de72015-03-13 13:35:14 +01001340static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001341{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001342 Error *local_err = NULL;
1343
1344 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001345 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001346 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001347 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001348 }
1349
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001350 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001351}
1352
Markus Armbruster28d0de72015-03-13 13:35:14 +01001353static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001354{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001355 Error *local_err = NULL;
1356 int ret;
1357
1358 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001359 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001360 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001361 return -1;
1362 }
1363
1364 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001365}
1366
1367int net_init_clients(void)
1368{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001369 QemuOptsList *net = qemu_find_opts("net");
1370
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001371 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001372 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001373 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001374#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001375 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001376#endif
1377 }
1378
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001379 net_change_state_entry =
1380 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1381
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001382 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001383
Markus Armbruster28d0de72015-03-13 13:35:14 +01001384 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1385 net_init_netdev, NULL, NULL)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001386 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001387 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001388
Markus Armbruster28d0de72015-03-13 13:35:14 +01001389 if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001390 return -1;
1391 }
1392
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001393 return 0;
1394}
1395
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001396int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001397{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001398#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001399 int ret;
1400 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001401 return ret;
1402 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001403#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001404
Markus Armbruster70b94332015-02-13 12:50:26 +01001405 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001406 return -1;
1407 }
1408
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001409 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001410 return 0;
1411}
Jason Wang7fc8d912012-03-05 11:08:50 +08001412
1413/* From FreeBSD */
1414/* XXX: optimize */
1415unsigned compute_mcast_idx(const uint8_t *ep)
1416{
1417 uint32_t crc;
1418 int carry, i, j;
1419 uint8_t b;
1420
1421 crc = 0xffffffff;
1422 for (i = 0; i < 6; i++) {
1423 b = *ep++;
1424 for (j = 0; j < 8; j++) {
1425 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1426 crc <<= 1;
1427 b >>= 1;
1428 if (carry) {
1429 crc = ((crc ^ POLYNOMIAL) | carry);
1430 }
1431 }
1432 }
1433 return crc >> 26;
1434}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001435
1436QemuOptsList qemu_netdev_opts = {
1437 .name = "netdev",
1438 .implied_opt_name = "type",
1439 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1440 .desc = {
1441 /*
1442 * no elements => accept any params
1443 * validation will happen later
1444 */
1445 { /* end of list */ }
1446 },
1447};
1448
1449QemuOptsList qemu_net_opts = {
1450 .name = "net",
1451 .implied_opt_name = "type",
1452 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1453 .desc = {
1454 /*
1455 * no elements => accept any params
1456 * validation will happen later
1457 */
1458 { /* end of list */ }
1459 },
1460};