2 * Copyright (c) 2003 Jeffrey Hsu
3 * Copyright (c) 2003 Jonathan Lemon
4 * Copyright (c) 2003 Matthew Dillon
6 * $DragonFly: src/sys/net/netisr.c,v 1.17 2004/06/07 07:01:36 dillon Exp $
10 #include <sys/systm.h>
11 #include <sys/kernel.h>
12 #include <sys/malloc.h>
13 #include <sys/msgport.h>
15 #include <sys/interrupt.h>
16 #include <sys/socket.h>
17 #include <sys/sysctl.h>
19 #include <net/if_var.h>
20 #include <net/netisr.h>
21 #include <machine/cpufunc.h>
22 #include <machine/ipl.h>
24 #include <sys/thread2.h>
25 #include <sys/msgport2.h>
27 static struct netisr netisrs[NETISR_MAX];
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;
35 * netisr_afree_rport replymsg function, only used to handle async
36 * messages which the sender has abandoned to their fate.
39 netisr_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
45 * We must construct a custom putport function (which runs in the context
46 * of the message originator)
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.
54 * note: ms_target_port does not need to be set when returning a synchronous
58 netmsg_put_port(lwkt_port_t port, lwkt_msg_t lmsg)
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");
68 return(lwkt_default_putport(port, lmsg));
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.
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
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.
88 netmsg_sync_putport(lwkt_port_t port, lwkt_msg_t lmsg)
92 lmsg->ms_flags &= ~MSGF_DONE;
93 lmsg->ms_target_port = port; /* required for abort */
94 error = lmsg->ms_cmd.cm_func(lmsg);
96 error = lwkt_waitmsg(lmsg);
98 lmsg->ms_flags |= MSGF_DONE;
103 netmsg_sync_abortport(lwkt_port_t port, lwkt_msg_t lmsg)
105 lmsg->ms_abort_port = lmsg->ms_reply_port;
106 lmsg->ms_flags |= MSGF_ABORTED;
107 lmsg->ms_abort.cm_func(lmsg);
116 * Create default per-cpu threads for generic protocol handling.
118 for (i = 0; i < ncpus; ++i) {
119 lwkt_create(netmsg_service_loop, NULL, NULL, &netisr_cpu[i], 0, i,
121 netisr_cpu[i].td_msgport.mp_putport = netmsg_put_port;
125 * The netisr_afree_rport is a special reply port which automatically
126 * frees the replied message.
128 lwkt_initport(&netisr_afree_rport, NULL);
129 netisr_afree_rport.mp_replyport = netisr_autofree_reply;
132 * The netisr_syncport is a special port which executes the message
133 * synchronously and waits for it if EASYNC is returned.
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;
140 SYSINIT(netisr, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, netisr_init, NULL);
143 netmsg_service_loop(void *arg)
147 while ((msg = lwkt_waitport(&curthread->td_msgport, NULL)))
148 msg->nm_lmsg.ms_cmd.cm_func(&msg->nm_lmsg);
152 * Call the netisr directly.
153 * Queueing may be done in the msg port layer at its discretion.
156 netisr_dispatch(int num, struct mbuf *m)
158 /* just queue it for now XXX JH */
159 netisr_queue(num, m);
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.
168 netisr_queue(int num, struct mbuf *m)
171 struct netmsg_packet *pmsg;
174 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
175 ("netisr_queue: bad isr %d", num));
178 if (ni->ni_handler == NULL) {
179 printf("netisr_queue: unregistered isr %d\n", num);
183 if (!(port = ni->ni_mport(m)))
186 /* use better message allocation system with limits later XXX JH */
187 pmsg = malloc(sizeof(struct netmsg_packet), M_LWKTMSG, M_WAITOK);
189 lwkt_initmsg(&pmsg->nm_lmsg, &netisr_afree_rport, 0,
190 lwkt_cmd_func((void *)ni->ni_handler), lwkt_cmd_op_none);
192 lwkt_sendmsg(port, &pmsg->nm_lmsg);
197 netisr_register(int num, lwkt_portfn_t mportfn, netisr_fn_t handler)
199 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
200 ("netisr_register: bad isr %d", num));
202 netisrs[num].ni_mport = mportfn;
203 netisrs[num].ni_handler = handler;
207 netisr_unregister(int num)
209 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
210 ("unregister_netisr: bad isr number: %d\n", num));
217 * Return message port for default handler thread on CPU 0.
220 cpu0_portfn(struct mbuf *m)
222 return (&netisr_cpu[0].td_msgport);
227 cpu0_soport(struct socket *so __unused, struct sockaddr *nam __unused,
230 return (&netisr_cpu[0].td_msgport);
234 sync_soport(struct socket *so __unused, struct sockaddr *nam __unused,
237 return (&netisr_sync_port);
241 * This function is used to call the netisr handler from the appropriate
242 * netisr thread for polling and other purposes.
247 struct netisr *ni = &netisrs[num];
249 lwkt_port_t port = &netisr_cpu[0].td_msgport;
251 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
252 ("schednetisr: bad isr %d", num));
254 pmsg = malloc(sizeof(struct netmsg), M_LWKTMSG, M_NOWAIT);
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);