blob: a094cf1d29292604290b72bedc9fa5db67f6d07e [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 */
Markus Armbrustere688df62018-02-01 12:18:31 +010024
Peter Maydell2744d922016-01-29 17:50:00 +000025#include "qemu/osdep.h"
blueswir1d40cdb12009-03-07 16:52:02 +000026
Paolo Bonzini1422e322012-10-24 08:43:34 +020027#include "net/net.h"
Paolo Bonzinifd9400b2012-10-24 11:27:28 +020028#include "clients.h"
29#include "hub.h"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020030#include "hw/qdev-properties.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020031#include "net/slirp.h"
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +040032#include "net/eth.h"
Paolo Bonzinifd9400b2012-10-24 11:27:28 +020033#include "util.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020034
Paolo Bonzini83c90892012-12-17 18:19:49 +010035#include "monitor/monitor.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/help_option.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010037#include "qapi/qapi-commands-net.h"
Jason Wangf9bb0c12021-04-02 11:05:20 +080038#include "qapi/qapi-visit-net.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010039#include "qapi/qmp/qdict.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010040#include "qapi/qmp/qerror.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010041#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010042#include "qemu/sockets.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020043#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010044#include "qemu/config-file.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020045#include "qemu/ctype.h"
Thomas Huth27eb3722021-02-15 10:02:25 +010046#include "qemu/id.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010047#include "qemu/iov.h"
Paolo Bonziniad6f9322020-11-11 05:52:22 -050048#include "qemu/qemu-print.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010049#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010050#include "qemu/option.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010051#include "qapi/error.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020052#include "qapi/opts-visitor.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020053#include "sysemu/runstate.h"
Rao, Lei0c7af1a2021-06-08 16:23:30 +080054#include "net/colo-compare.h"
Yang Hongyangfdccce42015-10-07 11:52:14 +080055#include "net/filter.h"
zhanghailiangaa9156f2016-01-26 14:43:33 +080056#include "qapi/string-output-visitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000057
Stefan Weil2944e4e2012-02-04 09:24:46 +010058/* Net bridge is currently not supported for W32. */
59#if !defined(_WIN32)
60# define CONFIG_NET_BRIDGE
61#endif
62
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030063static VMChangeStateEntry *net_change_state_entry;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010064static QTAILQ_HEAD(, NetClientState) net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000065
66/***********************************************************/
67/* network device redirectors */
68
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080069int parse_host_port(struct sockaddr_in *saddr, const char *str,
70 Error **errp)
aliguori63a01ef2008-10-31 19:10:00 +000071{
Stefano Garzarellaadd99342019-05-17 15:47:47 +020072 gchar **substrings;
aliguori63a01ef2008-10-31 19:10:00 +000073 struct hostent *he;
Stefano Garzarellaadd99342019-05-17 15:47:47 +020074 const char *addr, *p, *r;
75 int port, ret = 0;
aliguori63a01ef2008-10-31 19:10:00 +000076
Peter Maydell59292382021-08-13 16:05:03 +010077 memset(saddr, 0, sizeof(*saddr));
78
Stefano Garzarellaadd99342019-05-17 15:47:47 +020079 substrings = g_strsplit(str, ":", 2);
80 if (!substrings || !substrings[0] || !substrings[1]) {
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080081 error_setg(errp, "host address '%s' doesn't contain ':' "
82 "separating host from port", str);
Stefano Garzarellaadd99342019-05-17 15:47:47 +020083 ret = -1;
84 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080085 }
Stefano Garzarellaadd99342019-05-17 15:47:47 +020086
87 addr = substrings[0];
88 p = substrings[1];
89
aliguori63a01ef2008-10-31 19:10:00 +000090 saddr->sin_family = AF_INET;
Stefano Garzarellaadd99342019-05-17 15:47:47 +020091 if (addr[0] == '\0') {
aliguori63a01ef2008-10-31 19:10:00 +000092 saddr->sin_addr.s_addr = 0;
93 } else {
Stefano Garzarellaadd99342019-05-17 15:47:47 +020094 if (qemu_isdigit(addr[0])) {
95 if (!inet_aton(addr, &saddr->sin_addr)) {
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080096 error_setg(errp, "host address '%s' is not a valid "
Stefano Garzarellaadd99342019-05-17 15:47:47 +020097 "IPv4 address", addr);
98 ret = -1;
99 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800100 }
aliguori63a01ef2008-10-31 19:10:00 +0000101 } else {
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200102 he = gethostbyname(addr);
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800103 if (he == NULL) {
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200104 error_setg(errp, "can't resolve host address '%s'", addr);
105 ret = -1;
106 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800107 }
aliguori63a01ef2008-10-31 19:10:00 +0000108 saddr->sin_addr = *(struct in_addr *)he->h_addr;
109 }
110 }
111 port = strtol(p, (char **)&r, 0);
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800112 if (r == p) {
113 error_setg(errp, "port number '%s' is invalid", p);
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200114 ret = -1;
115 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800116 }
aliguori63a01ef2008-10-31 19:10:00 +0000117 saddr->sin_port = htons(port);
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200118
119out:
120 g_strfreev(substrings);
121 return ret;
aliguori63a01ef2008-10-31 19:10:00 +0000122}
123
Scott Feldman890ee6a2015-03-13 21:09:25 -0700124char *qemu_mac_strdup_printf(const uint8_t *macaddr)
125{
126 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
127 macaddr[0], macaddr[1], macaddr[2],
128 macaddr[3], macaddr[4], macaddr[5]);
129}
130
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100131void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000132{
Jason Wang56e6f592021-04-02 11:03:33 +0800133 snprintf(nc->info_str, sizeof(nc->info_str),
134 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
135 nc->model,
136 macaddr[0], macaddr[1], macaddr[2],
137 macaddr[3], macaddr[4], macaddr[5]);
aliguori7cb7434b2009-01-07 17:46:21 +0000138}
139
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800140static int mac_table[256] = {0};
141
142static void qemu_macaddr_set_used(MACAddr *macaddr)
143{
144 int index;
145
146 for (index = 0x56; index < 0xFF; index++) {
147 if (macaddr->a[5] == index) {
148 mac_table[index]++;
149 }
150 }
151}
152
153static void qemu_macaddr_set_free(MACAddr *macaddr)
154{
155 int index;
156 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
157
158 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
159 return;
160 }
161 for (index = 0x56; index < 0xFF; index++) {
162 if (macaddr->a[5] == index) {
163 mac_table[index]--;
164 }
165 }
166}
167
168static int qemu_macaddr_get_free(void)
169{
170 int index;
171
172 for (index = 0x56; index < 0xFF; index++) {
173 if (mac_table[index] == 0) {
174 return index;
175 }
176 }
177
178 return -1;
179}
180
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200181void qemu_macaddr_default_if_unset(MACAddr *macaddr)
182{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200183 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800184 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200185
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800186 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
187 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
188 return;
189 } else {
190 qemu_macaddr_set_used(macaddr);
191 return;
192 }
193 }
194
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200195 macaddr->a[0] = 0x52;
196 macaddr->a[1] = 0x54;
197 macaddr->a[2] = 0x00;
198 macaddr->a[3] = 0x12;
199 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800200 macaddr->a[5] = qemu_macaddr_get_free();
201 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200202}
203
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100204/**
205 * Generate a name for net client
206 *
Amos Kongc9635302013-04-15 18:55:19 +0800207 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100208 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100209static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000210{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100211 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000212 int id = 0;
213
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100214 QTAILQ_FOREACH(nc, &net_clients, next) {
215 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100216 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100217 }
Amos Kongc9635302013-04-15 18:55:19 +0800218 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200219 id++;
220 }
221 }
222
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100223 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000224}
225
Jason Wangf7860452013-01-30 19:12:27 +0800226static void qemu_net_client_destructor(NetClientState *nc)
227{
228 g_free(nc);
229}
Jason Wang25c01bd2018-12-04 11:53:43 +0800230static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
231 unsigned flags,
232 const struct iovec *iov,
233 int iovcnt,
234 void *opaque);
Jason Wangf7860452013-01-30 19:12:27 +0800235
Jason Wang18a15412013-01-30 19:12:26 +0800236static void qemu_net_client_setup(NetClientState *nc,
237 NetClientInfo *info,
238 NetClientState *peer,
239 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800240 const char *name,
Jason Wang2f849db2021-10-20 12:55:55 +0800241 NetClientDestructor *destructor,
242 bool is_datapath)
aliguori63a01ef2008-10-31 19:10:00 +0000243{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100244 nc->info = info;
245 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000246 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100247 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000248 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100249 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000250 }
aliguori63a01ef2008-10-31 19:10:00 +0000251
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100252 if (peer) {
253 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100254 nc->peer = peer;
255 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100256 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100257 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100258
Yang Hongyang3e033a42015-10-07 11:52:17 +0800259 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
Jason Wangf7860452013-01-30 19:12:27 +0800260 nc->destructor = destructor;
Jason Wang2f849db2021-10-20 12:55:55 +0800261 nc->is_datapath = is_datapath;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800262 QTAILQ_INIT(&nc->filters);
Jason Wang18a15412013-01-30 19:12:26 +0800263}
264
265NetClientState *qemu_new_net_client(NetClientInfo *info,
266 NetClientState *peer,
267 const char *model,
268 const char *name)
269{
270 NetClientState *nc;
271
272 assert(info->size >= sizeof(NetClientState));
273
274 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800275 qemu_net_client_setup(nc, info, peer, model, name,
Jason Wang2f849db2021-10-20 12:55:55 +0800276 qemu_net_client_destructor, true);
277
278 return nc;
279}
280
281NetClientState *qemu_new_net_control_client(NetClientInfo *info,
282 NetClientState *peer,
283 const char *model,
284 const char *name)
285{
286 NetClientState *nc;
287
288 assert(info->size >= sizeof(NetClientState));
289
290 nc = g_malloc0(info->size);
291 qemu_net_client_setup(nc, info, peer, model, name,
292 qemu_net_client_destructor, false);
Jason Wang18a15412013-01-30 19:12:26 +0800293
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100294 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000295}
296
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000297NICState *qemu_new_nic(NetClientInfo *info,
298 NICConf *conf,
299 const char *model,
300 const char *name,
301 void *opaque)
302{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800303 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000304 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200305 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000306
Eric Blakef394b2e2016-07-13 21:50:23 -0600307 assert(info->type == NET_CLIENT_DRIVER_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000308 assert(info->size >= sizeof(NICState));
309
Jason Wangf6b26cf2013-02-22 23:15:06 +0800310 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
311 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000312 nic->conf = conf;
313 nic->opaque = opaque;
314
Jason Wangf6b26cf2013-02-22 23:15:06 +0800315 for (i = 0; i < queues; i++) {
316 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang2f849db2021-10-20 12:55:55 +0800317 NULL, true);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800318 nic->ncs[i].queue_index = i;
319 }
320
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000321 return nic;
322}
323
Jason Wang1ceef9f2013-01-30 19:12:28 +0800324NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
325{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800326 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800327}
328
Jason Wangb356f762013-01-30 19:12:22 +0800329NetClientState *qemu_get_queue(NICState *nic)
330{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800331 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800332}
333
Jason Wangcc1f0f42013-01-30 19:12:23 +0800334NICState *qemu_get_nic(NetClientState *nc)
335{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800336 NetClientState *nc0 = nc - nc->queue_index;
337
Jason Wangf6b26cf2013-02-22 23:15:06 +0800338 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800339}
340
341void *qemu_get_nic_opaque(NetClientState *nc)
342{
343 NICState *nic = qemu_get_nic(nc);
344
345 return nic->opaque;
346}
347
Cindy Lu0165daa2020-07-01 22:55:25 +0800348NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
349{
350 assert(nc != NULL);
351 NetClientState *ncs = nc + queue_index;
352 return ncs->peer;
353}
354
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100355static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000356{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100357 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000358
Andreas Färbercc2a9042013-02-12 23:16:06 +0100359 if (nc->info->cleanup) {
360 nc->info->cleanup(nc);
361 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200362}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100363
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100364static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200365{
Jan Kiszka067404b2013-08-02 21:47:08 +0200366 if (nc->incoming_queue) {
367 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200368 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100369 if (nc->peer) {
370 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100371 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100372 g_free(nc->name);
373 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800374 if (nc->destructor) {
375 nc->destructor(nc);
376 }
aliguori63a01ef2008-10-31 19:10:00 +0000377}
378
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100379void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200380{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800381 NetClientState *ncs[MAX_QUEUE_NUM];
382 int queues, i;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800383 NetFilterState *nf, *next;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800384
Eric Blakef394b2e2016-07-13 21:50:23 -0600385 assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100386
Jason Wang1ceef9f2013-01-30 19:12:28 +0800387 /* If the NetClientState belongs to a multiqueue backend, we will change all
388 * other NetClientStates also.
389 */
390 queues = qemu_find_net_clients_except(nc->name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600391 NET_CLIENT_DRIVER_NIC,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800392 MAX_QUEUE_NUM);
393 assert(queues != 0);
394
Yang Hongyangfdccce42015-10-07 11:52:14 +0800395 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
396 object_unparent(OBJECT(nf));
397 }
398
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200399 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Eric Blakef394b2e2016-07-13 21:50:23 -0600400 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800401 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200402 if (nic->peer_deleted) {
403 return;
404 }
405 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800406
407 for (i = 0; i < queues; i++) {
408 ncs[i]->peer->link_down = true;
409 }
410
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100411 if (nc->peer->info->link_status_changed) {
412 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200413 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800414
415 for (i = 0; i < queues; i++) {
416 qemu_cleanup_net_client(ncs[i]);
417 }
418
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200419 return;
420 }
421
Jason Wang1ceef9f2013-01-30 19:12:28 +0800422 for (i = 0; i < queues; i++) {
423 qemu_cleanup_net_client(ncs[i]);
424 qemu_free_net_client(ncs[i]);
425 }
Jason Wang948ecf22013-01-30 19:12:24 +0800426}
427
428void qemu_del_nic(NICState *nic)
429{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200430 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800431
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800432 qemu_macaddr_set_free(&nic->conf->macaddr);
433
Yuri Benditovichd2abc562020-11-12 11:46:53 +0200434 for (i = 0; i < queues; i++) {
435 NetClientState *nc = qemu_get_subqueue(nic, i);
436 /* If this is a peer NIC and peer has already been deleted, free it now. */
437 if (nic->peer_deleted) {
438 qemu_free_net_client(nc->peer);
439 } else if (nc->peer) {
440 /* if there are RX packets pending, complete them */
441 qemu_purge_queued_packets(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200442 }
443 }
444
Jason Wang1ceef9f2013-01-30 19:12:28 +0800445 for (i = queues - 1; i >= 0; i--) {
446 NetClientState *nc = qemu_get_subqueue(nic, i);
447
448 qemu_cleanup_net_client(nc);
449 qemu_free_net_client(nc);
450 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800451
452 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200453}
454
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000455void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
456{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100457 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000458
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100459 QTAILQ_FOREACH(nc, &net_clients, next) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600460 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800461 if (nc->queue_index == 0) {
462 func(qemu_get_nic(nc), opaque);
463 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000464 }
465 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000466}
467
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100468bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100469{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100470 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100471 return false;
472 }
473
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100474 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100475}
476
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100477bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100478{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100479 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100480 return false;
481 }
482
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100483 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100484}
485
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100486bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100487{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100488 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100489 return false;
490 }
491
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100492 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100493}
494
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100495void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100496{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100497 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100498 return;
499 }
500
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100501 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100502}
503
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100504void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100505 int ecn, int ufo)
506{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100507 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100508 return;
509 }
510
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100511 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100512}
513
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100514void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100515{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100516 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100517 return;
518 }
519
Zhang Chend6b732e2017-07-04 14:53:45 +0800520 nc->vnet_hdr_len = len;
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100521 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100522}
523
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200524int qemu_set_vnet_le(NetClientState *nc, bool is_le)
525{
Marc-André Lureaue03b5682022-03-23 19:57:17 +0400526#if HOST_BIG_ENDIAN
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200527 if (!nc || !nc->info->set_vnet_le) {
528 return -ENOSYS;
529 }
530
531 return nc->info->set_vnet_le(nc, is_le);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300532#else
533 return 0;
534#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200535}
536
537int qemu_set_vnet_be(NetClientState *nc, bool is_be)
538{
Marc-André Lureaue03b5682022-03-23 19:57:17 +0400539#if HOST_BIG_ENDIAN
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300540 return 0;
541#else
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200542 if (!nc || !nc->info->set_vnet_be) {
543 return -ENOSYS;
544 }
545
546 return nc->info->set_vnet_be(nc, is_be);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300547#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200548}
549
Jason Wang705df542021-02-24 11:44:36 +0800550int qemu_can_receive_packet(NetClientState *nc)
551{
552 if (nc->receive_disabled) {
553 return 0;
554 } else if (nc->info->can_receive &&
555 !nc->info->can_receive(nc)) {
556 return 0;
557 }
558 return 1;
559}
560
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100561int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000562{
zhanghailiange1d64c02014-08-26 16:06:17 +0800563 int vm_running = runstate_is_running();
564
565 if (!vm_running) {
566 return 0;
567 }
568
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100569 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100570 return 1;
571 }
572
Jason Wang705df542021-02-24 11:44:36 +0800573 return qemu_can_receive_packet(sender->peer);
aliguori63a01ef2008-10-31 19:10:00 +0000574}
575
Yang Hongyange64c7702015-10-07 11:52:15 +0800576static ssize_t filter_receive_iov(NetClientState *nc,
577 NetFilterDirection direction,
578 NetClientState *sender,
579 unsigned flags,
580 const struct iovec *iov,
581 int iovcnt,
582 NetPacketSent *sent_cb)
583{
584 ssize_t ret = 0;
585 NetFilterState *nf = NULL;
586
Li Zhijian25aaadf2016-01-26 13:00:22 +0800587 if (direction == NET_FILTER_DIRECTION_TX) {
588 QTAILQ_FOREACH(nf, &nc->filters, next) {
589 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
590 iovcnt, sent_cb);
591 if (ret) {
592 return ret;
593 }
594 }
595 } else {
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100596 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
Li Zhijian25aaadf2016-01-26 13:00:22 +0800597 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
598 iovcnt, sent_cb);
599 if (ret) {
600 return ret;
601 }
Yang Hongyange64c7702015-10-07 11:52:15 +0800602 }
603 }
604
605 return ret;
606}
607
608static ssize_t filter_receive(NetClientState *nc,
609 NetFilterDirection direction,
610 NetClientState *sender,
611 unsigned flags,
612 const uint8_t *data,
613 size_t size,
614 NetPacketSent *sent_cb)
615{
616 struct iovec iov = {
617 .iov_base = (void *)data,
618 .iov_len = size
619 };
620
621 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
622}
623
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100624void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000625{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100626 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100627 return;
628 }
629
Jan Kiszka067404b2013-08-02 21:47:08 +0200630 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100631}
632
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300633void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100634{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100635 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100636
Eric Blakef394b2e2016-07-13 21:50:23 -0600637 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
Luigi Rizzo199ee602013-02-05 17:53:31 +0100638 if (net_hub_flush(nc->peer)) {
639 qemu_notify_event();
640 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100641 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200642 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200643 /* We emptied the queue successfully, signal to the IO thread to repoll
644 * the file descriptor (for tap, for example).
645 */
646 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300647 } else if (purge) {
648 /* Unable to empty the queue, purge remaining packets */
Jason Wang5fe19fb2020-05-11 12:04:53 +0800649 qemu_net_queue_purge(nc->incoming_queue, nc->peer);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200650 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100651}
652
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300653void qemu_flush_queued_packets(NetClientState *nc)
654{
655 qemu_flush_or_purge_queued_packets(nc, false);
656}
657
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100658static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100659 unsigned flags,
660 const uint8_t *buf, int size,
661 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000662{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100663 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800664 int ret;
Mark McLoughline94667b2009-04-29 11:48:12 +0100665
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100666#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100667 printf("qemu_send_packet_async:\n");
Philippe Mathieu-Daudéb42581f2020-08-22 20:09:50 +0200668 qemu_hexdump(stdout, "net", buf, size);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100669#endif
670
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100671 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100672 return size;
673 }
674
Yang Hongyange64c7702015-10-07 11:52:15 +0800675 /* Let filters handle the packet first */
676 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
677 sender, flags, buf, size, sent_cb);
678 if (ret) {
679 return ret;
680 }
681
682 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
683 sender, flags, buf, size, sent_cb);
684 if (ret) {
685 return ret;
686 }
687
Jan Kiszka067404b2013-08-02 21:47:08 +0200688 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100689
Mark McLoughlinca77d172009-10-22 17:43:41 +0100690 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
691}
692
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100693ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100694 const uint8_t *buf, int size,
695 NetPacketSent *sent_cb)
696{
697 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
698 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100699}
700
Marc-André Lureau625a5262019-01-17 15:43:54 +0400701ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100702{
Marc-André Lureau625a5262019-01-17 15:43:54 +0400703 return qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000704}
705
Jason Wang705df542021-02-24 11:44:36 +0800706ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size)
707{
708 if (!qemu_can_receive_packet(nc)) {
709 return 0;
710 }
711
712 return qemu_net_queue_receive(nc->incoming_queue, buf, size);
713}
714
715ssize_t qemu_receive_packet_iov(NetClientState *nc, const struct iovec *iov,
716 int iovcnt)
717{
718 if (!qemu_can_receive_packet(nc)) {
719 return 0;
720 }
721
722 return qemu_net_queue_receive_iov(nc->incoming_queue, iov, iovcnt);
723}
724
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100725ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100726{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100727 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100728 buf, size, NULL);
729}
730
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100731static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800732 int iovcnt, unsigned flags)
aliguorifbe78f42008-12-17 19:13:11 +0000733{
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530734 uint8_t *buf = NULL;
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800735 uint8_t *buffer;
Benjamin Poirierce053662011-02-23 19:57:21 -0500736 size_t offset;
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530737 ssize_t ret;
aliguorifbe78f42008-12-17 19:13:11 +0000738
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800739 if (iovcnt == 1) {
740 buffer = iov[0].iov_base;
741 offset = iov[0].iov_len;
742 } else {
Peter Lieven47f9f152016-06-30 11:49:40 +0200743 offset = iov_size(iov, iovcnt);
744 if (offset > NET_BUFSIZE) {
745 return -1;
746 }
747 buf = g_malloc(offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800748 buffer = buf;
Peter Lieven47f9f152016-06-30 11:49:40 +0200749 offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800750 }
aliguorifbe78f42008-12-17 19:13:11 +0000751
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800752 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530753 ret = nc->info->receive_raw(nc, buffer, offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800754 } else {
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530755 ret = nc->info->receive(nc, buffer, offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800756 }
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530757
758 g_free(buf);
759 return ret;
aliguorifbe78f42008-12-17 19:13:11 +0000760}
761
Jason Wang25c01bd2018-12-04 11:53:43 +0800762static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
763 unsigned flags,
764 const struct iovec *iov,
765 int iovcnt,
766 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100767{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100768 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100769 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100770
Jason Wang1592a992018-05-30 13:16:36 +0800771
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100772 if (nc->link_down) {
Jason Wang25c01bd2018-12-04 11:53:43 +0800773 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100774 }
775
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100776 if (nc->receive_disabled) {
777 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100778 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100779
Peter Lievenca1ee3d2016-06-09 11:39:27 +0200780 if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) {
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100781 ret = nc->info->receive_iov(nc, iov, iovcnt);
782 } else {
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800783 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100784 }
785
786 if (ret == 0) {
787 nc->receive_disabled = 1;
788 }
789
790 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100791}
792
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100793ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100794 const struct iovec *iov, int iovcnt,
795 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000796{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100797 NetQueue *queue;
Jason Wang25c01bd2018-12-04 11:53:43 +0800798 size_t size = iov_size(iov, iovcnt);
Yang Hongyange64c7702015-10-07 11:52:15 +0800799 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100800
Jason Wang25c01bd2018-12-04 11:53:43 +0800801 if (size > NET_BUFSIZE) {
802 return size;
803 }
804
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100805 if (sender->link_down || !sender->peer) {
Jason Wang25c01bd2018-12-04 11:53:43 +0800806 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000807 }
808
Yang Hongyange64c7702015-10-07 11:52:15 +0800809 /* Let filters handle the packet first */
810 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
811 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
812 if (ret) {
813 return ret;
814 }
815
816 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
817 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
818 if (ret) {
819 return ret;
820 }
821
Jan Kiszka067404b2013-08-02 21:47:08 +0200822 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100823
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100824 return qemu_net_queue_send_iov(queue, sender,
825 QEMU_NET_PACKET_FLAG_NONE,
826 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000827}
828
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100829ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100830qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100831{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100832 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100833}
834
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100835NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000836{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100837 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100838
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100839 QTAILQ_FOREACH(nc, &net_clients, next) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600840 if (nc->info->type == NET_CLIENT_DRIVER_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200841 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100842 if (!strcmp(nc->name, id)) {
843 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100844 }
845 }
846
847 return NULL;
848}
849
Jason Wang6c51ae72013-01-30 19:12:25 +0800850int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600851 NetClientDriver type, int max)
Jason Wang6c51ae72013-01-30 19:12:25 +0800852{
853 NetClientState *nc;
854 int ret = 0;
855
856 QTAILQ_FOREACH(nc, &net_clients, next) {
857 if (nc->info->type == type) {
858 continue;
859 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100860 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800861 if (ret < max) {
862 ncs[ret] = nc;
863 }
864 ret++;
865 }
866 }
867
868 return ret;
869}
870
aliguori76970792009-02-11 15:20:03 +0000871static int nic_get_free_idx(void)
872{
873 int index;
874
875 for (index = 0; index < MAX_NICS; index++)
876 if (!nd_table[index].used)
877 return index;
878 return -1;
879}
880
Markus Armbruster07caea32009-09-25 03:53:51 +0200881int qemu_show_nic_models(const char *arg, const char *const *models)
882{
883 int i;
884
Peter Maydellc8057f92012-08-02 13:45:54 +0100885 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200886 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100887 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200888
Thomas Huth7b71e032019-04-23 18:06:08 +0200889 printf("Supported NIC models:\n");
890 for (i = 0 ; models[i]; i++) {
891 printf("%s\n", models[i]);
892 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200893 return 1;
894}
895
aliguorid07f22c2009-01-13 19:03:57 +0000896void qemu_check_nic_model(NICInfo *nd, const char *model)
897{
898 const char *models[2];
899
900 models[0] = model;
901 models[1] = NULL;
902
Markus Armbruster07caea32009-09-25 03:53:51 +0200903 if (qemu_show_nic_models(nd->model, models))
904 exit(0);
905 if (qemu_find_nic_model(nd, models, model) < 0)
906 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000907}
908
Markus Armbruster07caea32009-09-25 03:53:51 +0200909int qemu_find_nic_model(NICInfo *nd, const char * const *models,
910 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000911{
Markus Armbruster07caea32009-09-25 03:53:51 +0200912 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000913
914 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500915 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000916
Markus Armbruster07caea32009-09-25 03:53:51 +0200917 for (i = 0 ; models[i]; i++) {
918 if (strcmp(nd->model, models[i]) == 0)
919 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000920 }
921
Markus Armbruster6daf1942011-06-22 14:03:54 +0200922 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200923 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000924}
925
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600926static int net_init_nic(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200927 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100928{
929 int idx;
930 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200931 const NetLegacyNicOptions *nic;
932
Eric Blakef394b2e2016-07-13 21:50:23 -0600933 assert(netdev->type == NET_CLIENT_DRIVER_NIC);
934 nic = &netdev->u.nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100935
936 idx = nic_get_free_idx();
937 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200938 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100939 return -1;
940 }
941
942 nd = &nd_table[idx];
943
944 memset(nd, 0, sizeof(*nd));
945
Laszlo Ersek2456f362012-07-17 16:17:14 +0200946 if (nic->has_netdev) {
947 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100948 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200949 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100950 return -1;
951 }
952 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100953 assert(peer);
954 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100955 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100956 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200957 if (nic->has_model) {
958 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100959 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200960 if (nic->has_addr) {
961 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100962 }
963
Laszlo Ersek2456f362012-07-17 16:17:14 +0200964 if (nic->has_macaddr &&
965 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200966 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100967 return -1;
968 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400969 if (nic->has_macaddr &&
970 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200971 error_setg(errp,
972 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400973 return -1;
974 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200975 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100976
Laszlo Ersek2456f362012-07-17 16:17:14 +0200977 if (nic->has_vectors) {
978 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200979 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200980 return -1;
981 }
982 nd->nvectors = nic->vectors;
983 } else {
984 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100985 }
986
987 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100988 nb_nics++;
989
990 return idx;
991}
992
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100993
Eric Blakef394b2e2016-07-13 21:50:23 -0600994static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600995 const Netdev *netdev,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200996 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200997 NetClientState *peer, Error **errp) = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600998 [NET_CLIENT_DRIVER_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100999#ifdef CONFIG_SLIRP
Eric Blakef394b2e2016-07-13 21:50:23 -06001000 [NET_CLIENT_DRIVER_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +01001001#endif
Eric Blakef394b2e2016-07-13 21:50:23 -06001002 [NET_CLIENT_DRIVER_TAP] = net_init_tap,
1003 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +01001004#ifdef CONFIG_VDE
Eric Blakef394b2e2016-07-13 21:50:23 -06001005 [NET_CLIENT_DRIVER_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +01001006#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +01001007#ifdef CONFIG_NETMAP
Eric Blakef394b2e2016-07-13 21:50:23 -06001008 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
Vincenzo Maffione58952132013-11-06 11:44:06 +01001009#endif
Stefan Weil2944e4e2012-02-04 09:24:46 +01001010#ifdef CONFIG_NET_BRIDGE
Eric Blakef394b2e2016-07-13 21:50:23 -06001011 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +02001012#endif
Eric Blakef394b2e2016-07-13 21:50:23 -06001013 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
Paolo Bonzini56f41de2019-02-14 18:35:49 +01001014#ifdef CONFIG_VHOST_NET_USER
Eric Blakef394b2e2016-07-13 21:50:23 -06001015 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +03001016#endif
Cindy Lu1e0a84e2020-07-01 22:55:38 +08001017#ifdef CONFIG_VHOST_NET_VDPA
1018 [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa,
1019#endif
Gonglei015a33b2014-07-01 20:58:08 +08001020#ifdef CONFIG_L2TPV3
Eric Blakef394b2e2016-07-13 21:50:23 -06001021 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
Anton Ivanov3fb69aa2014-06-20 10:34:41 +01001022#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001023};
1024
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001025
Thomas Huth71830d82020-05-18 20:01:03 +02001026static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +02001027{
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001028 NetClientState *peer = NULL;
Markus Armbruster831734c2020-11-25 11:02:20 +01001029 NetClientState *nc;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001030
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001031 if (is_netdev) {
Thomas Huth857d2082018-02-21 11:18:34 +01001032 if (netdev->type == NET_CLIENT_DRIVER_NIC ||
Eric Blakef394b2e2016-07-13 21:50:23 -06001033 !net_client_init_fun[netdev->type]) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001034 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1035 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001036 return -1;
1037 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001038 } else {
Thomas Huth71830d82020-05-18 20:01:03 +02001039 if (netdev->type == NET_CLIENT_DRIVER_NONE) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001040 return 0; /* nothing to do */
Markus Armbrusterca7eb182015-05-15 13:58:49 +02001041 }
Thomas Huth71830d82020-05-18 20:01:03 +02001042 if (netdev->type == NET_CLIENT_DRIVER_HUBPORT ||
1043 !net_client_init_fun[netdev->type]) {
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +01001044 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
1045 "a net backend type (maybe it is not compiled "
1046 "into this binary)");
1047 return -1;
1048 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001049
Thomas Huthaf1a5c32018-04-30 20:02:23 +02001050 /* Do not add to a hub if it's a nic with a netdev= parameter. */
Eric Blakef394b2e2016-07-13 21:50:23 -06001051 if (netdev->type != NET_CLIENT_DRIVER_NIC ||
Thomas Huth71830d82020-05-18 20:01:03 +02001052 !netdev->u.nic.has_netdev) {
Thomas Huthaf1a5c32018-04-30 20:02:23 +02001053 peer = net_hub_add_port(0, NULL, NULL);
Thomas Hutha2dbe132017-01-24 10:42:49 +01001054 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001055 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001056
Markus Armbruster831734c2020-11-25 11:02:20 +01001057 nc = qemu_find_netdev(netdev->id);
1058 if (nc) {
1059 error_setg(errp, "Duplicate ID '%s'", netdev->id);
1060 return -1;
1061 }
1062
Thomas Huth9d903f32020-05-18 20:01:02 +02001063 if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) {
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001064 /* FIXME drop when all init functions store an Error */
1065 if (errp && !*errp) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001066 error_setg(errp, "Device '%s' could not be initialized",
Markus Armbruster977c7362017-08-24 10:46:08 +02001067 NetClientDriver_str(netdev->type));
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001068 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001069 return -1;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001070 }
Eric Blake08712fc2020-03-17 15:17:11 -05001071
1072 if (is_netdev) {
Eric Blake08712fc2020-03-17 15:17:11 -05001073 nc = qemu_find_netdev(netdev->id);
1074 assert(nc);
1075 nc->is_netdev = true;
1076 }
1077
Laszlo Ersek6687b792012-07-17 16:17:13 +02001078 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001079}
1080
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001081void show_netdevs(void)
Thomas Huth547203e2018-02-21 11:18:31 +01001082{
1083 int idx;
1084 const char *available_netdevs[] = {
1085 "socket",
1086 "hubport",
1087 "tap",
1088#ifdef CONFIG_SLIRP
1089 "user",
1090#endif
1091#ifdef CONFIG_L2TPV3
1092 "l2tpv3",
1093#endif
1094#ifdef CONFIG_VDE
1095 "vde",
1096#endif
1097#ifdef CONFIG_NET_BRIDGE
1098 "bridge",
1099#endif
1100#ifdef CONFIG_NETMAP
1101 "netmap",
1102#endif
1103#ifdef CONFIG_POSIX
1104 "vhost-user",
1105#endif
Cindy Lu1bc211a2020-10-16 11:09:09 +08001106#ifdef CONFIG_VHOST_VDPA
1107 "vhost-vdpa",
1108#endif
Thomas Huth547203e2018-02-21 11:18:31 +01001109 };
Laszlo Ersek6687b792012-07-17 16:17:13 +02001110
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001111 qemu_printf("Available netdev backend types:\n");
Thomas Huth547203e2018-02-21 11:18:31 +01001112 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001113 qemu_printf("%s\n", available_netdevs[idx]);
Thomas Huth547203e2018-02-21 11:18:31 +01001114 }
1115}
Laszlo Ersek6687b792012-07-17 16:17:13 +02001116
Thomas Huthaa09a482018-02-21 11:18:33 +01001117static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +02001118{
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001119 gchar **substrings = NULL;
Thomas Huth71830d82020-05-18 20:01:03 +02001120 Netdev *object = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +02001121 int ret = -1;
Eric Blake09204ea2016-06-09 10:48:36 -06001122 Visitor *v = opts_visitor_new(opts);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001123
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001124 /* Parse convenience option format ip6-net=fec0::0[/64] */
1125 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
Lin Ma8b43f962018-06-11 17:23:05 +08001126
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001127 if (ip6_net) {
1128 char *prefix_addr;
1129 unsigned long prefix_len = 64; /* Default 64bit prefix length. */
Yann Bordenave7aac5312016-03-15 10:31:22 +01001130
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001131 substrings = g_strsplit(ip6_net, "/", 2);
1132 if (!substrings || !substrings[0]) {
1133 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net",
1134 "a valid IPv6 prefix");
1135 goto out;
Yann Bordenave7aac5312016-03-15 10:31:22 +01001136 }
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001137
1138 prefix_addr = substrings[0];
1139
1140 /* Handle user-specified prefix length. */
1141 if (substrings[1] &&
1142 qemu_strtoul(substrings[1], NULL, 10, &prefix_len))
1143 {
1144 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1145 "ipv6-prefixlen", "a number");
1146 goto out;
1147 }
1148
1149 qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort);
1150 qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len,
1151 &error_abort);
1152 qemu_opt_unset(opts, "ipv6-net");
Yann Bordenave7aac5312016-03-15 10:31:22 +01001153 }
1154
Thomas Huth71830d82020-05-18 20:01:03 +02001155 /* Create an ID for -net if the user did not specify one */
1156 if (!is_netdev && !qemu_opts_id(opts)) {
Thomas Huth27eb3722021-02-15 10:02:25 +01001157 qemu_opts_set_id(opts, id_generate(ID_NET));
Laszlo Ersek6687b792012-07-17 16:17:13 +02001158 }
1159
Markus Armbruster14217032020-07-07 18:05:47 +02001160 if (visit_type_Netdev(v, NULL, &object, errp)) {
1161 ret = net_client_init1(object, is_netdev, errp);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001162 }
1163
Thomas Huth71830d82020-05-18 20:01:03 +02001164 qapi_free_Netdev(object);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001165
Stefano Garzarella21c520d2019-05-17 15:47:45 +02001166out:
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001167 g_strfreev(substrings);
Eric Blake09204ea2016-06-09 10:48:36 -06001168 visit_free(v);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001169 return ret;
1170}
1171
Luiz Capitulino928059a2012-04-18 17:34:15 -03001172void netdev_add(QemuOpts *opts, Error **errp)
1173{
Eric Blake0e55c382016-07-13 21:50:24 -06001174 net_client_init(opts, true, errp);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001175}
1176
Eric Blakedb2a3802020-03-17 15:17:10 -05001177void qmp_netdev_add(Netdev *netdev, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001178{
Paolo Bonzinie73b4312021-03-12 09:51:38 -05001179 if (!id_wellformed(netdev->id)) {
1180 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
1181 return;
1182 }
1183
Eric Blake08712fc2020-03-17 15:17:11 -05001184 net_client_init1(netdev, true, errp);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001185}
1186
Luiz Capitulino5f964152012-04-16 14:36:32 -03001187void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001188{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001189 NetClientState *nc;
Markus Armbruster831734c2020-11-25 11:02:20 +01001190 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001191
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001192 nc = qemu_find_netdev(id);
1193 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001194 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1195 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001196 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001197 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001198
Eric Blake08712fc2020-03-17 15:17:11 -05001199 if (!nc->is_netdev) {
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001200 error_setg(errp, "Device '%s' is not a netdev", id);
1201 return;
1202 }
1203
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001204 qemu_del_net_client(nc);
Markus Armbruster831734c2020-11-25 11:02:20 +01001205
1206 /*
1207 * Wart: we need to delete the QemuOpts associated with netdevs
1208 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1209 * HMP netdev_add.
1210 */
1211 opts = qemu_opts_find(qemu_find_opts("netdev"), id);
1212 if (opts) {
1213 qemu_opts_del(opts);
1214 }
Markus Armbrusterae82d322010-03-25 17:22:40 +01001215}
1216
zhanghailiangaa9156f2016-01-26 14:43:33 +08001217static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1218{
1219 char *str;
1220 ObjectProperty *prop;
1221 ObjectPropertyIterator iter;
Eric Blake3b098d52016-06-09 10:48:43 -06001222 Visitor *v;
zhanghailiangaa9156f2016-01-26 14:43:33 +08001223
1224 /* generate info str */
1225 object_property_iter_init(&iter, OBJECT(nf));
1226 while ((prop = object_property_iter_next(&iter))) {
1227 if (!strcmp(prop->name, "type")) {
1228 continue;
1229 }
Eric Blake3b098d52016-06-09 10:48:43 -06001230 v = string_output_visitor_new(false, &str);
Markus Armbruster5325cc32020-07-07 18:05:54 +02001231 object_property_get(OBJECT(nf), prop->name, v, NULL);
Eric Blake3b098d52016-06-09 10:48:43 -06001232 visit_complete(v, &str);
1233 visit_free(v);
zhanghailiangaa9156f2016-01-26 14:43:33 +08001234 monitor_printf(mon, ",%s=%s", prop->name, str);
1235 g_free(str);
1236 }
1237 monitor_printf(mon, "\n");
1238}
1239
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001240void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001241{
Yang Hongyanga4960f52015-10-07 11:52:19 +08001242 NetFilterState *nf;
1243
Jason Wang1ceef9f2013-01-30 19:12:28 +08001244 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1245 nc->queue_index,
Markus Armbruster977c7362017-08-24 10:46:08 +02001246 NetClientDriver_str(nc->info->type),
Jason Wang56e6f592021-04-02 11:03:33 +08001247 nc->info_str);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001248 if (!QTAILQ_EMPTY(&nc->filters)) {
1249 monitor_printf(mon, "filters:\n");
1250 }
1251 QTAILQ_FOREACH(nf, &nc->filters, next) {
Markus Armbruster7a309cc2020-07-14 18:02:00 +02001252 monitor_printf(mon, " - %s: type=%s",
1253 object_get_canonical_path_component(OBJECT(nf)),
zhanghailiangaa9156f2016-01-26 14:43:33 +08001254 object_get_typename(OBJECT(nf)));
1255 netfilter_print_info(mon, nf);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001256 }
Jan Kiszka44e798d2011-07-20 12:20:21 +02001257}
1258
Amos Kongb1be4282013-06-14 15:45:52 +08001259RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1260 Error **errp)
1261{
1262 NetClientState *nc;
Eric Blake95b3a8c2021-01-13 16:10:13 -06001263 RxFilterInfoList *filter_list = NULL, **tail = &filter_list;
Amos Kongb1be4282013-06-14 15:45:52 +08001264
1265 QTAILQ_FOREACH(nc, &net_clients, next) {
Amos Kongb1be4282013-06-14 15:45:52 +08001266 RxFilterInfo *info;
1267
1268 if (has_name && strcmp(nc->name, name) != 0) {
1269 continue;
1270 }
1271
1272 /* only query rx-filter information of NIC */
Eric Blakef394b2e2016-07-13 21:50:23 -06001273 if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
Amos Kongb1be4282013-06-14 15:45:52 +08001274 if (has_name) {
1275 error_setg(errp, "net client(%s) isn't a NIC", name);
Eric Blakee9d635e2021-01-13 16:10:09 -06001276 assert(!filter_list);
Markus Armbruster9083da12014-04-24 15:44:18 +02001277 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001278 }
1279 continue;
1280 }
1281
Vladislav Yasevich5320c2c2015-10-19 09:04:38 -04001282 /* only query information on queue 0 since the info is per nic,
1283 * not per queue
1284 */
1285 if (nc->queue_index != 0)
1286 continue;
1287
Amos Kongb1be4282013-06-14 15:45:52 +08001288 if (nc->info->query_rx_filter) {
1289 info = nc->info->query_rx_filter(nc);
Eric Blake95b3a8c2021-01-13 16:10:13 -06001290 QAPI_LIST_APPEND(tail, info);
Amos Kongb1be4282013-06-14 15:45:52 +08001291 } else if (has_name) {
1292 error_setg(errp, "net client(%s) doesn't support"
1293 " rx-filter querying", name);
Eric Blakee9d635e2021-01-13 16:10:09 -06001294 assert(!filter_list);
Markus Armbruster9083da12014-04-24 15:44:18 +02001295 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001296 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001297
1298 if (has_name) {
1299 break;
1300 }
Amos Kongb1be4282013-06-14 15:45:52 +08001301 }
1302
Markus Armbruster9083da12014-04-24 15:44:18 +02001303 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001304 error_setg(errp, "invalid net client name: %s", name);
1305 }
1306
1307 return filter_list;
1308}
1309
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001310void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001311{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001312 NetClientState *nc, *peer;
Eric Blakef394b2e2016-07-13 21:50:23 -06001313 NetClientDriver type;
aliguori63a01ef2008-10-31 19:10:00 +00001314
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001315 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001316
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001317 QTAILQ_FOREACH(nc, &net_clients, next) {
1318 peer = nc->peer;
1319 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001320
1321 /* Skip if already printed in hub info */
1322 if (net_hub_id_for_client(nc, NULL) == 0) {
1323 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001324 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001325
Eric Blakef394b2e2016-07-13 21:50:23 -06001326 if (!peer || type == NET_CLIENT_DRIVER_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001327 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001328 } /* else it's a netdev connected to a NIC, printed with the NIC */
Eric Blakef394b2e2016-07-13 21:50:23 -06001329 if (peer && type == NET_CLIENT_DRIVER_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001330 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001331 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001332 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001333 }
aliguori63a01ef2008-10-31 19:10:00 +00001334}
1335
Zhang Chen5fbba3d2018-09-03 12:38:56 +08001336void colo_notify_filters_event(int event, Error **errp)
1337{
1338 NetClientState *nc;
1339 NetFilterState *nf;
1340 NetFilterClass *nfc = NULL;
1341 Error *local_err = NULL;
1342
1343 QTAILQ_FOREACH(nc, &net_clients, next) {
1344 QTAILQ_FOREACH(nf, &nc->filters, next) {
1345 nfc = NETFILTER_GET_CLASS(OBJECT(nf));
1346 nfc->handle_event(nf, event, &local_err);
1347 if (local_err) {
1348 error_propagate(errp, local_err);
1349 return;
1350 }
1351 }
1352 }
1353}
1354
Luiz Capitulino4b371562011-11-23 13:11:55 -02001355void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001356{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001357 NetClientState *ncs[MAX_QUEUE_NUM];
1358 NetClientState *nc;
1359 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001360
Jason Wang1ceef9f2013-01-30 19:12:28 +08001361 queues = qemu_find_net_clients_except(name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06001362 NET_CLIENT_DRIVER__MAX,
Jason Wang1ceef9f2013-01-30 19:12:28 +08001363 MAX_QUEUE_NUM);
1364
1365 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001366 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1367 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001368 return;
aliguori436e5e52009-01-08 19:44:06 +00001369 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001370 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001371
Jason Wang1ceef9f2013-01-30 19:12:28 +08001372 for (i = 0; i < queues; i++) {
1373 ncs[i]->link_down = !up;
1374 }
aliguori436e5e52009-01-08 19:44:06 +00001375
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001376 if (nc->info->link_status_changed) {
1377 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001378 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001379
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001380 if (nc->peer) {
1381 /* Change peer link only if the peer is NIC and then notify peer.
1382 * If the peer is a HUBPORT or a backend, we do not change the
1383 * link status.
1384 *
Thomas Huthaf1a5c32018-04-30 20:02:23 +02001385 * This behavior is compatible with qemu hubs where there could be
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001386 * multiple clients that can still communicate with each other in
1387 * disconnected mode. For now maintain this compatibility.
1388 */
Eric Blakef394b2e2016-07-13 21:50:23 -06001389 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001390 for (i = 0; i < queues; i++) {
1391 ncs[i]->peer->link_down = !up;
1392 }
1393 }
1394 if (nc->peer->info->link_status_changed) {
1395 nc->peer->info->link_status_changed(nc->peer);
1396 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001397 }
aliguori436e5e52009-01-08 19:44:06 +00001398}
1399
Philippe Mathieu-Daudé538f0492021-01-11 16:20:20 +01001400static void net_vm_change_state_handler(void *opaque, bool running,
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001401 RunState state)
1402{
Fam Zheng625de442015-07-07 09:21:07 +08001403 NetClientState *nc;
1404 NetClientState *tmp;
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001405
Fam Zheng625de442015-07-07 09:21:07 +08001406 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1407 if (running) {
1408 /* Flush queued packets and wake up backends. */
1409 if (nc->peer && qemu_can_send_packet(nc)) {
1410 qemu_flush_queued_packets(nc->peer);
1411 }
1412 } else {
1413 /* Complete all queued packets, to guarantee we don't modify
1414 * state later when VM is not running.
1415 */
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001416 qemu_flush_or_purge_queued_packets(nc, true);
1417 }
1418 }
1419}
1420
aliguori63a01ef2008-10-31 19:10:00 +00001421void net_cleanup(void)
1422{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001423 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001424
Rao, Lei0c7af1a2021-06-08 16:23:30 +08001425 /*cleanup colo compare module for COLO*/
1426 colo_compare_cleanup();
1427
Jason Wang1ceef9f2013-01-30 19:12:28 +08001428 /* We may del multiple entries during qemu_del_net_client(),
1429 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1430 */
1431 while (!QTAILQ_EMPTY(&net_clients)) {
1432 nc = QTAILQ_FIRST(&net_clients);
Eric Blakef394b2e2016-07-13 21:50:23 -06001433 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wang948ecf22013-01-30 19:12:24 +08001434 qemu_del_nic(qemu_get_nic(nc));
1435 } else {
1436 qemu_del_net_client(nc);
1437 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001438 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001439
1440 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001441}
1442
Markus Armbruster668680f2010-02-11 14:44:58 +01001443void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001444{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001445 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001446 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001447
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001448 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001449
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001450 QTAILQ_FOREACH(nc, &net_clients, next) {
1451 if (!nc->peer) {
Alistair Francis8297be82017-09-11 12:52:53 -07001452 warn_report("%s %s has no peer",
Alistair Francisb62e39b2017-09-11 12:52:56 -07001453 nc->info->type == NET_CLIENT_DRIVER_NIC
1454 ? "nic" : "netdev",
1455 nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001456 }
1457 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001458
1459 /* Check that all NICs requested via -net nic actually got created.
1460 * NICs created via -device don't need to be checked here because
1461 * they are always instantiated.
1462 */
1463 for (i = 0; i < MAX_NICS; i++) {
1464 NICInfo *nd = &nd_table[i];
1465 if (nd->used && !nd->instantiated) {
Alistair Francis8297be82017-09-11 12:52:53 -07001466 warn_report("requested NIC (%s, model %s) "
1467 "was not created (not supported by this machine?)",
1468 nd->name ? nd->name : "anonymous",
1469 nd->model ? nd->model : "unspecified");
Peter Maydell48e2faf2011-05-20 16:50:01 +01001470 }
1471 }
aliguori63a01ef2008-10-31 19:10:00 +00001472}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001473
Markus Armbruster28d0de72015-03-13 13:35:14 +01001474static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001475{
Thomas Huth34f708b2018-02-21 11:18:30 +01001476 return net_client_init(opts, false, errp);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001477}
1478
Markus Armbruster28d0de72015-03-13 13:35:14 +01001479static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001480{
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001481 const char *type = qemu_opt_get(opts, "type");
1482
1483 if (type && is_help_option(type)) {
1484 show_netdevs();
1485 exit(0);
1486 }
Thomas Huth34f708b2018-02-21 11:18:30 +01001487 return net_client_init(opts, true, errp);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001488}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001489
Thomas Huth78cd6f72018-02-21 11:18:36 +01001490/* For the convenience "--nic" parameter */
1491static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
1492{
1493 char *mac, *nd_id;
1494 int idx, ret;
1495 NICInfo *ni;
1496 const char *type;
1497
1498 type = qemu_opt_get(opts, "type");
1499 if (type && g_str_equal(type, "none")) {
1500 return 0; /* Nothing to do, default_net is cleared in vl.c */
1501 }
1502
1503 idx = nic_get_free_idx();
1504 if (idx == -1 || nb_nics >= MAX_NICS) {
1505 error_setg(errp, "no more on-board/default NIC slots available");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001506 return -1;
1507 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001508
Thomas Huth78cd6f72018-02-21 11:18:36 +01001509 if (!type) {
1510 qemu_opt_set(opts, "type", "user", &error_abort);
1511 }
1512
1513 ni = &nd_table[idx];
1514 memset(ni, 0, sizeof(*ni));
1515 ni->model = qemu_opt_get_del(opts, "model");
1516
1517 /* Create an ID if the user did not specify one */
1518 nd_id = g_strdup(qemu_opts_id(opts));
1519 if (!nd_id) {
Thomas Huth27eb3722021-02-15 10:02:25 +01001520 nd_id = id_generate(ID_NET);
Thomas Huth78cd6f72018-02-21 11:18:36 +01001521 qemu_opts_set_id(opts, nd_id);
1522 }
1523
1524 /* Handle MAC address */
1525 mac = qemu_opt_get_del(opts, "mac");
1526 if (mac) {
1527 ret = net_parse_macaddr(ni->macaddr.a, mac);
1528 g_free(mac);
1529 if (ret) {
1530 error_setg(errp, "invalid syntax for ethernet address");
Thomas Huth9d946192018-04-30 09:26:45 +02001531 goto out;
Thomas Huth78cd6f72018-02-21 11:18:36 +01001532 }
1533 if (is_multicast_ether_addr(ni->macaddr.a)) {
1534 error_setg(errp, "NIC cannot have multicast MAC address");
Thomas Huth9d946192018-04-30 09:26:45 +02001535 ret = -1;
1536 goto out;
Thomas Huth78cd6f72018-02-21 11:18:36 +01001537 }
1538 }
1539 qemu_macaddr_default_if_unset(&ni->macaddr);
1540
1541 ret = net_client_init(opts, true, errp);
1542 if (ret == 0) {
1543 ni->netdev = qemu_find_netdev(nd_id);
1544 ni->used = true;
1545 nb_nics++;
1546 }
1547
Thomas Huth9d946192018-04-30 09:26:45 +02001548out:
Thomas Huth78cd6f72018-02-21 11:18:36 +01001549 g_free(nd_id);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001550 return ret;
1551}
1552
Thomas Huth34f708b2018-02-21 11:18:30 +01001553int net_init_clients(Error **errp)
aliguori63a01ef2008-10-31 19:10:00 +00001554{
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001555 net_change_state_entry =
1556 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1557
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001558 QTAILQ_INIT(&net_clients);
aliguori63a01ef2008-10-31 19:10:00 +00001559
Markus Armbruster28d0de72015-03-13 13:35:14 +01001560 if (qemu_opts_foreach(qemu_find_opts("netdev"),
Thomas Huth34f708b2018-02-21 11:18:30 +01001561 net_init_netdev, NULL, errp)) {
aliguori63a01ef2008-10-31 19:10:00 +00001562 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001563 }
aliguori63a01ef2008-10-31 19:10:00 +00001564
Thomas Huth78cd6f72018-02-21 11:18:36 +01001565 if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, errp)) {
1566 return -1;
1567 }
1568
Thomas Huth34f708b2018-02-21 11:18:30 +01001569 if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, errp)) {
aliguori63a01ef2008-10-31 19:10:00 +00001570 return -1;
1571 }
1572
aliguori63a01ef2008-10-31 19:10:00 +00001573 return 0;
1574}
1575
1576int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1577{
Markus Armbruster70b94332015-02-13 12:50:26 +01001578 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
aliguori63a01ef2008-10-31 19:10:00 +00001579 return -1;
1580 }
1581
aliguori63a01ef2008-10-31 19:10:00 +00001582 return 0;
1583}
Jason Wang7fc8d912012-03-05 11:08:50 +08001584
1585/* From FreeBSD */
1586/* XXX: optimize */
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001587uint32_t net_crc32(const uint8_t *p, int len)
Jason Wang7fc8d912012-03-05 11:08:50 +08001588{
1589 uint32_t crc;
1590 int carry, i, j;
1591 uint8_t b;
1592
1593 crc = 0xffffffff;
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001594 for (i = 0; i < len; i++) {
1595 b = *p++;
Jason Wang7fc8d912012-03-05 11:08:50 +08001596 for (j = 0; j < 8; j++) {
1597 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1598 crc <<= 1;
1599 b >>= 1;
1600 if (carry) {
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001601 crc = ((crc ^ POLYNOMIAL_BE) | carry);
Jason Wang7fc8d912012-03-05 11:08:50 +08001602 }
1603 }
1604 }
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001605
1606 return crc;
1607}
1608
Mark Cave-Aylandf1a7deb2017-12-15 18:41:44 +00001609uint32_t net_crc32_le(const uint8_t *p, int len)
1610{
1611 uint32_t crc;
1612 int carry, i, j;
1613 uint8_t b;
1614
1615 crc = 0xffffffff;
1616 for (i = 0; i < len; i++) {
1617 b = *p++;
1618 for (j = 0; j < 8; j++) {
1619 carry = (crc & 0x1) ^ (b & 0x01);
1620 crc >>= 1;
1621 b >>= 1;
1622 if (carry) {
1623 crc ^= POLYNOMIAL_LE;
1624 }
1625 }
1626 }
1627
1628 return crc;
1629}
1630
Paolo Bonzini4d454572012-11-26 16:03:42 +01001631QemuOptsList qemu_netdev_opts = {
1632 .name = "netdev",
1633 .implied_opt_name = "type",
1634 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1635 .desc = {
1636 /*
1637 * no elements => accept any params
1638 * validation will happen later
1639 */
1640 { /* end of list */ }
1641 },
1642};
1643
Thomas Huth78cd6f72018-02-21 11:18:36 +01001644QemuOptsList qemu_nic_opts = {
1645 .name = "nic",
1646 .implied_opt_name = "type",
1647 .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
1648 .desc = {
1649 /*
1650 * no elements => accept any params
1651 * validation will happen later
1652 */
1653 { /* end of list */ }
1654 },
1655};
1656
Paolo Bonzini4d454572012-11-26 16:03:42 +01001657QemuOptsList qemu_net_opts = {
1658 .name = "net",
1659 .implied_opt_name = "type",
1660 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1661 .desc = {
1662 /*
1663 * no elements => accept any params
1664 * validation will happen later
1665 */
1666 { /* end of list */ }
1667 },
1668};
Zhang Chen16a3df42016-05-13 15:35:19 +08001669
1670void net_socket_rs_init(SocketReadState *rs,
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001671 SocketReadStateFinalize *finalize,
1672 bool vnet_hdr)
Zhang Chen16a3df42016-05-13 15:35:19 +08001673{
1674 rs->state = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001675 rs->vnet_hdr = vnet_hdr;
Zhang Chen16a3df42016-05-13 15:35:19 +08001676 rs->index = 0;
1677 rs->packet_len = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001678 rs->vnet_hdr_len = 0;
Zhang Chen16a3df42016-05-13 15:35:19 +08001679 memset(rs->buf, 0, sizeof(rs->buf));
1680 rs->finalize = finalize;
1681}
1682
1683/*
1684 * Returns
Zhang Chene9e0a582016-08-18 11:23:25 +08001685 * 0: success
1686 * -1: error occurs
Zhang Chen16a3df42016-05-13 15:35:19 +08001687 */
1688int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
1689{
1690 unsigned int l;
1691
1692 while (size > 0) {
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001693 /* Reassemble a packet from the network.
1694 * 0 = getting length.
1695 * 1 = getting vnet header length.
1696 * 2 = getting data.
1697 */
1698 switch (rs->state) {
Zhang Chen16a3df42016-05-13 15:35:19 +08001699 case 0:
1700 l = 4 - rs->index;
1701 if (l > size) {
1702 l = size;
1703 }
1704 memcpy(rs->buf + rs->index, buf, l);
1705 buf += l;
1706 size -= l;
1707 rs->index += l;
1708 if (rs->index == 4) {
1709 /* got length */
1710 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
1711 rs->index = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001712 if (rs->vnet_hdr) {
1713 rs->state = 1;
1714 } else {
1715 rs->state = 2;
1716 rs->vnet_hdr_len = 0;
1717 }
Zhang Chen16a3df42016-05-13 15:35:19 +08001718 }
1719 break;
1720 case 1:
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001721 l = 4 - rs->index;
1722 if (l > size) {
1723 l = size;
1724 }
1725 memcpy(rs->buf + rs->index, buf, l);
1726 buf += l;
1727 size -= l;
1728 rs->index += l;
1729 if (rs->index == 4) {
1730 /* got vnet header length */
1731 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
1732 rs->index = 0;
1733 rs->state = 2;
1734 }
1735 break;
1736 case 2:
Zhang Chen16a3df42016-05-13 15:35:19 +08001737 l = rs->packet_len - rs->index;
1738 if (l > size) {
1739 l = size;
1740 }
1741 if (rs->index + l <= sizeof(rs->buf)) {
1742 memcpy(rs->buf + rs->index, buf, l);
1743 } else {
1744 fprintf(stderr, "serious error: oversized packet received,"
1745 "connection terminated.\n");
1746 rs->index = rs->state = 0;
1747 return -1;
1748 }
1749
1750 rs->index += l;
1751 buf += l;
1752 size -= l;
1753 if (rs->index >= rs->packet_len) {
1754 rs->index = 0;
1755 rs->state = 0;
Daniel P. Berrangee79cd402016-11-04 15:46:33 +00001756 assert(rs->finalize);
1757 rs->finalize(rs);
Zhang Chen16a3df42016-05-13 15:35:19 +08001758 }
1759 break;
1760 }
1761 }
Zhang Chene9e0a582016-08-18 11:23:25 +08001762
1763 assert(size == 0);
Zhang Chen16a3df42016-05-13 15:35:19 +08001764 return 0;
1765}