busy_poll.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * net busy poll support
  3. * Copyright(c) 2013 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * Author: Eliezer Tamir
  19. *
  20. * Contact Information:
  21. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  22. */
  23. #ifndef _LINUX_NET_BUSY_POLL_H
  24. #define _LINUX_NET_BUSY_POLL_H
  25. #include <linux/netdevice.h>
  26. #include <linux/sched/clock.h>
  27. #include <linux/sched/signal.h>
  28. #include <net/ip.h>
  29. /* 0 - Reserved to indicate value not set
  30. * 1..NR_CPUS - Reserved for sender_cpu
  31. * NR_CPUS+1..~0 - Region available for NAPI IDs
  32. */
  33. #define MIN_NAPI_ID ((unsigned int)(NR_CPUS + 1))
  34. #ifdef CONFIG_NET_RX_BUSY_POLL
  35. struct napi_struct;
  36. extern unsigned int sysctl_net_busy_read __read_mostly;
  37. extern unsigned int sysctl_net_busy_poll __read_mostly;
  38. static inline bool net_busy_loop_on(void)
  39. {
  40. return sysctl_net_busy_poll;
  41. }
  42. static inline bool sk_can_busy_loop(const struct sock *sk)
  43. {
  44. return sk->sk_ll_usec && !signal_pending(current);
  45. }
  46. bool sk_busy_loop_end(void *p, unsigned long start_time);
  47. void napi_busy_loop(unsigned int napi_id,
  48. bool (*loop_end)(void *, unsigned long),
  49. void *loop_end_arg);
  50. #else /* CONFIG_NET_RX_BUSY_POLL */
  51. static inline unsigned long net_busy_loop_on(void)
  52. {
  53. return 0;
  54. }
  55. static inline bool sk_can_busy_loop(struct sock *sk)
  56. {
  57. return false;
  58. }
  59. #endif /* CONFIG_NET_RX_BUSY_POLL */
  60. static inline unsigned long busy_loop_current_time(void)
  61. {
  62. #ifdef CONFIG_NET_RX_BUSY_POLL
  63. return (unsigned long)(local_clock() >> 10);
  64. #else
  65. return 0;
  66. #endif
  67. }
  68. /* in poll/select we use the global sysctl_net_ll_poll value */
  69. static inline bool busy_loop_timeout(unsigned long start_time)
  70. {
  71. #ifdef CONFIG_NET_RX_BUSY_POLL
  72. unsigned long bp_usec = READ_ONCE(sysctl_net_busy_poll);
  73. if (bp_usec) {
  74. unsigned long end_time = start_time + bp_usec;
  75. unsigned long now = busy_loop_current_time();
  76. return time_after(now, end_time);
  77. }
  78. #endif
  79. return true;
  80. }
  81. static inline bool sk_busy_loop_timeout(struct sock *sk,
  82. unsigned long start_time)
  83. {
  84. #ifdef CONFIG_NET_RX_BUSY_POLL
  85. unsigned long bp_usec = READ_ONCE(sk->sk_ll_usec);
  86. if (bp_usec) {
  87. unsigned long end_time = start_time + bp_usec;
  88. unsigned long now = busy_loop_current_time();
  89. return time_after(now, end_time);
  90. }
  91. #endif
  92. return true;
  93. }
  94. static inline void sk_busy_loop(struct sock *sk, int nonblock)
  95. {
  96. #ifdef CONFIG_NET_RX_BUSY_POLL
  97. unsigned int napi_id = READ_ONCE(sk->sk_napi_id);
  98. if (napi_id >= MIN_NAPI_ID)
  99. napi_busy_loop(napi_id, nonblock ? NULL : sk_busy_loop_end, sk);
  100. #endif
  101. }
  102. /* used in the NIC receive handler to mark the skb */
  103. static inline void skb_mark_napi_id(struct sk_buff *skb,
  104. struct napi_struct *napi)
  105. {
  106. #ifdef CONFIG_NET_RX_BUSY_POLL
  107. skb->napi_id = napi->napi_id;
  108. #endif
  109. }
  110. /* used in the protocol hanlder to propagate the napi_id to the socket */
  111. static inline void sk_mark_napi_id(struct sock *sk, const struct sk_buff *skb)
  112. {
  113. #ifdef CONFIG_NET_RX_BUSY_POLL
  114. WRITE_ONCE(sk->sk_napi_id, skb->napi_id);
  115. #endif
  116. }
  117. /* variant used for unconnected sockets */
  118. static inline void sk_mark_napi_id_once(struct sock *sk,
  119. const struct sk_buff *skb)
  120. {
  121. #ifdef CONFIG_NET_RX_BUSY_POLL
  122. if (!READ_ONCE(sk->sk_napi_id))
  123. WRITE_ONCE(sk->sk_napi_id, skb->napi_id);
  124. #endif
  125. }
  126. #endif /* _LINUX_NET_BUSY_POLL_H */