]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - Documentation/lguest/lguest.c
lguest: turn Waker into a thread, not a process
[linux-2.6-omap-h63xx.git] / Documentation / lguest / lguest.c
index 0d1b0265d8e213478ce5262eb22cadd3fe624afa..b88b0ea54e90c597896aac6eb478e1b56bab8bd0 100644 (file)
@@ -66,8 +66,8 @@ typedef uint8_t u8;
 #endif
 /* We can have up to 256 pages for devices. */
 #define DEVICE_PAGES 256
-/* This will occupy 2 pages: it must be a power of 2. */
-#define VIRTQUEUE_NUM 128
+/* This will occupy 3 pages: it must be a power of 2. */
+#define VIRTQUEUE_NUM 256
 
 /*L:120 verbose is both a global flag and a macro.  The C preprocessor allows
  * this, and although I wouldn't recommend it, it works quite nicely here. */
@@ -76,8 +76,12 @@ static bool verbose;
        do { if (verbose) printf(args); } while(0)
 /*:*/
 
-/* The pipe to send commands to the waker process */
-static int waker_fd;
+/* File descriptors for the Waker. */
+struct {
+       int pipe[2];
+       int lguest_fd;
+} waker_fds;
+
 /* The pointer to the start of guest memory. */
 static void *guest_base;
 /* The maximum guest physical address allowed, and maximum possible. */
@@ -579,69 +583,64 @@ static void add_device_fd(int fd)
  * watch, but handing a file descriptor mask through to the kernel is fairly
  * icky.
  *
- * Instead, we fork off a process which watches the file descriptors and writes
+ * Instead, we clone off a thread which watches the file descriptors and writes
  * the LHREQ_BREAK command to the /dev/lguest file descriptor to tell the Host
  * stop running the Guest.  This causes the Launcher to return from the
  * /dev/lguest read with -EAGAIN, where it will write to /dev/lguest to reset
  * the LHREQ_BREAK and wake us up again.
  *
  * This, of course, is merely a different *kind* of icky.
+ *
+ * Given my well-known antipathy to threads, I'd prefer to use processes.  But
+ * it's easier to share Guest memory with threads, and trivial to share the
+ * devices.infds as the Launcher changes it.
  */
-static void wake_parent(int pipefd, int lguest_fd)
+static int waker(void *unused)
 {
-       /* Add the pipe from the Launcher to the fdset in the device_list, so
-        * we watch it, too. */
-       add_device_fd(pipefd);
+       /* Close the write end of the pipe: only the Launcher has it open. */
+       close(waker_fds.pipe[1]);
 
        for (;;) {
                fd_set rfds = devices.infds;
                unsigned long args[] = { LHREQ_BREAK, 1 };
+               unsigned int maxfd = devices.max_infd;
+
+               /* We also listen to the pipe from the Launcher. */
+               FD_SET(waker_fds.pipe[0], &rfds);
+               if (waker_fds.pipe[0] > maxfd)
+                       maxfd = waker_fds.pipe[0];
 
                /* Wait until input is ready from one of the devices. */
-               select(devices.max_infd+1, &rfds, NULL, NULL, NULL);
-               /* Is it a message from the Launcher? */
-               if (FD_ISSET(pipefd, &rfds)) {
-                       int fd;
-                       /* If read() returns 0, it means the Launcher has
-                        * exited.  We silently follow. */
-                       if (read(pipefd, &fd, sizeof(fd)) == 0)
-                               exit(0);
-                       /* Otherwise it's telling us to change what file
-                        * descriptors we're to listen to.  Positive means
-                        * listen to a new one, negative means stop
-                        * listening. */
-                       if (fd >= 0)
-                               FD_SET(fd, &devices.infds);
-                       else
-                               FD_CLR(-fd - 1, &devices.infds);
-               } else /* Send LHREQ_BREAK command. */
-                       pwrite(lguest_fd, args, sizeof(args), cpu_id);
+               select(maxfd+1, &rfds, NULL, NULL, NULL);
+
+               /* Message from Launcher? */
+               if (FD_ISSET(waker_fds.pipe[0], &rfds)) {
+                       char c;
+                       /* If this fails, then assume Launcher has exited.
+                        * Don't do anything on exit: we're just a thread! */
+                       if (read(waker_fds.pipe[0], &c, 1) != 1)
+                               _exit(0);
+                       continue;
+               }
+
+               /* Send LHREQ_BREAK command to snap the Launcher out of it. */
+               pwrite(waker_fds.lguest_fd, args, sizeof(args), cpu_id);
        }
+       return 0;
 }
 
 /* This routine just sets up a pipe to the Waker process. */
