send.c 30 KB

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