2 * Copyright (c) 2003 Jeffrey Hsu
5 * $DragonFly: src/sys/netinet/ip_demux.c,v 1.15 2004/04/07 17:01:25 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>
17 #include <sys/globaldata.h>
20 #include <net/netisr.h>
22 #include <netinet/in_systm.h>
23 #include <netinet/in.h>
24 #include <netinet/in_var.h>
25 #include <netinet/in_pcb.h>
26 #include <netinet/ip.h>
27 #include <netinet/ip_var.h>
28 #include <netinet/tcp.h>
29 #include <netinet/tcpip.h>
30 #include <netinet/tcp_var.h>
31 #include <netinet/udp.h>
32 #include <netinet/udp_var.h>
34 extern struct thread netisr_cpu[];
36 static struct thread tcp_thread[MAXCPU];
37 static struct thread udp_thread[MAXCPU];
40 INP_MPORT_HASH(in_addr_t faddr, in_addr_t laddr,
41 in_port_t fport, in_port_t lport)
44 * Use low order bytes.
47 #if (BYTE_ORDER == LITTLE_ENDIAN)
48 KASSERT(ncpus2 < 256, ("need different hash function")); /* XXX JH */
49 return (((faddr >> 24) ^ (fport >> 8) ^ (laddr >> 24) ^ (lport >> 8)) &
52 return ((faddr ^ fport ^ laddr ^ lport) & ncpus2_mask);
57 * Map a packet to a protocol processing thread.
60 ip_mport(struct mbuf *m)
66 int thoff; /* TCP data offset */
70 if (m->m_pkthdr.len < sizeof(struct ip)) {
71 ipstat.ips_tooshort++;
75 if (m->m_len < sizeof(struct ip) &&
76 (m = m_pullup(m, sizeof(struct ip))) == NULL) {
77 ipstat.ips_toosmall++;
81 ip = mtod(m, struct ip *);
84 * XXX generic packet handling defrag on CPU 0 for now.
86 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK))
87 return (&netisr_cpu[0].td_msgport);
89 iphlen = ip->ip_hl << 2;
90 if (iphlen < sizeof(struct ip)) { /* minimum header length */
97 if (m->m_len < iphlen + sizeof(struct tcphdr) &&
98 (m = m_pullup(m, iphlen + sizeof(struct tcphdr))) == NULL) {
99 tcpstat.tcps_rcvshort++;
102 th = (struct tcphdr *)((caddr_t)ip + iphlen);
103 thoff = th->th_off << 2;
104 if (thoff < sizeof(struct tcphdr) ||
105 thoff > ntohs(ip->ip_len)) {
106 tcpstat.tcps_rcvbadoff++;
109 if (m->m_len < iphlen + thoff) {
110 m = m_pullup(m, iphlen + thoff);
112 tcpstat.tcps_rcvshort++;
115 ip = mtod(m, struct ip *);
116 th = (struct tcphdr *)((caddr_t)ip + iphlen);
119 cpu = INP_MPORT_HASH(ip->ip_src.s_addr, ip->ip_dst.s_addr,
120 th->th_sport, th->th_dport);
121 port = &tcp_thread[cpu].td_msgport;
124 if (m->m_len < iphlen + sizeof(struct udphdr)) {
125 m = m_pullup(m, iphlen + sizeof(struct udphdr));
127 udpstat.udps_hdrops++;
130 ip = mtod(m, struct ip *);
132 uh = (struct udphdr *)((caddr_t)ip + iphlen);
134 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
135 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
138 cpu = INP_MPORT_HASH(ip->ip_src.s_addr,
139 ip->ip_dst.s_addr, uh->uh_sport, uh->uh_dport);
141 port = &udp_thread[cpu].td_msgport;
144 if (m->m_len < iphlen && (m = m_pullup(m, iphlen)) == NULL) {
145 ipstat.ips_badhlen++;
148 port = &netisr_cpu[0].td_msgport;
151 KKASSERT(port->mp_putport != NULL);
157 * Map a TCP socket to a protocol processing thread.
160 tcp_soport(struct socket *so, struct sockaddr *nam)
165 * The following processing all take place on Protocol Thread 0:
166 * only bind() and connect() have a non-null nam parameter
167 * attach() has a null socket parameter
168 * Fast and slow timeouts pass in two NULLs
170 if (nam != NULL || so == NULL)
171 return (&tcp_thread[0].td_msgport);
174 * Already bound and connected. For TCP connections, the
175 * (faddr, fport, laddr, lport) association cannot change now.
177 * Note: T/TCP code needs some reorganization to fit into
181 if (!inp) /* connection reset by peer */
182 return (&tcp_thread[0].td_msgport);
185 * Rely on type-stable memory and check in protocol handler
186 * to fix race condition here w/ deallocation of inp. XXX JH
189 return (&tcp_thread[INP_MPORT_HASH(inp->inp_faddr.s_addr,
190 inp->inp_laddr.s_addr, inp->inp_fport, inp->inp_lport)].td_msgport);
194 * Map a UDP socket to a protocol processing thread.
197 udp_soport(struct socket *so, struct sockaddr *nam)
202 * The following processing all take place on Protocol Thread 0:
203 * only bind() and connect() have a non-null nam parameter
204 * attach() has a null socket parameter
205 * Fast and slow timeouts pass in two NULLs
207 if (nam != NULL || so == NULL)
208 return (&udp_thread[0].td_msgport);
212 if (IN_MULTICAST(ntohl(inp->inp_laddr.s_addr)))
213 return (&udp_thread[0].td_msgport);
216 * Rely on type-stable memory and check in protocol handler
217 * to fix race condition here w/ deallocation of inp. XXX JH
220 return (&udp_thread[INP_MPORT_HASH(inp->inp_faddr.s_addr,
221 inp->inp_laddr.s_addr, inp->inp_fport, inp->inp_lport)].td_msgport);
225 * Map a network address to a processor.
228 tcp_addrcpu(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
230 return (INP_MPORT_HASH(faddr, laddr, fport, lport));
234 udp_addrcpu(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
236 if (IN_MULTICAST(ntohl(laddr)))
239 return (INP_MPORT_HASH(faddr, laddr, fport, lport));
243 * Return LWKT port for cpu.
248 return (&tcp_thread[cpu].td_msgport);
252 tcp_thread_init(void)
256 for (cpu = 0; cpu < ncpus2; cpu++) {
257 lwkt_create(netmsg_service_loop, NULL, NULL,
258 &tcp_thread[cpu], 0, cpu, "tcp_thread %d", cpu);
263 udp_thread_init(void)
267 for (cpu = 0; cpu < ncpus2; cpu++) {
268 lwkt_create(netmsg_service_loop, NULL, NULL,
269 &udp_thread[cpu], 0, cpu, "udp_thread %d", cpu);