1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/xprt.c
5 * This is a generic RPC call interface supporting congestion avoidance,
6 * and asynchronous calls.
8 * The interface works like this:
10 * - When a process places a call, it allocates a request slot if
11 * one is available. Otherwise, it sleeps on the backlog queue
13 * - Next, the caller puts together the RPC message, stuffs it into
14 * the request struct, and calls xprt_transmit().
15 * - xprt_transmit sends the message and installs the caller on the
16 * transport's wait list. At the same time, if a reply is expected,
17 * it installs a timer that is run after the packet's timeout has
19 * - When a packet arrives, the data_ready handler walks the list of
20 * pending requests for that transport. If a matching XID is found, the
21 * caller is woken up, and the timer removed.
22 * - When no reply arrives within the timeout interval, the timer is
23 * fired by the kernel and runs xprt_timer(). It either adjusts the
24 * timeout values (minor timeout) or wakes up the caller with a status
26 * - When the caller receives a notification from RPC that a reply arrived,
27 * it should release the RPC slot, and process the reply.
28 * If the call timed out, it may choose to retry the operation by
29 * adjusting the initial timeout value, and simply calling rpc_call
32 * Support for async RPC is done through a set of RPC-specific scheduling
33 * primitives that `transparently' work for processes as well as async
34 * tasks that rely on callbacks.
36 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
38 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
41 #include <linux/module.h>
43 #include <linux/types.h>
44 #include <linux/interrupt.h>
45 #include <linux/workqueue.h>
46 #include <linux/net.h>
47 #include <linux/ktime.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/metrics.h>
51 #include <linux/sunrpc/bc_xprt.h>
52 #include <linux/rcupdate.h>
53 #include <linux/sched/mm.h>
55 #include <trace/events/sunrpc.h>
65 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
66 # define RPCDBG_FACILITY RPCDBG_XPRT
72 static void xprt_init(struct rpc_xprt
*xprt
, struct net
*net
);
73 static __be32
xprt_alloc_xid(struct rpc_xprt
*xprt
);
74 static void xprt_destroy(struct rpc_xprt
*xprt
);
75 static void xprt_request_init(struct rpc_task
*task
);
77 static DEFINE_SPINLOCK(xprt_list_lock
);
78 static LIST_HEAD(xprt_list
);
80 static unsigned long xprt_request_timeout(const struct rpc_rqst
*req
)
82 unsigned long timeout
= jiffies
+ req
->rq_timeout
;
84 if (time_before(timeout
, req
->rq_majortimeo
))
86 return req
->rq_majortimeo
;
90 * xprt_register_transport - register a transport implementation
91 * @transport: transport to register
93 * If a transport implementation is loaded as a kernel module, it can
94 * call this interface to make itself known to the RPC client.
97 * 0: transport successfully registered
98 * -EEXIST: transport already registered
99 * -EINVAL: transport module being unloaded
101 int xprt_register_transport(struct xprt_class
*transport
)
103 struct xprt_class
*t
;
107 spin_lock(&xprt_list_lock
);
108 list_for_each_entry(t
, &xprt_list
, list
) {
109 /* don't register the same transport class twice */
110 if (t
->ident
== transport
->ident
)
114 list_add_tail(&transport
->list
, &xprt_list
);
115 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
);
152 spin_unlock(&xprt_list_lock
);
155 EXPORT_SYMBOL_GPL(xprt_unregister_transport
);
158 xprt_class_release(const struct xprt_class
*t
)
160 module_put(t
->owner
);
163 static const struct xprt_class
*
164 xprt_class_find_by_ident_locked(int ident
)
166 const struct xprt_class
*t
;
168 list_for_each_entry(t
, &xprt_list
, list
) {
169 if (t
->ident
!= ident
)
171 if (!try_module_get(t
->owner
))
178 static const struct xprt_class
*
179 xprt_class_find_by_ident(int ident
)
181 const struct xprt_class
*t
;
183 spin_lock(&xprt_list_lock
);
184 t
= xprt_class_find_by_ident_locked(ident
);
185 spin_unlock(&xprt_list_lock
);
189 static const struct xprt_class
*
190 xprt_class_find_by_netid_locked(const char *netid
)
192 const struct xprt_class
*t
;
195 list_for_each_entry(t
, &xprt_list
, list
) {
196 for (i
= 0; t
->netid
[i
][0] != '\0'; i
++) {
197 if (strcmp(t
->netid
[i
], netid
) != 0)
199 if (!try_module_get(t
->owner
))
207 static const struct xprt_class
*
208 xprt_class_find_by_netid(const char *netid
)
210 const struct xprt_class
*t
;
212 spin_lock(&xprt_list_lock
);
213 t
= xprt_class_find_by_netid_locked(netid
);
215 spin_unlock(&xprt_list_lock
);
216 request_module("rpc%s", netid
);
217 spin_lock(&xprt_list_lock
);
218 t
= xprt_class_find_by_netid_locked(netid
);
220 spin_unlock(&xprt_list_lock
);
225 * xprt_find_transport_ident - convert a netid into a transport identifier
226 * @netid: transport to load
229 * > 0: transport identifier
230 * -ENOENT: transport module not available
232 int xprt_find_transport_ident(const char *netid
)
234 const struct xprt_class
*t
;
237 t
= xprt_class_find_by_netid(netid
);
241 xprt_class_release(t
);
244 EXPORT_SYMBOL_GPL(xprt_find_transport_ident
);
246 static void xprt_clear_locked(struct rpc_xprt
*xprt
)
248 xprt
->snd_task
= NULL
;
249 if (!test_bit(XPRT_CLOSE_WAIT
, &xprt
->state
)) {
250 smp_mb__before_atomic();
251 clear_bit(XPRT_LOCKED
, &xprt
->state
);
252 smp_mb__after_atomic();
254 queue_work(xprtiod_workqueue
, &xprt
->task_cleanup
);
258 * xprt_reserve_xprt - serialize write access to transports
259 * @task: task that is requesting access to the transport
260 * @xprt: pointer to the target transport
262 * This prevents mixing the payload of separate requests, and prevents
263 * transport connects from colliding with writes. No congestion control
266 int xprt_reserve_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
268 struct rpc_rqst
*req
= task
->tk_rqstp
;
270 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
)) {
271 if (task
== xprt
->snd_task
)
275 if (test_bit(XPRT_WRITE_SPACE
, &xprt
->state
))
277 xprt
->snd_task
= task
;
280 trace_xprt_reserve_xprt(xprt
, task
);
284 xprt_clear_locked(xprt
);
286 task
->tk_status
= -EAGAIN
;
287 if (RPC_IS_SOFT(task
))
288 rpc_sleep_on_timeout(&xprt
->sending
, task
, NULL
,
289 xprt_request_timeout(req
));
291 rpc_sleep_on(&xprt
->sending
, task
, NULL
);
294 EXPORT_SYMBOL_GPL(xprt_reserve_xprt
);
297 xprt_need_congestion_window_wait(struct rpc_xprt
*xprt
)
299 return test_bit(XPRT_CWND_WAIT
, &xprt
->state
);
303 xprt_set_congestion_window_wait(struct rpc_xprt
*xprt
)
305 if (!list_empty(&xprt
->xmit_queue
)) {
306 /* Peek at head of queue to see if it can make progress */
307 if (list_first_entry(&xprt
->xmit_queue
, struct rpc_rqst
,
311 set_bit(XPRT_CWND_WAIT
, &xprt
->state
);
315 xprt_test_and_clear_congestion_window_wait(struct rpc_xprt
*xprt
)
317 if (!RPCXPRT_CONGESTED(xprt
))
318 clear_bit(XPRT_CWND_WAIT
, &xprt
->state
);
322 * xprt_reserve_xprt_cong - serialize write access to transports
323 * @task: task that is requesting access to the transport
325 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
326 * integrated into the decision of whether a request is allowed to be
327 * woken up and given access to the transport.
328 * Note that the lock is only granted if we know there are free slots.
330 int xprt_reserve_xprt_cong(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
332 struct rpc_rqst
*req
= task
->tk_rqstp
;
334 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
)) {
335 if (task
== xprt
->snd_task
)
340 xprt
->snd_task
= task
;
343 if (test_bit(XPRT_WRITE_SPACE
, &xprt
->state
))
345 if (!xprt_need_congestion_window_wait(xprt
)) {
346 xprt
->snd_task
= task
;
350 xprt_clear_locked(xprt
);
352 task
->tk_status
= -EAGAIN
;
353 if (RPC_IS_SOFT(task
))
354 rpc_sleep_on_timeout(&xprt
->sending
, task
, NULL
,
355 xprt_request_timeout(req
));
357 rpc_sleep_on(&xprt
->sending
, task
, NULL
);
360 trace_xprt_reserve_cong(xprt
, task
);
363 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong
);
365 static inline int xprt_lock_write(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
369 if (test_bit(XPRT_LOCKED
, &xprt
->state
) && xprt
->snd_task
== task
)
371 spin_lock(&xprt
->transport_lock
);
372 retval
= xprt
->ops
->reserve_xprt(xprt
, task
);
373 spin_unlock(&xprt
->transport_lock
);
377 static bool __xprt_lock_write_func(struct rpc_task
*task
, void *data
)
379 struct rpc_xprt
*xprt
= data
;
381 xprt
->snd_task
= task
;
385 static void __xprt_lock_write_next(struct rpc_xprt
*xprt
)
387 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
))
389 if (test_bit(XPRT_WRITE_SPACE
, &xprt
->state
))
391 if (rpc_wake_up_first_on_wq(xprtiod_workqueue
, &xprt
->sending
,
392 __xprt_lock_write_func
, xprt
))
395 xprt_clear_locked(xprt
);
398 static void __xprt_lock_write_next_cong(struct rpc_xprt
*xprt
)
400 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
))
402 if (test_bit(XPRT_WRITE_SPACE
, &xprt
->state
))
404 if (xprt_need_congestion_window_wait(xprt
))
406 if (rpc_wake_up_first_on_wq(xprtiod_workqueue
, &xprt
->sending
,
407 __xprt_lock_write_func
, xprt
))
410 xprt_clear_locked(xprt
);
414 * xprt_release_xprt - allow other requests to use a transport
415 * @xprt: transport with other tasks potentially waiting
416 * @task: task that is releasing access to the transport
418 * Note that "task" can be NULL. No congestion control is provided.
420 void xprt_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
422 if (xprt
->snd_task
== task
) {
423 xprt_clear_locked(xprt
);
424 __xprt_lock_write_next(xprt
);
426 trace_xprt_release_xprt(xprt
, task
);
428 EXPORT_SYMBOL_GPL(xprt_release_xprt
);
431 * xprt_release_xprt_cong - allow other requests to use a transport
432 * @xprt: transport with other tasks potentially waiting
433 * @task: task that is releasing access to the transport
435 * Note that "task" can be NULL. Another task is awoken to use the
436 * transport if the transport's congestion window allows it.
438 void xprt_release_xprt_cong(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
440 if (xprt
->snd_task
== task
) {
441 xprt_clear_locked(xprt
);
442 __xprt_lock_write_next_cong(xprt
);
444 trace_xprt_release_cong(xprt
, task
);
446 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong
);
448 void xprt_release_write(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
450 if (xprt
->snd_task
!= task
)
452 spin_lock(&xprt
->transport_lock
);
453 xprt
->ops
->release_xprt(xprt
, task
);
454 spin_unlock(&xprt
->transport_lock
);
458 * Van Jacobson congestion avoidance. Check if the congestion window
459 * overflowed. Put the task to sleep if this is the case.
462 __xprt_get_cong(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
466 trace_xprt_get_cong(xprt
, req
->rq_task
);
467 if (RPCXPRT_CONGESTED(xprt
)) {
468 xprt_set_congestion_window_wait(xprt
);
472 xprt
->cong
+= RPC_CWNDSCALE
;
477 * Adjust the congestion window, and wake up the next task
478 * that has been sleeping due to congestion
481 __xprt_put_cong(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
486 xprt
->cong
-= RPC_CWNDSCALE
;
487 xprt_test_and_clear_congestion_window_wait(xprt
);
488 trace_xprt_put_cong(xprt
, req
->rq_task
);
489 __xprt_lock_write_next_cong(xprt
);
493 * xprt_request_get_cong - Request congestion control credits
494 * @xprt: pointer to transport
495 * @req: pointer to RPC request
497 * Useful for transports that require congestion control.
500 xprt_request_get_cong(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
506 spin_lock(&xprt
->transport_lock
);
507 ret
= __xprt_get_cong(xprt
, req
) != 0;
508 spin_unlock(&xprt
->transport_lock
);
511 EXPORT_SYMBOL_GPL(xprt_request_get_cong
);
514 * xprt_release_rqst_cong - housekeeping when request is complete
515 * @task: RPC request that recently completed
517 * Useful for transports that require congestion control.
519 void xprt_release_rqst_cong(struct rpc_task
*task
)
521 struct rpc_rqst
*req
= task
->tk_rqstp
;
523 __xprt_put_cong(req
->rq_xprt
, req
);
525 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong
);
527 static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt
*xprt
)
529 if (test_and_clear_bit(XPRT_CWND_WAIT
, &xprt
->state
))
530 __xprt_lock_write_next_cong(xprt
);
534 * Clear the congestion window wait flag and wake up the next
535 * entry on xprt->sending
538 xprt_clear_congestion_window_wait(struct rpc_xprt
*xprt
)
540 if (test_and_clear_bit(XPRT_CWND_WAIT
, &xprt
->state
)) {
541 spin_lock(&xprt
->transport_lock
);
542 __xprt_lock_write_next_cong(xprt
);
543 spin_unlock(&xprt
->transport_lock
);
548 * xprt_adjust_cwnd - adjust transport congestion window
549 * @xprt: pointer to xprt
550 * @task: recently completed RPC request used to adjust window
551 * @result: result code of completed RPC request
553 * The transport code maintains an estimate on the maximum number of out-
554 * standing RPC requests, using a smoothed version of the congestion
555 * avoidance implemented in 44BSD. This is basically the Van Jacobson
556 * congestion algorithm: If a retransmit occurs, the congestion window is
557 * halved; otherwise, it is incremented by 1/cwnd when
559 * - a reply is received and
560 * - a full number of requests are outstanding and
561 * - the congestion window hasn't been updated recently.
563 void xprt_adjust_cwnd(struct rpc_xprt
*xprt
, struct rpc_task
*task
, int result
)
565 struct rpc_rqst
*req
= task
->tk_rqstp
;
566 unsigned long cwnd
= xprt
->cwnd
;
568 if (result
>= 0 && cwnd
<= xprt
->cong
) {
569 /* The (cwnd >> 1) term makes sure
570 * the result gets rounded properly. */
571 cwnd
+= (RPC_CWNDSCALE
* RPC_CWNDSCALE
+ (cwnd
>> 1)) / cwnd
;
572 if (cwnd
> RPC_MAXCWND(xprt
))
573 cwnd
= RPC_MAXCWND(xprt
);
574 __xprt_lock_write_next_cong(xprt
);
575 } else if (result
== -ETIMEDOUT
) {
577 if (cwnd
< RPC_CWNDSCALE
)
578 cwnd
= RPC_CWNDSCALE
;
580 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
581 xprt
->cong
, xprt
->cwnd
, cwnd
);
583 __xprt_put_cong(xprt
, req
);
585 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd
);
588 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
589 * @xprt: transport with waiting tasks
590 * @status: result code to plant in each task before waking it
593 void xprt_wake_pending_tasks(struct rpc_xprt
*xprt
, int status
)
596 rpc_wake_up_status(&xprt
->pending
, status
);
598 rpc_wake_up(&xprt
->pending
);
600 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks
);
603 * xprt_wait_for_buffer_space - wait for transport output buffer to clear
606 * Note that we only set the timer for the case of RPC_IS_SOFT(), since
607 * we don't in general want to force a socket disconnection due to
608 * an incomplete RPC call transmission.
610 void xprt_wait_for_buffer_space(struct rpc_xprt
*xprt
)
612 set_bit(XPRT_WRITE_SPACE
, &xprt
->state
);
614 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space
);
617 xprt_clear_write_space_locked(struct rpc_xprt
*xprt
)
619 if (test_and_clear_bit(XPRT_WRITE_SPACE
, &xprt
->state
)) {
620 __xprt_lock_write_next(xprt
);
621 dprintk("RPC: write space: waking waiting task on "
629 * xprt_write_space - wake the task waiting for transport output buffer space
630 * @xprt: transport with waiting tasks
632 * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
634 bool xprt_write_space(struct rpc_xprt
*xprt
)
638 if (!test_bit(XPRT_WRITE_SPACE
, &xprt
->state
))
640 spin_lock(&xprt
->transport_lock
);
641 ret
= xprt_clear_write_space_locked(xprt
);
642 spin_unlock(&xprt
->transport_lock
);
645 EXPORT_SYMBOL_GPL(xprt_write_space
);
647 static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime
)
649 s64 delta
= ktime_to_ns(ktime_get() - abstime
);
650 return likely(delta
>= 0) ?
651 jiffies
- nsecs_to_jiffies(delta
) :
652 jiffies
+ nsecs_to_jiffies(-delta
);
655 static unsigned long xprt_calc_majortimeo(struct rpc_rqst
*req
)
657 const struct rpc_timeout
*to
= req
->rq_task
->tk_client
->cl_timeout
;
658 unsigned long majortimeo
= req
->rq_timeout
;
660 if (to
->to_exponential
)
661 majortimeo
<<= to
->to_retries
;
663 majortimeo
+= to
->to_increment
* to
->to_retries
;
664 if (majortimeo
> to
->to_maxval
|| majortimeo
== 0)
665 majortimeo
= to
->to_maxval
;
669 static void xprt_reset_majortimeo(struct rpc_rqst
*req
)
671 req
->rq_majortimeo
+= xprt_calc_majortimeo(req
);
674 static void xprt_reset_minortimeo(struct rpc_rqst
*req
)
676 req
->rq_minortimeo
+= req
->rq_timeout
;
679 static void xprt_init_majortimeo(struct rpc_task
*task
, struct rpc_rqst
*req
)
681 unsigned long time_init
;
682 struct rpc_xprt
*xprt
= req
->rq_xprt
;
684 if (likely(xprt
&& xprt_connected(xprt
)))
687 time_init
= xprt_abs_ktime_to_jiffies(task
->tk_start
);
688 req
->rq_timeout
= task
->tk_client
->cl_timeout
->to_initval
;
689 req
->rq_majortimeo
= time_init
+ xprt_calc_majortimeo(req
);
690 req
->rq_minortimeo
= time_init
+ req
->rq_timeout
;
694 * xprt_adjust_timeout - adjust timeout values for next retransmit
695 * @req: RPC request containing parameters to use for the adjustment
698 int xprt_adjust_timeout(struct rpc_rqst
*req
)
700 struct rpc_xprt
*xprt
= req
->rq_xprt
;
701 const struct rpc_timeout
*to
= req
->rq_task
->tk_client
->cl_timeout
;
704 if (time_before(jiffies
, req
->rq_majortimeo
)) {
705 if (time_before(jiffies
, req
->rq_minortimeo
))
707 if (to
->to_exponential
)
708 req
->rq_timeout
<<= 1;
710 req
->rq_timeout
+= to
->to_increment
;
711 if (to
->to_maxval
&& req
->rq_timeout
>= to
->to_maxval
)
712 req
->rq_timeout
= to
->to_maxval
;
715 req
->rq_timeout
= to
->to_initval
;
717 xprt_reset_majortimeo(req
);
718 /* Reset the RTT counters == "slow start" */
719 spin_lock(&xprt
->transport_lock
);
720 rpc_init_rtt(req
->rq_task
->tk_client
->cl_rtt
, to
->to_initval
);
721 spin_unlock(&xprt
->transport_lock
);
724 xprt_reset_minortimeo(req
);
726 if (req
->rq_timeout
== 0) {
727 printk(KERN_WARNING
"xprt_adjust_timeout: rq_timeout = 0!\n");
728 req
->rq_timeout
= 5 * HZ
;
733 static void xprt_autoclose(struct work_struct
*work
)
735 struct rpc_xprt
*xprt
=
736 container_of(work
, struct rpc_xprt
, task_cleanup
);
737 unsigned int pflags
= memalloc_nofs_save();
739 trace_xprt_disconnect_auto(xprt
);
740 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
741 xprt
->ops
->close(xprt
);
742 xprt_release_write(xprt
, NULL
);
743 wake_up_bit(&xprt
->state
, XPRT_LOCKED
);
744 memalloc_nofs_restore(pflags
);
748 * xprt_disconnect_done - mark a transport as disconnected
749 * @xprt: transport to flag for disconnect
752 void xprt_disconnect_done(struct rpc_xprt
*xprt
)
754 trace_xprt_disconnect_done(xprt
);
755 spin_lock(&xprt
->transport_lock
);
756 xprt_clear_connected(xprt
);
757 xprt_clear_write_space_locked(xprt
);
758 xprt_clear_congestion_window_wait_locked(xprt
);
759 xprt_wake_pending_tasks(xprt
, -ENOTCONN
);
760 spin_unlock(&xprt
->transport_lock
);
762 EXPORT_SYMBOL_GPL(xprt_disconnect_done
);
765 * xprt_schedule_autoclose_locked - Try to schedule an autoclose RPC call
766 * @xprt: transport to disconnect
768 static void xprt_schedule_autoclose_locked(struct rpc_xprt
*xprt
)
770 set_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
771 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
) == 0)
772 queue_work(xprtiod_workqueue
, &xprt
->task_cleanup
);
773 else if (xprt
->snd_task
&& !test_bit(XPRT_SND_IS_COOKIE
, &xprt
->state
))
774 rpc_wake_up_queued_task_set_status(&xprt
->pending
,
775 xprt
->snd_task
, -ENOTCONN
);
779 * xprt_force_disconnect - force a transport to disconnect
780 * @xprt: transport to disconnect
783 void xprt_force_disconnect(struct rpc_xprt
*xprt
)
785 trace_xprt_disconnect_force(xprt
);
787 /* Don't race with the test_bit() in xprt_clear_locked() */
788 spin_lock(&xprt
->transport_lock
);
789 xprt_schedule_autoclose_locked(xprt
);
790 spin_unlock(&xprt
->transport_lock
);
792 EXPORT_SYMBOL_GPL(xprt_force_disconnect
);
795 xprt_connect_cookie(struct rpc_xprt
*xprt
)
797 return READ_ONCE(xprt
->connect_cookie
);
801 xprt_request_retransmit_after_disconnect(struct rpc_task
*task
)
803 struct rpc_rqst
*req
= task
->tk_rqstp
;
804 struct rpc_xprt
*xprt
= req
->rq_xprt
;
806 return req
->rq_connect_cookie
!= xprt_connect_cookie(xprt
) ||
807 !xprt_connected(xprt
);
811 * xprt_conditional_disconnect - force a transport to disconnect
812 * @xprt: transport to disconnect
813 * @cookie: 'connection cookie'
815 * This attempts to break the connection if and only if 'cookie' matches
816 * the current transport 'connection cookie'. It ensures that we don't
817 * try to break the connection more than once when we need to retransmit
818 * a batch of RPC requests.
821 void xprt_conditional_disconnect(struct rpc_xprt
*xprt
, unsigned int cookie
)
823 /* Don't race with the test_bit() in xprt_clear_locked() */
824 spin_lock(&xprt
->transport_lock
);
825 if (cookie
!= xprt
->connect_cookie
)
827 if (test_bit(XPRT_CLOSING
, &xprt
->state
))
829 xprt_schedule_autoclose_locked(xprt
);
831 spin_unlock(&xprt
->transport_lock
);
835 xprt_has_timer(const struct rpc_xprt
*xprt
)
837 return xprt
->idle_timeout
!= 0;
841 xprt_schedule_autodisconnect(struct rpc_xprt
*xprt
)
842 __must_hold(&xprt
->transport_lock
)
844 xprt
->last_used
= jiffies
;
845 if (RB_EMPTY_ROOT(&xprt
->recv_queue
) && xprt_has_timer(xprt
))
846 mod_timer(&xprt
->timer
, xprt
->last_used
+ xprt
->idle_timeout
);
850 xprt_init_autodisconnect(struct timer_list
*t
)
852 struct rpc_xprt
*xprt
= from_timer(xprt
, t
, timer
);
854 if (!RB_EMPTY_ROOT(&xprt
->recv_queue
))
856 /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
857 xprt
->last_used
= jiffies
;
858 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
))
860 queue_work(xprtiod_workqueue
, &xprt
->task_cleanup
);
863 #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
864 static void xprt_inject_disconnect(struct rpc_xprt
*xprt
)
866 if (!fail_sunrpc
.ignore_client_disconnect
&&
867 should_fail(&fail_sunrpc
.attr
, 1))
868 xprt
->ops
->inject_disconnect(xprt
);
871 static inline void xprt_inject_disconnect(struct rpc_xprt
*xprt
)
876 bool xprt_lock_connect(struct rpc_xprt
*xprt
,
877 struct rpc_task
*task
,
882 spin_lock(&xprt
->transport_lock
);
883 if (!test_bit(XPRT_LOCKED
, &xprt
->state
))
885 if (xprt
->snd_task
!= task
)
887 set_bit(XPRT_SND_IS_COOKIE
, &xprt
->state
);
888 xprt
->snd_task
= cookie
;
891 spin_unlock(&xprt
->transport_lock
);
894 EXPORT_SYMBOL_GPL(xprt_lock_connect
);
896 void xprt_unlock_connect(struct rpc_xprt
*xprt
, void *cookie
)
898 spin_lock(&xprt
->transport_lock
);
899 if (xprt
->snd_task
!= cookie
)
901 if (!test_bit(XPRT_LOCKED
, &xprt
->state
))
903 xprt
->snd_task
=NULL
;
904 clear_bit(XPRT_SND_IS_COOKIE
, &xprt
->state
);
905 xprt
->ops
->release_xprt(xprt
, NULL
);
906 xprt_schedule_autodisconnect(xprt
);
908 spin_unlock(&xprt
->transport_lock
);
909 wake_up_bit(&xprt
->state
, XPRT_LOCKED
);
911 EXPORT_SYMBOL_GPL(xprt_unlock_connect
);
914 * xprt_connect - schedule a transport connect operation
915 * @task: RPC task that is requesting the connect
918 void xprt_connect(struct rpc_task
*task
)
920 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
922 trace_xprt_connect(xprt
);
924 if (!xprt_bound(xprt
)) {
925 task
->tk_status
= -EAGAIN
;
928 if (!xprt_lock_write(xprt
, task
))
931 if (test_and_clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
)) {
932 trace_xprt_disconnect_cleanup(xprt
);
933 xprt
->ops
->close(xprt
);
936 if (!xprt_connected(xprt
)) {
937 task
->tk_rqstp
->rq_connect_cookie
= xprt
->connect_cookie
;
938 rpc_sleep_on_timeout(&xprt
->pending
, task
, NULL
,
939 xprt_request_timeout(task
->tk_rqstp
));
941 if (test_bit(XPRT_CLOSING
, &xprt
->state
))
943 if (xprt_test_and_set_connecting(xprt
))
946 if (!xprt_connected(xprt
)) {
947 xprt
->stat
.connect_start
= jiffies
;
948 xprt
->ops
->connect(xprt
, task
);
950 xprt_clear_connecting(xprt
);
952 rpc_wake_up_queued_task(&xprt
->pending
, task
);
955 xprt_release_write(xprt
, task
);
959 * xprt_reconnect_delay - compute the wait before scheduling a connect
960 * @xprt: transport instance
963 unsigned long xprt_reconnect_delay(const struct rpc_xprt
*xprt
)
965 unsigned long start
, now
= jiffies
;
967 start
= xprt
->stat
.connect_start
+ xprt
->reestablish_timeout
;
968 if (time_after(start
, now
))
972 EXPORT_SYMBOL_GPL(xprt_reconnect_delay
);
975 * xprt_reconnect_backoff - compute the new re-establish timeout
976 * @xprt: transport instance
977 * @init_to: initial reestablish timeout
980 void xprt_reconnect_backoff(struct rpc_xprt
*xprt
, unsigned long init_to
)
982 xprt
->reestablish_timeout
<<= 1;
983 if (xprt
->reestablish_timeout
> xprt
->max_reconnect_timeout
)
984 xprt
->reestablish_timeout
= xprt
->max_reconnect_timeout
;
985 if (xprt
->reestablish_timeout
< init_to
)
986 xprt
->reestablish_timeout
= init_to
;
988 EXPORT_SYMBOL_GPL(xprt_reconnect_backoff
);
990 enum xprt_xid_rb_cmp
{
995 static enum xprt_xid_rb_cmp
996 xprt_xid_cmp(__be32 xid1
, __be32 xid2
)
1000 if ((__force u32
)xid1
< (__force u32
)xid2
)
1002 return XID_RB_RIGHT
;
1005 static struct rpc_rqst
*
1006 xprt_request_rb_find(struct rpc_xprt
*xprt
, __be32 xid
)
1008 struct rb_node
*n
= xprt
->recv_queue
.rb_node
;
1009 struct rpc_rqst
*req
;
1012 req
= rb_entry(n
, struct rpc_rqst
, rq_recv
);
1013 switch (xprt_xid_cmp(xid
, req
->rq_xid
)) {
1028 xprt_request_rb_insert(struct rpc_xprt
*xprt
, struct rpc_rqst
*new)
1030 struct rb_node
**p
= &xprt
->recv_queue
.rb_node
;
1031 struct rb_node
*n
= NULL
;
1032 struct rpc_rqst
*req
;
1034 while (*p
!= NULL
) {
1036 req
= rb_entry(n
, struct rpc_rqst
, rq_recv
);
1037 switch(xprt_xid_cmp(new->rq_xid
, req
->rq_xid
)) {
1045 WARN_ON_ONCE(new != req
);
1049 rb_link_node(&new->rq_recv
, n
, p
);
1050 rb_insert_color(&new->rq_recv
, &xprt
->recv_queue
);
1054 xprt_request_rb_remove(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
1056 rb_erase(&req
->rq_recv
, &xprt
->recv_queue
);
1060 * xprt_lookup_rqst - find an RPC request corresponding to an XID
1061 * @xprt: transport on which the original request was transmitted
1062 * @xid: RPC XID of incoming reply
1064 * Caller holds xprt->queue_lock.
1066 struct rpc_rqst
*xprt_lookup_rqst(struct rpc_xprt
*xprt
, __be32 xid
)
1068 struct rpc_rqst
*entry
;
1070 entry
= xprt_request_rb_find(xprt
, xid
);
1071 if (entry
!= NULL
) {
1072 trace_xprt_lookup_rqst(xprt
, xid
, 0);
1073 entry
->rq_rtt
= ktime_sub(ktime_get(), entry
->rq_xtime
);
1077 dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
1079 trace_xprt_lookup_rqst(xprt
, xid
, -ENOENT
);
1080 xprt
->stat
.bad_xids
++;
1083 EXPORT_SYMBOL_GPL(xprt_lookup_rqst
);
1086 xprt_is_pinned_rqst(struct rpc_rqst
*req
)
1088 return atomic_read(&req
->rq_pin
) != 0;
1092 * xprt_pin_rqst - Pin a request on the transport receive list
1093 * @req: Request to pin
1095 * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
1096 * so should be holding xprt->queue_lock.
1098 void xprt_pin_rqst(struct rpc_rqst
*req
)
1100 atomic_inc(&req
->rq_pin
);
1102 EXPORT_SYMBOL_GPL(xprt_pin_rqst
);
1105 * xprt_unpin_rqst - Unpin a request on the transport receive list
1106 * @req: Request to pin
1108 * Caller should be holding xprt->queue_lock.
1110 void xprt_unpin_rqst(struct rpc_rqst
*req
)
1112 if (!test_bit(RPC_TASK_MSG_PIN_WAIT
, &req
->rq_task
->tk_runstate
)) {
1113 atomic_dec(&req
->rq_pin
);
1116 if (atomic_dec_and_test(&req
->rq_pin
))
1117 wake_up_var(&req
->rq_pin
);
1119 EXPORT_SYMBOL_GPL(xprt_unpin_rqst
);
1121 static void xprt_wait_on_pinned_rqst(struct rpc_rqst
*req
)
1123 wait_var_event(&req
->rq_pin
, !xprt_is_pinned_rqst(req
));
1127 xprt_request_data_received(struct rpc_task
*task
)
1129 return !test_bit(RPC_TASK_NEED_RECV
, &task
->tk_runstate
) &&
1130 READ_ONCE(task
->tk_rqstp
->rq_reply_bytes_recvd
) != 0;
1134 xprt_request_need_enqueue_receive(struct rpc_task
*task
, struct rpc_rqst
*req
)
1136 return !test_bit(RPC_TASK_NEED_RECV
, &task
->tk_runstate
) &&
1137 READ_ONCE(task
->tk_rqstp
->rq_reply_bytes_recvd
) == 0;
1141 * xprt_request_enqueue_receive - Add an request to the receive queue
1146 xprt_request_enqueue_receive(struct rpc_task
*task
)
1148 struct rpc_rqst
*req
= task
->tk_rqstp
;
1149 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1151 if (!xprt_request_need_enqueue_receive(task
, req
))
1154 xprt_request_prepare(task
->tk_rqstp
);
1155 spin_lock(&xprt
->queue_lock
);
1157 /* Update the softirq receive buffer */
1158 memcpy(&req
->rq_private_buf
, &req
->rq_rcv_buf
,
1159 sizeof(req
->rq_private_buf
));
1161 /* Add request to the receive list */
1162 xprt_request_rb_insert(xprt
, req
);
1163 set_bit(RPC_TASK_NEED_RECV
, &task
->tk_runstate
);
1164 spin_unlock(&xprt
->queue_lock
);
1166 /* Turn off autodisconnect */
1167 del_singleshot_timer_sync(&xprt
->timer
);
1171 * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1174 * Caller must hold xprt->queue_lock.
1177 xprt_request_dequeue_receive_locked(struct rpc_task
*task
)
1179 struct rpc_rqst
*req
= task
->tk_rqstp
;
1181 if (test_and_clear_bit(RPC_TASK_NEED_RECV
, &task
->tk_runstate
))
1182 xprt_request_rb_remove(req
->rq_xprt
, req
);
1186 * xprt_update_rtt - Update RPC RTT statistics
1187 * @task: RPC request that recently completed
1189 * Caller holds xprt->queue_lock.
1191 void xprt_update_rtt(struct rpc_task
*task
)
1193 struct rpc_rqst
*req
= task
->tk_rqstp
;
1194 struct rpc_rtt
*rtt
= task
->tk_client
->cl_rtt
;
1195 unsigned int timer
= task
->tk_msg
.rpc_proc
->p_timer
;
1196 long m
= usecs_to_jiffies(ktime_to_us(req
->rq_rtt
));
1199 if (req
->rq_ntrans
== 1)
1200 rpc_update_rtt(rtt
, timer
, m
);
1201 rpc_set_timeo(rtt
, timer
, req
->rq_ntrans
- 1);
1204 EXPORT_SYMBOL_GPL(xprt_update_rtt
);
1207 * xprt_complete_rqst - called when reply processing is complete
1208 * @task: RPC request that recently completed
1209 * @copied: actual number of bytes received from the transport
1211 * Caller holds xprt->queue_lock.
1213 void xprt_complete_rqst(struct rpc_task
*task
, int copied
)
1215 struct rpc_rqst
*req
= task
->tk_rqstp
;
1216 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1220 req
->rq_private_buf
.len
= copied
;
1221 /* Ensure all writes are done before we update */
1222 /* req->rq_reply_bytes_recvd */
1224 req
->rq_reply_bytes_recvd
= copied
;
1225 xprt_request_dequeue_receive_locked(task
);
1226 rpc_wake_up_queued_task(&xprt
->pending
, task
);
1228 EXPORT_SYMBOL_GPL(xprt_complete_rqst
);
1230 static void xprt_timer(struct rpc_task
*task
)
1232 struct rpc_rqst
*req
= task
->tk_rqstp
;
1233 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1235 if (task
->tk_status
!= -ETIMEDOUT
)
1238 trace_xprt_timer(xprt
, req
->rq_xid
, task
->tk_status
);
1239 if (!req
->rq_reply_bytes_recvd
) {
1240 if (xprt
->ops
->timer
)
1241 xprt
->ops
->timer(xprt
, task
);
1243 task
->tk_status
= 0;
1247 * xprt_wait_for_reply_request_def - wait for reply
1248 * @task: pointer to rpc_task
1250 * Set a request's retransmit timeout based on the transport's
1251 * default timeout parameters. Used by transports that don't adjust
1252 * the retransmit timeout based on round-trip time estimation,
1253 * and put the task to sleep on the pending queue.
1255 void xprt_wait_for_reply_request_def(struct rpc_task
*task
)
1257 struct rpc_rqst
*req
= task
->tk_rqstp
;
1259 rpc_sleep_on_timeout(&req
->rq_xprt
->pending
, task
, xprt_timer
,
1260 xprt_request_timeout(req
));
1262 EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def
);
1265 * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1266 * @task: pointer to rpc_task
1268 * Set a request's retransmit timeout using the RTT estimator,
1269 * and put the task to sleep on the pending queue.
1271 void xprt_wait_for_reply_request_rtt(struct rpc_task
*task
)
1273 int timer
= task
->tk_msg
.rpc_proc
->p_timer
;
1274 struct rpc_clnt
*clnt
= task
->tk_client
;
1275 struct rpc_rtt
*rtt
= clnt
->cl_rtt
;
1276 struct rpc_rqst
*req
= task
->tk_rqstp
;
1277 unsigned long max_timeout
= clnt
->cl_timeout
->to_maxval
;
1278 unsigned long timeout
;
1280 timeout
= rpc_calc_rto(rtt
, timer
);
1281 timeout
<<= rpc_ntimeo(rtt
, timer
) + req
->rq_retries
;
1282 if (timeout
> max_timeout
|| timeout
== 0)
1283 timeout
= max_timeout
;
1284 rpc_sleep_on_timeout(&req
->rq_xprt
->pending
, task
, xprt_timer
,
1287 EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt
);
1290 * xprt_request_wait_receive - wait for the reply to an RPC request
1291 * @task: RPC task about to send a request
1294 void xprt_request_wait_receive(struct rpc_task
*task
)
1296 struct rpc_rqst
*req
= task
->tk_rqstp
;
1297 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1299 if (!test_bit(RPC_TASK_NEED_RECV
, &task
->tk_runstate
))
1302 * Sleep on the pending queue if we're expecting a reply.
1303 * The spinlock ensures atomicity between the test of
1304 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1306 spin_lock(&xprt
->queue_lock
);
1307 if (test_bit(RPC_TASK_NEED_RECV
, &task
->tk_runstate
)) {
1308 xprt
->ops
->wait_for_reply_request(task
);
1310 * Send an extra queue wakeup call if the
1311 * connection was dropped in case the call to
1312 * rpc_sleep_on() raced.
1314 if (xprt_request_retransmit_after_disconnect(task
))
1315 rpc_wake_up_queued_task_set_status(&xprt
->pending
,
1318 spin_unlock(&xprt
->queue_lock
);
1322 xprt_request_need_enqueue_transmit(struct rpc_task
*task
, struct rpc_rqst
*req
)
1324 return !test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
);
1328 * xprt_request_enqueue_transmit - queue a task for transmission
1329 * @task: pointer to rpc_task
1331 * Add a task to the transmission queue.
1334 xprt_request_enqueue_transmit(struct rpc_task
*task
)
1336 struct rpc_rqst
*pos
, *req
= task
->tk_rqstp
;
1337 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1339 if (xprt_request_need_enqueue_transmit(task
, req
)) {
1340 req
->rq_bytes_sent
= 0;
1341 spin_lock(&xprt
->queue_lock
);
1343 * Requests that carry congestion control credits are added
1344 * to the head of the list to avoid starvation issues.
1347 xprt_clear_congestion_window_wait(xprt
);
1348 list_for_each_entry(pos
, &xprt
->xmit_queue
, rq_xmit
) {
1351 /* Note: req is added _before_ pos */
1352 list_add_tail(&req
->rq_xmit
, &pos
->rq_xmit
);
1353 INIT_LIST_HEAD(&req
->rq_xmit2
);
1356 } else if (RPC_IS_SWAPPER(task
)) {
1357 list_for_each_entry(pos
, &xprt
->xmit_queue
, rq_xmit
) {
1358 if (pos
->rq_cong
|| pos
->rq_bytes_sent
)
1360 if (RPC_IS_SWAPPER(pos
->rq_task
))
1362 /* Note: req is added _before_ pos */
1363 list_add_tail(&req
->rq_xmit
, &pos
->rq_xmit
);
1364 INIT_LIST_HEAD(&req
->rq_xmit2
);
1367 } else if (!req
->rq_seqno
) {
1368 list_for_each_entry(pos
, &xprt
->xmit_queue
, rq_xmit
) {
1369 if (pos
->rq_task
->tk_owner
!= task
->tk_owner
)
1371 list_add_tail(&req
->rq_xmit2
, &pos
->rq_xmit2
);
1372 INIT_LIST_HEAD(&req
->rq_xmit
);
1376 list_add_tail(&req
->rq_xmit
, &xprt
->xmit_queue
);
1377 INIT_LIST_HEAD(&req
->rq_xmit2
);
1379 atomic_long_inc(&xprt
->xmit_queuelen
);
1380 set_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
);
1381 spin_unlock(&xprt
->queue_lock
);
1386 * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1387 * @task: pointer to rpc_task
1389 * Remove a task from the transmission queue
1390 * Caller must hold xprt->queue_lock
1393 xprt_request_dequeue_transmit_locked(struct rpc_task
*task
)
1395 struct rpc_rqst
*req
= task
->tk_rqstp
;
1397 if (!test_and_clear_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
))
1399 if (!list_empty(&req
->rq_xmit
)) {
1400 list_del(&req
->rq_xmit
);
1401 if (!list_empty(&req
->rq_xmit2
)) {
1402 struct rpc_rqst
*next
= list_first_entry(&req
->rq_xmit2
,
1403 struct rpc_rqst
, rq_xmit2
);
1404 list_del(&req
->rq_xmit2
);
1405 list_add_tail(&next
->rq_xmit
, &next
->rq_xprt
->xmit_queue
);
1408 list_del(&req
->rq_xmit2
);
1409 atomic_long_dec(&req
->rq_xprt
->xmit_queuelen
);
1413 * xprt_request_dequeue_transmit - remove a task from the transmission queue
1414 * @task: pointer to rpc_task
1416 * Remove a task from the transmission queue
1419 xprt_request_dequeue_transmit(struct rpc_task
*task
)
1421 struct rpc_rqst
*req
= task
->tk_rqstp
;
1422 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1424 spin_lock(&xprt
->queue_lock
);
1425 xprt_request_dequeue_transmit_locked(task
);
1426 spin_unlock(&xprt
->queue_lock
);
1430 * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1431 * @task: pointer to rpc_task
1433 * Remove a task from the transmit and receive queues, and ensure that
1434 * it is not pinned by the receive work item.
1437 xprt_request_dequeue_xprt(struct rpc_task
*task
)
1439 struct rpc_rqst
*req
= task
->tk_rqstp
;
1440 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1442 if (test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
) ||
1443 test_bit(RPC_TASK_NEED_RECV
, &task
->tk_runstate
) ||
1444 xprt_is_pinned_rqst(req
)) {
1445 spin_lock(&xprt
->queue_lock
);
1446 xprt_request_dequeue_transmit_locked(task
);
1447 xprt_request_dequeue_receive_locked(task
);
1448 while (xprt_is_pinned_rqst(req
)) {
1449 set_bit(RPC_TASK_MSG_PIN_WAIT
, &task
->tk_runstate
);
1450 spin_unlock(&xprt
->queue_lock
);
1451 xprt_wait_on_pinned_rqst(req
);
1452 spin_lock(&xprt
->queue_lock
);
1453 clear_bit(RPC_TASK_MSG_PIN_WAIT
, &task
->tk_runstate
);
1455 spin_unlock(&xprt
->queue_lock
);
1460 * xprt_request_prepare - prepare an encoded request for transport
1461 * @req: pointer to rpc_rqst
1463 * Calls into the transport layer to do whatever is needed to prepare
1464 * the request for transmission or receive.
1467 xprt_request_prepare(struct rpc_rqst
*req
)
1469 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1471 if (xprt
->ops
->prepare_request
)
1472 xprt
->ops
->prepare_request(req
);
1476 * xprt_request_need_retransmit - Test if a task needs retransmission
1477 * @task: pointer to rpc_task
1479 * Test for whether a connection breakage requires the task to retransmit
1482 xprt_request_need_retransmit(struct rpc_task
*task
)
1484 return xprt_request_retransmit_after_disconnect(task
);
1488 * xprt_prepare_transmit - reserve the transport before sending a request
1489 * @task: RPC task about to send a request
1492 bool xprt_prepare_transmit(struct rpc_task
*task
)
1494 struct rpc_rqst
*req
= task
->tk_rqstp
;
1495 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1497 if (!xprt_lock_write(xprt
, task
)) {
1498 /* Race breaker: someone may have transmitted us */
1499 if (!test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
))
1500 rpc_wake_up_queued_task_set_status(&xprt
->sending
,
1508 void xprt_end_transmit(struct rpc_task
*task
)
1510 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1512 xprt_inject_disconnect(xprt
);
1513 xprt_release_write(xprt
, task
);
1517 * xprt_request_transmit - send an RPC request on a transport
1518 * @req: pointer to request to transmit
1519 * @snd_task: RPC task that owns the transport lock
1521 * This performs the transmission of a single request.
1522 * Note that if the request is not the same as snd_task, then it
1523 * does need to be pinned.
1524 * Returns '0' on success.
1527 xprt_request_transmit(struct rpc_rqst
*req
, struct rpc_task
*snd_task
)
1529 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1530 struct rpc_task
*task
= req
->rq_task
;
1531 unsigned int connect_cookie
;
1532 int is_retrans
= RPC_WAS_SENT(task
);
1535 if (!req
->rq_bytes_sent
) {
1536 if (xprt_request_data_received(task
)) {
1540 /* Verify that our message lies in the RPCSEC_GSS window */
1541 if (rpcauth_xmit_need_reencode(task
)) {
1545 if (RPC_SIGNALLED(task
)) {
1546 status
= -ERESTARTSYS
;
1552 * Update req->rq_ntrans before transmitting to avoid races with
1553 * xprt_update_rtt(), which needs to know that it is recording a
1554 * reply to the first transmission.
1558 trace_rpc_xdr_sendto(task
, &req
->rq_snd_buf
);
1559 connect_cookie
= xprt
->connect_cookie
;
1560 status
= xprt
->ops
->send_request(req
);
1563 trace_xprt_transmit(req
, status
);
1568 task
->tk_client
->cl_stats
->rpcretrans
++;
1569 trace_xprt_retransmit(req
);
1572 xprt_inject_disconnect(xprt
);
1574 task
->tk_flags
|= RPC_TASK_SENT
;
1575 spin_lock(&xprt
->transport_lock
);
1578 xprt
->stat
.req_u
+= xprt
->stat
.sends
- xprt
->stat
.recvs
;
1579 xprt
->stat
.bklog_u
+= xprt
->backlog
.qlen
;
1580 xprt
->stat
.sending_u
+= xprt
->sending
.qlen
;
1581 xprt
->stat
.pending_u
+= xprt
->pending
.qlen
;
1582 spin_unlock(&xprt
->transport_lock
);
1584 req
->rq_connect_cookie
= connect_cookie
;
1586 trace_xprt_transmit(req
, status
);
1587 xprt_request_dequeue_transmit(task
);
1588 rpc_wake_up_queued_task_set_status(&xprt
->sending
, task
, status
);
1593 * xprt_transmit - send an RPC request on a transport
1594 * @task: controlling RPC task
1596 * Attempts to drain the transmit queue. On exit, either the transport
1597 * signalled an error that needs to be handled before transmission can
1598 * resume, or @task finished transmitting, and detected that it already
1602 xprt_transmit(struct rpc_task
*task
)
1604 struct rpc_rqst
*next
, *req
= task
->tk_rqstp
;
1605 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1608 spin_lock(&xprt
->queue_lock
);
1610 next
= list_first_entry_or_null(&xprt
->xmit_queue
,
1611 struct rpc_rqst
, rq_xmit
);
1614 xprt_pin_rqst(next
);
1615 spin_unlock(&xprt
->queue_lock
);
1616 status
= xprt_request_transmit(next
, task
);
1617 if (status
== -EBADMSG
&& next
!= req
)
1619 spin_lock(&xprt
->queue_lock
);
1620 xprt_unpin_rqst(next
);
1622 if (test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
))
1623 task
->tk_status
= status
;
1626 /* Was @task transmitted, and has it received a reply? */
1627 if (xprt_request_data_received(task
) &&
1628 !test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
))
1630 cond_resched_lock(&xprt
->queue_lock
);
1632 spin_unlock(&xprt
->queue_lock
);
1635 static void xprt_complete_request_init(struct rpc_task
*task
)
1638 xprt_request_init(task
);
1641 void xprt_add_backlog(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1643 set_bit(XPRT_CONGESTED
, &xprt
->state
);
1644 rpc_sleep_on(&xprt
->backlog
, task
, xprt_complete_request_init
);
1646 EXPORT_SYMBOL_GPL(xprt_add_backlog
);
1648 static bool __xprt_set_rq(struct rpc_task
*task
, void *data
)
1650 struct rpc_rqst
*req
= data
;
1652 if (task
->tk_rqstp
== NULL
) {
1653 memset(req
, 0, sizeof(*req
)); /* mark unused */
1654 task
->tk_rqstp
= req
;
1660 bool xprt_wake_up_backlog(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
1662 if (rpc_wake_up_first(&xprt
->backlog
, __xprt_set_rq
, req
) == NULL
) {
1663 clear_bit(XPRT_CONGESTED
, &xprt
->state
);
1668 EXPORT_SYMBOL_GPL(xprt_wake_up_backlog
);
1670 static bool xprt_throttle_congested(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1674 if (!test_bit(XPRT_CONGESTED
, &xprt
->state
))
1676 spin_lock(&xprt
->reserve_lock
);
1677 if (test_bit(XPRT_CONGESTED
, &xprt
->state
)) {
1678 xprt_add_backlog(xprt
, task
);
1681 spin_unlock(&xprt
->reserve_lock
);
1686 static struct rpc_rqst
*xprt_dynamic_alloc_slot(struct rpc_xprt
*xprt
)
1688 struct rpc_rqst
*req
= ERR_PTR(-EAGAIN
);
1689 gfp_t gfp_mask
= GFP_KERNEL
;
1691 if (xprt
->num_reqs
>= xprt
->max_reqs
)
1694 spin_unlock(&xprt
->reserve_lock
);
1695 if (current
->flags
& PF_WQ_WORKER
)
1696 gfp_mask
|= __GFP_NORETRY
| __GFP_NOWARN
;
1697 req
= kzalloc(sizeof(*req
), gfp_mask
);
1698 spin_lock(&xprt
->reserve_lock
);
1702 req
= ERR_PTR(-ENOMEM
);
1707 static bool xprt_dynamic_free_slot(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
1709 if (xprt
->num_reqs
> xprt
->min_reqs
) {
1717 void xprt_alloc_slot(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1719 struct rpc_rqst
*req
;
1721 spin_lock(&xprt
->reserve_lock
);
1722 if (!list_empty(&xprt
->free
)) {
1723 req
= list_entry(xprt
->free
.next
, struct rpc_rqst
, rq_list
);
1724 list_del(&req
->rq_list
);
1727 req
= xprt_dynamic_alloc_slot(xprt
);
1730 switch (PTR_ERR(req
)) {
1732 dprintk("RPC: dynamic allocation of request slot "
1733 "failed! Retrying\n");
1734 task
->tk_status
= -ENOMEM
;
1737 xprt_add_backlog(xprt
, task
);
1738 dprintk("RPC: waiting for request slot\n");
1741 task
->tk_status
= -EAGAIN
;
1743 spin_unlock(&xprt
->reserve_lock
);
1746 xprt
->stat
.max_slots
= max_t(unsigned int, xprt
->stat
.max_slots
,
1748 spin_unlock(&xprt
->reserve_lock
);
1750 task
->tk_status
= 0;
1751 task
->tk_rqstp
= req
;
1753 EXPORT_SYMBOL_GPL(xprt_alloc_slot
);
1755 void xprt_free_slot(struct rpc_xprt
*xprt
, struct rpc_rqst
*req
)
1757 spin_lock(&xprt
->reserve_lock
);
1758 if (!xprt_wake_up_backlog(xprt
, req
) &&
1759 !xprt_dynamic_free_slot(xprt
, req
)) {
1760 memset(req
, 0, sizeof(*req
)); /* mark unused */
1761 list_add(&req
->rq_list
, &xprt
->free
);
1763 spin_unlock(&xprt
->reserve_lock
);
1765 EXPORT_SYMBOL_GPL(xprt_free_slot
);
1767 static void xprt_free_all_slots(struct rpc_xprt
*xprt
)
1769 struct rpc_rqst
*req
;
1770 while (!list_empty(&xprt
->free
)) {
1771 req
= list_first_entry(&xprt
->free
, struct rpc_rqst
, rq_list
);
1772 list_del(&req
->rq_list
);
1777 static DEFINE_IDA(rpc_xprt_ids
);
1779 void xprt_cleanup_ids(void)
1781 ida_destroy(&rpc_xprt_ids
);
1784 static int xprt_alloc_id(struct rpc_xprt
*xprt
)
1788 id
= ida_simple_get(&rpc_xprt_ids
, 0, 0, GFP_KERNEL
);
1796 static void xprt_free_id(struct rpc_xprt
*xprt
)
1798 ida_simple_remove(&rpc_xprt_ids
, xprt
->id
);
1801 struct rpc_xprt
*xprt_alloc(struct net
*net
, size_t size
,
1802 unsigned int num_prealloc
,
1803 unsigned int max_alloc
)
1805 struct rpc_xprt
*xprt
;
1806 struct rpc_rqst
*req
;
1809 xprt
= kzalloc(size
, GFP_KERNEL
);
1813 xprt_alloc_id(xprt
);
1814 xprt_init(xprt
, net
);
1816 for (i
= 0; i
< num_prealloc
; i
++) {
1817 req
= kzalloc(sizeof(struct rpc_rqst
), GFP_KERNEL
);
1820 list_add(&req
->rq_list
, &xprt
->free
);
1822 if (max_alloc
> num_prealloc
)
1823 xprt
->max_reqs
= max_alloc
;
1825 xprt
->max_reqs
= num_prealloc
;
1826 xprt
->min_reqs
= num_prealloc
;
1827 xprt
->num_reqs
= num_prealloc
;
1836 EXPORT_SYMBOL_GPL(xprt_alloc
);
1838 void xprt_free(struct rpc_xprt
*xprt
)
1840 put_net(xprt
->xprt_net
);
1841 xprt_free_all_slots(xprt
);
1843 rpc_sysfs_xprt_destroy(xprt
);
1844 kfree_rcu(xprt
, rcu
);
1846 EXPORT_SYMBOL_GPL(xprt_free
);
1849 xprt_init_connect_cookie(struct rpc_rqst
*req
, struct rpc_xprt
*xprt
)
1851 req
->rq_connect_cookie
= xprt_connect_cookie(xprt
) - 1;
1855 xprt_alloc_xid(struct rpc_xprt
*xprt
)
1859 spin_lock(&xprt
->reserve_lock
);
1860 xid
= (__force __be32
)xprt
->xid
++;
1861 spin_unlock(&xprt
->reserve_lock
);
1866 xprt_init_xid(struct rpc_xprt
*xprt
)
1868 xprt
->xid
= prandom_u32();
1872 xprt_request_init(struct rpc_task
*task
)
1874 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1875 struct rpc_rqst
*req
= task
->tk_rqstp
;
1877 req
->rq_task
= task
;
1878 req
->rq_xprt
= xprt
;
1879 req
->rq_buffer
= NULL
;
1880 req
->rq_xid
= xprt_alloc_xid(xprt
);
1881 xprt_init_connect_cookie(req
, xprt
);
1882 req
->rq_snd_buf
.len
= 0;
1883 req
->rq_snd_buf
.buflen
= 0;
1884 req
->rq_rcv_buf
.len
= 0;
1885 req
->rq_rcv_buf
.buflen
= 0;
1886 req
->rq_snd_buf
.bvec
= NULL
;
1887 req
->rq_rcv_buf
.bvec
= NULL
;
1888 req
->rq_release_snd_buf
= NULL
;
1889 xprt_init_majortimeo(task
, req
);
1891 trace_xprt_reserve(req
);
1895 xprt_do_reserve(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1897 xprt
->ops
->alloc_slot(xprt
, task
);
1898 if (task
->tk_rqstp
!= NULL
)
1899 xprt_request_init(task
);
1903 * xprt_reserve - allocate an RPC request slot
1904 * @task: RPC task requesting a slot allocation
1906 * If the transport is marked as being congested, or if no more
1907 * slots are available, place the task on the transport's
1910 void xprt_reserve(struct rpc_task
*task
)
1912 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1914 task
->tk_status
= 0;
1915 if (task
->tk_rqstp
!= NULL
)
1918 task
->tk_status
= -EAGAIN
;
1919 if (!xprt_throttle_congested(xprt
, task
))
1920 xprt_do_reserve(xprt
, task
);
1924 * xprt_retry_reserve - allocate an RPC request slot
1925 * @task: RPC task requesting a slot allocation
1927 * If no more slots are available, place the task on the transport's
1929 * Note that the only difference with xprt_reserve is that we now
1930 * ignore the value of the XPRT_CONGESTED flag.
1932 void xprt_retry_reserve(struct rpc_task
*task
)
1934 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1936 task
->tk_status
= 0;
1937 if (task
->tk_rqstp
!= NULL
)
1940 task
->tk_status
= -EAGAIN
;
1941 xprt_do_reserve(xprt
, task
);
1945 * xprt_release - release an RPC request slot
1946 * @task: task which is finished with the slot
1949 void xprt_release(struct rpc_task
*task
)
1951 struct rpc_xprt
*xprt
;
1952 struct rpc_rqst
*req
= task
->tk_rqstp
;
1955 if (task
->tk_client
) {
1956 xprt
= task
->tk_xprt
;
1957 xprt_release_write(xprt
, task
);
1962 xprt
= req
->rq_xprt
;
1963 xprt_request_dequeue_xprt(task
);
1964 spin_lock(&xprt
->transport_lock
);
1965 xprt
->ops
->release_xprt(xprt
, task
);
1966 if (xprt
->ops
->release_request
)
1967 xprt
->ops
->release_request(task
);
1968 xprt_schedule_autodisconnect(xprt
);
1969 spin_unlock(&xprt
->transport_lock
);
1971 xprt
->ops
->buf_free(task
);
1972 xdr_free_bvec(&req
->rq_rcv_buf
);
1973 xdr_free_bvec(&req
->rq_snd_buf
);
1974 if (req
->rq_cred
!= NULL
)
1975 put_rpccred(req
->rq_cred
);
1976 if (req
->rq_release_snd_buf
)
1977 req
->rq_release_snd_buf(req
);
1979 task
->tk_rqstp
= NULL
;
1980 if (likely(!bc_prealloc(req
)))
1981 xprt
->ops
->free_slot(xprt
, req
);
1983 xprt_free_bc_request(req
);
1986 #ifdef CONFIG_SUNRPC_BACKCHANNEL
1988 xprt_init_bc_request(struct rpc_rqst
*req
, struct rpc_task
*task
)
1990 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
1992 task
->tk_rqstp
= req
;
1993 req
->rq_task
= task
;
1994 xprt_init_connect_cookie(req
, req
->rq_xprt
);
1996 * Set up the xdr_buf length.
1997 * This also indicates that the buffer is XDR encoded already.
1999 xbufp
->len
= xbufp
->head
[0].iov_len
+ xbufp
->page_len
+
2000 xbufp
->tail
[0].iov_len
;
2004 static void xprt_init(struct rpc_xprt
*xprt
, struct net
*net
)
2006 kref_init(&xprt
->kref
);
2008 spin_lock_init(&xprt
->transport_lock
);
2009 spin_lock_init(&xprt
->reserve_lock
);
2010 spin_lock_init(&xprt
->queue_lock
);
2012 INIT_LIST_HEAD(&xprt
->free
);
2013 xprt
->recv_queue
= RB_ROOT
;
2014 INIT_LIST_HEAD(&xprt
->xmit_queue
);
2015 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2016 spin_lock_init(&xprt
->bc_pa_lock
);
2017 INIT_LIST_HEAD(&xprt
->bc_pa_list
);
2018 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2019 INIT_LIST_HEAD(&xprt
->xprt_switch
);
2021 xprt
->last_used
= jiffies
;
2022 xprt
->cwnd
= RPC_INITCWND
;
2023 xprt
->bind_index
= 0;
2025 rpc_init_wait_queue(&xprt
->binding
, "xprt_binding");
2026 rpc_init_wait_queue(&xprt
->pending
, "xprt_pending");
2027 rpc_init_wait_queue(&xprt
->sending
, "xprt_sending");
2028 rpc_init_priority_wait_queue(&xprt
->backlog
, "xprt_backlog");
2030 xprt_init_xid(xprt
);
2032 xprt
->xprt_net
= get_net(net
);
2036 * xprt_create_transport - create an RPC transport
2037 * @args: rpc transport creation arguments
2040 struct rpc_xprt
*xprt_create_transport(struct xprt_create
*args
)
2042 struct rpc_xprt
*xprt
;
2043 const struct xprt_class
*t
;
2045 t
= xprt_class_find_by_ident(args
->ident
);
2047 dprintk("RPC: transport (%d) not supported\n", args
->ident
);
2048 return ERR_PTR(-EIO
);
2051 xprt
= t
->setup(args
);
2052 xprt_class_release(t
);
2056 if (args
->flags
& XPRT_CREATE_NO_IDLE_TIMEOUT
)
2057 xprt
->idle_timeout
= 0;
2058 INIT_WORK(&xprt
->task_cleanup
, xprt_autoclose
);
2059 if (xprt_has_timer(xprt
))
2060 timer_setup(&xprt
->timer
, xprt_init_autodisconnect
, 0);
2062 timer_setup(&xprt
->timer
, NULL
, 0);
2064 if (strlen(args
->servername
) > RPC_MAXNETNAMELEN
) {
2066 return ERR_PTR(-EINVAL
);
2068 xprt
->servername
= kstrdup(args
->servername
, GFP_KERNEL
);
2069 if (xprt
->servername
== NULL
) {
2071 return ERR_PTR(-ENOMEM
);
2074 rpc_xprt_debugfs_register(xprt
);
2076 trace_xprt_create(xprt
);
2081 static void xprt_destroy_cb(struct work_struct
*work
)
2083 struct rpc_xprt
*xprt
=
2084 container_of(work
, struct rpc_xprt
, task_cleanup
);
2086 trace_xprt_destroy(xprt
);
2088 rpc_xprt_debugfs_unregister(xprt
);
2089 rpc_destroy_wait_queue(&xprt
->binding
);
2090 rpc_destroy_wait_queue(&xprt
->pending
);
2091 rpc_destroy_wait_queue(&xprt
->sending
);
2092 rpc_destroy_wait_queue(&xprt
->backlog
);
2093 kfree(xprt
->servername
);
2095 * Destroy any existing back channel
2097 xprt_destroy_backchannel(xprt
, UINT_MAX
);
2100 * Tear down transport state and free the rpc_xprt
2102 xprt
->ops
->destroy(xprt
);
2106 * xprt_destroy - destroy an RPC transport, killing off all requests.
2107 * @xprt: transport to destroy
2110 static void xprt_destroy(struct rpc_xprt
*xprt
)
2113 * Exclude transport connect/disconnect handlers and autoclose
2115 wait_on_bit_lock(&xprt
->state
, XPRT_LOCKED
, TASK_UNINTERRUPTIBLE
);
2118 * xprt_schedule_autodisconnect() can run after XPRT_LOCKED
2119 * is cleared. We use ->transport_lock to ensure the mod_timer()
2120 * can only run *before* del_time_sync(), never after.
2122 spin_lock(&xprt
->transport_lock
);
2123 del_timer_sync(&xprt
->timer
);
2124 spin_unlock(&xprt
->transport_lock
);
2127 * Destroy sockets etc from the system workqueue so they can
2128 * safely flush receive work running on rpciod.
2130 INIT_WORK(&xprt
->task_cleanup
, xprt_destroy_cb
);
2131 schedule_work(&xprt
->task_cleanup
);
2134 static void xprt_destroy_kref(struct kref
*kref
)
2136 xprt_destroy(container_of(kref
, struct rpc_xprt
, kref
));
2140 * xprt_get - return a reference to an RPC transport.
2141 * @xprt: pointer to the transport
2144 struct rpc_xprt
*xprt_get(struct rpc_xprt
*xprt
)
2146 if (xprt
!= NULL
&& kref_get_unless_zero(&xprt
->kref
))
2150 EXPORT_SYMBOL_GPL(xprt_get
);
2153 * xprt_put - release a reference to an RPC transport.
2154 * @xprt: pointer to the transport
2157 void xprt_put(struct rpc_xprt
*xprt
)
2160 kref_put(&xprt
->kref
, xprt_destroy_kref
);
2162 EXPORT_SYMBOL_GPL(xprt_put
);