send.c 31 KB

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