blob: c5f3912dad4c9d1276a964f11d15b76f7d6c29b9 [file] [log] [blame]
Eric Dumazet0744dd02011-11-28 05:22:18 +00001#include <linux/skbuff.h>
Jesper Dangaard Brouerc452ed72012-01-24 16:03:33 -05002#include <linux/export.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +00003#include <linux/ip.h>
4#include <linux/ipv6.h>
5#include <linux/if_vlan.h>
6#include <net/ip.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +00007#include <net/ipv6.h>
Daniel Borkmannf77668d2013-03-19 06:39:30 +00008#include <linux/igmp.h>
9#include <linux/icmp.h>
10#include <linux/sctp.h>
11#include <linux/dccp.h>
Eric Dumazet0744dd02011-11-28 05:22:18 +000012#include <linux/if_tunnel.h>
13#include <linux/if_pppox.h>
14#include <linux/ppp_defs.h>
15#include <net/flow_keys.h>
16
Eric Dumazet4d77d2b2011-11-28 20:30:35 +000017/* copy saddr & daddr, possibly using 64bit load/store
18 * Equivalent to : flow->src = iph->saddr;
19 * flow->dst = iph->daddr;
20 */
21static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
22{
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));
26}
Eric Dumazet0744dd02011-11-28 05:22:18 +000027
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020028/**
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
33 *
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
36 */
37__be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto)
38{
39 int poff = proto_ports_offset(ip_proto);
40
41 if (poff >= 0) {
42 __be32 *ports, _ports;
43
44 ports = skb_header_pointer(skb, thoff + poff,
45 sizeof(_ports), &_ports);
46 if (ports)
47 return *ports;
48 }
49
50 return 0;
51}
52EXPORT_SYMBOL(skb_flow_get_ports);
53
Eric Dumazet0744dd02011-11-28 05:22:18 +000054bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
55{
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +020056 int nhoff = skb_network_offset(skb);
Eric Dumazet0744dd02011-11-28 05:22:18 +000057 u8 ip_proto;
58 __be16 proto = skb->protocol;
59
60 memset(flow, 0, sizeof(*flow));
61
62again:
63 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -070064 case htons(ETH_P_IP): {
Eric Dumazet0744dd02011-11-28 05:22:18 +000065 const struct iphdr *iph;
66 struct iphdr _iph;
67ip:
68 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
Jason Wang6f092342013-11-01 15:01:10 +080069 if (!iph || iph->ihl < 5)
Eric Dumazet0744dd02011-11-28 05:22:18 +000070 return false;
Eric Dumazet3797d3e2013-11-07 08:37:28 -080071 nhoff += iph->ihl * 4;
Eric Dumazet0744dd02011-11-28 05:22:18 +000072
Eric Dumazet3797d3e2013-11-07 08:37:28 -080073 ip_proto = iph->protocol;
Eric Dumazet0744dd02011-11-28 05:22:18 +000074 if (ip_is_fragment(iph))
75 ip_proto = 0;
Eric Dumazet3797d3e2013-11-07 08:37:28 -080076
Eric Dumazet4d77d2b2011-11-28 20:30:35 +000077 iph_to_flow_copy_addrs(flow, iph);
Eric Dumazet0744dd02011-11-28 05:22:18 +000078 break;
79 }
Joe Perches2b8837a2014-03-12 10:04:17 -070080 case htons(ETH_P_IPV6): {
Eric Dumazet0744dd02011-11-28 05:22:18 +000081 const struct ipv6hdr *iph;
82 struct ipv6hdr _iph;
Tom Herbert19469a82014-07-01 21:33:01 -070083 __be32 flow_label;
84
Eric Dumazet0744dd02011-11-28 05:22:18 +000085ipv6:
86 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
87 if (!iph)
88 return false;
89
90 ip_proto = iph->nexthdr;
Eric Dumazetddbe5032012-07-18 08:11:12 +000091 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
92 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
Eric Dumazet0744dd02011-11-28 05:22:18 +000093 nhoff += sizeof(struct ipv6hdr);
Tom Herbert19469a82014-07-01 21:33:01 -070094
95 flow_label = ip6_flowlabel(iph);
96 if (flow_label) {
97 /* Awesome, IPv6 packet has a flow label so we can
98 * use that to represent the ports without any
99 * further dissection.
100 */
101 flow->n_proto = proto;
102 flow->ip_proto = ip_proto;
103 flow->ports = flow_label;
104 flow->thoff = (u16)nhoff;
105
106 return true;
107 }
108
Eric Dumazet0744dd02011-11-28 05:22:18 +0000109 break;
110 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700111 case htons(ETH_P_8021AD):
112 case htons(ETH_P_8021Q): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000113 const struct vlan_hdr *vlan;
114 struct vlan_hdr _vlan;
115
116 vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
117 if (!vlan)
118 return false;
119
120 proto = vlan->h_vlan_encapsulated_proto;
121 nhoff += sizeof(*vlan);
122 goto again;
123 }
Joe Perches2b8837a2014-03-12 10:04:17 -0700124 case htons(ETH_P_PPP_SES): {
Eric Dumazet0744dd02011-11-28 05:22:18 +0000125 struct {
126 struct pppoe_hdr hdr;
127 __be16 proto;
128 } *hdr, _hdr;
129 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
130 if (!hdr)
131 return false;
132 proto = hdr->proto;
133 nhoff += PPPOE_SES_HLEN;
134 switch (proto) {
Joe Perches2b8837a2014-03-12 10:04:17 -0700135 case htons(PPP_IP):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000136 goto ip;
Joe Perches2b8837a2014-03-12 10:04:17 -0700137 case htons(PPP_IPV6):
Eric Dumazet0744dd02011-11-28 05:22:18 +0000138 goto ipv6;
139 default:
140 return false;
141 }
142 }
143 default:
144 return false;
145 }
146
147 switch (ip_proto) {
148 case IPPROTO_GRE: {
149 struct gre_hdr {
150 __be16 flags;
151 __be16 proto;
152 } *hdr, _hdr;
153
154 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
155 if (!hdr)
156 return false;
157 /*
158 * Only look inside GRE if version zero and no
159 * routing
160 */
161 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
162 proto = hdr->proto;
163 nhoff += 4;
164 if (hdr->flags & GRE_CSUM)
165 nhoff += 4;
166 if (hdr->flags & GRE_KEY)
167 nhoff += 4;
168 if (hdr->flags & GRE_SEQ)
169 nhoff += 4;
Michael Daltone1733de2013-03-11 06:52:28 +0000170 if (proto == htons(ETH_P_TEB)) {
171 const struct ethhdr *eth;
172 struct ethhdr _eth;
173
174 eth = skb_header_pointer(skb, nhoff,
175 sizeof(_eth), &_eth);
176 if (!eth)
177 return false;
178 proto = eth->h_proto;
179 nhoff += sizeof(*eth);
180 }
Eric Dumazet0744dd02011-11-28 05:22:18 +0000181 goto again;
182 }
183 break;
184 }
185 case IPPROTO_IPIP:
Tom Herbertfca41892013-07-29 11:07:36 -0700186 proto = htons(ETH_P_IP);
187 goto ip;
Tom Herbertb438f942013-07-29 11:07:42 -0700188 case IPPROTO_IPV6:
189 proto = htons(ETH_P_IPV6);
190 goto ipv6;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000191 default:
192 break;
193 }
194
Govindarajulu Varadarajane0f31d82014-06-23 16:07:58 +0530195 flow->n_proto = proto;
Eric Dumazet0744dd02011-11-28 05:22:18 +0000196 flow->ip_proto = ip_proto;
Nikolay Aleksandrov357afe92013-10-02 13:39:24 +0200197 flow->ports = skb_flow_get_ports(skb, nhoff, ip_proto);
Daniel Borkmann8ed78162013-03-19 06:39:29 +0000198 flow->thoff = (u16) nhoff;
199
Eric Dumazet0744dd02011-11-28 05:22:18 +0000200 return true;
201}
202EXPORT_SYMBOL(skb_flow_dissect);
Cong Wang441d9d32013-01-21 00:39:24 +0000203
204static u32 hashrnd __read_mostly;
Hannes Frederic Sowa66415cf2013-10-23 20:06:00 +0200205static __always_inline void __flow_hash_secret_init(void)
206{
207 net_get_random_once(&hashrnd, sizeof(hashrnd));
208}
209
210static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c)
211{
212 __flow_hash_secret_init();
213 return jhash_3words(a, b, c, hashrnd);
214}
215
Tom Herbert5ed20a62014-07-01 21:32:05 -0700216static inline u32 __flow_hash_from_keys(struct flow_keys *keys)
217{
218 u32 hash;
219
220 /* get a consistent hash (same value on both flow directions) */
221 if (((__force u32)keys->dst < (__force u32)keys->src) ||
222 (((__force u32)keys->dst == (__force u32)keys->src) &&
223 ((__force u16)keys->port16[1] < (__force u16)keys->port16[0]))) {
224 swap(keys->dst, keys->src);
225 swap(keys->port16[0], keys->port16[1]);
226 }
227
228 hash = __flow_hash_3words((__force u32)keys->dst,
229 (__force u32)keys->src,
230 (__force u32)keys->ports);
231 if (!hash)
232 hash = 1;
233
234 return hash;
235}
236
237u32 flow_hash_from_keys(struct flow_keys *keys)
238{
239 return __flow_hash_from_keys(keys);
240}
241EXPORT_SYMBOL(flow_hash_from_keys);
242
Cong Wang441d9d32013-01-21 00:39:24 +0000243/*
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800244 * __skb_get_hash: calculate a flow hash based on src/dst addresses
Tom Herbert61b905d2014-03-24 15:34:47 -0700245 * and src/dst port numbers. Sets hash in skb to non-zero hash value
246 * on success, zero indicates no valid hash. Also, sets l4_hash in skb
Cong Wang441d9d32013-01-21 00:39:24 +0000247 * if hash is a canonical 4-tuple hash over transport ports.
248 */
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800249void __skb_get_hash(struct sk_buff *skb)
Cong Wang441d9d32013-01-21 00:39:24 +0000250{
251 struct flow_keys keys;
Cong Wang441d9d32013-01-21 00:39:24 +0000252
253 if (!skb_flow_dissect(skb, &keys))
254 return;
255
256 if (keys.ports)
Tom Herbert61b905d2014-03-24 15:34:47 -0700257 skb->l4_hash = 1;
Cong Wang441d9d32013-01-21 00:39:24 +0000258
Tom Herbert5ed20a62014-07-01 21:32:05 -0700259 skb->hash = __flow_hash_from_keys(&keys);
Cong Wang441d9d32013-01-21 00:39:24 +0000260}
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800261EXPORT_SYMBOL(__skb_get_hash);
Cong Wang441d9d32013-01-21 00:39:24 +0000262
263/*
264 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
265 * to be used as a distribution range.
266 */
Tom Herbert0e001612014-07-01 21:32:27 -0700267u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
Cong Wang441d9d32013-01-21 00:39:24 +0000268 unsigned int num_tx_queues)
269{
270 u32 hash;
271 u16 qoffset = 0;
272 u16 qcount = num_tx_queues;
273
274 if (skb_rx_queue_recorded(skb)) {
275 hash = skb_get_rx_queue(skb);
276 while (unlikely(hash >= num_tx_queues))
277 hash -= num_tx_queues;
278 return hash;
279 }
280
281 if (dev->num_tc) {
282 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
283 qoffset = dev->tc_to_txq[tc].offset;
284 qcount = dev->tc_to_txq[tc].count;
285 }
286
Tom Herbert0e001612014-07-01 21:32:27 -0700287 return (u16) (((u64)skb_get_hash(skb) * qcount) >> 32) + qoffset;
Cong Wang441d9d32013-01-21 00:39:24 +0000288}
289EXPORT_SYMBOL(__skb_tx_hash);
290
Daniel Borkmannf77668d2013-03-19 06:39:30 +0000291/* __skb_get_poff() returns the offset to the payload as far as it could
292 * be dissected. The main user is currently BPF, so that we can dynamically
293 * truncate packets without needing to push actual payload to the user
294 * space and can analyze headers only, instead.
295 */
296u32 __skb_get_poff(const struct sk_buff *skb)
297{
298 struct flow_keys keys;
299 u32 poff = 0;
300
301 if (!skb_flow_dissect(skb, &keys))
302 return 0;
303
304 poff += keys.thoff;
305 switch (keys.ip_proto) {
306 case IPPROTO_TCP: {
307 const struct tcphdr *tcph;
308 struct tcphdr _tcph;
309
310 tcph = skb_header_pointer(skb, poff, sizeof(_tcph), &_tcph);
311 if (!tcph)
312 return poff;
313
314 poff += max_t(u32, sizeof(struct tcphdr), tcph->doff * 4);
315 break;
316 }
317 case IPPROTO_UDP:
318 case IPPROTO_UDPLITE:
319 poff += sizeof(struct udphdr);
320 break;
321 /* For the rest, we do not really care about header
322 * extensions at this point for now.
323 */
324 case IPPROTO_ICMP:
325 poff += sizeof(struct icmphdr);
326 break;
327 case IPPROTO_ICMPV6:
328 poff += sizeof(struct icmp6hdr);
329 break;
330 case IPPROTO_IGMP:
331 poff += sizeof(struct igmphdr);
332 break;
333 case IPPROTO_DCCP:
334 poff += sizeof(struct dccp_hdr);
335 break;
336 case IPPROTO_SCTP:
337 poff += sizeof(struct sctphdr);
338 break;
339 }
340
341 return poff;
342}
343
Cong Wang441d9d32013-01-21 00:39:24 +0000344static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
345{
346#ifdef CONFIG_XPS
347 struct xps_dev_maps *dev_maps;
348 struct xps_map *map;
349 int queue_index = -1;
350
351 rcu_read_lock();
352 dev_maps = rcu_dereference(dev->xps_maps);
353 if (dev_maps) {
354 map = rcu_dereference(
355 dev_maps->cpu_map[raw_smp_processor_id()]);
356 if (map) {
357 if (map->len == 1)
358 queue_index = map->queues[0];
Tom Herbert0e001612014-07-01 21:32:27 -0700359 else
Cong Wang441d9d32013-01-21 00:39:24 +0000360 queue_index = map->queues[
Tom Herbert0e001612014-07-01 21:32:27 -0700361 ((u64)skb_get_hash(skb) * map->len) >> 32];
362
Cong Wang441d9d32013-01-21 00:39:24 +0000363 if (unlikely(queue_index >= dev->real_num_tx_queues))
364 queue_index = -1;
365 }
366 }
367 rcu_read_unlock();
368
369 return queue_index;
370#else
371 return -1;
372#endif
373}
374
Daniel Borkmann99932d42014-02-16 15:55:20 +0100375static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
Cong Wang441d9d32013-01-21 00:39:24 +0000376{
377 struct sock *sk = skb->sk;
378 int queue_index = sk_tx_queue_get(sk);
379
380 if (queue_index < 0 || skb->ooo_okay ||
381 queue_index >= dev->real_num_tx_queues) {
382 int new_index = get_xps_queue(dev, skb);
383 if (new_index < 0)
384 new_index = skb_tx_hash(dev, skb);
385
Eric Dumazet702821f2013-08-28 18:10:43 -0700386 if (queue_index != new_index && sk &&
387 rcu_access_pointer(sk->sk_dst_cache))
Eric Dumazet50d17842013-09-07 12:02:57 -0700388 sk_tx_queue_set(sk, new_index);
Cong Wang441d9d32013-01-21 00:39:24 +0000389
390 queue_index = new_index;
391 }
392
393 return queue_index;
394}
Cong Wang441d9d32013-01-21 00:39:24 +0000395
396struct netdev_queue *netdev_pick_tx(struct net_device *dev,
Jason Wangf663dd92014-01-10 16:18:26 +0800397 struct sk_buff *skb,
398 void *accel_priv)
Cong Wang441d9d32013-01-21 00:39:24 +0000399{
400 int queue_index = 0;
401
402 if (dev->real_num_tx_queues != 1) {
403 const struct net_device_ops *ops = dev->netdev_ops;
404 if (ops->ndo_select_queue)
Daniel Borkmann99932d42014-02-16 15:55:20 +0100405 queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
406 __netdev_pick_tx);
Cong Wang441d9d32013-01-21 00:39:24 +0000407 else
408 queue_index = __netdev_pick_tx(dev, skb);
Jason Wangf663dd92014-01-10 16:18:26 +0800409
410 if (!accel_priv)
Daniel Borkmannb9507bd2014-02-16 15:55:21 +0100411 queue_index = netdev_cap_txqueue(dev, queue_index);
Cong Wang441d9d32013-01-21 00:39:24 +0000412 }
413
414 skb_set_queue_mapping(skb, queue_index);
415 return netdev_get_tx_queue(dev, queue_index);
416}