flow_dissector.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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_TIPC): {
  160. struct {
  161. __be32 pre[3];
  162. __be32 srcnode;
  163. } *hdr, _hdr;
  164. hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
  165. if (!hdr)
  166. return false;
  167. flow->src = hdr->srcnode;
  168. flow->dst = 0;
  169. flow->n_proto = proto;
  170. flow->thoff = (u16)nhoff;
  171. return true;
  172. }
  173. case htons(ETH_P_FCOE):
  174. flow->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
  175. /* fall through */
  176. default:
  177. return false;
  178. }
  179. switch (ip_proto) {
  180. case IPPROTO_GRE: {
  181. struct gre_hdr {
  182. __be16 flags;
  183. __be16 proto;
  184. } *hdr, _hdr;
  185. hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
  186. if (!hdr)
  187. return false;
  188. /*
  189. * Only look inside GRE if version zero and no
  190. * routing
  191. */
  192. if (!(hdr->flags & (GRE_VERSION|GRE_ROUTING))) {
  193. proto = hdr->proto;
  194. nhoff += 4;
  195. if (hdr->flags & GRE_CSUM)
  196. nhoff += 4;
  197. if (hdr->flags & GRE_KEY)
  198. nhoff += 4;
  199. if (hdr->flags & GRE_SEQ)
  200. nhoff += 4;
  201. if (proto == htons(ETH_P_TEB)) {
  202. const struct ethhdr *eth;
  203. struct ethhdr _eth;
  204. eth = __skb_header_pointer(skb, nhoff,
  205. sizeof(_eth),
  206. data, hlen, &_eth);
  207. if (!eth)
  208. return false;
  209. proto = eth->h_proto;
  210. nhoff += sizeof(*eth);
  211. }
  212. goto again;
  213. }
  214. break;
  215. }
  216. case IPPROTO_IPIP:
  217. proto = htons(ETH_P_IP);
  218. goto ip;
  219. case IPPROTO_IPV6:
  220. proto = htons(ETH_P_IPV6);
  221. goto ipv6;
  222. default:
  223. break;
  224. }
  225. flow->n_proto = proto;
  226. flow->ip_proto = ip_proto;
  227. flow->thoff = (u16) nhoff;
  228. /* unless skb is set we don't need to record port info */
  229. if (skb)
  230. flow->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
  231. data, hlen);
  232. return true;
  233. }
  234. EXPORT_SYMBOL(__skb_flow_dissect);
  235. static u32 hashrnd __read_mostly;
  236. static __always_inline void __flow_hash_secret_init(void)
  237. {
  238. net_get_random_once(&hashrnd, sizeof(hashrnd));
  239. }
  240. static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c, u32 keyval)
  241. {
  242. return jhash_3words(a, b, c, keyval);
  243. }
  244. static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
  245. {
  246. u32 hash;
  247. /* get a consistent hash (same value on both flow directions) */
  248. if (((__force u32)keys->dst < (__force u32)keys->src) ||
  249. (((__force u32)keys->dst == (__force u32)keys->src) &&
  250. ((__force u16)keys->port16[1] < (__force u16)keys->port16[0]))) {
  251. swap(keys->dst, keys->src);
  252. swap(keys->port16[0], keys->port16[1]);
  253. }
  254. hash = __flow_hash_3words((__force u32)keys->dst,
  255. (__force u32)keys->src,
  256. (__force u32)keys->ports,
  257. keyval);
  258. if (!hash)
  259. hash = 1;
  260. return hash;
  261. }
  262. u32 flow_hash_from_keys(struct flow_keys *keys)
  263. {
  264. __flow_hash_secret_init();
  265. return __flow_hash_from_keys(keys, hashrnd);
  266. }
  267. EXPORT_SYMBOL(flow_hash_from_keys);
  268. static inline u32 ___skb_get_hash(const struct sk_buff *skb,
  269. struct flow_keys *keys, u32 keyval)
  270. {
  271. if (!skb_flow_dissect(skb, keys))
  272. return 0;
  273. return __flow_hash_from_keys(keys, keyval);
  274. }
  275. struct _flow_keys_digest_data {
  276. __be16 n_proto;
  277. u8 ip_proto;
  278. u8 padding;
  279. __be32 ports;
  280. __be32 src;
  281. __be32 dst;
  282. };
  283. void make_flow_keys_digest(struct flow_keys_digest *digest,
  284. const struct flow_keys *flow)
  285. {
  286. struct _flow_keys_digest_data *data =
  287. (struct _flow_keys_digest_data *)digest;
  288. BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
  289. memset(digest, 0, sizeof(*digest));
  290. data->n_proto = flow->n_proto;
  291. data->ip_proto = flow->ip_proto;
  292. data->ports = flow->ports;
  293. data->src = flow->src;
  294. data->dst = flow->dst;
  295. }
  296. EXPORT_SYMBOL(make_flow_keys_digest);
  297. /*
  298. * __skb_get_hash: calculate a flow hash based on src/dst addresses
  299. * and src/dst port numbers. Sets hash in skb to non-zero hash value
  300. * on success, zero indicates no valid hash. Also, sets l4_hash in skb
  301. * if hash is a canonical 4-tuple hash over transport ports.
  302. */
  303. void __skb_get_hash(struct sk_buff *skb)
  304. {
  305. struct flow_keys keys;
  306. u32 hash;
  307. __flow_hash_secret_init();
  308. hash = ___skb_get_hash(skb, &keys, hashrnd);
  309. if (!hash)
  310. return;
  311. if (keys.ports)
  312. skb->l4_hash = 1;
  313. skb->sw_hash = 1;
  314. skb->hash = hash;
  315. }
  316. EXPORT_SYMBOL(__skb_get_hash);
  317. __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
  318. {
  319. struct flow_keys keys;
  320. return ___skb_get_hash(skb, &keys, perturb);
  321. }
  322. EXPORT_SYMBOL(skb_get_hash_perturb);
  323. /*
  324. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  325. * to be used as a distribution range.
  326. */
  327. u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
  328. unsigned int num_tx_queues)
  329. {
  330. u32 hash;
  331. u16 qoffset = 0;
  332. u16 qcount = num_tx_queues;
  333. if (skb_rx_queue_recorded(skb)) {
  334. hash = skb_get_rx_queue(skb);
  335. while (unlikely(hash >= num_tx_queues))
  336. hash -= num_tx_queues;
  337. return hash;
  338. }
  339. if (dev->num_tc) {
  340. u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
  341. qoffset = dev->tc_to_txq[tc].offset;
  342. qcount = dev->tc_to_txq[tc].count;
  343. }
  344. return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
  345. }
  346. EXPORT_SYMBOL(__skb_tx_hash);
  347. u32 __skb_get_poff(const struct sk_buff *skb, void *data,
  348. const struct flow_keys *keys, int hlen)
  349. {
  350. u32 poff = keys->thoff;
  351. switch (keys->ip_proto) {
  352. case IPPROTO_TCP: {
  353. /* access doff as u8 to avoid unaligned access */
  354. const u8 *doff;
  355. u8 _doff;
  356. doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
  357. data, hlen, &_doff);
  358. if (!doff)
  359. return poff;
  360. poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
  361. break;
  362. }
  363. case IPPROTO_UDP:
  364. case IPPROTO_UDPLITE:
  365. poff += sizeof(struct udphdr);
  366. break;
  367. /* For the rest, we do not really care about header
  368. * extensions at this point for now.
  369. */
  370. case IPPROTO_ICMP:
  371. poff += sizeof(struct icmphdr);
  372. break;
  373. case IPPROTO_ICMPV6:
  374. poff += sizeof(struct icmp6hdr);
  375. break;
  376. case IPPROTO_IGMP:
  377. poff += sizeof(struct igmphdr);
  378. break;
  379. case IPPROTO_DCCP:
  380. poff += sizeof(struct dccp_hdr);
  381. break;
  382. case IPPROTO_SCTP:
  383. poff += sizeof(struct sctphdr);
  384. break;
  385. }
  386. return poff;
  387. }
  388. /* skb_get_poff() returns the offset to the payload as far as it could
  389. * be dissected. The main user is currently BPF, so that we can dynamically
  390. * truncate packets without needing to push actual payload to the user
  391. * space and can analyze headers only, instead.
  392. */
  393. u32 skb_get_poff(const struct sk_buff *skb)
  394. {
  395. struct flow_keys keys;
  396. if (!skb_flow_dissect(skb, &keys))
  397. return 0;
  398. return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
  399. }
  400. static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
  401. {
  402. #ifdef CONFIG_XPS
  403. struct xps_dev_maps *dev_maps;
  404. struct xps_map *map;
  405. int queue_index = -1;
  406. rcu_read_lock();
  407. dev_maps = rcu_dereference(dev->xps_maps);
  408. if (dev_maps) {
  409. map = rcu_dereference(
  410. dev_maps->cpu_map[skb->sender_cpu - 1]);
  411. if (map) {
  412. if (map->len == 1)
  413. queue_index = map->queues[0];
  414. else
  415. queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
  416. map->len)];
  417. if (unlikely(queue_index >= dev->real_num_tx_queues))
  418. queue_index = -1;
  419. }
  420. }
  421. rcu_read_unlock();
  422. return queue_index;
  423. #else
  424. return -1;
  425. #endif
  426. }
  427. static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
  428. {
  429. struct sock *sk = skb->sk;
  430. int queue_index = sk_tx_queue_get(sk);
  431. if (queue_index < 0 || skb->ooo_okay ||
  432. queue_index >= dev->real_num_tx_queues) {
  433. int new_index = get_xps_queue(dev, skb);
  434. if (new_index < 0)
  435. new_index = skb_tx_hash(dev, skb);
  436. if (queue_index != new_index && sk &&
  437. rcu_access_pointer(sk->sk_dst_cache))
  438. sk_tx_queue_set(sk, new_index);
  439. queue_index = new_index;
  440. }
  441. return queue_index;
  442. }
  443. struct netdev_queue *netdev_pick_tx(struct net_device *dev,
  444. struct sk_buff *skb,
  445. void *accel_priv)
  446. {
  447. int queue_index = 0;
  448. #ifdef CONFIG_XPS
  449. if (skb->sender_cpu == 0)
  450. skb->sender_cpu = raw_smp_processor_id() + 1;
  451. #endif
  452. if (dev->real_num_tx_queues != 1) {
  453. const struct net_device_ops *ops = dev->netdev_ops;
  454. if (ops->ndo_select_queue)
  455. queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
  456. __netdev_pick_tx);
  457. else
  458. queue_index = __netdev_pick_tx(dev, skb);
  459. if (!accel_priv)
  460. queue_index = netdev_cap_txqueue(dev, queue_index);
  461. }
  462. skb_set_queue_mapping(skb, queue_index);
  463. return netdev_get_tx_queue(dev, queue_index);
  464. }