2 * Copyright (c) 2003 Jeffrey Hsu
5 * $DragonFly: src/sys/netinet/ip_demux.c,v 1.20 2004/04/23 10:21:08 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>
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, int req)
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 tcp_addrport(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
196 return (&tcp_thread[tcp_addrcpu(faddr, fport,
197 laddr, lport)].td_msgport);
201 * Map a UDP socket to a protocol processing thread.
204 udp_soport(struct socket *so, struct sockaddr *nam, int req)
209 * The following processing all take place on Protocol Thread 0:
210 * only bind() and connect() have a non-null nam parameter
211 * attach() has a null socket parameter
212 * Fast and slow timeouts pass in two NULLs
214 if (nam != NULL || so == NULL)
215 return (&udp_thread[0].td_msgport);
219 if (IN_MULTICAST(ntohl(inp->inp_laddr.s_addr)))
220 return (&udp_thread[0].td_msgport);
223 * Rely on type-stable memory and check in protocol handler
224 * to fix race condition here w/ deallocation of inp. XXX JH
227 return (&udp_thread[INP_MPORT_HASH(inp->inp_faddr.s_addr,
228 inp->inp_laddr.s_addr, inp->inp_fport, inp->inp_lport)].td_msgport);
232 * Map a network address to a processor.
235 tcp_addrcpu(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
237 return (INP_MPORT_HASH(faddr, laddr, fport, lport));
241 udp_addrcpu(in_addr_t faddr, in_port_t fport, in_addr_t laddr, in_port_t lport)
243 if (IN_MULTICAST(ntohl(laddr)))
246 return (INP_MPORT_HASH(faddr, laddr, fport, lport));
250 * Return LWKT port for cpu.
255 return (&tcp_thread[cpu].td_msgport);
259 * We must construct a custom putport function (which runs in the context
260 * of the message originator)
262 * Our custom putport must check for self-referential messages, which can
263 * occur when the so_upcall routine is called (e.g. nfs). Self referential
264 * messages are executed synchronously. However, we must panic if the message
265 * is not marked DONE on completion because the self-referential case cannot
266 * block without deadlocking.
269 netmsg_put_port(lwkt_port_t port, lwkt_msg_t lmsg)
273 if ((lmsg->ms_flags & MSGF_ASYNC) == 0 && port->mp_td == curthread) {
274 error = lmsg->ms_cmd.cm_func(lmsg);
275 if (error == EASYNC && (lmsg->ms_flags & MSGF_DONE) == 0)
276 panic("netmsg_put_port: self-referential deadlock on netport");
279 return(lwkt_default_putport(port, lmsg));
284 tcp_thread_init(void)
288 for (cpu = 0; cpu < ncpus2; cpu++) {
289 lwkt_create(netmsg_service_loop, NULL, NULL,
290 &tcp_thread[cpu], 0, cpu, "tcp_thread %d", cpu);
291 tcp_thread[cpu].td_msgport.mp_putport = netmsg_put_port;
296 udp_thread_init(void)
300 for (cpu = 0; cpu < ncpus2; cpu++) {
301 lwkt_create(netmsg_service_loop, NULL, NULL,
302 &udp_thread[cpu], 0, cpu, "udp_thread %d", cpu);
303 udp_thread[cpu].td_msgport.mp_putport = netmsg_put_port;