xprt.c 26 KB

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