File:
[DragonFly] /
src /
sys /
netinet /
ip_demux.c
Revision
1.3:
download - view:
text,
annotated -
select for diffs
Thu Nov 20 06:05:31 2003 UTC (9 years, 6 months ago) by
dillon
Branches:
MAIN
CVS tags:
HEAD
This is a major cleanup of the LWKT message port code. The messaging code
is getting closer to being directly useable by userland. With these changes
message/port operations are now far better abstracted then they were before.
* Stale fields have been removed from struct lwkt_msg.
* lwkt_abortmsg() has been revamped to make it easier to support.
* lwkt_waitmsg has been converted to a port function.
* mp_*port() function fields have been renamed for better readability.
* ms_cleanupmsg has been removed from struct lwkt_msg.
* Union sysmsg is now struct sysmsg.
* A copyout function has been added to struct sysmsg.
* The system calls have been regenerated.
/*
* Copyright (c) 2003 Jeffrey Hsu
* All rights reserved.
*
* $DragonFly: src/sys/netinet/ip_demux.c,v 1.3 2003/11/20 06:05:31 dillon Exp $
*/
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/thread.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/netisr.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/in_pcb.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/tcpip.h>
#include <netinet/tcp_var.h>
#include <netinet/udp.h>
#include <netinet/udp_var.h>
extern struct thread netisr_cpu[];
static struct thread tcp_thread[MAXCPU];
static struct thread udp_thread[MAXCPU];
/*
* XXX when we remove the MP lock changes to this must be master-synchronized
*/
static int ip_mthread_enable = 0;
SYSCTL_INT(_net_inet_ip, OID_AUTO, mthread_enable, CTLFLAG_RW,
&ip_mthread_enable, 0, "");
static int
INP_MPORT_HASH(in_addr_t src, in_addr_t dst, int sport, int dport)
{
int hv;
hv = (int)ntohl(src) ^ (int)ntohl(dst) ^
(int)ntohs(sport) ^ (int)ntohs(dport);
return((hv & 0xFFFF) % ncpus);
}
lwkt_port_t
ip_mport(struct mbuf *m)
{
struct ip *ip = mtod(m, struct ip *);
int hlen;
struct tcphdr *th;
struct udphdr *uh;
lwkt_port_t port;
if (ip_mthread_enable == 0)
return (&netisr_cpu[0].td_msgport);
if (m->m_len < sizeof(struct ip) &&
(m = m_pullup(m, sizeof(struct ip))) == NULL) {
ipstat.ips_toosmall++;
return (NULL);
}
/*
* XXX generic packet handling defrag on CPU 0 for now.
*/
if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK))
return (&netisr_cpu[0].td_msgport);
hlen = ip->ip_hl << 2;
switch (ip->ip_p) {
case IPPROTO_TCP:
if (m->m_len < sizeof(struct tcpiphdr) &&
(m = m_pullup(m, sizeof(struct tcpiphdr))) == NULL) {
tcpstat.tcps_rcvshort++;
return (NULL);
}
th = (struct tcphdr *)((caddr_t)ip + hlen);
port = &tcp_thread[INP_MPORT_HASH(ip->ip_src.s_addr,
ip->ip_dst.s_addr, th->th_sport, th->th_dport)].td_msgport;
break;
case IPPROTO_UDP:
if (m->m_len < hlen + sizeof(struct udphdr) &&
(m = m_pullup(m, hlen + sizeof(struct udphdr))) == NULL) {
udpstat.udps_hdrops++;
return (NULL);
}
uh = (struct udphdr *)((caddr_t)ip + hlen);
port = &udp_thread[INP_MPORT_HASH(ip->ip_src.s_addr,
ip->ip_dst.s_addr, uh->uh_sport, uh->uh_dport)].td_msgport;
break;
default:
port = &netisr_cpu[0].td_msgport;
break;
}
KKASSERT(port->mp_putport != NULL);
return (port);
}
lwkt_port_t
tcp_soport(struct socket *so)
{
struct inpcb *inp = sotoinpcb(so);
return (&tcp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
inp->inp_faddr.s_addr, inp->inp_lport, inp->inp_fport)].td_msgport);
}
lwkt_port_t
udp_soport(struct socket *so)
{
struct inpcb *inp = sotoinpcb(so);
return (&udp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
inp->inp_faddr.s_addr, inp->inp_lport, inp->inp_fport)].td_msgport);
}
void
tcp_thread_init(void)
{
int cpu;
for (cpu = 0; cpu < ncpus; cpu++) {
lwkt_create(netmsg_service_loop, NULL, NULL,
&tcp_thread[cpu], 0, cpu, "tcp_thread %d", cpu);
}
}
void
udp_thread_init(void)
{
int cpu;
for (cpu = 0; cpu < ncpus; cpu++) {
lwkt_create(netmsg_service_loop, NULL, NULL,
&udp_thread[cpu], 0, cpu, "udp_thread %d", cpu);
}
}