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 | eb6db62 | 2013-06-14 16:33:25 +0300 | [diff] [blame] | 33 | extern unsigned int sysctl_net_ll_poll __read_mostly; |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 34 | |
| 35 | /* return values from ndo_ll_poll */ |
| 36 | #define LL_FLUSH_FAILED -1 |
| 37 | #define LL_FLUSH_BUSY -2 |
| 38 | |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame^] | 39 | /* we can use sched_clock() because we don't care much about precision |
| 40 | * we only care that the average is bounded |
| 41 | */ |
| 42 | static inline u64 ll_end_time(void) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 43 | { |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame^] | 44 | u64 end_time = ACCESS_ONCE(sysctl_net_ll_poll); |
| 45 | |
| 46 | /* we don't mind a ~2.5% imprecision |
| 47 | * sysctl_net_ll_poll is a u_int so this can't overflow |
| 48 | */ |
| 49 | end_time = (end_time << 10) + sched_clock(); |
| 50 | |
| 51 | return end_time; |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static inline bool sk_valid_ll(struct sock *sk) |
| 55 | { |
| 56 | return sysctl_net_ll_poll && sk->sk_napi_id && |
| 57 | !need_resched() && !signal_pending(current); |
| 58 | } |
| 59 | |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame^] | 60 | static inline bool can_poll_ll(u64 end_time) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 61 | { |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame^] | 62 | return !time_after64(sched_clock(), end_time); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static inline bool sk_poll_ll(struct sock *sk, int nonblock) |
| 66 | { |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 67 | const struct net_device_ops *ops; |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame^] | 68 | u64 end_time = ll_end_time(); |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 69 | struct napi_struct *napi; |
| 70 | int rc = false; |
| 71 | |
| 72 | /* |
| 73 | * rcu read lock for napi hash |
| 74 | * bh so we don't race with net_rx_action |
| 75 | */ |
| 76 | rcu_read_lock_bh(); |
| 77 | |
| 78 | napi = napi_by_id(sk->sk_napi_id); |
| 79 | if (!napi) |
| 80 | goto out; |
| 81 | |
| 82 | ops = napi->dev->netdev_ops; |
| 83 | if (!ops->ndo_ll_poll) |
| 84 | goto out; |
| 85 | |
| 86 | do { |
| 87 | |
| 88 | rc = ops->ndo_ll_poll(napi); |
| 89 | |
| 90 | if (rc == LL_FLUSH_FAILED) |
| 91 | break; /* permanent failure */ |
| 92 | |
| 93 | if (rc > 0) |
| 94 | /* local bh are disabled so it is ok to use _BH */ |
| 95 | NET_ADD_STATS_BH(sock_net(sk), |
| 96 | LINUX_MIB_LOWLATENCYRXPACKETS, rc); |
| 97 | |
| 98 | } while (skb_queue_empty(&sk->sk_receive_queue) |
| 99 | && can_poll_ll(end_time) && !nonblock); |
| 100 | |
| 101 | rc = !skb_queue_empty(&sk->sk_receive_queue); |
| 102 | out: |
| 103 | rcu_read_unlock_bh(); |
| 104 | return rc; |
| 105 | } |
| 106 | |
| 107 | /* used in the NIC receive handler to mark the skb */ |
| 108 | static inline void skb_mark_ll(struct sk_buff *skb, struct napi_struct *napi) |
| 109 | { |
| 110 | skb->napi_id = napi->napi_id; |
| 111 | } |
| 112 | |
| 113 | /* used in the protocol hanlder to propagate the napi_id to the socket */ |
| 114 | static inline void sk_mark_ll(struct sock *sk, struct sk_buff *skb) |
| 115 | { |
| 116 | sk->sk_napi_id = skb->napi_id; |
| 117 | } |
| 118 | |
| 119 | #else /* CONFIG_NET_LL_RX_POLL */ |
| 120 | |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame^] | 121 | static inline u64 ll_end_time(void) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 122 | { |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static inline bool sk_valid_ll(struct sock *sk) |
| 127 | { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | static inline bool sk_poll_ll(struct sock *sk, int nonblock) |
| 132 | { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | static inline void skb_mark_ll(struct sk_buff *skb, struct napi_struct *napi) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | static inline void sk_mark_ll(struct sock *sk, struct sk_buff *skb) |
| 141 | { |
| 142 | } |
| 143 | |
Eliezer Tamir | 9a3c71a | 2013-06-14 16:33:35 +0300 | [diff] [blame^] | 144 | static inline bool can_poll_ll(u64 end_time) |
Eliezer Tamir | 0602129 | 2013-06-10 11:39:50 +0300 | [diff] [blame] | 145 | { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | #endif /* CONFIG_NET_LL_RX_POLL */ |
| 150 | #endif /* _LINUX_NET_LL_POLL_H */ |