hw/ds1338: Remove 'now' field from state struct
[qemu/pbrook.git] / net / tap.c
bloba88ae8f61ae83b3058f878a68fa9cab119932298
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "net/tap.h"
28 #include "config-host.h"
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <net/if.h>
36 #include "net.h"
37 #include "monitor.h"
38 #include "sysemu.h"
39 #include "qemu-char.h"
40 #include "qemu-common.h"
41 #include "qemu-error.h"
43 #include "net/tap-linux.h"
45 #include "hw/vhost_net.h"
47 /* Maximum GSO packet size (64k) plus plenty of room for
48 * the ethernet and virtio_net headers
50 #define TAP_BUFSIZE (4096 + 65536)
52 typedef struct TAPState {
53 NetClientState nc;
54 int fd;
55 char down_script[1024];
56 char down_script_arg[128];
57 uint8_t buf[TAP_BUFSIZE];
58 unsigned int read_poll : 1;
59 unsigned int write_poll : 1;
60 unsigned int using_vnet_hdr : 1;
61 unsigned int has_ufo: 1;
62 VHostNetState *vhost_net;
63 unsigned host_vnet_hdr_len;
64 } TAPState;
66 static int launch_script(const char *setup_script, const char *ifname, int fd);
68 static int tap_can_send(void *opaque);
69 static void tap_send(void *opaque);
70 static void tap_writable(void *opaque);
72 static void tap_update_fd_handler(TAPState *s)
74 qemu_set_fd_handler2(s->fd,
75 s->read_poll ? tap_can_send : NULL,
76 s->read_poll ? tap_send : NULL,
77 s->write_poll ? tap_writable : NULL,
78 s);
81 static void tap_read_poll(TAPState *s, int enable)
83 s->read_poll = !!enable;
84 tap_update_fd_handler(s);
87 static void tap_write_poll(TAPState *s, int enable)
89 s->write_poll = !!enable;
90 tap_update_fd_handler(s);
93 static void tap_writable(void *opaque)
95 TAPState *s = opaque;
97 tap_write_poll(s, 0);
99 qemu_flush_queued_packets(&s->nc);
102 static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcnt)
104 ssize_t len;
106 do {
107 len = writev(s->fd, iov, iovcnt);
108 } while (len == -1 && errno == EINTR);
110 if (len == -1 && errno == EAGAIN) {
111 tap_write_poll(s, 1);
112 return 0;
115 return len;
118 static ssize_t tap_receive_iov(NetClientState *nc, const struct iovec *iov,
119 int iovcnt)
121 TAPState *s = DO_UPCAST(TAPState, nc, nc);
122 const struct iovec *iovp = iov;
123 struct iovec iov_copy[iovcnt + 1];
124 struct virtio_net_hdr_mrg_rxbuf hdr = { };
126 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
127 iov_copy[0].iov_base = &hdr;
128 iov_copy[0].iov_len = s->host_vnet_hdr_len;
129 memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
130 iovp = iov_copy;
131 iovcnt++;
134 return tap_write_packet(s, iovp, iovcnt);
137 static ssize_t tap_receive_raw(NetClientState *nc, const uint8_t *buf, size_t size)
139 TAPState *s = DO_UPCAST(TAPState, nc, nc);
140 struct iovec iov[2];
141 int iovcnt = 0;
142 struct virtio_net_hdr_mrg_rxbuf hdr = { };
144 if (s->host_vnet_hdr_len) {
145 iov[iovcnt].iov_base = &hdr;
146 iov[iovcnt].iov_len = s->host_vnet_hdr_len;
147 iovcnt++;
150 iov[iovcnt].iov_base = (char *)buf;
151 iov[iovcnt].iov_len = size;
152 iovcnt++;
154 return tap_write_packet(s, iov, iovcnt);
157 static ssize_t tap_receive(NetClientState *nc, const uint8_t *buf, size_t size)
159 TAPState *s = DO_UPCAST(TAPState, nc, nc);
160 struct iovec iov[1];
162 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
163 return tap_receive_raw(nc, buf, size);
166 iov[0].iov_base = (char *)buf;
167 iov[0].iov_len = size;
169 return tap_write_packet(s, iov, 1);
172 static int tap_can_send(void *opaque)
174 TAPState *s = opaque;
176 return qemu_can_send_packet(&s->nc);
179 #ifndef __sun__
180 ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
182 return read(tapfd, buf, maxlen);
184 #endif
186 static void tap_send_completed(NetClientState *nc, ssize_t len)
188 TAPState *s = DO_UPCAST(TAPState, nc, nc);
189 tap_read_poll(s, 1);
192 static void tap_send(void *opaque)
194 TAPState *s = opaque;
195 int size;
197 do {
198 uint8_t *buf = s->buf;
200 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
201 if (size <= 0) {
202 break;
205 if (s->host_vnet_hdr_len && !s->using_vnet_hdr) {
206 buf += s->host_vnet_hdr_len;
207 size -= s->host_vnet_hdr_len;
210 size = qemu_send_packet_async(&s->nc, buf, size, tap_send_completed);
211 if (size == 0) {
212 tap_read_poll(s, 0);
214 } while (size > 0 && qemu_can_send_packet(&s->nc));
217 int tap_has_ufo(NetClientState *nc)
219 TAPState *s = DO_UPCAST(TAPState, nc, nc);
221 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
223 return s->has_ufo;
226 int tap_has_vnet_hdr(NetClientState *nc)
228 TAPState *s = DO_UPCAST(TAPState, nc, nc);
230 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
232 return !!s->host_vnet_hdr_len;
235 int tap_has_vnet_hdr_len(NetClientState *nc, int len)
237 TAPState *s = DO_UPCAST(TAPState, nc, nc);
239 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
241 return tap_probe_vnet_hdr_len(s->fd, len);
244 void tap_set_vnet_hdr_len(NetClientState *nc, int len)
246 TAPState *s = DO_UPCAST(TAPState, nc, nc);
248 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
249 assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
250 len == sizeof(struct virtio_net_hdr));
252 tap_fd_set_vnet_hdr_len(s->fd, len);
253 s->host_vnet_hdr_len = len;
256 void tap_using_vnet_hdr(NetClientState *nc, int using_vnet_hdr)
258 TAPState *s = DO_UPCAST(TAPState, nc, nc);
260 using_vnet_hdr = using_vnet_hdr != 0;
262 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
263 assert(!!s->host_vnet_hdr_len == using_vnet_hdr);
265 s->using_vnet_hdr = using_vnet_hdr;
268 void tap_set_offload(NetClientState *nc, int csum, int tso4,
269 int tso6, int ecn, int ufo)
271 TAPState *s = DO_UPCAST(TAPState, nc, nc);
272 if (s->fd < 0) {
273 return;
276 tap_fd_set_offload(s->fd, csum, tso4, tso6, ecn, ufo);
279 static void tap_cleanup(NetClientState *nc)
281 TAPState *s = DO_UPCAST(TAPState, nc, nc);
283 if (s->vhost_net) {
284 vhost_net_cleanup(s->vhost_net);
285 s->vhost_net = NULL;
288 qemu_purge_queued_packets(nc);
290 if (s->down_script[0])
291 launch_script(s->down_script, s->down_script_arg, s->fd);
293 tap_read_poll(s, 0);
294 tap_write_poll(s, 0);
295 close(s->fd);
296 s->fd = -1;
299 static void tap_poll(NetClientState *nc, bool enable)
301 TAPState *s = DO_UPCAST(TAPState, nc, nc);
302 tap_read_poll(s, enable);
303 tap_write_poll(s, enable);
306 int tap_get_fd(NetClientState *nc)
308 TAPState *s = DO_UPCAST(TAPState, nc, nc);
309 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
310 return s->fd;
313 /* fd support */
315 static NetClientInfo net_tap_info = {
316 .type = NET_CLIENT_OPTIONS_KIND_TAP,
317 .size = sizeof(TAPState),
318 .receive = tap_receive,
319 .receive_raw = tap_receive_raw,
320 .receive_iov = tap_receive_iov,
321 .poll = tap_poll,
322 .cleanup = tap_cleanup,
325 static TAPState *net_tap_fd_init(NetClientState *peer,
326 const char *model,
327 const char *name,
328 int fd,
329 int vnet_hdr)
331 NetClientState *nc;
332 TAPState *s;
334 nc = qemu_new_net_client(&net_tap_info, peer, model, name);
336 s = DO_UPCAST(TAPState, nc, nc);
338 s->fd = fd;
339 s->host_vnet_hdr_len = vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
340 s->using_vnet_hdr = 0;
341 s->has_ufo = tap_probe_has_ufo(s->fd);
342 tap_set_offload(&s->nc, 0, 0, 0, 0, 0);
343 tap_read_poll(s, 1);
344 s->vhost_net = NULL;
345 return s;
348 static int launch_script(const char *setup_script, const char *ifname, int fd)
350 int pid, status;
351 char *args[3];
352 char **parg;
354 /* try to launch network script */
355 pid = fork();
356 if (pid == 0) {
357 int open_max = sysconf(_SC_OPEN_MAX), i;
359 for (i = 0; i < open_max; i++) {
360 if (i != STDIN_FILENO &&
361 i != STDOUT_FILENO &&
362 i != STDERR_FILENO &&
363 i != fd) {
364 close(i);
367 parg = args;
368 *parg++ = (char *)setup_script;
369 *parg++ = (char *)ifname;
370 *parg = NULL;
371 execv(setup_script, args);
372 _exit(1);
373 } else if (pid > 0) {
374 while (waitpid(pid, &status, 0) != pid) {
375 /* loop */
378 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
379 return 0;
382 fprintf(stderr, "%s: could not launch network script\n", setup_script);
383 return -1;
386 static int recv_fd(int c)
388 int fd;
389 uint8_t msgbuf[CMSG_SPACE(sizeof(fd))];
390 struct msghdr msg = {
391 .msg_control = msgbuf,
392 .msg_controllen = sizeof(msgbuf),
394 struct cmsghdr *cmsg;
395 struct iovec iov;
396 uint8_t req[1];
397 ssize_t len;
399 cmsg = CMSG_FIRSTHDR(&msg);
400 cmsg->cmsg_level = SOL_SOCKET;
401 cmsg->cmsg_type = SCM_RIGHTS;
402 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
403 msg.msg_controllen = cmsg->cmsg_len;
405 iov.iov_base = req;
406 iov.iov_len = sizeof(req);
408 msg.msg_iov = &iov;
409 msg.msg_iovlen = 1;
411 len = recvmsg(c, &msg, 0);
412 if (len > 0) {
413 memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
414 return fd;
417 return len;
420 static int net_bridge_run_helper(const char *helper, const char *bridge)
422 sigset_t oldmask, mask;
423 int pid, status;
424 char *args[5];
425 char **parg;
426 int sv[2];
428 sigemptyset(&mask);
429 sigaddset(&mask, SIGCHLD);
430 sigprocmask(SIG_BLOCK, &mask, &oldmask);
432 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
433 return -1;
436 /* try to launch bridge helper */
437 pid = fork();
438 if (pid == 0) {
439 int open_max = sysconf(_SC_OPEN_MAX), i;
440 char fd_buf[6+10];
441 char br_buf[6+IFNAMSIZ] = {0};
442 char helper_cmd[PATH_MAX + sizeof(fd_buf) + sizeof(br_buf) + 15];
444 for (i = 0; i < open_max; i++) {
445 if (i != STDIN_FILENO &&
446 i != STDOUT_FILENO &&
447 i != STDERR_FILENO &&
448 i != sv[1]) {
449 close(i);
453 snprintf(fd_buf, sizeof(fd_buf), "%s%d", "--fd=", sv[1]);
455 if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
456 /* assume helper is a command */
458 if (strstr(helper, "--br=") == NULL) {
459 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
462 snprintf(helper_cmd, sizeof(helper_cmd), "%s %s %s %s",
463 helper, "--use-vnet", fd_buf, br_buf);
465 parg = args;
466 *parg++ = (char *)"sh";
467 *parg++ = (char *)"-c";
468 *parg++ = helper_cmd;
469 *parg++ = NULL;
471 execv("/bin/sh", args);
472 } else {
473 /* assume helper is just the executable path name */
475 snprintf(br_buf, sizeof(br_buf), "%s%s", "--br=", bridge);
477 parg = args;
478 *parg++ = (char *)helper;
479 *parg++ = (char *)"--use-vnet";
480 *parg++ = fd_buf;
481 *parg++ = br_buf;
482 *parg++ = NULL;
484 execv(helper, args);
486 _exit(1);
488 } else if (pid > 0) {
489 int fd;
491 close(sv[1]);
493 do {
494 fd = recv_fd(sv[0]);
495 } while (fd == -1 && errno == EINTR);
497 close(sv[0]);
499 while (waitpid(pid, &status, 0) != pid) {
500 /* loop */
502 sigprocmask(SIG_SETMASK, &oldmask, NULL);
503 if (fd < 0) {
504 fprintf(stderr, "failed to recv file descriptor\n");
505 return -1;
508 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
509 return fd;
512 fprintf(stderr, "failed to launch bridge helper\n");
513 return -1;
516 int net_init_bridge(const NetClientOptions *opts, const char *name,
517 NetClientState *peer)
519 const NetdevBridgeOptions *bridge;
520 const char *helper, *br;
522 TAPState *s;
523 int fd, vnet_hdr;
525 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_BRIDGE);
526 bridge = opts->bridge;
528 helper = bridge->has_helper ? bridge->helper : DEFAULT_BRIDGE_HELPER;
529 br = bridge->has_br ? bridge->br : DEFAULT_BRIDGE_INTERFACE;
531 fd = net_bridge_run_helper(helper, br);
532 if (fd == -1) {
533 return -1;
536 fcntl(fd, F_SETFL, O_NONBLOCK);
538 vnet_hdr = tap_probe_vnet_hdr(fd);
540 s = net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr);
541 if (!s) {
542 close(fd);
543 return -1;
546 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s,br=%s", helper,
547 br);
549 return 0;
552 static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
553 const char *setup_script, char *ifname,
554 size_t ifname_sz)
556 int fd, vnet_hdr_required;
558 if (tap->has_ifname) {
559 pstrcpy(ifname, ifname_sz, tap->ifname);
560 } else {
561 assert(ifname_sz > 0);
562 ifname[0] = '\0';
565 if (tap->has_vnet_hdr) {
566 *vnet_hdr = tap->vnet_hdr;
567 vnet_hdr_required = *vnet_hdr;
568 } else {
569 *vnet_hdr = 1;
570 vnet_hdr_required = 0;
573 TFR(fd = tap_open(ifname, ifname_sz, vnet_hdr, vnet_hdr_required));
574 if (fd < 0) {
575 return -1;
578 if (setup_script &&
579 setup_script[0] != '\0' &&
580 strcmp(setup_script, "no") != 0 &&
581 launch_script(setup_script, ifname, fd)) {
582 close(fd);
583 return -1;
586 return fd;
589 int net_init_tap(const NetClientOptions *opts, const char *name,
590 NetClientState *peer)
592 const NetdevTapOptions *tap;
594 int fd, vnet_hdr = 0;
595 const char *model;
596 TAPState *s;
598 /* for the no-fd, no-helper case */
599 const char *script = NULL; /* suppress wrong "uninit'd use" gcc warning */
600 char ifname[128];
602 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_TAP);
603 tap = opts->tap;
605 if (tap->has_fd) {
606 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
607 tap->has_vnet_hdr || tap->has_helper) {
608 error_report("ifname=, script=, downscript=, vnet_hdr=, "
609 "and helper= are invalid with fd=");
610 return -1;
613 fd = monitor_handle_fd_param(cur_mon, tap->fd);
614 if (fd == -1) {
615 return -1;
618 fcntl(fd, F_SETFL, O_NONBLOCK);
620 vnet_hdr = tap_probe_vnet_hdr(fd);
622 model = "tap";
624 } else if (tap->has_helper) {
625 if (tap->has_ifname || tap->has_script || tap->has_downscript ||
626 tap->has_vnet_hdr) {
627 error_report("ifname=, script=, downscript=, and vnet_hdr= "
628 "are invalid with helper=");
629 return -1;
632 fd = net_bridge_run_helper(tap->helper, DEFAULT_BRIDGE_INTERFACE);
633 if (fd == -1) {
634 return -1;
637 fcntl(fd, F_SETFL, O_NONBLOCK);
639 vnet_hdr = tap_probe_vnet_hdr(fd);
641 model = "bridge";
643 } else {
644 script = tap->has_script ? tap->script : DEFAULT_NETWORK_SCRIPT;
645 fd = net_tap_init(tap, &vnet_hdr, script, ifname, sizeof ifname);
646 if (fd == -1) {
647 return -1;
650 model = "tap";
653 s = net_tap_fd_init(peer, model, name, fd, vnet_hdr);
654 if (!s) {
655 close(fd);
656 return -1;
659 if (tap_set_sndbuf(s->fd, tap) < 0) {
660 return -1;
663 if (tap->has_fd) {
664 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=%d", fd);
665 } else if (tap->has_helper) {
666 snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=%s",
667 tap->helper);
668 } else {
669 const char *downscript;
671 downscript = tap->has_downscript ? tap->downscript :
672 DEFAULT_NETWORK_DOWN_SCRIPT;
674 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
675 "ifname=%s,script=%s,downscript=%s", ifname, script,
676 downscript);
678 if (strcmp(downscript, "no") != 0) {
679 snprintf(s->down_script, sizeof(s->down_script), "%s", downscript);
680 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
684 if (tap->has_vhost ? tap->vhost :
685 tap->has_vhostfd || (tap->has_vhostforce && tap->vhostforce)) {
686 int vhostfd;
688 if (tap->has_vhostfd) {
689 vhostfd = monitor_handle_fd_param(cur_mon, tap->vhostfd);
690 if (vhostfd == -1) {
691 return -1;
693 } else {
694 vhostfd = -1;
697 s->vhost_net = vhost_net_init(&s->nc, vhostfd,
698 tap->has_vhostforce && tap->vhostforce);
699 if (!s->vhost_net) {
700 error_report("vhost-net requested but could not be initialized");
701 return -1;
703 } else if (tap->has_vhostfd) {
704 error_report("vhostfd= is not valid without vhost");
705 return -1;
708 return 0;
711 VHostNetState *tap_get_vhost_net(NetClientState *nc)
713 TAPState *s = DO_UPCAST(TAPState, nc, nc);
714 assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP);
715 return s->vhost_net;