send.c 30 KB

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