Home | History | Annotate | Line # | Download | only in librumpclient
rumpclient.c revision 1.5
      1  1.5  pooka /*      $NetBSD: rumpclient.c,v 1.5 2010/11/25 17:59:03 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka /*
     29  1.1  pooka  * Client side routines for rump syscall proxy.
     30  1.1  pooka  */
     31  1.1  pooka 
     32  1.1  pooka #include <sys/cdefs.h>
     33  1.1  pooka __RCSID("$NetBSD");
     34  1.1  pooka 
     35  1.5  pooka #include <sys/param.h>
     36  1.1  pooka #include <sys/mman.h>
     37  1.1  pooka #include <sys/socket.h>
     38  1.1  pooka 
     39  1.1  pooka #include <arpa/inet.h>
     40  1.1  pooka #include <netinet/in.h>
     41  1.1  pooka #include <netinet/tcp.h>
     42  1.1  pooka 
     43  1.1  pooka #include <assert.h>
     44  1.1  pooka #include <errno.h>
     45  1.1  pooka #include <fcntl.h>
     46  1.1  pooka #include <poll.h>
     47  1.1  pooka #include <pthread.h>
     48  1.1  pooka #include <stdarg.h>
     49  1.1  pooka #include <stdio.h>
     50  1.1  pooka #include <stdlib.h>
     51  1.1  pooka #include <string.h>
     52  1.1  pooka #include <unistd.h>
     53  1.1  pooka 
     54  1.1  pooka #include <rump/rumpclient.h>
     55  1.1  pooka 
     56  1.1  pooka #include "sp_common.c"
     57  1.1  pooka 
     58  1.1  pooka static struct spclient clispc;
     59  1.1  pooka 
     60  1.1  pooka static int
     61  1.3  pooka syscall_req(struct spclient *spc, int sysnum,
     62  1.3  pooka 	const void *data, size_t dlen, void **resp)
     63  1.1  pooka {
     64  1.1  pooka 	struct rsp_hdr rhdr;
     65  1.3  pooka 	struct respwait rw;
     66  1.3  pooka 	int rv;
     67  1.1  pooka 
     68  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
     69  1.3  pooka 	rhdr.rsp_class = RUMPSP_REQ;
     70  1.3  pooka 	rhdr.rsp_type = RUMPSP_SYSCALL;
     71  1.1  pooka 	rhdr.rsp_sysnum = sysnum;
     72  1.1  pooka 
     73  1.3  pooka 	putwait(spc, &rw, &rhdr);
     74  1.3  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
     75  1.3  pooka 	rv = dosend(spc, data, dlen);
     76  1.4  pooka 	if (rv) {
     77  1.4  pooka 		unputwait(spc, &rw);
     78  1.4  pooka 		return rv;
     79  1.4  pooka 	}
     80  1.3  pooka 
     81  1.3  pooka 	rv = waitresp(spc, &rw);
     82  1.3  pooka 	*resp = rw.rw_data;
     83  1.3  pooka 	return rv;
     84  1.1  pooka }
     85  1.1  pooka 
     86  1.1  pooka static int
     87  1.5  pooka send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen,
     88  1.5  pooka 	int wantstr)
     89  1.1  pooka {
     90  1.1  pooka 	struct rsp_hdr rhdr;
     91  1.3  pooka 	int rv;
     92  1.1  pooka 
     93  1.5  pooka 	if (wantstr)
     94  1.5  pooka 		dlen = MIN(dlen, strlen(data)+1);
     95  1.5  pooka 
     96  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + dlen;
     97  1.1  pooka 	rhdr.rsp_reqno = reqno;
     98  1.3  pooka 	rhdr.rsp_class = RUMPSP_RESP;
     99  1.3  pooka 	rhdr.rsp_type = RUMPSP_COPYIN;
    100  1.1  pooka 	rhdr.rsp_sysnum = 0;
    101  1.1  pooka 
    102  1.3  pooka 	sendlock(spc);
    103  1.3  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    104  1.3  pooka 	rv = dosend(spc, data, dlen);
    105  1.3  pooka 	sendunlock(spc);
    106  1.1  pooka 
    107  1.3  pooka 	return rv;
    108  1.1  pooka }
    109  1.1  pooka 
    110  1.1  pooka static int
    111  1.1  pooka send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
    112  1.1  pooka {
    113  1.1  pooka 	struct rsp_hdr rhdr;
    114  1.3  pooka 	int rv;
    115  1.1  pooka 
    116  1.1  pooka 	rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
    117  1.1  pooka 	rhdr.rsp_reqno = reqno;
    118  1.3  pooka 	rhdr.rsp_class = RUMPSP_RESP;
    119  1.3  pooka 	rhdr.rsp_type = RUMPSP_ANONMMAP;
    120  1.1  pooka 	rhdr.rsp_sysnum = 0;
    121  1.1  pooka 
    122  1.3  pooka 	sendlock(spc);
    123  1.3  pooka 	rv = dosend(spc, &rhdr, sizeof(rhdr));
    124  1.3  pooka 	rv = dosend(spc, &addr, sizeof(addr));
    125  1.3  pooka 	sendunlock(spc);
    126  1.1  pooka 
    127  1.3  pooka 	return rv;
    128  1.1  pooka }
    129  1.1  pooka 
    130  1.1  pooka int
    131  1.1  pooka rumpclient_syscall(int sysnum, const void *data, size_t dlen,
    132  1.1  pooka 	register_t *retval)
    133  1.1  pooka {
    134  1.1  pooka 	struct rsp_sysresp *resp;
    135  1.3  pooka 	void *rdata;
    136  1.3  pooka 	int rv;
    137  1.3  pooka 
    138  1.3  pooka 	DPRINTF(("rumpsp syscall_req: syscall %d with %p/%zu\n",
    139  1.3  pooka 	    sysnum, data, dlen));
    140  1.3  pooka 
    141  1.3  pooka 	rv = syscall_req(&clispc, sysnum, data, dlen, &rdata);
    142  1.3  pooka 	if (rv)
    143  1.3  pooka 		return rv;
    144  1.3  pooka 
    145  1.3  pooka 	resp = rdata;
    146  1.3  pooka 	DPRINTF(("rumpsp syscall_resp: syscall %d error %d, rv: %d/%d\n",
    147  1.3  pooka 	    sysnum, rv, resp->rsys_retval[0], resp->rsys_retval[1]));
    148  1.1  pooka 
    149  1.3  pooka 	memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
    150  1.3  pooka 	rv = resp->rsys_error;
    151  1.3  pooka 	free(rdata);
    152  1.1  pooka 
    153  1.3  pooka 	return rv;
    154  1.3  pooka }
    155  1.1  pooka 
    156  1.3  pooka static void
    157  1.3  pooka handlereq(struct spclient *spc)
    158  1.3  pooka {
    159  1.3  pooka 	struct rsp_copydata *copydata;
    160  1.3  pooka 	void *mapaddr;
    161  1.3  pooka 	size_t maplen;
    162  1.5  pooka 	int reqtype = spc->spc_hdr.rsp_type;
    163  1.1  pooka 
    164  1.5  pooka 	switch (reqtype) {
    165  1.3  pooka 	case RUMPSP_COPYIN:
    166  1.5  pooka 	case RUMPSP_COPYINSTR:
    167  1.3  pooka 		/*LINTED*/
    168  1.3  pooka 		copydata = (struct rsp_copydata *)spc->spc_buf;
    169  1.3  pooka 		DPRINTF(("rump_sp handlereq: copyin request: %p/%zu\n",
    170  1.3  pooka 		    copydata->rcp_addr, copydata->rcp_len));
    171  1.3  pooka 		send_copyin_resp(spc, spc->spc_hdr.rsp_reqno,
    172  1.5  pooka 		    copydata->rcp_addr, copydata->rcp_len,
    173  1.5  pooka 		    reqtype == RUMPSP_COPYINSTR);
    174  1.3  pooka 		break;
    175  1.3  pooka 	case RUMPSP_COPYOUT:
    176  1.5  pooka 	case RUMPSP_COPYOUTSTR:
    177  1.3  pooka 		/*LINTED*/
    178  1.3  pooka 		copydata = (struct rsp_copydata *)spc->spc_buf;
    179  1.3  pooka 		DPRINTF(("rump_sp handlereq: copyout request: %p/%zu\n",
    180  1.3  pooka 		    copydata->rcp_addr, copydata->rcp_len));
    181  1.3  pooka 		/*LINTED*/
    182  1.3  pooka 		memcpy(copydata->rcp_addr, copydata->rcp_data,
    183  1.3  pooka 		    copydata->rcp_len);
    184  1.3  pooka 		break;
    185  1.3  pooka 	case RUMPSP_ANONMMAP:
    186  1.3  pooka 		/*LINTED*/
    187  1.3  pooka 		maplen = *(size_t *)spc->spc_buf;
    188  1.3  pooka 		mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
    189  1.3  pooka 		    MAP_ANON, -1, 0);
    190  1.3  pooka 		if (mapaddr == MAP_FAILED)
    191  1.3  pooka 			mapaddr = NULL;
    192  1.3  pooka 		DPRINTF(("rump_sp handlereq: anonmmap: %p\n", mapaddr));
    193  1.3  pooka 		send_anonmmap_resp(spc, spc->spc_hdr.rsp_reqno, mapaddr);
    194  1.3  pooka 		break;
    195  1.3  pooka 	default:
    196  1.3  pooka 		printf("PANIC: INVALID TYPE\n");
    197  1.3  pooka 		abort();
    198  1.3  pooka 		break;
    199  1.1  pooka 	}
    200  1.1  pooka 
    201  1.3  pooka 	free(spc->spc_buf);
    202  1.3  pooka 	spc->spc_off = 0;
    203  1.3  pooka 	spc->spc_buf = NULL;
    204  1.1  pooka }
    205  1.1  pooka 
    206  1.1  pooka int
    207  1.1  pooka rumpclient_init()
    208  1.1  pooka {
    209  1.1  pooka 	struct sockaddr *sap;
    210  1.1  pooka 	char *p;
    211  1.1  pooka 	unsigned idx;
    212  1.1  pooka 	int error, s;
    213  1.1  pooka 
    214  1.2  pooka 	if ((p = getenv("RUMP_SP_CLIENT")) == NULL) {
    215  1.2  pooka 		errno = ENOENT;
    216  1.2  pooka 		return -1;
    217  1.2  pooka 	}
    218  1.1  pooka 
    219  1.2  pooka 	if ((error = parseurl(p, &sap, &idx, 0)) != 0) {
    220  1.2  pooka 		errno = error;
    221  1.2  pooka 		return -1;
    222  1.2  pooka 	}
    223  1.1  pooka 
    224  1.1  pooka 	s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
    225  1.1  pooka 	if (s == -1)
    226  1.2  pooka 		return -1;
    227  1.1  pooka 
    228  1.1  pooka 	if (connect(s, sap, sap->sa_len) == -1) {
    229  1.2  pooka 		error = errno;
    230  1.1  pooka 		fprintf(stderr, "rump_sp: client connect failed\n");
    231  1.2  pooka 		errno = error;
    232  1.2  pooka 		return -1;
    233  1.1  pooka 	}
    234  1.4  pooka 
    235  1.1  pooka 	if ((error = parsetab[idx].connhook(s)) != 0) {
    236  1.2  pooka 		error = errno;
    237  1.1  pooka 		fprintf(stderr, "rump_sp: connect hook failed\n");
    238  1.2  pooka 		errno = error;
    239  1.2  pooka 		return -1;
    240  1.1  pooka 	}
    241  1.1  pooka 	clispc.spc_fd = s;
    242  1.3  pooka 	TAILQ_INIT(&clispc.spc_respwait);
    243  1.3  pooka 	pthread_mutex_init(&clispc.spc_mtx, NULL);
    244  1.3  pooka 	pthread_cond_init(&clispc.spc_cv, NULL);
    245  1.1  pooka 
    246  1.1  pooka 	return 0;
    247  1.1  pooka }
    248