send.c 30 KB

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