blob: c5cbeaa7a2bef4b98e386eca889249feb79f8f5d [file] [log] [blame]
Mark McLoughlin5281d752009-10-22 17:49:07 +01001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
Peter Maydell2744d922016-01-29 17:50:00 +000026#include "qemu/osdep.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020027#include "tap_int.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010028
Mark McLoughlin5281d752009-10-22 17:49:07 +010029
Mark McLoughlin5281d752009-10-22 17:49:07 +010030#include <sys/ioctl.h>
Mark McLoughlin5281d752009-10-22 17:49:07 +010031#include <sys/wait.h>
Alexander Graf71f4eff2009-10-30 22:27:00 +010032#include <sys/socket.h>
Mark McLoughlin5281d752009-10-22 17:49:07 +010033#include <net/if.h>
34
Bin Meng969e50b2021-03-17 14:26:29 +080035#include "net/eth.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020037#include "clients.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010038#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/sysemu.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010040#include "qapi/error.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010041#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020042#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010043#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020044#include "qemu/main-loop.h"
Brijesh Singhd5428002018-04-06 13:51:25 -050045#include "qemu/sockets.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010046
Paolo Bonzini1422e322012-10-24 08:43:34 +020047#include "net/tap.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010048
Paolo Bonzini0d09e412013-02-05 17:06:20 +010049#include "net/vhost_net.h"
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +020050
Mark McLoughlin5281d752009-10-22 17:49:07 +010051typedef struct TAPState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010052 NetClientState nc;
Mark McLoughlin5281d752009-10-22 17:49:07 +010053 int fd;
54 char down_script[1024];
55 char down_script_arg[128];
Scott Feldmand32fcad2013-03-18 11:43:44 -070056 uint8_t buf[NET_BUFSIZE];
Jason Wangec45f082013-01-30 19:12:20 +080057 bool read_poll;
58 bool write_poll;
59 bool using_vnet_hdr;
60 bool has_ufo;
Jason Wang16dbaf92013-01-30 19:12:32 +080061 bool enabled;
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +020062 VHostNetState *vhost_net;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +030063 unsigned host_vnet_hdr_len;
Marc-André Lureau9e32ff32016-07-11 16:48:47 +020064 Notifier exit;
Mark McLoughlin5281d752009-10-22 17:49:07 +010065} TAPState;
66
Markus Armbrusterac4fcf52015-05-15 13:58:57 +020067static void launch_script(const char *setup_script, const char *ifname,
68 int fd, Error **errp);
Mark McLoughlin5281d752009-10-22 17:49:07 +010069
Mark McLoughlin5281d752009-10-22 17:49:07 +010070static void tap_send(void *opaque);
71static void tap_writable(void *opaque);
72
73static void tap_update_fd_handler(TAPState *s)
74{
Fam Zheng82e1cc42015-06-04 14:45:18 +080075 qemu_set_fd_handler(s->fd,
76 s->read_poll && s->enabled ? tap_send : NULL,
77 s->write_poll && s->enabled ? tap_writable : NULL,
78 s);
Mark McLoughlin5281d752009-10-22 17:49:07 +010079}
80
Jason Wangec45f082013-01-30 19:12:20 +080081static void tap_read_poll(TAPState *s, bool enable)
Mark McLoughlin5281d752009-10-22 17:49:07 +010082{
Jason Wangec45f082013-01-30 19:12:20 +080083 s->read_poll = enable;
Mark McLoughlin5281d752009-10-22 17:49:07 +010084 tap_update_fd_handler(s);
85}
86
Jason Wangec45f082013-01-30 19:12:20 +080087static void tap_write_poll(TAPState *s, bool enable)
Mark McLoughlin5281d752009-10-22 17:49:07 +010088{
Jason Wangec45f082013-01-30 19:12:20 +080089 s->write_poll = enable;
Mark McLoughlin5281d752009-10-22 17:49:07 +010090 tap_update_fd_handler(s);
91}
92
93static void tap_writable(void *opaque)
94{
95 TAPState *s = opaque;
96
Jason Wangec45f082013-01-30 19:12:20 +080097 tap_write_poll(s, false);
Mark McLoughlin5281d752009-10-22 17:49:07 +010098
Mark McLoughlin3e35ba92009-11-25 18:49:04 +000099 qemu_flush_queued_packets(&s->nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100100}
101
102static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
103{
104 ssize_t len;
105
106 do {
107 len = writev(s->fd, iov, iovcnt);
108 } while (len == -1 && errno == EINTR);
109
110 if (len == -1 && errno == EAGAIN) {
Jason Wangec45f082013-01-30 19:12:20 +0800111 tap_write_poll(s, true);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100112 return 0;
113 }
114
115 return len;
116}
117
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100118static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100119 int iovcnt)
120{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000121 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100122 const struct iovec *iovp = iov;
123 struct iovec iov_copy[iovcnt + 1];
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300124 struct virtio_net_hdr_mrg_rxbuf hdr = { };
Mark McLoughlin5281d752009-10-22 17:49:07 +0100125
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300126 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100127 iov_copy[0].iov_base = &hdr;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300128 iov_copy[0].iov_len = s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100129 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
130 iovp = iov_copy;
131 iovcnt++;
132 }
133
134 return tap_write_packet(s, iovp, iovcnt);
135}
136
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100137static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100138{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000139 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100140 struct iovec iov[2];
141 int iovcnt = 0;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300142 struct virtio_net_hdr_mrg_rxbuf hdr = { };
Mark McLoughlin5281d752009-10-22 17:49:07 +0100143
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300144 if (s->host_vnet_hdr_len) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100145 iov[iovcnt].iov_base = &hdr;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300146 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100147 iovcnt++;
148 }
149
150 iov[iovcnt].iov_base = (char *)buf;
151 iov[iovcnt].iov_len = size;
152 iovcnt++;
153
154 return tap_write_packet(s, iov, iovcnt);
155}
156
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100157static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100158{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000159 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100160 struct iovec iov[1];
161
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300162 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000163 return tap_receive_raw(nc, buf, size);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100164 }
165
166 iov[0].iov_base = (char *)buf;
167 iov[0].iov_len = size;
168
169 return tap_write_packet(s, iov, 1);
170}
171
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100172#ifndef __sun__
173ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100174{
175 return read(tapfd, buf, maxlen);
176}
177#endif
178
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100179static void tap_send_completed(NetClientState *nc, ssize_t len)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100180{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000181 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Jason Wangec45f082013-01-30 19:12:20 +0800182 tap_read_poll(s, true);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100183}
184
185static void tap_send(void *opaque)
186{
187 TAPState *s = opaque;
188 int size;
Wangkai (Kevin,C)756ae782014-07-18 09:33:42 +0000189 int packets = 0;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100190
Fam Zhenga90a7422015-06-04 14:45:17 +0800191 while (true) {
Mark McLoughlin5819c912009-10-27 18:16:39 +0000192 uint8_t *buf = s->buf;
Bin Meng969e50b2021-03-17 14:26:29 +0800193 uint8_t min_pkt[ETH_ZLEN];
194 size_t min_pktsz = sizeof(min_pkt);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100195
Mark McLoughlin5819c912009-10-27 18:16:39 +0000196 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
197 if (size <= 0) {
198 break;
199 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100200
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300201 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
202 buf += s->host_vnet_hdr_len;
203 size -= s->host_vnet_hdr_len;
Mark McLoughlin5819c912009-10-27 18:16:39 +0000204 }
205
Jason Wangbc38e312021-04-23 11:18:03 +0800206 if (net_peer_needs_padding(&s->nc)) {
Bin Meng969e50b2021-03-17 14:26:29 +0800207 if (eth_pad_short_frame(min_pkt, &min_pktsz, buf, size)) {
208 buf = min_pkt;
209 size = min_pktsz;
210 }
211 }
212
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000213 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
Mark McLoughlin5819c912009-10-27 18:16:39 +0000214 if (size == 0) {
Jason Wangec45f082013-01-30 19:12:20 +0800215 tap_read_poll(s, false);
Stefan Hajnoczi68e5ec62014-03-08 16:00:43 +0100216 break;
217 } else if (size < 0) {
218 break;
Mark McLoughlin5819c912009-10-27 18:16:39 +0000219 }
Wangkai (Kevin,C)756ae782014-07-18 09:33:42 +0000220
221 /*
222 * When the host keeps receiving more packets while tap_send() is
223 * running we can hog the QEMU global mutex. Limit the number of
224 * packets that are processed per tap_send() callback to prevent
225 * stalling the guest.
226 */
227 packets++;
228 if (packets >= 50) {
229 break;
230 }
Stefan Hajnoczi68e5ec62014-03-08 16:00:43 +0100231 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100232}
233
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100234static bool tap_has_ufo(NetClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100235{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000236 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100237
Eric Blakef394b2e2016-07-13 21:50:23 -0600238 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100239
240 return s->has_ufo;
241}
242
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100243static bool tap_has_vnet_hdr(NetClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100244{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000245 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100246
Eric Blakef394b2e2016-07-13 21:50:23 -0600247 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100248
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300249 return !!s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100250}
251
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100252static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300253{
254 TAPState *s = DO_UPCAST(TAPState, nc, nc);
255
Eric Blakef394b2e2016-07-13 21:50:23 -0600256 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300257
Vincenzo Maffionee96dfd12014-02-06 17:02:15 +0100258 return !!tap_probe_vnet_hdr_len(s->fd, len);
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300259}
260
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100261static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300262{
263 TAPState *s = DO_UPCAST(TAPState, nc, nc);
264
Eric Blakef394b2e2016-07-13 21:50:23 -0600265 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300266 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
Yuri Benditovichfbbdbdd2020-05-08 15:59:30 +0300267 len == sizeof(struct virtio_net_hdr) ||
268 len == sizeof(struct virtio_net_hdr_v1_hash));
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300269
270 tap_fd_set_vnet_hdr_len(s->fd, len);
271 s->host_vnet_hdr_len = len;
272}
273
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100274static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100275{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000276 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100277
Eric Blakef394b2e2016-07-13 21:50:23 -0600278 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300279 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100280
281 s->using_vnet_hdr = using_vnet_hdr;
282}
283
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200284static int tap_set_vnet_le(NetClientState *nc, bool is_le)
285{
286 TAPState *s = DO_UPCAST(TAPState, nc, nc);
287
288 return tap_fd_set_vnet_le(s->fd, is_le);
289}
290
291static int tap_set_vnet_be(NetClientState *nc, bool is_be)
292{
293 TAPState *s = DO_UPCAST(TAPState, nc, nc);
294
295 return tap_fd_set_vnet_be(s->fd, is_be);
296}
297
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100298static void tap_set_offload(NetClientState *nc, int csum, int tso4,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100299 int tso6, int ecn, int ufo)
300{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000301 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Michael S. Tsirkin27a63752010-10-31 19:06:47 +0200302 if (s->fd < 0) {
303 return;
304 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100305
Michael S. Tsirkin27a63752010-10-31 19:06:47 +0200306 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100307}
308
Marc-André Lureau9e32ff32016-07-11 16:48:47 +0200309static void tap_exit_notify(Notifier *notifier, void *data)
310{
311 TAPState *s = container_of(notifier, TAPState, exit);
312 Error *err = NULL;
313
314 if (s->down_script[0]) {
315 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
316 if (err) {
317 error_report_err(err);
318 }
319 }
320}
321
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100322static void tap_cleanup(NetClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100323{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000324 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100325
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200326 if (s->vhost_net) {
327 vhost_net_cleanup(s->vhost_net);
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400328 g_free(s->vhost_net);
Michael S. Tsirkin43849422010-10-27 20:03:43 +0200329 s->vhost_net = NULL;
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200330 }
331
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000332 qemu_purge_queued_packets(nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100333
Marc-André Lureau9e32ff32016-07-11 16:48:47 +0200334 tap_exit_notify(&s->exit, NULL);
335 qemu_remove_exit_notifier(&s->exit);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100336
Jason Wangec45f082013-01-30 19:12:20 +0800337 tap_read_poll(s, false);
338 tap_write_poll(s, false);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100339 close(s->fd);
Michael S. Tsirkin27a63752010-10-31 19:06:47 +0200340 s->fd = -1;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100341}
342
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100343static void tap_poll(NetClientState *nc, bool enable)
Michael S. Tsirkinceb69612009-12-24 14:46:29 +0200344{
345 TAPState *s = DO_UPCAST(TAPState, nc, nc);
346 tap_read_poll(s, enable);
347 tap_write_poll(s, enable);
348}
349
Andrew Melnychenko8f364e32021-05-14 14:48:30 +0300350static bool tap_set_steering_ebpf(NetClientState *nc, int prog_fd)
351{
352 TAPState *s = DO_UPCAST(TAPState, nc, nc);
353 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
354
355 return tap_fd_set_steering_ebpf(s->fd, prog_fd) == 0;
356}
357
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100358int tap_get_fd(NetClientState *nc)
Michael S. Tsirkin95d528a2010-03-17 13:07:50 +0200359{
360 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -0600361 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkin95d528a2010-03-17 13:07:50 +0200362 return s->fd;
363}
364
Mark McLoughlin5281d752009-10-22 17:49:07 +0100365/* fd support */
366
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000367static NetClientInfo net_tap_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600368 .type = NET_CLIENT_DRIVER_TAP,
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000369 .size = sizeof(TAPState),
370 .receive = tap_receive,
371 .receive_raw = tap_receive_raw,
372 .receive_iov = tap_receive_iov,
Michael S. Tsirkinceb69612009-12-24 14:46:29 +0200373 .poll = tap_poll,
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000374 .cleanup = tap_cleanup,
Vincenzo Maffione2e753bc2014-02-06 17:02:17 +0100375 .has_ufo = tap_has_ufo,
376 .has_vnet_hdr = tap_has_vnet_hdr,
377 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
378 .using_vnet_hdr = tap_using_vnet_hdr,
379 .set_offload = tap_set_offload,
380 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200381 .set_vnet_le = tap_set_vnet_le,
382 .set_vnet_be = tap_set_vnet_be,
Andrew Melnychenko8f364e32021-05-14 14:48:30 +0300383 .set_steering_ebpf = tap_set_steering_ebpf,
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000384};
385
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100386static TAPState *net_tap_fd_init(NetClientState *peer,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100387 const char *model,
388 const char *name,
389 int fd,
390 int vnet_hdr)
391{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100392 NetClientState *nc;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100393 TAPState *s;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100394
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100395 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000396
397 s = DO_UPCAST(TAPState, nc, nc);
398
Mark McLoughlin5281d752009-10-22 17:49:07 +0100399 s->fd = fd;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300400 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
Jason Wangec45f082013-01-30 19:12:20 +0800401 s->using_vnet_hdr = false;
Mark McLoughlin9c282712009-10-22 17:49:16 +0100402 s->has_ufo = tap_probe_has_ufo(s->fd);
Jason Wang16dbaf92013-01-30 19:12:32 +0800403 s->enabled = true;
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000404 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
Michael S. Tsirkin58ddcd52012-11-13 12:23:23 +0200405 /*
406 * Make sure host header length is set correctly in tap:
407 * it might have been modified by another instance of qemu.
408 */
409 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
410 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
411 }
Jason Wangec45f082013-01-30 19:12:20 +0800412 tap_read_poll(s, true);
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200413 s->vhost_net = NULL;
Marc-André Lureau9e32ff32016-07-11 16:48:47 +0200414
415 s->exit.notify = tap_exit_notify;
416 qemu_add_exit_notifier(&s->exit);
417
Mark McLoughlin5281d752009-10-22 17:49:07 +0100418 return s;
419}
420
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200421static void launch_script(const char *setup_script, const char *ifname,
422 int fd, Error **errp)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100423{
Mark McLoughlin5281d752009-10-22 17:49:07 +0100424 int pid, status;
425 char *args[3];
426 char **parg;
427
Mark McLoughlin5281d752009-10-22 17:49:07 +0100428 /* try to launch network script */
429 pid = fork();
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200430 if (pid < 0) {
431 error_setg_errno(errp, errno, "could not launch network script %s",
432 setup_script);
433 return;
434 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100435 if (pid == 0) {
436 int open_max = sysconf(_SC_OPEN_MAX), i;
437
Pankaj Gupta13a12f82014-03-12 22:24:27 +0530438 for (i = 3; i < open_max; i++) {
439 if (i != fd) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100440 close(i);
441 }
442 }
443 parg = args;
444 *parg++ = (char *)setup_script;
445 *parg++ = (char *)ifname;
Blue Swirl9678d952010-04-25 18:35:52 +0000446 *parg = NULL;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100447 execv(setup_script, args);
448 _exit(1);
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200449 } else {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100450 while (waitpid(pid, &status, 0) != pid) {
451 /* loop */
452 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100453
454 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200455 return;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100456 }
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200457 error_setg(errp, "network script %s failed with status %d",
458 setup_script, status);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100459 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100460}
461
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500462static int recv_fd(int c)
463{
464 int fd;
465 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
466 struct msghdr msg = {
467 .msg_control = msgbuf,
468 .msg_controllen = sizeof(msgbuf),
469 };
470 struct cmsghdr *cmsg;
471 struct iovec iov;
472 uint8_t req[1];
473 ssize_t len;
474
475 cmsg = CMSG_FIRSTHDR(&msg);
476 cmsg->cmsg_level = SOL_SOCKET;
477 cmsg->cmsg_type = SCM_RIGHTS;
478 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
479 msg.msg_controllen = cmsg->cmsg_len;
480
481 iov.iov_base = req;
482 iov.iov_len = sizeof(req);
483
484 msg.msg_iov = &iov;
485 msg.msg_iovlen = 1;
486
487 len = recvmsg(c, &msg, 0);
488 if (len > 0) {
489 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
490 return fd;
491 }
492
493 return len;
494}
495
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200496static int net_bridge_run_helper(const char *helper, const char *bridge,
497 Error **errp)
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500498{
499 sigset_t oldmask, mask;
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200500 g_autofree char *default_helper = NULL;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500501 int pid, status;
502 char *args[5];
503 char **parg;
504 int sv[2];
505
506 sigemptyset(&mask);
507 sigaddset(&mask, SIGCHLD);
508 sigprocmask(SIG_BLOCK, &mask, &oldmask);
509
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200510 if (!helper) {
511 helper = default_helper = get_relocated_path(DEFAULT_BRIDGE_HELPER);
512 }
513
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500514 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200515 error_setg_errno(errp, errno, "socketpair() failed");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500516 return -1;
517 }
518
519 /* try to launch bridge helper */
520 pid = fork();
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200521 if (pid < 0) {
522 error_setg_errno(errp, errno, "Can't fork bridge helper");
523 return -1;
524 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500525 if (pid == 0) {
526 int open_max = sysconf(_SC_OPEN_MAX), i;
Prasad J Pandit389abe12019-07-23 16:17:54 +0530527 char *fd_buf = NULL;
528 char *br_buf = NULL;
529 char *helper_cmd = NULL;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500530
Pankaj Gupta13a12f82014-03-12 22:24:27 +0530531 for (i = 3; i < open_max; i++) {
532 if (i != sv[1]) {
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500533 close(i);
534 }
535 }
536
Prasad J Pandit389abe12019-07-23 16:17:54 +0530537 fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500538
539 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
540 /* assume helper is a command */
541
542 if (strstr(helper, "--br=") == NULL) {
Prasad J Pandit389abe12019-07-23 16:17:54 +0530543 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500544 }
545
Prasad J Pandit389abe12019-07-23 16:17:54 +0530546 helper_cmd = g_strdup_printf("%s %s %s %s", helper,
547 "--use-vnet", fd_buf, br_buf ? br_buf : "");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500548
549 parg = args;
550 *parg++ = (char *)"sh";
551 *parg++ = (char *)"-c";
552 *parg++ = helper_cmd;
553 *parg++ = NULL;
554
555 execv("/bin/sh", args);
Prasad J Pandit389abe12019-07-23 16:17:54 +0530556 g_free(helper_cmd);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500557 } else {
558 /* assume helper is just the executable path name */
559
Prasad J Pandit389abe12019-07-23 16:17:54 +0530560 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500561
562 parg = args;
563 *parg++ = (char *)helper;
564 *parg++ = (char *)"--use-vnet";
565 *parg++ = fd_buf;
566 *parg++ = br_buf;
567 *parg++ = NULL;
568
569 execv(helper, args);
570 }
Prasad J Pandit389abe12019-07-23 16:17:54 +0530571 g_free(fd_buf);
572 g_free(br_buf);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500573 _exit(1);
574
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200575 } else {
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500576 int fd;
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200577 int saved_errno;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500578
579 close(sv[1]);
580
581 do {
582 fd = recv_fd(sv[0]);
583 } while (fd == -1 && errno == EINTR);
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200584 saved_errno = errno;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500585
586 close(sv[0]);
587
588 while (waitpid(pid, &status, 0) != pid) {
589 /* loop */
590 }
591 sigprocmask(SIG_SETMASK, &oldmask, NULL);
592 if (fd < 0) {
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200593 error_setg_errno(errp, saved_errno,
594 "failed to recv file descriptor");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500595 return -1;
596 }
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200597 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
598 error_setg(errp, "bridge helper failed");
599 return -1;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500600 }
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200601 return fd;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500602 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500603}
604
KÅ‘vĂ¡gĂ³, ZoltĂ¡ncebea512016-07-13 21:50:12 -0600605int net_init_bridge(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200606 NetClientState *peer, Error **errp)
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500607{
Laszlo Ersekf79b51b2012-07-17 16:17:20 +0200608 const NetdevBridgeOptions *bridge;
609 const char *helper, *br;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500610 TAPState *s;
611 int fd, vnet_hdr;
612
Eric Blakef394b2e2016-07-13 21:50:23 -0600613 assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
614 bridge = &netdev->u.bridge;
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200615 helper = bridge->has_helper ? bridge->helper : NULL;
Laszlo Ersekf79b51b2012-07-17 16:17:20 +0200616 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
617
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200618 fd = net_bridge_run_helper(helper, br, errp);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500619 if (fd == -1) {
620 return -1;
621 }
622
Li Qiangab792372018-11-21 03:21:59 -0800623 qemu_set_nonblock(fd);
Daniel P. Berrangee7b347d2020-07-07 20:45:15 +0200624 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
625 if (vnet_hdr < 0) {
626 close(fd);
627 return -1;
628 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100629 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500630
Jason Wang56e6f592021-04-02 11:03:33 +0800631 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
632 br);
Jason Wangd89b4f82021-04-02 11:03:12 +0800633
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500634 return 0;
635}
636
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200637static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
638 const char *setup_script, char *ifname,
Markus Armbruster468dd822015-05-15 13:58:58 +0200639 size_t ifname_sz, int mq_required, Error **errp)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100640{
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200641 Error *err = NULL;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100642 int fd, vnet_hdr_required;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100643
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200644 if (tap->has_vnet_hdr) {
645 *vnet_hdr = tap->vnet_hdr;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100646 vnet_hdr_required = *vnet_hdr;
647 } else {
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200648 *vnet_hdr = 1;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100649 vnet_hdr_required = 0;
650 }
651
Jason Wang264986e2013-01-30 19:12:34 +0800652 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
Markus Armbruster468dd822015-05-15 13:58:58 +0200653 mq_required, errp));
Mark McLoughlin5281d752009-10-22 17:49:07 +0100654 if (fd < 0) {
655 return -1;
656 }
657
Mark McLoughlin5281d752009-10-22 17:49:07 +0100658 if (setup_script &&
659 setup_script[0] != '\0' &&
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200660 strcmp(setup_script, "no") != 0) {
661 launch_script(setup_script, ifname, fd, &err);
662 if (err) {
Markus Armbruster468dd822015-05-15 13:58:58 +0200663 error_propagate(errp, err);
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200664 close(fd);
665 return -1;
666 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100667 }
668
Mark McLoughlin5281d752009-10-22 17:49:07 +0100669 return fd;
670}
671
Jason Wang264986e2013-01-30 19:12:34 +0800672#define MAX_TAP_QUEUES 1024
673
Markus Armbruster445f1162015-05-15 13:58:56 +0200674static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
675 const char *model, const char *name,
676 const char *ifname, const char *script,
677 const char *downscript, const char *vhostfdname,
Jason Wangf9bb0c12021-04-02 11:05:20 +0800678 int vnet_hdr, int fd, Error **errp)
Jason Wang5193e5f2013-01-30 19:12:30 +0800679{
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100680 Error *err = NULL;
Markus Armbrusterda4a4ea2015-05-15 13:58:53 +0200681 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300682 int vhostfd;
Jason Wang5193e5f2013-01-30 19:12:30 +0800683
Markus Armbruster80b832c2015-05-15 13:58:55 +0200684 tap_set_sndbuf(s->fd, tap, &err);
685 if (err) {
Markus Armbruster445f1162015-05-15 13:58:56 +0200686 error_propagate(errp, err);
687 return;
Jason Wang5193e5f2013-01-30 19:12:30 +0800688 }
689
Jason Wang264986e2013-01-30 19:12:34 +0800690 if (tap->has_fd || tap->has_fds) {
Jason Wang56e6f592021-04-02 11:03:33 +0800691 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
Jason Wang5193e5f2013-01-30 19:12:30 +0800692 } else if (tap->has_helper) {
Jason Wang56e6f592021-04-02 11:03:33 +0800693 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
694 tap->helper);
Jason Wang5193e5f2013-01-30 19:12:30 +0800695 } else {
Jason Wang56e6f592021-04-02 11:03:33 +0800696 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
697 "ifname=%s,script=%s,downscript=%s", ifname, script,
698 downscript);
Jason Wangd89b4f82021-04-02 11:03:12 +0800699
Jason Wang5193e5f2013-01-30 19:12:30 +0800700 if (strcmp(downscript, "no") != 0) {
701 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
702 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
703 "%s", ifname);
704 }
705 }
706
707 if (tap->has_vhost ? tap->vhost :
708 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300709 VhostNetOptions options;
710
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300711 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300712 options.net_backend = &s->nc;
Jason Wang69e87b32016-07-06 09:57:55 +0800713 if (tap->has_poll_us) {
714 options.busyloop_timeout = tap->poll_us;
715 } else {
716 options.busyloop_timeout = 0;
717 }
Jason Wang5193e5f2013-01-30 19:12:30 +0800718
Paolo Bonzini3a2d44f2016-02-26 00:05:57 +0100719 if (vhostfdname) {
Laurent Vivier894022e2020-07-07 20:45:14 +0200720 int ret;
721
Kevin Wolf947e4742020-10-05 17:58:44 +0200722 vhostfd = monitor_fd_param(monitor_cur(), vhostfdname, &err);
Jason Wang5193e5f2013-01-30 19:12:30 +0800723 if (vhostfd == -1) {
Jay Zhou46d4d362018-03-02 17:04:44 +0800724 if (tap->has_vhostforce && tap->vhostforce) {
725 error_propagate(errp, err);
726 } else {
727 warn_report_err(err);
728 }
Markus Armbruster445f1162015-05-15 13:58:56 +0200729 return;
Jason Wang5193e5f2013-01-30 19:12:30 +0800730 }
Laurent Vivier894022e2020-07-07 20:45:14 +0200731 ret = qemu_try_set_nonblock(vhostfd);
732 if (ret < 0) {
733 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
734 name, fd);
735 return;
736 }
Jason Wang5193e5f2013-01-30 19:12:30 +0800737 } else {
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300738 vhostfd = open("/dev/vhost-net", O_RDWR);
739 if (vhostfd < 0) {
Jay Zhou46d4d362018-03-02 17:04:44 +0800740 if (tap->has_vhostforce && tap->vhostforce) {
741 error_setg_errno(errp, errno,
742 "tap: open vhost char device failed");
743 } else {
744 warn_report("tap: open vhost char device failed: %s",
745 strerror(errno));
746 }
Markus Armbruster445f1162015-05-15 13:58:56 +0200747 return;
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300748 }
Li Qiangab792372018-11-21 03:21:59 -0800749 qemu_set_nonblock(vhostfd);
Jason Wang5193e5f2013-01-30 19:12:30 +0800750 }
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300751 options.opaque = (void *)(uintptr_t)vhostfd;
Jason Wang6a756d12021-09-03 17:10:15 +0800752 options.nvqs = 2;
Jason Wang5193e5f2013-01-30 19:12:30 +0800753
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300754 s->vhost_net = vhost_net_init(&options);
Jason Wang5193e5f2013-01-30 19:12:30 +0800755 if (!s->vhost_net) {
Jay Zhou46d4d362018-03-02 17:04:44 +0800756 if (tap->has_vhostforce && tap->vhostforce) {
757 error_setg(errp, VHOST_NET_INIT_FAILED);
758 } else {
759 warn_report(VHOST_NET_INIT_FAILED);
760 }
Markus Armbruster445f1162015-05-15 13:58:56 +0200761 return;
Jason Wang5193e5f2013-01-30 19:12:30 +0800762 }
Paolo Bonzini3a2d44f2016-02-26 00:05:57 +0100763 } else if (vhostfdname) {
Jason Wang69e87b32016-07-06 09:57:55 +0800764 error_setg(errp, "vhostfd(s)= is not valid without vhost");
Jason Wang5193e5f2013-01-30 19:12:30 +0800765 }
Jason Wang5193e5f2013-01-30 19:12:30 +0800766}
767
Jason Wang264986e2013-01-30 19:12:34 +0800768static int get_fds(char *str, char *fds[], int max)
769{
770 char *ptr = str, *this;
771 size_t len = strlen(str);
772 int i = 0;
773
774 while (i < max && ptr < str + len) {
775 this = strchr(ptr, ':');
776
777 if (this == NULL) {
778 fds[i] = g_strdup(ptr);
779 } else {
780 fds[i] = g_strndup(ptr, this - ptr);
781 }
782
783 i++;
784 if (this == NULL) {
785 break;
786 } else {
787 ptr = this + 1;
788 }
789 }
790
791 return i;
792}
793
KÅ‘vĂ¡gĂ³, ZoltĂ¡ncebea512016-07-13 21:50:12 -0600794int net_init_tap(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200795 NetClientState *peer, Error **errp)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100796{
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200797 const NetdevTapOptions *tap;
Jason Wang264986e2013-01-30 19:12:34 +0800798 int fd, vnet_hdr = 0, i = 0, queues;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200799 /* for the no-fd, no-helper case */
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200800 const char *script;
801 const char *downscript;
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100802 Error *err = NULL;
Jason Wang264986e2013-01-30 19:12:34 +0800803 const char *vhostfdname;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200804 char ifname[128];
Laurent Vivier894022e2020-07-07 20:45:14 +0200805 int ret = 0;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200806
Eric Blakef394b2e2016-07-13 21:50:23 -0600807 assert(netdev->type == NET_CLIENT_DRIVER_TAP);
808 tap = &netdev->u.tap;
Jason Wang264986e2013-01-30 19:12:34 +0800809 queues = tap->has_queues ? tap->queues : 1;
810 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200811 script = tap->has_script ? tap->script : NULL;
812 downscript = tap->has_downscript ? tap->downscript : NULL;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200813
Thomas Huth442da402018-04-30 20:02:24 +0200814 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
Jason Wangce675a72013-02-21 11:05:56 +0800815 * For -netdev, peer is always NULL. */
816 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
Thomas Huth442da402018-04-30 20:02:24 +0200817 error_setg(errp, "Multiqueue tap cannot be used with hubs");
Jason Wangce675a72013-02-21 11:05:56 +0800818 return -1;
819 }
820
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200821 if (tap->has_fd) {
822 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
Jason Wang264986e2013-01-30 19:12:34 +0800823 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
Jason Wangc87826a2013-06-04 13:18:17 +0800824 tap->has_fds || tap->has_vhostfds) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200825 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
826 "helper=, queues=, fds=, and vhostfds= "
827 "are invalid with fd=");
Mark McLoughlin5281d752009-10-22 17:49:07 +0100828 return -1;
829 }
830
Kevin Wolf947e4742020-10-05 17:58:44 +0200831 fd = monitor_fd_param(monitor_cur(), tap->fd, errp);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100832 if (fd == -1) {
833 return -1;
834 }
835
Laurent Vivier894022e2020-07-07 20:45:14 +0200836 ret = qemu_try_set_nonblock(fd);
837 if (ret < 0) {
838 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
839 name, fd);
yuanjungongf012bec2020-11-19 17:25:32 +0800840 close(fd);
Laurent Vivier894022e2020-07-07 20:45:14 +0200841 return -1;
842 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100843
Daniel P. Berrangee7b347d2020-07-07 20:45:15 +0200844 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
845 if (vnet_hdr < 0) {
846 close(fd);
847 return -1;
848 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500849
Markus Armbruster445f1162015-05-15 13:58:56 +0200850 net_init_tap_one(tap, peer, "tap", name, NULL,
851 script, downscript,
Jason Wangf9bb0c12021-04-02 11:05:20 +0800852 vhostfdname, vnet_hdr, fd, &err);
Markus Armbruster445f1162015-05-15 13:58:56 +0200853 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200854 error_propagate(errp, err);
yuanjungongf012bec2020-11-19 17:25:32 +0800855 close(fd);
Jason Wang264986e2013-01-30 19:12:34 +0800856 return -1;
857 }
858 } else if (tap->has_fds) {
Peter Maydellfac7d7b2017-01-10 19:21:54 +0000859 char **fds;
860 char **vhost_fds;
Yunjian Wang323e7c12018-05-31 15:28:22 +0800861 int nfds = 0, nvhosts = 0;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500862
Jason Wang264986e2013-01-30 19:12:34 +0800863 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
864 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
Jason Wangc87826a2013-06-04 13:18:17 +0800865 tap->has_vhostfd) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200866 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
867 "helper=, queues=, and vhostfd= "
868 "are invalid with fds=");
Jason Wang264986e2013-01-30 19:12:34 +0800869 return -1;
870 }
871
Peter Maydellfac7d7b2017-01-10 19:21:54 +0000872 fds = g_new0(char *, MAX_TAP_QUEUES);
873 vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
874
Jason Wang264986e2013-01-30 19:12:34 +0800875 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
876 if (tap->has_vhostfds) {
877 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
878 if (nfds != nvhosts) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200879 error_setg(errp, "The number of fds passed does not match "
880 "the number of vhostfds passed");
Yunjian Wang323e7c12018-05-31 15:28:22 +0800881 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200882 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800883 }
884 }
885
886 for (i = 0; i < nfds; i++) {
Kevin Wolf947e4742020-10-05 17:58:44 +0200887 fd = monitor_fd_param(monitor_cur(), fds[i], errp);
Jason Wang264986e2013-01-30 19:12:34 +0800888 if (fd == -1) {
Yunjian Wang323e7c12018-05-31 15:28:22 +0800889 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200890 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800891 }
892
Laurent Vivier894022e2020-07-07 20:45:14 +0200893 ret = qemu_try_set_nonblock(fd);
894 if (ret < 0) {
895 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
896 name, fd);
897 goto free_fail;
898 }
Jason Wang264986e2013-01-30 19:12:34 +0800899
900 if (i == 0) {
Daniel P. Berrangee7b347d2020-07-07 20:45:15 +0200901 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
902 if (vnet_hdr < 0) {
Peter Foley41bcea72022-01-14 13:08:58 +0800903 ret = -1;
Daniel P. Berrangee7b347d2020-07-07 20:45:15 +0200904 goto free_fail;
905 }
906 } else if (vnet_hdr != tap_probe_vnet_hdr(fd, NULL)) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200907 error_setg(errp,
908 "vnet_hdr not consistent across given tap fds");
Yunjian Wang323e7c12018-05-31 15:28:22 +0800909 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200910 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800911 }
912
Markus Armbruster445f1162015-05-15 13:58:56 +0200913 net_init_tap_one(tap, peer, "tap", name, ifname,
914 script, downscript,
915 tap->has_vhostfds ? vhost_fds[i] : NULL,
Jason Wangf9bb0c12021-04-02 11:05:20 +0800916 vnet_hdr, fd, &err);
Markus Armbruster445f1162015-05-15 13:58:56 +0200917 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200918 error_propagate(errp, err);
Yunjian Wang323e7c12018-05-31 15:28:22 +0800919 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200920 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800921 }
922 }
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200923
924free_fail:
Yunjian Wang323e7c12018-05-31 15:28:22 +0800925 for (i = 0; i < nvhosts; i++) {
926 g_free(vhost_fds[i]);
927 }
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200928 for (i = 0; i < nfds; i++) {
929 g_free(fds[i]);
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200930 }
931 g_free(fds);
932 g_free(vhost_fds);
Yunjian Wang323e7c12018-05-31 15:28:22 +0800933 return ret;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200934 } else if (tap->has_helper) {
935 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
Jason Wangc87826a2013-06-04 13:18:17 +0800936 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200937 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
938 "queues=, and vhostfds= are invalid with helper=");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500939 return -1;
940 }
941
Alexey Kardashevskiy584613e2016-09-13 17:11:54 +1000942 fd = net_bridge_run_helper(tap->helper,
943 tap->has_br ?
944 tap->br : DEFAULT_BRIDGE_INTERFACE,
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200945 errp);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500946 if (fd == -1) {
947 return -1;
948 }
949
Li Qiangab792372018-11-21 03:21:59 -0800950 qemu_set_nonblock(fd);
Daniel P. Berrangee7b347d2020-07-07 20:45:15 +0200951 vnet_hdr = tap_probe_vnet_hdr(fd, errp);
952 if (vnet_hdr < 0) {
953 close(fd);
954 return -1;
955 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500956
Markus Armbruster445f1162015-05-15 13:58:56 +0200957 net_init_tap_one(tap, peer, "bridge", name, ifname,
958 script, downscript, vhostfdname,
Jason Wangf9bb0c12021-04-02 11:05:20 +0800959 vnet_hdr, fd, &err);
Markus Armbruster445f1162015-05-15 13:58:56 +0200960 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200961 error_propagate(errp, err);
Gonglei84f8f3d2014-11-02 13:37:17 +0800962 close(fd);
Jason Wang264986e2013-01-30 19:12:34 +0800963 return -1;
964 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100965 } else {
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200966 g_autofree char *default_script = NULL;
967 g_autofree char *default_downscript = NULL;
Jason Wangc87826a2013-06-04 13:18:17 +0800968 if (tap->has_vhostfds) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200969 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
Jason Wangc87826a2013-06-04 13:18:17 +0800970 return -1;
971 }
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200972
973 if (!script) {
974 script = default_script = get_relocated_path(DEFAULT_NETWORK_SCRIPT);
975 }
976 if (!downscript) {
Keqian Zhu99259902020-11-23 14:29:54 +0800977 downscript = default_downscript =
978 get_relocated_path(DEFAULT_NETWORK_DOWN_SCRIPT);
Paolo Bonzini63c4db42020-08-18 11:56:16 +0200979 }
Jason Wang264986e2013-01-30 19:12:34 +0800980
981 if (tap->has_ifname) {
982 pstrcpy(ifname, sizeof ifname, tap->ifname);
983 } else {
984 ifname[0] = '\0';
Juergen Lock929fe492009-11-20 23:23:03 +0100985 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500986
Jason Wang264986e2013-01-30 19:12:34 +0800987 for (i = 0; i < queues; i++) {
988 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
Markus Armbrustera3088172015-05-15 13:59:03 +0200989 ifname, sizeof ifname, queues > 1, errp);
Jason Wang264986e2013-01-30 19:12:34 +0800990 if (fd == -1) {
991 return -1;
992 }
993
994 if (queues > 1 && i == 0 && !tap->has_ifname) {
995 if (tap_fd_get_ifname(fd, ifname)) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200996 error_setg(errp, "Fail to get ifname");
Gonglei84f8f3d2014-11-02 13:37:17 +0800997 close(fd);
Jason Wang264986e2013-01-30 19:12:34 +0800998 return -1;
999 }
1000 }
1001
Markus Armbruster445f1162015-05-15 13:58:56 +02001002 net_init_tap_one(tap, peer, "tap", name, ifname,
1003 i >= 1 ? "no" : script,
1004 i >= 1 ? "no" : downscript,
Jason Wangf9bb0c12021-04-02 11:05:20 +08001005 vhostfdname, vnet_hdr, fd, &err);
Markus Armbruster445f1162015-05-15 13:58:56 +02001006 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +02001007 error_propagate(errp, err);
Gonglei84f8f3d2014-11-02 13:37:17 +08001008 close(fd);
Jason Wang264986e2013-01-30 19:12:34 +08001009 return -1;
1010 }
1011 }
Mark McLoughlin5281d752009-10-22 17:49:07 +01001012 }
1013
Jason Wang264986e2013-01-30 19:12:34 +08001014 return 0;
Mark McLoughlin5281d752009-10-22 17:49:07 +01001015}
Michael S. Tsirkinb2025542010-03-17 13:08:38 +02001016
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +01001017VHostNetState *tap_get_vhost_net(NetClientState *nc)
Michael S. Tsirkinb2025542010-03-17 13:08:38 +02001018{
1019 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -06001020 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkinb2025542010-03-17 13:08:38 +02001021 return s->vhost_net;
1022}
Jason Wang16dbaf92013-01-30 19:12:32 +08001023
1024int tap_enable(NetClientState *nc)
1025{
1026 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1027 int ret;
1028
1029 if (s->enabled) {
1030 return 0;
1031 } else {
1032 ret = tap_fd_enable(s->fd);
1033 if (ret == 0) {
1034 s->enabled = true;
1035 tap_update_fd_handler(s);
1036 }
1037 return ret;
1038 }
1039}
1040
1041int tap_disable(NetClientState *nc)
1042{
1043 TAPState *s = DO_UPCAST(TAPState, nc, nc);
1044 int ret;
1045
1046 if (s->enabled == 0) {
1047 return 0;
1048 } else {
1049 ret = tap_fd_disable(s->fd);
1050 if (ret == 0) {
1051 qemu_purge_queued_packets(nc);
1052 s->enabled = false;
1053 tap_update_fd_handler(s);
1054 }
1055 return ret;
1056 }
1057}