send.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /*
  2. * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/moduleparam.h>
  35. #include <linux/gfp.h>
  36. #include <net/sock.h>
  37. #include <linux/in.h>
  38. #include <linux/list.h>
  39. #include <linux/ratelimit.h>
  40. #include <linux/export.h>
  41. #include <linux/sizes.h>
  42. #include "rds.h"
  43. /* When transmitting messages in rds_send_xmit, we need to emerge from
  44. * time to time and briefly release the CPU. Otherwise the softlock watchdog
  45. * will kick our shin.
  46. * Also, it seems fairer to not let one busy connection stall all the
  47. * others.
  48. *
  49. * send_batch_count is the number of times we'll loop in send_xmit. Setting
  50. * it to 0 will restore the old behavior (where we looped until we had
  51. * drained the queue).
  52. */
  53. static int send_batch_count = SZ_1K;
  54. module_param(send_batch_count, int, 0444);
  55. MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
  56. static void rds_send_remove_from_sock(struct list_head *messages, int status);
  57. /*
  58. * Reset the send state. Callers must ensure that this doesn't race with
  59. * rds_send_xmit().
  60. */
  61. void rds_send_path_reset(struct rds_conn_path *cp)
  62. {
  63. struct rds_message *rm, *tmp;
  64. unsigned long flags;
  65. if (cp->cp_xmit_rm) {
  66. rm = cp->cp_xmit_rm;
  67. cp->cp_xmit_rm = NULL;
  68. /* Tell the user the RDMA op is no longer mapped by the
  69. * transport. This isn't entirely true (it's flushed out
  70. * independently) but as the connection is down, there's
  71. * no ongoing RDMA to/from that memory */
  72. rds_message_unmapped(rm);
  73. rds_message_put(rm);
  74. }
  75. cp->cp_xmit_sg = 0;
  76. cp->cp_xmit_hdr_off = 0;
  77. cp->cp_xmit_data_off = 0;
  78. cp->cp_xmit_atomic_sent = 0;
  79. cp->cp_xmit_rdma_sent = 0;
  80. cp->cp_xmit_data_sent = 0;
  81. cp->cp_conn->c_map_queued = 0;
  82. cp->cp_unacked_packets = rds_sysctl_max_unacked_packets;
  83. cp->cp_unacked_bytes = rds_sysctl_max_unacked_bytes;
  84. /* Mark messages as retransmissions, and move them to the send q */
  85. spin_lock_irqsave(&cp->cp_lock, flags);
  86. list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
  87. set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  88. set_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags);
  89. }
  90. list_splice_init(&cp->cp_retrans, &cp->cp_send_queue);
  91. spin_unlock_irqrestore(&cp->cp_lock, flags);
  92. }
  93. EXPORT_SYMBOL_GPL(rds_send_path_reset);
  94. static int acquire_in_xmit(struct rds_conn_path *cp)
  95. {
  96. return test_and_set_bit(RDS_IN_XMIT, &cp->cp_flags) == 0;
  97. }
  98. static void release_in_xmit(struct rds_conn_path *cp)
  99. {
  100. clear_bit(RDS_IN_XMIT, &cp->cp_flags);
  101. smp_mb__after_atomic();
  102. /*
  103. * We don't use wait_on_bit()/wake_up_bit() because our waking is in a
  104. * hot path and finding waiters is very rare. We don't want to walk
  105. * the system-wide hashed waitqueue buckets in the fast path only to
  106. * almost never find waiters.
  107. */
  108. if (waitqueue_active(&cp->cp_waitq))
  109. wake_up_all(&cp->cp_waitq);
  110. }
  111. /*
  112. * We're making the conscious trade-off here to only send one message
  113. * down the connection at a time.
  114. * Pro:
  115. * - tx queueing is a simple fifo list
  116. * - reassembly is optional and easily done by transports per conn
  117. * - no per flow rx lookup at all, straight to the socket
  118. * - less per-frag memory and wire overhead
  119. * Con:
  120. * - queued acks can be delayed behind large messages
  121. * Depends:
  122. * - small message latency is higher behind queued large messages
  123. * - large message latency isn't starved by intervening small sends
  124. */
  125. int rds_send_xmit(struct rds_conn_path *cp)
  126. {
  127. struct rds_connection *conn = cp->cp_conn;
  128. struct rds_message *rm;
  129. unsigned long flags;
  130. unsigned int tmp;
  131. struct scatterlist *sg;
  132. int ret = 0;
  133. LIST_HEAD(to_be_dropped);
  134. int batch_count;
  135. unsigned long send_gen = 0;
  136. restart:
  137. batch_count = 0;
  138. /*
  139. * sendmsg calls here after having queued its message on the send
  140. * queue. We only have one task feeding the connection at a time. If
  141. * another thread is already feeding the queue then we back off. This
  142. * avoids blocking the caller and trading per-connection data between
  143. * caches per message.
  144. */
  145. if (!acquire_in_xmit(cp)) {
  146. rds_stats_inc(s_send_lock_contention);
  147. ret = -ENOMEM;
  148. goto out;
  149. }
  150. /*
  151. * we record the send generation after doing the xmit acquire.
  152. * if someone else manages to jump in and do some work, we'll use
  153. * this to avoid a goto restart farther down.
  154. *
  155. * The acquire_in_xmit() check above ensures that only one
  156. * caller can increment c_send_gen at any time.
  157. */
  158. send_gen = READ_ONCE(cp->cp_send_gen) + 1;
  159. WRITE_ONCE(cp->cp_send_gen, send_gen);
  160. /*
  161. * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT,
  162. * we do the opposite to avoid races.
  163. */
  164. if (!rds_conn_path_up(cp)) {
  165. release_in_xmit(cp);
  166. ret = 0;
  167. goto out;
  168. }
  169. if (conn->c_trans->xmit_path_prepare)
  170. conn->c_trans->xmit_path_prepare(cp);
  171. /*
  172. * spin trying to push headers and data down the connection until
  173. * the connection doesn't make forward progress.
  174. */
  175. while (1) {
  176. rm = cp->cp_xmit_rm;
  177. /*
  178. * If between sending messages, we can send a pending congestion
  179. * map update.
  180. */
  181. if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
  182. rm = rds_cong_update_alloc(conn);
  183. if (IS_ERR(rm)) {
  184. ret = PTR_ERR(rm);
  185. break;
  186. }
  187. rm->data.op_active = 1;
  188. rm->m_inc.i_conn_path = cp;
  189. rm->m_inc.i_conn = cp->cp_conn;
  190. cp->cp_xmit_rm = rm;
  191. }
  192. /*
  193. * If not already working on one, grab the next message.
  194. *
  195. * cp_xmit_rm holds a ref while we're sending this message down
  196. * the connction. We can use this ref while holding the
  197. * send_sem.. rds_send_reset() is serialized with it.
  198. */
  199. if (!rm) {
  200. unsigned int len;
  201. batch_count++;
  202. /* we want to process as big a batch as we can, but
  203. * we also want to avoid softlockups. If we've been
  204. * through a lot of messages, lets back off and see
  205. * if anyone else jumps in
  206. */
  207. if (batch_count >= send_batch_count)
  208. goto over_batch;
  209. spin_lock_irqsave(&cp->cp_lock, flags);
  210. if (!list_empty(&cp->cp_send_queue)) {
  211. rm = list_entry(cp->cp_send_queue.next,
  212. struct rds_message,
  213. m_conn_item);
  214. rds_message_addref(rm);
  215. /*
  216. * Move the message from the send queue to the retransmit
  217. * list right away.
  218. */
  219. list_move_tail(&rm->m_conn_item,
  220. &cp->cp_retrans);
  221. }
  222. spin_unlock_irqrestore(&cp->cp_lock, flags);
  223. if (!rm)
  224. break;
  225. /* Unfortunately, the way Infiniband deals with
  226. * RDMA to a bad MR key is by moving the entire
  227. * queue pair to error state. We cold possibly
  228. * recover from that, but right now we drop the
  229. * connection.
  230. * Therefore, we never retransmit messages with RDMA ops.
  231. */
  232. if (test_bit(RDS_MSG_FLUSH, &rm->m_flags) ||
  233. (rm->rdma.op_active &&
  234. test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))) {
  235. spin_lock_irqsave(&cp->cp_lock, flags);
  236. if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags))
  237. list_move(&rm->m_conn_item, &to_be_dropped);
  238. spin_unlock_irqrestore(&cp->cp_lock, flags);
  239. continue;
  240. }
  241. /* Require an ACK every once in a while */
  242. len = ntohl(rm->m_inc.i_hdr.h_len);
  243. if (cp->cp_unacked_packets == 0 ||
  244. cp->cp_unacked_bytes < len) {
  245. set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  246. cp->cp_unacked_packets =
  247. rds_sysctl_max_unacked_packets;
  248. cp->cp_unacked_bytes =
  249. rds_sysctl_max_unacked_bytes;
  250. rds_stats_inc(s_send_ack_required);
  251. } else {
  252. cp->cp_unacked_bytes -= len;
  253. cp->cp_unacked_packets--;
  254. }
  255. cp->cp_xmit_rm = rm;
  256. }
  257. /* The transport either sends the whole rdma or none of it */
  258. if (rm->rdma.op_active && !cp->cp_xmit_rdma_sent) {
  259. rm->m_final_op = &rm->rdma;
  260. /* The transport owns the mapped memory for now.
  261. * You can't unmap it while it's on the send queue
  262. */
  263. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  264. ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
  265. if (ret) {
  266. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  267. wake_up_interruptible(&rm->m_flush_wait);
  268. break;
  269. }
  270. cp->cp_xmit_rdma_sent = 1;
  271. }
  272. if (rm->atomic.op_active && !cp->cp_xmit_atomic_sent) {
  273. rm->m_final_op = &rm->atomic;
  274. /* The transport owns the mapped memory for now.
  275. * You can't unmap it while it's on the send queue
  276. */
  277. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  278. ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
  279. if (ret) {
  280. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  281. wake_up_interruptible(&rm->m_flush_wait);
  282. break;
  283. }
  284. cp->cp_xmit_atomic_sent = 1;
  285. }
  286. /*
  287. * A number of cases require an RDS header to be sent
  288. * even if there is no data.
  289. * We permit 0-byte sends; rds-ping depends on this.
  290. * However, if there are exclusively attached silent ops,
  291. * we skip the hdr/data send, to enable silent operation.
  292. */
  293. if (rm->data.op_nents == 0) {
  294. int ops_present;
  295. int all_ops_are_silent = 1;
  296. ops_present = (rm->atomic.op_active || rm->rdma.op_active);
  297. if (rm->atomic.op_active && !rm->atomic.op_silent)
  298. all_ops_are_silent = 0;
  299. if (rm->rdma.op_active && !rm->rdma.op_silent)
  300. all_ops_are_silent = 0;
  301. if (ops_present && all_ops_are_silent
  302. && !rm->m_rdma_cookie)
  303. rm->data.op_active = 0;
  304. }
  305. if (rm->data.op_active && !cp->cp_xmit_data_sent) {
  306. rm->m_final_op = &rm->data;
  307. ret = conn->c_trans->xmit(conn, rm,
  308. cp->cp_xmit_hdr_off,
  309. cp->cp_xmit_sg,
  310. cp->cp_xmit_data_off);
  311. if (ret <= 0)
  312. break;
  313. if (cp->cp_xmit_hdr_off < sizeof(struct rds_header)) {
  314. tmp = min_t(int, ret,
  315. sizeof(struct rds_header) -
  316. cp->cp_xmit_hdr_off);
  317. cp->cp_xmit_hdr_off += tmp;
  318. ret -= tmp;
  319. }
  320. sg = &rm->data.op_sg[cp->cp_xmit_sg];
  321. while (ret) {
  322. tmp = min_t(int, ret, sg->length -
  323. cp->cp_xmit_data_off);
  324. cp->cp_xmit_data_off += tmp;
  325. ret -= tmp;
  326. if (cp->cp_xmit_data_off == sg->length) {
  327. cp->cp_xmit_data_off = 0;
  328. sg++;
  329. cp->cp_xmit_sg++;
  330. BUG_ON(ret != 0 && cp->cp_xmit_sg ==
  331. rm->data.op_nents);
  332. }
  333. }
  334. if (cp->cp_xmit_hdr_off == sizeof(struct rds_header) &&
  335. (cp->cp_xmit_sg == rm->data.op_nents))
  336. cp->cp_xmit_data_sent = 1;
  337. }
  338. /*
  339. * A rm will only take multiple times through this loop
  340. * if there is a data op. Thus, if the data is sent (or there was
  341. * none), then we're done with the rm.
  342. */
  343. if (!rm->data.op_active || cp->cp_xmit_data_sent) {
  344. cp->cp_xmit_rm = NULL;
  345. cp->cp_xmit_sg = 0;
  346. cp->cp_xmit_hdr_off = 0;
  347. cp->cp_xmit_data_off = 0;
  348. cp->cp_xmit_rdma_sent = 0;
  349. cp->cp_xmit_atomic_sent = 0;
  350. cp->cp_xmit_data_sent = 0;
  351. rds_message_put(rm);
  352. }
  353. }
  354. over_batch:
  355. if (conn->c_trans->xmit_path_complete)
  356. conn->c_trans->xmit_path_complete(cp);
  357. release_in_xmit(cp);
  358. /* Nuke any messages we decided not to retransmit. */
  359. if (!list_empty(&to_be_dropped)) {
  360. /* irqs on here, so we can put(), unlike above */
  361. list_for_each_entry(rm, &to_be_dropped, m_conn_item)
  362. rds_message_put(rm);
  363. rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
  364. }
  365. /*
  366. * Other senders can queue a message after we last test the send queue
  367. * but before we clear RDS_IN_XMIT. In that case they'd back off and
  368. * not try and send their newly queued message. We need to check the
  369. * send queue after having cleared RDS_IN_XMIT so that their message
  370. * doesn't get stuck on the send queue.
  371. *
  372. * If the transport cannot continue (i.e ret != 0), then it must
  373. * call us when more room is available, such as from the tx
  374. * completion handler.
  375. *
  376. * We have an extra generation check here so that if someone manages
  377. * to jump in after our release_in_xmit, we'll see that they have done
  378. * some work and we will skip our goto
  379. */
  380. if (ret == 0) {
  381. bool raced;
  382. smp_mb();
  383. raced = send_gen != READ_ONCE(cp->cp_send_gen);
  384. if ((test_bit(0, &conn->c_map_queued) ||
  385. !list_empty(&cp->cp_send_queue)) && !raced) {
  386. if (batch_count < send_batch_count)
  387. goto restart;
  388. queue_delayed_work(rds_wq, &cp->cp_send_w, 1);
  389. } else if (raced) {
  390. rds_stats_inc(s_send_lock_queue_raced);
  391. }
  392. }
  393. out:
  394. return ret;
  395. }
  396. EXPORT_SYMBOL_GPL(rds_send_xmit);
  397. static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
  398. {
  399. u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  400. assert_spin_locked(&rs->rs_lock);
  401. BUG_ON(rs->rs_snd_bytes < len);
  402. rs->rs_snd_bytes -= len;
  403. if (rs->rs_snd_bytes == 0)
  404. rds_stats_inc(s_send_queue_empty);
  405. }
  406. static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
  407. is_acked_func is_acked)
  408. {
  409. if (is_acked)
  410. return is_acked(rm, ack);
  411. return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
  412. }
  413. /*
  414. * This is pretty similar to what happens below in the ACK
  415. * handling code - except that we call here as soon as we get
  416. * the IB send completion on the RDMA op and the accompanying
  417. * message.
  418. */
  419. void rds_rdma_send_complete(struct rds_message *rm, int status)
  420. {
  421. struct rds_sock *rs = NULL;
  422. struct rm_rdma_op *ro;
  423. struct rds_notifier *notifier;
  424. unsigned long flags;
  425. unsigned int notify = 0;
  426. spin_lock_irqsave(&rm->m_rs_lock, flags);
  427. notify = rm->rdma.op_notify | rm->data.op_notify;
  428. ro = &rm->rdma;
  429. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
  430. ro->op_active && notify && ro->op_notifier) {
  431. notifier = ro->op_notifier;
  432. rs = rm->m_rs;
  433. sock_hold(rds_rs_to_sk(rs));
  434. notifier->n_status = status;
  435. spin_lock(&rs->rs_lock);
  436. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  437. spin_unlock(&rs->rs_lock);
  438. ro->op_notifier = NULL;
  439. }
  440. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  441. if (rs) {
  442. rds_wake_sk_sleep(rs);
  443. sock_put(rds_rs_to_sk(rs));
  444. }
  445. }
  446. EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
  447. /*
  448. * Just like above, except looks at atomic op
  449. */
  450. void rds_atomic_send_complete(struct rds_message *rm, int status)
  451. {
  452. struct rds_sock *rs = NULL;
  453. struct rm_atomic_op *ao;
  454. struct rds_notifier *notifier;
  455. unsigned long flags;
  456. spin_lock_irqsave(&rm->m_rs_lock, flags);
  457. ao = &rm->atomic;
  458. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
  459. && ao->op_active && ao->op_notify && ao->op_notifier) {
  460. notifier = ao->op_notifier;
  461. rs = rm->m_rs;
  462. sock_hold(rds_rs_to_sk(rs));
  463. notifier->n_status = status;
  464. spin_lock(&rs->rs_lock);
  465. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  466. spin_unlock(&rs->rs_lock);
  467. ao->op_notifier = NULL;
  468. }
  469. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  470. if (rs) {
  471. rds_wake_sk_sleep(rs);
  472. sock_put(rds_rs_to_sk(rs));
  473. }
  474. }
  475. EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
  476. /*
  477. * This is the same as rds_rdma_send_complete except we
  478. * don't do any locking - we have all the ingredients (message,
  479. * socket, socket lock) and can just move the notifier.
  480. */
  481. static inline void
  482. __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
  483. {
  484. struct rm_rdma_op *ro;
  485. struct rm_atomic_op *ao;
  486. ro = &rm->rdma;
  487. if (ro->op_active && ro->op_notify && ro->op_notifier) {
  488. ro->op_notifier->n_status = status;
  489. list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
  490. ro->op_notifier = NULL;
  491. }
  492. ao = &rm->atomic;
  493. if (ao->op_active && ao->op_notify && ao->op_notifier) {
  494. ao->op_notifier->n_status = status;
  495. list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
  496. ao->op_notifier = NULL;
  497. }
  498. /* No need to wake the app - caller does this */
  499. }
  500. /*
  501. * This removes messages from the socket's list if they're on it. The list
  502. * argument must be private to the caller, we must be able to modify it
  503. * without locks. The messages must have a reference held for their
  504. * position on the list. This function will drop that reference after
  505. * removing the messages from the 'messages' list regardless of if it found
  506. * the messages on the socket list or not.
  507. */
  508. static void rds_send_remove_from_sock(struct list_head *messages, int status)
  509. {
  510. unsigned long flags;
  511. struct rds_sock *rs = NULL;
  512. struct rds_message *rm;
  513. while (!list_empty(messages)) {
  514. int was_on_sock = 0;
  515. rm = list_entry(messages->next, struct rds_message,
  516. m_conn_item);
  517. list_del_init(&rm->m_conn_item);
  518. /*
  519. * If we see this flag cleared then we're *sure* that someone
  520. * else beat us to removing it from the sock. If we race
  521. * with their flag update we'll get the lock and then really
  522. * see that the flag has been cleared.
  523. *
  524. * The message spinlock makes sure nobody clears rm->m_rs
  525. * while we're messing with it. It does not prevent the
  526. * message from being removed from the socket, though.
  527. */
  528. spin_lock_irqsave(&rm->m_rs_lock, flags);
  529. if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
  530. goto unlock_and_drop;
  531. if (rs != rm->m_rs) {
  532. if (rs) {
  533. rds_wake_sk_sleep(rs);
  534. sock_put(rds_rs_to_sk(rs));
  535. }
  536. rs = rm->m_rs;
  537. if (rs)
  538. sock_hold(rds_rs_to_sk(rs));
  539. }
  540. if (!rs)
  541. goto unlock_and_drop;
  542. spin_lock(&rs->rs_lock);
  543. if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
  544. struct rm_rdma_op *ro = &rm->rdma;
  545. struct rds_notifier *notifier;
  546. list_del_init(&rm->m_sock_item);
  547. rds_send_sndbuf_remove(rs, rm);
  548. if (ro->op_active && ro->op_notifier &&
  549. (ro->op_notify || (ro->op_recverr && status))) {
  550. notifier = ro->op_notifier;
  551. list_add_tail(&notifier->n_list,
  552. &rs->rs_notify_queue);
  553. if (!notifier->n_status)
  554. notifier->n_status = status;
  555. rm->rdma.op_notifier = NULL;
  556. }
  557. was_on_sock = 1;
  558. rm->m_rs = NULL;
  559. }
  560. spin_unlock(&rs->rs_lock);
  561. unlock_and_drop:
  562. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  563. rds_message_put(rm);
  564. if (was_on_sock)
  565. rds_message_put(rm);
  566. }
  567. if (rs) {
  568. rds_wake_sk_sleep(rs);
  569. sock_put(rds_rs_to_sk(rs));
  570. }
  571. }
  572. /*
  573. * Transports call here when they've determined that the receiver queued
  574. * messages up to, and including, the given sequence number. Messages are
  575. * moved to the retrans queue when rds_send_xmit picks them off the send
  576. * queue. This means that in the TCP case, the message may not have been
  577. * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
  578. * checks the RDS_MSG_HAS_ACK_SEQ bit.
  579. */
  580. void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack,
  581. is_acked_func is_acked)
  582. {
  583. struct rds_message *rm, *tmp;
  584. unsigned long flags;
  585. LIST_HEAD(list);
  586. spin_lock_irqsave(&cp->cp_lock, flags);
  587. list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
  588. if (!rds_send_is_acked(rm, ack, is_acked))
  589. break;
  590. list_move(&rm->m_conn_item, &list);
  591. clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  592. }
  593. /* order flag updates with spin locks */
  594. if (!list_empty(&list))
  595. smp_mb__after_atomic();
  596. spin_unlock_irqrestore(&cp->cp_lock, flags);
  597. /* now remove the messages from the sock list as needed */
  598. rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
  599. }
  600. EXPORT_SYMBOL_GPL(rds_send_path_drop_acked);
  601. void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
  602. is_acked_func is_acked)
  603. {
  604. WARN_ON(conn->c_trans->t_mp_capable);
  605. rds_send_path_drop_acked(&conn->c_path[0], ack, is_acked);
  606. }
  607. EXPORT_SYMBOL_GPL(rds_send_drop_acked);
  608. void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
  609. {
  610. struct rds_message *rm, *tmp;
  611. struct rds_connection *conn;
  612. struct rds_conn_path *cp;
  613. unsigned long flags;
  614. LIST_HEAD(list);
  615. /* get all the messages we're dropping under the rs lock */
  616. spin_lock_irqsave(&rs->rs_lock, flags);
  617. list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
  618. if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
  619. dest->sin_port != rm->m_inc.i_hdr.h_dport))
  620. continue;
  621. list_move(&rm->m_sock_item, &list);
  622. rds_send_sndbuf_remove(rs, rm);
  623. clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  624. }
  625. /* order flag updates with the rs lock */
  626. smp_mb__after_atomic();
  627. spin_unlock_irqrestore(&rs->rs_lock, flags);
  628. if (list_empty(&list))
  629. return;
  630. /* Remove the messages from the conn */
  631. list_for_each_entry(rm, &list, m_sock_item) {
  632. conn = rm->m_inc.i_conn;
  633. if (conn->c_trans->t_mp_capable)
  634. cp = rm->m_inc.i_conn_path;
  635. else
  636. cp = &conn->c_path[0];
  637. spin_lock_irqsave(&cp->cp_lock, flags);
  638. /*
  639. * Maybe someone else beat us to removing rm from the conn.
  640. * If we race with their flag update we'll get the lock and
  641. * then really see that the flag has been cleared.
  642. */
  643. if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
  644. spin_unlock_irqrestore(&cp->cp_lock, flags);
  645. spin_lock_irqsave(&rm->m_rs_lock, flags);
  646. rm->m_rs = NULL;
  647. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  648. continue;
  649. }
  650. list_del_init(&rm->m_conn_item);
  651. spin_unlock_irqrestore(&cp->cp_lock, flags);
  652. /*
  653. * Couldn't grab m_rs_lock in top loop (lock ordering),
  654. * but we can now.
  655. */
  656. spin_lock_irqsave(&rm->m_rs_lock, flags);
  657. spin_lock(&rs->rs_lock);
  658. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  659. spin_unlock(&rs->rs_lock);
  660. rm->m_rs = NULL;
  661. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  662. rds_message_put(rm);
  663. }
  664. rds_wake_sk_sleep(rs);
  665. while (!list_empty(&list)) {
  666. rm = list_entry(list.next, struct rds_message, m_sock_item);
  667. list_del_init(&rm->m_sock_item);
  668. rds_message_wait(rm);
  669. /* just in case the code above skipped this message
  670. * because RDS_MSG_ON_CONN wasn't set, run it again here
  671. * taking m_rs_lock is the only thing that keeps us
  672. * from racing with ack processing.
  673. */
  674. spin_lock_irqsave(&rm->m_rs_lock, flags);
  675. spin_lock(&rs->rs_lock);
  676. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  677. spin_unlock(&rs->rs_lock);
  678. rm->m_rs = NULL;
  679. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  680. rds_message_put(rm);
  681. }
  682. }
  683. /*
  684. * we only want this to fire once so we use the callers 'queued'. It's
  685. * possible that another thread can race with us and remove the
  686. * message from the flow with RDS_CANCEL_SENT_TO.
  687. */
  688. static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
  689. struct rds_conn_path *cp,
  690. struct rds_message *rm, __be16 sport,
  691. __be16 dport, int *queued)
  692. {
  693. unsigned long flags;
  694. u32 len;
  695. if (*queued)
  696. goto out;
  697. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  698. /* this is the only place which holds both the socket's rs_lock
  699. * and the connection's c_lock */
  700. spin_lock_irqsave(&rs->rs_lock, flags);
  701. /*
  702. * If there is a little space in sndbuf, we don't queue anything,
  703. * and userspace gets -EAGAIN. But poll() indicates there's send
  704. * room. This can lead to bad behavior (spinning) if snd_bytes isn't
  705. * freed up by incoming acks. So we check the *old* value of
  706. * rs_snd_bytes here to allow the last msg to exceed the buffer,
  707. * and poll() now knows no more data can be sent.
  708. */
  709. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
  710. rs->rs_snd_bytes += len;
  711. /* let recv side know we are close to send space exhaustion.
  712. * This is probably not the optimal way to do it, as this
  713. * means we set the flag on *all* messages as soon as our
  714. * throughput hits a certain threshold.
  715. */
  716. if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
  717. set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  718. list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
  719. set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  720. rds_message_addref(rm);
  721. rm->m_rs = rs;
  722. /* The code ordering is a little weird, but we're
  723. trying to minimize the time we hold c_lock */
  724. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
  725. rm->m_inc.i_conn = conn;
  726. rm->m_inc.i_conn_path = cp;
  727. rds_message_addref(rm);
  728. spin_lock(&cp->cp_lock);
  729. rm->m_inc.i_hdr.h_sequence = cpu_to_be64(cp->cp_next_tx_seq++);
  730. list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
  731. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  732. spin_unlock(&cp->cp_lock);
  733. rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
  734. rm, len, rs, rs->rs_snd_bytes,
  735. (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
  736. *queued = 1;
  737. }
  738. spin_unlock_irqrestore(&rs->rs_lock, flags);
  739. out:
  740. return *queued;
  741. }
  742. /*
  743. * rds_message is getting to be quite complicated, and we'd like to allocate
  744. * it all in one go. This figures out how big it needs to be up front.
  745. */
  746. static int rds_rm_size(struct msghdr *msg, int data_len)
  747. {
  748. struct cmsghdr *cmsg;
  749. int size = 0;
  750. int cmsg_groups = 0;
  751. int retval;
  752. for_each_cmsghdr(cmsg, msg) {
  753. if (!CMSG_OK(msg, cmsg))
  754. return -EINVAL;
  755. if (cmsg->cmsg_level != SOL_RDS)
  756. continue;
  757. switch (cmsg->cmsg_type) {
  758. case RDS_CMSG_RDMA_ARGS:
  759. cmsg_groups |= 1;
  760. retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
  761. if (retval < 0)
  762. return retval;
  763. size += retval;
  764. break;
  765. case RDS_CMSG_RDMA_DEST:
  766. case RDS_CMSG_RDMA_MAP:
  767. cmsg_groups |= 2;
  768. /* these are valid but do no add any size */
  769. break;
  770. case RDS_CMSG_ATOMIC_CSWP:
  771. case RDS_CMSG_ATOMIC_FADD:
  772. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  773. case RDS_CMSG_MASKED_ATOMIC_FADD:
  774. cmsg_groups |= 1;
  775. size += sizeof(struct scatterlist);
  776. break;
  777. default:
  778. return -EINVAL;
  779. }
  780. }
  781. size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
  782. /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
  783. if (cmsg_groups == 3)
  784. return -EINVAL;
  785. return size;
  786. }
  787. static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
  788. struct msghdr *msg, int *allocated_mr)
  789. {
  790. struct cmsghdr *cmsg;
  791. int ret = 0;
  792. for_each_cmsghdr(cmsg, msg) {
  793. if (!CMSG_OK(msg, cmsg))
  794. return -EINVAL;
  795. if (cmsg->cmsg_level != SOL_RDS)
  796. continue;
  797. /* As a side effect, RDMA_DEST and RDMA_MAP will set
  798. * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
  799. */
  800. switch (cmsg->cmsg_type) {
  801. case RDS_CMSG_RDMA_ARGS:
  802. ret = rds_cmsg_rdma_args(rs, rm, cmsg);
  803. break;
  804. case RDS_CMSG_RDMA_DEST:
  805. ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
  806. break;
  807. case RDS_CMSG_RDMA_MAP:
  808. ret = rds_cmsg_rdma_map(rs, rm, cmsg);
  809. if (!ret)
  810. *allocated_mr = 1;
  811. else if (ret == -ENODEV)
  812. /* Accommodate the get_mr() case which can fail
  813. * if connection isn't established yet.
  814. */
  815. ret = -EAGAIN;
  816. break;
  817. case RDS_CMSG_ATOMIC_CSWP:
  818. case RDS_CMSG_ATOMIC_FADD:
  819. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  820. case RDS_CMSG_MASKED_ATOMIC_FADD:
  821. ret = rds_cmsg_atomic(rs, rm, cmsg);
  822. break;
  823. default:
  824. return -EINVAL;
  825. }
  826. if (ret)
  827. break;
  828. }
  829. return ret;
  830. }
  831. static int rds_send_mprds_hash(struct rds_sock *rs, struct rds_connection *conn)
  832. {
  833. int hash;
  834. if (conn->c_npaths == 0)
  835. hash = RDS_MPATH_HASH(rs, RDS_MPATH_WORKERS);
  836. else
  837. hash = RDS_MPATH_HASH(rs, conn->c_npaths);
  838. if (conn->c_npaths == 0 && hash != 0) {
  839. rds_send_ping(conn, 0);
  840. /* The underlying connection is not up yet. Need to wait
  841. * until it is up to be sure that the non-zero c_path can be
  842. * used. But if we are interrupted, we have to use the zero
  843. * c_path in case the connection ends up being non-MP capable.
  844. */
  845. if (conn->c_npaths == 0)
  846. if (wait_event_interruptible(conn->c_hs_waitq,
  847. conn->c_npaths != 0))
  848. hash = 0;
  849. if (conn->c_npaths == 1)
  850. hash = 0;
  851. }
  852. return hash;
  853. }
  854. static int rds_rdma_bytes(struct msghdr *msg, size_t *rdma_bytes)
  855. {
  856. struct rds_rdma_args *args;
  857. struct cmsghdr *cmsg;
  858. for_each_cmsghdr(cmsg, msg) {
  859. if (!CMSG_OK(msg, cmsg))
  860. return -EINVAL;
  861. if (cmsg->cmsg_level != SOL_RDS)
  862. continue;
  863. if (cmsg->cmsg_type == RDS_CMSG_RDMA_ARGS) {
  864. if (cmsg->cmsg_len <
  865. CMSG_LEN(sizeof(struct rds_rdma_args)))
  866. return -EINVAL;
  867. args = CMSG_DATA(cmsg);
  868. *rdma_bytes += args->remote_vec.bytes;
  869. }
  870. }
  871. return 0;
  872. }
  873. int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
  874. {
  875. struct sock *sk = sock->sk;
  876. struct rds_sock *rs = rds_sk_to_rs(sk);
  877. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  878. __be32 daddr;
  879. __be16 dport;
  880. struct rds_message *rm = NULL;
  881. struct rds_connection *conn;
  882. int ret = 0;
  883. int queued = 0, allocated_mr = 0;
  884. int nonblock = msg->msg_flags & MSG_DONTWAIT;
  885. long timeo = sock_sndtimeo(sk, nonblock);
  886. struct rds_conn_path *cpath;
  887. size_t total_payload_len = payload_len, rdma_payload_len = 0;
  888. /* Mirror Linux UDP mirror of BSD error message compatibility */
  889. /* XXX: Perhaps MSG_MORE someday */
  890. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
  891. ret = -EOPNOTSUPP;
  892. goto out;
  893. }
  894. if (msg->msg_namelen) {
  895. /* XXX fail non-unicast destination IPs? */
  896. if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
  897. ret = -EINVAL;
  898. goto out;
  899. }
  900. daddr = usin->sin_addr.s_addr;
  901. dport = usin->sin_port;
  902. } else {
  903. /* We only care about consistency with ->connect() */
  904. lock_sock(sk);
  905. daddr = rs->rs_conn_addr;
  906. dport = rs->rs_conn_port;
  907. release_sock(sk);
  908. }
  909. lock_sock(sk);
  910. if (daddr == 0 || rs->rs_bound_addr == 0) {
  911. release_sock(sk);
  912. ret = -ENOTCONN; /* XXX not a great errno */
  913. goto out;
  914. }
  915. release_sock(sk);
  916. ret = rds_rdma_bytes(msg, &rdma_payload_len);
  917. if (ret)
  918. goto out;
  919. total_payload_len += rdma_payload_len;
  920. if (max_t(size_t, payload_len, rdma_payload_len) > RDS_MAX_MSG_SIZE) {
  921. ret = -EMSGSIZE;
  922. goto out;
  923. }
  924. if (payload_len > rds_sk_sndbuf(rs)) {
  925. ret = -EMSGSIZE;
  926. goto out;
  927. }
  928. /* size of rm including all sgs */
  929. ret = rds_rm_size(msg, payload_len);
  930. if (ret < 0)
  931. goto out;
  932. rm = rds_message_alloc(ret, GFP_KERNEL);
  933. if (!rm) {
  934. ret = -ENOMEM;
  935. goto out;
  936. }
  937. /* Attach data to the rm */
  938. if (payload_len) {
  939. rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
  940. if (!rm->data.op_sg) {
  941. ret = -ENOMEM;
  942. goto out;
  943. }
  944. ret = rds_message_copy_from_user(rm, &msg->msg_iter);
  945. if (ret)
  946. goto out;
  947. }
  948. rm->data.op_active = 1;
  949. rm->m_daddr = daddr;
  950. /* rds_conn_create has a spinlock that runs with IRQ off.
  951. * Caching the conn in the socket helps a lot. */
  952. if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
  953. conn = rs->rs_conn;
  954. else {
  955. conn = rds_conn_create_outgoing(sock_net(sock->sk),
  956. rs->rs_bound_addr, daddr,
  957. rs->rs_transport,
  958. sock->sk->sk_allocation);
  959. if (IS_ERR(conn)) {
  960. ret = PTR_ERR(conn);
  961. goto out;
  962. }
  963. rs->rs_conn = conn;
  964. }
  965. /* Parse any control messages the user may have included. */
  966. ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
  967. if (ret) {
  968. /* Trigger connection so that its ready for the next retry */
  969. if (ret == -EAGAIN)
  970. rds_conn_connect_if_down(conn);
  971. goto out;
  972. }
  973. if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
  974. printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
  975. &rm->rdma, conn->c_trans->xmit_rdma);
  976. ret = -EOPNOTSUPP;
  977. goto out;
  978. }
  979. if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
  980. printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
  981. &rm->atomic, conn->c_trans->xmit_atomic);
  982. ret = -EOPNOTSUPP;
  983. goto out;
  984. }
  985. if (conn->c_trans->t_mp_capable)
  986. cpath = &conn->c_path[rds_send_mprds_hash(rs, conn)];
  987. else
  988. cpath = &conn->c_path[0];
  989. rds_conn_path_connect_if_down(cpath);
  990. ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
  991. if (ret) {
  992. rs->rs_seen_congestion = 1;
  993. goto out;
  994. }
  995. while (!rds_send_queue_rm(rs, conn, cpath, rm, rs->rs_bound_port,
  996. dport, &queued)) {
  997. rds_stats_inc(s_send_queue_full);
  998. if (nonblock) {
  999. ret = -EAGAIN;
  1000. goto out;
  1001. }
  1002. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  1003. rds_send_queue_rm(rs, conn, cpath, rm,
  1004. rs->rs_bound_port,
  1005. dport,
  1006. &queued),
  1007. timeo);
  1008. rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
  1009. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  1010. continue;
  1011. ret = timeo;
  1012. if (ret == 0)
  1013. ret = -ETIMEDOUT;
  1014. goto out;
  1015. }
  1016. /*
  1017. * By now we've committed to the send. We reuse rds_send_worker()
  1018. * to retry sends in the rds thread if the transport asks us to.
  1019. */
  1020. rds_stats_inc(s_send_queued);
  1021. ret = rds_send_xmit(cpath);
  1022. if (ret == -ENOMEM || ret == -EAGAIN)
  1023. queue_delayed_work(rds_wq, &cpath->cp_send_w, 1);
  1024. rds_message_put(rm);
  1025. return payload_len;
  1026. out:
  1027. /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
  1028. * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
  1029. * or in any other way, we need to destroy the MR again */
  1030. if (allocated_mr)
  1031. rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
  1032. if (rm)
  1033. rds_message_put(rm);
  1034. return ret;
  1035. }
  1036. /*
  1037. * send out a probe. Can be shared by rds_send_ping,
  1038. * rds_send_pong, rds_send_hb.
  1039. * rds_send_hb should use h_flags
  1040. * RDS_FLAG_HB_PING|RDS_FLAG_ACK_REQUIRED
  1041. * or
  1042. * RDS_FLAG_HB_PONG|RDS_FLAG_ACK_REQUIRED
  1043. */
  1044. static int
  1045. rds_send_probe(struct rds_conn_path *cp, __be16 sport,
  1046. __be16 dport, u8 h_flags)
  1047. {
  1048. struct rds_message *rm;
  1049. unsigned long flags;
  1050. int ret = 0;
  1051. rm = rds_message_alloc(0, GFP_ATOMIC);
  1052. if (!rm) {
  1053. ret = -ENOMEM;
  1054. goto out;
  1055. }
  1056. rm->m_daddr = cp->cp_conn->c_faddr;
  1057. rm->data.op_active = 1;
  1058. rds_conn_path_connect_if_down(cp);
  1059. ret = rds_cong_wait(cp->cp_conn->c_fcong, dport, 1, NULL);
  1060. if (ret)
  1061. goto out;
  1062. spin_lock_irqsave(&cp->cp_lock, flags);
  1063. list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
  1064. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  1065. rds_message_addref(rm);
  1066. rm->m_inc.i_conn = cp->cp_conn;
  1067. rm->m_inc.i_conn_path = cp;
  1068. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport,
  1069. cp->cp_next_tx_seq);
  1070. rm->m_inc.i_hdr.h_flags |= h_flags;
  1071. cp->cp_next_tx_seq++;
  1072. if (RDS_HS_PROBE(be16_to_cpu(sport), be16_to_cpu(dport)) &&
  1073. cp->cp_conn->c_trans->t_mp_capable) {
  1074. u16 npaths = cpu_to_be16(RDS_MPATH_WORKERS);
  1075. u32 my_gen_num = cpu_to_be32(cp->cp_conn->c_my_gen_num);
  1076. rds_message_add_extension(&rm->m_inc.i_hdr,
  1077. RDS_EXTHDR_NPATHS, &npaths,
  1078. sizeof(npaths));
  1079. rds_message_add_extension(&rm->m_inc.i_hdr,
  1080. RDS_EXTHDR_GEN_NUM,
  1081. &my_gen_num,
  1082. sizeof(u32));
  1083. }
  1084. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1085. rds_stats_inc(s_send_queued);
  1086. rds_stats_inc(s_send_pong);
  1087. /* schedule the send work on rds_wq */
  1088. queue_delayed_work(rds_wq, &cp->cp_send_w, 1);
  1089. rds_message_put(rm);
  1090. return 0;
  1091. out:
  1092. if (rm)
  1093. rds_message_put(rm);
  1094. return ret;
  1095. }
  1096. int
  1097. rds_send_pong(struct rds_conn_path *cp, __be16 dport)
  1098. {
  1099. return rds_send_probe(cp, 0, dport, 0);
  1100. }
  1101. void
  1102. rds_send_ping(struct rds_connection *conn, int cp_index)
  1103. {
  1104. unsigned long flags;
  1105. struct rds_conn_path *cp = &conn->c_path[cp_index];
  1106. spin_lock_irqsave(&cp->cp_lock, flags);
  1107. if (conn->c_ping_triggered) {
  1108. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1109. return;
  1110. }
  1111. conn->c_ping_triggered = 1;
  1112. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1113. rds_send_probe(cp, cpu_to_be16(RDS_FLAG_PROBE_PORT), 0, 0);
  1114. }
  1115. EXPORT_SYMBOL_GPL(rds_send_ping);