rumpuser_sp.c revision 1.10 1 /* $NetBSD: rumpuser_sp.c,v 1.10 2010/11/22 20:42:19 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * Sysproxy routines. This provides system RPC support over host sockets.
30 * The most notable limitation is that the client and server must share
31 * the same ABI. This does not mean that they have to be the same
32 * machine or that they need to run the same version of the host OS,
33 * just that they must agree on the data structures. This even *might*
34 * work correctly from one hardware architecture to another.
35 *
36 * Not finished yet, i.e. don't use in production. Lacks locking plus
37 * handling of multiple clients and unexpected connection closes.
38 */
39
40 #include <sys/cdefs.h>
41 __RCSID("$NetBSD: rumpuser_sp.c,v 1.10 2010/11/22 20:42:19 pooka Exp $");
42
43 #include <sys/types.h>
44 #include <sys/mman.h>
45 #include <sys/socket.h>
46
47 #include <arpa/inet.h>
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
50
51 #include <assert.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <poll.h>
55 #include <pthread.h>
56 #include <stdarg.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 #include <rump/rumpuser.h>
63
64 #include "sp_common.c"
65
66 #define MAXCLI 4
67
68 static struct pollfd pfdlist[MAXCLI];
69 static struct spclient spclist[MAXCLI];
70 static unsigned int nfds, maxidx;
71
72 static struct rumpuser_sp_ops spops;
73
74 /*
75 * Manual wrappers, since librump does not have access to the
76 * user namespace wrapped interfaces.
77 */
78
79 static void
80 lwproc_switch(struct lwp *l)
81 {
82
83 spops.spop_schedule();
84 spops.spop_lwproc_switch(l);
85 spops.spop_unschedule();
86 }
87
88 static void
89 lwproc_release(void)
90 {
91
92 spops.spop_schedule();
93 spops.spop_lwproc_release();
94 spops.spop_unschedule();
95 }
96
97 static int
98 lwproc_newproc(struct spclient *spc)
99 {
100 int rv;
101
102 spops.spop_schedule();
103 rv = spops.spop_lwproc_newproc(spc);
104 spops.spop_unschedule();
105
106 return rv;
107 }
108
109 static int
110 lwproc_newlwp(pid_t pid)
111 {
112 int rv;
113
114 spops.spop_schedule();
115 rv = spops.spop_lwproc_newlwp(pid);
116 spops.spop_unschedule();
117
118 return rv;
119 }
120
121 static struct lwp *
122 lwproc_curlwp(void)
123 {
124 struct lwp *l;
125
126 spops.spop_schedule();
127 l = spops.spop_lwproc_curlwp();
128 spops.spop_unschedule();
129
130 return l;
131 }
132
133 static pid_t
134 lwproc_getpid(void)
135 {
136 pid_t p;
137
138 spops.spop_schedule();
139 p = spops.spop_getpid();
140 spops.spop_unschedule();
141
142 return p;
143 }
144
145 static int
146 rumpsyscall(int sysnum, void *data, register_t *retval)
147 {
148 int rv;
149
150 spops.spop_schedule();
151 rv = spops.spop_syscall(sysnum, data, retval);
152 spops.spop_unschedule();
153
154 return rv;
155 }
156
157 static uint64_t
158 nextreq(struct spclient *spc)
159 {
160 uint64_t nw;
161
162 pthread_mutex_lock(&spc->spc_mtx);
163 nw = spc->spc_nextreq++;
164 pthread_mutex_unlock(&spc->spc_mtx);
165
166 return nw;
167 }
168
169 static int
170 send_syscall_resp(struct spclient *spc, uint64_t reqno, int error,
171 register_t *retval)
172 {
173 struct rsp_hdr rhdr;
174 struct rsp_sysresp sysresp;
175 int rv;
176
177 rhdr.rsp_len = sizeof(rhdr) + sizeof(sysresp);
178 rhdr.rsp_reqno = reqno;
179 rhdr.rsp_class = RUMPSP_RESP;
180 rhdr.rsp_type = RUMPSP_SYSCALL;
181 rhdr.rsp_sysnum = 0;
182
183 sysresp.rsys_error = error;
184 memcpy(sysresp.rsys_retval, retval, sizeof(sysresp.rsys_retval));
185
186 sendlock(spc);
187 rv = dosend(spc, &rhdr, sizeof(rhdr));
188 rv = dosend(spc, &sysresp, sizeof(sysresp));
189 sendunlock(spc);
190
191 return rv;
192 }
193
194 static int
195 copyin_req(struct spclient *spc, const void *remaddr, size_t dlen, void **resp)
196 {
197 struct rsp_hdr rhdr;
198 struct rsp_copydata copydata;
199 struct respwait rw;
200 int rv;
201
202 DPRINTF(("copyin_req: %zu bytes from %p\n", dlen, remaddr));
203
204 rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata);
205 rhdr.rsp_class = RUMPSP_REQ;
206 rhdr.rsp_type = RUMPSP_COPYIN;
207 rhdr.rsp_sysnum = 0;
208
209 copydata.rcp_addr = __UNCONST(remaddr);
210 copydata.rcp_len = dlen;
211
212 putwait(spc, &rw, &rhdr);
213
214 sendlock(spc);
215 rv = dosend(spc, &rhdr, sizeof(rhdr));
216 rv = dosend(spc, ©data, sizeof(copydata));
217 sendunlock(spc);
218 if (rv)
219 return rv; /* XXX: unputwait */
220
221 rv = waitresp(spc, &rw);
222
223 DPRINTF(("copyin: response %d\n", rv));
224
225 *resp = rw.rw_data;
226 return rv;
227
228 }
229
230 static int
231 send_copyout_req(struct spclient *spc, const void *remaddr,
232 const void *data, size_t dlen)
233 {
234 struct rsp_hdr rhdr;
235 struct rsp_copydata copydata;
236 int rv;
237
238 DPRINTF(("copyout_req (async): %zu bytes to %p\n", dlen, remaddr));
239
240 rhdr.rsp_len = sizeof(rhdr) + sizeof(copydata) + dlen;
241 rhdr.rsp_reqno = nextreq(spc);
242 rhdr.rsp_class = RUMPSP_REQ;
243 rhdr.rsp_type = RUMPSP_COPYOUT;
244 rhdr.rsp_sysnum = 0;
245
246 copydata.rcp_addr = __UNCONST(remaddr);
247 copydata.rcp_len = dlen;
248
249 sendlock(spc);
250 rv = dosend(spc, &rhdr, sizeof(rhdr));
251 rv = dosend(spc, ©data, sizeof(copydata));
252 rv = dosend(spc, data, dlen);
253 sendunlock(spc);
254
255 return rv;
256 }
257
258 static int
259 anonmmap_req(struct spclient *spc, size_t howmuch, void **resp)
260 {
261 struct rsp_hdr rhdr;
262 struct respwait rw;
263 int rv;
264
265 DPRINTF(("anonmmap_req: %zu bytes\n", howmuch));
266
267 rhdr.rsp_len = sizeof(rhdr) + sizeof(howmuch);
268 rhdr.rsp_class = RUMPSP_REQ;
269 rhdr.rsp_type = RUMPSP_ANONMMAP;
270 rhdr.rsp_sysnum = 0;
271
272 putwait(spc, &rw, &rhdr);
273
274 sendlock(spc);
275 rv = dosend(spc, &rhdr, sizeof(rhdr));
276 rv = dosend(spc, &howmuch, sizeof(howmuch));
277 sendunlock(spc);
278 if (rv)
279 return rv; /* XXX: unputwait */
280
281 rv = waitresp(spc, &rw);
282 *resp = rw.rw_data;
283
284 DPRINTF(("anonmmap: mapped at %p\n", **(void ***)resp));
285
286 return rv;
287 }
288
289 static void
290 serv_handledisco(unsigned int idx)
291 {
292 struct spclient *spc = &spclist[idx];
293 int fd = spc->spc_fd;
294
295 DPRINTF(("rump_sp: disconnecting [%u]\n", idx));
296
297 lwproc_switch(spc->spc_mainlwp);
298 lwproc_release();
299
300 pthread_mutex_destroy(&spc->spc_mtx);
301 pthread_cond_destroy(&spc->spc_cv);
302 free(spc->spc_buf);
303 memset(spc, 0, sizeof(*spc));
304 close(fd);
305 pfdlist[idx].fd = -1;
306 nfds--;
307
308 if (idx == maxidx) {
309 while (idx--) {
310 if (pfdlist[idx].fd != -1) {
311 maxidx = idx;
312 break;
313 }
314 assert(idx != 0);
315 }
316 DPRINTF(("rump_sp: set maxidx to [%u]\n", maxidx));
317 }
318 }
319
320 static int
321 serv_handleconn(int fd, connecthook_fn connhook)
322 {
323 struct sockaddr_storage ss;
324 socklen_t sl = sizeof(ss);
325 int newfd, flags, error;
326 unsigned i;
327
328 /*LINTED: cast ok */
329 newfd = accept(fd, (struct sockaddr *)&ss, &sl);
330 if (newfd == -1)
331 return errno;
332
333 /* XXX: should do some sort of handshake too */
334
335 if (nfds == MAXCLI) {
336 close(newfd); /* EBUSY */
337 return EBUSY;
338 }
339
340 flags = fcntl(newfd, F_GETFL, 0);
341 if (fcntl(newfd, F_SETFL, flags | O_NONBLOCK) == -1) {
342 close(newfd);
343 return errno;
344 }
345 flags = 1;
346
347 if ((error = connhook(newfd)) != 0) {
348 close(newfd);
349 return error;
350 }
351
352 /* find empty slot the simple way */
353 for (i = 0; i < MAXCLI; i++) {
354 if (pfdlist[i].fd == -1)
355 break;
356 }
357
358 if ((error = lwproc_newproc(&spclist[i])) != 0) {
359 close(newfd);
360 return error;
361 }
362
363 assert(i < MAXCLI);
364 nfds++;
365
366 pfdlist[i].fd = newfd;
367 spclist[i].spc_fd = newfd;
368 spclist[i].spc_mainlwp = lwproc_curlwp();
369 spclist[i].spc_istatus = SPCSTATUS_BUSY; /* dedicated receiver */
370 spclist[i].spc_pid = lwproc_getpid();
371
372 TAILQ_INIT(&spclist[i].spc_respwait);
373 pthread_mutex_init(&spclist[i].spc_mtx, NULL);
374 pthread_cond_init(&spclist[i].spc_cv, NULL);
375
376 if (maxidx < i)
377 maxidx = i;
378
379 DPRINTF(("rump_sp: added new connection at idx %u, pid %d\n",
380 i, lwproc_getpid()));
381
382 lwproc_switch(NULL);
383
384 return 0;
385 }
386
387 static void
388 serv_handlesyscall(struct spclient *spc, struct rsp_hdr *rhdr, uint8_t *data)
389 {
390 register_t retval[2] = {0, 0};
391 int rv, sysnum;
392
393 sysnum = (int)rhdr->rsp_sysnum;
394 DPRINTF(("rump_sp: handling syscall %d from client %d\n",
395 sysnum, 0));
396
397 lwproc_newlwp(spc->spc_pid);
398 rv = rumpsyscall(sysnum, data, retval);
399 lwproc_switch(NULL);
400 free(data);
401
402 DPRINTF(("rump_sp: got return value %d & %d/%d\n",
403 rv, retval[0], retval[1]));
404
405 send_syscall_resp(spc, rhdr->rsp_reqno, rv, retval);
406 }
407
408 struct sysbouncearg {
409 struct spclient *sba_spc;
410 struct rsp_hdr sba_hdr;
411 uint8_t *sba_data;
412 };
413 static void *
414 serv_syscallbouncer(void *arg)
415 {
416 struct sysbouncearg *barg = arg;
417
418 serv_handlesyscall(barg->sba_spc, &barg->sba_hdr, barg->sba_data);
419 free(arg);
420 return NULL;
421 }
422
423 int
424 rumpuser_sp_copyin(void *arg, const void *uaddr, void *kaddr, size_t len)
425 {
426 struct spclient *spc = arg;
427 void *rdata = NULL; /* XXXuninit */
428
429 copyin_req(spc, uaddr, len, &rdata);
430
431 memcpy(kaddr, rdata, len);
432 free(rdata);
433
434 return 0;
435 }
436
437 int
438 rumpuser_sp_copyout(void *arg, const void *kaddr, void *uaddr, size_t dlen)
439 {
440 struct spclient *spc = arg;
441
442 if (send_copyout_req(spc, uaddr, kaddr, dlen) != 0)
443 return EFAULT;
444 return 0;
445 }
446
447 int
448 rumpuser_sp_anonmmap(void *arg, size_t howmuch, void **addr)
449 {
450 struct spclient *spc = arg;
451 void *resp, *rdata;
452 int rv;
453
454 rv = anonmmap_req(spc, howmuch, &rdata);
455 if (rv)
456 return rv;
457
458 resp = *(void **)rdata;
459 free(rdata);
460
461 if (resp == NULL) {
462 return ENOMEM;
463 }
464
465 *addr = resp;
466 return 0;
467 }
468
469 /*
470 *
471 * Startup routines and mainloop for server.
472 *
473 */
474
475 struct spservarg {
476 int sps_sock;
477 connecthook_fn sps_connhook;
478 };
479
480 static void
481 handlereq(struct spclient *spc)
482 {
483 struct sysbouncearg *sba;
484 pthread_attr_t pattr;
485 pthread_t pt;
486 int rv;
487
488 /* XXX: check that it's a syscall */
489
490 sba = malloc(sizeof(*sba));
491 if (sba == NULL) {
492 /* panic */
493 abort();
494 }
495
496 sba->sba_spc = spc;
497 sba->sba_hdr = spc->spc_hdr;
498 sba->sba_data = spc->spc_buf;
499
500 spc->spc_buf = NULL;
501 spc->spc_off = 0;
502
503 pthread_attr_init(&pattr);
504 pthread_attr_setdetachstate(&pattr, 1);
505
506 if ((rv = pthread_create(&pt, &pattr, serv_syscallbouncer, sba)) != 0) {
507 /* panic */
508 abort();
509 }
510 }
511
512 static void *
513 spserver(void *arg)
514 {
515 struct spservarg *sarg = arg;
516 unsigned idx;
517 int seen;
518 int rv;
519
520 for (idx = 1; idx < MAXCLI; idx++) {
521 pfdlist[idx].fd = -1;
522 pfdlist[idx].events = POLLIN;
523 }
524 pfdlist[0].fd = sarg->sps_sock;
525 pfdlist[0].events = POLLIN;
526 nfds = 1;
527 maxidx = 0;
528
529 DPRINTF(("rump_sp: server mainloop\n"));
530
531 for (;;) {
532 DPRINTF(("rump_sp: loop nfd %d\n", maxidx+1));
533 seen = 0;
534 rv = poll(pfdlist, maxidx+1, INFTIM);
535 assert(maxidx+1 <= MAXCLI);
536 assert(rv != 0);
537 if (rv == -1) {
538 if (errno == EINTR)
539 continue;
540 fprintf(stderr, "rump_spserver: poll returned %d\n",
541 errno);
542 break;
543 }
544
545 for (idx = 0; seen < rv; idx++) {
546 assert(idx < MAXCLI);
547
548 if ((pfdlist[idx].revents & POLLIN) == 0)
549 continue;
550
551 seen++;
552 DPRINTF(("rump_sp: activity at [%u] %d/%d\n",
553 idx, seen, rv));
554 if (idx > 0) {
555 struct spclient *spc = &spclist[idx];
556
557 DPRINTF(("rump_sp: mainloop read [%u]\n", idx));
558 switch (readframe(spc)) {
559 case 0:
560 break;
561 case -1:
562 serv_handledisco(idx);
563 break;
564 default:
565 switch (spc->spc_hdr.rsp_class) {
566 case RUMPSP_RESP:
567 kickwaiter(spc);
568 break;
569 case RUMPSP_REQ:
570 handlereq(spc);
571 break;
572 default:
573 printf("PANIC\n");
574 abort();
575 break;
576 }
577 break;
578 }
579 } else {
580 DPRINTF(("rump_sp: mainloop new connection\n"));
581 serv_handleconn(pfdlist[0].fd,
582 sarg->sps_connhook);
583 }
584 }
585 }
586
587 return NULL;
588 }
589
590 int
591 rumpuser_sp_init(const struct rumpuser_sp_ops *spopsp, const char *url)
592 {
593 pthread_t pt;
594 struct spservarg *sarg;
595 struct sockaddr *sap;
596 char *p;
597 unsigned idx;
598 int error, s;
599
600 p = strdup(url);
601 if (p == NULL)
602 return ENOMEM;
603 error = parseurl(p, &sap, &idx, 1);
604 free(p);
605 if (error)
606 return error;
607
608 s = socket(parsetab[idx].domain, SOCK_STREAM, 0);
609 if (s == -1)
610 return errno;
611
612 spops = *spopsp;
613 sarg = malloc(sizeof(*sarg));
614 if (sarg == NULL) {
615 close(s);
616 return ENOMEM;
617 }
618
619 sarg->sps_sock = s;
620 sarg->sps_connhook = parsetab[idx].connhook;
621
622 /* sloppy error recovery */
623
624 /*LINTED*/
625 if (bind(s, sap, sap->sa_len) == -1) {
626 fprintf(stderr, "rump_sp: server bind failed\n");
627 return errno;
628 }
629 if (listen(s, 20) == -1) {
630 fprintf(stderr, "rump_sp: server listen failed\n");
631 return errno;
632 }
633
634 if ((error = pthread_create(&pt, NULL, spserver, sarg)) != 0) {
635 fprintf(stderr, "rump_sp: cannot create wrkr thread\n");
636 return errno;
637 }
638 pthread_detach(pt);
639
640 return 0;
641 }
642