send.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * Copyright (c) 2006, 2018 Oracle and/or its affiliates. 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/moduleparam.h>
  35. #include <linux/gfp.h>
  36. #include <net/sock.h>
  37. #include <linux/in.h>
  38. #include <linux/list.h>
  39. #include <linux/ratelimit.h>
  40. #include <linux/export.h>
  41. #include <linux/sizes.h>
  42. #include "rds.h"
  43. /* When transmitting messages in rds_send_xmit, we need to emerge from
  44. * time to time and briefly release the CPU. Otherwise the softlock watchdog
  45. * will kick our shin.
  46. * Also, it seems fairer to not let one busy connection stall all the
  47. * others.
  48. *
  49. * send_batch_count is the number of times we'll loop in send_xmit. Setting
  50. * it to 0 will restore the old behavior (where we looped until we had
  51. * drained the queue).
  52. */
  53. static int send_batch_count = SZ_1K;
  54. module_param(send_batch_count, int, 0444);
  55. MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
  56. static void rds_send_remove_from_sock(struct list_head *messages, int status);
  57. /*
  58. * Reset the send state. Callers must ensure that this doesn't race with
  59. * rds_send_xmit().
  60. */
  61. void rds_send_path_reset(struct rds_conn_path *cp)
  62. {
  63. struct rds_message *rm, *tmp;
  64. unsigned long flags;
  65. if (cp->cp_xmit_rm) {
  66. rm = cp->cp_xmit_rm;
  67. cp->cp_xmit_rm = NULL;
  68. /* Tell the user the RDMA op is no longer mapped by the
  69. * transport. This isn't entirely true (it's flushed out
  70. * independently) but as the connection is down, there's
  71. * no ongoing RDMA to/from that memory */
  72. rds_message_unmapped(rm);
  73. rds_message_put(rm);
  74. }
  75. cp->cp_xmit_sg = 0;
  76. cp->cp_xmit_hdr_off = 0;
  77. cp->cp_xmit_data_off = 0;
  78. cp->cp_xmit_atomic_sent = 0;
  79. cp->cp_xmit_rdma_sent = 0;
  80. cp->cp_xmit_data_sent = 0;
  81. cp->cp_conn->c_map_queued = 0;
  82. cp->cp_unacked_packets = rds_sysctl_max_unacked_packets;
  83. cp->cp_unacked_bytes = rds_sysctl_max_unacked_bytes;
  84. /* Mark messages as retransmissions, and move them to the send q */
  85. spin_lock_irqsave(&cp->cp_lock, flags);
  86. list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
  87. set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  88. set_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags);
  89. }
  90. list_splice_init(&cp->cp_retrans, &cp->cp_send_queue);
  91. spin_unlock_irqrestore(&cp->cp_lock, flags);
  92. }
  93. EXPORT_SYMBOL_GPL(rds_send_path_reset);
  94. static int acquire_in_xmit(struct rds_conn_path *cp)
  95. {
  96. return test_and_set_bit_lock(RDS_IN_XMIT, &cp->cp_flags) == 0;
  97. }
  98. static void release_in_xmit(struct rds_conn_path *cp)
  99. {
  100. clear_bit_unlock(RDS_IN_XMIT, &cp->cp_flags);
  101. /*
  102. * We don't use wait_on_bit()/wake_up_bit() because our waking is in a
  103. * hot path and finding waiters is very rare. We don't want to walk
  104. * the system-wide hashed waitqueue buckets in the fast path only to
  105. * almost never find waiters.
  106. */
  107. if (waitqueue_active(&cp->cp_waitq))
  108. wake_up_all(&cp->cp_waitq);
  109. }
  110. /*
  111. * We're making the conscious trade-off here to only send one message
  112. * down the connection at a time.
  113. * Pro:
  114. * - tx queueing is a simple fifo list
  115. * - reassembly is optional and easily done by transports per conn
  116. * - no per flow rx lookup at all, straight to the socket
  117. * - less per-frag memory and wire overhead
  118. * Con:
  119. * - queued acks can be delayed behind large messages
  120. * Depends:
  121. * - small message latency is higher behind queued large messages
  122. * - large message latency isn't starved by intervening small sends
  123. */
  124. int rds_send_xmit(struct rds_conn_path *cp)
  125. {
  126. struct rds_connection *conn = cp->cp_conn;
  127. struct rds_message *rm;
  128. unsigned long flags;
  129. unsigned int tmp;
  130. struct scatterlist *sg;
  131. int ret = 0;
  132. LIST_HEAD(to_be_dropped);
  133. int batch_count;
  134. unsigned long send_gen = 0;
  135. int same_rm = 0;
  136. restart:
  137. batch_count = 0;
  138. /*
  139. * sendmsg calls here after having queued its message on the send
  140. * queue. We only have one task feeding the connection at a time. If
  141. * another thread is already feeding the queue then we back off. This
  142. * avoids blocking the caller and trading per-connection data between
  143. * caches per message.
  144. */
  145. if (!acquire_in_xmit(cp)) {
  146. rds_stats_inc(s_send_lock_contention);
  147. ret = -ENOMEM;
  148. goto out;
  149. }
  150. if (rds_destroy_pending(cp->cp_conn)) {
  151. release_in_xmit(cp);
  152. ret = -ENETUNREACH; /* dont requeue send work */
  153. goto out;
  154. }
  155. /*
  156. * we record the send generation after doing the xmit acquire.
  157. * if someone else manages to jump in and do some work, we'll use
  158. * this to avoid a goto restart farther down.
  159. *
  160. * The acquire_in_xmit() check above ensures that only one
  161. * caller can increment c_send_gen at any time.
  162. */
  163. send_gen = READ_ONCE(cp->cp_send_gen) + 1;
  164. WRITE_ONCE(cp->cp_send_gen, send_gen);
  165. /*
  166. * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT,
  167. * we do the opposite to avoid races.
  168. */
  169. if (!rds_conn_path_up(cp)) {
  170. release_in_xmit(cp);
  171. ret = 0;
  172. goto out;
  173. }
  174. if (conn->c_trans->xmit_path_prepare)
  175. conn->c_trans->xmit_path_prepare(cp);
  176. /*
  177. * spin trying to push headers and data down the connection until
  178. * the connection doesn't make forward progress.
  179. */
  180. while (1) {
  181. rm = cp->cp_xmit_rm;
  182. if (!rm) {
  183. same_rm = 0;
  184. } else {
  185. same_rm++;
  186. if (same_rm >= 4096) {
  187. rds_stats_inc(s_send_stuck_rm);
  188. ret = -EAGAIN;
  189. break;
  190. }
  191. }
  192. /*
  193. * If between sending messages, we can send a pending congestion
  194. * map update.
  195. */
  196. if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
  197. rm = rds_cong_update_alloc(conn);
  198. if (IS_ERR(rm)) {
  199. ret = PTR_ERR(rm);
  200. break;
  201. }
  202. rm->data.op_active = 1;
  203. rm->m_inc.i_conn_path = cp;
  204. rm->m_inc.i_conn = cp->cp_conn;
  205. cp->cp_xmit_rm = rm;
  206. }
  207. /*
  208. * If not already working on one, grab the next message.
  209. *
  210. * cp_xmit_rm holds a ref while we're sending this message down
  211. * the connction. We can use this ref while holding the
  212. * send_sem.. rds_send_reset() is serialized with it.
  213. */
  214. if (!rm) {
  215. unsigned int len;
  216. batch_count++;
  217. /* we want to process as big a batch as we can, but
  218. * we also want to avoid softlockups. If we've been
  219. * through a lot of messages, lets back off and see
  220. * if anyone else jumps in
  221. */
  222. if (batch_count >= send_batch_count)
  223. goto over_batch;
  224. spin_lock_irqsave(&cp->cp_lock, flags);
  225. if (!list_empty(&cp->cp_send_queue)) {
  226. rm = list_entry(cp->cp_send_queue.next,
  227. struct rds_message,
  228. m_conn_item);
  229. rds_message_addref(rm);
  230. /*
  231. * Move the message from the send queue to the retransmit
  232. * list right away.
  233. */
  234. list_move_tail(&rm->m_conn_item,
  235. &cp->cp_retrans);
  236. }
  237. spin_unlock_irqrestore(&cp->cp_lock, flags);
  238. if (!rm)
  239. break;
  240. /* Unfortunately, the way Infiniband deals with
  241. * RDMA to a bad MR key is by moving the entire
  242. * queue pair to error state. We could possibly
  243. * recover from that, but right now we drop the
  244. * connection.
  245. * Therefore, we never retransmit messages with RDMA ops.
  246. */
  247. if (test_bit(RDS_MSG_FLUSH, &rm->m_flags) ||
  248. (rm->rdma.op_active &&
  249. test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags))) {
  250. spin_lock_irqsave(&cp->cp_lock, flags);
  251. if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags))
  252. list_move(&rm->m_conn_item, &to_be_dropped);
  253. spin_unlock_irqrestore(&cp->cp_lock, flags);
  254. continue;
  255. }
  256. /* Require an ACK every once in a while */
  257. len = ntohl(rm->m_inc.i_hdr.h_len);
  258. if (cp->cp_unacked_packets == 0 ||
  259. cp->cp_unacked_bytes < len) {
  260. set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  261. cp->cp_unacked_packets =
  262. rds_sysctl_max_unacked_packets;
  263. cp->cp_unacked_bytes =
  264. rds_sysctl_max_unacked_bytes;
  265. rds_stats_inc(s_send_ack_required);
  266. } else {
  267. cp->cp_unacked_bytes -= len;
  268. cp->cp_unacked_packets--;
  269. }
  270. cp->cp_xmit_rm = rm;
  271. }
  272. /* The transport either sends the whole rdma or none of it */
  273. if (rm->rdma.op_active && !cp->cp_xmit_rdma_sent) {
  274. rm->m_final_op = &rm->rdma;
  275. /* The transport owns the mapped memory for now.
  276. * You can't unmap it while it's on the send queue
  277. */
  278. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  279. ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
  280. if (ret) {
  281. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  282. wake_up_interruptible(&rm->m_flush_wait);
  283. break;
  284. }
  285. cp->cp_xmit_rdma_sent = 1;
  286. }
  287. if (rm->atomic.op_active && !cp->cp_xmit_atomic_sent) {
  288. rm->m_final_op = &rm->atomic;
  289. /* The transport owns the mapped memory for now.
  290. * You can't unmap it while it's on the send queue
  291. */
  292. set_bit(RDS_MSG_MAPPED, &rm->m_flags);
  293. ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
  294. if (ret) {
  295. clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
  296. wake_up_interruptible(&rm->m_flush_wait);
  297. break;
  298. }
  299. cp->cp_xmit_atomic_sent = 1;
  300. }
  301. /*
  302. * A number of cases require an RDS header to be sent
  303. * even if there is no data.
  304. * We permit 0-byte sends; rds-ping depends on this.
  305. * However, if there are exclusively attached silent ops,
  306. * we skip the hdr/data send, to enable silent operation.
  307. */
  308. if (rm->data.op_nents == 0) {
  309. int ops_present;
  310. int all_ops_are_silent = 1;
  311. ops_present = (rm->atomic.op_active || rm->rdma.op_active);
  312. if (rm->atomic.op_active && !rm->atomic.op_silent)
  313. all_ops_are_silent = 0;
  314. if (rm->rdma.op_active && !rm->rdma.op_silent)
  315. all_ops_are_silent = 0;
  316. if (ops_present && all_ops_are_silent
  317. && !rm->m_rdma_cookie)
  318. rm->data.op_active = 0;
  319. }
  320. if (rm->data.op_active && !cp->cp_xmit_data_sent) {
  321. rm->m_final_op = &rm->data;
  322. ret = conn->c_trans->xmit(conn, rm,
  323. cp->cp_xmit_hdr_off,
  324. cp->cp_xmit_sg,
  325. cp->cp_xmit_data_off);
  326. if (ret <= 0)
  327. break;
  328. if (cp->cp_xmit_hdr_off < sizeof(struct rds_header)) {
  329. tmp = min_t(int, ret,
  330. sizeof(struct rds_header) -
  331. cp->cp_xmit_hdr_off);
  332. cp->cp_xmit_hdr_off += tmp;
  333. ret -= tmp;
  334. }
  335. sg = &rm->data.op_sg[cp->cp_xmit_sg];
  336. while (ret) {
  337. tmp = min_t(int, ret, sg->length -
  338. cp->cp_xmit_data_off);
  339. cp->cp_xmit_data_off += tmp;
  340. ret -= tmp;
  341. if (cp->cp_xmit_data_off == sg->length) {
  342. cp->cp_xmit_data_off = 0;
  343. sg++;
  344. cp->cp_xmit_sg++;
  345. BUG_ON(ret != 0 && cp->cp_xmit_sg ==
  346. rm->data.op_nents);
  347. }
  348. }
  349. if (cp->cp_xmit_hdr_off == sizeof(struct rds_header) &&
  350. (cp->cp_xmit_sg == rm->data.op_nents))
  351. cp->cp_xmit_data_sent = 1;
  352. }
  353. /*
  354. * A rm will only take multiple times through this loop
  355. * if there is a data op. Thus, if the data is sent (or there was
  356. * none), then we're done with the rm.
  357. */
  358. if (!rm->data.op_active || cp->cp_xmit_data_sent) {
  359. cp->cp_xmit_rm = NULL;
  360. cp->cp_xmit_sg = 0;
  361. cp->cp_xmit_hdr_off = 0;
  362. cp->cp_xmit_data_off = 0;
  363. cp->cp_xmit_rdma_sent = 0;
  364. cp->cp_xmit_atomic_sent = 0;
  365. cp->cp_xmit_data_sent = 0;
  366. rds_message_put(rm);
  367. }
  368. }
  369. over_batch:
  370. if (conn->c_trans->xmit_path_complete)
  371. conn->c_trans->xmit_path_complete(cp);
  372. release_in_xmit(cp);
  373. /* Nuke any messages we decided not to retransmit. */
  374. if (!list_empty(&to_be_dropped)) {
  375. /* irqs on here, so we can put(), unlike above */
  376. list_for_each_entry(rm, &to_be_dropped, m_conn_item)
  377. rds_message_put(rm);
  378. rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
  379. }
  380. /*
  381. * Other senders can queue a message after we last test the send queue
  382. * but before we clear RDS_IN_XMIT. In that case they'd back off and
  383. * not try and send their newly queued message. We need to check the
  384. * send queue after having cleared RDS_IN_XMIT so that their message
  385. * doesn't get stuck on the send queue.
  386. *
  387. * If the transport cannot continue (i.e ret != 0), then it must
  388. * call us when more room is available, such as from the tx
  389. * completion handler.
  390. *
  391. * We have an extra generation check here so that if someone manages
  392. * to jump in after our release_in_xmit, we'll see that they have done
  393. * some work and we will skip our goto
  394. */
  395. if (ret == 0) {
  396. bool raced;
  397. smp_mb();
  398. raced = send_gen != READ_ONCE(cp->cp_send_gen);
  399. if ((test_bit(0, &conn->c_map_queued) ||
  400. !list_empty(&cp->cp_send_queue)) && !raced) {
  401. if (batch_count < send_batch_count)
  402. goto restart;
  403. rcu_read_lock();
  404. if (rds_destroy_pending(cp->cp_conn))
  405. ret = -ENETUNREACH;
  406. else
  407. queue_delayed_work(rds_wq, &cp->cp_send_w, 1);
  408. rcu_read_unlock();
  409. } else if (raced) {
  410. rds_stats_inc(s_send_lock_queue_raced);
  411. }
  412. }
  413. out:
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(rds_send_xmit);
  417. static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
  418. {
  419. u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  420. assert_spin_locked(&rs->rs_lock);
  421. BUG_ON(rs->rs_snd_bytes < len);
  422. rs->rs_snd_bytes -= len;
  423. if (rs->rs_snd_bytes == 0)
  424. rds_stats_inc(s_send_queue_empty);
  425. }
  426. static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
  427. is_acked_func is_acked)
  428. {
  429. if (is_acked)
  430. return is_acked(rm, ack);
  431. return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
  432. }
  433. /*
  434. * This is pretty similar to what happens below in the ACK
  435. * handling code - except that we call here as soon as we get
  436. * the IB send completion on the RDMA op and the accompanying
  437. * message.
  438. */
  439. void rds_rdma_send_complete(struct rds_message *rm, int status)
  440. {
  441. struct rds_sock *rs = NULL;
  442. struct rm_rdma_op *ro;
  443. struct rds_notifier *notifier;
  444. unsigned long flags;
  445. spin_lock_irqsave(&rm->m_rs_lock, flags);
  446. ro = &rm->rdma;
  447. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
  448. ro->op_active && ro->op_notify && ro->op_notifier) {
  449. notifier = ro->op_notifier;
  450. rs = rm->m_rs;
  451. sock_hold(rds_rs_to_sk(rs));
  452. notifier->n_status = status;
  453. spin_lock(&rs->rs_lock);
  454. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  455. spin_unlock(&rs->rs_lock);
  456. ro->op_notifier = NULL;
  457. }
  458. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  459. if (rs) {
  460. rds_wake_sk_sleep(rs);
  461. sock_put(rds_rs_to_sk(rs));
  462. }
  463. }
  464. EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
  465. /*
  466. * Just like above, except looks at atomic op
  467. */
  468. void rds_atomic_send_complete(struct rds_message *rm, int status)
  469. {
  470. struct rds_sock *rs = NULL;
  471. struct rm_atomic_op *ao;
  472. struct rds_notifier *notifier;
  473. unsigned long flags;
  474. spin_lock_irqsave(&rm->m_rs_lock, flags);
  475. ao = &rm->atomic;
  476. if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
  477. && ao->op_active && ao->op_notify && ao->op_notifier) {
  478. notifier = ao->op_notifier;
  479. rs = rm->m_rs;
  480. sock_hold(rds_rs_to_sk(rs));
  481. notifier->n_status = status;
  482. spin_lock(&rs->rs_lock);
  483. list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
  484. spin_unlock(&rs->rs_lock);
  485. ao->op_notifier = NULL;
  486. }
  487. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  488. if (rs) {
  489. rds_wake_sk_sleep(rs);
  490. sock_put(rds_rs_to_sk(rs));
  491. }
  492. }
  493. EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
  494. /*
  495. * This is the same as rds_rdma_send_complete except we
  496. * don't do any locking - we have all the ingredients (message,
  497. * socket, socket lock) and can just move the notifier.
  498. */
  499. static inline void
  500. __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
  501. {
  502. struct rm_rdma_op *ro;
  503. struct rm_atomic_op *ao;
  504. ro = &rm->rdma;
  505. if (ro->op_active && ro->op_notify && ro->op_notifier) {
  506. ro->op_notifier->n_status = status;
  507. list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
  508. ro->op_notifier = NULL;
  509. }
  510. ao = &rm->atomic;
  511. if (ao->op_active && ao->op_notify && ao->op_notifier) {
  512. ao->op_notifier->n_status = status;
  513. list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
  514. ao->op_notifier = NULL;
  515. }
  516. /* No need to wake the app - caller does this */
  517. }
  518. /*
  519. * This removes messages from the socket's list if they're on it. The list
  520. * argument must be private to the caller, we must be able to modify it
  521. * without locks. The messages must have a reference held for their
  522. * position on the list. This function will drop that reference after
  523. * removing the messages from the 'messages' list regardless of if it found
  524. * the messages on the socket list or not.
  525. */
  526. static void rds_send_remove_from_sock(struct list_head *messages, int status)
  527. {
  528. unsigned long flags;
  529. struct rds_sock *rs = NULL;
  530. struct rds_message *rm;
  531. while (!list_empty(messages)) {
  532. int was_on_sock = 0;
  533. rm = list_entry(messages->next, struct rds_message,
  534. m_conn_item);
  535. list_del_init(&rm->m_conn_item);
  536. /*
  537. * If we see this flag cleared then we're *sure* that someone
  538. * else beat us to removing it from the sock. If we race
  539. * with their flag update we'll get the lock and then really
  540. * see that the flag has been cleared.
  541. *
  542. * The message spinlock makes sure nobody clears rm->m_rs
  543. * while we're messing with it. It does not prevent the
  544. * message from being removed from the socket, though.
  545. */
  546. spin_lock_irqsave(&rm->m_rs_lock, flags);
  547. if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
  548. goto unlock_and_drop;
  549. if (rs != rm->m_rs) {
  550. if (rs) {
  551. rds_wake_sk_sleep(rs);
  552. sock_put(rds_rs_to_sk(rs));
  553. }
  554. rs = rm->m_rs;
  555. if (rs)
  556. sock_hold(rds_rs_to_sk(rs));
  557. }
  558. if (!rs)
  559. goto unlock_and_drop;
  560. spin_lock(&rs->rs_lock);
  561. if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
  562. struct rm_rdma_op *ro = &rm->rdma;
  563. struct rds_notifier *notifier;
  564. list_del_init(&rm->m_sock_item);
  565. rds_send_sndbuf_remove(rs, rm);
  566. if (ro->op_active && ro->op_notifier &&
  567. (ro->op_notify || (ro->op_recverr && status))) {
  568. notifier = ro->op_notifier;
  569. list_add_tail(&notifier->n_list,
  570. &rs->rs_notify_queue);
  571. if (!notifier->n_status)
  572. notifier->n_status = status;
  573. rm->rdma.op_notifier = NULL;
  574. }
  575. was_on_sock = 1;
  576. }
  577. spin_unlock(&rs->rs_lock);
  578. unlock_and_drop:
  579. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  580. rds_message_put(rm);
  581. if (was_on_sock)
  582. rds_message_put(rm);
  583. }
  584. if (rs) {
  585. rds_wake_sk_sleep(rs);
  586. sock_put(rds_rs_to_sk(rs));
  587. }
  588. }
  589. /*
  590. * Transports call here when they've determined that the receiver queued
  591. * messages up to, and including, the given sequence number. Messages are
  592. * moved to the retrans queue when rds_send_xmit picks them off the send
  593. * queue. This means that in the TCP case, the message may not have been
  594. * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
  595. * checks the RDS_MSG_HAS_ACK_SEQ bit.
  596. */
  597. void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack,
  598. is_acked_func is_acked)
  599. {
  600. struct rds_message *rm, *tmp;
  601. unsigned long flags;
  602. LIST_HEAD(list);
  603. spin_lock_irqsave(&cp->cp_lock, flags);
  604. list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
  605. if (!rds_send_is_acked(rm, ack, is_acked))
  606. break;
  607. list_move(&rm->m_conn_item, &list);
  608. clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  609. }
  610. /* order flag updates with spin locks */
  611. if (!list_empty(&list))
  612. smp_mb__after_atomic();
  613. spin_unlock_irqrestore(&cp->cp_lock, flags);
  614. /* now remove the messages from the sock list as needed */
  615. rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
  616. }
  617. EXPORT_SYMBOL_GPL(rds_send_path_drop_acked);
  618. void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
  619. is_acked_func is_acked)
  620. {
  621. WARN_ON(conn->c_trans->t_mp_capable);
  622. rds_send_path_drop_acked(&conn->c_path[0], ack, is_acked);
  623. }
  624. EXPORT_SYMBOL_GPL(rds_send_drop_acked);
  625. void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in6 *dest)
  626. {
  627. struct rds_message *rm, *tmp;
  628. struct rds_connection *conn;
  629. struct rds_conn_path *cp;
  630. unsigned long flags;
  631. LIST_HEAD(list);
  632. /* get all the messages we're dropping under the rs lock */
  633. spin_lock_irqsave(&rs->rs_lock, flags);
  634. list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
  635. if (dest &&
  636. (!ipv6_addr_equal(&dest->sin6_addr, &rm->m_daddr) ||
  637. dest->sin6_port != rm->m_inc.i_hdr.h_dport))
  638. continue;
  639. list_move(&rm->m_sock_item, &list);
  640. rds_send_sndbuf_remove(rs, rm);
  641. clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  642. }
  643. /* order flag updates with the rs lock */
  644. smp_mb__after_atomic();
  645. spin_unlock_irqrestore(&rs->rs_lock, flags);
  646. if (list_empty(&list))
  647. return;
  648. /* Remove the messages from the conn */
  649. list_for_each_entry(rm, &list, m_sock_item) {
  650. conn = rm->m_inc.i_conn;
  651. if (conn->c_trans->t_mp_capable)
  652. cp = rm->m_inc.i_conn_path;
  653. else
  654. cp = &conn->c_path[0];
  655. spin_lock_irqsave(&cp->cp_lock, flags);
  656. /*
  657. * Maybe someone else beat us to removing rm from the conn.
  658. * If we race with their flag update we'll get the lock and
  659. * then really see that the flag has been cleared.
  660. */
  661. if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
  662. spin_unlock_irqrestore(&cp->cp_lock, flags);
  663. continue;
  664. }
  665. list_del_init(&rm->m_conn_item);
  666. spin_unlock_irqrestore(&cp->cp_lock, flags);
  667. /*
  668. * Couldn't grab m_rs_lock in top loop (lock ordering),
  669. * but we can now.
  670. */
  671. spin_lock_irqsave(&rm->m_rs_lock, flags);
  672. spin_lock(&rs->rs_lock);
  673. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  674. spin_unlock(&rs->rs_lock);
  675. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  676. rds_message_put(rm);
  677. }
  678. rds_wake_sk_sleep(rs);
  679. while (!list_empty(&list)) {
  680. rm = list_entry(list.next, struct rds_message, m_sock_item);
  681. list_del_init(&rm->m_sock_item);
  682. rds_message_wait(rm);
  683. /* just in case the code above skipped this message
  684. * because RDS_MSG_ON_CONN wasn't set, run it again here
  685. * taking m_rs_lock is the only thing that keeps us
  686. * from racing with ack processing.
  687. */
  688. spin_lock_irqsave(&rm->m_rs_lock, flags);
  689. spin_lock(&rs->rs_lock);
  690. __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
  691. spin_unlock(&rs->rs_lock);
  692. spin_unlock_irqrestore(&rm->m_rs_lock, flags);
  693. rds_message_put(rm);
  694. }
  695. }
  696. /*
  697. * we only want this to fire once so we use the callers 'queued'. It's
  698. * possible that another thread can race with us and remove the
  699. * message from the flow with RDS_CANCEL_SENT_TO.
  700. */
  701. static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
  702. struct rds_conn_path *cp,
  703. struct rds_message *rm, __be16 sport,
  704. __be16 dport, int *queued)
  705. {
  706. unsigned long flags;
  707. u32 len;
  708. if (*queued)
  709. goto out;
  710. len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
  711. /* this is the only place which holds both the socket's rs_lock
  712. * and the connection's c_lock */
  713. spin_lock_irqsave(&rs->rs_lock, flags);
  714. /*
  715. * If there is a little space in sndbuf, we don't queue anything,
  716. * and userspace gets -EAGAIN. But poll() indicates there's send
  717. * room. This can lead to bad behavior (spinning) if snd_bytes isn't
  718. * freed up by incoming acks. So we check the *old* value of
  719. * rs_snd_bytes here to allow the last msg to exceed the buffer,
  720. * and poll() now knows no more data can be sent.
  721. */
  722. if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
  723. rs->rs_snd_bytes += len;
  724. /* let recv side know we are close to send space exhaustion.
  725. * This is probably not the optimal way to do it, as this
  726. * means we set the flag on *all* messages as soon as our
  727. * throughput hits a certain threshold.
  728. */
  729. if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
  730. set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
  731. list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
  732. set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
  733. rds_message_addref(rm);
  734. sock_hold(rds_rs_to_sk(rs));
  735. rm->m_rs = rs;
  736. /* The code ordering is a little weird, but we're
  737. trying to minimize the time we hold c_lock */
  738. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
  739. rm->m_inc.i_conn = conn;
  740. rm->m_inc.i_conn_path = cp;
  741. rds_message_addref(rm);
  742. spin_lock(&cp->cp_lock);
  743. rm->m_inc.i_hdr.h_sequence = cpu_to_be64(cp->cp_next_tx_seq++);
  744. list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
  745. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  746. spin_unlock(&cp->cp_lock);
  747. rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
  748. rm, len, rs, rs->rs_snd_bytes,
  749. (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
  750. *queued = 1;
  751. }
  752. spin_unlock_irqrestore(&rs->rs_lock, flags);
  753. out:
  754. return *queued;
  755. }
  756. /*
  757. * rds_message is getting to be quite complicated, and we'd like to allocate
  758. * it all in one go. This figures out how big it needs to be up front.
  759. */
  760. static int rds_rm_size(struct msghdr *msg, int num_sgs,
  761. struct rds_iov_vector_arr *vct)
  762. {
  763. struct cmsghdr *cmsg;
  764. int size = 0;
  765. int cmsg_groups = 0;
  766. int retval;
  767. bool zcopy_cookie = false;
  768. struct rds_iov_vector *iov, *tmp_iov;
  769. if (num_sgs < 0)
  770. return -EINVAL;
  771. for_each_cmsghdr(cmsg, msg) {
  772. if (!CMSG_OK(msg, cmsg))
  773. return -EINVAL;
  774. if (cmsg->cmsg_level != SOL_RDS)
  775. continue;
  776. switch (cmsg->cmsg_type) {
  777. case RDS_CMSG_RDMA_ARGS:
  778. if (vct->indx >= vct->len) {
  779. vct->len += vct->incr;
  780. tmp_iov =
  781. krealloc(vct->vec,
  782. vct->len *
  783. sizeof(struct rds_iov_vector),
  784. GFP_KERNEL);
  785. if (!tmp_iov) {
  786. vct->len -= vct->incr;
  787. return -ENOMEM;
  788. }
  789. vct->vec = tmp_iov;
  790. }
  791. iov = &vct->vec[vct->indx];
  792. memset(iov, 0, sizeof(struct rds_iov_vector));
  793. vct->indx++;
  794. cmsg_groups |= 1;
  795. retval = rds_rdma_extra_size(CMSG_DATA(cmsg), iov);
  796. if (retval < 0)
  797. return retval;
  798. size += retval;
  799. break;
  800. case RDS_CMSG_ZCOPY_COOKIE:
  801. zcopy_cookie = true;
  802. fallthrough;
  803. case RDS_CMSG_RDMA_DEST:
  804. case RDS_CMSG_RDMA_MAP:
  805. cmsg_groups |= 2;
  806. /* these are valid but do no add any size */
  807. break;
  808. case RDS_CMSG_ATOMIC_CSWP:
  809. case RDS_CMSG_ATOMIC_FADD:
  810. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  811. case RDS_CMSG_MASKED_ATOMIC_FADD:
  812. cmsg_groups |= 1;
  813. size += sizeof(struct scatterlist);
  814. break;
  815. default:
  816. return -EINVAL;
  817. }
  818. }
  819. if ((msg->msg_flags & MSG_ZEROCOPY) && !zcopy_cookie)
  820. return -EINVAL;
  821. size += num_sgs * sizeof(struct scatterlist);
  822. /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
  823. if (cmsg_groups == 3)
  824. return -EINVAL;
  825. return size;
  826. }
  827. static int rds_cmsg_zcopy(struct rds_sock *rs, struct rds_message *rm,
  828. struct cmsghdr *cmsg)
  829. {
  830. u32 *cookie;
  831. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*cookie)) ||
  832. !rm->data.op_mmp_znotifier)
  833. return -EINVAL;
  834. cookie = CMSG_DATA(cmsg);
  835. rm->data.op_mmp_znotifier->z_cookie = *cookie;
  836. return 0;
  837. }
  838. static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
  839. struct msghdr *msg, int *allocated_mr,
  840. struct rds_iov_vector_arr *vct)
  841. {
  842. struct cmsghdr *cmsg;
  843. int ret = 0, ind = 0;
  844. for_each_cmsghdr(cmsg, msg) {
  845. if (!CMSG_OK(msg, cmsg))
  846. return -EINVAL;
  847. if (cmsg->cmsg_level != SOL_RDS)
  848. continue;
  849. /* As a side effect, RDMA_DEST and RDMA_MAP will set
  850. * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
  851. */
  852. switch (cmsg->cmsg_type) {
  853. case RDS_CMSG_RDMA_ARGS:
  854. if (ind >= vct->indx)
  855. return -ENOMEM;
  856. ret = rds_cmsg_rdma_args(rs, rm, cmsg, &vct->vec[ind]);
  857. ind++;
  858. break;
  859. case RDS_CMSG_RDMA_DEST:
  860. ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
  861. break;
  862. case RDS_CMSG_RDMA_MAP:
  863. ret = rds_cmsg_rdma_map(rs, rm, cmsg);
  864. if (!ret)
  865. *allocated_mr = 1;
  866. else if (ret == -ENODEV)
  867. /* Accommodate the get_mr() case which can fail
  868. * if connection isn't established yet.
  869. */
  870. ret = -EAGAIN;
  871. break;
  872. case RDS_CMSG_ATOMIC_CSWP:
  873. case RDS_CMSG_ATOMIC_FADD:
  874. case RDS_CMSG_MASKED_ATOMIC_CSWP:
  875. case RDS_CMSG_MASKED_ATOMIC_FADD:
  876. ret = rds_cmsg_atomic(rs, rm, cmsg);
  877. break;
  878. case RDS_CMSG_ZCOPY_COOKIE:
  879. ret = rds_cmsg_zcopy(rs, rm, cmsg);
  880. break;
  881. default:
  882. return -EINVAL;
  883. }
  884. if (ret)
  885. break;
  886. }
  887. return ret;
  888. }
  889. static int rds_send_mprds_hash(struct rds_sock *rs,
  890. struct rds_connection *conn, int nonblock)
  891. {
  892. int hash;
  893. if (conn->c_npaths == 0)
  894. hash = RDS_MPATH_HASH(rs, RDS_MPATH_WORKERS);
  895. else
  896. hash = RDS_MPATH_HASH(rs, conn->c_npaths);
  897. if (conn->c_npaths == 0 && hash != 0) {
  898. rds_send_ping(conn, 0);
  899. /* The underlying connection is not up yet. Need to wait
  900. * until it is up to be sure that the non-zero c_path can be
  901. * used. But if we are interrupted, we have to use the zero
  902. * c_path in case the connection ends up being non-MP capable.
  903. */
  904. if (conn->c_npaths == 0) {
  905. /* Cannot wait for the connection be made, so just use
  906. * the base c_path.
  907. */
  908. if (nonblock)
  909. return 0;
  910. if (wait_event_interruptible(conn->c_hs_waitq,
  911. conn->c_npaths != 0))
  912. hash = 0;
  913. }
  914. if (conn->c_npaths == 1)
  915. hash = 0;
  916. }
  917. return hash;
  918. }
  919. static int rds_rdma_bytes(struct msghdr *msg, size_t *rdma_bytes)
  920. {
  921. struct rds_rdma_args *args;
  922. struct cmsghdr *cmsg;
  923. for_each_cmsghdr(cmsg, msg) {
  924. if (!CMSG_OK(msg, cmsg))
  925. return -EINVAL;
  926. if (cmsg->cmsg_level != SOL_RDS)
  927. continue;
  928. if (cmsg->cmsg_type == RDS_CMSG_RDMA_ARGS) {
  929. if (cmsg->cmsg_len <
  930. CMSG_LEN(sizeof(struct rds_rdma_args)))
  931. return -EINVAL;
  932. args = CMSG_DATA(cmsg);
  933. *rdma_bytes += args->remote_vec.bytes;
  934. }
  935. }
  936. return 0;
  937. }
  938. int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
  939. {
  940. struct sock *sk = sock->sk;
  941. struct rds_sock *rs = rds_sk_to_rs(sk);
  942. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  943. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  944. __be16 dport;
  945. struct rds_message *rm = NULL;
  946. struct rds_connection *conn;
  947. int ret = 0;
  948. int queued = 0, allocated_mr = 0;
  949. int nonblock = msg->msg_flags & MSG_DONTWAIT;
  950. long timeo = sock_sndtimeo(sk, nonblock);
  951. struct rds_conn_path *cpath;
  952. struct in6_addr daddr;
  953. __u32 scope_id = 0;
  954. size_t rdma_payload_len = 0;
  955. bool zcopy = ((msg->msg_flags & MSG_ZEROCOPY) &&
  956. sock_flag(rds_rs_to_sk(rs), SOCK_ZEROCOPY));
  957. int num_sgs = DIV_ROUND_UP(payload_len, PAGE_SIZE);
  958. int namelen;
  959. struct rds_iov_vector_arr vct;
  960. int ind;
  961. memset(&vct, 0, sizeof(vct));
  962. /* expect 1 RDMA CMSG per rds_sendmsg. can still grow if more needed. */
  963. vct.incr = 1;
  964. /* Mirror Linux UDP mirror of BSD error message compatibility */
  965. /* XXX: Perhaps MSG_MORE someday */
  966. if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT | MSG_ZEROCOPY)) {
  967. ret = -EOPNOTSUPP;
  968. goto out;
  969. }
  970. namelen = msg->msg_namelen;
  971. if (namelen != 0) {
  972. if (namelen < sizeof(*usin)) {
  973. ret = -EINVAL;
  974. goto out;
  975. }
  976. switch (usin->sin_family) {
  977. case AF_INET:
  978. if (usin->sin_addr.s_addr == htonl(INADDR_ANY) ||
  979. usin->sin_addr.s_addr == htonl(INADDR_BROADCAST) ||
  980. ipv4_is_multicast(usin->sin_addr.s_addr)) {
  981. ret = -EINVAL;
  982. goto out;
  983. }
  984. ipv6_addr_set_v4mapped(usin->sin_addr.s_addr, &daddr);
  985. dport = usin->sin_port;
  986. break;
  987. #if IS_ENABLED(CONFIG_IPV6)
  988. case AF_INET6: {
  989. int addr_type;
  990. if (namelen < sizeof(*sin6)) {
  991. ret = -EINVAL;
  992. goto out;
  993. }
  994. addr_type = ipv6_addr_type(&sin6->sin6_addr);
  995. if (!(addr_type & IPV6_ADDR_UNICAST)) {
  996. __be32 addr4;
  997. if (!(addr_type & IPV6_ADDR_MAPPED)) {
  998. ret = -EINVAL;
  999. goto out;
  1000. }
  1001. /* It is a mapped address. Need to do some
  1002. * sanity checks.
  1003. */
  1004. addr4 = sin6->sin6_addr.s6_addr32[3];
  1005. if (addr4 == htonl(INADDR_ANY) ||
  1006. addr4 == htonl(INADDR_BROADCAST) ||
  1007. ipv4_is_multicast(addr4)) {
  1008. ret = -EINVAL;
  1009. goto out;
  1010. }
  1011. }
  1012. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  1013. if (sin6->sin6_scope_id == 0) {
  1014. ret = -EINVAL;
  1015. goto out;
  1016. }
  1017. scope_id = sin6->sin6_scope_id;
  1018. }
  1019. daddr = sin6->sin6_addr;
  1020. dport = sin6->sin6_port;
  1021. break;
  1022. }
  1023. #endif
  1024. default:
  1025. ret = -EINVAL;
  1026. goto out;
  1027. }
  1028. } else {
  1029. /* We only care about consistency with ->connect() */
  1030. lock_sock(sk);
  1031. daddr = rs->rs_conn_addr;
  1032. dport = rs->rs_conn_port;
  1033. scope_id = rs->rs_bound_scope_id;
  1034. release_sock(sk);
  1035. }
  1036. lock_sock(sk);
  1037. if (ipv6_addr_any(&rs->rs_bound_addr) || ipv6_addr_any(&daddr)) {
  1038. release_sock(sk);
  1039. ret = -ENOTCONN;
  1040. goto out;
  1041. } else if (namelen != 0) {
  1042. /* Cannot send to an IPv4 address using an IPv6 source
  1043. * address and cannot send to an IPv6 address using an
  1044. * IPv4 source address.
  1045. */
  1046. if (ipv6_addr_v4mapped(&daddr) ^
  1047. ipv6_addr_v4mapped(&rs->rs_bound_addr)) {
  1048. release_sock(sk);
  1049. ret = -EOPNOTSUPP;
  1050. goto out;
  1051. }
  1052. /* If the socket is already bound to a link local address,
  1053. * it can only send to peers on the same link. But allow
  1054. * communicating between link local and non-link local address.
  1055. */
  1056. if (scope_id != rs->rs_bound_scope_id) {
  1057. if (!scope_id) {
  1058. scope_id = rs->rs_bound_scope_id;
  1059. } else if (rs->rs_bound_scope_id) {
  1060. release_sock(sk);
  1061. ret = -EINVAL;
  1062. goto out;
  1063. }
  1064. }
  1065. }
  1066. release_sock(sk);
  1067. ret = rds_rdma_bytes(msg, &rdma_payload_len);
  1068. if (ret)
  1069. goto out;
  1070. if (max_t(size_t, payload_len, rdma_payload_len) > RDS_MAX_MSG_SIZE) {
  1071. ret = -EMSGSIZE;
  1072. goto out;
  1073. }
  1074. if (payload_len > rds_sk_sndbuf(rs)) {
  1075. ret = -EMSGSIZE;
  1076. goto out;
  1077. }
  1078. if (zcopy) {
  1079. if (rs->rs_transport->t_type != RDS_TRANS_TCP) {
  1080. ret = -EOPNOTSUPP;
  1081. goto out;
  1082. }
  1083. num_sgs = iov_iter_npages(&msg->msg_iter, INT_MAX);
  1084. }
  1085. /* size of rm including all sgs */
  1086. ret = rds_rm_size(msg, num_sgs, &vct);
  1087. if (ret < 0)
  1088. goto out;
  1089. rm = rds_message_alloc(ret, GFP_KERNEL);
  1090. if (!rm) {
  1091. ret = -ENOMEM;
  1092. goto out;
  1093. }
  1094. /* Attach data to the rm */
  1095. if (payload_len) {
  1096. rm->data.op_sg = rds_message_alloc_sgs(rm, num_sgs);
  1097. if (IS_ERR(rm->data.op_sg)) {
  1098. ret = PTR_ERR(rm->data.op_sg);
  1099. goto out;
  1100. }
  1101. ret = rds_message_copy_from_user(rm, &msg->msg_iter, zcopy);
  1102. if (ret)
  1103. goto out;
  1104. }
  1105. rm->data.op_active = 1;
  1106. rm->m_daddr = daddr;
  1107. /* rds_conn_create has a spinlock that runs with IRQ off.
  1108. * Caching the conn in the socket helps a lot. */
  1109. if (rs->rs_conn && ipv6_addr_equal(&rs->rs_conn->c_faddr, &daddr) &&
  1110. rs->rs_tos == rs->rs_conn->c_tos) {
  1111. conn = rs->rs_conn;
  1112. } else {
  1113. conn = rds_conn_create_outgoing(sock_net(sock->sk),
  1114. &rs->rs_bound_addr, &daddr,
  1115. rs->rs_transport, rs->rs_tos,
  1116. sock->sk->sk_allocation,
  1117. scope_id);
  1118. if (IS_ERR(conn)) {
  1119. ret = PTR_ERR(conn);
  1120. goto out;
  1121. }
  1122. rs->rs_conn = conn;
  1123. }
  1124. if (conn->c_trans->t_mp_capable)
  1125. cpath = &conn->c_path[rds_send_mprds_hash(rs, conn, nonblock)];
  1126. else
  1127. cpath = &conn->c_path[0];
  1128. rm->m_conn_path = cpath;
  1129. /* Parse any control messages the user may have included. */
  1130. ret = rds_cmsg_send(rs, rm, msg, &allocated_mr, &vct);
  1131. if (ret)
  1132. goto out;
  1133. if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
  1134. printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
  1135. &rm->rdma, conn->c_trans->xmit_rdma);
  1136. ret = -EOPNOTSUPP;
  1137. goto out;
  1138. }
  1139. if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
  1140. printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
  1141. &rm->atomic, conn->c_trans->xmit_atomic);
  1142. ret = -EOPNOTSUPP;
  1143. goto out;
  1144. }
  1145. if (rds_destroy_pending(conn)) {
  1146. ret = -EAGAIN;
  1147. goto out;
  1148. }
  1149. if (rds_conn_path_down(cpath))
  1150. rds_check_all_paths(conn);
  1151. ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
  1152. if (ret) {
  1153. rs->rs_seen_congestion = 1;
  1154. goto out;
  1155. }
  1156. while (!rds_send_queue_rm(rs, conn, cpath, rm, rs->rs_bound_port,
  1157. dport, &queued)) {
  1158. rds_stats_inc(s_send_queue_full);
  1159. if (nonblock) {
  1160. ret = -EAGAIN;
  1161. goto out;
  1162. }
  1163. timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
  1164. rds_send_queue_rm(rs, conn, cpath, rm,
  1165. rs->rs_bound_port,
  1166. dport,
  1167. &queued),
  1168. timeo);
  1169. rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
  1170. if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
  1171. continue;
  1172. ret = timeo;
  1173. if (ret == 0)
  1174. ret = -ETIMEDOUT;
  1175. goto out;
  1176. }
  1177. /*
  1178. * By now we've committed to the send. We reuse rds_send_worker()
  1179. * to retry sends in the rds thread if the transport asks us to.
  1180. */
  1181. rds_stats_inc(s_send_queued);
  1182. ret = rds_send_xmit(cpath);
  1183. if (ret == -ENOMEM || ret == -EAGAIN) {
  1184. ret = 0;
  1185. rcu_read_lock();
  1186. if (rds_destroy_pending(cpath->cp_conn))
  1187. ret = -ENETUNREACH;
  1188. else
  1189. queue_delayed_work(rds_wq, &cpath->cp_send_w, 1);
  1190. rcu_read_unlock();
  1191. }
  1192. if (ret)
  1193. goto out;
  1194. rds_message_put(rm);
  1195. for (ind = 0; ind < vct.indx; ind++)
  1196. kfree(vct.vec[ind].iov);
  1197. kfree(vct.vec);
  1198. return payload_len;
  1199. out:
  1200. for (ind = 0; ind < vct.indx; ind++)
  1201. kfree(vct.vec[ind].iov);
  1202. kfree(vct.vec);
  1203. /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
  1204. * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
  1205. * or in any other way, we need to destroy the MR again */
  1206. if (allocated_mr)
  1207. rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
  1208. if (rm)
  1209. rds_message_put(rm);
  1210. return ret;
  1211. }
  1212. /*
  1213. * send out a probe. Can be shared by rds_send_ping,
  1214. * rds_send_pong, rds_send_hb.
  1215. * rds_send_hb should use h_flags
  1216. * RDS_FLAG_HB_PING|RDS_FLAG_ACK_REQUIRED
  1217. * or
  1218. * RDS_FLAG_HB_PONG|RDS_FLAG_ACK_REQUIRED
  1219. */
  1220. static int
  1221. rds_send_probe(struct rds_conn_path *cp, __be16 sport,
  1222. __be16 dport, u8 h_flags)
  1223. {
  1224. struct rds_message *rm;
  1225. unsigned long flags;
  1226. int ret = 0;
  1227. rm = rds_message_alloc(0, GFP_ATOMIC);
  1228. if (!rm) {
  1229. ret = -ENOMEM;
  1230. goto out;
  1231. }
  1232. rm->m_daddr = cp->cp_conn->c_faddr;
  1233. rm->data.op_active = 1;
  1234. rds_conn_path_connect_if_down(cp);
  1235. ret = rds_cong_wait(cp->cp_conn->c_fcong, dport, 1, NULL);
  1236. if (ret)
  1237. goto out;
  1238. spin_lock_irqsave(&cp->cp_lock, flags);
  1239. list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
  1240. set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
  1241. rds_message_addref(rm);
  1242. rm->m_inc.i_conn = cp->cp_conn;
  1243. rm->m_inc.i_conn_path = cp;
  1244. rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport,
  1245. cp->cp_next_tx_seq);
  1246. rm->m_inc.i_hdr.h_flags |= h_flags;
  1247. cp->cp_next_tx_seq++;
  1248. if (RDS_HS_PROBE(be16_to_cpu(sport), be16_to_cpu(dport)) &&
  1249. cp->cp_conn->c_trans->t_mp_capable) {
  1250. u16 npaths = cpu_to_be16(RDS_MPATH_WORKERS);
  1251. u32 my_gen_num = cpu_to_be32(cp->cp_conn->c_my_gen_num);
  1252. rds_message_add_extension(&rm->m_inc.i_hdr,
  1253. RDS_EXTHDR_NPATHS, &npaths,
  1254. sizeof(npaths));
  1255. rds_message_add_extension(&rm->m_inc.i_hdr,
  1256. RDS_EXTHDR_GEN_NUM,
  1257. &my_gen_num,
  1258. sizeof(u32));
  1259. }
  1260. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1261. rds_stats_inc(s_send_queued);
  1262. rds_stats_inc(s_send_pong);
  1263. /* schedule the send work on rds_wq */
  1264. rcu_read_lock();
  1265. if (!rds_destroy_pending(cp->cp_conn))
  1266. queue_delayed_work(rds_wq, &cp->cp_send_w, 1);
  1267. rcu_read_unlock();
  1268. rds_message_put(rm);
  1269. return 0;
  1270. out:
  1271. if (rm)
  1272. rds_message_put(rm);
  1273. return ret;
  1274. }
  1275. int
  1276. rds_send_pong(struct rds_conn_path *cp, __be16 dport)
  1277. {
  1278. return rds_send_probe(cp, 0, dport, 0);
  1279. }
  1280. void
  1281. rds_send_ping(struct rds_connection *conn, int cp_index)
  1282. {
  1283. unsigned long flags;
  1284. struct rds_conn_path *cp = &conn->c_path[cp_index];
  1285. spin_lock_irqsave(&cp->cp_lock, flags);
  1286. if (conn->c_ping_triggered) {
  1287. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1288. return;
  1289. }
  1290. conn->c_ping_triggered = 1;
  1291. spin_unlock_irqrestore(&cp->cp_lock, flags);
  1292. rds_send_probe(cp, cpu_to_be16(RDS_FLAG_PROBE_PORT), 0, 0);
  1293. }
  1294. EXPORT_SYMBOL_GPL(rds_send_ping);