flow_dissector.c 11 KB

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