send.c 31 KB

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