blob: d1130296e1a4a338a3cddd478731d7e32c1643f5 [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"
38#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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010046#include "qemu/iov.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010047#include "qemu/main-loop.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010048#include "qemu/option.h"
Markus Armbrustere688df62018-02-01 12:18:31 +010049#include "qapi/error.h"
Laszlo Ersek6687b792012-07-17 16:17:13 +020050#include "qapi/opts-visitor.h"
zhanghailiange1d64c02014-08-26 16:06:17 +080051#include "sysemu/sysemu.h"
Thomas Huth559964a2017-05-15 15:32:56 +020052#include "sysemu/qtest.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020053#include "sysemu/runstate.h"
54#include "sysemu/sysemu.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
Stefano Garzarellaadd99342019-05-17 15:47:47 +020077 substrings = g_strsplit(str, ":", 2);
78 if (!substrings || !substrings[0] || !substrings[1]) {
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080079 error_setg(errp, "host address '%s' doesn't contain ':' "
80 "separating host from port", str);
Stefano Garzarellaadd99342019-05-17 15:47:47 +020081 ret = -1;
82 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080083 }
Stefano Garzarellaadd99342019-05-17 15:47:47 +020084
85 addr = substrings[0];
86 p = substrings[1];
87
aliguori63a01ef2008-10-31 19:10:00 +000088 saddr->sin_family = AF_INET;
Stefano Garzarellaadd99342019-05-17 15:47:47 +020089 if (addr[0] == '\0') {
aliguori63a01ef2008-10-31 19:10:00 +000090 saddr->sin_addr.s_addr = 0;
91 } else {
Stefano Garzarellaadd99342019-05-17 15:47:47 +020092 if (qemu_isdigit(addr[0])) {
93 if (!inet_aton(addr, &saddr->sin_addr)) {
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080094 error_setg(errp, "host address '%s' is not a valid "
Stefano Garzarellaadd99342019-05-17 15:47:47 +020095 "IPv4 address", addr);
96 ret = -1;
97 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +080098 }
aliguori63a01ef2008-10-31 19:10:00 +000099 } else {
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200100 he = gethostbyname(addr);
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800101 if (he == NULL) {
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200102 error_setg(errp, "can't resolve host address '%s'", addr);
103 ret = -1;
104 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800105 }
aliguori63a01ef2008-10-31 19:10:00 +0000106 saddr->sin_addr = *(struct in_addr *)he->h_addr;
107 }
108 }
109 port = strtol(p, (char **)&r, 0);
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800110 if (r == p) {
111 error_setg(errp, "port number '%s' is invalid", p);
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200112 ret = -1;
113 goto out;
Mao Zhongyibcd4dfd2017-09-04 22:35:39 +0800114 }
aliguori63a01ef2008-10-31 19:10:00 +0000115 saddr->sin_port = htons(port);
Stefano Garzarellaadd99342019-05-17 15:47:47 +0200116
117out:
118 g_strfreev(substrings);
119 return ret;
aliguori63a01ef2008-10-31 19:10:00 +0000120}
121
Scott Feldman890ee6a2015-03-13 21:09:25 -0700122char *qemu_mac_strdup_printf(const uint8_t *macaddr)
123{
124 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
125 macaddr[0], macaddr[1], macaddr[2],
126 macaddr[3], macaddr[4], macaddr[5]);
127}
128
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100129void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
aliguori7cb7434b2009-01-07 17:46:21 +0000130{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100131 snprintf(nc->info_str, sizeof(nc->info_str),
aliguori4dda4062009-01-08 19:01:37 +0000132 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100133 nc->model,
aliguori7cb7434b2009-01-07 17:46:21 +0000134 macaddr[0], macaddr[1], macaddr[2],
135 macaddr[3], macaddr[4], macaddr[5]);
136}
137
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800138static int mac_table[256] = {0};
139
140static void qemu_macaddr_set_used(MACAddr *macaddr)
141{
142 int index;
143
144 for (index = 0x56; index < 0xFF; index++) {
145 if (macaddr->a[5] == index) {
146 mac_table[index]++;
147 }
148 }
149}
150
151static void qemu_macaddr_set_free(MACAddr *macaddr)
152{
153 int index;
154 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
155
156 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
157 return;
158 }
159 for (index = 0x56; index < 0xFF; index++) {
160 if (macaddr->a[5] == index) {
161 mac_table[index]--;
162 }
163 }
164}
165
166static int qemu_macaddr_get_free(void)
167{
168 int index;
169
170 for (index = 0x56; index < 0xFF; index++) {
171 if (mac_table[index] == 0) {
172 return index;
173 }
174 }
175
176 return -1;
177}
178
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200179void qemu_macaddr_default_if_unset(MACAddr *macaddr)
180{
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200181 static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800182 static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200183
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800184 if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
185 if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
186 return;
187 } else {
188 qemu_macaddr_set_used(macaddr);
189 return;
190 }
191 }
192
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200193 macaddr->a[0] = 0x52;
194 macaddr->a[1] = 0x54;
195 macaddr->a[2] = 0x00;
196 macaddr->a[3] = 0x12;
197 macaddr->a[4] = 0x34;
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800198 macaddr->a[5] = qemu_macaddr_get_free();
199 qemu_macaddr_set_used(macaddr);
Gerd Hoffmann76d32cb2009-10-21 15:25:22 +0200200}
201
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100202/**
203 * Generate a name for net client
204 *
Amos Kongc9635302013-04-15 18:55:19 +0800205 * Only net clients created with the legacy -net option and NICs need this.
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100206 */
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100207static char *assign_name(NetClientState *nc1, const char *model)
aliguori676cff22009-01-07 17:43:44 +0000208{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100209 NetClientState *nc;
aliguori676cff22009-01-07 17:43:44 +0000210 int id = 0;
211
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100212 QTAILQ_FOREACH(nc, &net_clients, next) {
213 if (nc == nc1) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100214 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100215 }
Amos Kongc9635302013-04-15 18:55:19 +0800216 if (strcmp(nc->model, model) == 0) {
Markus Armbruster53e51d82011-06-16 18:45:36 +0200217 id++;
218 }
219 }
220
Hani Benhabiles4bf2c132014-01-09 19:34:27 +0100221 return g_strdup_printf("%s.%d", model, id);
aliguori676cff22009-01-07 17:43:44 +0000222}
223
Jason Wangf7860452013-01-30 19:12:27 +0800224static void qemu_net_client_destructor(NetClientState *nc)
225{
226 g_free(nc);
227}
Jason Wang25c01bd2018-12-04 11:53:43 +0800228static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
229 unsigned flags,
230 const struct iovec *iov,
231 int iovcnt,
232 void *opaque);
Jason Wangf7860452013-01-30 19:12:27 +0800233
Jason Wang18a15412013-01-30 19:12:26 +0800234static void qemu_net_client_setup(NetClientState *nc,
235 NetClientInfo *info,
236 NetClientState *peer,
237 const char *model,
Jason Wangf7860452013-01-30 19:12:27 +0800238 const char *name,
239 NetClientDestructor *destructor)
aliguori63a01ef2008-10-31 19:10:00 +0000240{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100241 nc->info = info;
242 nc->model = g_strdup(model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000243 if (name) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100244 nc->name = g_strdup(name);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000245 } else {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100246 nc->name = assign_name(nc, model);
Mark McLoughlin45460d12009-11-25 18:49:02 +0000247 }
aliguori63a01ef2008-10-31 19:10:00 +0000248
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100249 if (peer) {
250 assert(!peer->peer);
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100251 nc->peer = peer;
252 peer->peer = nc;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100253 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100254 QTAILQ_INSERT_TAIL(&net_clients, nc, next);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100255
Yang Hongyang3e033a42015-10-07 11:52:17 +0800256 nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
Jason Wangf7860452013-01-30 19:12:27 +0800257 nc->destructor = destructor;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800258 QTAILQ_INIT(&nc->filters);
Jason Wang18a15412013-01-30 19:12:26 +0800259}
260
261NetClientState *qemu_new_net_client(NetClientInfo *info,
262 NetClientState *peer,
263 const char *model,
264 const char *name)
265{
266 NetClientState *nc;
267
268 assert(info->size >= sizeof(NetClientState));
269
270 nc = g_malloc0(info->size);
Jason Wangf7860452013-01-30 19:12:27 +0800271 qemu_net_client_setup(nc, info, peer, model, name,
272 qemu_net_client_destructor);
Jason Wang18a15412013-01-30 19:12:26 +0800273
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100274 return nc;
aliguori63a01ef2008-10-31 19:10:00 +0000275}
276
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000277NICState *qemu_new_nic(NetClientInfo *info,
278 NICConf *conf,
279 const char *model,
280 const char *name,
281 void *opaque)
282{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800283 NetClientState **peers = conf->peers.ncs;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000284 NICState *nic;
Jiri Pirko575a1c02014-05-26 12:04:08 +0200285 int i, queues = MAX(1, conf->peers.queues);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000286
Eric Blakef394b2e2016-07-13 21:50:23 -0600287 assert(info->type == NET_CLIENT_DRIVER_NIC);
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000288 assert(info->size >= sizeof(NICState));
289
Jason Wangf6b26cf2013-02-22 23:15:06 +0800290 nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
291 nic->ncs = (void *)nic + info->size;
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000292 nic->conf = conf;
293 nic->opaque = opaque;
294
Jason Wangf6b26cf2013-02-22 23:15:06 +0800295 for (i = 0; i < queues; i++) {
296 qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800297 NULL);
298 nic->ncs[i].queue_index = i;
299 }
300
Mark McLoughlinebef2c02009-11-25 18:49:10 +0000301 return nic;
302}
303
Jason Wang1ceef9f2013-01-30 19:12:28 +0800304NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
305{
Jason Wangf6b26cf2013-02-22 23:15:06 +0800306 return nic->ncs + queue_index;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800307}
308
Jason Wangb356f762013-01-30 19:12:22 +0800309NetClientState *qemu_get_queue(NICState *nic)
310{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800311 return qemu_get_subqueue(nic, 0);
Jason Wangb356f762013-01-30 19:12:22 +0800312}
313
Jason Wangcc1f0f42013-01-30 19:12:23 +0800314NICState *qemu_get_nic(NetClientState *nc)
315{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800316 NetClientState *nc0 = nc - nc->queue_index;
317
Jason Wangf6b26cf2013-02-22 23:15:06 +0800318 return (NICState *)((void *)nc0 - nc->info->size);
Jason Wangcc1f0f42013-01-30 19:12:23 +0800319}
320
321void *qemu_get_nic_opaque(NetClientState *nc)
322{
323 NICState *nic = qemu_get_nic(nc);
324
325 return nic->opaque;
326}
327
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100328static void qemu_cleanup_net_client(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000329{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100330 QTAILQ_REMOVE(&net_clients, nc, next);
aliguori63a01ef2008-10-31 19:10:00 +0000331
Andreas Färbercc2a9042013-02-12 23:16:06 +0100332 if (nc->info->cleanup) {
333 nc->info->cleanup(nc);
334 }
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200335}
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100336
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100337static void qemu_free_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200338{
Jan Kiszka067404b2013-08-02 21:47:08 +0200339 if (nc->incoming_queue) {
340 qemu_del_net_queue(nc->incoming_queue);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200341 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100342 if (nc->peer) {
343 nc->peer->peer = NULL;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100344 }
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100345 g_free(nc->name);
346 g_free(nc->model);
Jason Wangf7860452013-01-30 19:12:27 +0800347 if (nc->destructor) {
348 nc->destructor(nc);
349 }
aliguori63a01ef2008-10-31 19:10:00 +0000350}
351
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100352void qemu_del_net_client(NetClientState *nc)
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200353{
Jason Wang1ceef9f2013-01-30 19:12:28 +0800354 NetClientState *ncs[MAX_QUEUE_NUM];
355 int queues, i;
Yang Hongyangfdccce42015-10-07 11:52:14 +0800356 NetFilterState *nf, *next;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800357
Eric Blakef394b2e2016-07-13 21:50:23 -0600358 assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
Paolo Bonzini7fb43912014-12-23 17:53:20 +0100359
Jason Wang1ceef9f2013-01-30 19:12:28 +0800360 /* If the NetClientState belongs to a multiqueue backend, we will change all
361 * other NetClientStates also.
362 */
363 queues = qemu_find_net_clients_except(nc->name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600364 NET_CLIENT_DRIVER_NIC,
Jason Wang1ceef9f2013-01-30 19:12:28 +0800365 MAX_QUEUE_NUM);
366 assert(queues != 0);
367
Yang Hongyangfdccce42015-10-07 11:52:14 +0800368 QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
369 object_unparent(OBJECT(nf));
370 }
371
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200372 /* If there is a peer NIC, delete and cleanup client, but do not free. */
Eric Blakef394b2e2016-07-13 21:50:23 -0600373 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wangcc1f0f42013-01-30 19:12:23 +0800374 NICState *nic = qemu_get_nic(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200375 if (nic->peer_deleted) {
376 return;
377 }
378 nic->peer_deleted = true;
Jason Wang1ceef9f2013-01-30 19:12:28 +0800379
380 for (i = 0; i < queues; i++) {
381 ncs[i]->peer->link_down = true;
382 }
383
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100384 if (nc->peer->info->link_status_changed) {
385 nc->peer->info->link_status_changed(nc->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200386 }
Jason Wang1ceef9f2013-01-30 19:12:28 +0800387
388 for (i = 0; i < queues; i++) {
389 qemu_cleanup_net_client(ncs[i]);
390 }
391
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200392 return;
393 }
394
Jason Wang1ceef9f2013-01-30 19:12:28 +0800395 for (i = 0; i < queues; i++) {
396 qemu_cleanup_net_client(ncs[i]);
397 qemu_free_net_client(ncs[i]);
398 }
Jason Wang948ecf22013-01-30 19:12:24 +0800399}
400
401void qemu_del_nic(NICState *nic)
402{
Jiri Pirko575a1c02014-05-26 12:04:08 +0200403 int i, queues = MAX(nic->conf->peers.queues, 1);
Jason Wang1ceef9f2013-01-30 19:12:28 +0800404
Shannon Zhao2bc22a52015-05-21 17:44:48 +0800405 qemu_macaddr_set_free(&nic->conf->macaddr);
406
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200407 /* If this is a peer NIC and peer has already been deleted, free it now. */
Jason Wang1ceef9f2013-01-30 19:12:28 +0800408 if (nic->peer_deleted) {
409 for (i = 0; i < queues; i++) {
410 qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
Michael S. Tsirkina083a892010-09-20 18:08:41 +0200411 }
412 }
413
Jason Wang1ceef9f2013-01-30 19:12:28 +0800414 for (i = queues - 1; i >= 0; i--) {
415 NetClientState *nc = qemu_get_subqueue(nic, i);
416
417 qemu_cleanup_net_client(nc);
418 qemu_free_net_client(nc);
419 }
Jason Wangf6b26cf2013-02-22 23:15:06 +0800420
421 g_free(nic);
Jan Kiszka1a609522009-06-24 14:42:31 +0200422}
423
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000424void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
425{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100426 NetClientState *nc;
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000427
Stefan Hajnoczi94878992012-07-24 16:35:12 +0100428 QTAILQ_FOREACH(nc, &net_clients, next) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600429 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wang1ceef9f2013-01-30 19:12:28 +0800430 if (nc->queue_index == 0) {
431 func(qemu_get_nic(nc), opaque);
432 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000433 }
434 }
Mark McLoughlin57f9ef12009-11-25 18:49:31 +0000435}
436
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100437bool qemu_has_ufo(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100438{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100439 if (!nc || !nc->info->has_ufo) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100440 return false;
441 }
442
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100443 return nc->info->has_ufo(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100444}
445
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100446bool qemu_has_vnet_hdr(NetClientState *nc)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100447{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100448 if (!nc || !nc->info->has_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100449 return false;
450 }
451
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100452 return nc->info->has_vnet_hdr(nc);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100453}
454
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100455bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100456{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100457 if (!nc || !nc->info->has_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100458 return false;
459 }
460
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100461 return nc->info->has_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100462}
463
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100464void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100465{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100466 if (!nc || !nc->info->using_vnet_hdr) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100467 return;
468 }
469
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100470 nc->info->using_vnet_hdr(nc, enable);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100471}
472
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100473void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100474 int ecn, int ufo)
475{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100476 if (!nc || !nc->info->set_offload) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100477 return;
478 }
479
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100480 nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100481}
482
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100483void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100484{
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100485 if (!nc || !nc->info->set_vnet_hdr_len) {
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100486 return;
487 }
488
Zhang Chend6b732e2017-07-04 14:53:45 +0800489 nc->vnet_hdr_len = len;
Stefan Hajnoczid6085e32014-02-20 12:14:07 +0100490 nc->info->set_vnet_hdr_len(nc, len);
Vincenzo Maffione1f55ac42014-02-06 17:02:16 +0100491}
492
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200493int qemu_set_vnet_le(NetClientState *nc, bool is_le)
494{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300495#ifdef HOST_WORDS_BIGENDIAN
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200496 if (!nc || !nc->info->set_vnet_le) {
497 return -ENOSYS;
498 }
499
500 return nc->info->set_vnet_le(nc, is_le);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300501#else
502 return 0;
503#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200504}
505
506int qemu_set_vnet_be(NetClientState *nc, bool is_be)
507{
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300508#ifdef HOST_WORDS_BIGENDIAN
509 return 0;
510#else
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200511 if (!nc || !nc->info->set_vnet_be) {
512 return -ENOSYS;
513 }
514
515 return nc->info->set_vnet_be(nc, is_be);
Michael S. Tsirkin052bd522015-10-14 12:11:27 +0300516#endif
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200517}
518
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100519int qemu_can_send_packet(NetClientState *sender)
aliguori63a01ef2008-10-31 19:10:00 +0000520{
zhanghailiange1d64c02014-08-26 16:06:17 +0800521 int vm_running = runstate_is_running();
522
523 if (!vm_running) {
524 return 0;
525 }
526
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100527 if (!sender->peer) {
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100528 return 1;
529 }
530
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100531 if (sender->peer->receive_disabled) {
532 return 0;
533 } else if (sender->peer->info->can_receive &&
534 !sender->peer->info->can_receive(sender->peer)) {
535 return 0;
aliguori63a01ef2008-10-31 19:10:00 +0000536 }
Vincent Palatin60c07d92011-03-02 17:25:02 -0500537 return 1;
aliguori63a01ef2008-10-31 19:10:00 +0000538}
539
Yang Hongyange64c7702015-10-07 11:52:15 +0800540static ssize_t filter_receive_iov(NetClientState *nc,
541 NetFilterDirection direction,
542 NetClientState *sender,
543 unsigned flags,
544 const struct iovec *iov,
545 int iovcnt,
546 NetPacketSent *sent_cb)
547{
548 ssize_t ret = 0;
549 NetFilterState *nf = NULL;
550
Li Zhijian25aaadf2016-01-26 13:00:22 +0800551 if (direction == NET_FILTER_DIRECTION_TX) {
552 QTAILQ_FOREACH(nf, &nc->filters, next) {
553 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
554 iovcnt, sent_cb);
555 if (ret) {
556 return ret;
557 }
558 }
559 } else {
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100560 QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
Li Zhijian25aaadf2016-01-26 13:00:22 +0800561 ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
562 iovcnt, sent_cb);
563 if (ret) {
564 return ret;
565 }
Yang Hongyange64c7702015-10-07 11:52:15 +0800566 }
567 }
568
569 return ret;
570}
571
572static ssize_t filter_receive(NetClientState *nc,
573 NetFilterDirection direction,
574 NetClientState *sender,
575 unsigned flags,
576 const uint8_t *data,
577 size_t size,
578 NetPacketSent *sent_cb)
579{
580 struct iovec iov = {
581 .iov_base = (void *)data,
582 .iov_len = size
583 };
584
585 return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
586}
587
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100588void qemu_purge_queued_packets(NetClientState *nc)
aliguori63a01ef2008-10-31 19:10:00 +0000589{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100590 if (!nc->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100591 return;
592 }
593
Jan Kiszka067404b2013-08-02 21:47:08 +0200594 qemu_net_queue_purge(nc->peer->incoming_queue, nc);
Mark McLoughlin8cad5512009-06-18 18:21:29 +0100595}
596
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300597void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
Mark McLoughline94667b2009-04-29 11:48:12 +0100598{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100599 nc->receive_disabled = 0;
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100600
Eric Blakef394b2e2016-07-13 21:50:23 -0600601 if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
Luigi Rizzo199ee602013-02-05 17:53:31 +0100602 if (net_hub_flush(nc->peer)) {
603 qemu_notify_event();
604 }
Luigi Rizzo199ee602013-02-05 17:53:31 +0100605 }
Jan Kiszka067404b2013-08-02 21:47:08 +0200606 if (qemu_net_queue_flush(nc->incoming_queue)) {
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200607 /* We emptied the queue successfully, signal to the IO thread to repoll
608 * the file descriptor (for tap, for example).
609 */
610 qemu_notify_event();
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300611 } else if (purge) {
612 /* Unable to empty the queue, purge remaining packets */
Jason Wang5fe19fb2020-05-11 12:04:53 +0800613 qemu_net_queue_purge(nc->incoming_queue, nc->peer);
Paolo Bonzini987a9b42012-08-09 16:45:55 +0200614 }
Mark McLoughline94667b2009-04-29 11:48:12 +0100615}
616
Michael S. Tsirkinca77d852014-09-04 11:39:13 +0300617void qemu_flush_queued_packets(NetClientState *nc)
618{
619 qemu_flush_or_purge_queued_packets(nc, false);
620}
621
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100622static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100623 unsigned flags,
624 const uint8_t *buf, int size,
625 NetPacketSent *sent_cb)
aliguori764a4d12009-04-21 19:56:41 +0000626{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100627 NetQueue *queue;
Yang Hongyange64c7702015-10-07 11:52:15 +0800628 int ret;
Mark McLoughline94667b2009-04-29 11:48:12 +0100629
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100630#ifdef DEBUG_NET
Mark McLoughlind80b9fc2009-10-08 19:58:24 +0100631 printf("qemu_send_packet_async:\n");
Isaac Lozanoa1555552016-03-25 03:42:15 -0700632 qemu_hexdump((const char *)buf, stdout, "net", size);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100633#endif
634
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100635 if (sender->link_down || !sender->peer) {
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100636 return size;
637 }
638
Yang Hongyange64c7702015-10-07 11:52:15 +0800639 /* Let filters handle the packet first */
640 ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
641 sender, flags, buf, size, sent_cb);
642 if (ret) {
643 return ret;
644 }
645
646 ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
647 sender, flags, buf, size, sent_cb);
648 if (ret) {
649 return ret;
650 }
651
Jan Kiszka067404b2013-08-02 21:47:08 +0200652 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100653
Mark McLoughlinca77d172009-10-22 17:43:41 +0100654 return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
655}
656
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100657ssize_t qemu_send_packet_async(NetClientState *sender,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100658 const uint8_t *buf, int size,
659 NetPacketSent *sent_cb)
660{
661 return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
662 buf, size, sent_cb);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100663}
664
Marc-André Lureau625a5262019-01-17 15:43:54 +0400665ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100666{
Marc-André Lureau625a5262019-01-17 15:43:54 +0400667 return qemu_send_packet_async(nc, buf, size, NULL);
aliguori63a01ef2008-10-31 19:10:00 +0000668}
669
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100670ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
Mark McLoughlinca77d172009-10-22 17:43:41 +0100671{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100672 return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
Mark McLoughlinca77d172009-10-22 17:43:41 +0100673 buf, size, NULL);
674}
675
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100676static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800677 int iovcnt, unsigned flags)
aliguorifbe78f42008-12-17 19:13:11 +0000678{
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530679 uint8_t *buf = NULL;
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800680 uint8_t *buffer;
Benjamin Poirierce053662011-02-23 19:57:21 -0500681 size_t offset;
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530682 ssize_t ret;
aliguorifbe78f42008-12-17 19:13:11 +0000683
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800684 if (iovcnt == 1) {
685 buffer = iov[0].iov_base;
686 offset = iov[0].iov_len;
687 } else {
Peter Lieven47f9f152016-06-30 11:49:40 +0200688 offset = iov_size(iov, iovcnt);
689 if (offset > NET_BUFSIZE) {
690 return -1;
691 }
692 buf = g_malloc(offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800693 buffer = buf;
Peter Lieven47f9f152016-06-30 11:49:40 +0200694 offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800695 }
aliguorifbe78f42008-12-17 19:13:11 +0000696
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800697 if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530698 ret = nc->info->receive_raw(nc, buffer, offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800699 } else {
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530700 ret = nc->info->receive(nc, buffer, offset);
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800701 }
Pooja Dhannawat74044c82016-03-28 18:04:11 +0530702
703 g_free(buf);
704 return ret;
aliguorifbe78f42008-12-17 19:13:11 +0000705}
706
Jason Wang25c01bd2018-12-04 11:53:43 +0800707static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
708 unsigned flags,
709 const struct iovec *iov,
710 int iovcnt,
711 void *opaque)
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100712{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100713 NetClientState *nc = opaque;
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100714 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100715
Jason Wang1592a992018-05-30 13:16:36 +0800716
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100717 if (nc->link_down) {
Jason Wang25c01bd2018-12-04 11:53:43 +0800718 return iov_size(iov, iovcnt);
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100719 }
720
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100721 if (nc->receive_disabled) {
722 return 0;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100723 }
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100724
Peter Lievenca1ee3d2016-06-09 11:39:27 +0200725 if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) {
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100726 ret = nc->info->receive_iov(nc, iov, iovcnt);
727 } else {
Yang Hongyangfefe2a72015-10-07 11:52:16 +0800728 ret = nc_sendv_compat(nc, iov, iovcnt, flags);
Stefan Hajnoczic67f5dc2012-08-17 21:16:42 +0100729 }
730
731 if (ret == 0) {
732 nc->receive_disabled = 1;
733 }
734
735 return ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100736}
737
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100738ssize_t qemu_sendv_packet_async(NetClientState *sender,
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100739 const struct iovec *iov, int iovcnt,
740 NetPacketSent *sent_cb)
aliguorifbe78f42008-12-17 19:13:11 +0000741{
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100742 NetQueue *queue;
Jason Wang25c01bd2018-12-04 11:53:43 +0800743 size_t size = iov_size(iov, iovcnt);
Yang Hongyange64c7702015-10-07 11:52:15 +0800744 int ret;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100745
Jason Wang25c01bd2018-12-04 11:53:43 +0800746 if (size > NET_BUFSIZE) {
747 return size;
748 }
749
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100750 if (sender->link_down || !sender->peer) {
Jason Wang25c01bd2018-12-04 11:53:43 +0800751 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000752 }
753
Yang Hongyange64c7702015-10-07 11:52:15 +0800754 /* Let filters handle the packet first */
755 ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
756 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
757 if (ret) {
758 return ret;
759 }
760
761 ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
762 QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
763 if (ret) {
764 return ret;
765 }
766
Jan Kiszka067404b2013-08-02 21:47:08 +0200767 queue = sender->peer->incoming_queue;
Mark McLoughlin9a6ecb32009-10-08 19:58:32 +0100768
Mark McLoughlinc0b8e492009-10-22 17:43:40 +0100769 return qemu_net_queue_send_iov(queue, sender,
770 QEMU_NET_PACKET_FLAG_NONE,
771 iov, iovcnt, sent_cb);
aliguorifbe78f42008-12-17 19:13:11 +0000772}
773
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100774ssize_t
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100775qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100776{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100777 return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
Mark McLoughlinf3b6c7f2009-04-29 12:15:26 +0100778}
779
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100780NetClientState *qemu_find_netdev(const char *id)
aliguori63a01ef2008-10-31 19:10:00 +0000781{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100782 NetClientState *nc;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +0100783
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100784 QTAILQ_FOREACH(nc, &net_clients, next) {
Eric Blakef394b2e2016-07-13 21:50:23 -0600785 if (nc->info->type == NET_CLIENT_DRIVER_NIC)
Markus Armbruster85dde9a2011-06-16 18:45:37 +0200786 continue;
Stefan Hajnoczi35277d12012-07-24 16:35:14 +0100787 if (!strcmp(nc->name, id)) {
788 return nc;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100789 }
790 }
791
792 return NULL;
793}
794
Jason Wang6c51ae72013-01-30 19:12:25 +0800795int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -0600796 NetClientDriver type, int max)
Jason Wang6c51ae72013-01-30 19:12:25 +0800797{
798 NetClientState *nc;
799 int ret = 0;
800
801 QTAILQ_FOREACH(nc, &net_clients, next) {
802 if (nc->info->type == type) {
803 continue;
804 }
Hani Benhabiles40d19392014-05-07 23:41:30 +0100805 if (!id || !strcmp(nc->name, id)) {
Jason Wang6c51ae72013-01-30 19:12:25 +0800806 if (ret < max) {
807 ncs[ret] = nc;
808 }
809 ret++;
810 }
811 }
812
813 return ret;
814}
815
aliguori76970792009-02-11 15:20:03 +0000816static int nic_get_free_idx(void)
817{
818 int index;
819
820 for (index = 0; index < MAX_NICS; index++)
821 if (!nd_table[index].used)
822 return index;
823 return -1;
824}
825
Markus Armbruster07caea32009-09-25 03:53:51 +0200826int qemu_show_nic_models(const char *arg, const char *const *models)
827{
828 int i;
829
Peter Maydellc8057f92012-08-02 13:45:54 +0100830 if (!arg || !is_help_option(arg)) {
Markus Armbruster07caea32009-09-25 03:53:51 +0200831 return 0;
Peter Maydellc8057f92012-08-02 13:45:54 +0100832 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200833
Thomas Huth7b71e032019-04-23 18:06:08 +0200834 printf("Supported NIC models:\n");
835 for (i = 0 ; models[i]; i++) {
836 printf("%s\n", models[i]);
837 }
Markus Armbruster07caea32009-09-25 03:53:51 +0200838 return 1;
839}
840
aliguorid07f22c2009-01-13 19:03:57 +0000841void qemu_check_nic_model(NICInfo *nd, const char *model)
842{
843 const char *models[2];
844
845 models[0] = model;
846 models[1] = NULL;
847
Markus Armbruster07caea32009-09-25 03:53:51 +0200848 if (qemu_show_nic_models(nd->model, models))
849 exit(0);
850 if (qemu_find_nic_model(nd, models, model) < 0)
851 exit(1);
aliguorid07f22c2009-01-13 19:03:57 +0000852}
853
Markus Armbruster07caea32009-09-25 03:53:51 +0200854int qemu_find_nic_model(NICInfo *nd, const char * const *models,
855 const char *default_model)
aliguorid07f22c2009-01-13 19:03:57 +0000856{
Markus Armbruster07caea32009-09-25 03:53:51 +0200857 int i;
aliguorid07f22c2009-01-13 19:03:57 +0000858
859 if (!nd->model)
Anthony Liguori7267c092011-08-20 22:09:37 -0500860 nd->model = g_strdup(default_model);
aliguorid07f22c2009-01-13 19:03:57 +0000861
Markus Armbruster07caea32009-09-25 03:53:51 +0200862 for (i = 0 ; models[i]; i++) {
863 if (strcmp(nd->model, models[i]) == 0)
864 return i;
aliguorid07f22c2009-01-13 19:03:57 +0000865 }
866
Markus Armbruster6daf1942011-06-22 14:03:54 +0200867 error_report("Unsupported NIC model: %s", nd->model);
Markus Armbruster07caea32009-09-25 03:53:51 +0200868 return -1;
aliguorid07f22c2009-01-13 19:03:57 +0000869}
870
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600871static int net_init_nic(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200872 NetClientState *peer, Error **errp)
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100873{
874 int idx;
875 NICInfo *nd;
Laszlo Ersek2456f362012-07-17 16:17:14 +0200876 const NetLegacyNicOptions *nic;
877
Eric Blakef394b2e2016-07-13 21:50:23 -0600878 assert(netdev->type == NET_CLIENT_DRIVER_NIC);
879 nic = &netdev->u.nic;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100880
881 idx = nic_get_free_idx();
882 if (idx == -1 || nb_nics >= MAX_NICS) {
Markus Armbruster66308862015-05-15 13:58:51 +0200883 error_setg(errp, "too many NICs");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100884 return -1;
885 }
886
887 nd = &nd_table[idx];
888
889 memset(nd, 0, sizeof(*nd));
890
Laszlo Ersek2456f362012-07-17 16:17:14 +0200891 if (nic->has_netdev) {
892 nd->netdev = qemu_find_netdev(nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100893 if (!nd->netdev) {
Markus Armbruster66308862015-05-15 13:58:51 +0200894 error_setg(errp, "netdev '%s' not found", nic->netdev);
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100895 return -1;
896 }
897 } else {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100898 assert(peer);
899 nd->netdev = peer;
Mark McLoughlin5869c4d2009-10-08 19:58:29 +0100900 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100901 nd->name = g_strdup(name);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200902 if (nic->has_model) {
903 nd->model = g_strdup(nic->model);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100904 }
Laszlo Ersek2456f362012-07-17 16:17:14 +0200905 if (nic->has_addr) {
906 nd->devaddr = g_strdup(nic->addr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100907 }
908
Laszlo Ersek2456f362012-07-17 16:17:14 +0200909 if (nic->has_macaddr &&
910 net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
Markus Armbruster66308862015-05-15 13:58:51 +0200911 error_setg(errp, "invalid syntax for ethernet address");
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100912 return -1;
913 }
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400914 if (nic->has_macaddr &&
915 is_multicast_ether_addr(nd->macaddr.a)) {
Markus Armbruster66308862015-05-15 13:58:51 +0200916 error_setg(errp,
917 "NIC cannot have multicast MAC address (odd 1st byte)");
Dmitry Krivenokd60b20c2013-10-21 12:08:44 +0400918 return -1;
919 }
Jan Kiszka6eed1852011-07-20 12:20:22 +0200920 qemu_macaddr_default_if_unset(&nd->macaddr);
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100921
Laszlo Ersek2456f362012-07-17 16:17:14 +0200922 if (nic->has_vectors) {
923 if (nic->vectors > 0x7ffffff) {
Markus Armbruster66308862015-05-15 13:58:51 +0200924 error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
Laszlo Ersek2456f362012-07-17 16:17:14 +0200925 return -1;
926 }
927 nd->nvectors = nic->vectors;
928 } else {
929 nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100930 }
931
932 nd->used = 1;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100933 nb_nics++;
934
935 return idx;
936}
937
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100938
Eric Blakef394b2e2016-07-13 21:50:23 -0600939static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600940 const Netdev *netdev,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200941 const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200942 NetClientState *peer, Error **errp) = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600943 [NET_CLIENT_DRIVER_NIC] = net_init_nic,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100944#ifdef CONFIG_SLIRP
Eric Blakef394b2e2016-07-13 21:50:23 -0600945 [NET_CLIENT_DRIVER_USER] = net_init_slirp,
Mark McLoughlinec302ff2009-10-06 12:17:07 +0100946#endif
Eric Blakef394b2e2016-07-13 21:50:23 -0600947 [NET_CLIENT_DRIVER_TAP] = net_init_tap,
948 [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
Mark McLoughlindd510582009-10-06 12:17:10 +0100949#ifdef CONFIG_VDE
Eric Blakef394b2e2016-07-13 21:50:23 -0600950 [NET_CLIENT_DRIVER_VDE] = net_init_vde,
Mark McLoughlindd510582009-10-06 12:17:10 +0100951#endif
Vincenzo Maffione58952132013-11-06 11:44:06 +0100952#ifdef CONFIG_NETMAP
Eric Blakef394b2e2016-07-13 21:50:23 -0600953 [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
Vincenzo Maffione58952132013-11-06 11:44:06 +0100954#endif
Stefan Weil2944e4e2012-02-04 09:24:46 +0100955#ifdef CONFIG_NET_BRIDGE
Eric Blakef394b2e2016-07-13 21:50:23 -0600956 [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
Laszlo Ersek6687b792012-07-17 16:17:13 +0200957#endif
Eric Blakef394b2e2016-07-13 21:50:23 -0600958 [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
Paolo Bonzini56f41de2019-02-14 18:35:49 +0100959#ifdef CONFIG_VHOST_NET_USER
Eric Blakef394b2e2016-07-13 21:50:23 -0600960 [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
Nikolay Nikolaev03ce5742014-06-10 13:02:16 +0300961#endif
Gonglei015a33b2014-07-01 20:58:08 +0800962#ifdef CONFIG_L2TPV3
Eric Blakef394b2e2016-07-13 21:50:23 -0600963 [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
Anton Ivanov3fb69aa2014-06-20 10:34:41 +0100964#endif
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100965};
966
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100967
Thomas Huth71830d82020-05-18 20:01:03 +0200968static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +0200969{
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100970 NetClientState *peer = NULL;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +0100971
Markus Armbruster5294e2c2010-03-25 17:22:38 +0100972 if (is_netdev) {
Thomas Huth857d2082018-02-21 11:18:34 +0100973 if (netdev->type == NET_CLIENT_DRIVER_NIC ||
Eric Blakef394b2e2016-07-13 21:50:23 -0600974 !net_client_init_fun[netdev->type]) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100975 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
976 "a netdev backend type");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100977 return -1;
978 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200979 } else {
Thomas Huth71830d82020-05-18 20:01:03 +0200980 if (netdev->type == NET_CLIENT_DRIVER_NONE) {
Stefan Hajnoczi1e81aba2015-05-27 17:16:52 +0100981 return 0; /* nothing to do */
Markus Armbrusterca7eb182015-05-15 13:58:49 +0200982 }
Thomas Huth71830d82020-05-18 20:01:03 +0200983 if (netdev->type == NET_CLIENT_DRIVER_HUBPORT ||
984 !net_client_init_fun[netdev->type]) {
Stefan Hajnoczid139e9a2015-05-27 17:16:50 +0100985 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
986 "a net backend type (maybe it is not compiled "
987 "into this binary)");
988 return -1;
989 }
Mark McLoughlinf6b134a2009-10-08 19:58:27 +0100990
Thomas Huthaf1a5c32018-04-30 20:02:23 +0200991 /* Do not add to a hub if it's a nic with a netdev= parameter. */
Eric Blakef394b2e2016-07-13 21:50:23 -0600992 if (netdev->type != NET_CLIENT_DRIVER_NIC ||
Thomas Huth71830d82020-05-18 20:01:03 +0200993 !netdev->u.nic.has_netdev) {
Thomas Huthaf1a5c32018-04-30 20:02:23 +0200994 peer = net_hub_add_port(0, NULL, NULL);
Thomas Hutha2dbe132017-01-24 10:42:49 +0100995 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100996 }
Laszlo Ersek6687b792012-07-17 16:17:13 +0200997
Thomas Huth9d903f32020-05-18 20:01:02 +0200998 if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) {
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +0100999 /* FIXME drop when all init functions store an Error */
1000 if (errp && !*errp) {
1001 error_setg(errp, QERR_DEVICE_INIT_FAILED,
Markus Armbruster977c7362017-08-24 10:46:08 +02001002 NetClientDriver_str(netdev->type));
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001003 }
Stefan Hajnoczi4ef0def2015-05-27 17:16:51 +01001004 return -1;
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001005 }
Eric Blake08712fc2020-03-17 15:17:11 -05001006
1007 if (is_netdev) {
1008 NetClientState *nc;
1009
1010 nc = qemu_find_netdev(netdev->id);
1011 assert(nc);
1012 nc->is_netdev = true;
1013 }
1014
Laszlo Ersek6687b792012-07-17 16:17:13 +02001015 return 0;
Mark McLoughlinf83c6e12009-10-06 12:17:06 +01001016}
1017
Thomas Huth547203e2018-02-21 11:18:31 +01001018static void show_netdevs(void)
1019{
1020 int idx;
1021 const char *available_netdevs[] = {
1022 "socket",
1023 "hubport",
1024 "tap",
1025#ifdef CONFIG_SLIRP
1026 "user",
1027#endif
1028#ifdef CONFIG_L2TPV3
1029 "l2tpv3",
1030#endif
1031#ifdef CONFIG_VDE
1032 "vde",
1033#endif
1034#ifdef CONFIG_NET_BRIDGE
1035 "bridge",
1036#endif
1037#ifdef CONFIG_NETMAP
1038 "netmap",
1039#endif
1040#ifdef CONFIG_POSIX
1041 "vhost-user",
1042#endif
1043 };
Laszlo Ersek6687b792012-07-17 16:17:13 +02001044
Thomas Huth547203e2018-02-21 11:18:31 +01001045 printf("Available netdev backend types:\n");
1046 for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
1047 puts(available_netdevs[idx]);
1048 }
1049}
Laszlo Ersek6687b792012-07-17 16:17:13 +02001050
Thomas Huthaa09a482018-02-21 11:18:33 +01001051static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
Laszlo Ersek6687b792012-07-17 16:17:13 +02001052{
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001053 gchar **substrings = NULL;
Thomas Huth71830d82020-05-18 20:01:03 +02001054 Netdev *object = NULL;
Laszlo Ersek6687b792012-07-17 16:17:13 +02001055 Error *err = NULL;
1056 int ret = -1;
Eric Blake09204ea2016-06-09 10:48:36 -06001057 Visitor *v = opts_visitor_new(opts);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001058
Lin Ma8b43f962018-06-11 17:23:05 +08001059 const char *type = qemu_opt_get(opts, "type");
1060
1061 if (is_netdev && type && is_help_option(type)) {
Thomas Huth547203e2018-02-21 11:18:31 +01001062 show_netdevs();
1063 exit(0);
1064 } else {
Yann Bordenave7aac5312016-03-15 10:31:22 +01001065 /* Parse convenience option format ip6-net=fec0::0[/64] */
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001066 const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
Yann Bordenave7aac5312016-03-15 10:31:22 +01001067
1068 if (ip6_net) {
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001069 char *prefix_addr;
1070 unsigned long prefix_len = 64; /* Default 64bit prefix length. */
Yann Bordenave7aac5312016-03-15 10:31:22 +01001071
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001072 substrings = g_strsplit(ip6_net, "/", 2);
1073 if (!substrings || !substrings[0]) {
1074 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net",
1075 "a valid IPv6 prefix");
1076 goto out;
1077 }
1078
1079 prefix_addr = substrings[0];
1080
Vladimir Sementsov-Ogievskiy33c96422019-12-05 20:46:16 +03001081 /* Handle user-specified prefix length. */
1082 if (substrings[1] &&
1083 qemu_strtoul(substrings[1], NULL, 10, &prefix_len))
1084 {
1085 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
1086 "ipv6-prefixlen", "a number");
1087 goto out;
Yann Bordenave7aac5312016-03-15 10:31:22 +01001088 }
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001089
1090 qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort);
1091 qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len,
1092 &error_abort);
Samuel Thibault891a2bb2016-03-31 00:52:50 +02001093 qemu_opt_unset(opts, "ipv6-net");
Yann Bordenave7aac5312016-03-15 10:31:22 +01001094 }
1095 }
1096
Thomas Huth71830d82020-05-18 20:01:03 +02001097 /* Create an ID for -net if the user did not specify one */
1098 if (!is_netdev && !qemu_opts_id(opts)) {
1099 static int idx;
1100 qemu_opts_set_id(opts, g_strdup_printf("__org.qemu.net%i", idx++));
Laszlo Ersek6687b792012-07-17 16:17:13 +02001101 }
1102
Thomas Huth71830d82020-05-18 20:01:03 +02001103 visit_type_Netdev(v, NULL, &object, &err);
1104
Laszlo Ersek6687b792012-07-17 16:17:13 +02001105 if (!err) {
Laszlo Ersek1a0c0952012-07-17 16:17:21 +02001106 ret = net_client_init1(object, is_netdev, &err);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001107 }
1108
Thomas Huth71830d82020-05-18 20:01:03 +02001109 qapi_free_Netdev(object);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001110
Stefano Garzarella21c520d2019-05-17 15:47:45 +02001111out:
Laszlo Ersek6687b792012-07-17 16:17:13 +02001112 error_propagate(errp, err);
Stefano Garzarellac1112b22019-05-17 15:47:46 +02001113 g_strfreev(substrings);
Eric Blake09204ea2016-06-09 10:48:36 -06001114 visit_free(v);
Laszlo Ersek6687b792012-07-17 16:17:13 +02001115 return ret;
1116}
1117
Luiz Capitulino928059a2012-04-18 17:34:15 -03001118void netdev_add(QemuOpts *opts, Error **errp)
1119{
Eric Blake0e55c382016-07-13 21:50:24 -06001120 net_client_init(opts, true, errp);
Luiz Capitulino928059a2012-04-18 17:34:15 -03001121}
1122
Eric Blakedb2a3802020-03-17 15:17:10 -05001123void qmp_netdev_add(Netdev *netdev, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001124{
Eric Blake08712fc2020-03-17 15:17:11 -05001125 net_client_init1(netdev, true, errp);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001126}
1127
Luiz Capitulino5f964152012-04-16 14:36:32 -03001128void qmp_netdev_del(const char *id, Error **errp)
Markus Armbrusterae82d322010-03-25 17:22:40 +01001129{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001130 NetClientState *nc;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001131
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001132 nc = qemu_find_netdev(id);
1133 if (!nc) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001134 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1135 "Device '%s' not found", id);
Luiz Capitulino5f964152012-04-16 14:36:32 -03001136 return;
Markus Armbrusterae82d322010-03-25 17:22:40 +01001137 }
Luiz Capitulino5f964152012-04-16 14:36:32 -03001138
Eric Blake08712fc2020-03-17 15:17:11 -05001139 if (!nc->is_netdev) {
Stefan Hajnoczi645c9492012-10-24 14:34:12 +02001140 error_setg(errp, "Device '%s' is not a netdev", id);
1141 return;
1142 }
1143
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001144 qemu_del_net_client(nc);
Markus Armbrusterae82d322010-03-25 17:22:40 +01001145}
1146
zhanghailiangaa9156f2016-01-26 14:43:33 +08001147static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
1148{
1149 char *str;
1150 ObjectProperty *prop;
1151 ObjectPropertyIterator iter;
Eric Blake3b098d52016-06-09 10:48:43 -06001152 Visitor *v;
zhanghailiangaa9156f2016-01-26 14:43:33 +08001153
1154 /* generate info str */
1155 object_property_iter_init(&iter, OBJECT(nf));
1156 while ((prop = object_property_iter_next(&iter))) {
1157 if (!strcmp(prop->name, "type")) {
1158 continue;
1159 }
Eric Blake3b098d52016-06-09 10:48:43 -06001160 v = string_output_visitor_new(false, &str);
1161 object_property_get(OBJECT(nf), v, prop->name, NULL);
1162 visit_complete(v, &str);
1163 visit_free(v);
zhanghailiangaa9156f2016-01-26 14:43:33 +08001164 monitor_printf(mon, ",%s=%s", prop->name, str);
1165 g_free(str);
1166 }
1167 monitor_printf(mon, "\n");
1168}
1169
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001170void print_net_client(Monitor *mon, NetClientState *nc)
Jan Kiszka44e798d2011-07-20 12:20:21 +02001171{
Yang Hongyanga4960f52015-10-07 11:52:19 +08001172 NetFilterState *nf;
1173
Jason Wang1ceef9f2013-01-30 19:12:28 +08001174 monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
1175 nc->queue_index,
Markus Armbruster977c7362017-08-24 10:46:08 +02001176 NetClientDriver_str(nc->info->type),
Jason Wang1ceef9f2013-01-30 19:12:28 +08001177 nc->info_str);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001178 if (!QTAILQ_EMPTY(&nc->filters)) {
1179 monitor_printf(mon, "filters:\n");
1180 }
1181 QTAILQ_FOREACH(nf, &nc->filters, next) {
Yang Hongyanga3e8a3f2015-10-20 09:51:26 +08001182 char *path = object_get_canonical_path_component(OBJECT(nf));
zhanghailiangaa9156f2016-01-26 14:43:33 +08001183
1184 monitor_printf(mon, " - %s: type=%s", path,
1185 object_get_typename(OBJECT(nf)));
1186 netfilter_print_info(mon, nf);
Yang Hongyanga3e8a3f2015-10-20 09:51:26 +08001187 g_free(path);
Yang Hongyanga4960f52015-10-07 11:52:19 +08001188 }
Jan Kiszka44e798d2011-07-20 12:20:21 +02001189}
1190
Amos Kongb1be4282013-06-14 15:45:52 +08001191RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
1192 Error **errp)
1193{
1194 NetClientState *nc;
1195 RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
1196
1197 QTAILQ_FOREACH(nc, &net_clients, next) {
1198 RxFilterInfoList *entry;
1199 RxFilterInfo *info;
1200
1201 if (has_name && strcmp(nc->name, name) != 0) {
1202 continue;
1203 }
1204
1205 /* only query rx-filter information of NIC */
Eric Blakef394b2e2016-07-13 21:50:23 -06001206 if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
Amos Kongb1be4282013-06-14 15:45:52 +08001207 if (has_name) {
1208 error_setg(errp, "net client(%s) isn't a NIC", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001209 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001210 }
1211 continue;
1212 }
1213
Vladislav Yasevich5320c2c2015-10-19 09:04:38 -04001214 /* only query information on queue 0 since the info is per nic,
1215 * not per queue
1216 */
1217 if (nc->queue_index != 0)
1218 continue;
1219
Amos Kongb1be4282013-06-14 15:45:52 +08001220 if (nc->info->query_rx_filter) {
1221 info = nc->info->query_rx_filter(nc);
1222 entry = g_malloc0(sizeof(*entry));
1223 entry->value = info;
1224
1225 if (!filter_list) {
1226 filter_list = entry;
1227 } else {
1228 last_entry->next = entry;
1229 }
1230 last_entry = entry;
1231 } else if (has_name) {
1232 error_setg(errp, "net client(%s) doesn't support"
1233 " rx-filter querying", name);
Markus Armbruster9083da12014-04-24 15:44:18 +02001234 return NULL;
Amos Kongb1be4282013-06-14 15:45:52 +08001235 }
Markus Armbruster638fb142014-04-24 15:44:17 +02001236
1237 if (has_name) {
1238 break;
1239 }
Amos Kongb1be4282013-06-14 15:45:52 +08001240 }
1241
Markus Armbruster9083da12014-04-24 15:44:18 +02001242 if (filter_list == NULL && has_name) {
Amos Kongb1be4282013-06-14 15:45:52 +08001243 error_setg(errp, "invalid net client name: %s", name);
1244 }
1245
1246 return filter_list;
1247}
1248
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001249void hmp_info_network(Monitor *mon, const QDict *qdict)
aliguori63a01ef2008-10-31 19:10:00 +00001250{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001251 NetClientState *nc, *peer;
Eric Blakef394b2e2016-07-13 21:50:23 -06001252 NetClientDriver type;
aliguori63a01ef2008-10-31 19:10:00 +00001253
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001254 net_hub_info(mon);
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001255
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001256 QTAILQ_FOREACH(nc, &net_clients, next) {
1257 peer = nc->peer;
1258 type = nc->info->type;
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001259
1260 /* Skip if already printed in hub info */
1261 if (net_hub_id_for_client(nc, NULL) == 0) {
1262 continue;
Mark McLoughlin5610c3a2009-10-08 19:58:23 +01001263 }
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001264
Eric Blakef394b2e2016-07-13 21:50:23 -06001265 if (!peer || type == NET_CLIENT_DRIVER_NIC) {
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001266 print_net_client(mon, nc);
Jan Kiszka19061e62011-07-20 12:20:19 +02001267 } /* else it's a netdev connected to a NIC, printed with the NIC */
Eric Blakef394b2e2016-07-13 21:50:23 -06001268 if (peer && type == NET_CLIENT_DRIVER_NIC) {
Zhi Yong Wu1a859592012-07-24 16:35:16 +01001269 monitor_printf(mon, " \\ ");
Jan Kiszka44e798d2011-07-20 12:20:21 +02001270 print_net_client(mon, peer);
Markus Armbrustera0104e02010-02-11 14:45:01 +01001271 }
Markus Armbrustera0104e02010-02-11 14:45:01 +01001272 }
aliguori63a01ef2008-10-31 19:10:00 +00001273}
1274
Zhang Chen5fbba3d2018-09-03 12:38:56 +08001275void colo_notify_filters_event(int event, Error **errp)
1276{
1277 NetClientState *nc;
1278 NetFilterState *nf;
1279 NetFilterClass *nfc = NULL;
1280 Error *local_err = NULL;
1281
1282 QTAILQ_FOREACH(nc, &net_clients, next) {
1283 QTAILQ_FOREACH(nf, &nc->filters, next) {
1284 nfc = NETFILTER_GET_CLASS(OBJECT(nf));
1285 nfc->handle_event(nf, event, &local_err);
1286 if (local_err) {
1287 error_propagate(errp, local_err);
1288 return;
1289 }
1290 }
1291 }
1292}
1293
Luiz Capitulino4b371562011-11-23 13:11:55 -02001294void qmp_set_link(const char *name, bool up, Error **errp)
aliguori436e5e52009-01-08 19:44:06 +00001295{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001296 NetClientState *ncs[MAX_QUEUE_NUM];
1297 NetClientState *nc;
1298 int queues, i;
aliguori436e5e52009-01-08 19:44:06 +00001299
Jason Wang1ceef9f2013-01-30 19:12:28 +08001300 queues = qemu_find_net_clients_except(name, ncs,
Eric Blakef394b2e2016-07-13 21:50:23 -06001301 NET_CLIENT_DRIVER__MAX,
Jason Wang1ceef9f2013-01-30 19:12:28 +08001302 MAX_QUEUE_NUM);
1303
1304 if (queues == 0) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001305 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1306 "Device '%s' not found", name);
Luiz Capitulino4b371562011-11-23 13:11:55 -02001307 return;
aliguori436e5e52009-01-08 19:44:06 +00001308 }
Jason Wang1ceef9f2013-01-30 19:12:28 +08001309 nc = ncs[0];
aliguori436e5e52009-01-08 19:44:06 +00001310
Jason Wang1ceef9f2013-01-30 19:12:28 +08001311 for (i = 0; i < queues; i++) {
1312 ncs[i]->link_down = !up;
1313 }
aliguori436e5e52009-01-08 19:44:06 +00001314
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001315 if (nc->info->link_status_changed) {
1316 nc->info->link_status_changed(nc);
Mark McLoughlin665a3b02009-11-25 18:49:30 +00001317 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001318
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001319 if (nc->peer) {
1320 /* Change peer link only if the peer is NIC and then notify peer.
1321 * If the peer is a HUBPORT or a backend, we do not change the
1322 * link status.
1323 *
Thomas Huthaf1a5c32018-04-30 20:02:23 +02001324 * This behavior is compatible with qemu hubs where there could be
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001325 * multiple clients that can still communicate with each other in
1326 * disconnected mode. For now maintain this compatibility.
1327 */
Eric Blakef394b2e2016-07-13 21:50:23 -06001328 if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
Vlad Yasevich02d38fc2013-11-21 21:05:51 -05001329 for (i = 0; i < queues; i++) {
1330 ncs[i]->peer->link_down = !up;
1331 }
1332 }
1333 if (nc->peer->info->link_status_changed) {
1334 nc->peer->info->link_status_changed(nc->peer);
1335 }
Michael S. Tsirkinab1cbe12011-02-09 18:45:04 +02001336 }
aliguori436e5e52009-01-08 19:44:06 +00001337}
1338
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001339static void net_vm_change_state_handler(void *opaque, int running,
1340 RunState state)
1341{
Fam Zheng625de442015-07-07 09:21:07 +08001342 NetClientState *nc;
1343 NetClientState *tmp;
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001344
Fam Zheng625de442015-07-07 09:21:07 +08001345 QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
1346 if (running) {
1347 /* Flush queued packets and wake up backends. */
1348 if (nc->peer && qemu_can_send_packet(nc)) {
1349 qemu_flush_queued_packets(nc->peer);
1350 }
1351 } else {
1352 /* Complete all queued packets, to guarantee we don't modify
1353 * state later when VM is not running.
1354 */
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001355 qemu_flush_or_purge_queued_packets(nc, true);
1356 }
1357 }
1358}
1359
aliguori63a01ef2008-10-31 19:10:00 +00001360void net_cleanup(void)
1361{
Jason Wang1ceef9f2013-01-30 19:12:28 +08001362 NetClientState *nc;
aliguori63a01ef2008-10-31 19:10:00 +00001363
Jason Wang1ceef9f2013-01-30 19:12:28 +08001364 /* We may del multiple entries during qemu_del_net_client(),
1365 * so QTAILQ_FOREACH_SAFE() is also not safe here.
1366 */
1367 while (!QTAILQ_EMPTY(&net_clients)) {
1368 nc = QTAILQ_FIRST(&net_clients);
Eric Blakef394b2e2016-07-13 21:50:23 -06001369 if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
Jason Wang948ecf22013-01-30 19:12:24 +08001370 qemu_del_nic(qemu_get_nic(nc));
1371 } else {
1372 qemu_del_net_client(nc);
1373 }
Mark McLoughlin577c4af2009-10-08 19:58:28 +01001374 }
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001375
1376 qemu_del_vm_change_state_handler(net_change_state_entry);
aliguori63a01ef2008-10-31 19:10:00 +00001377}
1378
Markus Armbruster668680f2010-02-11 14:44:58 +01001379void net_check_clients(void)
aliguori63a01ef2008-10-31 19:10:00 +00001380{
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001381 NetClientState *nc;
Peter Maydell48e2faf2011-05-20 16:50:01 +01001382 int i;
aliguori63a01ef2008-10-31 19:10:00 +00001383
Stefan Hajnoczi81017642012-07-24 16:35:07 +01001384 net_hub_check_clients();
Tristan Gingoldac60cc12011-03-15 14:20:54 +01001385
Stefan Hajnoczi35277d12012-07-24 16:35:14 +01001386 QTAILQ_FOREACH(nc, &net_clients, next) {
1387 if (!nc->peer) {
Alistair Francis8297be82017-09-11 12:52:53 -07001388 warn_report("%s %s has no peer",
Alistair Francisb62e39b2017-09-11 12:52:56 -07001389 nc->info->type == NET_CLIENT_DRIVER_NIC
1390 ? "nic" : "netdev",
1391 nc->name);
Markus Armbrusterefe32fd2010-02-11 14:45:00 +01001392 }
1393 }
Peter Maydell48e2faf2011-05-20 16:50:01 +01001394
1395 /* Check that all NICs requested via -net nic actually got created.
1396 * NICs created via -device don't need to be checked here because
1397 * they are always instantiated.
1398 */
1399 for (i = 0; i < MAX_NICS; i++) {
1400 NICInfo *nd = &nd_table[i];
1401 if (nd->used && !nd->instantiated) {
Alistair Francis8297be82017-09-11 12:52:53 -07001402 warn_report("requested NIC (%s, model %s) "
1403 "was not created (not supported by this machine?)",
1404 nd->name ? nd->name : "anonymous",
1405 nd->model ? nd->model : "unspecified");
Peter Maydell48e2faf2011-05-20 16:50:01 +01001406 }
1407 }
aliguori63a01ef2008-10-31 19:10:00 +00001408}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001409
Markus Armbruster28d0de72015-03-13 13:35:14 +01001410static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001411{
Thomas Huth34f708b2018-02-21 11:18:30 +01001412 return net_client_init(opts, false, errp);
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001413}
1414
Markus Armbruster28d0de72015-03-13 13:35:14 +01001415static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001416{
Thomas Huth34f708b2018-02-21 11:18:30 +01001417 return net_client_init(opts, true, errp);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001418}
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001419
Thomas Huth78cd6f72018-02-21 11:18:36 +01001420/* For the convenience "--nic" parameter */
1421static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
1422{
1423 char *mac, *nd_id;
1424 int idx, ret;
1425 NICInfo *ni;
1426 const char *type;
1427
1428 type = qemu_opt_get(opts, "type");
1429 if (type && g_str_equal(type, "none")) {
1430 return 0; /* Nothing to do, default_net is cleared in vl.c */
1431 }
1432
1433 idx = nic_get_free_idx();
1434 if (idx == -1 || nb_nics >= MAX_NICS) {
1435 error_setg(errp, "no more on-board/default NIC slots available");
Mark McLoughlinf6b134a2009-10-08 19:58:27 +01001436 return -1;
1437 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001438
Thomas Huth78cd6f72018-02-21 11:18:36 +01001439 if (!type) {
1440 qemu_opt_set(opts, "type", "user", &error_abort);
1441 }
1442
1443 ni = &nd_table[idx];
1444 memset(ni, 0, sizeof(*ni));
1445 ni->model = qemu_opt_get_del(opts, "model");
1446
1447 /* Create an ID if the user did not specify one */
1448 nd_id = g_strdup(qemu_opts_id(opts));
1449 if (!nd_id) {
Thomas Huth0561dfa2020-05-18 09:43:52 +02001450 nd_id = g_strdup_printf("__org.qemu.nic%i", idx);
Thomas Huth78cd6f72018-02-21 11:18:36 +01001451 qemu_opts_set_id(opts, nd_id);
1452 }
1453
1454 /* Handle MAC address */
1455 mac = qemu_opt_get_del(opts, "mac");
1456 if (mac) {
1457 ret = net_parse_macaddr(ni->macaddr.a, mac);
1458 g_free(mac);
1459 if (ret) {
1460 error_setg(errp, "invalid syntax for ethernet address");
Thomas Huth9d946192018-04-30 09:26:45 +02001461 goto out;
Thomas Huth78cd6f72018-02-21 11:18:36 +01001462 }
1463 if (is_multicast_ether_addr(ni->macaddr.a)) {
1464 error_setg(errp, "NIC cannot have multicast MAC address");
Thomas Huth9d946192018-04-30 09:26:45 +02001465 ret = -1;
1466 goto out;
Thomas Huth78cd6f72018-02-21 11:18:36 +01001467 }
1468 }
1469 qemu_macaddr_default_if_unset(&ni->macaddr);
1470
1471 ret = net_client_init(opts, true, errp);
1472 if (ret == 0) {
1473 ni->netdev = qemu_find_netdev(nd_id);
1474 ni->used = true;
1475 nb_nics++;
1476 }
1477
Thomas Huth9d946192018-04-30 09:26:45 +02001478out:
Thomas Huth78cd6f72018-02-21 11:18:36 +01001479 g_free(nd_id);
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01001480 return ret;
1481}
1482
Thomas Huth34f708b2018-02-21 11:18:30 +01001483int net_init_clients(Error **errp)
aliguori63a01ef2008-10-31 19:10:00 +00001484{
Michael S. Tsirkinca77d852014-09-04 11:39:13 +03001485 net_change_state_entry =
1486 qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
1487
Stefan Hajnoczi94878992012-07-24 16:35:12 +01001488 QTAILQ_INIT(&net_clients);
aliguori63a01ef2008-10-31 19:10:00 +00001489
Markus Armbruster28d0de72015-03-13 13:35:14 +01001490 if (qemu_opts_foreach(qemu_find_opts("netdev"),
Thomas Huth34f708b2018-02-21 11:18:30 +01001491 net_init_netdev, NULL, errp)) {
aliguori63a01ef2008-10-31 19:10:00 +00001492 return -1;
Markus Armbrustera4c73672015-03-13 11:07:24 +01001493 }
aliguori63a01ef2008-10-31 19:10:00 +00001494
Thomas Huth78cd6f72018-02-21 11:18:36 +01001495 if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, errp)) {
1496 return -1;
1497 }
1498
Thomas Huth34f708b2018-02-21 11:18:30 +01001499 if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, errp)) {
aliguori63a01ef2008-10-31 19:10:00 +00001500 return -1;
1501 }
1502
aliguori63a01ef2008-10-31 19:10:00 +00001503 return 0;
1504}
1505
1506int net_client_parse(QemuOptsList *opts_list, const char *optarg)
1507{
Markus Armbruster70b94332015-02-13 12:50:26 +01001508 if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
aliguori63a01ef2008-10-31 19:10:00 +00001509 return -1;
1510 }
1511
aliguori63a01ef2008-10-31 19:10:00 +00001512 return 0;
1513}
Jason Wang7fc8d912012-03-05 11:08:50 +08001514
1515/* From FreeBSD */
1516/* XXX: optimize */
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001517uint32_t net_crc32(const uint8_t *p, int len)
Jason Wang7fc8d912012-03-05 11:08:50 +08001518{
1519 uint32_t crc;
1520 int carry, i, j;
1521 uint8_t b;
1522
1523 crc = 0xffffffff;
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001524 for (i = 0; i < len; i++) {
1525 b = *p++;
Jason Wang7fc8d912012-03-05 11:08:50 +08001526 for (j = 0; j < 8; j++) {
1527 carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
1528 crc <<= 1;
1529 b >>= 1;
1530 if (carry) {
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001531 crc = ((crc ^ POLYNOMIAL_BE) | carry);
Jason Wang7fc8d912012-03-05 11:08:50 +08001532 }
1533 }
1534 }
Mark Cave-Aylandeaba8f32017-12-15 18:41:43 +00001535
1536 return crc;
1537}
1538
Mark Cave-Aylandf1a7deb2017-12-15 18:41:44 +00001539uint32_t net_crc32_le(const uint8_t *p, int len)
1540{
1541 uint32_t crc;
1542 int carry, i, j;
1543 uint8_t b;
1544
1545 crc = 0xffffffff;
1546 for (i = 0; i < len; i++) {
1547 b = *p++;
1548 for (j = 0; j < 8; j++) {
1549 carry = (crc & 0x1) ^ (b & 0x01);
1550 crc >>= 1;
1551 b >>= 1;
1552 if (carry) {
1553 crc ^= POLYNOMIAL_LE;
1554 }
1555 }
1556 }
1557
1558 return crc;
1559}
1560
Paolo Bonzini4d454572012-11-26 16:03:42 +01001561QemuOptsList qemu_netdev_opts = {
1562 .name = "netdev",
1563 .implied_opt_name = "type",
1564 .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
1565 .desc = {
1566 /*
1567 * no elements => accept any params
1568 * validation will happen later
1569 */
1570 { /* end of list */ }
1571 },
1572};
1573
Thomas Huth78cd6f72018-02-21 11:18:36 +01001574QemuOptsList qemu_nic_opts = {
1575 .name = "nic",
1576 .implied_opt_name = "type",
1577 .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
1578 .desc = {
1579 /*
1580 * no elements => accept any params
1581 * validation will happen later
1582 */
1583 { /* end of list */ }
1584 },
1585};
1586
Paolo Bonzini4d454572012-11-26 16:03:42 +01001587QemuOptsList qemu_net_opts = {
1588 .name = "net",
1589 .implied_opt_name = "type",
1590 .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
1591 .desc = {
1592 /*
1593 * no elements => accept any params
1594 * validation will happen later
1595 */
1596 { /* end of list */ }
1597 },
1598};
Zhang Chen16a3df42016-05-13 15:35:19 +08001599
1600void net_socket_rs_init(SocketReadState *rs,
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001601 SocketReadStateFinalize *finalize,
1602 bool vnet_hdr)
Zhang Chen16a3df42016-05-13 15:35:19 +08001603{
1604 rs->state = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001605 rs->vnet_hdr = vnet_hdr;
Zhang Chen16a3df42016-05-13 15:35:19 +08001606 rs->index = 0;
1607 rs->packet_len = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001608 rs->vnet_hdr_len = 0;
Zhang Chen16a3df42016-05-13 15:35:19 +08001609 memset(rs->buf, 0, sizeof(rs->buf));
1610 rs->finalize = finalize;
1611}
1612
1613/*
1614 * Returns
Zhang Chene9e0a582016-08-18 11:23:25 +08001615 * 0: success
1616 * -1: error occurs
Zhang Chen16a3df42016-05-13 15:35:19 +08001617 */
1618int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
1619{
1620 unsigned int l;
1621
1622 while (size > 0) {
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001623 /* Reassemble a packet from the network.
1624 * 0 = getting length.
1625 * 1 = getting vnet header length.
1626 * 2 = getting data.
1627 */
1628 switch (rs->state) {
Zhang Chen16a3df42016-05-13 15:35:19 +08001629 case 0:
1630 l = 4 - rs->index;
1631 if (l > size) {
1632 l = size;
1633 }
1634 memcpy(rs->buf + rs->index, buf, l);
1635 buf += l;
1636 size -= l;
1637 rs->index += l;
1638 if (rs->index == 4) {
1639 /* got length */
1640 rs->packet_len = ntohl(*(uint32_t *)rs->buf);
1641 rs->index = 0;
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001642 if (rs->vnet_hdr) {
1643 rs->state = 1;
1644 } else {
1645 rs->state = 2;
1646 rs->vnet_hdr_len = 0;
1647 }
Zhang Chen16a3df42016-05-13 15:35:19 +08001648 }
1649 break;
1650 case 1:
Zhang Chen3cde5ea2017-07-04 14:53:46 +08001651 l = 4 - rs->index;
1652 if (l > size) {
1653 l = size;
1654 }
1655 memcpy(rs->buf + rs->index, buf, l);
1656 buf += l;
1657 size -= l;
1658 rs->index += l;
1659 if (rs->index == 4) {
1660 /* got vnet header length */
1661 rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
1662 rs->index = 0;
1663 rs->state = 2;
1664 }
1665 break;
1666 case 2:
Zhang Chen16a3df42016-05-13 15:35:19 +08001667 l = rs->packet_len - rs->index;
1668 if (l > size) {
1669 l = size;
1670 }
1671 if (rs->index + l <= sizeof(rs->buf)) {
1672 memcpy(rs->buf + rs->index, buf, l);
1673 } else {
1674 fprintf(stderr, "serious error: oversized packet received,"
1675 "connection terminated.\n");
1676 rs->index = rs->state = 0;
1677 return -1;
1678 }
1679
1680 rs->index += l;
1681 buf += l;
1682 size -= l;
1683 if (rs->index >= rs->packet_len) {
1684 rs->index = 0;
1685 rs->state = 0;
Daniel P. Berrangee79cd402016-11-04 15:46:33 +00001686 assert(rs->finalize);
1687 rs->finalize(rs);
Zhang Chen16a3df42016-05-13 15:35:19 +08001688 }
1689 break;
1690 }
1691 }
Zhang Chene9e0a582016-08-18 11:23:25 +08001692
1693 assert(size == 0);
Zhang Chen16a3df42016-05-13 15:35:19 +08001694 return 0;
1695}