rps.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef _NET_RPS_H
  3. #define _NET_RPS_H
  4. #include <linux/types.h>
  5. #include <linux/static_key.h>
  6. #include <net/sock.h>
  7. #include <net/hotdata.h>
  8. #ifdef CONFIG_RPS
  9. extern struct static_key_false rps_needed;
  10. extern struct static_key_false rfs_needed;
  11. /*
  12. * This structure holds an RPS map which can be of variable length. The
  13. * map is an array of CPUs.
  14. */
  15. struct rps_map {
  16. unsigned int len;
  17. struct rcu_head rcu;
  18. u16 cpus[];
  19. };
  20. #define RPS_MAP_SIZE(_num) (sizeof(struct rps_map) + ((_num) * sizeof(u16)))
  21. /*
  22. * The rps_dev_flow structure contains the mapping of a flow to a CPU, the
  23. * tail pointer for that CPU's input queue at the time of last enqueue, a
  24. * hardware filter index, and the hash of the flow if aRFS is enabled.
  25. */
  26. struct rps_dev_flow {
  27. u16 cpu;
  28. u16 filter;
  29. unsigned int last_qtail;
  30. #ifdef CONFIG_RFS_ACCEL
  31. u32 hash;
  32. #endif
  33. };
  34. #define RPS_NO_FILTER 0xffff
  35. /*
  36. * The rps_dev_flow_table structure contains a table of flow mappings.
  37. */
  38. struct rps_dev_flow_table {
  39. u8 log;
  40. struct rcu_head rcu;
  41. struct rps_dev_flow flows[];
  42. };
  43. #define RPS_DEV_FLOW_TABLE_SIZE(_num) (sizeof(struct rps_dev_flow_table) + \
  44. ((_num) * sizeof(struct rps_dev_flow)))
  45. /*
  46. * The rps_sock_flow_table contains mappings of flows to the last CPU
  47. * on which they were processed by the application (set in recvmsg).
  48. * Each entry is a 32bit value. Upper part is the high-order bits
  49. * of flow hash, lower part is CPU number.
  50. * rps_cpu_mask is used to partition the space, depending on number of
  51. * possible CPUs : rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1
  52. * For example, if 64 CPUs are possible, rps_cpu_mask = 0x3f,
  53. * meaning we use 32-6=26 bits for the hash.
  54. */
  55. struct rps_sock_flow_table {
  56. struct rcu_head rcu;
  57. u32 mask;
  58. u32 ents[] ____cacheline_aligned_in_smp;
  59. };
  60. #define RPS_SOCK_FLOW_TABLE_SIZE(_num) (offsetof(struct rps_sock_flow_table, ents[_num]))
  61. #define RPS_NO_CPU 0xffff
  62. static inline void rps_record_sock_flow(struct rps_sock_flow_table *table,
  63. u32 hash)
  64. {
  65. unsigned int index = hash & table->mask;
  66. u32 val = hash & ~net_hotdata.rps_cpu_mask;
  67. /* We only give a hint, preemption can change CPU under us */
  68. val |= raw_smp_processor_id();
  69. /* The following WRITE_ONCE() is paired with the READ_ONCE()
  70. * here, and another one in get_rps_cpu().
  71. */
  72. if (READ_ONCE(table->ents[index]) != val)
  73. WRITE_ONCE(table->ents[index], val);
  74. }
  75. static inline void _sock_rps_record_flow_hash(__u32 hash)
  76. {
  77. struct rps_sock_flow_table *sock_flow_table;
  78. if (!hash)
  79. return;
  80. rcu_read_lock();
  81. sock_flow_table = rcu_dereference(net_hotdata.rps_sock_flow_table);
  82. if (sock_flow_table)
  83. rps_record_sock_flow(sock_flow_table, hash);
  84. rcu_read_unlock();
  85. }
  86. static inline void _sock_rps_record_flow(const struct sock *sk)
  87. {
  88. /* Reading sk->sk_rxhash might incur an expensive cache line
  89. * miss.
  90. *
  91. * TCP_ESTABLISHED does cover almost all states where RFS
  92. * might be useful, and is cheaper [1] than testing :
  93. * IPv4: inet_sk(sk)->inet_daddr
  94. * IPv6: ipv6_addr_any(&sk->sk_v6_daddr)
  95. * OR an additional socket flag
  96. * [1] : sk_state and sk_prot are in the same cache line.
  97. */
  98. if (sk->sk_state == TCP_ESTABLISHED) {
  99. /* This READ_ONCE() is paired with the WRITE_ONCE()
  100. * from sock_rps_save_rxhash() and sock_rps_reset_rxhash().
  101. */
  102. _sock_rps_record_flow_hash(READ_ONCE(sk->sk_rxhash));
  103. }
  104. }
  105. static inline void _sock_rps_delete_flow(const struct sock *sk)
  106. {
  107. struct rps_sock_flow_table *table;
  108. u32 hash, index;
  109. hash = READ_ONCE(sk->sk_rxhash);
  110. if (!hash)
  111. return;
  112. rcu_read_lock();
  113. table = rcu_dereference(net_hotdata.rps_sock_flow_table);
  114. if (table) {
  115. index = hash & table->mask;
  116. if (READ_ONCE(table->ents[index]) != RPS_NO_CPU)
  117. WRITE_ONCE(table->ents[index], RPS_NO_CPU);
  118. }
  119. rcu_read_unlock();
  120. }
  121. #endif /* CONFIG_RPS */
  122. static inline bool rfs_is_needed(void)
  123. {
  124. #ifdef CONFIG_RPS
  125. return static_branch_unlikely(&rfs_needed);
  126. #else
  127. return false;
  128. #endif
  129. }
  130. static inline void sock_rps_record_flow_hash(__u32 hash)
  131. {
  132. #ifdef CONFIG_RPS
  133. if (!rfs_is_needed())
  134. return;
  135. _sock_rps_record_flow_hash(hash);
  136. #endif
  137. }
  138. static inline void sock_rps_record_flow(const struct sock *sk)
  139. {
  140. #ifdef CONFIG_RPS
  141. if (!rfs_is_needed())
  142. return;
  143. _sock_rps_record_flow(sk);
  144. #endif
  145. }
  146. static inline void sock_rps_delete_flow(const struct sock *sk)
  147. {
  148. #ifdef CONFIG_RPS
  149. if (!rfs_is_needed())
  150. return;
  151. _sock_rps_delete_flow(sk);
  152. #endif
  153. }
  154. static inline u32 rps_input_queue_tail_incr(struct softnet_data *sd)
  155. {
  156. #ifdef CONFIG_RPS
  157. return ++sd->input_queue_tail;
  158. #else
  159. return 0;
  160. #endif
  161. }
  162. static inline void rps_input_queue_tail_save(u32 *dest, u32 tail)
  163. {
  164. #ifdef CONFIG_RPS
  165. WRITE_ONCE(*dest, tail);
  166. #endif
  167. }
  168. static inline void rps_input_queue_head_add(struct softnet_data *sd, int val)
  169. {
  170. #ifdef CONFIG_RPS
  171. WRITE_ONCE(sd->input_queue_head, sd->input_queue_head + val);
  172. #endif
  173. }
  174. static inline void rps_input_queue_head_incr(struct softnet_data *sd)
  175. {
  176. rps_input_queue_head_add(sd, 1);
  177. }
  178. #endif /* _NET_RPS_H */