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.19 2007/07/04 23:36:26 dillon 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
, 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
, 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
, 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
, 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
, 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
, 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
, 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
, 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
, 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
, 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
, 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
, 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
, 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
, NULL
, PRU_SEND
);
295 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
297 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_send
;
299 msg
.nm_flags
= flags
;
302 msg
.nm_control
= control
;
304 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
309 so_pru_sense(struct socket
*so
, struct stat
*sb
)
312 struct netmsg_pru_sense msg
;
315 port
= so
->so_proto
->pr_mport(so
, NULL
, PRU_SENSE
);
316 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
318 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sense
;
321 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
326 so_pru_shutdown(struct socket
*so
)
329 struct netmsg_pru_shutdown msg
;
332 port
= so
->so_proto
->pr_mport(so
, NULL
, PRU_SHUTDOWN
);
333 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
334 netmsg_pru_shutdown
);
335 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_shutdown
;
337 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
342 so_pru_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
345 struct netmsg_pru_sockaddr msg
;
348 port
= so
->so_proto
->pr_mport(so
, NULL
, PRU_SOCKADDR
);
349 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
350 netmsg_pru_sockaddr
);
351 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sockaddr
;
354 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
359 so_pru_sopoll(struct socket
*so
, int events
, struct ucred
*cred
)
362 struct netmsg_pru_sopoll msg
;
365 port
= so
->so_proto
->pr_mport(so
, NULL
, PRU_SOPOLL
);
366 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
368 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sopoll
;
370 msg
.nm_events
= events
;
372 msg
.nm_td
= curthread
;
373 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
378 so_pr_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
380 return ((*so
->so_proto
->pr_ctloutput
)(so
, sopt
));
381 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
382 struct netmsg_pr_ctloutput msg
;
386 port
= so
->so_proto
->pr_mport(so
, NULL
);
387 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
388 netmsg_pru_ctloutput
);
389 msg
.nm_prfn
= so
->so_proto
->pr_ctloutput
;
392 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
398 * If we convert all the protosw pr_ functions for all the protocols
399 * to take a message directly, this layer can go away. For the moment
400 * our dispatcher ignores the return value, but since we are handling
401 * the replymsg ourselves we return EASYNC by convention.
404 netmsg_pru_abort(netmsg_t msg
)
406 struct netmsg_pru_abort
*nm
= (void *)msg
;
408 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
413 netmsg_pru_accept(netmsg_t msg
)
415 struct netmsg_pru_accept
*nm
= (void *)msg
;
417 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
422 netmsg_pru_attach(netmsg_t msg
)
424 struct netmsg_pru_attach
*nm
= (void *)msg
;
426 lwkt_replymsg(&msg
->nm_lmsg
,
427 nm
->nm_prufn(nm
->nm_so
, nm
->nm_proto
, nm
->nm_ai
));
431 netmsg_pru_bind(netmsg_t msg
)
433 struct netmsg_pru_bind
*nm
= (void *)msg
;
435 lwkt_replymsg(&msg
->nm_lmsg
,
436 nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
, nm
->nm_td
));
440 netmsg_pru_connect(netmsg_t msg
)
442 struct netmsg_pru_connect
*nm
= (void *)msg
;
444 lwkt_replymsg(&msg
->nm_lmsg
,
445 nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
, nm
->nm_td
));
449 netmsg_pru_connect2(netmsg_t msg
)
451 struct netmsg_pru_connect2
*nm
= (void *)msg
;
453 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so1
, nm
->nm_so2
));
457 netmsg_pru_control(netmsg_t msg
)
459 struct netmsg_pru_control
*nm
= (void *)msg
;
462 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_cmd
, nm
->nm_data
,
463 nm
->nm_ifp
, nm
->nm_td
);
464 lwkt_replymsg(&msg
->nm_lmsg
, error
);
468 netmsg_pru_detach(netmsg_t msg
)
470 struct netmsg_pru_detach
*nm
= (void *)msg
;
472 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
476 netmsg_pru_disconnect(netmsg_t msg
)
478 struct netmsg_pru_disconnect
*nm
= (void *)msg
;
480 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
484 netmsg_pru_listen(netmsg_t msg
)
486 struct netmsg_pru_listen
*nm
= (void *)msg
;
488 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_td
));
492 netmsg_pru_peeraddr(netmsg_t msg
)
494 struct netmsg_pru_peeraddr
*nm
= (void *)msg
;
496 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
500 netmsg_pru_rcvd(netmsg_t msg
)
502 struct netmsg_pru_rcvd
*nm
= (void *)msg
;
504 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_flags
));
508 netmsg_pru_rcvoob(netmsg_t msg
)
510 struct netmsg_pru_rcvoob
*nm
= (void *)msg
;
512 lwkt_replymsg(&msg
->nm_lmsg
,
513 nm
->nm_prufn(nm
->nm_so
, nm
->nm_m
, nm
->nm_flags
));
517 netmsg_pru_send(netmsg_t msg
)
519 struct netmsg_pru_send
*nm
= (void *)msg
;
522 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_flags
, nm
->nm_m
,
523 nm
->nm_addr
, nm
->nm_control
, nm
->nm_td
);
524 lwkt_replymsg(&msg
->nm_lmsg
, error
);
528 netmsg_pru_sense(netmsg_t msg
)
530 struct netmsg_pru_sense
*nm
= (void *)msg
;
532 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_stat
));
536 netmsg_pru_shutdown(netmsg_t msg
)
538 struct netmsg_pru_shutdown
*nm
= (void *)msg
;
540 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
544 netmsg_pru_sockaddr(netmsg_t msg
)
546 struct netmsg_pru_sockaddr
*nm
= (void *)msg
;
548 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
552 netmsg_pru_sopoll(netmsg_t msg
)
554 struct netmsg_pru_sopoll
*nm
= (void *)msg
;
557 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_events
, nm
->nm_cred
, nm
->nm_td
);
558 lwkt_replymsg(&msg
->nm_lmsg
, error
);
562 netmsg_pr_ctloutput(netmsg_t msg
)
564 struct netmsg_pr_ctloutput
*nm
= (void *)msg
;
566 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prfn(nm
->nm_so
, nm
->nm_sopt
));
570 netmsg_pr_timeout(netmsg_t msg
)
572 struct netmsg_pr_timeout
*nm
= (void *)msg
;
574 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prfn());
578 * Handle a predicate event request. This function is only called once
579 * when the predicate message queueing request is received.
582 netmsg_so_notify(netmsg_t netmsg
)
584 struct netmsg_so_notify
*msg
= (void *)netmsg
;
585 struct signalsockbuf
*ssb
;
587 ssb
= (msg
->nm_etype
& NM_REVENT
) ?
588 &msg
->nm_so
->so_rcv
:
592 * Reply immediately if the event has occured, otherwise queue the
595 if (msg
->nm_predicate(&msg
->nm_netmsg
)) {
596 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
,
597 msg
->nm_netmsg
.nm_lmsg
.ms_error
);
599 TAILQ_INSERT_TAIL(&ssb
->ssb_sel
.si_mlist
, msg
, nm_list
);
600 ssb
->ssb_flags
|= SSB_MEVENT
;
605 * Called by doio when trying to abort a netmsg_so_notify message.
606 * Unlike the other functions this one is dispatched directly by
607 * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
609 * The original message, lmsg, is under the control of the caller and
610 * will not be destroyed until we return so we can safely reference it
611 * in our synchronous abort request.
613 * This part of the abort request occurs on the originating cpu which
614 * means we may race the message flags and the original message may
615 * not even have been processed by the target cpu yet.
618 netmsg_so_notify_doabort(lwkt_msg_t lmsg
)
620 struct netmsg_so_notify_abort msg
;
622 if ((lmsg
->ms_flags
& (MSGF_DONE
| MSGF_REPLY
)) == 0) {
623 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
624 netmsg_so_notify_abort
);
625 msg
.nm_notifymsg
= (void *)lmsg
;
626 lwkt_domsg(lmsg
->ms_target_port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
631 * Predicate requests can be aborted. This function is only called once
632 * and will interlock against processing/reply races (since such races
633 * occur on the same thread that controls the port where the abort is
636 * This part of the abort request occurs on the target cpu. The message
637 * flags must be tested again in case the test that we did on the
638 * originating cpu raced. Since messages are handled in sequence, the
639 * original message will have already been handled by the loop and either
640 * replied to or queued.
642 * We really only need to interlock with MSGF_REPLY (a bit that is set on
643 * our cpu when we reply). Note that MSGF_DONE is not set until the
644 * reply reaches the originating cpu. Test both bits anyway.
647 netmsg_so_notify_abort(netmsg_t netmsg
)
649 struct netmsg_so_notify_abort
*abrtmsg
= (void *)netmsg
;
650 struct netmsg_so_notify
*msg
= abrtmsg
->nm_notifymsg
;
651 struct signalsockbuf
*ssb
;
654 * The original notify message is not destroyed until after the
655 * abort request is returned, so we can check its state.
657 if ((msg
->nm_netmsg
.nm_lmsg
.ms_flags
& (MSGF_DONE
| MSGF_REPLY
)) == 0) {
658 ssb
= (msg
->nm_etype
& NM_REVENT
) ?
659 &msg
->nm_so
->so_rcv
:
661 TAILQ_REMOVE(&ssb
->ssb_sel
.si_mlist
, msg
, nm_list
);
662 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
, EINTR
);
666 * Reply to the abort message
668 lwkt_replymsg(&abrtmsg
->nm_netmsg
.nm_lmsg
, 0);