blob: 5bf2b3a6129ed4e8adf14dce128e3703f8e21a97 [file] [log] [blame]
Eliezer Tamir06021292013-06-10 11:39:50 +03001/*
2 * Low Latency Sockets
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
Eliezer Tamir06021292013-06-10 11:39:50 +030024#ifndef _LINUX_NET_LL_POLL_H
25#define _LINUX_NET_LL_POLL_H
26
27#include <linux/netdevice.h>
28#include <net/ip.h>
29
30#ifdef CONFIG_NET_LL_RX_POLL
31
32struct napi_struct;
Eliezer Tamir2d48d672013-06-24 10:28:03 +030033extern unsigned int sysctl_net_ll_read __read_mostly;
Eliezer Tamireb6db622013-06-14 16:33:25 +030034extern unsigned int sysctl_net_ll_poll __read_mostly;
Eliezer Tamir06021292013-06-10 11:39:50 +030035
36/* return values from ndo_ll_poll */
37#define LL_FLUSH_FAILED -1
38#define LL_FLUSH_BUSY -2
39
Eliezer Tamir9a3c71a2013-06-14 16:33:35 +030040/* we can use sched_clock() because we don't care much about precision
41 * we only care that the average is bounded
Eliezer Tamir2d48d672013-06-24 10:28:03 +030042 * we don't mind a ~2.5% imprecision so <<10 instead of *1000
43 * sk->sk_ll_usec is a u_int so this can't overflow
Eliezer Tamir9a3c71a2013-06-14 16:33:35 +030044 */
Eliezer Tamir2d48d672013-06-24 10:28:03 +030045static inline u64 ll_sk_end_time(struct sock *sk)
Eliezer Tamir06021292013-06-10 11:39:50 +030046{
Eliezer Tamir2d48d672013-06-24 10:28:03 +030047 return ((u64)ACCESS_ONCE(sk->sk_ll_usec) << 10) + sched_clock();
48}
Eliezer Tamir9a3c71a2013-06-14 16:33:35 +030049
Eliezer Tamir2d48d672013-06-24 10:28:03 +030050/* in poll/select we use the global sysctl_net_ll_poll value */
51static inline u64 ll_end_time(void)
52{
53 return ((u64)ACCESS_ONCE(sysctl_net_ll_poll) << 10) + sched_clock();
Eliezer Tamir06021292013-06-10 11:39:50 +030054}
55
56static inline bool sk_valid_ll(struct sock *sk)
57{
Eliezer Tamirdafcc432013-06-14 16:33:57 +030058 return sk->sk_ll_usec && sk->sk_napi_id &&
Eliezer Tamir06021292013-06-10 11:39:50 +030059 !need_resched() && !signal_pending(current);
60}
61
Eliezer Tamir9a3c71a2013-06-14 16:33:35 +030062static inline bool can_poll_ll(u64 end_time)
Eliezer Tamir06021292013-06-10 11:39:50 +030063{
Eliezer Tamir9a3c71a2013-06-14 16:33:35 +030064 return !time_after64(sched_clock(), end_time);
Eliezer Tamir06021292013-06-10 11:39:50 +030065}
66
Eliezer Tamir2d48d672013-06-24 10:28:03 +030067/* when used in sock_poll() nonblock is known at compile time to be true
68 * so the loop and end_time will be optimized out
69 */
Eliezer Tamir06021292013-06-10 11:39:50 +030070static inline bool sk_poll_ll(struct sock *sk, int nonblock)
71{
Eliezer Tamir2d48d672013-06-24 10:28:03 +030072 u64 end_time = nonblock ? 0 : ll_sk_end_time(sk);
Eliezer Tamir06021292013-06-10 11:39:50 +030073 const struct net_device_ops *ops;
74 struct napi_struct *napi;
75 int rc = false;
76
77 /*
78 * rcu read lock for napi hash
79 * bh so we don't race with net_rx_action
80 */
81 rcu_read_lock_bh();
82
83 napi = napi_by_id(sk->sk_napi_id);
84 if (!napi)
85 goto out;
86
87 ops = napi->dev->netdev_ops;
88 if (!ops->ndo_ll_poll)
89 goto out;
90
91 do {
Eliezer Tamir06021292013-06-10 11:39:50 +030092 rc = ops->ndo_ll_poll(napi);
93
94 if (rc == LL_FLUSH_FAILED)
95 break; /* permanent failure */
96
97 if (rc > 0)
98 /* local bh are disabled so it is ok to use _BH */
99 NET_ADD_STATS_BH(sock_net(sk),
100 LINUX_MIB_LOWLATENCYRXPACKETS, rc);
101
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300102 } while (!nonblock && skb_queue_empty(&sk->sk_receive_queue) &&
103 can_poll_ll(end_time));
Eliezer Tamir06021292013-06-10 11:39:50 +0300104
105 rc = !skb_queue_empty(&sk->sk_receive_queue);
106out:
107 rcu_read_unlock_bh();
108 return rc;
109}
110
111/* used in the NIC receive handler to mark the skb */
112static inline void skb_mark_ll(struct sk_buff *skb, struct napi_struct *napi)
113{
114 skb->napi_id = napi->napi_id;
115}
116
117/* used in the protocol hanlder to propagate the napi_id to the socket */
118static inline void sk_mark_ll(struct sock *sk, struct sk_buff *skb)
119{
120 sk->sk_napi_id = skb->napi_id;
121}
122
123#else /* CONFIG_NET_LL_RX_POLL */
124
Eliezer Tamir2d48d672013-06-24 10:28:03 +0300125static inline u64 sk_ll_end_time(struct sock *sk)
126{
127 return 0;
128}
129
130static inline u64 ll_end_time(void)
Eliezer Tamir06021292013-06-10 11:39:50 +0300131{
132 return 0;
133}
134
135static inline bool sk_valid_ll(struct sock *sk)
136{
137 return false;
138}
139
140static inline bool sk_poll_ll(struct sock *sk, int nonblock)
141{
142 return false;
143}
144
145static inline void skb_mark_ll(struct sk_buff *skb, struct napi_struct *napi)
146{
147}
148
149static inline void sk_mark_ll(struct sock *sk, struct sk_buff *skb)
150{
151}
152
Eliezer Tamir9a3c71a2013-06-14 16:33:35 +0300153static inline bool can_poll_ll(u64 end_time)
Eliezer Tamir06021292013-06-10 11:39:50 +0300154{
155 return false;
156}
157
158#endif /* CONFIG_NET_LL_RX_POLL */
159#endif /* _LINUX_NET_LL_POLL_H */