rumpuser_sp.c revision 1.28 1 1.28 pooka /* $NetBSD: rumpuser_sp.c,v 1.28 2011/01/02 13:01:45 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 * Sysproxy routines. This provides system RPC support over host sockets.
30 1.1 pooka * The most notable limitation is that the client and server must share
31 1.1 pooka * the same ABI. This does not mean that they have to be the same
32 1.1 pooka * machine or that they need to run the same version of the host OS,
33 1.1 pooka * just that they must agree on the data structures. This even *might*
34 1.1 pooka * work correctly from one hardware architecture to another.
35 1.1 pooka */
36 1.1 pooka
37 1.1 pooka #include <sys/cdefs.h>
38 1.28 pooka __RCSID("$NetBSD: rumpuser_sp.c,v 1.28 2011/01/02 13:01:45 pooka Exp $");
39 1.1 pooka
40 1.1 pooka #include <sys/types.h>
41 1.11 pooka #include <sys/atomic.h>
42 1.1 pooka #include <sys/mman.h>
43 1.1 pooka #include <sys/socket.h>
44 1.1 pooka
45 1.1 pooka #include <arpa/inet.h>
46 1.1 pooka #include <netinet/in.h>
47 1.1 pooka #include <netinet/tcp.h>
48 1.1 pooka
49 1.1 pooka #include <assert.h>
50 1.1 pooka #include <errno.h>
51 1.1 pooka #include <fcntl.h>
52 1.1 pooka #include <poll.h>
53 1.1 pooka #include <pthread.h>
54 1.1 pooka #include <stdarg.h>
55 1.1 pooka #include <stdio.h>
56 1.1 pooka #include <stdlib.h>
57 1.1 pooka #include <string.h>
58 1.1 pooka #include <unistd.h>
59 1.1 pooka
60 1.28 pooka #include <rump/rump.h> /* XXX: for rfork flags */
61 1.1 pooka #include <rump/rumpuser.h>
62 1.13 pooka #include "rumpuser_int.h"
63 1.1 pooka
64 1.5 pooka #include "sp_common.c"
65 1.1 pooka
66 1.20 pooka #ifndef MAXCLI
67 1.18 pooka #define MAXCLI 256
68 1.20 pooka #endif
69 1.20 pooka #ifndef MAXWORKER
70 1.20 pooka #define MAXWORKER 128
71 1.20 pooka #endif
72 1.20 pooka #ifndef IDLEWORKER
73 1.20 pooka #define IDLEWORKER 16
74 1.20 pooka #endif
75 1.20 pooka int rumpsp_maxworker = MAXWORKER;
76 1.20 pooka int rumpsp_idleworker = IDLEWORKER;
77 1.1 pooka
78 1.1 pooka static struct pollfd pfdlist[MAXCLI];
79 1.1 pooka static struct spclient spclist[MAXCLI];
80 1.11 pooka static unsigned int disco;
81 1.24 pooka static volatile int spfini;
82 1.1 pooka
83 1.3 pooka static struct rumpuser_sp_ops spops;
84 1.3 pooka
85 1.26 pooka static char banner[MAXBANNER];
86 1.26 pooka
87 1.26 pooka #define PROTOMAJOR 0
88 1.26 pooka #define PROTOMINOR 0
89 1.26 pooka
90 1.3 pooka /*
91 1.3 pooka * Manual wrappers, since librump does not have access to the
92 1.3 pooka * user namespace wrapped interfaces.
93 1.3 pooka */
94 1.3 pooka
95 1.3 pooka static void
96 1.3 pooka lwproc_switch(struct lwp *l)
97 1.3 pooka {
98 1.3 pooka
99 1.4 pooka spops.spop_schedule();
100 1.3 pooka spops.spop_lwproc_switch(l);
101 1.4 pooka spops.spop_unschedule();
102 1.3 pooka }
103 1.3 pooka
104 1.3 pooka static void
105 1.3 pooka lwproc_release(void)
106 1.3 pooka {
107 1.3 pooka
108 1.4 pooka spops.spop_schedule();
109 1.3 pooka spops.spop_lwproc_release();
110 1.4 pooka spops.spop_unschedule();
111 1.3 pooka }
112 1.3 pooka
113 1.3 pooka static int
114 1.28 pooka lwproc_rfork(struct spclient *spc, int flags)
115 1.3 pooka {
116 1.3 pooka int rv;
117 1.3 pooka
118 1.4 pooka spops.spop_schedule();
119 1.28 pooka rv = spops.spop_lwproc_rfork(spc, flags);
120 1.4 pooka spops.spop_unschedule();
121 1.3 pooka
122 1.3 pooka return rv;
123 1.3 pooka }
124 1.3 pooka
125 1.8 pooka static int
126 1.8 pooka lwproc_newlwp(pid_t pid)
127 1.8 pooka {
128 1.8 pooka int rv;
129 1.8 pooka
130 1.8 pooka spops.spop_schedule();
131 1.8 pooka rv = spops.spop_lwproc_newlwp(pid);
132 1.8 pooka spops.spop_unschedule();
133 1.8 pooka
134 1.8 pooka return rv;
135 1.8 pooka }
136 1.8 pooka
137 1.3 pooka static struct lwp *
138 1.3 pooka lwproc_curlwp(void)
139 1.3 pooka {
140 1.3 pooka struct lwp *l;
141 1.3 pooka
142 1.4 pooka spops.spop_schedule();
143 1.3 pooka l = spops.spop_lwproc_curlwp();
144 1.4 pooka spops.spop_unschedule();
145 1.3 pooka
146 1.3 pooka return l;
147 1.3 pooka }
148 1.3 pooka
149 1.8 pooka static pid_t
150 1.8 pooka lwproc_getpid(void)
151 1.8 pooka {
152 1.8 pooka pid_t p;
153 1.8 pooka
154 1.8 pooka spops.spop_schedule();
155 1.8 pooka p = spops.spop_getpid();
156 1.8 pooka spops.spop_unschedule();
157 1.8 pooka
158 1.8 pooka return p;
159 1.8 pooka }
160 1.8 pooka
161 1.3 pooka static int
162 1.3 pooka rumpsyscall(int sysnum, void *data, register_t *retval)
163 1.3 pooka {
164 1.3 pooka int rv;
165 1.3 pooka
166 1.4 pooka spops.spop_schedule();
167 1.3 pooka rv = spops.spop_syscall(sysnum, data, retval);
168 1.4 pooka spops.spop_unschedule();
169 1.3 pooka
170 1.3 pooka return rv;
171 1.3 pooka }
172 1.1 pooka
173 1.7 pooka static uint64_t
174 1.7 pooka nextreq(struct spclient *spc)
175 1.7 pooka {
176 1.7 pooka uint64_t nw;
177 1.7 pooka
178 1.7 pooka pthread_mutex_lock(&spc->spc_mtx);
179 1.7 pooka nw = spc->spc_nextreq++;
180 1.7 pooka pthread_mutex_unlock(&spc->spc_mtx);
181 1.7 pooka
182 1.7 pooka return nw;
183 1.7 pooka }
184 1.7 pooka
185 1.21 pooka static void
186 1.21 pooka send_error_resp(struct spclient *spc, uint64_t reqno, int error)
187 1.21 pooka {
188 1.21 pooka struct rsp_hdr rhdr;
189 1.21 pooka
190 1.21 pooka rhdr.rsp_len = sizeof(rhdr);
191 1.21 pooka rhdr.rsp_reqno = reqno;
192 1.21 pooka rhdr.rsp_class = RUMPSP_ERROR;
193 1.21 pooka rhdr.rsp_type = 0;
194 1.21 pooka rhdr.rsp_error = error;
195 1.21 pooka
196 1.21 pooka sendlock(spc);
197 1.21 pooka (void)dosend(spc, &rhdr, sizeof(rhdr));
198 1.21 pooka sendunlock(spc);
199 1.21 pooka }
200 1.21 pooka
201 1.1 pooka static int
202 1.27 pooka send_handshake_resp(struct spclient *spc, uint64_t reqno, int error)
203 1.27 pooka {
204 1.27 pooka struct rsp_hdr rhdr;
205 1.27 pooka int rv;
206 1.27 pooka
207 1.27 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(error);
208 1.27 pooka rhdr.rsp_reqno = reqno;
209 1.27 pooka rhdr.rsp_class = RUMPSP_RESP;
210 1.27 pooka rhdr.rsp_type = RUMPSP_HANDSHAKE;
211 1.27 pooka rhdr.rsp_error = 0;
212 1.27 pooka
213 1.27 pooka sendlock(spc);
214 1.27 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
215 1.27 pooka rv = dosend(spc, &error, sizeof(error));
216 1.27 pooka sendunlock(spc);
217 1.27 pooka
218 1.27 pooka return rv;
219 1.27 pooka }
220 1.27 pooka
221 1.27 pooka static int
222 1.1 pooka send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
223 1.7 pooka register_t *retval)
224 1.1 pooka {
225 1.1 pooka struct rsp_hdr rhdr;
226 1.1 pooka struct rsp_sysresp sysresp;
227 1.7 pooka int rv;
228 1.1 pooka
229 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
230 1.1 pooka rhdr.rsp_reqno = reqno;
231 1.7 pooka rhdr.rsp_class = RUMPSP_RESP;
232 1.7 pooka rhdr.rsp_type = RUMPSP_SYSCALL;
233 1.1 pooka rhdr.rsp_sysnum = 0;
234 1.1 pooka
235 1.1 pooka sysresp.rsys_error = error;
236 1.7 pooka memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
237 1.1 pooka
238 1.7 pooka sendlock(spc);
239 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
240 1.7 pooka rv = dosend(spc, &sysresp, sizeof(sysresp));
241 1.7 pooka sendunlock(spc);
242 1.1 pooka
243 1.7 pooka return rv;
244 1.1 pooka }
245 1.1 pooka
246 1.1 pooka static int
247 1.15 pooka copyin_req(struct spclient *spc, const void *remaddr, size_t *dlen,
248 1.15 pooka int wantstr, void **resp)
249 1.1 pooka {
250 1.1 pooka struct rsp_hdr rhdr;
251 1.1 pooka struct rsp_copydata copydata;
252 1.7 pooka struct respwait rw;
253 1.7 pooka int rv;
254 1.7 pooka
255 1.15 pooka DPRINTF(("copyin_req: %zu bytes from %p\n", *dlen, remaddr));
256 1.1 pooka
257 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
258 1.7 pooka rhdr.rsp_class = RUMPSP_REQ;
259 1.15 pooka if (wantstr)
260 1.15 pooka rhdr.rsp_type = RUMPSP_COPYINSTR;
261 1.15 pooka else
262 1.15 pooka rhdr.rsp_type = RUMPSP_COPYIN;
263 1.1 pooka rhdr.rsp_sysnum = 0;
264 1.1 pooka
265 1.1 pooka copydata.rcp_addr = __UNCONST(remaddr);
266 1.15 pooka copydata.rcp_len = *dlen;
267 1.1 pooka
268 1.7 pooka putwait(spc, &rw, &rhdr);
269 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
270 1.7 pooka rv = dosend(spc, ©data, sizeof(copydata));
271 1.13 pooka if (rv) {
272 1.13 pooka unputwait(spc, &rw);
273 1.13 pooka return rv;
274 1.13 pooka }
275 1.7 pooka
276 1.7 pooka rv = waitresp(spc, &rw);
277 1.7 pooka
278 1.7 pooka DPRINTF(("copyin: response %d\n", rv));
279 1.7 pooka
280 1.7 pooka *resp = rw.rw_data;
281 1.15 pooka if (wantstr)
282 1.15 pooka *dlen = rw.rw_dlen;
283 1.15 pooka
284 1.7 pooka return rv;
285 1.1 pooka
286 1.1 pooka }
287 1.1 pooka
288 1.1 pooka static int
289 1.1 pooka send_copyout_req(struct spclient *spc, const void *remaddr,
290 1.1 pooka const void *data, size_t dlen)
291 1.1 pooka {
292 1.1 pooka struct rsp_hdr rhdr;
293 1.1 pooka struct rsp_copydata copydata;
294 1.7 pooka int rv;
295 1.7 pooka
296 1.7 pooka DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
297 1.1 pooka
298 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
299 1.7 pooka rhdr.rsp_reqno = nextreq(spc);
300 1.7 pooka rhdr.rsp_class = RUMPSP_REQ;
301 1.7 pooka rhdr.rsp_type = RUMPSP_COPYOUT;
302 1.1 pooka rhdr.rsp_sysnum = 0;
303 1.1 pooka
304 1.1 pooka copydata.rcp_addr = __UNCONST(remaddr);
305 1.1 pooka copydata.rcp_len = dlen;
306 1.1 pooka
307 1.7 pooka sendlock(spc);
308 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
309 1.7 pooka rv = dosend(spc, ©data, sizeof(copydata));
310 1.7 pooka rv = dosend(spc, data, dlen);
311 1.7 pooka sendunlock(spc);
312 1.1 pooka
313 1.7 pooka return rv;
314 1.1 pooka }
315 1.1 pooka
316 1.1 pooka static int
317 1.7 pooka anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
318 1.1 pooka {
319 1.1 pooka struct rsp_hdr rhdr;
320 1.7 pooka struct respwait rw;
321 1.7 pooka int rv;
322 1.7 pooka
323 1.7 pooka DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
324 1.1 pooka
325 1.1 pooka rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
326 1.7 pooka rhdr.rsp_class = RUMPSP_REQ;
327 1.7 pooka rhdr.rsp_type = RUMPSP_ANONMMAP;
328 1.1 pooka rhdr.rsp_sysnum = 0;
329 1.1 pooka
330 1.7 pooka putwait(spc, &rw, &rhdr);
331 1.7 pooka rv = dosend(spc, &rhdr, sizeof(rhdr));
332 1.7 pooka rv = dosend(spc, &howmuch, sizeof(howmuch));
333 1.13 pooka if (rv) {
334 1.13 pooka unputwait(spc, &rw);
335 1.13 pooka return rv;
336 1.13 pooka }
337 1.1 pooka
338 1.7 pooka rv = waitresp(spc, &rw);
339 1.13 pooka
340 1.7 pooka *resp = rw.rw_data;
341 1.7 pooka
342 1.7 pooka DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
343 1.7 pooka
344 1.7 pooka return rv;
345 1.1 pooka }
346 1.1 pooka
347 1.1 pooka static void
348 1.11 pooka spcref(struct spclient *spc)
349 1.11 pooka {
350 1.11 pooka
351 1.11 pooka pthread_mutex_lock(&spc->spc_mtx);
352 1.11 pooka spc->spc_refcnt++;
353 1.11 pooka pthread_mutex_unlock(&spc->spc_mtx);
354 1.11 pooka }
355 1.11 pooka
356 1.11 pooka static void
357 1.11 pooka spcrelease(struct spclient *spc)
358 1.11 pooka {
359 1.11 pooka int ref;
360 1.11 pooka
361 1.11 pooka pthread_mutex_lock(&spc->spc_mtx);
362 1.11 pooka ref = --spc->spc_refcnt;
363 1.11 pooka pthread_mutex_unlock(&spc->spc_mtx);
364 1.11 pooka
365 1.11 pooka if (ref > 0)
366 1.11 pooka return;
367 1.11 pooka
368 1.12 pooka DPRINTF(("spcrelease: spc %p fd %d\n", spc, spc->spc_fd));
369 1.12 pooka
370 1.13 pooka _DIAGASSERT(TAILQ_EMPTY(&spc->spc_respwait));
371 1.11 pooka _DIAGASSERT(spc->spc_buf == NULL);
372 1.11 pooka
373 1.11 pooka lwproc_switch(spc->spc_mainlwp);
374 1.11 pooka lwproc_release();
375 1.11 pooka spc->spc_mainlwp = NULL;
376 1.11 pooka
377 1.11 pooka close(spc->spc_fd);
378 1.11 pooka spc->spc_fd = -1;
379 1.27 pooka spc->spc_state = SPCSTATE_NEW;
380 1.11 pooka
381 1.11 pooka atomic_inc_uint(&disco);
382 1.11 pooka }
383 1.11 pooka
384 1.11 pooka static void
385 1.2 pooka serv_handledisco(unsigned int idx)
386 1.1 pooka {
387 1.1 pooka struct spclient *spc = &spclist[idx];
388 1.1 pooka
389 1.1 pooka DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
390 1.1 pooka
391 1.12 pooka pfdlist[idx].fd = -1;
392 1.12 pooka pfdlist[idx].revents = 0;
393 1.12 pooka pthread_mutex_lock(&spc->spc_mtx);
394 1.27 pooka spc->spc_state = SPCSTATE_DYING;
395 1.12 pooka kickall(spc);
396 1.12 pooka pthread_mutex_unlock(&spc->spc_mtx);
397 1.17 pooka
398 1.17 pooka /*
399 1.17 pooka * Nobody's going to attempt to send/receive anymore,
400 1.17 pooka * so reinit info relevant to that.
401 1.17 pooka */
402 1.22 pooka /*LINTED:pointer casts may be ok*/
403 1.17 pooka memset((char *)spc + SPC_ZEROFF, 0, sizeof(*spc) - SPC_ZEROFF);
404 1.17 pooka
405 1.11 pooka spcrelease(spc);
406 1.1 pooka }
407 1.1 pooka
408 1.24 pooka static void
409 1.24 pooka serv_shutdown(void)
410 1.24 pooka {
411 1.24 pooka struct spclient *spc;
412 1.24 pooka unsigned int i;
413 1.24 pooka
414 1.24 pooka for (i = 1; i < MAXCLI; i++) {
415 1.24 pooka spc = &spclist[i];
416 1.24 pooka if (spc->spc_fd == -1)
417 1.24 pooka continue;
418 1.24 pooka
419 1.24 pooka shutdown(spc->spc_fd, SHUT_RDWR);
420 1.24 pooka serv_handledisco(i);
421 1.24 pooka
422 1.24 pooka spcrelease(spc);
423 1.24 pooka }
424 1.24 pooka }
425 1.24 pooka
426 1.11 pooka static unsigned
427 1.11 pooka serv_handleconn(int fd, connecthook_fn connhook, int busy)
428 1.1 pooka {
429 1.1 pooka struct sockaddr_storage ss;
430 1.1 pooka socklen_t sl = sizeof(ss);
431 1.11 pooka int newfd, flags;
432 1.1 pooka unsigned i;
433 1.1 pooka
434 1.1 pooka /*LINTED: cast ok */
435 1.1 pooka newfd = accept(fd, (struct sockaddr *)&ss, &sl);
436 1.1 pooka if (newfd == -1)
437 1.11 pooka return 0;
438 1.1 pooka
439 1.11 pooka if (busy) {
440 1.1 pooka close(newfd); /* EBUSY */
441 1.11 pooka return 0;
442 1.1 pooka }
443 1.1 pooka
444 1.1 pooka flags = fcntl(newfd, F_GETFL, 0);
445 1.1 pooka if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
446 1.1 pooka close(newfd);
447 1.11 pooka return 0;
448 1.1 pooka }
449 1.1 pooka
450 1.11 pooka if (connhook(newfd) != 0) {
451 1.1 pooka close(newfd);
452 1.11 pooka return 0;
453 1.1 pooka }
454 1.1 pooka
455 1.26 pooka /* write out a banner for the client */
456 1.26 pooka if (write(newfd, banner, strlen(banner)) != (ssize_t)strlen(banner)) {
457 1.26 pooka close(newfd);
458 1.26 pooka return 0;
459 1.26 pooka }
460 1.26 pooka
461 1.1 pooka /* find empty slot the simple way */
462 1.1 pooka for (i = 0; i < MAXCLI; i++) {
463 1.27 pooka if (pfdlist[i].fd == -1 && spclist[i].spc_state == SPCSTATE_NEW)
464 1.2 pooka break;
465 1.1 pooka }
466 1.1 pooka
467 1.28 pooka if (lwproc_rfork(&spclist[i], RUMP_RFCFDG) != 0) {
468 1.10 pooka close(newfd);
469 1.11 pooka return 0;
470 1.10 pooka }
471 1.10 pooka
472 1.1 pooka assert(i < MAXCLI);
473 1.1 pooka
474 1.1 pooka pfdlist[i].fd = newfd;
475 1.1 pooka spclist[i].spc_fd = newfd;
476 1.8 pooka spclist[i].spc_mainlwp = lwproc_curlwp();
477 1.7 pooka spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
478 1.8 pooka spclist[i].spc_pid = lwproc_getpid();
479 1.11 pooka spclist[i].spc_refcnt = 1;
480 1.7 pooka
481 1.7 pooka TAILQ_INIT(&spclist[i].spc_respwait);
482 1.1 pooka
483 1.13 pooka DPRINTF(("rump_sp: added new connection fd %d at idx %u, pid %d\n",
484 1.13 pooka newfd, i, lwproc_getpid()));
485 1.2 pooka
486 1.3 pooka lwproc_switch(NULL);
487 1.1 pooka
488 1.11 pooka return i;
489 1.1 pooka }
490 1.1 pooka
491 1.1 pooka static void
492 1.1 pooka serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
493 1.1 pooka {
494 1.7 pooka register_t retval[2] = {0, 0};
495 1.1 pooka int rv, sysnum;
496 1.1 pooka
497 1.1 pooka sysnum = (int)rhdr->rsp_sysnum;
498 1.1 pooka DPRINTF(("rump_sp: handling syscall %d from client %d\n",
499 1.1 pooka sysnum, 0));
500 1.1 pooka
501 1.8 pooka lwproc_newlwp(spc->spc_pid);
502 1.3 pooka rv = rumpsyscall(sysnum, data, retval);
503 1.16 pooka lwproc_release();
504 1.1 pooka
505 1.7 pooka DPRINTF(("rump_sp: got return value %d & %d/%d\n",
506 1.7 pooka rv, retval[0], retval[1]));
507 1.5 pooka
508 1.1 pooka send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
509 1.1 pooka }
510 1.1 pooka
511 1.7 pooka struct sysbouncearg {
512 1.7 pooka struct spclient *sba_spc;
513 1.7 pooka struct rsp_hdr sba_hdr;
514 1.7 pooka uint8_t *sba_data;
515 1.20 pooka
516 1.20 pooka TAILQ_ENTRY(sysbouncearg) sba_entries;
517 1.7 pooka };
518 1.20 pooka static pthread_mutex_t sbamtx;
519 1.20 pooka static pthread_cond_t sbacv;
520 1.20 pooka static int nworker, idleworker;
521 1.20 pooka static TAILQ_HEAD(, sysbouncearg) syslist = TAILQ_HEAD_INITIALIZER(syslist);
522 1.20 pooka
523 1.20 pooka /*ARGSUSED*/
524 1.7 pooka static void *
525 1.7 pooka serv_syscallbouncer(void *arg)
526 1.7 pooka {
527 1.20 pooka struct sysbouncearg *sba;
528 1.20 pooka
529 1.20 pooka for (;;) {
530 1.20 pooka pthread_mutex_lock(&sbamtx);
531 1.20 pooka if (idleworker >= rumpsp_idleworker) {
532 1.20 pooka nworker--;
533 1.20 pooka pthread_mutex_unlock(&sbamtx);
534 1.20 pooka break;
535 1.20 pooka }
536 1.20 pooka idleworker++;
537 1.20 pooka while (TAILQ_EMPTY(&syslist)) {
538 1.20 pooka pthread_cond_wait(&sbacv, &sbamtx);
539 1.20 pooka }
540 1.20 pooka
541 1.20 pooka sba = TAILQ_FIRST(&syslist);
542 1.20 pooka TAILQ_REMOVE(&syslist, sba, sba_entries);
543 1.20 pooka idleworker--;
544 1.20 pooka pthread_mutex_unlock(&sbamtx);
545 1.20 pooka
546 1.20 pooka serv_handlesyscall(sba->sba_spc,
547 1.20 pooka &sba->sba_hdr, sba->sba_data);
548 1.20 pooka spcrelease(sba->sba_spc);
549 1.20 pooka free(sba->sba_data);
550 1.20 pooka free(sba);
551 1.20 pooka }
552 1.7 pooka
553 1.7 pooka return NULL;
554 1.7 pooka }
555 1.7 pooka
556 1.15 pooka static int
557 1.15 pooka sp_copyin(void *arg, const void *raddr, void *laddr, size_t *len, int wantstr)
558 1.1 pooka {
559 1.10 pooka struct spclient *spc = arg;
560 1.9 pooka void *rdata = NULL; /* XXXuninit */
561 1.13 pooka int rv, nlocks;
562 1.13 pooka
563 1.13 pooka rumpuser__kunlock(0, &nlocks, NULL);
564 1.1 pooka
565 1.15 pooka rv = copyin_req(spc, raddr, len, wantstr, &rdata);
566 1.12 pooka if (rv)
567 1.13 pooka goto out;
568 1.1 pooka
569 1.15 pooka memcpy(laddr, rdata, *len);
570 1.7 pooka free(rdata);
571 1.1 pooka
572 1.13 pooka out:
573 1.13 pooka rumpuser__klock(nlocks, NULL);
574 1.13 pooka if (rv)
575 1.13 pooka return EFAULT;
576 1.1 pooka return 0;
577 1.1 pooka }
578 1.1 pooka
579 1.1 pooka int
580 1.15 pooka rumpuser_sp_copyin(void *arg, const void *raddr, void *laddr, size_t len)
581 1.15 pooka {
582 1.15 pooka
583 1.15 pooka return sp_copyin(arg, raddr, laddr, &len, 0);
584 1.15 pooka }
585 1.15 pooka
586 1.15 pooka int
587 1.15 pooka rumpuser_sp_copyinstr(void *arg, const void *raddr, void *laddr, size_t *len)
588 1.15 pooka {
589 1.15 pooka
590 1.15 pooka return sp_copyin(arg, raddr, laddr, len, 1);
591 1.15 pooka }
592 1.15 pooka
593 1.15 pooka static int
594 1.15 pooka sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
595 1.1 pooka {
596 1.10 pooka struct spclient *spc = arg;
597 1.13 pooka int nlocks, rv;
598 1.13 pooka
599 1.13 pooka rumpuser__kunlock(0, &nlocks, NULL);
600 1.15 pooka rv = send_copyout_req(spc, raddr, laddr, dlen);
601 1.13 pooka rumpuser__klock(nlocks, NULL);
602 1.1 pooka
603 1.13 pooka if (rv)
604 1.7 pooka return EFAULT;
605 1.1 pooka return 0;
606 1.1 pooka }
607 1.1 pooka
608 1.1 pooka int
609 1.15 pooka rumpuser_sp_copyout(void *arg, const void *laddr, void *raddr, size_t dlen)
610 1.15 pooka {
611 1.15 pooka
612 1.15 pooka return sp_copyout(arg, laddr, raddr, dlen);
613 1.15 pooka }
614 1.15 pooka
615 1.15 pooka int
616 1.15 pooka rumpuser_sp_copyoutstr(void *arg, const void *laddr, void *raddr, size_t *dlen)
617 1.15 pooka {
618 1.15 pooka
619 1.15 pooka return sp_copyout(arg, laddr, raddr, *dlen);
620 1.15 pooka }
621 1.15 pooka
622 1.15 pooka int
623 1.10 pooka rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
624 1.1 pooka {
625 1.10 pooka struct spclient *spc = arg;
626 1.7 pooka void *resp, *rdata;
627 1.13 pooka int nlocks, rv;
628 1.13 pooka
629 1.13 pooka rumpuser__kunlock(0, &nlocks, NULL);
630 1.1 pooka
631 1.7 pooka rv = anonmmap_req(spc, howmuch, &rdata);
632 1.13 pooka if (rv) {
633 1.13 pooka rv = EFAULT;
634 1.13 pooka goto out;
635 1.13 pooka }
636 1.1 pooka
637 1.7 pooka resp = *(void **)rdata;
638 1.7 pooka free(rdata);
639 1.1 pooka
640 1.7 pooka if (resp == NULL) {
641 1.13 pooka rv = ENOMEM;
642 1.1 pooka }
643 1.1 pooka
644 1.1 pooka *addr = resp;
645 1.13 pooka
646 1.13 pooka out:
647 1.13 pooka rumpuser__klock(nlocks, NULL);
648 1.13 pooka
649 1.13 pooka if (rv)
650 1.13 pooka return rv;
651 1.1 pooka return 0;
652 1.1 pooka }
653 1.1 pooka
654 1.1 pooka /*
655 1.1 pooka *
656 1.1 pooka * Startup routines and mainloop for server.
657 1.1 pooka *
658 1.1 pooka */
659 1.1 pooka
660 1.1 pooka struct spservarg {
661 1.1 pooka int sps_sock;
662 1.1 pooka connecthook_fn sps_connhook;
663 1.1 pooka };
664 1.1 pooka
665 1.14 pooka static pthread_attr_t pattr_detached;
666 1.7 pooka static void
667 1.7 pooka handlereq(struct spclient *spc)
668 1.7 pooka {
669 1.7 pooka struct sysbouncearg *sba;
670 1.7 pooka pthread_t pt;
671 1.27 pooka int retries, rv;
672 1.27 pooka
673 1.27 pooka if (__predict_false(spc->spc_state == SPCSTATE_NEW)) {
674 1.27 pooka if (spc->spc_hdr.rsp_type != RUMPSP_HANDSHAKE) {
675 1.27 pooka send_error_resp(spc, spc->spc_hdr.rsp_reqno, EAUTH);
676 1.27 pooka spcfreebuf(spc);
677 1.27 pooka return;
678 1.27 pooka }
679 1.27 pooka
680 1.27 pooka rv = send_handshake_resp(spc, spc->spc_hdr.rsp_reqno, 0);
681 1.27 pooka spcfreebuf(spc);
682 1.27 pooka if (rv) {
683 1.27 pooka shutdown(spc->spc_fd, SHUT_RDWR);
684 1.27 pooka return;
685 1.27 pooka }
686 1.27 pooka spc->spc_state = SPCSTATE_RUNNING;
687 1.27 pooka return;
688 1.27 pooka }
689 1.7 pooka
690 1.21 pooka if (__predict_false(spc->spc_hdr.rsp_type != RUMPSP_SYSCALL)) {
691 1.21 pooka send_error_resp(spc, spc->spc_hdr.rsp_reqno, EINVAL);
692 1.21 pooka spcfreebuf(spc);
693 1.21 pooka return;
694 1.21 pooka }
695 1.7 pooka
696 1.21 pooka retries = 0;
697 1.21 pooka while ((sba = malloc(sizeof(*sba))) == NULL) {
698 1.21 pooka if (nworker == 0 || retries > 10) {
699 1.21 pooka send_error_resp(spc, spc->spc_hdr.rsp_reqno, EAGAIN);
700 1.21 pooka spcfreebuf(spc);
701 1.21 pooka return;
702 1.21 pooka }
703 1.21 pooka /* slim chance of more memory? */
704 1.21 pooka usleep(10000);
705 1.7 pooka }
706 1.7 pooka
707 1.7 pooka sba->sba_spc = spc;
708 1.7 pooka sba->sba_hdr = spc->spc_hdr;
709 1.7 pooka sba->sba_data = spc->spc_buf;
710 1.21 pooka spcresetbuf(spc);
711 1.7 pooka
712 1.11 pooka spcref(spc);
713 1.20 pooka
714 1.20 pooka pthread_mutex_lock(&sbamtx);
715 1.20 pooka TAILQ_INSERT_TAIL(&syslist, sba, sba_entries);
716 1.20 pooka if (idleworker > 0) {
717 1.20 pooka /* do we have a daemon's tool (i.e. idle threads)? */
718 1.20 pooka pthread_cond_signal(&sbacv);
719 1.20 pooka } else if (nworker < rumpsp_maxworker) {
720 1.20 pooka /*
721 1.20 pooka * Else, need to create one
722 1.20 pooka * (if we can, otherwise just expect another
723 1.20 pooka * worker to pick up the syscall)
724 1.20 pooka */
725 1.20 pooka if (pthread_create(&pt, &pattr_detached,
726 1.20 pooka serv_syscallbouncer, NULL) == 0)
727 1.20 pooka nworker++;
728 1.7 pooka }
729 1.20 pooka pthread_mutex_unlock(&sbamtx);
730 1.7 pooka }
731 1.7 pooka
732 1.1 pooka static void *
733 1.1 pooka spserver(void *arg)
734 1.1 pooka {
735 1.1 pooka struct spservarg *sarg = arg;
736 1.11 pooka struct spclient *spc;
737 1.1 pooka unsigned idx;
738 1.1 pooka int seen;
739 1.1 pooka int rv;
740 1.11 pooka unsigned int nfds, maxidx;
741 1.1 pooka
742 1.11 pooka for (idx = 0; idx < MAXCLI; idx++) {
743 1.1 pooka pfdlist[idx].fd = -1;
744 1.1 pooka pfdlist[idx].events = POLLIN;
745 1.11 pooka
746 1.11 pooka spc = &spclist[idx];
747 1.11 pooka pthread_mutex_init(&spc->spc_mtx, NULL);
748 1.11 pooka pthread_cond_init(&spc->spc_cv, NULL);
749 1.24 pooka spc->spc_fd = -1;
750 1.1 pooka }
751 1.24 pooka pfdlist[0].fd = spclist[0].spc_fd = sarg->sps_sock;
752 1.1 pooka pfdlist[0].events = POLLIN;
753 1.1 pooka nfds = 1;
754 1.1 pooka maxidx = 0;
755 1.1 pooka
756 1.14 pooka pthread_attr_init(&pattr_detached);
757 1.14 pooka pthread_attr_setdetachstate(&pattr_detached, PTHREAD_CREATE_DETACHED);
758 1.19 pooka /* XXX: doesn't stacksize currently work on NetBSD */
759 1.19 pooka pthread_attr_setstacksize(&pattr_detached, 32*1024);
760 1.14 pooka
761 1.20 pooka pthread_mutex_init(&sbamtx, NULL);
762 1.20 pooka pthread_cond_init(&sbacv, NULL);
763 1.20 pooka
764 1.1 pooka DPRINTF(("rump_sp: server mainloop\n"));
765 1.1 pooka
766 1.1 pooka for (;;) {
767 1.18 pooka int discoed;
768 1.18 pooka
769 1.11 pooka /* g/c hangarounds (eventually) */
770 1.18 pooka discoed = atomic_swap_uint(&disco, 0);
771 1.18 pooka while (discoed--) {
772 1.18 pooka nfds--;
773 1.18 pooka idx = maxidx;
774 1.18 pooka while (idx) {
775 1.18 pooka if (pfdlist[idx].fd != -1) {
776 1.18 pooka maxidx = idx;
777 1.18 pooka break;
778 1.11 pooka }
779 1.18 pooka idx--;
780 1.11 pooka }
781 1.18 pooka DPRINTF(("rump_sp: set maxidx to [%u]\n",
782 1.18 pooka maxidx));
783 1.11 pooka }
784 1.11 pooka
785 1.1 pooka DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
786 1.1 pooka seen = 0;
787 1.1 pooka rv = poll(pfdlist, maxidx+1, INFTIM);
788 1.1 pooka assert(maxidx+1 <= MAXCLI);
789 1.1 pooka assert(rv != 0);
790 1.1 pooka if (rv == -1) {
791 1.1 pooka if (errno == EINTR)
792 1.1 pooka continue;
793 1.1 pooka fprintf(stderr, "rump_spserver: poll returned %d\n",
794 1.1 pooka errno);
795 1.1 pooka break;
796 1.1 pooka }
797 1.1 pooka
798 1.12 pooka for (idx = 0; seen < rv && idx < MAXCLI; idx++) {
799 1.1 pooka if ((pfdlist[idx].revents & POLLIN) == 0)
800 1.1 pooka continue;
801 1.1 pooka
802 1.1 pooka seen++;
803 1.1 pooka DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
804 1.1 pooka idx, seen, rv));
805 1.1 pooka if (idx > 0) {
806 1.11 pooka spc = &spclist[idx];
807 1.1 pooka DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
808 1.1 pooka switch (readframe(spc)) {
809 1.1 pooka case 0:
810 1.1 pooka break;
811 1.1 pooka case -1:
812 1.2 pooka serv_handledisco(idx);
813 1.1 pooka break;
814 1.1 pooka default:
815 1.7 pooka switch (spc->spc_hdr.rsp_class) {
816 1.7 pooka case RUMPSP_RESP:
817 1.7 pooka kickwaiter(spc);
818 1.7 pooka break;
819 1.7 pooka case RUMPSP_REQ:
820 1.7 pooka handlereq(spc);
821 1.7 pooka break;
822 1.7 pooka default:
823 1.21 pooka send_error_resp(spc,
824 1.21 pooka spc->spc_hdr.rsp_reqno,
825 1.21 pooka ENOENT);
826 1.21 pooka spcfreebuf(spc);
827 1.7 pooka break;
828 1.7 pooka }
829 1.1 pooka break;
830 1.1 pooka }
831 1.11 pooka
832 1.1 pooka } else {
833 1.1 pooka DPRINTF(("rump_sp: mainloop new connection\n"));
834 1.11 pooka
835 1.24 pooka if (__predict_false(spfini)) {
836 1.24 pooka close(spclist[0].spc_fd);
837 1.24 pooka serv_shutdown();
838 1.24 pooka goto out;
839 1.24 pooka }
840 1.24 pooka
841 1.11 pooka idx = serv_handleconn(pfdlist[0].fd,
842 1.11 pooka sarg->sps_connhook, nfds == MAXCLI);
843 1.11 pooka if (idx)
844 1.11 pooka nfds++;
845 1.11 pooka if (idx > maxidx)
846 1.11 pooka maxidx = idx;
847 1.12 pooka DPRINTF(("rump_sp: maxid now %d\n", maxidx));
848 1.1 pooka }
849 1.1 pooka }
850 1.1 pooka }
851 1.1 pooka
852 1.24 pooka out:
853 1.1 pooka return NULL;
854 1.1 pooka }
855 1.1 pooka
856 1.25 pooka static unsigned cleanupidx;
857 1.25 pooka static struct sockaddr *cleanupsa;
858 1.1 pooka int
859 1.26 pooka rumpuser_sp_init(const char *url, const struct rumpuser_sp_ops *spopsp,
860 1.26 pooka const char *ostype, const char *osrelease, const char *machine)
861 1.1 pooka {
862 1.5 pooka pthread_t pt;
863 1.1 pooka struct spservarg *sarg;
864 1.1 pooka struct sockaddr *sap;
865 1.5 pooka char *p;
866 1.5 pooka unsigned idx;
867 1.5 pooka int error, s;
868 1.5 pooka
869 1.5 pooka p = strdup(url);
870 1.5 pooka if (p == NULL)
871 1.5 pooka return ENOMEM;
872 1.5 pooka error = parseurl(p, &sap, &idx, 1);
873 1.5 pooka free(p);
874 1.5 pooka if (error)
875 1.5 pooka return error;
876 1.5 pooka
877 1.26 pooka snprintf(banner, sizeof(banner), "RUMPSP-%d.%d-%s-%s/%s\n",
878 1.26 pooka PROTOMAJOR, PROTOMINOR, ostype, osrelease, machine);
879 1.26 pooka
880 1.5 pooka s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
881 1.5 pooka if (s == -1)
882 1.5 pooka return errno;
883 1.1 pooka
884 1.3 pooka spops = *spopsp;
885 1.5 pooka sarg = malloc(sizeof(*sarg));
886 1.5 pooka if (sarg == NULL) {
887 1.5 pooka close(s);
888 1.5 pooka return ENOMEM;
889 1.5 pooka }
890 1.5 pooka
891 1.5 pooka sarg->sps_sock = s;
892 1.5 pooka sarg->sps_connhook = parsetab[idx].connhook;
893 1.1 pooka
894 1.25 pooka cleanupidx = idx;
895 1.25 pooka cleanupsa = sap;
896 1.25 pooka
897 1.5 pooka /* sloppy error recovery */
898 1.1 pooka
899 1.5 pooka /*LINTED*/
900 1.5 pooka if (bind(s, sap, sap->sa_len) == -1) {
901 1.5 pooka fprintf(stderr, "rump_sp: server bind failed\n");
902 1.5 pooka return errno;
903 1.1 pooka }
904 1.25 pooka
905 1.18 pooka if (listen(s, MAXCLI) == -1) {
906 1.5 pooka fprintf(stderr, "rump_sp: server listen failed\n");
907 1.5 pooka return errno;
908 1.1 pooka }
909 1.1 pooka
910 1.5 pooka if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
911 1.5 pooka fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
912 1.1 pooka return errno;
913 1.1 pooka }
914 1.5 pooka pthread_detach(pt);
915 1.1 pooka
916 1.1 pooka return 0;
917 1.1 pooka }
918 1.24 pooka
919 1.24 pooka void
920 1.24 pooka rumpuser_sp_fini()
921 1.24 pooka {
922 1.24 pooka
923 1.24 pooka if (spclist[0].spc_fd) {
924 1.25 pooka parsetab[cleanupidx].cleanup(cleanupsa);
925 1.24 pooka shutdown(spclist[0].spc_fd, SHUT_RDWR);
926 1.24 pooka spfini = 1;
927 1.24 pooka }
928 1.24 pooka }
929