11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/net/sunrpc/xprt.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This is a generic RPC call interface supporting congestion avoidance, 51da177e4SLinus Torvalds * and asynchronous calls. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * The interface works like this: 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * - When a process places a call, it allocates a request slot if 101da177e4SLinus Torvalds * one is available. Otherwise, it sleeps on the backlog queue 111da177e4SLinus Torvalds * (xprt_reserve). 121da177e4SLinus Torvalds * - Next, the caller puts together the RPC message, stuffs it into 1355aa4f58SChuck Lever * the request struct, and calls xprt_transmit(). 1455aa4f58SChuck Lever * - xprt_transmit sends the message and installs the caller on the 1555ae1aabSRicardo Labiaga * transport's wait list. At the same time, if a reply is expected, 1655ae1aabSRicardo Labiaga * it installs a timer that is run after the packet's timeout has 1755ae1aabSRicardo Labiaga * expired. 181da177e4SLinus Torvalds * - When a packet arrives, the data_ready handler walks the list of 1955aa4f58SChuck Lever * pending requests for that transport. If a matching XID is found, the 201da177e4SLinus Torvalds * caller is woken up, and the timer removed. 211da177e4SLinus Torvalds * - When no reply arrives within the timeout interval, the timer is 221da177e4SLinus Torvalds * fired by the kernel and runs xprt_timer(). It either adjusts the 231da177e4SLinus Torvalds * timeout values (minor timeout) or wakes up the caller with a status 241da177e4SLinus Torvalds * of -ETIMEDOUT. 251da177e4SLinus Torvalds * - When the caller receives a notification from RPC that a reply arrived, 261da177e4SLinus Torvalds * it should release the RPC slot, and process the reply. 271da177e4SLinus Torvalds * If the call timed out, it may choose to retry the operation by 281da177e4SLinus Torvalds * adjusting the initial timeout value, and simply calling rpc_call 291da177e4SLinus Torvalds * again. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Support for async RPC is done through a set of RPC-specific scheduling 321da177e4SLinus Torvalds * primitives that `transparently' work for processes as well as async 331da177e4SLinus Torvalds * tasks that rely on callbacks. 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 3655aa4f58SChuck Lever * 3755aa4f58SChuck Lever * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 40a246b010SChuck Lever #include <linux/module.h> 41a246b010SChuck Lever 421da177e4SLinus Torvalds #include <linux/types.h> 43a246b010SChuck Lever #include <linux/interrupt.h> 441da177e4SLinus Torvalds #include <linux/workqueue.h> 45bf3fcf89SChuck Lever #include <linux/net.h> 46ff839970SChuck Lever #include <linux/ktime.h> 471da177e4SLinus Torvalds 48a246b010SChuck Lever #include <linux/sunrpc/clnt.h> 4911c556b3SChuck Lever #include <linux/sunrpc/metrics.h> 50c9acb42eSTrond Myklebust #include <linux/sunrpc/bc_xprt.h> 511da177e4SLinus Torvalds 523705ad64SJeff Layton #include <trace/events/sunrpc.h> 533705ad64SJeff Layton 5455ae1aabSRicardo Labiaga #include "sunrpc.h" 5555ae1aabSRicardo Labiaga 561da177e4SLinus Torvalds /* 571da177e4SLinus Torvalds * Local variables 581da177e4SLinus Torvalds */ 591da177e4SLinus Torvalds 60f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 611da177e4SLinus Torvalds # define RPCDBG_FACILITY RPCDBG_XPRT 621da177e4SLinus Torvalds #endif 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * Local functions 661da177e4SLinus Torvalds */ 6721de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net); 681da177e4SLinus Torvalds static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); 691da177e4SLinus Torvalds static void xprt_connect_status(struct rpc_task *task); 701da177e4SLinus Torvalds static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); 714e0038b6STrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt); 721da177e4SLinus Torvalds 735ba03e82SJiri Slaby static DEFINE_SPINLOCK(xprt_list_lock); 7481c098afS\"Talpey, Thomas\ static LIST_HEAD(xprt_list); 7581c098afS\"Talpey, Thomas\ 7612a80469SChuck Lever /** 7781c098afS\"Talpey, Thomas\ * xprt_register_transport - register a transport implementation 7881c098afS\"Talpey, Thomas\ * @transport: transport to register 7981c098afS\"Talpey, Thomas\ * 8081c098afS\"Talpey, Thomas\ * If a transport implementation is loaded as a kernel module, it can 8181c098afS\"Talpey, Thomas\ * call this interface to make itself known to the RPC client. 8281c098afS\"Talpey, Thomas\ * 8381c098afS\"Talpey, Thomas\ * Returns: 8481c098afS\"Talpey, Thomas\ * 0: transport successfully registered 8581c098afS\"Talpey, Thomas\ * -EEXIST: transport already registered 8681c098afS\"Talpey, Thomas\ * -EINVAL: transport module being unloaded 8781c098afS\"Talpey, Thomas\ */ 8881c098afS\"Talpey, Thomas\ int xprt_register_transport(struct xprt_class *transport) 8981c098afS\"Talpey, Thomas\ { 9081c098afS\"Talpey, Thomas\ struct xprt_class *t; 9181c098afS\"Talpey, Thomas\ int result; 9281c098afS\"Talpey, Thomas\ 9381c098afS\"Talpey, Thomas\ result = -EEXIST; 9481c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 9581c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 9681c098afS\"Talpey, Thomas\ /* don't register the same transport class twice */ 974fa016ebS\"Talpey, Thomas\ if (t->ident == transport->ident) 9881c098afS\"Talpey, Thomas\ goto out; 9981c098afS\"Talpey, Thomas\ } 10081c098afS\"Talpey, Thomas\ 10181c098afS\"Talpey, Thomas\ list_add_tail(&transport->list, &xprt_list); 10281c098afS\"Talpey, Thomas\ printk(KERN_INFO "RPC: Registered %s transport module.\n", 10381c098afS\"Talpey, Thomas\ transport->name); 10481c098afS\"Talpey, Thomas\ result = 0; 10581c098afS\"Talpey, Thomas\ 10681c098afS\"Talpey, Thomas\ out: 10781c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 10881c098afS\"Talpey, Thomas\ return result; 10981c098afS\"Talpey, Thomas\ } 11081c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_register_transport); 11181c098afS\"Talpey, Thomas\ 11281c098afS\"Talpey, Thomas\ /** 11381c098afS\"Talpey, Thomas\ * xprt_unregister_transport - unregister a transport implementation 11465b6e42cSRandy Dunlap * @transport: transport to unregister 11581c098afS\"Talpey, Thomas\ * 11681c098afS\"Talpey, Thomas\ * Returns: 11781c098afS\"Talpey, Thomas\ * 0: transport successfully unregistered 11881c098afS\"Talpey, Thomas\ * -ENOENT: transport never registered 11981c098afS\"Talpey, Thomas\ */ 12081c098afS\"Talpey, Thomas\ int xprt_unregister_transport(struct xprt_class *transport) 12181c098afS\"Talpey, Thomas\ { 12281c098afS\"Talpey, Thomas\ struct xprt_class *t; 12381c098afS\"Talpey, Thomas\ int result; 12481c098afS\"Talpey, Thomas\ 12581c098afS\"Talpey, Thomas\ result = 0; 12681c098afS\"Talpey, Thomas\ spin_lock(&xprt_list_lock); 12781c098afS\"Talpey, Thomas\ list_for_each_entry(t, &xprt_list, list) { 12881c098afS\"Talpey, Thomas\ if (t == transport) { 12981c098afS\"Talpey, Thomas\ printk(KERN_INFO 13081c098afS\"Talpey, Thomas\ "RPC: Unregistered %s transport module.\n", 13181c098afS\"Talpey, Thomas\ transport->name); 13281c098afS\"Talpey, Thomas\ list_del_init(&transport->list); 13381c098afS\"Talpey, Thomas\ goto out; 13481c098afS\"Talpey, Thomas\ } 13581c098afS\"Talpey, Thomas\ } 13681c098afS\"Talpey, Thomas\ result = -ENOENT; 13781c098afS\"Talpey, Thomas\ 13881c098afS\"Talpey, Thomas\ out: 13981c098afS\"Talpey, Thomas\ spin_unlock(&xprt_list_lock); 14081c098afS\"Talpey, Thomas\ return result; 14181c098afS\"Talpey, Thomas\ } 14281c098afS\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_unregister_transport); 14381c098afS\"Talpey, Thomas\ 14481c098afS\"Talpey, Thomas\ /** 145441e3e24STom Talpey * xprt_load_transport - load a transport implementation 146441e3e24STom Talpey * @transport_name: transport to load 147441e3e24STom Talpey * 148441e3e24STom Talpey * Returns: 149441e3e24STom Talpey * 0: transport successfully loaded 150441e3e24STom Talpey * -ENOENT: transport module not available 151441e3e24STom Talpey */ 152441e3e24STom Talpey int xprt_load_transport(const char *transport_name) 153441e3e24STom Talpey { 154441e3e24STom Talpey struct xprt_class *t; 155441e3e24STom Talpey int result; 156441e3e24STom Talpey 157441e3e24STom Talpey result = 0; 158441e3e24STom Talpey spin_lock(&xprt_list_lock); 159441e3e24STom Talpey list_for_each_entry(t, &xprt_list, list) { 160441e3e24STom Talpey if (strcmp(t->name, transport_name) == 0) { 161441e3e24STom Talpey spin_unlock(&xprt_list_lock); 162441e3e24STom Talpey goto out; 163441e3e24STom Talpey } 164441e3e24STom Talpey } 165441e3e24STom Talpey spin_unlock(&xprt_list_lock); 166ef7ffe8fSAlex Riesen result = request_module("xprt%s", transport_name); 167441e3e24STom Talpey out: 168441e3e24STom Talpey return result; 169441e3e24STom Talpey } 170441e3e24STom Talpey EXPORT_SYMBOL_GPL(xprt_load_transport); 171441e3e24STom Talpey 172441e3e24STom Talpey /** 17312a80469SChuck Lever * xprt_reserve_xprt - serialize write access to transports 17412a80469SChuck Lever * @task: task that is requesting access to the transport 175177c27bfSRandy Dunlap * @xprt: pointer to the target transport 17612a80469SChuck Lever * 17712a80469SChuck Lever * This prevents mixing the payload of separate requests, and prevents 17812a80469SChuck Lever * transport connects from colliding with writes. No congestion control 17912a80469SChuck Lever * is provided. 1801da177e4SLinus Torvalds */ 18143cedbf0STrond Myklebust int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 1821da177e4SLinus Torvalds { 18312a80469SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 18434006ceeSTrond Myklebust int priority; 18512a80469SChuck Lever 18612a80469SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 18712a80469SChuck Lever if (task == xprt->snd_task) 18812a80469SChuck Lever return 1; 18912a80469SChuck Lever goto out_sleep; 19012a80469SChuck Lever } 19112a80469SChuck Lever xprt->snd_task = task; 19292551948STrond Myklebust if (req != NULL) 19312a80469SChuck Lever req->rq_ntrans++; 1944d4a76f3Sj223yang@asset.uwaterloo.ca 19512a80469SChuck Lever return 1; 19612a80469SChuck Lever 19712a80469SChuck Lever out_sleep: 19846121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", 19912a80469SChuck Lever task->tk_pid, xprt); 20012a80469SChuck Lever task->tk_timeout = 0; 20112a80469SChuck Lever task->tk_status = -EAGAIN; 20234006ceeSTrond Myklebust if (req == NULL) 20334006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 20434006ceeSTrond Myklebust else if (!req->rq_ntrans) 20534006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 20612a80469SChuck Lever else 20734006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 20834006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 20912a80469SChuck Lever return 0; 21012a80469SChuck Lever } 21112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt); 21212a80469SChuck Lever 213632e3bdcSTrond Myklebust static void xprt_clear_locked(struct rpc_xprt *xprt) 214632e3bdcSTrond Myklebust { 215632e3bdcSTrond Myklebust xprt->snd_task = NULL; 216d19751e7STrond Myklebust if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) { 2174e857c58SPeter Zijlstra smp_mb__before_atomic(); 218632e3bdcSTrond Myklebust clear_bit(XPRT_LOCKED, &xprt->state); 2194e857c58SPeter Zijlstra smp_mb__after_atomic(); 220632e3bdcSTrond Myklebust } else 221c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 222632e3bdcSTrond Myklebust } 223632e3bdcSTrond Myklebust 22412a80469SChuck Lever /* 22512a80469SChuck Lever * xprt_reserve_xprt_cong - serialize write access to transports 22612a80469SChuck Lever * @task: task that is requesting access to the transport 22712a80469SChuck Lever * 22812a80469SChuck Lever * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 22912a80469SChuck Lever * integrated into the decision of whether a request is allowed to be 23012a80469SChuck Lever * woken up and given access to the transport. 23112a80469SChuck Lever */ 23243cedbf0STrond Myklebust int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 23312a80469SChuck Lever { 2341da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 23534006ceeSTrond Myklebust int priority; 2361da177e4SLinus Torvalds 2372226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 2381da177e4SLinus Torvalds if (task == xprt->snd_task) 2391da177e4SLinus Torvalds return 1; 2401da177e4SLinus Torvalds goto out_sleep; 2411da177e4SLinus Torvalds } 24243cedbf0STrond Myklebust if (req == NULL) { 24343cedbf0STrond Myklebust xprt->snd_task = task; 24443cedbf0STrond Myklebust return 1; 24543cedbf0STrond Myklebust } 24612a80469SChuck Lever if (__xprt_get_cong(xprt, task)) { 2471da177e4SLinus Torvalds xprt->snd_task = task; 2481da177e4SLinus Torvalds req->rq_ntrans++; 2491da177e4SLinus Torvalds return 1; 2501da177e4SLinus Torvalds } 251632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 2521da177e4SLinus Torvalds out_sleep: 25346121cf7SChuck Lever dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt); 2541da177e4SLinus Torvalds task->tk_timeout = 0; 2551da177e4SLinus Torvalds task->tk_status = -EAGAIN; 25634006ceeSTrond Myklebust if (req == NULL) 25734006ceeSTrond Myklebust priority = RPC_PRIORITY_LOW; 25834006ceeSTrond Myklebust else if (!req->rq_ntrans) 25934006ceeSTrond Myklebust priority = RPC_PRIORITY_NORMAL; 2601da177e4SLinus Torvalds else 26134006ceeSTrond Myklebust priority = RPC_PRIORITY_HIGH; 26234006ceeSTrond Myklebust rpc_sleep_on_priority(&xprt->sending, task, NULL, priority); 2631da177e4SLinus Torvalds return 0; 2641da177e4SLinus Torvalds } 26512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong); 2661da177e4SLinus Torvalds 26712a80469SChuck Lever static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds int retval; 2701da177e4SLinus Torvalds 2714a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 27243cedbf0STrond Myklebust retval = xprt->ops->reserve_xprt(xprt, task); 2734a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 2741da177e4SLinus Torvalds return retval; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 277961a828dSTrond Myklebust static bool __xprt_lock_write_func(struct rpc_task *task, void *data) 2781da177e4SLinus Torvalds { 279961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 28049e9a890SChuck Lever struct rpc_rqst *req; 28149e9a890SChuck Lever 28249e9a890SChuck Lever req = task->tk_rqstp; 28349e9a890SChuck Lever xprt->snd_task = task; 28492551948STrond Myklebust if (req) 28549e9a890SChuck Lever req->rq_ntrans++; 286961a828dSTrond Myklebust return true; 287961a828dSTrond Myklebust } 288961a828dSTrond Myklebust 289961a828dSTrond Myklebust static void __xprt_lock_write_next(struct rpc_xprt *xprt) 290961a828dSTrond Myklebust { 291961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 29249e9a890SChuck Lever return; 29349e9a890SChuck Lever 294961a828dSTrond Myklebust if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt)) 295961a828dSTrond Myklebust return; 296632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 29749e9a890SChuck Lever } 29849e9a890SChuck Lever 299961a828dSTrond Myklebust static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data) 30049e9a890SChuck Lever { 301961a828dSTrond Myklebust struct rpc_xprt *xprt = data; 30243cedbf0STrond Myklebust struct rpc_rqst *req; 3031da177e4SLinus Torvalds 30443cedbf0STrond Myklebust req = task->tk_rqstp; 30543cedbf0STrond Myklebust if (req == NULL) { 3061da177e4SLinus Torvalds xprt->snd_task = task; 307961a828dSTrond Myklebust return true; 30843cedbf0STrond Myklebust } 30943cedbf0STrond Myklebust if (__xprt_get_cong(xprt, task)) { 31043cedbf0STrond Myklebust xprt->snd_task = task; 3111da177e4SLinus Torvalds req->rq_ntrans++; 312961a828dSTrond Myklebust return true; 3131da177e4SLinus Torvalds } 314961a828dSTrond Myklebust return false; 315961a828dSTrond Myklebust } 316961a828dSTrond Myklebust 317961a828dSTrond Myklebust static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 318961a828dSTrond Myklebust { 319961a828dSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 320961a828dSTrond Myklebust return; 321961a828dSTrond Myklebust if (RPCXPRT_CONGESTED(xprt)) 322961a828dSTrond Myklebust goto out_unlock; 323961a828dSTrond Myklebust if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt)) 324961a828dSTrond Myklebust return; 3251da177e4SLinus Torvalds out_unlock: 326632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 32949e9a890SChuck Lever /** 33049e9a890SChuck Lever * xprt_release_xprt - allow other requests to use a transport 33149e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 33249e9a890SChuck Lever * @task: task that is releasing access to the transport 33349e9a890SChuck Lever * 33449e9a890SChuck Lever * Note that "task" can be NULL. No congestion control is provided. 3351da177e4SLinus Torvalds */ 33649e9a890SChuck Lever void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds if (xprt->snd_task == task) { 339ee071effSTrond Myklebust if (task != NULL) { 340ee071effSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 341ee071effSTrond Myklebust if (req != NULL) 342ee071effSTrond Myklebust req->rq_bytes_sent = 0; 343ee071effSTrond Myklebust } 344632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 3451da177e4SLinus Torvalds __xprt_lock_write_next(xprt); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds } 34812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt); 3491da177e4SLinus Torvalds 35049e9a890SChuck Lever /** 35149e9a890SChuck Lever * xprt_release_xprt_cong - allow other requests to use a transport 35249e9a890SChuck Lever * @xprt: transport with other tasks potentially waiting 35349e9a890SChuck Lever * @task: task that is releasing access to the transport 35449e9a890SChuck Lever * 35549e9a890SChuck Lever * Note that "task" can be NULL. Another task is awoken to use the 35649e9a890SChuck Lever * transport if the transport's congestion window allows it. 35749e9a890SChuck Lever */ 35849e9a890SChuck Lever void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 35949e9a890SChuck Lever { 36049e9a890SChuck Lever if (xprt->snd_task == task) { 361ee071effSTrond Myklebust if (task != NULL) { 362ee071effSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 363ee071effSTrond Myklebust if (req != NULL) 364ee071effSTrond Myklebust req->rq_bytes_sent = 0; 365ee071effSTrond Myklebust } 366632e3bdcSTrond Myklebust xprt_clear_locked(xprt); 36749e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 36849e9a890SChuck Lever } 36949e9a890SChuck Lever } 37012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_xprt_cong); 37149e9a890SChuck Lever 37249e9a890SChuck Lever static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 3731da177e4SLinus Torvalds { 3744a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 37549e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 3764a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds /* 3801da177e4SLinus Torvalds * Van Jacobson congestion avoidance. Check if the congestion window 3811da177e4SLinus Torvalds * overflowed. Put the task to sleep if this is the case. 3821da177e4SLinus Torvalds */ 3831da177e4SLinus Torvalds static int 3841da177e4SLinus Torvalds __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (req->rq_cong) 3891da177e4SLinus Torvalds return 1; 39046121cf7SChuck Lever dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n", 3911da177e4SLinus Torvalds task->tk_pid, xprt->cong, xprt->cwnd); 3921da177e4SLinus Torvalds if (RPCXPRT_CONGESTED(xprt)) 3931da177e4SLinus Torvalds return 0; 3941da177e4SLinus Torvalds req->rq_cong = 1; 3951da177e4SLinus Torvalds xprt->cong += RPC_CWNDSCALE; 3961da177e4SLinus Torvalds return 1; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds /* 4001da177e4SLinus Torvalds * Adjust the congestion window, and wake up the next task 4011da177e4SLinus Torvalds * that has been sleeping due to congestion 4021da177e4SLinus Torvalds */ 4031da177e4SLinus Torvalds static void 4041da177e4SLinus Torvalds __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds if (!req->rq_cong) 4071da177e4SLinus Torvalds return; 4081da177e4SLinus Torvalds req->rq_cong = 0; 4091da177e4SLinus Torvalds xprt->cong -= RPC_CWNDSCALE; 41049e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 41346c0ee8bSChuck Lever /** 414a58dd398SChuck Lever * xprt_release_rqst_cong - housekeeping when request is complete 415a58dd398SChuck Lever * @task: RPC request that recently completed 416a58dd398SChuck Lever * 417a58dd398SChuck Lever * Useful for transports that require congestion control. 418a58dd398SChuck Lever */ 419a58dd398SChuck Lever void xprt_release_rqst_cong(struct rpc_task *task) 420a58dd398SChuck Lever { 421a4f0835cSTrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 422a4f0835cSTrond Myklebust 423a4f0835cSTrond Myklebust __xprt_put_cong(req->rq_xprt, req); 424a58dd398SChuck Lever } 42512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_release_rqst_cong); 426a58dd398SChuck Lever 427a58dd398SChuck Lever /** 42846c0ee8bSChuck Lever * xprt_adjust_cwnd - adjust transport congestion window 4296a24dfb6STrond Myklebust * @xprt: pointer to xprt 43046c0ee8bSChuck Lever * @task: recently completed RPC request used to adjust window 43146c0ee8bSChuck Lever * @result: result code of completed RPC request 43246c0ee8bSChuck Lever * 4334f4cf5adSChuck Lever * The transport code maintains an estimate on the maximum number of out- 4344f4cf5adSChuck Lever * standing RPC requests, using a smoothed version of the congestion 4354f4cf5adSChuck Lever * avoidance implemented in 44BSD. This is basically the Van Jacobson 4364f4cf5adSChuck Lever * congestion algorithm: If a retransmit occurs, the congestion window is 4374f4cf5adSChuck Lever * halved; otherwise, it is incremented by 1/cwnd when 4384f4cf5adSChuck Lever * 4394f4cf5adSChuck Lever * - a reply is received and 4404f4cf5adSChuck Lever * - a full number of requests are outstanding and 4414f4cf5adSChuck Lever * - the congestion window hasn't been updated recently. 4421da177e4SLinus Torvalds */ 4436a24dfb6STrond Myklebust void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result) 4441da177e4SLinus Torvalds { 44546c0ee8bSChuck Lever struct rpc_rqst *req = task->tk_rqstp; 44646c0ee8bSChuck Lever unsigned long cwnd = xprt->cwnd; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds if (result >= 0 && cwnd <= xprt->cong) { 4491da177e4SLinus Torvalds /* The (cwnd >> 1) term makes sure 4501da177e4SLinus Torvalds * the result gets rounded properly. */ 4511da177e4SLinus Torvalds cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 4521da177e4SLinus Torvalds if (cwnd > RPC_MAXCWND(xprt)) 4531da177e4SLinus Torvalds cwnd = RPC_MAXCWND(xprt); 45449e9a890SChuck Lever __xprt_lock_write_next_cong(xprt); 4551da177e4SLinus Torvalds } else if (result == -ETIMEDOUT) { 4561da177e4SLinus Torvalds cwnd >>= 1; 4571da177e4SLinus Torvalds if (cwnd < RPC_CWNDSCALE) 4581da177e4SLinus Torvalds cwnd = RPC_CWNDSCALE; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 4611da177e4SLinus Torvalds xprt->cong, xprt->cwnd, cwnd); 4621da177e4SLinus Torvalds xprt->cwnd = cwnd; 46346c0ee8bSChuck Lever __xprt_put_cong(xprt, req); 4641da177e4SLinus Torvalds } 46512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_adjust_cwnd); 4661da177e4SLinus Torvalds 46744fbac22SChuck Lever /** 46844fbac22SChuck Lever * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 46944fbac22SChuck Lever * @xprt: transport with waiting tasks 47044fbac22SChuck Lever * @status: result code to plant in each task before waking it 47144fbac22SChuck Lever * 47244fbac22SChuck Lever */ 47344fbac22SChuck Lever void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 47444fbac22SChuck Lever { 47544fbac22SChuck Lever if (status < 0) 47644fbac22SChuck Lever rpc_wake_up_status(&xprt->pending, status); 47744fbac22SChuck Lever else 47844fbac22SChuck Lever rpc_wake_up(&xprt->pending); 47944fbac22SChuck Lever } 48012444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks); 48144fbac22SChuck Lever 482c7b2cae8SChuck Lever /** 483c7b2cae8SChuck Lever * xprt_wait_for_buffer_space - wait for transport output buffer to clear 484c7b2cae8SChuck Lever * @task: task to be put to sleep 4850b80ae42SRandy Dunlap * @action: function pointer to be executed after wait 486a9a6b52eSTrond Myklebust * 487a9a6b52eSTrond Myklebust * Note that we only set the timer for the case of RPC_IS_SOFT(), since 488a9a6b52eSTrond Myklebust * we don't in general want to force a socket disconnection due to 489a9a6b52eSTrond Myklebust * an incomplete RPC call transmission. 490c7b2cae8SChuck Lever */ 491b6ddf64fSTrond Myklebust void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action) 492c7b2cae8SChuck Lever { 493c7b2cae8SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 494c7b2cae8SChuck Lever struct rpc_xprt *xprt = req->rq_xprt; 495c7b2cae8SChuck Lever 496a9a6b52eSTrond Myklebust task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0; 497b6ddf64fSTrond Myklebust rpc_sleep_on(&xprt->pending, task, action); 498c7b2cae8SChuck Lever } 49912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space); 500c7b2cae8SChuck Lever 501c7b2cae8SChuck Lever /** 502c7b2cae8SChuck Lever * xprt_write_space - wake the task waiting for transport output buffer space 503c7b2cae8SChuck Lever * @xprt: transport with waiting tasks 504c7b2cae8SChuck Lever * 505c7b2cae8SChuck Lever * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 506c7b2cae8SChuck Lever */ 507c7b2cae8SChuck Lever void xprt_write_space(struct rpc_xprt *xprt) 508c7b2cae8SChuck Lever { 509c7b2cae8SChuck Lever spin_lock_bh(&xprt->transport_lock); 510c7b2cae8SChuck Lever if (xprt->snd_task) { 51146121cf7SChuck Lever dprintk("RPC: write space: waking waiting task on " 51246121cf7SChuck Lever "xprt %p\n", xprt); 513fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task); 514c7b2cae8SChuck Lever } 515c7b2cae8SChuck Lever spin_unlock_bh(&xprt->transport_lock); 516c7b2cae8SChuck Lever } 51712444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_write_space); 518c7b2cae8SChuck Lever 519fe3aca29SChuck Lever /** 520fe3aca29SChuck Lever * xprt_set_retrans_timeout_def - set a request's retransmit timeout 521fe3aca29SChuck Lever * @task: task whose timeout is to be set 522fe3aca29SChuck Lever * 523fe3aca29SChuck Lever * Set a request's retransmit timeout based on the transport's 524fe3aca29SChuck Lever * default timeout parameters. Used by transports that don't adjust 525fe3aca29SChuck Lever * the retransmit timeout based on round-trip time estimation. 526fe3aca29SChuck Lever */ 527fe3aca29SChuck Lever void xprt_set_retrans_timeout_def(struct rpc_task *task) 528fe3aca29SChuck Lever { 529fe3aca29SChuck Lever task->tk_timeout = task->tk_rqstp->rq_timeout; 530fe3aca29SChuck Lever } 53112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def); 532fe3aca29SChuck Lever 5332c53040fSBen Hutchings /** 534fe3aca29SChuck Lever * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout 535fe3aca29SChuck Lever * @task: task whose timeout is to be set 536fe3aca29SChuck Lever * 537fe3aca29SChuck Lever * Set a request's retransmit timeout using the RTT estimator. 538fe3aca29SChuck Lever */ 539fe3aca29SChuck Lever void xprt_set_retrans_timeout_rtt(struct rpc_task *task) 540fe3aca29SChuck Lever { 541fe3aca29SChuck Lever int timer = task->tk_msg.rpc_proc->p_timer; 542ba7392bbSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 543ba7392bbSTrond Myklebust struct rpc_rtt *rtt = clnt->cl_rtt; 544fe3aca29SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 545ba7392bbSTrond Myklebust unsigned long max_timeout = clnt->cl_timeout->to_maxval; 546fe3aca29SChuck Lever 547fe3aca29SChuck Lever task->tk_timeout = rpc_calc_rto(rtt, timer); 548fe3aca29SChuck Lever task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 549fe3aca29SChuck Lever if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 550fe3aca29SChuck Lever task->tk_timeout = max_timeout; 551fe3aca29SChuck Lever } 55212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt); 553fe3aca29SChuck Lever 5541da177e4SLinus Torvalds static void xprt_reset_majortimeo(struct rpc_rqst *req) 5551da177e4SLinus Torvalds { 556ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds req->rq_majortimeo = req->rq_timeout; 5591da177e4SLinus Torvalds if (to->to_exponential) 5601da177e4SLinus Torvalds req->rq_majortimeo <<= to->to_retries; 5611da177e4SLinus Torvalds else 5621da177e4SLinus Torvalds req->rq_majortimeo += to->to_increment * to->to_retries; 5631da177e4SLinus Torvalds if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 5641da177e4SLinus Torvalds req->rq_majortimeo = to->to_maxval; 5651da177e4SLinus Torvalds req->rq_majortimeo += jiffies; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 5689903cd1cSChuck Lever /** 5699903cd1cSChuck Lever * xprt_adjust_timeout - adjust timeout values for next retransmit 5709903cd1cSChuck Lever * @req: RPC request containing parameters to use for the adjustment 5719903cd1cSChuck Lever * 5721da177e4SLinus Torvalds */ 5731da177e4SLinus Torvalds int xprt_adjust_timeout(struct rpc_rqst *req) 5741da177e4SLinus Torvalds { 5751da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 576ba7392bbSTrond Myklebust const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout; 5771da177e4SLinus Torvalds int status = 0; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds if (time_before(jiffies, req->rq_majortimeo)) { 5801da177e4SLinus Torvalds if (to->to_exponential) 5811da177e4SLinus Torvalds req->rq_timeout <<= 1; 5821da177e4SLinus Torvalds else 5831da177e4SLinus Torvalds req->rq_timeout += to->to_increment; 5841da177e4SLinus Torvalds if (to->to_maxval && req->rq_timeout >= to->to_maxval) 5851da177e4SLinus Torvalds req->rq_timeout = to->to_maxval; 5861da177e4SLinus Torvalds req->rq_retries++; 5871da177e4SLinus Torvalds } else { 5881da177e4SLinus Torvalds req->rq_timeout = to->to_initval; 5891da177e4SLinus Torvalds req->rq_retries = 0; 5901da177e4SLinus Torvalds xprt_reset_majortimeo(req); 5911da177e4SLinus Torvalds /* Reset the RTT counters == "slow start" */ 5924a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 5931da177e4SLinus Torvalds rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 5944a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 5951da177e4SLinus Torvalds status = -ETIMEDOUT; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds if (req->rq_timeout == 0) { 5991da177e4SLinus Torvalds printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 6001da177e4SLinus Torvalds req->rq_timeout = 5 * HZ; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds return status; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 60565f27f38SDavid Howells static void xprt_autoclose(struct work_struct *work) 6061da177e4SLinus Torvalds { 60765f27f38SDavid Howells struct rpc_xprt *xprt = 60865f27f38SDavid Howells container_of(work, struct rpc_xprt, task_cleanup); 6091da177e4SLinus Torvalds 610a246b010SChuck Lever xprt->ops->close(xprt); 61166af1e55STrond Myklebust clear_bit(XPRT_CLOSE_WAIT, &xprt->state); 6121da177e4SLinus Torvalds xprt_release_write(xprt, NULL); 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 6159903cd1cSChuck Lever /** 61662da3b24STrond Myklebust * xprt_disconnect_done - mark a transport as disconnected 6179903cd1cSChuck Lever * @xprt: transport to flag for disconnect 6189903cd1cSChuck Lever * 6191da177e4SLinus Torvalds */ 62062da3b24STrond Myklebust void xprt_disconnect_done(struct rpc_xprt *xprt) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds dprintk("RPC: disconnected transport %p\n", xprt); 6234a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 6241da177e4SLinus Torvalds xprt_clear_connected(xprt); 6252a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6264a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 6271da177e4SLinus Torvalds } 62862da3b24STrond Myklebust EXPORT_SYMBOL_GPL(xprt_disconnect_done); 6291da177e4SLinus Torvalds 63066af1e55STrond Myklebust /** 63166af1e55STrond Myklebust * xprt_force_disconnect - force a transport to disconnect 63266af1e55STrond Myklebust * @xprt: transport to disconnect 63366af1e55STrond Myklebust * 63466af1e55STrond Myklebust */ 63566af1e55STrond Myklebust void xprt_force_disconnect(struct rpc_xprt *xprt) 63666af1e55STrond Myklebust { 63766af1e55STrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 63866af1e55STrond Myklebust spin_lock_bh(&xprt->transport_lock); 63966af1e55STrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 64066af1e55STrond Myklebust /* Try to schedule an autoclose RPC call */ 64166af1e55STrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 64266af1e55STrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6432a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 64466af1e55STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 64566af1e55STrond Myklebust } 64666af1e55STrond Myklebust 6477c1d71cfSTrond Myklebust /** 6487c1d71cfSTrond Myklebust * xprt_conditional_disconnect - force a transport to disconnect 6497c1d71cfSTrond Myklebust * @xprt: transport to disconnect 6507c1d71cfSTrond Myklebust * @cookie: 'connection cookie' 6517c1d71cfSTrond Myklebust * 6527c1d71cfSTrond Myklebust * This attempts to break the connection if and only if 'cookie' matches 6537c1d71cfSTrond Myklebust * the current transport 'connection cookie'. It ensures that we don't 6547c1d71cfSTrond Myklebust * try to break the connection more than once when we need to retransmit 6557c1d71cfSTrond Myklebust * a batch of RPC requests. 6567c1d71cfSTrond Myklebust * 6577c1d71cfSTrond Myklebust */ 6587c1d71cfSTrond Myklebust void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie) 6597c1d71cfSTrond Myklebust { 6607c1d71cfSTrond Myklebust /* Don't race with the test_bit() in xprt_clear_locked() */ 6617c1d71cfSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 6627c1d71cfSTrond Myklebust if (cookie != xprt->connect_cookie) 6637c1d71cfSTrond Myklebust goto out; 6647c1d71cfSTrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt)) 6657c1d71cfSTrond Myklebust goto out; 6667c1d71cfSTrond Myklebust set_bit(XPRT_CLOSE_WAIT, &xprt->state); 6677c1d71cfSTrond Myklebust /* Try to schedule an autoclose RPC call */ 6687c1d71cfSTrond Myklebust if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0) 6697c1d71cfSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6702a491991STrond Myklebust xprt_wake_pending_tasks(xprt, -EAGAIN); 6717c1d71cfSTrond Myklebust out: 6727c1d71cfSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 6737c1d71cfSTrond Myklebust } 6747c1d71cfSTrond Myklebust 6751da177e4SLinus Torvalds static void 6761da177e4SLinus Torvalds xprt_init_autodisconnect(unsigned long data) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds struct rpc_xprt *xprt = (struct rpc_xprt *)data; 6791da177e4SLinus Torvalds 6804a0f8c04SChuck Lever spin_lock(&xprt->transport_lock); 681d19751e7STrond Myklebust if (!list_empty(&xprt->recv)) 6821da177e4SLinus Torvalds goto out_abort; 6832226feb6SChuck Lever if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 6841da177e4SLinus Torvalds goto out_abort; 6854a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 686c1384c9cSTrond Myklebust queue_work(rpciod_workqueue, &xprt->task_cleanup); 6871da177e4SLinus Torvalds return; 6881da177e4SLinus Torvalds out_abort: 6894a0f8c04SChuck Lever spin_unlock(&xprt->transport_lock); 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 692718ba5b8STrond Myklebust bool xprt_lock_connect(struct rpc_xprt *xprt, 693718ba5b8STrond Myklebust struct rpc_task *task, 694718ba5b8STrond Myklebust void *cookie) 695718ba5b8STrond Myklebust { 696718ba5b8STrond Myklebust bool ret = false; 697718ba5b8STrond Myklebust 698718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 699718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 700718ba5b8STrond Myklebust goto out; 701718ba5b8STrond Myklebust if (xprt->snd_task != task) 702718ba5b8STrond Myklebust goto out; 703718ba5b8STrond Myklebust xprt->snd_task = cookie; 704718ba5b8STrond Myklebust ret = true; 705718ba5b8STrond Myklebust out: 706718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 707718ba5b8STrond Myklebust return ret; 708718ba5b8STrond Myklebust } 709718ba5b8STrond Myklebust 710718ba5b8STrond Myklebust void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie) 711718ba5b8STrond Myklebust { 712718ba5b8STrond Myklebust spin_lock_bh(&xprt->transport_lock); 713718ba5b8STrond Myklebust if (xprt->snd_task != cookie) 714718ba5b8STrond Myklebust goto out; 715718ba5b8STrond Myklebust if (!test_bit(XPRT_LOCKED, &xprt->state)) 716718ba5b8STrond Myklebust goto out; 717718ba5b8STrond Myklebust xprt->snd_task =NULL; 718718ba5b8STrond Myklebust xprt->ops->release_xprt(xprt, NULL); 719718ba5b8STrond Myklebust out: 720718ba5b8STrond Myklebust spin_unlock_bh(&xprt->transport_lock); 721718ba5b8STrond Myklebust } 722718ba5b8STrond Myklebust 7239903cd1cSChuck Lever /** 7249903cd1cSChuck Lever * xprt_connect - schedule a transport connect operation 7259903cd1cSChuck Lever * @task: RPC task that is requesting the connect 7261da177e4SLinus Torvalds * 7271da177e4SLinus Torvalds */ 7281da177e4SLinus Torvalds void xprt_connect(struct rpc_task *task) 7291da177e4SLinus Torvalds { 730ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7311da177e4SLinus Torvalds 73246121cf7SChuck Lever dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid, 7331da177e4SLinus Torvalds xprt, (xprt_connected(xprt) ? "is" : "is not")); 7341da177e4SLinus Torvalds 735ec739ef0SChuck Lever if (!xprt_bound(xprt)) { 73601d37c42STrond Myklebust task->tk_status = -EAGAIN; 7371da177e4SLinus Torvalds return; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds if (!xprt_lock_write(xprt, task)) 7401da177e4SLinus Torvalds return; 741feb8ca37STrond Myklebust 742feb8ca37STrond Myklebust if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state)) 743feb8ca37STrond Myklebust xprt->ops->close(xprt); 744feb8ca37STrond Myklebust 745718ba5b8STrond Myklebust if (!xprt_connected(xprt)) { 7461da177e4SLinus Torvalds task->tk_rqstp->rq_bytes_sent = 0; 747a8ce4a8fSTrond Myklebust task->tk_timeout = task->tk_rqstp->rq_timeout; 7485d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_connect_status); 7490b9e7943STrond Myklebust 7500b9e7943STrond Myklebust if (test_bit(XPRT_CLOSING, &xprt->state)) 7510b9e7943STrond Myklebust return; 7520b9e7943STrond Myklebust if (xprt_test_and_set_connecting(xprt)) 7530b9e7943STrond Myklebust return; 754262ca07dSChuck Lever xprt->stat.connect_start = jiffies; 7551b092092STrond Myklebust xprt->ops->connect(xprt, task); 7561da177e4SLinus Torvalds } 757718ba5b8STrond Myklebust xprt_release_write(xprt, task); 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds 7609903cd1cSChuck Lever static void xprt_connect_status(struct rpc_task *task) 7611da177e4SLinus Torvalds { 762ad2368d6STrond Myklebust struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt; 7631da177e4SLinus Torvalds 764cd983ef8SChuck Lever if (task->tk_status == 0) { 765262ca07dSChuck Lever xprt->stat.connect_count++; 766262ca07dSChuck Lever xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 76746121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connection established\n", 7681da177e4SLinus Torvalds task->tk_pid); 7691da177e4SLinus Torvalds return; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds switch (task->tk_status) { 7730fe8d04eSTrond Myklebust case -ECONNREFUSED: 7740fe8d04eSTrond Myklebust case -ECONNRESET: 7750fe8d04eSTrond Myklebust case -ECONNABORTED: 7760fe8d04eSTrond Myklebust case -ENETUNREACH: 7770fe8d04eSTrond Myklebust case -EHOSTUNREACH: 7782fc193cfSTrond Myklebust case -EPIPE: 7792a491991STrond Myklebust case -EAGAIN: 7802a491991STrond Myklebust dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid); 78123475d66SChuck Lever break; 7821da177e4SLinus Torvalds case -ETIMEDOUT: 78346121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: connect attempt timed " 78446121cf7SChuck Lever "out\n", task->tk_pid); 7851da177e4SLinus Torvalds break; 7861da177e4SLinus Torvalds default: 78746121cf7SChuck Lever dprintk("RPC: %5u xprt_connect_status: error %d connecting to " 78846121cf7SChuck Lever "server %s\n", task->tk_pid, -task->tk_status, 7894e0038b6STrond Myklebust xprt->servername); 79023475d66SChuck Lever task->tk_status = -EIO; 79123475d66SChuck Lever } 7921da177e4SLinus Torvalds } 7931da177e4SLinus Torvalds 7949903cd1cSChuck Lever /** 7959903cd1cSChuck Lever * xprt_lookup_rqst - find an RPC request corresponding to an XID 7969903cd1cSChuck Lever * @xprt: transport on which the original request was transmitted 7979903cd1cSChuck Lever * @xid: RPC XID of incoming reply 7989903cd1cSChuck Lever * 7991da177e4SLinus Torvalds */ 800d8ed029dSAlexey Dobriyan struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid) 8011da177e4SLinus Torvalds { 8028f3a6de3SPavel Emelyanov struct rpc_rqst *entry; 8031da177e4SLinus Torvalds 8048f3a6de3SPavel Emelyanov list_for_each_entry(entry, &xprt->recv, rq_list) 8053705ad64SJeff Layton if (entry->rq_xid == xid) { 8063705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, 0); 807262ca07dSChuck Lever return entry; 8083705ad64SJeff Layton } 80946121cf7SChuck Lever 81046121cf7SChuck Lever dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n", 81146121cf7SChuck Lever ntohl(xid)); 8123705ad64SJeff Layton trace_xprt_lookup_rqst(xprt, xid, -ENOENT); 813262ca07dSChuck Lever xprt->stat.bad_xids++; 814262ca07dSChuck Lever return NULL; 8151da177e4SLinus Torvalds } 81612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_lookup_rqst); 8171da177e4SLinus Torvalds 818bbc72ceaSChuck Lever static void xprt_update_rtt(struct rpc_task *task) 8191da177e4SLinus Torvalds { 8201570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 8211570c1e4SChuck Lever struct rpc_rtt *rtt = task->tk_client->cl_rtt; 82295c96174SEric Dumazet unsigned int timer = task->tk_msg.rpc_proc->p_timer; 823d60dbb20STrond Myklebust long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt)); 8241570c1e4SChuck Lever 8251da177e4SLinus Torvalds if (timer) { 8261da177e4SLinus Torvalds if (req->rq_ntrans == 1) 827ff839970SChuck Lever rpc_update_rtt(rtt, timer, m); 8281570c1e4SChuck Lever rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321570c1e4SChuck Lever /** 8331570c1e4SChuck Lever * xprt_complete_rqst - called when reply processing is complete 8341570c1e4SChuck Lever * @task: RPC request that recently completed 8351570c1e4SChuck Lever * @copied: actual number of bytes received from the transport 8361570c1e4SChuck Lever * 8371570c1e4SChuck Lever * Caller holds transport lock. 8381570c1e4SChuck Lever */ 8391570c1e4SChuck Lever void xprt_complete_rqst(struct rpc_task *task, int copied) 8401570c1e4SChuck Lever { 8411570c1e4SChuck Lever struct rpc_rqst *req = task->tk_rqstp; 842fda13939STrond Myklebust struct rpc_xprt *xprt = req->rq_xprt; 8431da177e4SLinus Torvalds 8441570c1e4SChuck Lever dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 8451570c1e4SChuck Lever task->tk_pid, ntohl(req->rq_xid), copied); 8463705ad64SJeff Layton trace_xprt_complete_rqst(xprt, req->rq_xid, copied); 8471da177e4SLinus Torvalds 848fda13939STrond Myklebust xprt->stat.recvs++; 849d60dbb20STrond Myklebust req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime); 850bbc72ceaSChuck Lever if (xprt->ops->timer != NULL) 851bbc72ceaSChuck Lever xprt_update_rtt(task); 852ef759a2eSChuck Lever 8531da177e4SLinus Torvalds list_del_init(&req->rq_list); 8541e799b67STrond Myklebust req->rq_private_buf.len = copied; 855dd2b63d0SRicardo Labiaga /* Ensure all writes are done before we update */ 856dd2b63d0SRicardo Labiaga /* req->rq_reply_bytes_recvd */ 85743ac3f29STrond Myklebust smp_wmb(); 858dd2b63d0SRicardo Labiaga req->rq_reply_bytes_recvd = copied; 859fda13939STrond Myklebust rpc_wake_up_queued_task(&xprt->pending, task); 8601da177e4SLinus Torvalds } 86112444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(xprt_complete_rqst); 8621da177e4SLinus Torvalds 86346c0ee8bSChuck Lever static void xprt_timer(struct rpc_task *task) 8641da177e4SLinus Torvalds { 8651da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8661da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 8671da177e4SLinus Torvalds 8685d00837bSTrond Myklebust if (task->tk_status != -ETIMEDOUT) 8695d00837bSTrond Myklebust return; 87046121cf7SChuck Lever dprintk("RPC: %5u xprt_timer\n", task->tk_pid); 87146c0ee8bSChuck Lever 8725d00837bSTrond Myklebust spin_lock_bh(&xprt->transport_lock); 873dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 87446c0ee8bSChuck Lever if (xprt->ops->timer) 8756a24dfb6STrond Myklebust xprt->ops->timer(xprt, task); 8765d00837bSTrond Myklebust } else 8775d00837bSTrond Myklebust task->tk_status = 0; 8785d00837bSTrond Myklebust spin_unlock_bh(&xprt->transport_lock); 8791da177e4SLinus Torvalds } 8801da177e4SLinus Torvalds 8814cfc7e60SRahul Iyer static inline int xprt_has_timer(struct rpc_xprt *xprt) 8824cfc7e60SRahul Iyer { 8834cfc7e60SRahul Iyer return xprt->idle_timeout != 0; 8844cfc7e60SRahul Iyer } 8854cfc7e60SRahul Iyer 8869903cd1cSChuck Lever /** 8879903cd1cSChuck Lever * xprt_prepare_transmit - reserve the transport before sending a request 8889903cd1cSChuck Lever * @task: RPC task about to send a request 8899903cd1cSChuck Lever * 8901da177e4SLinus Torvalds */ 89190051ea7STrond Myklebust bool xprt_prepare_transmit(struct rpc_task *task) 8921da177e4SLinus Torvalds { 8931da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 8941da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 89590051ea7STrond Myklebust bool ret = false; 8961da177e4SLinus Torvalds 89746121cf7SChuck Lever dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 8981da177e4SLinus Torvalds 8994a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 9008a19a0b6STrond Myklebust if (!req->rq_bytes_sent) { 9018a19a0b6STrond Myklebust if (req->rq_reply_bytes_recvd) { 90290051ea7STrond Myklebust task->tk_status = req->rq_reply_bytes_recvd; 9031da177e4SLinus Torvalds goto out_unlock; 9041da177e4SLinus Torvalds } 9058a19a0b6STrond Myklebust if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) 9068a19a0b6STrond Myklebust && xprt_connected(xprt) 9078a19a0b6STrond Myklebust && req->rq_connect_cookie == xprt->connect_cookie) { 9088a19a0b6STrond Myklebust xprt->ops->set_retrans_timeout(task); 9098a19a0b6STrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 9108a19a0b6STrond Myklebust goto out_unlock; 9118a19a0b6STrond Myklebust } 9128a19a0b6STrond Myklebust } 91390051ea7STrond Myklebust if (!xprt->ops->reserve_xprt(xprt, task)) { 91490051ea7STrond Myklebust task->tk_status = -EAGAIN; 91590051ea7STrond Myklebust goto out_unlock; 91690051ea7STrond Myklebust } 91790051ea7STrond Myklebust ret = true; 9181da177e4SLinus Torvalds out_unlock: 9194a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 92090051ea7STrond Myklebust return ret; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 923e0ab53deSTrond Myklebust void xprt_end_transmit(struct rpc_task *task) 9245e5ce5beSTrond Myklebust { 925343952faSRahul Iyer xprt_release_write(task->tk_rqstp->rq_xprt, task); 9265e5ce5beSTrond Myklebust } 9275e5ce5beSTrond Myklebust 9289903cd1cSChuck Lever /** 9299903cd1cSChuck Lever * xprt_transmit - send an RPC request on a transport 9309903cd1cSChuck Lever * @task: controlling RPC task 9319903cd1cSChuck Lever * 9329903cd1cSChuck Lever * We have to copy the iovec because sendmsg fiddles with its contents. 9339903cd1cSChuck Lever */ 9349903cd1cSChuck Lever void xprt_transmit(struct rpc_task *task) 9351da177e4SLinus Torvalds { 9361da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 9371da177e4SLinus Torvalds struct rpc_xprt *xprt = req->rq_xprt; 93815a45206SAndy Adamson int status, numreqs; 9391da177e4SLinus Torvalds 94046121cf7SChuck Lever dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); 9411da177e4SLinus Torvalds 942dd2b63d0SRicardo Labiaga if (!req->rq_reply_bytes_recvd) { 94355ae1aabSRicardo Labiaga if (list_empty(&req->rq_list) && rpc_reply_expected(task)) { 94455ae1aabSRicardo Labiaga /* 94555ae1aabSRicardo Labiaga * Add to the list only if we're expecting a reply 94655ae1aabSRicardo Labiaga */ 9474a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 9481da177e4SLinus Torvalds /* Update the softirq receive buffer */ 9491da177e4SLinus Torvalds memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 9501da177e4SLinus Torvalds sizeof(req->rq_private_buf)); 9511da177e4SLinus Torvalds /* Add request to the receive list */ 9521da177e4SLinus Torvalds list_add_tail(&req->rq_list, &xprt->recv); 9534a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 9541da177e4SLinus Torvalds xprt_reset_majortimeo(req); 9550f9dc2b1STrond Myklebust /* Turn off autodisconnect */ 9560f9dc2b1STrond Myklebust del_singleshot_timer_sync(&xprt->timer); 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds } else if (!req->rq_bytes_sent) 9591da177e4SLinus Torvalds return; 9601da177e4SLinus Torvalds 961ff839970SChuck Lever req->rq_xtime = ktime_get(); 962a246b010SChuck Lever status = xprt->ops->send_request(task); 9633705ad64SJeff Layton trace_xprt_transmit(xprt, req->rq_xid, status); 964c8485e4dSTrond Myklebust if (status != 0) { 965c8485e4dSTrond Myklebust task->tk_status = status; 966c8485e4dSTrond Myklebust return; 967c8485e4dSTrond Myklebust } 968c8485e4dSTrond Myklebust 96946121cf7SChuck Lever dprintk("RPC: %5u xmit complete\n", task->tk_pid); 970468f8613SBryan Schumaker task->tk_flags |= RPC_TASK_SENT; 971fe3aca29SChuck Lever spin_lock_bh(&xprt->transport_lock); 972262ca07dSChuck Lever 973fe3aca29SChuck Lever xprt->ops->set_retrans_timeout(task); 974262ca07dSChuck Lever 97515a45206SAndy Adamson numreqs = atomic_read(&xprt->num_reqs); 97615a45206SAndy Adamson if (numreqs > xprt->stat.max_slots) 97715a45206SAndy Adamson xprt->stat.max_slots = numreqs; 978262ca07dSChuck Lever xprt->stat.sends++; 979262ca07dSChuck Lever xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 980262ca07dSChuck Lever xprt->stat.bklog_u += xprt->backlog.qlen; 98115a45206SAndy Adamson xprt->stat.sending_u += xprt->sending.qlen; 98215a45206SAndy Adamson xprt->stat.pending_u += xprt->pending.qlen; 983262ca07dSChuck Lever 984fe3aca29SChuck Lever /* Don't race with disconnect */ 985fe3aca29SChuck Lever if (!xprt_connected(xprt)) 986fe3aca29SChuck Lever task->tk_status = -ENOTCONN; 9870a660521STrond Myklebust else { 98855ae1aabSRicardo Labiaga /* 98955ae1aabSRicardo Labiaga * Sleep on the pending queue since 99055ae1aabSRicardo Labiaga * we're expecting a reply. 99155ae1aabSRicardo Labiaga */ 9920a660521STrond Myklebust if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) 9935d00837bSTrond Myklebust rpc_sleep_on(&xprt->pending, task, xprt_timer); 9940a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie; 99555ae1aabSRicardo Labiaga } 996fe3aca29SChuck Lever spin_unlock_bh(&xprt->transport_lock); 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds 999ba60eb25STrond Myklebust static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task) 1000ba60eb25STrond Myklebust { 1001ba60eb25STrond Myklebust set_bit(XPRT_CONGESTED, &xprt->state); 1002ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1003ba60eb25STrond Myklebust } 1004ba60eb25STrond Myklebust 1005ba60eb25STrond Myklebust static void xprt_wake_up_backlog(struct rpc_xprt *xprt) 1006ba60eb25STrond Myklebust { 1007ba60eb25STrond Myklebust if (rpc_wake_up_next(&xprt->backlog) == NULL) 1008ba60eb25STrond Myklebust clear_bit(XPRT_CONGESTED, &xprt->state); 1009ba60eb25STrond Myklebust } 1010ba60eb25STrond Myklebust 1011ba60eb25STrond Myklebust static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task) 1012ba60eb25STrond Myklebust { 1013ba60eb25STrond Myklebust bool ret = false; 1014ba60eb25STrond Myklebust 1015ba60eb25STrond Myklebust if (!test_bit(XPRT_CONGESTED, &xprt->state)) 1016ba60eb25STrond Myklebust goto out; 1017ba60eb25STrond Myklebust spin_lock(&xprt->reserve_lock); 1018ba60eb25STrond Myklebust if (test_bit(XPRT_CONGESTED, &xprt->state)) { 1019ba60eb25STrond Myklebust rpc_sleep_on(&xprt->backlog, task, NULL); 1020ba60eb25STrond Myklebust ret = true; 1021ba60eb25STrond Myklebust } 1022ba60eb25STrond Myklebust spin_unlock(&xprt->reserve_lock); 1023ba60eb25STrond Myklebust out: 1024ba60eb25STrond Myklebust return ret; 1025ba60eb25STrond Myklebust } 1026ba60eb25STrond Myklebust 1027d9ba131dSTrond Myklebust static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags) 1028d9ba131dSTrond Myklebust { 1029d9ba131dSTrond Myklebust struct rpc_rqst *req = ERR_PTR(-EAGAIN); 1030d9ba131dSTrond Myklebust 1031d9ba131dSTrond Myklebust if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs)) 1032d9ba131dSTrond Myklebust goto out; 1033d9ba131dSTrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), gfp_flags); 1034d9ba131dSTrond Myklebust if (req != NULL) 1035d9ba131dSTrond Myklebust goto out; 1036d9ba131dSTrond Myklebust atomic_dec(&xprt->num_reqs); 1037d9ba131dSTrond Myklebust req = ERR_PTR(-ENOMEM); 1038d9ba131dSTrond Myklebust out: 1039d9ba131dSTrond Myklebust return req; 1040d9ba131dSTrond Myklebust } 1041d9ba131dSTrond Myklebust 1042d9ba131dSTrond Myklebust static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1043d9ba131dSTrond Myklebust { 1044d9ba131dSTrond Myklebust if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) { 1045d9ba131dSTrond Myklebust kfree(req); 1046d9ba131dSTrond Myklebust return true; 1047d9ba131dSTrond Myklebust } 1048d9ba131dSTrond Myklebust return false; 1049d9ba131dSTrond Myklebust } 1050d9ba131dSTrond Myklebust 1051f39c1bfbSTrond Myklebust void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 10521da177e4SLinus Torvalds { 1053d9ba131dSTrond Myklebust struct rpc_rqst *req; 10541da177e4SLinus Torvalds 1055f39c1bfbSTrond Myklebust spin_lock(&xprt->reserve_lock); 10561da177e4SLinus Torvalds if (!list_empty(&xprt->free)) { 1057d9ba131dSTrond Myklebust req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 1058d9ba131dSTrond Myklebust list_del(&req->rq_list); 1059d9ba131dSTrond Myklebust goto out_init_req; 1060d9ba131dSTrond Myklebust } 10616b343099SJeff Layton req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT|__GFP_NOWARN); 1062d9ba131dSTrond Myklebust if (!IS_ERR(req)) 1063d9ba131dSTrond Myklebust goto out_init_req; 1064d9ba131dSTrond Myklebust switch (PTR_ERR(req)) { 1065d9ba131dSTrond Myklebust case -ENOMEM: 1066d9ba131dSTrond Myklebust dprintk("RPC: dynamic allocation of request slot " 1067d9ba131dSTrond Myklebust "failed! Retrying\n"); 10681afeaf5cSTrond Myklebust task->tk_status = -ENOMEM; 1069d9ba131dSTrond Myklebust break; 1070d9ba131dSTrond Myklebust case -EAGAIN: 1071ba60eb25STrond Myklebust xprt_add_backlog(xprt, task); 1072d9ba131dSTrond Myklebust dprintk("RPC: waiting for request slot\n"); 10731afeaf5cSTrond Myklebust default: 1074d9ba131dSTrond Myklebust task->tk_status = -EAGAIN; 10751afeaf5cSTrond Myklebust } 1076f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 1077d9ba131dSTrond Myklebust return; 1078d9ba131dSTrond Myklebust out_init_req: 1079d9ba131dSTrond Myklebust task->tk_status = 0; 10801da177e4SLinus Torvalds task->tk_rqstp = req; 10811da177e4SLinus Torvalds xprt_request_init(task, xprt); 1082f39c1bfbSTrond Myklebust spin_unlock(&xprt->reserve_lock); 10831da177e4SLinus Torvalds } 1084f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_alloc_slot); 1085f39c1bfbSTrond Myklebust 1086f39c1bfbSTrond Myklebust void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task) 1087f39c1bfbSTrond Myklebust { 1088f39c1bfbSTrond Myklebust /* Note: grabbing the xprt_lock_write() ensures that we throttle 1089f39c1bfbSTrond Myklebust * new slot allocation if the transport is congested (i.e. when 1090f39c1bfbSTrond Myklebust * reconnecting a stream transport or when out of socket write 1091f39c1bfbSTrond Myklebust * buffer space). 1092f39c1bfbSTrond Myklebust */ 1093f39c1bfbSTrond Myklebust if (xprt_lock_write(xprt, task)) { 1094f39c1bfbSTrond Myklebust xprt_alloc_slot(xprt, task); 1095f39c1bfbSTrond Myklebust xprt_release_write(xprt, task); 1096f39c1bfbSTrond Myklebust } 1097f39c1bfbSTrond Myklebust } 1098f39c1bfbSTrond Myklebust EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot); 10991da177e4SLinus Torvalds 1100ee5ebe85STrond Myklebust static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req) 1101ee5ebe85STrond Myklebust { 1102ee5ebe85STrond Myklebust spin_lock(&xprt->reserve_lock); 1103c25573b5STrond Myklebust if (!xprt_dynamic_free_slot(xprt, req)) { 1104c25573b5STrond Myklebust memset(req, 0, sizeof(*req)); /* mark unused */ 1105ee5ebe85STrond Myklebust list_add(&req->rq_list, &xprt->free); 1106c25573b5STrond Myklebust } 1107ba60eb25STrond Myklebust xprt_wake_up_backlog(xprt); 1108ee5ebe85STrond Myklebust spin_unlock(&xprt->reserve_lock); 1109ee5ebe85STrond Myklebust } 1110ee5ebe85STrond Myklebust 111121de0a95STrond Myklebust static void xprt_free_all_slots(struct rpc_xprt *xprt) 111221de0a95STrond Myklebust { 111321de0a95STrond Myklebust struct rpc_rqst *req; 111421de0a95STrond Myklebust while (!list_empty(&xprt->free)) { 111521de0a95STrond Myklebust req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list); 111621de0a95STrond Myklebust list_del(&req->rq_list); 111721de0a95STrond Myklebust kfree(req); 111821de0a95STrond Myklebust } 111921de0a95STrond Myklebust } 112021de0a95STrond Myklebust 1121d9ba131dSTrond Myklebust struct rpc_xprt *xprt_alloc(struct net *net, size_t size, 1122d9ba131dSTrond Myklebust unsigned int num_prealloc, 1123d9ba131dSTrond Myklebust unsigned int max_alloc) 1124bd1722d4SPavel Emelyanov { 1125bd1722d4SPavel Emelyanov struct rpc_xprt *xprt; 112621de0a95STrond Myklebust struct rpc_rqst *req; 112721de0a95STrond Myklebust int i; 1128bd1722d4SPavel Emelyanov 1129bd1722d4SPavel Emelyanov xprt = kzalloc(size, GFP_KERNEL); 1130bd1722d4SPavel Emelyanov if (xprt == NULL) 1131bd1722d4SPavel Emelyanov goto out; 1132bd1722d4SPavel Emelyanov 113321de0a95STrond Myklebust xprt_init(xprt, net); 113421de0a95STrond Myklebust 113521de0a95STrond Myklebust for (i = 0; i < num_prealloc; i++) { 113621de0a95STrond Myklebust req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 113721de0a95STrond Myklebust if (!req) 11388313164cSwangweidong goto out_free; 113921de0a95STrond Myklebust list_add(&req->rq_list, &xprt->free); 114021de0a95STrond Myklebust } 1141d9ba131dSTrond Myklebust if (max_alloc > num_prealloc) 1142d9ba131dSTrond Myklebust xprt->max_reqs = max_alloc; 1143d9ba131dSTrond Myklebust else 114421de0a95STrond Myklebust xprt->max_reqs = num_prealloc; 1145d9ba131dSTrond Myklebust xprt->min_reqs = num_prealloc; 1146d9ba131dSTrond Myklebust atomic_set(&xprt->num_reqs, num_prealloc); 1147bd1722d4SPavel Emelyanov 1148bd1722d4SPavel Emelyanov return xprt; 1149bd1722d4SPavel Emelyanov 1150bd1722d4SPavel Emelyanov out_free: 115121de0a95STrond Myklebust xprt_free(xprt); 1152bd1722d4SPavel Emelyanov out: 1153bd1722d4SPavel Emelyanov return NULL; 1154bd1722d4SPavel Emelyanov } 1155bd1722d4SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_alloc); 1156bd1722d4SPavel Emelyanov 1157e204e621SPavel Emelyanov void xprt_free(struct rpc_xprt *xprt) 1158e204e621SPavel Emelyanov { 115937aa2133SPavel Emelyanov put_net(xprt->xprt_net); 116021de0a95STrond Myklebust xprt_free_all_slots(xprt); 1161e204e621SPavel Emelyanov kfree(xprt); 1162e204e621SPavel Emelyanov } 1163e204e621SPavel Emelyanov EXPORT_SYMBOL_GPL(xprt_free); 1164e204e621SPavel Emelyanov 11659903cd1cSChuck Lever /** 11669903cd1cSChuck Lever * xprt_reserve - allocate an RPC request slot 11679903cd1cSChuck Lever * @task: RPC task requesting a slot allocation 11689903cd1cSChuck Lever * 1169ba60eb25STrond Myklebust * If the transport is marked as being congested, or if no more 1170ba60eb25STrond Myklebust * slots are available, place the task on the transport's 11719903cd1cSChuck Lever * backlog queue. 11729903cd1cSChuck Lever */ 11739903cd1cSChuck Lever void xprt_reserve(struct rpc_task *task) 11741da177e4SLinus Torvalds { 117545bc0dceSTrond Myklebust struct rpc_xprt *xprt; 11761da177e4SLinus Torvalds 117743cedbf0STrond Myklebust task->tk_status = 0; 117843cedbf0STrond Myklebust if (task->tk_rqstp != NULL) 117943cedbf0STrond Myklebust return; 118043cedbf0STrond Myklebust 118143cedbf0STrond Myklebust task->tk_timeout = 0; 118243cedbf0STrond Myklebust task->tk_status = -EAGAIN; 118345bc0dceSTrond Myklebust rcu_read_lock(); 118445bc0dceSTrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 1185ba60eb25STrond Myklebust if (!xprt_throttle_congested(xprt, task)) 1186ba60eb25STrond Myklebust xprt->ops->alloc_slot(xprt, task); 1187ba60eb25STrond Myklebust rcu_read_unlock(); 1188ba60eb25STrond Myklebust } 1189ba60eb25STrond Myklebust 1190ba60eb25STrond Myklebust /** 1191ba60eb25STrond Myklebust * xprt_retry_reserve - allocate an RPC request slot 1192ba60eb25STrond Myklebust * @task: RPC task requesting a slot allocation 1193ba60eb25STrond Myklebust * 1194ba60eb25STrond Myklebust * If no more slots are available, place the task on the transport's 1195ba60eb25STrond Myklebust * backlog queue. 1196ba60eb25STrond Myklebust * Note that the only difference with xprt_reserve is that we now 1197ba60eb25STrond Myklebust * ignore the value of the XPRT_CONGESTED flag. 1198ba60eb25STrond Myklebust */ 1199ba60eb25STrond Myklebust void xprt_retry_reserve(struct rpc_task *task) 1200ba60eb25STrond Myklebust { 1201ba60eb25STrond Myklebust struct rpc_xprt *xprt; 1202ba60eb25STrond Myklebust 1203ba60eb25STrond Myklebust task->tk_status = 0; 1204ba60eb25STrond Myklebust if (task->tk_rqstp != NULL) 1205ba60eb25STrond Myklebust return; 1206ba60eb25STrond Myklebust 1207ba60eb25STrond Myklebust task->tk_timeout = 0; 1208ba60eb25STrond Myklebust task->tk_status = -EAGAIN; 1209ba60eb25STrond Myklebust rcu_read_lock(); 1210ba60eb25STrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 1211f39c1bfbSTrond Myklebust xprt->ops->alloc_slot(xprt, task); 121245bc0dceSTrond Myklebust rcu_read_unlock(); 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 1215d8ed029dSAlexey Dobriyan static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt) 12161da177e4SLinus Torvalds { 12170eae88f3SEric Dumazet return (__force __be32)xprt->xid++; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds static inline void xprt_init_xid(struct rpc_xprt *xprt) 12211da177e4SLinus Torvalds { 122263862b5bSAruna-Hewapathirane xprt->xid = prandom_u32(); 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 12259903cd1cSChuck Lever static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds struct rpc_rqst *req = task->tk_rqstp; 12281da177e4SLinus Torvalds 1229d9ba131dSTrond Myklebust INIT_LIST_HEAD(&req->rq_list); 1230ba7392bbSTrond Myklebust req->rq_timeout = task->tk_client->cl_timeout->to_initval; 12311da177e4SLinus Torvalds req->rq_task = task; 12321da177e4SLinus Torvalds req->rq_xprt = xprt; 123302107148SChuck Lever req->rq_buffer = NULL; 12341da177e4SLinus Torvalds req->rq_xid = xprt_alloc_xid(xprt); 12350a660521STrond Myklebust req->rq_connect_cookie = xprt->connect_cookie - 1; 123692551948STrond Myklebust req->rq_bytes_sent = 0; 123792551948STrond Myklebust req->rq_snd_buf.len = 0; 123892551948STrond Myklebust req->rq_snd_buf.buflen = 0; 123992551948STrond Myklebust req->rq_rcv_buf.len = 0; 124092551948STrond Myklebust req->rq_rcv_buf.buflen = 0; 1241ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf = NULL; 1242da45828eSTrond Myklebust xprt_reset_majortimeo(req); 124346121cf7SChuck Lever dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 12441da177e4SLinus Torvalds req, ntohl(req->rq_xid)); 12451da177e4SLinus Torvalds } 12461da177e4SLinus Torvalds 12479903cd1cSChuck Lever /** 12489903cd1cSChuck Lever * xprt_release - release an RPC request slot 12499903cd1cSChuck Lever * @task: task which is finished with the slot 12509903cd1cSChuck Lever * 12511da177e4SLinus Torvalds */ 12529903cd1cSChuck Lever void xprt_release(struct rpc_task *task) 12531da177e4SLinus Torvalds { 125455ae1aabSRicardo Labiaga struct rpc_xprt *xprt; 125587ed5003STrond Myklebust struct rpc_rqst *req = task->tk_rqstp; 12561da177e4SLinus Torvalds 125787ed5003STrond Myklebust if (req == NULL) { 125887ed5003STrond Myklebust if (task->tk_client) { 125987ed5003STrond Myklebust rcu_read_lock(); 126087ed5003STrond Myklebust xprt = rcu_dereference(task->tk_client->cl_xprt); 126187ed5003STrond Myklebust if (xprt->snd_task == task) 126287ed5003STrond Myklebust xprt_release_write(xprt, task); 126387ed5003STrond Myklebust rcu_read_unlock(); 126487ed5003STrond Myklebust } 12651da177e4SLinus Torvalds return; 126687ed5003STrond Myklebust } 126755ae1aabSRicardo Labiaga 126855ae1aabSRicardo Labiaga xprt = req->rq_xprt; 12690a702195SWeston Andros Adamson if (task->tk_ops->rpc_count_stats != NULL) 12700a702195SWeston Andros Adamson task->tk_ops->rpc_count_stats(task, task->tk_calldata); 12710a702195SWeston Andros Adamson else if (task->tk_client) 12720a702195SWeston Andros Adamson rpc_count_iostats(task, task->tk_client->cl_metrics); 12734a0f8c04SChuck Lever spin_lock_bh(&xprt->transport_lock); 127449e9a890SChuck Lever xprt->ops->release_xprt(xprt, task); 1275a58dd398SChuck Lever if (xprt->ops->release_request) 1276a58dd398SChuck Lever xprt->ops->release_request(task); 12771da177e4SLinus Torvalds if (!list_empty(&req->rq_list)) 12781da177e4SLinus Torvalds list_del(&req->rq_list); 12791da177e4SLinus Torvalds xprt->last_used = jiffies; 12804cfc7e60SRahul Iyer if (list_empty(&xprt->recv) && xprt_has_timer(xprt)) 1281a246b010SChuck Lever mod_timer(&xprt->timer, 128203bf4b70SChuck Lever xprt->last_used + xprt->idle_timeout); 12834a0f8c04SChuck Lever spin_unlock_bh(&xprt->transport_lock); 1284ee5ebe85STrond Myklebust if (req->rq_buffer) 1285c5a4dd8bSChuck Lever xprt->ops->buf_free(req->rq_buffer); 1286a17c2153STrond Myklebust if (req->rq_cred != NULL) 1287a17c2153STrond Myklebust put_rpccred(req->rq_cred); 12881da177e4SLinus Torvalds task->tk_rqstp = NULL; 1289ead5e1c2SJ. Bruce Fields if (req->rq_release_snd_buf) 1290ead5e1c2SJ. Bruce Fields req->rq_release_snd_buf(req); 129155ae1aabSRicardo Labiaga 129246121cf7SChuck Lever dprintk("RPC: %5u release request %p\n", task->tk_pid, req); 1293ee5ebe85STrond Myklebust if (likely(!bc_prealloc(req))) 1294ee5ebe85STrond Myklebust xprt_free_slot(xprt, req); 1295ee5ebe85STrond Myklebust else 1296c9acb42eSTrond Myklebust xprt_free_bc_request(req); 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 129921de0a95STrond Myklebust static void xprt_init(struct rpc_xprt *xprt, struct net *net) 1300c2866763SChuck Lever { 130121de0a95STrond Myklebust atomic_set(&xprt->count, 1); 1302c2866763SChuck Lever 1303c2866763SChuck Lever spin_lock_init(&xprt->transport_lock); 1304c2866763SChuck Lever spin_lock_init(&xprt->reserve_lock); 1305c2866763SChuck Lever 1306c2866763SChuck Lever INIT_LIST_HEAD(&xprt->free); 1307c2866763SChuck Lever INIT_LIST_HEAD(&xprt->recv); 13089e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL) 1309f9acac1aSRicardo Labiaga spin_lock_init(&xprt->bc_pa_lock); 1310f9acac1aSRicardo Labiaga INIT_LIST_HEAD(&xprt->bc_pa_list); 13119e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */ 1312f9acac1aSRicardo Labiaga 1313c2866763SChuck Lever xprt->last_used = jiffies; 1314c2866763SChuck Lever xprt->cwnd = RPC_INITCWND; 1315a509050bSChuck Lever xprt->bind_index = 0; 1316c2866763SChuck Lever 1317c2866763SChuck Lever rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 1318c2866763SChuck Lever rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 131934006ceeSTrond Myklebust rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending"); 1320c2866763SChuck Lever rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 1321c2866763SChuck Lever 1322c2866763SChuck Lever xprt_init_xid(xprt); 1323c2866763SChuck Lever 132421de0a95STrond Myklebust xprt->xprt_net = get_net(net); 13258d9266ffSTrond Myklebust } 13268d9266ffSTrond Myklebust 13278d9266ffSTrond Myklebust /** 13288d9266ffSTrond Myklebust * xprt_create_transport - create an RPC transport 13298d9266ffSTrond Myklebust * @args: rpc transport creation arguments 13308d9266ffSTrond Myklebust * 13318d9266ffSTrond Myklebust */ 13328d9266ffSTrond Myklebust struct rpc_xprt *xprt_create_transport(struct xprt_create *args) 13338d9266ffSTrond Myklebust { 13348d9266ffSTrond Myklebust struct rpc_xprt *xprt; 13358d9266ffSTrond Myklebust struct xprt_class *t; 13368d9266ffSTrond Myklebust 13378d9266ffSTrond Myklebust spin_lock(&xprt_list_lock); 13388d9266ffSTrond Myklebust list_for_each_entry(t, &xprt_list, list) { 13398d9266ffSTrond Myklebust if (t->ident == args->ident) { 13408d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 13418d9266ffSTrond Myklebust goto found; 13428d9266ffSTrond Myklebust } 13438d9266ffSTrond Myklebust } 13448d9266ffSTrond Myklebust spin_unlock(&xprt_list_lock); 13453c45ddf8SChuck Lever dprintk("RPC: transport (%d) not supported\n", args->ident); 13468d9266ffSTrond Myklebust return ERR_PTR(-EIO); 13478d9266ffSTrond Myklebust 13488d9266ffSTrond Myklebust found: 13498d9266ffSTrond Myklebust xprt = t->setup(args); 13508d9266ffSTrond Myklebust if (IS_ERR(xprt)) { 13518d9266ffSTrond Myklebust dprintk("RPC: xprt_create_transport: failed, %ld\n", 13528d9266ffSTrond Myklebust -PTR_ERR(xprt)); 135321de0a95STrond Myklebust goto out; 13548d9266ffSTrond Myklebust } 135533d90ac0SJ. Bruce Fields if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT) 135633d90ac0SJ. Bruce Fields xprt->idle_timeout = 0; 135721de0a95STrond Myklebust INIT_WORK(&xprt->task_cleanup, xprt_autoclose); 135821de0a95STrond Myklebust if (xprt_has_timer(xprt)) 135921de0a95STrond Myklebust setup_timer(&xprt->timer, xprt_init_autodisconnect, 136021de0a95STrond Myklebust (unsigned long)xprt); 136121de0a95STrond Myklebust else 136221de0a95STrond Myklebust init_timer(&xprt->timer); 13634e0038b6STrond Myklebust 13644e0038b6STrond Myklebust if (strlen(args->servername) > RPC_MAXNETNAMELEN) { 13654e0038b6STrond Myklebust xprt_destroy(xprt); 13664e0038b6STrond Myklebust return ERR_PTR(-EINVAL); 13674e0038b6STrond Myklebust } 13684e0038b6STrond Myklebust xprt->servername = kstrdup(args->servername, GFP_KERNEL); 13694e0038b6STrond Myklebust if (xprt->servername == NULL) { 13704e0038b6STrond Myklebust xprt_destroy(xprt); 13714e0038b6STrond Myklebust return ERR_PTR(-ENOMEM); 13724e0038b6STrond Myklebust } 13734e0038b6STrond Myklebust 13743f940098SJeff Layton rpc_xprt_debugfs_register(xprt); 1375388f0c77SJeff Layton 1376c2866763SChuck Lever dprintk("RPC: created transport %p with %u slots\n", xprt, 1377c2866763SChuck Lever xprt->max_reqs); 137821de0a95STrond Myklebust out: 1379c2866763SChuck Lever return xprt; 1380c2866763SChuck Lever } 1381c2866763SChuck Lever 13829903cd1cSChuck Lever /** 13839903cd1cSChuck Lever * xprt_destroy - destroy an RPC transport, killing off all requests. 1384a8de240aSTrond Myklebust * @xprt: transport to destroy 13859903cd1cSChuck Lever * 13861da177e4SLinus Torvalds */ 1387a8de240aSTrond Myklebust static void xprt_destroy(struct rpc_xprt *xprt) 13881da177e4SLinus Torvalds { 13891da177e4SLinus Torvalds dprintk("RPC: destroying transport %p\n", xprt); 13900065db32STrond Myklebust del_timer_sync(&xprt->timer); 1391c8541ecdSChuck Lever 1392388f0c77SJeff Layton rpc_xprt_debugfs_unregister(xprt); 1393f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->binding); 1394f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->pending); 1395f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->sending); 1396f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&xprt->backlog); 1397c3ae62aeSJ. Bruce Fields cancel_work_sync(&xprt->task_cleanup); 13984e0038b6STrond Myklebust kfree(xprt->servername); 1399c8541ecdSChuck Lever /* 1400c8541ecdSChuck Lever * Tear down transport state and free the rpc_xprt 1401c8541ecdSChuck Lever */ 1402a246b010SChuck Lever xprt->ops->destroy(xprt); 14036b6ca86bSTrond Myklebust } 14041da177e4SLinus Torvalds 14056b6ca86bSTrond Myklebust /** 14066b6ca86bSTrond Myklebust * xprt_put - release a reference to an RPC transport. 14076b6ca86bSTrond Myklebust * @xprt: pointer to the transport 14086b6ca86bSTrond Myklebust * 14096b6ca86bSTrond Myklebust */ 14106b6ca86bSTrond Myklebust void xprt_put(struct rpc_xprt *xprt) 14116b6ca86bSTrond Myklebust { 1412a8de240aSTrond Myklebust if (atomic_dec_and_test(&xprt->count)) 1413a8de240aSTrond Myklebust xprt_destroy(xprt); 14146b6ca86bSTrond Myklebust } 1415