sockin.c revision 1.15.2.4 1 /* $NetBSD: sockin.c,v 1.15.2.4 2010/03/11 15:04:40 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2008, 2009 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 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: sockin.c,v 1.15.2.4 2010/03/11 15:04:40 yamt Exp $");
30
31 #include <sys/param.h>
32 #include <sys/condvar.h>
33 #include <sys/domain.h>
34 #include <sys/kmem.h>
35 #include <sys/kthread.h>
36 #include <sys/mbuf.h>
37 #include <sys/mutex.h>
38 #include <sys/poll.h>
39 #include <sys/protosw.h>
40 #include <sys/queue.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/time.h>
44
45 #include <net/bpf.h>
46 #include <net/if.h>
47 #include <net/radix.h>
48
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52
53 #include <rump/rumpuser.h>
54
55 #include "rump_private.h"
56
57 /*
58 * An inet communication domain which uses the socket interface.
59 * Currently supports only IPv4 UDP, but could easily be extended to
60 * support IPv6 and TCP by adding more stuff to the protosw.
61 */
62
63 DOMAIN_DEFINE(sockindomain);
64
65 static void sockin_init(void);
66 static int sockin_usrreq(struct socket *, int, struct mbuf *,
67 struct mbuf *, struct mbuf *, struct lwp *);
68 static int sockin_ctloutput(int op, struct socket *, struct sockopt *);
69
70 const struct protosw sockinsw[] = {
71 {
72 .pr_type = SOCK_DGRAM,
73 .pr_domain = &sockindomain,
74 .pr_protocol = IPPROTO_UDP,
75 .pr_flags = PR_ATOMIC|PR_ADDR,
76 .pr_usrreq = sockin_usrreq,
77 .pr_ctloutput = sockin_ctloutput,
78 },
79 {
80 .pr_type = SOCK_STREAM,
81 .pr_domain = &sockindomain,
82 .pr_protocol = IPPROTO_TCP,
83 .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
84 .pr_usrreq = sockin_usrreq,
85 .pr_ctloutput = sockin_ctloutput,
86 }};
87
88 struct domain sockindomain = {
89 .dom_family = PF_INET,
90 .dom_name = "socket_inet",
91 .dom_init = sockin_init,
92 .dom_externalize = NULL,
93 .dom_dispose = NULL,
94 .dom_protosw = sockinsw,
95 .dom_protoswNPROTOSW = &sockinsw[__arraycount(sockinsw)],
96 .dom_rtattach = rn_inithead,
97 .dom_rtoffset = 32,
98 .dom_maxrtkey = sizeof(struct sockaddr_in),
99 .dom_ifattach = NULL,
100 .dom_ifdetach = NULL,
101 .dom_ifqueues = { NULL },
102 .dom_link = { NULL },
103 .dom_mowner = MOWNER_INIT("",""),
104 .dom_rtcache = { NULL },
105 .dom_sockaddr_cmp = NULL
106 };
107
108 #define SO2S(so) ((intptr_t)(so->so_internal))
109 #define SOCKIN_SBSIZE 65536
110
111 struct sockin_unit {
112 struct socket *su_so;
113
114 LIST_ENTRY(sockin_unit) su_entries;
115 };
116 static LIST_HEAD(, sockin_unit) su_ent = LIST_HEAD_INITIALIZER(su_ent);
117 static kmutex_t su_mtx;
118 static bool rebuild;
119 static int nsock;
120
121 /* XXX: for the bpf hack */
122 static struct ifnet sockin_if;
123 int ifpromisc(struct ifnet *ifp, int pswitch) { return 0; }
124
125 static int
126 registersock(struct socket *so, int news)
127 {
128 struct sockin_unit *su;
129
130 su = kmem_alloc(sizeof(*su), KM_NOSLEEP);
131 if (!su)
132 return ENOMEM;
133
134 so->so_internal = (void *)(intptr_t)news;
135 su->su_so = so;
136
137 mutex_enter(&su_mtx);
138 LIST_INSERT_HEAD(&su_ent, su, su_entries);
139 nsock++;
140 rebuild = true;
141 mutex_exit(&su_mtx);
142
143 return 0;
144 }
145
146 static void
147 removesock(struct socket *so)
148 {
149 struct sockin_unit *su_iter;
150 int error;
151
152 mutex_enter(&su_mtx);
153 LIST_FOREACH(su_iter, &su_ent, su_entries) {
154 if (su_iter->su_so == so)
155 break;
156 }
157 if (!su_iter)
158 panic("no such socket");
159
160 LIST_REMOVE(su_iter, su_entries);
161 nsock--;
162 rebuild = true;
163 mutex_exit(&su_mtx);
164
165 rumpuser_close(SO2S(su_iter->su_so), &error);
166 kmem_free(su_iter, sizeof(*su_iter));
167 }
168
169 static void
170 sockin_process(struct socket *so)
171 {
172 struct sockaddr_in from;
173 struct iovec io;
174 struct msghdr rmsg;
175 struct mbuf *m;
176 ssize_t n;
177 size_t plen;
178 int error;
179
180 m = m_gethdr(M_WAIT, MT_DATA);
181 if (so->so_proto->pr_type == SOCK_DGRAM) {
182 plen = IP_MAXPACKET;
183 MEXTMALLOC(m, plen, M_DONTWAIT);
184 } else {
185 plen = MCLBYTES;
186 MCLGET(m, M_DONTWAIT);
187 }
188 if ((m->m_flags & M_EXT) == 0) {
189 m_freem(m);
190 return;
191 }
192
193 memset(&rmsg, 0, sizeof(rmsg));
194 io.iov_base = mtod(m, void *);
195 io.iov_len = plen;
196 rmsg.msg_iov = &io;
197 rmsg.msg_iovlen = 1;
198 rmsg.msg_name = (struct sockaddr *)&from;
199 rmsg.msg_namelen = sizeof(from);
200
201 n = rumpuser_net_recvmsg(SO2S(so), &rmsg, 0, &error);
202 if (n <= 0) {
203 m_freem(m);
204
205 /* Treat a TCP socket a goner */
206 if (error != EAGAIN && so->so_proto->pr_type == SOCK_STREAM) {
207 mutex_enter(softnet_lock);
208 soisdisconnected(so);
209 mutex_exit(softnet_lock);
210 removesock(so);
211 }
212 return;
213 }
214 m->m_len = m->m_pkthdr.len = n;
215
216 if (sockin_if.if_bpf)
217 bpf_ops->bpf_mtap_af(sockin_if.if_bpf, AF_UNSPEC, m);
218
219 mutex_enter(softnet_lock);
220 if (so->so_proto->pr_type == SOCK_DGRAM) {
221 if (!sbappendaddr(&so->so_rcv, rmsg.msg_name, m, NULL)) {
222 m_freem(m);
223 }
224 } else {
225 sbappendstream(&so->so_rcv, m);
226 }
227
228 sorwakeup(so);
229 mutex_exit(softnet_lock);
230 }
231
232 static void
233 sockin_accept(struct socket *so)
234 {
235 struct socket *nso;
236 struct sockaddr_in sin;
237 int news, error, slen;
238
239 slen = sizeof(sin);
240 news = rumpuser_net_accept(SO2S(so), (struct sockaddr *)&sin,
241 &slen, &error);
242 if (news == -1)
243 return;
244
245 mutex_enter(softnet_lock);
246 nso = sonewconn(so, SS_ISCONNECTED);
247 if (nso == NULL)
248 goto errout;
249 if (registersock(nso, news) != 0)
250 goto errout;
251 mutex_exit(softnet_lock);
252 return;
253
254 errout:
255 rumpuser_close(news, &error);
256 if (nso)
257 soclose(nso);
258 mutex_exit(softnet_lock);
259 }
260
261 #define POLLTIMEOUT 100 /* check for new entries every 100ms */
262
263 /* XXX: doesn't handle socket (kernel) locking properly? */
264 static void
265 sockinworker(void *arg)
266 {
267 struct pollfd *pfds = NULL, *npfds;
268 struct sockin_unit *su_iter;
269 struct socket *so;
270 int cursock = 0, i, rv, error;
271
272 /*
273 * Loop reading requests. Check for new sockets periodically
274 * (could be smarter, but I'm lazy).
275 */
276 for (;;) {
277 if (rebuild) {
278 npfds = NULL;
279 mutex_enter(&su_mtx);
280 if (nsock)
281 npfds = kmem_alloc(nsock * sizeof(*npfds),
282 KM_NOSLEEP);
283 if (npfds || nsock == 0) {
284 if (pfds)
285 kmem_free(pfds, cursock*sizeof(*pfds));
286 pfds = npfds;
287 cursock = nsock;
288 rebuild = false;
289
290 i = 0;
291 LIST_FOREACH(su_iter, &su_ent, su_entries) {
292 pfds[i].fd = SO2S(su_iter->su_so);
293 pfds[i].events = POLLIN;
294 pfds[i].revents = 0;
295 i++;
296 }
297 KASSERT(i == nsock);
298 }
299 mutex_exit(&su_mtx);
300 }
301
302 /* find affected sockets & process */
303 rv = rumpuser_poll(pfds, cursock, POLLTIMEOUT, &error);
304 for (i = 0; i < cursock && rv > 0; i++) {
305 if (pfds[i].revents & POLLIN) {
306 mutex_enter(&su_mtx);
307 LIST_FOREACH(su_iter, &su_ent, su_entries) {
308 if (SO2S(su_iter->su_so)==pfds[i].fd) {
309 so = su_iter->su_so;
310 mutex_exit(&su_mtx);
311 if(so->so_options&SO_ACCEPTCONN)
312 sockin_accept(so);
313 else
314 sockin_process(so);
315 mutex_enter(&su_mtx);
316 break;
317 }
318 }
319 /* if we can't find it, just wing it */
320 KASSERT(rebuild || su_iter);
321 mutex_exit(&su_mtx);
322 pfds[i].revents = 0;
323 rv--;
324 i = -1;
325 continue;
326 }
327
328 /* something else? ignore */
329 if (pfds[i].revents) {
330 pfds[i].revents = 0;
331 rv--;
332 }
333 }
334 KASSERT(rv <= 0);
335 }
336
337 }
338
339 static void
340 sockin_init(void)
341 {
342 int rv;
343
344 if (rump_threads) {
345 if ((rv = kthread_create(PRI_NONE, 0, NULL, sockinworker,
346 NULL, NULL, "sockwork")) != 0)
347 panic("sockin_init: could not create worker thread\n");
348 } else {
349 printf("sockin_init: no threads => no worker thread\n");
350 }
351 mutex_init(&su_mtx, MUTEX_DEFAULT, IPL_NONE);
352 strlcpy(sockin_if.if_xname, "sockin0", sizeof(sockin_if.if_xname));
353 bpf_ops->bpf_attach(&sockin_if, DLT_NULL, 0, &sockin_if.if_bpf);
354 }
355
356 static int
357 sockin_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
358 struct mbuf *control, struct lwp *l)
359 {
360 int error = 0, rv;
361
362 switch (req) {
363 case PRU_ATTACH:
364 {
365 int news, dummy;
366 int sbsize;
367
368 sosetlock(so);
369 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
370 error = soreserve(so, SOCKIN_SBSIZE, SOCKIN_SBSIZE);
371 if (error)
372 break;
373 }
374
375 news = rumpuser_net_socket(PF_INET, so->so_proto->pr_type,
376 0, &error);
377 if (news == -1)
378 break;
379
380 /* for UDP sockets, make sure we can send&recv max */
381 if (so->so_proto->pr_type == SOCK_DGRAM) {
382 sbsize = SOCKIN_SBSIZE;
383 rumpuser_net_setsockopt(news, SOL_SOCKET, SO_SNDBUF,
384 &sbsize, sizeof(sbsize), &error);
385 sbsize = SOCKIN_SBSIZE;
386 rumpuser_net_setsockopt(news, SOL_SOCKET, SO_RCVBUF,
387 &sbsize, sizeof(sbsize), &error);
388 }
389
390 if ((error = registersock(so, news)) != 0)
391 rumpuser_close(news, &dummy);
392
393 break;
394 }
395
396 case PRU_ACCEPT:
397 /* we do all the work in the worker thread */
398 break;
399
400 case PRU_BIND:
401 rumpuser_net_bind(SO2S(so), mtod(nam, const struct sockaddr *),
402 sizeof(struct sockaddr_in), &error);
403 break;
404
405 case PRU_CONNECT:
406 rv = rumpuser_net_connect(SO2S(so),
407 mtod(nam, struct sockaddr *), sizeof(struct sockaddr_in),
408 &error);
409 if (rv == 0)
410 soisconnected(so);
411 break;
412
413 case PRU_LISTEN:
414 rumpuser_net_listen(SO2S(so), so->so_qlimit, &error);
415 break;
416
417 case PRU_SEND:
418 {
419 struct sockaddr *saddr;
420 struct msghdr mhdr;
421 size_t iov_max, i;
422 struct iovec iov_buf[32], *iov;
423 struct mbuf *m2;
424 size_t tot;
425 int s;
426
427 if (sockin_if.if_bpf)
428 bpf_ops->bpf_mtap_af(sockin_if.if_bpf, AF_UNSPEC, m);
429
430 memset(&mhdr, 0, sizeof(mhdr));
431
432 iov_max = 0;
433 for (m2 = m; m2 != NULL; m2 = m2->m_next) {
434 iov_max++;
435 }
436
437 if (iov_max <= __arraycount(iov_buf)) {
438 iov = iov_buf;
439 } else {
440 iov = kmem_alloc(sizeof(struct iovec) * iov_max,
441 KM_SLEEP);
442 }
443
444 tot = 0;
445 for (i = 0, m2 = m; m2 != NULL; m2 = m2->m_next, i++) {
446 iov[i].iov_base = m2->m_data;
447 iov[i].iov_len = m2->m_len;
448 tot += m2->m_len;
449 }
450 mhdr.msg_iov = iov;
451 mhdr.msg_iovlen = i;
452 s = SO2S(so);
453
454 if (nam != NULL) {
455 saddr = mtod(nam, struct sockaddr *);
456 mhdr.msg_name = saddr;
457 mhdr.msg_namelen = saddr->sa_len;
458 }
459
460 rumpuser_net_sendmsg(s, &mhdr, 0, &error);
461
462 if (iov != iov_buf)
463 kmem_free(iov, sizeof(struct iovec) * iov_max);
464
465 m_freem(m);
466 m_freem(control);
467
468 /* this assumes too many things to list.. buthey, testing */
469 if (!rump_threads)
470 sockin_process(so);
471 }
472 break;
473
474 case PRU_SHUTDOWN:
475 removesock(so);
476 break;
477
478 case PRU_SOCKADDR:
479 case PRU_PEERADDR:
480 {
481 int slen = nam->m_len;
482 enum rumpuser_getnametype which;
483
484 if (req == PRU_SOCKADDR)
485 which = RUMPUSER_SOCKNAME;
486 else
487 which = RUMPUSER_PEERNAME;
488 rumpuser_net_getname(SO2S(so),
489 mtod(nam, struct sockaddr *), &slen, which, &error);
490 if (error == 0)
491 nam->m_len = slen;
492 break;
493 }
494
495 default:
496 panic("sockin_usrreq: IMPLEMENT ME, req %d not supported", req);
497 }
498
499 return error;
500 }
501
502 static int
503 sockin_ctloutput(int op, struct socket *so, struct sockopt *sopt)
504 {
505 int error;
506
507 rumpuser_net_setsockopt(SO2S(so), sopt->sopt_level,
508 sopt->sopt_name, sopt->sopt_data, sopt->sopt_size, &error);
509 return error;
510 }
511