1 #include <linux/skbuff.h> 2 #include <linux/export.h> 3 #include <linux/ip.h> 4 #include <linux/ipv6.h> 5 #include <linux/if_vlan.h> 6 #include <net/ip.h> 7 #include <net/ipv6.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> 16 17 /* copy saddr & daddr, possibly using 64bit load/store 18 * Equivalent to : flow->src = iph->saddr; 19 * flow->dst = iph->daddr; 20 */ 21 static 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 } 27 28 bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow) 29 { 30 int poff, nhoff = skb_network_offset(skb); 31 u8 ip_proto; 32 __be16 proto = skb->protocol; 33 34 memset(flow, 0, sizeof(*flow)); 35 36 again: 37 switch (proto) { 38 case __constant_htons(ETH_P_IP): { 39 const struct iphdr *iph; 40 struct iphdr _iph; 41 ip: 42 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); 43 if (!iph) 44 return false; 45 46 if (ip_is_fragment(iph)) 47 ip_proto = 0; 48 else 49 ip_proto = iph->protocol; 50 iph_to_flow_copy_addrs(flow, iph); 51 nhoff += iph->ihl * 4; 52 break; 53 } 54 case __constant_htons(ETH_P_IPV6): { 55 const struct ipv6hdr *iph; 56 struct ipv6hdr _iph; 57 ipv6: 58 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); 59 if (!iph) 60 return false; 61 62 ip_proto = iph->nexthdr; 63 flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr); 64 flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr); 65 nhoff += sizeof(struct ipv6hdr); 66 break; 67 } 68 case __constant_htons(ETH_P_8021AD): 69 case __constant_htons(ETH_P_8021Q): { 70 const struct vlan_hdr *vlan; 71 struct vlan_hdr _vlan; 72 73 vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan); 74 if (!vlan) 75 return false; 76 77 proto = vlan->h_vlan_encapsulated_proto; 78 nhoff += sizeof(*vlan); 79 goto again; 80 } 81 case __constant_htons(ETH_P_PPP_SES): { 82 struct { 83 struct pppoe_hdr hdr; 84 __be16 proto; 85 } *hdr, _hdr; 86 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr); 87 if (!hdr) 88 return false; 89 proto = hdr->proto; 90 nhoff += PPPOE_SES_HLEN; 91 switch (proto) { 92 case __constant_htons(PPP_IP): 93 goto ip; 94 case __constant_htons(PPP_IPV6): 95 goto ipv6; 96 default: 97 return false; 98 } 99 } 100 default: 101 return false; 102 } 103 104 switch (ip_proto) { 105 case IPPROTO_GRE: { 106 struct gre_hdr { 107 __be16 flags; 108 __be16 proto; 109 } *hdr, _hdr; 110 111 hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr); 112 if (!hdr) 113 return false; 114 /* 115 * Only look inside GRE if version zero and no 116 * routing 117 */ 118 if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) { 119 proto = hdr->proto; 120 nhoff += 4; 121 if (hdr->flags & GRE_CSUM) 122 nhoff += 4; 123 if (hdr->flags & GRE_KEY) 124 nhoff += 4; 125 if (hdr->flags & GRE_SEQ) 126 nhoff += 4; 127 if (proto == htons(ETH_P_TEB)) { 128 const struct ethhdr *eth; 129 struct ethhdr _eth; 130 131 eth = skb_header_pointer(skb, nhoff, 132 sizeof(_eth), &_eth); 133 if (!eth) 134 return false; 135 proto = eth->h_proto; 136 nhoff += sizeof(*eth); 137 } 138 goto again; 139 } 140 break; 141 } 142 case IPPROTO_IPIP: 143 proto = htons(ETH_P_IP); 144 goto ip; 145 case IPPROTO_IPV6: 146 proto = htons(ETH_P_IPV6); 147 goto ipv6; 148 default: 149 break; 150 } 151 152 flow->ip_proto = ip_proto; 153 poff = proto_ports_offset(ip_proto); 154 if (poff >= 0) { 155 __be32 *ports, _ports; 156 157 ports = skb_header_pointer(skb, nhoff + poff, 158 sizeof(_ports), &_ports); 159 if (ports) 160 flow->ports = *ports; 161 } 162 163 flow->thoff = (u16) nhoff; 164 165 return true; 166 } 167 EXPORT_SYMBOL(skb_flow_dissect); 168 169 static u32 hashrnd __read_mostly; 170 171 /* 172 * __skb_get_rxhash: calculate a flow hash based on src/dst addresses 173 * and src/dst port numbers. Sets rxhash in skb to non-zero hash value 174 * on success, zero indicates no valid hash. Also, sets l4_rxhash in skb 175 * if hash is a canonical 4-tuple hash over transport ports. 176 */ 177 void __skb_get_rxhash(struct sk_buff *skb) 178 { 179 struct flow_keys keys; 180 u32 hash; 181 182 if (!skb_flow_dissect(skb, &keys)) 183 return; 184 185 if (keys.ports) 186 skb->l4_rxhash = 1; 187 188 /* get a consistent hash (same value on both flow directions) */ 189 if (((__force u32)keys.dst < (__force u32)keys.src) || 190 (((__force u32)keys.dst == (__force u32)keys.src) && 191 ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) { 192 swap(keys.dst, keys.src); 193 swap(keys.port16[0], keys.port16[1]); 194 } 195 196 hash = jhash_3words((__force u32)keys.dst, 197 (__force u32)keys.src, 198 (__force u32)keys.ports, hashrnd); 199 if (!hash) 200 hash = 1; 201 202 skb->rxhash = hash; 203 } 204 EXPORT_SYMBOL(__skb_get_rxhash); 205 206 /* 207 * Returns a Tx hash based on the given packet descriptor a Tx queues' number 208 * to be used as a distribution range. 209 */ 210 u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb, 211 unsigned int num_tx_queues) 212 { 213 u32 hash; 214 u16 qoffset = 0; 215 u16 qcount = num_tx_queues; 216 217 if (skb_rx_queue_recorded(skb)) { 218 hash = skb_get_rx_queue(skb); 219 while (unlikely(hash >= num_tx_queues)) 220 hash -= num_tx_queues; 221 return hash; 222 } 223 224 if (dev->num_tc) { 225 u8 tc = netdev_get_prio_tc_map(dev, skb->priority); 226 qoffset = dev->tc_to_txq[tc].offset; 227 qcount = dev->tc_to_txq[tc].count; 228 } 229 230 if (skb->sk && skb->sk->sk_hash) 231 hash = skb->sk->sk_hash; 232 else 233 hash = (__force u16) skb->protocol; 234 hash = jhash_1word(hash, hashrnd); 235 236 return (u16) (((u64) hash * qcount) >> 32) + qoffset; 237 } 238 EXPORT_SYMBOL(__skb_tx_hash); 239 240 /* __skb_get_poff() returns the offset to the payload as far as it could 241 * be dissected. The main user is currently BPF, so that we can dynamically 242 * truncate packets without needing to push actual payload to the user 243 * space and can analyze headers only, instead. 244 */ 245 u32 __skb_get_poff(const struct sk_buff *skb) 246 { 247 struct flow_keys keys; 248 u32 poff = 0; 249 250 if (!skb_flow_dissect(skb, &keys)) 251 return 0; 252 253 poff += keys.thoff; 254 switch (keys.ip_proto) { 255 case IPPROTO_TCP: { 256 const struct tcphdr *tcph; 257 struct tcphdr _tcph; 258 259 tcph = skb_header_pointer(skb, poff, sizeof(_tcph), &_tcph); 260 if (!tcph) 261 return poff; 262 263 poff += max_t(u32, sizeof(struct tcphdr), tcph->doff * 4); 264 break; 265 } 266 case IPPROTO_UDP: 267 case IPPROTO_UDPLITE: 268 poff += sizeof(struct udphdr); 269 break; 270 /* For the rest, we do not really care about header 271 * extensions at this point for now. 272 */ 273 case IPPROTO_ICMP: 274 poff += sizeof(struct icmphdr); 275 break; 276 case IPPROTO_ICMPV6: 277 poff += sizeof(struct icmp6hdr); 278 break; 279 case IPPROTO_IGMP: 280 poff += sizeof(struct igmphdr); 281 break; 282 case IPPROTO_DCCP: 283 poff += sizeof(struct dccp_hdr); 284 break; 285 case IPPROTO_SCTP: 286 poff += sizeof(struct sctphdr); 287 break; 288 } 289 290 return poff; 291 } 292 293 static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index) 294 { 295 if (unlikely(queue_index >= dev->real_num_tx_queues)) { 296 net_warn_ratelimited("%s selects TX queue %d, but real number of TX queues is %d\n", 297 dev->name, queue_index, 298 dev->real_num_tx_queues); 299 return 0; 300 } 301 return queue_index; 302 } 303 304 static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb) 305 { 306 #ifdef CONFIG_XPS 307 struct xps_dev_maps *dev_maps; 308 struct xps_map *map; 309 int queue_index = -1; 310 311 rcu_read_lock(); 312 dev_maps = rcu_dereference(dev->xps_maps); 313 if (dev_maps) { 314 map = rcu_dereference( 315 dev_maps->cpu_map[raw_smp_processor_id()]); 316 if (map) { 317 if (map->len == 1) 318 queue_index = map->queues[0]; 319 else { 320 u32 hash; 321 if (skb->sk && skb->sk->sk_hash) 322 hash = skb->sk->sk_hash; 323 else 324 hash = (__force u16) skb->protocol ^ 325 skb->rxhash; 326 hash = jhash_1word(hash, hashrnd); 327 queue_index = map->queues[ 328 ((u64)hash * map->len) >> 32]; 329 } 330 if (unlikely(queue_index >= dev->real_num_tx_queues)) 331 queue_index = -1; 332 } 333 } 334 rcu_read_unlock(); 335 336 return queue_index; 337 #else 338 return -1; 339 #endif 340 } 341 342 u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb) 343 { 344 struct sock *sk = skb->sk; 345 int queue_index = sk_tx_queue_get(sk); 346 347 if (queue_index < 0 || skb->ooo_okay || 348 queue_index >= dev->real_num_tx_queues) { 349 int new_index = get_xps_queue(dev, skb); 350 if (new_index < 0) 351 new_index = skb_tx_hash(dev, skb); 352 353 if (queue_index != new_index && sk && 354 rcu_access_pointer(sk->sk_dst_cache)) 355 sk_tx_queue_set(sk, new_index); 356 357 queue_index = new_index; 358 } 359 360 return queue_index; 361 } 362 EXPORT_SYMBOL(__netdev_pick_tx); 363 364 struct netdev_queue *netdev_pick_tx(struct net_device *dev, 365 struct sk_buff *skb) 366 { 367 int queue_index = 0; 368 369 if (dev->real_num_tx_queues != 1) { 370 const struct net_device_ops *ops = dev->netdev_ops; 371 if (ops->ndo_select_queue) 372 queue_index = ops->ndo_select_queue(dev, skb); 373 else 374 queue_index = __netdev_pick_tx(dev, skb); 375 queue_index = dev_cap_txqueue(dev, queue_index); 376 } 377 378 skb_set_queue_mapping(skb, queue_index); 379 return netdev_get_tx_queue(dev, queue_index); 380 } 381 382 static int __init initialize_hashrnd(void) 383 { 384 get_random_bytes(&hashrnd, sizeof(hashrnd)); 385 return 0; 386 } 387 388 late_initcall_sync(initialize_hashrnd); 389