xprt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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/random.h>
  43. #include <linux/sunrpc/clnt.h>
  44. /*
  45. * Local variables
  46. */
  47. #ifdef RPC_DEBUG
  48. # undef RPC_DEBUG_DATA
  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. pprintk("RPC: %lu retrans\n", jiffies);
  419. } else {
  420. req->rq_timeout = to->to_initval;
  421. req->rq_retries = 0;
  422. xprt_reset_majortimeo(req);
  423. /* Reset the RTT counters == "slow start" */
  424. spin_lock_bh(&xprt->transport_lock);
  425. rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
  426. spin_unlock_bh(&xprt->transport_lock);
  427. pprintk("RPC: %lu timeout\n", jiffies);
  428. status = -ETIMEDOUT;
  429. }
  430. if (req->rq_timeout == 0) {
  431. printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
  432. req->rq_timeout = 5 * HZ;
  433. }
  434. return status;
  435. }
  436. static void xprt_autoclose(void *args)
  437. {
  438. struct rpc_xprt *xprt = (struct rpc_xprt *)args;
  439. xprt_disconnect(xprt);
  440. xprt->ops->close(xprt);
  441. xprt_release_write(xprt, NULL);
  442. }
  443. /**
  444. * xprt_disconnect - mark a transport as disconnected
  445. * @xprt: transport to flag for disconnect
  446. *
  447. */
  448. void xprt_disconnect(struct rpc_xprt *xprt)
  449. {
  450. dprintk("RPC: disconnected transport %p\n", xprt);
  451. spin_lock_bh(&xprt->transport_lock);
  452. xprt_clear_connected(xprt);
  453. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  454. spin_unlock_bh(&xprt->transport_lock);
  455. }
  456. static void
  457. xprt_init_autodisconnect(unsigned long data)
  458. {
  459. struct rpc_xprt *xprt = (struct rpc_xprt *)data;
  460. spin_lock(&xprt->transport_lock);
  461. if (!list_empty(&xprt->recv) || xprt->shutdown)
  462. goto out_abort;
  463. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  464. goto out_abort;
  465. spin_unlock(&xprt->transport_lock);
  466. if (xprt_connecting(xprt))
  467. xprt_release_write(xprt, NULL);
  468. else
  469. schedule_work(&xprt->task_cleanup);
  470. return;
  471. out_abort:
  472. spin_unlock(&xprt->transport_lock);
  473. }
  474. /**
  475. * xprt_connect - schedule a transport connect operation
  476. * @task: RPC task that is requesting the connect
  477. *
  478. */
  479. void xprt_connect(struct rpc_task *task)
  480. {
  481. struct rpc_xprt *xprt = task->tk_xprt;
  482. dprintk("RPC: %4d xprt_connect xprt %p %s connected\n", task->tk_pid,
  483. xprt, (xprt_connected(xprt) ? "is" : "is not"));
  484. if (!xprt->addr.sin_port) {
  485. task->tk_status = -EIO;
  486. return;
  487. }
  488. if (!xprt_lock_write(xprt, task))
  489. return;
  490. if (xprt_connected(xprt))
  491. xprt_release_write(xprt, task);
  492. else {
  493. if (task->tk_rqstp)
  494. task->tk_rqstp->rq_bytes_sent = 0;
  495. task->tk_timeout = xprt->connect_timeout;
  496. rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL);
  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. dprintk("RPC: %4d xprt_connect_status: connection established\n",
  506. task->tk_pid);
  507. return;
  508. }
  509. switch (task->tk_status) {
  510. case -ECONNREFUSED:
  511. case -ECONNRESET:
  512. dprintk("RPC: %4d xprt_connect_status: server %s refused connection\n",
  513. task->tk_pid, task->tk_client->cl_server);
  514. break;
  515. case -ENOTCONN:
  516. dprintk("RPC: %4d xprt_connect_status: connection broken\n",
  517. task->tk_pid);
  518. break;
  519. case -ETIMEDOUT:
  520. dprintk("RPC: %4d xprt_connect_status: connect attempt timed out\n",
  521. task->tk_pid);
  522. break;
  523. default:
  524. dprintk("RPC: %4d xprt_connect_status: error %d connecting to server %s\n",
  525. task->tk_pid, -task->tk_status, task->tk_client->cl_server);
  526. xprt_release_write(xprt, task);
  527. task->tk_status = -EIO;
  528. return;
  529. }
  530. /* if soft mounted, just cause this RPC to fail */
  531. if (RPC_IS_SOFT(task)) {
  532. xprt_release_write(xprt, task);
  533. task->tk_status = -EIO;
  534. }
  535. }
  536. /**
  537. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  538. * @xprt: transport on which the original request was transmitted
  539. * @xid: RPC XID of incoming reply
  540. *
  541. */
  542. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, u32 xid)
  543. {
  544. struct list_head *pos;
  545. struct rpc_rqst *req = NULL;
  546. list_for_each(pos, &xprt->recv) {
  547. struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
  548. if (entry->rq_xid == xid) {
  549. req = entry;
  550. break;
  551. }
  552. }
  553. return req;
  554. }
  555. /**
  556. * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
  557. * @task: RPC request that recently completed
  558. *
  559. */
  560. void xprt_update_rtt(struct rpc_task *task)
  561. {
  562. struct rpc_rqst *req = task->tk_rqstp;
  563. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  564. unsigned timer = task->tk_msg.rpc_proc->p_timer;
  565. if (timer) {
  566. if (req->rq_ntrans == 1)
  567. rpc_update_rtt(rtt, timer,
  568. (long)jiffies - req->rq_xtime);
  569. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  570. }
  571. }
  572. /**
  573. * xprt_complete_rqst - called when reply processing is complete
  574. * @task: RPC request that recently completed
  575. * @copied: actual number of bytes received from the transport
  576. *
  577. * Caller holds transport lock.
  578. */
  579. void xprt_complete_rqst(struct rpc_task *task, int copied)
  580. {
  581. struct rpc_rqst *req = task->tk_rqstp;
  582. dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
  583. task->tk_pid, ntohl(req->rq_xid), copied);
  584. list_del_init(&req->rq_list);
  585. req->rq_received = req->rq_private_buf.len = copied;
  586. rpc_wake_up_task(task);
  587. }
  588. static void xprt_timer(struct rpc_task *task)
  589. {
  590. struct rpc_rqst *req = task->tk_rqstp;
  591. struct rpc_xprt *xprt = req->rq_xprt;
  592. dprintk("RPC: %4d xprt_timer\n", task->tk_pid);
  593. spin_lock(&xprt->transport_lock);
  594. if (!req->rq_received) {
  595. if (xprt->ops->timer)
  596. xprt->ops->timer(task);
  597. task->tk_status = -ETIMEDOUT;
  598. }
  599. task->tk_timeout = 0;
  600. rpc_wake_up_task(task);
  601. spin_unlock(&xprt->transport_lock);
  602. }
  603. /**
  604. * xprt_prepare_transmit - reserve the transport before sending a request
  605. * @task: RPC task about to send a request
  606. *
  607. */
  608. int xprt_prepare_transmit(struct rpc_task *task)
  609. {
  610. struct rpc_rqst *req = task->tk_rqstp;
  611. struct rpc_xprt *xprt = req->rq_xprt;
  612. int err = 0;
  613. dprintk("RPC: %4d xprt_prepare_transmit\n", task->tk_pid);
  614. spin_lock_bh(&xprt->transport_lock);
  615. if (req->rq_received && !req->rq_bytes_sent) {
  616. err = req->rq_received;
  617. goto out_unlock;
  618. }
  619. if (!xprt->ops->reserve_xprt(task)) {
  620. err = -EAGAIN;
  621. goto out_unlock;
  622. }
  623. if (!xprt_connected(xprt)) {
  624. err = -ENOTCONN;
  625. goto out_unlock;
  626. }
  627. out_unlock:
  628. spin_unlock_bh(&xprt->transport_lock);
  629. return err;
  630. }
  631. void
  632. xprt_abort_transmit(struct rpc_task *task)
  633. {
  634. struct rpc_xprt *xprt = task->tk_xprt;
  635. xprt_release_write(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: %4d xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  649. smp_rmb();
  650. if (!req->rq_received) {
  651. if (list_empty(&req->rq_list)) {
  652. spin_lock_bh(&xprt->transport_lock);
  653. /* Update the softirq receive buffer */
  654. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  655. sizeof(req->rq_private_buf));
  656. /* Add request to the receive list */
  657. list_add_tail(&req->rq_list, &xprt->recv);
  658. spin_unlock_bh(&xprt->transport_lock);
  659. xprt_reset_majortimeo(req);
  660. /* Turn off autodisconnect */
  661. del_singleshot_timer_sync(&xprt->timer);
  662. }
  663. } else if (!req->rq_bytes_sent)
  664. return;
  665. status = xprt->ops->send_request(task);
  666. if (status == 0) {
  667. dprintk("RPC: %4d xmit complete\n", task->tk_pid);
  668. spin_lock_bh(&xprt->transport_lock);
  669. xprt->ops->set_retrans_timeout(task);
  670. /* Don't race with disconnect */
  671. if (!xprt_connected(xprt))
  672. task->tk_status = -ENOTCONN;
  673. else if (!req->rq_received)
  674. rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer);
  675. xprt->ops->release_xprt(xprt, task);
  676. spin_unlock_bh(&xprt->transport_lock);
  677. return;
  678. }
  679. /* Note: at this point, task->tk_sleeping has not yet been set,
  680. * hence there is no danger of the waking up task being put on
  681. * schedq, and being picked up by a parallel run of rpciod().
  682. */
  683. task->tk_status = status;
  684. switch (status) {
  685. case -ECONNREFUSED:
  686. rpc_sleep_on(&xprt->sending, task, NULL, NULL);
  687. case -EAGAIN:
  688. case -ENOTCONN:
  689. return;
  690. default:
  691. break;
  692. }
  693. xprt_release_write(xprt, task);
  694. return;
  695. }
  696. static inline void do_xprt_reserve(struct rpc_task *task)
  697. {
  698. struct rpc_xprt *xprt = task->tk_xprt;
  699. task->tk_status = 0;
  700. if (task->tk_rqstp)
  701. return;
  702. if (!list_empty(&xprt->free)) {
  703. struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  704. list_del_init(&req->rq_list);
  705. task->tk_rqstp = req;
  706. xprt_request_init(task, xprt);
  707. return;
  708. }
  709. dprintk("RPC: waiting for request slot\n");
  710. task->tk_status = -EAGAIN;
  711. task->tk_timeout = 0;
  712. rpc_sleep_on(&xprt->backlog, task, NULL, NULL);
  713. }
  714. /**
  715. * xprt_reserve - allocate an RPC request slot
  716. * @task: RPC task requesting a slot allocation
  717. *
  718. * If no more slots are available, place the task on the transport's
  719. * backlog queue.
  720. */
  721. void xprt_reserve(struct rpc_task *task)
  722. {
  723. struct rpc_xprt *xprt = task->tk_xprt;
  724. task->tk_status = -EIO;
  725. spin_lock(&xprt->reserve_lock);
  726. do_xprt_reserve(task);
  727. spin_unlock(&xprt->reserve_lock);
  728. }
  729. static inline u32 xprt_alloc_xid(struct rpc_xprt *xprt)
  730. {
  731. return xprt->xid++;
  732. }
  733. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  734. {
  735. get_random_bytes(&xprt->xid, sizeof(xprt->xid));
  736. }
  737. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  738. {
  739. struct rpc_rqst *req = task->tk_rqstp;
  740. req->rq_timeout = xprt->timeout.to_initval;
  741. req->rq_task = task;
  742. req->rq_xprt = xprt;
  743. req->rq_buffer = NULL;
  744. req->rq_bufsize = 0;
  745. req->rq_xid = xprt_alloc_xid(xprt);
  746. req->rq_release_snd_buf = NULL;
  747. dprintk("RPC: %4d reserved req %p xid %08x\n", task->tk_pid,
  748. req, ntohl(req->rq_xid));
  749. }
  750. /**
  751. * xprt_release - release an RPC request slot
  752. * @task: task which is finished with the slot
  753. *
  754. */
  755. void xprt_release(struct rpc_task *task)
  756. {
  757. struct rpc_xprt *xprt = task->tk_xprt;
  758. struct rpc_rqst *req;
  759. if (!(req = task->tk_rqstp))
  760. return;
  761. spin_lock_bh(&xprt->transport_lock);
  762. xprt->ops->release_xprt(xprt, task);
  763. if (xprt->ops->release_request)
  764. xprt->ops->release_request(task);
  765. if (!list_empty(&req->rq_list))
  766. list_del(&req->rq_list);
  767. xprt->last_used = jiffies;
  768. if (list_empty(&xprt->recv))
  769. mod_timer(&xprt->timer,
  770. xprt->last_used + xprt->idle_timeout);
  771. spin_unlock_bh(&xprt->transport_lock);
  772. xprt->ops->buf_free(task);
  773. task->tk_rqstp = NULL;
  774. if (req->rq_release_snd_buf)
  775. req->rq_release_snd_buf(req);
  776. memset(req, 0, sizeof(*req)); /* mark unused */
  777. dprintk("RPC: %4d release request %p\n", task->tk_pid, req);
  778. spin_lock(&xprt->reserve_lock);
  779. list_add(&req->rq_list, &xprt->free);
  780. rpc_wake_up_next(&xprt->backlog);
  781. spin_unlock(&xprt->reserve_lock);
  782. }
  783. /**
  784. * xprt_set_timeout - set constant RPC timeout
  785. * @to: RPC timeout parameters to set up
  786. * @retr: number of retries
  787. * @incr: amount of increase after each retry
  788. *
  789. */
  790. void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
  791. {
  792. to->to_initval =
  793. to->to_increment = incr;
  794. to->to_maxval = to->to_initval + (incr * retr);
  795. to->to_retries = retr;
  796. to->to_exponential = 0;
  797. }
  798. static struct rpc_xprt *xprt_setup(int proto, struct sockaddr_in *ap, struct rpc_timeout *to)
  799. {
  800. int result;
  801. struct rpc_xprt *xprt;
  802. struct rpc_rqst *req;
  803. if ((xprt = kmalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL)
  804. return ERR_PTR(-ENOMEM);
  805. memset(xprt, 0, sizeof(*xprt)); /* Nnnngh! */
  806. xprt->addr = *ap;
  807. switch (proto) {
  808. case IPPROTO_UDP:
  809. result = xs_setup_udp(xprt, to);
  810. break;
  811. case IPPROTO_TCP:
  812. result = xs_setup_tcp(xprt, to);
  813. break;
  814. default:
  815. printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
  816. proto);
  817. result = -EIO;
  818. break;
  819. }
  820. if (result) {
  821. kfree(xprt);
  822. return ERR_PTR(result);
  823. }
  824. spin_lock_init(&xprt->transport_lock);
  825. spin_lock_init(&xprt->reserve_lock);
  826. INIT_LIST_HEAD(&xprt->free);
  827. INIT_LIST_HEAD(&xprt->recv);
  828. INIT_WORK(&xprt->task_cleanup, xprt_autoclose, xprt);
  829. init_timer(&xprt->timer);
  830. xprt->timer.function = xprt_init_autodisconnect;
  831. xprt->timer.data = (unsigned long) xprt;
  832. xprt->last_used = jiffies;
  833. xprt->cwnd = RPC_INITCWND;
  834. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  835. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  836. rpc_init_wait_queue(&xprt->resend, "xprt_resend");
  837. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  838. /* initialize free list */
  839. for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
  840. list_add(&req->rq_list, &xprt->free);
  841. xprt_init_xid(xprt);
  842. dprintk("RPC: created transport %p with %u slots\n", xprt,
  843. xprt->max_reqs);
  844. return xprt;
  845. }
  846. /**
  847. * xprt_create_proto - create an RPC client transport
  848. * @proto: requested transport protocol
  849. * @sap: remote peer's address
  850. * @to: timeout parameters for new transport
  851. *
  852. */
  853. struct rpc_xprt *xprt_create_proto(int proto, struct sockaddr_in *sap, struct rpc_timeout *to)
  854. {
  855. struct rpc_xprt *xprt;
  856. xprt = xprt_setup(proto, sap, to);
  857. if (IS_ERR(xprt))
  858. dprintk("RPC: xprt_create_proto failed\n");
  859. else
  860. dprintk("RPC: xprt_create_proto created xprt %p\n", xprt);
  861. return xprt;
  862. }
  863. /**
  864. * xprt_destroy - destroy an RPC transport, killing off all requests.
  865. * @xprt: transport to destroy
  866. *
  867. */
  868. int xprt_destroy(struct rpc_xprt *xprt)
  869. {
  870. dprintk("RPC: destroying transport %p\n", xprt);
  871. xprt->shutdown = 1;
  872. del_timer_sync(&xprt->timer);
  873. xprt->ops->destroy(xprt);
  874. kfree(xprt);
  875. return 0;
  876. }