blob: 5148aacf817def881d8747874e2105aafd12dab6 [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
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200513int qemu_set_vnet_le(NetClientState *nc, bool is_le)
514{
515 if (!nc || !nc->info->set_vnet_le) {
516 return -ENOSYS;
517 }
518
519 return nc->info->set_vnet_le(nc, is_le);
520}
521
522int qemu_set_vnet_be(NetClientState *nc, bool is_be)
523{
524 if (!nc || !nc->info->set_vnet_be) {
525 return -ENOSYS;
526 }
527
528 return nc->info->set_vnet_be(nc, is_be);
529}
530
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100531int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000532{
zhanghailiange1d64c02014-08-26 16:06:17 +0800533 int vm_running = runstate_is_running();
534
535 if (!vm_running) {
536 return 0;
537 }
538
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100539 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100540 return 1;
541 }
542
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100543 if (sender->peer->receive_disabled) {
544 return 0;
545 } else if (sender->peer->info->can_receive &&
546 !sender->peer->info->can_receive(sender->peer)) {
547 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000548 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500549 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000550}
551
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100552ssize_t qemu_deliver_packet(NetClientState *sender,
553 unsigned flags,
554 const uint8_t *data,
555 size_t size,
556 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100557{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100558 NetClientState *nc = opaque;
Mark McLoughlin893379e2009-10-27 18:16:36 +0000559 ssize_t ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100560
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100561 if (nc->link_down) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100562 return size;
563 }
564
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100565 if (nc->receive_disabled) {
Mark McLoughlin893379e2009-10-27 18:16:36 +0000566 return 0;
567 }
568
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100569 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
570 ret = nc->info->receive_raw(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000571 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100572 ret = nc->info->receive(nc, data, size);
Mark McLoughlin893379e2009-10-27 18:16:36 +0000573 }
574
575 if (ret == 0) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100576 nc->receive_disabled = 1;
Igor Ryzhovb9259652014-04-16 17:43:07 +0400577 }
Mark McLoughlin893379e2009-10-27 18:16:36 +0000578
579 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100580}
581
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100582void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000583{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100584 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100585 return;
586 }
587
Jan Kiszka067404b2013-08-02 21:47:08 +0200588 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100589}
590
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300591static
592void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100593{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100594 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100595
Luigi Rizzo199ee602013-02-05 17:53:31 +0100596 if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
597 if (net_hub_flush(nc->peer)) {
598 qemu_notify_event();
599 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100600 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200601 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200602 /* We emptied the queue successfully, signal to the IO thread to repoll
603 * the file descriptor (for tap, for example).
604 */
605 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300606 } else if (purge) {
607 /* Unable to empty the queue, purge remaining packets */
608 qemu_net_queue_purge(nc->incoming_queue, nc);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200609 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100610}
611
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300612void qemu_flush_queued_packets(NetClientState *nc)
613{
614 qemu_flush_or_purge_queued_packets(nc, false);
615}
616
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100617static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100618 unsigned flags,
619 const uint8_t *buf, int size,
620 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000621{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100622 NetQueue *queue;
Mark McLoughline94667b2009-04-29 11:48:12 +0100623
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100624#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100625 printf("qemu_send_packet_async:\n");
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100626 hex_dump(stdout, buf, size);
627#endif
628
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100629 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100630 return size;
631 }
632
Jan Kiszka067404b2013-08-02 21:47:08 +0200633 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100634
Mark McLoughlinca77d172009-10-22 17:43:41 +0100635 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
636}
637
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100638ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100639 const uint8_t *buf, int size,
640 NetPacketSent *sent_cb)
641{
642 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
643 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100644}
645
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100646void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100647{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100648 qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000649}
650
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100651ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100652{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100653 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100654 buf, size, NULL);
655}
656
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100657static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
aliguorifbe78f42008-12-17 19:13:11 +0000658 int iovcnt)
659{
Scott Feldmand32fcad2013-03-18 11:43:44 -0700660 uint8_t buffer[NET_BUFSIZE];
Benjamin Poirierce053662011-02-23 19:57:21 -0500661 size_t offset;
aliguorifbe78f42008-12-17 19:13:11 +0000662
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400663 offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
aliguorifbe78f42008-12-17 19:13:11 +0000664
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100665 return nc->info->receive(nc, buffer, offset);
aliguorifbe78f42008-12-17 19:13:11 +0000666}
667
Zhi Yong Wu86a77c32012-07-24 16:35:17 +0100668ssize_t qemu_deliver_packet_iov(NetClientState *sender,
669 unsigned flags,
670 const struct iovec *iov,
671 int iovcnt,
672 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100673{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100674 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100675 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100676
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100677 if (nc->link_down) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500678 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100679 }
680
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100681 if (nc->receive_disabled) {
682 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100683 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100684
685 if (nc->info->receive_iov) {
686 ret = nc->info->receive_iov(nc, iov, iovcnt);
687 } else {
688 ret = nc_sendv_compat(nc, iov, iovcnt);
689 }
690
691 if (ret == 0) {
692 nc->receive_disabled = 1;
693 }
694
695 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100696}
697
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100698ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100699 const struct iovec *iov, int iovcnt,
700 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000701{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100702 NetQueue *queue;
703
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100704 if (sender->link_down || !sender->peer) {
Benjamin Poirierce053662011-02-23 19:57:21 -0500705 return iov_size(iov, iovcnt);
aliguorifbe78f42008-12-17 19:13:11 +0000706 }
707
Jan Kiszka067404b2013-08-02 21:47:08 +0200708 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100709
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100710 return qemu_net_queue_send_iov(queue, sender,
711 QEMU_NET_PACKET_FLAG_NONE,
712 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000713}
714
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100715ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100716qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100717{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100718 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100719}
720
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100721NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000722{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100723 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100724
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100725 QTAILQ_FOREACH(nc, &net_clients, next) {
726 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200727 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100728 if (!strcmp(nc->name, id)) {
729 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100730 }
731 }
732
733 return NULL;
734}
735
Jason Wang6c51ae72013-01-30 19:12:25 +0800736int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
737 NetClientOptionsKind type, int max)
738{
739 NetClientState *nc;
740 int ret = 0;
741
742 QTAILQ_FOREACH(nc, &net_clients, next) {
743 if (nc->info->type == type) {
744 continue;
745 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100746 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800747 if (ret < max) {
748 ncs[ret] = nc;
749 }
750 ret++;
751 }
752 }
753
754 return ret;
755}
756
aliguori76970792009-02-11 15:20:03 +0000757static int nic_get_free_idx(void)
758{
759 int index;
760
761 for (index = 0; index < MAX_NICS; index++)
762 if (!nd_table[index].used)
763 return index;
764 return -1;
765}
766
Markus Armbruster07caea32009-09-25 03:53:51 +0200767int qemu_show_nic_models(const char *arg, const char *const *models)
768{
769 int i;
770
Peter Maydellc8057f92012-08-02 13:45:54 +0100771 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200772 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100773 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200774
775 fprintf(stderr, "qemu: Supported NIC models: ");
776 for (i = 0 ; models[i]; i++)
777 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
778 return 1;
779}
780
aliguorid07f22c2009-01-13 19:03:57 +0000781void qemu_check_nic_model(NICInfo *nd, const char *model)
782{
783 const char *models[2];
784
785 models[0] = model;
786 models[1] = NULL;
787
Markus Armbruster07caea32009-09-25 03:53:51 +0200788 if (qemu_show_nic_models(nd->model, models))
789 exit(0);
790 if (qemu_find_nic_model(nd, models, model) < 0)
791 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000792}
793
Markus Armbruster07caea32009-09-25 03:53:51 +0200794int qemu_find_nic_model(NICInfo *nd, const char * const *models,
795 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000796{
Markus Armbruster07caea32009-09-25 03:53:51 +0200797 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000798
799 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500800 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000801
Markus Armbruster07caea32009-09-25 03:53:51 +0200802 for (i = 0 ; models[i]; i++) {
803 if (strcmp(nd->model, models[i]) == 0)
804 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000805 }
806
Markus Armbruster6daf1942011-06-22 14:03:54 +0200807 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200808 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000809}
810
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200811static int net_init_nic(const NetClientOptions *opts, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200812 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100813{
814 int idx;
815 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200816 const NetLegacyNicOptions *nic;
817
818 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
819 nic = opts->nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100820
821 idx = nic_get_free_idx();
822 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200823 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100824 return -1;
825 }
826
827 nd = &nd_table[idx];
828
829 memset(nd, 0, sizeof(*nd));
830
Laszlo Ersek2456f362012-07-17 16:17:14 +0200831 if (nic->has_netdev) {
832 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100833 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200834 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100835 return -1;
836 }
837 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100838 assert(peer);
839 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100840 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100841 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200842 if (nic->has_model) {
843 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100844 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200845 if (nic->has_addr) {
846 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100847 }
848
Laszlo Ersek2456f362012-07-17 16:17:14 +0200849 if (nic->has_macaddr &&
850 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200851 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100852 return -1;
853 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400854 if (nic->has_macaddr &&
855 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200856 error_setg(errp,
857 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400858 return -1;
859 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200860 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100861
Laszlo Ersek2456f362012-07-17 16:17:14 +0200862 if (nic->has_vectors) {
863 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200864 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200865 return -1;
866 }
867 nd->nvectors = nic->vectors;
868 } else {
869 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100870 }
871
872 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100873 nb_nics++;
874
875 return idx;
876}
877
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100878
Laszlo Ersek6687b792012-07-17 16:17:13 +0200879static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200880 const NetClientOptions *opts,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200881 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200882 NetClientState *peer, Error **errp) = {
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100883 [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100884#ifdef CONFIG_SLIRP
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100885 [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100886#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100887 [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
888 [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100889#ifdef CONFIG_VDE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100890 [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100891#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100892#ifdef CONFIG_NETMAP
893 [NET_CLIENT_OPTIONS_KIND_NETMAP] = net_init_netmap,
894#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100895 [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
Stefan Weil2944e4e2012-02-04 09:24:46 +0100896#ifdef CONFIG_NET_BRIDGE
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100897 [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200898#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100899 [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300900#ifdef CONFIG_VHOST_NET_USED
901 [NET_CLIENT_OPTIONS_KIND_VHOST_USER] = net_init_vhost_user,
902#endif
Gonglei015a33b2014-07-01 20:58:08 +0800903#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100904 [NET_CLIENT_OPTIONS_KIND_L2TPV3] = net_init_l2tpv3,
905#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100906};
907
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100908
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200909static int net_client_init1(const void *object, int is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200910{
911 union {
912 const Netdev *netdev;
913 const NetLegacy *net;
914 } u;
915 const NetClientOptions *opts;
916 const char *name;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100917
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100918 if (is_netdev) {
Laszlo Ersek6687b792012-07-17 16:17:13 +0200919 u.netdev = object;
920 opts = u.netdev->opts;
921 name = u.netdev->id;
922
923 switch (opts->kind) {
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100924#ifdef CONFIG_SLIRP
Laszlo Ersek6687b792012-07-17 16:17:13 +0200925 case NET_CLIENT_OPTIONS_KIND_USER:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100926#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200927 case NET_CLIENT_OPTIONS_KIND_TAP:
928 case NET_CLIENT_OPTIONS_KIND_SOCKET:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100929#ifdef CONFIG_VDE
Laszlo Ersek6687b792012-07-17 16:17:13 +0200930 case NET_CLIENT_OPTIONS_KIND_VDE:
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100931#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100932#ifdef CONFIG_NETMAP
933 case NET_CLIENT_OPTIONS_KIND_NETMAP:
934#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200935#ifdef CONFIG_NET_BRIDGE
936 case NET_CLIENT_OPTIONS_KIND_BRIDGE:
937#endif
Stefan Hajnoczif6c874e2012-07-24 16:35:04 +0100938 case NET_CLIENT_OPTIONS_KIND_HUBPORT:
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300939#ifdef CONFIG_VHOST_NET_USED
940 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
941#endif
Gonglei015a33b2014-07-01 20:58:08 +0800942#ifdef CONFIG_L2TPV3
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100943 case NET_CLIENT_OPTIONS_KIND_L2TPV3:
944#endif
Laszlo Ersek6687b792012-07-17 16:17:13 +0200945 break;
946
947 default:
Luiz Capitulino4559a1d2012-04-20 16:50:25 -0300948 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
949 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100950 return -1;
951 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200952 } else {
953 u.net = object;
954 opts = u.net->opts;
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200955 if (opts->kind == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
956 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
957 "a net type");
958 return -1;
959 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200960 /* missing optional values have been initialized to "all bits zero" */
961 name = u.net->has_id ? u.net->id : u.net->name;
962 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100963
Laszlo Ersek6687b792012-07-17 16:17:13 +0200964 if (net_client_init_fun[opts->kind]) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100965 NetClientState *peer = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +0200966
967 /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
968 * parameter. */
969 if (!is_netdev &&
970 (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
971 !opts->nic->has_netdev)) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100972 peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100973 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200974
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200975 if (net_client_init_fun[opts->kind](opts, name, peer, errp) < 0) {
976 /* FIXME drop when all init functions store an Error */
977 if (errp && !*errp) {
978 error_set(errp, QERR_DEVICE_INIT_FAILED,
979 NetClientOptionsKind_lookup[opts->kind]);
980 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100981 return -1;
982 }
983 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200984 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100985}
986
Laszlo Ersek6687b792012-07-17 16:17:13 +0200987
988static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
989{
990 if (is_netdev) {
991 visit_type_Netdev(v, (Netdev **)object, NULL, errp);
992 } else {
993 visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
994 }
995}
996
997
998int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
999{
1000 void *object = NULL;
1001 Error *err = NULL;
1002 int ret = -1;
1003
1004 {
1005 OptsVisitor *ov = opts_visitor_new(opts);
1006
1007 net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
1008 opts_visitor_cleanup(ov);
1009 }
1010
1011 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +02001012 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001013 }
1014
1015 if (object) {
1016 QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
1017
1018 net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
1019 qapi_dealloc_visitor_cleanup(dv);
1020 }
1021
1022 error_propagate(errp, err);
1023 return ret;
1024}
1025
1026
aliguori6f338c32009-02-11 15:21:54 +00001027static int net_host_check_device(const char *device)
1028{
1029 int i;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001030 for (i = 0; host_net_devices[i]; i++) {
1031 if (!strncmp(host_net_devices[i], device,
1032 strlen(host_net_devices[i]))) {
aliguori6f338c32009-02-11 15:21:54 +00001033 return 1;
Hani Benhabiles84007e82014-05-27 23:39:33 +01001034 }
aliguori6f338c32009-02-11 15:21:54 +00001035 }
1036
1037 return 0;
1038}
1039
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001040void hmp_host_net_add(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001041{
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001042 const char *device = qdict_get_str(qdict, "device");
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001043 const char *opts_str = qdict_get_try_str(qdict, "opts");
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001044 Error *local_err = NULL;
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001045 QemuOpts *opts;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001046
aliguori6f338c32009-02-11 15:21:54 +00001047 if (!net_host_check_device(device)) {
aliguori376253e2009-03-05 23:01:23 +00001048 monitor_printf(mon, "invalid host network device %s\n", device);
aliguori6f338c32009-02-11 15:21:54 +00001049 return;
1050 }
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001051
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001052 opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001053 if (!opts) {
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001054 return;
1055 }
1056
Markus Armbrusterf43e47d2015-02-12 17:52:20 +01001057 qemu_opt_set(opts, "type", device, &error_abort);
Mark McLoughlin7f1c9d22009-10-06 12:17:13 +01001058
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001059 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001060 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001061 error_report_err(local_err);
aliguori5c8be672009-04-21 19:56:32 +00001062 monitor_printf(mon, "adding host network device %s failed\n", device);
1063 }
aliguori6f338c32009-02-11 15:21:54 +00001064}
1065
Markus Armbruster3e5a50d2015-02-06 13:55:43 +01001066void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
aliguori6f338c32009-02-11 15:21:54 +00001067{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001068 NetClientState *nc;
Luiz Capitulinof18c16d2009-08-28 15:27:14 -03001069 int vlan_id = qdict_get_int(qdict, "vlan_id");
1070 const char *device = qdict_get_str(qdict, "device");
aliguori6f338c32009-02-11 15:21:54 +00001071
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001072 nc = net_hub_find_client_by_name(vlan_id, device);
1073 if (!nc) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001074 error_report("Host network device '%s' on hub '%d' not found",
1075 device, vlan_id);
aliguori6f338c32009-02-11 15:21:54 +00001076 return;
1077 }
Paolo Bonzini7fb43912014-12-23 17:53:20 +01001078 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
Hani Benhabiles86e11772014-04-01 00:05:14 +01001079 error_report("invalid host network device '%s'", device);
aliguorie8f1f9d2009-04-21 19:56:08 +00001080 return;
1081 }
Jason Wang64a55d62015-02-02 15:06:37 +08001082
1083 qemu_del_net_client(nc->peer);
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001084 qemu_del_net_client(nc);
aliguori6f338c32009-02-11 15:21:54 +00001085}
1086
Luiz Capitulino928059a2012-04-18 17:34:15 -03001087void netdev_add(QemuOpts *opts, Error **errp)
1088{
1089 net_client_init(opts, 1, errp);
1090}
1091
1092int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001093{
Luiz Capitulino4e899782012-04-18 17:24:01 -03001094 Error *local_err = NULL;
Luiz Capitulino928059a2012-04-18 17:34:15 -03001095 QemuOptsList *opts_list;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001096 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001097
Luiz Capitulino928059a2012-04-18 17:34:15 -03001098 opts_list = qemu_find_opts_err("netdev", &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001099 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001100 goto exit_err;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001101 }
1102
Luiz Capitulino928059a2012-04-18 17:34:15 -03001103 opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001104 if (local_err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001105 goto exit_err;
1106 }
1107
1108 netdev_add(opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001109 if (local_err) {
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001110 qemu_opts_del(opts);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001111 goto exit_err;
Yoshiaki Tamura410cbaf2010-06-21 10:41:36 +09001112 }
1113
Luiz Capitulino928059a2012-04-18 17:34:15 -03001114 return 0;
1115
1116exit_err:
1117 qerror_report_err(local_err);
1118 error_free(local_err);
1119 return -1;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001120}
1121
Luiz Capitulino5f964152012-04-16 14:36:32 -03001122void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001123{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001124 NetClientState *nc;
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001125 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001126
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001127 nc = qemu_find_netdev(id);
1128 if (!nc) {
Luiz Capitulino5f964152012-04-16 14:36:32 -03001129 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
1130 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001131 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001132
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001133 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
1134 if (!opts) {
1135 error_setg(errp, "Device '%s' is not a netdev", id);
1136 return;
1137 }
1138
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001139 qemu_del_net_client(nc);
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001140 qemu_opts_del(opts);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001141}
1142
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001143void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001144{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001145 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1146 nc->queue_index,
1147 NetClientOptionsKind_lookup[nc->info->type],
1148 nc->info_str);
Jan Kiszka44e798d2011-07-20 12:20:21 +02001149}
1150
Amos Kongb1be4282013-06-14 15:45:52 +08001151RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1152 Error **errp)
1153{
1154 NetClientState *nc;
1155 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1156
1157 QTAILQ_FOREACH(nc, &net_clients, next) {
1158 RxFilterInfoList *entry;
1159 RxFilterInfo *info;
1160
1161 if (has_name && strcmp(nc->name, name) != 0) {
1162 continue;
1163 }
1164
1165 /* only query rx-filter information of NIC */
1166 if (nc->info->type != NET_CLIENT_OPTIONS_KIND_NIC) {
1167 if (has_name) {
1168 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001169 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001170 }
1171 continue;
1172 }
1173
1174 if (nc->info->query_rx_filter) {
1175 info = nc->info->query_rx_filter(nc);
1176 entry = g_malloc0(sizeof(*entry));
1177 entry->value = info;
1178
1179 if (!filter_list) {
1180 filter_list = entry;
1181 } else {
1182 last_entry->next = entry;
1183 }
1184 last_entry = entry;
1185 } else if (has_name) {
1186 error_setg(errp, "net client(%s) doesn't support"
1187 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001188 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001189 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001190
1191 if (has_name) {
1192 break;
1193 }
Amos Kongb1be4282013-06-14 15:45:52 +08001194 }
1195
Markus Armbruster9083da12014-04-24 15:44:18 +02001196 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001197 error_setg(errp, "invalid net client name: %s", name);
1198 }
1199
1200 return filter_list;
1201}
1202
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001203void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001204{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001205 NetClientState *nc, *peer;
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001206 NetClientOptionsKind type;
aliguori63a01ef2008-10-31 19:10:00 +00001207
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001208 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001209
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001210 QTAILQ_FOREACH(nc, &net_clients, next) {
1211 peer = nc->peer;
1212 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001213
1214 /* Skip if already printed in hub info */
1215 if (net_hub_id_for_client(nc, NULL) == 0) {
1216 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001217 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001218
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001219 if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001220 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001221 } /* else it's a netdev connected to a NIC, printed with the NIC */
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001222 if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001223 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001224 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001225 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001226 }
aliguori63a01ef2008-10-31 19:10:00 +00001227}
1228
Luiz Capitulino4b371562011-11-23 13:11:55 -02001229void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001230{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001231 NetClientState *ncs[MAX_QUEUE_NUM];
1232 NetClientState *nc;
1233 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001234
Jason Wang1ceef9f2013-01-30 19:12:28 +08001235 queues = qemu_find_net_clients_except(name, ncs,
1236 NET_CLIENT_OPTIONS_KIND_MAX,
1237 MAX_QUEUE_NUM);
1238
1239 if (queues == 0) {
Luiz Capitulino4b371562011-11-23 13:11:55 -02001240 error_set(errp, QERR_DEVICE_NOT_FOUND, name);
1241 return;
aliguori436e5e52009-01-08 19:44:06 +00001242 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001243 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001244
Jason Wang1ceef9f2013-01-30 19:12:28 +08001245 for (i = 0; i < queues; i++) {
1246 ncs[i]->link_down = !up;
1247 }
aliguori436e5e52009-01-08 19:44:06 +00001248
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001249 if (nc->info->link_status_changed) {
1250 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001251 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001252
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001253 if (nc->peer) {
1254 /* Change peer link only if the peer is NIC and then notify peer.
1255 * If the peer is a HUBPORT or a backend, we do not change the
1256 * link status.
1257 *
1258 * This behavior is compatible with qemu vlans where there could be
1259 * multiple clients that can still communicate with each other in
1260 * disconnected mode. For now maintain this compatibility.
1261 */
1262 if (nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1263 for (i = 0; i < queues; i++) {
1264 ncs[i]->peer->link_down = !up;
1265 }
1266 }
1267 if (nc->peer->info->link_status_changed) {
1268 nc->peer->info->link_status_changed(nc->peer);
1269 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001270 }
aliguori436e5e52009-01-08 19:44:06 +00001271}
1272
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001273static void net_vm_change_state_handler(void *opaque, int running,
1274 RunState state)
1275{
1276 /* Complete all queued packets, to guarantee we don't modify
1277 * state later when VM is not running.
1278 */
1279 if (!running) {
1280 NetClientState *nc;
1281 NetClientState *tmp;
1282
1283 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1284 qemu_flush_or_purge_queued_packets(nc, true);
1285 }
1286 }
1287}
1288
aliguori63a01ef2008-10-31 19:10:00 +00001289void net_cleanup(void)
1290{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001291 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001292
Jason Wang1ceef9f2013-01-30 19:12:28 +08001293 /* We may del multiple entries during qemu_del_net_client(),
1294 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1295 */
1296 while (!QTAILQ_EMPTY(&net_clients)) {
1297 nc = QTAILQ_FIRST(&net_clients);
Jason Wang948ecf22013-01-30 19:12:24 +08001298 if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
1299 qemu_del_nic(qemu_get_nic(nc));
1300 } else {
1301 qemu_del_net_client(nc);
1302 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001303 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001304
1305 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001306}
1307
Markus Armbruster668680f2010-02-11 14:44:58 +01001308void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001309{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001310 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001311 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001312
Peter Maydell641f6ea2011-05-20 16:50:00 +01001313 /* Don't warn about the default network setup that you get if
1314 * no command line -net or -netdev options are specified. There
1315 * are two cases that we would otherwise complain about:
1316 * (1) board doesn't support a NIC but the implicit "-net nic"
1317 * requested one
1318 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
1319 * sets up a nic that isn't connected to anything.
1320 */
1321 if (default_net) {
1322 return;
1323 }
1324
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001325 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001326
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001327 QTAILQ_FOREACH(nc, &net_clients, next) {
1328 if (!nc->peer) {
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001329 fprintf(stderr, "Warning: %s %s has no peer\n",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001330 nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
1331 "nic" : "netdev", nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001332 }
1333 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001334
1335 /* Check that all NICs requested via -net nic actually got created.
1336 * NICs created via -device don't need to be checked here because
1337 * they are always instantiated.
1338 */
1339 for (i = 0; i < MAX_NICS; i++) {
1340 NICInfo *nd = &nd_table[i];
1341 if (nd->used && !nd->instantiated) {
1342 fprintf(stderr, "Warning: requested NIC (%s, model %s) "
1343 "was not created (not supported by this machine?)\n",
1344 nd->name ? nd->name : "anonymous",
1345 nd->model ? nd->model : "unspecified");
1346 }
1347 }
aliguori63a01ef2008-10-31 19:10:00 +00001348}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001349
1350static int net_init_client(QemuOpts *opts, void *dummy)
1351{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001352 Error *local_err = NULL;
1353
1354 net_client_init(opts, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001355 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001356 error_report_err(local_err);
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001357 return -1;
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001358 }
1359
Mark McLoughlinc1671a02009-10-12 09:52:00 +01001360 return 0;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001361}
1362
1363static int net_init_netdev(QemuOpts *opts, void *dummy)
1364{
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001365 Error *local_err = NULL;
1366 int ret;
1367
1368 ret = net_client_init(opts, 1, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001369 if (local_err) {
Markus Armbruster12d0cc22015-02-10 15:02:06 +01001370 error_report_err(local_err);
Luiz Capitulino4559a1d2012-04-20 16:50:25 -03001371 return -1;
1372 }
1373
1374 return ret;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001375}
1376
1377int net_init_clients(void)
1378{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001379 QemuOptsList *net = qemu_find_opts("net");
1380
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001381 if (default_net) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001382 /* if no clients, we use a default config */
Markus Armbruster79087c72015-02-12 17:07:34 +01001383 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001384#ifdef CONFIG_SLIRP
Markus Armbruster79087c72015-02-12 17:07:34 +01001385 qemu_opts_set(net, NULL, "type", "user", &error_abort);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001386#endif
1387 }
1388
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001389 net_change_state_entry =
1390 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1391
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001392 QTAILQ_INIT(&net_clients);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001393
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001394 if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001395 return -1;
1396
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001397 if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001398 return -1;
1399 }
1400
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001401 return 0;
1402}
1403
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01001404int net_client_parse(QemuOptsList *opts_list, const char *optarg)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001405{
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001406#if defined(CONFIG_SLIRP)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001407 int ret;
1408 if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001409 return ret;
1410 }
Juan Quintelaa3a766e2009-10-07 23:44:15 +02001411#endif
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001412
Markus Armbruster8212c642010-02-10 19:52:18 +01001413 if (!qemu_opts_parse(opts_list, optarg, 1)) {
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001414 return -1;
1415 }
1416
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01001417 default_net = 0;
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001418 return 0;
1419}
Jason Wang7fc8d912012-03-05 11:08:50 +08001420
1421/* From FreeBSD */
1422/* XXX: optimize */
1423unsigned compute_mcast_idx(const uint8_t *ep)
1424{
1425 uint32_t crc;
1426 int carry, i, j;
1427 uint8_t b;
1428
1429 crc = 0xffffffff;
1430 for (i = 0; i < 6; i++) {
1431 b = *ep++;
1432 for (j = 0; j < 8; j++) {
1433 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1434 crc <<= 1;
1435 b >>= 1;
1436 if (carry) {
1437 crc = ((crc ^ POLYNOMIAL) | carry);
1438 }
1439 }
1440 }
1441 return crc >> 26;
1442}
Paolo Bonzini4d454572012-11-26 16:03:42 +01001443
1444QemuOptsList qemu_netdev_opts = {
1445 .name = "netdev",
1446 .implied_opt_name = "type",
1447 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1448 .desc = {
1449 /*
1450 * no elements => accept any params
1451 * validation will happen later
1452 */
1453 { /* end of list */ }
1454 },
1455};
1456
1457QemuOptsList qemu_net_opts = {
1458 .name = "net",
1459 .implied_opt_name = "type",
1460 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1461 .desc = {
1462 /*
1463 * no elements => accept any params
1464 * validation will happen later
1465 */
1466 { /* end of list */ }
1467 },
1468};