flow_dissector.c 9.8 KB

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