xprt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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: %5u 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: %5u 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: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\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 "
  354. "xprt %p\n", 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: %5u 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: %5u 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: %5u xprt_connect_status: server %s refused "
  515. "connection\n", task->tk_pid,
  516. task->tk_client->cl_server);
  517. break;
  518. case -ENOTCONN:
  519. dprintk("RPC: %5u xprt_connect_status: connection broken\n",
  520. task->tk_pid);
  521. break;
  522. case -ETIMEDOUT:
  523. dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
  524. "out\n", task->tk_pid);
  525. break;
  526. default:
  527. dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
  528. "server %s\n", task->tk_pid, -task->tk_status,
  529. task->tk_client->cl_server);
  530. xprt_release_write(xprt, task);
  531. task->tk_status = -EIO;
  532. }
  533. }
  534. /**
  535. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  536. * @xprt: transport on which the original request was transmitted
  537. * @xid: RPC XID of incoming reply
  538. *
  539. */
  540. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  541. {
  542. struct list_head *pos;
  543. list_for_each(pos, &xprt->recv) {
  544. struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
  545. if (entry->rq_xid == xid)
  546. return entry;
  547. }
  548. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  549. ntohl(xid));
  550. xprt->stat.bad_xids++;
  551. return NULL;
  552. }
  553. /**
  554. * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
  555. * @task: RPC request that recently completed
  556. *
  557. */
  558. void xprt_update_rtt(struct rpc_task *task)
  559. {
  560. struct rpc_rqst *req = task->tk_rqstp;
  561. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  562. unsigned timer = task->tk_msg.rpc_proc->p_timer;
  563. if (timer) {
  564. if (req->rq_ntrans == 1)
  565. rpc_update_rtt(rtt, timer,
  566. (long)jiffies - req->rq_xtime);
  567. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  568. }
  569. }
  570. /**
  571. * xprt_complete_rqst - called when reply processing is complete
  572. * @task: RPC request that recently completed
  573. * @copied: actual number of bytes received from the transport
  574. *
  575. * Caller holds transport lock.
  576. */
  577. void xprt_complete_rqst(struct rpc_task *task, int copied)
  578. {
  579. struct rpc_rqst *req = task->tk_rqstp;
  580. dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
  581. task->tk_pid, ntohl(req->rq_xid), copied);
  582. task->tk_xprt->stat.recvs++;
  583. task->tk_rtt = (long)jiffies - req->rq_xtime;
  584. list_del_init(&req->rq_list);
  585. /* Ensure all writes are done before we update req->rq_received */
  586. smp_wmb();
  587. req->rq_received = req->rq_private_buf.len = copied;
  588. rpc_wake_up_task(task);
  589. }
  590. static void xprt_timer(struct rpc_task *task)
  591. {
  592. struct rpc_rqst *req = task->tk_rqstp;
  593. struct rpc_xprt *xprt = req->rq_xprt;
  594. dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
  595. spin_lock(&xprt->transport_lock);
  596. if (!req->rq_received) {
  597. if (xprt->ops->timer)
  598. xprt->ops->timer(task);
  599. task->tk_status = -ETIMEDOUT;
  600. }
  601. task->tk_timeout = 0;
  602. rpc_wake_up_task(task);
  603. spin_unlock(&xprt->transport_lock);
  604. }
  605. /**
  606. * xprt_prepare_transmit - reserve the transport before sending a request
  607. * @task: RPC task about to send a request
  608. *
  609. */
  610. int xprt_prepare_transmit(struct rpc_task *task)
  611. {
  612. struct rpc_rqst *req = task->tk_rqstp;
  613. struct rpc_xprt *xprt = req->rq_xprt;
  614. int err = 0;
  615. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  616. spin_lock_bh(&xprt->transport_lock);
  617. if (req->rq_received && !req->rq_bytes_sent) {
  618. err = req->rq_received;
  619. goto out_unlock;
  620. }
  621. if (!xprt->ops->reserve_xprt(task)) {
  622. err = -EAGAIN;
  623. goto out_unlock;
  624. }
  625. if (!xprt_connected(xprt)) {
  626. err = -ENOTCONN;
  627. goto out_unlock;
  628. }
  629. out_unlock:
  630. spin_unlock_bh(&xprt->transport_lock);
  631. return err;
  632. }
  633. void xprt_end_transmit(struct rpc_task *task)
  634. {
  635. xprt_release_write(task->tk_xprt, task);
  636. }
  637. /**
  638. * xprt_transmit - send an RPC request on a transport
  639. * @task: controlling RPC task
  640. *
  641. * We have to copy the iovec because sendmsg fiddles with its contents.
  642. */
  643. void xprt_transmit(struct rpc_task *task)
  644. {
  645. struct rpc_rqst *req = task->tk_rqstp;
  646. struct rpc_xprt *xprt = req->rq_xprt;
  647. int status;
  648. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  649. if (!req->rq_received) {
  650. if (list_empty(&req->rq_list)) {
  651. spin_lock_bh(&xprt->transport_lock);
  652. /* Update the softirq receive buffer */
  653. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  654. sizeof(req->rq_private_buf));
  655. /* Add request to the receive list */
  656. list_add_tail(&req->rq_list, &xprt->recv);
  657. spin_unlock_bh(&xprt->transport_lock);
  658. xprt_reset_majortimeo(req);
  659. /* Turn off autodisconnect */
  660. del_singleshot_timer_sync(&xprt->timer);
  661. }
  662. } else if (!req->rq_bytes_sent)
  663. return;
  664. status = xprt->ops->send_request(task);
  665. if (status == 0) {
  666. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  667. spin_lock_bh(&xprt->transport_lock);
  668. xprt->ops->set_retrans_timeout(task);
  669. xprt->stat.sends++;
  670. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  671. xprt->stat.bklog_u += xprt->backlog.qlen;
  672. /* Don't race with disconnect */
  673. if (!xprt_connected(xprt))
  674. task->tk_status = -ENOTCONN;
  675. else if (!req->rq_received)
  676. rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer);
  677. spin_unlock_bh(&xprt->transport_lock);
  678. return;
  679. }
  680. /* Note: at this point, task->tk_sleeping has not yet been set,
  681. * hence there is no danger of the waking up task being put on
  682. * schedq, and being picked up by a parallel run of rpciod().
  683. */
  684. task->tk_status = status;
  685. if (status == -ECONNREFUSED)
  686. rpc_sleep_on(&xprt->sending, task, NULL, NULL);
  687. }
  688. static inline void do_xprt_reserve(struct rpc_task *task)
  689. {
  690. struct rpc_xprt *xprt = task->tk_xprt;
  691. task->tk_status = 0;
  692. if (task->tk_rqstp)
  693. return;
  694. if (!list_empty(&xprt->free)) {
  695. struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  696. list_del_init(&req->rq_list);
  697. task->tk_rqstp = req;
  698. xprt_request_init(task, xprt);
  699. return;
  700. }
  701. dprintk("RPC: waiting for request slot\n");
  702. task->tk_status = -EAGAIN;
  703. task->tk_timeout = 0;
  704. rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
  705. }
  706. /**
  707. * xprt_reserve - allocate an RPC request slot
  708. * @task: RPC task requesting a slot allocation
  709. *
  710. * If no more slots are available, place the task on the transport's
  711. * backlog queue.
  712. */
  713. void xprt_reserve(struct rpc_task *task)
  714. {
  715. struct rpc_xprt *xprt = task->tk_xprt;
  716. task->tk_status = -EIO;
  717. spin_lock(&xprt->reserve_lock);
  718. do_xprt_reserve(task);
  719. spin_unlock(&xprt->reserve_lock);
  720. }
  721. static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
  722. {
  723. return xprt->xid++;
  724. }
  725. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  726. {
  727. xprt->xid = net_random();
  728. }
  729. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  730. {
  731. struct rpc_rqst *req = task->tk_rqstp;
  732. req->rq_timeout = xprt->timeout.to_initval;
  733. req->rq_task = task;
  734. req->rq_xprt = xprt;
  735. req->rq_buffer = NULL;
  736. req->rq_bufsize = 0;
  737. req->rq_xid = xprt_alloc_xid(xprt);
  738. req->rq_release_snd_buf = NULL;
  739. xprt_reset_majortimeo(req);
  740. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  741. req, ntohl(req->rq_xid));
  742. }
  743. /**
  744. * xprt_release - release an RPC request slot
  745. * @task: task which is finished with the slot
  746. *
  747. */
  748. void xprt_release(struct rpc_task *task)
  749. {
  750. struct rpc_xprt *xprt = task->tk_xprt;
  751. struct rpc_rqst *req;
  752. if (!(req = task->tk_rqstp))
  753. return;
  754. rpc_count_iostats(task);
  755. spin_lock_bh(&xprt->transport_lock);
  756. xprt->ops->release_xprt(xprt, task);
  757. if (xprt->ops->release_request)
  758. xprt->ops->release_request(task);
  759. if (!list_empty(&req->rq_list))
  760. list_del(&req->rq_list);
  761. xprt->last_used = jiffies;
  762. if (list_empty(&xprt->recv))
  763. mod_timer(&xprt->timer,
  764. xprt->last_used + xprt->idle_timeout);
  765. spin_unlock_bh(&xprt->transport_lock);
  766. xprt->ops->buf_free(task);
  767. task->tk_rqstp = NULL;
  768. if (req->rq_release_snd_buf)
  769. req->rq_release_snd_buf(req);
  770. memset(req, 0, sizeof(*req)); /* mark unused */
  771. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  772. spin_lock(&xprt->reserve_lock);
  773. list_add(&req->rq_list, &xprt->free);
  774. rpc_wake_up_next(&xprt->backlog);
  775. spin_unlock(&xprt->reserve_lock);
  776. }
  777. /**
  778. * xprt_set_timeout - set constant RPC timeout
  779. * @to: RPC timeout parameters to set up
  780. * @retr: number of retries
  781. * @incr: amount of increase after each retry
  782. *
  783. */
  784. void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
  785. {
  786. to->to_initval =
  787. to->to_increment = incr;
  788. to->to_maxval = to->to_initval + (incr * retr);
  789. to->to_retries = retr;
  790. to->to_exponential = 0;
  791. }
  792. /**
  793. * xprt_create_transport - create an RPC transport
  794. * @proto: requested transport protocol
  795. * @ap: remote peer address
  796. * @size: length of address
  797. * @to: timeout parameters
  798. *
  799. */
  800. struct rpc_xprt *xprt_create_transport(int proto, struct sockaddr *ap, size_t size, struct rpc_timeout *to)
  801. {
  802. struct rpc_xprt *xprt;
  803. struct rpc_rqst *req;
  804. switch (proto) {
  805. case IPPROTO_UDP:
  806. xprt = xs_setup_udp(ap, size, to);
  807. break;
  808. case IPPROTO_TCP:
  809. xprt = xs_setup_tcp(ap, size, to);
  810. break;
  811. default:
  812. printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
  813. proto);
  814. return ERR_PTR(-EIO);
  815. }
  816. if (IS_ERR(xprt)) {
  817. dprintk("RPC: xprt_create_transport: failed, %ld\n",
  818. -PTR_ERR(xprt));
  819. return xprt;
  820. }
  821. kref_init(&xprt->kref);
  822. spin_lock_init(&xprt->transport_lock);
  823. spin_lock_init(&xprt->reserve_lock);
  824. INIT_LIST_HEAD(&xprt->free);
  825. INIT_LIST_HEAD(&xprt->recv);
  826. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  827. init_timer(&xprt->timer);
  828. xprt->timer.function = xprt_init_autodisconnect;
  829. xprt->timer.data = (unsigned long) xprt;
  830. xprt->last_used = jiffies;
  831. xprt->cwnd = RPC_INITCWND;
  832. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  833. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  834. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  835. rpc_init_wait_queue(&xprt->resend, "xprt_resend");
  836. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  837. /* initialize free list */
  838. for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
  839. list_add(&req->rq_list, &xprt->free);
  840. xprt_init_xid(xprt);
  841. dprintk("RPC: created transport %p with %u slots\n", xprt,
  842. xprt->max_reqs);
  843. return xprt;
  844. }
  845. /**
  846. * xprt_destroy - destroy an RPC transport, killing off all requests.
  847. * @kref: kref for the transport to destroy
  848. *
  849. */
  850. static void xprt_destroy(struct kref *kref)
  851. {
  852. struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
  853. dprintk("RPC: destroying transport %p\n", xprt);
  854. xprt->shutdown = 1;
  855. del_timer_sync(&xprt->timer);
  856. /*
  857. * Tear down transport state and free the rpc_xprt
  858. */
  859. xprt->ops->destroy(xprt);
  860. }
  861. /**
  862. * xprt_put - release a reference to an RPC transport.
  863. * @xprt: pointer to the transport
  864. *
  865. */
  866. void xprt_put(struct rpc_xprt *xprt)
  867. {
  868. kref_put(&xprt->kref, xprt_destroy);
  869. }
  870. /**
  871. * xprt_get - return a reference to an RPC transport.
  872. * @xprt: pointer to the transport
  873. *
  874. */
  875. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  876. {
  877. kref_get(&xprt->kref);
  878. return xprt;
  879. }