Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 1 | /* |
| 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 Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 24 | #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 | |
| 32 | struct napi_struct; |
Eliezer Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 33 | extern unsigned int sysctl_net_ll_read __read_mostly; |
Eliezer Tamir | eb6db62 | 2013-06-14 16:33:25 +0300 | [diff] [blame] | 34 | extern unsigned int sysctl_net_ll_poll __read_mostly; |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 35 | |
| 36 | /* return values from ndo_ll_poll */ |
| 37 | #define LL_FLUSH_FAILED -1 |
| 38 | #define LL_FLUSH_BUSY -2 |
| 39 | |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame] | 40 | /* we can use sched_clock() because we don't care much about precision |
| 41 | * we only care that the average is bounded |
Eliezer Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 42 | * 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 Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame] | 44 | */ |
Eliezer Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 45 | static inline u64 ll_sk_end_time(struct sock *sk) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 46 | { |
Eliezer Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 47 | return ((u64)ACCESS_ONCE(sk->sk_ll_usec) << 10) + sched_clock(); |
| 48 | } |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame] | 49 | |
Eliezer Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 50 | /* in poll/select we use the global sysctl_net_ll_poll value */ |
| 51 | static inline u64 ll_end_time(void) |
| 52 | { |
| 53 | return ((u64)ACCESS_ONCE(sysctl_net_ll_poll) << 10) + sched_clock(); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static inline bool sk_valid_ll(struct sock *sk) |
| 57 | { |
Eliezer Tamir | dafcc43 | 2013-06-14 16:33:57 +0300 | [diff] [blame] | 58 | return sk->sk_ll_usec && sk->sk_napi_id && |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 59 | !need_resched() && !signal_pending(current); |
| 60 | } |
| 61 | |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame] | 62 | static inline bool can_poll_ll(u64 end_time) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 63 | { |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame] | 64 | return !time_after64(sched_clock(), end_time); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 65 | } |
| 66 | |
Eliezer Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 67 | /* 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 Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 70 | static inline bool sk_poll_ll(struct sock *sk, int nonblock) |
| 71 | { |
Eliezer Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 72 | u64 end_time = nonblock ? 0 : ll_sk_end_time(sk); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 73 | 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 Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 92 | 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 Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 102 | } while (!nonblock && skb_queue_empty(&sk->sk_receive_queue) && |
| 103 | can_poll_ll(end_time)); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 104 | |
| 105 | rc = !skb_queue_empty(&sk->sk_receive_queue); |
| 106 | out: |
| 107 | rcu_read_unlock_bh(); |
| 108 | return rc; |
| 109 | } |
| 110 | |
| 111 | /* used in the NIC receive handler to mark the skb */ |
| 112 | static 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 */ |
| 118 | static 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 Tamir | 2d48d67 | 2013-06-24 10:28:03 +0300 | [diff] [blame^] | 125 | static inline u64 sk_ll_end_time(struct sock *sk) |
| 126 | { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static inline u64 ll_end_time(void) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 131 | { |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static inline bool sk_valid_ll(struct sock *sk) |
| 136 | { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | static inline bool sk_poll_ll(struct sock *sk, int nonblock) |
| 141 | { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | static inline void skb_mark_ll(struct sk_buff *skb, struct napi_struct *napi) |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | static inline void sk_mark_ll(struct sock *sk, struct sk_buff *skb) |
| 150 | { |
| 151 | } |
| 152 | |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame] | 153 | static inline bool can_poll_ll(u64 end_time) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 154 | { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | #endif /* CONFIG_NET_LL_RX_POLL */ |
| 159 | #endif /* _LINUX_NET_LL_POLL_H */ |