Home | History | Annotate | Line # | Download | only in agr
      1  1.4  knakahar /*	$NetBSD: if_agrether_hash.c,v 1.4 2017/09/26 07:42:06 knakahara Exp $	*/
      2  1.1      yamt 
      3  1.1      yamt /*-
      4  1.1      yamt  * Copyright (c)2005 YAMAMOTO Takashi,
      5  1.1      yamt  * All rights reserved.
      6  1.1      yamt  *
      7  1.1      yamt  * Redistribution and use in source and binary forms, with or without
      8  1.1      yamt  * modification, are permitted provided that the following conditions
      9  1.1      yamt  * are met:
     10  1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      yamt  *    documentation and/or other materials provided with the distribution.
     15  1.1      yamt  *
     16  1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1      yamt  * SUCH DAMAGE.
     27  1.1      yamt  */
     28  1.1      yamt 
     29  1.1      yamt #include <sys/cdefs.h>
     30  1.4  knakahar __KERNEL_RCSID(0, "$NetBSD: if_agrether_hash.c,v 1.4 2017/09/26 07:42:06 knakahara Exp $");
     31  1.1      yamt 
     32  1.1      yamt #include <sys/param.h>
     33  1.1      yamt #include <sys/mbuf.h>
     34  1.1      yamt #include <sys/hash.h>
     35  1.1      yamt 
     36  1.1      yamt #include <net/if.h>
     37  1.1      yamt #include <net/if_dl.h>
     38  1.1      yamt #include <net/if_ether.h>
     39  1.1      yamt #include <net/if_vlanvar.h>
     40  1.1      yamt 
     41  1.1      yamt #include <net/agr/if_agrethervar.h>
     42  1.1      yamt 
     43  1.1      yamt #include <netinet/in.h>
     44  1.1      yamt #include <netinet/in_systm.h>
     45  1.1      yamt #include <netinet/ip.h>
     46  1.1      yamt #include <netinet/ip6.h>
     47  1.1      yamt 
     48  1.1      yamt #define	HASH(p, l, h)	hash32_buf((p), (l), (h))
     49  1.1      yamt 
     50  1.1      yamt static const void *agr_m_extract(struct mbuf *, int, int, void *);
     51  1.1      yamt 
     52  1.1      yamt static const void *
     53  1.1      yamt agr_m_extract(struct mbuf *m, int off, int len, void *buf)
     54  1.1      yamt {
     55  1.1      yamt 	const void *result;
     56  1.1      yamt 
     57  1.1      yamt 	KASSERT((m->m_flags & M_PKTHDR) != 0);
     58  1.1      yamt 
     59  1.1      yamt 	if (m->m_pkthdr.len < off + len) {
     60  1.1      yamt 		result = NULL;
     61  1.1      yamt 	} else if (m->m_len >= off + len) {
     62  1.1      yamt 		result = mtod(m, char *) + off;
     63  1.1      yamt 	} else {
     64  1.1      yamt 		m_copydata(m, off, len, buf);
     65  1.1      yamt 		result = buf;
     66  1.1      yamt 	}
     67  1.1      yamt 
     68  1.1      yamt 	return result;
     69  1.1      yamt }
     70  1.1      yamt 
     71  1.1      yamt uint32_t
     72  1.1      yamt agrether_hashmbuf(struct agr_softc *sc, struct mbuf *m)
     73  1.1      yamt {
     74  1.1      yamt 	struct ether_header eh_store;
     75  1.1      yamt 	const struct ether_header *eh;
     76  1.1      yamt 	uint32_t hash = HASH32_BUF_INIT;
     77  1.1      yamt 	int off = 0;
     78  1.1      yamt 	uint16_t tci;
     79  1.1      yamt 	uint16_t etype;
     80  1.1      yamt 
     81  1.1      yamt 	eh = agr_m_extract(m, off, sizeof(*eh), &eh_store);
     82  1.1      yamt 	if (eh == NULL) {
     83  1.1      yamt 		return hash;
     84  1.1      yamt 	}
     85  1.1      yamt 
     86  1.1      yamt 	hash = HASH(&eh->ether_dhost, sizeof(eh->ether_dhost), hash);
     87  1.1      yamt 	hash = HASH(&eh->ether_shost, sizeof(eh->ether_shost), hash);
     88  1.1      yamt 	etype = eh->ether_type;
     89  1.1      yamt 
     90  1.1      yamt 	if (etype == htobe16(ETHERTYPE_VLAN)) {
     91  1.1      yamt 		struct ether_vlan_header vlanhdr_store;
     92  1.1      yamt 		const struct ether_vlan_header *vlanhdr;
     93  1.1      yamt 
     94  1.1      yamt 		vlanhdr = agr_m_extract(m, off, sizeof(*vlanhdr),
     95  1.1      yamt 		    &vlanhdr_store);
     96  1.1      yamt 		if (vlanhdr == NULL) {
     97  1.1      yamt 			return hash;
     98  1.1      yamt 		}
     99  1.1      yamt 
    100  1.1      yamt 		tci = vlanhdr->evl_tag;
    101  1.1      yamt 		etype = vlanhdr->evl_proto;
    102  1.1      yamt 		off += sizeof(*vlanhdr) - sizeof(*eh);
    103  1.4  knakahar 	} else if (vlan_has_tag(m)) {
    104  1.4  knakahar 		tci = htole16(vlan_get_tag(m));
    105  1.1      yamt 	} else {
    106  1.1      yamt 		tci = 0;
    107  1.1      yamt 	}
    108  1.1      yamt 	hash = HASH(&tci, sizeof(tci), hash);
    109  1.1      yamt 
    110  1.1      yamt 	off += sizeof(*eh);
    111  1.1      yamt 	if (etype == htobe16(ETHERTYPE_IP)) {
    112  1.1      yamt 		struct ip ip_store;
    113  1.1      yamt 		const struct ip *ip;
    114  1.1      yamt 
    115  1.1      yamt 		ip = agr_m_extract(m, off, sizeof(*ip), &ip_store);
    116  1.1      yamt 		if (ip == NULL) {
    117  1.1      yamt 			return hash;
    118  1.1      yamt 		}
    119  1.1      yamt 
    120  1.1      yamt 		hash = HASH(&ip->ip_src, sizeof(ip->ip_src), hash);
    121  1.1      yamt 		hash = HASH(&ip->ip_dst, sizeof(ip->ip_dst), hash);
    122  1.1      yamt 		hash = HASH(&ip->ip_p, sizeof(ip->ip_p), hash);
    123  1.1      yamt 
    124  1.1      yamt 		/* use port numbers for tcp and udp? */
    125  1.1      yamt 
    126  1.1      yamt 	} else if (etype == htobe16(ETHERTYPE_IPV6)) {
    127  1.1      yamt 		struct ip6_hdr ip6_store;
    128  1.1      yamt 		const struct ip6_hdr *ip6;
    129  1.3      yamt 		uint32_t flowlabel;
    130  1.1      yamt 
    131  1.1      yamt 		ip6 = agr_m_extract(m, off, sizeof(*ip6), &ip6_store);
    132  1.1      yamt 		if (ip6 == NULL) {
    133  1.1      yamt 			return hash;
    134  1.1      yamt 		}
    135  1.1      yamt 
    136  1.1      yamt 		hash = HASH(&ip6->ip6_src, sizeof(ip6->ip6_src), hash);
    137  1.1      yamt 		hash = HASH(&ip6->ip6_dst, sizeof(ip6->ip6_dst), hash);
    138  1.1      yamt 		/* hash = HASH(&ip6->ip6_nxt, sizeof(ip6->ip6_nxt), hash); */
    139  1.1      yamt 
    140  1.3      yamt 		flowlabel = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
    141  1.3      yamt 		hash = HASH(&flowlabel, sizeof(flowlabel), hash);
    142  1.1      yamt 
    143  1.1      yamt 		/* use port numbers for tcp and udp? */
    144  1.1      yamt 	}
    145  1.1      yamt 
    146  1.1      yamt 	return hash;
    147  1.1      yamt }
    148