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.13 2004/07/08 22:07:34 hsu 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,
181 return ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, ifp,
183 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
185 struct netmsg_pru_control msg;
188 port = so->so_proto->pr_mport(so, NULL, PRU_CONTROL);
189 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
190 lwkt_cmd_func(netmsg_pru_control), lwkt_cmd_op_none);
191 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_control;
197 error = lwkt_domsg(port, &msg.nm_lmsg);
203 so_pru_detach(struct socket *so)
206 struct netmsg_pru_detach msg;
209 port = so->so_proto->pr_mport(so, NULL, PRU_DETACH);
210 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
211 lwkt_cmd_func(netmsg_pru_detach), lwkt_cmd_op_none);
212 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_detach;
214 error = lwkt_domsg(port, &msg.nm_lmsg);
219 so_pru_disconnect(struct socket *so)
222 struct netmsg_pru_disconnect msg;
225 port = so->so_proto->pr_mport(so, NULL, PRU_DISCONNECT);
226 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
227 lwkt_cmd_func(netmsg_pru_disconnect), lwkt_cmd_op_none);
228 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_disconnect;
230 error = lwkt_domsg(port, &msg.nm_lmsg);
235 so_pru_listen(struct socket *so, struct thread *td)
238 struct netmsg_pru_listen msg;
241 port = so->so_proto->pr_mport(so, NULL, PRU_LISTEN);
242 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
243 lwkt_cmd_func(netmsg_pru_listen), lwkt_cmd_op_none);
244 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_listen;
246 msg.nm_td = td; /* used only for prison_ip() XXX JH */
247 error = lwkt_domsg(port, &msg.nm_lmsg);
252 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
255 struct netmsg_pru_peeraddr msg;
258 port = so->so_proto->pr_mport(so, NULL, PRU_PEERADDR);
259 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
260 lwkt_cmd_func(netmsg_pru_peeraddr), lwkt_cmd_op_none);
261 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_peeraddr;
264 error = lwkt_domsg(port, &msg.nm_lmsg);
269 so_pru_rcvd(struct socket *so, int flags)
272 struct netmsg_pru_rcvd msg;
275 port = so->so_proto->pr_mport(so, NULL, PRU_RCVD);
276 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
277 lwkt_cmd_func(netmsg_pru_rcvd), lwkt_cmd_op_none);
278 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_rcvd;
280 msg.nm_flags = flags;
281 error = lwkt_domsg(port, &msg.nm_lmsg);
286 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
289 struct netmsg_pru_rcvoob msg;
292 port = so->so_proto->pr_mport(so, NULL, PRU_RCVOOB);
293 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
294 lwkt_cmd_func(netmsg_pru_rcvoob), lwkt_cmd_op_none);
295 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_rcvoob;
298 msg.nm_flags = flags;
299 error = lwkt_domsg(port, &msg.nm_lmsg);
304 so_pru_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
305 struct mbuf *control, struct thread *td)
308 struct netmsg_pru_send msg;
311 port = so->so_proto->pr_mport(so, NULL, PRU_SEND);
312 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
313 lwkt_cmd_func(netmsg_pru_send), lwkt_cmd_op_none);
314 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_send;
316 msg.nm_flags = flags;
319 msg.nm_control = control;
321 error = lwkt_domsg(port, &msg.nm_lmsg);
326 so_pru_sense(struct socket *so, struct stat *sb)
329 struct netmsg_pru_sense msg;
332 port = so->so_proto->pr_mport(so, NULL, PRU_SENSE);
333 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
334 lwkt_cmd_func(netmsg_pru_sense), lwkt_cmd_op_none);
335 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sense;
338 error = lwkt_domsg(port, &msg.nm_lmsg);
343 so_pru_shutdown(struct socket *so)
346 struct netmsg_pru_shutdown msg;
349 port = so->so_proto->pr_mport(so, NULL, PRU_SHUTDOWN);
350 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
351 lwkt_cmd_func(netmsg_pru_shutdown), lwkt_cmd_op_none);
352 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_shutdown;
354 error = lwkt_domsg(port, &msg.nm_lmsg);
359 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
362 struct netmsg_pru_sockaddr msg;
365 port = so->so_proto->pr_mport(so, NULL, PRU_SOCKADDR);
366 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
367 lwkt_cmd_func(netmsg_pru_sockaddr), lwkt_cmd_op_none);
368 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sockaddr;
371 error = lwkt_domsg(port, &msg.nm_lmsg);
376 so_pru_sopoll(struct socket *so, int events, struct ucred *cred,
380 struct netmsg_pru_sopoll msg;
383 port = so->so_proto->pr_mport(so, NULL, PRU_SOPOLL);
384 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
385 lwkt_cmd_func(netmsg_pru_sopoll), lwkt_cmd_op_none);
386 msg.nm_prufn = so->so_proto->pr_usrreqs->pru_sopoll;
388 msg.nm_events = events;
391 error = lwkt_domsg(port, &msg.nm_lmsg);
396 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
398 return ((*so->so_proto->pr_ctloutput)(so, sopt));
399 #ifdef gag /* does copyin and copyout deep inside stack XXX JH */
400 struct netmsg_pr_ctloutput msg;
404 port = so->so_proto->pr_mport(so, NULL);
405 lwkt_initmsg(&msg.nm_lmsg, &curthread->td_msgport, 0,
406 lwkt_cmd_func(netmsg_pru_ctloutput), lwkt_cmd_op_none);
407 msg.nm_prfn = so->so_proto->pr_ctloutput;
410 error = lwkt_domsg(port, &msg.nm_lmsg);
416 * If we convert all the protosw pr_ functions for all the protocols
417 * to take a message directly, this layer can go away. For the moment
418 * our dispatcher ignores the return value, but since we are handling
419 * the replymsg ourselves we return EASYNC by convention.
422 netmsg_pru_abort(lwkt_msg_t msg)
424 struct netmsg_pru_abort *nm = (void *)msg;
426 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
432 netmsg_pru_accept(lwkt_msg_t msg)
434 struct netmsg_pru_accept *nm = (void *)msg;
436 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam));
442 netmsg_pru_attach(lwkt_msg_t msg)
444 struct netmsg_pru_attach *nm = (void *)msg;
446 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_proto, nm->nm_ai));
451 netmsg_pru_bind(lwkt_msg_t msg)
453 struct netmsg_pru_bind *nm = (void *)msg;
455 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam, nm->nm_td));
460 netmsg_pru_connect(lwkt_msg_t msg)
462 struct netmsg_pru_connect *nm = (void *)msg;
464 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam, nm->nm_td));
469 netmsg_pru_connect2(lwkt_msg_t msg)
471 struct netmsg_pru_connect2 *nm = (void *)msg;
473 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so1, nm->nm_so2));
478 netmsg_pru_control(lwkt_msg_t msg)
480 struct netmsg_pru_control *nm = (void *)msg;
483 error = nm->nm_prufn(nm->nm_so, nm->nm_cmd, nm->nm_data,
484 nm->nm_ifp, nm->nm_td);
485 lwkt_replymsg(msg, error);
490 netmsg_pru_detach(lwkt_msg_t msg)
492 struct netmsg_pru_detach *nm = (void *)msg;
494 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
499 netmsg_pru_disconnect(lwkt_msg_t msg)
501 struct netmsg_pru_disconnect *nm = (void *)msg;
503 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
508 netmsg_pru_listen(lwkt_msg_t msg)
510 struct netmsg_pru_listen *nm = (void *)msg;
512 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_td));
517 netmsg_pru_peeraddr(lwkt_msg_t msg)
519 struct netmsg_pru_peeraddr *nm = (void *)msg;
521 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam));
526 netmsg_pru_rcvd(lwkt_msg_t msg)
528 struct netmsg_pru_rcvd *nm = (void *)msg;
530 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_flags));
535 netmsg_pru_rcvoob(lwkt_msg_t msg)
537 struct netmsg_pru_rcvoob *nm = (void *)msg;
539 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_m, nm->nm_flags));
544 netmsg_pru_send(lwkt_msg_t msg)
546 struct netmsg_pru_send *nm = (void *)msg;
549 error = nm->nm_prufn(nm->nm_so, nm->nm_flags, nm->nm_m,
550 nm->nm_addr, nm->nm_control, nm->nm_td);
551 lwkt_replymsg(msg, error);
556 netmsg_pru_sense(lwkt_msg_t msg)
558 struct netmsg_pru_sense *nm = (void *)msg;
560 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_stat));
565 netmsg_pru_shutdown(lwkt_msg_t msg)
567 struct netmsg_pru_shutdown *nm = (void *)msg;
569 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so));
574 netmsg_pru_sockaddr(lwkt_msg_t msg)
576 struct netmsg_pru_sockaddr *nm = (void *)msg;
578 lwkt_replymsg(msg, nm->nm_prufn(nm->nm_so, nm->nm_nam));
583 netmsg_pru_sopoll(lwkt_msg_t msg)
585 struct netmsg_pru_sopoll *nm = (void *)msg;
588 error = nm->nm_prufn(nm->nm_so, nm->nm_events, nm->nm_cred, nm->nm_td);
589 lwkt_replymsg(msg, error);
594 netmsg_pr_ctloutput(lwkt_msg_t msg)
596 struct netmsg_pr_ctloutput *nm = (void *)msg;
598 lwkt_replymsg(msg, nm->nm_prfn(nm->nm_so, nm->nm_sopt));
603 netmsg_pr_timeout(lwkt_msg_t msg)
605 struct netmsg_pr_timeout *nm = (void *)msg;
607 lwkt_replymsg(msg, nm->nm_prfn());
612 * Handle a predicate event request. This function is only called once
613 * when the predicate message queueing request is received.
616 netmsg_so_notify(lwkt_msg_t lmsg)
618 struct netmsg_so_notify *msg = (void *)lmsg;
621 sb = (msg->nm_etype & NM_REVENT) ?
622 &msg->nm_so->so_rcv :
626 * Reply immediately if the event has occured, otherwise queue the
629 if (msg->nm_predicate((struct netmsg *)msg)) {
630 lwkt_replymsg(lmsg, lmsg->ms_error);
632 TAILQ_INSERT_TAIL(&sb->sb_sel.si_mlist, msg, nm_list);
633 sb->sb_flags |= SB_MEVENT;
639 * Predicate requests can be aborted. This function is only called once
640 * and will interlock against processing/reply races (since such races
641 * occur on the same thread that controls the port where the abort is
645 netmsg_so_notify_abort(lwkt_msg_t lmsg)
647 struct netmsg_so_notify *msg = (void *)lmsg;
650 sb = (msg->nm_etype & NM_REVENT) ?
651 &msg->nm_so->so_rcv :
653 TAILQ_REMOVE(&sb->sb_sel.si_mlist, msg, nm_list);
654 lwkt_replymsg(lmsg, EINTR);