rumpclient.c revision 1.1 1 1.1 pooka /* $NetBSD: rumpclient.c,v 1.1 2010/11/04 21:01:29 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.1 pooka #include <sys/types.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.1 pooka send_syscall_req(struct spclient *spc, int sysnum,
62 1.1 pooka const void *data, size_t dlen)
63 1.1 pooka {
64 1.1 pooka struct rsp_hdr rhdr;
65 1.1 pooka
66 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + dlen;
67 1.1 pooka rhdr.rsp_reqno = nextreq++;
68 1.1 pooka rhdr.rsp_type = RUMPSP_SYSCALL_REQ;
69 1.1 pooka rhdr.rsp_sysnum = sysnum;
70 1.1 pooka
71 1.1 pooka dosend(spc, &rhdr, sizeof(rhdr));
72 1.1 pooka dosend(spc, data, dlen);
73 1.1 pooka
74 1.1 pooka return 0;
75 1.1 pooka }
76 1.1 pooka
77 1.1 pooka static int
78 1.1 pooka send_copyin_resp(struct spclient *spc, uint64_t reqno, void *data, size_t dlen)
79 1.1 pooka {
80 1.1 pooka struct rsp_hdr rhdr;
81 1.1 pooka
82 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + dlen;
83 1.1 pooka rhdr.rsp_reqno = reqno;
84 1.1 pooka rhdr.rsp_type = RUMPSP_COPYIN_RESP;
85 1.1 pooka rhdr.rsp_sysnum = 0;
86 1.1 pooka
87 1.1 pooka dosend(spc, &rhdr, sizeof(rhdr));
88 1.1 pooka dosend(spc, data, dlen);
89 1.1 pooka
90 1.1 pooka return 0;
91 1.1 pooka }
92 1.1 pooka
93 1.1 pooka static int
94 1.1 pooka send_anonmmap_resp(struct spclient *spc, uint64_t reqno, void *addr)
95 1.1 pooka {
96 1.1 pooka struct rsp_hdr rhdr;
97 1.1 pooka
98 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(addr);
99 1.1 pooka rhdr.rsp_reqno = reqno;
100 1.1 pooka rhdr.rsp_type = RUMPSP_ANONMMAP_RESP;
101 1.1 pooka rhdr.rsp_sysnum = 0;
102 1.1 pooka
103 1.1 pooka dosend(spc, &rhdr, sizeof(rhdr));
104 1.1 pooka dosend(spc, &addr, sizeof(addr));
105 1.1 pooka
106 1.1 pooka return 0;
107 1.1 pooka }
108 1.1 pooka
109 1.1 pooka int
110 1.1 pooka rumpclient_syscall(int sysnum, const void *data, size_t dlen,
111 1.1 pooka register_t *retval)
112 1.1 pooka {
113 1.1 pooka struct rsp_sysresp *resp;
114 1.1 pooka struct rsp_copydata *copydata;
115 1.1 pooka struct pollfd pfd;
116 1.1 pooka size_t maplen;
117 1.1 pooka void *mapaddr;
118 1.1 pooka int gotresp;
119 1.1 pooka
120 1.1 pooka DPRINTF(("rump_sp_syscall: executing syscall %d\n", sysnum));
121 1.1 pooka
122 1.1 pooka send_syscall_req(&clispc, sysnum, data, dlen);
123 1.1 pooka
124 1.1 pooka DPRINTF(("rump_sp_syscall: syscall %d request sent. "
125 1.1 pooka "waiting for response\n", sysnum));
126 1.1 pooka
127 1.1 pooka pfd.fd = clispc.spc_fd;
128 1.1 pooka pfd.events = POLLIN;
129 1.1 pooka
130 1.1 pooka gotresp = 0;
131 1.1 pooka while (!gotresp) {
132 1.1 pooka while (readframe(&clispc) < 1)
133 1.1 pooka poll(&pfd, 1, INFTIM);
134 1.1 pooka
135 1.1 pooka switch (clispc.spc_hdr.rsp_type) {
136 1.1 pooka case RUMPSP_COPYIN_REQ:
137 1.1 pooka /*LINTED*/
138 1.1 pooka copydata = (struct rsp_copydata *)clispc.spc_buf;
139 1.1 pooka DPRINTF(("rump_sp_syscall: copyin request: %p/%zu\n",
140 1.1 pooka copydata->rcp_addr, copydata->rcp_len));
141 1.1 pooka send_copyin_resp(&clispc, clispc.spc_hdr.rsp_reqno,
142 1.1 pooka copydata->rcp_addr, copydata->rcp_len);
143 1.1 pooka clispc.spc_off = 0;
144 1.1 pooka break;
145 1.1 pooka case RUMPSP_COPYOUT_REQ:
146 1.1 pooka /*LINTED*/
147 1.1 pooka copydata = (struct rsp_copydata *)clispc.spc_buf;
148 1.1 pooka DPRINTF(("rump_sp_syscall: copyout request: %p/%zu\n",
149 1.1 pooka copydata->rcp_addr, copydata->rcp_len));
150 1.1 pooka /*LINTED*/
151 1.1 pooka memcpy(copydata->rcp_addr, copydata->rcp_data,
152 1.1 pooka copydata->rcp_len);
153 1.1 pooka clispc.spc_off = 0;
154 1.1 pooka break;
155 1.1 pooka case RUMPSP_ANONMMAP_REQ:
156 1.1 pooka /*LINTED*/
157 1.1 pooka maplen = *(size_t *)clispc.spc_buf;
158 1.1 pooka mapaddr = mmap(NULL, maplen, PROT_READ|PROT_WRITE,
159 1.1 pooka MAP_ANON, -1, 0);
160 1.1 pooka if (mapaddr == MAP_FAILED)
161 1.1 pooka mapaddr = NULL;
162 1.1 pooka send_anonmmap_resp(&clispc,
163 1.1 pooka clispc.spc_hdr.rsp_reqno, mapaddr);
164 1.1 pooka clispc.spc_off = 0;
165 1.1 pooka break;
166 1.1 pooka case RUMPSP_SYSCALL_RESP:
167 1.1 pooka DPRINTF(("rump_sp_syscall: got response \n"));
168 1.1 pooka gotresp = 1;
169 1.1 pooka break;
170 1.1 pooka }
171 1.1 pooka }
172 1.1 pooka
173 1.1 pooka /*LINTED*/
174 1.1 pooka resp = (struct rsp_sysresp *)clispc.spc_buf;
175 1.1 pooka memcpy(retval, &resp->rsys_retval, sizeof(resp->rsys_retval));
176 1.1 pooka clispc.spc_off = 0;
177 1.1 pooka
178 1.1 pooka return resp->rsys_error;
179 1.1 pooka }
180 1.1 pooka
181 1.1 pooka int
182 1.1 pooka rumpclient_init()
183 1.1 pooka {
184 1.1 pooka struct sockaddr *sap;
185 1.1 pooka char *p;
186 1.1 pooka unsigned idx;
187 1.1 pooka int error, s;
188 1.1 pooka
189 1.1 pooka if ((p = getenv("RUMP_SP_CLIENT")) == NULL)
190 1.1 pooka return ENOENT;
191 1.1 pooka
192 1.1 pooka if ((error = parseurl(p, &sap, &idx, 0)) != 0)
193 1.1 pooka return error;
194 1.1 pooka
195 1.1 pooka s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
196 1.1 pooka if (s == -1)
197 1.1 pooka return errno;
198 1.1 pooka
199 1.1 pooka if (connect(s, sap, sap->sa_len) == -1) {
200 1.1 pooka fprintf(stderr, "rump_sp: client connect failed\n");
201 1.1 pooka return errno;
202 1.1 pooka }
203 1.1 pooka if ((error = parsetab[idx].connhook(s)) != 0) {
204 1.1 pooka fprintf(stderr, "rump_sp: connect hook failed\n");
205 1.1 pooka return error;
206 1.1 pooka }
207 1.1 pooka
208 1.1 pooka clispc.spc_fd = s;
209 1.1 pooka
210 1.1 pooka return 0;
211 1.1 pooka }
212