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