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