if_agrether_hash.c revision 1.1.8.1 1 1.1.8.1 yamt /* $NetBSD: if_agrether_hash.c,v 1.1.8.1 2007/09/03 14:42:26 yamt 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.1.8.1 yamt __KERNEL_RCSID(0, "$NetBSD: if_agrether_hash.c,v 1.1.8.1 2007/09/03 14:42:26 yamt 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 struct m_tag *mtag;
81 1.1 yamt
82 1.1 yamt eh = agr_m_extract(m, off, sizeof(*eh), &eh_store);
83 1.1 yamt if (eh == NULL) {
84 1.1 yamt return hash;
85 1.1 yamt }
86 1.1 yamt
87 1.1 yamt hash = HASH(&eh->ether_dhost, sizeof(eh->ether_dhost), hash);
88 1.1 yamt hash = HASH(&eh->ether_shost, sizeof(eh->ether_shost), hash);
89 1.1 yamt etype = eh->ether_type;
90 1.1 yamt
91 1.1 yamt if (etype == htobe16(ETHERTYPE_VLAN)) {
92 1.1 yamt struct ether_vlan_header vlanhdr_store;
93 1.1 yamt const struct ether_vlan_header *vlanhdr;
94 1.1 yamt
95 1.1 yamt vlanhdr = agr_m_extract(m, off, sizeof(*vlanhdr),
96 1.1 yamt &vlanhdr_store);
97 1.1 yamt if (vlanhdr == NULL) {
98 1.1 yamt return hash;
99 1.1 yamt }
100 1.1 yamt
101 1.1 yamt tci = vlanhdr->evl_tag;
102 1.1 yamt etype = vlanhdr->evl_proto;
103 1.1 yamt off += sizeof(*vlanhdr) - sizeof(*eh);
104 1.1 yamt } else if ((mtag = m_tag_find(m, PACKET_TAG_VLAN, NULL)) != NULL) {
105 1.1 yamt tci = htole16((*(u_int *)(mtag + 1)) & 0xffff);
106 1.1 yamt } else {
107 1.1 yamt tci = 0;
108 1.1 yamt }
109 1.1 yamt hash = HASH(&tci, sizeof(tci), hash);
110 1.1 yamt
111 1.1 yamt off += sizeof(*eh);
112 1.1 yamt if (etype == htobe16(ETHERTYPE_IP)) {
113 1.1 yamt struct ip ip_store;
114 1.1 yamt const struct ip *ip;
115 1.1 yamt
116 1.1 yamt ip = agr_m_extract(m, off, sizeof(*ip), &ip_store);
117 1.1 yamt if (ip == NULL) {
118 1.1 yamt return hash;
119 1.1 yamt }
120 1.1 yamt
121 1.1 yamt hash = HASH(&ip->ip_src, sizeof(ip->ip_src), hash);
122 1.1 yamt hash = HASH(&ip->ip_dst, sizeof(ip->ip_dst), hash);
123 1.1 yamt hash = HASH(&ip->ip_p, sizeof(ip->ip_p), hash);
124 1.1 yamt
125 1.1 yamt /* use port numbers for tcp and udp? */
126 1.1 yamt
127 1.1 yamt } else if (etype == htobe16(ETHERTYPE_IPV6)) {
128 1.1 yamt struct ip6_hdr ip6_store;
129 1.1 yamt const struct ip6_hdr *ip6;
130 1.1.8.1 yamt uint32_t flowlabel;
131 1.1 yamt
132 1.1 yamt ip6 = agr_m_extract(m, off, sizeof(*ip6), &ip6_store);
133 1.1 yamt if (ip6 == NULL) {
134 1.1 yamt return hash;
135 1.1 yamt }
136 1.1 yamt
137 1.1 yamt hash = HASH(&ip6->ip6_src, sizeof(ip6->ip6_src), hash);
138 1.1 yamt hash = HASH(&ip6->ip6_dst, sizeof(ip6->ip6_dst), hash);
139 1.1 yamt /* hash = HASH(&ip6->ip6_nxt, sizeof(ip6->ip6_nxt), hash); */
140 1.1 yamt
141 1.1.8.1 yamt flowlabel = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
142 1.1.8.1 yamt hash = HASH(&flowlabel, sizeof(flowlabel), hash);
143 1.1 yamt
144 1.1 yamt /* use port numbers for tcp and udp? */
145 1.1 yamt }
146 1.1 yamt
147 1.1 yamt return hash;
148 1.1 yamt }
149