blob: 594c3b8b5d40e9126789322c0671a13e6de31063 [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 */
Peter Maydell2744d922016-01-29 17:50:00 +000024#include "qemu/osdep.h"
blueswir1d40cdb12009-03-07 16:52:02 +000025
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"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/help_option.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010036#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010037#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/sockets.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020039#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010040#include "qemu/config-file.h"
Luiz Capitulino4b371562011-11-23 13:11:55 -020041#include "qmp-commands.h"
Amit Shah75422b02010-02-25 17:24:43 +053042#include "hw/qdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010044#include "qemu/main-loop.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020045#include "qapi-visit.h"
46#include "qapi/opts-visitor.h"
zhanghailiange1d64c02014-08-26 16:06:17 +080047#include "sysemu/sysemu.h"
Yang Hongyangfdccce42015-10-07 11:52:14 +080048#include "net/filter.h"
zhanghailiangaa9156f2016-01-26 14:43:33 +080049#include "qapi/string-output-visitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000050
Stefan Weil2944e4e2012-02-04 09:24:46 +010051/* Net bridge is currently not supported for W32. */
52#if !defined(_WIN32)
53# define CONFIG_NET_BRIDGE
54#endif
55
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030056static VMChangeStateEntry *net_change_state_entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010057static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000058
Hani Benhabiles84007e82014-05-27 23:39:33 +010059const char *host_net_devices[] = {
60 "tap",
61 "socket",
62 "dump",
63#ifdef CONFIG_NET_BRIDGE
64 "bridge",
65#endif
Stefan Hajnoczi027a2472015-05-27 17:16:48 +010066#ifdef CONFIG_NETMAP
67 "netmap",
68#endif
Hani Benhabiles84007e82014-05-27 23:39:33 +010069#ifdef CONFIG_SLIRP
70 "user",
71#endif
72#ifdef CONFIG_VDE
73 "vde",
74#endif
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030075 "vhost-user",
Hani Benhabiles84007e82014-05-27 23:39:33 +010076 NULL,
77};
78
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010079int default_net = 1;
80
aliguori63a01ef2008-10-31 19:10:00 +000081/***********************************************************/
82/* network device redirectors */
83
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000084#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000085static void hex_dump(FILE *f, const uint8_t *buf, int size)
86{
87 int len, i, j, c;
88
89 for(i=0;i<size;i+=16) {
90 len = size - i;
91 if (len > 16)
92 len = 16;
93 fprintf(f, "%08x ", i);
94 for(j=0;j<16;j++) {
95 if (j < len)
96 fprintf(f, " %02x", buf[i+j]);
97 else
98 fprintf(f, " ");
99 }
100 fprintf(f, " ");
101 for(j=0;j<len;j++) {
102 c = buf[i+j];
103 if (c < ' ' || c > '~')
104 c = '.';
105 fprintf(f, "%c", c);
106 }
107 fprintf(f, "\n");
108 }
109}
110#endif
111
aliguori63a01ef2008-10-31 19:10:00 +0000112static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
113{
114 const char *p, *p1;
115 int len;
116 p = *pp;
117 p1 = strchr(p, sep);
118 if (!p1)
119 return -1;
120 len = p1 - p;
121 p1++;
122 if (buf_size > 0) {
123 if (len > buf_size - 1)
124 len = buf_size - 1;
125 memcpy(buf, p, len);
126 buf[len] = '\0';
127 }
128 *pp = p1;
129 return 0;
130}
131
aliguori63a01ef2008-10-31 19:10:00 +0000132int parse_host_port(struct sockaddr_in *saddr, const char *str)
133{
134 char buf[512];
135 struct hostent *he;
136 const char *p, *r;
137 int port;
138
139 p = str;
140 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
141 return -1;
142 saddr->sin_family = AF_INET;
143 if (buf[0] == '\0') {
144 saddr->sin_addr.s_addr = 0;
145 } else {
blueswir1cd390082008-11-16 13:53:32 +0000146 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000147 if (!inet_aton(buf, &saddr->sin_addr))
148 return -1;
149 } else {
150 if ((he = gethostbyname(buf)) == NULL)
151 return - 1;
152 saddr->sin_addr = *(struct in_addr *)he->h_addr;
153 }
154 }
155 port = strtol(p, (char **)&r, 0);
156 if (r == p)
157 return -1;
158 saddr->sin_port = htons(port);
159 return 0;
160}
161
Scott Feldman890ee6a2015-03-13 21:09:25 -0700162char *qemu_mac_strdup_printf(const uint8_t *macaddr)
163{
164 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
165 macaddr[0], macaddr[1], macaddr[2],
166 macaddr[3], macaddr[4], macaddr[5]);
167}
168
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100169void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000170{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100171 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000172 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100173 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000174 macaddr[0], macaddr[1], macaddr[2],
175 macaddr[3], macaddr[4], macaddr[5]);
176}
177
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800178static int mac_table[256] = {0};
179
180static void qemu_macaddr_set_used(MACAddr *macaddr)
181{
182 int index;
183
184 for (index = 0x56; index < 0xFF; index++) {
185 if (macaddr->a[5] == index) {
186 mac_table[index]++;
187 }
188 }
189}
190
191static void qemu_macaddr_set_free(MACAddr *macaddr)
192{
193 int index;
194 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
195
196 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
197 return;
198 }
199 for (index = 0x56; index < 0xFF; index++) {
200 if (macaddr->a[5] == index) {
201 mac_table[index]--;
202 }
203 }
204}
205
206static int qemu_macaddr_get_free(void)
207{
208 int index;
209
210 for (index = 0x56; index < 0xFF; index++) {
211 if (mac_table[index] == 0) {
212 return index;
213 }
214 }
215
216 return -1;
217}
218
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200219void qemu_macaddr_default_if_unset(MACAddr *macaddr)
220{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200221 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800222 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200223
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800224 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
225 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
226 return;
227 } else {
228 qemu_macaddr_set_used(macaddr);
229 return;
230 }
231 }
232
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200233 macaddr->a[0] = 0x52;
234 macaddr->a[1] = 0x54;
235 macaddr->a[2] = 0x00;
236 macaddr->a[3] = 0x12;
237 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800238 macaddr->a[5] = qemu_macaddr_get_free();
239 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200240}
241
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100242/**
243 * Generate a name for net client
244 *
Amos Kongc9635302013-04-15 18:55:19 +0800245 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100246 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100247static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000248{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100249 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000250 int id = 0;
251
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100252 QTAILQ_FOREACH(nc, &net_clients, next) {
253 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100254 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100255 }
Amos Kongc9635302013-04-15 18:55:19 +0800256 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200257 id++;
258 }
259 }
260
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100261 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000262}
263
Jason Wangf7860452013-01-30 19:12:27 +0800264static void qemu_net_client_destructor(NetClientState *nc)
265{
266 g_free(nc);
267}
268
Jason Wang18a15412013-01-30 19:12:26 +0800269static void qemu_net_client_setup(NetClientState *nc,
270 NetClientInfo *info,
271 NetClientState *peer,
272 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800273 const char *name,
274 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000275{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100276 nc->info = info;
277 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000278 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100279 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000280 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100281 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000282 }
aliguori63a01ef2008-10-31 19:10:00 +0000283
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100284 if (peer) {
285 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100286 nc->peer = peer;
287 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100288 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100289 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100290
Yang Hongyang3e033a42015-10-07 11:52:17 +0800291 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
Jason Wangf7860452013-01-30 19:12:27 +0800292 nc->destructor = destructor;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800293 QTAILQ_INIT(&nc->filters);
Jason Wang18a15412013-01-30 19:12:26 +0800294}
295
296NetClientState *qemu_new_net_client(NetClientInfo *info,
297 NetClientState *peer,
298 const char *model,
299 const char *name)
300{
301 NetClientState *nc;
302
303 assert(info->size >= sizeof(NetClientState));
304
305 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800306 qemu_net_client_setup(nc, info, peer, model, name,
307 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800308
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100309 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000310}
311
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000312NICState *qemu_new_nic(NetClientInfo *info,
313 NICConf *conf,
314 const char *model,
315 const char *name,
316 void *opaque)
317{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800318 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000319 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200320 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000321
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200322 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000323 assert(info->size >= sizeof(NICState));
324
Jason Wangf6b26cf2013-02-22 23:15:06 +0800325 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
326 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000327 nic->conf = conf;
328 nic->opaque = opaque;
329
Jason Wangf6b26cf2013-02-22 23:15:06 +0800330 for (i = 0; i < queues; i++) {
331 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800332 NULL);
333 nic->ncs[i].queue_index = i;
334 }
335
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000336 return nic;
337}
338
Jason Wang1ceef9f2013-01-30 19:12:28 +0800339NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
340{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800341 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800342}
343
Jason Wangb356f762013-01-30 19:12:22 +0800344NetClientState *qemu_get_queue(NICState *nic)
345{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800346 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800347}
348
Jason Wangcc1f0f42013-01-30 19:12:23 +0800349NICState *qemu_get_nic(NetClientState *nc)
350{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800351 NetClientState *nc0 = nc - nc->queue_index;
352
Jason Wangf6b26cf2013-02-22 23:15:06 +0800353 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800354}
355
356void *qemu_get_nic_opaque(NetClientState *nc)
357{
358 NICState *nic = qemu_get_nic(nc);
359
360 return nic->opaque;
361}
362
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100363static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000364{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100365 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000366
Andreas Färbercc2a9042013-02-12 23:16:06 +0100367 if (nc->info->cleanup) {
368 nc->info->cleanup(nc);
369 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200370}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100371
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100372static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200373{
Jan Kiszka067404b2013-08-02 21:47:08 +0200374 if (nc->incoming_queue) {
375 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200376 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100377 if (nc->peer) {
378 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100379 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100380 g_free(nc->name);
381 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800382 if (nc->destructor) {
383 nc->destructor(nc);
384 }
aliguori63a01ef2008-10-31 19:10:00 +0000385}
386
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100387void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200388{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800389 NetClientState *ncs[MAX_QUEUE_NUM];
390 int queues, i;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800391 NetFilterState *nf, *next;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800392
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100393 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
394
Jason Wang1ceef9f2013-01-30 19:12:28 +0800395 /* If the NetClientState belongs to a multiqueue backend, we will change all
396 * other NetClientStates also.
397 */
398 queues = qemu_find_net_clients_except(nc->name, ncs,
399 NET_CLIENT_OPTIONS_KIND_NIC,
400 MAX_QUEUE_NUM);
401 assert(queues != 0);
402
Yang Hongyangfdccce42015-10-07 11:52:14 +0800403 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
404 object_unparent(OBJECT(nf));
405 }
406
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200407 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100408 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800409 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200410 if (nic->peer_deleted) {
411 return;
412 }
413 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800414
415 for (i = 0; i < queues; i++) {
416 ncs[i]->peer->link_down = true;
417 }
418
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100419 if (nc->peer->info->link_status_changed) {
420 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200421 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800422
423 for (i = 0; i < queues; i++) {
424 qemu_cleanup_net_client(ncs[i]);
425 }
426
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200427 return;
428 }
429
Jason Wang1ceef9f2013-01-30 19:12:28 +0800430 for (i = 0; i < queues; i++) {
431 qemu_cleanup_net_client(ncs[i]);
432 qemu_free_net_client(ncs[i]);
433 }
Jason Wang948ecf22013-01-30 19:12:24 +0800434}
435
436void qemu_del_nic(NICState *nic)
437{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200438 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800439
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800440 qemu_macaddr_set_free(&nic->conf->macaddr);
441
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200442 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800443 if (nic->peer_deleted) {
444 for (i = 0; i < queues; i++) {
445 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200446 }
447 }
448
Jason Wang1ceef9f2013-01-30 19:12:28 +0800449 for (i = queues - 1; i >= 0; i--) {
450 NetClientState *nc = qemu_get_subqueue(nic, i);
451
452 qemu_cleanup_net_client(nc);
453 qemu_free_net_client(nc);
454 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800455
456 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200457}
458
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000459void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
460{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100461 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000462
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100463 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200464 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800465 if (nc->queue_index == 0) {
466 func(qemu_get_nic(nc), opaque);
467 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000468 }
469 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000470}
471
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100472bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100473{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100474 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100475 return false;
476 }
477
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100478 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100479}
480
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100481bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100482{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100483 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100484 return false;
485 }
486
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100487 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100488}
489
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100490bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100491{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100492 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100493 return false;
494 }
495
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100496 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100497}
498
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100499void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100500{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100501 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100502 return;
503 }
504
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100505 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100506}
507
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100508void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100509 int ecn, int ufo)
510{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100511 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100512 return;
513 }
514
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100515 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100516}
517
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100518void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100519{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100520 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100521 return;
522 }
523
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100524 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100525}
526
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200527int qemu_set_vnet_le(NetClientState *nc, bool is_le)
528{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300529#ifdef HOST_WORDS_BIGENDIAN
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200530 if (!nc || !nc->info->set_vnet_le) {
531 return -ENOSYS;
532 }
533
534 return nc->info->set_vnet_le(nc, is_le);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300535#else
536 return 0;
537#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200538}
539
540int qemu_set_vnet_be(NetClientState *nc, bool is_be)
541{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300542#ifdef HOST_WORDS_BIGENDIAN
543 return 0;
544#else
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200545 if (!nc || !nc->info->set_vnet_be) {
546 return -ENOSYS;
547 }
548
549 return nc->info->set_vnet_be(nc, is_be);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300550#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200551}
552
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100553int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000554{
zhanghailiange1d64c02014-08-26 16:06:17 +0800555 int vm_running = runstate_is_running();
556
557 if (!vm_running) {
558 return 0;
559 }
560
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100561 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100562 return 1;
563 }
564
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100565 if (sender->peer->receive_disabled) {
566 return 0;
567 } else if (sender->peer->info->can_receive &&
568 !sender->peer->info->can_receive(sender->peer)) {
569 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000570 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500571 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000572}
573
Yang Hongyange64c7702015-10-07 11:52:15 +0800574static ssize_t filter_receive_iov(NetClientState *nc,
575 NetFilterDirection direction,
576 NetClientState *sender,
577 unsigned flags,
578 const struct iovec *iov,
579 int iovcnt,
580 NetPacketSent *sent_cb)
581{
582 ssize_t ret = 0;
583 NetFilterState *nf = NULL;
584
Li Zhijian25aaadf2016-01-26 13:00:22 +0800585 if (direction == NET_FILTER_DIRECTION_TX) {
586 QTAILQ_FOREACH(nf, &nc->filters, next) {
587 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
588 iovcnt, sent_cb);
589 if (ret) {
590 return ret;
591 }
592 }
593 } else {
594 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, NetFilterHead, next) {
595 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
596 iovcnt, sent_cb);
597 if (ret) {
598 return ret;
599 }
Yang Hongyange64c7702015-10-07 11:52:15 +0800600 }
601 }
602
603 return ret;
604}
605
606static ssize_t filter_receive(NetClientState *nc,
607 NetFilterDirection direction,
608 NetClientState *sender,
609 unsigned flags,
610 const uint8_t *data,
611 size_t size,
612 NetPacketSent *sent_cb)
613{
614 struct iovec iov = {
615 .iov_base = (void *)data,
616 .iov_len = size
617 };
618
619 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
620}
621
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100622void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000623{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100624 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100625 return;
626 }
627
Jan Kiszka067404b2013-08-02 21:47:08 +0200628 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100629}
630
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300631static
632void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100633{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100634 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100635
Luigi Rizzo199ee602013-02-05 17:53:31 +0100636 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
637 if (net_hub_flush(nc->peer)) {
638 qemu_notify_event();
639 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100640 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200641 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200642 /* We emptied the queue successfully, signal to the IO thread to repoll
643 * the file descriptor (for tap, for example).
644 */
645 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300646 } else if (purge) {
647 /* Unable to empty the queue, purge remaining packets */
648 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200649 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100650}
651
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300652void qemu_flush_queued_packets(NetClientState *nc)
653{
654 qemu_flush_or_purge_queued_packets(nc, false);
655}
656
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100657static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100658 unsigned flags,
659 const uint8_t *buf, int size,
660 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000661{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100662 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800663 int ret;
Mark McLoughline94667b2009-04-29 11:48:12 +0100664
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100665#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100666 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100667 hex_dump(stdout, buf, size);
668#endif
669
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100670 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100671 return size;
672 }
673
Yang Hongyange64c7702015-10-07 11:52:15 +0800674 /* Let filters handle the packet first */
675 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
676 sender, flags, buf, size, sent_cb);
677 if (ret) {
678 return ret;
679 }
680
681 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
682 sender, flags, buf, size, sent_cb);
683 if (ret) {
684 return ret;
685 }
686
Jan Kiszka067404b2013-08-02 21:47:08 +0200687 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100688
Mark McLoughlinca77d172009-10-22 17:43:41 +0100689 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
690}
691
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100692ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100693 const uint8_t *buf, int size,
694 NetPacketSent *sent_cb)
695{
696 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
697 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100698}
699
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100700void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100701{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100702 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000703}
704
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100705ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100706{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100707 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100708 buf, size, NULL);
709}
710
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100711static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800712 int iovcnt, unsigned flags)
aliguorifbe78f42008-12-17 19:13:11 +0000713{
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800714 uint8_t buf[NET_BUFSIZE];
715 uint8_t *buffer;
Benjamin Poirierce053662011-02-23 19:57:21 -0500716 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000717
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800718 if (iovcnt == 1) {
719 buffer = iov[0].iov_base;
720 offset = iov[0].iov_len;
721 } else {
722 buffer = buf;
Yang Hongyangedc98142015-10-20 09:51:25 +0800723 offset = iov_to_buf(iov, iovcnt, 0, buf, sizeof(buf));
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800724 }
aliguorifbe78f42008-12-17 19:13:11 +0000725
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800726 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
727 return nc->info->receive_raw(nc, buffer, offset);
728 } else {
729 return nc->info->receive(nc, buffer, offset);
730 }
aliguorifbe78f42008-12-17 19:13:11 +0000731}
732
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100733ssize_t qemu_deliver_packet_iov(NetClientState *sender,
734 unsigned flags,
735 const struct iovec *iov,
736 int iovcnt,
737 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100738{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100739 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100740 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100741
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100742 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500743 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100744 }
745
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100746 if (nc->receive_disabled) {
747 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100748 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100749
750 if (nc->info->receive_iov) {
751 ret = nc->info->receive_iov(nc, iov, iovcnt);
752 } else {
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800753 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100754 }
755
756 if (ret == 0) {
757 nc->receive_disabled = 1;
758 }
759
760 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100761}
762
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100763ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100764 const struct iovec *iov, int iovcnt,
765 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000766{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100767 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800768 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100769
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100770 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500771 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000772 }
773
Yang Hongyange64c7702015-10-07 11:52:15 +0800774 /* Let filters handle the packet first */
775 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
776 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
777 if (ret) {
778 return ret;
779 }
780
781 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
782 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
783 if (ret) {
784 return ret;
785 }
786
Jan Kiszka067404b2013-08-02 21:47:08 +0200787 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100788
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100789 return qemu_net_queue_send_iov(queue, sender,
790 QEMU_NET_PACKET_FLAG_NONE,
791 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000792}
793
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100794ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100795qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100796{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100797 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100798}
799
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100800NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000801{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100802 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100803
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100804 QTAILQ_FOREACH(nc, &net_clients, next) {
805 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200806 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100807 if (!strcmp(nc->name, id)) {
808 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100809 }
810 }
811
812 return NULL;
813}
814
Jason Wang6c51ae72013-01-30 19:12:25 +0800815int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
816 NetClientOptionsKind type, int max)
817{
818 NetClientState *nc;
819 int ret = 0;
820
821 QTAILQ_FOREACH(nc, &net_clients, next) {
822 if (nc->info->type == type) {
823 continue;
824 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100825 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800826 if (ret < max) {
827 ncs[ret] = nc;
828 }
829 ret++;
830 }
831 }
832
833 return ret;
834}
835
aliguori76970792009-02-11 15:20:03 +0000836static int nic_get_free_idx(void)
837{
838 int index;
839
840 for (index = 0; index < MAX_NICS; index++)
841 if (!nd_table[index].used)
842 return index;
843 return -1;
844}
845
Markus Armbruster07caea32009-09-25 03:53:51 +0200846int qemu_show_nic_models(const char *arg, const char *const *models)
847{
848 int i;
849
Peter Maydellc8057f92012-08-02 13:45:54 +0100850 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200851 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100852 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200853
854 fprintf(stderr, "qemu: Supported NIC models: ");
855 for (i = 0 ; models[i]; i++)
856 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
857 return 1;
858}
859
aliguorid07f22c2009-01-13 19:03:57 +0000860void qemu_check_nic_model(NICInfo *nd, const char *model)
861{
862 const char *models[2];
863
864 models[0] = model;
865 models[1] = NULL;
866
Markus Armbruster07caea32009-09-25 03:53:51 +0200867 if (qemu_show_nic_models(nd->model, models))
868 exit(0);
869 if (qemu_find_nic_model(nd, models, model) < 0)
870 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000871}
872
Markus Armbruster07caea32009-09-25 03:53:51 +0200873int qemu_find_nic_model(NICInfo *nd, const char * const *models,
874 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000875{
Markus Armbruster07caea32009-09-25 03:53:51 +0200876 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000877
878 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500879 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000880
Markus Armbruster07caea32009-09-25 03:53:51 +0200881 for (i = 0 ; models[i]; i++) {
882 if (strcmp(nd->model, models[i]) == 0)
883 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000884 }
885
Markus Armbruster6daf1942011-06-22 14:03:54 +0200886 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200887 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000888}
889
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200890static int net_init_nic(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200891 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100892{
893 int idx;
894 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200895 const NetLegacyNicOptions *nic;
896
Eric Blake8d0bcba2015-10-26 16:34:56 -0600897 assert(opts->type == NET_CLIENT_OPTIONS_KIND_NIC);
Eric Blake32bafa82016-03-17 16:48:37 -0600898 nic = opts->u.nic.data;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100899
900 idx = nic_get_free_idx();
901 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200902 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100903 return -1;
904 }
905
906 nd = &nd_table[idx];
907
908 memset(nd, 0, sizeof(*nd));
909
Laszlo Ersek2456f362012-07-17 16:17:14 +0200910 if (nic->has_netdev) {
911 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100912 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200913 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100914 return -1;
915 }
916 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100917 assert(peer);
918 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100919 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100920 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200921 if (nic->has_model) {
922 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100923 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200924 if (nic->has_addr) {
925 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100926 }
927
Laszlo Ersek2456f362012-07-17 16:17:14 +0200928 if (nic->has_macaddr &&
929 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200930 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100931 return -1;
932 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400933 if (nic->has_macaddr &&
934 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200935 error_setg(errp,
936 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400937 return -1;
938 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200939 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100940
Laszlo Ersek2456f362012-07-17 16:17:14 +0200941 if (nic->has_vectors) {
942 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200943 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200944 return -1;
945 }
946 nd->nvectors = nic->vectors;
947 } else {
948 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100949 }
950
951 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100952 nb_nics++;
953
954 return idx;
955}
956
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100957
Eric Blake7fb1cf12015-11-18 01:52:57 -0700958static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND__MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200959 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200960 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200961 NetClientState *peer, Error **errp) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100962 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100963#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100964 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100965#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100966 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
967 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100968#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100969 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100970#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100971#ifdef CONFIG_NETMAP
972 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
973#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100974 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100975#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100976 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200977#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100978 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300979#ifdef CONFIG_VHOST_NET_USED
980 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
981#endif
Gonglei015a33b2014-07-01 20:58:08 +0800982#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100983 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
984#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100985};
986
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100987
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200988static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200989{
Laszlo Ersek6687b792012-07-17 16:17:13 +0200990 const NetClientOptions *opts;
991 const char *name;
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100992 NetClientState *peer = NULL;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100993
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100994 if (is_netdev) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +0100995 const Netdev *netdev = object;
996 opts = netdev->opts;
997 name = netdev->id;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200998
Eric Blake8d0bcba2015-10-26 16:34:56 -0600999 if (opts->type == NET_CLIENT_OPTIONS_KIND_DUMP ||
1000 opts->type == NET_CLIENT_OPTIONS_KIND_NIC ||
1001 !net_client_init_fun[opts->type]) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001002 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1003 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001004 return -1;
1005 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001006 } else {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001007 const NetLegacy *net = object;
1008 opts = net->opts;
1009 /* missing optional values have been initialized to "all bits zero" */
1010 name = net->has_id ? net->id : net->name;
1011
Eric Blake8d0bcba2015-10-26 16:34:56 -06001012 if (opts->type == NET_CLIENT_OPTIONS_KIND_NONE) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001013 return 0; /* nothing to do */
1014 }
Eric Blake8d0bcba2015-10-26 16:34:56 -06001015 if (opts->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001016 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1017 "a net type");
Markus Armbrusterca7eb182015-05-15 13:58:49 +02001018 return -1;
1019 }
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +01001020
Eric Blake8d0bcba2015-10-26 16:34:56 -06001021 if (!net_client_init_fun[opts->type]) {
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +01001022 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1023 "a net backend type (maybe it is not compiled "
1024 "into this binary)");
1025 return -1;
1026 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001027
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001028 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
Eric Blake8d0bcba2015-10-26 16:34:56 -06001029 if (opts->type != NET_CLIENT_OPTIONS_KIND_NIC ||
Eric Blake32bafa82016-03-17 16:48:37 -06001030 !opts->u.nic.data->has_netdev) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001031 peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL);
1032 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001033 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001034
Eric Blake8d0bcba2015-10-26 16:34:56 -06001035 if (net_client_init_fun[opts->type](opts, name, peer, errp) < 0) {
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001036 /* FIXME drop when all init functions store an Error */
1037 if (errp && !*errp) {
1038 error_setg(errp, QERR_DEVICE_INIT_FAILED,
Eric Blake8d0bcba2015-10-26 16:34:56 -06001039 NetClientOptionsKind_lookup[opts->type]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001040 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001041 return -1;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001042 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001043 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001044}
1045
Laszlo Ersek6687b792012-07-17 16:17:13 +02001046
Laszlo Ersek6687b792012-07-17 16:17:13 +02001047int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
1048{
1049 void *object = NULL;
1050 Error *err = NULL;
1051 int ret = -1;
Eric Blake96a16162016-02-23 14:14:33 -07001052 OptsVisitor *ov = opts_visitor_new(opts);
1053 Visitor *v = opts_get_visitor(ov);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001054
Yann Bordenave7aac5312016-03-15 10:31:22 +01001055 {
1056 /* Parse convenience option format ip6-net=fec0::0[/64] */
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001057 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
Yann Bordenave7aac5312016-03-15 10:31:22 +01001058
1059 if (ip6_net) {
1060 char buf[strlen(ip6_net) + 1];
1061
1062 if (get_str_sep(buf, sizeof(buf), &ip6_net, '/') < 0) {
1063 /* Default 64bit prefix length. */
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001064 qemu_opt_set(opts, "ipv6-prefix", ip6_net, &error_abort);
1065 qemu_opt_set_number(opts, "ipv6-prefixlen", 64, &error_abort);
Yann Bordenave7aac5312016-03-15 10:31:22 +01001066 } else {
1067 /* User-specified prefix length. */
1068 unsigned long len;
1069 int err;
1070
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001071 qemu_opt_set(opts, "ipv6-prefix", buf, &error_abort);
Yann Bordenave7aac5312016-03-15 10:31:22 +01001072 err = qemu_strtoul(ip6_net, NULL, 10, &len);
1073
1074 if (err) {
1075 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001076 "ipv6-prefix", "a number");
Yann Bordenave7aac5312016-03-15 10:31:22 +01001077 } else {
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001078 qemu_opt_set_number(opts, "ipv6-prefixlen", len,
Yann Bordenave7aac5312016-03-15 10:31:22 +01001079 &error_abort);
1080 }
1081 }
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001082 qemu_opt_unset(opts, "ipv6-net");
Yann Bordenave7aac5312016-03-15 10:31:22 +01001083 }
1084 }
1085
Eric Blake96a16162016-02-23 14:14:33 -07001086 if (is_netdev) {
1087 visit_type_Netdev(v, NULL, (Netdev **)&object, &err);
1088 } else {
1089 visit_type_NetLegacy(v, NULL, (NetLegacy **)&object, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001090 }
1091
1092 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +02001093 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001094 }
1095
Eric Blake96a16162016-02-23 14:14:33 -07001096 if (is_netdev) {
1097 qapi_free_Netdev(object);
1098 } else {
1099 qapi_free_NetLegacy(object);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001100 }
1101
1102 error_propagate(errp, err);
1103 return ret;
1104}
1105
1106
aliguori6f338c32009-02-11 15:21:54 +00001107static int net_host_check_device(const char *device)
1108{
1109 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001110 for (i = 0; host_net_devices[i]; i++) {
1111 if (!strncmp(host_net_devices[i], device,
1112 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +00001113 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001114 }
aliguori6f338c32009-02-11 15:21:54 +00001115 }
1116
1117 return 0;
1118}
1119
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001120void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001121{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001122 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001123 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001124 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001125 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001126
aliguori6f338c32009-02-11 15:21:54 +00001127 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001128 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001129 return;
1130 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001131
Markus Armbruster70b94332015-02-13 12:50:26 +01001132 opts = qemu_opts_parse_noisily(qemu_find_opts("net"),
1133 opts_str ? opts_str : "", false);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001134 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001135 return;
1136 }
1137
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001138 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001139
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001140 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001141 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001142 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +00001143 monitor_printf(mon, "adding host network device %s failed\n", device);
1144 }
aliguori6f338c32009-02-11 15:21:54 +00001145}
1146
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001147void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001148{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001149 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001150 int vlan_id = qdict_get_int(qdict, "vlan_id");
1151 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001152
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001153 nc = net_hub_find_client_by_name(vlan_id, device);
1154 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001155 error_report("Host network device '%s' on hub '%d' not found",
1156 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001157 return;
1158 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001159 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001160 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001161 return;
1162 }
Jason Wang64a55d62015-02-02 15:06:37 +08001163
1164 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001165 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001166}
1167
Luiz Capitulino928059a2012-04-18 17:34:15 -03001168void netdev_add(QemuOpts *opts, Error **errp)
1169{
1170 net_client_init(opts, 1, errp);
1171}
1172
Markus Armbruster485febc2015-03-13 17:25:50 +01001173void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001174{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001175 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001176 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001177 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001178
Luiz Capitulino928059a2012-04-18 17:34:15 -03001179 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001180 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001181 goto out;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001182 }
1183
Luiz Capitulino928059a2012-04-18 17:34:15 -03001184 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001185 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001186 goto out;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001187 }
1188
1189 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001190 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001191 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +01001192 goto out;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001193 }
1194
Markus Armbruster485febc2015-03-13 17:25:50 +01001195out:
1196 error_propagate(errp, local_err);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001197}
1198
Luiz Capitulino5f964152012-04-16 14:36:32 -03001199void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001200{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001201 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001202 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001203
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001204 nc = qemu_find_netdev(id);
1205 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001206 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1207 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001208 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001209 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001210
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001211 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1212 if (!opts) {
1213 error_setg(errp, "Device '%s' is not a netdev", id);
1214 return;
1215 }
1216
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001217 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001218 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001219}
1220
zhanghailiangaa9156f2016-01-26 14:43:33 +08001221static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1222{
1223 char *str;
1224 ObjectProperty *prop;
1225 ObjectPropertyIterator iter;
1226 StringOutputVisitor *ov;
1227
1228 /* generate info str */
1229 object_property_iter_init(&iter, OBJECT(nf));
1230 while ((prop = object_property_iter_next(&iter))) {
1231 if (!strcmp(prop->name, "type")) {
1232 continue;
1233 }
1234 ov = string_output_visitor_new(false);
1235 object_property_get(OBJECT(nf), string_output_get_visitor(ov),
1236 prop->name, NULL);
1237 str = string_output_get_string(ov);
1238 string_output_visitor_cleanup(ov);
1239 monitor_printf(mon, ",%s=%s", prop->name, str);
1240 g_free(str);
1241 }
1242 monitor_printf(mon, "\n");
1243}
1244
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001245void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001246{
Yang Hongyanga4960f52015-10-07 11:52:19 +08001247 NetFilterState *nf;
1248
Jason Wang1ceef9f2013-01-30 19:12:28 +08001249 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1250 nc->queue_index,
1251 NetClientOptionsKind_lookup[nc->info->type],
1252 nc->info_str);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001253 if (!QTAILQ_EMPTY(&nc->filters)) {
1254 monitor_printf(mon, "filters:\n");
1255 }
1256 QTAILQ_FOREACH(nf, &nc->filters, next) {
Yang Hongyanga3e8a3f2015-10-20 09:51:26 +08001257 char *path = object_get_canonical_path_component(OBJECT(nf));
zhanghailiangaa9156f2016-01-26 14:43:33 +08001258
1259 monitor_printf(mon, " - %s: type=%s", path,
1260 object_get_typename(OBJECT(nf)));
1261 netfilter_print_info(mon, nf);
Yang Hongyanga3e8a3f2015-10-20 09:51:26 +08001262 g_free(path);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001263 }
Jan Kiszka44e798d2011-07-20 12:20:21 +02001264}
1265
Amos Kongb1be4282013-06-14 15:45:52 +08001266RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1267 Error **errp)
1268{
1269 NetClientState *nc;
1270 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1271
1272 QTAILQ_FOREACH(nc, &net_clients, next) {
1273 RxFilterInfoList *entry;
1274 RxFilterInfo *info;
1275
1276 if (has_name && strcmp(nc->name, name) != 0) {
1277 continue;
1278 }
1279
1280 /* only query rx-filter information of NIC */
1281 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1282 if (has_name) {
1283 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001284 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001285 }
1286 continue;
1287 }
1288
Vladislav Yasevich5320c2c2015-10-19 09:04:38 -04001289 /* only query information on queue 0 since the info is per nic,
1290 * not per queue
1291 */
1292 if (nc->queue_index != 0)
1293 continue;
1294
Amos Kongb1be4282013-06-14 15:45:52 +08001295 if (nc->info->query_rx_filter) {
1296 info = nc->info->query_rx_filter(nc);
1297 entry = g_malloc0(sizeof(*entry));
1298 entry->value = info;
1299
1300 if (!filter_list) {
1301 filter_list = entry;
1302 } else {
1303 last_entry->next = entry;
1304 }
1305 last_entry = entry;
1306 } else if (has_name) {
1307 error_setg(errp, "net client(%s) doesn't support"
1308 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001309 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001310 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001311
1312 if (has_name) {
1313 break;
1314 }
Amos Kongb1be4282013-06-14 15:45:52 +08001315 }
1316
Markus Armbruster9083da12014-04-24 15:44:18 +02001317 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001318 error_setg(errp, "invalid net client name: %s", name);
1319 }
1320
1321 return filter_list;
1322}
1323
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001324void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001325{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001326 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001327 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001328
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001329 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001330
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001331 QTAILQ_FOREACH(nc, &net_clients, next) {
1332 peer = nc->peer;
1333 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001334
1335 /* Skip if already printed in hub info */
1336 if (net_hub_id_for_client(nc, NULL) == 0) {
1337 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001338 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001339
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001340 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001341 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001342 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001343 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001344 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001345 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001346 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001347 }
aliguori63a01ef2008-10-31 19:10:00 +00001348}
1349
Luiz Capitulino4b371562011-11-23 13:11:55 -02001350void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001351{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001352 NetClientState *ncs[MAX_QUEUE_NUM];
1353 NetClientState *nc;
1354 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001355
Jason Wang1ceef9f2013-01-30 19:12:28 +08001356 queues = qemu_find_net_clients_except(name, ncs,
Eric Blake7fb1cf12015-11-18 01:52:57 -07001357 NET_CLIENT_OPTIONS_KIND__MAX,
Jason Wang1ceef9f2013-01-30 19:12:28 +08001358 MAX_QUEUE_NUM);
1359
1360 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001361 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1362 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001363 return;
aliguori436e5e52009-01-08 19:44:06 +00001364 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001365 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001366
Jason Wang1ceef9f2013-01-30 19:12:28 +08001367 for (i = 0; i < queues; i++) {
1368 ncs[i]->link_down = !up;
1369 }
aliguori436e5e52009-01-08 19:44:06 +00001370
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001371 if (nc->info->link_status_changed) {
1372 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001373 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001374
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001375 if (nc->peer) {
1376 /* Change peer link only if the peer is NIC and then notify peer.
1377 * If the peer is a HUBPORT or a backend, we do not change the
1378 * link status.
1379 *
1380 * This behavior is compatible with qemu vlans where there could be
1381 * multiple clients that can still communicate with each other in
1382 * disconnected mode. For now maintain this compatibility.
1383 */
1384 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1385 for (i = 0; i < queues; i++) {
1386 ncs[i]->peer->link_down = !up;
1387 }
1388 }
1389 if (nc->peer->info->link_status_changed) {
1390 nc->peer->info->link_status_changed(nc->peer);
1391 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001392 }
aliguori436e5e52009-01-08 19:44:06 +00001393}
1394
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001395static void net_vm_change_state_handler(void *opaque, int running,
1396 RunState state)
1397{
Fam Zheng625de442015-07-07 09:21:07 +08001398 NetClientState *nc;
1399 NetClientState *tmp;
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001400
Fam Zheng625de442015-07-07 09:21:07 +08001401 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1402 if (running) {
1403 /* Flush queued packets and wake up backends. */
1404 if (nc->peer && qemu_can_send_packet(nc)) {
1405 qemu_flush_queued_packets(nc->peer);
1406 }
1407 } else {
1408 /* Complete all queued packets, to guarantee we don't modify
1409 * state later when VM is not running.
1410 */
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001411 qemu_flush_or_purge_queued_packets(nc, true);
1412 }
1413 }
1414}
1415
aliguori63a01ef2008-10-31 19:10:00 +00001416void net_cleanup(void)
1417{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001418 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001419
Jason Wang1ceef9f2013-01-30 19:12:28 +08001420 /* We may del multiple entries during qemu_del_net_client(),
1421 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1422 */
1423 while (!QTAILQ_EMPTY(&net_clients)) {
1424 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001425 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1426 qemu_del_nic(qemu_get_nic(nc));
1427 } else {
1428 qemu_del_net_client(nc);
1429 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001430 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001431
1432 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001433}
1434
Markus Armbruster668680f2010-02-11 14:44:58 +01001435void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001436{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001437 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001438 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001439
Peter Maydell641f6ea2011-05-20 16:50:00 +01001440 /* Don't warn about the default network setup that you get if
1441 * no command line -net or -netdev options are specified. There
1442 * are two cases that we would otherwise complain about:
1443 * (1) board doesn't support a NIC but the implicit "-net nic"
1444 * requested one
1445 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1446 * sets up a nic that isn't connected to anything.
1447 */
1448 if (default_net) {
1449 return;
1450 }
1451
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001452 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001453
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001454 QTAILQ_FOREACH(nc, &net_clients, next) {
1455 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001456 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001457 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1458 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001459 }
1460 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001461
1462 /* Check that all NICs requested via -net nic actually got created.
1463 * NICs created via -device don't need to be checked here because
1464 * they are always instantiated.
1465 */
1466 for (i = 0; i < MAX_NICS; i++) {
1467 NICInfo *nd = &nd_table[i];
1468 if (nd->used && !nd->instantiated) {
1469 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1470 "was not created (not supported by this machine?)\n",
1471 nd->name ? nd->name : "anonymous",
1472 nd->model ? nd->model : "unspecified");
1473 }
1474 }
aliguori63a01ef2008-10-31 19:10:00 +00001475}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001476
Markus Armbruster28d0de72015-03-13 13:35:14 +01001477static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001478{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001479 Error *local_err = NULL;
1480
1481 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001482 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001483 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001484 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001485 }
1486
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001487 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001488}
1489
Markus Armbruster28d0de72015-03-13 13:35:14 +01001490static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001491{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001492 Error *local_err = NULL;
1493 int ret;
1494
1495 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001496 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001497 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001498 return -1;
1499 }
1500
1501 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001502}
1503
1504int net_init_clients(void)
1505{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001506 QemuOptsList *net = qemu_find_opts("net");
1507
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001508 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001509 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001510 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001511#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001512 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001513#endif
1514 }
1515
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001516 net_change_state_entry =
1517 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1518
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001519 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001520
Markus Armbruster28d0de72015-03-13 13:35:14 +01001521 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1522 net_init_netdev, NULL, NULL)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001523 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001524 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001525
Markus Armbruster28d0de72015-03-13 13:35:14 +01001526 if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001527 return -1;
1528 }
1529
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001530 return 0;
1531}
1532
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001533int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001534{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001535#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001536 int ret;
1537 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001538 return ret;
1539 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001540#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001541
Markus Armbruster70b94332015-02-13 12:50:26 +01001542 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001543 return -1;
1544 }
1545
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001546 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001547 return 0;
1548}
Jason Wang7fc8d912012-03-05 11:08:50 +08001549
1550/* From FreeBSD */
1551/* XXX: optimize */
1552unsigned compute_mcast_idx(const uint8_t *ep)
1553{
1554 uint32_t crc;
1555 int carry, i, j;
1556 uint8_t b;
1557
1558 crc = 0xffffffff;
1559 for (i = 0; i < 6; i++) {
1560 b = *ep++;
1561 for (j = 0; j < 8; j++) {
1562 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1563 crc <<= 1;
1564 b >>= 1;
1565 if (carry) {
1566 crc = ((crc ^ POLYNOMIAL) | carry);
1567 }
1568 }
1569 }
1570 return crc >> 26;
1571}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001572
1573QemuOptsList qemu_netdev_opts = {
1574 .name = "netdev",
1575 .implied_opt_name = "type",
1576 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1577 .desc = {
1578 /*
1579 * no elements => accept any params
1580 * validation will happen later
1581 */
1582 { /* end of list */ }
1583 },
1584};
1585
1586QemuOptsList qemu_net_opts = {
1587 .name = "net",
1588 .implied_opt_name = "type",
1589 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1590 .desc = {
1591 /*
1592 * no elements => accept any params
1593 * validation will happen later
1594 */
1595 { /* end of list */ }
1596 },
1597};