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