send.c 30 KB

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