send.c 30 KB

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