Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 2 | /* |
Eliezer Tamir | 8b80cda | 2013-07-10 17:13:26 +0300 | [diff] [blame] | 3 | * net busy poll support |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 4 | * Copyright(c) 2013 Intel Corporation. |
| 5 | * |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 6 | * Author: Eliezer Tamir |
| 7 | * |
| 8 | * Contact Information: |
| 9 | * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> |
| 10 | */ |
| 11 | |
Eliezer Tamir | 8b80cda | 2013-07-10 17:13:26 +0300 | [diff] [blame] | 12 | #ifndef _LINUX_NET_BUSY_POLL_H |
| 13 | #define _LINUX_NET_BUSY_POLL_H |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 14 | |
| 15 | #include <linux/netdevice.h> |
Ingo Molnar | e601757 | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 16 | #include <linux/sched/clock.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 18 | #include <net/ip.h> |
| 19 | |
Alexander Duyck | 545cd5e | 2017-03-24 10:07:53 -0700 | [diff] [blame] | 20 | /* 0 - Reserved to indicate value not set |
| 21 | * 1..NR_CPUS - Reserved for sender_cpu |
| 22 | * NR_CPUS+1..~0 - Region available for NAPI IDs |
| 23 | */ |
| 24 | #define MIN_NAPI_ID ((unsigned int)(NR_CPUS + 1)) |
| 25 | |
Björn Töpel | 7c951caf | 2020-11-30 19:51:57 +0100 | [diff] [blame] | 26 | #define BUSY_POLL_BUDGET 8 |
| 27 | |
Daniel Borkmann | e4dde41 | 2017-08-11 18:31:24 +0200 | [diff] [blame] | 28 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 29 | |
| 30 | struct napi_struct; |
| 31 | extern unsigned int sysctl_net_busy_read __read_mostly; |
| 32 | extern unsigned int sysctl_net_busy_poll __read_mostly; |
| 33 | |
Eliezer Tamir | cbf5500 | 2013-07-08 16:20:34 +0300 | [diff] [blame] | 34 | static inline bool net_busy_loop_on(void) |
Eliezer Tamir | 91e2fd33 | 2013-06-28 15:59:35 +0300 | [diff] [blame] | 35 | { |
Eliezer Tamir | 64b0dc5 | 2013-07-10 17:13:36 +0300 | [diff] [blame] | 36 | return sysctl_net_busy_poll; |
Eliezer Tamir | 91e2fd33 | 2013-06-28 15:59:35 +0300 | [diff] [blame] | 37 | } |
| 38 | |
Eric Dumazet | 21cb84c | 2016-11-15 10:15:12 -0800 | [diff] [blame] | 39 | static inline bool sk_can_busy_loop(const struct sock *sk) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 40 | { |
Alexander Duyck | 545cd5e | 2017-03-24 10:07:53 -0700 | [diff] [blame] | 41 | return sk->sk_ll_usec && !signal_pending(current); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 42 | } |
| 43 | |
Sridhar Samudrala | 7db6b04 | 2017-03-24 10:08:24 -0700 | [diff] [blame] | 44 | bool sk_busy_loop_end(void *p, unsigned long start_time); |
| 45 | |
| 46 | void napi_busy_loop(unsigned int napi_id, |
| 47 | bool (*loop_end)(void *, unsigned long), |
Björn Töpel | 7c951caf | 2020-11-30 19:51:57 +0100 | [diff] [blame] | 48 | void *loop_end_arg, bool prefer_busy_poll, u16 budget); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 49 | |
Cong Wang | e0d1095 | 2013-08-01 11:10:25 +0800 | [diff] [blame] | 50 | #else /* CONFIG_NET_RX_BUSY_POLL */ |
Eliezer Tamir | cbf5500 | 2013-07-08 16:20:34 +0300 | [diff] [blame] | 51 | static inline unsigned long net_busy_loop_on(void) |
Eliezer Tamir | 91e2fd33 | 2013-06-28 15:59:35 +0300 | [diff] [blame] | 52 | { |
| 53 | return 0; |
| 54 | } |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 55 | |
Eliezer Tamir | cbf5500 | 2013-07-08 16:20:34 +0300 | [diff] [blame] | 56 | static inline bool sk_can_busy_loop(struct sock *sk) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 57 | { |
| 58 | return false; |
| 59 | } |
| 60 | |
Cong Wang | e0d1095 | 2013-08-01 11:10:25 +0800 | [diff] [blame] | 61 | #endif /* CONFIG_NET_RX_BUSY_POLL */ |
Eric Dumazet | e68b6e5 | 2016-11-16 09:10:42 -0800 | [diff] [blame] | 62 | |
Alexander Duyck | 3705671 | 2017-03-24 10:08:18 -0700 | [diff] [blame] | 63 | static inline unsigned long busy_loop_current_time(void) |
| 64 | { |
| 65 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 66 | return (unsigned long)(local_clock() >> 10); |
| 67 | #else |
| 68 | return 0; |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | /* in poll/select we use the global sysctl_net_ll_poll value */ |
| 73 | static inline bool busy_loop_timeout(unsigned long start_time) |
| 74 | { |
| 75 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 76 | unsigned long bp_usec = READ_ONCE(sysctl_net_busy_poll); |
| 77 | |
| 78 | if (bp_usec) { |
| 79 | unsigned long end_time = start_time + bp_usec; |
| 80 | unsigned long now = busy_loop_current_time(); |
| 81 | |
| 82 | return time_after(now, end_time); |
| 83 | } |
| 84 | #endif |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | static inline bool sk_busy_loop_timeout(struct sock *sk, |
| 89 | unsigned long start_time) |
| 90 | { |
| 91 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 92 | unsigned long bp_usec = READ_ONCE(sk->sk_ll_usec); |
| 93 | |
| 94 | if (bp_usec) { |
| 95 | unsigned long end_time = start_time + bp_usec; |
| 96 | unsigned long now = busy_loop_current_time(); |
| 97 | |
| 98 | return time_after(now, end_time); |
| 99 | } |
| 100 | #endif |
| 101 | return true; |
| 102 | } |
| 103 | |
Sridhar Samudrala | 7db6b04 | 2017-03-24 10:08:24 -0700 | [diff] [blame] | 104 | static inline void sk_busy_loop(struct sock *sk, int nonblock) |
| 105 | { |
| 106 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 107 | unsigned int napi_id = READ_ONCE(sk->sk_napi_id); |
| 108 | |
| 109 | if (napi_id >= MIN_NAPI_ID) |
Björn Töpel | 7fd3253 | 2020-11-30 19:51:56 +0100 | [diff] [blame] | 110 | napi_busy_loop(napi_id, nonblock ? NULL : sk_busy_loop_end, sk, |
Björn Töpel | 7c951caf | 2020-11-30 19:51:57 +0100 | [diff] [blame] | 111 | READ_ONCE(sk->sk_prefer_busy_poll), |
| 112 | READ_ONCE(sk->sk_busy_poll_budget) ?: BUSY_POLL_BUDGET); |
Sridhar Samudrala | 7db6b04 | 2017-03-24 10:08:24 -0700 | [diff] [blame] | 113 | #endif |
| 114 | } |
| 115 | |
Alexander Duyck | d2e64db | 2017-03-24 10:08:06 -0700 | [diff] [blame] | 116 | /* used in the NIC receive handler to mark the skb */ |
| 117 | static inline void skb_mark_napi_id(struct sk_buff *skb, |
| 118 | struct napi_struct *napi) |
| 119 | { |
| 120 | #ifdef CONFIG_NET_RX_BUSY_POLL |
Amritha Nambiar | 78e57f1 | 2020-06-18 14:22:15 -0700 | [diff] [blame] | 121 | /* If the skb was already marked with a valid NAPI ID, avoid overwriting |
| 122 | * it. |
| 123 | */ |
| 124 | if (skb->napi_id < MIN_NAPI_ID) |
| 125 | skb->napi_id = napi->napi_id; |
Alexander Duyck | d2e64db | 2017-03-24 10:08:06 -0700 | [diff] [blame] | 126 | #endif |
| 127 | } |
| 128 | |
Eric Dumazet | e68b6e5 | 2016-11-16 09:10:42 -0800 | [diff] [blame] | 129 | /* used in the protocol hanlder to propagate the napi_id to the socket */ |
| 130 | static inline void sk_mark_napi_id(struct sock *sk, const struct sk_buff *skb) |
| 131 | { |
| 132 | #ifdef CONFIG_NET_RX_BUSY_POLL |
Eric Dumazet | ee8d153 | 2019-10-29 10:54:44 -0700 | [diff] [blame] | 133 | WRITE_ONCE(sk->sk_napi_id, skb->napi_id); |
Eric Dumazet | e68b6e5 | 2016-11-16 09:10:42 -0800 | [diff] [blame] | 134 | #endif |
Amritha Nambiar | c6345ce | 2018-06-29 21:26:57 -0700 | [diff] [blame] | 135 | sk_rx_queue_set(sk, skb); |
Eric Dumazet | e68b6e5 | 2016-11-16 09:10:42 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Borkmann | ba05817 | 2020-12-01 15:22:59 +0100 | [diff] [blame] | 138 | static inline void __sk_mark_napi_id_once(struct sock *sk, unsigned int napi_id) |
Björn Töpel | b02e5a0 | 2020-11-30 19:52:01 +0100 | [diff] [blame] | 139 | { |
| 140 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 141 | if (!READ_ONCE(sk->sk_napi_id)) |
| 142 | WRITE_ONCE(sk->sk_napi_id, napi_id); |
| 143 | #endif |
| 144 | } |
| 145 | |
Eric Dumazet | e68b6e5 | 2016-11-16 09:10:42 -0800 | [diff] [blame] | 146 | /* variant used for unconnected sockets */ |
| 147 | static inline void sk_mark_napi_id_once(struct sock *sk, |
| 148 | const struct sk_buff *skb) |
| 149 | { |
Daniel Borkmann | ba05817 | 2020-12-01 15:22:59 +0100 | [diff] [blame] | 150 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 151 | __sk_mark_napi_id_once(sk, skb->napi_id); |
| 152 | #endif |
Björn Töpel | b02e5a0 | 2020-11-30 19:52:01 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static inline void sk_mark_napi_id_once_xdp(struct sock *sk, |
| 156 | const struct xdp_buff *xdp) |
| 157 | { |
Daniel Borkmann | ba05817 | 2020-12-01 15:22:59 +0100 | [diff] [blame] | 158 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 159 | __sk_mark_napi_id_once(sk, xdp->rxq->napi_id); |
| 160 | #endif |
Eric Dumazet | e68b6e5 | 2016-11-16 09:10:42 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Eliezer Tamir | 8b80cda | 2013-07-10 17:13:26 +0300 | [diff] [blame] | 163 | #endif /* _LINUX_NET_BUSY_POLL_H */ |