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