2 * Copyright (c) 2007-2013 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * This code is derived from kernel flow_dissector.c
21 #include <linux/version.h>
22 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,8,0)
24 #include <linux/ipv6.h>
25 #include <linux/if_vlan.h>
28 #include <linux/igmp.h>
29 #include <linux/icmp.h>
30 #include <linux/sctp.h>
31 #include <linux/dccp.h>
32 #include <linux/if_tunnel.h>
33 #include <linux/if_pppox.h>
34 #include <linux/ppp_defs.h>
35 #include <net/flow_keys.h>
38 /* copy saddr & daddr, possibly using 64bit load/store
39 * Equivalent to : flow->src = iph->saddr;
40 * flow->dst = iph->daddr;
42 static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
44 BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
45 offsetof(typeof(*flow), src) + sizeof(flow->src));
46 memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
49 static __be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto)
51 int poff = proto_ports_offset(ip_proto);
54 __be32 *ports, _ports;
56 ports = skb_header_pointer(skb, thoff + poff,
57 sizeof(_ports), &_ports);
65 static bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
67 int nhoff = skb_network_offset(skb);
69 __be16 proto = skb->protocol;
71 memset(flow, 0, sizeof(*flow));
75 case __constant_htons(ETH_P_IP): {
76 const struct iphdr *iph;
79 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
83 if (ip_is_fragment(iph))
86 ip_proto = iph->protocol;
87 iph_to_flow_copy_addrs(flow, iph);
88 nhoff += iph->ihl * 4;
91 case __constant_htons(ETH_P_IPV6): {
92 const struct ipv6hdr *iph;
95 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
99 ip_proto = iph->nexthdr;
100 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
101 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
102 nhoff += sizeof(struct ipv6hdr);
105 case __constant_htons(ETH_P_8021AD):
106 case __constant_htons(ETH_P_8021Q): {
107 const struct vlan_hdr *vlan;
108 struct vlan_hdr _vlan;
110 vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
114 proto = vlan->h_vlan_encapsulated_proto;
115 nhoff += sizeof(*vlan);
118 case __constant_htons(ETH_P_PPP_SES): {
120 struct pppoe_hdr hdr;
123 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
127 nhoff += PPPOE_SES_HLEN;
129 case __constant_htons(PPP_IP):
131 case __constant_htons(PPP_IPV6):
148 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
152 * Only look inside GRE if version zero and no
155 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
158 if (hdr->flags & GRE_CSUM)
160 if (hdr->flags & GRE_KEY)
162 if (hdr->flags & GRE_SEQ)
164 if (proto == htons(ETH_P_TEB)) {
165 const struct ethhdr *eth;
168 eth = skb_header_pointer(skb, nhoff,
169 sizeof(_eth), &_eth);
172 proto = eth->h_proto;
173 nhoff += sizeof(*eth);
182 proto = htons(ETH_P_IPV6);
188 flow->ip_proto = ip_proto;
189 flow->ports = skb_flow_get_ports(skb, nhoff, ip_proto);
190 flow->thoff = (u16) nhoff;
195 static u32 hashrnd __read_mostly;
196 static __always_inline void __flow_hash_secret_init(void)
198 net_get_random_once(&hashrnd, sizeof(hashrnd));
201 static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c)
203 __flow_hash_secret_init();
204 return jhash_3words(a, b, c, hashrnd);
207 u32 __skb_get_rxhash(struct sk_buff *skb)
209 struct flow_keys keys;
212 if (!skb_flow_dissect(skb, &keys))
215 /* get a consistent hash (same value on both flow directions) */
216 if (((__force u32)keys.dst < (__force u32)keys.src) ||
217 (((__force u32)keys.dst == (__force u32)keys.src) &&
218 ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
219 swap(keys.dst, keys.src);
220 swap(keys.port16[0], keys.port16[1]);
223 hash = __flow_hash_3words((__force u32)keys.dst,
224 (__force u32)keys.src,
225 (__force u32)keys.ports);