1 #include <linux/skbuff.h>
2 #include <linux/export.h>
4 #include <linux/ipv6.h>
5 #include <linux/if_vlan.h>
8 #include <linux/igmp.h>
9 #include <linux/icmp.h>
10 #include <linux/sctp.h>
11 #include <linux/dccp.h>
12 #include <linux/if_tunnel.h>
13 #include <linux/if_pppox.h>
14 #include <linux/ppp_defs.h>
15 #include <net/flow_keys.h>
17 /* copy saddr & daddr, possibly using 64bit load/store
18 * Equivalent to : flow->src = iph->saddr;
19 * flow->dst = iph->daddr;
21 static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
23 BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
24 offsetof(typeof(*flow), src) + sizeof(flow->src));
25 memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
29 * skb_flow_get_ports - extract the upper layer ports and return them
30 * @skb: buffer to extract the ports from
31 * @thoff: transport header offset
32 * @ip_proto: protocol for which to get port offset
34 * The function will try to retrieve the ports at offset thoff + poff where poff
35 * is the protocol port offset returned from proto_ports_offset
37 __be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto)
39 int poff = proto_ports_offset(ip_proto);
42 __be32 *ports, _ports;
44 ports = skb_header_pointer(skb, thoff + poff,
45 sizeof(_ports), &_ports);
52 EXPORT_SYMBOL(skb_flow_get_ports);
54 bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
56 int nhoff = skb_network_offset(skb);
58 __be16 proto = skb->protocol;
60 memset(flow, 0, sizeof(*flow));
64 case __constant_htons(ETH_P_IP): {
65 const struct iphdr *iph;
68 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
72 if (ip_is_fragment(iph))
75 ip_proto = iph->protocol;
76 iph_to_flow_copy_addrs(flow, iph);
77 nhoff += iph->ihl * 4;
80 case __constant_htons(ETH_P_IPV6): {
81 const struct ipv6hdr *iph;
84 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
88 ip_proto = iph->nexthdr;
89 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
90 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
91 nhoff += sizeof(struct ipv6hdr);
94 case __constant_htons(ETH_P_8021AD):
95 case __constant_htons(ETH_P_8021Q): {
96 const struct vlan_hdr *vlan;
97 struct vlan_hdr _vlan;
99 vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
103 proto = vlan->h_vlan_encapsulated_proto;
104 nhoff += sizeof(*vlan);
107 case __constant_htons(ETH_P_PPP_SES): {
109 struct pppoe_hdr hdr;
112 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
116 nhoff += PPPOE_SES_HLEN;
118 case __constant_htons(PPP_IP):
120 case __constant_htons(PPP_IPV6):
137 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
141 * Only look inside GRE if version zero and no
144 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
147 if (hdr->flags & GRE_CSUM)
149 if (hdr->flags & GRE_KEY)
151 if (hdr->flags & GRE_SEQ)
153 if (proto == htons(ETH_P_TEB)) {
154 const struct ethhdr *eth;
157 eth = skb_header_pointer(skb, nhoff,
158 sizeof(_eth), &_eth);
161 proto = eth->h_proto;
162 nhoff += sizeof(*eth);
169 proto = htons(ETH_P_IP);
172 proto = htons(ETH_P_IPV6);
178 flow->ip_proto = ip_proto;
179 flow->ports = skb_flow_get_ports(skb, nhoff, ip_proto);
180 flow->thoff = (u16) nhoff;
184 EXPORT_SYMBOL(skb_flow_dissect);
186 static u32 hashrnd __read_mostly;
189 * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
190 * and src/dst port numbers. Sets rxhash in skb to non-zero hash value
191 * on success, zero indicates no valid hash. Also, sets l4_rxhash in skb
192 * if hash is a canonical 4-tuple hash over transport ports.
194 void __skb_get_rxhash(struct sk_buff *skb)
196 struct flow_keys keys;
199 if (!skb_flow_dissect(skb, &keys))
205 /* get a consistent hash (same value on both flow directions) */
206 if (((__force u32)keys.dst < (__force u32)keys.src) ||
207 (((__force u32)keys.dst == (__force u32)keys.src) &&
208 ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
209 swap(keys.dst, keys.src);
210 swap(keys.port16[0], keys.port16[1]);
213 hash = jhash_3words((__force u32)keys.dst,
214 (__force u32)keys.src,
215 (__force u32)keys.ports, hashrnd);
221 EXPORT_SYMBOL(__skb_get_rxhash);
224 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
225 * to be used as a distribution range.
227 u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
228 unsigned int num_tx_queues)
232 u16 qcount = num_tx_queues;
234 if (skb_rx_queue_recorded(skb)) {
235 hash = skb_get_rx_queue(skb);
236 while (unlikely(hash >= num_tx_queues))
237 hash -= num_tx_queues;
242 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
243 qoffset = dev->tc_to_txq[tc].offset;
244 qcount = dev->tc_to_txq[tc].count;
247 if (skb->sk && skb->sk->sk_hash)
248 hash = skb->sk->sk_hash;
250 hash = (__force u16) skb->protocol;
251 hash = jhash_1word(hash, hashrnd);
253 return (u16) (((u64) hash * qcount) >> 32) + qoffset;
255 EXPORT_SYMBOL(__skb_tx_hash);
257 /* __skb_get_poff() returns the offset to the payload as far as it could
258 * be dissected. The main user is currently BPF, so that we can dynamically
259 * truncate packets without needing to push actual payload to the user
260 * space and can analyze headers only, instead.
262 u32 __skb_get_poff(const struct sk_buff *skb)
264 struct flow_keys keys;
267 if (!skb_flow_dissect(skb, &keys))
271 switch (keys.ip_proto) {
273 const struct tcphdr *tcph;
276 tcph = skb_header_pointer(skb, poff, sizeof(_tcph), &_tcph);
280 poff += max_t(u32, sizeof(struct tcphdr), tcph->doff * 4);
284 case IPPROTO_UDPLITE:
285 poff += sizeof(struct udphdr);
287 /* For the rest, we do not really care about header
288 * extensions at this point for now.
291 poff += sizeof(struct icmphdr);
294 poff += sizeof(struct icmp6hdr);
297 poff += sizeof(struct igmphdr);
300 poff += sizeof(struct dccp_hdr);
303 poff += sizeof(struct sctphdr);
310 static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
312 if (unlikely(queue_index >= dev->real_num_tx_queues)) {
313 net_warn_ratelimited("%s selects TX queue %d, but real number of TX queues is %d\n",
314 dev->name, queue_index,
315 dev->real_num_tx_queues);
321 static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
324 struct xps_dev_maps *dev_maps;
326 int queue_index = -1;
329 dev_maps = rcu_dereference(dev->xps_maps);
331 map = rcu_dereference(
332 dev_maps->cpu_map[raw_smp_processor_id()]);
335 queue_index = map->queues[0];
338 if (skb->sk && skb->sk->sk_hash)
339 hash = skb->sk->sk_hash;
341 hash = (__force u16) skb->protocol ^
343 hash = jhash_1word(hash, hashrnd);
344 queue_index = map->queues[
345 ((u64)hash * map->len) >> 32];
347 if (unlikely(queue_index >= dev->real_num_tx_queues))
359 u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
361 struct sock *sk = skb->sk;
362 int queue_index = sk_tx_queue_get(sk);
364 if (queue_index < 0 || skb->ooo_okay ||
365 queue_index >= dev->real_num_tx_queues) {
366 int new_index = get_xps_queue(dev, skb);
368 new_index = skb_tx_hash(dev, skb);
370 if (queue_index != new_index && sk &&
371 rcu_access_pointer(sk->sk_dst_cache))
372 sk_tx_queue_set(sk, new_index);
374 queue_index = new_index;
379 EXPORT_SYMBOL(__netdev_pick_tx);
381 struct netdev_queue *netdev_pick_tx(struct net_device *dev,
386 if (dev->real_num_tx_queues != 1) {
387 const struct net_device_ops *ops = dev->netdev_ops;
388 if (ops->ndo_select_queue)
389 queue_index = ops->ndo_select_queue(dev, skb);
391 queue_index = __netdev_pick_tx(dev, skb);
392 queue_index = dev_cap_txqueue(dev, queue_index);
395 skb_set_queue_mapping(skb, queue_index);
396 return netdev_get_tx_queue(dev, queue_index);
399 static int __init initialize_hashrnd(void)
401 get_random_bytes(&hashrnd, sizeof(hashrnd));
405 late_initcall_sync(initialize_hashrnd);