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.14 2006/05/06 02:43:12 dillon Exp $
37 * Copyright (c) 2003, 2004 Jeffrey M. Hsu. All rights reserved.
39 * License terms: all terms for the DragonFly license above plus the following:
41 * 4. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
44 * This product includes software developed by Jeffrey M. Hsu
45 * for the DragonFly Project.
47 * This requirement may be waived with permission from Jeffrey Hsu.
48 * This requirement will sunset and may be removed on July 8 2005,
49 * after which the standard DragonFly license (as shown above) will
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/msgport.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/socketops.h>
60 #include <sys/thread.h>
61 #include <sys/thread2.h>
62 #include <sys/msgport2.h>
64 #include <net/netisr.h>
65 #include <net/netmsg.h>
68 so_pru_abort(struct socket *so)
71 struct netmsg_pru_abort msg;
74 port = so->so_proto->pr_mport(so, NULL, PRU_ABORT);
75 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
76 lwkt_cmd_func(netmsg_pru_abort), lwkt_cmd_op_none);
77 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_abort;
79 error = lwkt_domsg(port, &msg.nm_lmsg);
84 so_pru_accept(struct socket *so, struct sockaddr **nam)
86 /* Block (memory allocation) in process context. XXX JH */
87 return ((*so->so_proto->pr_usrreqs->pru_accept)(so, nam));
91 struct netmsg_pru_accept msg;
94 port = so->so_proto->pr_mport(so, NULL, PRU_ACCEPT);
95 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
96 lwkt_cmd_func(netmsg_pru_accept), lwkt_cmd_op_none);
97 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_accept;
100 error = lwkt_domsg(port, &msg.nm_lmsg);
106 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
109 struct netmsg_pru_attach msg;
112 port = so->so_proto->pr_mport(NULL, NULL, PRU_ATTACH);
113 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
114 lwkt_cmd_func(netmsg_pru_attach), lwkt_cmd_op_none);
115 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_attach;
117 msg.nm_proto = proto;
119 error = lwkt_domsg(port, &msg.nm_lmsg);
124 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
127 struct netmsg_pru_bind msg;
130 /* Send mesg to thread for new address. */
131 port = so->so_proto->pr_mport(NULL, nam, PRU_BIND);
132 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
133 lwkt_cmd_func(netmsg_pru_bind), lwkt_cmd_op_none);
134 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_bind;
137 msg.nm_td = td; /* used only for prison_ip() XXX JH */
138 error = lwkt_domsg(port, &msg.nm_lmsg);
143 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
146 struct netmsg_pru_connect msg;
149 port = so->so_proto->pr_mport(so, nam, PRU_CONNECT);
150 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
151 lwkt_cmd_func(netmsg_pru_connect), lwkt_cmd_op_none);
152 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_connect;
156 error = lwkt_domsg(port, &msg.nm_lmsg);
161 so_pru_connect2(struct socket *so1, struct socket *so2)
164 struct netmsg_pru_connect2 msg;
167 port = so1->so_proto->pr_mport(so1, NULL, PRU_CONNECT2);
168 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
169 lwkt_cmd_func(netmsg_pru_connect2), lwkt_cmd_op_none);
170 msg.nm_prufn = so1->so_proto->pr_usrreqs->pru_connect2;
173 error = lwkt_domsg(port, &msg.nm_lmsg);
178 so_pru_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp)
180 return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, ifp,
182 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
184 struct netmsg_pru_control msg;
187 port = so->so_proto->pr_mport(so, NULL, PRU_CONTROL);
188 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
189 lwkt_cmd_func(netmsg_pru_control), lwkt_cmd_op_none);
190 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_control;
196 error = lwkt_domsg(port, &msg.nm_lmsg);
202 so_pru_detach(struct socket *so)
205 struct netmsg_pru_detach msg;
208 port = so->so_proto->pr_mport(so, NULL, PRU_DETACH);
209 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
210 lwkt_cmd_func(netmsg_pru_detach), lwkt_cmd_op_none);
211 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_detach;
213 error = lwkt_domsg(port, &msg.nm_lmsg);
218 so_pru_disconnect(struct socket *so)
221 struct netmsg_pru_disconnect msg;
224 port = so->so_proto->pr_mport(so, NULL, PRU_DISCONNECT);
225 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
226 lwkt_cmd_func(netmsg_pru_disconnect), lwkt_cmd_op_none);
227 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_disconnect;
229 error = lwkt_domsg(port, &msg.nm_lmsg);
234 so_pru_listen(struct socket *so, struct thread *td)
237 struct netmsg_pru_listen msg;
240 port = so->so_proto->pr_mport(so, NULL, PRU_LISTEN);
241 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
242 lwkt_cmd_func(netmsg_pru_listen), lwkt_cmd_op_none);
243 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_listen;
245 msg.nm_td = td; /* used only for prison_ip() XXX JH */
246 error = lwkt_domsg(port, &msg.nm_lmsg);
251 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
254 struct netmsg_pru_peeraddr msg;
257 port = so->so_proto->pr_mport(so, NULL, PRU_PEERADDR);
258 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
259 lwkt_cmd_func(netmsg_pru_peeraddr), lwkt_cmd_op_none);
260 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_peeraddr;
263 error = lwkt_domsg(port, &msg.nm_lmsg);
268 so_pru_rcvd(struct socket *so, int flags)
271 struct netmsg_pru_rcvd msg;
274 port = so->so_proto->pr_mport(so, NULL, PRU_RCVD);
275 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
276 lwkt_cmd_func(netmsg_pru_rcvd), lwkt_cmd_op_none);
277 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_rcvd;
279 msg.nm_flags = flags;
280 error = lwkt_domsg(port, &msg.nm_lmsg);
285 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
288 struct netmsg_pru_rcvoob msg;
291 port = so->so_proto->pr_mport(so, NULL, PRU_RCVOOB);
292 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
293 lwkt_cmd_func(netmsg_pru_rcvoob), lwkt_cmd_op_none);
294 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_rcvoob;
297 msg.nm_flags = flags;
298 error = lwkt_domsg(port, &msg.nm_lmsg);
303 so_pru_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
304 struct mbuf *control, struct thread *td)
307 struct netmsg_pru_send msg;
310 port = so->so_proto->pr_mport(so, NULL, PRU_SEND);
311 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
312 lwkt_cmd_func(netmsg_pru_send), lwkt_cmd_op_none);
313 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_send;
315 msg.nm_flags = flags;
318 msg.nm_control = control;
320 error = lwkt_domsg(port, &msg.nm_lmsg);
325 so_pru_sense(struct socket *so, struct stat *sb)
328 struct netmsg_pru_sense msg;
331 port = so->so_proto->pr_mport(so, NULL, PRU_SENSE);
332 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
333 lwkt_cmd_func(netmsg_pru_sense), lwkt_cmd_op_none);
334 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sense;
337 error = lwkt_domsg(port, &msg.nm_lmsg);
342 so_pru_shutdown(struct socket *so)
345 struct netmsg_pru_shutdown msg;
348 port = so->so_proto->pr_mport(so, NULL, PRU_SHUTDOWN);
349 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
350 lwkt_cmd_func(netmsg_pru_shutdown), lwkt_cmd_op_none);
351 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_shutdown;
353 error = lwkt_domsg(port, &msg.nm_lmsg);
358 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
361 struct netmsg_pru_sockaddr msg;
364 port = so->so_proto->pr_mport(so, NULL, PRU_SOCKADDR);
365 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
366 lwkt_cmd_func(netmsg_pru_sockaddr), lwkt_cmd_op_none);
367 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sockaddr;
370 error = lwkt_domsg(port, &msg.nm_lmsg);
375 so_pru_sopoll(struct socket *so, int events, struct ucred *cred)
378 struct netmsg_pru_sopoll msg;
381 port = so->so_proto->pr_mport(so, NULL, PRU_SOPOLL);
382 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
383 lwkt_cmd_func(netmsg_pru_sopoll), lwkt_cmd_op_none);
384 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sopoll;
386 msg.nm_events = events;
388 msg.nm_td = curthread;
389 error = lwkt_domsg(port, &msg.nm_lmsg);
394 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
396 return ((*so->so_proto->pr_ctloutput)(so, sopt));
397 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
398 struct netmsg_pr_ctloutput msg;
402 port = so->so_proto->pr_mport(so, NULL);
403 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
404 lwkt_cmd_func(netmsg_pru_ctloutput), lwkt_cmd_op_none);
405 msg.nm_prfn = so->so_proto->pr_ctloutput;
408 error = lwkt_domsg(port, &msg.nm_lmsg);
414 * If we convert all the protosw pr_ functions for all the protocols
415 * to take a message directly, this layer can go away. For the moment
416 * our dispatcher ignores the return value, but since we are handling
417 * the replymsg ourselves we return EASYNC by convention.
420 netmsg_pru_abort(lwkt_msg_t msg)
422 struct netmsg_pru_abort *nm = (void *)msg;
424 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
430 netmsg_pru_accept(lwkt_msg_t msg)
432 struct netmsg_pru_accept *nm = (void *)msg;
434 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam));
440 netmsg_pru_attach(lwkt_msg_t msg)
442 struct netmsg_pru_attach *nm = (void *)msg;
444 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_proto, nm->nm_ai));
449 netmsg_pru_bind(lwkt_msg_t msg)
451 struct netmsg_pru_bind *nm = (void *)msg;
453 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam, nm->nm_td));
458 netmsg_pru_connect(lwkt_msg_t msg)
460 struct netmsg_pru_connect *nm = (void *)msg;
462 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam, nm->nm_td));
467 netmsg_pru_connect2(lwkt_msg_t msg)
469 struct netmsg_pru_connect2 *nm = (void *)msg;
471 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so1, nm->nm_so2));
476 netmsg_pru_control(lwkt_msg_t msg)
478 struct netmsg_pru_control *nm = (void *)msg;
481 error = nm->nm_prufn(nm->nm_so, nm->nm_cmd, nm->nm_data,
482 nm->nm_ifp, nm->nm_td);
483 lwkt_replymsg(msg, error);
488 netmsg_pru_detach(lwkt_msg_t msg)
490 struct netmsg_pru_detach *nm = (void *)msg;
492 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
497 netmsg_pru_disconnect(lwkt_msg_t msg)
499 struct netmsg_pru_disconnect *nm = (void *)msg;
501 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
506 netmsg_pru_listen(lwkt_msg_t msg)
508 struct netmsg_pru_listen *nm = (void *)msg;
510 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_td));
515 netmsg_pru_peeraddr(lwkt_msg_t msg)
517 struct netmsg_pru_peeraddr *nm = (void *)msg;
519 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam));
524 netmsg_pru_rcvd(lwkt_msg_t msg)
526 struct netmsg_pru_rcvd *nm = (void *)msg;
528 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_flags));
533 netmsg_pru_rcvoob(lwkt_msg_t msg)
535 struct netmsg_pru_rcvoob *nm = (void *)msg;
537 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_m, nm->nm_flags));
542 netmsg_pru_send(lwkt_msg_t msg)
544 struct netmsg_pru_send *nm = (void *)msg;
547 error = nm->nm_prufn(nm->nm_so, nm->nm_flags, nm->nm_m,
548 nm->nm_addr, nm->nm_control, nm->nm_td);
549 lwkt_replymsg(msg, error);
554 netmsg_pru_sense(lwkt_msg_t msg)
556 struct netmsg_pru_sense *nm = (void *)msg;
558 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_stat));
563 netmsg_pru_shutdown(lwkt_msg_t msg)
565 struct netmsg_pru_shutdown *nm = (void *)msg;
567 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
572 netmsg_pru_sockaddr(lwkt_msg_t msg)
574 struct netmsg_pru_sockaddr *nm = (void *)msg;
576 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam));
581 netmsg_pru_sopoll(lwkt_msg_t msg)
583 struct netmsg_pru_sopoll *nm = (void *)msg;
586 error = nm->nm_prufn(nm->nm_so, nm->nm_events, nm->nm_cred, nm->nm_td);
587 lwkt_replymsg(msg, error);
592 netmsg_pr_ctloutput(lwkt_msg_t msg)
594 struct netmsg_pr_ctloutput *nm = (void *)msg;
596 lwkt_replymsg(msg, nm->nm_prfn(nm->nm_so, nm->nm_sopt));
601 netmsg_pr_timeout(lwkt_msg_t msg)
603 struct netmsg_pr_timeout *nm = (void *)msg;
605 lwkt_replymsg(msg, nm->nm_prfn());
610 * Handle a predicate event request. This function is only called once
611 * when the predicate message queueing request is received.
614 netmsg_so_notify(lwkt_msg_t lmsg)
616 struct netmsg_so_notify *msg = (void *)lmsg;
619 sb = (msg->nm_etype & NM_REVENT) ?
620 &msg->nm_so->so_rcv :
624 * Reply immediately if the event has occured, otherwise queue the
627 if (msg->nm_predicate((struct netmsg *)msg)) {
628 lwkt_replymsg(lmsg, lmsg->ms_error);
630 TAILQ_INSERT_TAIL(&sb->sb_sel.si_mlist, msg, nm_list);
631 sb->sb_flags |= SB_MEVENT;
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
643 netmsg_so_notify_abort(lwkt_msg_t lmsg)
645 struct netmsg_so_notify *msg = (void *)lmsg;
648 sb = (msg->nm_etype & NM_REVENT) ?
649 &msg->nm_so->so_rcv :
651 TAILQ_REMOVE(&sb->sb_sel.si_mlist, msg, nm_list);
652 lwkt_replymsg(lmsg, EINTR);