blob: 25c2ef338d4f53ae6470f203746360647f0f7a8e [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"
zhanghailiange1d64c02014-08-26 16:06:17 +080044#include "sysemu/sysemu.h"
blueswir1511d2b12009-03-07 15:32:56 +000045
Stefan Weil2944e4e2012-02-04 09:24:46 +010046/* Net bridge is currently not supported for W32. */
47#if !defined(_WIN32)
48# define CONFIG_NET_BRIDGE
49#endif
50
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030051static VMChangeStateEntry *net_change_state_entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010052static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000053
Hani Benhabiles84007e82014-05-27 23:39:33 +010054const char *host_net_devices[] = {
55 "tap",
56 "socket",
57 "dump",
58#ifdef CONFIG_NET_BRIDGE
59 "bridge",
60#endif
61#ifdef CONFIG_SLIRP
62 "user",
63#endif
64#ifdef CONFIG_VDE
65 "vde",
66#endif
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +030067 "vhost-user",
Hani Benhabiles84007e82014-05-27 23:39:33 +010068 NULL,
69};
70
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +010071int default_net = 1;
72
aliguori63a01ef2008-10-31 19:10:00 +000073/***********************************************************/
74/* network device redirectors */
75
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000076#if defined(DEBUG_NET)
aliguori63a01ef2008-10-31 19:10:00 +000077static void hex_dump(FILE *f, const uint8_t *buf, int size)
78{
79 int len, i, j, c;
80
81 for(i=0;i<size;i+=16) {
82 len = size - i;
83 if (len > 16)
84 len = 16;
85 fprintf(f, "%08x ", i);
86 for(j=0;j<16;j++) {
87 if (j < len)
88 fprintf(f, " %02x", buf[i+j]);
89 else
90 fprintf(f, " ");
91 }
92 fprintf(f, " ");
93 for(j=0;j<len;j++) {
94 c = buf[i+j];
95 if (c < ' ' || c > '~')
96 c = '.';
97 fprintf(f, "%c", c);
98 }
99 fprintf(f, "\n");
100 }
101}
102#endif
103
aliguori63a01ef2008-10-31 19:10:00 +0000104static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
105{
106 const char *p, *p1;
107 int len;
108 p = *pp;
109 p1 = strchr(p, sep);
110 if (!p1)
111 return -1;
112 len = p1 - p;
113 p1++;
114 if (buf_size > 0) {
115 if (len > buf_size - 1)
116 len = buf_size - 1;
117 memcpy(buf, p, len);
118 buf[len] = '\0';
119 }
120 *pp = p1;
121 return 0;
122}
123
aliguori63a01ef2008-10-31 19:10:00 +0000124int parse_host_port(struct sockaddr_in *saddr, const char *str)
125{
126 char buf[512];
127 struct hostent *he;
128 const char *p, *r;
129 int port;
130
131 p = str;
132 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
133 return -1;
134 saddr->sin_family = AF_INET;
135 if (buf[0] == '\0') {
136 saddr->sin_addr.s_addr = 0;
137 } else {
blueswir1cd390082008-11-16 13:53:32 +0000138 if (qemu_isdigit(buf[0])) {
aliguori63a01ef2008-10-31 19:10:00 +0000139 if (!inet_aton(buf, &saddr->sin_addr))
140 return -1;
141 } else {
142 if ((he = gethostbyname(buf)) == NULL)
143 return - 1;
144 saddr->sin_addr = *(struct in_addr *)he->h_addr;
145 }
146 }
147 port = strtol(p, (char **)&r, 0);
148 if (r == p)
149 return -1;
150 saddr->sin_port = htons(port);
151 return 0;
152}
153
Scott Feldman890ee6a2015-03-13 21:09:25 -0700154char *qemu_mac_strdup_printf(const uint8_t *macaddr)
155{
156 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
157 macaddr[0], macaddr[1], macaddr[2],
158 macaddr[3], macaddr[4], macaddr[5]);
159}
160
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100161void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000162{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100163 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000164 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100165 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000166 macaddr[0], macaddr[1], macaddr[2],
167 macaddr[3], macaddr[4], macaddr[5]);
168}
169
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800170static int mac_table[256] = {0};
171
172static void qemu_macaddr_set_used(MACAddr *macaddr)
173{
174 int index;
175
176 for (index = 0x56; index < 0xFF; index++) {
177 if (macaddr->a[5] == index) {
178 mac_table[index]++;
179 }
180 }
181}
182
183static void qemu_macaddr_set_free(MACAddr *macaddr)
184{
185 int index;
186 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
187
188 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
189 return;
190 }
191 for (index = 0x56; index < 0xFF; index++) {
192 if (macaddr->a[5] == index) {
193 mac_table[index]--;
194 }
195 }
196}
197
198static int qemu_macaddr_get_free(void)
199{
200 int index;
201
202 for (index = 0x56; index < 0xFF; index++) {
203 if (mac_table[index] == 0) {
204 return index;
205 }
206 }
207
208 return -1;
209}
210
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200211void qemu_macaddr_default_if_unset(MACAddr *macaddr)
212{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200213 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800214 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200215
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800216 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
217 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
218 return;
219 } else {
220 qemu_macaddr_set_used(macaddr);
221 return;
222 }
223 }
224
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200225 macaddr->a[0] = 0x52;
226 macaddr->a[1] = 0x54;
227 macaddr->a[2] = 0x00;
228 macaddr->a[3] = 0x12;
229 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800230 macaddr->a[5] = qemu_macaddr_get_free();
231 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200232}
233
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100234/**
235 * Generate a name for net client
236 *
Amos Kongc9635302013-04-15 18:55:19 +0800237 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100238 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100239static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000240{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100241 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000242 int id = 0;
243
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100244 QTAILQ_FOREACH(nc, &net_clients, next) {
245 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100246 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100247 }
Amos Kongc9635302013-04-15 18:55:19 +0800248 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200249 id++;
250 }
251 }
252
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100253 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000254}
255
Jason Wangf7860452013-01-30 19:12:27 +0800256static void qemu_net_client_destructor(NetClientState *nc)
257{
258 g_free(nc);
259}
260
Jason Wang18a15412013-01-30 19:12:26 +0800261static void qemu_net_client_setup(NetClientState *nc,
262 NetClientInfo *info,
263 NetClientState *peer,
264 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800265 const char *name,
266 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000267{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100268 nc->info = info;
269 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000270 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100271 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000272 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100273 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000274 }
aliguori63a01ef2008-10-31 19:10:00 +0000275
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100276 if (peer) {
277 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100278 nc->peer = peer;
279 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100280 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100281 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100282
Jan Kiszka067404b2013-08-02 21:47:08 +0200283 nc->incoming_queue = qemu_new_net_queue(nc);
Jason Wangf7860452013-01-30 19:12:27 +0800284 nc->destructor = destructor;
Jason Wang18a15412013-01-30 19:12:26 +0800285}
286
287NetClientState *qemu_new_net_client(NetClientInfo *info,
288 NetClientState *peer,
289 const char *model,
290 const char *name)
291{
292 NetClientState *nc;
293
294 assert(info->size >= sizeof(NetClientState));
295
296 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800297 qemu_net_client_setup(nc, info, peer, model, name,
298 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800299
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100300 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000301}
302
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000303NICState *qemu_new_nic(NetClientInfo *info,
304 NICConf *conf,
305 const char *model,
306 const char *name,
307 void *opaque)
308{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800309 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000310 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200311 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000312
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200313 assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000314 assert(info->size >= sizeof(NICState));
315
Jason Wangf6b26cf2013-02-22 23:15:06 +0800316 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
317 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000318 nic->conf = conf;
319 nic->opaque = opaque;
320
Jason Wangf6b26cf2013-02-22 23:15:06 +0800321 for (i = 0; i < queues; i++) {
322 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800323 NULL);
324 nic->ncs[i].queue_index = i;
325 }
326
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000327 return nic;
328}
329
Jason Wang1ceef9f2013-01-30 19:12:28 +0800330NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
331{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800332 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800333}
334
Jason Wangb356f762013-01-30 19:12:22 +0800335NetClientState *qemu_get_queue(NICState *nic)
336{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800337 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800338}
339
Jason Wangcc1f0f42013-01-30 19:12:23 +0800340NICState *qemu_get_nic(NetClientState *nc)
341{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800342 NetClientState *nc0 = nc - nc->queue_index;
343
Jason Wangf6b26cf2013-02-22 23:15:06 +0800344 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800345}
346
347void *qemu_get_nic_opaque(NetClientState *nc)
348{
349 NICState *nic = qemu_get_nic(nc);
350
351 return nic->opaque;
352}
353
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100354static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000355{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100356 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000357
Andreas Färbercc2a9042013-02-12 23:16:06 +0100358 if (nc->info->cleanup) {
359 nc->info->cleanup(nc);
360 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200361}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100362
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100363static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200364{
Jan Kiszka067404b2013-08-02 21:47:08 +0200365 if (nc->incoming_queue) {
366 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200367 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100368 if (nc->peer) {
369 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100370 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100371 g_free(nc->name);
372 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800373 if (nc->destructor) {
374 nc->destructor(nc);
375 }
aliguori63a01ef2008-10-31 19:10:00 +0000376}
377
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100378void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200379{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800380 NetClientState *ncs[MAX_QUEUE_NUM];
381 int queues, i;
382
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100383 assert(nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC);
384
Jason Wang1ceef9f2013-01-30 19:12:28 +0800385 /* If the NetClientState belongs to a multiqueue backend, we will change all
386 * other NetClientStates also.
387 */
388 queues = qemu_find_net_clients_except(nc->name, ncs,
389 NET_CLIENT_OPTIONS_KIND_NIC,
390 MAX_QUEUE_NUM);
391 assert(queues != 0);
392
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200393 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100394 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800395 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200396 if (nic->peer_deleted) {
397 return;
398 }
399 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800400
401 for (i = 0; i < queues; i++) {
402 ncs[i]->peer->link_down = true;
403 }
404
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100405 if (nc->peer->info->link_status_changed) {
406 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200407 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800408
409 for (i = 0; i < queues; i++) {
410 qemu_cleanup_net_client(ncs[i]);
411 }
412
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200413 return;
414 }
415
Jason Wang1ceef9f2013-01-30 19:12:28 +0800416 for (i = 0; i < queues; i++) {
417 qemu_cleanup_net_client(ncs[i]);
418 qemu_free_net_client(ncs[i]);
419 }
Jason Wang948ecf22013-01-30 19:12:24 +0800420}
421
422void qemu_del_nic(NICState *nic)
423{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200424 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800425
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800426 qemu_macaddr_set_free(&nic->conf->macaddr);
427
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200428 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800429 if (nic->peer_deleted) {
430 for (i = 0; i < queues; i++) {
431 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200432 }
433 }
434
Jason Wang1ceef9f2013-01-30 19:12:28 +0800435 for (i = queues - 1; i >= 0; i--) {
436 NetClientState *nc = qemu_get_subqueue(nic, i);
437
438 qemu_cleanup_net_client(nc);
439 qemu_free_net_client(nc);
440 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800441
442 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200443}
444
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000445void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
446{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100447 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000448
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100449 QTAILQ_FOREACH(nc, &net_clients, next) {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200450 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800451 if (nc->queue_index == 0) {
452 func(qemu_get_nic(nc), opaque);
453 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000454 }
455 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000456}
457
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100458bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100459{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100460 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100461 return false;
462 }
463
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100464 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100465}
466
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100467bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100468{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100469 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100470 return false;
471 }
472
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100473 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100474}
475
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100476bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100477{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100478 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100479 return false;
480 }
481
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100482 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100483}
484
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100485void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100486{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100487 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100488 return;
489 }
490
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100491 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100492}
493
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100494void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100495 int ecn, int ufo)
496{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100497 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100498 return;
499 }
500
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100501 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100502}
503
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100504void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100505{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100506 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100507 return;
508 }
509
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100510 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100511}
512
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100513int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000514{
zhanghailiange1d64c02014-08-26 16:06:17 +0800515 int vm_running = runstate_is_running();
516
517 if (!vm_running) {
518 return 0;
519 }
520
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100521 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100522 return 1;
523 }
524
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100525 if (sender->peer->receive_disabled) {
526 return 0;
527 } else if (sender->peer->info->can_receive &&
528 !sender->peer->info->can_receive(sender->peer)) {
529 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000530 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500531 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000532}
533
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100534ssize_t qemu_deliver_packet(NetClientState *sender,
535 unsigned flags,
536 const uint8_t *data,
537 size_t size,
538 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100539{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100540 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000541 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100542
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100543 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100544 return size;
545 }
546
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100547 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000548 return 0;
549 }
550
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100551 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
552 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000553 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100554 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000555 }
556
557 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100558 nc->receive_disabled = 1;
Igor Ryzhovb9259652014-04-16 17:43:07 +0400559 }
Mark McLoughlin893379e2009-10-27 18:16:36 +0000560
561 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100562}
563
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100564void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000565{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100566 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100567 return;
568 }
569
Jan Kiszka067404b2013-08-02 21:47:08 +0200570 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100571}
572
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300573static
574void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100575{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100576 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100577
Luigi Rizzo199ee602013-02-05 17:53:31 +0100578 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
579 if (net_hub_flush(nc->peer)) {
580 qemu_notify_event();
581 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100582 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200583 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200584 /* We emptied the queue successfully, signal to the IO thread to repoll
585 * the file descriptor (for tap, for example).
586 */
587 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300588 } else if (purge) {
589 /* Unable to empty the queue, purge remaining packets */
590 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200591 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100592}
593
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300594void qemu_flush_queued_packets(NetClientState *nc)
595{
596 qemu_flush_or_purge_queued_packets(nc, false);
597}
598
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100599static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100600 unsigned flags,
601 const uint8_t *buf, int size,
602 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000603{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100604 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100605
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100606#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100607 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100608 hex_dump(stdout, buf, size);
609#endif
610
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100611 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100612 return size;
613 }
614
Jan Kiszka067404b2013-08-02 21:47:08 +0200615 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100616
Mark McLoughlinca77d172009-10-22 17:43:41 +0100617 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
618}
619
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100620ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100621 const uint8_t *buf, int size,
622 NetPacketSent *sent_cb)
623{
624 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
625 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100626}
627
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100628void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100629{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100630 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000631}
632
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100633ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100634{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100635 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100636 buf, size, NULL);
637}
638
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100639static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000640 int iovcnt)
641{
Scott Feldmand32fcad2013-03-18 11:43:44 -0700642 uint8_t buffer[NET_BUFSIZE];
Benjamin Poirierce053662011-02-23 19:57:21 -0500643 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000644
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400645 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000646
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100647 return nc->info->receive(nc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000648}
649
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100650ssize_t qemu_deliver_packet_iov(NetClientState *sender,
651 unsigned flags,
652 const struct iovec *iov,
653 int iovcnt,
654 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100655{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100656 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100657 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100658
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100659 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500660 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100661 }
662
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100663 if (nc->receive_disabled) {
664 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100665 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100666
667 if (nc->info->receive_iov) {
668 ret = nc->info->receive_iov(nc, iov, iovcnt);
669 } else {
670 ret = nc_sendv_compat(nc, iov, iovcnt);
671 }
672
673 if (ret == 0) {
674 nc->receive_disabled = 1;
675 }
676
677 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100678}
679
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100680ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100681 const struct iovec *iov, int iovcnt,
682 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000683{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100684 NetQueue *queue;
685
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100686 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500687 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000688 }
689
Jan Kiszka067404b2013-08-02 21:47:08 +0200690 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100691
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100692 return qemu_net_queue_send_iov(queue, sender,
693 QEMU_NET_PACKET_FLAG_NONE,
694 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000695}
696
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100697ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100698qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100699{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100700 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100701}
702
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100703NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000704{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100705 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100706
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100707 QTAILQ_FOREACH(nc, &net_clients, next) {
708 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200709 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100710 if (!strcmp(nc->name, id)) {
711 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100712 }
713 }
714
715 return NULL;
716}
717
Jason Wang6c51ae72013-01-30 19:12:25 +0800718int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
719 NetClientOptionsKind type, int max)
720{
721 NetClientState *nc;
722 int ret = 0;
723
724 QTAILQ_FOREACH(nc, &net_clients, next) {
725 if (nc->info->type == type) {
726 continue;
727 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100728 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800729 if (ret < max) {
730 ncs[ret] = nc;
731 }
732 ret++;
733 }
734 }
735
736 return ret;
737}
738
aliguori76970792009-02-11 15:20:03 +0000739static int nic_get_free_idx(void)
740{
741 int index;
742
743 for (index = 0; index < MAX_NICS; index++)
744 if (!nd_table[index].used)
745 return index;
746 return -1;
747}
748
Markus Armbruster07caea32009-09-25 03:53:51 +0200749int qemu_show_nic_models(const char *arg, const char *const *models)
750{
751 int i;
752
Peter Maydellc8057f92012-08-02 13:45:54 +0100753 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200754 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100755 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200756
757 fprintf(stderr, "qemu: Supported NIC models: ");
758 for (i = 0 ; models[i]; i++)
759 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
760 return 1;
761}
762
aliguorid07f22c2009-01-13 19:03:57 +0000763void qemu_check_nic_model(NICInfo *nd, const char *model)
764{
765 const char *models[2];
766
767 models[0] = model;
768 models[1] = NULL;
769
Markus Armbruster07caea32009-09-25 03:53:51 +0200770 if (qemu_show_nic_models(nd->model, models))
771 exit(0);
772 if (qemu_find_nic_model(nd, models, model) < 0)
773 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000774}
775
Markus Armbruster07caea32009-09-25 03:53:51 +0200776int qemu_find_nic_model(NICInfo *nd, const char * const *models,
777 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000778{
Markus Armbruster07caea32009-09-25 03:53:51 +0200779 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000780
781 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500782 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000783
Markus Armbruster07caea32009-09-25 03:53:51 +0200784 for (i = 0 ; models[i]; i++) {
785 if (strcmp(nd->model, models[i]) == 0)
786 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000787 }
788
Markus Armbruster6daf1942011-06-22 14:03:54 +0200789 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200790 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000791}
792
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200793static int net_init_nic(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200794 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100795{
796 int idx;
797 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200798 const NetLegacyNicOptions *nic;
799
800 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
801 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100802
803 idx = nic_get_free_idx();
804 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200805 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100806 return -1;
807 }
808
809 nd = &nd_table[idx];
810
811 memset(nd, 0, sizeof(*nd));
812
Laszlo Ersek2456f362012-07-17 16:17:14 +0200813 if (nic->has_netdev) {
814 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100815 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200816 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100817 return -1;
818 }
819 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100820 assert(peer);
821 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100822 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100823 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200824 if (nic->has_model) {
825 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100826 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200827 if (nic->has_addr) {
828 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100829 }
830
Laszlo Ersek2456f362012-07-17 16:17:14 +0200831 if (nic->has_macaddr &&
832 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200833 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100834 return -1;
835 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400836 if (nic->has_macaddr &&
837 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200838 error_setg(errp,
839 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400840 return -1;
841 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200842 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100843
Laszlo Ersek2456f362012-07-17 16:17:14 +0200844 if (nic->has_vectors) {
845 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200846 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200847 return -1;
848 }
849 nd->nvectors = nic->vectors;
850 } else {
851 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100852 }
853
854 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100855 nb_nics++;
856
857 return idx;
858}
859
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100860
Laszlo Ersek6687b792012-07-17 16:17:13 +0200861static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200862 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200863 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200864 NetClientState *peer, Error **errp) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100865 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100866#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100867 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100868#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100869 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
870 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100871#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100872 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100873#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100874#ifdef CONFIG_NETMAP
875 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
876#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100877 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100878#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100879 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200880#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100881 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300882#ifdef CONFIG_VHOST_NET_USED
883 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
884#endif
Gonglei015a33b2014-07-01 20:58:08 +0800885#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100886 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
887#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100888};
889
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100890
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200891static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200892{
893 union {
894 const Netdev *netdev;
895 const NetLegacy *net;
896 } u;
897 const NetClientOptions *opts;
898 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100899
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100900 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200901 u.netdev = object;
902 opts = u.netdev->opts;
903 name = u.netdev->id;
904
905 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100906#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200907 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100908#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200909 case NET_CLIENT_OPTIONS_KIND_TAP:
910 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100911#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200912 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100913#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100914#ifdef CONFIG_NETMAP
915 case NET_CLIENT_OPTIONS_KIND_NETMAP:
916#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200917#ifdef CONFIG_NET_BRIDGE
918 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
919#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100920 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300921#ifdef CONFIG_VHOST_NET_USED
922 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
923#endif
Gonglei015a33b2014-07-01 20:58:08 +0800924#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100925 case NET_CLIENT_OPTIONS_KIND_L2TPV3:
926#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200927 break;
928
929 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300930 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
931 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100932 return -1;
933 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200934 } else {
935 u.net = object;
936 opts = u.net->opts;
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200937 if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
938 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
939 "a net type");
940 return -1;
941 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200942 /* missing optional values have been initialized to "all bits zero" */
943 name = u.net->has_id ? u.net->id : u.net->name;
944 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100945
Laszlo Ersek6687b792012-07-17 16:17:13 +0200946 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100947 NetClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200948
949 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
950 * parameter. */
951 if (!is_netdev &&
952 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
953 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100954 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100955 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200956
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200957 if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
958 /* FIXME drop when all init functions store an Error */
959 if (errp && !*errp) {
960 error_set(errp, QERR_DEVICE_INIT_FAILED,
961 NetClientOptionsKind_lookup[opts->kind]);
962 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100963 return -1;
964 }
965 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200966 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100967}
968
Laszlo Ersek6687b792012-07-17 16:17:13 +0200969
970static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
971{
972 if (is_netdev) {
973 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
974 } else {
975 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
976 }
977}
978
979
980int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
981{
982 void *object = NULL;
983 Error *err = NULL;
984 int ret = -1;
985
986 {
987 OptsVisitor *ov = opts_visitor_new(opts);
988
989 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
990 opts_visitor_cleanup(ov);
991 }
992
993 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200994 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +0200995 }
996
997 if (object) {
998 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
999
1000 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
1001 qapi_dealloc_visitor_cleanup(dv);
1002 }
1003
1004 error_propagate(errp, err);
1005 return ret;
1006}
1007
1008
aliguori6f338c32009-02-11 15:21:54 +00001009static int net_host_check_device(const char *device)
1010{
1011 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001012 for (i = 0; host_net_devices[i]; i++) {
1013 if (!strncmp(host_net_devices[i], device,
1014 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +00001015 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001016 }
aliguori6f338c32009-02-11 15:21:54 +00001017 }
1018
1019 return 0;
1020}
1021
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001022void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001023{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001024 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001025 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001026 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001027 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001028
aliguori6f338c32009-02-11 15:21:54 +00001029 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001030 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001031 return;
1032 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001033
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001034 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001035 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001036 return;
1037 }
1038
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001039 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001040
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001041 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001042 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001043 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +00001044 monitor_printf(mon, "adding host network device %s failed\n", device);
1045 }
aliguori6f338c32009-02-11 15:21:54 +00001046}
1047
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001048void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001049{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001050 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001051 int vlan_id = qdict_get_int(qdict, "vlan_id");
1052 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001053
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001054 nc = net_hub_find_client_by_name(vlan_id, device);
1055 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001056 error_report("Host network device '%s' on hub '%d' not found",
1057 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001058 return;
1059 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001060 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001061 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001062 return;
1063 }
Jason Wang64a55d62015-02-02 15:06:37 +08001064
1065 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001066 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001067}
1068
Luiz Capitulino928059a2012-04-18 17:34:15 -03001069void netdev_add(QemuOpts *opts, Error **errp)
1070{
1071 net_client_init(opts, 1, errp);
1072}
1073
1074int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001075{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001076 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001077 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001078 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001079
Luiz Capitulino928059a2012-04-18 17:34:15 -03001080 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001081 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001082 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001083 }
1084
Luiz Capitulino928059a2012-04-18 17:34:15 -03001085 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001086 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001087 goto exit_err;
1088 }
1089
1090 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001091 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001092 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001093 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001094 }
1095
Luiz Capitulino928059a2012-04-18 17:34:15 -03001096 return 0;
1097
1098exit_err:
1099 qerror_report_err(local_err);
1100 error_free(local_err);
1101 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001102}
1103
Luiz Capitulino5f964152012-04-16 14:36:32 -03001104void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001105{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001106 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001107 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001108
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001109 nc = qemu_find_netdev(id);
1110 if (!nc) {
Luiz Capitulino5f964152012-04-16 14:36:32 -03001111 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
1112 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001113 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001114
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001115 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1116 if (!opts) {
1117 error_setg(errp, "Device '%s' is not a netdev", id);
1118 return;
1119 }
1120
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001121 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001122 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001123}
1124
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001125void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001126{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001127 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1128 nc->queue_index,
1129 NetClientOptionsKind_lookup[nc->info->type],
1130 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +02001131}
1132
Amos Kongb1be4282013-06-14 15:45:52 +08001133RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1134 Error **errp)
1135{
1136 NetClientState *nc;
1137 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1138
1139 QTAILQ_FOREACH(nc, &net_clients, next) {
1140 RxFilterInfoList *entry;
1141 RxFilterInfo *info;
1142
1143 if (has_name && strcmp(nc->name, name) != 0) {
1144 continue;
1145 }
1146
1147 /* only query rx-filter information of NIC */
1148 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1149 if (has_name) {
1150 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001151 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001152 }
1153 continue;
1154 }
1155
1156 if (nc->info->query_rx_filter) {
1157 info = nc->info->query_rx_filter(nc);
1158 entry = g_malloc0(sizeof(*entry));
1159 entry->value = info;
1160
1161 if (!filter_list) {
1162 filter_list = entry;
1163 } else {
1164 last_entry->next = entry;
1165 }
1166 last_entry = entry;
1167 } else if (has_name) {
1168 error_setg(errp, "net client(%s) doesn't support"
1169 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001170 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001171 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001172
1173 if (has_name) {
1174 break;
1175 }
Amos Kongb1be4282013-06-14 15:45:52 +08001176 }
1177
Markus Armbruster9083da12014-04-24 15:44:18 +02001178 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001179 error_setg(errp, "invalid net client name: %s", name);
1180 }
1181
1182 return filter_list;
1183}
1184
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001185void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001186{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001187 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001188 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001189
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001190 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001191
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001192 QTAILQ_FOREACH(nc, &net_clients, next) {
1193 peer = nc->peer;
1194 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001195
1196 /* Skip if already printed in hub info */
1197 if (net_hub_id_for_client(nc, NULL) == 0) {
1198 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001199 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001200
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001201 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001202 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001203 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001204 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001205 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001206 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001207 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001208 }
aliguori63a01ef2008-10-31 19:10:00 +00001209}
1210
Luiz Capitulino4b371562011-11-23 13:11:55 -02001211void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001212{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001213 NetClientState *ncs[MAX_QUEUE_NUM];
1214 NetClientState *nc;
1215 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001216
Jason Wang1ceef9f2013-01-30 19:12:28 +08001217 queues = qemu_find_net_clients_except(name, ncs,
1218 NET_CLIENT_OPTIONS_KIND_MAX,
1219 MAX_QUEUE_NUM);
1220
1221 if (queues == 0) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001222 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1223 return;
aliguori436e5e52009-01-08 19:44:06 +00001224 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001225 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001226
Jason Wang1ceef9f2013-01-30 19:12:28 +08001227 for (i = 0; i < queues; i++) {
1228 ncs[i]->link_down = !up;
1229 }
aliguori436e5e52009-01-08 19:44:06 +00001230
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001231 if (nc->info->link_status_changed) {
1232 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001233 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001234
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001235 if (nc->peer) {
1236 /* Change peer link only if the peer is NIC and then notify peer.
1237 * If the peer is a HUBPORT or a backend, we do not change the
1238 * link status.
1239 *
1240 * This behavior is compatible with qemu vlans where there could be
1241 * multiple clients that can still communicate with each other in
1242 * disconnected mode. For now maintain this compatibility.
1243 */
1244 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1245 for (i = 0; i < queues; i++) {
1246 ncs[i]->peer->link_down = !up;
1247 }
1248 }
1249 if (nc->peer->info->link_status_changed) {
1250 nc->peer->info->link_status_changed(nc->peer);
1251 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001252 }
aliguori436e5e52009-01-08 19:44:06 +00001253}
1254
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001255static void net_vm_change_state_handler(void *opaque, int running,
1256 RunState state)
1257{
1258 /* Complete all queued packets, to guarantee we don't modify
1259 * state later when VM is not running.
1260 */
1261 if (!running) {
1262 NetClientState *nc;
1263 NetClientState *tmp;
1264
1265 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1266 qemu_flush_or_purge_queued_packets(nc, true);
1267 }
1268 }
1269}
1270
aliguori63a01ef2008-10-31 19:10:00 +00001271void net_cleanup(void)
1272{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001273 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001274
Jason Wang1ceef9f2013-01-30 19:12:28 +08001275 /* We may del multiple entries during qemu_del_net_client(),
1276 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1277 */
1278 while (!QTAILQ_EMPTY(&net_clients)) {
1279 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001280 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1281 qemu_del_nic(qemu_get_nic(nc));
1282 } else {
1283 qemu_del_net_client(nc);
1284 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001285 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001286
1287 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001288}
1289
Markus Armbruster668680f2010-02-11 14:44:58 +01001290void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001291{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001292 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001293 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001294
Peter Maydell641f6ea2011-05-20 16:50:00 +01001295 /* Don't warn about the default network setup that you get if
1296 * no command line -net or -netdev options are specified. There
1297 * are two cases that we would otherwise complain about:
1298 * (1) board doesn't support a NIC but the implicit "-net nic"
1299 * requested one
1300 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1301 * sets up a nic that isn't connected to anything.
1302 */
1303 if (default_net) {
1304 return;
1305 }
1306
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001307 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001308
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001309 QTAILQ_FOREACH(nc, &net_clients, next) {
1310 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001311 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001312 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1313 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001314 }
1315 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001316
1317 /* Check that all NICs requested via -net nic actually got created.
1318 * NICs created via -device don't need to be checked here because
1319 * they are always instantiated.
1320 */
1321 for (i = 0; i < MAX_NICS; i++) {
1322 NICInfo *nd = &nd_table[i];
1323 if (nd->used && !nd->instantiated) {
1324 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1325 "was not created (not supported by this machine?)\n",
1326 nd->name ? nd->name : "anonymous",
1327 nd->model ? nd->model : "unspecified");
1328 }
1329 }
aliguori63a01ef2008-10-31 19:10:00 +00001330}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001331
Markus Armbruster28d0de72015-03-13 13:35:14 +01001332static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001333{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001334 Error *local_err = NULL;
1335
1336 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001337 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001338 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001339 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001340 }
1341
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001342 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001343}
1344
Markus Armbruster28d0de72015-03-13 13:35:14 +01001345static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001346{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001347 Error *local_err = NULL;
1348 int ret;
1349
1350 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001351 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001352 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001353 return -1;
1354 }
1355
1356 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001357}
1358
1359int net_init_clients(void)
1360{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001361 QemuOptsList *net = qemu_find_opts("net");
1362
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001363 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001364 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001365 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001366#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001367 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001368#endif
1369 }
1370
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001371 net_change_state_entry =
1372 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1373
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001374 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001375
Markus Armbruster28d0de72015-03-13 13:35:14 +01001376 if (qemu_opts_foreach(qemu_find_opts("netdev"),
1377 net_init_netdev, NULL, NULL)) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001378 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001379 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001380
Markus Armbruster28d0de72015-03-13 13:35:14 +01001381 if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001382 return -1;
1383 }
1384
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001385 return 0;
1386}
1387
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001388int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001389{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001390#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001391 int ret;
1392 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001393 return ret;
1394 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001395#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001396
Markus Armbruster8212c642010-02-10 19:52:18 +01001397 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001398 return -1;
1399 }
1400
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001401 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001402 return 0;
1403}
Jason Wang7fc8d912012-03-05 11:08:50 +08001404
1405/* From FreeBSD */
1406/* XXX: optimize */
1407unsigned compute_mcast_idx(const uint8_t *ep)
1408{
1409 uint32_t crc;
1410 int carry, i, j;
1411 uint8_t b;
1412
1413 crc = 0xffffffff;
1414 for (i = 0; i < 6; i++) {
1415 b = *ep++;
1416 for (j = 0; j < 8; j++) {
1417 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1418 crc <<= 1;
1419 b >>= 1;
1420 if (carry) {
1421 crc = ((crc ^ POLYNOMIAL) | carry);
1422 }
1423 }
1424 }
1425 return crc >> 26;
1426}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001427
1428QemuOptsList qemu_netdev_opts = {
1429 .name = "netdev",
1430 .implied_opt_name = "type",
1431 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1432 .desc = {
1433 /*
1434 * no elements => accept any params
1435 * validation will happen later
1436 */
1437 { /* end of list */ }
1438 },
1439};
1440
1441QemuOptsList qemu_net_opts = {
1442 .name = "net",
1443 .implied_opt_name = "type",
1444 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1445 .desc = {
1446 /*
1447 * no elements => accept any params
1448 * validation will happen later
1449 */
1450 { /* end of list */ }
1451 },
1452};