File:  [DragonFly] / src / sys / netinet / ip_demux.c
Revision 1.13: download - view: text, annotated - select for diffs
Sat Apr 3 22:17:59 2004 UTC (10 years ago) by hsu
Branches: MAIN
CVS tags: HEAD
Fix byte-order.

    1: /*
    2:  * Copyright (c) 2003 Jeffrey Hsu
    3:  * All rights reserved.
    4:  *
    5:  * $DragonFly: src/sys/netinet/ip_demux.c,v 1.13 2004/04/03 22:17:59 hsu Exp $
    6:  */
    7: 
    8: #include "opt_inet.h"
    9: 
   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: 
   18: #include <net/if.h>
   19: #include <net/netisr.h>
   20: 
   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>
   32: 
   33: extern struct thread netisr_cpu[];
   34: 
   35: static struct thread tcp_thread[MAXCPU];
   36: static struct thread udp_thread[MAXCPU];
   37: 
   38: static __inline int
   39: INP_MPORT_HASH(in_addr_t src, in_addr_t dst, in_port_t sport, in_port_t dport)
   40: {
   41: 	/*
   42: 	 * Use low order bytes.
   43: 	 */
   44: 
   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)) &
   48: 		ncpus2_mask);
   49: #else
   50: 	return ((src ^ sport ^ dst ^ dport) & ncpus2_mask);
   51: #endif
   52: }
   53: 
   54: /*
   55:  * Map a packet to a protocol processing thread.
   56:  */
   57: lwkt_port_t
   58: ip_mport(struct mbuf *m)
   59: {
   60: 	struct ip *ip;
   61: 	int iphlen;
   62: 	struct tcphdr *th;
   63: 	struct udphdr *uh;
   64: 	int thoff;				/* TCP data offset */
   65: 	lwkt_port_t port;
   66: 	int cpu;
   67: 
   68: 	if (m->m_pkthdr.len < sizeof(struct ip)) {
   69: 		ipstat.ips_tooshort++;
   70: 		return (NULL);
   71: 	}
   72: 
   73: 	if (m->m_len < sizeof(struct ip) &&
   74: 	    (m = m_pullup(m, sizeof(struct ip))) == NULL) {
   75: 		ipstat.ips_toosmall++;
   76: 		return (NULL);
   77: 	}
   78: 
   79: 	ip = mtod(m, struct ip *);
   80: 
   81: 	/*
   82: 	 * XXX generic packet handling defrag on CPU 0 for now.
   83: 	 */
   84: 	if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK))
   85: 		return (&netisr_cpu[0].td_msgport);
   86: 
   87: 	iphlen = ip->ip_hl << 2;
   88: 	if (iphlen < sizeof(struct ip)) {	/* minimum header length */
   89: 		ipstat.ips_badhlen++;
   90: 		return (NULL);
   91: 	}
   92: 
   93: 	switch (ip->ip_p) {
   94: 	case IPPROTO_TCP:
   95: 		if (m->m_len < iphlen + sizeof(struct tcphdr) &&
   96: 		    (m = m_pullup(m, iphlen + sizeof(struct tcphdr))) == NULL) {
   97: 			tcpstat.tcps_rcvshort++;
   98: 			return (NULL);
   99: 		}
  100: 		th = (struct tcphdr *)((caddr_t)ip + iphlen);
  101: 		thoff = th->th_off << 2;
  102: 		if (thoff < sizeof(struct tcphdr) ||
  103: 		    thoff > ntohs(ip->ip_len)) {
  104: 			tcpstat.tcps_rcvbadoff++;
  105: 			return (NULL);
  106: 		}
  107: 		if (m->m_len < iphlen + thoff) {
  108: 			m = m_pullup(m, iphlen + thoff);
  109: 			if (m == NULL) {
  110: 				tcpstat.tcps_rcvshort++;
  111: 				return (NULL);
  112: 			}
  113: 			ip = mtod(m, struct ip *);
  114: 			th = (struct tcphdr *)((caddr_t)ip + iphlen);
  115: 		}
  116: 
  117: 		cpu = INP_MPORT_HASH(ip->ip_src.s_addr, ip->ip_dst.s_addr,
  118: 		    th->th_sport, th->th_dport);
  119: 		port = &tcp_thread[cpu].td_msgport;
  120: 		break;
  121: 	case IPPROTO_UDP:
  122: 		if (m->m_len < iphlen + sizeof(struct udphdr)) {
  123: 			m = m_pullup(m, iphlen + sizeof(struct udphdr));
  124: 			if (m == NULL) {
  125: 				udpstat.udps_hdrops++;
  126: 				return (NULL);
  127: 			}
  128: 			ip = mtod(m, struct ip *);
  129: 		}
  130: 		uh = (struct udphdr *)((caddr_t)ip + iphlen);
  131: 
  132: 		if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  133: 		    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
  134: 			cpu = 0;
  135: 		} else {
  136: 			cpu = INP_MPORT_HASH(ip->ip_src.s_addr,
  137: 			    ip->ip_dst.s_addr, uh->uh_sport, uh->uh_dport);
  138: 		}
  139: 		port = &udp_thread[cpu].td_msgport;
  140: 		break;
  141: 	default:
  142: 		if (m->m_len < iphlen && (m = m_pullup(m, iphlen)) == NULL) {
  143: 			ipstat.ips_badhlen++;
  144: 			return (NULL);
  145: 		}
  146: 		port = &netisr_cpu[0].td_msgport;
  147: 		break;
  148: 	}
  149: 	KKASSERT(port->mp_putport != NULL);
  150: 
  151: 	return (port);
  152: }
  153: 
  154: /*
  155:  * Map a TCP socket to a protocol processing thread.
  156:  */
  157: lwkt_port_t
  158: tcp_soport(struct socket *so, struct sockaddr *nam)
  159: {
  160: 	struct inpcb *inp;
  161: 
  162: 	/*
  163: 	 * The following processing all take place on Protocol Thread 0:
  164: 	 *   only bind() and connect() have a non-null nam parameter
  165: 	 *   attach() has a null socket parameter
  166: 	 *   Fast and slow timeouts pass in two NULLs
  167: 	 */
  168: 	if (nam != NULL || so == NULL)
  169: 		return (&tcp_thread[0].td_msgport);
  170: 
  171: 	/*
  172: 	 * Already bound and connected.  For TCP connections, the
  173: 	 * (faddr, fport, laddr, lport) association cannot change now.
  174: 	 *
  175: 	 * Note: T/TCP code needs some reorganization to fit into
  176: 	 * this model.  XXX JH
  177: 	 */
  178: 	inp = sotoinpcb(so);
  179: 	if (!inp)		/* connection reset by peer */
  180: 		return (&tcp_thread[0].td_msgport);
  181: 
  182: 	/*
  183: 	 * Rely on type-stable memory and check in protocol handler
  184: 	 * to fix race condition here w/ deallocation of inp.  XXX JH
  185: 	 */
  186: 
  187: 	return (&tcp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
  188: 	    inp->inp_faddr.s_addr, inp->inp_lport,
  189: 	    inp->inp_fport)].td_msgport);
  190: }
  191: 
  192: /*
  193:  * Map a UDP socket to a protocol processing thread.
  194:  */
  195: lwkt_port_t
  196: udp_soport(struct socket *so, struct sockaddr *nam)
  197: {
  198: 	struct inpcb *inp;
  199: 
  200: 	/*
  201: 	 * The following processing all take place on Protocol Thread 0:
  202: 	 *   only bind() and connect() have a non-null nam parameter
  203: 	 *   attach() has a null socket parameter
  204: 	 *   Fast and slow timeouts pass in two NULLs
  205: 	 */
  206: 	if (nam != NULL || so == NULL)
  207: 		return (&udp_thread[0].td_msgport);
  208: 
  209: 	inp = sotoinpcb(so);
  210: 
  211: 	if (IN_MULTICAST(ntohl(inp->inp_laddr.s_addr)))
  212: 		return (&udp_thread[0].td_msgport);
  213: 
  214: 	/*
  215: 	 * Rely on type-stable memory and check in protocol handler
  216: 	 * to fix race condition here w/ deallocation of inp.  XXX JH
  217: 	 */
  218: 
  219: 	return (&udp_thread[INP_MPORT_HASH(inp->inp_laddr.s_addr,
  220: 	    inp->inp_faddr.s_addr, inp->inp_lport,
  221: 	    inp->inp_fport)].td_msgport);
  222: }
  223: 
  224: /*
  225:  * Map a network address to a processor.
  226:  */
  227: int
  228: tcp_addrcpu(in_addr_t src, in_port_t sport, in_addr_t dst, in_port_t dport)
  229: {
  230: 	return (INP_MPORT_HASH(src, dst, sport, dport));
  231: }
  232: 
  233: int
  234: udp_addrcpu(in_addr_t src, in_port_t sport, in_addr_t dst, in_port_t dport)
  235: {
  236: 	if (IN_MULTICAST(ntohl(dst)))
  237: 		return (0);
  238: 	else
  239: 		return (INP_MPORT_HASH(src, dst, sport, dport));
  240: }
  241: 
  242: /*
  243:  * Return LWKT port for cpu.
  244:  */
  245: lwkt_port_t
  246: tcp_cport(int cpu)
  247: {
  248: 	return (&tcp_thread[cpu].td_msgport);
  249: }
  250: 
  251: void
  252: tcp_thread_init(void)
  253: {
  254: 	int cpu;
  255: 
  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);
  259: 	}
  260: }
  261: 
  262: void
  263: udp_thread_init(void)
  264: {
  265: 	int cpu;
  266: 
  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);
  270: 	}
  271: }