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