flow_dissector.c 8.9 KB

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