2 * Copyright (c) 2003 Jeffrey Hsu
5 * $DragonFly: src/sys/netinet/ip_demux.c,v 1.12 2004/04/01 23:04:50 hsu 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 INP_MPORT_HASH(in_addr_t src, in_addr_t dst, in_port_t sport, in_port_t dport)
42 * Use low order bytes.
45 #if (BYTE_ORDER == LITTLE_ENDIAN)
46 KASSERT(ncpus2 < 256, ("need different hash function")); /* XXX JH */
47 return (((src >> 24) ^ (sport >> 8) ^ (dst >> 24) ^ (dport >> 8)) &
50 return ((src ^ sport ^ dst ^ dport) & ncpus2_mask);
55 * Map a packet to a protocol processing thread.
58 ip_mport(struct mbuf *m)
64 int thoff; /* TCP data offset */
68 if (m->m_pkthdr.len < sizeof(struct ip)) {
69 ipstat.ips_tooshort++;
73 if (m->m_len < sizeof(struct ip) &&
74 (m = m_pullup(m, sizeof(struct ip))) == NULL) {
75 ipstat.ips_toosmall++;
79 ip = mtod(m, struct ip *);
82 * XXX generic packet handling defrag on CPU 0 for now.
84 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK))
85 return (&netisr_cpu[0].td_msgport);
87 iphlen = ip->ip_hl << 2;
88 if (iphlen < sizeof(struct ip)) { /* minimum header length */
95 if (m->m_len < iphlen + sizeof(struct tcphdr) &&
96 (m = m_pullup(m, iphlen + sizeof(struct tcphdr))) == NULL) {
97 tcpstat.tcps_rcvshort++;
100 th = (struct tcphdr *)((caddr_t)ip + iphlen);
101 thoff = th->th_off << 2;
102 if (thoff < sizeof(struct tcphdr) || thoff > ip->ip_len) {
103 tcpstat.tcps_rcvbadoff++;
106 if (m->m_len < iphlen + thoff) {
107 m = m_pullup(m, iphlen + thoff);
109 tcpstat.tcps_rcvshort++;
112 ip = mtod(m, struct ip *);
113 th = (struct tcphdr *)((caddr_t)ip + iphlen);
116 cpu = INP_MPORT_HASH(ip->ip_src.s_addr, ip->ip_dst.s_addr,
117 th->th_sport, th->th_dport);
118 port = &tcp_thread[cpu].td_msgport;
121 if (m->m_len < iphlen + sizeof(struct udphdr)) {
122 m = m_pullup(m, iphlen + sizeof(struct udphdr));
124 udpstat.udps_hdrops++;
127 ip = mtod(m, struct ip *);
129 uh = (struct udphdr *)((caddr_t)ip + iphlen);
131 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
132 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
135 cpu = INP_MPORT_HASH(ip->ip_src.s_addr,
136 ip->ip_dst.s_addr, uh->uh_sport, uh->uh_dport);
138 port = &udp_thread[cpu].td_msgport;
141 if (m->m_len < iphlen && (m = m_pullup(m, iphlen)) == NULL) {
142 ipstat.ips_badhlen++;
145 port = &netisr_cpu[0].td_msgport;
148 KKASSERT(port->mp_putport != NULL);
154 * Map a TCP socket to a protocol processing thread.
157 tcp_soport(struct socket *so, struct sockaddr *nam)
162 * The following processing all take place on Protocol Thread 0:
163 * only bind() and connect() have a non-null nam parameter
164 * attach() has a null socket parameter
165 * Fast and slow timeouts pass in two NULLs
167 if (nam != NULL || so == NULL)
168 return (&tcp_thread[0].td_msgport);
171 * Already bound and connected. For TCP connections, the
172 * (faddr, fport, laddr, lport) association cannot change now.
174 * Note: T/TCP code needs some reorganization to fit into
178 if (!inp) /* connection reset by peer */
179 return (&tcp_thread[0].td_msgport);
182 * Rely on type-stable memory and check in protocol handler
183 * to fix race condition here w/ deallocation of inp. XXX JH
186 return (&tcp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
187 inp->inp_faddr.s_addr, inp->inp_lport,
188 inp->inp_fport)].td_msgport);
192 * Map a UDP socket to a protocol processing thread.
195 udp_soport(struct socket *so, struct sockaddr *nam)
200 * The following processing all take place on Protocol Thread 0:
201 * only bind() and connect() have a non-null nam parameter
202 * attach() has a null socket parameter
203 * Fast and slow timeouts pass in two NULLs
205 if (nam != NULL || so == NULL)
206 return (&udp_thread[0].td_msgport);
210 if (IN_MULTICAST(ntohl(inp->inp_laddr.s_addr)))
211 return (&udp_thread[0].td_msgport);
214 * Rely on type-stable memory and check in protocol handler
215 * to fix race condition here w/ deallocation of inp. XXX JH
218 return (&udp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
219 inp->inp_faddr.s_addr, inp->inp_lport,
220 inp->inp_fport)].td_msgport);
224 * Map a network address to a processor.
227 tcp_addrcpu(in_addr_t src, in_port_t sport, in_addr_t dst, in_port_t dport)
229 return (INP_MPORT_HASH(src, dst, sport, dport));
233 udp_addrcpu(in_addr_t src, in_port_t sport, in_addr_t dst, in_port_t dport)
235 if (IN_MULTICAST(ntohl(dst)))
238 return (INP_MPORT_HASH(src, dst, sport, dport));
242 * Return LWKT port for cpu.
247 return (&tcp_thread[cpu].td_msgport);
251 tcp_thread_init(void)
255 for (cpu = 0; cpu < ncpus2; cpu++) {
256 lwkt_create(netmsg_service_loop, NULL, NULL,
257 &tcp_thread[cpu], 0, cpu, "tcp_thread %d", cpu);
262 udp_thread_init(void)
266 for (cpu = 0; cpu < ncpus2; cpu++) {
267 lwkt_create(netmsg_service_loop, NULL, NULL,
268 &udp_thread[cpu], 0, cpu, "udp_thread %d", cpu);