Merge from vendor branch GCC:
[dragonfly.git] / sys / net / netisr.c
1 /*-
2  * Copyright (c) 2003 Jeffrey Hsu
3  * Copyright (c) 2003 Jonathan Lemon
4  * Copyright (c) 2003 Matthew Dillon
5  *
6  * $DragonFly: src/sys/net/netisr.c,v 1.17 2004/06/07 07:01:36 dillon Exp $
7  */
8
9 #include <sys/param.h>
10 #include <sys/systm.h>
11 #include <sys/kernel.h>
12 #include <sys/malloc.h>
13 #include <sys/msgport.h>
14 #include <sys/proc.h>
15 #include <sys/interrupt.h>
16 #include <sys/socket.h>
17 #include <sys/sysctl.h>
18 #include <net/if.h>
19 #include <net/if_var.h>
20 #include <net/netisr.h>
21 #include <machine/cpufunc.h>
22 #include <machine/ipl.h>
23
24 #include <sys/thread2.h>
25 #include <sys/msgport2.h>
26
27 static struct netisr netisrs[NETISR_MAX];
28
29 /* Per-CPU thread to handle any protocol.  */
30 struct thread netisr_cpu[MAXCPU];
31 lwkt_port netisr_afree_rport;
32 lwkt_port netisr_sync_port;
33
34 /*
35  * netisr_afree_rport replymsg function, only used to handle async
36  * messages which the sender has abandoned to their fate.
37  */
38 static void
39 netisr_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
40 {
41     free(msg, M_LWKTMSG);
42 }
43
44 /*
45  * We must construct a custom putport function (which runs in the context
46  * of the message originator)
47  *
48  * Our custom putport must check for self-referential messages, which can
49  * occur when the so_upcall routine is called (e.g. nfs).  Self referential
50  * messages are executed synchronously.  However, we must panic if the message
51  * is not marked DONE on completion because the self-referential case cannot
52  * block without deadlocking.
53  *
54  * note: ms_target_port does not need to be set when returning a synchronous
55  * error code.
56  */
57 int
58 netmsg_put_port(lwkt_port_t port, lwkt_msg_t lmsg)
59 {
60     int error;
61
62     if ((lmsg->ms_flags & MSGF_ASYNC) == 0 && port->mp_td == curthread) {
63         error = lmsg->ms_cmd.cm_func(lmsg);
64         if (error == EASYNC && (lmsg->ms_flags & MSGF_DONE) == 0)
65             panic("netmsg_put_port: self-referential deadlock on netport");
66         return(error);
67     } else {
68         return(lwkt_default_putport(port, lmsg));
69     }
70 }
71
72 /*
73  * UNIX DOMAIN sockets still have to run their uipc functions synchronously,
74  * because they depend on the user proc context for a number of things 
75  * (like creds) which we have not yet incorporated into the message structure.
76  *
77  * However, we maintain or message/port abstraction.  Having a special 
78  * synchronous port which runs the commands synchronously gives us the
79  * ability to serialize operations in one place later on when we start
80  * removing the BGL.
81  *
82  * We clear MSGF_DONE prior to executing the message in order to close
83  * any potential replymsg races with the flags field.  If a synchronous
84  * result code is returned we set MSGF_DONE again.  MSGF_DONE's flag state
85  * must be correct or the caller will be confused.
86  */
87 static int
88 netmsg_sync_putport(lwkt_port_t port, lwkt_msg_t lmsg)
89 {
90     int error;
91
92     lmsg->ms_flags &= ~MSGF_DONE;
93     lmsg->ms_target_port = port;        /* required for abort */
94     error = lmsg->ms_cmd.cm_func(lmsg);
95     if (error == EASYNC)
96         error = lwkt_waitmsg(lmsg);
97     else
98         lmsg->ms_flags |= MSGF_DONE;
99     return(error);
100 }
101
102 static void
103 netmsg_sync_abortport(lwkt_port_t port, lwkt_msg_t lmsg)
104 {
105     lmsg->ms_abort_port = lmsg->ms_reply_port;
106     lmsg->ms_flags |= MSGF_ABORTED;
107     lmsg->ms_abort.cm_func(lmsg);
108 }
109
110 static void
111 netisr_init(void)
112 {
113     int i;
114
115     /*
116      * Create default per-cpu threads for generic protocol handling.
117      */
118     for (i = 0; i < ncpus; ++i) {
119         lwkt_create(netmsg_service_loop, NULL, NULL, &netisr_cpu[i], 0, i,
120             "netisr_cpu %d", i);
121         netisr_cpu[i].td_msgport.mp_putport = netmsg_put_port;
122     }
123
124     /*
125      * The netisr_afree_rport is a special reply port which automatically
126      * frees the replied message.
127      */
128     lwkt_initport(&netisr_afree_rport, NULL);
129     netisr_afree_rport.mp_replyport = netisr_autofree_reply;
130
131     /*
132      * The netisr_syncport is a special port which executes the message
133      * synchronously and waits for it if EASYNC is returned.
134      */
135     lwkt_initport(&netisr_sync_port, NULL);
136     netisr_sync_port.mp_putport = netmsg_sync_putport;
137     netisr_sync_port.mp_abortport = netmsg_sync_abortport;
138 }
139
140 SYSINIT(netisr, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, netisr_init, NULL);
141
142 void
143 netmsg_service_loop(void *arg)
144 {
145     struct netmsg *msg;
146
147     while ((msg = lwkt_waitport(&curthread->td_msgport, NULL)))
148         msg->nm_lmsg.ms_cmd.cm_func(&msg->nm_lmsg);
149 }
150
151 /*
152  * Call the netisr directly.
153  * Queueing may be done in the msg port layer at its discretion.
154  */
155 void
156 netisr_dispatch(int num, struct mbuf *m)
157 {
158     /* just queue it for now XXX JH */
159     netisr_queue(num, m);
160 }
161
162 /*
163  * Same as netisr_dispatch(), but always queue.
164  * This is either used in places where we are not confident that
165  * direct dispatch is possible, or where queueing is required.
166  */
167 int
168 netisr_queue(int num, struct mbuf *m)
169 {
170     struct netisr *ni;
171     struct netmsg_packet *pmsg;
172     lwkt_port_t port;
173
174     KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
175         ("netisr_queue: bad isr %d", num));
176
177     ni = &netisrs[num];
178     if (ni->ni_handler == NULL) {
179         printf("netisr_queue: unregistered isr %d\n", num);
180         return (EIO);
181     }
182
183     if (!(port = ni->ni_mport(m)))
184         return (EIO);
185
186     /* use better message allocation system with limits later XXX JH */
187     pmsg = malloc(sizeof(struct netmsg_packet), M_LWKTMSG, M_WAITOK);
188
189     lwkt_initmsg(&pmsg->nm_lmsg, &netisr_afree_rport, 0,
190                 lwkt_cmd_func((void *)ni->ni_handler), lwkt_cmd_op_none);
191     pmsg->nm_packet = m;
192     lwkt_sendmsg(port, &pmsg->nm_lmsg);
193     return (0);
194 }
195
196 void
197 netisr_register(int num, lwkt_portfn_t mportfn, netisr_fn_t handler)
198 {
199     KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
200         ("netisr_register: bad isr %d", num));
201
202     netisrs[num].ni_mport = mportfn;
203     netisrs[num].ni_handler = handler;
204 }
205
206 int
207 netisr_unregister(int num)
208 {
209     KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
210         ("unregister_netisr: bad isr number: %d\n", num));
211
212     /* XXX JH */
213     return (0);
214 }
215
216 /*
217  * Return message port for default handler thread on CPU 0.
218  */
219 lwkt_port_t
220 cpu0_portfn(struct mbuf *m)
221 {
222     return (&netisr_cpu[0].td_msgport);
223 }
224
225 /* ARGSUSED */
226 lwkt_port_t
227 cpu0_soport(struct socket *so __unused, struct sockaddr *nam __unused,
228             int req __unused)
229 {
230     return (&netisr_cpu[0].td_msgport);
231 }
232
233 lwkt_port_t
234 sync_soport(struct socket *so __unused, struct sockaddr *nam __unused,
235             int req __unused)
236 {
237     return (&netisr_sync_port);
238 }
239
240 /*
241  * This function is used to call the netisr handler from the appropriate
242  * netisr thread for polling and other purposes.
243  */
244 void
245 schednetisr(int num)
246 {
247     struct netisr *ni = &netisrs[num];
248     struct netmsg *pmsg;
249     lwkt_port_t port = &netisr_cpu[0].td_msgport;
250
251     KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
252         ("schednetisr: bad isr %d", num));
253
254     pmsg = malloc(sizeof(struct netmsg), M_LWKTMSG, M_NOWAIT);
255     if (pmsg) {
256         lwkt_initmsg(&pmsg->nm_lmsg, &netisr_afree_rport, 0,
257                     lwkt_cmd_func((void *)ni->ni_handler), lwkt_cmd_op_none);
258         lwkt_sendmsg(port, &pmsg->nm_lmsg);
259     }
260 }