send.c 28 KB

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