send.c 31 KB

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