send.c 31 KB

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