Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/xprt.c |
| 3 | * |
| 4 | * This is a generic RPC call interface supporting congestion avoidance, |
| 5 | * and asynchronous calls. |
| 6 | * |
| 7 | * The interface works like this: |
| 8 | * |
| 9 | * - When a process places a call, it allocates a request slot if |
| 10 | * one is available. Otherwise, it sleeps on the backlog queue |
| 11 | * (xprt_reserve). |
| 12 | * - Next, the caller puts together the RPC message, stuffs it into |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 13 | * the request struct, and calls xprt_transmit(). |
| 14 | * - xprt_transmit sends the message and installs the caller on the |
| 15 | * transport's wait list. At the same time, it installs a timer that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * is run after the packet's timeout has expired. |
| 17 | * - When a packet arrives, the data_ready handler walks the list of |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 18 | * pending requests for that transport. If a matching XID is found, the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * caller is woken up, and the timer removed. |
| 20 | * - When no reply arrives within the timeout interval, the timer is |
| 21 | * fired by the kernel and runs xprt_timer(). It either adjusts the |
| 22 | * timeout values (minor timeout) or wakes up the caller with a status |
| 23 | * of -ETIMEDOUT. |
| 24 | * - When the caller receives a notification from RPC that a reply arrived, |
| 25 | * it should release the RPC slot, and process the reply. |
| 26 | * If the call timed out, it may choose to retry the operation by |
| 27 | * adjusting the initial timeout value, and simply calling rpc_call |
| 28 | * again. |
| 29 | * |
| 30 | * Support for async RPC is done through a set of RPC-specific scheduling |
| 31 | * primitives that `transparently' work for processes as well as async |
| 32 | * tasks that rely on callbacks. |
| 33 | * |
| 34 | * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 35 | * |
| 36 | * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 39 | #include <linux/module.h> |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/types.h> |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 42 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/workqueue.h> |
| 44 | #include <linux/random.h> |
| 45 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 46 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Local variables |
| 50 | */ |
| 51 | |
| 52 | #ifdef RPC_DEBUG |
| 53 | # undef RPC_DEBUG_DATA |
| 54 | # define RPCDBG_FACILITY RPCDBG_XPRT |
| 55 | #endif |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Local functions |
| 59 | */ |
| 60 | static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); |
| 61 | static inline void do_xprt_reserve(struct rpc_task *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static void xprt_connect_status(struct rpc_task *task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); |
| 64 | |
| 65 | static int xprt_clear_backlog(struct rpc_xprt *xprt); |
| 66 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 67 | /** |
| 68 | * xprt_reserve_xprt - serialize write access to transports |
| 69 | * @task: task that is requesting access to the transport |
| 70 | * |
| 71 | * This prevents mixing the payload of separate requests, and prevents |
| 72 | * transport connects from colliding with writes. No congestion control |
| 73 | * is provided. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 75 | int xprt_reserve_xprt(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 77 | struct rpc_xprt *xprt = task->tk_xprt; |
| 78 | struct rpc_rqst *req = task->tk_rqstp; |
| 79 | |
| 80 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
| 81 | if (task == xprt->snd_task) |
| 82 | return 1; |
| 83 | if (task == NULL) |
| 84 | return 0; |
| 85 | goto out_sleep; |
| 86 | } |
| 87 | xprt->snd_task = task; |
| 88 | if (req) { |
| 89 | req->rq_bytes_sent = 0; |
| 90 | req->rq_ntrans++; |
| 91 | } |
| 92 | return 1; |
| 93 | |
| 94 | out_sleep: |
| 95 | dprintk("RPC: %4d failed to lock transport %p\n", |
| 96 | task->tk_pid, xprt); |
| 97 | task->tk_timeout = 0; |
| 98 | task->tk_status = -EAGAIN; |
| 99 | if (req && req->rq_ntrans) |
| 100 | rpc_sleep_on(&xprt->resend, task, NULL, NULL); |
| 101 | else |
| 102 | rpc_sleep_on(&xprt->sending, task, NULL, NULL); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * xprt_reserve_xprt_cong - serialize write access to transports |
| 108 | * @task: task that is requesting access to the transport |
| 109 | * |
| 110 | * Same as xprt_reserve_xprt, but Van Jacobson congestion control is |
| 111 | * integrated into the decision of whether a request is allowed to be |
| 112 | * woken up and given access to the transport. |
| 113 | */ |
| 114 | int xprt_reserve_xprt_cong(struct rpc_task *task) |
| 115 | { |
| 116 | struct rpc_xprt *xprt = task->tk_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | struct rpc_rqst *req = task->tk_rqstp; |
| 118 | |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 119 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | if (task == xprt->snd_task) |
| 121 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | goto out_sleep; |
| 123 | } |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 124 | if (__xprt_get_cong(xprt, task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | xprt->snd_task = task; |
| 126 | if (req) { |
| 127 | req->rq_bytes_sent = 0; |
| 128 | req->rq_ntrans++; |
| 129 | } |
| 130 | return 1; |
| 131 | } |
| 132 | smp_mb__before_clear_bit(); |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 133 | clear_bit(XPRT_LOCKED, &xprt->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | smp_mb__after_clear_bit(); |
| 135 | out_sleep: |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 136 | dprintk("RPC: %4d failed to lock transport %p\n", task->tk_pid, xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | task->tk_timeout = 0; |
| 138 | task->tk_status = -EAGAIN; |
| 139 | if (req && req->rq_ntrans) |
| 140 | rpc_sleep_on(&xprt->resend, task, NULL, NULL); |
| 141 | else |
| 142 | rpc_sleep_on(&xprt->sending, task, NULL, NULL); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 146 | static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | int retval; |
| 149 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 150 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 151 | retval = xprt->ops->reserve_xprt(task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 152 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return retval; |
| 154 | } |
| 155 | |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 156 | static void __xprt_lock_write_next(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | struct rpc_task *task; |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 159 | struct rpc_rqst *req; |
| 160 | |
| 161 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
| 162 | return; |
| 163 | |
| 164 | task = rpc_wake_up_next(&xprt->resend); |
| 165 | if (!task) { |
| 166 | task = rpc_wake_up_next(&xprt->sending); |
| 167 | if (!task) |
| 168 | goto out_unlock; |
| 169 | } |
| 170 | |
| 171 | req = task->tk_rqstp; |
| 172 | xprt->snd_task = task; |
| 173 | if (req) { |
| 174 | req->rq_bytes_sent = 0; |
| 175 | req->rq_ntrans++; |
| 176 | } |
| 177 | return; |
| 178 | |
| 179 | out_unlock: |
| 180 | smp_mb__before_clear_bit(); |
| 181 | clear_bit(XPRT_LOCKED, &xprt->state); |
| 182 | smp_mb__after_clear_bit(); |
| 183 | } |
| 184 | |
| 185 | static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) |
| 186 | { |
| 187 | struct rpc_task *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 189 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return; |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 191 | if (RPCXPRT_CONGESTED(xprt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | goto out_unlock; |
| 193 | task = rpc_wake_up_next(&xprt->resend); |
| 194 | if (!task) { |
| 195 | task = rpc_wake_up_next(&xprt->sending); |
| 196 | if (!task) |
| 197 | goto out_unlock; |
| 198 | } |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 199 | if (__xprt_get_cong(xprt, task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | struct rpc_rqst *req = task->tk_rqstp; |
| 201 | xprt->snd_task = task; |
| 202 | if (req) { |
| 203 | req->rq_bytes_sent = 0; |
| 204 | req->rq_ntrans++; |
| 205 | } |
| 206 | return; |
| 207 | } |
| 208 | out_unlock: |
| 209 | smp_mb__before_clear_bit(); |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 210 | clear_bit(XPRT_LOCKED, &xprt->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | smp_mb__after_clear_bit(); |
| 212 | } |
| 213 | |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 214 | /** |
| 215 | * xprt_release_xprt - allow other requests to use a transport |
| 216 | * @xprt: transport with other tasks potentially waiting |
| 217 | * @task: task that is releasing access to the transport |
| 218 | * |
| 219 | * Note that "task" can be NULL. No congestion control is provided. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | */ |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 221 | void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | if (xprt->snd_task == task) { |
| 224 | xprt->snd_task = NULL; |
| 225 | smp_mb__before_clear_bit(); |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 226 | clear_bit(XPRT_LOCKED, &xprt->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | smp_mb__after_clear_bit(); |
| 228 | __xprt_lock_write_next(xprt); |
| 229 | } |
| 230 | } |
| 231 | |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 232 | /** |
| 233 | * xprt_release_xprt_cong - allow other requests to use a transport |
| 234 | * @xprt: transport with other tasks potentially waiting |
| 235 | * @task: task that is releasing access to the transport |
| 236 | * |
| 237 | * Note that "task" can be NULL. Another task is awoken to use the |
| 238 | * transport if the transport's congestion window allows it. |
| 239 | */ |
| 240 | void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) |
| 241 | { |
| 242 | if (xprt->snd_task == task) { |
| 243 | xprt->snd_task = NULL; |
| 244 | smp_mb__before_clear_bit(); |
| 245 | clear_bit(XPRT_LOCKED, &xprt->state); |
| 246 | smp_mb__after_clear_bit(); |
| 247 | __xprt_lock_write_next_cong(xprt); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 253 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 254 | xprt->ops->release_xprt(xprt, task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 255 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | * Van Jacobson congestion avoidance. Check if the congestion window |
| 260 | * overflowed. Put the task to sleep if this is the case. |
| 261 | */ |
| 262 | static int |
| 263 | __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) |
| 264 | { |
| 265 | struct rpc_rqst *req = task->tk_rqstp; |
| 266 | |
| 267 | if (req->rq_cong) |
| 268 | return 1; |
| 269 | dprintk("RPC: %4d xprt_cwnd_limited cong = %ld cwnd = %ld\n", |
| 270 | task->tk_pid, xprt->cong, xprt->cwnd); |
| 271 | if (RPCXPRT_CONGESTED(xprt)) |
| 272 | return 0; |
| 273 | req->rq_cong = 1; |
| 274 | xprt->cong += RPC_CWNDSCALE; |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Adjust the congestion window, and wake up the next task |
| 280 | * that has been sleeping due to congestion |
| 281 | */ |
| 282 | static void |
| 283 | __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) |
| 284 | { |
| 285 | if (!req->rq_cong) |
| 286 | return; |
| 287 | req->rq_cong = 0; |
| 288 | xprt->cong -= RPC_CWNDSCALE; |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 289 | __xprt_lock_write_next_cong(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 292 | /** |
| 293 | * xprt_adjust_cwnd - adjust transport congestion window |
| 294 | * @task: recently completed RPC request used to adjust window |
| 295 | * @result: result code of completed RPC request |
| 296 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * We use a time-smoothed congestion estimator to avoid heavy oscillation. |
| 298 | */ |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 299 | void xprt_adjust_cwnd(struct rpc_task *task, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 301 | struct rpc_rqst *req = task->tk_rqstp; |
| 302 | struct rpc_xprt *xprt = task->tk_xprt; |
| 303 | unsigned long cwnd = xprt->cwnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | if (result >= 0 && cwnd <= xprt->cong) { |
| 306 | /* The (cwnd >> 1) term makes sure |
| 307 | * the result gets rounded properly. */ |
| 308 | cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; |
| 309 | if (cwnd > RPC_MAXCWND(xprt)) |
| 310 | cwnd = RPC_MAXCWND(xprt); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 311 | __xprt_lock_write_next_cong(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } else if (result == -ETIMEDOUT) { |
| 313 | cwnd >>= 1; |
| 314 | if (cwnd < RPC_CWNDSCALE) |
| 315 | cwnd = RPC_CWNDSCALE; |
| 316 | } |
| 317 | dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", |
| 318 | xprt->cong, xprt->cwnd, cwnd); |
| 319 | xprt->cwnd = cwnd; |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 320 | __xprt_put_cong(xprt, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 323 | /** |
| 324 | * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue |
| 325 | * @xprt: transport with waiting tasks |
| 326 | * @status: result code to plant in each task before waking it |
| 327 | * |
| 328 | */ |
| 329 | void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) |
| 330 | { |
| 331 | if (status < 0) |
| 332 | rpc_wake_up_status(&xprt->pending, status); |
| 333 | else |
| 334 | rpc_wake_up(&xprt->pending); |
| 335 | } |
| 336 | |
Chuck Lever | c7b2cae | 2005-08-11 16:25:50 -0400 | [diff] [blame] | 337 | /** |
| 338 | * xprt_wait_for_buffer_space - wait for transport output buffer to clear |
| 339 | * @task: task to be put to sleep |
| 340 | * |
| 341 | */ |
| 342 | void xprt_wait_for_buffer_space(struct rpc_task *task) |
| 343 | { |
| 344 | struct rpc_rqst *req = task->tk_rqstp; |
| 345 | struct rpc_xprt *xprt = req->rq_xprt; |
| 346 | |
| 347 | task->tk_timeout = req->rq_timeout; |
| 348 | rpc_sleep_on(&xprt->pending, task, NULL, NULL); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * xprt_write_space - wake the task waiting for transport output buffer space |
| 353 | * @xprt: transport with waiting tasks |
| 354 | * |
| 355 | * Can be called in a soft IRQ context, so xprt_write_space never sleeps. |
| 356 | */ |
| 357 | void xprt_write_space(struct rpc_xprt *xprt) |
| 358 | { |
| 359 | if (unlikely(xprt->shutdown)) |
| 360 | return; |
| 361 | |
| 362 | spin_lock_bh(&xprt->transport_lock); |
| 363 | if (xprt->snd_task) { |
| 364 | dprintk("RPC: write space: waking waiting task on xprt %p\n", |
| 365 | xprt); |
| 366 | rpc_wake_up_task(xprt->snd_task); |
| 367 | } |
| 368 | spin_unlock_bh(&xprt->transport_lock); |
| 369 | } |
| 370 | |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 371 | /** |
| 372 | * xprt_set_retrans_timeout_def - set a request's retransmit timeout |
| 373 | * @task: task whose timeout is to be set |
| 374 | * |
| 375 | * Set a request's retransmit timeout based on the transport's |
| 376 | * default timeout parameters. Used by transports that don't adjust |
| 377 | * the retransmit timeout based on round-trip time estimation. |
| 378 | */ |
| 379 | void xprt_set_retrans_timeout_def(struct rpc_task *task) |
| 380 | { |
| 381 | task->tk_timeout = task->tk_rqstp->rq_timeout; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout |
| 386 | * @task: task whose timeout is to be set |
| 387 | * |
| 388 | * Set a request's retransmit timeout using the RTT estimator. |
| 389 | */ |
| 390 | void xprt_set_retrans_timeout_rtt(struct rpc_task *task) |
| 391 | { |
| 392 | int timer = task->tk_msg.rpc_proc->p_timer; |
| 393 | struct rpc_rtt *rtt = task->tk_client->cl_rtt; |
| 394 | struct rpc_rqst *req = task->tk_rqstp; |
| 395 | unsigned long max_timeout = req->rq_xprt->timeout.to_maxval; |
| 396 | |
| 397 | task->tk_timeout = rpc_calc_rto(rtt, timer); |
| 398 | task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; |
| 399 | if (task->tk_timeout > max_timeout || task->tk_timeout == 0) |
| 400 | task->tk_timeout = max_timeout; |
| 401 | } |
| 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | static void xprt_reset_majortimeo(struct rpc_rqst *req) |
| 404 | { |
| 405 | struct rpc_timeout *to = &req->rq_xprt->timeout; |
| 406 | |
| 407 | req->rq_majortimeo = req->rq_timeout; |
| 408 | if (to->to_exponential) |
| 409 | req->rq_majortimeo <<= to->to_retries; |
| 410 | else |
| 411 | req->rq_majortimeo += to->to_increment * to->to_retries; |
| 412 | if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) |
| 413 | req->rq_majortimeo = to->to_maxval; |
| 414 | req->rq_majortimeo += jiffies; |
| 415 | } |
| 416 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 417 | /** |
| 418 | * xprt_adjust_timeout - adjust timeout values for next retransmit |
| 419 | * @req: RPC request containing parameters to use for the adjustment |
| 420 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | */ |
| 422 | int xprt_adjust_timeout(struct rpc_rqst *req) |
| 423 | { |
| 424 | struct rpc_xprt *xprt = req->rq_xprt; |
| 425 | struct rpc_timeout *to = &xprt->timeout; |
| 426 | int status = 0; |
| 427 | |
| 428 | if (time_before(jiffies, req->rq_majortimeo)) { |
| 429 | if (to->to_exponential) |
| 430 | req->rq_timeout <<= 1; |
| 431 | else |
| 432 | req->rq_timeout += to->to_increment; |
| 433 | if (to->to_maxval && req->rq_timeout >= to->to_maxval) |
| 434 | req->rq_timeout = to->to_maxval; |
| 435 | req->rq_retries++; |
| 436 | pprintk("RPC: %lu retrans\n", jiffies); |
| 437 | } else { |
| 438 | req->rq_timeout = to->to_initval; |
| 439 | req->rq_retries = 0; |
| 440 | xprt_reset_majortimeo(req); |
| 441 | /* Reset the RTT counters == "slow start" */ |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 442 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 444 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | pprintk("RPC: %lu timeout\n", jiffies); |
| 446 | status = -ETIMEDOUT; |
| 447 | } |
| 448 | |
| 449 | if (req->rq_timeout == 0) { |
| 450 | printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); |
| 451 | req->rq_timeout = 5 * HZ; |
| 452 | } |
| 453 | return status; |
| 454 | } |
| 455 | |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 456 | static void xprt_autoclose(void *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
| 458 | struct rpc_xprt *xprt = (struct rpc_xprt *)args; |
| 459 | |
| 460 | xprt_disconnect(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 461 | xprt->ops->close(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | xprt_release_write(xprt, NULL); |
| 463 | } |
| 464 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 465 | /** |
| 466 | * xprt_disconnect - mark a transport as disconnected |
| 467 | * @xprt: transport to flag for disconnect |
| 468 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 470 | void xprt_disconnect(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
| 472 | dprintk("RPC: disconnected transport %p\n", xprt); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 473 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | xprt_clear_connected(xprt); |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 475 | xprt_wake_pending_tasks(xprt, -ENOTCONN); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 476 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | static void |
| 480 | xprt_init_autodisconnect(unsigned long data) |
| 481 | { |
| 482 | struct rpc_xprt *xprt = (struct rpc_xprt *)data; |
| 483 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 484 | spin_lock(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | if (!list_empty(&xprt->recv) || xprt->shutdown) |
| 486 | goto out_abort; |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 487 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | goto out_abort; |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 489 | spin_unlock(&xprt->transport_lock); |
Chuck Lever | 2226feb | 2005-08-11 16:25:38 -0400 | [diff] [blame] | 490 | if (xprt_connecting(xprt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | xprt_release_write(xprt, NULL); |
| 492 | else |
| 493 | schedule_work(&xprt->task_cleanup); |
| 494 | return; |
| 495 | out_abort: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 496 | spin_unlock(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 499 | /** |
| 500 | * xprt_connect - schedule a transport connect operation |
| 501 | * @task: RPC task that is requesting the connect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | * |
| 503 | */ |
| 504 | void xprt_connect(struct rpc_task *task) |
| 505 | { |
| 506 | struct rpc_xprt *xprt = task->tk_xprt; |
| 507 | |
| 508 | dprintk("RPC: %4d xprt_connect xprt %p %s connected\n", task->tk_pid, |
| 509 | xprt, (xprt_connected(xprt) ? "is" : "is not")); |
| 510 | |
| 511 | if (xprt->shutdown) { |
| 512 | task->tk_status = -EIO; |
| 513 | return; |
| 514 | } |
| 515 | if (!xprt->addr.sin_port) { |
| 516 | task->tk_status = -EIO; |
| 517 | return; |
| 518 | } |
| 519 | if (!xprt_lock_write(xprt, task)) |
| 520 | return; |
| 521 | if (xprt_connected(xprt)) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 522 | xprt_release_write(xprt, task); |
| 523 | else { |
| 524 | if (task->tk_rqstp) |
| 525 | task->tk_rqstp->rq_bytes_sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 527 | task->tk_timeout = RPC_CONNECT_TIMEOUT; |
| 528 | rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL); |
| 529 | xprt->ops->connect(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | } |
| 531 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 534 | static void xprt_connect_status(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
| 536 | struct rpc_xprt *xprt = task->tk_xprt; |
| 537 | |
| 538 | if (task->tk_status >= 0) { |
| 539 | dprintk("RPC: %4d xprt_connect_status: connection established\n", |
| 540 | task->tk_pid); |
| 541 | return; |
| 542 | } |
| 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | switch (task->tk_status) { |
| 545 | case -ECONNREFUSED: |
| 546 | case -ECONNRESET: |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 547 | dprintk("RPC: %4d xprt_connect_status: server %s refused connection\n", |
| 548 | task->tk_pid, task->tk_client->cl_server); |
| 549 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | case -ENOTCONN: |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 551 | dprintk("RPC: %4d xprt_connect_status: connection broken\n", |
| 552 | task->tk_pid); |
| 553 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | case -ETIMEDOUT: |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 555 | dprintk("RPC: %4d xprt_connect_status: connect attempt timed out\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | task->tk_pid); |
| 557 | break; |
| 558 | default: |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 559 | dprintk("RPC: %4d xprt_connect_status: error %d connecting to server %s\n", |
| 560 | task->tk_pid, -task->tk_status, task->tk_client->cl_server); |
| 561 | xprt_release_write(xprt, task); |
| 562 | task->tk_status = -EIO; |
| 563 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
Chuck Lever | 23475d6 | 2005-08-11 16:25:08 -0400 | [diff] [blame] | 565 | |
| 566 | /* if soft mounted, just cause this RPC to fail */ |
| 567 | if (RPC_IS_SOFT(task)) { |
| 568 | xprt_release_write(xprt, task); |
| 569 | task->tk_status = -EIO; |
| 570 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 573 | /** |
| 574 | * xprt_lookup_rqst - find an RPC request corresponding to an XID |
| 575 | * @xprt: transport on which the original request was transmitted |
| 576 | * @xid: RPC XID of incoming reply |
| 577 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 579 | struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, u32 xid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
| 581 | struct list_head *pos; |
| 582 | struct rpc_rqst *req = NULL; |
| 583 | |
| 584 | list_for_each(pos, &xprt->recv) { |
| 585 | struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list); |
| 586 | if (entry->rq_xid == xid) { |
| 587 | req = entry; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | return req; |
| 592 | } |
| 593 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 594 | /** |
| 595 | * xprt_complete_rqst - called when reply processing is complete |
| 596 | * @xprt: controlling transport |
| 597 | * @req: RPC request that just completed |
| 598 | * @copied: actual number of bytes received from the transport |
| 599 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 601 | void xprt_complete_rqst(struct rpc_xprt *xprt, struct rpc_rqst *req, int copied) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | { |
| 603 | struct rpc_task *task = req->rq_task; |
| 604 | struct rpc_clnt *clnt = task->tk_client; |
| 605 | |
| 606 | /* Adjust congestion window */ |
| 607 | if (!xprt->nocong) { |
| 608 | unsigned timer = task->tk_msg.rpc_proc->p_timer; |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 609 | xprt_adjust_cwnd(task, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (timer) { |
| 611 | if (req->rq_ntrans == 1) |
| 612 | rpc_update_rtt(clnt->cl_rtt, timer, |
| 613 | (long)jiffies - req->rq_xtime); |
| 614 | rpc_set_timeo(clnt->cl_rtt, timer, req->rq_ntrans - 1); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | #ifdef RPC_PROFILE |
| 619 | /* Profile only reads for now */ |
| 620 | if (copied > 1024) { |
| 621 | static unsigned long nextstat; |
| 622 | static unsigned long pkt_rtt, pkt_len, pkt_cnt; |
| 623 | |
| 624 | pkt_cnt++; |
| 625 | pkt_len += req->rq_slen + copied; |
| 626 | pkt_rtt += jiffies - req->rq_xtime; |
| 627 | if (time_before(nextstat, jiffies)) { |
| 628 | printk("RPC: %lu %ld cwnd\n", jiffies, xprt->cwnd); |
| 629 | printk("RPC: %ld %ld %ld %ld stat\n", |
| 630 | jiffies, pkt_cnt, pkt_len, pkt_rtt); |
| 631 | pkt_rtt = pkt_len = pkt_cnt = 0; |
| 632 | nextstat = jiffies + 5 * HZ; |
| 633 | } |
| 634 | } |
| 635 | #endif |
| 636 | |
| 637 | dprintk("RPC: %4d has input (%d bytes)\n", task->tk_pid, copied); |
| 638 | list_del_init(&req->rq_list); |
| 639 | req->rq_received = req->rq_private_buf.len = copied; |
| 640 | |
| 641 | /* ... and wake up the process. */ |
| 642 | rpc_wake_up_task(task); |
| 643 | return; |
| 644 | } |
| 645 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 646 | static void xprt_timer(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 648 | struct rpc_rqst *req = task->tk_rqstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | struct rpc_xprt *xprt = req->rq_xprt; |
| 650 | |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 651 | dprintk("RPC: %4d xprt_timer\n", task->tk_pid); |
| 652 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 653 | spin_lock(&xprt->transport_lock); |
Chuck Lever | 46c0ee8 | 2005-08-25 16:25:52 -0700 | [diff] [blame^] | 654 | if (!req->rq_received) { |
| 655 | if (xprt->ops->timer) |
| 656 | xprt->ops->timer(task); |
| 657 | task->tk_status = -ETIMEDOUT; |
| 658 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | task->tk_timeout = 0; |
| 660 | rpc_wake_up_task(task); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 661 | spin_unlock(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 664 | /** |
| 665 | * xprt_prepare_transmit - reserve the transport before sending a request |
| 666 | * @task: RPC task about to send a request |
| 667 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 669 | int xprt_prepare_transmit(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | { |
| 671 | struct rpc_rqst *req = task->tk_rqstp; |
| 672 | struct rpc_xprt *xprt = req->rq_xprt; |
| 673 | int err = 0; |
| 674 | |
| 675 | dprintk("RPC: %4d xprt_prepare_transmit\n", task->tk_pid); |
| 676 | |
| 677 | if (xprt->shutdown) |
| 678 | return -EIO; |
| 679 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 680 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | if (req->rq_received && !req->rq_bytes_sent) { |
| 682 | err = req->rq_received; |
| 683 | goto out_unlock; |
| 684 | } |
Chuck Lever | 12a8046 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 685 | if (!xprt->ops->reserve_xprt(task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | err = -EAGAIN; |
| 687 | goto out_unlock; |
| 688 | } |
| 689 | |
| 690 | if (!xprt_connected(xprt)) { |
| 691 | err = -ENOTCONN; |
| 692 | goto out_unlock; |
| 693 | } |
| 694 | out_unlock: |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 695 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | return err; |
| 697 | } |
| 698 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 699 | /** |
| 700 | * xprt_transmit - send an RPC request on a transport |
| 701 | * @task: controlling RPC task |
| 702 | * |
| 703 | * We have to copy the iovec because sendmsg fiddles with its contents. |
| 704 | */ |
| 705 | void xprt_transmit(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | struct rpc_rqst *req = task->tk_rqstp; |
| 708 | struct rpc_xprt *xprt = req->rq_xprt; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 709 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
| 711 | dprintk("RPC: %4d xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); |
| 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | smp_rmb(); |
| 714 | if (!req->rq_received) { |
| 715 | if (list_empty(&req->rq_list)) { |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 716 | spin_lock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | /* Update the softirq receive buffer */ |
| 718 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, |
| 719 | sizeof(req->rq_private_buf)); |
| 720 | /* Add request to the receive list */ |
| 721 | list_add_tail(&req->rq_list, &xprt->recv); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 722 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | xprt_reset_majortimeo(req); |
Trond Myklebust | 0f9dc2b | 2005-06-22 17:16:28 +0000 | [diff] [blame] | 724 | /* Turn off autodisconnect */ |
| 725 | del_singleshot_timer_sync(&xprt->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | } |
| 727 | } else if (!req->rq_bytes_sent) |
| 728 | return; |
| 729 | |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 730 | status = xprt->ops->send_request(task); |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 731 | if (status == 0) { |
| 732 | dprintk("RPC: %4d xmit complete\n", task->tk_pid); |
| 733 | spin_lock_bh(&xprt->transport_lock); |
| 734 | xprt->ops->set_retrans_timeout(task); |
| 735 | /* Don't race with disconnect */ |
| 736 | if (!xprt_connected(xprt)) |
| 737 | task->tk_status = -ENOTCONN; |
| 738 | else if (!req->rq_received) |
| 739 | rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 740 | xprt->ops->release_xprt(xprt, task); |
Chuck Lever | fe3aca2 | 2005-08-25 16:25:50 -0700 | [diff] [blame] | 741 | spin_unlock_bh(&xprt->transport_lock); |
| 742 | return; |
| 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
| 745 | /* Note: at this point, task->tk_sleeping has not yet been set, |
| 746 | * hence there is no danger of the waking up task being put on |
| 747 | * schedq, and being picked up by a parallel run of rpciod(). |
| 748 | */ |
| 749 | task->tk_status = status; |
| 750 | |
| 751 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | case -ECONNREFUSED: |
| 753 | task->tk_timeout = RPC_REESTABLISH_TIMEOUT; |
| 754 | rpc_sleep_on(&xprt->sending, task, NULL, NULL); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 755 | case -EAGAIN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | case -ENOTCONN: |
| 757 | return; |
| 758 | default: |
Chuck Lever | 43118c2 | 2005-08-25 16:25:49 -0700 | [diff] [blame] | 759 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | xprt_release_write(xprt, task); |
| 762 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 765 | static inline void do_xprt_reserve(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | { |
| 767 | struct rpc_xprt *xprt = task->tk_xprt; |
| 768 | |
| 769 | task->tk_status = 0; |
| 770 | if (task->tk_rqstp) |
| 771 | return; |
| 772 | if (!list_empty(&xprt->free)) { |
| 773 | struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); |
| 774 | list_del_init(&req->rq_list); |
| 775 | task->tk_rqstp = req; |
| 776 | xprt_request_init(task, xprt); |
| 777 | return; |
| 778 | } |
| 779 | dprintk("RPC: waiting for request slot\n"); |
| 780 | task->tk_status = -EAGAIN; |
| 781 | task->tk_timeout = 0; |
| 782 | rpc_sleep_on(&xprt->backlog, task, NULL, NULL); |
| 783 | } |
| 784 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 785 | /** |
| 786 | * xprt_reserve - allocate an RPC request slot |
| 787 | * @task: RPC task requesting a slot allocation |
| 788 | * |
| 789 | * If no more slots are available, place the task on the transport's |
| 790 | * backlog queue. |
| 791 | */ |
| 792 | void xprt_reserve(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
| 794 | struct rpc_xprt *xprt = task->tk_xprt; |
| 795 | |
| 796 | task->tk_status = -EIO; |
| 797 | if (!xprt->shutdown) { |
Chuck Lever | 5dc0772 | 2005-08-11 16:25:35 -0400 | [diff] [blame] | 798 | spin_lock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | do_xprt_reserve(task); |
Chuck Lever | 5dc0772 | 2005-08-11 16:25:35 -0400 | [diff] [blame] | 800 | spin_unlock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | static inline u32 xprt_alloc_xid(struct rpc_xprt *xprt) |
| 805 | { |
| 806 | return xprt->xid++; |
| 807 | } |
| 808 | |
| 809 | static inline void xprt_init_xid(struct rpc_xprt *xprt) |
| 810 | { |
| 811 | get_random_bytes(&xprt->xid, sizeof(xprt->xid)); |
| 812 | } |
| 813 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 814 | static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | { |
| 816 | struct rpc_rqst *req = task->tk_rqstp; |
| 817 | |
| 818 | req->rq_timeout = xprt->timeout.to_initval; |
| 819 | req->rq_task = task; |
| 820 | req->rq_xprt = xprt; |
| 821 | req->rq_xid = xprt_alloc_xid(xprt); |
| 822 | dprintk("RPC: %4d reserved req %p xid %08x\n", task->tk_pid, |
| 823 | req, ntohl(req->rq_xid)); |
| 824 | } |
| 825 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 826 | /** |
| 827 | * xprt_release - release an RPC request slot |
| 828 | * @task: task which is finished with the slot |
| 829 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 831 | void xprt_release(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | { |
| 833 | struct rpc_xprt *xprt = task->tk_xprt; |
| 834 | struct rpc_rqst *req; |
| 835 | |
| 836 | if (!(req = task->tk_rqstp)) |
| 837 | return; |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 838 | spin_lock_bh(&xprt->transport_lock); |
Chuck Lever | 49e9a89 | 2005-08-25 16:25:51 -0700 | [diff] [blame] | 839 | xprt->ops->release_xprt(xprt, task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | __xprt_put_cong(xprt, req); |
| 841 | if (!list_empty(&req->rq_list)) |
| 842 | list_del(&req->rq_list); |
| 843 | xprt->last_used = jiffies; |
| 844 | if (list_empty(&xprt->recv) && !xprt->shutdown) |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 845 | mod_timer(&xprt->timer, |
| 846 | xprt->last_used + RPC_IDLE_DISCONNECT_TIMEOUT); |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 847 | spin_unlock_bh(&xprt->transport_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | task->tk_rqstp = NULL; |
| 849 | memset(req, 0, sizeof(*req)); /* mark unused */ |
| 850 | |
| 851 | dprintk("RPC: %4d release request %p\n", task->tk_pid, req); |
| 852 | |
Chuck Lever | 5dc0772 | 2005-08-11 16:25:35 -0400 | [diff] [blame] | 853 | spin_lock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | list_add(&req->rq_list, &xprt->free); |
| 855 | xprt_clear_backlog(xprt); |
Chuck Lever | 5dc0772 | 2005-08-11 16:25:35 -0400 | [diff] [blame] | 856 | spin_unlock(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 859 | /** |
| 860 | * xprt_set_timeout - set constant RPC timeout |
| 861 | * @to: RPC timeout parameters to set up |
| 862 | * @retr: number of retries |
| 863 | * @incr: amount of increase after each retry |
| 864 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 866 | void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
| 868 | to->to_initval = |
| 869 | to->to_increment = incr; |
Chuck Lever | eab5c08 | 2005-08-11 16:25:14 -0400 | [diff] [blame] | 870 | to->to_maxval = to->to_initval + (incr * retr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | to->to_retries = retr; |
| 872 | to->to_exponential = 0; |
| 873 | } |
| 874 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 875 | static struct rpc_xprt *xprt_setup(int proto, struct sockaddr_in *ap, struct rpc_timeout *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | { |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 877 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | struct rpc_xprt *xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | struct rpc_rqst *req; |
| 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | if ((xprt = kmalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL) |
| 882 | return ERR_PTR(-ENOMEM); |
| 883 | memset(xprt, 0, sizeof(*xprt)); /* Nnnngh! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | xprt->addr = *ap; |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 886 | |
| 887 | switch (proto) { |
| 888 | case IPPROTO_UDP: |
| 889 | result = xs_setup_udp(xprt, to); |
| 890 | break; |
| 891 | case IPPROTO_TCP: |
| 892 | result = xs_setup_tcp(xprt, to); |
| 893 | break; |
| 894 | default: |
| 895 | printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n", |
| 896 | proto); |
| 897 | result = -EIO; |
| 898 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | } |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 900 | if (result) { |
| 901 | kfree(xprt); |
| 902 | return ERR_PTR(result); |
| 903 | } |
| 904 | |
Chuck Lever | 4a0f8c0 | 2005-08-11 16:25:32 -0400 | [diff] [blame] | 905 | spin_lock_init(&xprt->transport_lock); |
Chuck Lever | 5dc0772 | 2005-08-11 16:25:35 -0400 | [diff] [blame] | 906 | spin_lock_init(&xprt->reserve_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | init_waitqueue_head(&xprt->cong_wait); |
| 908 | |
| 909 | INIT_LIST_HEAD(&xprt->free); |
| 910 | INIT_LIST_HEAD(&xprt->recv); |
Chuck Lever | 55aa4f5 | 2005-08-11 16:25:47 -0400 | [diff] [blame] | 911 | INIT_WORK(&xprt->task_cleanup, xprt_autoclose, xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | init_timer(&xprt->timer); |
| 913 | xprt->timer.function = xprt_init_autodisconnect; |
| 914 | xprt->timer.data = (unsigned long) xprt; |
| 915 | xprt->last_used = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | |
| 917 | rpc_init_wait_queue(&xprt->pending, "xprt_pending"); |
| 918 | rpc_init_wait_queue(&xprt->sending, "xprt_sending"); |
| 919 | rpc_init_wait_queue(&xprt->resend, "xprt_resend"); |
| 920 | rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); |
| 921 | |
| 922 | /* initialize free list */ |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 923 | for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | list_add(&req->rq_list, &xprt->free); |
| 925 | |
| 926 | xprt_init_xid(xprt); |
| 927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | dprintk("RPC: created transport %p with %u slots\n", xprt, |
| 929 | xprt->max_reqs); |
| 930 | |
| 931 | return xprt; |
| 932 | } |
| 933 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 934 | /** |
| 935 | * xprt_create_proto - create an RPC client transport |
| 936 | * @proto: requested transport protocol |
| 937 | * @sap: remote peer's address |
| 938 | * @to: timeout parameters for new transport |
| 939 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 941 | struct rpc_xprt *xprt_create_proto(int proto, struct sockaddr_in *sap, struct rpc_timeout *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | { |
| 943 | struct rpc_xprt *xprt; |
| 944 | |
| 945 | xprt = xprt_setup(proto, sap, to); |
| 946 | if (IS_ERR(xprt)) |
| 947 | dprintk("RPC: xprt_create_proto failed\n"); |
| 948 | else |
| 949 | dprintk("RPC: xprt_create_proto created xprt %p\n", xprt); |
| 950 | return xprt; |
| 951 | } |
| 952 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 953 | static void xprt_shutdown(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | { |
| 955 | xprt->shutdown = 1; |
| 956 | rpc_wake_up(&xprt->sending); |
| 957 | rpc_wake_up(&xprt->resend); |
Chuck Lever | 44fbac2 | 2005-08-11 16:25:44 -0400 | [diff] [blame] | 958 | xprt_wake_pending_tasks(xprt, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | rpc_wake_up(&xprt->backlog); |
| 960 | wake_up(&xprt->cong_wait); |
| 961 | del_timer_sync(&xprt->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 964 | static int xprt_clear_backlog(struct rpc_xprt *xprt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | rpc_wake_up_next(&xprt->backlog); |
| 966 | wake_up(&xprt->cong_wait); |
| 967 | return 1; |
| 968 | } |
| 969 | |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 970 | /** |
| 971 | * xprt_destroy - destroy an RPC transport, killing off all requests. |
| 972 | * @xprt: transport to destroy |
| 973 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | */ |
Chuck Lever | 9903cd1 | 2005-08-11 16:25:26 -0400 | [diff] [blame] | 975 | int xprt_destroy(struct rpc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
| 977 | dprintk("RPC: destroying transport %p\n", xprt); |
| 978 | xprt_shutdown(xprt); |
Chuck Lever | a246b01 | 2005-08-11 16:25:23 -0400 | [diff] [blame] | 979 | xprt->ops->destroy(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | kfree(xprt); |
| 981 | |
| 982 | return 0; |
| 983 | } |