send.c 31 KB

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