send.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 <net/sock.h>
  35. #include <linux/in.h>
  36. #include <linux/list.h>
  37. #include "rds.h"
  38. #include "rdma.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 != NULL &&
  154. conn->c_xmit_hdr_off == sizeof(struct rds_header) &&
  155. conn->c_xmit_sg == rm->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 == NULL && test_and_clear_bit(0, &conn->c_map_queued)) {
  168. if (conn->c_trans->xmit_cong_map != NULL) {
  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 == NULL) {
  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 == NULL) {
  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->m_rdma_op &&
  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->m_rdma_op && !conn->c_xmit_rdma_sent) {
  243. ret = conn->c_trans->xmit_rdma(conn, rm->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->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->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->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. spin_lock(&rm->m_rs_lock);
  370. ro = rm->m_rdma_op;
  371. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
  372. ro && ro->r_notify && ro->r_notifier) {
  373. notifier = ro->r_notifier;
  374. rs = rm->m_rs;
  375. sock_hold(rds_rs_to_sk(rs));
  376. notifier->n_status = status;
  377. spin_lock(&rs->rs_lock);
  378. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  379. spin_unlock(&rs->rs_lock);
  380. ro->r_notifier = NULL;
  381. }
  382. spin_unlock(&rm->m_rs_lock);
  383. if (rs) {
  384. rds_wake_sk_sleep(rs);
  385. sock_put(rds_rs_to_sk(rs));
  386. }
  387. }
  388. EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
  389. /*
  390. * This is the same as rds_rdma_send_complete except we
  391. * don't do any locking - we have all the ingredients (message,
  392. * socket, socket lock) and can just move the notifier.
  393. */
  394. static inline void
  395. __rds_rdma_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
  396. {
  397. struct rds_rdma_op *ro;
  398. ro = rm->m_rdma_op;
  399. if (ro && ro->r_notify && ro->r_notifier) {
  400. ro->r_notifier->n_status = status;
  401. list_add_tail(&ro->r_notifier->n_list, &rs->rs_notify_queue);
  402. ro->r_notifier = NULL;
  403. }
  404. /* No need to wake the app - caller does this */
  405. }
  406. /*
  407. * This is called from the IB send completion when we detect
  408. * a RDMA operation that failed with remote access error.
  409. * So speed is not an issue here.
  410. */
  411. struct rds_message *rds_send_get_message(struct rds_connection *conn,
  412. struct rds_rdma_op *op)
  413. {
  414. struct rds_message *rm, *tmp, *found = NULL;
  415. unsigned long flags;
  416. spin_lock_irqsave(&conn->c_lock, flags);
  417. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  418. if (rm->m_rdma_op == op) {
  419. atomic_inc(&rm->m_refcount);
  420. found = rm;
  421. goto out;
  422. }
  423. }
  424. list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
  425. if (rm->m_rdma_op == op) {
  426. atomic_inc(&rm->m_refcount);
  427. found = rm;
  428. break;
  429. }
  430. }
  431. out:
  432. spin_unlock_irqrestore(&conn->c_lock, flags);
  433. return found;
  434. }
  435. EXPORT_SYMBOL_GPL(rds_send_get_message);
  436. /*
  437. * This removes messages from the socket's list if they're on it. The list
  438. * argument must be private to the caller, we must be able to modify it
  439. * without locks. The messages must have a reference held for their
  440. * position on the list. This function will drop that reference after
  441. * removing the messages from the 'messages' list regardless of if it found
  442. * the messages on the socket list or not.
  443. */
  444. void rds_send_remove_from_sock(struct list_head *messages, int status)
  445. {
  446. unsigned long flags = 0; /* silence gcc :P */
  447. struct rds_sock *rs = NULL;
  448. struct rds_message *rm;
  449. local_irq_save(flags);
  450. while (!list_empty(messages)) {
  451. rm = list_entry(messages->next, struct rds_message,
  452. m_conn_item);
  453. list_del_init(&rm->m_conn_item);
  454. /*
  455. * If we see this flag cleared then we're *sure* that someone
  456. * else beat us to removing it from the sock. If we race
  457. * with their flag update we'll get the lock and then really
  458. * see that the flag has been cleared.
  459. *
  460. * The message spinlock makes sure nobody clears rm->m_rs
  461. * while we're messing with it. It does not prevent the
  462. * message from being removed from the socket, though.
  463. */
  464. spin_lock(&rm->m_rs_lock);
  465. if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
  466. goto unlock_and_drop;
  467. if (rs != rm->m_rs) {
  468. if (rs) {
  469. rds_wake_sk_sleep(rs);
  470. sock_put(rds_rs_to_sk(rs));
  471. }
  472. rs = rm->m_rs;
  473. sock_hold(rds_rs_to_sk(rs));
  474. }
  475. spin_lock(&rs->rs_lock);
  476. if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
  477. struct rds_rdma_op *ro = rm->m_rdma_op;
  478. struct rds_notifier *notifier;
  479. list_del_init(&rm->m_sock_item);
  480. rds_send_sndbuf_remove(rs, rm);
  481. if (ro && ro->r_notifier && (status || ro->r_notify)) {
  482. notifier = ro->r_notifier;
  483. list_add_tail(&notifier->n_list,
  484. &rs->rs_notify_queue);
  485. if (!notifier->n_status)
  486. notifier->n_status = status;
  487. rm->m_rdma_op->r_notifier = NULL;
  488. }
  489. rds_message_put(rm);
  490. rm->m_rs = NULL;
  491. }
  492. spin_unlock(&rs->rs_lock);
  493. unlock_and_drop:
  494. spin_unlock(&rm->m_rs_lock);
  495. rds_message_put(rm);
  496. }
  497. if (rs) {
  498. rds_wake_sk_sleep(rs);
  499. sock_put(rds_rs_to_sk(rs));
  500. }
  501. local_irq_restore(flags);
  502. }
  503. /*
  504. * Transports call here when they've determined that the receiver queued
  505. * messages up to, and including, the given sequence number. Messages are
  506. * moved to the retrans queue when rds_send_xmit picks them off the send
  507. * queue. This means that in the TCP case, the message may not have been
  508. * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
  509. * checks the RDS_MSG_HAS_ACK_SEQ bit.
  510. *
  511. * XXX It's not clear to me how this is safely serialized with socket
  512. * destruction. Maybe it should bail if it sees SOCK_DEAD.
  513. */
  514. void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
  515. is_acked_func is_acked)
  516. {
  517. struct rds_message *rm, *tmp;
  518. unsigned long flags;
  519. LIST_HEAD(list);
  520. spin_lock_irqsave(&conn->c_lock, flags);
  521. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  522. if (!rds_send_is_acked(rm, ack, is_acked))
  523. break;
  524. list_move(&rm->m_conn_item, &list);
  525. clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  526. }
  527. /* order flag updates with spin locks */
  528. if (!list_empty(&list))
  529. smp_mb__after_clear_bit();
  530. spin_unlock_irqrestore(&conn->c_lock, flags);
  531. /* now remove the messages from the sock list as needed */
  532. rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
  533. }
  534. EXPORT_SYMBOL_GPL(rds_send_drop_acked);
  535. void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
  536. {
  537. struct rds_message *rm, *tmp;
  538. struct rds_connection *conn;
  539. unsigned long flags, flags2;
  540. LIST_HEAD(list);
  541. int wake = 0;
  542. /* get all the messages we're dropping under the rs lock */
  543. spin_lock_irqsave(&rs->rs_lock, flags);
  544. list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
  545. if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
  546. dest->sin_port != rm->m_inc.i_hdr.h_dport))
  547. continue;
  548. wake = 1;
  549. list_move(&rm->m_sock_item, &list);
  550. rds_send_sndbuf_remove(rs, rm);
  551. clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  552. /* If this is a RDMA operation, notify the app. */
  553. __rds_rdma_send_complete(rs, rm, RDS_RDMA_CANCELED);
  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. if (wake)
  560. rds_wake_sk_sleep(rs);
  561. conn = NULL;
  562. /* now remove the messages from the conn list as needed */
  563. list_for_each_entry(rm, &list, m_sock_item) {
  564. /* We do this here rather than in the loop above, so that
  565. * we don't have to nest m_rs_lock under rs->rs_lock */
  566. spin_lock_irqsave(&rm->m_rs_lock, flags2);
  567. rm->m_rs = NULL;
  568. spin_unlock_irqrestore(&rm->m_rs_lock, flags2);
  569. /*
  570. * If we see this flag cleared then we're *sure* that someone
  571. * else beat us to removing it from the conn. If we race
  572. * with their flag update we'll get the lock and then really
  573. * see that the flag has been cleared.
  574. */
  575. if (!test_bit(RDS_MSG_ON_CONN, &rm->m_flags))
  576. continue;
  577. if (conn != rm->m_inc.i_conn) {
  578. if (conn)
  579. spin_unlock_irqrestore(&conn->c_lock, flags);
  580. conn = rm->m_inc.i_conn;
  581. spin_lock_irqsave(&conn->c_lock, flags);
  582. }
  583. if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
  584. list_del_init(&rm->m_conn_item);
  585. rds_message_put(rm);
  586. }
  587. }
  588. if (conn)
  589. spin_unlock_irqrestore(&conn->c_lock, flags);
  590. while (!list_empty(&list)) {
  591. rm = list_entry(list.next, struct rds_message, m_sock_item);
  592. list_del_init(&rm->m_sock_item);
  593. rds_message_wait(rm);
  594. rds_message_put(rm);
  595. }
  596. }
  597. /*
  598. * we only want this to fire once so we use the callers 'queued'. It's
  599. * possible that another thread can race with us and remove the
  600. * message from the flow with RDS_CANCEL_SENT_TO.
  601. */
  602. static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
  603. struct rds_message *rm, __be16 sport,
  604. __be16 dport, int *queued)
  605. {
  606. unsigned long flags;
  607. u32 len;
  608. if (*queued)
  609. goto out;
  610. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  611. /* this is the only place which holds both the socket's rs_lock
  612. * and the connection's c_lock */
  613. spin_lock_irqsave(&rs->rs_lock, flags);
  614. /*
  615. * If there is a little space in sndbuf, we don't queue anything,
  616. * and userspace gets -EAGAIN. But poll() indicates there's send
  617. * room. This can lead to bad behavior (spinning) if snd_bytes isn't
  618. * freed up by incoming acks. So we check the *old* value of
  619. * rs_snd_bytes here to allow the last msg to exceed the buffer,
  620. * and poll() now knows no more data can be sent.
  621. */
  622. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
  623. rs->rs_snd_bytes += len;
  624. /* let recv side know we are close to send space exhaustion.
  625. * This is probably not the optimal way to do it, as this
  626. * means we set the flag on *all* messages as soon as our
  627. * throughput hits a certain threshold.
  628. */
  629. if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
  630. __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  631. list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
  632. set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  633. rds_message_addref(rm);
  634. rm->m_rs = rs;
  635. /* The code ordering is a little weird, but we're
  636. trying to minimize the time we hold c_lock */
  637. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
  638. rm->m_inc.i_conn = conn;
  639. rds_message_addref(rm);
  640. spin_lock(&conn->c_lock);
  641. rm->m_inc.i_hdr.h_sequence = cpu_to_be64(conn->c_next_tx_seq++);
  642. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  643. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  644. spin_unlock(&conn->c_lock);
  645. rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
  646. rm, len, rs, rs->rs_snd_bytes,
  647. (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
  648. *queued = 1;
  649. }
  650. spin_unlock_irqrestore(&rs->rs_lock, flags);
  651. out:
  652. return *queued;
  653. }
  654. static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
  655. struct msghdr *msg, int *allocated_mr)
  656. {
  657. struct cmsghdr *cmsg;
  658. int ret = 0;
  659. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  660. if (!CMSG_OK(msg, cmsg))
  661. return -EINVAL;
  662. if (cmsg->cmsg_level != SOL_RDS)
  663. continue;
  664. /* As a side effect, RDMA_DEST and RDMA_MAP will set
  665. * rm->m_rdma_cookie and rm->m_rdma_mr.
  666. */
  667. switch (cmsg->cmsg_type) {
  668. case RDS_CMSG_RDMA_ARGS:
  669. ret = rds_cmsg_rdma_args(rs, rm, cmsg);
  670. break;
  671. case RDS_CMSG_RDMA_DEST:
  672. ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
  673. break;
  674. case RDS_CMSG_RDMA_MAP:
  675. ret = rds_cmsg_rdma_map(rs, rm, cmsg);
  676. if (!ret)
  677. *allocated_mr = 1;
  678. break;
  679. default:
  680. return -EINVAL;
  681. }
  682. if (ret)
  683. break;
  684. }
  685. return ret;
  686. }
  687. int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  688. size_t payload_len)
  689. {
  690. struct sock *sk = sock->sk;
  691. struct rds_sock *rs = rds_sk_to_rs(sk);
  692. struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
  693. __be32 daddr;
  694. __be16 dport;
  695. struct rds_message *rm = NULL;
  696. struct rds_connection *conn;
  697. int ret = 0;
  698. int queued = 0, allocated_mr = 0;
  699. int nonblock = msg->msg_flags & MSG_DONTWAIT;
  700. long timeo = sock_sndtimeo(sk, nonblock);
  701. /* Mirror Linux UDP mirror of BSD error message compatibility */
  702. /* XXX: Perhaps MSG_MORE someday */
  703. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
  704. printk(KERN_INFO "msg_flags 0x%08X\n", msg->msg_flags);
  705. ret = -EOPNOTSUPP;
  706. goto out;
  707. }
  708. if (msg->msg_namelen) {
  709. /* XXX fail non-unicast destination IPs? */
  710. if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
  711. ret = -EINVAL;
  712. goto out;
  713. }
  714. daddr = usin->sin_addr.s_addr;
  715. dport = usin->sin_port;
  716. } else {
  717. /* We only care about consistency with ->connect() */
  718. lock_sock(sk);
  719. daddr = rs->rs_conn_addr;
  720. dport = rs->rs_conn_port;
  721. release_sock(sk);
  722. }
  723. /* racing with another thread binding seems ok here */
  724. if (daddr == 0 || rs->rs_bound_addr == 0) {
  725. ret = -ENOTCONN; /* XXX not a great errno */
  726. goto out;
  727. }
  728. rm = rds_message_copy_from_user(msg->msg_iov, payload_len);
  729. if (IS_ERR(rm)) {
  730. ret = PTR_ERR(rm);
  731. rm = NULL;
  732. goto out;
  733. }
  734. rm->m_daddr = daddr;
  735. /* rds_conn_create has a spinlock that runs with IRQ off.
  736. * Caching the conn in the socket helps a lot. */
  737. if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
  738. conn = rs->rs_conn;
  739. else {
  740. conn = rds_conn_create_outgoing(rs->rs_bound_addr, daddr,
  741. rs->rs_transport,
  742. sock->sk->sk_allocation);
  743. if (IS_ERR(conn)) {
  744. ret = PTR_ERR(conn);
  745. goto out;
  746. }
  747. rs->rs_conn = conn;
  748. }
  749. /* Parse any control messages the user may have included. */
  750. ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
  751. if (ret)
  752. goto out;
  753. if ((rm->m_rdma_cookie || rm->m_rdma_op) &&
  754. conn->c_trans->xmit_rdma == NULL) {
  755. if (printk_ratelimit())
  756. printk(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
  757. rm->m_rdma_op, conn->c_trans->xmit_rdma);
  758. ret = -EOPNOTSUPP;
  759. goto out;
  760. }
  761. /* If the connection is down, trigger a connect. We may
  762. * have scheduled a delayed reconnect however - in this case
  763. * we should not interfere.
  764. */
  765. if (rds_conn_state(conn) == RDS_CONN_DOWN &&
  766. !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
  767. queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
  768. ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
  769. if (ret)
  770. goto out;
  771. while (!rds_send_queue_rm(rs, conn, rm, rs->rs_bound_port,
  772. dport, &queued)) {
  773. rds_stats_inc(s_send_queue_full);
  774. /* XXX make sure this is reasonable */
  775. if (payload_len > rds_sk_sndbuf(rs)) {
  776. ret = -EMSGSIZE;
  777. goto out;
  778. }
  779. if (nonblock) {
  780. ret = -EAGAIN;
  781. goto out;
  782. }
  783. timeo = wait_event_interruptible_timeout(*sk->sk_sleep,
  784. rds_send_queue_rm(rs, conn, rm,
  785. rs->rs_bound_port,
  786. dport,
  787. &queued),
  788. timeo);
  789. rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
  790. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  791. continue;
  792. ret = timeo;
  793. if (ret == 0)
  794. ret = -ETIMEDOUT;
  795. goto out;
  796. }
  797. /*
  798. * By now we've committed to the send. We reuse rds_send_worker()
  799. * to retry sends in the rds thread if the transport asks us to.
  800. */
  801. rds_stats_inc(s_send_queued);
  802. if (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags))
  803. rds_send_worker(&conn->c_send_w.work);
  804. rds_message_put(rm);
  805. return payload_len;
  806. out:
  807. /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
  808. * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
  809. * or in any other way, we need to destroy the MR again */
  810. if (allocated_mr)
  811. rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
  812. if (rm)
  813. rds_message_put(rm);
  814. return ret;
  815. }
  816. /*
  817. * Reply to a ping packet.
  818. */
  819. int
  820. rds_send_pong(struct rds_connection *conn, __be16 dport)
  821. {
  822. struct rds_message *rm;
  823. unsigned long flags;
  824. int ret = 0;
  825. rm = rds_message_alloc(0, GFP_ATOMIC);
  826. if (rm == NULL) {
  827. ret = -ENOMEM;
  828. goto out;
  829. }
  830. rm->m_daddr = conn->c_faddr;
  831. /* If the connection is down, trigger a connect. We may
  832. * have scheduled a delayed reconnect however - in this case
  833. * we should not interfere.
  834. */
  835. if (rds_conn_state(conn) == RDS_CONN_DOWN &&
  836. !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
  837. queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
  838. ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
  839. if (ret)
  840. goto out;
  841. spin_lock_irqsave(&conn->c_lock, flags);
  842. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  843. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  844. rds_message_addref(rm);
  845. rm->m_inc.i_conn = conn;
  846. rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
  847. conn->c_next_tx_seq);
  848. conn->c_next_tx_seq++;
  849. spin_unlock_irqrestore(&conn->c_lock, flags);
  850. rds_stats_inc(s_send_queued);
  851. rds_stats_inc(s_send_pong);
  852. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  853. rds_message_put(rm);
  854. return 0;
  855. out:
  856. if (rm)
  857. rds_message_put(rm);
  858. return ret;
  859. }