2 * Copyright (c) 2003 Jeffrey Hsu
5 * $DragonFly: src/sys/netinet/ip_demux.c,v 1.1 2003/11/08 07:57:51 dillon Exp $
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/kernel.h>
13 #include <sys/socket.h>
14 #include <sys/socketvar.h>
15 #include <sys/thread.h>
16 #include <sys/sysctl.h>
19 #include <net/netisr.h>
21 #include <netinet/in_systm.h>
22 #include <netinet/in.h>
23 #include <netinet/in_var.h>
24 #include <netinet/in_pcb.h>
25 #include <netinet/ip.h>
26 #include <netinet/ip_var.h>
27 #include <netinet/tcp.h>
28 #include <netinet/tcpip.h>
29 #include <netinet/tcp_var.h>
30 #include <netinet/udp.h>
31 #include <netinet/udp_var.h>
33 extern struct thread netisr_cpu[];
35 static struct thread tcp_thread[MAXCPU];
36 static struct thread udp_thread[MAXCPU];
39 * XXX when we remove the MP lock changes to this must be master-synchronized
41 static int ip_mthread_enable = 1;
42 SYSCTL_INT(_net_inet_ip, OID_AUTO, mthread_enable, CTLFLAG_RW,
43 &ip_mthread_enable, 0, "");
46 INP_MPORT_HASH(in_addr_t src, in_addr_t dst, int sport, int dport)
50 hv = (int)ntohl(src) ^ (int)ntohl(dst) ^
51 (int)ntohs(sport) ^ (int)ntohs(dport);
56 ip_mport(struct mbuf *m)
58 struct ip *ip = mtod(m, struct ip *);
64 if (ip_mthread_enable == 0)
65 return (&netisr_cpu[0].td_msgport);
67 if (m->m_len < sizeof(struct ip) &&
68 (m = m_pullup(m, sizeof(struct ip))) == NULL) {
69 ipstat.ips_toosmall++;
74 * XXX generic packet handling defrag on CPU 0 for now.
76 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK))
77 return (&netisr_cpu[0].td_msgport);
79 hlen = ip->ip_hl << 2;
83 if (m->m_len < sizeof(struct tcpiphdr) &&
84 (m = m_pullup(m, sizeof(struct tcpiphdr))) == NULL) {
85 tcpstat.tcps_rcvshort++;
89 th = (struct tcphdr *)((caddr_t)ip + hlen);
90 port = &tcp_thread[INP_MPORT_HASH(ip->ip_src.s_addr,
91 ip->ip_dst.s_addr, th->th_sport, th->th_dport)].td_msgport;
94 if (m->m_len < hlen + sizeof(struct udphdr) &&
95 (m = m_pullup(m, hlen + sizeof(struct udphdr))) == NULL) {
96 udpstat.udps_hdrops++;
100 uh = (struct udphdr *)((caddr_t)ip + hlen);
101 port = &udp_thread[INP_MPORT_HASH(ip->ip_src.s_addr,
102 ip->ip_dst.s_addr, uh->uh_sport, uh->uh_dport)].td_msgport;
105 port = &netisr_cpu[0].td_msgport;
113 tcp_soport(struct socket *so)
115 struct inpcb *inp = sotoinpcb(so);
117 return (&tcp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
118 inp->inp_faddr.s_addr, inp->inp_lport, inp->inp_fport)].td_msgport);
122 udp_soport(struct socket *so)
124 struct inpcb *inp = sotoinpcb(so);
126 return (&udp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
127 inp->inp_faddr.s_addr, inp->inp_lport, inp->inp_fport)].td_msgport);
131 tcp_thread_init(void)
135 for (cpu = 0; cpu < ncpus; cpu++) {
136 lwkt_create(netmsg_service_loop, NULL, NULL,
137 &tcp_thread[cpu], 0, cpu, "tcp_thread %d", cpu);
142 udp_thread_init(void)
146 for (cpu = 0; cpu < ncpus; cpu++) {
147 lwkt_create(netmsg_service_loop, NULL, NULL,
148 &udp_thread[cpu], 0, cpu, "udp_thread %d", cpu);