1 /* $OpenBSD: if_trunk.c,v 1.30 2007/01/31 06:20:19 reyk Exp $ */
4 * Copyright (c) 2005, 2006 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
24 #include "opt_inet6.h"
26 #include <sys/param.h>
28 #include <sys/fnv_hash.h>
30 #include <net/ethernet.h>
31 #include <net/infiniband.h>
33 #if defined(INET) || defined(INET6)
34 #include <netinet/in.h>
38 #include <netinet/ip.h>
42 #include <netinet/ip6.h>
46 m_common_hash_gethdr(const struct mbuf *m, const u_int off,
47 const u_int len, void *buf)
50 if (m->m_pkthdr.len < (off + len)) {
52 } else if (m->m_len < (off + len)) {
53 m_copydata(m, off, len, buf);
56 return (mtod(m, char *) + off);
60 m_ether_tcpip_hash_init(void)
65 return (fnv_32_buf(&seed, sizeof(seed), FNV1_32_INIT));
69 m_infiniband_tcpip_hash_init(void)
74 return (fnv_32_buf(&seed, sizeof(seed), FNV1_32_INIT));
77 static inline uint32_t
78 m_tcpip_hash(const uint32_t flags, const struct mbuf *m,
79 uint32_t p, int off, const uint16_t etype)
81 #if defined(INET) || defined(INET6)
95 const struct ip6_hdr *ip6;
101 ip = m_common_hash_gethdr(m, off, sizeof(*ip), &buf);
104 if (flags & MBUF_HASHFLAG_L3) {
105 p = fnv_32_buf(&ip->ip_src, sizeof(struct in_addr), p);
106 p = fnv_32_buf(&ip->ip_dst, sizeof(struct in_addr), p);
108 if (flags & MBUF_HASHFLAG_L4) {
109 const uint32_t *ports;
116 iphlen = ip->ip_hl << 2;
117 if (iphlen < sizeof(*ip))
120 ports = m_common_hash_gethdr(m,
121 off, sizeof(*ports), &buf);
124 p = fnv_32_buf(ports, sizeof(*ports), p);
134 ip6 = m_common_hash_gethdr(m, off, sizeof(*ip6), &buf);
137 if (flags & MBUF_HASHFLAG_L3) {
138 p = fnv_32_buf(&ip6->ip6_src, sizeof(struct in6_addr), p);
139 p = fnv_32_buf(&ip6->ip6_dst, sizeof(struct in6_addr), p);
141 if (flags & MBUF_HASHFLAG_L4) {
144 /* IPv6 flow label */
145 flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK;
146 p = fnv_32_buf(&flow, sizeof(flow), p);
157 m_ether_tcpip_hash(const uint32_t flags, const struct mbuf *m,
161 struct ether_vlan_header vlan;
163 const struct ether_header *eh;
164 const struct ether_vlan_header *vlan;
171 eh = mtod(m, struct ether_header *);
172 etype = ntohs(eh->ether_type);
173 if (flags & MBUF_HASHFLAG_L2) {
174 p = fnv_32_buf(&eh->ether_shost, ETHER_ADDR_LEN, p);
175 p = fnv_32_buf(&eh->ether_dhost, ETHER_ADDR_LEN, p);
177 /* Special handling for encapsulating VLAN frames */
178 if ((m->m_flags & M_VLANTAG) && (flags & MBUF_HASHFLAG_L2)) {
179 p = fnv_32_buf(&m->m_pkthdr.ether_vtag,
180 sizeof(m->m_pkthdr.ether_vtag), p);
181 } else if (etype == ETHERTYPE_VLAN) {
182 vlan = m_common_hash_gethdr(m, off, sizeof(*vlan), &buf);
186 if (flags & MBUF_HASHFLAG_L2)
187 p = fnv_32_buf(&vlan->evl_tag, sizeof(vlan->evl_tag), p);
188 etype = ntohs(vlan->evl_proto);
189 off += sizeof(*vlan) - sizeof(*eh);
191 return (m_tcpip_hash(flags, m, p, off, etype));
195 m_infiniband_tcpip_hash(const uint32_t flags, const struct mbuf *m,
198 const struct infiniband_header *ibh;
205 ibh = mtod(m, struct infiniband_header *);
206 etype = ntohs(ibh->ib_protocol);
207 if (flags & MBUF_HASHFLAG_L2)
208 p = fnv_32_buf(&ibh->ib_hwaddr, INFINIBAND_ADDR_LEN, p);
210 return (m_tcpip_hash(flags, m, p, off, etype));