send.c 31 KB

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