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