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