blob: f8db85f30b41973b1973ff5d21020994a7292105 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/sockets.h"
36#include "qemu/config-file.h"
Luiz Capitulino4b371562011-11-23 13:11:55 -020037#include "qmp-commands.h"
Amit Shah75422b02010-02-25 17:24:43 +053038#include "hw/qdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010040#include "qemu/main-loop.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020041#include "qapi-visit.h"
42#include "qapi/opts-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010043#include "qapi/dealloc-visitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000044
Stefan Weil2944e4e2012-02-04 09:24:46 +010045/* Net bridge is currently not supported for W32. */
46#if !defined(_WIN32)
47# define CONFIG_NET_BRIDGE
48#endif
49
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010050static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000051
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010052int default_net = 1;
53
aliguori63a01ef2008-10-31 19:10:00 +000054/***********************************************************/
55/* network device redirectors */
56
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000057#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000058static void hex_dump(FILE *f, const uint8_t *buf, int size)
59{
60 int len, i, j, c;
61
62 for(i=0;i<size;i+=16) {
63 len = size - i;
64 if (len > 16)
65 len = 16;
66 fprintf(f, "%08x ", i);
67 for(j=0;j<16;j++) {
68 if (j < len)
69 fprintf(f, " %02x", buf[i+j]);
70 else
71 fprintf(f, " ");
72 }
73 fprintf(f, " ");
74 for(j=0;j<len;j++) {
75 c = buf[i+j];
76 if (c < ' ' || c > '~')
77 c = '.';
78 fprintf(f, "%c", c);
79 }
80 fprintf(f, "\n");
81 }
82}
83#endif
84
aliguori63a01ef2008-10-31 19:10:00 +000085static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
86{
87 const char *p, *p1;
88 int len;
89 p = *pp;
90 p1 = strchr(p, sep);
91 if (!p1)
92 return -1;
93 len = p1 - p;
94 p1++;
95 if (buf_size > 0) {
96 if (len > buf_size - 1)
97 len = buf_size - 1;
98 memcpy(buf, p, len);
99 buf[len] = '\0';
100 }
101 *pp = p1;
102 return 0;
103}
104
aliguori63a01ef2008-10-31 19:10:00 +0000105int parse_host_port(struct sockaddr_in *saddr, const char *str)
106{
107 char buf[512];
108 struct hostent *he;
109 const char *p, *r;
110 int port;
111
112 p = str;
113 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
114 return -1;
115 saddr->sin_family = AF_INET;
116 if (buf[0] == '\0') {
117 saddr->sin_addr.s_addr = 0;
118 } else {
blueswir1cd390082008-11-16 13:53:32 +0000119 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000120 if (!inet_aton(buf, &saddr->sin_addr))
121 return -1;
122 } else {
123 if ((he = gethostbyname(buf)) == NULL)
124 return - 1;
125 saddr->sin_addr = *(struct in_addr *)he->h_addr;
126 }
127 }
128 port = strtol(p, (char **)&r, 0);
129 if (r == p)
130 return -1;
131 saddr->sin_port = htons(port);
132 return 0;
133}
134
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100135void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000136{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100137 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000138 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100139 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000140 macaddr[0], macaddr[1], macaddr[2],
141 macaddr[3], macaddr[4], macaddr[5]);
142}
143
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200144void qemu_macaddr_default_if_unset(MACAddr *macaddr)
145{
146 static int index = 0;
147 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
148
149 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
150 return;
151 macaddr->a[0] = 0x52;
152 macaddr->a[1] = 0x54;
153 macaddr->a[2] = 0x00;
154 macaddr->a[3] = 0x12;
155 macaddr->a[4] = 0x34;
156 macaddr->a[5] = 0x56 + index++;
157}
158
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100159/**
160 * Generate a name for net client
161 *
Amos Kongc9635302013-04-15 18:55:19 +0800162 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100163 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100164static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000165{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100166 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000167 char buf[256];
168 int id = 0;
169
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100170 QTAILQ_FOREACH(nc, &net_clients, next) {
171 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100172 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100173 }
Amos Kongc9635302013-04-15 18:55:19 +0800174 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200175 id++;
176 }
177 }
178
aliguori676cff22009-01-07 17:43:44 +0000179 snprintf(buf, sizeof(buf), "%s.%d", model, id);
180
Anthony Liguori7267c092011-08-20 22:09:37 -0500181 return g_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000182}
183
Jason Wangf7860452013-01-30 19:12:27 +0800184static void qemu_net_client_destructor(NetClientState *nc)
185{
186 g_free(nc);
187}
188
Jason Wang18a15412013-01-30 19:12:26 +0800189static void qemu_net_client_setup(NetClientState *nc,
190 NetClientInfo *info,
191 NetClientState *peer,
192 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800193 const char *name,
194 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000195{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100196 nc->info = info;
197 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000198 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100199 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000200 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100201 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000202 }
aliguori63a01ef2008-10-31 19:10:00 +0000203
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100204 if (peer) {
205 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100206 nc->peer = peer;
207 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100208 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100209 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100210
Jan Kiszka067404b2013-08-02 21:47:08 +0200211 nc->incoming_queue = qemu_new_net_queue(nc);
Jason Wangf7860452013-01-30 19:12:27 +0800212 nc->destructor = destructor;
Jason Wang18a15412013-01-30 19:12:26 +0800213}
214
215NetClientState *qemu_new_net_client(NetClientInfo *info,
216 NetClientState *peer,
217 const char *model,
218 const char *name)
219{
220 NetClientState *nc;
221
222 assert(info->size >= sizeof(NetClientState));
223
224 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800225 qemu_net_client_setup(nc, info, peer, model, name,
226 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800227
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100228 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000229}
230
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000231NICState *qemu_new_nic(NetClientInfo *info,
232 NICConf *conf,
233 const char *model,
234 const char *name,
235 void *opaque)
236{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800237 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000238 NICState *nic;
Jason Wangf6b26cf2013-02-22 23:15:06 +0800239 int i, queues = MAX(1, conf->queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000240
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200241 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000242 assert(info->size >= sizeof(NICState));
243
Jason Wangf6b26cf2013-02-22 23:15:06 +0800244 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
245 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000246 nic->conf = conf;
247 nic->opaque = opaque;
248
Jason Wangf6b26cf2013-02-22 23:15:06 +0800249 for (i = 0; i < queues; i++) {
250 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800251 NULL);
252 nic->ncs[i].queue_index = i;
253 }
254
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000255 return nic;
256}
257
Jason Wang1ceef9f2013-01-30 19:12:28 +0800258NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
259{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800260 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800261}
262
Jason Wangb356f762013-01-30 19:12:22 +0800263NetClientState *qemu_get_queue(NICState *nic)
264{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800265 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800266}
267
Jason Wangcc1f0f42013-01-30 19:12:23 +0800268NICState *qemu_get_nic(NetClientState *nc)
269{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800270 NetClientState *nc0 = nc - nc->queue_index;
271
Jason Wangf6b26cf2013-02-22 23:15:06 +0800272 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800273}
274
275void *qemu_get_nic_opaque(NetClientState *nc)
276{
277 NICState *nic = qemu_get_nic(nc);
278
279 return nic->opaque;
280}
281
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100282static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000283{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100284 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000285
Andreas Färbercc2a9042013-02-12 23:16:06 +0100286 if (nc->info->cleanup) {
287 nc->info->cleanup(nc);
288 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200289}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100290
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100291static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200292{
Jan Kiszka067404b2013-08-02 21:47:08 +0200293 if (nc->incoming_queue) {
294 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200295 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100296 if (nc->peer) {
297 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100298 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100299 g_free(nc->name);
300 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800301 if (nc->destructor) {
302 nc->destructor(nc);
303 }
aliguori63a01ef2008-10-31 19:10:00 +0000304}
305
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100306void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200307{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800308 NetClientState *ncs[MAX_QUEUE_NUM];
309 int queues, i;
310
311 /* If the NetClientState belongs to a multiqueue backend, we will change all
312 * other NetClientStates also.
313 */
314 queues = qemu_find_net_clients_except(nc->name, ncs,
315 NET_CLIENT_OPTIONS_KIND_NIC,
316 MAX_QUEUE_NUM);
317 assert(queues != 0);
318
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200319 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100320 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800321 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200322 if (nic->peer_deleted) {
323 return;
324 }
325 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800326
327 for (i = 0; i < queues; i++) {
328 ncs[i]->peer->link_down = true;
329 }
330
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100331 if (nc->peer->info->link_status_changed) {
332 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200333 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800334
335 for (i = 0; i < queues; i++) {
336 qemu_cleanup_net_client(ncs[i]);
337 }
338
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200339 return;
340 }
341
Jason Wang948ecf22013-01-30 19:12:24 +0800342 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
343
Jason Wang1ceef9f2013-01-30 19:12:28 +0800344 for (i = 0; i < queues; i++) {
345 qemu_cleanup_net_client(ncs[i]);
346 qemu_free_net_client(ncs[i]);
347 }
Jason Wang948ecf22013-01-30 19:12:24 +0800348}
349
350void qemu_del_nic(NICState *nic)
351{
Michael Rothb8904922013-02-06 18:25:48 -0600352 int i, queues = MAX(nic->conf->queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800353
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200354 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800355 if (nic->peer_deleted) {
356 for (i = 0; i < queues; i++) {
357 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200358 }
359 }
360
Jason Wang1ceef9f2013-01-30 19:12:28 +0800361 for (i = queues - 1; i >= 0; i--) {
362 NetClientState *nc = qemu_get_subqueue(nic, i);
363
364 qemu_cleanup_net_client(nc);
365 qemu_free_net_client(nc);
366 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800367
368 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200369}
370
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000371void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
372{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100373 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000374
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100375 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200376 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800377 if (nc->queue_index == 0) {
378 func(qemu_get_nic(nc), opaque);
379 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000380 }
381 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000382}
383
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100384int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000385{
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100386 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100387 return 1;
388 }
389
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100390 if (sender->peer->receive_disabled) {
391 return 0;
392 } else if (sender->peer->info->can_receive &&
393 !sender->peer->info->can_receive(sender->peer)) {
394 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000395 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500396 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000397}
398
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100399ssize_t qemu_deliver_packet(NetClientState *sender,
400 unsigned flags,
401 const uint8_t *data,
402 size_t size,
403 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100404{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100405 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000406 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100407
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100408 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100409 return size;
410 }
411
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100412 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000413 return 0;
414 }
415
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100416 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
417 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000418 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100419 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000420 }
421
422 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100423 nc->receive_disabled = 1;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000424 };
425
426 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100427}
428
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100429void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000430{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100431 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100432 return;
433 }
434
Jan Kiszka067404b2013-08-02 21:47:08 +0200435 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100436}
437
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100438void qemu_flush_queued_packets(NetClientState *nc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100439{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100440 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100441
Luigi Rizzo199ee602013-02-05 17:53:31 +0100442 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
443 if (net_hub_flush(nc->peer)) {
444 qemu_notify_event();
445 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100446 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200447 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200448 /* We emptied the queue successfully, signal to the IO thread to repoll
449 * the file descriptor (for tap, for example).
450 */
451 qemu_notify_event();
452 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100453}
454
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100455static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100456 unsigned flags,
457 const uint8_t *buf, int size,
458 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000459{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100460 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100461
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100462#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100463 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100464 hex_dump(stdout, buf, size);
465#endif
466
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100467 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100468 return size;
469 }
470
Jan Kiszka067404b2013-08-02 21:47:08 +0200471 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100472
Mark McLoughlinca77d172009-10-22 17:43:41 +0100473 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
474}
475
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100476ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100477 const uint8_t *buf, int size,
478 NetPacketSent *sent_cb)
479{
480 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
481 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100482}
483
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100484void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100485{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100486 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000487}
488
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100489ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100490{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100491 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100492 buf, size, NULL);
493}
494
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100495static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000496 int iovcnt)
497{
Scott Feldmand32fcad2013-03-18 11:43:44 -0700498 uint8_t buffer[NET_BUFSIZE];
Benjamin Poirierce053662011-02-23 19:57:21 -0500499 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000500
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400501 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000502
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100503 return nc->info->receive(nc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000504}
505
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100506ssize_t qemu_deliver_packet_iov(NetClientState *sender,
507 unsigned flags,
508 const struct iovec *iov,
509 int iovcnt,
510 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100511{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100512 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100513 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100514
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100515 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500516 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100517 }
518
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100519 if (nc->receive_disabled) {
520 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100521 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100522
523 if (nc->info->receive_iov) {
524 ret = nc->info->receive_iov(nc, iov, iovcnt);
525 } else {
526 ret = nc_sendv_compat(nc, iov, iovcnt);
527 }
528
529 if (ret == 0) {
530 nc->receive_disabled = 1;
531 }
532
533 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100534}
535
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100536ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100537 const struct iovec *iov, int iovcnt,
538 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000539{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100540 NetQueue *queue;
541
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100542 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500543 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000544 }
545
Jan Kiszka067404b2013-08-02 21:47:08 +0200546 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100547
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100548 return qemu_net_queue_send_iov(queue, sender,
549 QEMU_NET_PACKET_FLAG_NONE,
550 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000551}
552
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100553ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100554qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100555{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100556 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100557}
558
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100559NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000560{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100561 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100562
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100563 QTAILQ_FOREACH(nc, &net_clients, next) {
564 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200565 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100566 if (!strcmp(nc->name, id)) {
567 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100568 }
569 }
570
571 return NULL;
572}
573
Jason Wang6c51ae72013-01-30 19:12:25 +0800574int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
575 NetClientOptionsKind type, int max)
576{
577 NetClientState *nc;
578 int ret = 0;
579
580 QTAILQ_FOREACH(nc, &net_clients, next) {
581 if (nc->info->type == type) {
582 continue;
583 }
584 if (!strcmp(nc->name, id)) {
585 if (ret < max) {
586 ncs[ret] = nc;
587 }
588 ret++;
589 }
590 }
591
592 return ret;
593}
594
aliguori76970792009-02-11 15:20:03 +0000595static int nic_get_free_idx(void)
596{
597 int index;
598
599 for (index = 0; index < MAX_NICS; index++)
600 if (!nd_table[index].used)
601 return index;
602 return -1;
603}
604
Markus Armbruster07caea32009-09-25 03:53:51 +0200605int qemu_show_nic_models(const char *arg, const char *const *models)
606{
607 int i;
608
Peter Maydellc8057f92012-08-02 13:45:54 +0100609 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200610 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100611 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200612
613 fprintf(stderr, "qemu: Supported NIC models: ");
614 for (i = 0 ; models[i]; i++)
615 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
616 return 1;
617}
618
aliguorid07f22c2009-01-13 19:03:57 +0000619void qemu_check_nic_model(NICInfo *nd, const char *model)
620{
621 const char *models[2];
622
623 models[0] = model;
624 models[1] = NULL;
625
Markus Armbruster07caea32009-09-25 03:53:51 +0200626 if (qemu_show_nic_models(nd->model, models))
627 exit(0);
628 if (qemu_find_nic_model(nd, models, model) < 0)
629 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000630}
631
Markus Armbruster07caea32009-09-25 03:53:51 +0200632int qemu_find_nic_model(NICInfo *nd, const char * const *models,
633 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000634{
Markus Armbruster07caea32009-09-25 03:53:51 +0200635 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000636
637 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500638 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000639
Markus Armbruster07caea32009-09-25 03:53:51 +0200640 for (i = 0 ; models[i]; i++) {
641 if (strcmp(nd->model, models[i]) == 0)
642 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000643 }
644
Markus Armbruster6daf1942011-06-22 14:03:54 +0200645 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200646 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000647}
648
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200649static int net_init_nic(const NetClientOptions *opts, const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100650 NetClientState *peer)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100651{
652 int idx;
653 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200654 const NetLegacyNicOptions *nic;
655
656 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
657 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100658
659 idx = nic_get_free_idx();
660 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100661 error_report("Too Many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100662 return -1;
663 }
664
665 nd = &nd_table[idx];
666
667 memset(nd, 0, sizeof(*nd));
668
Laszlo Ersek2456f362012-07-17 16:17:14 +0200669 if (nic->has_netdev) {
670 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100671 if (!nd->netdev) {
Laszlo Ersek2456f362012-07-17 16:17:14 +0200672 error_report("netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100673 return -1;
674 }
675 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100676 assert(peer);
677 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100678 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100679 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200680 if (nic->has_model) {
681 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100682 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200683 if (nic->has_addr) {
684 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100685 }
686
Laszlo Ersek2456f362012-07-17 16:17:14 +0200687 if (nic->has_macaddr &&
688 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100689 error_report("invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100690 return -1;
691 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400692 if (nic->has_macaddr &&
693 is_multicast_ether_addr(nd->macaddr.a)) {
694 error_report("NIC cannot have multicast MAC address (odd 1st byte)");
695 return -1;
696 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200697 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100698
Laszlo Ersek2456f362012-07-17 16:17:14 +0200699 if (nic->has_vectors) {
700 if (nic->vectors > 0x7ffffff) {
701 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
702 return -1;
703 }
704 nd->nvectors = nic->vectors;
705 } else {
706 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100707 }
708
709 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100710 nb_nics++;
711
712 return idx;
713}
714
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100715
Laszlo Ersek6687b792012-07-17 16:17:13 +0200716static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200717 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200718 const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100719 NetClientState *peer) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100720 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100721#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100722 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100723#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100724 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
725 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100726#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100727 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100728#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100729#ifdef CONFIG_NETMAP
730 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
731#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100732 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100733#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100734 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200735#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100736 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100737};
738
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100739
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200740static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200741{
742 union {
743 const Netdev *netdev;
744 const NetLegacy *net;
745 } u;
746 const NetClientOptions *opts;
747 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100748
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100749 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200750 u.netdev = object;
751 opts = u.netdev->opts;
752 name = u.netdev->id;
753
754 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100755#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200756 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100757#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200758 case NET_CLIENT_OPTIONS_KIND_TAP:
759 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100760#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200761 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100762#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100763#ifdef CONFIG_NETMAP
764 case NET_CLIENT_OPTIONS_KIND_NETMAP:
765#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200766#ifdef CONFIG_NET_BRIDGE
767 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
768#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100769 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Laszlo Ersek6687b792012-07-17 16:17:13 +0200770 break;
771
772 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300773 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
774 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100775 return -1;
776 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200777 } else {
778 u.net = object;
779 opts = u.net->opts;
780 /* missing optional values have been initialized to "all bits zero" */
781 name = u.net->has_id ? u.net->id : u.net->name;
782 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100783
Laszlo Ersek6687b792012-07-17 16:17:13 +0200784 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100785 NetClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200786
787 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
788 * parameter. */
789 if (!is_netdev &&
790 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
791 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100792 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100793 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200794
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100795 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200796 /* TODO push error reporting into init() methods */
797 error_set(errp, QERR_DEVICE_INIT_FAILED,
798 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100799 return -1;
800 }
801 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200802 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100803}
804
Laszlo Ersek6687b792012-07-17 16:17:13 +0200805
806static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
807{
808 if (is_netdev) {
809 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
810 } else {
811 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
812 }
813}
814
815
816int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
817{
818 void *object = NULL;
819 Error *err = NULL;
820 int ret = -1;
821
822 {
823 OptsVisitor *ov = opts_visitor_new(opts);
824
825 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
826 opts_visitor_cleanup(ov);
827 }
828
829 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200830 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +0200831 }
832
833 if (object) {
834 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
835
836 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
837 qapi_dealloc_visitor_cleanup(dv);
838 }
839
840 error_propagate(errp, err);
841 return ret;
842}
843
844
aliguori6f338c32009-02-11 15:21:54 +0000845static int net_host_check_device(const char *device)
846{
847 int i;
aliguoribb9ea792009-04-21 19:56:28 +0000848 const char *valid_param_list[] = { "tap", "socket", "dump"
Stefan Weil2944e4e2012-02-04 09:24:46 +0100849#ifdef CONFIG_NET_BRIDGE
850 , "bridge"
851#endif
aliguori6f338c32009-02-11 15:21:54 +0000852#ifdef CONFIG_SLIRP
853 ,"user"
854#endif
855#ifdef CONFIG_VDE
856 ,"vde"
857#endif
858 };
Stefan Weildff74242013-12-07 14:48:04 +0100859 for (i = 0; i < ARRAY_SIZE(valid_param_list); i++) {
aliguori6f338c32009-02-11 15:21:54 +0000860 if (!strncmp(valid_param_list[i], device,
861 strlen(valid_param_list[i])))
862 return 1;
863 }
864
865 return 0;
866}
867
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300868void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000869{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300870 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100871 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300872 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100873 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300874
aliguori6f338c32009-02-11 15:21:54 +0000875 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +0000876 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +0000877 return;
878 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100879
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200880 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100881 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100882 return;
883 }
884
885 qemu_opt_set(opts, "type", device);
886
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300887 net_client_init(opts, 0, &local_err);
888 if (error_is_set(&local_err)) {
889 qerror_report_err(local_err);
890 error_free(local_err);
aliguori5c8be672009-04-21 19:56:32 +0000891 monitor_printf(mon, "adding host network device %s failed\n", device);
892 }
aliguori6f338c32009-02-11 15:21:54 +0000893}
894
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300895void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000896{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100897 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300898 int vlan_id = qdict_get_int(qdict, "vlan_id");
899 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +0000900
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100901 nc = net_hub_find_client_by_name(vlan_id, device);
902 if (!nc) {
aliguori6f338c32009-02-11 15:21:54 +0000903 return;
904 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100905 if (!net_host_check_device(nc->model)) {
aliguorie8f1f9d2009-04-21 19:56:08 +0000906 monitor_printf(mon, "invalid host network device %s\n", device);
907 return;
908 }
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100909 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +0000910}
911
Luiz Capitulino928059a2012-04-18 17:34:15 -0300912void netdev_add(QemuOpts *opts, Error **errp)
913{
914 net_client_init(opts, 1, errp);
915}
916
917int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +0100918{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300919 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -0300920 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100921 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100922
Luiz Capitulino928059a2012-04-18 17:34:15 -0300923 opts_list = qemu_find_opts_err("netdev", &local_err);
924 if (error_is_set(&local_err)) {
925 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100926 }
927
Luiz Capitulino928059a2012-04-18 17:34:15 -0300928 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
929 if (error_is_set(&local_err)) {
930 goto exit_err;
931 }
932
933 netdev_add(opts, &local_err);
934 if (error_is_set(&local_err)) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +0900935 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -0300936 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +0900937 }
938
Luiz Capitulino928059a2012-04-18 17:34:15 -0300939 return 0;
940
941exit_err:
942 qerror_report_err(local_err);
943 error_free(local_err);
944 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100945}
946
Luiz Capitulino5f964152012-04-16 14:36:32 -0300947void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +0100948{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100949 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +0200950 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100951
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100952 nc = qemu_find_netdev(id);
953 if (!nc) {
Luiz Capitulino5f964152012-04-16 14:36:32 -0300954 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
955 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100956 }
Luiz Capitulino5f964152012-04-16 14:36:32 -0300957
Stefan Hajnoczi645c9492012-10-24 14:34:12 +0200958 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
959 if (!opts) {
960 error_setg(errp, "Device '%s' is not a netdev", id);
961 return;
962 }
963
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100964 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +0200965 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +0100966}
967
Zhi Yong Wu1a859592012-07-24 16:35:16 +0100968void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +0200969{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800970 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
971 nc->queue_index,
972 NetClientOptionsKind_lookup[nc->info->type],
973 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +0200974}
975
Amos Kongb1be4282013-06-14 15:45:52 +0800976RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
977 Error **errp)
978{
979 NetClientState *nc;
980 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
981
982 QTAILQ_FOREACH(nc, &net_clients, next) {
983 RxFilterInfoList *entry;
984 RxFilterInfo *info;
985
986 if (has_name && strcmp(nc->name, name) != 0) {
987 continue;
988 }
989
990 /* only query rx-filter information of NIC */
991 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
992 if (has_name) {
993 error_setg(errp, "net client(%s) isn't a NIC", name);
994 break;
995 }
996 continue;
997 }
998
999 if (nc->info->query_rx_filter) {
1000 info = nc->info->query_rx_filter(nc);
1001 entry = g_malloc0(sizeof(*entry));
1002 entry->value = info;
1003
1004 if (!filter_list) {
1005 filter_list = entry;
1006 } else {
1007 last_entry->next = entry;
1008 }
1009 last_entry = entry;
1010 } else if (has_name) {
1011 error_setg(errp, "net client(%s) doesn't support"
1012 " rx-filter querying", name);
1013 break;
1014 }
1015 }
1016
1017 if (filter_list == NULL && !error_is_set(errp) && has_name) {
1018 error_setg(errp, "invalid net client name: %s", name);
1019 }
1020
1021 return filter_list;
1022}
1023
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001024void do_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001025{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001026 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001027 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001028
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001029 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001030
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001031 QTAILQ_FOREACH(nc, &net_clients, next) {
1032 peer = nc->peer;
1033 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001034
1035 /* Skip if already printed in hub info */
1036 if (net_hub_id_for_client(nc, NULL) == 0) {
1037 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001038 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001039
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001040 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001041 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001042 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001043 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001044 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001045 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001046 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001047 }
aliguori63a01ef2008-10-31 19:10:00 +00001048}
1049
Luiz Capitulino4b371562011-11-23 13:11:55 -02001050void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001051{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001052 NetClientState *ncs[MAX_QUEUE_NUM];
1053 NetClientState *nc;
1054 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001055
Jason Wang1ceef9f2013-01-30 19:12:28 +08001056 queues = qemu_find_net_clients_except(name, ncs,
1057 NET_CLIENT_OPTIONS_KIND_MAX,
1058 MAX_QUEUE_NUM);
1059
1060 if (queues == 0) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001061 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1062 return;
aliguori436e5e52009-01-08 19:44:06 +00001063 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001064 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001065
Jason Wang1ceef9f2013-01-30 19:12:28 +08001066 for (i = 0; i < queues; i++) {
1067 ncs[i]->link_down = !up;
1068 }
aliguori436e5e52009-01-08 19:44:06 +00001069
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001070 if (nc->info->link_status_changed) {
1071 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001072 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001073
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001074 if (nc->peer) {
1075 /* Change peer link only if the peer is NIC and then notify peer.
1076 * If the peer is a HUBPORT or a backend, we do not change the
1077 * link status.
1078 *
1079 * This behavior is compatible with qemu vlans where there could be
1080 * multiple clients that can still communicate with each other in
1081 * disconnected mode. For now maintain this compatibility.
1082 */
1083 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1084 for (i = 0; i < queues; i++) {
1085 ncs[i]->peer->link_down = !up;
1086 }
1087 }
1088 if (nc->peer->info->link_status_changed) {
1089 nc->peer->info->link_status_changed(nc->peer);
1090 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001091 }
aliguori436e5e52009-01-08 19:44:06 +00001092}
1093
aliguori63a01ef2008-10-31 19:10:00 +00001094void net_cleanup(void)
1095{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001096 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001097
Jason Wang1ceef9f2013-01-30 19:12:28 +08001098 /* We may del multiple entries during qemu_del_net_client(),
1099 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1100 */
1101 while (!QTAILQ_EMPTY(&net_clients)) {
1102 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001103 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1104 qemu_del_nic(qemu_get_nic(nc));
1105 } else {
1106 qemu_del_net_client(nc);
1107 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001108 }
aliguori63a01ef2008-10-31 19:10:00 +00001109}
1110
Markus Armbruster668680f2010-02-11 14:44:58 +01001111void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001112{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001113 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001114 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001115
Peter Maydell641f6ea2011-05-20 16:50:00 +01001116 /* Don't warn about the default network setup that you get if
1117 * no command line -net or -netdev options are specified. There
1118 * are two cases that we would otherwise complain about:
1119 * (1) board doesn't support a NIC but the implicit "-net nic"
1120 * requested one
1121 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1122 * sets up a nic that isn't connected to anything.
1123 */
1124 if (default_net) {
1125 return;
1126 }
1127
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001128 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001129
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001130 QTAILQ_FOREACH(nc, &net_clients, next) {
1131 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001132 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001133 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1134 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001135 }
1136 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001137
1138 /* Check that all NICs requested via -net nic actually got created.
1139 * NICs created via -device don't need to be checked here because
1140 * they are always instantiated.
1141 */
1142 for (i = 0; i < MAX_NICS; i++) {
1143 NICInfo *nd = &nd_table[i];
1144 if (nd->used && !nd->instantiated) {
1145 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1146 "was not created (not supported by this machine?)\n",
1147 nd->name ? nd->name : "anonymous",
1148 nd->model ? nd->model : "unspecified");
1149 }
1150 }
aliguori63a01ef2008-10-31 19:10:00 +00001151}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001152
1153static int net_init_client(QemuOpts *opts, void *dummy)
1154{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001155 Error *local_err = NULL;
1156
1157 net_client_init(opts, 0, &local_err);
1158 if (error_is_set(&local_err)) {
1159 qerror_report_err(local_err);
1160 error_free(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001161 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001162 }
1163
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001164 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001165}
1166
1167static int net_init_netdev(QemuOpts *opts, void *dummy)
1168{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001169 Error *local_err = NULL;
1170 int ret;
1171
1172 ret = net_client_init(opts, 1, &local_err);
1173 if (error_is_set(&local_err)) {
1174 qerror_report_err(local_err);
1175 error_free(local_err);
1176 return -1;
1177 }
1178
1179 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001180}
1181
1182int net_init_clients(void)
1183{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001184 QemuOptsList *net = qemu_find_opts("net");
1185
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001186 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001187 /* if no clients, we use a default config */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001188 qemu_opts_set(net, NULL, "type", "nic");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001189#ifdef CONFIG_SLIRP
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001190 qemu_opts_set(net, NULL, "type", "user");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001191#endif
1192 }
1193
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001194 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001195
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001196 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001197 return -1;
1198
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001199 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001200 return -1;
1201 }
1202
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001203 return 0;
1204}
1205
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001206int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001207{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001208#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001209 int ret;
1210 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001211 return ret;
1212 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001213#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001214
Markus Armbruster8212c642010-02-10 19:52:18 +01001215 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001216 return -1;
1217 }
1218
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001219 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001220 return 0;
1221}
Jason Wang7fc8d912012-03-05 11:08:50 +08001222
1223/* From FreeBSD */
1224/* XXX: optimize */
1225unsigned compute_mcast_idx(const uint8_t *ep)
1226{
1227 uint32_t crc;
1228 int carry, i, j;
1229 uint8_t b;
1230
1231 crc = 0xffffffff;
1232 for (i = 0; i < 6; i++) {
1233 b = *ep++;
1234 for (j = 0; j < 8; j++) {
1235 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1236 crc <<= 1;
1237 b >>= 1;
1238 if (carry) {
1239 crc = ((crc ^ POLYNOMIAL) | carry);
1240 }
1241 }
1242 }
1243 return crc >> 26;
1244}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001245
1246QemuOptsList qemu_netdev_opts = {
1247 .name = "netdev",
1248 .implied_opt_name = "type",
1249 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1250 .desc = {
1251 /*
1252 * no elements => accept any params
1253 * validation will happen later
1254 */
1255 { /* end of list */ }
1256 },
1257};
1258
1259QemuOptsList qemu_net_opts = {
1260 .name = "net",
1261 .implied_opt_name = "type",
1262 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1263 .desc = {
1264 /*
1265 * no elements => accept any params
1266 * validation will happen later
1267 */
1268 { /* end of list */ }
1269 },
1270};