flow_dissector.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/if_tunnel.h>
  9. #include <linux/if_pppox.h>
  10. #include <linux/ppp_defs.h>
  11. #include <net/flow_keys.h>
  12. /* copy saddr & daddr, possibly using 64bit load/store
  13. * Equivalent to : flow->src = iph->saddr;
  14. * flow->dst = iph->daddr;
  15. */
  16. static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *iph)
  17. {
  18. BUILD_BUG_ON(offsetof(typeof(*flow), dst) !=
  19. offsetof(typeof(*flow), src) + sizeof(flow->src));
  20. memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
  21. }
  22. bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
  23. {
  24. int poff, nhoff = skb_network_offset(skb);
  25. u8 ip_proto;
  26. __be16 proto = skb->protocol;
  27. memset(flow, 0, sizeof(*flow));
  28. again:
  29. switch (proto) {
  30. case __constant_htons(ETH_P_IP): {
  31. const struct iphdr *iph;
  32. struct iphdr _iph;
  33. ip:
  34. iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
  35. if (!iph)
  36. return false;
  37. if (ip_is_fragment(iph))
  38. ip_proto = 0;
  39. else
  40. ip_proto = iph->protocol;
  41. iph_to_flow_copy_addrs(flow, iph);
  42. nhoff += iph->ihl * 4;
  43. break;
  44. }
  45. case __constant_htons(ETH_P_IPV6): {
  46. const struct ipv6hdr *iph;
  47. struct ipv6hdr _iph;
  48. ipv6:
  49. iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
  50. if (!iph)
  51. return false;
  52. ip_proto = iph->nexthdr;
  53. flow->src = (__force __be32)ipv6_addr_hash(&iph->saddr);
  54. flow->dst = (__force __be32)ipv6_addr_hash(&iph->daddr);
  55. nhoff += sizeof(struct ipv6hdr);
  56. break;
  57. }
  58. case __constant_htons(ETH_P_8021Q): {
  59. const struct vlan_hdr *vlan;
  60. struct vlan_hdr _vlan;
  61. vlan = skb_header_pointer(skb, nhoff, sizeof(_vlan), &_vlan);
  62. if (!vlan)
  63. return false;
  64. proto = vlan->h_vlan_encapsulated_proto;
  65. nhoff += sizeof(*vlan);
  66. goto again;
  67. }
  68. case __constant_htons(ETH_P_PPP_SES): {
  69. struct {
  70. struct pppoe_hdr hdr;
  71. __be16 proto;
  72. } *hdr, _hdr;
  73. hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
  74. if (!hdr)
  75. return false;
  76. proto = hdr->proto;
  77. nhoff += PPPOE_SES_HLEN;
  78. switch (proto) {
  79. case __constant_htons(PPP_IP):
  80. goto ip;
  81. case __constant_htons(PPP_IPV6):
  82. goto ipv6;
  83. default:
  84. return false;
  85. }
  86. }
  87. default:
  88. return false;
  89. }
  90. switch (ip_proto) {
  91. case IPPROTO_GRE: {
  92. struct gre_hdr {
  93. __be16 flags;
  94. __be16 proto;
  95. } *hdr, _hdr;
  96. hdr = skb_header_pointer(skb, nhoff, sizeof(_hdr), &_hdr);
  97. if (!hdr)
  98. return false;
  99. /*
  100. * Only look inside GRE if version zero and no
  101. * routing
  102. */
  103. if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
  104. proto = hdr->proto;
  105. nhoff += 4;
  106. if (hdr->flags & GRE_CSUM)
  107. nhoff += 4;
  108. if (hdr->flags & GRE_KEY)
  109. nhoff += 4;
  110. if (hdr->flags & GRE_SEQ)
  111. nhoff += 4;
  112. if (proto == htons(ETH_P_TEB)) {
  113. const struct ethhdr *eth;
  114. struct ethhdr _eth;
  115. eth = skb_header_pointer(skb, nhoff,
  116. sizeof(_eth), &_eth);
  117. if (!eth)
  118. return false;
  119. proto = eth->h_proto;
  120. nhoff += sizeof(*eth);
  121. }
  122. goto again;
  123. }
  124. break;
  125. }
  126. case IPPROTO_IPIP:
  127. goto again;
  128. default:
  129. break;
  130. }
  131. flow->ip_proto = ip_proto;
  132. poff = proto_ports_offset(ip_proto);
  133. if (poff >= 0) {
  134. __be32 *ports, _ports;
  135. nhoff += poff;
  136. ports = skb_header_pointer(skb, nhoff, sizeof(_ports), &_ports);
  137. if (ports)
  138. flow->ports = *ports;
  139. }
  140. return true;
  141. }
  142. EXPORT_SYMBOL(skb_flow_dissect);
  143. static u32 hashrnd __read_mostly;
  144. /*
  145. * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
  146. * and src/dst port numbers. Sets rxhash in skb to non-zero hash value
  147. * on success, zero indicates no valid hash. Also, sets l4_rxhash in skb
  148. * if hash is a canonical 4-tuple hash over transport ports.
  149. */
  150. void __skb_get_rxhash(struct sk_buff *skb)
  151. {
  152. struct flow_keys keys;
  153. u32 hash;
  154. if (!skb_flow_dissect(skb, &keys))
  155. return;
  156. if (keys.ports)
  157. skb->l4_rxhash = 1;
  158. /* get a consistent hash (same value on both flow directions) */
  159. if (((__force u32)keys.dst < (__force u32)keys.src) ||
  160. (((__force u32)keys.dst == (__force u32)keys.src) &&
  161. ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
  162. swap(keys.dst, keys.src);
  163. swap(keys.port16[0], keys.port16[1]);
  164. }
  165. hash = jhash_3words((__force u32)keys.dst,
  166. (__force u32)keys.src,
  167. (__force u32)keys.ports, hashrnd);
  168. if (!hash)
  169. hash = 1;
  170. skb->rxhash = hash;
  171. }
  172. EXPORT_SYMBOL(__skb_get_rxhash);
  173. /*
  174. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  175. * to be used as a distribution range.
  176. */
  177. u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
  178. unsigned int num_tx_queues)
  179. {
  180. u32 hash;
  181. u16 qoffset = 0;
  182. u16 qcount = num_tx_queues;
  183. if (skb_rx_queue_recorded(skb)) {
  184. hash = skb_get_rx_queue(skb);
  185. while (unlikely(hash >= num_tx_queues))
  186. hash -= num_tx_queues;
  187. return hash;
  188. }
  189. if (dev->num_tc) {
  190. u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
  191. qoffset = dev->tc_to_txq[tc].offset;
  192. qcount = dev->tc_to_txq[tc].count;
  193. }
  194. if (skb->sk && skb->sk->sk_hash)
  195. hash = skb->sk->sk_hash;
  196. else
  197. hash = (__force u16) skb->protocol;
  198. hash = jhash_1word(hash, hashrnd);
  199. return (u16) (((u64) hash * qcount) >> 32) + qoffset;
  200. }
  201. EXPORT_SYMBOL(__skb_tx_hash);
  202. static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
  203. {
  204. if (unlikely(queue_index >= dev->real_num_tx_queues)) {
  205. net_warn_ratelimited("%s selects TX queue %d, but real number of TX queues is %d\n",
  206. dev->name, queue_index,
  207. dev->real_num_tx_queues);
  208. return 0;
  209. }
  210. return queue_index;
  211. }
  212. static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
  213. {
  214. #ifdef CONFIG_XPS
  215. struct xps_dev_maps *dev_maps;
  216. struct xps_map *map;
  217. int queue_index = -1;
  218. rcu_read_lock();
  219. dev_maps = rcu_dereference(dev->xps_maps);
  220. if (dev_maps) {
  221. map = rcu_dereference(
  222. dev_maps->cpu_map[raw_smp_processor_id()]);
  223. if (map) {
  224. if (map->len == 1)
  225. queue_index = map->queues[0];
  226. else {
  227. u32 hash;
  228. if (skb->sk && skb->sk->sk_hash)
  229. hash = skb->sk->sk_hash;
  230. else
  231. hash = (__force u16) skb->protocol ^
  232. skb->rxhash;
  233. hash = jhash_1word(hash, hashrnd);
  234. queue_index = map->queues[
  235. ((u64)hash * map->len) >> 32];
  236. }
  237. if (unlikely(queue_index >= dev->real_num_tx_queues))
  238. queue_index = -1;
  239. }
  240. }
  241. rcu_read_unlock();
  242. return queue_index;
  243. #else
  244. return -1;
  245. #endif
  246. }
  247. u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
  248. {
  249. struct sock *sk = skb->sk;
  250. int queue_index = sk_tx_queue_get(sk);
  251. if (queue_index < 0 || skb->ooo_okay ||
  252. queue_index >= dev->real_num_tx_queues) {
  253. int new_index = get_xps_queue(dev, skb);
  254. if (new_index < 0)
  255. new_index = skb_tx_hash(dev, skb);
  256. if (queue_index != new_index && sk) {
  257. struct dst_entry *dst =
  258. rcu_dereference_check(sk->sk_dst_cache, 1);
  259. if (dst && skb_dst(skb) == dst)
  260. sk_tx_queue_set(sk, queue_index);
  261. }
  262. queue_index = new_index;
  263. }
  264. return queue_index;
  265. }
  266. EXPORT_SYMBOL(__netdev_pick_tx);
  267. struct netdev_queue *netdev_pick_tx(struct net_device *dev,
  268. struct sk_buff *skb)
  269. {
  270. int queue_index = 0;
  271. if (dev->real_num_tx_queues != 1) {
  272. const struct net_device_ops *ops = dev->netdev_ops;
  273. if (ops->ndo_select_queue)
  274. queue_index = ops->ndo_select_queue(dev, skb);
  275. else
  276. queue_index = __netdev_pick_tx(dev, skb);
  277. queue_index = dev_cap_txqueue(dev, queue_index);
  278. }
  279. skb_set_queue_mapping(skb, queue_index);
  280. return netdev_get_tx_queue(dev, queue_index);
  281. }
  282. static int __init initialize_hashrnd(void)
  283. {
  284. get_random_bytes(&hashrnd, sizeof(hashrnd));
  285. return 0;
  286. }
  287. late_initcall_sync(initialize_hashrnd);