flow_dissector.c 9.6 KB

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