Module Name: src Committed By: pooka Date: Thu Mar 13 18:14:13 UTC 2014
Modified Files: src/sys/rump/net/lib/libvirtif: Makefile if_virt.c Added Files: src/sys/rump/net/lib/libvirtif: virtif_user.c virtif_user.h Removed Files: src/sys/rump/net/lib/libvirtif: rumpcomp_user.c rumpcomp_user.h Log Message: Rename rumpcomp_user.c -> virtif_user.c Convert to new-style RUMPCOMP_USER_SRCS in Makefile. To generate a diff of this commit: cvs rdiff -u -r1.8 -r1.9 src/sys/rump/net/lib/libvirtif/Makefile cvs rdiff -u -r1.40 -r1.41 src/sys/rump/net/lib/libvirtif/if_virt.c cvs rdiff -u -r1.12 -r0 src/sys/rump/net/lib/libvirtif/rumpcomp_user.c cvs rdiff -u -r1.6 -r0 src/sys/rump/net/lib/libvirtif/rumpcomp_user.h cvs rdiff -u -r0 -r1.1 src/sys/rump/net/lib/libvirtif/virtif_user.c \ src/sys/rump/net/lib/libvirtif/virtif_user.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/rump/net/lib/libvirtif/Makefile diff -u src/sys/rump/net/lib/libvirtif/Makefile:1.8 src/sys/rump/net/lib/libvirtif/Makefile:1.9 --- src/sys/rump/net/lib/libvirtif/Makefile:1.8 Wed Mar 12 23:40:58 2014 +++ src/sys/rump/net/lib/libvirtif/Makefile Thu Mar 13 18:14:13 2014 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.8 2014/03/12 23:40:58 pooka Exp $ +# $NetBSD: Makefile,v 1.9 2014/03/13 18:14:13 pooka Exp $ # LIB= rumpnet_virtif @@ -6,7 +6,7 @@ VIRTIF= virt .include "${.CURDIR}/Makefile.virtif" -RUMPCOMP_USER= #defined +RUMPCOMP_USER_SRCS= virtif_user.c .include <bsd.lib.mk> .include <bsd.klinks.mk> Index: src/sys/rump/net/lib/libvirtif/if_virt.c diff -u src/sys/rump/net/lib/libvirtif/if_virt.c:1.40 src/sys/rump/net/lib/libvirtif/if_virt.c:1.41 --- src/sys/rump/net/lib/libvirtif/if_virt.c:1.40 Wed Mar 12 17:49:13 2014 +++ src/sys/rump/net/lib/libvirtif/if_virt.c Thu Mar 13 18:14:13 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: if_virt.c,v 1.40 2014/03/12 17:49:13 pooka Exp $ */ +/* $NetBSD: if_virt.c,v 1.41 2014/03/13 18:14:13 pooka Exp $ */ /* * Copyright (c) 2008, 2013 Antti Kantee. All Rights Reserved. @@ -26,7 +26,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.40 2014/03/12 17:49:13 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if_virt.c,v 1.41 2014/03/13 18:14:13 pooka Exp $"); #include <sys/param.h> #include <sys/kernel.h> @@ -47,7 +47,7 @@ __KERNEL_RCSID(0, "$NetBSD: if_virt.c,v #include "rump_net_private.h" #include "if_virt.h" -#include "rumpcomp_user.h" +#include "virtif_user.h" /* * Virtual interface. Uses hypercalls to shovel packets back Added files: Index: src/sys/rump/net/lib/libvirtif/virtif_user.c diff -u /dev/null src/sys/rump/net/lib/libvirtif/virtif_user.c:1.1 --- /dev/null Thu Mar 13 18:14:13 2014 +++ src/sys/rump/net/lib/libvirtif/virtif_user.c Thu Mar 13 18:14:13 2014 @@ -0,0 +1,282 @@ +/* $NetBSD: virtif_user.c,v 1.1 2014/03/13 18:14:13 pooka Exp $ */ + +/* + * Copyright (c) 2013 Antti Kantee. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _KERNEL +#include <sys/types.h> +#include <sys/ioctl.h> +#include <sys/uio.h> + +#include <assert.h> +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> +#include <poll.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#ifdef __linux__ +#include <net/if.h> +#include <linux/if_tun.h> +#endif + +#include <rump/rumpuser_component.h> + +#include "if_virt.h" +#include "virtif_user.h" + +#if VIFHYPER_REVISION != 20140302 +#error VIFHYPER_REVISION mismatch +#endif + +struct virtif_user { + struct virtif_sc *viu_virtifsc; + int viu_devnum; + + int viu_fd; + int viu_pipe[2]; + pthread_t viu_rcvthr; + + int viu_dying; + + char viu_rcvbuf[9018]; /* jumbo frame max len */ +}; + +static int +opentapdev(int devnum) +{ + int fd = -1; + +#if defined(__NetBSD__) || defined(__DragonFly__) + char tapdev[64]; + + snprintf(tapdev, sizeof(tapdev), "/dev/tap%d", devnum); + fd = open(tapdev, O_RDWR); + if (fd == -1) { + fprintf(stderr, "rumpcomp_virtif_create: can't open %s: " + "%s\n", tapdev, strerror(errno)); + } + +#elif defined(__linux__) + struct ifreq ifr; + char devname[16]; + + fd = open("/dev/net/tun", O_RDWR); + if (fd == -1) { + fprintf(stderr, "rumpcomp_virtif_create: can't open %s: " + "%s\n", "/dev/net/tun", strerror(errno)); + return -1; + } + + snprintf(devname, sizeof(devname), "tun%d", devnum); + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TAP | IFF_NO_PI; + strncpy(ifr.ifr_name, devname, sizeof(ifr.ifr_name)-1); + + if (ioctl(fd, TUNSETIFF, &ifr) == -1) { + close(fd); + fd = -1; + } + +#else + fprintf(stderr, "virtif not supported on this platform\n"); +#endif + + return fd; +} + +static void +closetapdev(struct virtif_user *viu) +{ + + close(viu->viu_fd); +} + +static void * +rcvthread(void *aaargh) +{ + struct virtif_user *viu = aaargh; + struct pollfd pfd[2]; + struct iovec iov; + ssize_t nn = 0; + int prv; + + rumpuser_component_kthread(); + + pfd[0].fd = viu->viu_fd; + pfd[0].events = POLLIN; + pfd[1].fd = viu->viu_pipe[0]; + pfd[1].events = POLLIN; + + while (!viu->viu_dying) { + prv = poll(pfd, 2, -1); + if (prv == 0) + continue; + if (prv == -1) { + /* XXX */ + fprintf(stderr, "virt%d: poll error: %d\n", + viu->viu_devnum, errno); + sleep(1); + continue; + } + if (pfd[1].revents & POLLIN) + continue; + + nn = read(viu->viu_fd, + viu->viu_rcvbuf, sizeof(viu->viu_rcvbuf)); + if (nn == -1 && errno == EAGAIN) + continue; + + if (nn < 1) { + /* XXX */ + fprintf(stderr, "virt%d: receive failed\n", + viu->viu_devnum); + sleep(1); + continue; + } + iov.iov_base = viu->viu_rcvbuf; + iov.iov_len = nn; + + rumpuser_component_schedule(NULL); + VIF_DELIVERPKT(viu->viu_virtifsc, &iov, 1); + rumpuser_component_unschedule(); + } + + assert(viu->viu_dying); + + rumpuser_component_kthread_release(); + return NULL; +} + +int +VIFHYPER_CREATE(const char *devstr, struct virtif_sc *vif_sc, uint8_t *enaddr, + struct virtif_user **viup) +{ + struct virtif_user *viu = NULL; + void *cookie; + int devnum; + int rv; + + cookie = rumpuser_component_unschedule(); + + /* + * Since this interface doesn't do LINKSTR, we know devstr to be + * well-formatted. + */ + devnum = atoi(devstr); + + viu = calloc(1, sizeof(*viu)); + if (viu == NULL) { + rv = errno; + goto oerr1; + } + viu->viu_virtifsc = vif_sc; + + viu->viu_fd = opentapdev(devnum); + if (viu->viu_fd == -1) { + rv = errno; + goto oerr2; + } + viu->viu_devnum = devnum; + + if (pipe(viu->viu_pipe) == -1) { + rv = errno; + goto oerr3; + } + + if ((rv = pthread_create(&viu->viu_rcvthr, NULL, rcvthread, viu)) != 0) + goto oerr4; + + rumpuser_component_schedule(cookie); + *viup = viu; + return 0; + + oerr4: + close(viu->viu_pipe[0]); + close(viu->viu_pipe[1]); + oerr3: + closetapdev(viu); + oerr2: + free(viu); + oerr1: + rumpuser_component_schedule(cookie); + return rumpuser_component_errtrans(rv); +} + +void +VIFHYPER_SEND(struct virtif_user *viu, + struct iovec *iov, size_t iovlen) +{ + void *cookie = rumpuser_component_unschedule(); + ssize_t idontcare __attribute__((__unused__)); + + /* + * no need to check for return value; packets may be dropped + * + * ... sorry, I spoke too soon. We need to check it because + * apparently gcc reinvented const poisoning and it's very + * hard to say "thanks, I know I'm not using the result, + * but please STFU and let's get on with something useful". + * So let's trick gcc into letting us share the compiler + * experience. + */ + idontcare = writev(viu->viu_fd, iov, iovlen); + + rumpuser_component_schedule(cookie); +} + +void +VIFHYPER_DYING(struct virtif_user *viu) +{ + void *cookie = rumpuser_component_unschedule(); + + viu->viu_dying = 1; + if (write(viu->viu_pipe[1], + &viu->viu_dying, sizeof(viu->viu_dying)) == -1) { + fprintf(stderr, "%s: failed to signal thread\n", + VIF_STRING(VIFHYPER_DYING)); + } + + rumpuser_component_schedule(cookie); +} + +void +VIFHYPER_DESTROY(struct virtif_user *viu) +{ + void *cookie = rumpuser_component_unschedule(); + + pthread_join(viu->viu_rcvthr, NULL); + closetapdev(viu); + close(viu->viu_pipe[0]); + close(viu->viu_pipe[1]); + free(viu); + + rumpuser_component_schedule(cookie); +} +#endif Index: src/sys/rump/net/lib/libvirtif/virtif_user.h diff -u /dev/null src/sys/rump/net/lib/libvirtif/virtif_user.h:1.1 --- /dev/null Thu Mar 13 18:14:13 2014 +++ src/sys/rump/net/lib/libvirtif/virtif_user.h Thu Mar 13 18:14:13 2014 @@ -0,0 +1,39 @@ +/* $NetBSD: virtif_user.h,v 1.1 2014/03/13 18:14:13 pooka Exp $ */ + +/* + * Copyright (c) 2013 Antti Kantee. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +struct virtif_user; + +#define VIFHYPER_REVISION 20140302 + +int VIFHYPER_CREATE(const char *, struct virtif_sc *, uint8_t *, + struct virtif_user **); +void VIFHYPER_DYING(struct virtif_user *); +void VIFHYPER_DESTROY(struct virtif_user *); + +void VIFHYPER_SEND(struct virtif_user *, struct iovec *, size_t); + +void VIF_DELIVERPKT(struct virtif_sc *, struct iovec *, size_t);