busy_poll.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * net busy poll support
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * Author: Eliezer Tamir
  7. *
  8. * Contact Information:
  9. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  10. */
  11. #ifndef _LINUX_NET_BUSY_POLL_H
  12. #define _LINUX_NET_BUSY_POLL_H
  13. #include <linux/netdevice.h>
  14. #include <linux/sched/clock.h>
  15. #include <linux/sched/signal.h>
  16. #include <net/ip.h>
  17. #include <net/xdp.h>
  18. /* 0 - Reserved to indicate value not set
  19. * 1..NR_CPUS - Reserved for sender_cpu
  20. * NR_CPUS+1..~0 - Region available for NAPI IDs
  21. */
  22. #define MIN_NAPI_ID ((unsigned int)(NR_CPUS + 1))
  23. #define BUSY_POLL_BUDGET 8
  24. #ifdef CONFIG_NET_RX_BUSY_POLL
  25. struct napi_struct;
  26. extern unsigned int sysctl_net_busy_read __read_mostly;
  27. extern unsigned int sysctl_net_busy_poll __read_mostly;
  28. static inline bool net_busy_loop_on(void)
  29. {
  30. return READ_ONCE(sysctl_net_busy_poll);
  31. }
  32. static inline bool sk_can_busy_loop(const struct sock *sk)
  33. {
  34. return READ_ONCE(sk->sk_ll_usec) && !signal_pending(current);
  35. }
  36. bool sk_busy_loop_end(void *p, unsigned long start_time);
  37. void napi_busy_loop(unsigned int napi_id,
  38. bool (*loop_end)(void *, unsigned long),
  39. void *loop_end_arg, bool prefer_busy_poll, u16 budget);
  40. void napi_busy_loop_rcu(unsigned int napi_id,
  41. bool (*loop_end)(void *, unsigned long),
  42. void *loop_end_arg, bool prefer_busy_poll, u16 budget);
  43. #else /* CONFIG_NET_RX_BUSY_POLL */
  44. static inline unsigned long net_busy_loop_on(void)
  45. {
  46. return 0;
  47. }
  48. static inline bool sk_can_busy_loop(struct sock *sk)
  49. {
  50. return false;
  51. }
  52. #endif /* CONFIG_NET_RX_BUSY_POLL */
  53. static inline unsigned long busy_loop_current_time(void)
  54. {
  55. #ifdef CONFIG_NET_RX_BUSY_POLL
  56. return (unsigned long)(ktime_get_ns() >> 10);
  57. #else
  58. return 0;
  59. #endif
  60. }
  61. /* in poll/select we use the global sysctl_net_ll_poll value */
  62. static inline bool busy_loop_timeout(unsigned long start_time)
  63. {
  64. #ifdef CONFIG_NET_RX_BUSY_POLL
  65. unsigned long bp_usec = READ_ONCE(sysctl_net_busy_poll);
  66. if (bp_usec) {
  67. unsigned long end_time = start_time + bp_usec;
  68. unsigned long now = busy_loop_current_time();
  69. return time_after(now, end_time);
  70. }
  71. #endif
  72. return true;
  73. }
  74. static inline bool sk_busy_loop_timeout(struct sock *sk,
  75. unsigned long start_time)
  76. {
  77. #ifdef CONFIG_NET_RX_BUSY_POLL
  78. unsigned long bp_usec = READ_ONCE(sk->sk_ll_usec);
  79. if (bp_usec) {
  80. unsigned long end_time = start_time + bp_usec;
  81. unsigned long now = busy_loop_current_time();
  82. return time_after(now, end_time);
  83. }
  84. #endif
  85. return true;
  86. }
  87. static inline void sk_busy_loop(struct sock *sk, int nonblock)
  88. {
  89. #ifdef CONFIG_NET_RX_BUSY_POLL
  90. unsigned int napi_id = READ_ONCE(sk->sk_napi_id);
  91. if (napi_id >= MIN_NAPI_ID)
  92. napi_busy_loop(napi_id, nonblock ? NULL : sk_busy_loop_end, sk,
  93. READ_ONCE(sk->sk_prefer_busy_poll),
  94. READ_ONCE(sk->sk_busy_poll_budget) ?: BUSY_POLL_BUDGET);
  95. #endif
  96. }
  97. /* used in the NIC receive handler to mark the skb */
  98. static inline void skb_mark_napi_id(struct sk_buff *skb,
  99. struct napi_struct *napi)
  100. {
  101. #ifdef CONFIG_NET_RX_BUSY_POLL
  102. /* If the skb was already marked with a valid NAPI ID, avoid overwriting
  103. * it.
  104. */
  105. if (skb->napi_id < MIN_NAPI_ID)
  106. skb->napi_id = napi->napi_id;
  107. #endif
  108. }
  109. /* used in the protocol handler to propagate the napi_id to the socket */
  110. static inline void sk_mark_napi_id(struct sock *sk, const struct sk_buff *skb)
  111. {
  112. #ifdef CONFIG_NET_RX_BUSY_POLL
  113. if (unlikely(READ_ONCE(sk->sk_napi_id) != skb->napi_id))
  114. WRITE_ONCE(sk->sk_napi_id, skb->napi_id);
  115. #endif
  116. sk_rx_queue_update(sk, skb);
  117. }
  118. /* Variant of sk_mark_napi_id() for passive flow setup,
  119. * as sk->sk_napi_id and sk->sk_rx_queue_mapping content
  120. * needs to be set.
  121. */
  122. static inline void sk_mark_napi_id_set(struct sock *sk,
  123. const struct sk_buff *skb)
  124. {
  125. #ifdef CONFIG_NET_RX_BUSY_POLL
  126. WRITE_ONCE(sk->sk_napi_id, skb->napi_id);
  127. #endif
  128. sk_rx_queue_set(sk, skb);
  129. }
  130. static inline void __sk_mark_napi_id_once(struct sock *sk, unsigned int napi_id)
  131. {
  132. #ifdef CONFIG_NET_RX_BUSY_POLL
  133. if (!READ_ONCE(sk->sk_napi_id))
  134. WRITE_ONCE(sk->sk_napi_id, napi_id);
  135. #endif
  136. }
  137. /* variant used for unconnected sockets */
  138. static inline void sk_mark_napi_id_once(struct sock *sk,
  139. const struct sk_buff *skb)
  140. {
  141. #ifdef CONFIG_NET_RX_BUSY_POLL
  142. __sk_mark_napi_id_once(sk, skb->napi_id);
  143. #endif
  144. }
  145. static inline void sk_mark_napi_id_once_xdp(struct sock *sk,
  146. const struct xdp_buff *xdp)
  147. {
  148. #ifdef CONFIG_NET_RX_BUSY_POLL
  149. __sk_mark_napi_id_once(sk, xdp->rxq->napi_id);
  150. #endif
  151. }
  152. #endif /* _LINUX_NET_BUSY_POLL_H */