send.c 30 KB

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