send.c 36 KB

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