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.23 2008/07/10 00:19:27 aggelos Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/msgport.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/socketops.h>
44 #include <sys/thread.h>
45 #include <sys/thread2.h>
46 #include <sys/msgport2.h>
48 #include <net/netmsg2.h>
50 #include <net/netisr.h>
51 #include <net/netmsg.h>
54 so_pru_abort(struct socket
*so
)
57 struct netmsg_pru_abort msg
;
60 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_ABORT
);
61 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
63 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_abort
;
65 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
70 so_pru_accept(struct socket
*so
, struct sockaddr
**nam
)
72 /* Block (memory allocation) in process context. XXX JH */
73 return ((*so
->so_proto
->pr_usrreqs
->pru_accept
)(so
, nam
));
77 struct netmsg_pru_accept msg
;
80 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_ACCEPT
);
81 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
83 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_accept
;
86 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
92 so_pru_attach(struct socket
*so
, int proto
, struct pru_attach_info
*ai
)
95 struct netmsg_pru_attach msg
;
98 port
= so
->so_proto
->pr_mport(NULL
, NULL
, NULL
, PRU_ATTACH
);
99 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
101 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_attach
;
103 msg
.nm_proto
= proto
;
105 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
110 so_pru_bind(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
113 struct netmsg_pru_bind msg
;
116 /* Send mesg to thread for new address. */
117 port
= so
->so_proto
->pr_mport(NULL
, nam
, NULL
, PRU_BIND
);
118 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
120 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_bind
;
123 msg
.nm_td
= td
; /* used only for prison_ip() XXX JH */
124 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
129 so_pru_connect(struct socket
*so
, struct sockaddr
*nam
, struct thread
*td
)
132 struct netmsg_pru_connect msg
;
135 port
= so
->so_proto
->pr_mport(so
, nam
, NULL
, PRU_CONNECT
);
136 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
138 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_connect
;
142 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
147 so_pru_connect2(struct socket
*so1
, struct socket
*so2
)
150 struct netmsg_pru_connect2 msg
;
153 port
= so1
->so_proto
->pr_mport(so1
, NULL
, NULL
, PRU_CONNECT2
);
154 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
155 netmsg_pru_connect2
);
156 msg
.nm_prufn
= so1
->so_proto
->pr_usrreqs
->pru_connect2
;
159 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
164 so_pru_control(struct socket
*so
, u_long cmd
, caddr_t data
, struct ifnet
*ifp
)
166 return ((*so
->so_proto
->pr_usrreqs
->pru_control
)(so
, cmd
, data
, ifp
,
168 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
170 struct netmsg_pru_control msg
;
173 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_CONTROL
);
174 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
176 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_control
;
182 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
188 so_pru_detach(struct socket
*so
)
191 struct netmsg_pru_detach msg
;
194 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_DETACH
);
195 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
197 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_detach
;
199 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
204 so_pru_disconnect(struct socket
*so
)
207 struct netmsg_pru_disconnect msg
;
210 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_DISCONNECT
);
211 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
212 netmsg_pru_disconnect
);
213 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_disconnect
;
215 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
220 so_pru_listen(struct socket
*so
, struct thread
*td
)
223 struct netmsg_pru_listen msg
;
226 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_LISTEN
);
227 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
229 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_listen
;
231 msg
.nm_td
= td
; /* used only for prison_ip() XXX JH */
232 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
237 so_pru_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
240 struct netmsg_pru_peeraddr msg
;
243 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_PEERADDR
);
244 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
245 netmsg_pru_peeraddr
);
246 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_peeraddr
;
249 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
254 so_pru_rcvd(struct socket
*so
, int flags
)
257 struct netmsg_pru_rcvd msg
;
260 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_RCVD
);
261 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
263 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_rcvd
;
265 msg
.nm_flags
= flags
;
266 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
271 so_pru_rcvoob(struct socket
*so
, struct mbuf
*m
, int flags
)
274 struct netmsg_pru_rcvoob msg
;
277 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_RCVOOB
);
278 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
280 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_rcvoob
;
283 msg
.nm_flags
= flags
;
284 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
289 so_pru_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
290 struct mbuf
*control
, struct thread
*td
)
293 struct netmsg_pru_send msg
;
296 port
= so
->so_proto
->pr_mport(so
, addr
, &m
, PRU_SEND
);
302 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
304 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_send
;
306 msg
.nm_flags
= flags
;
309 msg
.nm_control
= control
;
311 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
316 so_pru_sense(struct socket
*so
, struct stat
*sb
)
319 struct netmsg_pru_sense msg
;
322 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SENSE
);
323 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
325 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sense
;
328 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
333 so_pru_shutdown(struct socket
*so
)
336 struct netmsg_pru_shutdown msg
;
339 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SHUTDOWN
);
340 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
341 netmsg_pru_shutdown
);
342 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_shutdown
;
344 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
349 so_pru_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
352 struct netmsg_pru_sockaddr msg
;
355 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SOCKADDR
);
356 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
357 netmsg_pru_sockaddr
);
358 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sockaddr
;
361 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
366 so_pru_sopoll(struct socket
*so
, int events
, struct ucred
*cred
)
369 struct netmsg_pru_sopoll msg
;
372 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_SOPOLL
);
373 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
375 msg
.nm_prufn
= so
->so_proto
->pr_usrreqs
->pru_sopoll
;
377 msg
.nm_events
= events
;
379 msg
.nm_td
= curthread
;
380 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
385 so_pru_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
387 struct netmsg_pru_ctloutput msg
;
391 KKASSERT(!sopt
->sopt_val
|| kva_p(sopt
->sopt_val
));
392 port
= so
->so_proto
->pr_mport(so
, NULL
, NULL
, PRU_CTLOUTPUT
);
393 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
394 netmsg_pru_ctloutput
);
395 /* TBD: move pr_ctloutput to pr_usrreqs */
396 msg
.nm_prufn
= so
->so_proto
->pr_ctloutput
;
399 error
= lwkt_domsg(port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
404 * If we convert all the protosw pr_ functions for all the protocols
405 * to take a message directly, this layer can go away. For the moment
406 * our dispatcher ignores the return value, but since we are handling
407 * the replymsg ourselves we return EASYNC by convention.
410 netmsg_pru_abort(netmsg_t msg
)
412 struct netmsg_pru_abort
*nm
= (void *)msg
;
414 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
419 netmsg_pru_accept(netmsg_t msg
)
421 struct netmsg_pru_accept
*nm
= (void *)msg
;
423 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
428 netmsg_pru_attach(netmsg_t msg
)
430 struct netmsg_pru_attach
*nm
= (void *)msg
;
432 lwkt_replymsg(&msg
->nm_lmsg
,
433 nm
->nm_prufn(nm
->nm_so
, nm
->nm_proto
, nm
->nm_ai
));
437 netmsg_pru_bind(netmsg_t msg
)
439 struct netmsg_pru_bind
*nm
= (void *)msg
;
441 lwkt_replymsg(&msg
->nm_lmsg
,
442 nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
, nm
->nm_td
));
446 netmsg_pru_connect(netmsg_t msg
)
448 struct netmsg_pru_connect
*nm
= (void *)msg
;
450 lwkt_replymsg(&msg
->nm_lmsg
,
451 nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
, nm
->nm_td
));
455 netmsg_pru_connect2(netmsg_t msg
)
457 struct netmsg_pru_connect2
*nm
= (void *)msg
;
459 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so1
, nm
->nm_so2
));
463 netmsg_pru_control(netmsg_t msg
)
465 struct netmsg_pru_control
*nm
= (void *)msg
;
468 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_cmd
, nm
->nm_data
,
469 nm
->nm_ifp
, nm
->nm_td
);
470 lwkt_replymsg(&msg
->nm_lmsg
, error
);
474 netmsg_pru_detach(netmsg_t msg
)
476 struct netmsg_pru_detach
*nm
= (void *)msg
;
478 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
482 netmsg_pru_disconnect(netmsg_t msg
)
484 struct netmsg_pru_disconnect
*nm
= (void *)msg
;
486 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
490 netmsg_pru_listen(netmsg_t msg
)
492 struct netmsg_pru_listen
*nm
= (void *)msg
;
494 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_td
));
498 netmsg_pru_peeraddr(netmsg_t msg
)
500 struct netmsg_pru_peeraddr
*nm
= (void *)msg
;
502 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
506 netmsg_pru_rcvd(netmsg_t msg
)
508 struct netmsg_pru_rcvd
*nm
= (void *)msg
;
510 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_flags
));
514 netmsg_pru_rcvoob(netmsg_t msg
)
516 struct netmsg_pru_rcvoob
*nm
= (void *)msg
;
518 lwkt_replymsg(&msg
->nm_lmsg
,
519 nm
->nm_prufn(nm
->nm_so
, nm
->nm_m
, nm
->nm_flags
));
523 netmsg_pru_send(netmsg_t msg
)
525 struct netmsg_pru_send
*nm
= (void *)msg
;
528 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_flags
, nm
->nm_m
,
529 nm
->nm_addr
, nm
->nm_control
, nm
->nm_td
);
530 lwkt_replymsg(&msg
->nm_lmsg
, error
);
534 netmsg_pru_sense(netmsg_t msg
)
536 struct netmsg_pru_sense
*nm
= (void *)msg
;
538 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_stat
));
542 netmsg_pru_shutdown(netmsg_t msg
)
544 struct netmsg_pru_shutdown
*nm
= (void *)msg
;
546 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
));
550 netmsg_pru_sockaddr(netmsg_t msg
)
552 struct netmsg_pru_sockaddr
*nm
= (void *)msg
;
554 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_nam
));
558 netmsg_pru_sopoll(netmsg_t msg
)
560 struct netmsg_pru_sopoll
*nm
= (void *)msg
;
563 error
= nm
->nm_prufn(nm
->nm_so
, nm
->nm_events
, nm
->nm_cred
, nm
->nm_td
);
564 lwkt_replymsg(&msg
->nm_lmsg
, error
);
568 netmsg_pru_ctloutput(netmsg_t msg
)
570 struct netmsg_pru_ctloutput
*nm
= (void *)msg
;
572 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prufn(nm
->nm_so
, nm
->nm_sopt
));
576 netmsg_pr_timeout(netmsg_t msg
)
578 struct netmsg_pr_timeout
*nm
= (void *)msg
;
580 lwkt_replymsg(&msg
->nm_lmsg
, nm
->nm_prfn());
584 * Handle a predicate event request. This function is only called once
585 * when the predicate message queueing request is received.
588 netmsg_so_notify(netmsg_t netmsg
)
590 struct netmsg_so_notify
*msg
= (void *)netmsg
;
591 struct signalsockbuf
*ssb
;
593 ssb
= (msg
->nm_etype
& NM_REVENT
) ?
594 &msg
->nm_so
->so_rcv
:
598 * Reply immediately if the event has occured, otherwise queue the
601 if (msg
->nm_predicate(&msg
->nm_netmsg
)) {
602 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
,
603 msg
->nm_netmsg
.nm_lmsg
.ms_error
);
605 TAILQ_INSERT_TAIL(&ssb
->ssb_sel
.si_mlist
, msg
, nm_list
);
606 ssb
->ssb_flags
|= SSB_MEVENT
;
611 * Called by doio when trying to abort a netmsg_so_notify message.
612 * Unlike the other functions this one is dispatched directly by
613 * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
615 * The original message, lmsg, is under the control of the caller and
616 * will not be destroyed until we return so we can safely reference it
617 * in our synchronous abort request.
619 * This part of the abort request occurs on the originating cpu which
620 * means we may race the message flags and the original message may
621 * not even have been processed by the target cpu yet.
624 netmsg_so_notify_doabort(lwkt_msg_t lmsg
)
626 struct netmsg_so_notify_abort msg
;
628 if ((lmsg
->ms_flags
& (MSGF_DONE
| MSGF_REPLY
)) == 0) {
629 netmsg_init(&msg
.nm_netmsg
, &curthread
->td_msgport
, 0,
630 netmsg_so_notify_abort
);
631 msg
.nm_notifymsg
= (void *)lmsg
;
632 lwkt_domsg(lmsg
->ms_target_port
, &msg
.nm_netmsg
.nm_lmsg
, 0);
637 * Predicate requests can be aborted. This function is only called once
638 * and will interlock against processing/reply races (since such races
639 * occur on the same thread that controls the port where the abort is
642 * This part of the abort request occurs on the target cpu. The message
643 * flags must be tested again in case the test that we did on the
644 * originating cpu raced. Since messages are handled in sequence, the
645 * original message will have already been handled by the loop and either
646 * replied to or queued.
648 * We really only need to interlock with MSGF_REPLY (a bit that is set on
649 * our cpu when we reply). Note that MSGF_DONE is not set until the
650 * reply reaches the originating cpu. Test both bits anyway.
653 netmsg_so_notify_abort(netmsg_t netmsg
)
655 struct netmsg_so_notify_abort
*abrtmsg
= (void *)netmsg
;
656 struct netmsg_so_notify
*msg
= abrtmsg
->nm_notifymsg
;
657 struct signalsockbuf
*ssb
;
660 * The original notify message is not destroyed until after the
661 * abort request is returned, so we can check its state.
663 if ((msg
->nm_netmsg
.nm_lmsg
.ms_flags
& (MSGF_DONE
| MSGF_REPLY
)) == 0) {
664 ssb
= (msg
->nm_etype
& NM_REVENT
) ?
665 &msg
->nm_so
->so_rcv
:
667 TAILQ_REMOVE(&ssb
->ssb_sel
.si_mlist
, msg
, nm_list
);
668 lwkt_replymsg(&msg
->nm_netmsg
.nm_lmsg
, EINTR
);
672 * Reply to the abort message
674 lwkt_replymsg(&abrtmsg
->nm_netmsg
.nm_lmsg
, 0);