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.26 2008/10/27 02:56:30 sephe 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>
49 #include <net/netmsg2.h>
51 #include <net/netisr.h>
52 #include <net/netmsg.h>
55 * Abort a socket and free it. Called from soabort() only. soabort()
56 * got a ref on the socket which we must free on reply.
59 so_pru_abort(struct socket *so)
61 struct netmsg_pru_abort msg;
63 netmsg_init(&msg.base, so, &curthread->td_msgport,
64 0, so->so_proto->pr_usrreqs->pru_abort);
65 (void)lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
66 sofree(msg.base.nm_so);
70 * Abort a socket and free it, asynchronously. Called from
71 * soaborta() only. soaborta() got a ref on the socket which we must
75 so_pru_aborta(struct socket *so)
77 struct netmsg_pru_abort *msg;
79 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_ZERO);
80 netmsg_init(&msg->base, so, &netisr_afree_free_so_rport,
81 0, so->so_proto->pr_usrreqs->pru_abort);
82 lwkt_sendmsg(so->so_port, &msg->base.lmsg);
86 * Abort a socket and free it. Called from soabort_oncpu() only.
87 * Caller must make sure that the current CPU is inpcb's owner CPU.
90 so_pru_abort_oncpu(struct socket *so)
92 struct netmsg_pru_abort msg;
93 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_abort;
95 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
96 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
97 msg.base.lmsg.ms_flags |= MSGF_SYNC;
99 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
100 sofree(msg.base.nm_so);
104 so_pru_accept(struct socket *so, struct sockaddr **nam)
106 struct netmsg_pru_accept msg;
108 netmsg_init(&msg.base, so, &curthread->td_msgport,
109 0, so->so_proto->pr_usrreqs->pru_accept);
112 return lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
116 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
118 struct netmsg_pru_attach msg;
121 netmsg_init(&msg.base, so, &curthread->td_msgport,
122 0, so->so_proto->pr_usrreqs->pru_attach);
123 msg.nm_proto = proto;
125 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
130 so_pru_attach_direct(struct socket *so, int proto, struct pru_attach_info *ai)
132 struct netmsg_pru_attach msg;
133 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_attach;
135 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
136 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
137 msg.base.lmsg.ms_flags |= MSGF_SYNC;
138 msg.nm_proto = proto;
140 func((netmsg_t)&msg);
141 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
142 return(msg.base.lmsg.ms_error);
146 * NOTE: If the target port changes the bind operation will deal with it.
149 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
151 struct netmsg_pru_bind msg;
154 netmsg_init(&msg.base, so, &curthread->td_msgport,
155 0, so->so_proto->pr_usrreqs->pru_bind);
157 msg.nm_td = td; /* used only for prison_ip() */
158 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
163 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
165 struct netmsg_pru_connect msg;
168 netmsg_init(&msg.base, so, &curthread->td_msgport,
169 0, so->so_proto->pr_usrreqs->pru_connect);
174 msg.nm_reconnect = 0;
175 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
180 so_pru_connect2(struct socket *so1, struct socket *so2)
182 struct netmsg_pru_connect2 msg;
185 netmsg_init(&msg.base, so1, &curthread->td_msgport,
186 0, so1->so_proto->pr_usrreqs->pru_connect2);
189 error = lwkt_domsg(so1->so_port, &msg.base.lmsg, 0);
194 * WARNING! Synchronous call from user context. Control function may do
198 so_pru_control_direct(struct socket *so, u_long cmd, caddr_t data,
201 struct netmsg_pru_control msg;
202 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_control;
204 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
205 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
206 msg.base.lmsg.ms_flags |= MSGF_SYNC;
210 msg.nm_td = curthread;
211 func((netmsg_t)&msg);
212 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
213 return(msg.base.lmsg.ms_error);
217 so_pru_detach(struct socket *so)
219 struct netmsg_pru_detach msg;
222 netmsg_init(&msg.base, so, &curthread->td_msgport,
223 0, so->so_proto->pr_usrreqs->pru_detach);
224 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
229 so_pru_detach_direct(struct socket *so)
231 struct netmsg_pru_detach msg;
232 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_detach;
234 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
235 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
236 msg.base.lmsg.ms_flags |= MSGF_SYNC;
237 func((netmsg_t)&msg);
238 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
242 so_pru_disconnect(struct socket *so)
244 struct netmsg_pru_disconnect msg;
247 netmsg_init(&msg.base, so, &curthread->td_msgport,
248 0, so->so_proto->pr_usrreqs->pru_disconnect);
249 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
254 so_pru_disconnect_direct(struct socket *so)
256 struct netmsg_pru_disconnect msg;
257 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_disconnect;
259 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
260 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
261 msg.base.lmsg.ms_flags |= MSGF_SYNC;
262 func((netmsg_t)&msg);
263 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
267 so_pru_listen(struct socket *so, struct thread *td)
269 struct netmsg_pru_listen msg;
272 netmsg_init(&msg.base, so, &curthread->td_msgport,
273 0, so->so_proto->pr_usrreqs->pru_listen);
274 msg.nm_td = td; /* used only for prison_ip() XXX JH */
275 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
280 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
282 struct netmsg_pru_peeraddr msg;
285 netmsg_init(&msg.base, so, &curthread->td_msgport,
286 0, so->so_proto->pr_usrreqs->pru_peeraddr);
288 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
293 so_pru_rcvd(struct socket *so, int flags)
295 struct netmsg_pru_rcvd msg;
298 netmsg_init(&msg.base, so, &curthread->td_msgport,
299 0, so->so_proto->pr_usrreqs->pru_rcvd);
300 msg.nm_flags = flags;
301 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
306 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
308 struct netmsg_pru_rcvoob msg;
311 netmsg_init(&msg.base, so, &curthread->td_msgport,
312 0, so->so_proto->pr_usrreqs->pru_rcvoob);
314 msg.nm_flags = flags;
315 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
320 * NOTE: If the target port changes the implied connect will deal with it.
323 so_pru_send(struct socket *so, int flags, struct mbuf *m,
324 struct sockaddr *addr, struct mbuf *control, struct thread *td)
326 struct netmsg_pru_send msg;
329 netmsg_init(&msg.base, so, &curthread->td_msgport,
330 0, so->so_proto->pr_usrreqs->pru_send);
331 msg.nm_flags = flags;
334 msg.nm_control = control;
336 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
341 so_pru_sync(struct socket *so)
343 struct netmsg_base msg;
345 netmsg_init(&msg, so, &curthread->td_msgport, 0,
346 netmsg_sync_handler);
347 lwkt_domsg(so->so_port, &msg.lmsg, 0);
351 so_pru_send_async(struct socket *so, int flags, struct mbuf *m,
352 struct sockaddr *addr, struct mbuf *control, struct thread *td)
354 struct netmsg_pru_send *msg;
356 KASSERT(so->so_proto->pr_flags & PR_ASYNC_SEND,
357 ("async pru_send is not supported\n"));
359 msg = &m->m_hdr.mh_sndmsg;
360 netmsg_init(&msg->base, so, &netisr_apanic_rport,
361 0, so->so_proto->pr_usrreqs->pru_send);
362 msg->nm_flags = flags | PRUS_NOREPLY;
365 msg->nm_control = control;
367 lwkt_sendmsg(so->so_port, &msg->base.lmsg);
371 so_pru_sense(struct socket *so, struct stat *sb)
373 struct netmsg_pru_sense msg;
376 netmsg_init(&msg.base, so, &curthread->td_msgport,
377 0, so->so_proto->pr_usrreqs->pru_sense);
379 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
384 so_pru_shutdown(struct socket *so)
386 struct netmsg_pru_shutdown msg;
389 netmsg_init(&msg.base, so, &curthread->td_msgport,
390 0, so->so_proto->pr_usrreqs->pru_shutdown);
391 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
396 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
398 struct netmsg_pru_sockaddr msg;
401 netmsg_init(&msg.base, so, &curthread->td_msgport,
402 0, so->so_proto->pr_usrreqs->pru_sockaddr);
404 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
409 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
411 struct netmsg_pr_ctloutput msg;
414 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
415 netmsg_init(&msg.base, so, &curthread->td_msgport,
416 0, so->so_proto->pr_ctloutput);
418 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
423 * Protocol control input, typically via icmp.
425 * If the protocol pr_ctlport is not NULL we call it to figure out the
426 * protocol port. If NULL is returned we can just return, otherwise
427 * we issue a netmsg to call pr_ctlinput in the proper thread.
429 * This must be done synchronously as arg and/or extra may point to
433 so_pru_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra)
435 struct netmsg_pru_ctlinput msg;
438 if (pr->pr_ctlport == NULL)
440 KKASSERT(pr->pr_ctlinput != NULL);
441 port = pr->pr_ctlport(cmd, arg, extra);
444 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
448 msg.nm_extra = extra;
449 lwkt_domsg(port, &msg.base.lmsg, 0);
453 * If we convert all the protosw pr_ functions for all the protocols
454 * to take a message directly, this layer can go away. For the moment
455 * our dispatcher ignores the return value, but since we are handling
456 * the replymsg ourselves we return EASYNC by convention.
460 * Handle a predicate event request. This function is only called once
461 * when the predicate message queueing request is received.
464 netmsg_so_notify(netmsg_t msg)
466 struct lwkt_token *tok;
467 struct signalsockbuf *ssb;
469 ssb = (msg->notify.nm_etype & NM_REVENT) ?
470 &msg->base.nm_so->so_rcv :
471 &msg->base.nm_so->so_snd;
474 * Reply immediately if the event has occured, otherwise queue the
477 * NOTE: Socket can change if this is an accept predicate so cache
480 tok = lwkt_token_pool_lookup(msg->base.nm_so);
482 if (msg->notify.nm_predicate(&msg->notify)) {
484 lwkt_replymsg(&msg->base.lmsg,
485 msg->base.lmsg.ms_error);
487 TAILQ_INSERT_TAIL(&ssb->ssb_kq.ki_mlist, &msg->notify, nm_list);
488 atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
494 * Called by doio when trying to abort a netmsg_so_notify message.
495 * Unlike the other functions this one is dispatched directly by
496 * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
498 * The original message, lmsg, is under the control of the caller and
499 * will not be destroyed until we return so we can safely reference it
500 * in our synchronous abort request.
502 * This part of the abort request occurs on the originating cpu which
503 * means we may race the message flags and the original message may
504 * not even have been processed by the target cpu yet.
507 netmsg_so_notify_doabort(lwkt_msg_t lmsg)
509 struct netmsg_so_notify_abort msg;
511 if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
512 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
513 0, netmsg_so_notify_abort);
514 msg.nm_notifymsg = (void *)lmsg;
515 lwkt_domsg(lmsg->ms_target_port, &msg.base.lmsg, 0);
520 * Predicate requests can be aborted. This function is only called once
521 * and will interlock against processing/reply races (since such races
522 * occur on the same thread that controls the port where the abort is
525 * This part of the abort request occurs on the target cpu. The message
526 * flags must be tested again in case the test that we did on the
527 * originating cpu raced. Since messages are handled in sequence, the
528 * original message will have already been handled by the loop and either
529 * replied to or queued.
531 * We really only need to interlock with MSGF_REPLY (a bit that is set on
532 * our cpu when we reply). Note that MSGF_DONE is not set until the
533 * reply reaches the originating cpu. Test both bits anyway.
536 netmsg_so_notify_abort(netmsg_t msg)
538 struct netmsg_so_notify_abort *abrtmsg = &msg->notify_abort;
539 struct netmsg_so_notify *nmsg = abrtmsg->nm_notifymsg;
540 struct signalsockbuf *ssb;
543 * The original notify message is not destroyed until after the
544 * abort request is returned, so we can check its state.
546 lwkt_getpooltoken(nmsg->base.nm_so);
547 if ((nmsg->base.lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
548 ssb = (nmsg->nm_etype & NM_REVENT) ?
549 &nmsg->base.nm_so->so_rcv :
550 &nmsg->base.nm_so->so_snd;
551 TAILQ_REMOVE(&ssb->ssb_kq.ki_mlist, nmsg, nm_list);
552 lwkt_relpooltoken(nmsg->base.nm_so);
553 lwkt_replymsg(&nmsg->base.lmsg, EINTR);
555 lwkt_relpooltoken(nmsg->base.nm_so);
559 * Reply to the abort message
561 lwkt_replymsg(&abrtmsg->base.lmsg, 0);