send.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. /*
  2. * Copyright (c) 2006 Oracle. 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. smp_mb();
  382. if ((test_bit(0, &conn->c_map_queued) ||
  383. !list_empty(&cp->cp_send_queue)) &&
  384. send_gen == READ_ONCE(cp->cp_send_gen)) {
  385. rds_stats_inc(s_send_lock_queue_raced);
  386. if (batch_count < send_batch_count)
  387. goto restart;
  388. queue_delayed_work(rds_wq, &cp->cp_send_w, 1);
  389. }
  390. }
  391. out:
  392. return ret;
  393. }
  394. EXPORT_SYMBOL_GPL(rds_send_xmit);
  395. static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
  396. {
  397. u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  398. assert_spin_locked(&rs->rs_lock);
  399. BUG_ON(rs->rs_snd_bytes < len);
  400. rs->rs_snd_bytes -= len;
  401. if (rs->rs_snd_bytes == 0)
  402. rds_stats_inc(s_send_queue_empty);
  403. }
  404. static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
  405. is_acked_func is_acked)
  406. {
  407. if (is_acked)
  408. return is_acked(rm, ack);
  409. return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
  410. }
  411. /*
  412. * This is pretty similar to what happens below in the ACK
  413. * handling code - except that we call here as soon as we get
  414. * the IB send completion on the RDMA op and the accompanying
  415. * message.
  416. */
  417. void rds_rdma_send_complete(struct rds_message *rm, int status)
  418. {
  419. struct rds_sock *rs = NULL;
  420. struct rm_rdma_op *ro;
  421. struct rds_notifier *notifier;
  422. unsigned long flags;
  423. unsigned int notify = 0;
  424. spin_lock_irqsave(&rm->m_rs_lock, flags);
  425. notify = rm->rdma.op_notify | rm->data.op_notify;
  426. ro = &rm->rdma;
  427. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
  428. ro->op_active && notify && ro->op_notifier) {
  429. notifier = ro->op_notifier;
  430. rs = rm->m_rs;
  431. sock_hold(rds_rs_to_sk(rs));
  432. notifier->n_status = status;
  433. spin_lock(&rs->rs_lock);
  434. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  435. spin_unlock(&rs->rs_lock);
  436. ro->op_notifier = NULL;
  437. }
  438. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  439. if (rs) {
  440. rds_wake_sk_sleep(rs);
  441. sock_put(rds_rs_to_sk(rs));
  442. }
  443. }
  444. EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
  445. /*
  446. * Just like above, except looks at atomic op
  447. */
  448. void rds_atomic_send_complete(struct rds_message *rm, int status)
  449. {
  450. struct rds_sock *rs = NULL;
  451. struct rm_atomic_op *ao;
  452. struct rds_notifier *notifier;
  453. unsigned long flags;
  454. spin_lock_irqsave(&rm->m_rs_lock, flags);
  455. ao = &rm->atomic;
  456. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
  457. && ao->op_active && ao->op_notify && ao->op_notifier) {
  458. notifier = ao->op_notifier;
  459. rs = rm->m_rs;
  460. sock_hold(rds_rs_to_sk(rs));
  461. notifier->n_status = status;
  462. spin_lock(&rs->rs_lock);
  463. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  464. spin_unlock(&rs->rs_lock);
  465. ao->op_notifier = NULL;
  466. }
  467. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  468. if (rs) {
  469. rds_wake_sk_sleep(rs);
  470. sock_put(rds_rs_to_sk(rs));
  471. }
  472. }
  473. EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
  474. /*
  475. * This is the same as rds_rdma_send_complete except we
  476. * don't do any locking - we have all the ingredients (message,
  477. * socket, socket lock) and can just move the notifier.
  478. */
  479. static inline void
  480. __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
  481. {
  482. struct rm_rdma_op *ro;
  483. struct rm_atomic_op *ao;
  484. ro = &rm->rdma;
  485. if (ro->op_active && ro->op_notify && ro->op_notifier) {
  486. ro->op_notifier->n_status = status;
  487. list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
  488. ro->op_notifier = NULL;
  489. }
  490. ao = &rm->atomic;
  491. if (ao->op_active && ao->op_notify && ao->op_notifier) {
  492. ao->op_notifier->n_status = status;
  493. list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
  494. ao->op_notifier = NULL;
  495. }
  496. /* No need to wake the app - caller does this */
  497. }
  498. /*
  499. * This removes messages from the socket's list if they're on it. The list
  500. * argument must be private to the caller, we must be able to modify it
  501. * without locks. The messages must have a reference held for their
  502. * position on the list. This function will drop that reference after
  503. * removing the messages from the 'messages' list regardless of if it found
  504. * the messages on the socket list or not.
  505. */
  506. static void rds_send_remove_from_sock(struct list_head *messages, int status)
  507. {
  508. unsigned long flags;
  509. struct rds_sock *rs = NULL;
  510. struct rds_message *rm;
  511. while (!list_empty(messages)) {
  512. int was_on_sock = 0;
  513. rm = list_entry(messages->next, struct rds_message,
  514. m_conn_item);
  515. list_del_init(&rm->m_conn_item);
  516. /*
  517. * If we see this flag cleared then we're *sure* that someone
  518. * else beat us to removing it from the sock. If we race
  519. * with their flag update we'll get the lock and then really
  520. * see that the flag has been cleared.
  521. *
  522. * The message spinlock makes sure nobody clears rm->m_rs
  523. * while we're messing with it. It does not prevent the
  524. * message from being removed from the socket, though.
  525. */
  526. spin_lock_irqsave(&rm->m_rs_lock, flags);
  527. if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
  528. goto unlock_and_drop;
  529. if (rs != rm->m_rs) {
  530. if (rs) {
  531. rds_wake_sk_sleep(rs);
  532. sock_put(rds_rs_to_sk(rs));
  533. }
  534. rs = rm->m_rs;
  535. if (rs)
  536. sock_hold(rds_rs_to_sk(rs));
  537. }
  538. if (!rs)
  539. goto unlock_and_drop;
  540. spin_lock(&rs->rs_lock);
  541. if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
  542. struct rm_rdma_op *ro = &rm->rdma;
  543. struct rds_notifier *notifier;
  544. list_del_init(&rm->m_sock_item);
  545. rds_send_sndbuf_remove(rs, rm);
  546. if (ro->op_active && ro->op_notifier &&
  547. (ro->op_notify || (ro->op_recverr && status))) {
  548. notifier = ro->op_notifier;
  549. list_add_tail(&notifier->n_list,
  550. &rs->rs_notify_queue);
  551. if (!notifier->n_status)
  552. notifier->n_status = status;
  553. rm->rdma.op_notifier = NULL;
  554. }
  555. was_on_sock = 1;
  556. rm->m_rs = NULL;
  557. }
  558. spin_unlock(&rs->rs_lock);
  559. unlock_and_drop:
  560. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  561. rds_message_put(rm);
  562. if (was_on_sock)
  563. rds_message_put(rm);
  564. }
  565. if (rs) {
  566. rds_wake_sk_sleep(rs);
  567. sock_put(rds_rs_to_sk(rs));
  568. }
  569. }
  570. /*
  571. * Transports call here when they've determined that the receiver queued
  572. * messages up to, and including, the given sequence number. Messages are
  573. * moved to the retrans queue when rds_send_xmit picks them off the send
  574. * queue. This means that in the TCP case, the message may not have been
  575. * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
  576. * checks the RDS_MSG_HAS_ACK_SEQ bit.
  577. */
  578. void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack,
  579. is_acked_func is_acked)
  580. {
  581. struct rds_message *rm, *tmp;
  582. unsigned long flags;
  583. LIST_HEAD(list);
  584. spin_lock_irqsave(&cp->cp_lock, flags);
  585. list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
  586. if (!rds_send_is_acked(rm, ack, is_acked))
  587. break;
  588. list_move(&rm->m_conn_item, &list);
  589. clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  590. }
  591. /* order flag updates with spin locks */
  592. if (!list_empty(&list))
  593. smp_mb__after_atomic();
  594. spin_unlock_irqrestore(&cp->cp_lock, flags);
  595. /* now remove the messages from the sock list as needed */
  596. rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
  597. }
  598. EXPORT_SYMBOL_GPL(rds_send_path_drop_acked);
  599. void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
  600. is_acked_func is_acked)
  601. {
  602. WARN_ON(conn->c_trans->t_mp_capable);
  603. rds_send_path_drop_acked(&conn->c_path[0], ack, is_acked);
  604. }
  605. EXPORT_SYMBOL_GPL(rds_send_drop_acked);
  606. void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
  607. {
  608. struct rds_message *rm, *tmp;
  609. struct rds_connection *conn;
  610. struct rds_conn_path *cp;
  611. unsigned long flags;
  612. LIST_HEAD(list);
  613. /* get all the messages we're dropping under the rs lock */
  614. spin_lock_irqsave(&rs->rs_lock, flags);
  615. list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
  616. if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
  617. dest->sin_port != rm->m_inc.i_hdr.h_dport))
  618. continue;
  619. list_move(&rm->m_sock_item, &list);
  620. rds_send_sndbuf_remove(rs, rm);
  621. clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  622. }
  623. /* order flag updates with the rs lock */
  624. smp_mb__after_atomic();
  625. spin_unlock_irqrestore(&rs->rs_lock, flags);
  626. if (list_empty(&list))
  627. return;
  628. /* Remove the messages from the conn */
  629. list_for_each_entry(rm, &list, m_sock_item) {
  630. conn = rm->m_inc.i_conn;
  631. if (conn->c_trans->t_mp_capable)
  632. cp = rm->m_inc.i_conn_path;
  633. else
  634. cp = &conn->c_path[0];
  635. spin_lock_irqsave(&cp->cp_lock, flags);
  636. /*
  637. * Maybe someone else beat us to removing rm from the conn.
  638. * If we race with their flag update we'll get the lock and
  639. * then really see that the flag has been cleared.
  640. */
  641. if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
  642. spin_unlock_irqrestore(&cp->cp_lock, flags);
  643. spin_lock_irqsave(&rm->m_rs_lock, flags);
  644. rm->m_rs = NULL;
  645. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  646. continue;
  647. }
  648. list_del_init(&rm->m_conn_item);
  649. spin_unlock_irqrestore(&cp->cp_lock, flags);
  650. /*
  651. * Couldn't grab m_rs_lock in top loop (lock ordering),
  652. * but we can now.
  653. */
  654. spin_lock_irqsave(&rm->m_rs_lock, flags);
  655. spin_lock(&rs->rs_lock);
  656. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  657. spin_unlock(&rs->rs_lock);
  658. rm->m_rs = NULL;
  659. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  660. rds_message_put(rm);
  661. }
  662. rds_wake_sk_sleep(rs);
  663. while (!list_empty(&list)) {
  664. rm = list_entry(list.next, struct rds_message, m_sock_item);
  665. list_del_init(&rm->m_sock_item);
  666. rds_message_wait(rm);
  667. /* just in case the code above skipped this message
  668. * because RDS_MSG_ON_CONN wasn't set, run it again here
  669. * taking m_rs_lock is the only thing that keeps us
  670. * from racing with ack processing.
  671. */
  672. spin_lock_irqsave(&rm->m_rs_lock, flags);
  673. spin_lock(&rs->rs_lock);
  674. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  675. spin_unlock(&rs->rs_lock);
  676. rm->m_rs = NULL;
  677. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  678. rds_message_put(rm);
  679. }
  680. }
  681. /*
  682. * we only want this to fire once so we use the callers 'queued'. It's
  683. * possible that another thread can race with us and remove the
  684. * message from the flow with RDS_CANCEL_SENT_TO.
  685. */
  686. static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
  687. struct rds_conn_path *cp,
  688. struct rds_message *rm, __be16 sport,
  689. __be16 dport, int *queued)
  690. {
  691. unsigned long flags;
  692. u32 len;
  693. if (*queued)
  694. goto out;
  695. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  696. /* this is the only place which holds both the socket's rs_lock
  697. * and the connection's c_lock */
  698. spin_lock_irqsave(&rs->rs_lock, flags);
  699. /*
  700. * If there is a little space in sndbuf, we don't queue anything,
  701. * and userspace gets -EAGAIN. But poll() indicates there's send
  702. * room. This can lead to bad behavior (spinning) if snd_bytes isn't
  703. * freed up by incoming acks. So we check the *old* value of
  704. * rs_snd_bytes here to allow the last msg to exceed the buffer,
  705. * and poll() now knows no more data can be sent.
  706. */
  707. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
  708. rs->rs_snd_bytes += len;
  709. /* let recv side know we are close to send space exhaustion.
  710. * This is probably not the optimal way to do it, as this
  711. * means we set the flag on *all* messages as soon as our
  712. * throughput hits a certain threshold.
  713. */
  714. if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
  715. __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  716. list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
  717. set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  718. rds_message_addref(rm);
  719. rm->m_rs = rs;
  720. /* The code ordering is a little weird, but we're
  721. trying to minimize the time we hold c_lock */
  722. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
  723. rm->m_inc.i_conn = conn;
  724. rm->m_inc.i_conn_path = cp;
  725. rds_message_addref(rm);
  726. spin_lock(&cp->cp_lock);
  727. rm->m_inc.i_hdr.h_sequence = cpu_to_be64(cp->cp_next_tx_seq++);
  728. list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
  729. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  730. spin_unlock(&cp->cp_lock);
  731. rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
  732. rm, len, rs, rs->rs_snd_bytes,
  733. (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
  734. *queued = 1;
  735. }
  736. spin_unlock_irqrestore(&rs->rs_lock, flags);
  737. out:
  738. return *queued;
  739. }
  740. /*
  741. * rds_message is getting to be quite complicated, and we'd like to allocate
  742. * it all in one go. This figures out how big it needs to be up front.
  743. */
  744. static int rds_rm_size(struct msghdr *msg, int data_len)
  745. {
  746. struct cmsghdr *cmsg;
  747. int size = 0;
  748. int cmsg_groups = 0;
  749. int retval;
  750. for_each_cmsghdr(cmsg, msg) {
  751. if (!CMSG_OK(msg, cmsg))
  752. return -EINVAL;
  753. if (cmsg->cmsg_level != SOL_RDS)
  754. continue;
  755. switch (cmsg->cmsg_type) {
  756. case RDS_CMSG_RDMA_ARGS:
  757. cmsg_groups |= 1;
  758. retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
  759. if (retval < 0)
  760. return retval;
  761. size += retval;
  762. break;
  763. case RDS_CMSG_RDMA_DEST:
  764. case RDS_CMSG_RDMA_MAP:
  765. cmsg_groups |= 2;
  766. /* these are valid but do no add any size */
  767. break;
  768. case RDS_CMSG_ATOMIC_CSWP:
  769. case RDS_CMSG_ATOMIC_FADD:
  770. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  771. case RDS_CMSG_MASKED_ATOMIC_FADD:
  772. cmsg_groups |= 1;
  773. size += sizeof(struct scatterlist);
  774. break;
  775. default:
  776. return -EINVAL;
  777. }
  778. }
  779. size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
  780. /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
  781. if (cmsg_groups == 3)
  782. return -EINVAL;
  783. return size;
  784. }
  785. static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
  786. struct msghdr *msg, int *allocated_mr)
  787. {
  788. struct cmsghdr *cmsg;
  789. int ret = 0;
  790. for_each_cmsghdr(cmsg, msg) {
  791. if (!CMSG_OK(msg, cmsg))
  792. return -EINVAL;
  793. if (cmsg->cmsg_level != SOL_RDS)
  794. continue;
  795. /* As a side effect, RDMA_DEST and RDMA_MAP will set
  796. * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
  797. */
  798. switch (cmsg->cmsg_type) {
  799. case RDS_CMSG_RDMA_ARGS:
  800. ret = rds_cmsg_rdma_args(rs, rm, cmsg);
  801. break;
  802. case RDS_CMSG_RDMA_DEST:
  803. ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
  804. break;
  805. case RDS_CMSG_RDMA_MAP:
  806. ret = rds_cmsg_rdma_map(rs, rm, cmsg);
  807. if (!ret)
  808. *allocated_mr = 1;
  809. else if (ret == -ENODEV)
  810. /* Accommodate the get_mr() case which can fail
  811. * if connection isn't established yet.
  812. */
  813. ret = -EAGAIN;
  814. break;
  815. case RDS_CMSG_ATOMIC_CSWP:
  816. case RDS_CMSG_ATOMIC_FADD:
  817. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  818. case RDS_CMSG_MASKED_ATOMIC_FADD:
  819. ret = rds_cmsg_atomic(rs, rm, cmsg);
  820. break;
  821. default:
  822. return -EINVAL;
  823. }
  824. if (ret)
  825. break;
  826. }
  827. return ret;
  828. }
  829. static int rds_send_mprds_hash(struct rds_sock *rs, struct rds_connection *conn)
  830. {
  831. int hash;
  832. if (conn->c_npaths == 0)
  833. hash = RDS_MPATH_HASH(rs, RDS_MPATH_WORKERS);
  834. else
  835. hash = RDS_MPATH_HASH(rs, conn->c_npaths);
  836. if (conn->c_npaths == 0 && hash != 0) {
  837. rds_send_ping(conn, 0);
  838. if (conn->c_npaths == 0) {
  839. wait_event_interruptible(conn->c_hs_waitq,
  840. (conn->c_npaths != 0));
  841. }
  842. if (conn->c_npaths == 1)
  843. hash = 0;
  844. }
  845. return hash;
  846. }
  847. static int rds_rdma_bytes(struct msghdr *msg, size_t *rdma_bytes)
  848. {
  849. struct rds_rdma_args *args;
  850. struct cmsghdr *cmsg;
  851. for_each_cmsghdr(cmsg, msg) {
  852. if (!CMSG_OK(msg, cmsg))
  853. return -EINVAL;
  854. if (cmsg->cmsg_level != SOL_RDS)
  855. continue;
  856. if (cmsg->cmsg_type == RDS_CMSG_RDMA_ARGS) {
  857. args = CMSG_DATA(cmsg);
  858. *rdma_bytes += args->remote_vec.bytes;
  859. }
  860. }
  861. return 0;
  862. }
  863. int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
  864. {
  865. struct sock *sk = sock->sk;
  866. struct rds_sock *rs = rds_sk_to_rs(sk);
  867. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  868. __be32 daddr;
  869. __be16 dport;
  870. struct rds_message *rm = NULL;
  871. struct rds_connection *conn;
  872. int ret = 0;
  873. int queued = 0, allocated_mr = 0;
  874. int nonblock = msg->msg_flags & MSG_DONTWAIT;
  875. long timeo = sock_sndtimeo(sk, nonblock);
  876. struct rds_conn_path *cpath;
  877. size_t total_payload_len = payload_len, rdma_payload_len = 0;
  878. /* Mirror Linux UDP mirror of BSD error message compatibility */
  879. /* XXX: Perhaps MSG_MORE someday */
  880. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
  881. ret = -EOPNOTSUPP;
  882. goto out;
  883. }
  884. if (msg->msg_namelen) {
  885. /* XXX fail non-unicast destination IPs? */
  886. if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
  887. ret = -EINVAL;
  888. goto out;
  889. }
  890. daddr = usin->sin_addr.s_addr;
  891. dport = usin->sin_port;
  892. } else {
  893. /* We only care about consistency with ->connect() */
  894. lock_sock(sk);
  895. daddr = rs->rs_conn_addr;
  896. dport = rs->rs_conn_port;
  897. release_sock(sk);
  898. }
  899. lock_sock(sk);
  900. if (daddr == 0 || rs->rs_bound_addr == 0) {
  901. release_sock(sk);
  902. ret = -ENOTCONN; /* XXX not a great errno */
  903. goto out;
  904. }
  905. release_sock(sk);
  906. ret = rds_rdma_bytes(msg, &rdma_payload_len);
  907. if (ret)
  908. goto out;
  909. total_payload_len += rdma_payload_len;
  910. if (max_t(size_t, payload_len, rdma_payload_len) > RDS_MAX_MSG_SIZE) {
  911. ret = -EMSGSIZE;
  912. goto out;
  913. }
  914. if (payload_len > rds_sk_sndbuf(rs)) {
  915. ret = -EMSGSIZE;
  916. goto out;
  917. }
  918. /* size of rm including all sgs */
  919. ret = rds_rm_size(msg, payload_len);
  920. if (ret < 0)
  921. goto out;
  922. rm = rds_message_alloc(ret, GFP_KERNEL);
  923. if (!rm) {
  924. ret = -ENOMEM;
  925. goto out;
  926. }
  927. /* Attach data to the rm */
  928. if (payload_len) {
  929. rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
  930. if (!rm->data.op_sg) {
  931. ret = -ENOMEM;
  932. goto out;
  933. }
  934. ret = rds_message_copy_from_user(rm, &msg->msg_iter);
  935. if (ret)
  936. goto out;
  937. }
  938. rm->data.op_active = 1;
  939. rm->m_daddr = daddr;
  940. /* rds_conn_create has a spinlock that runs with IRQ off.
  941. * Caching the conn in the socket helps a lot. */
  942. if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
  943. conn = rs->rs_conn;
  944. else {
  945. conn = rds_conn_create_outgoing(sock_net(sock->sk),
  946. rs->rs_bound_addr, daddr,
  947. rs->rs_transport,
  948. sock->sk->sk_allocation);
  949. if (IS_ERR(conn)) {
  950. ret = PTR_ERR(conn);
  951. goto out;
  952. }
  953. rs->rs_conn = conn;
  954. }
  955. /* Parse any control messages the user may have included. */
  956. ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
  957. if (ret) {
  958. /* Trigger connection so that its ready for the next retry */
  959. if (ret == -EAGAIN)
  960. rds_conn_connect_if_down(conn);
  961. goto out;
  962. }
  963. if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
  964. printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
  965. &rm->rdma, conn->c_trans->xmit_rdma);
  966. ret = -EOPNOTSUPP;
  967. goto out;
  968. }
  969. if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
  970. printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
  971. &rm->atomic, conn->c_trans->xmit_atomic);
  972. ret = -EOPNOTSUPP;
  973. goto out;
  974. }
  975. if (conn->c_trans->t_mp_capable)
  976. cpath = &conn->c_path[rds_send_mprds_hash(rs, conn)];
  977. else
  978. cpath = &conn->c_path[0];
  979. rds_conn_path_connect_if_down(cpath);
  980. ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
  981. if (ret) {
  982. rs->rs_seen_congestion = 1;
  983. goto out;
  984. }
  985. while (!rds_send_queue_rm(rs, conn, cpath, rm, rs->rs_bound_port,
  986. dport, &queued)) {
  987. rds_stats_inc(s_send_queue_full);
  988. if (nonblock) {
  989. ret = -EAGAIN;
  990. goto out;
  991. }
  992. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  993. rds_send_queue_rm(rs, conn, cpath, rm,
  994. rs->rs_bound_port,
  995. dport,
  996. &queued),
  997. timeo);
  998. rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
  999. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  1000. continue;
  1001. ret = timeo;
  1002. if (ret == 0)
  1003. ret = -ETIMEDOUT;
  1004. goto out;
  1005. }
  1006. /*
  1007. * By now we've committed to the send. We reuse rds_send_worker()
  1008. * to retry sends in the rds thread if the transport asks us to.
  1009. */
  1010. rds_stats_inc(s_send_queued);
  1011. ret = rds_send_xmit(cpath);
  1012. if (ret == -ENOMEM || ret == -EAGAIN)
  1013. queue_delayed_work(rds_wq, &cpath->cp_send_w, 1);
  1014. rds_message_put(rm);
  1015. return payload_len;
  1016. out:
  1017. /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
  1018. * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
  1019. * or in any other way, we need to destroy the MR again */
  1020. if (allocated_mr)
  1021. rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
  1022. if (rm)
  1023. rds_message_put(rm);
  1024. return ret;
  1025. }
  1026. /*
  1027. * send out a probe. Can be shared by rds_send_ping,
  1028. * rds_send_pong, rds_send_hb.
  1029. * rds_send_hb should use h_flags
  1030. * RDS_FLAG_HB_PING|RDS_FLAG_ACK_REQUIRED
  1031. * or
  1032. * RDS_FLAG_HB_PONG|RDS_FLAG_ACK_REQUIRED
  1033. */
  1034. static int
  1035. rds_send_probe(struct rds_conn_path *cp, __be16 sport,
  1036. __be16 dport, u8 h_flags)
  1037. {
  1038. struct rds_message *rm;
  1039. unsigned long flags;
  1040. int ret = 0;
  1041. rm = rds_message_alloc(0, GFP_ATOMIC);
  1042. if (!rm) {
  1043. ret = -ENOMEM;
  1044. goto out;
  1045. }
  1046. rm->m_daddr = cp->cp_conn->c_faddr;
  1047. rm->data.op_active = 1;
  1048. rds_conn_path_connect_if_down(cp);
  1049. ret = rds_cong_wait(cp->cp_conn->c_fcong, dport, 1, NULL);
  1050. if (ret)
  1051. goto out;
  1052. spin_lock_irqsave(&cp->cp_lock, flags);
  1053. list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
  1054. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  1055. rds_message_addref(rm);
  1056. rm->m_inc.i_conn = cp->cp_conn;
  1057. rm->m_inc.i_conn_path = cp;
  1058. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport,
  1059. cp->cp_next_tx_seq);
  1060. rm->m_inc.i_hdr.h_flags |= h_flags;
  1061. cp->cp_next_tx_seq++;
  1062. if (RDS_HS_PROBE(be16_to_cpu(sport), be16_to_cpu(dport)) &&
  1063. cp->cp_conn->c_trans->t_mp_capable) {
  1064. u16 npaths = cpu_to_be16(RDS_MPATH_WORKERS);
  1065. u32 my_gen_num = cpu_to_be32(cp->cp_conn->c_my_gen_num);
  1066. rds_message_add_extension(&rm->m_inc.i_hdr,
  1067. RDS_EXTHDR_NPATHS, &npaths,
  1068. sizeof(npaths));
  1069. rds_message_add_extension(&rm->m_inc.i_hdr,
  1070. RDS_EXTHDR_GEN_NUM,
  1071. &my_gen_num,
  1072. sizeof(u32));
  1073. }
  1074. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1075. rds_stats_inc(s_send_queued);
  1076. rds_stats_inc(s_send_pong);
  1077. /* schedule the send work on rds_wq */
  1078. queue_delayed_work(rds_wq, &cp->cp_send_w, 1);
  1079. rds_message_put(rm);
  1080. return 0;
  1081. out:
  1082. if (rm)
  1083. rds_message_put(rm);
  1084. return ret;
  1085. }
  1086. int
  1087. rds_send_pong(struct rds_conn_path *cp, __be16 dport)
  1088. {
  1089. return rds_send_probe(cp, 0, dport, 0);
  1090. }
  1091. void
  1092. rds_send_ping(struct rds_connection *conn, int cp_index)
  1093. {
  1094. unsigned long flags;
  1095. struct rds_conn_path *cp = &conn->c_path[cp_index];
  1096. spin_lock_irqsave(&cp->cp_lock, flags);
  1097. if (conn->c_ping_triggered) {
  1098. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1099. return;
  1100. }
  1101. conn->c_ping_triggered = 1;
  1102. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1103. rds_send_probe(cp, cpu_to_be16(RDS_FLAG_PROBE_PORT), 0, 0);
  1104. }
  1105. EXPORT_SYMBOL_GPL(rds_send_ping);