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