flow_dissector.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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)
  241. {
  242. __flow_hash_secret_init();
  243. return jhash_3words(a, b, c, hashrnd);
  244. }
  245. static inline u32 __flow_hash_from_keys(struct flow_keys *keys)
  246. {
  247. u32 hash;
  248. /* get a consistent hash (same value on both flow directions) */
  249. if (((__force u32)keys->dst < (__force u32)keys->src) ||
  250. (((__force u32)keys->dst == (__force u32)keys->src) &&
  251. ((__force u16)keys->port16[1] < (__force u16)keys->port16[0]))) {
  252. swap(keys->dst, keys->src);
  253. swap(keys->port16[0], keys->port16[1]);
  254. }
  255. hash = __flow_hash_3words((__force u32)keys->dst,
  256. (__force u32)keys->src,
  257. (__force u32)keys->ports);
  258. if (!hash)
  259. hash = 1;
  260. return hash;
  261. }
  262. u32 flow_hash_from_keys(struct flow_keys *keys)
  263. {
  264. return __flow_hash_from_keys(keys);
  265. }
  266. EXPORT_SYMBOL(flow_hash_from_keys);
  267. /*
  268. * __skb_get_hash: calculate a flow hash based on src/dst addresses
  269. * and src/dst port numbers. Sets hash in skb to non-zero hash value
  270. * on success, zero indicates no valid hash. Also, sets l4_hash in skb
  271. * if hash is a canonical 4-tuple hash over transport ports.
  272. */
  273. void __skb_get_hash(struct sk_buff *skb)
  274. {
  275. struct flow_keys keys;
  276. if (!skb_flow_dissect(skb, &keys))
  277. return;
  278. if (keys.ports)
  279. skb->l4_hash = 1;
  280. skb->sw_hash = 1;
  281. skb->hash = __flow_hash_from_keys(&keys);
  282. }
  283. EXPORT_SYMBOL(__skb_get_hash);
  284. /*
  285. * Returns a Tx hash based on the given packet descriptor a Tx queues' number
  286. * to be used as a distribution range.
  287. */
  288. u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
  289. unsigned int num_tx_queues)
  290. {
  291. u32 hash;
  292. u16 qoffset = 0;
  293. u16 qcount = num_tx_queues;
  294. if (skb_rx_queue_recorded(skb)) {
  295. hash = skb_get_rx_queue(skb);
  296. while (unlikely(hash >= num_tx_queues))
  297. hash -= num_tx_queues;
  298. return hash;
  299. }
  300. if (dev->num_tc) {
  301. u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
  302. qoffset = dev->tc_to_txq[tc].offset;
  303. qcount = dev->tc_to_txq[tc].count;
  304. }
  305. return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
  306. }
  307. EXPORT_SYMBOL(__skb_tx_hash);
  308. u32 __skb_get_poff(const struct sk_buff *skb, void *data,
  309. const struct flow_keys *keys, int hlen)
  310. {
  311. u32 poff = keys->thoff;
  312. switch (keys->ip_proto) {
  313. case IPPROTO_TCP: {
  314. /* access doff as u8 to avoid unaligned access */
  315. const u8 *doff;
  316. u8 _doff;
  317. doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
  318. data, hlen, &_doff);
  319. if (!doff)
  320. return poff;
  321. poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
  322. break;
  323. }
  324. case IPPROTO_UDP:
  325. case IPPROTO_UDPLITE:
  326. poff += sizeof(struct udphdr);
  327. break;
  328. /* For the rest, we do not really care about header
  329. * extensions at this point for now.
  330. */
  331. case IPPROTO_ICMP:
  332. poff += sizeof(struct icmphdr);
  333. break;
  334. case IPPROTO_ICMPV6:
  335. poff += sizeof(struct icmp6hdr);
  336. break;
  337. case IPPROTO_IGMP:
  338. poff += sizeof(struct igmphdr);
  339. break;
  340. case IPPROTO_DCCP:
  341. poff += sizeof(struct dccp_hdr);
  342. break;
  343. case IPPROTO_SCTP:
  344. poff += sizeof(struct sctphdr);
  345. break;
  346. }
  347. return poff;
  348. }
  349. /* skb_get_poff() returns the offset to the payload as far as it could
  350. * be dissected. The main user is currently BPF, so that we can dynamically
  351. * truncate packets without needing to push actual payload to the user
  352. * space and can analyze headers only, instead.
  353. */
  354. u32 skb_get_poff(const struct sk_buff *skb)
  355. {
  356. struct flow_keys keys;
  357. if (!skb_flow_dissect(skb, &keys))
  358. return 0;
  359. return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
  360. }
  361. static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
  362. {
  363. #ifdef CONFIG_XPS
  364. struct xps_dev_maps *dev_maps;
  365. struct xps_map *map;
  366. int queue_index = -1;
  367. rcu_read_lock();
  368. dev_maps = rcu_dereference(dev->xps_maps);
  369. if (dev_maps) {
  370. map = rcu_dereference(
  371. dev_maps->cpu_map[skb->sender_cpu - 1]);
  372. if (map) {
  373. if (map->len == 1)
  374. queue_index = map->queues[0];
  375. else
  376. queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
  377. map->len)];
  378. if (unlikely(queue_index >= dev->real_num_tx_queues))
  379. queue_index = -1;
  380. }
  381. }
  382. rcu_read_unlock();
  383. return queue_index;
  384. #else
  385. return -1;
  386. #endif
  387. }
  388. static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
  389. {
  390. struct sock *sk = skb->sk;
  391. int queue_index = sk_tx_queue_get(sk);
  392. if (queue_index < 0 || skb->ooo_okay ||
  393. queue_index >= dev->real_num_tx_queues) {
  394. int new_index = get_xps_queue(dev, skb);
  395. if (new_index < 0)
  396. new_index = skb_tx_hash(dev, skb);
  397. if (queue_index != new_index && sk &&
  398. rcu_access_pointer(sk->sk_dst_cache))
  399. sk_tx_queue_set(sk, new_index);
  400. queue_index = new_index;
  401. }
  402. return queue_index;
  403. }
  404. struct netdev_queue *netdev_pick_tx(struct net_device *dev,
  405. struct sk_buff *skb,
  406. void *accel_priv)
  407. {
  408. int queue_index = 0;
  409. #ifdef CONFIG_XPS
  410. if (skb->sender_cpu == 0)
  411. skb->sender_cpu = raw_smp_processor_id() + 1;
  412. #endif
  413. if (dev->real_num_tx_queues != 1) {
  414. const struct net_device_ops *ops = dev->netdev_ops;
  415. if (ops->ndo_select_queue)
  416. queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
  417. __netdev_pick_tx);
  418. else
  419. queue_index = __netdev_pick_tx(dev, skb);
  420. if (!accel_priv)
  421. queue_index = netdev_cap_txqueue(dev, queue_index);
  422. }
  423. skb_set_queue_mapping(skb, queue_index);
  424. return netdev_get_tx_queue(dev, queue_index);
  425. }