blob: c330c9a3a89f30ace3fa49ae2f6e63319f698684 [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"
Paolo Bonzinifd9400b2012-10-24 11:27:28 +020030#include "util.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020031
Paolo Bonzini83c90892012-12-17 18:19:49 +010032#include "monitor/monitor.h"
Mark McLoughlin1df49e02009-11-25 18:48:58 +000033#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010034#include "qemu/sockets.h"
35#include "qemu/config-file.h"
Luiz Capitulino4b371562011-11-23 13:11:55 -020036#include "qmp-commands.h"
Amit Shah75422b02010-02-25 17:24:43 +053037#include "hw/qdev.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010039#include "qemu/main-loop.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020040#include "qapi-visit.h"
41#include "qapi/opts-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010042#include "qapi/dealloc-visitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000043
Stefan Weil2944e4e2012-02-04 09:24:46 +010044/* Net bridge is currently not supported for W32. */
45#if !defined(_WIN32)
46# define CONFIG_NET_BRIDGE
47#endif
48
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010049static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000050
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010051int default_net = 1;
52
aliguori63a01ef2008-10-31 19:10:00 +000053/***********************************************************/
54/* network device redirectors */
55
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000056#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000057static void hex_dump(FILE *f, const uint8_t *buf, int size)
58{
59 int len, i, j, c;
60
61 for(i=0;i<size;i+=16) {
62 len = size - i;
63 if (len > 16)
64 len = 16;
65 fprintf(f, "%08x ", i);
66 for(j=0;j<16;j++) {
67 if (j < len)
68 fprintf(f, " %02x", buf[i+j]);
69 else
70 fprintf(f, " ");
71 }
72 fprintf(f, " ");
73 for(j=0;j<len;j++) {
74 c = buf[i+j];
75 if (c < ' ' || c > '~')
76 c = '.';
77 fprintf(f, "%c", c);
78 }
79 fprintf(f, "\n");
80 }
81}
82#endif
83
aliguori63a01ef2008-10-31 19:10:00 +000084static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
85{
86 const char *p, *p1;
87 int len;
88 p = *pp;
89 p1 = strchr(p, sep);
90 if (!p1)
91 return -1;
92 len = p1 - p;
93 p1++;
94 if (buf_size > 0) {
95 if (len > buf_size - 1)
96 len = buf_size - 1;
97 memcpy(buf, p, len);
98 buf[len] = '\0';
99 }
100 *pp = p1;
101 return 0;
102}
103
aliguori63a01ef2008-10-31 19:10:00 +0000104int parse_host_port(struct sockaddr_in *saddr, const char *str)
105{
106 char buf[512];
107 struct hostent *he;
108 const char *p, *r;
109 int port;
110
111 p = str;
112 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
113 return -1;
114 saddr->sin_family = AF_INET;
115 if (buf[0] == '\0') {
116 saddr->sin_addr.s_addr = 0;
117 } else {
blueswir1cd390082008-11-16 13:53:32 +0000118 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000119 if (!inet_aton(buf, &saddr->sin_addr))
120 return -1;
121 } else {
122 if ((he = gethostbyname(buf)) == NULL)
123 return - 1;
124 saddr->sin_addr = *(struct in_addr *)he->h_addr;
125 }
126 }
127 port = strtol(p, (char **)&r, 0);
128 if (r == p)
129 return -1;
130 saddr->sin_port = htons(port);
131 return 0;
132}
133
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100134void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000135{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100136 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000137 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100138 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000139 macaddr[0], macaddr[1], macaddr[2],
140 macaddr[3], macaddr[4], macaddr[5]);
141}
142
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200143void qemu_macaddr_default_if_unset(MACAddr *macaddr)
144{
145 static int index = 0;
146 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
147
148 if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
149 return;
150 macaddr->a[0] = 0x52;
151 macaddr->a[1] = 0x54;
152 macaddr->a[2] = 0x00;
153 macaddr->a[3] = 0x12;
154 macaddr->a[4] = 0x34;
155 macaddr->a[5] = 0x56 + index++;
156}
157
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100158/**
159 * Generate a name for net client
160 *
Amos Kongc9635302013-04-15 18:55:19 +0800161 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100162 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100163static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000164{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100165 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000166 char buf[256];
167 int id = 0;
168
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100169 QTAILQ_FOREACH(nc, &net_clients, next) {
170 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100171 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100172 }
Amos Kongc9635302013-04-15 18:55:19 +0800173 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200174 id++;
175 }
176 }
177
aliguori676cff22009-01-07 17:43:44 +0000178 snprintf(buf, sizeof(buf), "%s.%d", model, id);
179
Anthony Liguori7267c092011-08-20 22:09:37 -0500180 return g_strdup(buf);
aliguori676cff22009-01-07 17:43:44 +0000181}
182
Jason Wangf7860452013-01-30 19:12:27 +0800183static void qemu_net_client_destructor(NetClientState *nc)
184{
185 g_free(nc);
186}
187
Jason Wang18a15412013-01-30 19:12:26 +0800188static void qemu_net_client_setup(NetClientState *nc,
189 NetClientInfo *info,
190 NetClientState *peer,
191 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800192 const char *name,
193 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000194{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100195 nc->info = info;
196 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000197 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100198 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000199 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100200 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000201 }
aliguori63a01ef2008-10-31 19:10:00 +0000202
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100203 if (peer) {
204 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100205 nc->peer = peer;
206 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100207 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100208 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100209
Jan Kiszka067404b2013-08-02 21:47:08 +0200210 nc->incoming_queue = qemu_new_net_queue(nc);
Jason Wangf7860452013-01-30 19:12:27 +0800211 nc->destructor = destructor;
Jason Wang18a15412013-01-30 19:12:26 +0800212}
213
214NetClientState *qemu_new_net_client(NetClientInfo *info,
215 NetClientState *peer,
216 const char *model,
217 const char *name)
218{
219 NetClientState *nc;
220
221 assert(info->size >= sizeof(NetClientState));
222
223 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800224 qemu_net_client_setup(nc, info, peer, model, name,
225 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800226
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100227 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000228}
229
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000230NICState *qemu_new_nic(NetClientInfo *info,
231 NICConf *conf,
232 const char *model,
233 const char *name,
234 void *opaque)
235{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800236 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000237 NICState *nic;
Jason Wangf6b26cf2013-02-22 23:15:06 +0800238 int i, queues = MAX(1, conf->queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000239
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200240 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000241 assert(info->size >= sizeof(NICState));
242
Jason Wangf6b26cf2013-02-22 23:15:06 +0800243 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
244 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000245 nic->conf = conf;
246 nic->opaque = opaque;
247
Jason Wangf6b26cf2013-02-22 23:15:06 +0800248 for (i = 0; i < queues; i++) {
249 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800250 NULL);
251 nic->ncs[i].queue_index = i;
252 }
253
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000254 return nic;
255}
256
Jason Wang1ceef9f2013-01-30 19:12:28 +0800257NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
258{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800259 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800260}
261
Jason Wangb356f762013-01-30 19:12:22 +0800262NetClientState *qemu_get_queue(NICState *nic)
263{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800264 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800265}
266
Jason Wangcc1f0f42013-01-30 19:12:23 +0800267NICState *qemu_get_nic(NetClientState *nc)
268{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800269 NetClientState *nc0 = nc - nc->queue_index;
270
Jason Wangf6b26cf2013-02-22 23:15:06 +0800271 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800272}
273
274void *qemu_get_nic_opaque(NetClientState *nc)
275{
276 NICState *nic = qemu_get_nic(nc);
277
278 return nic->opaque;
279}
280
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100281static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000282{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100283 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000284
Andreas Färbercc2a9042013-02-12 23:16:06 +0100285 if (nc->info->cleanup) {
286 nc->info->cleanup(nc);
287 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200288}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100289
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100290static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200291{
Jan Kiszka067404b2013-08-02 21:47:08 +0200292 if (nc->incoming_queue) {
293 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200294 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100295 if (nc->peer) {
296 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100297 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100298 g_free(nc->name);
299 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800300 if (nc->destructor) {
301 nc->destructor(nc);
302 }
aliguori63a01ef2008-10-31 19:10:00 +0000303}
304
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100305void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200306{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800307 NetClientState *ncs[MAX_QUEUE_NUM];
308 int queues, i;
309
310 /* If the NetClientState belongs to a multiqueue backend, we will change all
311 * other NetClientStates also.
312 */
313 queues = qemu_find_net_clients_except(nc->name, ncs,
314 NET_CLIENT_OPTIONS_KIND_NIC,
315 MAX_QUEUE_NUM);
316 assert(queues != 0);
317
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200318 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100319 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800320 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200321 if (nic->peer_deleted) {
322 return;
323 }
324 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800325
326 for (i = 0; i < queues; i++) {
327 ncs[i]->peer->link_down = true;
328 }
329
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100330 if (nc->peer->info->link_status_changed) {
331 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200332 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800333
334 for (i = 0; i < queues; i++) {
335 qemu_cleanup_net_client(ncs[i]);
336 }
337
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200338 return;
339 }
340
Jason Wang948ecf22013-01-30 19:12:24 +0800341 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
342
Jason Wang1ceef9f2013-01-30 19:12:28 +0800343 for (i = 0; i < queues; i++) {
344 qemu_cleanup_net_client(ncs[i]);
345 qemu_free_net_client(ncs[i]);
346 }
Jason Wang948ecf22013-01-30 19:12:24 +0800347}
348
349void qemu_del_nic(NICState *nic)
350{
Michael Rothb8904922013-02-06 18:25:48 -0600351 int i, queues = MAX(nic->conf->queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800352
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200353 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800354 if (nic->peer_deleted) {
355 for (i = 0; i < queues; i++) {
356 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200357 }
358 }
359
Jason Wang1ceef9f2013-01-30 19:12:28 +0800360 for (i = queues - 1; i >= 0; i--) {
361 NetClientState *nc = qemu_get_subqueue(nic, i);
362
363 qemu_cleanup_net_client(nc);
364 qemu_free_net_client(nc);
365 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800366
367 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200368}
369
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000370void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
371{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100372 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000373
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100374 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200375 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800376 if (nc->queue_index == 0) {
377 func(qemu_get_nic(nc), opaque);
378 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000379 }
380 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000381}
382
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100383int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000384{
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100385 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100386 return 1;
387 }
388
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100389 if (sender->peer->receive_disabled) {
390 return 0;
391 } else if (sender->peer->info->can_receive &&
392 !sender->peer->info->can_receive(sender->peer)) {
393 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000394 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500395 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000396}
397
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100398ssize_t qemu_deliver_packet(NetClientState *sender,
399 unsigned flags,
400 const uint8_t *data,
401 size_t size,
402 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100403{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100404 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000405 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100406
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100407 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100408 return size;
409 }
410
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100411 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000412 return 0;
413 }
414
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100415 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
416 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000417 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100418 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000419 }
420
421 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100422 nc->receive_disabled = 1;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000423 };
424
425 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100426}
427
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100428void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000429{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100430 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100431 return;
432 }
433
Jan Kiszka067404b2013-08-02 21:47:08 +0200434 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100435}
436
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100437void qemu_flush_queued_packets(NetClientState *nc)
Mark McLoughline94667b2009-04-29 11:48:12 +0100438{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100439 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100440
Luigi Rizzo199ee602013-02-05 17:53:31 +0100441 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
442 if (net_hub_flush(nc->peer)) {
443 qemu_notify_event();
444 }
445 return;
446 }
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 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200692 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100693
Laszlo Ersek2456f362012-07-17 16:17:14 +0200694 if (nic->has_vectors) {
695 if (nic->vectors > 0x7ffffff) {
696 error_report("invalid # of vectors: %"PRIu32, nic->vectors);
697 return -1;
698 }
699 nd->nvectors = nic->vectors;
700 } else {
701 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100702 }
703
704 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100705 nb_nics++;
706
707 return idx;
708}
709
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100710
Laszlo Ersek6687b792012-07-17 16:17:13 +0200711static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200712 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200713 const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100714 NetClientState *peer) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100715 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100716#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100717 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100718#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100719 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
720 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100721#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100722 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100723#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100724 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100725#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100726 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200727#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100728 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100729};
730
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100731
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200732static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200733{
734 union {
735 const Netdev *netdev;
736 const NetLegacy *net;
737 } u;
738 const NetClientOptions *opts;
739 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100740
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100741 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200742 u.netdev = object;
743 opts = u.netdev->opts;
744 name = u.netdev->id;
745
746 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100747#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200748 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100749#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200750 case NET_CLIENT_OPTIONS_KIND_TAP:
751 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100752#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200753 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100754#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200755#ifdef CONFIG_NET_BRIDGE
756 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
757#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100758 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Laszlo Ersek6687b792012-07-17 16:17:13 +0200759 break;
760
761 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300762 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
763 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100764 return -1;
765 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200766 } else {
767 u.net = object;
768 opts = u.net->opts;
769 /* missing optional values have been initialized to "all bits zero" */
770 name = u.net->has_id ? u.net->id : u.net->name;
771 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100772
Laszlo Ersek6687b792012-07-17 16:17:13 +0200773 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100774 NetClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200775
776 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
777 * parameter. */
778 if (!is_netdev &&
779 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
780 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100781 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100782 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200783
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100784 if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200785 /* TODO push error reporting into init() methods */
786 error_set(errp, QERR_DEVICE_INIT_FAILED,
787 NetClientOptionsKind_lookup[opts->kind]);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100788 return -1;
789 }
790 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200791 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100792}
793
Laszlo Ersek6687b792012-07-17 16:17:13 +0200794
795static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
796{
797 if (is_netdev) {
798 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
799 } else {
800 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
801 }
802}
803
804
805int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
806{
807 void *object = NULL;
808 Error *err = NULL;
809 int ret = -1;
810
811 {
812 OptsVisitor *ov = opts_visitor_new(opts);
813
814 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
815 opts_visitor_cleanup(ov);
816 }
817
818 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200819 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +0200820 }
821
822 if (object) {
823 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
824
825 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
826 qapi_dealloc_visitor_cleanup(dv);
827 }
828
829 error_propagate(errp, err);
830 return ret;
831}
832
833
aliguori6f338c32009-02-11 15:21:54 +0000834static int net_host_check_device(const char *device)
835{
836 int i;
aliguoribb9ea792009-04-21 19:56:28 +0000837 const char *valid_param_list[] = { "tap", "socket", "dump"
Stefan Weil2944e4e2012-02-04 09:24:46 +0100838#ifdef CONFIG_NET_BRIDGE
839 , "bridge"
840#endif
aliguori6f338c32009-02-11 15:21:54 +0000841#ifdef CONFIG_SLIRP
842 ,"user"
843#endif
844#ifdef CONFIG_VDE
845 ,"vde"
846#endif
847 };
848 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
849 if (!strncmp(valid_param_list[i], device,
850 strlen(valid_param_list[i])))
851 return 1;
852 }
853
854 return 0;
855}
856
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300857void net_host_device_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000858{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300859 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100860 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300861 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100862 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300863
aliguori6f338c32009-02-11 15:21:54 +0000864 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +0000865 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +0000866 return;
867 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100868
Gerd Hoffmann3329f072010-08-20 13:52:01 +0200869 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100870 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +0100871 return;
872 }
873
874 qemu_opt_set(opts, "type", device);
875
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300876 net_client_init(opts, 0, &local_err);
877 if (error_is_set(&local_err)) {
878 qerror_report_err(local_err);
879 error_free(local_err);
aliguori5c8be672009-04-21 19:56:32 +0000880 monitor_printf(mon, "adding host network device %s failed\n", device);
881 }
aliguori6f338c32009-02-11 15:21:54 +0000882}
883
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300884void net_host_device_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +0000885{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100886 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -0300887 int vlan_id = qdict_get_int(qdict, "vlan_id");
888 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +0000889
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100890 nc = net_hub_find_client_by_name(vlan_id, device);
891 if (!nc) {
aliguori6f338c32009-02-11 15:21:54 +0000892 return;
893 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100894 if (!net_host_check_device(nc->model)) {
aliguorie8f1f9d2009-04-21 19:56:08 +0000895 monitor_printf(mon, "invalid host network device %s\n", device);
896 return;
897 }
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100898 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +0000899}
900
Luiz Capitulino928059a2012-04-18 17:34:15 -0300901void netdev_add(QemuOpts *opts, Error **errp)
902{
903 net_client_init(opts, 1, errp);
904}
905
906int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +0100907{
Luiz Capitulino4e899782012-04-18 17:24:01 -0300908 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -0300909 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100910 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100911
Luiz Capitulino928059a2012-04-18 17:34:15 -0300912 opts_list = qemu_find_opts_err("netdev", &local_err);
913 if (error_is_set(&local_err)) {
914 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100915 }
916
Luiz Capitulino928059a2012-04-18 17:34:15 -0300917 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
918 if (error_is_set(&local_err)) {
919 goto exit_err;
920 }
921
922 netdev_add(opts, &local_err);
923 if (error_is_set(&local_err)) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +0900924 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -0300925 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +0900926 }
927
Luiz Capitulino928059a2012-04-18 17:34:15 -0300928 return 0;
929
930exit_err:
931 qerror_report_err(local_err);
932 error_free(local_err);
933 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100934}
935
Luiz Capitulino5f964152012-04-16 14:36:32 -0300936void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +0100937{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100938 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +0200939 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100940
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100941 nc = qemu_find_netdev(id);
942 if (!nc) {
Luiz Capitulino5f964152012-04-16 14:36:32 -0300943 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
944 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +0100945 }
Luiz Capitulino5f964152012-04-16 14:36:32 -0300946
Stefan Hajnoczi645c9492012-10-24 14:34:12 +0200947 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
948 if (!opts) {
949 error_setg(errp, "Device '%s' is not a netdev", id);
950 return;
951 }
952
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100953 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +0200954 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +0100955}
956
Zhi Yong Wu1a859592012-07-24 16:35:16 +0100957void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +0200958{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800959 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
960 nc->queue_index,
961 NetClientOptionsKind_lookup[nc->info->type],
962 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +0200963}
964
Amos Kongb1be4282013-06-14 15:45:52 +0800965RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
966 Error **errp)
967{
968 NetClientState *nc;
969 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
970
971 QTAILQ_FOREACH(nc, &net_clients, next) {
972 RxFilterInfoList *entry;
973 RxFilterInfo *info;
974
975 if (has_name && strcmp(nc->name, name) != 0) {
976 continue;
977 }
978
979 /* only query rx-filter information of NIC */
980 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
981 if (has_name) {
982 error_setg(errp, "net client(%s) isn't a NIC", name);
983 break;
984 }
985 continue;
986 }
987
988 if (nc->info->query_rx_filter) {
989 info = nc->info->query_rx_filter(nc);
990 entry = g_malloc0(sizeof(*entry));
991 entry->value = info;
992
993 if (!filter_list) {
994 filter_list = entry;
995 } else {
996 last_entry->next = entry;
997 }
998 last_entry = entry;
999 } else if (has_name) {
1000 error_setg(errp, "net client(%s) doesn't support"
1001 " rx-filter querying", name);
1002 break;
1003 }
1004 }
1005
1006 if (filter_list == NULL && !error_is_set(errp) && has_name) {
1007 error_setg(errp, "invalid net client name: %s", name);
1008 }
1009
1010 return filter_list;
1011}
1012
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08001013void do_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001014{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001015 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001016 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001017
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001018 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001019
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001020 QTAILQ_FOREACH(nc, &net_clients, next) {
1021 peer = nc->peer;
1022 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001023
1024 /* Skip if already printed in hub info */
1025 if (net_hub_id_for_client(nc, NULL) == 0) {
1026 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001027 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001028
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001029 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001030 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001031 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001032 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001033 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001034 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001035 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001036 }
aliguori63a01ef2008-10-31 19:10:00 +00001037}
1038
Luiz Capitulino4b371562011-11-23 13:11:55 -02001039void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001040{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001041 NetClientState *ncs[MAX_QUEUE_NUM];
1042 NetClientState *nc;
1043 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001044
Jason Wang1ceef9f2013-01-30 19:12:28 +08001045 queues = qemu_find_net_clients_except(name, ncs,
1046 NET_CLIENT_OPTIONS_KIND_MAX,
1047 MAX_QUEUE_NUM);
1048
1049 if (queues == 0) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001050 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1051 return;
aliguori436e5e52009-01-08 19:44:06 +00001052 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001053 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001054
Jason Wang1ceef9f2013-01-30 19:12:28 +08001055 for (i = 0; i < queues; i++) {
1056 ncs[i]->link_down = !up;
1057 }
aliguori436e5e52009-01-08 19:44:06 +00001058
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001059 if (nc->info->link_status_changed) {
1060 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001061 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001062
1063 /* Notify peer. Don't update peer link status: this makes it possible to
1064 * disconnect from host network without notifying the guest.
1065 * FIXME: is disconnected link status change operation useful?
1066 *
1067 * Current behaviour is compatible with qemu vlans where there could be
1068 * multiple clients that can still communicate with each other in
1069 * disconnected mode. For now maintain this compatibility. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001070 if (nc->peer && nc->peer->info->link_status_changed) {
1071 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001072 }
aliguori436e5e52009-01-08 19:44:06 +00001073}
1074
aliguori63a01ef2008-10-31 19:10:00 +00001075void net_cleanup(void)
1076{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001077 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001078
Jason Wang1ceef9f2013-01-30 19:12:28 +08001079 /* We may del multiple entries during qemu_del_net_client(),
1080 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1081 */
1082 while (!QTAILQ_EMPTY(&net_clients)) {
1083 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001084 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1085 qemu_del_nic(qemu_get_nic(nc));
1086 } else {
1087 qemu_del_net_client(nc);
1088 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001089 }
aliguori63a01ef2008-10-31 19:10:00 +00001090}
1091
Markus Armbruster668680f2010-02-11 14:44:58 +01001092void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001093{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001094 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001095 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001096
Peter Maydell641f6ea2011-05-20 16:50:00 +01001097 /* Don't warn about the default network setup that you get if
1098 * no command line -net or -netdev options are specified. There
1099 * are two cases that we would otherwise complain about:
1100 * (1) board doesn't support a NIC but the implicit "-net nic"
1101 * requested one
1102 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1103 * sets up a nic that isn't connected to anything.
1104 */
1105 if (default_net) {
1106 return;
1107 }
1108
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001109 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001110
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001111 QTAILQ_FOREACH(nc, &net_clients, next) {
1112 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001113 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001114 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1115 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001116 }
1117 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001118
1119 /* Check that all NICs requested via -net nic actually got created.
1120 * NICs created via -device don't need to be checked here because
1121 * they are always instantiated.
1122 */
1123 for (i = 0; i < MAX_NICS; i++) {
1124 NICInfo *nd = &nd_table[i];
1125 if (nd->used && !nd->instantiated) {
1126 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1127 "was not created (not supported by this machine?)\n",
1128 nd->name ? nd->name : "anonymous",
1129 nd->model ? nd->model : "unspecified");
1130 }
1131 }
aliguori63a01ef2008-10-31 19:10:00 +00001132}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001133
1134static int net_init_client(QemuOpts *opts, void *dummy)
1135{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001136 Error *local_err = NULL;
1137
1138 net_client_init(opts, 0, &local_err);
1139 if (error_is_set(&local_err)) {
1140 qerror_report_err(local_err);
1141 error_free(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001142 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001143 }
1144
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001145 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001146}
1147
1148static int net_init_netdev(QemuOpts *opts, void *dummy)
1149{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001150 Error *local_err = NULL;
1151 int ret;
1152
1153 ret = net_client_init(opts, 1, &local_err);
1154 if (error_is_set(&local_err)) {
1155 qerror_report_err(local_err);
1156 error_free(local_err);
1157 return -1;
1158 }
1159
1160 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001161}
1162
1163int net_init_clients(void)
1164{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001165 QemuOptsList *net = qemu_find_opts("net");
1166
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001167 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001168 /* if no clients, we use a default config */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001169 qemu_opts_set(net, NULL, "type", "nic");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001170#ifdef CONFIG_SLIRP
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001171 qemu_opts_set(net, NULL, "type", "user");
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001172#endif
1173 }
1174
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001175 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001176
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001177 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001178 return -1;
1179
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001180 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001181 return -1;
1182 }
1183
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001184 return 0;
1185}
1186
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001187int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001188{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001189#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001190 int ret;
1191 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001192 return ret;
1193 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001194#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001195
Markus Armbruster8212c642010-02-10 19:52:18 +01001196 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001197 return -1;
1198 }
1199
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001200 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001201 return 0;
1202}
Jason Wang7fc8d912012-03-05 11:08:50 +08001203
1204/* From FreeBSD */
1205/* XXX: optimize */
1206unsigned compute_mcast_idx(const uint8_t *ep)
1207{
1208 uint32_t crc;
1209 int carry, i, j;
1210 uint8_t b;
1211
1212 crc = 0xffffffff;
1213 for (i = 0; i < 6; i++) {
1214 b = *ep++;
1215 for (j = 0; j < 8; j++) {
1216 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1217 crc <<= 1;
1218 b >>= 1;
1219 if (carry) {
1220 crc = ((crc ^ POLYNOMIAL) | carry);
1221 }
1222 }
1223 }
1224 return crc >> 26;
1225}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001226
1227QemuOptsList qemu_netdev_opts = {
1228 .name = "netdev",
1229 .implied_opt_name = "type",
1230 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1231 .desc = {
1232 /*
1233 * no elements => accept any params
1234 * validation will happen later
1235 */
1236 { /* end of list */ }
1237 },
1238};
1239
1240QemuOptsList qemu_net_opts = {
1241 .name = "net",
1242 .implied_opt_name = "type",
1243 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1244 .desc = {
1245 /*
1246 * no elements => accept any params
1247 * validation will happen later
1248 */
1249 { /* end of list */ }
1250 },
1251};