1a61127c2SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2076bb0c8SEliezer Tamir /* 38b80cda5SEliezer Tamir * net busy poll support 4076bb0c8SEliezer Tamir * Copyright(c) 2013 Intel Corporation. 5076bb0c8SEliezer Tamir * 6076bb0c8SEliezer Tamir * Author: Eliezer Tamir 7076bb0c8SEliezer Tamir * 8076bb0c8SEliezer Tamir * Contact Information: 9076bb0c8SEliezer Tamir * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net> 10076bb0c8SEliezer Tamir */ 11076bb0c8SEliezer Tamir 128b80cda5SEliezer Tamir #ifndef _LINUX_NET_BUSY_POLL_H 138b80cda5SEliezer Tamir #define _LINUX_NET_BUSY_POLL_H 14076bb0c8SEliezer Tamir 15076bb0c8SEliezer Tamir #include <linux/netdevice.h> 16e6017571SIngo Molnar #include <linux/sched/clock.h> 17174cd4b1SIngo Molnar #include <linux/sched/signal.h> 18076bb0c8SEliezer Tamir #include <net/ip.h> 19076bb0c8SEliezer Tamir 20545cd5e5SAlexander Duyck /* 0 - Reserved to indicate value not set 21545cd5e5SAlexander Duyck * 1..NR_CPUS - Reserved for sender_cpu 22545cd5e5SAlexander Duyck * NR_CPUS+1..~0 - Region available for NAPI IDs 23545cd5e5SAlexander Duyck */ 24545cd5e5SAlexander Duyck #define MIN_NAPI_ID ((unsigned int)(NR_CPUS + 1)) 25545cd5e5SAlexander Duyck 26e4dde412SDaniel Borkmann #ifdef CONFIG_NET_RX_BUSY_POLL 27e4dde412SDaniel Borkmann 28e4dde412SDaniel Borkmann struct napi_struct; 29e4dde412SDaniel Borkmann extern unsigned int sysctl_net_busy_read __read_mostly; 30e4dde412SDaniel Borkmann extern unsigned int sysctl_net_busy_poll __read_mostly; 31e4dde412SDaniel Borkmann 32076bb0c8SEliezer Tamir static inline bool net_busy_loop_on(void) 33076bb0c8SEliezer Tamir { 3464b0dc51SEliezer Tamir return sysctl_net_busy_poll; 35076bb0c8SEliezer Tamir } 36076bb0c8SEliezer Tamir 3721cb84c4SEric Dumazet static inline bool sk_can_busy_loop(const struct sock *sk) 38076bb0c8SEliezer Tamir { 39545cd5e5SAlexander Duyck return sk->sk_ll_usec && !signal_pending(current); 40076bb0c8SEliezer Tamir } 41076bb0c8SEliezer Tamir 427db6b048SSridhar Samudrala bool sk_busy_loop_end(void *p, unsigned long start_time); 437db6b048SSridhar Samudrala 447db6b048SSridhar Samudrala void napi_busy_loop(unsigned int napi_id, 457db6b048SSridhar Samudrala bool (*loop_end)(void *, unsigned long), 46*7fd3253aSBjörn Töpel void *loop_end_arg, bool prefer_busy_poll); 47076bb0c8SEliezer Tamir 48e0d1095aSCong Wang #else /* CONFIG_NET_RX_BUSY_POLL */ 49076bb0c8SEliezer Tamir static inline unsigned long net_busy_loop_on(void) 50076bb0c8SEliezer Tamir { 51076bb0c8SEliezer Tamir return 0; 52076bb0c8SEliezer Tamir } 53076bb0c8SEliezer Tamir 54076bb0c8SEliezer Tamir static inline bool sk_can_busy_loop(struct sock *sk) 55076bb0c8SEliezer Tamir { 56076bb0c8SEliezer Tamir return false; 57076bb0c8SEliezer Tamir } 58076bb0c8SEliezer Tamir 59e0d1095aSCong Wang #endif /* CONFIG_NET_RX_BUSY_POLL */ 60e68b6e50SEric Dumazet 6137056719SAlexander Duyck static inline unsigned long busy_loop_current_time(void) 6237056719SAlexander Duyck { 6337056719SAlexander Duyck #ifdef CONFIG_NET_RX_BUSY_POLL 6437056719SAlexander Duyck return (unsigned long)(local_clock() >> 10); 6537056719SAlexander Duyck #else 6637056719SAlexander Duyck return 0; 6737056719SAlexander Duyck #endif 6837056719SAlexander Duyck } 6937056719SAlexander Duyck 7037056719SAlexander Duyck /* in poll/select we use the global sysctl_net_ll_poll value */ 7137056719SAlexander Duyck static inline bool busy_loop_timeout(unsigned long start_time) 7237056719SAlexander Duyck { 7337056719SAlexander Duyck #ifdef CONFIG_NET_RX_BUSY_POLL 7437056719SAlexander Duyck unsigned long bp_usec = READ_ONCE(sysctl_net_busy_poll); 7537056719SAlexander Duyck 7637056719SAlexander Duyck if (bp_usec) { 7737056719SAlexander Duyck unsigned long end_time = start_time + bp_usec; 7837056719SAlexander Duyck unsigned long now = busy_loop_current_time(); 7937056719SAlexander Duyck 8037056719SAlexander Duyck return time_after(now, end_time); 8137056719SAlexander Duyck } 8237056719SAlexander Duyck #endif 8337056719SAlexander Duyck return true; 8437056719SAlexander Duyck } 8537056719SAlexander Duyck 8637056719SAlexander Duyck static inline bool sk_busy_loop_timeout(struct sock *sk, 8737056719SAlexander Duyck unsigned long start_time) 8837056719SAlexander Duyck { 8937056719SAlexander Duyck #ifdef CONFIG_NET_RX_BUSY_POLL 9037056719SAlexander Duyck unsigned long bp_usec = READ_ONCE(sk->sk_ll_usec); 9137056719SAlexander Duyck 9237056719SAlexander Duyck if (bp_usec) { 9337056719SAlexander Duyck unsigned long end_time = start_time + bp_usec; 9437056719SAlexander Duyck unsigned long now = busy_loop_current_time(); 9537056719SAlexander Duyck 9637056719SAlexander Duyck return time_after(now, end_time); 9737056719SAlexander Duyck } 9837056719SAlexander Duyck #endif 9937056719SAlexander Duyck return true; 10037056719SAlexander Duyck } 10137056719SAlexander Duyck 1027db6b048SSridhar Samudrala static inline void sk_busy_loop(struct sock *sk, int nonblock) 1037db6b048SSridhar Samudrala { 1047db6b048SSridhar Samudrala #ifdef CONFIG_NET_RX_BUSY_POLL 1057db6b048SSridhar Samudrala unsigned int napi_id = READ_ONCE(sk->sk_napi_id); 1067db6b048SSridhar Samudrala 1077db6b048SSridhar Samudrala if (napi_id >= MIN_NAPI_ID) 108*7fd3253aSBjörn Töpel napi_busy_loop(napi_id, nonblock ? NULL : sk_busy_loop_end, sk, 109*7fd3253aSBjörn Töpel READ_ONCE(sk->sk_prefer_busy_poll)); 1107db6b048SSridhar Samudrala #endif 1117db6b048SSridhar Samudrala } 1127db6b048SSridhar Samudrala 113d2e64dbbSAlexander Duyck /* used in the NIC receive handler to mark the skb */ 114d2e64dbbSAlexander Duyck static inline void skb_mark_napi_id(struct sk_buff *skb, 115d2e64dbbSAlexander Duyck struct napi_struct *napi) 116d2e64dbbSAlexander Duyck { 117d2e64dbbSAlexander Duyck #ifdef CONFIG_NET_RX_BUSY_POLL 11878e57f15SAmritha Nambiar /* If the skb was already marked with a valid NAPI ID, avoid overwriting 11978e57f15SAmritha Nambiar * it. 12078e57f15SAmritha Nambiar */ 12178e57f15SAmritha Nambiar if (skb->napi_id < MIN_NAPI_ID) 122d2e64dbbSAlexander Duyck skb->napi_id = napi->napi_id; 123d2e64dbbSAlexander Duyck #endif 124d2e64dbbSAlexander Duyck } 125d2e64dbbSAlexander Duyck 126e68b6e50SEric Dumazet /* used in the protocol hanlder to propagate the napi_id to the socket */ 127e68b6e50SEric Dumazet static inline void sk_mark_napi_id(struct sock *sk, const struct sk_buff *skb) 128e68b6e50SEric Dumazet { 129e68b6e50SEric Dumazet #ifdef CONFIG_NET_RX_BUSY_POLL 130ee8d153dSEric Dumazet WRITE_ONCE(sk->sk_napi_id, skb->napi_id); 131e68b6e50SEric Dumazet #endif 132c6345ce7SAmritha Nambiar sk_rx_queue_set(sk, skb); 133e68b6e50SEric Dumazet } 134e68b6e50SEric Dumazet 135e68b6e50SEric Dumazet /* variant used for unconnected sockets */ 136e68b6e50SEric Dumazet static inline void sk_mark_napi_id_once(struct sock *sk, 137e68b6e50SEric Dumazet const struct sk_buff *skb) 138e68b6e50SEric Dumazet { 139e68b6e50SEric Dumazet #ifdef CONFIG_NET_RX_BUSY_POLL 140ee8d153dSEric Dumazet if (!READ_ONCE(sk->sk_napi_id)) 141ee8d153dSEric Dumazet WRITE_ONCE(sk->sk_napi_id, skb->napi_id); 142e68b6e50SEric Dumazet #endif 143e68b6e50SEric Dumazet } 144e68b6e50SEric Dumazet 1458b80cda5SEliezer Tamir #endif /* _LINUX_NET_BUSY_POLL_H */ 146