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