2 * linux/net/sunrpc/xprt.c
4 * This is a generic RPC call interface supporting congestion avoidance,
5 * and asynchronous calls.
7 * The interface works like this:
9 * - When a process places a call, it allocates a request slot if
10 * one is available. Otherwise, it sleeps on the backlog queue
12 * - Next, the caller puts together the RPC message, stuffs it into
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
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 transport. 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
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
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.
34 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
36 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
39 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/interrupt.h>
43 #include <linux/workqueue.h>
44 #include <linux/net.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/sunrpc/metrics.h>
54 # define RPCDBG_FACILITY RPCDBG_XPRT
60 static void xprt_request_init(struct rpc_task
*, struct rpc_xprt
*);
61 static inline void do_xprt_reserve(struct rpc_task
*);
62 static void xprt_connect_status(struct rpc_task
*task
);
63 static int __xprt_get_cong(struct rpc_xprt
*, struct rpc_task
*);
65 static DEFINE_SPINLOCK(xprt_list_lock
);
66 static LIST_HEAD(xprt_list
);
69 * The transport code maintains an estimate on the maximum number of out-
70 * standing RPC requests, using a smoothed version of the congestion
71 * avoidance implemented in 44BSD. This is basically the Van Jacobson
72 * congestion algorithm: If a retransmit occurs, the congestion window is
73 * halved; otherwise, it is incremented by 1/cwnd when
75 * - a reply is received and
76 * - a full number of requests are outstanding and
77 * - the congestion window hasn't been updated recently.
79 #define RPC_CWNDSHIFT (8U)
80 #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
81 #define RPC_INITCWND RPC_CWNDSCALE
82 #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
84 #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
87 * xprt_register_transport - register a transport implementation
88 * @transport: transport to register
90 * If a transport implementation is loaded as a kernel module, it can
91 * call this interface to make itself known to the RPC client.
94 * 0: transport successfully registered
95 * -EEXIST: transport already registered
96 * -EINVAL: transport module being unloaded
98 int xprt_register_transport(struct xprt_class
*transport
)
100 struct xprt_class
*t
;
104 spin_lock(&xprt_list_lock
);
105 list_for_each_entry(t
, &xprt_list
, list
) {
106 /* don't register the same transport class twice */
107 if (t
->ident
== transport
->ident
)
112 if (try_module_get(THIS_MODULE
)) {
113 list_add_tail(&transport
->list
, &xprt_list
);
114 printk(KERN_INFO
"RPC: Registered %s transport module.\n",
120 spin_unlock(&xprt_list_lock
);
123 EXPORT_SYMBOL_GPL(xprt_register_transport
);
126 * xprt_unregister_transport - unregister a transport implementation
127 * transport: transport to unregister
130 * 0: transport successfully unregistered
131 * -ENOENT: transport never registered
133 int xprt_unregister_transport(struct xprt_class
*transport
)
135 struct xprt_class
*t
;
139 spin_lock(&xprt_list_lock
);
140 list_for_each_entry(t
, &xprt_list
, list
) {
141 if (t
== transport
) {
143 "RPC: Unregistered %s transport module.\n",
145 list_del_init(&transport
->list
);
146 module_put(THIS_MODULE
);
153 spin_unlock(&xprt_list_lock
);
156 EXPORT_SYMBOL_GPL(xprt_unregister_transport
);
159 * xprt_reserve_xprt - serialize write access to transports
160 * @task: task that is requesting access to the transport
162 * This prevents mixing the payload of separate requests, and prevents
163 * transport connects from colliding with writes. No congestion control
166 int xprt_reserve_xprt(struct rpc_task
*task
)
168 struct rpc_xprt
*xprt
= task
->tk_xprt
;
169 struct rpc_rqst
*req
= task
->tk_rqstp
;
171 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
)) {
172 if (task
== xprt
->snd_task
)
178 xprt
->snd_task
= task
;
180 req
->rq_bytes_sent
= 0;
186 dprintk("RPC: %5u failed to lock transport %p\n",
188 task
->tk_timeout
= 0;
189 task
->tk_status
= -EAGAIN
;
190 if (req
&& req
->rq_ntrans
)
191 rpc_sleep_on(&xprt
->resend
, task
, NULL
, NULL
);
193 rpc_sleep_on(&xprt
->sending
, task
, NULL
, NULL
);
196 EXPORT_SYMBOL_GPL(xprt_reserve_xprt
);
198 static void xprt_clear_locked(struct rpc_xprt
*xprt
)
200 xprt
->snd_task
= NULL
;
201 if (!test_bit(XPRT_CLOSE_WAIT
, &xprt
->state
) || xprt
->shutdown
) {
202 smp_mb__before_clear_bit();
203 clear_bit(XPRT_LOCKED
, &xprt
->state
);
204 smp_mb__after_clear_bit();
206 queue_work(rpciod_workqueue
, &xprt
->task_cleanup
);
210 * xprt_reserve_xprt_cong - serialize write access to transports
211 * @task: task that is requesting access to the transport
213 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
214 * integrated into the decision of whether a request is allowed to be
215 * woken up and given access to the transport.
217 int xprt_reserve_xprt_cong(struct rpc_task
*task
)
219 struct rpc_xprt
*xprt
= task
->tk_xprt
;
220 struct rpc_rqst
*req
= task
->tk_rqstp
;
222 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
)) {
223 if (task
== xprt
->snd_task
)
227 if (__xprt_get_cong(xprt
, task
)) {
228 xprt
->snd_task
= task
;
230 req
->rq_bytes_sent
= 0;
235 xprt_clear_locked(xprt
);
237 dprintk("RPC: %5u failed to lock transport %p\n", task
->tk_pid
, xprt
);
238 task
->tk_timeout
= 0;
239 task
->tk_status
= -EAGAIN
;
240 if (req
&& req
->rq_ntrans
)
241 rpc_sleep_on(&xprt
->resend
, task
, NULL
, NULL
);
243 rpc_sleep_on(&xprt
->sending
, task
, NULL
, NULL
);
246 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong
);
248 static inline int xprt_lock_write(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
252 spin_lock_bh(&xprt
->transport_lock
);
253 retval
= xprt
->ops
->reserve_xprt(task
);
254 spin_unlock_bh(&xprt
->transport_lock
);
258 static void __xprt_lock_write_next(struct rpc_xprt
*xprt
)
260 struct rpc_task
*task
;
261 struct rpc_rqst
*req
;
263 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
))
266 task
= rpc_wake_up_next(&xprt
->resend
);
268 task
= rpc_wake_up_next(&xprt
->sending
);
273 req
= task
->tk_rqstp
;
274 xprt
->snd_task
= task
;
276 req
->rq_bytes_sent
= 0;
282 xprt_clear_locked(xprt
);
285 static void __xprt_lock_write_next_cong(struct rpc_xprt
*xprt
)
287 struct rpc_task
*task
;
289 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
))
291 if (RPCXPRT_CONGESTED(xprt
))
293 task
= rpc_wake_up_next(&xprt
->resend
);
295 task
= rpc_wake_up_next(&xprt
->sending
);
299 if (__xprt_get_cong(xprt
, task
)) {
300 struct rpc_rqst
*req
= task
->tk_rqstp
;
301 xprt
->snd_task
= task
;
303 req
->rq_bytes_sent
= 0;
309 xprt_clear_locked(xprt
);
313 * xprt_release_xprt - allow other requests to use a transport
314 * @xprt: transport with other tasks potentially waiting
315 * @task: task that is releasing access to the transport
317 * Note that "task" can be NULL. No congestion control is provided.
319 void xprt_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
321 if (xprt
->snd_task
== task
) {
322 xprt_clear_locked(xprt
);
323 __xprt_lock_write_next(xprt
);
326 EXPORT_SYMBOL_GPL(xprt_release_xprt
);
329 * xprt_release_xprt_cong - allow other requests to use a transport
330 * @xprt: transport with other tasks potentially waiting
331 * @task: task that is releasing access to the transport
333 * Note that "task" can be NULL. Another task is awoken to use the
334 * transport if the transport's congestion window allows it.
336 void xprt_release_xprt_cong(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
338 if (xprt
->snd_task
== task
) {
339 xprt_clear_locked(xprt
);
340 __xprt_lock_write_next_cong(xprt
);
343 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong
);
345 static inline void xprt_release_write(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
347 spin_lock_bh(&xprt
->transport_lock
);
348 xprt
->ops
->release_xprt(xprt
, task
);
349 spin_unlock_bh(&xprt
->transport_lock
);
353 * Van Jacobson congestion avoidance. Check if the congestion window
354 * overflowed. Put the task to sleep if this is the case.
357 __xprt_get_cong(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
359 struct rpc_rqst
*req
= task
->tk_rqstp
;
363 dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
364 task
->tk_pid
, xprt
->cong
, xprt
->cwnd
);
365 if (RPCXPRT_CONGESTED(xprt
))
368 xprt
->cong
+= RPC_CWNDSCALE
;
373 * Adjust the congestion window, and wake up the next task
374 * that has been sleeping due to congestion
377 __xprt_put_cong(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
382 xprt
->cong
-= RPC_CWNDSCALE
;
383 __xprt_lock_write_next_cong(xprt
);
387 * xprt_release_rqst_cong - housekeeping when request is complete
388 * @task: RPC request that recently completed
390 * Useful for transports that require congestion control.
392 void xprt_release_rqst_cong(struct rpc_task
*task
)
394 __xprt_put_cong(task
->tk_xprt
, task
->tk_rqstp
);
396 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong
);
399 * xprt_adjust_cwnd - adjust transport congestion window
400 * @task: recently completed RPC request used to adjust window
401 * @result: result code of completed RPC request
403 * We use a time-smoothed congestion estimator to avoid heavy oscillation.
405 void xprt_adjust_cwnd(struct rpc_task
*task
, int result
)
407 struct rpc_rqst
*req
= task
->tk_rqstp
;
408 struct rpc_xprt
*xprt
= task
->tk_xprt
;
409 unsigned long cwnd
= xprt
->cwnd
;
411 if (result
>= 0 && cwnd
<= xprt
->cong
) {
412 /* The (cwnd >> 1) term makes sure
413 * the result gets rounded properly. */
414 cwnd
+= (RPC_CWNDSCALE
* RPC_CWNDSCALE
+ (cwnd
>> 1)) / cwnd
;
415 if (cwnd
> RPC_MAXCWND(xprt
))
416 cwnd
= RPC_MAXCWND(xprt
);
417 __xprt_lock_write_next_cong(xprt
);
418 } else if (result
== -ETIMEDOUT
) {
420 if (cwnd
< RPC_CWNDSCALE
)
421 cwnd
= RPC_CWNDSCALE
;
423 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
424 xprt
->cong
, xprt
->cwnd
, cwnd
);
426 __xprt_put_cong(xprt
, req
);
428 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd
);
431 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
432 * @xprt: transport with waiting tasks
433 * @status: result code to plant in each task before waking it
436 void xprt_wake_pending_tasks(struct rpc_xprt
*xprt
, int status
)
439 rpc_wake_up_status(&xprt
->pending
, status
);
441 rpc_wake_up(&xprt
->pending
);
443 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks
);
446 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
447 * @task: task to be put to sleep
450 void xprt_wait_for_buffer_space(struct rpc_task
*task
)
452 struct rpc_rqst
*req
= task
->tk_rqstp
;
453 struct rpc_xprt
*xprt
= req
->rq_xprt
;
455 task
->tk_timeout
= req
->rq_timeout
;
456 rpc_sleep_on(&xprt
->pending
, task
, NULL
, NULL
);
458 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space
);
461 * xprt_write_space - wake the task waiting for transport output buffer space
462 * @xprt: transport with waiting tasks
464 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
466 void xprt_write_space(struct rpc_xprt
*xprt
)
468 if (unlikely(xprt
->shutdown
))
471 spin_lock_bh(&xprt
->transport_lock
);
472 if (xprt
->snd_task
) {
473 dprintk("RPC: write space: waking waiting task on "
475 rpc_wake_up_task(xprt
->snd_task
);
477 spin_unlock_bh(&xprt
->transport_lock
);
479 EXPORT_SYMBOL_GPL(xprt_write_space
);
482 * xprt_set_retrans_timeout_def - set a request's retransmit timeout
483 * @task: task whose timeout is to be set
485 * Set a request's retransmit timeout based on the transport's
486 * default timeout parameters. Used by transports that don't adjust
487 * the retransmit timeout based on round-trip time estimation.
489 void xprt_set_retrans_timeout_def(struct rpc_task
*task
)
491 task
->tk_timeout
= task
->tk_rqstp
->rq_timeout
;
493 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def
);
496 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
497 * @task: task whose timeout is to be set
499 * Set a request's retransmit timeout using the RTT estimator.
501 void xprt_set_retrans_timeout_rtt(struct rpc_task
*task
)
503 int timer
= task
->tk_msg
.rpc_proc
->p_timer
;
504 struct rpc_rtt
*rtt
= task
->tk_client
->cl_rtt
;
505 struct rpc_rqst
*req
= task
->tk_rqstp
;
506 unsigned long max_timeout
= req
->rq_xprt
->timeout
.to_maxval
;
508 task
->tk_timeout
= rpc_calc_rto(rtt
, timer
);
509 task
->tk_timeout
<<= rpc_ntimeo(rtt
, timer
) + req
->rq_retries
;
510 if (task
->tk_timeout
> max_timeout
|| task
->tk_timeout
== 0)
511 task
->tk_timeout
= max_timeout
;
513 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt
);
515 static void xprt_reset_majortimeo(struct rpc_rqst
*req
)
517 struct rpc_timeout
*to
= &req
->rq_xprt
->timeout
;
519 req
->rq_majortimeo
= req
->rq_timeout
;
520 if (to
->to_exponential
)
521 req
->rq_majortimeo
<<= to
->to_retries
;
523 req
->rq_majortimeo
+= to
->to_increment
* to
->to_retries
;
524 if (req
->rq_majortimeo
> to
->to_maxval
|| req
->rq_majortimeo
== 0)
525 req
->rq_majortimeo
= to
->to_maxval
;
526 req
->rq_majortimeo
+= jiffies
;
530 * xprt_adjust_timeout - adjust timeout values for next retransmit
531 * @req: RPC request containing parameters to use for the adjustment
534 int xprt_adjust_timeout(struct rpc_rqst
*req
)
536 struct rpc_xprt
*xprt
= req
->rq_xprt
;
537 struct rpc_timeout
*to
= &xprt
->timeout
;
540 if (time_before(jiffies
, req
->rq_majortimeo
)) {
541 if (to
->to_exponential
)
542 req
->rq_timeout
<<= 1;
544 req
->rq_timeout
+= to
->to_increment
;
545 if (to
->to_maxval
&& req
->rq_timeout
>= to
->to_maxval
)
546 req
->rq_timeout
= to
->to_maxval
;
549 req
->rq_timeout
= to
->to_initval
;
551 xprt_reset_majortimeo(req
);
552 /* Reset the RTT counters == "slow start" */
553 spin_lock_bh(&xprt
->transport_lock
);
554 rpc_init_rtt(req
->rq_task
->tk_client
->cl_rtt
, to
->to_initval
);
555 spin_unlock_bh(&xprt
->transport_lock
);
559 if (req
->rq_timeout
== 0) {
560 printk(KERN_WARNING
"xprt_adjust_timeout: rq_timeout = 0!\n");
561 req
->rq_timeout
= 5 * HZ
;
566 static void xprt_autoclose(struct work_struct
*work
)
568 struct rpc_xprt
*xprt
=
569 container_of(work
, struct rpc_xprt
, task_cleanup
);
571 xprt_disconnect(xprt
);
572 xprt
->ops
->close(xprt
);
573 xprt_release_write(xprt
, NULL
);
577 * xprt_disconnect - mark a transport as disconnected
578 * @xprt: transport to flag for disconnect
581 void xprt_disconnect(struct rpc_xprt
*xprt
)
583 dprintk("RPC: disconnected transport %p\n", xprt
);
584 spin_lock_bh(&xprt
->transport_lock
);
585 xprt_clear_connected(xprt
);
586 xprt_wake_pending_tasks(xprt
, -ENOTCONN
);
587 spin_unlock_bh(&xprt
->transport_lock
);
589 EXPORT_SYMBOL_GPL(xprt_disconnect
);
592 xprt_init_autodisconnect(unsigned long data
)
594 struct rpc_xprt
*xprt
= (struct rpc_xprt
*)data
;
596 spin_lock(&xprt
->transport_lock
);
597 if (!list_empty(&xprt
->recv
) || xprt
->shutdown
)
599 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
))
601 spin_unlock(&xprt
->transport_lock
);
602 if (xprt_connecting(xprt
))
603 xprt_release_write(xprt
, NULL
);
605 queue_work(rpciod_workqueue
, &xprt
->task_cleanup
);
608 spin_unlock(&xprt
->transport_lock
);
612 * xprt_connect - schedule a transport connect operation
613 * @task: RPC task that is requesting the connect
616 void xprt_connect(struct rpc_task
*task
)
618 struct rpc_xprt
*xprt
= task
->tk_xprt
;
620 dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task
->tk_pid
,
621 xprt
, (xprt_connected(xprt
) ? "is" : "is not"));
623 if (!xprt_bound(xprt
)) {
624 task
->tk_status
= -EIO
;
627 if (!xprt_lock_write(xprt
, task
))
629 if (xprt_connected(xprt
))
630 xprt_release_write(xprt
, task
);
633 task
->tk_rqstp
->rq_bytes_sent
= 0;
635 task
->tk_timeout
= xprt
->connect_timeout
;
636 rpc_sleep_on(&xprt
->pending
, task
, xprt_connect_status
, NULL
);
637 xprt
->stat
.connect_start
= jiffies
;
638 xprt
->ops
->connect(task
);
643 static void xprt_connect_status(struct rpc_task
*task
)
645 struct rpc_xprt
*xprt
= task
->tk_xprt
;
647 if (task
->tk_status
>= 0) {
648 xprt
->stat
.connect_count
++;
649 xprt
->stat
.connect_time
+= (long)jiffies
- xprt
->stat
.connect_start
;
650 dprintk("RPC: %5u xprt_connect_status: connection established\n",
655 switch (task
->tk_status
) {
658 dprintk("RPC: %5u xprt_connect_status: server %s refused "
659 "connection\n", task
->tk_pid
,
660 task
->tk_client
->cl_server
);
663 dprintk("RPC: %5u xprt_connect_status: connection broken\n",
667 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
668 "out\n", task
->tk_pid
);
671 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
672 "server %s\n", task
->tk_pid
, -task
->tk_status
,
673 task
->tk_client
->cl_server
);
674 xprt_release_write(xprt
, task
);
675 task
->tk_status
= -EIO
;
680 * xprt_lookup_rqst - find an RPC request corresponding to an XID
681 * @xprt: transport on which the original request was transmitted
682 * @xid: RPC XID of incoming reply
685 struct rpc_rqst
*xprt_lookup_rqst(struct rpc_xprt
*xprt
, __be32 xid
)
687 struct list_head
*pos
;
689 list_for_each(pos
, &xprt
->recv
) {
690 struct rpc_rqst
*entry
= list_entry(pos
, struct rpc_rqst
, rq_list
);
691 if (entry
->rq_xid
== xid
)
695 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
697 xprt
->stat
.bad_xids
++;
700 EXPORT_SYMBOL_GPL(xprt_lookup_rqst
);
703 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
704 * @task: RPC request that recently completed
707 void xprt_update_rtt(struct rpc_task
*task
)
709 struct rpc_rqst
*req
= task
->tk_rqstp
;
710 struct rpc_rtt
*rtt
= task
->tk_client
->cl_rtt
;
711 unsigned timer
= task
->tk_msg
.rpc_proc
->p_timer
;
714 if (req
->rq_ntrans
== 1)
715 rpc_update_rtt(rtt
, timer
,
716 (long)jiffies
- req
->rq_xtime
);
717 rpc_set_timeo(rtt
, timer
, req
->rq_ntrans
- 1);
720 EXPORT_SYMBOL_GPL(xprt_update_rtt
);
723 * xprt_complete_rqst - called when reply processing is complete
724 * @task: RPC request that recently completed
725 * @copied: actual number of bytes received from the transport
727 * Caller holds transport lock.
729 void xprt_complete_rqst(struct rpc_task
*task
, int copied
)
731 struct rpc_rqst
*req
= task
->tk_rqstp
;
733 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
734 task
->tk_pid
, ntohl(req
->rq_xid
), copied
);
736 task
->tk_xprt
->stat
.recvs
++;
737 task
->tk_rtt
= (long)jiffies
- req
->rq_xtime
;
739 list_del_init(&req
->rq_list
);
740 /* Ensure all writes are done before we update req->rq_received */
742 req
->rq_received
= req
->rq_private_buf
.len
= copied
;
743 rpc_wake_up_task(task
);
745 EXPORT_SYMBOL_GPL(xprt_complete_rqst
);
747 static void xprt_timer(struct rpc_task
*task
)
749 struct rpc_rqst
*req
= task
->tk_rqstp
;
750 struct rpc_xprt
*xprt
= req
->rq_xprt
;
752 dprintk("RPC: %5u xprt_timer\n", task
->tk_pid
);
754 spin_lock(&xprt
->transport_lock
);
755 if (!req
->rq_received
) {
756 if (xprt
->ops
->timer
)
757 xprt
->ops
->timer(task
);
758 task
->tk_status
= -ETIMEDOUT
;
760 task
->tk_timeout
= 0;
761 rpc_wake_up_task(task
);
762 spin_unlock(&xprt
->transport_lock
);
766 * xprt_prepare_transmit - reserve the transport before sending a request
767 * @task: RPC task about to send a request
770 int xprt_prepare_transmit(struct rpc_task
*task
)
772 struct rpc_rqst
*req
= task
->tk_rqstp
;
773 struct rpc_xprt
*xprt
= req
->rq_xprt
;
776 dprintk("RPC: %5u xprt_prepare_transmit\n", task
->tk_pid
);
778 spin_lock_bh(&xprt
->transport_lock
);
779 if (req
->rq_received
&& !req
->rq_bytes_sent
) {
780 err
= req
->rq_received
;
783 if (!xprt
->ops
->reserve_xprt(task
)) {
788 if (!xprt_connected(xprt
)) {
793 spin_unlock_bh(&xprt
->transport_lock
);
797 void xprt_end_transmit(struct rpc_task
*task
)
799 xprt_release_write(task
->tk_xprt
, task
);
803 * xprt_transmit - send an RPC request on a transport
804 * @task: controlling RPC task
806 * We have to copy the iovec because sendmsg fiddles with its contents.
808 void xprt_transmit(struct rpc_task
*task
)
810 struct rpc_rqst
*req
= task
->tk_rqstp
;
811 struct rpc_xprt
*xprt
= req
->rq_xprt
;
814 dprintk("RPC: %5u xprt_transmit(%u)\n", task
->tk_pid
, req
->rq_slen
);
816 if (!req
->rq_received
) {
817 if (list_empty(&req
->rq_list
)) {
818 spin_lock_bh(&xprt
->transport_lock
);
819 /* Update the softirq receive buffer */
820 memcpy(&req
->rq_private_buf
, &req
->rq_rcv_buf
,
821 sizeof(req
->rq_private_buf
));
822 /* Add request to the receive list */
823 list_add_tail(&req
->rq_list
, &xprt
->recv
);
824 spin_unlock_bh(&xprt
->transport_lock
);
825 xprt_reset_majortimeo(req
);
826 /* Turn off autodisconnect */
827 del_singleshot_timer_sync(&xprt
->timer
);
829 } else if (!req
->rq_bytes_sent
)
832 status
= xprt
->ops
->send_request(task
);
834 dprintk("RPC: %5u xmit complete\n", task
->tk_pid
);
835 spin_lock_bh(&xprt
->transport_lock
);
837 xprt
->ops
->set_retrans_timeout(task
);
840 xprt
->stat
.req_u
+= xprt
->stat
.sends
- xprt
->stat
.recvs
;
841 xprt
->stat
.bklog_u
+= xprt
->backlog
.qlen
;
843 /* Don't race with disconnect */
844 if (!xprt_connected(xprt
))
845 task
->tk_status
= -ENOTCONN
;
846 else if (!req
->rq_received
)
847 rpc_sleep_on(&xprt
->pending
, task
, NULL
, xprt_timer
);
848 spin_unlock_bh(&xprt
->transport_lock
);
852 /* Note: at this point, task->tk_sleeping has not yet been set,
853 * hence there is no danger of the waking up task being put on
854 * schedq, and being picked up by a parallel run of rpciod().
856 task
->tk_status
= status
;
857 if (status
== -ECONNREFUSED
)
858 rpc_sleep_on(&xprt
->sending
, task
, NULL
, NULL
);
861 static inline void do_xprt_reserve(struct rpc_task
*task
)
863 struct rpc_xprt
*xprt
= task
->tk_xprt
;
868 if (!list_empty(&xprt
->free
)) {
869 struct rpc_rqst
*req
= list_entry(xprt
->free
.next
, struct rpc_rqst
, rq_list
);
870 list_del_init(&req
->rq_list
);
871 task
->tk_rqstp
= req
;
872 xprt_request_init(task
, xprt
);
875 dprintk("RPC: waiting for request slot\n");
876 task
->tk_status
= -EAGAIN
;
877 task
->tk_timeout
= 0;
878 rpc_sleep_on(&xprt
->backlog
, task
, NULL
, NULL
);
882 * xprt_reserve - allocate an RPC request slot
883 * @task: RPC task requesting a slot allocation
885 * If no more slots are available, place the task on the transport's
888 void xprt_reserve(struct rpc_task
*task
)
890 struct rpc_xprt
*xprt
= task
->tk_xprt
;
892 task
->tk_status
= -EIO
;
893 spin_lock(&xprt
->reserve_lock
);
894 do_xprt_reserve(task
);
895 spin_unlock(&xprt
->reserve_lock
);
898 static inline __be32
xprt_alloc_xid(struct rpc_xprt
*xprt
)
903 static inline void xprt_init_xid(struct rpc_xprt
*xprt
)
905 xprt
->xid
= net_random();
908 static void xprt_request_init(struct rpc_task
*task
, struct rpc_xprt
*xprt
)
910 struct rpc_rqst
*req
= task
->tk_rqstp
;
912 req
->rq_timeout
= xprt
->timeout
.to_initval
;
915 req
->rq_buffer
= NULL
;
916 req
->rq_xid
= xprt_alloc_xid(xprt
);
917 req
->rq_release_snd_buf
= NULL
;
918 xprt_reset_majortimeo(req
);
919 dprintk("RPC: %5u reserved req %p xid %08x\n", task
->tk_pid
,
920 req
, ntohl(req
->rq_xid
));
924 * xprt_release - release an RPC request slot
925 * @task: task which is finished with the slot
928 void xprt_release(struct rpc_task
*task
)
930 struct rpc_xprt
*xprt
= task
->tk_xprt
;
931 struct rpc_rqst
*req
;
933 if (!(req
= task
->tk_rqstp
))
935 rpc_count_iostats(task
);
936 spin_lock_bh(&xprt
->transport_lock
);
937 xprt
->ops
->release_xprt(xprt
, task
);
938 if (xprt
->ops
->release_request
)
939 xprt
->ops
->release_request(task
);
940 if (!list_empty(&req
->rq_list
))
941 list_del(&req
->rq_list
);
942 xprt
->last_used
= jiffies
;
943 if (list_empty(&xprt
->recv
))
944 mod_timer(&xprt
->timer
,
945 xprt
->last_used
+ xprt
->idle_timeout
);
946 spin_unlock_bh(&xprt
->transport_lock
);
947 xprt
->ops
->buf_free(req
->rq_buffer
);
948 task
->tk_rqstp
= NULL
;
949 if (req
->rq_release_snd_buf
)
950 req
->rq_release_snd_buf(req
);
951 memset(req
, 0, sizeof(*req
)); /* mark unused */
953 dprintk("RPC: %5u release request %p\n", task
->tk_pid
, req
);
955 spin_lock(&xprt
->reserve_lock
);
956 list_add(&req
->rq_list
, &xprt
->free
);
957 rpc_wake_up_next(&xprt
->backlog
);
958 spin_unlock(&xprt
->reserve_lock
);
962 * xprt_set_timeout - set constant RPC timeout
963 * @to: RPC timeout parameters to set up
964 * @retr: number of retries
965 * @incr: amount of increase after each retry
968 void xprt_set_timeout(struct rpc_timeout
*to
, unsigned int retr
, unsigned long incr
)
971 to
->to_increment
= incr
;
972 to
->to_maxval
= to
->to_initval
+ (incr
* retr
);
973 to
->to_retries
= retr
;
974 to
->to_exponential
= 0;
978 * xprt_create_transport - create an RPC transport
979 * @args: rpc transport creation arguments
982 struct rpc_xprt
*xprt_create_transport(struct xprt_create
*args
)
984 struct rpc_xprt
*xprt
;
985 struct rpc_rqst
*req
;
986 struct xprt_class
*t
;
988 spin_lock(&xprt_list_lock
);
989 list_for_each_entry(t
, &xprt_list
, list
) {
990 if (t
->ident
== args
->ident
) {
991 spin_unlock(&xprt_list_lock
);
995 spin_unlock(&xprt_list_lock
);
996 printk(KERN_ERR
"RPC: transport (%d) not supported\n", args
->ident
);
997 return ERR_PTR(-EIO
);
1000 xprt
= t
->setup(args
);
1002 dprintk("RPC: xprt_create_transport: failed, %ld\n",
1007 kref_init(&xprt
->kref
);
1008 spin_lock_init(&xprt
->transport_lock
);
1009 spin_lock_init(&xprt
->reserve_lock
);
1011 INIT_LIST_HEAD(&xprt
->free
);
1012 INIT_LIST_HEAD(&xprt
->recv
);
1013 INIT_WORK(&xprt
->task_cleanup
, xprt_autoclose
);
1014 init_timer(&xprt
->timer
);
1015 xprt
->timer
.function
= xprt_init_autodisconnect
;
1016 xprt
->timer
.data
= (unsigned long) xprt
;
1017 xprt
->last_used
= jiffies
;
1018 xprt
->cwnd
= RPC_INITCWND
;
1019 xprt
->bind_index
= 0;
1021 rpc_init_wait_queue(&xprt
->binding
, "xprt_binding");
1022 rpc_init_wait_queue(&xprt
->pending
, "xprt_pending");
1023 rpc_init_wait_queue(&xprt
->sending
, "xprt_sending");
1024 rpc_init_wait_queue(&xprt
->resend
, "xprt_resend");
1025 rpc_init_priority_wait_queue(&xprt
->backlog
, "xprt_backlog");
1027 /* initialize free list */
1028 for (req
= &xprt
->slot
[xprt
->max_reqs
-1]; req
>= &xprt
->slot
[0]; req
--)
1029 list_add(&req
->rq_list
, &xprt
->free
);
1031 xprt_init_xid(xprt
);
1033 dprintk("RPC: created transport %p with %u slots\n", xprt
,
1040 * xprt_destroy - destroy an RPC transport, killing off all requests.
1041 * @kref: kref for the transport to destroy
1044 static void xprt_destroy(struct kref
*kref
)
1046 struct rpc_xprt
*xprt
= container_of(kref
, struct rpc_xprt
, kref
);
1048 dprintk("RPC: destroying transport %p\n", xprt
);
1050 del_timer_sync(&xprt
->timer
);
1053 * Tear down transport state and free the rpc_xprt
1055 xprt
->ops
->destroy(xprt
);
1059 * xprt_put - release a reference to an RPC transport.
1060 * @xprt: pointer to the transport
1063 void xprt_put(struct rpc_xprt
*xprt
)
1065 kref_put(&xprt
->kref
, xprt_destroy
);
1069 * xprt_get - return a reference to an RPC transport.
1070 * @xprt: pointer to the transport
1073 struct rpc_xprt
*xprt_get(struct rpc_xprt
*xprt
)
1075 kref_get(&xprt
->kref
);