blob: 3c68f3faa8a62c90f5c18fe65a2ae6449cfe7b70 [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"
blueswir1511d2b12009-03-07 15:32:56 +000048
Stefan Weil2944e4e2012-02-04 09:24:46 +010049/* Net bridge is currently not supported for W32. */
50#if !defined(_WIN32)
51# define CONFIG_NET_BRIDGE
52#endif
53
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030054static VMChangeStateEntry *net_change_state_entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010055static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000056
Hani Benhabiles84007e82014-05-27 23:39:33 +010057const char *host_net_devices[] = {
58 "tap",
59 "socket",
60 "dump",
61#ifdef CONFIG_NET_BRIDGE
62 "bridge",
63#endif
Stefan Hajnoczi027a2472015-05-27 17:16:48 +010064#ifdef CONFIG_NETMAP
65 "netmap",
66#endif
Hani Benhabiles84007e82014-05-27 23:39:33 +010067#ifdef CONFIG_SLIRP
68 "user",
69#endif
70#ifdef CONFIG_VDE
71 "vde",
72#endif
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030073 "vhost-user",
Hani Benhabiles84007e82014-05-27 23:39:33 +010074 NULL,
75};
76
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010077int default_net = 1;
78
aliguori63a01ef2008-10-31 19:10:00 +000079/***********************************************************/
80/* network device redirectors */
81
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000082#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000083static void hex_dump(FILE *f, const uint8_t *buf, int size)
84{
85 int len, i, j, c;
86
87 for(i=0;i<size;i+=16) {
88 len = size - i;
89 if (len > 16)
90 len = 16;
91 fprintf(f, "%08x ", i);
92 for(j=0;j<16;j++) {
93 if (j < len)
94 fprintf(f, " %02x", buf[i+j]);
95 else
96 fprintf(f, " ");
97 }
98 fprintf(f, " ");
99 for(j=0;j<len;j++) {
100 c = buf[i+j];
101 if (c < ' ' || c > '~')
102 c = '.';
103 fprintf(f, "%c", c);
104 }
105 fprintf(f, "\n");
106 }
107}
108#endif
109
aliguori63a01ef2008-10-31 19:10:00 +0000110static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
111{
112 const char *p, *p1;
113 int len;
114 p = *pp;
115 p1 = strchr(p, sep);
116 if (!p1)
117 return -1;
118 len = p1 - p;
119 p1++;
120 if (buf_size > 0) {
121 if (len > buf_size - 1)
122 len = buf_size - 1;
123 memcpy(buf, p, len);
124 buf[len] = '\0';
125 }
126 *pp = p1;
127 return 0;
128}
129
aliguori63a01ef2008-10-31 19:10:00 +0000130int parse_host_port(struct sockaddr_in *saddr, const char *str)
131{
132 char buf[512];
133 struct hostent *he;
134 const char *p, *r;
135 int port;
136
137 p = str;
138 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
139 return -1;
140 saddr->sin_family = AF_INET;
141 if (buf[0] == '\0') {
142 saddr->sin_addr.s_addr = 0;
143 } else {
blueswir1cd390082008-11-16 13:53:32 +0000144 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000145 if (!inet_aton(buf, &saddr->sin_addr))
146 return -1;
147 } else {
148 if ((he = gethostbyname(buf)) == NULL)
149 return - 1;
150 saddr->sin_addr = *(struct in_addr *)he->h_addr;
151 }
152 }
153 port = strtol(p, (char **)&r, 0);
154 if (r == p)
155 return -1;
156 saddr->sin_port = htons(port);
157 return 0;
158}
159
Scott Feldman890ee6a2015-03-13 21:09:25 -0700160char *qemu_mac_strdup_printf(const uint8_t *macaddr)
161{
162 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
163 macaddr[0], macaddr[1], macaddr[2],
164 macaddr[3], macaddr[4], macaddr[5]);
165}
166
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100167void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000168{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100169 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000170 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100171 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000172 macaddr[0], macaddr[1], macaddr[2],
173 macaddr[3], macaddr[4], macaddr[5]);
174}
175
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800176static int mac_table[256] = {0};
177
178static void qemu_macaddr_set_used(MACAddr *macaddr)
179{
180 int index;
181
182 for (index = 0x56; index < 0xFF; index++) {
183 if (macaddr->a[5] == index) {
184 mac_table[index]++;
185 }
186 }
187}
188
189static void qemu_macaddr_set_free(MACAddr *macaddr)
190{
191 int index;
192 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
193
194 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
195 return;
196 }
197 for (index = 0x56; index < 0xFF; index++) {
198 if (macaddr->a[5] == index) {
199 mac_table[index]--;
200 }
201 }
202}
203
204static int qemu_macaddr_get_free(void)
205{
206 int index;
207
208 for (index = 0x56; index < 0xFF; index++) {
209 if (mac_table[index] == 0) {
210 return index;
211 }
212 }
213
214 return -1;
215}
216
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200217void qemu_macaddr_default_if_unset(MACAddr *macaddr)
218{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200219 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800220 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200221
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800222 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
223 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
224 return;
225 } else {
226 qemu_macaddr_set_used(macaddr);
227 return;
228 }
229 }
230
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200231 macaddr->a[0] = 0x52;
232 macaddr->a[1] = 0x54;
233 macaddr->a[2] = 0x00;
234 macaddr->a[3] = 0x12;
235 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800236 macaddr->a[5] = qemu_macaddr_get_free();
237 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200238}
239
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100240/**
241 * Generate a name for net client
242 *
Amos Kongc9635302013-04-15 18:55:19 +0800243 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100244 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100245static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000246{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100247 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000248 int id = 0;
249
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100250 QTAILQ_FOREACH(nc, &net_clients, next) {
251 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100252 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100253 }
Amos Kongc9635302013-04-15 18:55:19 +0800254 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200255 id++;
256 }
257 }
258
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100259 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000260}
261
Jason Wangf7860452013-01-30 19:12:27 +0800262static void qemu_net_client_destructor(NetClientState *nc)
263{
264 g_free(nc);
265}
266
Jason Wang18a15412013-01-30 19:12:26 +0800267static void qemu_net_client_setup(NetClientState *nc,
268 NetClientInfo *info,
269 NetClientState *peer,
270 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800271 const char *name,
272 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000273{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100274 nc->info = info;
275 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000276 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100277 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000278 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100279 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000280 }
aliguori63a01ef2008-10-31 19:10:00 +0000281
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100282 if (peer) {
283 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100284 nc->peer = peer;
285 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100286 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100287 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100288
Yang Hongyang3e033a42015-10-07 11:52:17 +0800289 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
Jason Wangf7860452013-01-30 19:12:27 +0800290 nc->destructor = destructor;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800291 QTAILQ_INIT(&nc->filters);
Jason Wang18a15412013-01-30 19:12:26 +0800292}
293
294NetClientState *qemu_new_net_client(NetClientInfo *info,
295 NetClientState *peer,
296 const char *model,
297 const char *name)
298{
299 NetClientState *nc;
300
301 assert(info->size >= sizeof(NetClientState));
302
303 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800304 qemu_net_client_setup(nc, info, peer, model, name,
305 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800306
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100307 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000308}
309
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000310NICState *qemu_new_nic(NetClientInfo *info,
311 NICConf *conf,
312 const char *model,
313 const char *name,
314 void *opaque)
315{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800316 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000317 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200318 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000319
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200320 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000321 assert(info->size >= sizeof(NICState));
322
Jason Wangf6b26cf2013-02-22 23:15:06 +0800323 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
324 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000325 nic->conf = conf;
326 nic->opaque = opaque;
327
Jason Wangf6b26cf2013-02-22 23:15:06 +0800328 for (i = 0; i < queues; i++) {
329 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800330 NULL);
331 nic->ncs[i].queue_index = i;
332 }
333
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000334 return nic;
335}
336
Jason Wang1ceef9f2013-01-30 19:12:28 +0800337NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
338{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800339 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800340}
341
Jason Wangb356f762013-01-30 19:12:22 +0800342NetClientState *qemu_get_queue(NICState *nic)
343{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800344 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800345}
346
Jason Wangcc1f0f42013-01-30 19:12:23 +0800347NICState *qemu_get_nic(NetClientState *nc)
348{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800349 NetClientState *nc0 = nc - nc->queue_index;
350
Jason Wangf6b26cf2013-02-22 23:15:06 +0800351 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800352}
353
354void *qemu_get_nic_opaque(NetClientState *nc)
355{
356 NICState *nic = qemu_get_nic(nc);
357
358 return nic->opaque;
359}
360
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100361static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000362{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100363 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000364
Andreas Färbercc2a9042013-02-12 23:16:06 +0100365 if (nc->info->cleanup) {
366 nc->info->cleanup(nc);
367 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200368}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100369
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100370static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200371{
Jan Kiszka067404b2013-08-02 21:47:08 +0200372 if (nc->incoming_queue) {
373 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200374 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100375 if (nc->peer) {
376 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100377 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100378 g_free(nc->name);
379 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800380 if (nc->destructor) {
381 nc->destructor(nc);
382 }
aliguori63a01ef2008-10-31 19:10:00 +0000383}
384
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100385void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200386{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800387 NetClientState *ncs[MAX_QUEUE_NUM];
388 int queues, i;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800389 NetFilterState *nf, *next;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800390
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100391 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
392
Jason Wang1ceef9f2013-01-30 19:12:28 +0800393 /* If the NetClientState belongs to a multiqueue backend, we will change all
394 * other NetClientStates also.
395 */
396 queues = qemu_find_net_clients_except(nc->name, ncs,
397 NET_CLIENT_OPTIONS_KIND_NIC,
398 MAX_QUEUE_NUM);
399 assert(queues != 0);
400
Yang Hongyangfdccce42015-10-07 11:52:14 +0800401 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
402 object_unparent(OBJECT(nf));
403 }
404
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200405 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100406 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800407 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200408 if (nic->peer_deleted) {
409 return;
410 }
411 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800412
413 for (i = 0; i < queues; i++) {
414 ncs[i]->peer->link_down = true;
415 }
416
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100417 if (nc->peer->info->link_status_changed) {
418 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200419 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800420
421 for (i = 0; i < queues; i++) {
422 qemu_cleanup_net_client(ncs[i]);
423 }
424
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200425 return;
426 }
427
Jason Wang1ceef9f2013-01-30 19:12:28 +0800428 for (i = 0; i < queues; i++) {
429 qemu_cleanup_net_client(ncs[i]);
430 qemu_free_net_client(ncs[i]);
431 }
Jason Wang948ecf22013-01-30 19:12:24 +0800432}
433
434void qemu_del_nic(NICState *nic)
435{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200436 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800437
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800438 qemu_macaddr_set_free(&nic->conf->macaddr);
439
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200440 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800441 if (nic->peer_deleted) {
442 for (i = 0; i < queues; i++) {
443 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200444 }
445 }
446
Jason Wang1ceef9f2013-01-30 19:12:28 +0800447 for (i = queues - 1; i >= 0; i--) {
448 NetClientState *nc = qemu_get_subqueue(nic, i);
449
450 qemu_cleanup_net_client(nc);
451 qemu_free_net_client(nc);
452 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800453
454 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200455}
456
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000457void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
458{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100459 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000460
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100461 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200462 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800463 if (nc->queue_index == 0) {
464 func(qemu_get_nic(nc), opaque);
465 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000466 }
467 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000468}
469
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100470bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100471{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100472 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100473 return false;
474 }
475
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100476 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100477}
478
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100479bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100480{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100481 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100482 return false;
483 }
484
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100485 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100486}
487
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100488bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100489{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100490 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100491 return false;
492 }
493
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100494 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100495}
496
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100497void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100498{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100499 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100500 return;
501 }
502
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100503 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100504}
505
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100506void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100507 int ecn, int ufo)
508{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100509 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100510 return;
511 }
512
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100513 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100514}
515
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100516void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100517{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100518 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100519 return;
520 }
521
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100522 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100523}
524
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200525int qemu_set_vnet_le(NetClientState *nc, bool is_le)
526{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300527#ifdef HOST_WORDS_BIGENDIAN
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200528 if (!nc || !nc->info->set_vnet_le) {
529 return -ENOSYS;
530 }
531
532 return nc->info->set_vnet_le(nc, is_le);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300533#else
534 return 0;
535#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200536}
537
538int qemu_set_vnet_be(NetClientState *nc, bool is_be)
539{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300540#ifdef HOST_WORDS_BIGENDIAN
541 return 0;
542#else
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200543 if (!nc || !nc->info->set_vnet_be) {
544 return -ENOSYS;
545 }
546
547 return nc->info->set_vnet_be(nc, is_be);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300548#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200549}
550
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100551int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000552{
zhanghailiange1d64c02014-08-26 16:06:17 +0800553 int vm_running = runstate_is_running();
554
555 if (!vm_running) {
556 return 0;
557 }
558
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100559 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100560 return 1;
561 }
562
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100563 if (sender->peer->receive_disabled) {
564 return 0;
565 } else if (sender->peer->info->can_receive &&
566 !sender->peer->info->can_receive(sender->peer)) {
567 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000568 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500569 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000570}
571
Yang Hongyange64c7702015-10-07 11:52:15 +0800572static ssize_t filter_receive_iov(NetClientState *nc,
573 NetFilterDirection direction,
574 NetClientState *sender,
575 unsigned flags,
576 const struct iovec *iov,
577 int iovcnt,
578 NetPacketSent *sent_cb)
579{
580 ssize_t ret = 0;
581 NetFilterState *nf = NULL;
582
583 QTAILQ_FOREACH(nf, &nc->filters, next) {
584 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
585 iovcnt, sent_cb);
586 if (ret) {
587 return ret;
588 }
589 }
590
591 return ret;
592}
593
594static ssize_t filter_receive(NetClientState *nc,
595 NetFilterDirection direction,
596 NetClientState *sender,
597 unsigned flags,
598 const uint8_t *data,
599 size_t size,
600 NetPacketSent *sent_cb)
601{
602 struct iovec iov = {
603 .iov_base = (void *)data,
604 .iov_len = size
605 };
606
607 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
608}
609
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100610void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000611{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100612 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100613 return;
614 }
615
Jan Kiszka067404b2013-08-02 21:47:08 +0200616 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100617}
618
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300619static
620void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100621{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100622 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100623
Luigi Rizzo199ee602013-02-05 17:53:31 +0100624 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
625 if (net_hub_flush(nc->peer)) {
626 qemu_notify_event();
627 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100628 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200629 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200630 /* We emptied the queue successfully, signal to the IO thread to repoll
631 * the file descriptor (for tap, for example).
632 */
633 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300634 } else if (purge) {
635 /* Unable to empty the queue, purge remaining packets */
636 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200637 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100638}
639
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300640void qemu_flush_queued_packets(NetClientState *nc)
641{
642 qemu_flush_or_purge_queued_packets(nc, false);
643}
644
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100645static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100646 unsigned flags,
647 const uint8_t *buf, int size,
648 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000649{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100650 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800651 int ret;
Mark McLoughline94667b2009-04-29 11:48:12 +0100652
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100653#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100654 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100655 hex_dump(stdout, buf, size);
656#endif
657
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100658 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100659 return size;
660 }
661
Yang Hongyange64c7702015-10-07 11:52:15 +0800662 /* Let filters handle the packet first */
663 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
664 sender, flags, buf, size, sent_cb);
665 if (ret) {
666 return ret;
667 }
668
669 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
670 sender, flags, buf, size, sent_cb);
671 if (ret) {
672 return ret;
673 }
674
Jan Kiszka067404b2013-08-02 21:47:08 +0200675 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100676
Mark McLoughlinca77d172009-10-22 17:43:41 +0100677 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
678}
679
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100680ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100681 const uint8_t *buf, int size,
682 NetPacketSent *sent_cb)
683{
684 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
685 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100686}
687
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100688void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100689{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100690 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000691}
692
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100693ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100694{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100695 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100696 buf, size, NULL);
697}
698
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100699static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800700 int iovcnt, unsigned flags)
aliguorifbe78f42008-12-17 19:13:11 +0000701{
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800702 uint8_t buf[NET_BUFSIZE];
703 uint8_t *buffer;
Benjamin Poirierce053662011-02-23 19:57:21 -0500704 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000705
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800706 if (iovcnt == 1) {
707 buffer = iov[0].iov_base;
708 offset = iov[0].iov_len;
709 } else {
710 buffer = buf;
711 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
712 }
aliguorifbe78f42008-12-17 19:13:11 +0000713
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800714 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
715 return nc->info->receive_raw(nc, buffer, offset);
716 } else {
717 return nc->info->receive(nc, buffer, offset);
718 }
aliguorifbe78f42008-12-17 19:13:11 +0000719}
720
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100721ssize_t qemu_deliver_packet_iov(NetClientState *sender,
722 unsigned flags,
723 const struct iovec *iov,
724 int iovcnt,
725 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100726{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100727 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100728 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100729
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100730 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500731 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100732 }
733
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100734 if (nc->receive_disabled) {
735 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100736 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100737
738 if (nc->info->receive_iov) {
739 ret = nc->info->receive_iov(nc, iov, iovcnt);
740 } else {
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800741 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100742 }
743
744 if (ret == 0) {
745 nc->receive_disabled = 1;
746 }
747
748 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100749}
750
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100751ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100752 const struct iovec *iov, int iovcnt,
753 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000754{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100755 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800756 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100757
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100758 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500759 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000760 }
761
Yang Hongyange64c7702015-10-07 11:52:15 +0800762 /* Let filters handle the packet first */
763 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
764 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
765 if (ret) {
766 return ret;
767 }
768
769 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
770 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
771 if (ret) {
772 return ret;
773 }
774
Jan Kiszka067404b2013-08-02 21:47:08 +0200775 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100776
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100777 return qemu_net_queue_send_iov(queue, sender,
778 QEMU_NET_PACKET_FLAG_NONE,
779 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000780}
781
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100782ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100783qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100784{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100785 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100786}
787
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100788NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000789{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100790 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100791
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100792 QTAILQ_FOREACH(nc, &net_clients, next) {
793 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200794 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100795 if (!strcmp(nc->name, id)) {
796 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100797 }
798 }
799
800 return NULL;
801}
802
Jason Wang6c51ae72013-01-30 19:12:25 +0800803int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
804 NetClientOptionsKind type, int max)
805{
806 NetClientState *nc;
807 int ret = 0;
808
809 QTAILQ_FOREACH(nc, &net_clients, next) {
810 if (nc->info->type == type) {
811 continue;
812 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100813 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800814 if (ret < max) {
815 ncs[ret] = nc;
816 }
817 ret++;
818 }
819 }
820
821 return ret;
822}
823
aliguori76970792009-02-11 15:20:03 +0000824static int nic_get_free_idx(void)
825{
826 int index;
827
828 for (index = 0; index < MAX_NICS; index++)
829 if (!nd_table[index].used)
830 return index;
831 return -1;
832}
833
Markus Armbruster07caea32009-09-25 03:53:51 +0200834int qemu_show_nic_models(const char *arg, const char *const *models)
835{
836 int i;
837
Peter Maydellc8057f92012-08-02 13:45:54 +0100838 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200839 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100840 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200841
842 fprintf(stderr, "qemu: Supported NIC models: ");
843 for (i = 0 ; models[i]; i++)
844 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
845 return 1;
846}
847
aliguorid07f22c2009-01-13 19:03:57 +0000848void qemu_check_nic_model(NICInfo *nd, const char *model)
849{
850 const char *models[2];
851
852 models[0] = model;
853 models[1] = NULL;
854
Markus Armbruster07caea32009-09-25 03:53:51 +0200855 if (qemu_show_nic_models(nd->model, models))
856 exit(0);
857 if (qemu_find_nic_model(nd, models, model) < 0)
858 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000859}
860
Markus Armbruster07caea32009-09-25 03:53:51 +0200861int qemu_find_nic_model(NICInfo *nd, const char * const *models,
862 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000863{
Markus Armbruster07caea32009-09-25 03:53:51 +0200864 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000865
866 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500867 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000868
Markus Armbruster07caea32009-09-25 03:53:51 +0200869 for (i = 0 ; models[i]; i++) {
870 if (strcmp(nd->model, models[i]) == 0)
871 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000872 }
873
Markus Armbruster6daf1942011-06-22 14:03:54 +0200874 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200875 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000876}
877
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200878static int net_init_nic(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200879 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100880{
881 int idx;
882 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200883 const NetLegacyNicOptions *nic;
884
885 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
886 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100887
888 idx = nic_get_free_idx();
889 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200890 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100891 return -1;
892 }
893
894 nd = &nd_table[idx];
895
896 memset(nd, 0, sizeof(*nd));
897
Laszlo Ersek2456f362012-07-17 16:17:14 +0200898 if (nic->has_netdev) {
899 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100900 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200901 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100902 return -1;
903 }
904 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100905 assert(peer);
906 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100907 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100908 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200909 if (nic->has_model) {
910 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100911 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200912 if (nic->has_addr) {
913 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100914 }
915
Laszlo Ersek2456f362012-07-17 16:17:14 +0200916 if (nic->has_macaddr &&
917 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200918 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100919 return -1;
920 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400921 if (nic->has_macaddr &&
922 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200923 error_setg(errp,
924 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400925 return -1;
926 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200927 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100928
Laszlo Ersek2456f362012-07-17 16:17:14 +0200929 if (nic->has_vectors) {
930 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200931 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200932 return -1;
933 }
934 nd->nvectors = nic->vectors;
935 } else {
936 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100937 }
938
939 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100940 nb_nics++;
941
942 return idx;
943}
944
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100945
Laszlo Ersek6687b792012-07-17 16:17:13 +0200946static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200947 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200948 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200949 NetClientState *peer, Error **errp) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100950 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100951#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100952 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100953#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100954 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
955 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100956#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100957 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100958#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100959#ifdef CONFIG_NETMAP
960 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
961#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100962 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100963#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100964 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200965#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100966 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300967#ifdef CONFIG_VHOST_NET_USED
968 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
969#endif
Gonglei015a33b2014-07-01 20:58:08 +0800970#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100971 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
972#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100973};
974
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100975
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200976static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200977{
Laszlo Ersek6687b792012-07-17 16:17:13 +0200978 const NetClientOptions *opts;
979 const char *name;
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100980 NetClientState *peer = NULL;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100981
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100982 if (is_netdev) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +0100983 const Netdev *netdev = object;
984 opts = netdev->opts;
985 name = netdev->id;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200986
Stefan Hajnoczi13226292015-05-27 17:16:49 +0100987 if (opts->kind == NET_CLIENT_OPTIONS_KIND_DUMP ||
988 opts->kind == NET_CLIENT_OPTIONS_KIND_NIC ||
989 !net_client_init_fun[opts->kind]) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100990 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
991 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100992 return -1;
993 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200994 } else {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +0100995 const NetLegacy *net = object;
996 opts = net->opts;
997 /* missing optional values have been initialized to "all bits zero" */
998 name = net->has_id ? net->id : net->name;
999
1000 if (opts->kind == NET_CLIENT_OPTIONS_KIND_NONE) {
1001 return 0; /* nothing to do */
1002 }
Markus Armbrusterca7eb182015-05-15 13:58:49 +02001003 if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001004 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1005 "a net type");
Markus Armbrusterca7eb182015-05-15 13:58:49 +02001006 return -1;
1007 }
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +01001008
1009 if (!net_client_init_fun[opts->kind]) {
1010 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1011 "a net backend type (maybe it is not compiled "
1012 "into this binary)");
1013 return -1;
1014 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001015
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001016 /* Do not add to a vlan if it's a nic with a netdev= parameter. */
1017 if (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
1018 !opts->nic->has_netdev) {
1019 peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL);
1020 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001021 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001022
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001023 if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
1024 /* FIXME drop when all init functions store an Error */
1025 if (errp && !*errp) {
1026 error_setg(errp, QERR_DEVICE_INIT_FAILED,
1027 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001028 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001029 return -1;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001030 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001031 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001032}
1033
Laszlo Ersek6687b792012-07-17 16:17:13 +02001034
1035static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
1036{
1037 if (is_netdev) {
1038 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
1039 } else {
1040 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
1041 }
1042}
1043
1044
1045int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
1046{
1047 void *object = NULL;
1048 Error *err = NULL;
1049 int ret = -1;
1050
1051 {
1052 OptsVisitor *ov = opts_visitor_new(opts);
1053
1054 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
1055 opts_visitor_cleanup(ov);
1056 }
1057
1058 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +02001059 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001060 }
1061
1062 if (object) {
1063 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
1064
1065 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
1066 qapi_dealloc_visitor_cleanup(dv);
1067 }
1068
1069 error_propagate(errp, err);
1070 return ret;
1071}
1072
1073
aliguori6f338c32009-02-11 15:21:54 +00001074static int net_host_check_device(const char *device)
1075{
1076 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001077 for (i = 0; host_net_devices[i]; i++) {
1078 if (!strncmp(host_net_devices[i], device,
1079 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +00001080 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001081 }
aliguori6f338c32009-02-11 15:21:54 +00001082 }
1083
1084 return 0;
1085}
1086
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001087void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001088{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001089 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001090 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001091 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001092 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001093
aliguori6f338c32009-02-11 15:21:54 +00001094 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001095 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001096 return;
1097 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001098
Markus Armbruster70b94332015-02-13 12:50:26 +01001099 opts = qemu_opts_parse_noisily(qemu_find_opts("net"),
1100 opts_str ? opts_str : "", false);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001101 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001102 return;
1103 }
1104
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001105 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001106
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001107 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001108 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001109 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +00001110 monitor_printf(mon, "adding host network device %s failed\n", device);
1111 }
aliguori6f338c32009-02-11 15:21:54 +00001112}
1113
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001114void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001115{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001116 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001117 int vlan_id = qdict_get_int(qdict, "vlan_id");
1118 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001119
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001120 nc = net_hub_find_client_by_name(vlan_id, device);
1121 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001122 error_report("Host network device '%s' on hub '%d' not found",
1123 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001124 return;
1125 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001126 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001127 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001128 return;
1129 }
Jason Wang64a55d62015-02-02 15:06:37 +08001130
1131 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001132 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001133}
1134
Luiz Capitulino928059a2012-04-18 17:34:15 -03001135void netdev_add(QemuOpts *opts, Error **errp)
1136{
1137 net_client_init(opts, 1, errp);
1138}
1139
Markus Armbruster485febc2015-03-13 17:25:50 +01001140void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001141{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001142 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001143 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001144 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001145
Luiz Capitulino928059a2012-04-18 17:34:15 -03001146 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001147 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001148 goto out;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001149 }
1150
Luiz Capitulino928059a2012-04-18 17:34:15 -03001151 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001152 if (local_err) {
Markus Armbruster485febc2015-03-13 17:25:50 +01001153 goto out;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001154 }
1155
1156 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001157 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001158 qemu_opts_del(opts);
Markus Armbruster485febc2015-03-13 17:25:50 +01001159 goto out;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001160 }
1161
Markus Armbruster485febc2015-03-13 17:25:50 +01001162out:
1163 error_propagate(errp, local_err);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001164}
1165
Luiz Capitulino5f964152012-04-16 14:36:32 -03001166void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001167{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001168 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001169 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001170
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001171 nc = qemu_find_netdev(id);
1172 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001173 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1174 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001175 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001176 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001177
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001178 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1179 if (!opts) {
1180 error_setg(errp, "Device '%s' is not a netdev", id);
1181 return;
1182 }
1183
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001184 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001185 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001186}
1187
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001188void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001189{
Yang Hongyanga4960f52015-10-07 11:52:19 +08001190 NetFilterState *nf;
1191
Jason Wang1ceef9f2013-01-30 19:12:28 +08001192 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1193 nc->queue_index,
1194 NetClientOptionsKind_lookup[nc->info->type],
1195 nc->info_str);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001196 if (!QTAILQ_EMPTY(&nc->filters)) {
1197 monitor_printf(mon, "filters:\n");
1198 }
1199 QTAILQ_FOREACH(nf, &nc->filters, next) {
1200 monitor_printf(mon, " - %s: type=%s%s\n",
1201 object_get_canonical_path_component(OBJECT(nf)),
1202 object_get_typename(OBJECT(nf)),
1203 nf->info_str);
1204 }
Jan Kiszka44e798d2011-07-20 12:20:21 +02001205}
1206
Amos Kongb1be4282013-06-14 15:45:52 +08001207RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1208 Error **errp)
1209{
1210 NetClientState *nc;
1211 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1212
1213 QTAILQ_FOREACH(nc, &net_clients, next) {
1214 RxFilterInfoList *entry;
1215 RxFilterInfo *info;
1216
1217 if (has_name && strcmp(nc->name, name) != 0) {
1218 continue;
1219 }
1220
1221 /* only query rx-filter information of NIC */
1222 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1223 if (has_name) {
1224 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001225 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001226 }
1227 continue;
1228 }
1229
1230 if (nc->info->query_rx_filter) {
1231 info = nc->info->query_rx_filter(nc);
1232 entry = g_malloc0(sizeof(*entry));
1233 entry->value = info;
1234
1235 if (!filter_list) {
1236 filter_list = entry;
1237 } else {
1238 last_entry->next = entry;
1239 }
1240 last_entry = entry;
1241 } else if (has_name) {
1242 error_setg(errp, "net client(%s) doesn't support"
1243 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001244 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001245 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001246
1247 if (has_name) {
1248 break;
1249 }
Amos Kongb1be4282013-06-14 15:45:52 +08001250 }
1251
Markus Armbruster9083da12014-04-24 15:44:18 +02001252 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001253 error_setg(errp, "invalid net client name: %s", name);
1254 }
1255
1256 return filter_list;
1257}
1258
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001259void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001260{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001261 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001262 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001263
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001264 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001265
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001266 QTAILQ_FOREACH(nc, &net_clients, next) {
1267 peer = nc->peer;
1268 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001269
1270 /* Skip if already printed in hub info */
1271 if (net_hub_id_for_client(nc, NULL) == 0) {
1272 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001273 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001274
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001275 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001276 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001277 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001278 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001279 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001280 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001281 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001282 }
aliguori63a01ef2008-10-31 19:10:00 +00001283}
1284
Luiz Capitulino4b371562011-11-23 13:11:55 -02001285void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001286{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001287 NetClientState *ncs[MAX_QUEUE_NUM];
1288 NetClientState *nc;
1289 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001290
Jason Wang1ceef9f2013-01-30 19:12:28 +08001291 queues = qemu_find_net_clients_except(name, ncs,
1292 NET_CLIENT_OPTIONS_KIND_MAX,
1293 MAX_QUEUE_NUM);
1294
1295 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001296 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1297 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001298 return;
aliguori436e5e52009-01-08 19:44:06 +00001299 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001300 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001301
Jason Wang1ceef9f2013-01-30 19:12:28 +08001302 for (i = 0; i < queues; i++) {
1303 ncs[i]->link_down = !up;
1304 }
aliguori436e5e52009-01-08 19:44:06 +00001305
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001306 if (nc->info->link_status_changed) {
1307 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001308 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001309
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001310 if (nc->peer) {
1311 /* Change peer link only if the peer is NIC and then notify peer.
1312 * If the peer is a HUBPORT or a backend, we do not change the
1313 * link status.
1314 *
1315 * This behavior is compatible with qemu vlans where there could be
1316 * multiple clients that can still communicate with each other in
1317 * disconnected mode. For now maintain this compatibility.
1318 */
1319 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1320 for (i = 0; i < queues; i++) {
1321 ncs[i]->peer->link_down = !up;
1322 }
1323 }
1324 if (nc->peer->info->link_status_changed) {
1325 nc->peer->info->link_status_changed(nc->peer);
1326 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001327 }
aliguori436e5e52009-01-08 19:44:06 +00001328}
1329
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001330static void net_vm_change_state_handler(void *opaque, int running,
1331 RunState state)
1332{
Fam Zheng625de442015-07-07 09:21:07 +08001333 NetClientState *nc;
1334 NetClientState *tmp;
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001335
Fam Zheng625de442015-07-07 09:21:07 +08001336 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1337 if (running) {
1338 /* Flush queued packets and wake up backends. */
1339 if (nc->peer && qemu_can_send_packet(nc)) {
1340 qemu_flush_queued_packets(nc->peer);
1341 }
1342 } else {
1343 /* Complete all queued packets, to guarantee we don't modify
1344 * state later when VM is not running.
1345 */
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001346 qemu_flush_or_purge_queued_packets(nc, true);
1347 }
1348 }
1349}
1350
aliguori63a01ef2008-10-31 19:10:00 +00001351void net_cleanup(void)
1352{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001353 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001354
Jason Wang1ceef9f2013-01-30 19:12:28 +08001355 /* We may del multiple entries during qemu_del_net_client(),
1356 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1357 */
1358 while (!QTAILQ_EMPTY(&net_clients)) {
1359 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001360 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1361 qemu_del_nic(qemu_get_nic(nc));
1362 } else {
1363 qemu_del_net_client(nc);
1364 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001365 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001366
1367 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001368}
1369
Markus Armbruster668680f2010-02-11 14:44:58 +01001370void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001371{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001372 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001373 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001374
Peter Maydell641f6ea2011-05-20 16:50:00 +01001375 /* Don't warn about the default network setup that you get if
1376 * no command line -net or -netdev options are specified. There
1377 * are two cases that we would otherwise complain about:
1378 * (1) board doesn't support a NIC but the implicit "-net nic"
1379 * requested one
1380 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1381 * sets up a nic that isn't connected to anything.
1382 */
1383 if (default_net) {
1384 return;
1385 }
1386
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001387 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001388
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001389 QTAILQ_FOREACH(nc, &net_clients, next) {
1390 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001391 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001392 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1393 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001394 }
1395 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001396
1397 /* Check that all NICs requested via -net nic actually got created.
1398 * NICs created via -device don't need to be checked here because
1399 * they are always instantiated.
1400 */
1401 for (i = 0; i < MAX_NICS; i++) {
1402 NICInfo *nd = &nd_table[i];
1403 if (nd->used && !nd->instantiated) {
1404 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1405 "was not created (not supported by this machine?)\n",
1406 nd->name ? nd->name : "anonymous",
1407 nd->model ? nd->model : "unspecified");
1408 }
1409 }
aliguori63a01ef2008-10-31 19:10:00 +00001410}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001411
Markus Armbruster28d0de72015-03-13 13:35:14 +01001412static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001413{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001414 Error *local_err = NULL;
1415
1416 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001417 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001418 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001419 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001420 }
1421
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001422 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001423}
1424
Markus Armbruster28d0de72015-03-13 13:35:14 +01001425static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001426{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001427 Error *local_err = NULL;
1428 int ret;
1429
1430 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001431 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001432 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001433 return -1;
1434 }
1435
1436 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001437}
1438
1439int net_init_clients(void)
1440{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001441 QemuOptsList *net = qemu_find_opts("net");
1442
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001443 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001444 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001445 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001446#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001447 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001448#endif
1449 }
1450
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001451 net_change_state_entry =
1452 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1453
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001454 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001455
Markus Armbruster28d0de72015-03-13 13:35:14 +01001456 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1457 net_init_netdev, NULL, NULL)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001458 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001459 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001460
Markus Armbruster28d0de72015-03-13 13:35:14 +01001461 if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001462 return -1;
1463 }
1464
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001465 return 0;
1466}
1467
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001468int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001469{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001470#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001471 int ret;
1472 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001473 return ret;
1474 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001475#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001476
Markus Armbruster70b94332015-02-13 12:50:26 +01001477 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001478 return -1;
1479 }
1480
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001481 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001482 return 0;
1483}
Jason Wang7fc8d912012-03-05 11:08:50 +08001484
1485/* From FreeBSD */
1486/* XXX: optimize */
1487unsigned compute_mcast_idx(const uint8_t *ep)
1488{
1489 uint32_t crc;
1490 int carry, i, j;
1491 uint8_t b;
1492
1493 crc = 0xffffffff;
1494 for (i = 0; i < 6; i++) {
1495 b = *ep++;
1496 for (j = 0; j < 8; j++) {
1497 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1498 crc <<= 1;
1499 b >>= 1;
1500 if (carry) {
1501 crc = ((crc ^ POLYNOMIAL) | carry);
1502 }
1503 }
1504 }
1505 return crc >> 26;
1506}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001507
1508QemuOptsList qemu_netdev_opts = {
1509 .name = "netdev",
1510 .implied_opt_name = "type",
1511 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1512 .desc = {
1513 /*
1514 * no elements => accept any params
1515 * validation will happen later
1516 */
1517 { /* end of list */ }
1518 },
1519};
1520
1521QemuOptsList qemu_net_opts = {
1522 .name = "net",
1523 .implied_opt_name = "type",
1524 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1525 .desc = {
1526 /*
1527 * no elements => accept any params
1528 * validation will happen later
1529 */
1530 { /* end of list */ }
1531 },
1532};