xprt.c 28 KB

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