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