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.13 2004/04/20 01:52:26 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;
34 * netisr_afree_rport replymsg function, only used to handle async
35 * messages which the sender has abandoned to their fate.
38 netisr_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
48 /* Create default per-cpu threads for generic protocol handling. */
49 for (i = 0; i < ncpus; ++i) {
50 lwkt_create(netmsg_service_loop, NULL, NULL, &netisr_cpu[i], 0, i,
53 lwkt_initport(&netisr_afree_rport, NULL);
54 netisr_afree_rport.mp_replyport = netisr_autofree_reply;
57 SYSINIT(netisr, SI_SUB_PROTO_BEGIN, SI_ORDER_FIRST, netisr_init, NULL);
60 netmsg_service_loop(void *arg)
64 while ((msg = lwkt_waitport(&curthread->td_msgport, NULL)))
65 msg->nm_lmsg.ms_cmd.cm_func(&msg->nm_lmsg);
69 * Call the netisr directly.
70 * Queueing may be done in the msg port layer at its discretion.
73 netisr_dispatch(int num, struct mbuf *m)
75 /* just queue it for now XXX JH */
80 * Same as netisr_dispatch(), but always queue.
81 * This is either used in places where we are not confident that
82 * direct dispatch is possible, or where queueing is required.
85 netisr_queue(int num, struct mbuf *m)
88 struct netmsg_packet *pmsg;
91 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
92 ("netisr_queue: bad isr %d", num));
95 if (ni->ni_handler == NULL) {
96 printf("netisr_queue: unregistered isr %d\n", num);
100 if (!(port = ni->ni_mport(m)))
103 /* use better message allocation system with limits later XXX JH */
104 pmsg = malloc(sizeof(struct netmsg_packet), M_LWKTMSG, M_WAITOK);
106 lwkt_initmsg(&pmsg->nm_lmsg, &netisr_afree_rport, 0,
107 lwkt_cmd_func((void *)ni->ni_handler), lwkt_cmd_op_none);
109 lwkt_sendmsg(port, &pmsg->nm_lmsg);
114 netisr_register(int num, lwkt_portfn_t mportfn, netisr_fn_t handler)
116 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
117 ("netisr_register: bad isr %d", num));
119 netisrs[num].ni_mport = mportfn;
120 netisrs[num].ni_handler = handler;
124 netisr_unregister(int num)
126 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
127 ("unregister_netisr: bad isr number: %d\n", num));
134 * Return message port for default handler thread on CPU 0.
137 cpu0_portfn(struct mbuf *m)
139 return (&netisr_cpu[0].td_msgport);
144 cpu0_soport(struct socket *so __unused, struct sockaddr *nam __unused)
146 return (&netisr_cpu[0].td_msgport);
150 * This function is used to call the netisr handler from the appropriate
151 * netisr thread for polling and other purposes.
156 struct netisr *ni = &netisrs[num];
158 lwkt_port_t port = &netisr_cpu[0].td_msgport;
160 KASSERT((num > 0 && num <= (sizeof(netisrs)/sizeof(netisrs[0]))),
161 ("schednetisr: bad isr %d", num));
163 if (!(pmsg = malloc(sizeof(struct netmsg), M_LWKTMSG, M_NOWAIT)))
166 lwkt_initmsg(&pmsg->nm_lmsg, &netisr_afree_rport, 0,
167 lwkt_cmd_func((void *)ni->ni_handler), lwkt_cmd_op_none);
168 lwkt_sendmsg(port, &pmsg->nm_lmsg);