blob: 6207f61f84abd7735723adec4ad0f71fafe5fa10 [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
Paolo Bonzini1422e322012-10-24 08:43:34 +020035#include "net/net.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020036#include "clients.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010037#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/sysemu.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010039#include "qapi/error.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010040#include "qemu-common.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020041#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010042#include "qemu/error-report.h"
Markus Armbrusterdb725812019-08-12 07:23:50 +020043#include "qemu/main-loop.h"
Brijesh Singhd5428002018-04-06 13:51:25 -050044#include "qemu/sockets.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010045
Paolo Bonzini1422e322012-10-24 08:43:34 +020046#include "net/tap.h"
Mark McLoughlin5281d752009-10-22 17:49:07 +010047
Paolo Bonzini0d09e412013-02-05 17:06:20 +010048#include "net/vhost_net.h"
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +020049
Mark McLoughlin5281d752009-10-22 17:49:07 +010050typedef struct TAPState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010051 NetClientState nc;
Mark McLoughlin5281d752009-10-22 17:49:07 +010052 int fd;
53 char down_script[1024];
54 char down_script_arg[128];
Scott Feldmand32fcad2013-03-18 11:43:44 -070055 uint8_t buf[NET_BUFSIZE];
Jason Wangec45f082013-01-30 19:12:20 +080056 bool read_poll;
57 bool write_poll;
58 bool using_vnet_hdr;
59 bool has_ufo;
Jason Wang16dbaf92013-01-30 19:12:32 +080060 bool enabled;
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +020061 VHostNetState *vhost_net;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +030062 unsigned host_vnet_hdr_len;
Marc-André Lureau9e32ff32016-07-11 16:48:47 +020063 Notifier exit;
Mark McLoughlin5281d752009-10-22 17:49:07 +010064} TAPState;
65
Markus Armbrusterac4fcf52015-05-15 13:58:57 +020066static void launch_script(const char *setup_script, const char *ifname,
67 int fd, Error **errp);
Mark McLoughlin5281d752009-10-22 17:49:07 +010068
Mark McLoughlin5281d752009-10-22 17:49:07 +010069static void tap_send(void *opaque);
70static void tap_writable(void *opaque);
71
72static void tap_update_fd_handler(TAPState *s)
73{
Fam Zheng82e1cc42015-06-04 14:45:18 +080074 qemu_set_fd_handler(s->fd,
75 s->read_poll && s->enabled ? tap_send : NULL,
76 s->write_poll && s->enabled ? tap_writable : NULL,
77 s);
Mark McLoughlin5281d752009-10-22 17:49:07 +010078}
79
Jason Wangec45f082013-01-30 19:12:20 +080080static void tap_read_poll(TAPState *s, bool enable)
Mark McLoughlin5281d752009-10-22 17:49:07 +010081{
Jason Wangec45f082013-01-30 19:12:20 +080082 s->read_poll = enable;
Mark McLoughlin5281d752009-10-22 17:49:07 +010083 tap_update_fd_handler(s);
84}
85
Jason Wangec45f082013-01-30 19:12:20 +080086static void tap_write_poll(TAPState *s, bool enable)
Mark McLoughlin5281d752009-10-22 17:49:07 +010087{
Jason Wangec45f082013-01-30 19:12:20 +080088 s->write_poll = enable;
Mark McLoughlin5281d752009-10-22 17:49:07 +010089 tap_update_fd_handler(s);
90}
91
92static void tap_writable(void *opaque)
93{
94 TAPState *s = opaque;
95
Jason Wangec45f082013-01-30 19:12:20 +080096 tap_write_poll(s, false);
Mark McLoughlin5281d752009-10-22 17:49:07 +010097
Mark McLoughlin3e35ba92009-11-25 18:49:04 +000098 qemu_flush_queued_packets(&s->nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +010099}
100
101static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
102{
103 ssize_t len;
104
105 do {
106 len = writev(s->fd, iov, iovcnt);
107 } while (len == -1 && errno == EINTR);
108
109 if (len == -1 && errno == EAGAIN) {
Jason Wangec45f082013-01-30 19:12:20 +0800110 tap_write_poll(s, true);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100111 return 0;
112 }
113
114 return len;
115}
116
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100117static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100118 int iovcnt)
119{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000120 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100121 const struct iovec *iovp = iov;
122 struct iovec iov_copy[iovcnt + 1];
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300123 struct virtio_net_hdr_mrg_rxbuf hdr = { };
Mark McLoughlin5281d752009-10-22 17:49:07 +0100124
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300125 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100126 iov_copy[0].iov_base = &hdr;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300127 iov_copy[0].iov_len = s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100128 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
129 iovp = iov_copy;
130 iovcnt++;
131 }
132
133 return tap_write_packet(s, iovp, iovcnt);
134}
135
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100136static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100137{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000138 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100139 struct iovec iov[2];
140 int iovcnt = 0;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300141 struct virtio_net_hdr_mrg_rxbuf hdr = { };
Mark McLoughlin5281d752009-10-22 17:49:07 +0100142
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300143 if (s->host_vnet_hdr_len) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100144 iov[iovcnt].iov_base = &hdr;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300145 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100146 iovcnt++;
147 }
148
149 iov[iovcnt].iov_base = (char *)buf;
150 iov[iovcnt].iov_len = size;
151 iovcnt++;
152
153 return tap_write_packet(s, iov, iovcnt);
154}
155
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100156static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100157{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000158 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100159 struct iovec iov[1];
160
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300161 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000162 return tap_receive_raw(nc, buf, size);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100163 }
164
165 iov[0].iov_base = (char *)buf;
166 iov[0].iov_len = size;
167
168 return tap_write_packet(s, iov, 1);
169}
170
Mark McLoughlin966ea5e2009-10-22 17:49:09 +0100171#ifndef __sun__
172ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100173{
174 return read(tapfd, buf, maxlen);
175}
176#endif
177
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100178static void tap_send_completed(NetClientState *nc, ssize_t len)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100179{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000180 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Jason Wangec45f082013-01-30 19:12:20 +0800181 tap_read_poll(s, true);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100182}
183
184static void tap_send(void *opaque)
185{
186 TAPState *s = opaque;
187 int size;
Wangkai (Kevin,C)756ae782014-07-18 09:33:42 +0000188 int packets = 0;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100189
Fam Zhenga90a7422015-06-04 14:45:17 +0800190 while (true) {
Mark McLoughlin5819c912009-10-27 18:16:39 +0000191 uint8_t *buf = s->buf;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100192
Mark McLoughlin5819c912009-10-27 18:16:39 +0000193 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
194 if (size <= 0) {
195 break;
196 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100197
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300198 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
199 buf += s->host_vnet_hdr_len;
200 size -= s->host_vnet_hdr_len;
Mark McLoughlin5819c912009-10-27 18:16:39 +0000201 }
202
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000203 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
Mark McLoughlin5819c912009-10-27 18:16:39 +0000204 if (size == 0) {
Jason Wangec45f082013-01-30 19:12:20 +0800205 tap_read_poll(s, false);
Stefan Hajnoczi68e5ec62014-03-08 16:00:43 +0100206 break;
207 } else if (size < 0) {
208 break;
Mark McLoughlin5819c912009-10-27 18:16:39 +0000209 }
Wangkai (Kevin,C)756ae782014-07-18 09:33:42 +0000210
211 /*
212 * When the host keeps receiving more packets while tap_send() is
213 * running we can hog the QEMU global mutex. Limit the number of
214 * packets that are processed per tap_send() callback to prevent
215 * stalling the guest.
216 */
217 packets++;
218 if (packets >= 50) {
219 break;
220 }
Stefan Hajnoczi68e5ec62014-03-08 16:00:43 +0100221 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100222}
223
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100224static bool tap_has_ufo(NetClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100225{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000226 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100227
Eric Blakef394b2e2016-07-13 21:50:23 -0600228 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100229
230 return s->has_ufo;
231}
232
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100233static bool tap_has_vnet_hdr(NetClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100234{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000235 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100236
Eric Blakef394b2e2016-07-13 21:50:23 -0600237 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100238
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300239 return !!s->host_vnet_hdr_len;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100240}
241
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100242static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300243{
244 TAPState *s = DO_UPCAST(TAPState, nc, nc);
245
Eric Blakef394b2e2016-07-13 21:50:23 -0600246 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300247
Vincenzo Maffionee96dfd12014-02-06 17:02:15 +0100248 return !!tap_probe_vnet_hdr_len(s->fd, len);
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300249}
250
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100251static void tap_set_vnet_hdr_len(NetClientState *nc, int len)
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300252{
253 TAPState *s = DO_UPCAST(TAPState, nc, nc);
254
Eric Blakef394b2e2016-07-13 21:50:23 -0600255 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkin445d8922010-07-16 11:16:06 +0300256 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
257 len == sizeof(struct virtio_net_hdr));
258
259 tap_fd_set_vnet_hdr_len(s->fd, len);
260 s->host_vnet_hdr_len = len;
261}
262
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100263static void tap_using_vnet_hdr(NetClientState *nc, bool using_vnet_hdr)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100264{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000265 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100266
Eric Blakef394b2e2016-07-13 21:50:23 -0600267 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300268 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100269
270 s->using_vnet_hdr = using_vnet_hdr;
271}
272
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200273static int tap_set_vnet_le(NetClientState *nc, bool is_le)
274{
275 TAPState *s = DO_UPCAST(TAPState, nc, nc);
276
277 return tap_fd_set_vnet_le(s->fd, is_le);
278}
279
280static int tap_set_vnet_be(NetClientState *nc, bool is_be)
281{
282 TAPState *s = DO_UPCAST(TAPState, nc, nc);
283
284 return tap_fd_set_vnet_be(s->fd, is_be);
285}
286
Vincenzo Maffione3bac80d2014-02-06 17:02:19 +0100287static void tap_set_offload(NetClientState *nc, int csum, int tso4,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100288 int tso6, int ecn, int ufo)
289{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000290 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Michael S. Tsirkin27a63752010-10-31 19:06:47 +0200291 if (s->fd < 0) {
292 return;
293 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100294
Michael S. Tsirkin27a63752010-10-31 19:06:47 +0200295 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100296}
297
Marc-André Lureau9e32ff32016-07-11 16:48:47 +0200298static void tap_exit_notify(Notifier *notifier, void *data)
299{
300 TAPState *s = container_of(notifier, TAPState, exit);
301 Error *err = NULL;
302
303 if (s->down_script[0]) {
304 launch_script(s->down_script, s->down_script_arg, s->fd, &err);
305 if (err) {
306 error_report_err(err);
307 }
308 }
309}
310
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100311static void tap_cleanup(NetClientState *nc)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100312{
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000313 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100314
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200315 if (s->vhost_net) {
316 vhost_net_cleanup(s->vhost_net);
Marc-André Lureaue6bcb1b2016-07-27 01:15:12 +0400317 g_free(s->vhost_net);
Michael S. Tsirkin43849422010-10-27 20:03:43 +0200318 s->vhost_net = NULL;
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200319 }
320
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000321 qemu_purge_queued_packets(nc);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100322
Marc-André Lureau9e32ff32016-07-11 16:48:47 +0200323 tap_exit_notify(&s->exit, NULL);
324 qemu_remove_exit_notifier(&s->exit);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100325
Jason Wangec45f082013-01-30 19:12:20 +0800326 tap_read_poll(s, false);
327 tap_write_poll(s, false);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100328 close(s->fd);
Michael S. Tsirkin27a63752010-10-31 19:06:47 +0200329 s->fd = -1;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100330}
331
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100332static void tap_poll(NetClientState *nc, bool enable)
Michael S. Tsirkinceb69612009-12-24 14:46:29 +0200333{
334 TAPState *s = DO_UPCAST(TAPState, nc, nc);
335 tap_read_poll(s, enable);
336 tap_write_poll(s, enable);
337}
338
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100339int tap_get_fd(NetClientState *nc)
Michael S. Tsirkin95d528a2010-03-17 13:07:50 +0200340{
341 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -0600342 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkin95d528a2010-03-17 13:07:50 +0200343 return s->fd;
344}
345
Mark McLoughlin5281d752009-10-22 17:49:07 +0100346/* fd support */
347
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000348static NetClientInfo net_tap_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600349 .type = NET_CLIENT_DRIVER_TAP,
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000350 .size = sizeof(TAPState),
351 .receive = tap_receive,
352 .receive_raw = tap_receive_raw,
353 .receive_iov = tap_receive_iov,
Michael S. Tsirkinceb69612009-12-24 14:46:29 +0200354 .poll = tap_poll,
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000355 .cleanup = tap_cleanup,
Vincenzo Maffione2e753bc2014-02-06 17:02:17 +0100356 .has_ufo = tap_has_ufo,
357 .has_vnet_hdr = tap_has_vnet_hdr,
358 .has_vnet_hdr_len = tap_has_vnet_hdr_len,
359 .using_vnet_hdr = tap_using_vnet_hdr,
360 .set_offload = tap_set_offload,
361 .set_vnet_hdr_len = tap_set_vnet_hdr_len,
Greg Kurzc80cd6b2015-06-17 15:23:44 +0200362 .set_vnet_le = tap_set_vnet_le,
363 .set_vnet_be = tap_set_vnet_be,
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000364};
365
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100366static TAPState *net_tap_fd_init(NetClientState *peer,
Mark McLoughlin5281d752009-10-22 17:49:07 +0100367 const char *model,
368 const char *name,
369 int fd,
370 int vnet_hdr)
371{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100372 NetClientState *nc;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100373 TAPState *s;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100374
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100375 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000376
377 s = DO_UPCAST(TAPState, nc, nc);
378
Mark McLoughlin5281d752009-10-22 17:49:07 +0100379 s->fd = fd;
Michael S. Tsirkinef4252b2010-07-13 17:55:31 +0300380 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
Jason Wangec45f082013-01-30 19:12:20 +0800381 s->using_vnet_hdr = false;
Mark McLoughlin9c282712009-10-22 17:49:16 +0100382 s->has_ufo = tap_probe_has_ufo(s->fd);
Jason Wang16dbaf92013-01-30 19:12:32 +0800383 s->enabled = true;
Mark McLoughlin3e35ba92009-11-25 18:49:04 +0000384 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
Michael S. Tsirkin58ddcd52012-11-13 12:23:23 +0200385 /*
386 * Make sure host header length is set correctly in tap:
387 * it might have been modified by another instance of qemu.
388 */
389 if (tap_probe_vnet_hdr_len(s->fd, s->host_vnet_hdr_len)) {
390 tap_fd_set_vnet_hdr_len(s->fd, s->host_vnet_hdr_len);
391 }
Jason Wangec45f082013-01-30 19:12:20 +0800392 tap_read_poll(s, true);
Michael S. Tsirkin82b0d802010-03-17 13:08:24 +0200393 s->vhost_net = NULL;
Marc-André Lureau9e32ff32016-07-11 16:48:47 +0200394
395 s->exit.notify = tap_exit_notify;
396 qemu_add_exit_notifier(&s->exit);
397
Mark McLoughlin5281d752009-10-22 17:49:07 +0100398 return s;
399}
400
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200401static void launch_script(const char *setup_script, const char *ifname,
402 int fd, Error **errp)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100403{
Mark McLoughlin5281d752009-10-22 17:49:07 +0100404 int pid, status;
405 char *args[3];
406 char **parg;
407
Mark McLoughlin5281d752009-10-22 17:49:07 +0100408 /* try to launch network script */
409 pid = fork();
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200410 if (pid < 0) {
411 error_setg_errno(errp, errno, "could not launch network script %s",
412 setup_script);
413 return;
414 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100415 if (pid == 0) {
416 int open_max = sysconf(_SC_OPEN_MAX), i;
417
Pankaj Gupta13a12f82014-03-12 22:24:27 +0530418 for (i = 3; i < open_max; i++) {
419 if (i != fd) {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100420 close(i);
421 }
422 }
423 parg = args;
424 *parg++ = (char *)setup_script;
425 *parg++ = (char *)ifname;
Blue Swirl9678d952010-04-25 18:35:52 +0000426 *parg = NULL;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100427 execv(setup_script, args);
428 _exit(1);
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200429 } else {
Mark McLoughlin5281d752009-10-22 17:49:07 +0100430 while (waitpid(pid, &status, 0) != pid) {
431 /* loop */
432 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100433
434 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200435 return;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100436 }
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200437 error_setg(errp, "network script %s failed with status %d",
438 setup_script, status);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100439 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100440}
441
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500442static int recv_fd(int c)
443{
444 int fd;
445 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
446 struct msghdr msg = {
447 .msg_control = msgbuf,
448 .msg_controllen = sizeof(msgbuf),
449 };
450 struct cmsghdr *cmsg;
451 struct iovec iov;
452 uint8_t req[1];
453 ssize_t len;
454
455 cmsg = CMSG_FIRSTHDR(&msg);
456 cmsg->cmsg_level = SOL_SOCKET;
457 cmsg->cmsg_type = SCM_RIGHTS;
458 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
459 msg.msg_controllen = cmsg->cmsg_len;
460
461 iov.iov_base = req;
462 iov.iov_len = sizeof(req);
463
464 msg.msg_iov = &iov;
465 msg.msg_iovlen = 1;
466
467 len = recvmsg(c, &msg, 0);
468 if (len > 0) {
469 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
470 return fd;
471 }
472
473 return len;
474}
475
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200476static int net_bridge_run_helper(const char *helper, const char *bridge,
477 Error **errp)
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500478{
479 sigset_t oldmask, mask;
480 int pid, status;
481 char *args[5];
482 char **parg;
483 int sv[2];
484
485 sigemptyset(&mask);
486 sigaddset(&mask, SIGCHLD);
487 sigprocmask(SIG_BLOCK, &mask, &oldmask);
488
489 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200490 error_setg_errno(errp, errno, "socketpair() failed");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500491 return -1;
492 }
493
494 /* try to launch bridge helper */
495 pid = fork();
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200496 if (pid < 0) {
497 error_setg_errno(errp, errno, "Can't fork bridge helper");
498 return -1;
499 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500500 if (pid == 0) {
501 int open_max = sysconf(_SC_OPEN_MAX), i;
Prasad J Pandit389abe12019-07-23 16:17:54 +0530502 char *fd_buf = NULL;
503 char *br_buf = NULL;
504 char *helper_cmd = NULL;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500505
Pankaj Gupta13a12f82014-03-12 22:24:27 +0530506 for (i = 3; i < open_max; i++) {
507 if (i != sv[1]) {
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500508 close(i);
509 }
510 }
511
Prasad J Pandit389abe12019-07-23 16:17:54 +0530512 fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500513
514 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
515 /* assume helper is a command */
516
517 if (strstr(helper, "--br=") == NULL) {
Prasad J Pandit389abe12019-07-23 16:17:54 +0530518 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500519 }
520
Prasad J Pandit389abe12019-07-23 16:17:54 +0530521 helper_cmd = g_strdup_printf("%s %s %s %s", helper,
522 "--use-vnet", fd_buf, br_buf ? br_buf : "");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500523
524 parg = args;
525 *parg++ = (char *)"sh";
526 *parg++ = (char *)"-c";
527 *parg++ = helper_cmd;
528 *parg++ = NULL;
529
530 execv("/bin/sh", args);
Prasad J Pandit389abe12019-07-23 16:17:54 +0530531 g_free(helper_cmd);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500532 } else {
533 /* assume helper is just the executable path name */
534
Prasad J Pandit389abe12019-07-23 16:17:54 +0530535 br_buf = g_strdup_printf("%s%s", "--br=", bridge);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500536
537 parg = args;
538 *parg++ = (char *)helper;
539 *parg++ = (char *)"--use-vnet";
540 *parg++ = fd_buf;
541 *parg++ = br_buf;
542 *parg++ = NULL;
543
544 execv(helper, args);
545 }
Prasad J Pandit389abe12019-07-23 16:17:54 +0530546 g_free(fd_buf);
547 g_free(br_buf);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500548 _exit(1);
549
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200550 } else {
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500551 int fd;
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200552 int saved_errno;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500553
554 close(sv[1]);
555
556 do {
557 fd = recv_fd(sv[0]);
558 } while (fd == -1 && errno == EINTR);
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200559 saved_errno = errno;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500560
561 close(sv[0]);
562
563 while (waitpid(pid, &status, 0) != pid) {
564 /* loop */
565 }
566 sigprocmask(SIG_SETMASK, &oldmask, NULL);
567 if (fd < 0) {
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200568 error_setg_errno(errp, saved_errno,
569 "failed to recv file descriptor");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500570 return -1;
571 }
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200572 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
573 error_setg(errp, "bridge helper failed");
574 return -1;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500575 }
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200576 return fd;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500577 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500578}
579
KÅ‘vĂ¡gĂ³, ZoltĂ¡ncebea512016-07-13 21:50:12 -0600580int net_init_bridge(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200581 NetClientState *peer, Error **errp)
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500582{
Laszlo Ersekf79b51b2012-07-17 16:17:20 +0200583 const NetdevBridgeOptions *bridge;
584 const char *helper, *br;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500585 TAPState *s;
586 int fd, vnet_hdr;
587
Eric Blakef394b2e2016-07-13 21:50:23 -0600588 assert(netdev->type == NET_CLIENT_DRIVER_BRIDGE);
589 bridge = &netdev->u.bridge;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500590
Laszlo Ersekf79b51b2012-07-17 16:17:20 +0200591 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
592 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
593
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200594 fd = net_bridge_run_helper(helper, br, errp);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500595 if (fd == -1) {
596 return -1;
597 }
598
Li Qiangab792372018-11-21 03:21:59 -0800599 qemu_set_nonblock(fd);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500600 vnet_hdr = tap_probe_vnet_hdr(fd);
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100601 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500602
Laszlo Ersekf79b51b2012-07-17 16:17:20 +0200603 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
604 br);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500605
606 return 0;
607}
608
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200609static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
610 const char *setup_script, char *ifname,
Markus Armbruster468dd822015-05-15 13:58:58 +0200611 size_t ifname_sz, int mq_required, Error **errp)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100612{
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200613 Error *err = NULL;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100614 int fd, vnet_hdr_required;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100615
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200616 if (tap->has_vnet_hdr) {
617 *vnet_hdr = tap->vnet_hdr;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100618 vnet_hdr_required = *vnet_hdr;
619 } else {
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200620 *vnet_hdr = 1;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100621 vnet_hdr_required = 0;
622 }
623
Jason Wang264986e2013-01-30 19:12:34 +0800624 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required,
Markus Armbruster468dd822015-05-15 13:58:58 +0200625 mq_required, errp));
Mark McLoughlin5281d752009-10-22 17:49:07 +0100626 if (fd < 0) {
627 return -1;
628 }
629
Mark McLoughlin5281d752009-10-22 17:49:07 +0100630 if (setup_script &&
631 setup_script[0] != '\0' &&
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200632 strcmp(setup_script, "no") != 0) {
633 launch_script(setup_script, ifname, fd, &err);
634 if (err) {
Markus Armbruster468dd822015-05-15 13:58:58 +0200635 error_propagate(errp, err);
Markus Armbrusterac4fcf52015-05-15 13:58:57 +0200636 close(fd);
637 return -1;
638 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100639 }
640
Mark McLoughlin5281d752009-10-22 17:49:07 +0100641 return fd;
642}
643
Jason Wang264986e2013-01-30 19:12:34 +0800644#define MAX_TAP_QUEUES 1024
645
Markus Armbruster445f1162015-05-15 13:58:56 +0200646static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
647 const char *model, const char *name,
648 const char *ifname, const char *script,
649 const char *downscript, const char *vhostfdname,
650 int vnet_hdr, int fd, Error **errp)
Jason Wang5193e5f2013-01-30 19:12:30 +0800651{
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100652 Error *err = NULL;
Markus Armbrusterda4a4ea2015-05-15 13:58:53 +0200653 TAPState *s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300654 int vhostfd;
Jason Wang5193e5f2013-01-30 19:12:30 +0800655
Markus Armbruster80b832c2015-05-15 13:58:55 +0200656 tap_set_sndbuf(s->fd, tap, &err);
657 if (err) {
Markus Armbruster445f1162015-05-15 13:58:56 +0200658 error_propagate(errp, err);
659 return;
Jason Wang5193e5f2013-01-30 19:12:30 +0800660 }
661
Jason Wang264986e2013-01-30 19:12:34 +0800662 if (tap->has_fd || tap->has_fds) {
Jason Wang5193e5f2013-01-30 19:12:30 +0800663 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
664 } else if (tap->has_helper) {
665 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
666 tap->helper);
667 } else {
Jason Wang5193e5f2013-01-30 19:12:30 +0800668 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
669 "ifname=%s,script=%s,downscript=%s", ifname, script,
670 downscript);
671
672 if (strcmp(downscript, "no") != 0) {
673 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
674 snprintf(s->down_script_arg, sizeof(s->down_script_arg),
675 "%s", ifname);
676 }
677 }
678
679 if (tap->has_vhost ? tap->vhost :
680 vhostfdname || (tap->has_vhostforce && tap->vhostforce)) {
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300681 VhostNetOptions options;
682
Nikolay Nikolaev1a1bfac2014-05-27 15:05:49 +0300683 options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300684 options.net_backend = &s->nc;
Jason Wang69e87b32016-07-06 09:57:55 +0800685 if (tap->has_poll_us) {
686 options.busyloop_timeout = tap->poll_us;
687 } else {
688 options.busyloop_timeout = 0;
689 }
Jason Wang5193e5f2013-01-30 19:12:30 +0800690
Paolo Bonzini3a2d44f2016-02-26 00:05:57 +0100691 if (vhostfdname) {
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100692 vhostfd = monitor_fd_param(cur_mon, vhostfdname, &err);
Jason Wang5193e5f2013-01-30 19:12:30 +0800693 if (vhostfd == -1) {
Jay Zhou46d4d362018-03-02 17:04:44 +0800694 if (tap->has_vhostforce && tap->vhostforce) {
695 error_propagate(errp, err);
696 } else {
697 warn_report_err(err);
698 }
Markus Armbruster445f1162015-05-15 13:58:56 +0200699 return;
Jason Wang5193e5f2013-01-30 19:12:30 +0800700 }
Brijesh Singhd5428002018-04-06 13:51:25 -0500701 qemu_set_nonblock(vhostfd);
Jason Wang5193e5f2013-01-30 19:12:30 +0800702 } else {
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300703 vhostfd = open("/dev/vhost-net", O_RDWR);
704 if (vhostfd < 0) {
Jay Zhou46d4d362018-03-02 17:04:44 +0800705 if (tap->has_vhostforce && tap->vhostforce) {
706 error_setg_errno(errp, errno,
707 "tap: open vhost char device failed");
708 } else {
709 warn_report("tap: open vhost char device failed: %s",
710 strerror(errno));
711 }
Markus Armbruster445f1162015-05-15 13:58:56 +0200712 return;
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300713 }
Li Qiangab792372018-11-21 03:21:59 -0800714 qemu_set_nonblock(vhostfd);
Jason Wang5193e5f2013-01-30 19:12:30 +0800715 }
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300716 options.opaque = (void *)(uintptr_t)vhostfd;
Jason Wang5193e5f2013-01-30 19:12:30 +0800717
Nikolay Nikolaev81647a62014-05-27 15:05:22 +0300718 s->vhost_net = vhost_net_init(&options);
Jason Wang5193e5f2013-01-30 19:12:30 +0800719 if (!s->vhost_net) {
Jay Zhou46d4d362018-03-02 17:04:44 +0800720 if (tap->has_vhostforce && tap->vhostforce) {
721 error_setg(errp, VHOST_NET_INIT_FAILED);
722 } else {
723 warn_report(VHOST_NET_INIT_FAILED);
724 }
Markus Armbruster445f1162015-05-15 13:58:56 +0200725 return;
Jason Wang5193e5f2013-01-30 19:12:30 +0800726 }
Paolo Bonzini3a2d44f2016-02-26 00:05:57 +0100727 } else if (vhostfdname) {
Jason Wang69e87b32016-07-06 09:57:55 +0800728 error_setg(errp, "vhostfd(s)= is not valid without vhost");
Jason Wang5193e5f2013-01-30 19:12:30 +0800729 }
Jason Wang5193e5f2013-01-30 19:12:30 +0800730}
731
Jason Wang264986e2013-01-30 19:12:34 +0800732static int get_fds(char *str, char *fds[], int max)
733{
734 char *ptr = str, *this;
735 size_t len = strlen(str);
736 int i = 0;
737
738 while (i < max && ptr < str + len) {
739 this = strchr(ptr, ':');
740
741 if (this == NULL) {
742 fds[i] = g_strdup(ptr);
743 } else {
744 fds[i] = g_strndup(ptr, this - ptr);
745 }
746
747 i++;
748 if (this == NULL) {
749 break;
750 } else {
751 ptr = this + 1;
752 }
753 }
754
755 return i;
756}
757
KÅ‘vĂ¡gĂ³, ZoltĂ¡ncebea512016-07-13 21:50:12 -0600758int net_init_tap(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200759 NetClientState *peer, Error **errp)
Mark McLoughlin5281d752009-10-22 17:49:07 +0100760{
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200761 const NetdevTapOptions *tap;
Jason Wang264986e2013-01-30 19:12:34 +0800762 int fd, vnet_hdr = 0, i = 0, queues;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200763 /* for the no-fd, no-helper case */
764 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
Jason Wang5193e5f2013-01-30 19:12:30 +0800765 const char *downscript = NULL;
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100766 Error *err = NULL;
Jason Wang264986e2013-01-30 19:12:34 +0800767 const char *vhostfdname;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200768 char ifname[128];
769
Eric Blakef394b2e2016-07-13 21:50:23 -0600770 assert(netdev->type == NET_CLIENT_DRIVER_TAP);
771 tap = &netdev->u.tap;
Jason Wang264986e2013-01-30 19:12:34 +0800772 queues = tap->has_queues ? tap->queues : 1;
773 vhostfdname = tap->has_vhostfd ? tap->vhostfd : NULL;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200774
Thomas Huth442da402018-04-30 20:02:24 +0200775 /* QEMU hubs do not support multiqueue tap, in this case peer is set.
Jason Wangce675a72013-02-21 11:05:56 +0800776 * For -netdev, peer is always NULL. */
777 if (peer && (tap->has_queues || tap->has_fds || tap->has_vhostfds)) {
Thomas Huth442da402018-04-30 20:02:24 +0200778 error_setg(errp, "Multiqueue tap cannot be used with hubs");
Jason Wangce675a72013-02-21 11:05:56 +0800779 return -1;
780 }
781
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200782 if (tap->has_fd) {
783 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
Jason Wang264986e2013-01-30 19:12:34 +0800784 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
Jason Wangc87826a2013-06-04 13:18:17 +0800785 tap->has_fds || tap->has_vhostfds) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200786 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
787 "helper=, queues=, fds=, and vhostfds= "
788 "are invalid with fd=");
Mark McLoughlin5281d752009-10-22 17:49:07 +0100789 return -1;
790 }
791
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100792 fd = monitor_fd_param(cur_mon, tap->fd, &err);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100793 if (fd == -1) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200794 error_propagate(errp, err);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100795 return -1;
796 }
797
Li Qiangab792372018-11-21 03:21:59 -0800798 qemu_set_nonblock(fd);
Mark McLoughlin5281d752009-10-22 17:49:07 +0100799
800 vnet_hdr = tap_probe_vnet_hdr(fd);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500801
Markus Armbruster445f1162015-05-15 13:58:56 +0200802 net_init_tap_one(tap, peer, "tap", name, NULL,
803 script, downscript,
804 vhostfdname, vnet_hdr, fd, &err);
805 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200806 error_propagate(errp, err);
Jason Wang264986e2013-01-30 19:12:34 +0800807 return -1;
808 }
809 } else if (tap->has_fds) {
Peter Maydellfac7d7b2017-01-10 19:21:54 +0000810 char **fds;
811 char **vhost_fds;
Yunjian Wang323e7c12018-05-31 15:28:22 +0800812 int nfds = 0, nvhosts = 0;
813 int ret = 0;
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500814
Jason Wang264986e2013-01-30 19:12:34 +0800815 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
816 tap->has_vnet_hdr || tap->has_helper || tap->has_queues ||
Jason Wangc87826a2013-06-04 13:18:17 +0800817 tap->has_vhostfd) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200818 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
819 "helper=, queues=, and vhostfd= "
820 "are invalid with fds=");
Jason Wang264986e2013-01-30 19:12:34 +0800821 return -1;
822 }
823
Peter Maydellfac7d7b2017-01-10 19:21:54 +0000824 fds = g_new0(char *, MAX_TAP_QUEUES);
825 vhost_fds = g_new0(char *, MAX_TAP_QUEUES);
826
Jason Wang264986e2013-01-30 19:12:34 +0800827 nfds = get_fds(tap->fds, fds, MAX_TAP_QUEUES);
828 if (tap->has_vhostfds) {
829 nvhosts = get_fds(tap->vhostfds, vhost_fds, MAX_TAP_QUEUES);
830 if (nfds != nvhosts) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200831 error_setg(errp, "The number of fds passed does not match "
832 "the number of vhostfds passed");
Yunjian Wang323e7c12018-05-31 15:28:22 +0800833 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200834 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800835 }
836 }
837
838 for (i = 0; i < nfds; i++) {
Markus Armbruster1677f4c2015-02-09 14:03:19 +0100839 fd = monitor_fd_param(cur_mon, fds[i], &err);
Jason Wang264986e2013-01-30 19:12:34 +0800840 if (fd == -1) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200841 error_propagate(errp, err);
Yunjian Wang323e7c12018-05-31 15:28:22 +0800842 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200843 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800844 }
845
Li Qiangab792372018-11-21 03:21:59 -0800846 qemu_set_nonblock(fd);
Jason Wang264986e2013-01-30 19:12:34 +0800847
848 if (i == 0) {
849 vnet_hdr = tap_probe_vnet_hdr(fd);
850 } else if (vnet_hdr != tap_probe_vnet_hdr(fd)) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200851 error_setg(errp,
852 "vnet_hdr not consistent across given tap fds");
Yunjian Wang323e7c12018-05-31 15:28:22 +0800853 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200854 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800855 }
856
Markus Armbruster445f1162015-05-15 13:58:56 +0200857 net_init_tap_one(tap, peer, "tap", name, ifname,
858 script, downscript,
859 tap->has_vhostfds ? vhost_fds[i] : NULL,
860 vnet_hdr, fd, &err);
861 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200862 error_propagate(errp, err);
Yunjian Wang323e7c12018-05-31 15:28:22 +0800863 ret = -1;
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200864 goto free_fail;
Jason Wang264986e2013-01-30 19:12:34 +0800865 }
866 }
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200867
868free_fail:
Yunjian Wang323e7c12018-05-31 15:28:22 +0800869 for (i = 0; i < nvhosts; i++) {
870 g_free(vhost_fds[i]);
871 }
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200872 for (i = 0; i < nfds; i++) {
873 g_free(fds[i]);
Paolo Bonzini091a6b22016-07-15 10:56:07 +0200874 }
875 g_free(fds);
876 g_free(vhost_fds);
Yunjian Wang323e7c12018-05-31 15:28:22 +0800877 return ret;
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200878 } else if (tap->has_helper) {
879 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
Jason Wangc87826a2013-06-04 13:18:17 +0800880 tap->has_vnet_hdr || tap->has_queues || tap->has_vhostfds) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200881 error_setg(errp, "ifname=, script=, downscript=, vnet_hdr=, "
882 "queues=, and vhostfds= are invalid with helper=");
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500883 return -1;
884 }
885
Alexey Kardashevskiy584613e2016-09-13 17:11:54 +1000886 fd = net_bridge_run_helper(tap->helper,
887 tap->has_br ?
888 tap->br : DEFAULT_BRIDGE_INTERFACE,
Markus Armbrustera8a21be2015-05-15 13:58:54 +0200889 errp);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500890 if (fd == -1) {
891 return -1;
892 }
893
Li Qiangab792372018-11-21 03:21:59 -0800894 qemu_set_nonblock(fd);
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500895 vnet_hdr = tap_probe_vnet_hdr(fd);
896
Markus Armbruster445f1162015-05-15 13:58:56 +0200897 net_init_tap_one(tap, peer, "bridge", name, ifname,
898 script, downscript, vhostfdname,
899 vnet_hdr, fd, &err);
900 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200901 error_propagate(errp, err);
Gonglei84f8f3d2014-11-02 13:37:17 +0800902 close(fd);
Jason Wang264986e2013-01-30 19:12:34 +0800903 return -1;
904 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100905 } else {
Jason Wangc87826a2013-06-04 13:18:17 +0800906 if (tap->has_vhostfds) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200907 error_setg(errp, "vhostfds= is invalid if fds= wasn't specified");
Jason Wangc87826a2013-06-04 13:18:17 +0800908 return -1;
909 }
Laszlo Ersek08c573a2012-07-17 16:17:19 +0200910 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
Jason Wang5193e5f2013-01-30 19:12:30 +0800911 downscript = tap->has_downscript ? tap->downscript :
912 DEFAULT_NETWORK_DOWN_SCRIPT;
Jason Wang264986e2013-01-30 19:12:34 +0800913
914 if (tap->has_ifname) {
915 pstrcpy(ifname, sizeof ifname, tap->ifname);
916 } else {
917 ifname[0] = '\0';
Juergen Lock929fe492009-11-20 23:23:03 +0100918 }
Corey Bryanta7c36ee2012-01-26 09:42:27 -0500919
Jason Wang264986e2013-01-30 19:12:34 +0800920 for (i = 0; i < queues; i++) {
921 fd = net_tap_init(tap, &vnet_hdr, i >= 1 ? "no" : script,
Markus Armbrustera3088172015-05-15 13:59:03 +0200922 ifname, sizeof ifname, queues > 1, errp);
Jason Wang264986e2013-01-30 19:12:34 +0800923 if (fd == -1) {
924 return -1;
925 }
926
927 if (queues > 1 && i == 0 && !tap->has_ifname) {
928 if (tap_fd_get_ifname(fd, ifname)) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200929 error_setg(errp, "Fail to get ifname");
Gonglei84f8f3d2014-11-02 13:37:17 +0800930 close(fd);
Jason Wang264986e2013-01-30 19:12:34 +0800931 return -1;
932 }
933 }
934
Markus Armbruster445f1162015-05-15 13:58:56 +0200935 net_init_tap_one(tap, peer, "tap", name, ifname,
936 i >= 1 ? "no" : script,
937 i >= 1 ? "no" : downscript,
938 vhostfdname, vnet_hdr, fd, &err);
939 if (err) {
Markus Armbrustera3088172015-05-15 13:59:03 +0200940 error_propagate(errp, err);
Gonglei84f8f3d2014-11-02 13:37:17 +0800941 close(fd);
Jason Wang264986e2013-01-30 19:12:34 +0800942 return -1;
943 }
944 }
Mark McLoughlin5281d752009-10-22 17:49:07 +0100945 }
946
Jason Wang264986e2013-01-30 19:12:34 +0800947 return 0;
Mark McLoughlin5281d752009-10-22 17:49:07 +0100948}
Michael S. Tsirkinb2025542010-03-17 13:08:38 +0200949
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100950VHostNetState *tap_get_vhost_net(NetClientState *nc)
Michael S. Tsirkinb2025542010-03-17 13:08:38 +0200951{
952 TAPState *s = DO_UPCAST(TAPState, nc, nc);
Eric Blakef394b2e2016-07-13 21:50:23 -0600953 assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
Michael S. Tsirkinb2025542010-03-17 13:08:38 +0200954 return s->vhost_net;
955}
Jason Wang16dbaf92013-01-30 19:12:32 +0800956
957int tap_enable(NetClientState *nc)
958{
959 TAPState *s = DO_UPCAST(TAPState, nc, nc);
960 int ret;
961
962 if (s->enabled) {
963 return 0;
964 } else {
965 ret = tap_fd_enable(s->fd);
966 if (ret == 0) {
967 s->enabled = true;
968 tap_update_fd_handler(s);
969 }
970 return ret;
971 }
972}
973
974int tap_disable(NetClientState *nc)
975{
976 TAPState *s = DO_UPCAST(TAPState, nc, nc);
977 int ret;
978
979 if (s->enabled == 0) {
980 return 0;
981 } else {
982 ret = tap_fd_disable(s->fd);
983 if (ret == 0) {
984 qemu_purge_queued_packets(nc);
985 s->enabled = false;
986 tap_update_fd_handler(s);
987 }
988 return ret;
989 }
990}