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