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