-static int setup_waker(int lguest_fd)
-{
-       int pipefd[2], child;
-
-       /* We create a pipe to talk to the Waker, and also so it knows when the
-        * Launcher dies (and closes pipe). */
-       pipe(pipefd);
-       child = fork();
-       if (child == -1)
-               err(1, "forking");
-
-       if (child == 0) {
-               /* We are the Waker: close the "writing" end of our copy of the
-                * pipe and start waiting for input. */
-               close(pipefd[1]);
-               wake_parent(pipefd[0], lguest_fd);
-       }
-       /* Close the reading end of our copy of the pipe. */
-       close(pipefd[0]);
+static void setup_waker(int lguest_fd)
+{
+       /* This pipe is closed when Launcher dies, telling Waker. */
+       if (pipe(waker_fds.pipe) != 0)
+               err(1, "Creating pipe for Waker");
+
+       /* Waker also needs to know the lguest fd */
+       waker_fds.lguest_fd = lguest_fd;
 
-       /* Here is the fd used to talk to the waker. */
-       return pipefd[1];
+       if (clone(waker, malloc(4096) + 4096, CLONE_VM | SIGCHLD, NULL) == -1)
+               err(1, "Creating Waker");
 }
 
 /*
@@ -863,8 +862,8 @@ static bool handle_console_input(int fd, struct device *dev)
                                unsigned long args[] = { LHREQ_BREAK, 0 };
                                /* Close the fd so Waker will know it has to
                                 * exit. */
-                               close(waker_fd);
-                               /* Just in case waker is blocked in BREAK, send
+                               close(waker_fds.pipe[1]);
+                               /* Just in case Waker is blocked in BREAK, send
                                 * unbreak now. */
                                write(fd, args, sizeof(args));
                                exit(2);
@@ -929,11 +928,9 @@ static void handle_net_output(int fd, struct virtqueue *vq, bool timeout)
        while ((head = get_vq_desc(vq, iov, &out, &in)) != vq->vring.num) {
                if (in)
                        errx(1, "Input buffers in output queue?");
-               /* Check header, but otherwise ignore it (we told the Guest we
-                * supported no features, so it shouldn't have anything
-                * interesting). */
-               (void)convert(&iov[0], struct virtio_net_hdr);
-               len = writev(vq->dev->fd, iov+1, out-1);
+               len = writev(vq->dev->fd, iov, out);
+               if (len < 0)
+                       err(1, "Writing network packet to tun");
                add_used_and_trigger(fd, vq, head, len);
                num++;
        }
@@ -958,7 +955,6 @@ static bool handle_tun_input(int fd, struct device *dev)
        unsigned int head, in_num, out_num;
        int len;
        struct iovec iov[dev->vq->vring.num];
-       struct virtio_net_hdr *hdr;
 
        /* First we need a network buffer from the Guests's recv virtqueue. */
        head = get_vq_desc(dev->vq, iov, &out_num, &in_num);
@@ -977,18 +973,13 @@ static bool handle_tun_input(int fd, struct device *dev)
        } else if (out_num)
                errx(1, "Output buffers in network recv queue?");
 
-       /* First element is the header: we set it to 0 (no features). */
-       hdr = convert(&iov[0], struct virtio_net_hdr);
-       hdr->flags = 0;
-       hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
-
        /* Read the packet from the device directly into the Guest's buffer. */
-       len = readv(dev->fd, iov+1, in_num-1);
+       len = readv(dev->fd, iov, in_num);
        if (len <= 0)
                err(1, "reading network");
 
        /* Tell the Guest about the new packet. */
