flow_dissector.c 7.3 KB

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