flow_dissector.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. goto again;
  113. }
  114. break;
  115. }
  116. case IPPROTO_IPIP:
  117. goto again;
  118. default:
  119. break;
  120. }
  121. flow->ip_proto = ip_proto;
  122. poff = proto_ports_offset(ip_proto);
  123. if (poff >= 0) {
  124. __be32 *ports, _ports;
  125. nhoff += poff;
  126. ports = skb_header_pointer(skb, nhoff, sizeof(_ports), &_ports);
  127. if (ports)
  128. flow->ports = *ports;
  129. }
  130. return true;
  131. }
  132. EXPORT_SYMBOL(skb_flow_dissect);
  133. static u32 hashrnd __read_mostly;
  134. /*
  135. * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
  136. * and src/dst port numbers. Sets rxhash in skb to non-zero hash value
  137. * on success, zero indicates no valid hash. Also, sets l4_rxhash in skb
  138. * if hash is a canonical 4-tuple hash over transport ports.
  139. */
  140. void __skb_get_rxhash(struct sk_buff *skb)
  141. {
  142. struct flow_keys keys;
  143. u32 hash;
  144. if (!skb_flow_dissect(skb, &keys))
  145. return;
  146. if (keys.ports)
  147. skb->l4_rxhash = 1;
  148. /* get a consistent hash (same value on both flow directions) */
  149. if (((__force u32)keys.dst < (__force u32)keys.src) ||
  150. (((__force u32)keys.dst == (__force u32)keys.src) &&
  151. ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) {
  152. swap(keys.dst, keys.src);
  153. swap(keys.port16[0], keys.port16[1]);
  154. }
  155. hash = jhash_3words((__force u32)keys.dst,
  156. (__force u32)keys.src,
  157. (__force u32)keys.ports, hashrnd);
  158. if (!hash)
  159. hash = 1;
  160. skb->rxhash = hash;
  161. }
  162. EXPORT_SYMBOL(__skb_get_rxhash);
  163. /*
  164. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  165. * to be used as a distribution range.
  166. */
  167. u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
  168. unsigned int num_tx_queues)
  169. {
  170. u32 hash;
  171. u16 qoffset = 0;
  172. u16 qcount = num_tx_queues;
  173. if (skb_rx_queue_recorded(skb)) {
  174. hash = skb_get_rx_queue(skb);
  175. while (unlikely(hash >= num_tx_queues))
  176. hash -= num_tx_queues;
  177. return hash;
  178. }
  179. if (dev->num_tc) {
  180. u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
  181. qoffset = dev->tc_to_txq[tc].offset;
  182. qcount = dev->tc_to_txq[tc].count;
  183. }
  184. if (skb->sk && skb->sk->sk_hash)
  185. hash = skb->sk->sk_hash;
  186. else
  187. hash = (__force u16) skb->protocol;
  188. hash = jhash_1word(hash, hashrnd);
  189. return (u16) (((u64) hash * qcount) >> 32) + qoffset;
  190. }
  191. EXPORT_SYMBOL(__skb_tx_hash);
  192. static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index)
  193. {
  194. if (unlikely(queue_index >= dev->real_num_tx_queues)) {
  195. net_warn_ratelimited("%s selects TX queue %d, but real number of TX queues is %d\n",
  196. dev->name, queue_index,
  197. dev->real_num_tx_queues);
  198. return 0;
  199. }
  200. return queue_index;
  201. }
  202. static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
  203. {
  204. #ifdef CONFIG_XPS
  205. struct xps_dev_maps *dev_maps;
  206. struct xps_map *map;
  207. int queue_index = -1;
  208. rcu_read_lock();
  209. dev_maps = rcu_dereference(dev->xps_maps);
  210. if (dev_maps) {
  211. map = rcu_dereference(
  212. dev_maps->cpu_map[raw_smp_processor_id()]);
  213. if (map) {
  214. if (map->len == 1)
  215. queue_index = map->queues[0];
  216. else {
  217. u32 hash;
  218. if (skb->sk && skb->sk->sk_hash)
  219. hash = skb->sk->sk_hash;
  220. else
  221. hash = (__force u16) skb->protocol ^
  222. skb->rxhash;
  223. hash = jhash_1word(hash, hashrnd);
  224. queue_index = map->queues[
  225. ((u64)hash * map->len) >> 32];
  226. }
  227. if (unlikely(queue_index >= dev->real_num_tx_queues))
  228. queue_index = -1;
  229. }
  230. }
  231. rcu_read_unlock();
  232. return queue_index;
  233. #else
  234. return -1;
  235. #endif
  236. }
  237. u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
  238. {
  239. struct sock *sk = skb->sk;
  240. int queue_index = sk_tx_queue_get(sk);
  241. if (queue_index < 0 || skb->ooo_okay ||
  242. queue_index >= dev->real_num_tx_queues) {
  243. int new_index = get_xps_queue(dev, skb);
  244. if (new_index < 0)
  245. new_index = skb_tx_hash(dev, skb);
  246. if (queue_index != new_index && sk) {
  247. struct dst_entry *dst =
  248. rcu_dereference_check(sk->sk_dst_cache, 1);
  249. if (dst && skb_dst(skb) == dst)
  250. sk_tx_queue_set(sk, queue_index);
  251. }
  252. queue_index = new_index;
  253. }
  254. return queue_index;
  255. }
  256. EXPORT_SYMBOL(__netdev_pick_tx);
  257. struct netdev_queue *netdev_pick_tx(struct net_device *dev,
  258. struct sk_buff *skb)
  259. {
  260. int queue_index = 0;
  261. if (dev->real_num_tx_queues != 1) {
  262. const struct net_device_ops *ops = dev->netdev_ops;
  263. if (ops->ndo_select_queue)
  264. queue_index = ops->ndo_select_queue(dev, skb);
  265. else
  266. queue_index = __netdev_pick_tx(dev, skb);
  267. queue_index = dev_cap_txqueue(dev, queue_index);
  268. }
  269. skb_set_queue_mapping(skb, queue_index);
  270. return netdev_get_tx_queue(dev, queue_index);
  271. }
  272. static int __init initialize_hashrnd(void)
  273. {
  274. get_random_bytes(&hashrnd, sizeof(hashrnd));
  275. return 0;
  276. }
  277. late_initcall_sync(initialize_hashrnd);