Merge branch 'devel'
[dragonfly.git] / sys / kern / uipc_msg.c
1 /*
2  * Copyright (c) 2003, 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2003, 2004 The DragonFly Project.  All rights reserved.
4  * 
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  * 
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
31  * SUCH DAMAGE.
32  *
33  * $DragonFly: src/sys/kern/uipc_msg.c,v 1.26 2008/10/27 02:56:30 sephe Exp $
34  */
35
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>
47 #include <sys/mbuf.h>
48 #include <vm/pmap.h>
49 #include <net/netmsg2.h>
50
51 #include <net/netisr.h>
52 #include <net/netmsg.h>
53
54 /*
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.
57  */
58 void
59 so_pru_abort(struct socket *so)
60 {
61         struct netmsg_pru_abort msg;
62
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);
67 }
68
69 /*
70  * Abort a socket and free it, asynchronously.  Called from
71  * soaborta() only.  soaborta() got a ref on the socket which we must
72  * free on reply.
73  */
74 void
75 so_pru_aborta(struct socket *so)
76 {
77         struct netmsg_pru_abort *msg;
78
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);
83 }
84
85 /*
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.
88  */
89 void
90 so_pru_abort_oncpu(struct socket *so)
91 {
92         struct netmsg_pru_abort msg;
93         netisr_fn_t func = so->so_proto->pr_usrreqs->pru_abort;
94
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;
98         func((netmsg_t)&msg);
99         KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
100         sofree(msg.base.nm_so);
101 }
102
103 /*
104  * WARNING!  Synchronous call from user context
105  */
106 int
107 so_pru_accept_direct(struct socket *so, struct sockaddr **nam)
108 {
109         struct netmsg_pru_accept msg;
110         netisr_fn_t func = so->so_proto->pr_usrreqs->pru_accept;
111
112         netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
113         msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
114         msg.base.lmsg.ms_flags |= MSGF_SYNC;
115         msg.nm_nam = nam;
116         func((netmsg_t)&msg);
117         KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
118         return(msg.base.lmsg.ms_error);
119 }
120
121 int
122 so_pru_attach(struct socket *so, int proto, struct pru_attach_info *ai)
123 {
124         struct netmsg_pru_attach msg;
125         int error;
126
127         netmsg_init(&msg.base, so, &curthread->td_msgport,
128                     0, so->so_proto->pr_usrreqs->pru_attach);
129         msg.nm_proto = proto;
130         msg.nm_ai = ai;
131         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
132         return (error);
133 }
134
135 int
136 so_pru_attach_direct(struct socket *so, int proto, struct pru_attach_info *ai)
137 {
138         struct netmsg_pru_attach msg;
139         netisr_fn_t func = so->so_proto->pr_usrreqs->pru_attach;
140
141         netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
142         msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
143         msg.base.lmsg.ms_flags |= MSGF_SYNC;
144         msg.nm_proto = proto;
145         msg.nm_ai = ai;
146         func((netmsg_t)&msg);
147         KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
148         return(msg.base.lmsg.ms_error);
149 }
150
151 /*
152  * NOTE: If the target port changes the bind operation will deal with it.
153  */
154 int
155 so_pru_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
156 {
157         struct netmsg_pru_bind msg;
158         int error;
159
160         netmsg_init(&msg.base, so, &curthread->td_msgport,
161                     0, so->so_proto->pr_usrreqs->pru_bind);
162         msg.nm_nam = nam;
163         msg.nm_td = td;         /* used only for prison_ip() */
164         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
165         return (error);
166 }
167
168 int
169 so_pru_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
170 {
171         struct netmsg_pru_connect msg;
172         int error;
173
174         netmsg_init(&msg.base, so, &curthread->td_msgport,
175                     0, so->so_proto->pr_usrreqs->pru_connect);
176         msg.nm_nam = nam;
177         msg.nm_td = td;
178         msg.nm_m = NULL;
179         msg.nm_flags = 0;
180         msg.nm_reconnect = 0;
181         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
182         return (error);
183 }
184
185 int
186 so_pru_connect2(struct socket *so1, struct socket *so2)
187 {
188         struct netmsg_pru_connect2 msg;
189         int error;
190
191         netmsg_init(&msg.base, so1, &curthread->td_msgport,
192                     0, so1->so_proto->pr_usrreqs->pru_connect2);
193         msg.nm_so1 = so1;
194         msg.nm_so2 = so2;
195         error = lwkt_domsg(so1->so_port, &msg.base.lmsg, 0);
196         return (error);
197 }
198
199 /*
200  * WARNING!  Synchronous call from user context.  Control function may do
201  *           copyin/copyout.
202  */
203 int
204 so_pru_control_direct(struct socket *so, u_long cmd, caddr_t data,
205                       struct ifnet *ifp)
206 {
207         struct netmsg_pru_control msg;
208         netisr_fn_t func = so->so_proto->pr_usrreqs->pru_control;
209
210         netmsg_init(&msg.base, so, &netisr_adone_rport, 0, func);
211         msg.base.lmsg.ms_flags &= ~(MSGF_REPLY | MSGF_DONE);
212         msg.base.lmsg.ms_flags |= MSGF_SYNC;
213         msg.nm_cmd = cmd;
214         msg.nm_data = data;
215         msg.nm_ifp = ifp;
216         msg.nm_td = curthread;
217         func((netmsg_t)&msg);
218         KKASSERT(msg.base.lmsg.ms_flags & MSGF_DONE);
219         return(msg.base.lmsg.ms_error);
220 }
221
222 int
223 so_pru_detach(struct socket *so)
224 {
225         struct netmsg_pru_detach msg;
226         int error;
227
228         netmsg_init(&msg.base, so, &curthread->td_msgport,
229                     0, so->so_proto->pr_usrreqs->pru_detach);
230         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
231         return (error);
232 }
233
234 int
235 so_pru_disconnect(struct socket *so)
236 {
237         struct netmsg_pru_disconnect msg;
238         int error;
239
240         netmsg_init(&msg.base, so, &curthread->td_msgport,
241                     0, so->so_proto->pr_usrreqs->pru_disconnect);
242         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
243         return (error);
244 }
245
246 int
247 so_pru_listen(struct socket *so, struct thread *td)
248 {
249         struct netmsg_pru_listen msg;
250         int error;
251
252         netmsg_init(&msg.base, so, &curthread->td_msgport,
253                     0, so->so_proto->pr_usrreqs->pru_listen);
254         msg.nm_td = td;         /* used only for prison_ip() XXX JH */
255         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
256         return (error);
257 }
258
259 int
260 so_pru_peeraddr(struct socket *so, struct sockaddr **nam)
261 {
262         struct netmsg_pru_peeraddr msg;
263         int error;
264
265         netmsg_init(&msg.base, so, &curthread->td_msgport,
266                     0, so->so_proto->pr_usrreqs->pru_peeraddr);
267         msg.nm_nam = nam;
268         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
269         return (error);
270 }
271
272 int
273 so_pru_rcvd(struct socket *so, int flags)
274 {
275         struct netmsg_pru_rcvd msg;
276         int error;
277
278         netmsg_init(&msg.base, so, &curthread->td_msgport,
279                     0, so->so_proto->pr_usrreqs->pru_rcvd);
280         msg.nm_flags = flags;
281         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
282         return (error);
283 }
284
285 int
286 so_pru_rcvoob(struct socket *so, struct mbuf *m, int flags)
287 {
288         struct netmsg_pru_rcvoob msg;
289         int error;
290
291         netmsg_init(&msg.base, so, &curthread->td_msgport,
292                     0, so->so_proto->pr_usrreqs->pru_rcvoob);
293         msg.nm_m = m;
294         msg.nm_flags = flags;
295         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
296         return (error);
297 }
298
299 /*
300  * NOTE: If the target port changes the implied connect will deal with it.
301  */
302 int
303 so_pru_send(struct socket *so, int flags, struct mbuf *m,
304             struct sockaddr *addr, struct mbuf *control, struct thread *td)
305 {
306         struct netmsg_pru_send msg;
307         int error;
308
309         netmsg_init(&msg.base, so, &curthread->td_msgport,
310                     0, so->so_proto->pr_usrreqs->pru_send);
311         msg.nm_flags = flags;
312         msg.nm_m = m;
313         msg.nm_addr = addr;
314         msg.nm_control = control;
315         msg.nm_td = td;
316         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
317         return (error);
318 }
319
320 void
321 so_pru_send_async(struct socket *so, int flags, struct mbuf *m,
322             struct sockaddr *addr, struct mbuf *control, struct thread *td)
323 {
324         struct netmsg_pru_send *msg;
325
326         msg = &m->m_hdr.mh_sndmsg;
327         netmsg_init(&msg->base, so, &netisr_apanic_rport,
328                     0, so->so_proto->pr_usrreqs->pru_send);
329         msg->nm_flags = flags | PRUS_NOREPLY;
330         msg->nm_m = m;
331         msg->nm_addr = addr;
332         msg->nm_control = control;
333         msg->nm_td = td;
334         lwkt_sendmsg(so->so_port, &msg->base.lmsg);
335 }
336
337 int
338 so_pru_sense(struct socket *so, struct stat *sb)
339 {
340         struct netmsg_pru_sense msg;
341         int error;
342
343         netmsg_init(&msg.base, so, &curthread->td_msgport,
344                     0, so->so_proto->pr_usrreqs->pru_sense);
345         msg.nm_stat = sb;
346         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
347         return (error);
348 }
349
350 int
351 so_pru_shutdown(struct socket *so)
352 {
353         struct netmsg_pru_shutdown msg;
354         int error;
355
356         netmsg_init(&msg.base, so, &curthread->td_msgport,
357                     0, so->so_proto->pr_usrreqs->pru_shutdown);
358         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
359         return (error);
360 }
361
362 int
363 so_pru_sockaddr(struct socket *so, struct sockaddr **nam)
364 {
365         struct netmsg_pru_sockaddr msg;
366         int error;
367
368         netmsg_init(&msg.base, so, &curthread->td_msgport,
369                     0, so->so_proto->pr_usrreqs->pru_sockaddr);
370         msg.nm_nam = nam;
371         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
372         return (error);
373 }
374
375 int
376 so_pr_ctloutput(struct socket *so, struct sockopt *sopt)
377 {
378         struct netmsg_pr_ctloutput msg;
379         int error;
380
381         KKASSERT(!sopt->sopt_val || kva_p(sopt->sopt_val));
382         netmsg_init(&msg.base, so, &curthread->td_msgport,
383                     0, so->so_proto->pr_ctloutput);
384         msg.nm_sopt = sopt;
385         error = lwkt_domsg(so->so_port, &msg.base.lmsg, 0);
386         return (error);
387 }
388
389 /*
390  * Protocol control input, typically via icmp.
391  *
392  * If the protocol pr_ctlport is not NULL we call it to figure out the
393  * protocol port.  If NULL is returned we can just return, otherwise
394  * we issue a netmsg to call pr_ctlinput in the proper thread.
395  *
396  * This must be done synchronously as arg and/or extra may point to
397  * temporary data.
398  */
399 void
400 so_pru_ctlinput(struct protosw *pr, int cmd, struct sockaddr *arg, void *extra)
401 {
402         struct netmsg_pru_ctlinput msg;
403         lwkt_port_t port;
404
405         if (pr->pr_ctlport == NULL)
406                 return;
407         KKASSERT(pr->pr_ctlinput != NULL);
408         port = pr->pr_ctlport(cmd, arg, extra);
409         if (port == NULL)
410                 return;
411         netmsg_init(&msg.base, NULL, &curthread->td_msgport,
412                     0, pr->pr_ctlinput);
413         msg.nm_cmd = cmd;
414         msg.nm_arg = arg;
415         msg.nm_extra = extra;
416         lwkt_domsg(port, &msg.base.lmsg, 0);
417 }
418
419 /*
420  * If we convert all the protosw pr_ functions for all the protocols
421  * to take a message directly, this layer can go away.  For the moment
422  * our dispatcher ignores the return value, but since we are handling
423  * the replymsg ourselves we return EASYNC by convention.
424  */
425
426 /*
427  * Handle a predicate event request.  This function is only called once
428  * when the predicate message queueing request is received.
429  */
430 void
431 netmsg_so_notify(netmsg_t msg)
432 {
433         struct signalsockbuf *ssb;
434
435         ssb = (msg->notify.nm_etype & NM_REVENT) ?
436                         &msg->base.nm_so->so_rcv :
437                         &msg->base.nm_so->so_snd;
438
439         /*
440          * Reply immediately if the event has occured, otherwise queue the
441          * request.
442          */
443         if (msg->notify.nm_predicate(&msg->notify)) {
444                 lwkt_replymsg(&msg->base.lmsg,
445                               msg->base.lmsg.ms_error);
446         } else {
447                 lwkt_gettoken(&kq_token);
448                 TAILQ_INSERT_TAIL(&ssb->ssb_kq.ki_mlist, &msg->notify, nm_list);
449                 atomic_set_int(&ssb->ssb_flags, SSB_MEVENT);
450                 lwkt_reltoken(&kq_token);
451         }
452 }
453
454 /*
455  * Called by doio when trying to abort a netmsg_so_notify message.
456  * Unlike the other functions this one is dispatched directly by
457  * the LWKT subsystem, so it takes a lwkt_msg_t as an argument.
458  *
459  * The original message, lmsg, is under the control of the caller and
460  * will not be destroyed until we return so we can safely reference it
461  * in our synchronous abort request.
462  *
463  * This part of the abort request occurs on the originating cpu which
464  * means we may race the message flags and the original message may
465  * not even have been processed by the target cpu yet.
466  */
467 void
468 netmsg_so_notify_doabort(lwkt_msg_t lmsg)
469 {
470         struct netmsg_so_notify_abort msg;
471
472         if ((lmsg->ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
473                 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
474                             0, netmsg_so_notify_abort);
475                 msg.nm_notifymsg = (void *)lmsg;
476                 lwkt_domsg(lmsg->ms_target_port, &msg.base.lmsg, 0);
477         }
478 }
479
480 /*
481  * Predicate requests can be aborted.  This function is only called once
482  * and will interlock against processing/reply races (since such races
483  * occur on the same thread that controls the port where the abort is 
484  * requeued).
485  *
486  * This part of the abort request occurs on the target cpu.  The message
487  * flags must be tested again in case the test that we did on the
488  * originating cpu raced.  Since messages are handled in sequence, the
489  * original message will have already been handled by the loop and either
490  * replied to or queued.
491  *
492  * We really only need to interlock with MSGF_REPLY (a bit that is set on
493  * our cpu when we reply).  Note that MSGF_DONE is not set until the
494  * reply reaches the originating cpu.  Test both bits anyway.
495  */
496 void
497 netmsg_so_notify_abort(netmsg_t msg)
498 {
499         struct netmsg_so_notify_abort *abrtmsg = &msg->notify_abort;
500         struct netmsg_so_notify *nmsg = abrtmsg->nm_notifymsg;
501         struct signalsockbuf *ssb;
502
503         /*
504          * The original notify message is not destroyed until after the
505          * abort request is returned, so we can check its state.
506          */
507         if ((nmsg->base.lmsg.ms_flags & (MSGF_DONE | MSGF_REPLY)) == 0) {
508                 ssb = (nmsg->nm_etype & NM_REVENT) ?
509                                 &nmsg->base.nm_so->so_rcv :
510                                 &nmsg->base.nm_so->so_snd;
511                 lwkt_gettoken(&kq_token);
512                 TAILQ_REMOVE(&ssb->ssb_kq.ki_mlist, nmsg, nm_list);
513                 lwkt_reltoken(&kq_token);
514                 lwkt_replymsg(&nmsg->base.lmsg, EINTR);
515         }
516
517         /*
518          * Reply to the abort message
519          */
520         lwkt_replymsg(&abrtmsg->base.lmsg, 0);
521 }