blob: 55ce154a0e578514a964b84f72366cefafc4d74e [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"
Yang Hongyangfdccce42015-10-07 11:52:14 +080047#include "net/filter.h"
zhanghailiangaa9156f2016-01-26 14:43:33 +080048#include "qapi/string-output-visitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000049
Stefan Weil2944e4e2012-02-04 09:24:46 +010050/* Net bridge is currently not supported for W32. */
51#if !defined(_WIN32)
52# define CONFIG_NET_BRIDGE
53#endif
54
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030055static VMChangeStateEntry *net_change_state_entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010056static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000057
Hani Benhabiles84007e82014-05-27 23:39:33 +010058const char *host_net_devices[] = {
59 "tap",
60 "socket",
61 "dump",
62#ifdef CONFIG_NET_BRIDGE
63 "bridge",
64#endif
Stefan Hajnoczi027a2472015-05-27 17:16:48 +010065#ifdef CONFIG_NETMAP
66 "netmap",
67#endif
Hani Benhabiles84007e82014-05-27 23:39:33 +010068#ifdef CONFIG_SLIRP
69 "user",
70#endif
71#ifdef CONFIG_VDE
72 "vde",
73#endif
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030074 "vhost-user",
Hani Benhabiles84007e82014-05-27 23:39:33 +010075 NULL,
76};
77
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010078int default_net = 1;
79
aliguori63a01ef2008-10-31 19:10:00 +000080/***********************************************************/
81/* network device redirectors */
82
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000083#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000084static void hex_dump(FILE *f, const uint8_t *buf, int size)
85{
86 int len, i, j, c;
87
88 for(i=0;i<size;i+=16) {
89 len = size - i;
90 if (len > 16)
91 len = 16;
92 fprintf(f, "%08x ", i);
93 for(j=0;j<16;j++) {
94 if (j < len)
95 fprintf(f, " %02x", buf[i+j]);
96 else
97 fprintf(f, " ");
98 }
99 fprintf(f, " ");
100 for(j=0;j<len;j++) {
101 c = buf[i+j];
102 if (c < ' ' || c > '~')
103 c = '.';
104 fprintf(f, "%c", c);
105 }
106 fprintf(f, "\n");
107 }
108}
109#endif
110
aliguori63a01ef2008-10-31 19:10:00 +0000111static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
112{
113 const char *p, *p1;
114 int len;
115 p = *pp;
116 p1 = strchr(p, sep);
117 if (!p1)
118 return -1;
119 len = p1 - p;
120 p1++;
121 if (buf_size > 0) {
122 if (len > buf_size - 1)
123 len = buf_size - 1;
124 memcpy(buf, p, len);
125 buf[len] = '\0';
126 }
127 *pp = p1;
128 return 0;
129}
130
aliguori63a01ef2008-10-31 19:10:00 +0000131int parse_host_port(struct sockaddr_in *saddr, const char *str)
132{
133 char buf[512];
134 struct hostent *he;
135 const char *p, *r;
136 int port;
137
138 p = str;
139 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
140 return -1;
141 saddr->sin_family = AF_INET;
142 if (buf[0] == '\0') {
143 saddr->sin_addr.s_addr = 0;
144 } else {
blueswir1cd390082008-11-16 13:53:32 +0000145 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000146 if (!inet_aton(buf, &saddr->sin_addr))
147 return -1;
148 } else {
149 if ((he = gethostbyname(buf)) == NULL)
150 return - 1;
151 saddr->sin_addr = *(struct in_addr *)he->h_addr;
152 }
153 }
154 port = strtol(p, (char **)&r, 0);
155 if (r == p)
156 return -1;
157 saddr->sin_port = htons(port);
158 return 0;
159}
160
Scott Feldman890ee6a2015-03-13 21:09:25 -0700161char *qemu_mac_strdup_printf(const uint8_t *macaddr)
162{
163 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
164 macaddr[0], macaddr[1], macaddr[2],
165 macaddr[3], macaddr[4], macaddr[5]);
166}
167
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100168void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000169{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100170 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000171 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100172 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000173 macaddr[0], macaddr[1], macaddr[2],
174 macaddr[3], macaddr[4], macaddr[5]);
175}
176
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800177static int mac_table[256] = {0};
178
179static void qemu_macaddr_set_used(MACAddr *macaddr)
180{
181 int index;
182
183 for (index = 0x56; index < 0xFF; index++) {
184 if (macaddr->a[5] == index) {
185 mac_table[index]++;
186 }
187 }
188}
189
190static void qemu_macaddr_set_free(MACAddr *macaddr)
191{
192 int index;
193 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
194
195 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
196 return;
197 }
198 for (index = 0x56; index < 0xFF; index++) {
199 if (macaddr->a[5] == index) {
200 mac_table[index]--;
201 }
202 }
203}
204
205static int qemu_macaddr_get_free(void)
206{
207 int index;
208
209 for (index = 0x56; index < 0xFF; index++) {
210 if (mac_table[index] == 0) {
211 return index;
212 }
213 }
214
215 return -1;
216}
217
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200218void qemu_macaddr_default_if_unset(MACAddr *macaddr)
219{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200220 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800221 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200222
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800223 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
224 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
225 return;
226 } else {
227 qemu_macaddr_set_used(macaddr);
228 return;
229 }
230 }
231
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200232 macaddr->a[0] = 0x52;
233 macaddr->a[1] = 0x54;
234 macaddr->a[2] = 0x00;
235 macaddr->a[3] = 0x12;
236 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800237 macaddr->a[5] = qemu_macaddr_get_free();
238 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200239}
240
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100241/**
242 * Generate a name for net client
243 *
Amos Kongc9635302013-04-15 18:55:19 +0800244 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100245 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100246static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000247{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100248 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000249 int id = 0;
250
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100251 QTAILQ_FOREACH(nc, &net_clients, next) {
252 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100253 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100254 }
Amos Kongc9635302013-04-15 18:55:19 +0800255 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200256 id++;
257 }
258 }
259
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100260 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000261}
262
Jason Wangf7860452013-01-30 19:12:27 +0800263static void qemu_net_client_destructor(NetClientState *nc)
264{
265 g_free(nc);
266}
267
Jason Wang18a15412013-01-30 19:12:26 +0800268static void qemu_net_client_setup(NetClientState *nc,
269 NetClientInfo *info,
270 NetClientState *peer,
271 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800272 const char *name,
273 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000274{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100275 nc->info = info;
276 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000277 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100278 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000279 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100280 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000281 }
aliguori63a01ef2008-10-31 19:10:00 +0000282
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100283 if (peer) {
284 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100285 nc->peer = peer;
286 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100287 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100288 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100289
Yang Hongyang3e033a42015-10-07 11:52:17 +0800290 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
Jason Wangf7860452013-01-30 19:12:27 +0800291 nc->destructor = destructor;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800292 QTAILQ_INIT(&nc->filters);
Jason Wang18a15412013-01-30 19:12:26 +0800293}
294
295NetClientState *qemu_new_net_client(NetClientInfo *info,
296 NetClientState *peer,
297 const char *model,
298 const char *name)
299{
300 NetClientState *nc;
301
302 assert(info->size >= sizeof(NetClientState));
303
304 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800305 qemu_net_client_setup(nc, info, peer, model, name,
306 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800307
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100308 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000309}
310
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000311NICState *qemu_new_nic(NetClientInfo *info,
312 NICConf *conf,
313 const char *model,
314 const char *name,
315 void *opaque)
316{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800317 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000318 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200319 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000320
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200321 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000322 assert(info->size >= sizeof(NICState));
323
Jason Wangf6b26cf2013-02-22 23:15:06 +0800324 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
325 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000326 nic->conf = conf;
327 nic->opaque = opaque;
328
Jason Wangf6b26cf2013-02-22 23:15:06 +0800329 for (i = 0; i < queues; i++) {
330 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800331 NULL);
332 nic->ncs[i].queue_index = i;
333 }
334
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000335 return nic;
336}
337
Jason Wang1ceef9f2013-01-30 19:12:28 +0800338NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
339{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800340 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800341}
342
Jason Wangb356f762013-01-30 19:12:22 +0800343NetClientState *qemu_get_queue(NICState *nic)
344{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800345 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800346}
347
Jason Wangcc1f0f42013-01-30 19:12:23 +0800348NICState *qemu_get_nic(NetClientState *nc)
349{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800350 NetClientState *nc0 = nc - nc->queue_index;
351
Jason Wangf6b26cf2013-02-22 23:15:06 +0800352 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800353}
354
355void *qemu_get_nic_opaque(NetClientState *nc)
356{
357 NICState *nic = qemu_get_nic(nc);
358
359 return nic->opaque;
360}
361
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100362static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000363{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100364 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000365
Andreas Färbercc2a9042013-02-12 23:16:06 +0100366 if (nc->info->cleanup) {
367 nc->info->cleanup(nc);
368 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200369}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100370
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100371static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200372{
Jan Kiszka067404b2013-08-02 21:47:08 +0200373 if (nc->incoming_queue) {
374 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200375 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100376 if (nc->peer) {
377 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100378 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100379 g_free(nc->name);
380 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800381 if (nc->destructor) {
382 nc->destructor(nc);
383 }
aliguori63a01ef2008-10-31 19:10:00 +0000384}
385
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100386void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200387{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800388 NetClientState *ncs[MAX_QUEUE_NUM];
389 int queues, i;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800390 NetFilterState *nf, *next;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800391
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100392 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
393
Jason Wang1ceef9f2013-01-30 19:12:28 +0800394 /* If the NetClientState belongs to a multiqueue backend, we will change all
395 * other NetClientStates also.
396 */
397 queues = qemu_find_net_clients_except(nc->name, ncs,
398 NET_CLIENT_OPTIONS_KIND_NIC,
399 MAX_QUEUE_NUM);
400 assert(queues != 0);
401
Yang Hongyangfdccce42015-10-07 11:52:14 +0800402 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
403 object_unparent(OBJECT(nf));
404 }
405
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200406 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100407 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800408 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200409 if (nic->peer_deleted) {
410 return;
411 }
412 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800413
414 for (i = 0; i < queues; i++) {
415 ncs[i]->peer->link_down = true;
416 }
417
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100418 if (nc->peer->info->link_status_changed) {
419 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200420 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800421
422 for (i = 0; i < queues; i++) {
423 qemu_cleanup_net_client(ncs[i]);
424 }
425
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200426 return;
427 }
428
Jason Wang1ceef9f2013-01-30 19:12:28 +0800429 for (i = 0; i < queues; i++) {
430 qemu_cleanup_net_client(ncs[i]);
431 qemu_free_net_client(ncs[i]);
432 }
Jason Wang948ecf22013-01-30 19:12:24 +0800433}
434
435void qemu_del_nic(NICState *nic)
436{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200437 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800438
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800439 qemu_macaddr_set_free(&nic->conf->macaddr);
440
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200441 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800442 if (nic->peer_deleted) {
443 for (i = 0; i < queues; i++) {
444 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200445 }
446 }
447
Jason Wang1ceef9f2013-01-30 19:12:28 +0800448 for (i = queues - 1; i >= 0; i--) {
449 NetClientState *nc = qemu_get_subqueue(nic, i);
450
451 qemu_cleanup_net_client(nc);
452 qemu_free_net_client(nc);
453 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800454
455 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200456}
457
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000458void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
459{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100460 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000461
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100462 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200463 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800464 if (nc->queue_index == 0) {
465 func(qemu_get_nic(nc), opaque);
466 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000467 }
468 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000469}
470
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100471bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100472{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100473 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100474 return false;
475 }
476
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100477 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100478}
479
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100480bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100481{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100482 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100483 return false;
484 }
485
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100486 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100487}
488
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100489bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100490{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100491 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100492 return false;
493 }
494
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100495 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100496}
497
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100498void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100499{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100500 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100501 return;
502 }
503
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100504 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100505}
506
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100507void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100508 int ecn, int ufo)
509{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100510 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100511 return;
512 }
513
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100514 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100515}
516
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100517void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100518{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100519 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100520 return;
521 }
522
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100523 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100524}
525
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200526int qemu_set_vnet_le(NetClientState *nc, bool is_le)
527{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300528#ifdef HOST_WORDS_BIGENDIAN
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200529 if (!nc || !nc->info->set_vnet_le) {
530 return -ENOSYS;
531 }
532
533 return nc->info->set_vnet_le(nc, is_le);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300534#else
535 return 0;
536#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200537}
538
539int qemu_set_vnet_be(NetClientState *nc, bool is_be)
540{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300541#ifdef HOST_WORDS_BIGENDIAN
542 return 0;
543#else
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200544 if (!nc || !nc->info->set_vnet_be) {
545 return -ENOSYS;
546 }
547
548 return nc->info->set_vnet_be(nc, is_be);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300549#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200550}
551
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100552int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000553{
zhanghailiange1d64c02014-08-26 16:06:17 +0800554 int vm_running = runstate_is_running();
555
556 if (!vm_running) {
557 return 0;
558 }
559
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100560 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100561 return 1;
562 }
563
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100564 if (sender->peer->receive_disabled) {
565 return 0;
566 } else if (sender->peer->info->can_receive &&
567 !sender->peer->info->can_receive(sender->peer)) {
568 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000569 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500570 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000571}
572
Yang Hongyange64c7702015-10-07 11:52:15 +0800573static ssize_t filter_receive_iov(NetClientState *nc,
574 NetFilterDirection direction,
575 NetClientState *sender,
576 unsigned flags,
577 const struct iovec *iov,
578 int iovcnt,
579 NetPacketSent *sent_cb)
580{
581 ssize_t ret = 0;
582 NetFilterState *nf = NULL;
583
Li Zhijian25aaadf2016-01-26 13:00:22 +0800584 if (direction == NET_FILTER_DIRECTION_TX) {
585 QTAILQ_FOREACH(nf, &nc->filters, next) {
586 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
587 iovcnt, sent_cb);
588 if (ret) {
589 return ret;
590 }
591 }
592 } else {
593 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, NetFilterHead, next) {
594 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
595 iovcnt, sent_cb);
596 if (ret) {
597 return ret;
598 }
Yang Hongyange64c7702015-10-07 11:52:15 +0800599 }
600 }
601
602 return ret;
603}
604
605static ssize_t filter_receive(NetClientState *nc,
606 NetFilterDirection direction,
607 NetClientState *sender,
608 unsigned flags,
609 const uint8_t *data,
610 size_t size,
611 NetPacketSent *sent_cb)
612{
613 struct iovec iov = {
614 .iov_base = (void *)data,
615 .iov_len = size
616 };
617
618 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
619}
620
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100621void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000622{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100623 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100624 return;
625 }
626
Jan Kiszka067404b2013-08-02 21:47:08 +0200627 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100628}
629
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300630static
631void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100632{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100633 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100634
Luigi Rizzo199ee602013-02-05 17:53:31 +0100635 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
636 if (net_hub_flush(nc->peer)) {
637 qemu_notify_event();
638 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100639 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200640 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200641 /* We emptied the queue successfully, signal to the IO thread to repoll
642 * the file descriptor (for tap, for example).
643 */
644 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300645 } else if (purge) {
646 /* Unable to empty the queue, purge remaining packets */
647 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200648 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100649}
650
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300651void qemu_flush_queued_packets(NetClientState *nc)
652{
653 qemu_flush_or_purge_queued_packets(nc, false);
654}
655
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100656static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100657 unsigned flags,
658 const uint8_t *buf, int size,
659 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000660{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100661 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800662 int ret;
Mark McLoughline94667b2009-04-29 11:48:12 +0100663
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100664#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100665 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100666 hex_dump(stdout, buf, size);
667#endif
668
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100669 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100670 return size;
671 }
672
Yang Hongyange64c7702015-10-07 11:52:15 +0800673 /* Let filters handle the packet first */
674 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
675 sender, flags, buf, size, sent_cb);
676 if (ret) {
677 return ret;
678 }
679
680 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
681 sender, flags, buf, size, sent_cb);
682 if (ret) {
683 return ret;
684 }
685
Jan Kiszka067404b2013-08-02 21:47:08 +0200686 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100687
Mark McLoughlinca77d172009-10-22 17:43:41 +0100688 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
689}
690
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100691ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100692 const uint8_t *buf, int size,
693 NetPacketSent *sent_cb)
694{
695 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
696 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100697}
698
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100699void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100700{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100701 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000702}
703
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100704ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100705{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100706 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100707 buf, size, NULL);
708}
709
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100710static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800711 int iovcnt, unsigned flags)
aliguorifbe78f42008-12-17 19:13:11 +0000712{
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800713 uint8_t buf[NET_BUFSIZE];
714 uint8_t *buffer;
Benjamin Poirierce053662011-02-23 19:57:21 -0500715 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000716
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800717 if (iovcnt == 1) {
718 buffer = iov[0].iov_base;
719 offset = iov[0].iov_len;
720 } else {
721 buffer = buf;
Yang Hongyangedc98142015-10-20 09:51:25 +0800722 offset = iov_to_buf(iov, iovcnt, 0, buf, sizeof(buf));
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800723 }
aliguorifbe78f42008-12-17 19:13:11 +0000724
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800725 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
726 return nc->info->receive_raw(nc, buffer, offset);
727 } else {
728 return nc->info->receive(nc, buffer, offset);
729 }
aliguorifbe78f42008-12-17 19:13:11 +0000730}
731
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100732ssize_t qemu_deliver_packet_iov(NetClientState *sender,
733 unsigned flags,
734 const struct iovec *iov,
735 int iovcnt,
736 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100737{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100738 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100739 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100740
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100741 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500742 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100743 }
744
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100745 if (nc->receive_disabled) {
746 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100747 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100748
749 if (nc->info->receive_iov) {
750 ret = nc->info->receive_iov(nc, iov, iovcnt);
751 } else {
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800752 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100753 }
754
755 if (ret == 0) {
756 nc->receive_disabled = 1;
757 }
758
759 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100760}
761
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100762ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100763 const struct iovec *iov, int iovcnt,
764 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000765{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100766 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800767 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100768
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100769 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500770 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000771 }
772
Yang Hongyange64c7702015-10-07 11:52:15 +0800773 /* Let filters handle the packet first */
774 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
775 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
776 if (ret) {
777 return ret;
778 }
779
780 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
781 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
782 if (ret) {
783 return ret;
784 }
785
Jan Kiszka067404b2013-08-02 21:47:08 +0200786 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100787
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100788 return qemu_net_queue_send_iov(queue, sender,
789 QEMU_NET_PACKET_FLAG_NONE,
790 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000791}
792
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100793ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100794qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100795{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100796 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100797}
798
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100799NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000800{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100801 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100802
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100803 QTAILQ_FOREACH(nc, &net_clients, next) {
804 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200805 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100806 if (!strcmp(nc->name, id)) {
807 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100808 }
809 }
810
811 return NULL;
812}
813
Jason Wang6c51ae72013-01-30 19:12:25 +0800814int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
815 NetClientOptionsKind type, int max)
816{
817 NetClientState *nc;
818 int ret = 0;
819
820 QTAILQ_FOREACH(nc, &net_clients, next) {
821 if (nc->info->type == type) {
822 continue;
823 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100824 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800825 if (ret < max) {
826 ncs[ret] = nc;
827 }
828 ret++;
829 }
830 }
831
832 return ret;
833}
834
aliguori76970792009-02-11 15:20:03 +0000835static int nic_get_free_idx(void)
836{
837 int index;
838
839 for (index = 0; index < MAX_NICS; index++)
840 if (!nd_table[index].used)
841 return index;
842 return -1;
843}
844
Markus Armbruster07caea32009-09-25 03:53:51 +0200845int qemu_show_nic_models(const char *arg, const char *const *models)
846{
847 int i;
848
Peter Maydellc8057f92012-08-02 13:45:54 +0100849 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200850 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100851 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200852
853 fprintf(stderr, "qemu: Supported NIC models: ");
854 for (i = 0 ; models[i]; i++)
855 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
856 return 1;
857}
858
aliguorid07f22c2009-01-13 19:03:57 +0000859void qemu_check_nic_model(NICInfo *nd, const char *model)
860{
861 const char *models[2];
862
863 models[0] = model;
864 models[1] = NULL;
865
Markus Armbruster07caea32009-09-25 03:53:51 +0200866 if (qemu_show_nic_models(nd->model, models))
867 exit(0);
868 if (qemu_find_nic_model(nd, models, model) < 0)
869 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000870}
871
Markus Armbruster07caea32009-09-25 03:53:51 +0200872int qemu_find_nic_model(NICInfo *nd, const char * const *models,
873 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000874{
Markus Armbruster07caea32009-09-25 03:53:51 +0200875 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000876
877 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500878 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000879
Markus Armbruster07caea32009-09-25 03:53:51 +0200880 for (i = 0 ; models[i]; i++) {
881 if (strcmp(nd->model, models[i]) == 0)
882 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000883 }
884
Markus Armbruster6daf1942011-06-22 14:03:54 +0200885 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200886 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000887}
888
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200889static int net_init_nic(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200890 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100891{
892 int idx;
893 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200894 const NetLegacyNicOptions *nic;
895
Eric Blake8d0bcba2015-10-26 16:34:56 -0600896 assert(opts->type == NET_CLIENT_OPTIONS_KIND_NIC);
897 nic = opts->u.nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100898
899 idx = nic_get_free_idx();
900 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200901 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100902 return -1;
903 }
904
905 nd = &nd_table[idx];
906
907 memset(nd, 0, sizeof(*nd));
908
Laszlo Ersek2456f362012-07-17 16:17:14 +0200909 if (nic->has_netdev) {
910 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100911 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200912 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100913 return -1;
914 }
915 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100916 assert(peer);
917 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100918 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100919 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200920 if (nic->has_model) {
921 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100922 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200923 if (nic->has_addr) {
924 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100925 }
926
Laszlo Ersek2456f362012-07-17 16:17:14 +0200927 if (nic->has_macaddr &&
928 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200929 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100930 return -1;
931 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400932 if (nic->has_macaddr &&
933 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200934 error_setg(errp,
935 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400936 return -1;
937 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200938 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100939
Laszlo Ersek2456f362012-07-17 16:17:14 +0200940 if (nic->has_vectors) {
941 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200942 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200943 return -1;
944 }
945 nd->nvectors = nic->vectors;
946 } else {
947 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100948 }
949
950 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100951 nb_nics++;
952
953 return idx;
954}
955
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100956
Eric Blake7fb1cf12015-11-18 01:52:57 -0700957static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND__MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200958 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200959 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200960 NetClientState *peer, Error **errp) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100961 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100962#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100963 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100964#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100965 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
966 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100967#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100968 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100969#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100970#ifdef CONFIG_NETMAP
971 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
972#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100973 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100974#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100975 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200976#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100977 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300978#ifdef CONFIG_VHOST_NET_USED
979 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
980#endif
Gonglei015a33b2014-07-01 20:58:08 +0800981#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100982 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
983#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100984};
985
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100986
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200987static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200988{
Laszlo Ersek6687b792012-07-17 16:17:13 +0200989 const NetClientOptions *opts;
990 const char *name;
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100991 NetClientState *peer = NULL;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100992
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100993 if (is_netdev) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +0100994 const Netdev *netdev = object;
995 opts = netdev->opts;
996 name = netdev->id;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200997
Eric Blake8d0bcba2015-10-26 16:34:56 -0600998 if (opts->type == NET_CLIENT_OPTIONS_KIND_DUMP ||
999 opts->type == NET_CLIENT_OPTIONS_KIND_NIC ||
1000 !net_client_init_fun[opts->type]) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001001 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1002 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001003 return -1;
1004 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001005 } else {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001006 const NetLegacy *net = object;
1007 opts = net->opts;
1008 /* missing optional values have been initialized to "all bits zero" */
1009 name = net->has_id ? net->id : net->name;
1010
Eric Blake8d0bcba2015-10-26 16:34:56 -06001011 if (opts->type == NET_CLIENT_OPTIONS_KIND_NONE) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001012 return 0; /* nothing to do */
1013 }
Eric Blake8d0bcba2015-10-26 16:34:56 -06001014 if (opts->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001015 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1016 "a net type");
Markus Armbrusterca7eb182015-05-15 13:58:49 +02001017 return -1;
1018 }
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +01001019
Eric Blake8d0bcba2015-10-26 16:34:56 -06001020 if (!net_client_init_fun[opts->type]) {
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +01001021 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1022 "a net backend type (maybe it is not compiled "
1023 "into this binary)");
1024 return -1;
1025 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001026
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001027 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
Eric Blake8d0bcba2015-10-26 16:34:56 -06001028 if (opts->type != NET_CLIENT_OPTIONS_KIND_NIC ||
1029 !opts->u.nic->has_netdev) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001030 peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL);
1031 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001032 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001033
Eric Blake8d0bcba2015-10-26 16:34:56 -06001034 if (net_client_init_fun[opts->type](opts, name, peer, errp) < 0) {
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001035 /* FIXME drop when all init functions store an Error */
1036 if (errp && !*errp) {
1037 error_setg(errp, QERR_DEVICE_INIT_FAILED,
Eric Blake8d0bcba2015-10-26 16:34:56 -06001038 NetClientOptionsKind_lookup[opts->type]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001039 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001040 return -1;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001041 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001042 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001043}
1044
Laszlo Ersek6687b792012-07-17 16:17:13 +02001045
1046static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
1047{
1048 if (is_netdev) {
1049 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
1050 } else {
1051 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
1052 }
1053}
1054
1055
1056int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
1057{
1058 void *object = NULL;
1059 Error *err = NULL;
1060 int ret = -1;
1061
1062 {
1063 OptsVisitor *ov = opts_visitor_new(opts);
1064
1065 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
1066 opts_visitor_cleanup(ov);
1067 }
1068
1069 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +02001070 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001071 }
1072
1073 if (object) {
1074 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
1075
1076 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
1077 qapi_dealloc_visitor_cleanup(dv);
1078 }
1079
1080 error_propagate(errp, err);
1081 return ret;
1082}
1083
1084
aliguori6f338c32009-02-11 15:21:54 +00001085static int net_host_check_device(const char *device)
1086{
1087 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001088 for (i = 0; host_net_devices[i]; i++) {
1089 if (!strncmp(host_net_devices[i], device,
1090 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +00001091 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001092 }
aliguori6f338c32009-02-11 15:21:54 +00001093 }
1094
1095 return 0;
1096}
1097
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001098void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001099{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001100 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001101 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001102 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001103 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001104
aliguori6f338c32009-02-11 15:21:54 +00001105 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001106 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001107 return;
1108 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001109
Markus Armbruster70b94332015-02-13 12:50:26 +01001110 opts = qemu_opts_parse_noisily(qemu_find_opts("net"),
1111 opts_str ? opts_str : "", false);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001112 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001113 return;
1114 }
1115
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001116 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001117
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001118 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001119 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001120 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +00001121 monitor_printf(mon, "adding host network device %s failed\n", device);
1122 }
aliguori6f338c32009-02-11 15:21:54 +00001123}
1124
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001125void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001126{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001127 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001128 int vlan_id = qdict_get_int(qdict, "vlan_id");
1129 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001130
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001131 nc = net_hub_find_client_by_name(vlan_id, device);
1132 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001133 error_report("Host network device '%s' on hub '%d' not found",
1134 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001135 return;
1136 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001137 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001138 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001139 return;
1140 }
Jason Wang64a55d62015-02-02 15:06:37 +08001141
1142 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001143 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001144}
1145
Luiz Capitulino928059a2012-04-18 17:34:15 -03001146void netdev_add(QemuOpts *opts, Error **errp)
1147{
1148 net_client_init(opts, 1, errp);
1149}
1150
Markus Armbruster485febc2015-03-13 17:25:50 +01001151void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001152{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001153 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001154 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001155 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001156
Luiz Capitulino928059a2012-04-18 17:34:15 -03001157 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001158 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001159 goto out;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001160 }
1161
Luiz Capitulino928059a2012-04-18 17:34:15 -03001162 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001163 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001164 goto out;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001165 }
1166
1167 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001168 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001169 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +01001170 goto out;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001171 }
1172
Markus Armbruster485febc2015-03-13 17:25:50 +01001173out:
1174 error_propagate(errp, local_err);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001175}
1176
Luiz Capitulino5f964152012-04-16 14:36:32 -03001177void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001178{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001179 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001180 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001181
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001182 nc = qemu_find_netdev(id);
1183 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001184 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1185 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001186 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001187 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001188
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001189 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1190 if (!opts) {
1191 error_setg(errp, "Device '%s' is not a netdev", id);
1192 return;
1193 }
1194
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001195 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001196 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001197}
1198
zhanghailiangaa9156f2016-01-26 14:43:33 +08001199static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1200{
1201 char *str;
1202 ObjectProperty *prop;
1203 ObjectPropertyIterator iter;
1204 StringOutputVisitor *ov;
1205
1206 /* generate info str */
1207 object_property_iter_init(&iter, OBJECT(nf));
1208 while ((prop = object_property_iter_next(&iter))) {
1209 if (!strcmp(prop->name, "type")) {
1210 continue;
1211 }
1212 ov = string_output_visitor_new(false);
1213 object_property_get(OBJECT(nf), string_output_get_visitor(ov),
1214 prop->name, NULL);
1215 str = string_output_get_string(ov);
1216 string_output_visitor_cleanup(ov);
1217 monitor_printf(mon, ",%s=%s", prop->name, str);
1218 g_free(str);
1219 }
1220 monitor_printf(mon, "\n");
1221}
1222
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001223void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001224{
Yang Hongyanga4960f52015-10-07 11:52:19 +08001225 NetFilterState *nf;
1226
Jason Wang1ceef9f2013-01-30 19:12:28 +08001227 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1228 nc->queue_index,
1229 NetClientOptionsKind_lookup[nc->info->type],
1230 nc->info_str);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001231 if (!QTAILQ_EMPTY(&nc->filters)) {
1232 monitor_printf(mon, "filters:\n");
1233 }
1234 QTAILQ_FOREACH(nf, &nc->filters, next) {
Yang Hongyanga3e8a3f2015-10-20 09:51:26 +08001235 char *path = object_get_canonical_path_component(OBJECT(nf));
zhanghailiangaa9156f2016-01-26 14:43:33 +08001236
1237 monitor_printf(mon, " - %s: type=%s", path,
1238 object_get_typename(OBJECT(nf)));
1239 netfilter_print_info(mon, nf);
Yang Hongyanga3e8a3f2015-10-20 09:51:26 +08001240 g_free(path);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001241 }
Jan Kiszka44e798d2011-07-20 12:20:21 +02001242}
1243
Amos Kongb1be4282013-06-14 15:45:52 +08001244RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1245 Error **errp)
1246{
1247 NetClientState *nc;
1248 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1249
1250 QTAILQ_FOREACH(nc, &net_clients, next) {
1251 RxFilterInfoList *entry;
1252 RxFilterInfo *info;
1253
1254 if (has_name && strcmp(nc->name, name) != 0) {
1255 continue;
1256 }
1257
1258 /* only query rx-filter information of NIC */
1259 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1260 if (has_name) {
1261 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001262 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001263 }
1264 continue;
1265 }
1266
Vladislav Yasevich5320c2c2015-10-19 09:04:38 -04001267 /* only query information on queue 0 since the info is per nic,
1268 * not per queue
1269 */
1270 if (nc->queue_index != 0)
1271 continue;
1272
Amos Kongb1be4282013-06-14 15:45:52 +08001273 if (nc->info->query_rx_filter) {
1274 info = nc->info->query_rx_filter(nc);
1275 entry = g_malloc0(sizeof(*entry));
1276 entry->value = info;
1277
1278 if (!filter_list) {
1279 filter_list = entry;
1280 } else {
1281 last_entry->next = entry;
1282 }
1283 last_entry = entry;
1284 } else if (has_name) {
1285 error_setg(errp, "net client(%s) doesn't support"
1286 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001287 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001288 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001289
1290 if (has_name) {
1291 break;
1292 }
Amos Kongb1be4282013-06-14 15:45:52 +08001293 }
1294
Markus Armbruster9083da12014-04-24 15:44:18 +02001295 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001296 error_setg(errp, "invalid net client name: %s", name);
1297 }
1298
1299 return filter_list;
1300}
1301
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001302void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001303{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001304 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001305 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001306
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001307 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001308
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001309 QTAILQ_FOREACH(nc, &net_clients, next) {
1310 peer = nc->peer;
1311 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001312
1313 /* Skip if already printed in hub info */
1314 if (net_hub_id_for_client(nc, NULL) == 0) {
1315 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001316 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001317
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001318 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001319 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001320 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001321 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001322 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001323 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001324 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001325 }
aliguori63a01ef2008-10-31 19:10:00 +00001326}
1327
Luiz Capitulino4b371562011-11-23 13:11:55 -02001328void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001329{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001330 NetClientState *ncs[MAX_QUEUE_NUM];
1331 NetClientState *nc;
1332 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001333
Jason Wang1ceef9f2013-01-30 19:12:28 +08001334 queues = qemu_find_net_clients_except(name, ncs,
Eric Blake7fb1cf12015-11-18 01:52:57 -07001335 NET_CLIENT_OPTIONS_KIND__MAX,
Jason Wang1ceef9f2013-01-30 19:12:28 +08001336 MAX_QUEUE_NUM);
1337
1338 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001339 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1340 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001341 return;
aliguori436e5e52009-01-08 19:44:06 +00001342 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001343 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001344
Jason Wang1ceef9f2013-01-30 19:12:28 +08001345 for (i = 0; i < queues; i++) {
1346 ncs[i]->link_down = !up;
1347 }
aliguori436e5e52009-01-08 19:44:06 +00001348
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001349 if (nc->info->link_status_changed) {
1350 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001351 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001352
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001353 if (nc->peer) {
1354 /* Change peer link only if the peer is NIC and then notify peer.
1355 * If the peer is a HUBPORT or a backend, we do not change the
1356 * link status.
1357 *
1358 * This behavior is compatible with qemu vlans where there could be
1359 * multiple clients that can still communicate with each other in
1360 * disconnected mode. For now maintain this compatibility.
1361 */
1362 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1363 for (i = 0; i < queues; i++) {
1364 ncs[i]->peer->link_down = !up;
1365 }
1366 }
1367 if (nc->peer->info->link_status_changed) {
1368 nc->peer->info->link_status_changed(nc->peer);
1369 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001370 }
aliguori436e5e52009-01-08 19:44:06 +00001371}
1372
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001373static void net_vm_change_state_handler(void *opaque, int running,
1374 RunState state)
1375{
Fam Zheng625de442015-07-07 09:21:07 +08001376 NetClientState *nc;
1377 NetClientState *tmp;
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001378
Fam Zheng625de442015-07-07 09:21:07 +08001379 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1380 if (running) {
1381 /* Flush queued packets and wake up backends. */
1382 if (nc->peer && qemu_can_send_packet(nc)) {
1383 qemu_flush_queued_packets(nc->peer);
1384 }
1385 } else {
1386 /* Complete all queued packets, to guarantee we don't modify
1387 * state later when VM is not running.
1388 */
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001389 qemu_flush_or_purge_queued_packets(nc, true);
1390 }
1391 }
1392}
1393
aliguori63a01ef2008-10-31 19:10:00 +00001394void net_cleanup(void)
1395{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001396 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001397
Jason Wang1ceef9f2013-01-30 19:12:28 +08001398 /* We may del multiple entries during qemu_del_net_client(),
1399 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1400 */
1401 while (!QTAILQ_EMPTY(&net_clients)) {
1402 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001403 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1404 qemu_del_nic(qemu_get_nic(nc));
1405 } else {
1406 qemu_del_net_client(nc);
1407 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001408 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001409
1410 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001411}
1412
Markus Armbruster668680f2010-02-11 14:44:58 +01001413void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001414{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001415 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001416 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001417
Peter Maydell641f6ea2011-05-20 16:50:00 +01001418 /* Don't warn about the default network setup that you get if
1419 * no command line -net or -netdev options are specified. There
1420 * are two cases that we would otherwise complain about:
1421 * (1) board doesn't support a NIC but the implicit "-net nic"
1422 * requested one
1423 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1424 * sets up a nic that isn't connected to anything.
1425 */
1426 if (default_net) {
1427 return;
1428 }
1429
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001430 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001431
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001432 QTAILQ_FOREACH(nc, &net_clients, next) {
1433 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001434 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001435 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1436 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001437 }
1438 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001439
1440 /* Check that all NICs requested via -net nic actually got created.
1441 * NICs created via -device don't need to be checked here because
1442 * they are always instantiated.
1443 */
1444 for (i = 0; i < MAX_NICS; i++) {
1445 NICInfo *nd = &nd_table[i];
1446 if (nd->used && !nd->instantiated) {
1447 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1448 "was not created (not supported by this machine?)\n",
1449 nd->name ? nd->name : "anonymous",
1450 nd->model ? nd->model : "unspecified");
1451 }
1452 }
aliguori63a01ef2008-10-31 19:10:00 +00001453}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001454
Markus Armbruster28d0de72015-03-13 13:35:14 +01001455static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001456{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001457 Error *local_err = NULL;
1458
1459 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001460 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001461 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001462 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001463 }
1464
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001465 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001466}
1467
Markus Armbruster28d0de72015-03-13 13:35:14 +01001468static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001469{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001470 Error *local_err = NULL;
1471 int ret;
1472
1473 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001474 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001475 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001476 return -1;
1477 }
1478
1479 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001480}
1481
1482int net_init_clients(void)
1483{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001484 QemuOptsList *net = qemu_find_opts("net");
1485
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001486 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001487 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001488 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001489#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001490 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001491#endif
1492 }
1493
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001494 net_change_state_entry =
1495 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1496
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001497 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001498
Markus Armbruster28d0de72015-03-13 13:35:14 +01001499 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1500 net_init_netdev, NULL, NULL)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001501 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001502 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001503
Markus Armbruster28d0de72015-03-13 13:35:14 +01001504 if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001505 return -1;
1506 }
1507
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001508 return 0;
1509}
1510
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001511int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001512{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001513#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001514 int ret;
1515 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001516 return ret;
1517 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001518#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001519
Markus Armbruster70b94332015-02-13 12:50:26 +01001520 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001521 return -1;
1522 }
1523
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001524 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001525 return 0;
1526}
Jason Wang7fc8d912012-03-05 11:08:50 +08001527
1528/* From FreeBSD */
1529/* XXX: optimize */
1530unsigned compute_mcast_idx(const uint8_t *ep)
1531{
1532 uint32_t crc;
1533 int carry, i, j;
1534 uint8_t b;
1535
1536 crc = 0xffffffff;
1537 for (i = 0; i < 6; i++) {
1538 b = *ep++;
1539 for (j = 0; j < 8; j++) {
1540 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1541 crc <<= 1;
1542 b >>= 1;
1543 if (carry) {
1544 crc = ((crc ^ POLYNOMIAL) | carry);
1545 }
1546 }
1547 }
1548 return crc >> 26;
1549}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001550
1551QemuOptsList qemu_netdev_opts = {
1552 .name = "netdev",
1553 .implied_opt_name = "type",
1554 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1555 .desc = {
1556 /*
1557 * no elements => accept any params
1558 * validation will happen later
1559 */
1560 { /* end of list */ }
1561 },
1562};
1563
1564QemuOptsList qemu_net_opts = {
1565 .name = "net",
1566 .implied_opt_name = "type",
1567 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1568 .desc = {
1569 /*
1570 * no elements => accept any params
1571 * validation will happen later
1572 */
1573 { /* end of list */ }
1574 },
1575};