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 | ||
65 | static int xprt_clear_backlog(struct rpc_xprt *xprt); | |
66 | ||
12a80469 CL |
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. | |
74 | */ | |
75 | int xprt_reserve_xprt(struct rpc_task *task) | |
76 | { | |
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 | ||
1da177e4 | 106 | /* |
12a80469 CL |
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. | |
1da177e4 | 113 | */ |
12a80469 | 114 | int xprt_reserve_xprt_cong(struct rpc_task *task) |
1da177e4 | 115 | { |
12a80469 | 116 | struct rpc_xprt *xprt = task->tk_xprt; |
1da177e4 LT |
117 | struct rpc_rqst *req = task->tk_rqstp; |
118 | ||
2226feb6 | 119 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { |
1da177e4 LT |
120 | if (task == xprt->snd_task) |
121 | return 1; | |
1da177e4 LT |
122 | goto out_sleep; |
123 | } | |
12a80469 | 124 | if (__xprt_get_cong(xprt, task)) { |
1da177e4 LT |
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(); | |
2226feb6 | 133 | clear_bit(XPRT_LOCKED, &xprt->state); |
1da177e4 LT |
134 | smp_mb__after_clear_bit(); |
135 | out_sleep: | |
55aa4f58 | 136 | dprintk("RPC: %4d failed to lock transport %p\n", task->tk_pid, xprt); |
1da177e4 LT |
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 | ||
12a80469 | 146 | static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) |
1da177e4 LT |
147 | { |
148 | int retval; | |
149 | ||
4a0f8c04 | 150 | spin_lock_bh(&xprt->transport_lock); |
12a80469 | 151 | retval = xprt->ops->reserve_xprt(task); |
4a0f8c04 | 152 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
153 | return retval; |
154 | } | |
155 | ||
12a80469 | 156 | static void __xprt_lock_write_next(struct rpc_xprt *xprt) |
49e9a890 CL |
157 | { |
158 | struct rpc_task *task; | |
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) | |
1da177e4 LT |
186 | { |
187 | struct rpc_task *task; | |
188 | ||
2226feb6 | 189 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
1da177e4 | 190 | return; |
49e9a890 | 191 | if (RPCXPRT_CONGESTED(xprt)) |
1da177e4 LT |
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 | } | |
49e9a890 | 199 | if (__xprt_get_cong(xprt, task)) { |
1da177e4 LT |
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(); | |
2226feb6 | 210 | clear_bit(XPRT_LOCKED, &xprt->state); |
1da177e4 LT |
211 | smp_mb__after_clear_bit(); |
212 | } | |
213 | ||
49e9a890 CL |
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. | |
1da177e4 | 220 | */ |
49e9a890 | 221 | void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) |
1da177e4 LT |
222 | { |
223 | if (xprt->snd_task == task) { | |
224 | xprt->snd_task = NULL; | |
225 | smp_mb__before_clear_bit(); | |
2226feb6 | 226 | clear_bit(XPRT_LOCKED, &xprt->state); |
1da177e4 LT |
227 | smp_mb__after_clear_bit(); |
228 | __xprt_lock_write_next(xprt); | |
229 | } | |
230 | } | |
231 | ||
49e9a890 CL |
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) | |
1da177e4 | 252 | { |
4a0f8c04 | 253 | spin_lock_bh(&xprt->transport_lock); |
49e9a890 | 254 | xprt->ops->release_xprt(xprt, task); |
4a0f8c04 | 255 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
256 | } |
257 | ||
1da177e4 LT |
258 | /* |
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; | |
49e9a890 | 289 | __xprt_lock_write_next_cong(xprt); |
1da177e4 LT |
290 | } |
291 | ||
46c0ee8b CL |
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 | * | |
1da177e4 LT |
297 | * We use a time-smoothed congestion estimator to avoid heavy oscillation. |
298 | */ | |
46c0ee8b | 299 | void xprt_adjust_cwnd(struct rpc_task *task, int result) |
1da177e4 | 300 | { |
46c0ee8b CL |
301 | struct rpc_rqst *req = task->tk_rqstp; |
302 | struct rpc_xprt *xprt = task->tk_xprt; | |
303 | unsigned long cwnd = xprt->cwnd; | |
1da177e4 | 304 | |
1da177e4 LT |
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); | |
49e9a890 | 311 | __xprt_lock_write_next_cong(xprt); |
1da177e4 LT |
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; | |
46c0ee8b | 320 | __xprt_put_cong(xprt, req); |
1da177e4 LT |
321 | } |
322 | ||
44fbac22 CL |
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 | ||
c7b2cae8 CL |
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 | ||
fe3aca29 CL |
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 | ||
1da177e4 LT |
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 | ||
9903cd1c CL |
417 | /** |
418 | * xprt_adjust_timeout - adjust timeout values for next retransmit | |
419 | * @req: RPC request containing parameters to use for the adjustment | |
420 | * | |
1da177e4 LT |
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" */ | |
4a0f8c04 | 442 | spin_lock_bh(&xprt->transport_lock); |
1da177e4 | 443 | rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); |
4a0f8c04 | 444 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
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 | ||
55aa4f58 | 456 | static void xprt_autoclose(void *args) |
1da177e4 LT |
457 | { |
458 | struct rpc_xprt *xprt = (struct rpc_xprt *)args; | |
459 | ||
460 | xprt_disconnect(xprt); | |
a246b010 | 461 | xprt->ops->close(xprt); |
1da177e4 LT |
462 | xprt_release_write(xprt, NULL); |
463 | } | |
464 | ||
9903cd1c CL |
465 | /** |
466 | * xprt_disconnect - mark a transport as disconnected | |
467 | * @xprt: transport to flag for disconnect | |
468 | * | |
1da177e4 | 469 | */ |
a246b010 | 470 | void xprt_disconnect(struct rpc_xprt *xprt) |
1da177e4 LT |
471 | { |
472 | dprintk("RPC: disconnected transport %p\n", xprt); | |
4a0f8c04 | 473 | spin_lock_bh(&xprt->transport_lock); |
1da177e4 | 474 | xprt_clear_connected(xprt); |
44fbac22 | 475 | xprt_wake_pending_tasks(xprt, -ENOTCONN); |
4a0f8c04 | 476 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
477 | } |
478 | ||
1da177e4 LT |
479 | static void |
480 | xprt_init_autodisconnect(unsigned long data) | |
481 | { | |
482 | struct rpc_xprt *xprt = (struct rpc_xprt *)data; | |
483 | ||
4a0f8c04 | 484 | spin_lock(&xprt->transport_lock); |
1da177e4 LT |
485 | if (!list_empty(&xprt->recv) || xprt->shutdown) |
486 | goto out_abort; | |
2226feb6 | 487 | if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) |
1da177e4 | 488 | goto out_abort; |
4a0f8c04 | 489 | spin_unlock(&xprt->transport_lock); |
2226feb6 | 490 | if (xprt_connecting(xprt)) |
1da177e4 LT |
491 | xprt_release_write(xprt, NULL); |
492 | else | |
493 | schedule_work(&xprt->task_cleanup); | |
494 | return; | |
495 | out_abort: | |
4a0f8c04 | 496 | spin_unlock(&xprt->transport_lock); |
1da177e4 LT |
497 | } |
498 | ||
9903cd1c CL |
499 | /** |
500 | * xprt_connect - schedule a transport connect operation | |
501 | * @task: RPC task that is requesting the connect | |
1da177e4 LT |
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)) | |
a246b010 CL |
522 | xprt_release_write(xprt, task); |
523 | else { | |
524 | if (task->tk_rqstp) | |
525 | task->tk_rqstp->rq_bytes_sent = 0; | |
1da177e4 | 526 | |
a246b010 CL |
527 | task->tk_timeout = RPC_CONNECT_TIMEOUT; |
528 | rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL); | |
529 | xprt->ops->connect(task); | |
1da177e4 LT |
530 | } |
531 | return; | |
1da177e4 LT |
532 | } |
533 | ||
9903cd1c | 534 | static void xprt_connect_status(struct rpc_task *task) |
1da177e4 LT |
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 | ||
1da177e4 LT |
544 | switch (task->tk_status) { |
545 | case -ECONNREFUSED: | |
546 | case -ECONNRESET: | |
23475d66 CL |
547 | dprintk("RPC: %4d xprt_connect_status: server %s refused connection\n", |
548 | task->tk_pid, task->tk_client->cl_server); | |
549 | break; | |
1da177e4 | 550 | case -ENOTCONN: |
23475d66 CL |
551 | dprintk("RPC: %4d xprt_connect_status: connection broken\n", |
552 | task->tk_pid); | |
553 | break; | |
1da177e4 | 554 | case -ETIMEDOUT: |
23475d66 | 555 | dprintk("RPC: %4d xprt_connect_status: connect attempt timed out\n", |
1da177e4 LT |
556 | task->tk_pid); |
557 | break; | |
558 | default: | |
23475d66 CL |
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; | |
564 | } | |
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; | |
1da177e4 | 570 | } |
1da177e4 LT |
571 | } |
572 | ||
9903cd1c CL |
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 | * | |
1da177e4 | 578 | */ |
a246b010 | 579 | struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, u32 xid) |
1da177e4 LT |
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 | ||
1570c1e4 CL |
594 | /** |
595 | * xprt_update_rtt - update an RPC client's RTT state after receiving a reply | |
596 | * @task: RPC request that recently completed | |
597 | * | |
598 | */ | |
599 | void xprt_update_rtt(struct rpc_task *task) | |
600 | { | |
601 | struct rpc_rqst *req = task->tk_rqstp; | |
602 | struct rpc_rtt *rtt = task->tk_client->cl_rtt; | |
603 | unsigned timer = task->tk_msg.rpc_proc->p_timer; | |
604 | ||
605 | if (timer) { | |
606 | if (req->rq_ntrans == 1) | |
607 | rpc_update_rtt(rtt, timer, | |
608 | (long)jiffies - req->rq_xtime); | |
609 | rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); | |
610 | } | |
611 | } | |
612 | ||
9903cd1c CL |
613 | /** |
614 | * xprt_complete_rqst - called when reply processing is complete | |
1570c1e4 | 615 | * @task: RPC request that recently completed |
9903cd1c CL |
616 | * @copied: actual number of bytes received from the transport |
617 | * | |
1570c1e4 | 618 | * Caller holds transport lock. |
1da177e4 | 619 | */ |
1570c1e4 | 620 | void xprt_complete_rqst(struct rpc_task *task, int copied) |
1da177e4 | 621 | { |
1570c1e4 | 622 | struct rpc_rqst *req = task->tk_rqstp; |
1da177e4 | 623 | |
1570c1e4 CL |
624 | dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", |
625 | task->tk_pid, ntohl(req->rq_xid), copied); | |
1da177e4 | 626 | |
1da177e4 LT |
627 | list_del_init(&req->rq_list); |
628 | req->rq_received = req->rq_private_buf.len = copied; | |
1da177e4 | 629 | rpc_wake_up_task(task); |
1da177e4 LT |
630 | } |
631 | ||
46c0ee8b | 632 | static void xprt_timer(struct rpc_task *task) |
1da177e4 | 633 | { |
46c0ee8b | 634 | struct rpc_rqst *req = task->tk_rqstp; |
1da177e4 LT |
635 | struct rpc_xprt *xprt = req->rq_xprt; |
636 | ||
46c0ee8b | 637 | dprintk("RPC: %4d xprt_timer\n", task->tk_pid); |
1da177e4 | 638 | |
46c0ee8b CL |
639 | spin_lock(&xprt->transport_lock); |
640 | if (!req->rq_received) { | |
641 | if (xprt->ops->timer) | |
642 | xprt->ops->timer(task); | |
643 | task->tk_status = -ETIMEDOUT; | |
644 | } | |
1da177e4 LT |
645 | task->tk_timeout = 0; |
646 | rpc_wake_up_task(task); | |
4a0f8c04 | 647 | spin_unlock(&xprt->transport_lock); |
1da177e4 LT |
648 | } |
649 | ||
9903cd1c CL |
650 | /** |
651 | * xprt_prepare_transmit - reserve the transport before sending a request | |
652 | * @task: RPC task about to send a request | |
653 | * | |
1da177e4 | 654 | */ |
9903cd1c | 655 | int xprt_prepare_transmit(struct rpc_task *task) |
1da177e4 LT |
656 | { |
657 | struct rpc_rqst *req = task->tk_rqstp; | |
658 | struct rpc_xprt *xprt = req->rq_xprt; | |
659 | int err = 0; | |
660 | ||
661 | dprintk("RPC: %4d xprt_prepare_transmit\n", task->tk_pid); | |
662 | ||
663 | if (xprt->shutdown) | |
664 | return -EIO; | |
665 | ||
4a0f8c04 | 666 | spin_lock_bh(&xprt->transport_lock); |
1da177e4 LT |
667 | if (req->rq_received && !req->rq_bytes_sent) { |
668 | err = req->rq_received; | |
669 | goto out_unlock; | |
670 | } | |
12a80469 | 671 | if (!xprt->ops->reserve_xprt(task)) { |
1da177e4 LT |
672 | err = -EAGAIN; |
673 | goto out_unlock; | |
674 | } | |
675 | ||
676 | if (!xprt_connected(xprt)) { | |
677 | err = -ENOTCONN; | |
678 | goto out_unlock; | |
679 | } | |
680 | out_unlock: | |
4a0f8c04 | 681 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
682 | return err; |
683 | } | |
684 | ||
9903cd1c CL |
685 | /** |
686 | * xprt_transmit - send an RPC request on a transport | |
687 | * @task: controlling RPC task | |
688 | * | |
689 | * We have to copy the iovec because sendmsg fiddles with its contents. | |
690 | */ | |
691 | void xprt_transmit(struct rpc_task *task) | |
1da177e4 | 692 | { |
1da177e4 LT |
693 | struct rpc_rqst *req = task->tk_rqstp; |
694 | struct rpc_xprt *xprt = req->rq_xprt; | |
a246b010 | 695 | int status; |
1da177e4 LT |
696 | |
697 | dprintk("RPC: %4d xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); | |
698 | ||
1da177e4 LT |
699 | smp_rmb(); |
700 | if (!req->rq_received) { | |
701 | if (list_empty(&req->rq_list)) { | |
4a0f8c04 | 702 | spin_lock_bh(&xprt->transport_lock); |
1da177e4 LT |
703 | /* Update the softirq receive buffer */ |
704 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, | |
705 | sizeof(req->rq_private_buf)); | |
706 | /* Add request to the receive list */ | |
707 | list_add_tail(&req->rq_list, &xprt->recv); | |
4a0f8c04 | 708 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 | 709 | xprt_reset_majortimeo(req); |
0f9dc2b1 TM |
710 | /* Turn off autodisconnect */ |
711 | del_singleshot_timer_sync(&xprt->timer); | |
1da177e4 LT |
712 | } |
713 | } else if (!req->rq_bytes_sent) | |
714 | return; | |
715 | ||
a246b010 | 716 | status = xprt->ops->send_request(task); |
fe3aca29 CL |
717 | if (status == 0) { |
718 | dprintk("RPC: %4d xmit complete\n", task->tk_pid); | |
719 | spin_lock_bh(&xprt->transport_lock); | |
720 | xprt->ops->set_retrans_timeout(task); | |
721 | /* Don't race with disconnect */ | |
722 | if (!xprt_connected(xprt)) | |
723 | task->tk_status = -ENOTCONN; | |
724 | else if (!req->rq_received) | |
725 | rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer); | |
49e9a890 | 726 | xprt->ops->release_xprt(xprt, task); |
fe3aca29 CL |
727 | spin_unlock_bh(&xprt->transport_lock); |
728 | return; | |
729 | } | |
1da177e4 LT |
730 | |
731 | /* Note: at this point, task->tk_sleeping has not yet been set, | |
732 | * hence there is no danger of the waking up task being put on | |
733 | * schedq, and being picked up by a parallel run of rpciod(). | |
734 | */ | |
735 | task->tk_status = status; | |
736 | ||
737 | switch (status) { | |
1da177e4 LT |
738 | case -ECONNREFUSED: |
739 | task->tk_timeout = RPC_REESTABLISH_TIMEOUT; | |
740 | rpc_sleep_on(&xprt->sending, task, NULL, NULL); | |
a246b010 | 741 | case -EAGAIN: |
1da177e4 LT |
742 | case -ENOTCONN: |
743 | return; | |
744 | default: | |
43118c29 | 745 | break; |
1da177e4 LT |
746 | } |
747 | xprt_release_write(xprt, task); | |
748 | return; | |
1da177e4 LT |
749 | } |
750 | ||
9903cd1c | 751 | static inline void do_xprt_reserve(struct rpc_task *task) |
1da177e4 LT |
752 | { |
753 | struct rpc_xprt *xprt = task->tk_xprt; | |
754 | ||
755 | task->tk_status = 0; | |
756 | if (task->tk_rqstp) | |
757 | return; | |
758 | if (!list_empty(&xprt->free)) { | |
759 | struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); | |
760 | list_del_init(&req->rq_list); | |
761 | task->tk_rqstp = req; | |
762 | xprt_request_init(task, xprt); | |
763 | return; | |
764 | } | |
765 | dprintk("RPC: waiting for request slot\n"); | |
766 | task->tk_status = -EAGAIN; | |
767 | task->tk_timeout = 0; | |
768 | rpc_sleep_on(&xprt->backlog, task, NULL, NULL); | |
769 | } | |
770 | ||
9903cd1c CL |
771 | /** |
772 | * xprt_reserve - allocate an RPC request slot | |
773 | * @task: RPC task requesting a slot allocation | |
774 | * | |
775 | * If no more slots are available, place the task on the transport's | |
776 | * backlog queue. | |
777 | */ | |
778 | void xprt_reserve(struct rpc_task *task) | |
1da177e4 LT |
779 | { |
780 | struct rpc_xprt *xprt = task->tk_xprt; | |
781 | ||
782 | task->tk_status = -EIO; | |
783 | if (!xprt->shutdown) { | |
5dc07727 | 784 | spin_lock(&xprt->reserve_lock); |
1da177e4 | 785 | do_xprt_reserve(task); |
5dc07727 | 786 | spin_unlock(&xprt->reserve_lock); |
1da177e4 LT |
787 | } |
788 | } | |
789 | ||
1da177e4 LT |
790 | static inline u32 xprt_alloc_xid(struct rpc_xprt *xprt) |
791 | { | |
792 | return xprt->xid++; | |
793 | } | |
794 | ||
795 | static inline void xprt_init_xid(struct rpc_xprt *xprt) | |
796 | { | |
797 | get_random_bytes(&xprt->xid, sizeof(xprt->xid)); | |
798 | } | |
799 | ||
9903cd1c | 800 | static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) |
1da177e4 LT |
801 | { |
802 | struct rpc_rqst *req = task->tk_rqstp; | |
803 | ||
804 | req->rq_timeout = xprt->timeout.to_initval; | |
805 | req->rq_task = task; | |
806 | req->rq_xprt = xprt; | |
807 | req->rq_xid = xprt_alloc_xid(xprt); | |
808 | dprintk("RPC: %4d reserved req %p xid %08x\n", task->tk_pid, | |
809 | req, ntohl(req->rq_xid)); | |
810 | } | |
811 | ||
9903cd1c CL |
812 | /** |
813 | * xprt_release - release an RPC request slot | |
814 | * @task: task which is finished with the slot | |
815 | * | |
1da177e4 | 816 | */ |
9903cd1c | 817 | void xprt_release(struct rpc_task *task) |
1da177e4 LT |
818 | { |
819 | struct rpc_xprt *xprt = task->tk_xprt; | |
820 | struct rpc_rqst *req; | |
821 | ||
822 | if (!(req = task->tk_rqstp)) | |
823 | return; | |
4a0f8c04 | 824 | spin_lock_bh(&xprt->transport_lock); |
49e9a890 | 825 | xprt->ops->release_xprt(xprt, task); |
1da177e4 LT |
826 | __xprt_put_cong(xprt, req); |
827 | if (!list_empty(&req->rq_list)) | |
828 | list_del(&req->rq_list); | |
829 | xprt->last_used = jiffies; | |
830 | if (list_empty(&xprt->recv) && !xprt->shutdown) | |
a246b010 CL |
831 | mod_timer(&xprt->timer, |
832 | xprt->last_used + RPC_IDLE_DISCONNECT_TIMEOUT); | |
4a0f8c04 | 833 | spin_unlock_bh(&xprt->transport_lock); |
1da177e4 LT |
834 | task->tk_rqstp = NULL; |
835 | memset(req, 0, sizeof(*req)); /* mark unused */ | |
836 | ||
837 | dprintk("RPC: %4d release request %p\n", task->tk_pid, req); | |
838 | ||
5dc07727 | 839 | spin_lock(&xprt->reserve_lock); |
1da177e4 LT |
840 | list_add(&req->rq_list, &xprt->free); |
841 | xprt_clear_backlog(xprt); | |
5dc07727 | 842 | spin_unlock(&xprt->reserve_lock); |
1da177e4 LT |
843 | } |
844 | ||
9903cd1c CL |
845 | /** |
846 | * xprt_set_timeout - set constant RPC timeout | |
847 | * @to: RPC timeout parameters to set up | |
848 | * @retr: number of retries | |
849 | * @incr: amount of increase after each retry | |
850 | * | |
1da177e4 | 851 | */ |
9903cd1c | 852 | void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr) |
1da177e4 LT |
853 | { |
854 | to->to_initval = | |
855 | to->to_increment = incr; | |
eab5c084 | 856 | to->to_maxval = to->to_initval + (incr * retr); |
1da177e4 LT |
857 | to->to_retries = retr; |
858 | to->to_exponential = 0; | |
859 | } | |
860 | ||
9903cd1c | 861 | static struct rpc_xprt *xprt_setup(int proto, struct sockaddr_in *ap, struct rpc_timeout *to) |
1da177e4 | 862 | { |
a246b010 | 863 | int result; |
1da177e4 | 864 | struct rpc_xprt *xprt; |
1da177e4 LT |
865 | struct rpc_rqst *req; |
866 | ||
1da177e4 LT |
867 | if ((xprt = kmalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL) |
868 | return ERR_PTR(-ENOMEM); | |
869 | memset(xprt, 0, sizeof(*xprt)); /* Nnnngh! */ | |
1da177e4 LT |
870 | |
871 | xprt->addr = *ap; | |
a246b010 CL |
872 | |
873 | switch (proto) { | |
874 | case IPPROTO_UDP: | |
875 | result = xs_setup_udp(xprt, to); | |
876 | break; | |
877 | case IPPROTO_TCP: | |
878 | result = xs_setup_tcp(xprt, to); | |
879 | break; | |
880 | default: | |
881 | printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n", | |
882 | proto); | |
883 | result = -EIO; | |
884 | break; | |
885 | } | |
886 | if (result) { | |
887 | kfree(xprt); | |
888 | return ERR_PTR(result); | |
1da177e4 | 889 | } |
a246b010 | 890 | |
4a0f8c04 | 891 | spin_lock_init(&xprt->transport_lock); |
5dc07727 | 892 | spin_lock_init(&xprt->reserve_lock); |
1da177e4 LT |
893 | init_waitqueue_head(&xprt->cong_wait); |
894 | ||
895 | INIT_LIST_HEAD(&xprt->free); | |
896 | INIT_LIST_HEAD(&xprt->recv); | |
55aa4f58 | 897 | INIT_WORK(&xprt->task_cleanup, xprt_autoclose, xprt); |
1da177e4 LT |
898 | init_timer(&xprt->timer); |
899 | xprt->timer.function = xprt_init_autodisconnect; | |
900 | xprt->timer.data = (unsigned long) xprt; | |
901 | xprt->last_used = jiffies; | |
1da177e4 LT |
902 | |
903 | rpc_init_wait_queue(&xprt->pending, "xprt_pending"); | |
904 | rpc_init_wait_queue(&xprt->sending, "xprt_sending"); | |
905 | rpc_init_wait_queue(&xprt->resend, "xprt_resend"); | |
906 | rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); | |
907 | ||
908 | /* initialize free list */ | |
a246b010 | 909 | for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--) |
1da177e4 LT |
910 | list_add(&req->rq_list, &xprt->free); |
911 | ||
912 | xprt_init_xid(xprt); | |
913 | ||
1da177e4 LT |
914 | dprintk("RPC: created transport %p with %u slots\n", xprt, |
915 | xprt->max_reqs); | |
916 | ||
917 | return xprt; | |
918 | } | |
919 | ||
9903cd1c CL |
920 | /** |
921 | * xprt_create_proto - create an RPC client transport | |
922 | * @proto: requested transport protocol | |
923 | * @sap: remote peer's address | |
924 | * @to: timeout parameters for new transport | |
925 | * | |
1da177e4 | 926 | */ |
9903cd1c | 927 | struct rpc_xprt *xprt_create_proto(int proto, struct sockaddr_in *sap, struct rpc_timeout *to) |
1da177e4 LT |
928 | { |
929 | struct rpc_xprt *xprt; | |
930 | ||
931 | xprt = xprt_setup(proto, sap, to); | |
932 | if (IS_ERR(xprt)) | |
933 | dprintk("RPC: xprt_create_proto failed\n"); | |
934 | else | |
935 | dprintk("RPC: xprt_create_proto created xprt %p\n", xprt); | |
936 | return xprt; | |
937 | } | |
938 | ||
9903cd1c | 939 | static void xprt_shutdown(struct rpc_xprt *xprt) |
1da177e4 LT |
940 | { |
941 | xprt->shutdown = 1; | |
942 | rpc_wake_up(&xprt->sending); | |
943 | rpc_wake_up(&xprt->resend); | |
44fbac22 | 944 | xprt_wake_pending_tasks(xprt, -EIO); |
1da177e4 LT |
945 | rpc_wake_up(&xprt->backlog); |
946 | wake_up(&xprt->cong_wait); | |
947 | del_timer_sync(&xprt->timer); | |
948 | } | |
949 | ||
9903cd1c | 950 | static int xprt_clear_backlog(struct rpc_xprt *xprt) { |
1da177e4 LT |
951 | rpc_wake_up_next(&xprt->backlog); |
952 | wake_up(&xprt->cong_wait); | |
953 | return 1; | |
954 | } | |
955 | ||
9903cd1c CL |
956 | /** |
957 | * xprt_destroy - destroy an RPC transport, killing off all requests. | |
958 | * @xprt: transport to destroy | |
959 | * | |
1da177e4 | 960 | */ |
9903cd1c | 961 | int xprt_destroy(struct rpc_xprt *xprt) |
1da177e4 LT |
962 | { |
963 | dprintk("RPC: destroying transport %p\n", xprt); | |
964 | xprt_shutdown(xprt); | |
a246b010 | 965 | xprt->ops->destroy(xprt); |
1da177e4 LT |
966 | kfree(xprt); |
967 | ||
968 | return 0; | |
969 | } |