]> git.proxmox.com Git - qemu.git/blame - net/tap.c
vhost: vhost net support
[qemu.git] / net / tap.c
CommitLineData
5281d757
MM
1/*
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
26#include "net/tap.h"
27
28#include "config-host.h"
29
30#include <signal.h>
31#include <sys/ioctl.h>
32#include <sys/stat.h>
33#include <sys/wait.h>
71f4effc 34#include <sys/socket.h>
5281d757
MM
35#include <net/if.h>
36
37#include "net.h"
38#include "sysemu.h"
39#include "qemu-char.h"
40#include "qemu-common.h"
2f792016 41#include "qemu-error.h"
5281d757 42
5281d757 43#include "net/tap-linux.h"
5281d757 44
5281d757
MM
45/* Maximum GSO packet size (64k) plus plenty of room for
46 * the ethernet and virtio_net headers
47 */
48#define TAP_BUFSIZE (4096 + 65536)
49
50typedef struct TAPState {
3e35ba93 51 VLANClientState nc;
5281d757
MM
52 int fd;
53 char down_script[1024];
54 char down_script_arg[128];
55 uint8_t buf[TAP_BUFSIZE];
56 unsigned int read_poll : 1;
57 unsigned int write_poll : 1;
58 unsigned int has_vnet_hdr : 1;
59 unsigned int using_vnet_hdr : 1;
60 unsigned int has_ufo: 1;
61} TAPState;
62
63static int launch_script(const char *setup_script, const char *ifname, int fd);
64
65static int tap_can_send(void *opaque);
66static void tap_send(void *opaque);
67static void tap_writable(void *opaque);
68
69static void tap_update_fd_handler(TAPState *s)
70{
71 qemu_set_fd_handler2(s->fd,
72 s->read_poll ? tap_can_send : NULL,
73 s->read_poll ? tap_send : NULL,
74 s->write_poll ? tap_writable : NULL,
75 s);
76}
77
78static void tap_read_poll(TAPState *s, int enable)
79{
80 s->read_poll = !!enable;
81 tap_update_fd_handler(s);
82}
83
84static void tap_write_poll(TAPState *s, int enable)
85{
86 s->write_poll = !!enable;
87 tap_update_fd_handler(s);
88}
89
90static void tap_writable(void *opaque)
91{
92 TAPState *s = opaque;
93
94 tap_write_poll(s, 0);
95
3e35ba93 96 qemu_flush_queued_packets(&s->nc);
5281d757
MM
97}
98
99static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
100{
101 ssize_t len;
102
103 do {
104 len = writev(s->fd, iov, iovcnt);
105 } while (len == -1 && errno == EINTR);
106
107 if (len == -1 && errno == EAGAIN) {
108 tap_write_poll(s, 1);
109 return 0;
110 }
111
112 return len;
113}
114
3e35ba93 115static ssize_t tap_receive_iov(VLANClientState *nc, const struct iovec *iov,
5281d757
MM
116 int iovcnt)
117{
3e35ba93 118 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
119 const struct iovec *iovp = iov;
120 struct iovec iov_copy[iovcnt + 1];
121 struct virtio_net_hdr hdr = { 0, };
122
123 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
124 iov_copy[0].iov_base = &hdr;
125 iov_copy[0].iov_len = sizeof(hdr);
126 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
127 iovp = iov_copy;
128 iovcnt++;
129 }
130
131 return tap_write_packet(s, iovp, iovcnt);
132}
133
3e35ba93 134static ssize_t tap_receive_raw(VLANClientState *nc, const uint8_t *buf, size_t size)
5281d757 135{
3e35ba93 136 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
137 struct iovec iov[2];
138 int iovcnt = 0;
139 struct virtio_net_hdr hdr = { 0, };
140
141 if (s->has_vnet_hdr) {
142 iov[iovcnt].iov_base = &hdr;
143 iov[iovcnt].iov_len = sizeof(hdr);
144 iovcnt++;
145 }
146
147 iov[iovcnt].iov_base = (char *)buf;
148 iov[iovcnt].iov_len = size;
149 iovcnt++;
150
151 return tap_write_packet(s, iov, iovcnt);
152}
153
3e35ba93 154static ssize_t tap_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
5281d757 155{
3e35ba93 156 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
157 struct iovec iov[1];
158
159 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
3e35ba93 160 return tap_receive_raw(nc, buf, size);
5281d757
MM
161 }
162
163 iov[0].iov_base = (char *)buf;
164 iov[0].iov_len = size;
165
166 return tap_write_packet(s, iov, 1);
167}
168
169static int tap_can_send(void *opaque)
170{
171 TAPState *s = opaque;
172
3e35ba93 173 return qemu_can_send_packet(&s->nc);
5281d757
MM
174}
175
966ea5ec
MM
176#ifndef __sun__
177ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
5281d757
MM
178{
179 return read(tapfd, buf, maxlen);
180}
181#endif
182
3e35ba93 183static void tap_send_completed(VLANClientState *nc, ssize_t len)
5281d757 184{
3e35ba93 185 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
186 tap_read_poll(s, 1);
187}
188
189static void tap_send(void *opaque)
190{
191 TAPState *s = opaque;
192 int size;
193
5819c918
MM
194 do {
195 uint8_t *buf = s->buf;
196
197 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
198 if (size <= 0) {
199 break;
200 }
201
202 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
203 buf += sizeof(struct virtio_net_hdr);
204 size -= sizeof(struct virtio_net_hdr);
205 }
206
3e35ba93 207 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
5819c918
MM
208 if (size == 0) {
209 tap_read_poll(s, 0);
210 }
3e35ba93 211 } while (size > 0 && qemu_can_send_packet(&s->nc));
5281d757
MM
212}
213
3e35ba93 214int tap_has_ufo(VLANClientState *nc)
5281d757 215{
3e35ba93 216 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757 217
665a3b07 218 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
5281d757
MM
219
220 return s->has_ufo;
221}
222
3e35ba93 223int tap_has_vnet_hdr(VLANClientState *nc)
5281d757 224{
3e35ba93 225 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757 226
665a3b07 227 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
5281d757
MM
228
229 return s->has_vnet_hdr;
230}
231
3e35ba93 232void tap_using_vnet_hdr(VLANClientState *nc, int using_vnet_hdr)
5281d757 233{
3e35ba93 234 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757
MM
235
236 using_vnet_hdr = using_vnet_hdr != 0;
237
665a3b07 238 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
5281d757
MM
239 assert(s->has_vnet_hdr == using_vnet_hdr);
240
241 s->using_vnet_hdr = using_vnet_hdr;
242}
243
3e35ba93 244void tap_set_offload(VLANClientState *nc, int csum, int tso4,
5281d757
MM
245 int tso6, int ecn, int ufo)
246{
3e35ba93 247 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757 248
1faac1f7 249 return tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
5281d757
MM
250}
251
3e35ba93 252static void tap_cleanup(VLANClientState *nc)
5281d757 253{
3e35ba93 254 TAPState *s = DO_UPCAST(TAPState, nc, nc);
5281d757 255
3e35ba93 256 qemu_purge_queued_packets(nc);
5281d757
MM
257
258 if (s->down_script[0])
259 launch_script(s->down_script, s->down_script_arg, s->fd);
260
261 tap_read_poll(s, 0);
262 tap_write_poll(s, 0);
263 close(s->fd);
5281d757
MM
264}
265
ceb69615
MT
266static void tap_poll(VLANClientState *nc, bool enable)
267{
268 TAPState *s = DO_UPCAST(TAPState, nc, nc);
269 tap_read_poll(s, enable);
270 tap_write_poll(s, enable);
271}
272
95d528a2
MT
273int tap_get_fd(VLANClientState *nc)
274{
275 TAPState *s = DO_UPCAST(TAPState, nc, nc);
276 assert(nc->info->type == NET_CLIENT_TYPE_TAP);
277 return s->fd;
278}
279
5281d757
MM
280/* fd support */
281
3e35ba93
MM
282static NetClientInfo net_tap_info = {
283 .type = NET_CLIENT_TYPE_TAP,
284 .size = sizeof(TAPState),
285 .receive = tap_receive,
286 .receive_raw = tap_receive_raw,
287 .receive_iov = tap_receive_iov,
ceb69615 288 .poll = tap_poll,
3e35ba93
MM
289 .cleanup = tap_cleanup,
290};
291
5281d757
MM
292static TAPState *net_tap_fd_init(VLANState *vlan,
293 const char *model,
294 const char *name,
295 int fd,
296 int vnet_hdr)
297{
3e35ba93 298 VLANClientState *nc;
5281d757 299 TAPState *s;
5281d757 300
3e35ba93
MM
301 nc = qemu_new_net_client(&net_tap_info, vlan, NULL, model, name);
302
303 s = DO_UPCAST(TAPState, nc, nc);
304
5281d757
MM
305 s->fd = fd;
306 s->has_vnet_hdr = vnet_hdr != 0;
307 s->using_vnet_hdr = 0;
9c282718 308 s->has_ufo = tap_probe_has_ufo(s->fd);
3e35ba93 309 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
5281d757
MM
310 tap_read_poll(s, 1);
311 return s;
312}
313
5281d757
MM
314static int launch_script(const char *setup_script, const char *ifname, int fd)
315{
316 sigset_t oldmask, mask;
317 int pid, status;
318 char *args[3];
319 char **parg;
320
321 sigemptyset(&mask);
322 sigaddset(&mask, SIGCHLD);
323 sigprocmask(SIG_BLOCK, &mask, &oldmask);
324
325 /* try to launch network script */
326 pid = fork();
327 if (pid == 0) {
328 int open_max = sysconf(_SC_OPEN_MAX), i;
329
330 for (i = 0; i < open_max; i++) {
331 if (i != STDIN_FILENO &&
332 i != STDOUT_FILENO &&
333 i != STDERR_FILENO &&
334 i != fd) {
335 close(i);
336 }
337 }
338 parg = args;
339 *parg++ = (char *)setup_script;
340 *parg++ = (char *)ifname;
341 *parg++ = NULL;
342 execv(setup_script, args);
343 _exit(1);
344 } else if (pid > 0) {
345 while (waitpid(pid, &status, 0) != pid) {
346 /* loop */
347 }
348 sigprocmask(SIG_SETMASK, &oldmask, NULL);
349
350 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
351 return 0;
352 }
353 }
354 fprintf(stderr, "%s: could not launch network script\n", setup_script);
355 return -1;
356}
357
358static int net_tap_init(QemuOpts *opts, int *vnet_hdr)
359{
360 int fd, vnet_hdr_required;
361 char ifname[128] = {0,};
362 const char *setup_script;
363
364 if (qemu_opt_get(opts, "ifname")) {
365 pstrcpy(ifname, sizeof(ifname), qemu_opt_get(opts, "ifname"));
366 }
367
368 *vnet_hdr = qemu_opt_get_bool(opts, "vnet_hdr", 1);
369 if (qemu_opt_get(opts, "vnet_hdr")) {
370 vnet_hdr_required = *vnet_hdr;
371 } else {
372 vnet_hdr_required = 0;
373 }
374
375 TFR(fd = tap_open(ifname, sizeof(ifname), vnet_hdr, vnet_hdr_required));
376 if (fd < 0) {
377 return -1;
378 }
379
380 setup_script = qemu_opt_get(opts, "script");
381 if (setup_script &&
382 setup_script[0] != '\0' &&
383 strcmp(setup_script, "no") != 0 &&
384 launch_script(setup_script, ifname, fd)) {
385 close(fd);
386 return -1;
387 }
388
389 qemu_opt_set(opts, "ifname", ifname);
390
391 return fd;
392}
393
394int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan)
395{
396 TAPState *s;
df6c2a0f 397 int fd, vnet_hdr = 0;
5281d757
MM
398
399 if (qemu_opt_get(opts, "fd")) {
400 if (qemu_opt_get(opts, "ifname") ||
401 qemu_opt_get(opts, "script") ||
402 qemu_opt_get(opts, "downscript") ||
403 qemu_opt_get(opts, "vnet_hdr")) {
1ecda02b 404 error_report("ifname=, script=, downscript= and vnet_hdr= is invalid with fd=");
5281d757
MM
405 return -1;
406 }
407
408 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
409 if (fd == -1) {
410 return -1;
411 }
412
413 fcntl(fd, F_SETFL, O_NONBLOCK);
414
415 vnet_hdr = tap_probe_vnet_hdr(fd);
416 } else {
417 if (!qemu_opt_get(opts, "script")) {
418 qemu_opt_set(opts, "script", DEFAULT_NETWORK_SCRIPT);
419 }
420
421 if (!qemu_opt_get(opts, "downscript")) {
422 qemu_opt_set(opts, "downscript", DEFAULT_NETWORK_DOWN_SCRIPT);
423 }
424
425 fd = net_tap_init(opts, &vnet_hdr);
929fe497
JL
426 if (fd == -1) {
427 return -1;
428 }
5281d757
MM
429 }
430
431 s = net_tap_fd_init(vlan, "tap", name, fd, vnet_hdr);
432 if (!s) {
433 close(fd);
434 return -1;
435 }
436
15ac913b 437 if (tap_set_sndbuf(s->fd, opts) < 0) {
5281d757
MM
438 return -1;
439 }
440
441 if (qemu_opt_get(opts, "fd")) {
3e35ba93 442 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
5281d757
MM
443 } else {
444 const char *ifname, *script, *downscript;
445
446 ifname = qemu_opt_get(opts, "ifname");
447 script = qemu_opt_get(opts, "script");
448 downscript = qemu_opt_get(opts, "downscript");
449
3e35ba93 450 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
5281d757
MM
451 "ifname=%s,script=%s,downscript=%s",
452 ifname, script, downscript);
453
454 if (strcmp(downscript, "no") != 0) {
455 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
456 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
457 }
458 }
459
5281d757
MM
460 return 0;
461}