xref: /dragonfly/sys/kern/uipc_msg.c (revision 4662ec71)
1 /*
2  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of The DragonFly Project nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific, prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/msgport.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/socketops.h>
42 #include <sys/thread.h>
43 #include <sys/thread2.h>
44 #include <sys/msgport2.h>
45 #include <sys/spinlock2.h>
46 #include <sys/sysctl.h>
47 #include <sys/mbuf.h>
48 #include <vm/pmap.h>
49 
50 #include <net/netmsg2.h>
51 #include <sys/socketvar2.h>
52 
53 #include <net/netisr.h>
54 #include <net/netmsg.h>
55 
56 static int async_rcvd_drop_race = 0;
57 SYSCTL_INT(_kern_ipc, OID_AUTO, async_rcvd_drop_race, CTLFLAG_RW,
58     &async_rcvd_drop_race, 0, "# of asynchronized pru_rcvd msg drop races");
59 
60 /*
61  * Abort a socket and free it, asynchronously.  Called from
62  * soabort_async() only.  soabort_async() got a ref on the
63  * socket which we must free on reply.
64  */
65 void
66 so_pru_abort_async(struct socket *so)
67 {
68 	struct netmsg_pru_abort *msg;
69 
70 	msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_ZERO);
71 	netmsg_init(&msg->base, so, &netisr_afree_free_so_rport,
72 		    0, so->so_proto->pr_usrreqs->pru_abort);
73 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
74 }
75 
76 /*
77  * Abort a socket and free it.  Called from soabort_direct() only.
78  * Caller must make sure that the current CPU is inpcb's owner CPU.
79  * soabort_direct() got a ref on the socket which we must free.
80  */
81 void
82 so_pru_abort_direct(struct socket *so)
83 {
84 	struct netmsg_pru_abort msg;
85 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_abort;
86 
87 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
88 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
89 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
90 	func((netmsg_t)&msg);
91 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
92 	sofree(msg.base.nm_so);
93 }
94 
95 int
96 so_pru_accept(struct socket *so, struct sockaddr **nam)
97 {
98 	struct netmsg_pru_accept msg;
99 
100 	netmsg_init(&msg.base, so, &curthread->td_msgport,
101 	    0, so->so_proto->pr_usrreqs->pru_accept);
102 	msg.nm_nam = nam;
103 
104 	return lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
105 }
106 
107 int
108 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
109 {
110 	struct netmsg_pru_attach msg;
111 	int error;
112 
113 	netmsg_init(&msg.base, so, &curthread->td_msgport,
114 		    0, so->so_proto->pr_usrreqs->pru_attach);
115 	msg.nm_proto = proto;
116 	msg.nm_ai = ai;
117 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
118 	return (error);
119 }
120 
121 int
122 so_pru_attach_direct(struct socket *so, int proto, struct pru_attach_info *ai)
123 {
124 	struct netmsg_pru_attach msg;
125 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_attach;
126 
127 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
128 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
129 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
130 	msg.nm_proto = proto;
131 	msg.nm_ai = ai;
132 	func((netmsg_t)&msg);
133 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
134 	return(msg.base.lmsg.ms_error);
135 }
136 
137 int
138 so_pru_attach_fast(struct socket *so, int proto, struct pru_attach_info *ai)
139 {
140 	struct netmsg_pru_attach *msg;
141 	int error;
142 
143 	error = so->so_proto->pr_usrreqs->pru_preattach(so, proto, ai);
144 	if (error)
145 		return error;
146 
147 	msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_NULLOK);
148 	if (msg == NULL) {
149 		/*
150 		 * Fail to allocate message; fallback to
151 		 * synchronized pru_attach.
152 		 */
153 		return so_pru_attach(so, proto, NULL /* postattach */);
154 	}
155 
156 	netmsg_init(&msg->base, so, &netisr_afree_rport, 0,
157 	    so->so_proto->pr_usrreqs->pru_attach);
158 	msg->nm_proto = proto;
159 	msg->nm_ai = NULL; /* postattach */
160 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
161 
162 	return 0;
163 }
164 
165 /*
166  * NOTE: If the target port changes the bind operation will deal with it.
167  */
168 int
169 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
170 {
171 	struct netmsg_pru_bind msg;
172 	int error;
173 
174 	netmsg_init(&msg.base, so, &curthread->td_msgport,
175 		    0, so->so_proto->pr_usrreqs->pru_bind);
176 	msg.nm_nam = nam;
177 	msg.nm_td = td;		/* used only for prison_ip() */
178 	msg.nm_flags = 0;
179 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
180 	return (error);
181 }
182 
183 int
184 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
185 {
186 	struct netmsg_pru_connect msg;
187 	int error;
188 
189 	netmsg_init(&msg.base, so, &curthread->td_msgport,
190 		    0, so->so_proto->pr_usrreqs->pru_connect);
191 	msg.nm_nam = nam;
192 	msg.nm_td = td;
193 	msg.nm_m = NULL;
194 	msg.nm_sndflags = 0;
195 	msg.nm_flags = 0;
196 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
197 	return (error);
198 }
199 
200 int
201 so_pru_connect_async(struct socket *so, struct sockaddr *nam, struct thread *td)
202 {
203 	struct netmsg_pru_connect *msg;
204 	int error, flags;
205 
206 	KASSERT(so->so_proto->pr_usrreqs->pru_preconnect != NULL,
207 	    ("async pru_connect is not supported"));
208 
209 	/* NOTE: sockaddr immediately follows netmsg */
210 	msg = kmalloc(sizeof(*msg) + nam->sa_len, M_LWKTMSG,
211 	    M_WAITOK | M_NULLOK);
212 	if (msg == NULL) {
213 		/*
214 		 * Fail to allocate message; fallback to
215 		 * synchronized pru_connect.
216 		 */
217 		return so_pru_connect(so, nam, td);
218 	}
219 
220 	error = so->so_proto->pr_usrreqs->pru_preconnect(so, nam, td);
221 	if (error) {
222 		kfree(msg, M_LWKTMSG);
223 		return error;
224 	}
225 
226 	flags = PRUC_ASYNC;
227 	if (td != NULL && (so->so_proto->pr_flags & PR_ACONN_HOLDTD)) {
228 		lwkt_hold(td);
229 		flags |= PRUC_HELDTD;
230 	}
231 
232 	netmsg_init(&msg->base, so, &netisr_afree_rport, 0,
233 	    so->so_proto->pr_usrreqs->pru_connect);
234 	msg->nm_nam = (struct sockaddr *)(msg + 1);
235 	memcpy(msg->nm_nam, nam, nam->sa_len);
236 	msg->nm_td = td;
237 	msg->nm_m = NULL;
238 	msg->nm_sndflags = 0;
239 	msg->nm_flags = flags;
240 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
241 	return 0;
242 }
243 
244 int
245 so_pru_connect2(struct socket *so1, struct socket *so2)
246 {
247 	struct netmsg_pru_connect2 msg;
248 	int error;
249 
250 	netmsg_init(&msg.base, so1, &curthread->td_msgport,
251 		    0, so1->so_proto->pr_usrreqs->pru_connect2);
252 	msg.nm_so1 = so1;
253 	msg.nm_so2 = so2;
254 	error = lwkt_domsg(so1->so_port, &msg.base.lmsg, 0);
255 	return (error);
256 }
257 
258 /*
259  * WARNING!  Synchronous call from user context.  Control function may do
260  *	     copyin/copyout.
261  */
262 int
263 so_pru_control_direct(struct socket *so, u_long cmd, caddr_t data,
264 		      struct ifnet *ifp)
265 {
266 	struct netmsg_pru_control msg;
267 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_control;
268 
269 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
270 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
271 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
272 	msg.nm_cmd = cmd;
273 	msg.nm_data = data;
274 	msg.nm_ifp = ifp;
275 	msg.nm_td = curthread;
276 	func((netmsg_t)&msg);
277 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
278 	return(msg.base.lmsg.ms_error);
279 }
280 
281 int
282 so_pru_detach(struct socket *so)
283 {
284 	struct netmsg_pru_detach msg;
285 	int error;
286 
287 	netmsg_init(&msg.base, so, &curthread->td_msgport,
288 		    0, so->so_proto->pr_usrreqs->pru_detach);
289 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
290 	return (error);
291 }
292 
293 int
294 so_pru_detach_direct(struct socket *so)
295 {
296 	struct netmsg_pru_detach msg;
297 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_detach;
298 
299 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
300 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
301 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
302 	func((netmsg_t)&msg);
303 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
304 	return(msg.base.lmsg.ms_error);
305 }
306 
307 int
308 so_pru_disconnect(struct socket *so)
309 {
310 	struct netmsg_pru_disconnect msg;
311 	int error;
312 
313 	netmsg_init(&msg.base, so, &curthread->td_msgport,
314 		    0, so->so_proto->pr_usrreqs->pru_disconnect);
315 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
316 	return (error);
317 }
318 
319 void
320 so_pru_disconnect_direct(struct socket *so)
321 {
322 	struct netmsg_pru_disconnect msg;
323 	netisr_fn_t func = so->so_proto->pr_usrreqs->pru_disconnect;
324 
325 	netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
326 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
327 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
328 	func((netmsg_t)&msg);
329 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
330 }
331 
332 int
333 so_pru_listen(struct socket *so, struct thread *td)
334 {
335 	struct netmsg_pru_listen msg;
336 	int error;
337 
338 	netmsg_init(&msg.base, so, &curthread->td_msgport,
339 		    0, so->so_proto->pr_usrreqs->pru_listen);
340 	msg.nm_td = td;		/* used only for prison_ip() XXX JH */
341 	msg.nm_flags = 0;
342 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
343 	return (error);
344 }
345 
346 int
347 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
348 {
349 	struct netmsg_pru_peeraddr msg;
350 	int error;
351 
352 	netmsg_init(&msg.base, so, &curthread->td_msgport,
353 		    0, so->so_proto->pr_usrreqs->pru_peeraddr);
354 	msg.nm_nam = nam;
355 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
356 	return (error);
357 }
358 
359 int
360 so_pru_rcvd(struct socket *so, int flags)
361 {
362 	struct netmsg_pru_rcvd msg;
363 	int error;
364 
365 	netmsg_init(&msg.base, so, &curthread->td_msgport,
366 		    0, so->so_proto->pr_usrreqs->pru_rcvd);
367 	msg.nm_flags = flags;
368 	msg.nm_pru_flags = 0;
369 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
370 	return (error);
371 }
372 
373 void
374 so_pru_rcvd_async(struct socket *so)
375 {
376 	lwkt_msg_t lmsg = &so->so_rcvd_msg.base.lmsg;
377 
378 	KASSERT(so->so_proto->pr_flags & PR_ASYNC_RCVD,
379 	    ("async pru_rcvd is not supported"));
380 
381 	/*
382 	 * WARNING!  Spinlock is a bit dodgy, use hacked up sendmsg
383 	 *	     to avoid deadlocking.
384 	 */
385 	spin_lock(&so->so_rcvd_spin);
386 	if ((so->so_rcvd_msg.nm_pru_flags & PRUR_DEAD) == 0) {
387 		if (lmsg->ms_flags & MSGF_DONE) {
388 			lwkt_sendmsg_prepare(so->so_port, lmsg);
389 			spin_unlock(&so->so_rcvd_spin);
390 			lwkt_sendmsg_start(so->so_port, lmsg);
391 		} else {
392 			spin_unlock(&so->so_rcvd_spin);
393 		}
394 	} else {
395 		spin_unlock(&so->so_rcvd_spin);
396 	}
397 }
398 
399 int
400 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
401 {
402 	struct netmsg_pru_rcvoob msg;
403 	int error;
404 
405 	netmsg_init(&msg.base, so, &curthread->td_msgport,
406 		    0, so->so_proto->pr_usrreqs->pru_rcvoob);
407 	msg.nm_m = m;
408 	msg.nm_flags = flags;
409 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
410 	return (error);
411 }
412 
413 /*
414  * NOTE: If the target port changes the implied connect will deal with it.
415  */
416 int
417 so_pru_send(struct socket *so, int flags, struct mbuf *m,
418 	    struct sockaddr *addr, struct mbuf *control, struct thread *td)
419 {
420 	struct netmsg_pru_send msg;
421 	int error;
422 
423 	netmsg_init(&msg.base, so, &curthread->td_msgport,
424 		    0, so->so_proto->pr_usrreqs->pru_send);
425 	msg.nm_flags = flags;
426 	msg.nm_m = m;
427 	msg.nm_addr = addr;
428 	msg.nm_control = control;
429 	msg.nm_td = td;
430 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
431 	return (error);
432 }
433 
434 void
435 so_pru_sync(struct socket *so)
436 {
437 	struct netmsg_base msg;
438 
439 	netmsg_init(&msg, so, &curthread->td_msgport, 0,
440 	    netmsg_sync_handler);
441 	lwkt_domsg(so->so_port, &msg.lmsg, 0);
442 }
443 
444 void
445 so_pru_send_async(struct socket *so, int flags, struct mbuf *m,
446     struct sockaddr *addr0, struct mbuf *control, struct thread *td)
447 {
448 	struct netmsg_pru_send *msg;
449 	struct sockaddr *addr = NULL;
450 
451 	KASSERT(so->so_proto->pr_flags & PR_ASYNC_SEND,
452 	    ("async pru_send is not supported"));
453 
454 	if (addr0 != NULL) {
455 		addr = kmalloc(addr0->sa_len, M_SONAME, M_WAITOK | M_NULLOK);
456 		if (addr == NULL) {
457 			/*
458 			 * Fail to allocate address; fallback to
459 			 * synchronized pru_send.
460 			 */
461 			so_pru_send(so, flags, m, addr0, control, td);
462 			return;
463 		}
464 		memcpy(addr, addr0, addr0->sa_len);
465 		flags |= PRUS_FREEADDR;
466 	}
467 	flags |= PRUS_NOREPLY;
468 
469 	if (td != NULL && (so->so_proto->pr_flags & PR_ASEND_HOLDTD)) {
470 		lwkt_hold(td);
471 		flags |= PRUS_HELDTD;
472 	}
473 
474 	msg = &m->m_hdr.mh_sndmsg;
475 	netmsg_init(&msg->base, so, &netisr_apanic_rport,
476 		    0, so->so_proto->pr_usrreqs->pru_send);
477 	msg->nm_flags = flags;
478 	msg->nm_m = m;
479 	msg->nm_addr = addr;
480 	msg->nm_control = control;
481 	msg->nm_td = td;
482 	lwkt_sendmsg(so->so_port, &msg->base.lmsg);
483 }
484 
485 int
486 so_pru_sense(struct socket *so, struct stat *sb)
487 {
488 	struct netmsg_pru_sense msg;
489 	int error;
490 
491 	netmsg_init(&msg.base, so, &curthread->td_msgport,
492 		    0, so->so_proto->pr_usrreqs->pru_sense);
493 	msg.nm_stat = sb;
494 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
495 	return (error);
496 }
497 
498 int
499 so_pru_shutdown(struct socket *so)
500 {
501 	struct netmsg_pru_shutdown msg;
502 	int error;
503 
504 	netmsg_init(&msg.base, so, &curthread->td_msgport,
505 		    0, so->so_proto->pr_usrreqs->pru_shutdown);
506 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
507 	return (error);
508 }
509 
510 int
511 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
512 {
513 	struct netmsg_pru_sockaddr msg;
514 	int error;
515 
516 	netmsg_init(&msg.base, so, &curthread->td_msgport,
517 		    0, so->so_proto->pr_usrreqs->pru_sockaddr);
518 	msg.nm_nam = nam;
519 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
520 	return (error);
521 }
522 
523 int
524 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
525 {
526 	struct netmsg_pr_ctloutput msg;
527 	int error;
528 
529 	KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
530 	netmsg_init(&msg.base, so, &curthread->td_msgport,
531 		    0, so->so_proto->pr_ctloutput);
532 	msg.nm_sopt = sopt;
533 	error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
534 	return (error);
535 }
536 
537 struct lwkt_port *
538 so_pr_ctlport(struct protosw *pr, int cmd, struct sockaddr *arg,
539     void *extra, int *cpuid)
540 {
541 	if (pr->pr_ctlport == NULL)
542 		return NULL;
543 	KKASSERT(pr->pr_ctlinput != NULL);
544 
545 	return pr->pr_ctlport(cmd, arg, extra, cpuid);
546 }
547 
548 /*
549  * Protocol control input, typically via icmp.
550  *
551  * If the protocol pr_ctlport is not NULL we call it to figure out the
552  * protocol port.  If NULL is returned we can just return, otherwise
553  * we issue a netmsg to call pr_ctlinput in the proper thread.
554  *
555  * This must be done synchronously as arg and/or extra may point to
556  * temporary data.
557  */
558 void
559 so_pr_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra)
560 {
561 	struct netmsg_pr_ctlinput msg;
562 	lwkt_port_t port;
563 	int cpuid;
564 
565 	port = so_pr_ctlport(pr, cmd, arg, extra, &cpuid);
566 	if (port == NULL)
567 		return;
568 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
569 		    0, pr->pr_ctlinput);
570 	msg.nm_cmd = cmd;
571 	msg.nm_direct = 0;
572 	msg.nm_arg = arg;
573 	msg.nm_extra = extra;
574 	lwkt_domsg(port, &msg.base.lmsg, 0);
575 }
576 
577 void
578 so_pr_ctlinput_direct(struct protosw *pr, int cmd, struct sockaddr *arg,
579     void *extra)
580 {
581 	struct netmsg_pr_ctlinput msg;
582 	netisr_fn_t func;
583 	lwkt_port_t port;
584 	int cpuid;
585 
586 	port = so_pr_ctlport(pr, cmd, arg, extra, &cpuid);
587 	if (port == NULL)
588 		return;
589 	if (cpuid != ncpus && cpuid != mycpuid)
590 		return;
591 
592 	func = pr->pr_ctlinput;
593 	netmsg_init(&msg.base, NULL, &netisr_adone_rport, 0, func);
594 	msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
595 	msg.base.lmsg.ms_flags |= MSGF_SYNC;
596 	msg.nm_cmd = cmd;
597 	msg.nm_direct = 1;
598 	msg.nm_arg = arg;
599 	msg.nm_extra = extra;
600 	func((netmsg_t)&msg);
601 	KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
602 }
603 
604 /*
605  * If we convert all the protosw pr_ functions for all the protocols
606  * to take a message directly, this layer can go away.  For the moment
607  * our dispatcher ignores the return value, but since we are handling
608  * the replymsg ourselves we return EASYNC by convention.
609  */
610 
611 /*
612  * Handle a predicate event request.  This function is only called once
613  * when the predicate message queueing request is received.
614  */
615 void
616 netmsg_so_notify(netmsg_t msg)
617 {
618 	struct socket *so = msg->base.nm_so;
619 	struct signalsockbuf *ssb;
620 
621 	ssb = (msg->notify.nm_etype & NM_REVENT) ? &so->so_rcv : &so->so_snd;
622 
623 	/*
624 	 * Reply immediately if the event has occured, otherwise queue the
625 	 * request.
626 	 *
627 	 * NOTE: Socket can change if this is an accept predicate so cache
628 	 *	 the token.
629 	 */
630 	lwkt_getpooltoken(so);
631 	atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
632 	if (msg->notify.nm_predicate(&msg->notify)) {
633 		if (TAILQ_EMPTY(&ssb->ssb_mlist))
634 			atomic_clear_int(&ssb->ssb_flags, SSB_MEVENT);
635 		lwkt_relpooltoken(so);
636 		lwkt_replymsg(&msg->base.lmsg,
637 			      msg->base.lmsg.ms_error);
638 	} else {
639 		TAILQ_INSERT_TAIL(&ssb->ssb_mlist, &msg->notify, nm_list);
640 		/*
641 		 * NOTE:
642 		 * If predict ever blocks, 'tok' will be released, so
643 		 * SSB_MEVENT set beforehand could have been cleared
644 		 * when we reach here.  In case that happens, we set
645 		 * SSB_MEVENT again, after the notify has been queued.
646 		 */
647 		atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
648 		lwkt_relpooltoken(so);
649 	}
650 }
651 
652 /*
653  * Called by doio when trying to abort a netmsg_so_notify message.
654  * Unlike the other functions this one is dispatched directly by
655  * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
656  *
657  * The original message, lmsg, is under the control of the caller and
658  * will not be destroyed until we return so we can safely reference it
659  * in our synchronous abort request.
660  *
661  * This part of the abort request occurs on the originating cpu which
662  * means we may race the message flags and the original message may
663  * not even have been processed by the target cpu yet.
664  */
665 void
666 netmsg_so_notify_doabort(lwkt_msg_t lmsg)
667 {
668 	struct netmsg_so_notify_abort msg;
669 
670 	if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
671 		const struct netmsg_base *nmsg =
672 		    (const struct netmsg_base *)lmsg;
673 
674 		netmsg_init(&msg.base, nmsg->nm_so, &curthread->td_msgport,
675 			    0, netmsg_so_notify_abort);
676 		msg.nm_notifymsg = (void *)lmsg;
677 		lwkt_domsg(lmsg->ms_target_port, &msg.base.lmsg, 0);
678 	}
679 }
680 
681 /*
682  * Predicate requests can be aborted.  This function is only called once
683  * and will interlock against processing/reply races (since such races
684  * occur on the same thread that controls the port where the abort is
685  * requeued).
686  *
687  * This part of the abort request occurs on the target cpu.  The message
688  * flags must be tested again in case the test that we did on the
689  * originating cpu raced.  Since messages are handled in sequence, the
690  * original message will have already been handled by the loop and either
691  * replied to or queued.
692  *
693  * We really only need to interlock with MSGF_REPLY (a bit that is set on
694  * our cpu when we reply).  Note that MSGF_DONE is not set until the
695  * reply reaches the originating cpu.  Test both bits anyway.
696  */
697 void
698 netmsg_so_notify_abort(netmsg_t msg)
699 {
700 	struct netmsg_so_notify_abort *abrtmsg = &msg->notify_abort;
701 	struct netmsg_so_notify *nmsg = abrtmsg->nm_notifymsg;
702 	struct signalsockbuf *ssb;
703 
704 	/*
705 	 * The original notify message is not destroyed until after the
706 	 * abort request is returned, so we can check its state.
707 	 */
708 	lwkt_getpooltoken(nmsg->base.nm_so);
709 	if ((nmsg->base.lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
710 		ssb = (nmsg->nm_etype & NM_REVENT) ?
711 				&nmsg->base.nm_so->so_rcv :
712 				&nmsg->base.nm_so->so_snd;
713 		TAILQ_REMOVE(&ssb->ssb_mlist, nmsg, nm_list);
714 		lwkt_relpooltoken(nmsg->base.nm_so);
715 		lwkt_replymsg(&nmsg->base.lmsg, EINTR);
716 	} else {
717 		lwkt_relpooltoken(nmsg->base.nm_so);
718 	}
719 
720 	/*
721 	 * Reply to the abort message
722 	 */
723 	lwkt_replymsg(&abrtmsg->base.lmsg, 0);
724 }
725 
726 void
727 so_async_rcvd_reply(struct socket *so)
728 {
729 	/*
730 	 * Spinlock safe, reply runs to degenerate lwkt_null_replyport()
731 	 */
732 	spin_lock(&so->so_rcvd_spin);
733 	lwkt_replymsg(&so->so_rcvd_msg.base.lmsg, 0);
734 	spin_unlock(&so->so_rcvd_spin);
735 }
736 
737 void
738 so_async_rcvd_drop(struct socket *so)
739 {
740 	lwkt_msg_t lmsg = &so->so_rcvd_msg.base.lmsg;
741 
742 	/*
743 	 * Spinlock safe, drop runs to degenerate lwkt_spin_dropmsg()
744 	 */
745 	spin_lock(&so->so_rcvd_spin);
746 	so->so_rcvd_msg.nm_pru_flags |= PRUR_DEAD;
747 again:
748 	lwkt_dropmsg(lmsg);
749 	if ((lmsg->ms_flags & MSGF_DONE) == 0) {
750 		++async_rcvd_drop_race;
751 		ssleep(so, &so->so_rcvd_spin, 0, "soadrop", 1);
752 		goto again;
753 	}
754 	spin_unlock(&so->so_rcvd_spin);
755 }
756