-       add_used_and_trigger(fd, dev->vq, head, sizeof(*hdr) + len);
+       add_used_and_trigger(fd, dev->vq, head, len);
 
        verbose("tun input packet len %i [%02x %02x] (%s)\n", len,
                ((u8 *)iov[1].iov_base)[0], ((u8 *)iov[1].iov_base)[1],
@@ -1004,8 +995,8 @@ static bool handle_tun_input(int fd, struct device *dev)
 static void enable_fd(int fd, struct virtqueue *vq, bool timeout)
 {
        add_device_fd(vq->dev->fd);
-       /* Tell waker to listen to it again */
-       write(waker_fd, &vq->dev->fd, sizeof(vq->dev->fd));
+       /* Snap the Waker out of its select loop. */
+       write(waker_fds.pipe[1], "", 1);
 }
 
 static void net_enable_fd(int fd, struct virtqueue *vq, bool timeout)
@@ -1142,7 +1133,6 @@ static void handle_input(int fd)
                 * descriptors and a method of handling them.  */
                for (i = devices.dev; i; i = i->next) {
                        if (i->handle_input && FD_ISSET(i->fd, &fds)) {
-                               int dev_fd;
                                if (i->handle_input(fd, i))
                                        continue;
 
@@ -1152,11 +1142,6 @@ static void handle_input(int fd)
                                 * buffers to deliver into.  Console also uses
                                 * it when it discovers that stdin is closed. */
                                FD_CLR(i->fd, &devices.infds);
-                               /* Tell waker to ignore it too, by sending a
-                                * negative fd number (-1, since 0 is a valid
-                                * FD number). */
-                               dev_fd = -i->fd - 1;
-                               write(waker_fd, &dev_fd, sizeof(dev_fd));
                        }
                }
 
@@ -1490,11 +1475,15 @@ static int get_tun_device(char tapif[IFNAMSIZ])
         * the truth, I completely blundered my way through this code, but it
         * works now! */
        netfd = open_or_die("/dev/net/tun", O_RDWR);
-       ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
+       ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_VNET_HDR;
        strcpy(ifr.ifr_name, "tap%d");
        if (ioctl(netfd, TUNSETIFF, &ifr) != 0)
                err(1, "configuring /dev/net/tun");
 
+       if (ioctl(netfd, TUNSETOFFLOAD,
+                 TUN_F_CSUM|TUN_F_TSO4|TUN_F_TSO6|TUN_F_TSO_ECN) != 0)
+               err(1, "Could not set features for tun device");
+
        /* We don't need checksums calculated for packets coming in this
         * device: trust us! */
        ioctl(netfd, TUNSETNOCSUM, 1);
@@ -1561,6 +1550,16 @@ static void setup_tun_net(char *arg)
        /* Tell Guest what MAC address to use. */
        add_feature(dev, VIRTIO_NET_F_MAC);
        add_feature(dev, VIRTIO_F_NOTIFY_ON_EMPTY);
+       /* Expect Guest to handle everything except UFO */
+       add_feature(dev, VIRTIO_NET_F_CSUM);
+       add_feature(dev, VIRTIO_NET_F_GUEST_CSUM);
+       add_feature(dev, VIRTIO_NET_F_MAC);
+       add_feature(dev, VIRTIO_NET_F_GUEST_TSO4);
+       add_feature(dev, VIRTIO_NET_F_GUEST_TSO6);
+       add_feature(dev, VIRTIO_NET_F_GUEST_ECN);
+       add_feature(dev, VIRTIO_NET_F_HOST_TSO4);
+       add_feature(dev, VIRTIO_NET_F_HOST_TSO6);
+       add_feature(dev, VIRTIO_NET_F_HOST_ECN);
        set_config(dev, sizeof(conf), &conf);
 
        /* We don't need the socket any more; setup is done. */
@@ -1874,11 +1873,12 @@ static void __attribute__((noreturn)) restart_guest(void)
 {
        unsigned int i;
 
-       /* Closing pipes causes the Waker thread and io_threads to die, and
-        * closing /dev/lguest cleans up the Guest.  Since we don't track all
-        * open fds, we simply close everything beyond stderr. */
+       /* Since we don't track all open fds, we simply close everything beyond
+        * stderr. */
        for (i = 3; i < FD_SETSIZE; i++)
                close(i);
+
+       /* The exec automatically gets rid of the I/O and Waker threads. */
        execv(main_args[0], main_args);
        err(1, "Could not exec %s", main_args[0]);
 }
@@ -2079,10 +2079,10 @@ int main(int argc, char *argv[])
         * /dev/lguest file descriptor. */
        lguest_fd = tell_kernel(pgdir, start);
 
-       /* We fork off a child process, which wakes the Launcher whenever one
-        * of the input file descriptors needs attention.  We call this the
-        * Waker, and we'll cover it in a moment. */
-       waker_fd = setup_waker(lguest_fd);
+       /* We clone off a thread, which wakes the Launcher whenever one of the
+        * input file descriptors needs attention.  We call this the Waker, and
+        * we'll cover it in a moment. */
+       setup_waker(lguest_fd);
 
        /* Finally, run the Guest.  This doesn't return. */
        run_guest(lguest_fd);