send.c 41 KB

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