flow_dissector.c 9.4 KB

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