rps.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, and
  24. * a hardware filter index.
  25. */
  26. struct rps_dev_flow {
  27. u16 cpu;
  28. u16 filter;
  29. unsigned int last_qtail;
  30. };
  31. #define RPS_NO_FILTER 0xffff
  32. /*
  33. * The rps_dev_flow_table structure contains a table of flow mappings.
  34. */
  35. struct rps_dev_flow_table {
  36. unsigned int mask;
  37. struct rcu_head rcu;
  38. struct rps_dev_flow flows[];
  39. };
  40. #define RPS_DEV_FLOW_TABLE_SIZE(_num) (sizeof(struct rps_dev_flow_table) + \
  41. ((_num) * sizeof(struct rps_dev_flow)))
  42. /*
  43. * The rps_sock_flow_table contains mappings of flows to the last CPU
  44. * on which they were processed by the application (set in recvmsg).
  45. * Each entry is a 32bit value. Upper part is the high-order bits
  46. * of flow hash, lower part is CPU number.
  47. * rps_cpu_mask is used to partition the space, depending on number of
  48. * possible CPUs : rps_cpu_mask = roundup_pow_of_two(nr_cpu_ids) - 1
  49. * For example, if 64 CPUs are possible, rps_cpu_mask = 0x3f,
  50. * meaning we use 32-6=26 bits for the hash.
  51. */
  52. struct rps_sock_flow_table {
  53. u32 mask;
  54. u32 ents[] ____cacheline_aligned_in_smp;
  55. };
  56. #define RPS_SOCK_FLOW_TABLE_SIZE(_num) (offsetof(struct rps_sock_flow_table, ents[_num]))
  57. #define RPS_NO_CPU 0xffff
  58. static inline void rps_record_sock_flow(struct rps_sock_flow_table *table,
  59. u32 hash)
  60. {
  61. unsigned int index = hash & table->mask;
  62. u32 val = hash & ~net_hotdata.rps_cpu_mask;
  63. /* We only give a hint, preemption can change CPU under us */
  64. val |= raw_smp_processor_id();
  65. /* The following WRITE_ONCE() is paired with the READ_ONCE()
  66. * here, and another one in get_rps_cpu().
  67. */
  68. if (READ_ONCE(table->ents[index]) != val)
  69. WRITE_ONCE(table->ents[index], val);
  70. }
  71. #endif /* CONFIG_RPS */
  72. static inline void sock_rps_record_flow_hash(__u32 hash)
  73. {
  74. #ifdef CONFIG_RPS
  75. struct rps_sock_flow_table *sock_flow_table;
  76. if (!hash)
  77. return;
  78. rcu_read_lock();
  79. sock_flow_table = rcu_dereference(net_hotdata.rps_sock_flow_table);
  80. if (sock_flow_table)
  81. rps_record_sock_flow(sock_flow_table, hash);
  82. rcu_read_unlock();
  83. #endif
  84. }
  85. static inline void sock_rps_record_flow(const struct sock *sk)
  86. {
  87. #ifdef CONFIG_RPS
  88. if (static_branch_unlikely(&rfs_needed)) {
  89. /* Reading sk->sk_rxhash might incur an expensive cache line
  90. * miss.
  91. *
  92. * TCP_ESTABLISHED does cover almost all states where RFS
  93. * might be useful, and is cheaper [1] than testing :
  94. * IPv4: inet_sk(sk)->inet_daddr
  95. * IPv6: ipv6_addr_any(&sk->sk_v6_daddr)
  96. * OR an additional socket flag
  97. * [1] : sk_state and sk_prot are in the same cache line.
  98. */
  99. if (sk->sk_state == TCP_ESTABLISHED) {
  100. /* This READ_ONCE() is paired with the WRITE_ONCE()
  101. * from sock_rps_save_rxhash() and sock_rps_reset_rxhash().
  102. */
  103. sock_rps_record_flow_hash(READ_ONCE(sk->sk_rxhash));
  104. }
  105. }
  106. #endif
  107. }
  108. static inline u32 rps_input_queue_tail_incr(struct softnet_data *sd)
  109. {
  110. #ifdef CONFIG_RPS
  111. return ++sd->input_queue_tail;
  112. #else
  113. return 0;
  114. #endif
  115. }
  116. static inline void rps_input_queue_tail_save(u32 *dest, u32 tail)
  117. {
  118. #ifdef CONFIG_RPS
  119. WRITE_ONCE(*dest, tail);
  120. #endif
  121. }
  122. static inline void rps_input_queue_head_add(struct softnet_data *sd, int val)
  123. {
  124. #ifdef CONFIG_RPS
  125. WRITE_ONCE(sd->input_queue_head, sd->input_queue_head + val);
  126. #endif
  127. }
  128. static inline void rps_input_queue_head_incr(struct softnet_data *sd)
  129. {
  130. rps_input_queue_head_add(sd, 1);
  131. }
  132. #endif /* _NET_RPS_H */