blob: 73af4a64a5999660127dab0a4d111e50eeadf1b5 [file] [log] [blame]
Thomas Gleixnera61127c2019-05-29 16:57:49 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Eliezer Tamir06021292013-06-10 11:39:50 +03002/*
Eliezer Tamir8b80cda2013-07-10 17:13:26 +03003 * net busy poll support
Eliezer Tamir06021292013-06-10 11:39:50 +03004 * Copyright(c) 2013 Intel Corporation.
5 *
Eliezer Tamir06021292013-06-10 11:39:50 +03006 * Author: Eliezer Tamir
7 *
8 * Contact Information:
9 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
10 */
11
Eliezer Tamir8b80cda2013-07-10 17:13:26 +030012#ifndef _LINUX_NET_BUSY_POLL_H
13#define _LINUX_NET_BUSY_POLL_H
Eliezer Tamir06021292013-06-10 11:39:50 +030014
15#include <linux/netdevice.h>
Ingo Molnare6017572017-02-01 16:36:40 +010016#include <linux/sched/clock.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010017#include <linux/sched/signal.h>
Eliezer Tamir06021292013-06-10 11:39:50 +030018#include <net/ip.h>
19
Alexander Duyck545cd5e2017-03-24 10:07:53 -070020/* 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öpel7c951caf2020-11-30 19:51:57 +010026#define BUSY_POLL_BUDGET 8
27
Daniel Borkmanne4dde412017-08-11 18:31:24 +020028#ifdef CONFIG_NET_RX_BUSY_POLL
29
30struct napi_struct;
31extern unsigned int sysctl_net_busy_read __read_mostly;
32extern unsigned int sysctl_net_busy_poll __read_mostly;
33
Eliezer Tamircbf55002013-07-08 16:20:34 +030034static inline bool net_busy_loop_on(void)
Eliezer Tamir91e2fd332013-06-28 15:59:35 +030035{
Eliezer Tamir64b0dc52013-07-10 17:13:36 +030036 return sysctl_net_busy_poll;
Eliezer Tamir91e2fd332013-06-28 15:59:35 +030037}
38
Eric Dumazet21cb84c2016-11-15 10:15:12 -080039static inline bool sk_can_busy_loop(const struct sock *sk)
Eliezer Tamir06021292013-06-10 11:39:50 +030040{
Alexander Duyck545cd5e2017-03-24 10:07:53 -070041 return sk->sk_ll_usec && !signal_pending(current);
Eliezer Tamir06021292013-06-10 11:39:50 +030042}
43
Sridhar Samudrala7db6b042017-03-24 10:08:24 -070044bool sk_busy_loop_end(void *p, unsigned long start_time);
45
46void napi_busy_loop(unsigned int napi_id,
47 bool (*loop_end)(void *, unsigned long),
Björn Töpel7c951caf2020-11-30 19:51:57 +010048 void *loop_end_arg, bool prefer_busy_poll, u16 budget);
Eliezer Tamir06021292013-06-10 11:39:50 +030049
Cong Wange0d10952013-08-01 11:10:25 +080050#else /* CONFIG_NET_RX_BUSY_POLL */
Eliezer Tamircbf55002013-07-08 16:20:34 +030051static inline unsigned long net_busy_loop_on(void)
Eliezer Tamir91e2fd332013-06-28 15:59:35 +030052{
53 return 0;
54}
Eliezer Tamir06021292013-06-10 11:39:50 +030055
Eliezer Tamircbf55002013-07-08 16:20:34 +030056static inline bool sk_can_busy_loop(struct sock *sk)
Eliezer Tamir06021292013-06-10 11:39:50 +030057{
58 return false;
59}
60
Cong Wange0d10952013-08-01 11:10:25 +080061#endif /* CONFIG_NET_RX_BUSY_POLL */
Eric Dumazete68b6e52016-11-16 09:10:42 -080062
Alexander Duyck37056712017-03-24 10:08:18 -070063static 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 */
73static 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
88static 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 Samudrala7db6b042017-03-24 10:08:24 -0700104static 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öpel7fd32532020-11-30 19:51:56 +0100110 napi_busy_loop(napi_id, nonblock ? NULL : sk_busy_loop_end, sk,
Björn Töpel7c951caf2020-11-30 19:51:57 +0100111 READ_ONCE(sk->sk_prefer_busy_poll),
112 READ_ONCE(sk->sk_busy_poll_budget) ?: BUSY_POLL_BUDGET);
Sridhar Samudrala7db6b042017-03-24 10:08:24 -0700113#endif
114}
115
Alexander Duyckd2e64db2017-03-24 10:08:06 -0700116/* used in the NIC receive handler to mark the skb */
117static inline void skb_mark_napi_id(struct sk_buff *skb,
118 struct napi_struct *napi)
119{
120#ifdef CONFIG_NET_RX_BUSY_POLL
Amritha Nambiar78e57f12020-06-18 14:22:15 -0700121 /* 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 Duyckd2e64db2017-03-24 10:08:06 -0700126#endif
127}
128
Eric Dumazete68b6e52016-11-16 09:10:42 -0800129/* used in the protocol hanlder to propagate the napi_id to the socket */
130static inline void sk_mark_napi_id(struct sock *sk, const struct sk_buff *skb)
131{
132#ifdef CONFIG_NET_RX_BUSY_POLL
Eric Dumazetee8d1532019-10-29 10:54:44 -0700133 WRITE_ONCE(sk->sk_napi_id, skb->napi_id);
Eric Dumazete68b6e52016-11-16 09:10:42 -0800134#endif
Amritha Nambiarc6345ce2018-06-29 21:26:57 -0700135 sk_rx_queue_set(sk, skb);
Eric Dumazete68b6e52016-11-16 09:10:42 -0800136}
137
Daniel Borkmannba058172020-12-01 15:22:59 +0100138static inline void __sk_mark_napi_id_once(struct sock *sk, unsigned int napi_id)
Björn Töpelb02e5a02020-11-30 19:52:01 +0100139{
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 Dumazete68b6e52016-11-16 09:10:42 -0800146/* variant used for unconnected sockets */
147static inline void sk_mark_napi_id_once(struct sock *sk,
148 const struct sk_buff *skb)
149{
Daniel Borkmannba058172020-12-01 15:22:59 +0100150#ifdef CONFIG_NET_RX_BUSY_POLL
151 __sk_mark_napi_id_once(sk, skb->napi_id);
152#endif
Björn Töpelb02e5a02020-11-30 19:52:01 +0100153}
154
155static inline void sk_mark_napi_id_once_xdp(struct sock *sk,
156 const struct xdp_buff *xdp)
157{
Daniel Borkmannba058172020-12-01 15:22:59 +0100158#ifdef CONFIG_NET_RX_BUSY_POLL
159 __sk_mark_napi_id_once(sk, xdp->rxq->napi_id);
160#endif
Eric Dumazete68b6e52016-11-16 09:10:42 -0800161}
162
Eliezer Tamir8b80cda2013-07-10 17:13:26 +0300163#endif /* _LINUX_NET_BUSY_POLL_H */