send.c 31 KB

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