send.c 29 KB

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