Home | History | Annotate | Line # | Download | only in libpuffs
requests.c revision 1.18
      1  1.18  dogcow /*	$NetBSD: requests.c,v 1.18 2007/12/05 04:29:10 dogcow Exp $	*/
      2   1.1   pooka 
      3   1.1   pooka /*
      4  1.17   pooka  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  1.17   pooka  *
      6  1.17   pooka  * Development of this software was supported by the
      7  1.17   pooka  * Research Foundation of Helsinki University of Technology
      8   1.1   pooka  *
      9   1.1   pooka  * Redistribution and use in source and binary forms, with or without
     10   1.1   pooka  * modification, are permitted provided that the following conditions
     11   1.1   pooka  * are met:
     12   1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     13   1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     14   1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     16   1.1   pooka  *    documentation and/or other materials provided with the distribution.
     17   1.1   pooka  *
     18   1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19   1.1   pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20   1.1   pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21   1.1   pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22   1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24   1.1   pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1   pooka  * SUCH DAMAGE.
     29   1.1   pooka  */
     30   1.1   pooka 
     31   1.1   pooka #include <sys/cdefs.h>
     32   1.1   pooka #if !defined(lint)
     33  1.18  dogcow __RCSID("$NetBSD: requests.c,v 1.18 2007/12/05 04:29:10 dogcow Exp $");
     34   1.1   pooka #endif /* !lint */
     35   1.1   pooka 
     36   1.1   pooka #include <sys/types.h>
     37   1.1   pooka #include <sys/ioctl.h>
     38   1.2   pooka #include <sys/queue.h>
     39  1.17   pooka #include <sys/socket.h>
     40  1.17   pooka 
     41  1.17   pooka #include <dev/putter/putter.h>
     42   1.1   pooka 
     43   1.2   pooka #include <assert.h>
     44  1.10   pooka #include <errno.h>
     45   1.1   pooka #include <puffs.h>
     46   1.1   pooka #include <stdio.h>
     47   1.1   pooka #include <stdlib.h>
     48  1.10   pooka #include <unistd.h>
     49   1.1   pooka 
     50   1.1   pooka #include "puffs_priv.h"
     51   1.1   pooka 
     52  1.10   pooka /*
     53  1.17   pooka  * Read a frame from the upstream provider.  First read the frame
     54  1.17   pooka  * length and after this read the actual contents.  Yes, optimize
     55  1.17   pooka  * me some day.
     56  1.10   pooka  */
     57  1.10   pooka /*ARGSUSED*/
     58  1.17   pooka int
     59  1.17   pooka puffs_fsframe_read(struct puffs_usermount *pu, struct puffs_framebuf *pb,
     60  1.17   pooka 	int fd, int *done)
     61   1.1   pooka {
     62  1.17   pooka 	struct putter_hdr phdr;
     63  1.17   pooka 	void *win;
     64  1.17   pooka 	size_t howmuch, winlen, curoff;
     65  1.17   pooka 	ssize_t n;
     66  1.17   pooka 	int lenstate;
     67   1.1   pooka 
     68  1.17   pooka 	puffs_framebuf_reserve_space(pb, PUFFS_MSG_MAXSIZE);
     69   1.1   pooka 
     70  1.17   pooka 	/* How much to read? */
     71  1.17   pooka  the_next_level:
     72  1.17   pooka 	curoff = puffs_framebuf_telloff(pb);
     73  1.17   pooka 	if (curoff < sizeof(struct putter_hdr)) {
     74  1.17   pooka 		howmuch = sizeof(struct putter_hdr) - curoff;
     75  1.17   pooka 		lenstate = 1;
     76  1.17   pooka 	} else {
     77  1.17   pooka 		puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
     78  1.17   pooka 		/*LINTED*/
     79  1.17   pooka 		howmuch = phdr.pth_framelen - curoff;
     80  1.17   pooka 		lenstate = 0;
     81  1.17   pooka 	}
     82   1.2   pooka 
     83  1.17   pooka 	if (puffs_framebuf_reserve_space(pb, howmuch) == -1)
     84  1.17   pooka 		return errno;
     85   1.2   pooka 
     86  1.17   pooka 	/* Read contents */
     87  1.17   pooka 	while (howmuch) {
     88  1.17   pooka 		winlen = howmuch;
     89  1.17   pooka 		curoff = puffs_framebuf_telloff(pb);
     90  1.17   pooka 		if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
     91  1.17   pooka 			return errno;
     92  1.17   pooka 		n = read(fd, win, winlen);
     93  1.17   pooka 		switch (n) {
     94  1.17   pooka 		case 0:
     95  1.17   pooka 			return ECONNRESET;
     96  1.17   pooka 		case -1:
     97  1.17   pooka 			if (errno == EAGAIN)
     98  1.17   pooka 				return 0;
     99  1.17   pooka 			return errno;
    100  1.17   pooka 		default:
    101  1.17   pooka 			howmuch -= n;
    102  1.17   pooka 			puffs_framebuf_seekset(pb, curoff + n);
    103  1.17   pooka 			break;
    104  1.17   pooka 		}
    105  1.10   pooka 	}
    106   1.1   pooka 
    107  1.17   pooka 	if (lenstate)
    108  1.17   pooka 		goto the_next_level;
    109  1.17   pooka 
    110  1.17   pooka 	puffs_framebuf_seekset(pb, 0);
    111  1.17   pooka 	*done = 1;
    112   1.2   pooka 	return 0;
    113   1.1   pooka }
    114   1.1   pooka 
    115  1.17   pooka /*
    116  1.17   pooka  * Write a frame upstream
    117  1.17   pooka  */
    118  1.17   pooka /*ARGSUSED*/
    119   1.1   pooka int
    120  1.17   pooka puffs_fsframe_write(struct puffs_usermount *pu, struct puffs_framebuf *pb,
    121  1.17   pooka 	int fd, int *done)
    122   1.1   pooka {
    123  1.17   pooka 	void *win;
    124  1.17   pooka 	uint64_t flen;
    125  1.17   pooka 	size_t winlen, howmuch, curoff;
    126  1.17   pooka 	ssize_t n;
    127  1.17   pooka 	int rv;
    128   1.1   pooka 
    129  1.17   pooka 	/*
    130  1.17   pooka 	 * Finalize it if we haven't written anything yet (or we're still
    131  1.17   pooka 	 * attempting to write the first byte)
    132  1.17   pooka 	 *
    133  1.17   pooka 	 * XXX: this shouldn't be here
    134  1.17   pooka 	 */
    135  1.17   pooka 	if (puffs_framebuf_telloff(pb) == 0) {
    136  1.18  dogcow 		void *preq;
    137  1.17   pooka 
    138  1.17   pooka 		winlen = sizeof(struct puffs_req);
    139  1.17   pooka 		rv = puffs_framebuf_getwindow(pb, 0, (void **)&preq, &winlen);
    140  1.17   pooka 		if (rv == -1)
    141  1.17   pooka 			return errno;
    142  1.18  dogcow 		flen = ((struct puffs_req *)preq)->preq_buflen;
    143  1.18  dogcow 		((struct puffs_req *)preq)->preq_pth.pth_framelen = flen;
    144  1.17   pooka 	} else {
    145  1.17   pooka 		struct putter_hdr phdr;
    146   1.1   pooka 
    147  1.17   pooka 		puffs_framebuf_getdata_atoff(pb, 0, &phdr, sizeof(phdr));
    148  1.17   pooka 		flen = phdr.pth_framelen;
    149  1.17   pooka 	}
    150   1.2   pooka 
    151  1.17   pooka 	/*
    152  1.17   pooka 	 * Then write it.  Chances are if we are talking to the kernel it'll
    153  1.17   pooka 	 * just shlosh in all at once, but if we're e.g. talking to the
    154  1.17   pooka 	 * network it might take a few tries.
    155  1.17   pooka 	 */
    156  1.17   pooka 	/*LINTED*/
    157  1.17   pooka 	howmuch = flen - puffs_framebuf_telloff(pb);
    158  1.17   pooka 
    159  1.17   pooka 	while (howmuch) {
    160  1.17   pooka 		winlen = howmuch;
    161  1.17   pooka 		curoff = puffs_framebuf_telloff(pb);
    162  1.17   pooka 		if (puffs_framebuf_getwindow(pb, curoff, &win, &winlen) == -1)
    163  1.17   pooka 			return errno;
    164  1.17   pooka 
    165  1.17   pooka 		/*
    166  1.17   pooka 		 * XXX: we know from the framebuf implementation that we
    167  1.17   pooka 		 * will always managed to map the entire window.  But if
    168  1.17   pooka 		 * that changes, this will catch it.  Then we can do stuff
    169  1.17   pooka 		 * iov stuff instead.
    170  1.17   pooka 		 */
    171  1.17   pooka 		assert(winlen == howmuch);
    172  1.17   pooka 
    173  1.17   pooka 		/* XXX: want NOSIGNAL if writing to a pipe */
    174  1.17   pooka #if 0
    175  1.17   pooka 		n = send(fd, win, winlen, MSG_NOSIGNAL);
    176  1.17   pooka #else
    177  1.17   pooka 		n = write(fd, win, winlen);
    178  1.17   pooka #endif
    179  1.17   pooka 		switch (n) {
    180  1.17   pooka 		case 0:
    181  1.17   pooka 			return ECONNRESET;
    182  1.17   pooka 		case -1:
    183  1.17   pooka 			if (errno == EAGAIN)
    184  1.17   pooka 				return 0;
    185  1.17   pooka 			return errno;
    186  1.17   pooka 		default:
    187  1.17   pooka 			howmuch -= n;
    188  1.17   pooka 			puffs_framebuf_seekset(pb, curoff + n);
    189  1.17   pooka 			break;
    190  1.17   pooka 		}
    191  1.17   pooka 	}
    192   1.2   pooka 
    193  1.17   pooka 	*done = 1;
    194  1.17   pooka 	return 0;
    195   1.1   pooka }
    196   1.1   pooka 
    197   1.2   pooka /*
    198  1.17   pooka  * Compare if "pb1" is a response to a previously sent frame pb2.
    199  1.17   pooka  * More often than not "pb1" is not a response to anything but
    200  1.17   pooka  * rather a fresh request from the kernel.
    201   1.2   pooka  */
    202  1.10   pooka /*ARGSUSED*/
    203   1.1   pooka int
    204  1.17   pooka puffs_fsframe_cmp(struct puffs_usermount *pu,
    205  1.17   pooka 	struct puffs_framebuf *pb1, struct puffs_framebuf *pb2, int *notresp)
    206   1.1   pooka {
    207  1.18  dogcow 	void *preq1, *preq2;
    208  1.17   pooka 	size_t winlen;
    209  1.17   pooka 	int rv;
    210  1.17   pooka 
    211  1.17   pooka 	/* map incoming preq */
    212  1.17   pooka 	winlen = sizeof(struct puffs_req);
    213  1.18  dogcow 	rv = puffs_framebuf_getwindow(pb1, 0, &preq1, &winlen);
    214  1.17   pooka 	assert(rv == 0); /* frames are always at least puffs_req in size */
    215  1.17   pooka 	assert(winlen = sizeof(struct puffs_req));
    216  1.17   pooka 
    217  1.17   pooka 	/*
    218  1.17   pooka 	 * Check if this is not a response in this slot.  That's the
    219  1.17   pooka 	 * likely case.
    220  1.17   pooka 	 */
    221  1.18  dogcow 	if ((((struct puffs_req *)preq1)->preq_opclass & PUFFSOPFLAG_ISRESPONSE) == 0) {
    222  1.17   pooka 		*notresp = 1;
    223  1.17   pooka 		return 0;
    224  1.17   pooka 	}
    225   1.1   pooka 
    226  1.17   pooka 	/* map second preq */
    227  1.17   pooka 	winlen = sizeof(struct puffs_req);
    228  1.18  dogcow 	rv = puffs_framebuf_getwindow(pb2, 0, &preq2, &winlen);
    229  1.17   pooka 	assert(rv == 0); /* frames are always at least puffs_req in size */
    230  1.17   pooka 	assert(winlen = sizeof(struct puffs_req));
    231   1.1   pooka 
    232  1.17   pooka 	/* then compare: resid equal? */
    233  1.18  dogcow 	return ((struct puffs_req *)preq1)->preq_id ==
    234  1.18  dogcow 	    ((struct puffs_req *)preq2)->preq_id;
    235   1.2   pooka }
    236   1.2   pooka 
    237   1.2   pooka void
    238  1.17   pooka puffs_fsframe_gotframe(struct puffs_usermount *pu, struct puffs_framebuf *pb)
    239   1.1   pooka {
    240  1.17   pooka 	struct puffs_framebuf *newpb;
    241   1.1   pooka 
    242  1.17   pooka 	if ((newpb = puffs_framebuf_make()) == NULL)
    243  1.17   pooka 		abort();
    244  1.17   pooka 	/* XXX: optimize */
    245  1.17   pooka 	puffs__framebuf_moveinfo(pb, newpb);
    246  1.17   pooka 	puffs_framebuf_seekset(newpb, 0);
    247  1.17   pooka 	if (puffs_framebuf_reserve_space(newpb, PUFFS_MSG_MAXSIZE) == -1)
    248  1.17   pooka 		abort();
    249   1.3   pooka 
    250  1.17   pooka 	puffs_dopufbuf(pu, newpb);
    251   1.3   pooka }
    252