send.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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(&conn->c_lock);
  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(&conn->c_lock);
  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(&conn->c_lock);
  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(&conn->c_lock);
  184. continue;
  185. }
  186. /* Require an ACK every once in a while */
  187. len = ntohl(rm->m_inc.i_hdr.h_len);
  188. if (conn->c_unacked_packets == 0 ||
  189. conn->c_unacked_bytes < len) {
  190. __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  191. conn->c_unacked_packets = rds_sysctl_max_unacked_packets;
  192. conn->c_unacked_bytes = rds_sysctl_max_unacked_bytes;
  193. rds_stats_inc(s_send_ack_required);
  194. } else {
  195. conn->c_unacked_bytes -= len;
  196. conn->c_unacked_packets--;
  197. }
  198. conn->c_xmit_rm = rm;
  199. }
  200. /* The transport either sends the whole rdma or none of it */
  201. if (rm->rdma.op_active && !conn->c_xmit_rdma_sent) {
  202. rm->m_final_op = &rm->rdma;
  203. ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
  204. if (ret)
  205. break;
  206. conn->c_xmit_rdma_sent = 1;
  207. /* The transport owns the mapped memory for now.
  208. * You can't unmap it while it's on the send queue */
  209. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  210. }
  211. if (rm->atomic.op_active && !conn->c_xmit_atomic_sent) {
  212. rm->m_final_op = &rm->atomic;
  213. ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
  214. if (ret)
  215. break;
  216. conn->c_xmit_atomic_sent = 1;
  217. /* The transport owns the mapped memory for now.
  218. * You can't unmap it while it's on the send queue */
  219. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  220. }
  221. /*
  222. * A number of cases require an RDS header to be sent
  223. * even if there is no data.
  224. * We permit 0-byte sends; rds-ping depends on this.
  225. * However, if there are exclusively attached silent ops,
  226. * we skip the hdr/data send, to enable silent operation.
  227. */
  228. if (rm->data.op_nents == 0) {
  229. int ops_present;
  230. int all_ops_are_silent = 1;
  231. ops_present = (rm->atomic.op_active || rm->rdma.op_active);
  232. if (rm->atomic.op_active && !rm->atomic.op_silent)
  233. all_ops_are_silent = 0;
  234. if (rm->rdma.op_active && !rm->rdma.op_silent)
  235. all_ops_are_silent = 0;
  236. if (ops_present && all_ops_are_silent
  237. && !rm->m_rdma_cookie)
  238. rm->data.op_active = 0;
  239. }
  240. if (rm->data.op_active && !conn->c_xmit_data_sent) {
  241. rm->m_final_op = &rm->data;
  242. ret = conn->c_trans->xmit(conn, rm,
  243. conn->c_xmit_hdr_off,
  244. conn->c_xmit_sg,
  245. conn->c_xmit_data_off);
  246. if (ret <= 0)
  247. break;
  248. if (conn->c_xmit_hdr_off < sizeof(struct rds_header)) {
  249. tmp = min_t(int, ret,
  250. sizeof(struct rds_header) -
  251. conn->c_xmit_hdr_off);
  252. conn->c_xmit_hdr_off += tmp;
  253. ret -= tmp;
  254. }
  255. sg = &rm->data.op_sg[conn->c_xmit_sg];
  256. while (ret) {
  257. tmp = min_t(int, ret, sg->length -
  258. conn->c_xmit_data_off);
  259. conn->c_xmit_data_off += tmp;
  260. ret -= tmp;
  261. if (conn->c_xmit_data_off == sg->length) {
  262. conn->c_xmit_data_off = 0;
  263. sg++;
  264. conn->c_xmit_sg++;
  265. BUG_ON(ret != 0 &&
  266. conn->c_xmit_sg == rm->data.op_nents);
  267. }
  268. }
  269. if (conn->c_xmit_hdr_off == sizeof(struct rds_header) &&
  270. (conn->c_xmit_sg == rm->data.op_nents))
  271. conn->c_xmit_data_sent = 1;
  272. }
  273. /*
  274. * A rm will only take multiple times through this loop
  275. * if there is a data op. Thus, if the data is sent (or there was
  276. * none), then we're done with the rm.
  277. */
  278. if (!rm->data.op_active || conn->c_xmit_data_sent) {
  279. conn->c_xmit_rm = NULL;
  280. conn->c_xmit_sg = 0;
  281. conn->c_xmit_hdr_off = 0;
  282. conn->c_xmit_data_off = 0;
  283. conn->c_xmit_rdma_sent = 0;
  284. conn->c_xmit_atomic_sent = 0;
  285. conn->c_xmit_data_sent = 0;
  286. rds_message_put(rm);
  287. }
  288. }
  289. if (conn->c_trans->xmit_complete)
  290. conn->c_trans->xmit_complete(conn);
  291. /*
  292. * We might be racing with another sender who queued a message but
  293. * backed off on noticing that we held the c_send_lock. If we check
  294. * for queued messages after dropping the sem then either we'll
  295. * see the queued message or the queuer will get the sem. If we
  296. * notice the queued message then we trigger an immediate retry.
  297. *
  298. * We need to be careful only to do this when we stopped processing
  299. * the send queue because it was empty. It's the only way we
  300. * stop processing the loop when the transport hasn't taken
  301. * responsibility for forward progress.
  302. */
  303. spin_unlock_irqrestore(&conn->c_send_lock, flags);
  304. /* Nuke any messages we decided not to retransmit. */
  305. if (!list_empty(&to_be_dropped)) {
  306. /* irqs on here, so we can put(), unlike above */
  307. list_for_each_entry(rm, &to_be_dropped, m_conn_item)
  308. rds_message_put(rm);
  309. rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
  310. }
  311. if (send_quota == 0 && !was_empty) {
  312. /* We exhausted the send quota, but there's work left to
  313. * do. Return and (re-)schedule the send worker.
  314. */
  315. ret = -EAGAIN;
  316. }
  317. if (ret == 0 && was_empty) {
  318. /* A simple bit test would be way faster than taking the
  319. * spin lock */
  320. spin_lock_irqsave(&conn->c_lock, flags);
  321. if (!list_empty(&conn->c_send_queue)) {
  322. rds_stats_inc(s_send_lock_queue_raced);
  323. ret = -EAGAIN;
  324. }
  325. spin_unlock_irqrestore(&conn->c_lock, flags);
  326. }
  327. out:
  328. return ret;
  329. }
  330. static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
  331. {
  332. u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  333. assert_spin_locked(&rs->rs_lock);
  334. BUG_ON(rs->rs_snd_bytes < len);
  335. rs->rs_snd_bytes -= len;
  336. if (rs->rs_snd_bytes == 0)
  337. rds_stats_inc(s_send_queue_empty);
  338. }
  339. static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
  340. is_acked_func is_acked)
  341. {
  342. if (is_acked)
  343. return is_acked(rm, ack);
  344. return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
  345. }
  346. /*
  347. * Returns true if there are no messages on the send and retransmit queues
  348. * which have a sequence number greater than or equal to the given sequence
  349. * number.
  350. */
  351. int rds_send_acked_before(struct rds_connection *conn, u64 seq)
  352. {
  353. struct rds_message *rm, *tmp;
  354. int ret = 1;
  355. spin_lock(&conn->c_lock);
  356. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  357. if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
  358. ret = 0;
  359. break;
  360. }
  361. list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
  362. if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
  363. ret = 0;
  364. break;
  365. }
  366. spin_unlock(&conn->c_lock);
  367. return ret;
  368. }
  369. /*
  370. * This is pretty similar to what happens below in the ACK
  371. * handling code - except that we call here as soon as we get
  372. * the IB send completion on the RDMA op and the accompanying
  373. * message.
  374. */
  375. void rds_rdma_send_complete(struct rds_message *rm, int status)
  376. {
  377. struct rds_sock *rs = NULL;
  378. struct rm_rdma_op *ro;
  379. struct rds_notifier *notifier;
  380. unsigned long flags;
  381. spin_lock_irqsave(&rm->m_rs_lock, flags);
  382. ro = &rm->rdma;
  383. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
  384. ro->op_active && ro->op_notify && ro->op_notifier) {
  385. notifier = ro->op_notifier;
  386. rs = rm->m_rs;
  387. sock_hold(rds_rs_to_sk(rs));
  388. notifier->n_status = status;
  389. spin_lock(&rs->rs_lock);
  390. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  391. spin_unlock(&rs->rs_lock);
  392. ro->op_notifier = NULL;
  393. }
  394. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  395. if (rs) {
  396. rds_wake_sk_sleep(rs);
  397. sock_put(rds_rs_to_sk(rs));
  398. }
  399. }
  400. EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
  401. /*
  402. * Just like above, except looks at atomic op
  403. */
  404. void rds_atomic_send_complete(struct rds_message *rm, int status)
  405. {
  406. struct rds_sock *rs = NULL;
  407. struct rm_atomic_op *ao;
  408. struct rds_notifier *notifier;
  409. spin_lock(&rm->m_rs_lock);
  410. ao = &rm->atomic;
  411. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
  412. && ao->op_active && ao->op_notify && ao->op_notifier) {
  413. notifier = ao->op_notifier;
  414. rs = rm->m_rs;
  415. sock_hold(rds_rs_to_sk(rs));
  416. notifier->n_status = status;
  417. spin_lock(&rs->rs_lock);
  418. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  419. spin_unlock(&rs->rs_lock);
  420. ao->op_notifier = NULL;
  421. }
  422. spin_unlock(&rm->m_rs_lock);
  423. if (rs) {
  424. rds_wake_sk_sleep(rs);
  425. sock_put(rds_rs_to_sk(rs));
  426. }
  427. }
  428. EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
  429. /*
  430. * This is the same as rds_rdma_send_complete except we
  431. * don't do any locking - we have all the ingredients (message,
  432. * socket, socket lock) and can just move the notifier.
  433. */
  434. static inline void
  435. __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
  436. {
  437. struct rm_rdma_op *ro;
  438. struct rm_atomic_op *ao;
  439. ro = &rm->rdma;
  440. if (ro->op_active && ro->op_notify && ro->op_notifier) {
  441. ro->op_notifier->n_status = status;
  442. list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
  443. ro->op_notifier = NULL;
  444. }
  445. ao = &rm->atomic;
  446. if (ao->op_active && ao->op_notify && ao->op_notifier) {
  447. ao->op_notifier->n_status = status;
  448. list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
  449. ao->op_notifier = NULL;
  450. }
  451. /* No need to wake the app - caller does this */
  452. }
  453. /*
  454. * This is called from the IB send completion when we detect
  455. * a RDMA operation that failed with remote access error.
  456. * So speed is not an issue here.
  457. */
  458. struct rds_message *rds_send_get_message(struct rds_connection *conn,
  459. struct rm_rdma_op *op)
  460. {
  461. struct rds_message *rm, *tmp, *found = NULL;
  462. unsigned long flags;
  463. spin_lock_irqsave(&conn->c_lock, flags);
  464. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  465. if (&rm->rdma == op) {
  466. atomic_inc(&rm->m_refcount);
  467. found = rm;
  468. goto out;
  469. }
  470. }
  471. list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
  472. if (&rm->rdma == op) {
  473. atomic_inc(&rm->m_refcount);
  474. found = rm;
  475. break;
  476. }
  477. }
  478. out:
  479. spin_unlock_irqrestore(&conn->c_lock, flags);
  480. return found;
  481. }
  482. EXPORT_SYMBOL_GPL(rds_send_get_message);
  483. /*
  484. * This removes messages from the socket's list if they're on it. The list
  485. * argument must be private to the caller, we must be able to modify it
  486. * without locks. The messages must have a reference held for their
  487. * position on the list. This function will drop that reference after
  488. * removing the messages from the 'messages' list regardless of if it found
  489. * the messages on the socket list or not.
  490. */
  491. void rds_send_remove_from_sock(struct list_head *messages, int status)
  492. {
  493. unsigned long flags;
  494. struct rds_sock *rs = NULL;
  495. struct rds_message *rm;
  496. while (!list_empty(messages)) {
  497. int was_on_sock = 0;
  498. rm = list_entry(messages->next, struct rds_message,
  499. m_conn_item);
  500. list_del_init(&rm->m_conn_item);
  501. /*
  502. * If we see this flag cleared then we're *sure* that someone
  503. * else beat us to removing it from the sock. If we race
  504. * with their flag update we'll get the lock and then really
  505. * see that the flag has been cleared.
  506. *
  507. * The message spinlock makes sure nobody clears rm->m_rs
  508. * while we're messing with it. It does not prevent the
  509. * message from being removed from the socket, though.
  510. */
  511. spin_lock_irqsave(&rm->m_rs_lock, flags);
  512. if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
  513. goto unlock_and_drop;
  514. if (rs != rm->m_rs) {
  515. if (rs) {
  516. rds_wake_sk_sleep(rs);
  517. sock_put(rds_rs_to_sk(rs));
  518. }
  519. rs = rm->m_rs;
  520. sock_hold(rds_rs_to_sk(rs));
  521. }
  522. spin_lock(&rs->rs_lock);
  523. if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
  524. struct rm_rdma_op *ro = &rm->rdma;
  525. struct rds_notifier *notifier;
  526. list_del_init(&rm->m_sock_item);
  527. rds_send_sndbuf_remove(rs, rm);
  528. if (ro->op_active && ro->op_notifier &&
  529. (ro->op_notify || (ro->op_recverr && status))) {
  530. notifier = ro->op_notifier;
  531. list_add_tail(&notifier->n_list,
  532. &rs->rs_notify_queue);
  533. if (!notifier->n_status)
  534. notifier->n_status = status;
  535. rm->rdma.op_notifier = NULL;
  536. }
  537. was_on_sock = 1;
  538. rm->m_rs = NULL;
  539. }
  540. spin_unlock(&rs->rs_lock);
  541. unlock_and_drop:
  542. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  543. rds_message_put(rm);
  544. if (was_on_sock)
  545. rds_message_put(rm);
  546. }
  547. if (rs) {
  548. rds_wake_sk_sleep(rs);
  549. sock_put(rds_rs_to_sk(rs));
  550. }
  551. }
  552. /*
  553. * Transports call here when they've determined that the receiver queued
  554. * messages up to, and including, the given sequence number. Messages are
  555. * moved to the retrans queue when rds_send_xmit picks them off the send
  556. * queue. This means that in the TCP case, the message may not have been
  557. * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
  558. * checks the RDS_MSG_HAS_ACK_SEQ bit.
  559. *
  560. * XXX It's not clear to me how this is safely serialized with socket
  561. * destruction. Maybe it should bail if it sees SOCK_DEAD.
  562. */
  563. void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
  564. is_acked_func is_acked)
  565. {
  566. struct rds_message *rm, *tmp;
  567. unsigned long flags;
  568. LIST_HEAD(list);
  569. spin_lock_irqsave(&conn->c_lock, flags);
  570. list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
  571. if (!rds_send_is_acked(rm, ack, is_acked))
  572. break;
  573. list_move(&rm->m_conn_item, &list);
  574. clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  575. }
  576. /* order flag updates with spin locks */
  577. if (!list_empty(&list))
  578. smp_mb__after_clear_bit();
  579. spin_unlock_irqrestore(&conn->c_lock, flags);
  580. /* now remove the messages from the sock list as needed */
  581. rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
  582. }
  583. EXPORT_SYMBOL_GPL(rds_send_drop_acked);
  584. void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
  585. {
  586. struct rds_message *rm, *tmp;
  587. struct rds_connection *conn;
  588. unsigned long flags;
  589. LIST_HEAD(list);
  590. /* get all the messages we're dropping under the rs lock */
  591. spin_lock_irqsave(&rs->rs_lock, flags);
  592. list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
  593. if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
  594. dest->sin_port != rm->m_inc.i_hdr.h_dport))
  595. continue;
  596. list_move(&rm->m_sock_item, &list);
  597. rds_send_sndbuf_remove(rs, rm);
  598. clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  599. }
  600. /* order flag updates with the rs lock */
  601. smp_mb__after_clear_bit();
  602. spin_unlock_irqrestore(&rs->rs_lock, flags);
  603. if (list_empty(&list))
  604. return;
  605. /* Remove the messages from the conn */
  606. list_for_each_entry(rm, &list, m_sock_item) {
  607. conn = rm->m_inc.i_conn;
  608. spin_lock_irqsave(&conn->c_lock, flags);
  609. /*
  610. * Maybe someone else beat us to removing rm from the conn.
  611. * If we race with their flag update we'll get the lock and
  612. * then really see that the flag has been cleared.
  613. */
  614. if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
  615. spin_unlock_irqrestore(&conn->c_lock, flags);
  616. continue;
  617. }
  618. list_del_init(&rm->m_conn_item);
  619. spin_unlock_irqrestore(&conn->c_lock, flags);
  620. /*
  621. * Couldn't grab m_rs_lock in top loop (lock ordering),
  622. * but we can now.
  623. */
  624. spin_lock_irqsave(&rm->m_rs_lock, flags);
  625. spin_lock(&rs->rs_lock);
  626. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  627. spin_unlock(&rs->rs_lock);
  628. rm->m_rs = NULL;
  629. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  630. rds_message_put(rm);
  631. }
  632. rds_wake_sk_sleep(rs);
  633. while (!list_empty(&list)) {
  634. rm = list_entry(list.next, struct rds_message, m_sock_item);
  635. list_del_init(&rm->m_sock_item);
  636. rds_message_wait(rm);
  637. rds_message_put(rm);
  638. }
  639. }
  640. /*
  641. * we only want this to fire once so we use the callers 'queued'. It's
  642. * possible that another thread can race with us and remove the
  643. * message from the flow with RDS_CANCEL_SENT_TO.
  644. */
  645. static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
  646. struct rds_message *rm, __be16 sport,
  647. __be16 dport, int *queued)
  648. {
  649. unsigned long flags;
  650. u32 len;
  651. if (*queued)
  652. goto out;
  653. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  654. /* this is the only place which holds both the socket's rs_lock
  655. * and the connection's c_lock */
  656. spin_lock_irqsave(&rs->rs_lock, flags);
  657. /*
  658. * If there is a little space in sndbuf, we don't queue anything,
  659. * and userspace gets -EAGAIN. But poll() indicates there's send
  660. * room. This can lead to bad behavior (spinning) if snd_bytes isn't
  661. * freed up by incoming acks. So we check the *old* value of
  662. * rs_snd_bytes here to allow the last msg to exceed the buffer,
  663. * and poll() now knows no more data can be sent.
  664. */
  665. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
  666. rs->rs_snd_bytes += len;
  667. /* let recv side know we are close to send space exhaustion.
  668. * This is probably not the optimal way to do it, as this
  669. * means we set the flag on *all* messages as soon as our
  670. * throughput hits a certain threshold.
  671. */
  672. if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
  673. __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  674. list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
  675. set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  676. rds_message_addref(rm);
  677. rm->m_rs = rs;
  678. /* The code ordering is a little weird, but we're
  679. trying to minimize the time we hold c_lock */
  680. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
  681. rm->m_inc.i_conn = conn;
  682. rds_message_addref(rm);
  683. spin_lock(&conn->c_lock);
  684. rm->m_inc.i_hdr.h_sequence = cpu_to_be64(conn->c_next_tx_seq++);
  685. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  686. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  687. spin_unlock(&conn->c_lock);
  688. rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
  689. rm, len, rs, rs->rs_snd_bytes,
  690. (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
  691. *queued = 1;
  692. }
  693. spin_unlock_irqrestore(&rs->rs_lock, flags);
  694. out:
  695. return *queued;
  696. }
  697. /*
  698. * rds_message is getting to be quite complicated, and we'd like to allocate
  699. * it all in one go. This figures out how big it needs to be up front.
  700. */
  701. static int rds_rm_size(struct msghdr *msg, int data_len)
  702. {
  703. struct cmsghdr *cmsg;
  704. int size = 0;
  705. int cmsg_groups = 0;
  706. int retval;
  707. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  708. if (!CMSG_OK(msg, cmsg))
  709. return -EINVAL;
  710. if (cmsg->cmsg_level != SOL_RDS)
  711. continue;
  712. switch (cmsg->cmsg_type) {
  713. case RDS_CMSG_RDMA_ARGS:
  714. cmsg_groups |= 1;
  715. retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
  716. if (retval < 0)
  717. return retval;
  718. size += retval;
  719. break;
  720. case RDS_CMSG_RDMA_DEST:
  721. case RDS_CMSG_RDMA_MAP:
  722. cmsg_groups |= 2;
  723. /* these are valid but do no add any size */
  724. break;
  725. case RDS_CMSG_ATOMIC_CSWP:
  726. case RDS_CMSG_ATOMIC_FADD:
  727. cmsg_groups |= 1;
  728. size += sizeof(struct scatterlist);
  729. break;
  730. default:
  731. return -EINVAL;
  732. }
  733. }
  734. size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
  735. /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
  736. if (cmsg_groups == 3)
  737. return -EINVAL;
  738. return size;
  739. }
  740. static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
  741. struct msghdr *msg, int *allocated_mr)
  742. {
  743. struct cmsghdr *cmsg;
  744. int ret = 0;
  745. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  746. if (!CMSG_OK(msg, cmsg))
  747. return -EINVAL;
  748. if (cmsg->cmsg_level != SOL_RDS)
  749. continue;
  750. /* As a side effect, RDMA_DEST and RDMA_MAP will set
  751. * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
  752. */
  753. switch (cmsg->cmsg_type) {
  754. case RDS_CMSG_RDMA_ARGS:
  755. ret = rds_cmsg_rdma_args(rs, rm, cmsg);
  756. break;
  757. case RDS_CMSG_RDMA_DEST:
  758. ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
  759. break;
  760. case RDS_CMSG_RDMA_MAP:
  761. ret = rds_cmsg_rdma_map(rs, rm, cmsg);
  762. if (!ret)
  763. *allocated_mr = 1;
  764. break;
  765. case RDS_CMSG_ATOMIC_CSWP:
  766. case RDS_CMSG_ATOMIC_FADD:
  767. ret = rds_cmsg_atomic(rs, rm, cmsg);
  768. break;
  769. default:
  770. return -EINVAL;
  771. }
  772. if (ret)
  773. break;
  774. }
  775. return ret;
  776. }
  777. int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  778. size_t payload_len)
  779. {
  780. struct sock *sk = sock->sk;
  781. struct rds_sock *rs = rds_sk_to_rs(sk);
  782. struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
  783. __be32 daddr;
  784. __be16 dport;
  785. struct rds_message *rm = NULL;
  786. struct rds_connection *conn;
  787. int ret = 0;
  788. int queued = 0, allocated_mr = 0;
  789. int nonblock = msg->msg_flags & MSG_DONTWAIT;
  790. long timeo = sock_sndtimeo(sk, nonblock);
  791. /* Mirror Linux UDP mirror of BSD error message compatibility */
  792. /* XXX: Perhaps MSG_MORE someday */
  793. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
  794. printk(KERN_INFO "msg_flags 0x%08X\n", msg->msg_flags);
  795. ret = -EOPNOTSUPP;
  796. goto out;
  797. }
  798. if (msg->msg_namelen) {
  799. /* XXX fail non-unicast destination IPs? */
  800. if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
  801. ret = -EINVAL;
  802. goto out;
  803. }
  804. daddr = usin->sin_addr.s_addr;
  805. dport = usin->sin_port;
  806. } else {
  807. /* We only care about consistency with ->connect() */
  808. lock_sock(sk);
  809. daddr = rs->rs_conn_addr;
  810. dport = rs->rs_conn_port;
  811. release_sock(sk);
  812. }
  813. /* racing with another thread binding seems ok here */
  814. if (daddr == 0 || rs->rs_bound_addr == 0) {
  815. ret = -ENOTCONN; /* XXX not a great errno */
  816. goto out;
  817. }
  818. /* size of rm including all sgs */
  819. ret = rds_rm_size(msg, payload_len);
  820. if (ret < 0)
  821. goto out;
  822. rm = rds_message_alloc(ret, GFP_KERNEL);
  823. if (!rm) {
  824. ret = -ENOMEM;
  825. goto out;
  826. }
  827. /* Attach data to the rm */
  828. if (payload_len) {
  829. rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
  830. ret = rds_message_copy_from_user(rm, msg->msg_iov, payload_len);
  831. if (ret)
  832. goto out;
  833. }
  834. rm->data.op_active = 1;
  835. rm->m_daddr = daddr;
  836. /* rds_conn_create has a spinlock that runs with IRQ off.
  837. * Caching the conn in the socket helps a lot. */
  838. if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
  839. conn = rs->rs_conn;
  840. else {
  841. conn = rds_conn_create_outgoing(rs->rs_bound_addr, daddr,
  842. rs->rs_transport,
  843. sock->sk->sk_allocation);
  844. if (IS_ERR(conn)) {
  845. ret = PTR_ERR(conn);
  846. goto out;
  847. }
  848. rs->rs_conn = conn;
  849. }
  850. /* Parse any control messages the user may have included. */
  851. ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
  852. if (ret)
  853. goto out;
  854. if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
  855. if (printk_ratelimit())
  856. printk(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
  857. &rm->rdma, conn->c_trans->xmit_rdma);
  858. ret = -EOPNOTSUPP;
  859. goto out;
  860. }
  861. if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
  862. if (printk_ratelimit())
  863. printk(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
  864. &rm->atomic, conn->c_trans->xmit_atomic);
  865. ret = -EOPNOTSUPP;
  866. goto out;
  867. }
  868. /* If the connection is down, trigger a connect. We may
  869. * have scheduled a delayed reconnect however - in this case
  870. * we should not interfere.
  871. */
  872. if (rds_conn_state(conn) == RDS_CONN_DOWN &&
  873. !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
  874. queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
  875. ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
  876. if (ret) {
  877. rs->rs_seen_congestion = 1;
  878. goto out;
  879. }
  880. while (!rds_send_queue_rm(rs, conn, rm, rs->rs_bound_port,
  881. dport, &queued)) {
  882. rds_stats_inc(s_send_queue_full);
  883. /* XXX make sure this is reasonable */
  884. if (payload_len > rds_sk_sndbuf(rs)) {
  885. ret = -EMSGSIZE;
  886. goto out;
  887. }
  888. if (nonblock) {
  889. ret = -EAGAIN;
  890. goto out;
  891. }
  892. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  893. rds_send_queue_rm(rs, conn, rm,
  894. rs->rs_bound_port,
  895. dport,
  896. &queued),
  897. timeo);
  898. rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
  899. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  900. continue;
  901. ret = timeo;
  902. if (ret == 0)
  903. ret = -ETIMEDOUT;
  904. goto out;
  905. }
  906. /*
  907. * By now we've committed to the send. We reuse rds_send_worker()
  908. * to retry sends in the rds thread if the transport asks us to.
  909. */
  910. rds_stats_inc(s_send_queued);
  911. if (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags))
  912. rds_send_worker(&conn->c_send_w.work);
  913. rds_message_put(rm);
  914. return payload_len;
  915. out:
  916. /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
  917. * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
  918. * or in any other way, we need to destroy the MR again */
  919. if (allocated_mr)
  920. rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
  921. if (rm)
  922. rds_message_put(rm);
  923. return ret;
  924. }
  925. /*
  926. * Reply to a ping packet.
  927. */
  928. int
  929. rds_send_pong(struct rds_connection *conn, __be16 dport)
  930. {
  931. struct rds_message *rm;
  932. unsigned long flags;
  933. int ret = 0;
  934. rm = rds_message_alloc(0, GFP_ATOMIC);
  935. if (!rm) {
  936. ret = -ENOMEM;
  937. goto out;
  938. }
  939. rm->m_daddr = conn->c_faddr;
  940. /* If the connection is down, trigger a connect. We may
  941. * have scheduled a delayed reconnect however - in this case
  942. * we should not interfere.
  943. */
  944. if (rds_conn_state(conn) == RDS_CONN_DOWN &&
  945. !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
  946. queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
  947. ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
  948. if (ret)
  949. goto out;
  950. spin_lock_irqsave(&conn->c_lock, flags);
  951. list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
  952. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  953. rds_message_addref(rm);
  954. rm->m_inc.i_conn = conn;
  955. rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
  956. conn->c_next_tx_seq);
  957. conn->c_next_tx_seq++;
  958. spin_unlock_irqrestore(&conn->c_lock, flags);
  959. rds_stats_inc(s_send_queued);
  960. rds_stats_inc(s_send_pong);
  961. queue_delayed_work(rds_wq, &conn->c_send_w, 0);
  962. rds_message_put(rm);
  963. return 0;
  964. out:
  965. if (rm)
  966. rds_message_put(rm);
  967. return ret;
  968. }