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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/msgport.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/socketops.h>
42 #include <sys/thread.h>
43 #include <sys/thread2.h>
44 #include <sys/msgport2.h>
47 #include <net/netmsg2.h>
49 #include <net/netisr.h>
50 #include <net/netmsg.h>
53 * Abort a socket and free it. Called from soabort() only. soabort()
54 * got a ref on the socket which we must free on reply.
57 so_pru_abort(struct socket *so)
59 struct netmsg_pru_abort msg;
61 netmsg_init(&msg.base, so, &curthread->td_msgport,
62 0, so->so_proto->pr_usrreqs->pru_abort);
63 (void)lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
64 sofree(msg.base.nm_so);
68 * Abort a socket and free it, asynchronously. Called from
69 * soaborta() only. soaborta() got a ref on the socket which we must
73 so_pru_aborta(struct socket *so)
75 struct netmsg_pru_abort *msg;
77 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_WAITOK | M_ZERO);
78 netmsg_init(&msg->base, so, &netisr_afree_free_so_rport,
79 0, so->so_proto->pr_usrreqs->pru_abort);
80 lwkt_sendmsg(so->so_port, &msg->base.lmsg);
84 * Abort a socket and free it. Called from soabort_oncpu() only.
85 * Caller must make sure that the current CPU is inpcb's owner CPU.
88 so_pru_abort_oncpu(struct socket *so)
90 struct netmsg_pru_abort msg;
91 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_abort;
93 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
94 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
95 msg.base.lmsg.ms_flags |= MSGF_SYNC;
97 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
98 sofree(msg.base.nm_so);
102 so_pru_accept(struct socket *so, struct sockaddr **nam)
104 struct netmsg_pru_accept msg;
106 netmsg_init(&msg.base, so, &curthread->td_msgport,
107 0, so->so_proto->pr_usrreqs->pru_accept);
110 return lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
114 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
116 struct netmsg_pru_attach msg;
119 netmsg_init(&msg.base, so, &curthread->td_msgport,
120 0, so->so_proto->pr_usrreqs->pru_attach);
121 msg.nm_proto = proto;
123 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
128 so_pru_attach_direct(struct socket *so, int proto, struct pru_attach_info *ai)
130 struct netmsg_pru_attach msg;
131 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_attach;
133 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
134 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
135 msg.base.lmsg.ms_flags |= MSGF_SYNC;
136 msg.nm_proto = proto;
138 func((netmsg_t)&msg);
139 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
140 return(msg.base.lmsg.ms_error);
144 * NOTE: If the target port changes the bind operation will deal with it.
147 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
149 struct netmsg_pru_bind msg;
152 netmsg_init(&msg.base, so, &curthread->td_msgport,
153 0, so->so_proto->pr_usrreqs->pru_bind);
155 msg.nm_td = td; /* used only for prison_ip() */
156 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
161 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
163 struct netmsg_pru_connect msg;
166 netmsg_init(&msg.base, so, &curthread->td_msgport,
167 0, so->so_proto->pr_usrreqs->pru_connect);
172 msg.nm_reconnect = 0;
173 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
178 so_pru_connect2(struct socket *so1, struct socket *so2)
180 struct netmsg_pru_connect2 msg;
183 netmsg_init(&msg.base, so1, &curthread->td_msgport,
184 0, so1->so_proto->pr_usrreqs->pru_connect2);
187 error = lwkt_domsg(so1->so_port, &msg.base.lmsg, 0);
192 * WARNING! Synchronous call from user context. Control function may do
196 so_pru_control_direct(struct socket *so, u_long cmd, caddr_t data,
199 struct netmsg_pru_control msg;
200 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_control;
202 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
203 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
204 msg.base.lmsg.ms_flags |= MSGF_SYNC;
208 msg.nm_td = curthread;
209 func((netmsg_t)&msg);
210 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
211 return(msg.base.lmsg.ms_error);
215 so_pru_detach(struct socket *so)
217 struct netmsg_pru_detach msg;
220 netmsg_init(&msg.base, so, &curthread->td_msgport,
221 0, so->so_proto->pr_usrreqs->pru_detach);
222 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
227 so_pru_detach_direct(struct socket *so)
229 struct netmsg_pru_detach msg;
230 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_detach;
232 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
233 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
234 msg.base.lmsg.ms_flags |= MSGF_SYNC;
235 func((netmsg_t)&msg);
236 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
240 so_pru_disconnect(struct socket *so)
242 struct netmsg_pru_disconnect msg;
245 netmsg_init(&msg.base, so, &curthread->td_msgport,
246 0, so->so_proto->pr_usrreqs->pru_disconnect);
247 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
252 so_pru_disconnect_direct(struct socket *so)
254 struct netmsg_pru_disconnect msg;
255 netisr_fn_t func = so->so_proto->pr_usrreqs->pru_disconnect;
257 netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
258 msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
259 msg.base.lmsg.ms_flags |= MSGF_SYNC;
260 func((netmsg_t)&msg);
261 KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
265 so_pru_listen(struct socket *so, struct thread *td)
267 struct netmsg_pru_listen msg;
270 netmsg_init(&msg.base, so, &curthread->td_msgport,
271 0, so->so_proto->pr_usrreqs->pru_listen);
272 msg.nm_td = td; /* used only for prison_ip() XXX JH */
273 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
278 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
280 struct netmsg_pru_peeraddr msg;
283 netmsg_init(&msg.base, so, &curthread->td_msgport,
284 0, so->so_proto->pr_usrreqs->pru_peeraddr);
286 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
291 so_pru_rcvd(struct socket *so, int flags)
293 struct netmsg_pru_rcvd msg;
296 netmsg_init(&msg.base, so, &curthread->td_msgport,
297 0, so->so_proto->pr_usrreqs->pru_rcvd);
298 msg.nm_flags = flags;
299 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
304 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
306 struct netmsg_pru_rcvoob msg;
309 netmsg_init(&msg.base, so, &curthread->td_msgport,
310 0, so->so_proto->pr_usrreqs->pru_rcvoob);
312 msg.nm_flags = flags;
313 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
318 * NOTE: If the target port changes the implied connect will deal with it.
321 so_pru_send(struct socket *so, int flags, struct mbuf *m,
322 struct sockaddr *addr, struct mbuf *control, struct thread *td)
324 struct netmsg_pru_send msg;
327 netmsg_init(&msg.base, so, &curthread->td_msgport,
328 0, so->so_proto->pr_usrreqs->pru_send);
329 msg.nm_flags = flags;
332 msg.nm_control = control;
334 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
339 so_pru_sync(struct socket *so)
341 struct netmsg_base msg;
343 netmsg_init(&msg, so, &curthread->td_msgport, 0,
344 netmsg_sync_handler);
345 lwkt_domsg(so->so_port, &msg.lmsg, 0);
349 so_pru_send_async(struct socket *so, int flags, struct mbuf *m,
350 struct sockaddr *addr0, struct mbuf *control, struct thread *td)
352 struct netmsg_pru_send *msg;
353 struct sockaddr *addr = NULL;
355 KASSERT(so->so_proto->pr_flags & PR_ASYNC_SEND,
356 ("async pru_send is not supported"));
358 flags |= PRUS_NOREPLY;
360 addr = kmalloc(addr0->sa_len, M_SONAME, M_WAITOK);
361 memcpy(addr, addr0, addr0->sa_len);
362 flags |= PRUS_FREEADDR;
365 msg = &m->m_hdr.mh_sndmsg;
366 netmsg_init(&msg->base, so, &netisr_apanic_rport,
367 0, so->so_proto->pr_usrreqs->pru_send);
368 msg->nm_flags = flags;
371 msg->nm_control = control;
373 lwkt_sendmsg(so->so_port, &msg->base.lmsg);
377 so_pru_sense(struct socket *so, struct stat *sb)
379 struct netmsg_pru_sense msg;
382 netmsg_init(&msg.base, so, &curthread->td_msgport,
383 0, so->so_proto->pr_usrreqs->pru_sense);
385 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
390 so_pru_shutdown(struct socket *so)
392 struct netmsg_pru_shutdown msg;
395 netmsg_init(&msg.base, so, &curthread->td_msgport,
396 0, so->so_proto->pr_usrreqs->pru_shutdown);
397 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
402 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
404 struct netmsg_pru_sockaddr msg;
407 netmsg_init(&msg.base, so, &curthread->td_msgport,
408 0, so->so_proto->pr_usrreqs->pru_sockaddr);
410 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
415 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
417 struct netmsg_pr_ctloutput msg;
420 KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
421 netmsg_init(&msg.base, so, &curthread->td_msgport,
422 0, so->so_proto->pr_ctloutput);
424 error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
429 * Protocol control input, typically via icmp.
431 * If the protocol pr_ctlport is not NULL we call it to figure out the
432 * protocol port. If NULL is returned we can just return, otherwise
433 * we issue a netmsg to call pr_ctlinput in the proper thread.
435 * This must be done synchronously as arg and/or extra may point to
439 so_pru_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra)
441 struct netmsg_pru_ctlinput msg;
444 if (pr->pr_ctlport == NULL)
446 KKASSERT(pr->pr_ctlinput != NULL);
447 port = pr->pr_ctlport(cmd, arg, extra);
450 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
454 msg.nm_extra = extra;
455 lwkt_domsg(port, &msg.base.lmsg, 0);
459 * If we convert all the protosw pr_ functions for all the protocols
460 * to take a message directly, this layer can go away. For the moment
461 * our dispatcher ignores the return value, but since we are handling
462 * the replymsg ourselves we return EASYNC by convention.
466 * Handle a predicate event request. This function is only called once
467 * when the predicate message queueing request is received.
470 netmsg_so_notify(netmsg_t msg)
472 struct lwkt_token *tok;
473 struct signalsockbuf *ssb;
475 ssb = (msg->notify.nm_etype & NM_REVENT) ?
476 &msg->base.nm_so->so_rcv :
477 &msg->base.nm_so->so_snd;
480 * Reply immediately if the event has occured, otherwise queue the
483 * NOTE: Socket can change if this is an accept predicate so cache
486 tok = lwkt_token_pool_lookup(msg->base.nm_so);
488 if (msg->notify.nm_predicate(&msg->notify)) {
490 lwkt_replymsg(&msg->base.lmsg,
491 msg->base.lmsg.ms_error);
493 TAILQ_INSERT_TAIL(&ssb->ssb_kq.ki_mlist, &msg->notify, nm_list);
494 atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
500 * Called by doio when trying to abort a netmsg_so_notify message.
501 * Unlike the other functions this one is dispatched directly by
502 * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
504 * The original message, lmsg, is under the control of the caller and
505 * will not be destroyed until we return so we can safely reference it
506 * in our synchronous abort request.
508 * This part of the abort request occurs on the originating cpu which
509 * means we may race the message flags and the original message may
510 * not even have been processed by the target cpu yet.
513 netmsg_so_notify_doabort(lwkt_msg_t lmsg)
515 struct netmsg_so_notify_abort msg;
517 if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
518 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
519 0, netmsg_so_notify_abort);
520 msg.nm_notifymsg = (void *)lmsg;
521 lwkt_domsg(lmsg->ms_target_port, &msg.base.lmsg, 0);
526 * Predicate requests can be aborted. This function is only called once
527 * and will interlock against processing/reply races (since such races
528 * occur on the same thread that controls the port where the abort is
531 * This part of the abort request occurs on the target cpu. The message
532 * flags must be tested again in case the test that we did on the
533 * originating cpu raced. Since messages are handled in sequence, the
534 * original message will have already been handled by the loop and either
535 * replied to or queued.
537 * We really only need to interlock with MSGF_REPLY (a bit that is set on
538 * our cpu when we reply). Note that MSGF_DONE is not set until the
539 * reply reaches the originating cpu. Test both bits anyway.
542 netmsg_so_notify_abort(netmsg_t msg)
544 struct netmsg_so_notify_abort *abrtmsg = &msg->notify_abort;
545 struct netmsg_so_notify *nmsg = abrtmsg->nm_notifymsg;
546 struct signalsockbuf *ssb;
549 * The original notify message is not destroyed until after the
550 * abort request is returned, so we can check its state.
552 lwkt_getpooltoken(nmsg->base.nm_so);
553 if ((nmsg->base.lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
554 ssb = (nmsg->nm_etype & NM_REVENT) ?
555 &nmsg->base.nm_so->so_rcv :
556 &nmsg->base.nm_so->so_snd;
557 TAILQ_REMOVE(&ssb->ssb_kq.ki_mlist, nmsg, nm_list);
558 lwkt_relpooltoken(nmsg->base.nm_so);
559 lwkt_replymsg(&nmsg->base.lmsg, EINTR);
561 lwkt_relpooltoken(nmsg->base.nm_so);
565 * Reply to the abort message
567 lwkt_replymsg(&abrtmsg->base.lmsg, 0);