xref: /dflybsd-src/sys/kern/uipc_msg.c (revision 22db36083bc98aa8a7876ca8e7811e6a33362f59)
19eeaa8a9SJeffrey Hsu /*
266d6c637SJeffrey Hsu  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
366d6c637SJeffrey Hsu  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
466d6c637SJeffrey Hsu  *
566d6c637SJeffrey Hsu  * This code is derived from software contributed to The DragonFly Project
666d6c637SJeffrey Hsu  * by Jeffrey M. Hsu.
79eeaa8a9SJeffrey Hsu  *
89eeaa8a9SJeffrey Hsu  * Redistribution and use in source and binary forms, with or without
99eeaa8a9SJeffrey Hsu  * modification, are permitted provided that the following conditions
109eeaa8a9SJeffrey Hsu  * are met:
119eeaa8a9SJeffrey Hsu  * 1. Redistributions of source code must retain the above copyright
129eeaa8a9SJeffrey Hsu  *    notice, this list of conditions and the following disclaimer.
139eeaa8a9SJeffrey Hsu  * 2. Redistributions in binary form must reproduce the above copyright
149eeaa8a9SJeffrey Hsu  *    notice, this list of conditions and the following disclaimer in the
159eeaa8a9SJeffrey Hsu  *    documentation and/or other materials provided with the distribution.
1666d6c637SJeffrey Hsu  * 3. Neither the name of The DragonFly Project nor the names of its
1766d6c637SJeffrey Hsu  *    contributors may be used to endorse or promote products derived
1866d6c637SJeffrey Hsu  *    from this software without specific, prior written permission.
1966d6c637SJeffrey Hsu  *
2066d6c637SJeffrey Hsu  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2166d6c637SJeffrey Hsu  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2266d6c637SJeffrey Hsu  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2366d6c637SJeffrey Hsu  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2466d6c637SJeffrey Hsu  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2566d6c637SJeffrey Hsu  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2666d6c637SJeffrey Hsu  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2766d6c637SJeffrey Hsu  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2866d6c637SJeffrey Hsu  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2966d6c637SJeffrey Hsu  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3066d6c637SJeffrey Hsu  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3166d6c637SJeffrey Hsu  * SUCH DAMAGE.
329eeaa8a9SJeffrey Hsu  */
339eeaa8a9SJeffrey Hsu 
349eeaa8a9SJeffrey Hsu #include <sys/param.h>
359eeaa8a9SJeffrey Hsu #include <sys/systm.h>
36e71a125fSAggelos Economopoulos #include <sys/kernel.h>
37805c8e8eSzrj #include <sys/malloc.h>
389eeaa8a9SJeffrey Hsu #include <sys/msgport.h>
399eeaa8a9SJeffrey Hsu #include <sys/protosw.h>
409eeaa8a9SJeffrey Hsu #include <sys/socket.h>
419eeaa8a9SJeffrey Hsu #include <sys/socketvar.h>
429eeaa8a9SJeffrey Hsu #include <sys/socketops.h>
439eeaa8a9SJeffrey Hsu #include <sys/thread.h>
44d3266d67SMatthew Dillon #include <sys/msgport2.h>
4596c6eb29SSepherosa Ziehau #include <sys/spinlock2.h>
46f65f767cSSepherosa Ziehau #include <sys/sysctl.h>
470ad8e15eSSepherosa Ziehau #include <sys/mbuf.h>
48de0003feSAggelos Economopoulos #include <vm/pmap.h>
496999cd81SMatthew Dillon 
504599cf19SMatthew Dillon #include <net/netmsg2.h>
510ebcb559SSepherosa Ziehau #include <net/netisr2.h>
526999cd81SMatthew Dillon #include <sys/socketvar2.h>
539eeaa8a9SJeffrey Hsu 
549eeaa8a9SJeffrey Hsu #include <net/netisr.h>
559eeaa8a9SJeffrey Hsu #include <net/netmsg.h>
569eeaa8a9SJeffrey Hsu 
57f65f767cSSepherosa Ziehau static int async_rcvd_drop_race = 0;
58f65f767cSSepherosa Ziehau SYSCTL_INT(_kern_ipc, OID_AUTO, async_rcvd_drop_race, CTLFLAG_RW,
59f65f767cSSepherosa Ziehau     &async_rcvd_drop_race, 0, "# of asynchronized pru_rcvd msg drop races");
60f65f767cSSepherosa Ziehau 
614402d8a2SMatthew Dillon /*
629116be8eSMatthew Dillon  * Abort a socket and free it, asynchronously.  Called from
63fd27efb4SSepherosa Ziehau  * soabort_async() only.  soabort_async() got a ref on the
64fd27efb4SSepherosa Ziehau  * socket which we must free on reply.
654402d8a2SMatthew Dillon  */
664402d8a2SMatthew Dillon void
so_pru_abort_async(struct socket * so)67fd27efb4SSepherosa Ziehau so_pru_abort_async(struct socket *so)
684402d8a2SMatthew Dillon {
694402d8a2SMatthew Dillon 	struct netmsg_pru_abort *msg;
704402d8a2SMatthew Dillon 
714402d8a2SMatthew Dillon 	msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_ZERO);
72c3d495a1SMatthew Dillon 	netmsg_init(&msg->base, so, &netisr_afree_free_so_rport,
73002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_abort);
74002c1265SMatthew Dillon 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
759eeaa8a9SJeffrey Hsu }
769eeaa8a9SJeffrey Hsu 
77fd86a41cSSepherosa Ziehau /*
7894aba184SSepherosa Ziehau  * Abort a socket and free it.  Called from soabort_direct() only.
79fd86a41cSSepherosa Ziehau  * Caller must make sure that the current CPU is inpcb's owner CPU.
8094aba184SSepherosa Ziehau  * soabort_direct() got a ref on the socket which we must free.
81fd86a41cSSepherosa Ziehau  */
82fd86a41cSSepherosa Ziehau void
so_pru_abort_direct(struct socket * so)832deaa561SSepherosa Ziehau so_pru_abort_direct(struct socket *so)
84fd86a41cSSepherosa Ziehau {
85002c1265SMatthew Dillon 	struct netmsg_pru_abort msg;
86002c1265SMatthew Dillon 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_abort;
87002c1265SMatthew Dillon 
88002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
89002c1265SMatthew Dillon 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
90002c1265SMatthew Dillon 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
91002c1265SMatthew Dillon 	func((netmsg_t)&msg);
920fec1ecdSMatthew Dillon 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
93c3d495a1SMatthew Dillon 	sofree(msg.base.nm_so);
94fd86a41cSSepherosa Ziehau }
95fd86a41cSSepherosa Ziehau 
969eeaa8a9SJeffrey Hsu int
so_pru_accept(struct socket * so,struct sockaddr ** nam)97f328adf5SSepherosa Ziehau so_pru_accept(struct socket *so, struct sockaddr **nam)
989eeaa8a9SJeffrey Hsu {
999eeaa8a9SJeffrey Hsu 	struct netmsg_pru_accept msg;
1009eeaa8a9SJeffrey Hsu 
101f328adf5SSepherosa Ziehau 	netmsg_init(&msg.base, so, &curthread->td_msgport,
102f328adf5SSepherosa Ziehau 	    0, so->so_proto->pr_usrreqs->pru_accept);
1039eeaa8a9SJeffrey Hsu 	msg.nm_nam = nam;
104f328adf5SSepherosa Ziehau 
105f328adf5SSepherosa Ziehau 	return lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
1069eeaa8a9SJeffrey Hsu }
1079eeaa8a9SJeffrey Hsu 
1089eeaa8a9SJeffrey Hsu int
so_pru_attach(struct socket * so,int proto,struct pru_attach_info * ai)1099eeaa8a9SJeffrey Hsu so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
1109eeaa8a9SJeffrey Hsu {
1119eeaa8a9SJeffrey Hsu 	struct netmsg_pru_attach msg;
112002c1265SMatthew Dillon 	int error;
1139eeaa8a9SJeffrey Hsu 
114002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
115002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_attach);
1169eeaa8a9SJeffrey Hsu 	msg.nm_proto = proto;
1179eeaa8a9SJeffrey Hsu 	msg.nm_ai = ai;
118002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
1199eeaa8a9SJeffrey Hsu 	return (error);
1209eeaa8a9SJeffrey Hsu }
1219eeaa8a9SJeffrey Hsu 
122002c1265SMatthew Dillon int
so_pru_attach_direct(struct socket * so,int proto,struct pru_attach_info * ai)123002c1265SMatthew Dillon so_pru_attach_direct(struct socket *so, int proto, struct pru_attach_info *ai)
124002c1265SMatthew Dillon {
125002c1265SMatthew Dillon 	struct netmsg_pru_attach msg;
126002c1265SMatthew Dillon 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_attach;
127002c1265SMatthew Dillon 
128002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
129002c1265SMatthew Dillon 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
130002c1265SMatthew Dillon 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
131002c1265SMatthew Dillon 	msg.nm_proto = proto;
132002c1265SMatthew Dillon 	msg.nm_ai = ai;
133002c1265SMatthew Dillon 	func((netmsg_t)&msg);
1340fec1ecdSMatthew Dillon 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
135002c1265SMatthew Dillon 	return(msg.base.lmsg.ms_error);
136002c1265SMatthew Dillon }
137002c1265SMatthew Dillon 
138c892825fSSepherosa Ziehau int
so_pru_attach_fast(struct socket * so,int proto,struct pru_attach_info * ai)139c892825fSSepherosa Ziehau so_pru_attach_fast(struct socket *so, int proto, struct pru_attach_info *ai)
140c892825fSSepherosa Ziehau {
141c892825fSSepherosa Ziehau 	struct netmsg_pru_attach *msg;
142c892825fSSepherosa Ziehau 	int error;
143c892825fSSepherosa Ziehau 
144c892825fSSepherosa Ziehau 	error = so->so_proto->pr_usrreqs->pru_preattach(so, proto, ai);
145c892825fSSepherosa Ziehau 	if (error)
146c892825fSSepherosa Ziehau 		return error;
147c892825fSSepherosa Ziehau 
148c892825fSSepherosa Ziehau 	msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_NULLOK);
149c892825fSSepherosa Ziehau 	if (msg == NULL) {
150c892825fSSepherosa Ziehau 		/*
151c892825fSSepherosa Ziehau 		 * Fail to allocate message; fallback to
152c892825fSSepherosa Ziehau 		 * synchronized pru_attach.
153c892825fSSepherosa Ziehau 		 */
154c892825fSSepherosa Ziehau 		return so_pru_attach(so, proto, NULL /* postattach */);
155c892825fSSepherosa Ziehau 	}
156c892825fSSepherosa Ziehau 
157c892825fSSepherosa Ziehau 	netmsg_init(&msg->base, so, &netisr_afree_rport, 0,
158c892825fSSepherosa Ziehau 	    so->so_proto->pr_usrreqs->pru_attach);
159c892825fSSepherosa Ziehau 	msg->nm_proto = proto;
160c892825fSSepherosa Ziehau 	msg->nm_ai = NULL; /* postattach */
1610ebcb559SSepherosa Ziehau 	if (so->so_port == netisr_curport())
1620ebcb559SSepherosa Ziehau 		lwkt_sendmsg_oncpu(so->so_port, &msg->base.lmsg);
1630ebcb559SSepherosa Ziehau 	else
164c892825fSSepherosa Ziehau 		lwkt_sendmsg(so->so_port, &msg->base.lmsg);
165c892825fSSepherosa Ziehau 
166c892825fSSepherosa Ziehau 	return 0;
167c892825fSSepherosa Ziehau }
168c892825fSSepherosa Ziehau 
16948e7b118SMatthew Dillon /*
17048e7b118SMatthew Dillon  * NOTE: If the target port changes the bind operation will deal with it.
17148e7b118SMatthew Dillon  */
1729eeaa8a9SJeffrey Hsu int
so_pru_bind(struct socket * so,struct sockaddr * nam,struct thread * td)1739eeaa8a9SJeffrey Hsu so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1749eeaa8a9SJeffrey Hsu {
1759eeaa8a9SJeffrey Hsu 	struct netmsg_pru_bind msg;
176002c1265SMatthew Dillon 	int error;
1779eeaa8a9SJeffrey Hsu 
178002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
179002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_bind);
1809eeaa8a9SJeffrey Hsu 	msg.nm_nam = nam;
181002c1265SMatthew Dillon 	msg.nm_td = td;		/* used only for prison_ip() */
1827c0074d8SSepherosa Ziehau 	msg.nm_flags = 0;
183002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
1849eeaa8a9SJeffrey Hsu 	return (error);
1859eeaa8a9SJeffrey Hsu }
1869eeaa8a9SJeffrey Hsu 
1879eeaa8a9SJeffrey Hsu int
so_pru_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1889eeaa8a9SJeffrey Hsu so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1899eeaa8a9SJeffrey Hsu {
1909eeaa8a9SJeffrey Hsu 	struct netmsg_pru_connect msg;
191002c1265SMatthew Dillon 	int error;
1929eeaa8a9SJeffrey Hsu 
193002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
194002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_connect);
1959eeaa8a9SJeffrey Hsu 	msg.nm_nam = nam;
1969eeaa8a9SJeffrey Hsu 	msg.nm_td = td;
197002c1265SMatthew Dillon 	msg.nm_m = NULL;
198e368a6e9SSepherosa Ziehau 	msg.nm_sndflags = 0;
199002c1265SMatthew Dillon 	msg.nm_flags = 0;
200002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
2019eeaa8a9SJeffrey Hsu 	return (error);
2029eeaa8a9SJeffrey Hsu }
2039eeaa8a9SJeffrey Hsu 
2049eeaa8a9SJeffrey Hsu int
so_pru_connect_async(struct socket * so,struct sockaddr * nam,struct thread * td)205e368a6e9SSepherosa Ziehau so_pru_connect_async(struct socket *so, struct sockaddr *nam, struct thread *td)
206e368a6e9SSepherosa Ziehau {
207e368a6e9SSepherosa Ziehau 	struct netmsg_pru_connect *msg;
208e368a6e9SSepherosa Ziehau 	int error, flags;
209e368a6e9SSepherosa Ziehau 
210e368a6e9SSepherosa Ziehau 	KASSERT(so->so_proto->pr_usrreqs->pru_preconnect != NULL,
211e368a6e9SSepherosa Ziehau 	    ("async pru_connect is not supported"));
212e368a6e9SSepherosa Ziehau 
213e368a6e9SSepherosa Ziehau 	/* NOTE: sockaddr immediately follows netmsg */
214d8af44dcSSepherosa Ziehau 	msg = kmalloc(sizeof(*msg) + nam->sa_len, M_LWKTMSG,
215d8af44dcSSepherosa Ziehau 	    M_WAITOK | M_NULLOK);
216e368a6e9SSepherosa Ziehau 	if (msg == NULL) {
217e368a6e9SSepherosa Ziehau 		/*
218d8af44dcSSepherosa Ziehau 		 * Fail to allocate message; fallback to
219d8af44dcSSepherosa Ziehau 		 * synchronized pru_connect.
220e368a6e9SSepherosa Ziehau 		 */
221e368a6e9SSepherosa Ziehau 		return so_pru_connect(so, nam, td);
222e368a6e9SSepherosa Ziehau 	}
223e368a6e9SSepherosa Ziehau 
224e368a6e9SSepherosa Ziehau 	error = so->so_proto->pr_usrreqs->pru_preconnect(so, nam, td);
225e368a6e9SSepherosa Ziehau 	if (error) {
226e368a6e9SSepherosa Ziehau 		kfree(msg, M_LWKTMSG);
227e368a6e9SSepherosa Ziehau 		return error;
228e368a6e9SSepherosa Ziehau 	}
229e368a6e9SSepherosa Ziehau 
230e368a6e9SSepherosa Ziehau 	flags = PRUC_ASYNC;
231e368a6e9SSepherosa Ziehau 	if (td != NULL && (so->so_proto->pr_flags & PR_ACONN_HOLDTD)) {
232e368a6e9SSepherosa Ziehau 		lwkt_hold(td);
233e368a6e9SSepherosa Ziehau 		flags |= PRUC_HELDTD;
234e368a6e9SSepherosa Ziehau 	}
235e368a6e9SSepherosa Ziehau 
236e368a6e9SSepherosa Ziehau 	netmsg_init(&msg->base, so, &netisr_afree_rport, 0,
237e368a6e9SSepherosa Ziehau 	    so->so_proto->pr_usrreqs->pru_connect);
238e368a6e9SSepherosa Ziehau 	msg->nm_nam = (struct sockaddr *)(msg + 1);
239e368a6e9SSepherosa Ziehau 	memcpy(msg->nm_nam, nam, nam->sa_len);
240e368a6e9SSepherosa Ziehau 	msg->nm_td = td;
241e368a6e9SSepherosa Ziehau 	msg->nm_m = NULL;
242e368a6e9SSepherosa Ziehau 	msg->nm_sndflags = 0;
243e368a6e9SSepherosa Ziehau 	msg->nm_flags = flags;
2440ebcb559SSepherosa Ziehau 	if (so->so_port == netisr_curport())
2450ebcb559SSepherosa Ziehau 		lwkt_sendmsg_oncpu(so->so_port, &msg->base.lmsg);
2460ebcb559SSepherosa Ziehau 	else
247e368a6e9SSepherosa Ziehau 		lwkt_sendmsg(so->so_port, &msg->base.lmsg);
248e368a6e9SSepherosa Ziehau 	return 0;
249e368a6e9SSepherosa Ziehau }
250e368a6e9SSepherosa Ziehau 
251e368a6e9SSepherosa Ziehau int
so_pru_connect2(struct socket * so1,struct socket * so2,struct ucred * cred)252*22db3608SMatthew Dillon so_pru_connect2(struct socket *so1, struct socket *so2, struct ucred *cred)
2539eeaa8a9SJeffrey Hsu {
2549eeaa8a9SJeffrey Hsu 	struct netmsg_pru_connect2 msg;
255002c1265SMatthew Dillon 	int error;
2569eeaa8a9SJeffrey Hsu 
257002c1265SMatthew Dillon 	netmsg_init(&msg.base, so1, &curthread->td_msgport,
258002c1265SMatthew Dillon 		    0, so1->so_proto->pr_usrreqs->pru_connect2);
2599eeaa8a9SJeffrey Hsu 	msg.nm_so1 = so1;
2609eeaa8a9SJeffrey Hsu 	msg.nm_so2 = so2;
261*22db3608SMatthew Dillon 	msg.nm_cred = cred;
262002c1265SMatthew Dillon 	error = lwkt_domsg(so1->so_port, &msg.base.lmsg, 0);
2639eeaa8a9SJeffrey Hsu 	return (error);
2649eeaa8a9SJeffrey Hsu }
2659eeaa8a9SJeffrey Hsu 
266002c1265SMatthew Dillon /*
267002c1265SMatthew Dillon  * WARNING!  Synchronous call from user context.  Control function may do
268002c1265SMatthew Dillon  *	     copyin/copyout.
269002c1265SMatthew Dillon  */
2709eeaa8a9SJeffrey Hsu int
so_pru_control_direct(struct socket * so,u_long cmd,caddr_t data,struct ifnet * ifp)271002c1265SMatthew Dillon so_pru_control_direct(struct socket *so, u_long cmd, caddr_t data,
272002c1265SMatthew Dillon 		      struct ifnet *ifp)
2739eeaa8a9SJeffrey Hsu {
2749eeaa8a9SJeffrey Hsu 	struct netmsg_pru_control msg;
275002c1265SMatthew Dillon 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_control;
2769eeaa8a9SJeffrey Hsu 
277002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
278002c1265SMatthew Dillon 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
279002c1265SMatthew Dillon 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
2809eeaa8a9SJeffrey Hsu 	msg.nm_cmd = cmd;
2819eeaa8a9SJeffrey Hsu 	msg.nm_data = data;
2829eeaa8a9SJeffrey Hsu 	msg.nm_ifp = ifp;
283002c1265SMatthew Dillon 	msg.nm_td = curthread;
284002c1265SMatthew Dillon 	func((netmsg_t)&msg);
2850fec1ecdSMatthew Dillon 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
286002c1265SMatthew Dillon 	return(msg.base.lmsg.ms_error);
2879eeaa8a9SJeffrey Hsu }
2889eeaa8a9SJeffrey Hsu 
2899eeaa8a9SJeffrey Hsu int
so_pru_detach(struct socket * so)2909eeaa8a9SJeffrey Hsu so_pru_detach(struct socket *so)
2919eeaa8a9SJeffrey Hsu {
2929eeaa8a9SJeffrey Hsu 	struct netmsg_pru_detach msg;
293002c1265SMatthew Dillon 	int error;
2949eeaa8a9SJeffrey Hsu 
295002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
296002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_detach);
297002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
2989eeaa8a9SJeffrey Hsu 	return (error);
2999eeaa8a9SJeffrey Hsu }
3009eeaa8a9SJeffrey Hsu 
301be4519a2SSepherosa Ziehau int
so_pru_detach_direct(struct socket * so)302acd31a69SSepherosa Ziehau so_pru_detach_direct(struct socket *so)
303acd31a69SSepherosa Ziehau {
304acd31a69SSepherosa Ziehau 	struct netmsg_pru_detach msg;
305acd31a69SSepherosa Ziehau 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_detach;
306acd31a69SSepherosa Ziehau 
307acd31a69SSepherosa Ziehau 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
308acd31a69SSepherosa Ziehau 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
309acd31a69SSepherosa Ziehau 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
310acd31a69SSepherosa Ziehau 	func((netmsg_t)&msg);
311acd31a69SSepherosa Ziehau 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
312be4519a2SSepherosa Ziehau 	return(msg.base.lmsg.ms_error);
313acd31a69SSepherosa Ziehau }
314acd31a69SSepherosa Ziehau 
3159eeaa8a9SJeffrey Hsu int
so_pru_disconnect(struct socket * so)3169eeaa8a9SJeffrey Hsu so_pru_disconnect(struct socket *so)
3179eeaa8a9SJeffrey Hsu {
3189eeaa8a9SJeffrey Hsu 	struct netmsg_pru_disconnect msg;
319002c1265SMatthew Dillon 	int error;
3209eeaa8a9SJeffrey Hsu 
321002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
322002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_disconnect);
323002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
3249eeaa8a9SJeffrey Hsu 	return (error);
3259eeaa8a9SJeffrey Hsu }
3269eeaa8a9SJeffrey Hsu 
327acd31a69SSepherosa Ziehau void
so_pru_disconnect_direct(struct socket * so)328acd31a69SSepherosa Ziehau so_pru_disconnect_direct(struct socket *so)
329acd31a69SSepherosa Ziehau {
330acd31a69SSepherosa Ziehau 	struct netmsg_pru_disconnect msg;
331acd31a69SSepherosa Ziehau 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_disconnect;
332acd31a69SSepherosa Ziehau 
333acd31a69SSepherosa Ziehau 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
334acd31a69SSepherosa Ziehau 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
335acd31a69SSepherosa Ziehau 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
336acd31a69SSepherosa Ziehau 	func((netmsg_t)&msg);
337acd31a69SSepherosa Ziehau 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
338acd31a69SSepherosa Ziehau }
339acd31a69SSepherosa Ziehau 
3409eeaa8a9SJeffrey Hsu int
so_pru_listen(struct socket * so,struct thread * td)3419eeaa8a9SJeffrey Hsu so_pru_listen(struct socket *so, struct thread *td)
3429eeaa8a9SJeffrey Hsu {
3439eeaa8a9SJeffrey Hsu 	struct netmsg_pru_listen msg;
344002c1265SMatthew Dillon 	int error;
3459eeaa8a9SJeffrey Hsu 
346002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
347002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_listen);
3489eeaa8a9SJeffrey Hsu 	msg.nm_td = td;		/* used only for prison_ip() XXX JH */
3491c92f416SSepherosa Ziehau 	msg.nm_flags = 0;
350002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
3519eeaa8a9SJeffrey Hsu 	return (error);
3529eeaa8a9SJeffrey Hsu }
3539eeaa8a9SJeffrey Hsu 
3549eeaa8a9SJeffrey Hsu int
so_pru_peeraddr(struct socket * so,struct sockaddr ** nam)3559eeaa8a9SJeffrey Hsu so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
3569eeaa8a9SJeffrey Hsu {
3579eeaa8a9SJeffrey Hsu 	struct netmsg_pru_peeraddr msg;
358002c1265SMatthew Dillon 	int error;
3599eeaa8a9SJeffrey Hsu 
360002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
361002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_peeraddr);
3629eeaa8a9SJeffrey Hsu 	msg.nm_nam = nam;
363002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
3649eeaa8a9SJeffrey Hsu 	return (error);
3659eeaa8a9SJeffrey Hsu }
3669eeaa8a9SJeffrey Hsu 
3679eeaa8a9SJeffrey Hsu int
so_pru_rcvd(struct socket * so,int flags)3689eeaa8a9SJeffrey Hsu so_pru_rcvd(struct socket *so, int flags)
3699eeaa8a9SJeffrey Hsu {
3709eeaa8a9SJeffrey Hsu 	struct netmsg_pru_rcvd msg;
371002c1265SMatthew Dillon 	int error;
3729eeaa8a9SJeffrey Hsu 
373002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
374002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_rcvd);
3759eeaa8a9SJeffrey Hsu 	msg.nm_flags = flags;
37696c6eb29SSepherosa Ziehau 	msg.nm_pru_flags = 0;
377002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
3789eeaa8a9SJeffrey Hsu 	return (error);
3799eeaa8a9SJeffrey Hsu }
3809eeaa8a9SJeffrey Hsu 
38196c6eb29SSepherosa Ziehau void
so_pru_rcvd_async(struct socket * so)38296c6eb29SSepherosa Ziehau so_pru_rcvd_async(struct socket *so)
38396c6eb29SSepherosa Ziehau {
38496c6eb29SSepherosa Ziehau 	lwkt_msg_t lmsg = &so->so_rcvd_msg.base.lmsg;
38596c6eb29SSepherosa Ziehau 
38696c6eb29SSepherosa Ziehau 	KASSERT(so->so_proto->pr_flags & PR_ASYNC_RCVD,
38796c6eb29SSepherosa Ziehau 	    ("async pru_rcvd is not supported"));
38896c6eb29SSepherosa Ziehau 
3896999cd81SMatthew Dillon 	/*
3906999cd81SMatthew Dillon 	 * WARNING!  Spinlock is a bit dodgy, use hacked up sendmsg
3916999cd81SMatthew Dillon 	 *	     to avoid deadlocking.
3926999cd81SMatthew Dillon 	 */
39396c6eb29SSepherosa Ziehau 	spin_lock(&so->so_rcvd_spin);
394a77d3dfeSSepherosa Ziehau 	if ((so->so_rcvd_msg.nm_pru_flags & PRUR_DEAD) == 0) {
3956999cd81SMatthew Dillon 		if (lmsg->ms_flags & MSGF_DONE) {
39662734c13SSepherosa Ziehau 			lwkt_sendmsg_prepare(so->so_port, lmsg);
3976999cd81SMatthew Dillon 			spin_unlock(&so->so_rcvd_spin);
3980ebcb559SSepherosa Ziehau 			if (so->so_port == netisr_curport())
3990ebcb559SSepherosa Ziehau 				lwkt_sendmsg_start_oncpu(so->so_port, lmsg);
4000ebcb559SSepherosa Ziehau 			else
40162734c13SSepherosa Ziehau 				lwkt_sendmsg_start(so->so_port, lmsg);
4026999cd81SMatthew Dillon 		} else {
4036999cd81SMatthew Dillon 			spin_unlock(&so->so_rcvd_spin);
4046999cd81SMatthew Dillon 		}
405a77d3dfeSSepherosa Ziehau 	} else {
40696c6eb29SSepherosa Ziehau 		spin_unlock(&so->so_rcvd_spin);
40796c6eb29SSepherosa Ziehau 	}
4086999cd81SMatthew Dillon }
40996c6eb29SSepherosa Ziehau 
4109eeaa8a9SJeffrey Hsu int
so_pru_rcvoob(struct socket * so,struct mbuf * m,int flags)4119eeaa8a9SJeffrey Hsu so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
4129eeaa8a9SJeffrey Hsu {
4139eeaa8a9SJeffrey Hsu 	struct netmsg_pru_rcvoob msg;
414002c1265SMatthew Dillon 	int error;
4159eeaa8a9SJeffrey Hsu 
416002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
417002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_rcvoob);
4189eeaa8a9SJeffrey Hsu 	msg.nm_m = m;
4199eeaa8a9SJeffrey Hsu 	msg.nm_flags = flags;
420002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
4219eeaa8a9SJeffrey Hsu 	return (error);
4229eeaa8a9SJeffrey Hsu }
4239eeaa8a9SJeffrey Hsu 
42448e7b118SMatthew Dillon /*
42548e7b118SMatthew Dillon  * NOTE: If the target port changes the implied connect will deal with it.
42648e7b118SMatthew Dillon  */
4279eeaa8a9SJeffrey Hsu int
so_pru_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct thread * td)42848e7b118SMatthew Dillon so_pru_send(struct socket *so, int flags, struct mbuf *m,
42948e7b118SMatthew Dillon 	    struct sockaddr *addr, struct mbuf *control, struct thread *td)
4309eeaa8a9SJeffrey Hsu {
4319eeaa8a9SJeffrey Hsu 	struct netmsg_pru_send msg;
432002c1265SMatthew Dillon 	int error;
4339eeaa8a9SJeffrey Hsu 
434002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
435002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_send);
4369eeaa8a9SJeffrey Hsu 	msg.nm_flags = flags;
4379eeaa8a9SJeffrey Hsu 	msg.nm_m = m;
4389eeaa8a9SJeffrey Hsu 	msg.nm_addr = addr;
4399eeaa8a9SJeffrey Hsu 	msg.nm_control = control;
4409eeaa8a9SJeffrey Hsu 	msg.nm_td = td;
441002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
4429eeaa8a9SJeffrey Hsu 	return (error);
4439eeaa8a9SJeffrey Hsu }
4449eeaa8a9SJeffrey Hsu 
445f2a3782eSSepherosa Ziehau void
so_pru_sync(struct socket * so)446f2a3782eSSepherosa Ziehau so_pru_sync(struct socket *so)
447f2a3782eSSepherosa Ziehau {
448f2a3782eSSepherosa Ziehau 	struct netmsg_base msg;
449f2a3782eSSepherosa Ziehau 
450f2a3782eSSepherosa Ziehau 	netmsg_init(&msg, so, &curthread->td_msgport, 0,
45179f504caSSepherosa Ziehau 	    netmsg_sync_handler);
452f2a3782eSSepherosa Ziehau 	lwkt_domsg(so->so_port, &msg.lmsg, 0);
453f2a3782eSSepherosa Ziehau }
454f2a3782eSSepherosa Ziehau 
4550ad8e15eSSepherosa Ziehau void
so_pru_send_async(struct socket * so,int flags,struct mbuf * m,struct sockaddr * addr0,struct mbuf * control,struct thread * td)4560ad8e15eSSepherosa Ziehau so_pru_send_async(struct socket *so, int flags, struct mbuf *m,
4577b7dc575SSepherosa Ziehau     struct sockaddr *addr0, struct mbuf *control, struct thread *td)
4580ad8e15eSSepherosa Ziehau {
4590ad8e15eSSepherosa Ziehau 	struct netmsg_pru_send *msg;
4607b7dc575SSepherosa Ziehau 	struct sockaddr *addr = NULL;
4610ad8e15eSSepherosa Ziehau 
4622ef7b293SSepherosa Ziehau 	KASSERT(so->so_proto->pr_flags & PR_ASYNC_SEND,
463ed20d0e3SSascha Wildner 	    ("async pru_send is not supported"));
4642ef7b293SSepherosa Ziehau 
4657b7dc575SSepherosa Ziehau 	if (addr0 != NULL) {
46633e0f82bSSepherosa Ziehau 		addr = kmalloc(addr0->sa_len, M_SONAME, M_WAITOK | M_NULLOK);
467a007c531SSepherosa Ziehau 		if (addr == NULL) {
468a007c531SSepherosa Ziehau 			/*
46933e0f82bSSepherosa Ziehau 			 * Fail to allocate address; fallback to
47033e0f82bSSepherosa Ziehau 			 * synchronized pru_send.
471a007c531SSepherosa Ziehau 			 */
472a007c531SSepherosa Ziehau 			so_pru_send(so, flags, m, addr0, control, td);
473a007c531SSepherosa Ziehau 			return;
474a007c531SSepherosa Ziehau 		}
4757b7dc575SSepherosa Ziehau 		memcpy(addr, addr0, addr0->sa_len);
4767b7dc575SSepherosa Ziehau 		flags |= PRUS_FREEADDR;
4777b7dc575SSepherosa Ziehau 	}
478a007c531SSepherosa Ziehau 	flags |= PRUS_NOREPLY;
4797b7dc575SSepherosa Ziehau 
480c0467c22SSepherosa Ziehau 	if (td != NULL && (so->so_proto->pr_flags & PR_ASEND_HOLDTD)) {
481c0467c22SSepherosa Ziehau 		lwkt_hold(td);
482c0467c22SSepherosa Ziehau 		flags |= PRUS_HELDTD;
483c0467c22SSepherosa Ziehau 	}
484c0467c22SSepherosa Ziehau 
4850ad8e15eSSepherosa Ziehau 	msg = &m->m_hdr.mh_sndmsg;
4860ad8e15eSSepherosa Ziehau 	netmsg_init(&msg->base, so, &netisr_apanic_rport,
4870ad8e15eSSepherosa Ziehau 		    0, so->so_proto->pr_usrreqs->pru_send);
4887b7dc575SSepherosa Ziehau 	msg->nm_flags = flags;
4890ad8e15eSSepherosa Ziehau 	msg->nm_m = m;
4900ad8e15eSSepherosa Ziehau 	msg->nm_addr = addr;
4910ad8e15eSSepherosa Ziehau 	msg->nm_control = control;
4920ad8e15eSSepherosa Ziehau 	msg->nm_td = td;
4930ebcb559SSepherosa Ziehau 	if (so->so_port == netisr_curport())
4940ebcb559SSepherosa Ziehau 		lwkt_sendmsg_oncpu(so->so_port, &msg->base.lmsg);
4950ebcb559SSepherosa Ziehau 	else
4960ad8e15eSSepherosa Ziehau 		lwkt_sendmsg(so->so_port, &msg->base.lmsg);
4970ad8e15eSSepherosa Ziehau }
4980ad8e15eSSepherosa Ziehau 
4999eeaa8a9SJeffrey Hsu int
so_pru_sense(struct socket * so,struct stat * sb)5009eeaa8a9SJeffrey Hsu so_pru_sense(struct socket *so, struct stat *sb)
5019eeaa8a9SJeffrey Hsu {
5029eeaa8a9SJeffrey Hsu 	struct netmsg_pru_sense msg;
503002c1265SMatthew Dillon 	int error;
5049eeaa8a9SJeffrey Hsu 
505002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
506002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_sense);
5079eeaa8a9SJeffrey Hsu 	msg.nm_stat = sb;
508002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
5099eeaa8a9SJeffrey Hsu 	return (error);
5109eeaa8a9SJeffrey Hsu }
5119eeaa8a9SJeffrey Hsu 
5129eeaa8a9SJeffrey Hsu int
so_pru_shutdown(struct socket * so)5139eeaa8a9SJeffrey Hsu so_pru_shutdown(struct socket *so)
5149eeaa8a9SJeffrey Hsu {
5159eeaa8a9SJeffrey Hsu 	struct netmsg_pru_shutdown msg;
516002c1265SMatthew Dillon 	int error;
5179eeaa8a9SJeffrey Hsu 
518002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
519002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_shutdown);
520002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
5219eeaa8a9SJeffrey Hsu 	return (error);
5229eeaa8a9SJeffrey Hsu }
5239eeaa8a9SJeffrey Hsu 
5249eeaa8a9SJeffrey Hsu int
so_pru_sockaddr(struct socket * so,struct sockaddr ** nam)5259eeaa8a9SJeffrey Hsu so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
5269eeaa8a9SJeffrey Hsu {
5279eeaa8a9SJeffrey Hsu 	struct netmsg_pru_sockaddr msg;
528002c1265SMatthew Dillon 	int error;
5299eeaa8a9SJeffrey Hsu 
530002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
531002c1265SMatthew Dillon 		    0, so->so_proto->pr_usrreqs->pru_sockaddr);
5329eeaa8a9SJeffrey Hsu 	msg.nm_nam = nam;
533002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
5349eeaa8a9SJeffrey Hsu 	return (error);
5359eeaa8a9SJeffrey Hsu }
5369eeaa8a9SJeffrey Hsu 
5379eeaa8a9SJeffrey Hsu int
so_pr_ctloutput(struct socket * so,struct sockopt * sopt)538002c1265SMatthew Dillon so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
5399eeaa8a9SJeffrey Hsu {
540002c1265SMatthew Dillon 	struct netmsg_pr_ctloutput msg;
5419eeaa8a9SJeffrey Hsu 	int error;
5429eeaa8a9SJeffrey Hsu 
543792239dfSAggelos Economopoulos 	KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
544680c4dd3SSepherosa Ziehau 
545680c4dd3SSepherosa Ziehau 	if (sopt->sopt_dir == SOPT_SET && so->so_proto->pr_ctloutmsg != NULL) {
546680c4dd3SSepherosa Ziehau 		struct netmsg_pr_ctloutput *amsg;
547680c4dd3SSepherosa Ziehau 
548680c4dd3SSepherosa Ziehau 		/* Fast path: asynchronous pr_ctloutput */
549680c4dd3SSepherosa Ziehau 		amsg = so->so_proto->pr_ctloutmsg(sopt);
550680c4dd3SSepherosa Ziehau 		if (amsg != NULL) {
551680c4dd3SSepherosa Ziehau 			netmsg_init(&amsg->base, so, &netisr_afree_rport, 0,
552680c4dd3SSepherosa Ziehau 			    so->so_proto->pr_ctloutput);
553680c4dd3SSepherosa Ziehau 			/* nm_flags and nm_sopt are setup by pr_ctloutmsg */
5540ebcb559SSepherosa Ziehau 			if (so->so_port == netisr_curport()) {
5550ebcb559SSepherosa Ziehau 				lwkt_sendmsg_oncpu(so->so_port,
5560ebcb559SSepherosa Ziehau 				    &amsg->base.lmsg);
5570ebcb559SSepherosa Ziehau 			} else {
558680c4dd3SSepherosa Ziehau 				lwkt_sendmsg(so->so_port, &amsg->base.lmsg);
5590ebcb559SSepherosa Ziehau 			}
560680c4dd3SSepherosa Ziehau 			return 0;
561680c4dd3SSepherosa Ziehau 		}
562680c4dd3SSepherosa Ziehau 		/* FALLTHROUGH */
563680c4dd3SSepherosa Ziehau 	}
564680c4dd3SSepherosa Ziehau 
565002c1265SMatthew Dillon 	netmsg_init(&msg.base, so, &curthread->td_msgport,
566002c1265SMatthew Dillon 		    0, so->so_proto->pr_ctloutput);
567680c4dd3SSepherosa Ziehau 	msg.nm_flags = 0;
5689eeaa8a9SJeffrey Hsu 	msg.nm_sopt = sopt;
569002c1265SMatthew Dillon 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
5709eeaa8a9SJeffrey Hsu 	return (error);
5719eeaa8a9SJeffrey Hsu }
5729eeaa8a9SJeffrey Hsu 
573130b7902SSepherosa Ziehau struct lwkt_port *
so_pr_ctlport(struct protosw * pr,int cmd,struct sockaddr * arg,void * extra,int * cpuid)574130b7902SSepherosa Ziehau so_pr_ctlport(struct protosw *pr, int cmd, struct sockaddr *arg,
575130b7902SSepherosa Ziehau     void *extra, int *cpuid)
576130b7902SSepherosa Ziehau {
577130b7902SSepherosa Ziehau 	if (pr->pr_ctlport == NULL)
578130b7902SSepherosa Ziehau 		return NULL;
579130b7902SSepherosa Ziehau 	KKASSERT(pr->pr_ctlinput != NULL);
580130b7902SSepherosa Ziehau 
581130b7902SSepherosa Ziehau 	return pr->pr_ctlport(cmd, arg, extra, cpuid);
582130b7902SSepherosa Ziehau }
583130b7902SSepherosa Ziehau 
5849eeaa8a9SJeffrey Hsu /*
585e3873585SSepherosa Ziehau  * Protocol control input, typically via icmp.
586e3873585SSepherosa Ziehau  *
587e3873585SSepherosa Ziehau  * If the protocol pr_ctlport is not NULL we call it to figure out the
588e3873585SSepherosa Ziehau  * protocol port.  If NULL is returned we can just return, otherwise
589e3873585SSepherosa Ziehau  * we issue a netmsg to call pr_ctlinput in the proper thread.
590e3873585SSepherosa Ziehau  *
591e3873585SSepherosa Ziehau  * This must be done synchronously as arg and/or extra may point to
592e3873585SSepherosa Ziehau  * temporary data.
593e3873585SSepherosa Ziehau  */
594e3873585SSepherosa Ziehau void
so_pr_ctlinput(struct protosw * pr,int cmd,struct sockaddr * arg,void * extra)5954255649eSSepherosa Ziehau so_pr_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra)
596e3873585SSepherosa Ziehau {
597b5cb4e31SSepherosa Ziehau 	struct netmsg_pr_ctlinput msg;
598e3873585SSepherosa Ziehau 	lwkt_port_t port;
599130b7902SSepherosa Ziehau 	int cpuid;
600e3873585SSepherosa Ziehau 
601130b7902SSepherosa Ziehau 	port = so_pr_ctlport(pr, cmd, arg, extra, &cpuid);
602e3873585SSepherosa Ziehau 	if (port == NULL)
603e3873585SSepherosa Ziehau 		return;
604002c1265SMatthew Dillon 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
605002c1265SMatthew Dillon 		    0, pr->pr_ctlinput);
606e3873585SSepherosa Ziehau 	msg.nm_cmd = cmd;
607130b7902SSepherosa Ziehau 	msg.nm_direct = 0;
608e3873585SSepherosa Ziehau 	msg.nm_arg = arg;
609e3873585SSepherosa Ziehau 	msg.nm_extra = extra;
610002c1265SMatthew Dillon 	lwkt_domsg(port, &msg.base.lmsg, 0);
611e3873585SSepherosa Ziehau }
612e3873585SSepherosa Ziehau 
613130b7902SSepherosa Ziehau void
so_pr_ctlinput_direct(struct protosw * pr,int cmd,struct sockaddr * arg,void * extra)614130b7902SSepherosa Ziehau so_pr_ctlinput_direct(struct protosw *pr, int cmd, struct sockaddr *arg,
615130b7902SSepherosa Ziehau     void *extra)
616130b7902SSepherosa Ziehau {
617130b7902SSepherosa Ziehau 	struct netmsg_pr_ctlinput msg;
618130b7902SSepherosa Ziehau 	netisr_fn_t func;
619130b7902SSepherosa Ziehau 	lwkt_port_t port;
620130b7902SSepherosa Ziehau 	int cpuid;
621130b7902SSepherosa Ziehau 
622130b7902SSepherosa Ziehau 	port = so_pr_ctlport(pr, cmd, arg, extra, &cpuid);
623130b7902SSepherosa Ziehau 	if (port == NULL)
624130b7902SSepherosa Ziehau 		return;
62543dbcc2aSSepherosa Ziehau 	if (cpuid != netisr_ncpus && cpuid != mycpuid)
626130b7902SSepherosa Ziehau 		return;
627130b7902SSepherosa Ziehau 
628130b7902SSepherosa Ziehau 	func = pr->pr_ctlinput;
629130b7902SSepherosa Ziehau 	netmsg_init(&msg.base, NULL, &netisr_adone_rport, 0, func);
630130b7902SSepherosa Ziehau 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
631130b7902SSepherosa Ziehau 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
632130b7902SSepherosa Ziehau 	msg.nm_cmd = cmd;
633130b7902SSepherosa Ziehau 	msg.nm_direct = 1;
634130b7902SSepherosa Ziehau 	msg.nm_arg = arg;
635130b7902SSepherosa Ziehau 	msg.nm_extra = extra;
636130b7902SSepherosa Ziehau 	func((netmsg_t)&msg);
637130b7902SSepherosa Ziehau 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
638130b7902SSepherosa Ziehau }
639130b7902SSepherosa Ziehau 
640e3873585SSepherosa Ziehau /*
641b44419cbSMatthew Dillon  * If we convert all the protosw pr_ functions for all the protocols
642b44419cbSMatthew Dillon  * to take a message directly, this layer can go away.  For the moment
643b44419cbSMatthew Dillon  * our dispatcher ignores the return value, but since we are handling
644b44419cbSMatthew Dillon  * the replymsg ourselves we return EASYNC by convention.
6459eeaa8a9SJeffrey Hsu  */
6464402d8a2SMatthew Dillon 
6474402d8a2SMatthew Dillon /*
648b44419cbSMatthew Dillon  * Handle a predicate event request.  This function is only called once
649b44419cbSMatthew Dillon  * when the predicate message queueing request is received.
6509eeaa8a9SJeffrey Hsu  */
6514599cf19SMatthew Dillon void
netmsg_so_notify(netmsg_t msg)652002c1265SMatthew Dillon netmsg_so_notify(netmsg_t msg)
6539eeaa8a9SJeffrey Hsu {
654c30db19bSSepherosa Ziehau 	struct socket *so = msg->base.nm_so;
6556d49aa6fSMatthew Dillon 	struct signalsockbuf *ssb;
656c1d0003cSJeffrey Hsu 
657c30db19bSSepherosa Ziehau 	ssb = (msg->notify.nm_etype & NM_REVENT) ? &so->so_rcv : &so->so_snd;
658c1d0003cSJeffrey Hsu 
659b44419cbSMatthew Dillon 	/*
660b44419cbSMatthew Dillon 	 * Reply immediately if the event has occured, otherwise queue the
661b44419cbSMatthew Dillon 	 * request.
662e2b148c6SMatthew Dillon 	 *
663e2b148c6SMatthew Dillon 	 * NOTE: Socket can change if this is an accept predicate so cache
664e2b148c6SMatthew Dillon 	 *	 the token.
665b44419cbSMatthew Dillon 	 */
666c30db19bSSepherosa Ziehau 	lwkt_getpooltoken(so);
667ccae972dSSepherosa Ziehau 	atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
668002c1265SMatthew Dillon 	if (msg->notify.nm_predicate(&msg->notify)) {
66920faa324SSepherosa Ziehau 		if (TAILQ_EMPTY(&ssb->ssb_mlist))
670ccae972dSSepherosa Ziehau 			atomic_clear_int(&ssb->ssb_flags, SSB_MEVENT);
671c30db19bSSepherosa Ziehau 		lwkt_relpooltoken(so);
672002c1265SMatthew Dillon 		lwkt_replymsg(&msg->base.lmsg,
673002c1265SMatthew Dillon 			      msg->base.lmsg.ms_error);
674b44419cbSMatthew Dillon 	} else {
67520faa324SSepherosa Ziehau 		TAILQ_INSERT_TAIL(&ssb->ssb_mlist, &msg->notify, nm_list);
676da633f87SSepherosa Ziehau 		/*
677da633f87SSepherosa Ziehau 		 * NOTE:
678da633f87SSepherosa Ziehau 		 * If predict ever blocks, 'tok' will be released, so
679da633f87SSepherosa Ziehau 		 * SSB_MEVENT set beforehand could have been cleared
680da633f87SSepherosa Ziehau 		 * when we reach here.  In case that happens, we set
681da633f87SSepherosa Ziehau 		 * SSB_MEVENT again, after the notify has been queued.
682da633f87SSepherosa Ziehau 		 */
683da633f87SSepherosa Ziehau 		atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
684c30db19bSSepherosa Ziehau 		lwkt_relpooltoken(so);
685c1d0003cSJeffrey Hsu 	}
6864599cf19SMatthew Dillon }
6874599cf19SMatthew Dillon 
6884599cf19SMatthew Dillon /*
6894599cf19SMatthew Dillon  * Called by doio when trying to abort a netmsg_so_notify message.
6904599cf19SMatthew Dillon  * Unlike the other functions this one is dispatched directly by
6914599cf19SMatthew Dillon  * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
692ebe43e15SMatthew Dillon  *
693ebe43e15SMatthew Dillon  * The original message, lmsg, is under the control of the caller and
694ebe43e15SMatthew Dillon  * will not be destroyed until we return so we can safely reference it
695ebe43e15SMatthew Dillon  * in our synchronous abort request.
696ebe43e15SMatthew Dillon  *
697ebe43e15SMatthew Dillon  * This part of the abort request occurs on the originating cpu which
698ebe43e15SMatthew Dillon  * means we may race the message flags and the original message may
699ebe43e15SMatthew Dillon  * not even have been processed by the target cpu yet.
7004599cf19SMatthew Dillon  */
7014599cf19SMatthew Dillon void
netmsg_so_notify_doabort(lwkt_msg_t lmsg)7024599cf19SMatthew Dillon netmsg_so_notify_doabort(lwkt_msg_t lmsg)
7034599cf19SMatthew Dillon {
7044599cf19SMatthew Dillon 	struct netmsg_so_notify_abort msg;
7054599cf19SMatthew Dillon 
706ebe43e15SMatthew Dillon 	if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
707d0d6a61fSSepherosa Ziehau 		const struct netmsg_base *nmsg =
708d0d6a61fSSepherosa Ziehau 		    (const struct netmsg_base *)lmsg;
709d0d6a61fSSepherosa Ziehau 
710d0d6a61fSSepherosa Ziehau 		netmsg_init(&msg.base, nmsg->nm_so, &curthread->td_msgport,
71148e7b118SMatthew Dillon 			    0, netmsg_so_notify_abort);
7124599cf19SMatthew Dillon 		msg.nm_notifymsg = (void *)lmsg;
713002c1265SMatthew Dillon 		lwkt_domsg(lmsg->ms_target_port, &msg.base.lmsg, 0);
7144599cf19SMatthew Dillon 	}
715b44419cbSMatthew Dillon }
716b44419cbSMatthew Dillon 
717b44419cbSMatthew Dillon /*
718b44419cbSMatthew Dillon  * Predicate requests can be aborted.  This function is only called once
719b44419cbSMatthew Dillon  * and will interlock against processing/reply races (since such races
720b44419cbSMatthew Dillon  * occur on the same thread that controls the port where the abort is
721b44419cbSMatthew Dillon  * requeued).
722ebe43e15SMatthew Dillon  *
723ebe43e15SMatthew Dillon  * This part of the abort request occurs on the target cpu.  The message
724ebe43e15SMatthew Dillon  * flags must be tested again in case the test that we did on the
725ebe43e15SMatthew Dillon  * originating cpu raced.  Since messages are handled in sequence, the
726ebe43e15SMatthew Dillon  * original message will have already been handled by the loop and either
727ebe43e15SMatthew Dillon  * replied to or queued.
728ebe43e15SMatthew Dillon  *
729ebe43e15SMatthew Dillon  * We really only need to interlock with MSGF_REPLY (a bit that is set on
730ebe43e15SMatthew Dillon  * our cpu when we reply).  Note that MSGF_DONE is not set until the
731ebe43e15SMatthew Dillon  * reply reaches the originating cpu.  Test both bits anyway.
732b44419cbSMatthew Dillon  */
7334599cf19SMatthew Dillon void
netmsg_so_notify_abort(netmsg_t msg)734002c1265SMatthew Dillon netmsg_so_notify_abort(netmsg_t msg)
735b44419cbSMatthew Dillon {
736002c1265SMatthew Dillon 	struct netmsg_so_notify_abort *abrtmsg = &msg->notify_abort;
737002c1265SMatthew Dillon 	struct netmsg_so_notify *nmsg = abrtmsg->nm_notifymsg;
7386d49aa6fSMatthew Dillon 	struct signalsockbuf *ssb;
739b44419cbSMatthew Dillon 
7404599cf19SMatthew Dillon 	/*
7414599cf19SMatthew Dillon 	 * The original notify message is not destroyed until after the
742ebe43e15SMatthew Dillon 	 * abort request is returned, so we can check its state.
7434599cf19SMatthew Dillon 	 */
744e2b148c6SMatthew Dillon 	lwkt_getpooltoken(nmsg->base.nm_so);
745002c1265SMatthew Dillon 	if ((nmsg->base.lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
746002c1265SMatthew Dillon 		ssb = (nmsg->nm_etype & NM_REVENT) ?
747002c1265SMatthew Dillon 				&nmsg->base.nm_so->so_rcv :
748002c1265SMatthew Dillon 				&nmsg->base.nm_so->so_snd;
74920faa324SSepherosa Ziehau 		TAILQ_REMOVE(&ssb->ssb_mlist, nmsg, nm_list);
750e2b148c6SMatthew Dillon 		lwkt_relpooltoken(nmsg->base.nm_so);
751002c1265SMatthew Dillon 		lwkt_replymsg(&nmsg->base.lmsg, EINTR);
752e2b148c6SMatthew Dillon 	} else {
753e2b148c6SMatthew Dillon 		lwkt_relpooltoken(nmsg->base.nm_so);
7544599cf19SMatthew Dillon 	}
7554599cf19SMatthew Dillon 
7564599cf19SMatthew Dillon 	/*
7574599cf19SMatthew Dillon 	 * Reply to the abort message
7584599cf19SMatthew Dillon 	 */
759002c1265SMatthew Dillon 	lwkt_replymsg(&abrtmsg->base.lmsg, 0);
760b44419cbSMatthew Dillon }
76196c6eb29SSepherosa Ziehau 
76296c6eb29SSepherosa Ziehau void
so_async_rcvd_reply(struct socket * so)76396c6eb29SSepherosa Ziehau so_async_rcvd_reply(struct socket *so)
76496c6eb29SSepherosa Ziehau {
7656999cd81SMatthew Dillon 	/*
7666999cd81SMatthew Dillon 	 * Spinlock safe, reply runs to degenerate lwkt_null_replyport()
7676999cd81SMatthew Dillon 	 */
76896c6eb29SSepherosa Ziehau 	spin_lock(&so->so_rcvd_spin);
76996c6eb29SSepherosa Ziehau 	lwkt_replymsg(&so->so_rcvd_msg.base.lmsg, 0);
77096c6eb29SSepherosa Ziehau 	spin_unlock(&so->so_rcvd_spin);
77196c6eb29SSepherosa Ziehau }
77296c6eb29SSepherosa Ziehau 
77396c6eb29SSepherosa Ziehau void
so_async_rcvd_drop(struct socket * so)77496c6eb29SSepherosa Ziehau so_async_rcvd_drop(struct socket *so)
77596c6eb29SSepherosa Ziehau {
77696c6eb29SSepherosa Ziehau 	lwkt_msg_t lmsg = &so->so_rcvd_msg.base.lmsg;
77796c6eb29SSepherosa Ziehau 
7786999cd81SMatthew Dillon 	/*
779f65f767cSSepherosa Ziehau 	 * Spinlock safe, drop runs to degenerate lwkt_spin_dropmsg()
7806999cd81SMatthew Dillon 	 */
78196c6eb29SSepherosa Ziehau 	spin_lock(&so->so_rcvd_spin);
782f65f767cSSepherosa Ziehau 	so->so_rcvd_msg.nm_pru_flags |= PRUR_DEAD;
783f65f767cSSepherosa Ziehau again:
784901b9bd6SSepherosa Ziehau 	lwkt_dropmsg(lmsg);
7856999cd81SMatthew Dillon 	if ((lmsg->ms_flags & MSGF_DONE) == 0) {
786f65f767cSSepherosa Ziehau 		++async_rcvd_drop_race;
787f65f767cSSepherosa Ziehau 		ssleep(so, &so->so_rcvd_spin, 0, "soadrop", 1);
788d4cbc551SSepherosa Ziehau 		goto again;
7896999cd81SMatthew Dillon 	}
790f65f767cSSepherosa Ziehau 	spin_unlock(&so->so_rcvd_spin);
79196c6eb29SSepherosa Ziehau }
792