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