blob: 7ef6885876ffb6ae072c290bebe2cb7fe1570c82 [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"
Laurent Vivier5166fe02022-10-21 11:09:11 +020051#include "qemu/keyval.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010052#include "qapi/error.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020053#include "qapi/opts-visitor.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020054#include "sysemu/runstate.h"
Rao, Lei0c7af1a2021-06-08 16:23:30 +080055#include "net/colo-compare.h"
Yang Hongyangfdccce42015-10-07 11:52:14 +080056#include "net/filter.h"
zhanghailiangaa9156f2016-01-26 14:43:33 +080057#include "qapi/string-output-visitor.h"
Laurent Vivierf3eedcd2022-10-21 11:09:09 +020058#include "qapi/qobject-input-visitor.h"
Akihiko Odakia6775372024-04-28 16:00:46 +090059#include "standard-headers/linux/virtio_net.h"
blueswir1511d2b12009-03-07 15:32:56 +000060
Stefan Weil2944e4e2012-02-04 09:24:46 +010061/* Net bridge is currently not supported for W32. */
62#if !defined(_WIN32)
63# define CONFIG_NET_BRIDGE
64#endif
65
Michael S. Tsirkinca77d852014-09-04 11:39:13 +030066static VMChangeStateEntry *net_change_state_entry;
Markus Armbrusterae71d132023-01-24 13:19:30 +010067NetClientStateList net_clients;
aliguori63a01ef2008-10-31 19:10:00 +000068
Laurent Vivierf3eedcd2022-10-21 11:09:09 +020069typedef struct NetdevQueueEntry {
70 Netdev *nd;
71 Location loc;
72 QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
73} NetdevQueueEntry;
74
75typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
76
77static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
78
David Woodhouse2cdeca02023-10-21 23:09:38 +010079static GHashTable *nic_model_help;
80
David Woodhousee8c5c452023-10-23 09:38:01 +010081static int nb_nics;
82static NICInfo nd_table[MAX_NICS];
83
aliguori63a01ef2008-10-31 19:10:00 +000084/***********************************************************/
85/* network device redirectors */
86
Laurent Vivier30e42262022-10-21 11:09:06 +020087int convert_host_port(struct sockaddr_in *saddr, const char *host,
88 const char *port, Error **errp)
89{
90 struct hostent *he;
91 const char *r;
92 long p;
93
94 memset(saddr, 0, sizeof(*saddr));
95
96 saddr->sin_family = AF_INET;
97 if (host[0] == '\0') {
98 saddr->sin_addr.s_addr = 0;
99 } else {
100 if (qemu_isdigit(host[0])) {
101 if (!inet_aton(host, &saddr->sin_addr)) {
102 error_setg(errp, "host address '%s' is not a valid "
103 "IPv4 address", host);
104 return -1;
105 }
106 } else {
107 he = gethostbyname(host);
108 if (he == NULL) {
109 error_setg(errp, "can't resolve host address '%s'", host);
110 return -1;
111 }
112 saddr->sin_addr = *(struct in_addr *)he->h_addr;
113 }
114 }
115 if (qemu_strtol(port, &r, 0, &p) != 0) {
116 error_setg(errp, "port number '%s' is invalid", port);
117 return -1;
118 }
119 saddr->sin_port = htons(p);
120 return 0;
121}
122
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800123int parse_host_port(struct sockaddr_in *saddr, const char *str,
124 Error **errp)
aliguori63a01ef2008-10-31 19:10:00 +0000125{
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200126 gchar **substrings;
Laurent Vivier30e42262022-10-21 11:09:06 +0200127 int ret;
Peter Maydell59292382021-08-13 16:05:03 +0100128
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200129 substrings = g_strsplit(str, ":", 2);
130 if (!substrings || !substrings[0] || !substrings[1]) {
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800131 error_setg(errp, "host address '%s' doesn't contain ':' "
132 "separating host from port", str);
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200133 ret = -1;
134 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800135 }
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200136
Laurent Vivier30e42262022-10-21 11:09:06 +0200137 ret = convert_host_port(saddr, substrings[0], substrings[1], errp);
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200138
139out:
140 g_strfreev(substrings);
141 return ret;
aliguori63a01ef2008-10-31 19:10:00 +0000142}
143
Scott Feldman890ee6a2015-03-13 21:09:25 -0700144char *qemu_mac_strdup_printf(const uint8_t *macaddr)
145{
146 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
147 macaddr[0], macaddr[1], macaddr[2],
148 macaddr[3], macaddr[4], macaddr[5]);
149}
150
Laurent Vivier53b85d92022-10-21 11:09:10 +0200151void qemu_set_info_str(NetClientState *nc, const char *fmt, ...)
152{
153 va_list ap;
154
155 va_start(ap, fmt);
156 vsnprintf(nc->info_str, sizeof(nc->info_str), fmt, ap);
157 va_end(ap);
158}
159
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100160void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000161{
Laurent Vivier53b85d92022-10-21 11:09:10 +0200162 qemu_set_info_str(nc, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
163 nc->model, macaddr[0], macaddr[1], macaddr[2],
164 macaddr[3], macaddr[4], macaddr[5]);
aliguori7cb7434b2009-01-07 17:46:21 +0000165}
166
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800167static int mac_table[256] = {0};
168
169static void qemu_macaddr_set_used(MACAddr *macaddr)
170{
171 int index;
172
173 for (index = 0x56; index < 0xFF; index++) {
174 if (macaddr->a[5] == index) {
175 mac_table[index]++;
176 }
177 }
178}
179
180static void qemu_macaddr_set_free(MACAddr *macaddr)
181{
182 int index;
183 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
184
185 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
186 return;
187 }
188 for (index = 0x56; index < 0xFF; index++) {
189 if (macaddr->a[5] == index) {
190 mac_table[index]--;
191 }
192 }
193}
194
195static int qemu_macaddr_get_free(void)
196{
197 int index;
198
199 for (index = 0x56; index < 0xFF; index++) {
200 if (mac_table[index] == 0) {
201 return index;
202 }
203 }
204
205 return -1;
206}
207
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200208void qemu_macaddr_default_if_unset(MACAddr *macaddr)
209{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200210 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800211 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200212
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800213 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
214 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
215 return;
216 } else {
217 qemu_macaddr_set_used(macaddr);
218 return;
219 }
220 }
221
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200222 macaddr->a[0] = 0x52;
223 macaddr->a[1] = 0x54;
224 macaddr->a[2] = 0x00;
225 macaddr->a[3] = 0x12;
226 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800227 macaddr->a[5] = qemu_macaddr_get_free();
228 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200229}
230
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100231/**
232 * Generate a name for net client
233 *
Amos Kongc9635302013-04-15 18:55:19 +0800234 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100235 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100236static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000237{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100238 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000239 int id = 0;
240
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100241 QTAILQ_FOREACH(nc, &net_clients, next) {
242 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100243 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100244 }
Amos Kongc9635302013-04-15 18:55:19 +0800245 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200246 id++;
247 }
248 }
249
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100250 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000251}
252
Jason Wangf7860452013-01-30 19:12:27 +0800253static void qemu_net_client_destructor(NetClientState *nc)
254{
255 g_free(nc);
256}
Jason Wang25c01bd2018-12-04 11:53:43 +0800257static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
258 unsigned flags,
259 const struct iovec *iov,
260 int iovcnt,
261 void *opaque);
Jason Wangf7860452013-01-30 19:12:27 +0800262
Jason Wang18a15412013-01-30 19:12:26 +0800263static void qemu_net_client_setup(NetClientState *nc,
264 NetClientInfo *info,
265 NetClientState *peer,
266 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800267 const char *name,
Jason Wang2f849db2021-10-20 12:55:55 +0800268 NetClientDestructor *destructor,
269 bool is_datapath)
aliguori63a01ef2008-10-31 19:10:00 +0000270{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100271 nc->info = info;
272 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000273 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100274 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000275 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100276 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000277 }
aliguori63a01ef2008-10-31 19:10:00 +0000278
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100279 if (peer) {
280 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100281 nc->peer = peer;
282 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100283 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100284 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100285
Yang Hongyang3e033a42015-10-07 11:52:17 +0800286 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
Jason Wangf7860452013-01-30 19:12:27 +0800287 nc->destructor = destructor;
Jason Wang2f849db2021-10-20 12:55:55 +0800288 nc->is_datapath = is_datapath;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800289 QTAILQ_INIT(&nc->filters);
Jason Wang18a15412013-01-30 19:12:26 +0800290}
291
292NetClientState *qemu_new_net_client(NetClientInfo *info,
293 NetClientState *peer,
294 const char *model,
295 const char *name)
296{
297 NetClientState *nc;
298
299 assert(info->size >= sizeof(NetClientState));
300
301 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800302 qemu_net_client_setup(nc, info, peer, model, name,
Jason Wang2f849db2021-10-20 12:55:55 +0800303 qemu_net_client_destructor, true);
304
305 return nc;
306}
307
308NetClientState *qemu_new_net_control_client(NetClientInfo *info,
309 NetClientState *peer,
310 const char *model,
311 const char *name)
312{
313 NetClientState *nc;
314
315 assert(info->size >= sizeof(NetClientState));
316
317 nc = g_malloc0(info->size);
318 qemu_net_client_setup(nc, info, peer, model, name,
319 qemu_net_client_destructor, false);
Jason Wang18a15412013-01-30 19:12:26 +0800320
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100321 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000322}
323
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000324NICState *qemu_new_nic(NetClientInfo *info,
325 NICConf *conf,
326 const char *model,
327 const char *name,
Akihiko Odaki7d0fefd2023-06-01 12:18:58 +0900328 MemReentrancyGuard *reentrancy_guard,
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000329 void *opaque)
330{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800331 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000332 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200333 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000334
Eric Blakef394b2e2016-07-13 21:50:23 -0600335 assert(info->type == NET_CLIENT_DRIVER_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000336 assert(info->size >= sizeof(NICState));
337
Jason Wangf6b26cf2013-02-22 23:15:06 +0800338 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
339 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000340 nic->conf = conf;
Akihiko Odaki9050f972023-06-01 12:18:59 +0900341 nic->reentrancy_guard = reentrancy_guard,
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000342 nic->opaque = opaque;
343
Jason Wangf6b26cf2013-02-22 23:15:06 +0800344 for (i = 0; i < queues; i++) {
345 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang2f849db2021-10-20 12:55:55 +0800346 NULL, true);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800347 nic->ncs[i].queue_index = i;
348 }
349
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000350 return nic;
351}
352
Jason Wang1ceef9f2013-01-30 19:12:28 +0800353NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
354{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800355 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800356}
357
Jason Wangb356f762013-01-30 19:12:22 +0800358NetClientState *qemu_get_queue(NICState *nic)
359{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800360 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800361}
362
Jason Wangcc1f0f42013-01-30 19:12:23 +0800363NICState *qemu_get_nic(NetClientState *nc)
364{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800365 NetClientState *nc0 = nc - nc->queue_index;
366
Jason Wangf6b26cf2013-02-22 23:15:06 +0800367 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800368}
369
370void *qemu_get_nic_opaque(NetClientState *nc)
371{
372 NICState *nic = qemu_get_nic(nc);
373
374 return nic->opaque;
375}
376
Cindy Lu0165daa2020-07-01 22:55:25 +0800377NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
378{
379 assert(nc != NULL);
380 NetClientState *ncs = nc + queue_index;
381 return ncs->peer;
382}
383
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100384static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000385{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100386 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000387
Andreas Färbercc2a9042013-02-12 23:16:06 +0100388 if (nc->info->cleanup) {
389 nc->info->cleanup(nc);
390 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200391}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100392
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100393static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200394{
Jan Kiszka067404b2013-08-02 21:47:08 +0200395 if (nc->incoming_queue) {
396 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200397 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100398 if (nc->peer) {
399 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100400 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100401 g_free(nc->name);
402 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800403 if (nc->destructor) {
404 nc->destructor(nc);
405 }
aliguori63a01ef2008-10-31 19:10:00 +0000406}
407
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100408void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200409{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800410 NetClientState *ncs[MAX_QUEUE_NUM];
411 int queues, i;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800412 NetFilterState *nf, *next;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800413
Eric Blakef394b2e2016-07-13 21:50:23 -0600414 assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100415
Jason Wang1ceef9f2013-01-30 19:12:28 +0800416 /* If the NetClientState belongs to a multiqueue backend, we will change all
417 * other NetClientStates also.
418 */
419 queues = qemu_find_net_clients_except(nc->name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600420 NET_CLIENT_DRIVER_NIC,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800421 MAX_QUEUE_NUM);
422 assert(queues != 0);
423
Yang Hongyangfdccce42015-10-07 11:52:14 +0800424 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
425 object_unparent(OBJECT(nf));
426 }
427
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200428 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Eric Blakef394b2e2016-07-13 21:50:23 -0600429 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800430 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200431 if (nic->peer_deleted) {
432 return;
433 }
434 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800435
436 for (i = 0; i < queues; i++) {
437 ncs[i]->peer->link_down = true;
438 }
439
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100440 if (nc->peer->info->link_status_changed) {
441 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200442 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800443
444 for (i = 0; i < queues; i++) {
445 qemu_cleanup_net_client(ncs[i]);
446 }
447
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200448 return;
449 }
450
Jason Wang1ceef9f2013-01-30 19:12:28 +0800451 for (i = 0; i < queues; i++) {
452 qemu_cleanup_net_client(ncs[i]);
453 qemu_free_net_client(ncs[i]);
454 }
Jason Wang948ecf22013-01-30 19:12:24 +0800455}
456
457void qemu_del_nic(NICState *nic)
458{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200459 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800460
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800461 qemu_macaddr_set_free(&nic->conf->macaddr);
462
Yuri Benditovichd2abc562020-11-12 11:46:53 +0200463 for (i = 0; i < queues; i++) {
464 NetClientState *nc = qemu_get_subqueue(nic, i);
465 /* If this is a peer NIC and peer has already been deleted, free it now. */
466 if (nic->peer_deleted) {
467 qemu_free_net_client(nc->peer);
468 } else if (nc->peer) {
469 /* if there are RX packets pending, complete them */
470 qemu_purge_queued_packets(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200471 }
472 }
473
Jason Wang1ceef9f2013-01-30 19:12:28 +0800474 for (i = queues - 1; i >= 0; i--) {
475 NetClientState *nc = qemu_get_subqueue(nic, i);
476
477 qemu_cleanup_net_client(nc);
478 qemu_free_net_client(nc);
479 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800480
481 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200482}
483
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000484void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
485{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100486 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000487
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100488 QTAILQ_FOREACH(nc, &net_clients, next) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600489 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800490 if (nc->queue_index == 0) {
491 func(qemu_get_nic(nc), opaque);
492 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000493 }
494 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000495}
496
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100497bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100498{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100499 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100500 return false;
501 }
502
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100503 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100504}
505
Yuri Benditovichf03e0cf2023-08-01 01:31:46 +0300506bool qemu_has_uso(NetClientState *nc)
507{
508 if (!nc || !nc->info->has_uso) {
509 return false;
510 }
511
512 return nc->info->has_uso(nc);
513}
514
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100515bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100516{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100517 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100518 return false;
519 }
520
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100521 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100522}
523
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100524bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100525{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100526 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100527 return false;
528 }
529
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100530 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100531}
532
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100533void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Andrew Melnychenko2ab0ec32023-08-01 01:31:45 +0300534 int ecn, int ufo, int uso4, int uso6)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100535{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100536 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100537 return;
538 }
539
Andrew Melnychenko2ab0ec32023-08-01 01:31:45 +0300540 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo, uso4, uso6);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100541}
542
Akihiko Odaki481c5232023-02-23 19:20:05 +0900543int qemu_get_vnet_hdr_len(NetClientState *nc)
544{
Akihiko Odaki76240dd2024-08-17 16:00:43 +0900545 if (!nc) {
546 return 0;
547 }
548
Akihiko Odaki4b52d632024-04-28 16:00:45 +0900549 return nc->vnet_hdr_len;
Akihiko Odaki481c5232023-02-23 19:20:05 +0900550}
551
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100552void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100553{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100554 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100555 return;
556 }
557
Akihiko Odakia6775372024-04-28 16:00:46 +0900558 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
559 len == sizeof(struct virtio_net_hdr) ||
560 len == sizeof(struct virtio_net_hdr_v1_hash));
561
Zhang Chend6b732e2017-07-04 14:53:45 +0800562 nc->vnet_hdr_len = len;
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100563 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100564}
565
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200566int qemu_set_vnet_le(NetClientState *nc, bool is_le)
567{
Marc-André Lureaue03b5682022-03-23 19:57:17 +0400568#if HOST_BIG_ENDIAN
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200569 if (!nc || !nc->info->set_vnet_le) {
570 return -ENOSYS;
571 }
572
573 return nc->info->set_vnet_le(nc, is_le);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300574#else
575 return 0;
576#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200577}
578
579int qemu_set_vnet_be(NetClientState *nc, bool is_be)
580{
Marc-André Lureaue03b5682022-03-23 19:57:17 +0400581#if HOST_BIG_ENDIAN
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300582 return 0;
583#else
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200584 if (!nc || !nc->info->set_vnet_be) {
585 return -ENOSYS;
586 }
587
588 return nc->info->set_vnet_be(nc, is_be);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300589#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200590}
591
Jason Wang705df542021-02-24 11:44:36 +0800592int qemu_can_receive_packet(NetClientState *nc)
593{
594 if (nc->receive_disabled) {
595 return 0;
596 } else if (nc->info->can_receive &&
597 !nc->info->can_receive(nc)) {
598 return 0;
599 }
600 return 1;
601}
602
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100603int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000604{
zhanghailiange1d64c02014-08-26 16:06:17 +0800605 int vm_running = runstate_is_running();
606
607 if (!vm_running) {
608 return 0;
609 }
610
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100611 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100612 return 1;
613 }
614
Jason Wang705df542021-02-24 11:44:36 +0800615 return qemu_can_receive_packet(sender->peer);
aliguori63a01ef2008-10-31 19:10:00 +0000616}
617
Yang Hongyange64c7702015-10-07 11:52:15 +0800618static ssize_t filter_receive_iov(NetClientState *nc,
619 NetFilterDirection direction,
620 NetClientState *sender,
621 unsigned flags,
622 const struct iovec *iov,
623 int iovcnt,
624 NetPacketSent *sent_cb)
625{
626 ssize_t ret = 0;
627 NetFilterState *nf = NULL;
628
Li Zhijian25aaadf2016-01-26 13:00:22 +0800629 if (direction == NET_FILTER_DIRECTION_TX) {
630 QTAILQ_FOREACH(nf, &nc->filters, next) {
631 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
632 iovcnt, sent_cb);
633 if (ret) {
634 return ret;
635 }
636 }
637 } else {
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100638 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
Li Zhijian25aaadf2016-01-26 13:00:22 +0800639 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
640 iovcnt, sent_cb);
641 if (ret) {
642 return ret;
643 }
Yang Hongyange64c7702015-10-07 11:52:15 +0800644 }
645 }
646
647 return ret;
648}
649
650static ssize_t filter_receive(NetClientState *nc,
651 NetFilterDirection direction,
652 NetClientState *sender,
653 unsigned flags,
654 const uint8_t *data,
655 size_t size,
656 NetPacketSent *sent_cb)
657{
658 struct iovec iov = {
659 .iov_base = (void *)data,
660 .iov_len = size
661 };
662
663 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
664}
665
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100666void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000667{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100668 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100669 return;
670 }
671
Jan Kiszka067404b2013-08-02 21:47:08 +0200672 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100673}
674
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300675void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100676{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100677 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100678
Eric Blakef394b2e2016-07-13 21:50:23 -0600679 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
Luigi Rizzo199ee602013-02-05 17:53:31 +0100680 if (net_hub_flush(nc->peer)) {
681 qemu_notify_event();
682 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100683 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200684 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200685 /* We emptied the queue successfully, signal to the IO thread to repoll
686 * the file descriptor (for tap, for example).
687 */
688 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300689 } else if (purge) {
690 /* Unable to empty the queue, purge remaining packets */
Jason Wang5fe19fb2020-05-11 12:04:53 +0800691 qemu_net_queue_purge(nc->incoming_queue, nc->peer);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200692 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100693}
694
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300695void qemu_flush_queued_packets(NetClientState *nc)
696{
697 qemu_flush_or_purge_queued_packets(nc, false);
698}
699
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100700static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100701 unsigned flags,
702 const uint8_t *buf, int size,
703 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000704{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100705 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800706 int ret;
Mark McLoughline94667b2009-04-29 11:48:12 +0100707
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100708#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100709 printf("qemu_send_packet_async:\n");
Philippe Mathieu-Daudéb42581f2020-08-22 20:09:50 +0200710 qemu_hexdump(stdout, "net", buf, size);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100711#endif
712
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100713 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100714 return size;
715 }
716
Yang Hongyange64c7702015-10-07 11:52:15 +0800717 /* Let filters handle the packet first */
718 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
719 sender, flags, buf, size, sent_cb);
720 if (ret) {
721 return ret;
722 }
723
724 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
725 sender, flags, buf, size, sent_cb);
726 if (ret) {
727 return ret;
728 }
729
Jan Kiszka067404b2013-08-02 21:47:08 +0200730 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100731
Mark McLoughlinca77d172009-10-22 17:43:41 +0100732 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
733}
734
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100735ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100736 const uint8_t *buf, int size,
737 NetPacketSent *sent_cb)
738{
739 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
740 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100741}
742
Marc-André Lureau625a5262019-01-17 15:43:54 +0400743ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100744{
Marc-André Lureau625a5262019-01-17 15:43:54 +0400745 return qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000746}
747
Jason Wang705df542021-02-24 11:44:36 +0800748ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size)
749{
750 if (!qemu_can_receive_packet(nc)) {
751 return 0;
752 }
753
754 return qemu_net_queue_receive(nc->incoming_queue, buf, size);
755}
756
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100757ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100758{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100759 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100760 buf, size, NULL);
761}
762
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100763static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800764 int iovcnt, unsigned flags)
aliguorifbe78f42008-12-17 19:13:11 +0000765{
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530766 uint8_t *buf = NULL;
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800767 uint8_t *buffer;
Benjamin Poirierce053662011-02-23 19:57:21 -0500768 size_t offset;
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530769 ssize_t ret;
aliguorifbe78f42008-12-17 19:13:11 +0000770
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800771 if (iovcnt == 1) {
772 buffer = iov[0].iov_base;
773 offset = iov[0].iov_len;
774 } else {
Peter Lieven47f9f152016-06-30 11:49:40 +0200775 offset = iov_size(iov, iovcnt);
776 if (offset > NET_BUFSIZE) {
777 return -1;
778 }
779 buf = g_malloc(offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800780 buffer = buf;
Peter Lieven47f9f152016-06-30 11:49:40 +0200781 offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800782 }
aliguorifbe78f42008-12-17 19:13:11 +0000783
Akihiko Odakib9ad5132024-04-28 16:00:47 +0900784 ret = nc->info->receive(nc, buffer, offset);
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530785
786 g_free(buf);
787 return ret;
aliguorifbe78f42008-12-17 19:13:11 +0000788}
789
Jason Wang25c01bd2018-12-04 11:53:43 +0800790static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
791 unsigned flags,
792 const struct iovec *iov,
793 int iovcnt,
794 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100795{
Akihiko Odaki9050f972023-06-01 12:18:59 +0900796 MemReentrancyGuard *owned_reentrancy_guard;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100797 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100798 int ret;
Akihiko Odakib9ad5132024-04-28 16:00:47 +0900799 struct virtio_net_hdr_v1_hash vnet_hdr = { };
800 g_autofree struct iovec *iov_copy = NULL;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100801
Jason Wang1592a992018-05-30 13:16:36 +0800802
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100803 if (nc->link_down) {
Jason Wang25c01bd2018-12-04 11:53:43 +0800804 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100805 }
806
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100807 if (nc->receive_disabled) {
808 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100809 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100810
Akihiko Odaki9050f972023-06-01 12:18:59 +0900811 if (nc->info->type != NET_CLIENT_DRIVER_NIC ||
812 qemu_get_nic(nc)->reentrancy_guard->engaged_in_io) {
813 owned_reentrancy_guard = NULL;
814 } else {
815 owned_reentrancy_guard = qemu_get_nic(nc)->reentrancy_guard;
816 owned_reentrancy_guard->engaged_in_io = true;
817 }
818
Akihiko Odakib9ad5132024-04-28 16:00:47 +0900819 if ((flags & QEMU_NET_PACKET_FLAG_RAW) && nc->vnet_hdr_len) {
820 iov_copy = g_new(struct iovec, iovcnt + 1);
821 iov_copy[0].iov_base = &vnet_hdr;
822 iov_copy[0].iov_len = nc->vnet_hdr_len;
823 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
824 iov = iov_copy;
825 }
826
827 if (nc->info->receive_iov) {
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100828 ret = nc->info->receive_iov(nc, iov, iovcnt);
829 } else {
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800830 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100831 }
832
Akihiko Odaki9050f972023-06-01 12:18:59 +0900833 if (owned_reentrancy_guard) {
834 owned_reentrancy_guard->engaged_in_io = false;
835 }
836
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100837 if (ret == 0) {
838 nc->receive_disabled = 1;
839 }
840
841 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100842}
843
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100844ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100845 const struct iovec *iov, int iovcnt,
846 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000847{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100848 NetQueue *queue;
Jason Wang25c01bd2018-12-04 11:53:43 +0800849 size_t size = iov_size(iov, iovcnt);
Yang Hongyange64c7702015-10-07 11:52:15 +0800850 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100851
Jason Wang25c01bd2018-12-04 11:53:43 +0800852 if (size > NET_BUFSIZE) {
853 return size;
854 }
855
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100856 if (sender->link_down || !sender->peer) {
Jason Wang25c01bd2018-12-04 11:53:43 +0800857 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000858 }
859
Yang Hongyange64c7702015-10-07 11:52:15 +0800860 /* Let filters handle the packet first */
861 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
862 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
863 if (ret) {
864 return ret;
865 }
866
867 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
868 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
869 if (ret) {
870 return ret;
871 }
872
Jan Kiszka067404b2013-08-02 21:47:08 +0200873 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100874
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100875 return qemu_net_queue_send_iov(queue, sender,
876 QEMU_NET_PACKET_FLAG_NONE,
877 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000878}
879
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100880ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100881qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100882{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100883 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100884}
885
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100886NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000887{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100888 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100889
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100890 QTAILQ_FOREACH(nc, &net_clients, next) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600891 if (nc->info->type == NET_CLIENT_DRIVER_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200892 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100893 if (!strcmp(nc->name, id)) {
894 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100895 }
896 }
897
898 return NULL;
899}
900
Jason Wang6c51ae72013-01-30 19:12:25 +0800901int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600902 NetClientDriver type, int max)
Jason Wang6c51ae72013-01-30 19:12:25 +0800903{
904 NetClientState *nc;
905 int ret = 0;
906
907 QTAILQ_FOREACH(nc, &net_clients, next) {
908 if (nc->info->type == type) {
909 continue;
910 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100911 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800912 if (ret < max) {
913 ncs[ret] = nc;
914 }
915 ret++;
916 }
917 }
918
919 return ret;
920}
921
aliguori76970792009-02-11 15:20:03 +0000922static int nic_get_free_idx(void)
923{
924 int index;
925
926 for (index = 0; index < MAX_NICS; index++)
927 if (!nd_table[index].used)
928 return index;
929 return -1;
930}
931
Thomas Huthc6941b32022-11-10 13:52:22 +0100932GPtrArray *qemu_get_nic_models(const char *device_type)
933{
934 GPtrArray *nic_models = g_ptr_array_new();
935 GSList *list = object_class_get_list_sorted(device_type, false);
936
937 while (list) {
938 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
939 TYPE_DEVICE);
940 GSList *next;
941 if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
942 dc->user_creatable) {
943 const char *name = object_class_get_name(list->data);
944 /*
945 * A network device might also be something else than a NIC, see
946 * e.g. the "rocker" device. Thus we have to look for the "netdev"
947 * property, too. Unfortunately, some devices like virtio-net only
948 * create this property during instance_init, so we have to create
949 * a temporary instance here to be able to check it.
950 */
951 Object *obj = object_new_with_class(OBJECT_CLASS(dc));
952 if (object_property_find(obj, "netdev")) {
953 g_ptr_array_add(nic_models, (gpointer)name);
954 }
955 object_unref(obj);
956 }
957 next = list->next;
958 g_slist_free_1(list);
959 list = next;
960 }
961 g_ptr_array_add(nic_models, NULL);
962
963 return nic_models;
964}
965
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600966static int net_init_nic(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200967 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100968{
969 int idx;
970 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200971 const NetLegacyNicOptions *nic;
972
Eric Blakef394b2e2016-07-13 21:50:23 -0600973 assert(netdev->type == NET_CLIENT_DRIVER_NIC);
974 nic = &netdev->u.nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100975
976 idx = nic_get_free_idx();
977 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200978 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100979 return -1;
980 }
981
982 nd = &nd_table[idx];
983
984 memset(nd, 0, sizeof(*nd));
985
Markus Armbruster74808742022-11-04 17:07:00 +0100986 if (nic->netdev) {
Laszlo Ersek2456f362012-07-17 16:17:14 +0200987 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100988 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200989 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100990 return -1;
991 }
992 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100993 assert(peer);
994 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100995 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100996 nd->name = g_strdup(name);
Markus Armbruster74808742022-11-04 17:07:00 +0100997 if (nic->model) {
Laszlo Ersek2456f362012-07-17 16:17:14 +0200998 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100999 }
Markus Armbruster74808742022-11-04 17:07:00 +01001000 if (nic->addr) {
Laszlo Ersek2456f362012-07-17 16:17:14 +02001001 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001002 }
1003
Markus Armbruster74808742022-11-04 17:07:00 +01001004 if (nic->macaddr &&
Laszlo Ersek2456f362012-07-17 16:17:14 +02001005 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +02001006 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001007 return -1;
1008 }
Markus Armbruster74808742022-11-04 17:07:00 +01001009 if (nic->macaddr &&
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +04001010 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +02001011 error_setg(errp,
1012 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +04001013 return -1;
1014 }
Jan Kiszka6eed1852011-07-20 12:20:22 +02001015 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001016
Laszlo Ersek2456f362012-07-17 16:17:14 +02001017 if (nic->has_vectors) {
1018 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +02001019 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +02001020 return -1;
1021 }
1022 nd->nvectors = nic->vectors;
1023 } else {
1024 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001025 }
1026
1027 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001028 nb_nics++;
1029
1030 return idx;
1031}
1032
David Woodhouse2cdeca02023-10-21 23:09:38 +01001033static gboolean add_nic_result(gpointer key, gpointer value, gpointer user_data)
1034{
1035 GPtrArray *results = user_data;
1036 GPtrArray *alias_list = value;
1037 const char *model = key;
1038 char *result;
1039
1040 if (!alias_list) {
1041 result = g_strdup(model);
1042 } else {
1043 GString *result_str = g_string_new(model);
1044 int i;
1045
1046 g_string_append(result_str, " (aka ");
1047 for (i = 0; i < alias_list->len; i++) {
1048 if (i) {
1049 g_string_append(result_str, ", ");
1050 }
1051 g_string_append(result_str, alias_list->pdata[i]);
1052 }
1053 g_string_append(result_str, ")");
1054 result = result_str->str;
1055 g_string_free(result_str, false);
1056 g_ptr_array_unref(alias_list);
1057 }
1058 g_ptr_array_add(results, result);
1059 return true;
1060}
1061
1062static int model_cmp(char **a, char **b)
1063{
1064 return strcmp(*a, *b);
1065}
1066
1067static void show_nic_models(void)
1068{
1069 GPtrArray *results = g_ptr_array_new();
1070 int i;
1071
1072 g_hash_table_foreach_remove(nic_model_help, add_nic_result, results);
1073 g_ptr_array_sort(results, (GCompareFunc)model_cmp);
1074
1075 printf("Available NIC models for this configuration:\n");
1076 for (i = 0 ; i < results->len; i++) {
1077 printf("%s\n", (char *)results->pdata[i]);
1078 }
1079 g_hash_table_unref(nic_model_help);
1080 nic_model_help = NULL;
1081}
1082
1083static void add_nic_model_help(const char *model, const char *alias)
1084{
1085 GPtrArray *alias_list = NULL;
1086
1087 if (g_hash_table_lookup_extended(nic_model_help, model, NULL,
1088 (gpointer *)&alias_list)) {
1089 /* Already exists, no alias to add: return */
1090 if (!alias) {
1091 return;
1092 }
1093 if (alias_list) {
1094 /* Check if this alias is already in the list. Add if not. */
1095 if (!g_ptr_array_find_with_equal_func(alias_list, alias,
1096 g_str_equal, NULL)) {
1097 g_ptr_array_add(alias_list, g_strdup(alias));
1098 }
1099 return;
1100 }
1101 }
1102 /* Either this model wasn't in the list already, or a first alias added */
1103 if (alias) {
1104 alias_list = g_ptr_array_new();
1105 g_ptr_array_set_free_func(alias_list, g_free);
1106 g_ptr_array_add(alias_list, g_strdup(alias));
1107 }
1108 g_hash_table_replace(nic_model_help, g_strdup(model), alias_list);
1109}
1110
David Woodhouse93e9d732023-10-19 21:28:29 +01001111NICInfo *qemu_find_nic_info(const char *typename, bool match_default,
1112 const char *alias)
1113{
1114 NICInfo *nd;
1115 int i;
1116
David Woodhouse2cdeca02023-10-21 23:09:38 +01001117 if (nic_model_help) {
1118 add_nic_model_help(typename, alias);
1119 }
1120
David Woodhouse93e9d732023-10-19 21:28:29 +01001121 for (i = 0; i < nb_nics; i++) {
1122 nd = &nd_table[i];
1123
1124 if (!nd->used || nd->instantiated) {
1125 continue;
1126 }
1127
1128 if ((match_default && !nd->model) || !g_strcmp0(nd->model, typename)
1129 || (alias && !g_strcmp0(nd->model, alias))) {
1130 return nd;
1131 }
1132 }
1133 return NULL;
1134}
1135
David Woodhouse64f75f52024-07-09 13:34:44 +01001136static bool is_nic_model_help_option(const char *model)
1137{
1138 if (model && is_help_option(model)) {
1139 /*
1140 * Trigger the help output by instantiating the hash table which
1141 * will gather tha available models as they get registered.
1142 */
1143 if (!nic_model_help) {
1144 nic_model_help = g_hash_table_new_full(g_str_hash, g_str_equal,
1145 g_free, NULL);
1146 }
1147 return true;
1148 }
1149 return false;
1150}
David Woodhouse93e9d732023-10-19 21:28:29 +01001151
1152/* "I have created a device. Please configure it if you can" */
1153bool qemu_configure_nic_device(DeviceState *dev, bool match_default,
1154 const char *alias)
1155{
1156 NICInfo *nd = qemu_find_nic_info(object_get_typename(OBJECT(dev)),
1157 match_default, alias);
1158
1159 if (nd) {
1160 qdev_set_nic_properties(dev, nd);
1161 return true;
1162 }
1163 return false;
1164}
1165
1166/* "Please create a device, if you have a configuration for it" */
1167DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
1168 const char *alias)
1169{
1170 NICInfo *nd = qemu_find_nic_info(typename, match_default, alias);
1171 DeviceState *dev;
1172
1173 if (!nd) {
1174 return NULL;
1175 }
1176
1177 dev = qdev_new(typename);
1178 qdev_set_nic_properties(dev, nd);
1179 return dev;
1180}
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001181
David Woodhouse93125e42023-10-22 09:13:41 +01001182void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type,
1183 const char *default_model,
1184 const char *alias, const char *alias_target)
1185{
1186 GPtrArray *nic_models = qemu_get_nic_models(parent_type);
1187 const char *model;
1188 DeviceState *dev;
1189 NICInfo *nd;
1190 int i;
1191
1192 if (nic_model_help) {
1193 if (alias_target) {
1194 add_nic_model_help(alias_target, alias);
1195 }
1196 for (i = 0; i < nic_models->len - 1; i++) {
1197 add_nic_model_help(nic_models->pdata[i], NULL);
1198 }
1199 }
1200
1201 /* Drop the NULL terminator which would make g_str_equal() unhappy */
1202 nic_models->len--;
1203
1204 for (i = 0; i < nb_nics; i++) {
1205 nd = &nd_table[i];
1206
1207 if (!nd->used || nd->instantiated) {
1208 continue;
1209 }
1210
1211 model = nd->model ? nd->model : default_model;
1212 if (!model) {
1213 continue;
1214 }
1215
1216 /* Each bus type is allowed *one* substitution */
1217 if (g_str_equal(model, alias)) {
1218 model = alias_target;
1219 }
1220
1221 if (!g_ptr_array_find_with_equal_func(nic_models, model,
1222 g_str_equal, NULL)) {
1223 /* This NIC does not live on this bus. */
1224 continue;
1225 }
1226
1227 dev = qdev_new(model);
1228 qdev_set_nic_properties(dev, nd);
1229 qdev_realize_and_unref(dev, bus, &error_fatal);
1230 }
1231
1232 g_ptr_array_free(nic_models, true);
1233}
1234
Eric Blakef394b2e2016-07-13 21:50:23 -06001235static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
Kővágó, Zoltáncebea512016-07-13 21:50:12 -06001236 const Netdev *netdev,
Laszlo Ersek6687b792012-07-17 16:17:13 +02001237 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +02001238 NetClientState *peer, Error **errp) = {
Eric Blakef394b2e2016-07-13 21:50:23 -06001239 [NET_CLIENT_DRIVER_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +01001240#ifdef CONFIG_SLIRP
Eric Blakef394b2e2016-07-13 21:50:23 -06001241 [NET_CLIENT_DRIVER_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +01001242#endif
Eric Blakef394b2e2016-07-13 21:50:23 -06001243 [NET_CLIENT_DRIVER_TAP] = net_init_tap,
1244 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
Laurent Vivier5166fe02022-10-21 11:09:11 +02001245 [NET_CLIENT_DRIVER_STREAM] = net_init_stream,
1246 [NET_CLIENT_DRIVER_DGRAM] = net_init_dgram,
Mark McLoughlindd510582009-10-06 12:17:10 +01001247#ifdef CONFIG_VDE
Eric Blakef394b2e2016-07-13 21:50:23 -06001248 [NET_CLIENT_DRIVER_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +01001249#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +01001250#ifdef CONFIG_NETMAP
Eric Blakef394b2e2016-07-13 21:50:23 -06001251 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
Vincenzo Maffione58952132013-11-06 11:44:06 +01001252#endif
Ilya Maximetscb039ef2023-09-13 20:34:37 +02001253#ifdef CONFIG_AF_XDP
1254 [NET_CLIENT_DRIVER_AF_XDP] = net_init_af_xdp,
1255#endif
Stefan Weil2944e4e2012-02-04 09:24:46 +01001256#ifdef CONFIG_NET_BRIDGE
Eric Blakef394b2e2016-07-13 21:50:23 -06001257 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +02001258#endif
Eric Blakef394b2e2016-07-13 21:50:23 -06001259 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
Paolo Bonzini56f41de2019-02-14 18:35:49 +01001260#ifdef CONFIG_VHOST_NET_USER
Eric Blakef394b2e2016-07-13 21:50:23 -06001261 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +03001262#endif
Cindy Lu1e0a84e2020-07-01 22:55:38 +08001263#ifdef CONFIG_VHOST_NET_VDPA
1264 [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa,
1265#endif
Gonglei015a33b2014-07-01 20:58:08 +08001266#ifdef CONFIG_L2TPV3
Eric Blakef394b2e2016-07-13 21:50:23 -06001267 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
Anton Ivanov3fb69aa2014-06-20 10:34:41 +01001268#endif
Vladislav Yaroshchuk81ad2962022-03-17 20:28:34 +03001269#ifdef CONFIG_VMNET
1270 [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host,
1271 [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared,
1272 [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged,
1273#endif /* CONFIG_VMNET */
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001274};
1275
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001276
Thomas Huth71830d82020-05-18 20:01:03 +02001277static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +02001278{
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001279 NetClientState *peer = NULL;
Markus Armbruster831734c2020-11-25 11:02:20 +01001280 NetClientState *nc;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001281
Markus Armbruster5294e2c2010-03-25 17:22:38 +01001282 if (is_netdev) {
Thomas Huth857d2082018-02-21 11:18:34 +01001283 if (netdev->type == NET_CLIENT_DRIVER_NIC ||
Eric Blakef394b2e2016-07-13 21:50:23 -06001284 !net_client_init_fun[netdev->type]) {
Daniel P. Berrangé7d0e12a2022-10-03 11:06:12 +01001285 error_setg(errp, "network backend '%s' is not compiled into this binary",
1286 NetClientDriver_str(netdev->type));
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001287 return -1;
1288 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001289 } else {
Thomas Huth71830d82020-05-18 20:01:03 +02001290 if (netdev->type == NET_CLIENT_DRIVER_NONE) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +01001291 return 0; /* nothing to do */
Markus Armbrusterca7eb182015-05-15 13:58:49 +02001292 }
Daniel P. Berrangé7d0e12a2022-10-03 11:06:12 +01001293 if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) {
1294 error_setg(errp, "network backend '%s' is only supported with -netdev/-nic",
1295 NetClientDriver_str(netdev->type));
1296 return -1;
1297 }
1298
1299 if (!net_client_init_fun[netdev->type]) {
1300 error_setg(errp, "network backend '%s' is not compiled into this binary",
1301 NetClientDriver_str(netdev->type));
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +01001302 return -1;
1303 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001304
Thomas Huthaf1a5c32018-04-30 20:02:23 +02001305 /* Do not add to a hub if it's a nic with a netdev= parameter. */
Eric Blakef394b2e2016-07-13 21:50:23 -06001306 if (netdev->type != NET_CLIENT_DRIVER_NIC ||
Markus Armbruster74808742022-11-04 17:07:00 +01001307 !netdev->u.nic.netdev) {
Thomas Huthaf1a5c32018-04-30 20:02:23 +02001308 peer = net_hub_add_port(0, NULL, NULL);
Thomas Hutha2dbe132017-01-24 10:42:49 +01001309 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001310 }
Laszlo Ersek6687b792012-07-17 16:17:13 +02001311
Markus Armbruster831734c2020-11-25 11:02:20 +01001312 nc = qemu_find_netdev(netdev->id);
1313 if (nc) {
1314 error_setg(errp, "Duplicate ID '%s'", netdev->id);
1315 return -1;
1316 }
1317
Thomas Huth9d903f32020-05-18 20:01:02 +02001318 if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) {
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001319 /* FIXME drop when all init functions store an Error */
1320 if (errp && !*errp) {
Markus Armbrusterf820af82020-11-13 09:26:18 +01001321 error_setg(errp, "Device '%s' could not be initialized",
Markus Armbruster977c7362017-08-24 10:46:08 +02001322 NetClientDriver_str(netdev->type));
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001323 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001324 return -1;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001325 }
Eric Blake08712fc2020-03-17 15:17:11 -05001326
1327 if (is_netdev) {
Eric Blake08712fc2020-03-17 15:17:11 -05001328 nc = qemu_find_netdev(netdev->id);
1329 assert(nc);
1330 nc->is_netdev = true;
1331 }
1332
Laszlo Ersek6687b792012-07-17 16:17:13 +02001333 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001334}
1335
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001336void show_netdevs(void)
Thomas Huth547203e2018-02-21 11:18:31 +01001337{
1338 int idx;
1339 const char *available_netdevs[] = {
1340 "socket",
Laurent Vivier5166fe02022-10-21 11:09:11 +02001341 "stream",
1342 "dgram",
Thomas Huth547203e2018-02-21 11:18:31 +01001343 "hubport",
1344 "tap",
1345#ifdef CONFIG_SLIRP
1346 "user",
1347#endif
1348#ifdef CONFIG_L2TPV3
1349 "l2tpv3",
1350#endif
1351#ifdef CONFIG_VDE
1352 "vde",
1353#endif
1354#ifdef CONFIG_NET_BRIDGE
1355 "bridge",
1356#endif
1357#ifdef CONFIG_NETMAP
1358 "netmap",
1359#endif
Ilya Maximetscb039ef2023-09-13 20:34:37 +02001360#ifdef CONFIG_AF_XDP
1361 "af-xdp",
1362#endif
Thomas Huth547203e2018-02-21 11:18:31 +01001363#ifdef CONFIG_POSIX
1364 "vhost-user",
1365#endif
Cindy Lu1bc211a2020-10-16 11:09:09 +08001366#ifdef CONFIG_VHOST_VDPA
1367 "vhost-vdpa",
1368#endif
Vladislav Yaroshchuk81ad2962022-03-17 20:28:34 +03001369#ifdef CONFIG_VMNET
1370 "vmnet-host",
1371 "vmnet-shared",
1372 "vmnet-bridged",
1373#endif
Thomas Huth547203e2018-02-21 11:18:31 +01001374 };
Laszlo Ersek6687b792012-07-17 16:17:13 +02001375
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001376 qemu_printf("Available netdev backend types:\n");
Thomas Huth547203e2018-02-21 11:18:31 +01001377 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001378 qemu_printf("%s\n", available_netdevs[idx]);
Thomas Huth547203e2018-02-21 11:18:31 +01001379 }
1380}
Laszlo Ersek6687b792012-07-17 16:17:13 +02001381
Thomas Huthaa09a482018-02-21 11:18:33 +01001382static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +02001383{
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001384 gchar **substrings = NULL;
Thomas Huth71830d82020-05-18 20:01:03 +02001385 Netdev *object = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +02001386 int ret = -1;
Eric Blake09204ea2016-06-09 10:48:36 -06001387 Visitor *v = opts_visitor_new(opts);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001388
Markus Armbruster0a4a1512023-10-31 12:10:57 +01001389 /* Parse convenience option format ipv6-net=fec0::0[/64] */
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001390 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
Lin Ma8b43f962018-06-11 17:23:05 +08001391
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001392 if (ip6_net) {
1393 char *prefix_addr;
1394 unsigned long prefix_len = 64; /* Default 64bit prefix length. */
Yann Bordenave7aac5312016-03-15 10:31:22 +01001395
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001396 substrings = g_strsplit(ip6_net, "/", 2);
1397 if (!substrings || !substrings[0]) {
1398 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net",
1399 "a valid IPv6 prefix");
1400 goto out;
Yann Bordenave7aac5312016-03-15 10:31:22 +01001401 }
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001402
1403 prefix_addr = substrings[0];
1404
1405 /* Handle user-specified prefix length. */
1406 if (substrings[1] &&
1407 qemu_strtoul(substrings[1], NULL, 10, &prefix_len))
1408 {
Markus Armbruster0a4a1512023-10-31 12:10:57 +01001409 error_setg(errp,
1410 "parameter 'ipv6-net' expects a number after '/'");
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001411 goto out;
1412 }
1413
1414 qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort);
1415 qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len,
1416 &error_abort);
1417 qemu_opt_unset(opts, "ipv6-net");
Yann Bordenave7aac5312016-03-15 10:31:22 +01001418 }
1419
Thomas Huth71830d82020-05-18 20:01:03 +02001420 /* Create an ID for -net if the user did not specify one */
1421 if (!is_netdev && !qemu_opts_id(opts)) {
Thomas Huth27eb3722021-02-15 10:02:25 +01001422 qemu_opts_set_id(opts, id_generate(ID_NET));
Laszlo Ersek6687b792012-07-17 16:17:13 +02001423 }
1424
Markus Armbruster14217032020-07-07 18:05:47 +02001425 if (visit_type_Netdev(v, NULL, &object, errp)) {
1426 ret = net_client_init1(object, is_netdev, errp);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001427 }
1428
Thomas Huth71830d82020-05-18 20:01:03 +02001429 qapi_free_Netdev(object);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001430
Stefano Garzarella21c520d2019-05-17 15:47:45 +02001431out:
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001432 g_strfreev(substrings);
Eric Blake09204ea2016-06-09 10:48:36 -06001433 visit_free(v);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001434 return ret;
1435}
1436
Luiz Capitulino928059a2012-04-18 17:34:15 -03001437void netdev_add(QemuOpts *opts, Error **errp)
1438{
Eric Blake0e55c382016-07-13 21:50:24 -06001439 net_client_init(opts, true, errp);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001440}
1441
Eric Blakedb2a3802020-03-17 15:17:10 -05001442void qmp_netdev_add(Netdev *netdev, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001443{
Paolo Bonzinie73b4312021-03-12 09:51:38 -05001444 if (!id_wellformed(netdev->id)) {
1445 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
1446 return;
1447 }
1448
Eric Blake08712fc2020-03-17 15:17:11 -05001449 net_client_init1(netdev, true, errp);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001450}
1451
Luiz Capitulino5f964152012-04-16 14:36:32 -03001452void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001453{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001454 NetClientState *nc;
Markus Armbruster831734c2020-11-25 11:02:20 +01001455 QemuOpts *opts;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001456
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001457 nc = qemu_find_netdev(id);
1458 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001459 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1460 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001461 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001462 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001463
Eric Blake08712fc2020-03-17 15:17:11 -05001464 if (!nc->is_netdev) {
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001465 error_setg(errp, "Device '%s' is not a netdev", id);
1466 return;
1467 }
1468
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001469 qemu_del_net_client(nc);
Markus Armbruster831734c2020-11-25 11:02:20 +01001470
1471 /*
1472 * Wart: we need to delete the QemuOpts associated with netdevs
1473 * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
1474 * HMP netdev_add.
1475 */
1476 opts = qemu_opts_find(qemu_find_opts("netdev"), id);
1477 if (opts) {
1478 qemu_opts_del(opts);
1479 }
Markus Armbrusterae82d322010-03-25 17:22:40 +01001480}
1481
zhanghailiangaa9156f2016-01-26 14:43:33 +08001482static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1483{
1484 char *str;
1485 ObjectProperty *prop;
1486 ObjectPropertyIterator iter;
Eric Blake3b098d52016-06-09 10:48:43 -06001487 Visitor *v;
zhanghailiangaa9156f2016-01-26 14:43:33 +08001488
1489 /* generate info str */
1490 object_property_iter_init(&iter, OBJECT(nf));
1491 while ((prop = object_property_iter_next(&iter))) {
1492 if (!strcmp(prop->name, "type")) {
1493 continue;
1494 }
Eric Blake3b098d52016-06-09 10:48:43 -06001495 v = string_output_visitor_new(false, &str);
Markus Armbruster5325cc32020-07-07 18:05:54 +02001496 object_property_get(OBJECT(nf), prop->name, v, NULL);
Eric Blake3b098d52016-06-09 10:48:43 -06001497 visit_complete(v, &str);
1498 visit_free(v);
zhanghailiangaa9156f2016-01-26 14:43:33 +08001499 monitor_printf(mon, ",%s=%s", prop->name, str);
1500 g_free(str);
1501 }
1502 monitor_printf(mon, "\n");
1503}
1504
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001505void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001506{
Yang Hongyanga4960f52015-10-07 11:52:19 +08001507 NetFilterState *nf;
1508
Jason Wang1ceef9f2013-01-30 19:12:28 +08001509 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1510 nc->queue_index,
Markus Armbruster977c7362017-08-24 10:46:08 +02001511 NetClientDriver_str(nc->info->type),
Jason Wang56e6f592021-04-02 11:03:33 +08001512 nc->info_str);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001513 if (!QTAILQ_EMPTY(&nc->filters)) {
1514 monitor_printf(mon, "filters:\n");
1515 }
1516 QTAILQ_FOREACH(nf, &nc->filters, next) {
Markus Armbruster7a309cc2020-07-14 18:02:00 +02001517 monitor_printf(mon, " - %s: type=%s",
1518 object_get_canonical_path_component(OBJECT(nf)),
zhanghailiangaa9156f2016-01-26 14:43:33 +08001519 object_get_typename(OBJECT(nf)));
1520 netfilter_print_info(mon, nf);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001521 }
Jan Kiszka44e798d2011-07-20 12:20:21 +02001522}
1523
Markus Armbruster74808742022-11-04 17:07:00 +01001524RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp)
Amos Kongb1be4282013-06-14 15:45:52 +08001525{
1526 NetClientState *nc;
Eric Blake95b3a8c2021-01-13 16:10:13 -06001527 RxFilterInfoList *filter_list = NULL, **tail = &filter_list;
Amos Kongb1be4282013-06-14 15:45:52 +08001528
1529 QTAILQ_FOREACH(nc, &net_clients, next) {
Amos Kongb1be4282013-06-14 15:45:52 +08001530 RxFilterInfo *info;
1531
Markus Armbruster74808742022-11-04 17:07:00 +01001532 if (name && strcmp(nc->name, name) != 0) {
Amos Kongb1be4282013-06-14 15:45:52 +08001533 continue;
1534 }
1535
1536 /* only query rx-filter information of NIC */
Eric Blakef394b2e2016-07-13 21:50:23 -06001537 if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
Markus Armbruster74808742022-11-04 17:07:00 +01001538 if (name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001539 error_setg(errp, "net client(%s) isn't a NIC", name);
Eric Blakee9d635e2021-01-13 16:10:09 -06001540 assert(!filter_list);
Markus Armbruster9083da12014-04-24 15:44:18 +02001541 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001542 }
1543 continue;
1544 }
1545
Vladislav Yasevich5320c2c2015-10-19 09:04:38 -04001546 /* only query information on queue 0 since the info is per nic,
1547 * not per queue
1548 */
1549 if (nc->queue_index != 0)
1550 continue;
1551
Amos Kongb1be4282013-06-14 15:45:52 +08001552 if (nc->info->query_rx_filter) {
1553 info = nc->info->query_rx_filter(nc);
Eric Blake95b3a8c2021-01-13 16:10:13 -06001554 QAPI_LIST_APPEND(tail, info);
Markus Armbruster74808742022-11-04 17:07:00 +01001555 } else if (name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001556 error_setg(errp, "net client(%s) doesn't support"
1557 " rx-filter querying", name);
Eric Blakee9d635e2021-01-13 16:10:09 -06001558 assert(!filter_list);
Markus Armbruster9083da12014-04-24 15:44:18 +02001559 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001560 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001561
Markus Armbruster74808742022-11-04 17:07:00 +01001562 if (name) {
Markus Armbruster638fb142014-04-24 15:44:17 +02001563 break;
1564 }
Amos Kongb1be4282013-06-14 15:45:52 +08001565 }
1566
Markus Armbruster74808742022-11-04 17:07:00 +01001567 if (filter_list == NULL && name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001568 error_setg(errp, "invalid net client name: %s", name);
1569 }
1570
1571 return filter_list;
1572}
1573
Zhang Chen5fbba3d2018-09-03 12:38:56 +08001574void colo_notify_filters_event(int event, Error **errp)
1575{
1576 NetClientState *nc;
1577 NetFilterState *nf;
1578 NetFilterClass *nfc = NULL;
1579 Error *local_err = NULL;
1580
1581 QTAILQ_FOREACH(nc, &net_clients, next) {
1582 QTAILQ_FOREACH(nf, &nc->filters, next) {
1583 nfc = NETFILTER_GET_CLASS(OBJECT(nf));
1584 nfc->handle_event(nf, event, &local_err);
1585 if (local_err) {
1586 error_propagate(errp, local_err);
1587 return;
1588 }
1589 }
1590 }
1591}
1592
Luiz Capitulino4b371562011-11-23 13:11:55 -02001593void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001594{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001595 NetClientState *ncs[MAX_QUEUE_NUM];
1596 NetClientState *nc;
1597 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001598
Jason Wang1ceef9f2013-01-30 19:12:28 +08001599 queues = qemu_find_net_clients_except(name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06001600 NET_CLIENT_DRIVER__MAX,
Jason Wang1ceef9f2013-01-30 19:12:28 +08001601 MAX_QUEUE_NUM);
1602
1603 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001604 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1605 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001606 return;
aliguori436e5e52009-01-08 19:44:06 +00001607 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001608 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001609
Jason Wang1ceef9f2013-01-30 19:12:28 +08001610 for (i = 0; i < queues; i++) {
1611 ncs[i]->link_down = !up;
1612 }
aliguori436e5e52009-01-08 19:44:06 +00001613
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001614 if (nc->info->link_status_changed) {
1615 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001616 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001617
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001618 if (nc->peer) {
1619 /* Change peer link only if the peer is NIC and then notify peer.
1620 * If the peer is a HUBPORT or a backend, we do not change the
1621 * link status.
1622 *
Thomas Huthaf1a5c32018-04-30 20:02:23 +02001623 * This behavior is compatible with qemu hubs where there could be
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001624 * multiple clients that can still communicate with each other in
1625 * disconnected mode. For now maintain this compatibility.
1626 */
Eric Blakef394b2e2016-07-13 21:50:23 -06001627 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001628 for (i = 0; i < queues; i++) {
1629 ncs[i]->peer->link_down = !up;
1630 }
1631 }
1632 if (nc->peer->info->link_status_changed) {
1633 nc->peer->info->link_status_changed(nc->peer);
1634 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001635 }
aliguori436e5e52009-01-08 19:44:06 +00001636}
1637
Philippe Mathieu-Daudé538f0492021-01-11 16:20:20 +01001638static void net_vm_change_state_handler(void *opaque, bool running,
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001639 RunState state)
1640{
Fam Zheng625de442015-07-07 09:21:07 +08001641 NetClientState *nc;
1642 NetClientState *tmp;
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001643
Fam Zheng625de442015-07-07 09:21:07 +08001644 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1645 if (running) {
1646 /* Flush queued packets and wake up backends. */
1647 if (nc->peer && qemu_can_send_packet(nc)) {
1648 qemu_flush_queued_packets(nc->peer);
1649 }
1650 } else {
1651 /* Complete all queued packets, to guarantee we don't modify
1652 * state later when VM is not running.
1653 */
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001654 qemu_flush_or_purge_queued_packets(nc, true);
1655 }
1656 }
1657}
1658
aliguori63a01ef2008-10-31 19:10:00 +00001659void net_cleanup(void)
1660{
David Woodhouse84f85eb2023-11-15 17:24:35 +00001661 NetClientState *nc, **p = &QTAILQ_FIRST(&net_clients);
aliguori63a01ef2008-10-31 19:10:00 +00001662
Rao, Lei0c7af1a2021-06-08 16:23:30 +08001663 /*cleanup colo compare module for COLO*/
1664 colo_compare_cleanup();
1665
David Woodhouse84f85eb2023-11-15 17:24:35 +00001666 /*
1667 * Walk the net_clients list and remove the netdevs but *not* any
1668 * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
1669 * model which created them, and in some cases (e.g. xen-net-device)
1670 * the device itself may do cleanup at exit and will be upset if we
1671 * just delete its NIC from underneath it.
1672 *
1673 * Since qemu_del_net_client() may delete multiple entries, using
1674 * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
1675 * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
1676 * 'p' pointing to either the head of the list, or the 'next' field
1677 * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
1678 * the list.
1679 *
1680 * The 'nc' variable isn't part of the list traversal; it's purely
1681 * for convenience as too much '(*p)->' has a tendency to make the
1682 * readers' eyes bleed.
Jason Wang1ceef9f2013-01-30 19:12:28 +08001683 */
David Woodhouse84f85eb2023-11-15 17:24:35 +00001684 while (*p) {
1685 nc = *p;
Eric Blakef394b2e2016-07-13 21:50:23 -06001686 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
David Woodhouse84f85eb2023-11-15 17:24:35 +00001687 /* Skip NET_CLIENT_DRIVER_NIC entries */
1688 p = &QTAILQ_NEXT(nc, next);
Jason Wang948ecf22013-01-30 19:12:24 +08001689 } else {
1690 qemu_del_net_client(nc);
1691 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001692 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001693
1694 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001695}
1696
Markus Armbruster668680f2010-02-11 14:44:58 +01001697void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001698{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001699 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001700 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001701
David Woodhouse2cdeca02023-10-21 23:09:38 +01001702 if (nic_model_help) {
1703 show_nic_models();
1704 exit(0);
1705 }
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001706 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001707
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001708 QTAILQ_FOREACH(nc, &net_clients, next) {
1709 if (!nc->peer) {
Alistair Francis8297be82017-09-11 12:52:53 -07001710 warn_report("%s %s has no peer",
Alistair Francisb62e39b2017-09-11 12:52:56 -07001711 nc->info->type == NET_CLIENT_DRIVER_NIC
1712 ? "nic" : "netdev",
1713 nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001714 }
1715 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001716
1717 /* Check that all NICs requested via -net nic actually got created.
1718 * NICs created via -device don't need to be checked here because
1719 * they are always instantiated.
1720 */
1721 for (i = 0; i < MAX_NICS; i++) {
1722 NICInfo *nd = &nd_table[i];
1723 if (nd->used && !nd->instantiated) {
Alistair Francis8297be82017-09-11 12:52:53 -07001724 warn_report("requested NIC (%s, model %s) "
1725 "was not created (not supported by this machine?)",
1726 nd->name ? nd->name : "anonymous",
1727 nd->model ? nd->model : "unspecified");
Peter Maydell48e2faf2011-05-20 16:50:01 +01001728 }
1729 }
aliguori63a01ef2008-10-31 19:10:00 +00001730}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001731
Markus Armbruster28d0de72015-03-13 13:35:14 +01001732static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001733{
David Woodhousefa62cb982024-08-06 18:21:37 +01001734 const char *model = qemu_opt_get(opts, "model");
David Woodhouse64f75f52024-07-09 13:34:44 +01001735
1736 if (is_nic_model_help_option(model)) {
1737 return 0;
1738 }
1739
Thomas Huth34f708b2018-02-21 11:18:30 +01001740 return net_client_init(opts, false, errp);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001741}
1742
Markus Armbruster28d0de72015-03-13 13:35:14 +01001743static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001744{
Paolo Bonziniad6f9322020-11-11 05:52:22 -05001745 const char *type = qemu_opt_get(opts, "type");
1746
1747 if (type && is_help_option(type)) {
1748 show_netdevs();
1749 exit(0);
1750 }
Thomas Huth34f708b2018-02-21 11:18:30 +01001751 return net_client_init(opts, true, errp);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001752}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001753
Thomas Huth78cd6f72018-02-21 11:18:36 +01001754/* For the convenience "--nic" parameter */
1755static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
1756{
1757 char *mac, *nd_id;
1758 int idx, ret;
1759 NICInfo *ni;
1760 const char *type;
1761
1762 type = qemu_opt_get(opts, "type");
Thomas Huth27c81922022-11-10 13:52:23 +01001763 if (type) {
1764 if (g_str_equal(type, "none")) {
1765 return 0; /* Nothing to do, default_net is cleared in vl.c */
1766 }
1767 if (is_help_option(type)) {
1768 GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
David Woodhouse481434f2023-10-23 09:38:00 +01001769 int i;
Thomas Huth27c81922022-11-10 13:52:23 +01001770 show_netdevs();
1771 printf("\n");
David Woodhouse481434f2023-10-23 09:38:00 +01001772 printf("Available NIC models "
1773 "(use -nic model=help for a filtered list):\n");
1774 for (i = 0 ; nic_models->pdata[i]; i++) {
1775 printf("%s\n", (char *)nic_models->pdata[i]);
1776 }
Thomas Huth27c81922022-11-10 13:52:23 +01001777 g_ptr_array_free(nic_models, true);
1778 exit(0);
1779 }
Thomas Huth78cd6f72018-02-21 11:18:36 +01001780 }
1781
1782 idx = nic_get_free_idx();
1783 if (idx == -1 || nb_nics >= MAX_NICS) {
1784 error_setg(errp, "no more on-board/default NIC slots available");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001785 return -1;
1786 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001787
Thomas Huth78cd6f72018-02-21 11:18:36 +01001788 if (!type) {
1789 qemu_opt_set(opts, "type", "user", &error_abort);
1790 }
1791
1792 ni = &nd_table[idx];
1793 memset(ni, 0, sizeof(*ni));
1794 ni->model = qemu_opt_get_del(opts, "model");
1795
David Woodhouse64f75f52024-07-09 13:34:44 +01001796 if (is_nic_model_help_option(ni->model)) {
David Woodhouse2cdeca02023-10-21 23:09:38 +01001797 return 0;
1798 }
1799
Thomas Huth78cd6f72018-02-21 11:18:36 +01001800 /* Create an ID if the user did not specify one */
1801 nd_id = g_strdup(qemu_opts_id(opts));
1802 if (!nd_id) {
Thomas Huth27eb3722021-02-15 10:02:25 +01001803 nd_id = id_generate(ID_NET);
Thomas Huth78cd6f72018-02-21 11:18:36 +01001804 qemu_opts_set_id(opts, nd_id);
1805 }
1806
1807 /* Handle MAC address */
1808 mac = qemu_opt_get_del(opts, "mac");
1809 if (mac) {
1810 ret = net_parse_macaddr(ni->macaddr.a, mac);
1811 g_free(mac);
1812 if (ret) {
1813 error_setg(errp, "invalid syntax for ethernet address");
Thomas Huth9d946192018-04-30 09:26:45 +02001814 goto out;
Thomas Huth78cd6f72018-02-21 11:18:36 +01001815 }
1816 if (is_multicast_ether_addr(ni->macaddr.a)) {
1817 error_setg(errp, "NIC cannot have multicast MAC address");
Thomas Huth9d946192018-04-30 09:26:45 +02001818 ret = -1;
1819 goto out;
Thomas Huth78cd6f72018-02-21 11:18:36 +01001820 }
1821 }
1822 qemu_macaddr_default_if_unset(&ni->macaddr);
1823
1824 ret = net_client_init(opts, true, errp);
1825 if (ret == 0) {
1826 ni->netdev = qemu_find_netdev(nd_id);
1827 ni->used = true;
1828 nb_nics++;
1829 }
1830
Thomas Huth9d946192018-04-30 09:26:45 +02001831out:
Thomas Huth78cd6f72018-02-21 11:18:36 +01001832 g_free(nd_id);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001833 return ret;
1834}
1835
Laurent Vivierf3eedcd2022-10-21 11:09:09 +02001836static void netdev_init_modern(void)
1837{
1838 while (!QSIMPLEQ_EMPTY(&nd_queue)) {
1839 NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
1840
1841 QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
1842 loc_push_restore(&nd->loc);
1843 net_client_init1(nd->nd, true, &error_fatal);
1844 loc_pop(&nd->loc);
1845 qapi_free_Netdev(nd->nd);
1846 g_free(nd);
1847 }
1848}
1849
Laurent Vivierd63ef172022-10-21 11:09:07 +02001850void net_init_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001851{
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001852 net_change_state_entry =
1853 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1854
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001855 QTAILQ_INIT(&net_clients);
aliguori63a01ef2008-10-31 19:10:00 +00001856
Laurent Vivierf3eedcd2022-10-21 11:09:09 +02001857 netdev_init_modern();
1858
Laurent Vivierd63ef172022-10-21 11:09:07 +02001859 qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL,
1860 &error_fatal);
aliguori63a01ef2008-10-31 19:10:00 +00001861
Laurent Vivierd63ef172022-10-21 11:09:07 +02001862 qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL,
1863 &error_fatal);
Thomas Huth78cd6f72018-02-21 11:18:36 +01001864
Laurent Vivierd63ef172022-10-21 11:09:07 +02001865 qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL,
1866 &error_fatal);
aliguori63a01ef2008-10-31 19:10:00 +00001867}
1868
Laurent Vivierf3eedcd2022-10-21 11:09:09 +02001869/*
1870 * Does this -netdev argument use modern rather than traditional syntax?
1871 * Modern syntax is to be parsed with netdev_parse_modern().
1872 * Traditional syntax is to be parsed with net_client_parse().
1873 */
Philippe Mathieu-Daudé73071f12023-10-04 14:00:06 +02001874bool netdev_is_modern(const char *optstr)
Laurent Vivierf3eedcd2022-10-21 11:09:09 +02001875{
Laurent Vivier5166fe02022-10-21 11:09:11 +02001876 QemuOpts *opts;
1877 bool is_modern;
1878 const char *type;
1879 static QemuOptsList dummy_opts = {
1880 .name = "netdev",
1881 .implied_opt_name = "type",
1882 .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
1883 .desc = { { } },
1884 };
1885
Philippe Mathieu-Daudé73071f12023-10-04 14:00:06 +02001886 if (optstr[0] == '{') {
Laurent Vivier5166fe02022-10-21 11:09:11 +02001887 /* This is JSON, which means it's modern syntax */
1888 return true;
1889 }
1890
1891 opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort);
Philippe Mathieu-Daudé73071f12023-10-04 14:00:06 +02001892 qemu_opts_do_parse(opts, optstr, dummy_opts.implied_opt_name,
Laurent Vivier5166fe02022-10-21 11:09:11 +02001893 &error_abort);
1894 type = qemu_opt_get(opts, "type");
1895 is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
1896
1897 qemu_opts_reset(&dummy_opts);
1898
1899 return is_modern;
Laurent Vivierf3eedcd2022-10-21 11:09:09 +02001900}
1901
1902/*
1903 * netdev_parse_modern() uses modern, more expressive syntax than
1904 * net_client_parse(), but supports only the -netdev option.
1905 * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
1906 * appends to @qemu_netdev_opts.
1907 */
Philippe Mathieu-Daudé73071f12023-10-04 14:00:06 +02001908void netdev_parse_modern(const char *optstr)
Laurent Vivierf3eedcd2022-10-21 11:09:09 +02001909{
1910 Visitor *v;
1911 NetdevQueueEntry *nd;
1912
Philippe Mathieu-Daudé73071f12023-10-04 14:00:06 +02001913 v = qobject_input_visitor_new_str(optstr, "type", &error_fatal);
Laurent Vivierf3eedcd2022-10-21 11:09:09 +02001914 nd = g_new(NetdevQueueEntry, 1);
1915 visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
1916 visit_free(v);
1917 loc_save(&nd->loc);
1918
1919 QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
1920}
1921
Philippe Mathieu-Daudé73071f12023-10-04 14:00:06 +02001922void net_client_parse(QemuOptsList *opts_list, const char *optstr)
aliguori63a01ef2008-10-31 19:10:00 +00001923{
Philippe Mathieu-Daudé73071f12023-10-04 14:00:06 +02001924 if (!qemu_opts_parse_noisily(opts_list, optstr, true)) {
Laurent Vivier21fccb22022-10-21 11:09:08 +02001925 exit(1);
aliguori63a01ef2008-10-31 19:10:00 +00001926 }
aliguori63a01ef2008-10-31 19:10:00 +00001927}
Jason Wang7fc8d912012-03-05 11:08:50 +08001928
1929/* From FreeBSD */
1930/* XXX: optimize */
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001931uint32_t net_crc32(const uint8_t *p, int len)
Jason Wang7fc8d912012-03-05 11:08:50 +08001932{
1933 uint32_t crc;
1934 int carry, i, j;
1935 uint8_t b;
1936
1937 crc = 0xffffffff;
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001938 for (i = 0; i < len; i++) {
1939 b = *p++;
Jason Wang7fc8d912012-03-05 11:08:50 +08001940 for (j = 0; j < 8; j++) {
1941 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1942 crc <<= 1;
1943 b >>= 1;
1944 if (carry) {
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001945 crc = ((crc ^ POLYNOMIAL_BE) | carry);
Jason Wang7fc8d912012-03-05 11:08:50 +08001946 }
1947 }
1948 }
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001949
1950 return crc;
1951}
1952
Mark Cave-Aylandf1a7deb2017-12-15 18:41:44 +00001953uint32_t net_crc32_le(const uint8_t *p, int len)
1954{
1955 uint32_t crc;
1956 int carry, i, j;
1957 uint8_t b;
1958
1959 crc = 0xffffffff;
1960 for (i = 0; i < len; i++) {
1961 b = *p++;
1962 for (j = 0; j < 8; j++) {
1963 carry = (crc & 0x1) ^ (b & 0x01);
1964 crc >>= 1;
1965 b >>= 1;
1966 if (carry) {
1967 crc ^= POLYNOMIAL_LE;
1968 }
1969 }
1970 }
1971
1972 return crc;
1973}
1974
Paolo Bonzini4d454572012-11-26 16:03:42 +01001975QemuOptsList qemu_netdev_opts = {
1976 .name = "netdev",
1977 .implied_opt_name = "type",
1978 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1979 .desc = {
1980 /*
1981 * no elements => accept any params
1982 * validation will happen later
1983 */
1984 { /* end of list */ }
1985 },
1986};
1987
Thomas Huth78cd6f72018-02-21 11:18:36 +01001988QemuOptsList qemu_nic_opts = {
1989 .name = "nic",
1990 .implied_opt_name = "type",
1991 .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
1992 .desc = {
1993 /*
1994 * no elements => accept any params
1995 * validation will happen later
1996 */
1997 { /* end of list */ }
1998 },
1999};
2000
Paolo Bonzini4d454572012-11-26 16:03:42 +01002001QemuOptsList qemu_net_opts = {
2002 .name = "net",
2003 .implied_opt_name = "type",
2004 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
2005 .desc = {
2006 /*
2007 * no elements => accept any params
2008 * validation will happen later
2009 */
2010 { /* end of list */ }
2011 },
2012};
Zhang Chen16a3df42016-05-13 15:35:19 +08002013
2014void net_socket_rs_init(SocketReadState *rs,
Zhang Chen3cde5ea2017-07-04 14:53:46 +08002015 SocketReadStateFinalize *finalize,
2016 bool vnet_hdr)
Zhang Chen16a3df42016-05-13 15:35:19 +08002017{
2018 rs->state = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08002019 rs->vnet_hdr = vnet_hdr;
Zhang Chen16a3df42016-05-13 15:35:19 +08002020 rs->index = 0;
2021 rs->packet_len = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08002022 rs->vnet_hdr_len = 0;
Zhang Chen16a3df42016-05-13 15:35:19 +08002023 memset(rs->buf, 0, sizeof(rs->buf));
2024 rs->finalize = finalize;
2025}
2026
2027/*
2028 * Returns
Zhang Chene9e0a582016-08-18 11:23:25 +08002029 * 0: success
2030 * -1: error occurs
Zhang Chen16a3df42016-05-13 15:35:19 +08002031 */
2032int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
2033{
2034 unsigned int l;
2035
2036 while (size > 0) {
Zhang Chen3cde5ea2017-07-04 14:53:46 +08002037 /* Reassemble a packet from the network.
2038 * 0 = getting length.
2039 * 1 = getting vnet header length.
2040 * 2 = getting data.
2041 */
2042 switch (rs->state) {
Zhang Chen16a3df42016-05-13 15:35:19 +08002043 case 0:
2044 l = 4 - rs->index;
2045 if (l > size) {
2046 l = size;
2047 }
2048 memcpy(rs->buf + rs->index, buf, l);
2049 buf += l;
2050 size -= l;
2051 rs->index += l;
2052 if (rs->index == 4) {
2053 /* got length */
2054 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
2055 rs->index = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08002056 if (rs->vnet_hdr) {
2057 rs->state = 1;
2058 } else {
2059 rs->state = 2;
2060 rs->vnet_hdr_len = 0;
2061 }
Zhang Chen16a3df42016-05-13 15:35:19 +08002062 }
2063 break;
2064 case 1:
Zhang Chen3cde5ea2017-07-04 14:53:46 +08002065 l = 4 - rs->index;
2066 if (l > size) {
2067 l = size;
2068 }
2069 memcpy(rs->buf + rs->index, buf, l);
2070 buf += l;
2071 size -= l;
2072 rs->index += l;
2073 if (rs->index == 4) {
2074 /* got vnet header length */
2075 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
2076 rs->index = 0;
2077 rs->state = 2;
2078 }
2079 break;
2080 case 2:
Zhang Chen16a3df42016-05-13 15:35:19 +08002081 l = rs->packet_len - rs->index;
2082 if (l > size) {
2083 l = size;
2084 }
2085 if (rs->index + l <= sizeof(rs->buf)) {
2086 memcpy(rs->buf + rs->index, buf, l);
2087 } else {
2088 fprintf(stderr, "serious error: oversized packet received,"
2089 "connection terminated.\n");
2090 rs->index = rs->state = 0;
2091 return -1;
2092 }
2093
2094 rs->index += l;
2095 buf += l;
2096 size -= l;
2097 if (rs->index >= rs->packet_len) {
2098 rs->index = 0;
2099 rs->state = 0;
Daniel P. Berrangee79cd402016-11-04 15:46:33 +00002100 assert(rs->finalize);
2101 rs->finalize(rs);
Zhang Chen16a3df42016-05-13 15:35:19 +08002102 }
2103 break;
2104 }
2105 }
Zhang Chene9e0a582016-08-18 11:23:25 +08002106
2107 assert(size == 0);
Zhang Chen16a3df42016-05-13 15:35:19 +08002108 return 0;
2109}