2 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2003, 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 * $DragonFly: src/sys/kern/uipc_msg.c,v 1.20 2007/12/19 11:00:22 sephe Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/msgport.h>
39 #include <sys/protosw.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/socketops.h>
43 #include <sys/thread.h>
44 #include <sys/thread2.h>
45 #include <sys/msgport2.h>
46 #include <net/netmsg2.h>
48 #include <net/netisr.h>
49 #include <net/netmsg.h>
52 so_pru_abort(struct socket
*so
)
55 struct netmsg_pru_abort msg
;
58 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_ABORT
);
59 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
61 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_abort
;
63 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
68 so_pru_accept(struct socket
*so
, struct sockaddr
**nam
)
70 /* Block (memory allocation) in process context. XXX JH */
71 return ((*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
));
75 struct netmsg_pru_accept msg
;
78 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_ACCEPT
);
79 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
81 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_accept
;
84 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
90 so_pru_attach(struct socket
*so
, int proto
, struct pru_attach_info
*ai
)
93 struct netmsg_pru_attach msg
;
96 port
= so
->so_proto
->pr_mport(NULL
, NULL
, NULL
, PRU_ATTACH
);
97 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
99 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_attach
;
101 msg
.nm_proto
= proto
;
103 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
108 so_pru_bind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
111 struct netmsg_pru_bind msg
;
114 /* Send mesg to thread for new address. */
115 port
= so
->so_proto
->pr_mport(NULL
, nam
, NULL
, PRU_BIND
);
116 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
118 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_bind
;
121 msg
.nm_td
= td
; /* used only for prison_ip() XXX JH */
122 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
127 so_pru_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
130 struct netmsg_pru_connect msg
;
133 port
= so
->so_proto
->pr_mport(so
, nam
, NULL
, PRU_CONNECT
);
134 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
136 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_connect
;
140 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
145 so_pru_connect2(struct socket
*so1
, struct socket
*so2
)
148 struct netmsg_pru_connect2 msg
;
151 port
= so1
->so_proto
->pr_mport(so1
, NULL
, NULL
, PRU_CONNECT2
);
152 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
153 netmsg_pru_connect2
);
154 msg
.nm_prufn
= so1
->so_proto
->pr_usrreqs
->pru_connect2
;
157 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
162 so_pru_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
164 return ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, ifp
,
166 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
168 struct netmsg_pru_control msg
;
171 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_CONTROL
);
172 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
174 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_control
;
180 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
186 so_pru_detach(struct socket
*so
)
189 struct netmsg_pru_detach msg
;
192 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_DETACH
);
193 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
195 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_detach
;
197 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
202 so_pru_disconnect(struct socket
*so
)
205 struct netmsg_pru_disconnect msg
;
208 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_DISCONNECT
);
209 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
210 netmsg_pru_disconnect
);
211 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_disconnect
;
213 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
218 so_pru_listen(struct socket
*so
, struct thread
*td
)
221 struct netmsg_pru_listen msg
;
224 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_LISTEN
);
225 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
227 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_listen
;
229 msg
.nm_td
= td
; /* used only for prison_ip() XXX JH */
230 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
235 so_pru_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
238 struct netmsg_pru_peeraddr msg
;
241 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_PEERADDR
);
242 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
243 netmsg_pru_peeraddr
);
244 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_peeraddr
;
247 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
252 so_pru_rcvd(struct socket
*so
, int flags
)
255 struct netmsg_pru_rcvd msg
;
258 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_RCVD
);
259 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
261 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_rcvd
;
263 msg
.nm_flags
= flags
;
264 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
269 so_pru_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
272 struct netmsg_pru_rcvoob msg
;
275 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_RCVOOB
);
276 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
278 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_rcvoob
;
281 msg
.nm_flags
= flags
;
282 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
287 so_pru_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
288 struct mbuf
*control
, struct thread
*td
)
291 struct netmsg_pru_send msg
;
294 port
= so
->so_proto
->pr_mport(so
, addr
, &m
, PRU_SEND
);
300 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
302 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_send
;
304 msg
.nm_flags
= flags
;
307 msg
.nm_control
= control
;
309 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
314 so_pru_sense(struct socket
*so
, struct stat
*sb
)
317 struct netmsg_pru_sense msg
;
320 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SENSE
);
321 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
323 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sense
;
326 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
331 so_pru_shutdown(struct socket
*so
)
334 struct netmsg_pru_shutdown msg
;
337 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SHUTDOWN
);
338 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
339 netmsg_pru_shutdown
);
340 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_shutdown
;
342 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
347 so_pru_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
350 struct netmsg_pru_sockaddr msg
;
353 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SOCKADDR
);
354 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
355 netmsg_pru_sockaddr
);
356 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sockaddr
;
359 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
364 so_pru_sopoll(struct socket
*so
, int events
, struct ucred
*cred
)
367 struct netmsg_pru_sopoll msg
;
370 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SOPOLL
);
371 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
373 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sopoll
;
375 msg
.nm_events
= events
;
377 msg
.nm_td
= curthread
;
378 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
383 so_pr_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
385 return ((*so
->so_proto
->pr_ctloutput
)(so
, sopt
));
386 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
387 struct netmsg_pr_ctloutput msg
;
391 port
= so
->so_proto
->pr_mport(so
, NULL
);
392 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
393 netmsg_pru_ctloutput
);
394 msg
.nm_prfn
= so
->so_proto
->pr_ctloutput
;
397 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
403 * If we convert all the protosw pr_ functions for all the protocols
404 * to take a message directly, this layer can go away. For the moment
405 * our dispatcher ignores the return value, but since we are handling
406 * the replymsg ourselves we return EASYNC by convention.
409 netmsg_pru_abort(netmsg_t msg
)
411 struct netmsg_pru_abort
*nm
= (void *)msg
;
413 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
418 netmsg_pru_accept(netmsg_t msg
)
420 struct netmsg_pru_accept
*nm
= (void *)msg
;
422 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
427 netmsg_pru_attach(netmsg_t msg
)
429 struct netmsg_pru_attach
*nm
= (void *)msg
;
431 lwkt_replymsg(&msg
->nm_lmsg
,
432 nm
->nm_prufn(nm
->nm_so
, nm
->nm_proto
, nm
->nm_ai
));
436 netmsg_pru_bind(netmsg_t msg
)
438 struct netmsg_pru_bind
*nm
= (void *)msg
;
440 lwkt_replymsg(&msg
->nm_lmsg
,
441 nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
, nm
->nm_td
));
445 netmsg_pru_connect(netmsg_t msg
)
447 struct netmsg_pru_connect
*nm
= (void *)msg
;
449 lwkt_replymsg(&msg
->nm_lmsg
,
450 nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
, nm
->nm_td
));
454 netmsg_pru_connect2(netmsg_t msg
)
456 struct netmsg_pru_connect2
*nm
= (void *)msg
;
458 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so1
, nm
->nm_so2
));
462 netmsg_pru_control(netmsg_t msg
)
464 struct netmsg_pru_control
*nm
= (void *)msg
;
467 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_cmd
, nm
->nm_data
,
468 nm
->nm_ifp
, nm
->nm_td
);
469 lwkt_replymsg(&msg
->nm_lmsg
, error
);
473 netmsg_pru_detach(netmsg_t msg
)
475 struct netmsg_pru_detach
*nm
= (void *)msg
;
477 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
481 netmsg_pru_disconnect(netmsg_t msg
)
483 struct netmsg_pru_disconnect
*nm
= (void *)msg
;
485 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
489 netmsg_pru_listen(netmsg_t msg
)
491 struct netmsg_pru_listen
*nm
= (void *)msg
;
493 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_td
));
497 netmsg_pru_peeraddr(netmsg_t msg
)
499 struct netmsg_pru_peeraddr
*nm
= (void *)msg
;
501 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
505 netmsg_pru_rcvd(netmsg_t msg
)
507 struct netmsg_pru_rcvd
*nm
= (void *)msg
;
509 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_flags
));
513 netmsg_pru_rcvoob(netmsg_t msg
)
515 struct netmsg_pru_rcvoob
*nm
= (void *)msg
;
517 lwkt_replymsg(&msg
->nm_lmsg
,
518 nm
->nm_prufn(nm
->nm_so
, nm
->nm_m
, nm
->nm_flags
));
522 netmsg_pru_send(netmsg_t msg
)
524 struct netmsg_pru_send
*nm
= (void *)msg
;
527 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_flags
, nm
->nm_m
,
528 nm
->nm_addr
, nm
->nm_control
, nm
->nm_td
);
529 lwkt_replymsg(&msg
->nm_lmsg
, error
);
533 netmsg_pru_sense(netmsg_t msg
)
535 struct netmsg_pru_sense
*nm
= (void *)msg
;
537 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_stat
));
541 netmsg_pru_shutdown(netmsg_t msg
)
543 struct netmsg_pru_shutdown
*nm
= (void *)msg
;
545 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
549 netmsg_pru_sockaddr(netmsg_t msg
)
551 struct netmsg_pru_sockaddr
*nm
= (void *)msg
;
553 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
557 netmsg_pru_sopoll(netmsg_t msg
)
559 struct netmsg_pru_sopoll
*nm
= (void *)msg
;
562 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_events
, nm
->nm_cred
, nm
->nm_td
);
563 lwkt_replymsg(&msg
->nm_lmsg
, error
);
567 netmsg_pr_ctloutput(netmsg_t msg
)
569 struct netmsg_pr_ctloutput
*nm
= (void *)msg
;
571 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prfn(nm
->nm_so
, nm
->nm_sopt
));
575 netmsg_pr_timeout(netmsg_t msg
)
577 struct netmsg_pr_timeout
*nm
= (void *)msg
;
579 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prfn());
583 * Handle a predicate event request. This function is only called once
584 * when the predicate message queueing request is received.
587 netmsg_so_notify(netmsg_t netmsg
)
589 struct netmsg_so_notify
*msg
= (void *)netmsg
;
590 struct signalsockbuf
*ssb
;
592 ssb
= (msg
->nm_etype
& NM_REVENT
) ?
593 &msg
->nm_so
->so_rcv
:
597 * Reply immediately if the event has occured, otherwise queue the
600 if (msg
->nm_predicate(&msg
->nm_netmsg
)) {
601 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
,
602 msg
->nm_netmsg
.nm_lmsg
.ms_error
);
604 TAILQ_INSERT_TAIL(&ssb
->ssb_sel
.si_mlist
, msg
, nm_list
);
605 ssb
->ssb_flags
|= SSB_MEVENT
;
610 * Called by doio when trying to abort a netmsg_so_notify message.
611 * Unlike the other functions this one is dispatched directly by
612 * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
614 * The original message, lmsg, is under the control of the caller and
615 * will not be destroyed until we return so we can safely reference it
616 * in our synchronous abort request.
618 * This part of the abort request occurs on the originating cpu which
619 * means we may race the message flags and the original message may
620 * not even have been processed by the target cpu yet.
623 netmsg_so_notify_doabort(lwkt_msg_t lmsg
)
625 struct netmsg_so_notify_abort msg
;
627 if ((lmsg
->ms_flags
& (MSGF_DONE
| MSGF_REPLY
)) == 0) {
628 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
629 netmsg_so_notify_abort
);
630 msg
.nm_notifymsg
= (void *)lmsg
;
631 lwkt_domsg(lmsg
->ms_target_port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
636 * Predicate requests can be aborted. This function is only called once
637 * and will interlock against processing/reply races (since such races
638 * occur on the same thread that controls the port where the abort is
641 * This part of the abort request occurs on the target cpu. The message
642 * flags must be tested again in case the test that we did on the
643 * originating cpu raced. Since messages are handled in sequence, the
644 * original message will have already been handled by the loop and either
645 * replied to or queued.
647 * We really only need to interlock with MSGF_REPLY (a bit that is set on
648 * our cpu when we reply). Note that MSGF_DONE is not set until the
649 * reply reaches the originating cpu. Test both bits anyway.
652 netmsg_so_notify_abort(netmsg_t netmsg
)
654 struct netmsg_so_notify_abort
*abrtmsg
= (void *)netmsg
;
655 struct netmsg_so_notify
*msg
= abrtmsg
->nm_notifymsg
;
656 struct signalsockbuf
*ssb
;
659 * The original notify message is not destroyed until after the
660 * abort request is returned, so we can check its state.
662 if ((msg
->nm_netmsg
.nm_lmsg
.ms_flags
& (MSGF_DONE
| MSGF_REPLY
)) == 0) {
663 ssb
= (msg
->nm_etype
& NM_REVENT
) ?
664 &msg
->nm_so
->so_rcv
:
666 TAILQ_REMOVE(&ssb
->ssb_sel
.si_mlist
, msg
, nm_list
);
667 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
, EINTR
);
671 * Reply to the abort message
673 lwkt_replymsg(&abrtmsg
->nm_netmsg
.nm_lmsg
, 0);