blob: cc36c7b4fe63f48fab07a79939a3b2090cd7ce6f [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
63#ifdef CONFIG_SLIRP
64 "user",
65#endif
66#ifdef CONFIG_VDE
67 "vde",
68#endif
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030069 "vhost-user",
Hani Benhabiles84007e82014-05-27 23:39:33 +010070 NULL,
71};
72
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010073int default_net = 1;
74
aliguori63a01ef2008-10-31 19:10:00 +000075/***********************************************************/
76/* network device redirectors */
77
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000078#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000079static void hex_dump(FILE *f, const uint8_t *buf, int size)
80{
81 int len, i, j, c;
82
83 for(i=0;i<size;i+=16) {
84 len = size - i;
85 if (len > 16)
86 len = 16;
87 fprintf(f, "%08x ", i);
88 for(j=0;j<16;j++) {
89 if (j < len)
90 fprintf(f, " %02x", buf[i+j]);
91 else
92 fprintf(f, " ");
93 }
94 fprintf(f, " ");
95 for(j=0;j<len;j++) {
96 c = buf[i+j];
97 if (c < ' ' || c > '~')
98 c = '.';
99 fprintf(f, "%c", c);
100 }
101 fprintf(f, "\n");
102 }
103}
104#endif
105
aliguori63a01ef2008-10-31 19:10:00 +0000106static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
107{
108 const char *p, *p1;
109 int len;
110 p = *pp;
111 p1 = strchr(p, sep);
112 if (!p1)
113 return -1;
114 len = p1 - p;
115 p1++;
116 if (buf_size > 0) {
117 if (len > buf_size - 1)
118 len = buf_size - 1;
119 memcpy(buf, p, len);
120 buf[len] = '\0';
121 }
122 *pp = p1;
123 return 0;
124}
125
aliguori63a01ef2008-10-31 19:10:00 +0000126int parse_host_port(struct sockaddr_in *saddr, const char *str)
127{
128 char buf[512];
129 struct hostent *he;
130 const char *p, *r;
131 int port;
132
133 p = str;
134 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
135 return -1;
136 saddr->sin_family = AF_INET;
137 if (buf[0] == '\0') {
138 saddr->sin_addr.s_addr = 0;
139 } else {
blueswir1cd390082008-11-16 13:53:32 +0000140 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000141 if (!inet_aton(buf, &saddr->sin_addr))
142 return -1;
143 } else {
144 if ((he = gethostbyname(buf)) == NULL)
145 return - 1;
146 saddr->sin_addr = *(struct in_addr *)he->h_addr;
147 }
148 }
149 port = strtol(p, (char **)&r, 0);
150 if (r == p)
151 return -1;
152 saddr->sin_port = htons(port);
153 return 0;
154}
155
Scott Feldman890ee6a2015-03-13 21:09:25 -0700156char *qemu_mac_strdup_printf(const uint8_t *macaddr)
157{
158 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
159 macaddr[0], macaddr[1], macaddr[2],
160 macaddr[3], macaddr[4], macaddr[5]);
161}
162
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100163void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000164{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100165 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000166 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100167 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000168 macaddr[0], macaddr[1], macaddr[2],
169 macaddr[3], macaddr[4], macaddr[5]);
170}
171
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800172static int mac_table[256] = {0};
173
174static void qemu_macaddr_set_used(MACAddr *macaddr)
175{
176 int index;
177
178 for (index = 0x56; index < 0xFF; index++) {
179 if (macaddr->a[5] == index) {
180 mac_table[index]++;
181 }
182 }
183}
184
185static void qemu_macaddr_set_free(MACAddr *macaddr)
186{
187 int index;
188 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
189
190 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
191 return;
192 }
193 for (index = 0x56; index < 0xFF; index++) {
194 if (macaddr->a[5] == index) {
195 mac_table[index]--;
196 }
197 }
198}
199
200static int qemu_macaddr_get_free(void)
201{
202 int index;
203
204 for (index = 0x56; index < 0xFF; index++) {
205 if (mac_table[index] == 0) {
206 return index;
207 }
208 }
209
210 return -1;
211}
212
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200213void qemu_macaddr_default_if_unset(MACAddr *macaddr)
214{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200215 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800216 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200217
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800218 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
219 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
220 return;
221 } else {
222 qemu_macaddr_set_used(macaddr);
223 return;
224 }
225 }
226
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200227 macaddr->a[0] = 0x52;
228 macaddr->a[1] = 0x54;
229 macaddr->a[2] = 0x00;
230 macaddr->a[3] = 0x12;
231 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800232 macaddr->a[5] = qemu_macaddr_get_free();
233 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200234}
235
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100236/**
237 * Generate a name for net client
238 *
Amos Kongc9635302013-04-15 18:55:19 +0800239 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100240 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100241static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000242{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100243 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000244 int id = 0;
245
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100246 QTAILQ_FOREACH(nc, &net_clients, next) {
247 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100248 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100249 }
Amos Kongc9635302013-04-15 18:55:19 +0800250 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200251 id++;
252 }
253 }
254
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100255 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000256}
257
Jason Wangf7860452013-01-30 19:12:27 +0800258static void qemu_net_client_destructor(NetClientState *nc)
259{
260 g_free(nc);
261}
262
Jason Wang18a15412013-01-30 19:12:26 +0800263static void qemu_net_client_setup(NetClientState *nc,
264 NetClientInfo *info,
265 NetClientState *peer,
266 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800267 const char *name,
268 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000269{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100270 nc->info = info;
271 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000272 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100273 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000274 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100275 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000276 }
aliguori63a01ef2008-10-31 19:10:00 +0000277
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100278 if (peer) {
279 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100280 nc->peer = peer;
281 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100282 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100283 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100284
Jan Kiszka067404b2013-08-02 21:47:08 +0200285 nc->incoming_queue = qemu_new_net_queue(nc);
Jason Wangf7860452013-01-30 19:12:27 +0800286 nc->destructor = destructor;
Jason Wang18a15412013-01-30 19:12:26 +0800287}
288
289NetClientState *qemu_new_net_client(NetClientInfo *info,
290 NetClientState *peer,
291 const char *model,
292 const char *name)
293{
294 NetClientState *nc;
295
296 assert(info->size >= sizeof(NetClientState));
297
298 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800299 qemu_net_client_setup(nc, info, peer, model, name,
300 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800301
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100302 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000303}
304
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000305NICState *qemu_new_nic(NetClientInfo *info,
306 NICConf *conf,
307 const char *model,
308 const char *name,
309 void *opaque)
310{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800311 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000312 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200313 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000314
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200315 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000316 assert(info->size >= sizeof(NICState));
317
Jason Wangf6b26cf2013-02-22 23:15:06 +0800318 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
319 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000320 nic->conf = conf;
321 nic->opaque = opaque;
322
Jason Wangf6b26cf2013-02-22 23:15:06 +0800323 for (i = 0; i < queues; i++) {
324 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800325 NULL);
326 nic->ncs[i].queue_index = i;
327 }
328
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000329 return nic;
330}
331
Jason Wang1ceef9f2013-01-30 19:12:28 +0800332NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
333{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800334 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800335}
336
Jason Wangb356f762013-01-30 19:12:22 +0800337NetClientState *qemu_get_queue(NICState *nic)
338{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800339 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800340}
341
Jason Wangcc1f0f42013-01-30 19:12:23 +0800342NICState *qemu_get_nic(NetClientState *nc)
343{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800344 NetClientState *nc0 = nc - nc->queue_index;
345
Jason Wangf6b26cf2013-02-22 23:15:06 +0800346 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800347}
348
349void *qemu_get_nic_opaque(NetClientState *nc)
350{
351 NICState *nic = qemu_get_nic(nc);
352
353 return nic->opaque;
354}
355
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100356static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000357{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100358 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000359
Andreas Färbercc2a9042013-02-12 23:16:06 +0100360 if (nc->info->cleanup) {
361 nc->info->cleanup(nc);
362 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200363}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100364
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100365static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200366{
Jan Kiszka067404b2013-08-02 21:47:08 +0200367 if (nc->incoming_queue) {
368 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200369 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100370 if (nc->peer) {
371 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100372 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100373 g_free(nc->name);
374 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800375 if (nc->destructor) {
376 nc->destructor(nc);
377 }
aliguori63a01ef2008-10-31 19:10:00 +0000378}
379
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100380void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200381{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800382 NetClientState *ncs[MAX_QUEUE_NUM];
383 int queues, i;
384
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100385 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
386
Jason Wang1ceef9f2013-01-30 19:12:28 +0800387 /* If the NetClientState belongs to a multiqueue backend, we will change all
388 * other NetClientStates also.
389 */
390 queues = qemu_find_net_clients_except(nc->name, ncs,
391 NET_CLIENT_OPTIONS_KIND_NIC,
392 MAX_QUEUE_NUM);
393 assert(queues != 0);
394
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200395 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100396 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800397 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200398 if (nic->peer_deleted) {
399 return;
400 }
401 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800402
403 for (i = 0; i < queues; i++) {
404 ncs[i]->peer->link_down = true;
405 }
406
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100407 if (nc->peer->info->link_status_changed) {
408 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200409 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800410
411 for (i = 0; i < queues; i++) {
412 qemu_cleanup_net_client(ncs[i]);
413 }
414
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200415 return;
416 }
417
Jason Wang1ceef9f2013-01-30 19:12:28 +0800418 for (i = 0; i < queues; i++) {
419 qemu_cleanup_net_client(ncs[i]);
420 qemu_free_net_client(ncs[i]);
421 }
Jason Wang948ecf22013-01-30 19:12:24 +0800422}
423
424void qemu_del_nic(NICState *nic)
425{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200426 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800427
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800428 qemu_macaddr_set_free(&nic->conf->macaddr);
429
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200430 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800431 if (nic->peer_deleted) {
432 for (i = 0; i < queues; i++) {
433 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200434 }
435 }
436
Jason Wang1ceef9f2013-01-30 19:12:28 +0800437 for (i = queues - 1; i >= 0; i--) {
438 NetClientState *nc = qemu_get_subqueue(nic, i);
439
440 qemu_cleanup_net_client(nc);
441 qemu_free_net_client(nc);
442 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800443
444 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200445}
446
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000447void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
448{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100449 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000450
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100451 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200452 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800453 if (nc->queue_index == 0) {
454 func(qemu_get_nic(nc), opaque);
455 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000456 }
457 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000458}
459
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100460bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100461{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100462 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100463 return false;
464 }
465
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100466 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100467}
468
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100469bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100470{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100471 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100472 return false;
473 }
474
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100475 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100476}
477
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100478bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100479{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100480 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100481 return false;
482 }
483
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100484 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100485}
486
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100487void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100488{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100489 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100490 return;
491 }
492
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100493 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100494}
495
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100496void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100497 int ecn, int ufo)
498{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100499 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100500 return;
501 }
502
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100503 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100504}
505
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100506void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100507{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100508 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100509 return;
510 }
511
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100512 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100513}
514
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200515int qemu_set_vnet_le(NetClientState *nc, bool is_le)
516{
517 if (!nc || !nc->info->set_vnet_le) {
518 return -ENOSYS;
519 }
520
521 return nc->info->set_vnet_le(nc, is_le);
522}
523
524int qemu_set_vnet_be(NetClientState *nc, bool is_be)
525{
526 if (!nc || !nc->info->set_vnet_be) {
527 return -ENOSYS;
528 }
529
530 return nc->info->set_vnet_be(nc, is_be);
531}
532
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100533int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000534{
zhanghailiange1d64c02014-08-26 16:06:17 +0800535 int vm_running = runstate_is_running();
536
537 if (!vm_running) {
538 return 0;
539 }
540
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100541 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100542 return 1;
543 }
544
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100545 if (sender->peer->receive_disabled) {
546 return 0;
547 } else if (sender->peer->info->can_receive &&
548 !sender->peer->info->can_receive(sender->peer)) {
549 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000550 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500551 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000552}
553
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100554ssize_t qemu_deliver_packet(NetClientState *sender,
555 unsigned flags,
556 const uint8_t *data,
557 size_t size,
558 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100559{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100560 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000561 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100562
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100563 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100564 return size;
565 }
566
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100567 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000568 return 0;
569 }
570
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100571 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
572 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000573 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100574 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000575 }
576
577 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100578 nc->receive_disabled = 1;
Igor Ryzhovb9259652014-04-16 17:43:07 +0400579 }
Mark McLoughlin893379e2009-10-27 18:16:36 +0000580
581 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100582}
583
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100584void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000585{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100586 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100587 return;
588 }
589
Jan Kiszka067404b2013-08-02 21:47:08 +0200590 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100591}
592
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300593static
594void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100595{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100596 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100597
Luigi Rizzo199ee602013-02-05 17:53:31 +0100598 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
599 if (net_hub_flush(nc->peer)) {
600 qemu_notify_event();
601 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100602 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200603 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200604 /* We emptied the queue successfully, signal to the IO thread to repoll
605 * the file descriptor (for tap, for example).
606 */
607 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300608 } else if (purge) {
609 /* Unable to empty the queue, purge remaining packets */
610 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200611 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100612}
613
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300614void qemu_flush_queued_packets(NetClientState *nc)
615{
616 qemu_flush_or_purge_queued_packets(nc, false);
617}
618
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100619static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100620 unsigned flags,
621 const uint8_t *buf, int size,
622 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000623{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100624 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100625
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100626#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100627 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100628 hex_dump(stdout, buf, size);
629#endif
630
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100631 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100632 return size;
633 }
634
Jan Kiszka067404b2013-08-02 21:47:08 +0200635 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100636
Mark McLoughlinca77d172009-10-22 17:43:41 +0100637 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
638}
639
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100640ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100641 const uint8_t *buf, int size,
642 NetPacketSent *sent_cb)
643{
644 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
645 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100646}
647
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100648void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100649{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100650 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000651}
652
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100653ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100654{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100655 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100656 buf, size, NULL);
657}
658
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100659static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000660 int iovcnt)
661{
Scott Feldmand32fcad2013-03-18 11:43:44 -0700662 uint8_t buffer[NET_BUFSIZE];
Benjamin Poirierce053662011-02-23 19:57:21 -0500663 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000664
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400665 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000666
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100667 return nc->info->receive(nc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000668}
669
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100670ssize_t qemu_deliver_packet_iov(NetClientState *sender,
671 unsigned flags,
672 const struct iovec *iov,
673 int iovcnt,
674 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100675{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100676 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100677 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100678
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100679 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500680 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100681 }
682
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100683 if (nc->receive_disabled) {
684 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100685 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100686
687 if (nc->info->receive_iov) {
688 ret = nc->info->receive_iov(nc, iov, iovcnt);
689 } else {
690 ret = nc_sendv_compat(nc, iov, iovcnt);
691 }
692
693 if (ret == 0) {
694 nc->receive_disabled = 1;
695 }
696
697 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100698}
699
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100700ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100701 const struct iovec *iov, int iovcnt,
702 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000703{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100704 NetQueue *queue;
705
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100706 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500707 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000708 }
709
Jan Kiszka067404b2013-08-02 21:47:08 +0200710 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100711
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100712 return qemu_net_queue_send_iov(queue, sender,
713 QEMU_NET_PACKET_FLAG_NONE,
714 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000715}
716
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100717ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100718qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100719{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100720 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100721}
722
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100723NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000724{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100725 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100726
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100727 QTAILQ_FOREACH(nc, &net_clients, next) {
728 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200729 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100730 if (!strcmp(nc->name, id)) {
731 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100732 }
733 }
734
735 return NULL;
736}
737
Jason Wang6c51ae72013-01-30 19:12:25 +0800738int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
739 NetClientOptionsKind type, int max)
740{
741 NetClientState *nc;
742 int ret = 0;
743
744 QTAILQ_FOREACH(nc, &net_clients, next) {
745 if (nc->info->type == type) {
746 continue;
747 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100748 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800749 if (ret < max) {
750 ncs[ret] = nc;
751 }
752 ret++;
753 }
754 }
755
756 return ret;
757}
758
aliguori76970792009-02-11 15:20:03 +0000759static int nic_get_free_idx(void)
760{
761 int index;
762
763 for (index = 0; index < MAX_NICS; index++)
764 if (!nd_table[index].used)
765 return index;
766 return -1;
767}
768
Markus Armbruster07caea32009-09-25 03:53:51 +0200769int qemu_show_nic_models(const char *arg, const char *const *models)
770{
771 int i;
772
Peter Maydellc8057f92012-08-02 13:45:54 +0100773 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200774 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100775 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200776
777 fprintf(stderr, "qemu: Supported NIC models: ");
778 for (i = 0 ; models[i]; i++)
779 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
780 return 1;
781}
782
aliguorid07f22c2009-01-13 19:03:57 +0000783void qemu_check_nic_model(NICInfo *nd, const char *model)
784{
785 const char *models[2];
786
787 models[0] = model;
788 models[1] = NULL;
789
Markus Armbruster07caea32009-09-25 03:53:51 +0200790 if (qemu_show_nic_models(nd->model, models))
791 exit(0);
792 if (qemu_find_nic_model(nd, models, model) < 0)
793 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000794}
795
Markus Armbruster07caea32009-09-25 03:53:51 +0200796int qemu_find_nic_model(NICInfo *nd, const char * const *models,
797 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000798{
Markus Armbruster07caea32009-09-25 03:53:51 +0200799 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000800
801 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500802 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000803
Markus Armbruster07caea32009-09-25 03:53:51 +0200804 for (i = 0 ; models[i]; i++) {
805 if (strcmp(nd->model, models[i]) == 0)
806 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000807 }
808
Markus Armbruster6daf1942011-06-22 14:03:54 +0200809 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200810 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000811}
812
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200813static int net_init_nic(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200814 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100815{
816 int idx;
817 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200818 const NetLegacyNicOptions *nic;
819
820 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
821 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100822
823 idx = nic_get_free_idx();
824 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200825 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100826 return -1;
827 }
828
829 nd = &nd_table[idx];
830
831 memset(nd, 0, sizeof(*nd));
832
Laszlo Ersek2456f362012-07-17 16:17:14 +0200833 if (nic->has_netdev) {
834 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100835 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200836 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100837 return -1;
838 }
839 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100840 assert(peer);
841 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100842 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100843 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200844 if (nic->has_model) {
845 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100846 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200847 if (nic->has_addr) {
848 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100849 }
850
Laszlo Ersek2456f362012-07-17 16:17:14 +0200851 if (nic->has_macaddr &&
852 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200853 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100854 return -1;
855 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400856 if (nic->has_macaddr &&
857 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200858 error_setg(errp,
859 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400860 return -1;
861 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200862 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100863
Laszlo Ersek2456f362012-07-17 16:17:14 +0200864 if (nic->has_vectors) {
865 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200866 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200867 return -1;
868 }
869 nd->nvectors = nic->vectors;
870 } else {
871 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100872 }
873
874 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100875 nb_nics++;
876
877 return idx;
878}
879
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100880
Laszlo Ersek6687b792012-07-17 16:17:13 +0200881static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200882 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200883 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200884 NetClientState *peer, Error **errp) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100885 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100886#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100887 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100888#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100889 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
890 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100891#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100892 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100893#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100894#ifdef CONFIG_NETMAP
895 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
896#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100897 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100898#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100899 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200900#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100901 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300902#ifdef CONFIG_VHOST_NET_USED
903 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
904#endif
Gonglei015a33b2014-07-01 20:58:08 +0800905#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100906 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
907#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100908};
909
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100910
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200911static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200912{
913 union {
914 const Netdev *netdev;
915 const NetLegacy *net;
916 } u;
917 const NetClientOptions *opts;
918 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100919
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100920 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200921 u.netdev = object;
922 opts = u.netdev->opts;
923 name = u.netdev->id;
924
925 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100926#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200927 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100928#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200929 case NET_CLIENT_OPTIONS_KIND_TAP:
930 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100931#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200932 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100933#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100934#ifdef CONFIG_NETMAP
935 case NET_CLIENT_OPTIONS_KIND_NETMAP:
936#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200937#ifdef CONFIG_NET_BRIDGE
938 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
939#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100940 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300941#ifdef CONFIG_VHOST_NET_USED
942 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
943#endif
Gonglei015a33b2014-07-01 20:58:08 +0800944#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100945 case NET_CLIENT_OPTIONS_KIND_L2TPV3:
946#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200947 break;
948
949 default:
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100950 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
951 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100952 return -1;
953 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200954 } else {
955 u.net = object;
956 opts = u.net->opts;
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200957 if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100958 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
959 "a net type");
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200960 return -1;
961 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200962 /* missing optional values have been initialized to "all bits zero" */
963 name = u.net->has_id ? u.net->id : u.net->name;
964 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100965
Laszlo Ersek6687b792012-07-17 16:17:13 +0200966 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100967 NetClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200968
969 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
970 * parameter. */
971 if (!is_netdev &&
972 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
973 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100974 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100975 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200976
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200977 if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
978 /* FIXME drop when all init functions store an Error */
979 if (errp && !*errp) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100980 error_setg(errp, QERR_DEVICE_INIT_FAILED,
981 NetClientOptionsKind_lookup[opts->kind]);
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200982 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100983 return -1;
984 }
985 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200986 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100987}
988
Laszlo Ersek6687b792012-07-17 16:17:13 +0200989
990static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
991{
992 if (is_netdev) {
993 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
994 } else {
995 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
996 }
997}
998
999
1000int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
1001{
1002 void *object = NULL;
1003 Error *err = NULL;
1004 int ret = -1;
1005
1006 {
1007 OptsVisitor *ov = opts_visitor_new(opts);
1008
1009 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
1010 opts_visitor_cleanup(ov);
1011 }
1012
1013 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +02001014 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001015 }
1016
1017 if (object) {
1018 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
1019
1020 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
1021 qapi_dealloc_visitor_cleanup(dv);
1022 }
1023
1024 error_propagate(errp, err);
1025 return ret;
1026}
1027
1028
aliguori6f338c32009-02-11 15:21:54 +00001029static int net_host_check_device(const char *device)
1030{
1031 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001032 for (i = 0; host_net_devices[i]; i++) {
1033 if (!strncmp(host_net_devices[i], device,
1034 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +00001035 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001036 }
aliguori6f338c32009-02-11 15:21:54 +00001037 }
1038
1039 return 0;
1040}
1041
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001042void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001043{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001044 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001045 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001046 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001047 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001048
aliguori6f338c32009-02-11 15:21:54 +00001049 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001050 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001051 return;
1052 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001053
Markus Armbruster70b94332015-02-13 12:50:26 +01001054 opts = qemu_opts_parse_noisily(qemu_find_opts("net"),
1055 opts_str ? opts_str : "", false);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001056 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001057 return;
1058 }
1059
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001060 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001061
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001062 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001063 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001064 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +00001065 monitor_printf(mon, "adding host network device %s failed\n", device);
1066 }
aliguori6f338c32009-02-11 15:21:54 +00001067}
1068
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001069void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001070{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001071 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001072 int vlan_id = qdict_get_int(qdict, "vlan_id");
1073 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001074
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001075 nc = net_hub_find_client_by_name(vlan_id, device);
1076 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001077 error_report("Host network device '%s' on hub '%d' not found",
1078 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001079 return;
1080 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001081 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001082 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001083 return;
1084 }
Jason Wang64a55d62015-02-02 15:06:37 +08001085
1086 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001087 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001088}
1089
Luiz Capitulino928059a2012-04-18 17:34:15 -03001090void netdev_add(QemuOpts *opts, Error **errp)
1091{
1092 net_client_init(opts, 1, errp);
1093}
1094
Markus Armbruster485febc2015-03-13 17:25:50 +01001095void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001096{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001097 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001098 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001099 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001100
Luiz Capitulino928059a2012-04-18 17:34:15 -03001101 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001102 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001103 goto out;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001104 }
1105
Luiz Capitulino928059a2012-04-18 17:34:15 -03001106 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001107 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001108 goto out;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001109 }
1110
1111 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001112 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001113 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +01001114 goto out;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001115 }
1116
Markus Armbruster485febc2015-03-13 17:25:50 +01001117out:
1118 error_propagate(errp, local_err);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001119}
1120
Luiz Capitulino5f964152012-04-16 14:36:32 -03001121void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001122{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001123 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001124 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001125
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001126 nc = qemu_find_netdev(id);
1127 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001128 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1129 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001130 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001131 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001132
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001133 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1134 if (!opts) {
1135 error_setg(errp, "Device '%s' is not a netdev", id);
1136 return;
1137 }
1138
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001139 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001140 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001141}
1142
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001143void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001144{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001145 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1146 nc->queue_index,
1147 NetClientOptionsKind_lookup[nc->info->type],
1148 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +02001149}
1150
Amos Kongb1be4282013-06-14 15:45:52 +08001151RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1152 Error **errp)
1153{
1154 NetClientState *nc;
1155 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1156
1157 QTAILQ_FOREACH(nc, &net_clients, next) {
1158 RxFilterInfoList *entry;
1159 RxFilterInfo *info;
1160
1161 if (has_name && strcmp(nc->name, name) != 0) {
1162 continue;
1163 }
1164
1165 /* only query rx-filter information of NIC */
1166 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1167 if (has_name) {
1168 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001169 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001170 }
1171 continue;
1172 }
1173
1174 if (nc->info->query_rx_filter) {
1175 info = nc->info->query_rx_filter(nc);
1176 entry = g_malloc0(sizeof(*entry));
1177 entry->value = info;
1178
1179 if (!filter_list) {
1180 filter_list = entry;
1181 } else {
1182 last_entry->next = entry;
1183 }
1184 last_entry = entry;
1185 } else if (has_name) {
1186 error_setg(errp, "net client(%s) doesn't support"
1187 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001188 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001189 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001190
1191 if (has_name) {
1192 break;
1193 }
Amos Kongb1be4282013-06-14 15:45:52 +08001194 }
1195
Markus Armbruster9083da12014-04-24 15:44:18 +02001196 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001197 error_setg(errp, "invalid net client name: %s", name);
1198 }
1199
1200 return filter_list;
1201}
1202
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001203void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001204{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001205 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001206 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001207
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001208 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001209
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001210 QTAILQ_FOREACH(nc, &net_clients, next) {
1211 peer = nc->peer;
1212 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001213
1214 /* Skip if already printed in hub info */
1215 if (net_hub_id_for_client(nc, NULL) == 0) {
1216 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001217 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001218
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001219 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001220 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001221 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001222 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001223 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001224 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001225 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001226 }
aliguori63a01ef2008-10-31 19:10:00 +00001227}
1228
Luiz Capitulino4b371562011-11-23 13:11:55 -02001229void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001230{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001231 NetClientState *ncs[MAX_QUEUE_NUM];
1232 NetClientState *nc;
1233 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001234
Jason Wang1ceef9f2013-01-30 19:12:28 +08001235 queues = qemu_find_net_clients_except(name, ncs,
1236 NET_CLIENT_OPTIONS_KIND_MAX,
1237 MAX_QUEUE_NUM);
1238
1239 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001240 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1241 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001242 return;
aliguori436e5e52009-01-08 19:44:06 +00001243 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001244 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001245
Jason Wang1ceef9f2013-01-30 19:12:28 +08001246 for (i = 0; i < queues; i++) {
1247 ncs[i]->link_down = !up;
1248 }
aliguori436e5e52009-01-08 19:44:06 +00001249
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001250 if (nc->info->link_status_changed) {
1251 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001252 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001253
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001254 if (nc->peer) {
1255 /* Change peer link only if the peer is NIC and then notify peer.
1256 * If the peer is a HUBPORT or a backend, we do not change the
1257 * link status.
1258 *
1259 * This behavior is compatible with qemu vlans where there could be
1260 * multiple clients that can still communicate with each other in
1261 * disconnected mode. For now maintain this compatibility.
1262 */
1263 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1264 for (i = 0; i < queues; i++) {
1265 ncs[i]->peer->link_down = !up;
1266 }
1267 }
1268 if (nc->peer->info->link_status_changed) {
1269 nc->peer->info->link_status_changed(nc->peer);
1270 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001271 }
aliguori436e5e52009-01-08 19:44:06 +00001272}
1273
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001274static void net_vm_change_state_handler(void *opaque, int running,
1275 RunState state)
1276{
1277 /* Complete all queued packets, to guarantee we don't modify
1278 * state later when VM is not running.
1279 */
1280 if (!running) {
1281 NetClientState *nc;
1282 NetClientState *tmp;
1283
1284 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1285 qemu_flush_or_purge_queued_packets(nc, true);
1286 }
1287 }
1288}
1289
aliguori63a01ef2008-10-31 19:10:00 +00001290void net_cleanup(void)
1291{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001292 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001293
Jason Wang1ceef9f2013-01-30 19:12:28 +08001294 /* We may del multiple entries during qemu_del_net_client(),
1295 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1296 */
1297 while (!QTAILQ_EMPTY(&net_clients)) {
1298 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001299 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1300 qemu_del_nic(qemu_get_nic(nc));
1301 } else {
1302 qemu_del_net_client(nc);
1303 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001304 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001305
1306 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001307}
1308
Markus Armbruster668680f2010-02-11 14:44:58 +01001309void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001310{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001311 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001312 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001313
Peter Maydell641f6ea2011-05-20 16:50:00 +01001314 /* Don't warn about the default network setup that you get if
1315 * no command line -net or -netdev options are specified. There
1316 * are two cases that we would otherwise complain about:
1317 * (1) board doesn't support a NIC but the implicit "-net nic"
1318 * requested one
1319 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1320 * sets up a nic that isn't connected to anything.
1321 */
1322 if (default_net) {
1323 return;
1324 }
1325
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001326 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001327
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001328 QTAILQ_FOREACH(nc, &net_clients, next) {
1329 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001330 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001331 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1332 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001333 }
1334 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001335
1336 /* Check that all NICs requested via -net nic actually got created.
1337 * NICs created via -device don't need to be checked here because
1338 * they are always instantiated.
1339 */
1340 for (i = 0; i < MAX_NICS; i++) {
1341 NICInfo *nd = &nd_table[i];
1342 if (nd->used && !nd->instantiated) {
1343 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1344 "was not created (not supported by this machine?)\n",
1345 nd->name ? nd->name : "anonymous",
1346 nd->model ? nd->model : "unspecified");
1347 }
1348 }
aliguori63a01ef2008-10-31 19:10:00 +00001349}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001350
Markus Armbruster28d0de72015-03-13 13:35:14 +01001351static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001352{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001353 Error *local_err = NULL;
1354
1355 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001356 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001357 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001358 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001359 }
1360
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001361 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001362}
1363
Markus Armbruster28d0de72015-03-13 13:35:14 +01001364static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001365{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001366 Error *local_err = NULL;
1367 int ret;
1368
1369 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001370 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001371 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001372 return -1;
1373 }
1374
1375 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001376}
1377
1378int net_init_clients(void)
1379{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001380 QemuOptsList *net = qemu_find_opts("net");
1381
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001382 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001383 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001384 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001385#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001386 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001387#endif
1388 }
1389
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001390 net_change_state_entry =
1391 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1392
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001393 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001394
Markus Armbruster28d0de72015-03-13 13:35:14 +01001395 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1396 net_init_netdev, NULL, NULL)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001397 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001398 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001399
Markus Armbruster28d0de72015-03-13 13:35:14 +01001400 if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001401 return -1;
1402 }
1403
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001404 return 0;
1405}
1406
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001407int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001408{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001409#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001410 int ret;
1411 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001412 return ret;
1413 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001414#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001415
Markus Armbruster70b94332015-02-13 12:50:26 +01001416 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001417 return -1;
1418 }
1419
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001420 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001421 return 0;
1422}
Jason Wang7fc8d912012-03-05 11:08:50 +08001423
1424/* From FreeBSD */
1425/* XXX: optimize */
1426unsigned compute_mcast_idx(const uint8_t *ep)
1427{
1428 uint32_t crc;
1429 int carry, i, j;
1430 uint8_t b;
1431
1432 crc = 0xffffffff;
1433 for (i = 0; i < 6; i++) {
1434 b = *ep++;
1435 for (j = 0; j < 8; j++) {
1436 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1437 crc <<= 1;
1438 b >>= 1;
1439 if (carry) {
1440 crc = ((crc ^ POLYNOMIAL) | carry);
1441 }
1442 }
1443 }
1444 return crc >> 26;
1445}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001446
1447QemuOptsList qemu_netdev_opts = {
1448 .name = "netdev",
1449 .implied_opt_name = "type",
1450 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1451 .desc = {
1452 /*
1453 * no elements => accept any params
1454 * validation will happen later
1455 */
1456 { /* end of list */ }
1457 },
1458};
1459
1460QemuOptsList qemu_net_opts = {
1461 .name = "net",
1462 .implied_opt_name = "type",
1463 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1464 .desc = {
1465 /*
1466 * no elements => accept any params
1467 * validation will happen later
1468 */
1469 { /* end of list */ }
1470 },
1471};