flow_dissector.c 8.9 KB

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