blob: 966311d135af4c5e869e242b29a864524b6ed9c6 [file] [log] [blame]
Andy Grover5c115592009-02-24 15:30:27 +00001/*
2 * Copyright (c) 2006 Oracle. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33#include <linux/kernel.h>
Paul Gortmakerd9b93842011-09-18 13:21:27 -040034#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/gfp.h>
Andy Grover5c115592009-02-24 15:30:27 +000036#include <net/sock.h>
37#include <linux/in.h>
38#include <linux/list.h>
Manuel Zerpiescb0a6052011-06-16 02:09:57 +000039#include <linux/ratelimit.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040040#include <linux/export.h>
Santosh Shilimkar4bebdd72015-09-10 11:57:14 -070041#include <linux/sizes.h>
Andy Grover5c115592009-02-24 15:30:27 +000042
Sowmini Varadhan0cb43962016-06-13 09:44:26 -070043#include "rds_single_path.h"
Andy Grover5c115592009-02-24 15:30:27 +000044#include "rds.h"
Andy Grover5c115592009-02-24 15:30:27 +000045
46/* When transmitting messages in rds_send_xmit, we need to emerge from
47 * time to time and briefly release the CPU. Otherwise the softlock watchdog
48 * will kick our shin.
49 * Also, it seems fairer to not let one busy connection stall all the
50 * others.
51 *
52 * send_batch_count is the number of times we'll loop in send_xmit. Setting
53 * it to 0 will restore the old behavior (where we looped until we had
54 * drained the queue).
55 */
Santosh Shilimkar4bebdd72015-09-10 11:57:14 -070056static int send_batch_count = SZ_1K;
Andy Grover5c115592009-02-24 15:30:27 +000057module_param(send_batch_count, int, 0444);
58MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
59
stephen hemmingerff51bf82010-10-19 08:08:33 +000060static void rds_send_remove_from_sock(struct list_head *messages, int status);
61
Andy Grover5c115592009-02-24 15:30:27 +000062/*
Zach Brown0f4b1c72010-06-04 14:41:41 -070063 * Reset the send state. Callers must ensure that this doesn't race with
64 * rds_send_xmit().
Andy Grover5c115592009-02-24 15:30:27 +000065 */
Sowmini Varadhan4e9b5512016-06-13 09:44:30 -070066static void rds_send_path_reset(struct rds_conn_path *cp)
Andy Grover5c115592009-02-24 15:30:27 +000067{
68 struct rds_message *rm, *tmp;
69 unsigned long flags;
70
Sowmini Varadhan4e9b5512016-06-13 09:44:30 -070071 if (cp->cp_xmit_rm) {
72 rm = cp->cp_xmit_rm;
73 cp->cp_xmit_rm = NULL;
Andy Grover5c115592009-02-24 15:30:27 +000074 /* Tell the user the RDMA op is no longer mapped by the
75 * transport. This isn't entirely true (it's flushed out
76 * independently) but as the connection is down, there's
77 * no ongoing RDMA to/from that memory */
Chris Mason7e3f2952010-05-11 15:11:11 -070078 rds_message_unmapped(rm);
Chris Mason7e3f2952010-05-11 15:11:11 -070079 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +000080 }
Chris Mason7e3f2952010-05-11 15:11:11 -070081
Sowmini Varadhan4e9b5512016-06-13 09:44:30 -070082 cp->cp_xmit_sg = 0;
83 cp->cp_xmit_hdr_off = 0;
84 cp->cp_xmit_data_off = 0;
85 cp->cp_xmit_atomic_sent = 0;
86 cp->cp_xmit_rdma_sent = 0;
87 cp->cp_xmit_data_sent = 0;
Andy Grover5c115592009-02-24 15:30:27 +000088
Sowmini Varadhan4e9b5512016-06-13 09:44:30 -070089 cp->cp_conn->c_map_queued = 0;
Andy Grover5c115592009-02-24 15:30:27 +000090
Sowmini Varadhan4e9b5512016-06-13 09:44:30 -070091 cp->cp_unacked_packets = rds_sysctl_max_unacked_packets;
92 cp->cp_unacked_bytes = rds_sysctl_max_unacked_bytes;
Andy Grover5c115592009-02-24 15:30:27 +000093
94 /* Mark messages as retransmissions, and move them to the send q */
Sowmini Varadhan4e9b5512016-06-13 09:44:30 -070095 spin_lock_irqsave(&cp->cp_lock, flags);
96 list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
Andy Grover5c115592009-02-24 15:30:27 +000097 set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
98 set_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags);
99 }
Sowmini Varadhan4e9b5512016-06-13 09:44:30 -0700100 list_splice_init(&cp->cp_retrans, &cp->cp_send_queue);
101 spin_unlock_irqrestore(&cp->cp_lock, flags);
102}
103
104void rds_send_reset(struct rds_connection *conn)
105{
106 rds_send_path_reset(&conn->c_path[0]);
Andy Grover5c115592009-02-24 15:30:27 +0000107}
Sowmini Varadhan0b6f7602016-06-04 13:59:59 -0700108EXPORT_SYMBOL_GPL(rds_send_reset);
Andy Grover5c115592009-02-24 15:30:27 +0000109
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700110static int acquire_in_xmit(struct rds_conn_path *cp)
Zach Brown0f4b1c72010-06-04 14:41:41 -0700111{
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700112 return test_and_set_bit(RDS_IN_XMIT, &cp->cp_flags) == 0;
Zach Brown0f4b1c72010-06-04 14:41:41 -0700113}
114
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700115static void release_in_xmit(struct rds_conn_path *cp)
Zach Brown0f4b1c72010-06-04 14:41:41 -0700116{
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700117 clear_bit(RDS_IN_XMIT, &cp->cp_flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100118 smp_mb__after_atomic();
Zach Brown0f4b1c72010-06-04 14:41:41 -0700119 /*
120 * We don't use wait_on_bit()/wake_up_bit() because our waking is in a
121 * hot path and finding waiters is very rare. We don't want to walk
122 * the system-wide hashed waitqueue buckets in the fast path only to
123 * almost never find waiters.
124 */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700125 if (waitqueue_active(&cp->cp_waitq))
126 wake_up_all(&cp->cp_waitq);
Zach Brown0f4b1c72010-06-04 14:41:41 -0700127}
128
Andy Grover5c115592009-02-24 15:30:27 +0000129/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300130 * We're making the conscious trade-off here to only send one message
Andy Grover5c115592009-02-24 15:30:27 +0000131 * down the connection at a time.
132 * Pro:
133 * - tx queueing is a simple fifo list
134 * - reassembly is optional and easily done by transports per conn
135 * - no per flow rx lookup at all, straight to the socket
136 * - less per-frag memory and wire overhead
137 * Con:
138 * - queued acks can be delayed behind large messages
139 * Depends:
140 * - small message latency is higher behind queued large messages
141 * - large message latency isn't starved by intervening small sends
142 */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700143int rds_send_xmit(struct rds_conn_path *cp)
Andy Grover5c115592009-02-24 15:30:27 +0000144{
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700145 struct rds_connection *conn = cp->cp_conn;
Andy Grover5c115592009-02-24 15:30:27 +0000146 struct rds_message *rm;
147 unsigned long flags;
148 unsigned int tmp;
Andy Grover5c115592009-02-24 15:30:27 +0000149 struct scatterlist *sg;
150 int ret = 0;
Andy Grover5c115592009-02-24 15:30:27 +0000151 LIST_HEAD(to_be_dropped);
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400152 int batch_count;
153 unsigned long send_gen = 0;
Andy Grover5c115592009-02-24 15:30:27 +0000154
Andy Groverfcc54502010-03-29 17:08:49 -0700155restart:
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400156 batch_count = 0;
Andy Grover049ee3f2010-03-23 17:39:07 -0700157
Andy Grover5c115592009-02-24 15:30:27 +0000158 /*
159 * sendmsg calls here after having queued its message on the send
160 * queue. We only have one task feeding the connection at a time. If
161 * another thread is already feeding the queue then we back off. This
162 * avoids blocking the caller and trading per-connection data between
163 * caches per message.
Andy Grover5c115592009-02-24 15:30:27 +0000164 */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700165 if (!acquire_in_xmit(cp)) {
Andy Grover049ee3f2010-03-23 17:39:07 -0700166 rds_stats_inc(s_send_lock_contention);
Andy Grover5c115592009-02-24 15:30:27 +0000167 ret = -ENOMEM;
168 goto out;
169 }
Zach Brown0f4b1c72010-06-04 14:41:41 -0700170
171 /*
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400172 * we record the send generation after doing the xmit acquire.
173 * if someone else manages to jump in and do some work, we'll use
174 * this to avoid a goto restart farther down.
175 *
176 * The acquire_in_xmit() check above ensures that only one
177 * caller can increment c_send_gen at any time.
178 */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700179 cp->cp_send_gen++;
180 send_gen = cp->cp_send_gen;
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400181
182 /*
Zach Brown0f4b1c72010-06-04 14:41:41 -0700183 * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT,
184 * we do the opposite to avoid races.
185 */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700186 if (!rds_conn_path_up(cp)) {
187 release_in_xmit(cp);
Zach Brown0f4b1c72010-06-04 14:41:41 -0700188 ret = 0;
189 goto out;
190 }
Andy Grover5c115592009-02-24 15:30:27 +0000191
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700192 if (conn->c_trans->t_mp_capable) {
193 if (conn->c_trans->xmit_path_prepare)
194 conn->c_trans->xmit_path_prepare(cp);
195 } else if (conn->c_trans->xmit_prepare) {
Andy Grover5c115592009-02-24 15:30:27 +0000196 conn->c_trans->xmit_prepare(conn);
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700197 }
Andy Grover5c115592009-02-24 15:30:27 +0000198
199 /*
200 * spin trying to push headers and data down the connection until
Andy Grover5b2366b2010-02-03 19:36:44 -0800201 * the connection doesn't make forward progress.
Andy Grover5c115592009-02-24 15:30:27 +0000202 */
Andy Groverfcc54502010-03-29 17:08:49 -0700203 while (1) {
Andy Grover5c115592009-02-24 15:30:27 +0000204
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700205 rm = cp->cp_xmit_rm;
Andy Grover5c115592009-02-24 15:30:27 +0000206
Andy Grover5b2366b2010-02-03 19:36:44 -0800207 /*
208 * If between sending messages, we can send a pending congestion
209 * map update.
Andy Grover5c115592009-02-24 15:30:27 +0000210 */
Andy Grover8690bfa2010-01-12 11:56:44 -0800211 if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
Andy Grover77dd5502010-03-22 15:22:04 -0700212 rm = rds_cong_update_alloc(conn);
213 if (IS_ERR(rm)) {
214 ret = PTR_ERR(rm);
215 break;
Andy Grover5b2366b2010-02-03 19:36:44 -0800216 }
Andy Grover77dd5502010-03-22 15:22:04 -0700217 rm->data.op_active = 1;
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700218 rm->m_inc.i_conn_path = cp;
219 rm->m_inc.i_conn = cp->cp_conn;
Andy Grover77dd5502010-03-22 15:22:04 -0700220
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700221 cp->cp_xmit_rm = rm;
Andy Grover5c115592009-02-24 15:30:27 +0000222 }
223
224 /*
Andy Grover5b2366b2010-02-03 19:36:44 -0800225 * If not already working on one, grab the next message.
Andy Grover5c115592009-02-24 15:30:27 +0000226 *
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700227 * cp_xmit_rm holds a ref while we're sending this message down
Andy Grover5c115592009-02-24 15:30:27 +0000228 * the connction. We can use this ref while holding the
229 * send_sem.. rds_send_reset() is serialized with it.
230 */
Andy Grover8690bfa2010-01-12 11:56:44 -0800231 if (!rm) {
Andy Grover5c115592009-02-24 15:30:27 +0000232 unsigned int len;
233
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400234 batch_count++;
235
236 /* we want to process as big a batch as we can, but
237 * we also want to avoid softlockups. If we've been
238 * through a lot of messages, lets back off and see
239 * if anyone else jumps in
240 */
Santosh Shilimkar4bebdd72015-09-10 11:57:14 -0700241 if (batch_count >= send_batch_count)
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400242 goto over_batch;
243
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700244 spin_lock_irqsave(&cp->cp_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000245
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700246 if (!list_empty(&cp->cp_send_queue)) {
247 rm = list_entry(cp->cp_send_queue.next,
Andy Grover5c115592009-02-24 15:30:27 +0000248 struct rds_message,
249 m_conn_item);
250 rds_message_addref(rm);
251
252 /*
253 * Move the message from the send queue to the retransmit
254 * list right away.
255 */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700256 list_move_tail(&rm->m_conn_item,
257 &cp->cp_retrans);
Andy Grover5c115592009-02-24 15:30:27 +0000258 }
259
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700260 spin_unlock_irqrestore(&cp->cp_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000261
Andy Groverfcc54502010-03-29 17:08:49 -0700262 if (!rm)
Andy Grover5c115592009-02-24 15:30:27 +0000263 break;
Andy Grover5c115592009-02-24 15:30:27 +0000264
265 /* Unfortunately, the way Infiniband deals with
266 * RDMA to a bad MR key is by moving the entire
267 * queue pair to error state. We cold possibly
268 * recover from that, but right now we drop the
269 * connection.
270 * Therefore, we never retransmit messages with RDMA ops.
271 */
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800272 if (rm->rdma.op_active &&
Joe Perchesf64f9e72009-11-29 16:55:45 -0800273 test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) {
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700274 spin_lock_irqsave(&cp->cp_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000275 if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags))
276 list_move(&rm->m_conn_item, &to_be_dropped);
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700277 spin_unlock_irqrestore(&cp->cp_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000278 continue;
279 }
280
281 /* Require an ACK every once in a while */
282 len = ntohl(rm->m_inc.i_hdr.h_len);
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700283 if (cp->cp_unacked_packets == 0 ||
284 cp->cp_unacked_bytes < len) {
Andy Grover5c115592009-02-24 15:30:27 +0000285 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
286
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700287 cp->cp_unacked_packets =
288 rds_sysctl_max_unacked_packets;
289 cp->cp_unacked_bytes =
290 rds_sysctl_max_unacked_bytes;
Andy Grover5c115592009-02-24 15:30:27 +0000291 rds_stats_inc(s_send_ack_required);
292 } else {
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700293 cp->cp_unacked_bytes -= len;
294 cp->cp_unacked_packets--;
Andy Grover5c115592009-02-24 15:30:27 +0000295 }
296
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700297 cp->cp_xmit_rm = rm;
Andy Grover5c115592009-02-24 15:30:27 +0000298 }
299
Andy Grover2c3a5f92010-03-01 16:10:40 -0800300 /* The transport either sends the whole rdma or none of it */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700301 if (rm->rdma.op_active && !cp->cp_xmit_rdma_sent) {
Andy Groverff3d7d32010-03-01 14:03:09 -0800302 rm->m_final_op = &rm->rdma;
santosh.shilimkar@oracle.com4f731132015-08-22 15:45:29 -0700303 /* The transport owns the mapped memory for now.
304 * You can't unmap it while it's on the send queue
305 */
306 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
Andy Grover2c3a5f92010-03-01 16:10:40 -0800307 ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
santosh.shilimkar@oracle.com4f731132015-08-22 15:45:29 -0700308 if (ret) {
309 clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
310 wake_up_interruptible(&rm->m_flush_wait);
Andy Grover2c3a5f92010-03-01 16:10:40 -0800311 break;
santosh.shilimkar@oracle.com4f731132015-08-22 15:45:29 -0700312 }
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700313 cp->cp_xmit_rdma_sent = 1;
Andy Grover2c3a5f92010-03-01 16:10:40 -0800314
Andy Grover2c3a5f92010-03-01 16:10:40 -0800315 }
316
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700317 if (rm->atomic.op_active && !cp->cp_xmit_atomic_sent) {
Andy Groverff3d7d32010-03-01 14:03:09 -0800318 rm->m_final_op = &rm->atomic;
santosh.shilimkar@oracle.com4f731132015-08-22 15:45:29 -0700319 /* The transport owns the mapped memory for now.
320 * You can't unmap it while it's on the send queue
321 */
322 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
Andy Groverff3d7d32010-03-01 14:03:09 -0800323 ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
santosh.shilimkar@oracle.com4f731132015-08-22 15:45:29 -0700324 if (ret) {
325 clear_bit(RDS_MSG_MAPPED, &rm->m_flags);
326 wake_up_interruptible(&rm->m_flush_wait);
Andy Grover15133f62010-01-12 14:33:38 -0800327 break;
santosh.shilimkar@oracle.com4f731132015-08-22 15:45:29 -0700328 }
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700329 cp->cp_xmit_atomic_sent = 1;
Andy Groverff3d7d32010-03-01 14:03:09 -0800330
Andy Grover15133f62010-01-12 14:33:38 -0800331 }
332
Andy Grover2c3a5f92010-03-01 16:10:40 -0800333 /*
334 * A number of cases require an RDS header to be sent
335 * even if there is no data.
336 * We permit 0-byte sends; rds-ping depends on this.
337 * However, if there are exclusively attached silent ops,
338 * we skip the hdr/data send, to enable silent operation.
339 */
340 if (rm->data.op_nents == 0) {
341 int ops_present;
342 int all_ops_are_silent = 1;
Andy Grover241eef32010-01-19 21:25:26 -0800343
Andy Grover2c3a5f92010-03-01 16:10:40 -0800344 ops_present = (rm->atomic.op_active || rm->rdma.op_active);
345 if (rm->atomic.op_active && !rm->atomic.op_silent)
346 all_ops_are_silent = 0;
347 if (rm->rdma.op_active && !rm->rdma.op_silent)
348 all_ops_are_silent = 0;
Andy Grover241eef32010-01-19 21:25:26 -0800349
Andy Grover2c3a5f92010-03-01 16:10:40 -0800350 if (ops_present && all_ops_are_silent
351 && !rm->m_rdma_cookie)
352 rm->data.op_active = 0;
Andy Grover5c115592009-02-24 15:30:27 +0000353 }
354
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700355 if (rm->data.op_active && !cp->cp_xmit_data_sent) {
Andy Groverff3d7d32010-03-01 14:03:09 -0800356 rm->m_final_op = &rm->data;
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700357
Andy Grover5c115592009-02-24 15:30:27 +0000358 ret = conn->c_trans->xmit(conn, rm,
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700359 cp->cp_xmit_hdr_off,
360 cp->cp_xmit_sg,
361 cp->cp_xmit_data_off);
Andy Grover5c115592009-02-24 15:30:27 +0000362 if (ret <= 0)
363 break;
364
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700365 if (cp->cp_xmit_hdr_off < sizeof(struct rds_header)) {
Andy Grover5c115592009-02-24 15:30:27 +0000366 tmp = min_t(int, ret,
367 sizeof(struct rds_header) -
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700368 cp->cp_xmit_hdr_off);
369 cp->cp_xmit_hdr_off += tmp;
Andy Grover5c115592009-02-24 15:30:27 +0000370 ret -= tmp;
371 }
372
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700373 sg = &rm->data.op_sg[cp->cp_xmit_sg];
Andy Grover5c115592009-02-24 15:30:27 +0000374 while (ret) {
375 tmp = min_t(int, ret, sg->length -
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700376 cp->cp_xmit_data_off);
377 cp->cp_xmit_data_off += tmp;
Andy Grover5c115592009-02-24 15:30:27 +0000378 ret -= tmp;
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700379 if (cp->cp_xmit_data_off == sg->length) {
380 cp->cp_xmit_data_off = 0;
Andy Grover5c115592009-02-24 15:30:27 +0000381 sg++;
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700382 cp->cp_xmit_sg++;
383 BUG_ON(ret != 0 && cp->cp_xmit_sg ==
384 rm->data.op_nents);
Andy Grover5c115592009-02-24 15:30:27 +0000385 }
386 }
Andy Grover5b2366b2010-02-03 19:36:44 -0800387
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700388 if (cp->cp_xmit_hdr_off == sizeof(struct rds_header) &&
389 (cp->cp_xmit_sg == rm->data.op_nents))
390 cp->cp_xmit_data_sent = 1;
Andy Grover5b2366b2010-02-03 19:36:44 -0800391 }
392
393 /*
394 * A rm will only take multiple times through this loop
395 * if there is a data op. Thus, if the data is sent (or there was
396 * none), then we're done with the rm.
397 */
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700398 if (!rm->data.op_active || cp->cp_xmit_data_sent) {
399 cp->cp_xmit_rm = NULL;
400 cp->cp_xmit_sg = 0;
401 cp->cp_xmit_hdr_off = 0;
402 cp->cp_xmit_data_off = 0;
403 cp->cp_xmit_rdma_sent = 0;
404 cp->cp_xmit_atomic_sent = 0;
405 cp->cp_xmit_data_sent = 0;
Andy Grover5b2366b2010-02-03 19:36:44 -0800406
407 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +0000408 }
409 }
410
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400411over_batch:
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700412 if (conn->c_trans->t_mp_capable) {
413 if (conn->c_trans->xmit_path_complete)
414 conn->c_trans->xmit_path_complete(cp);
415 } else if (conn->c_trans->xmit_complete) {
Andy Grover5c115592009-02-24 15:30:27 +0000416 conn->c_trans->xmit_complete(conn);
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700417 }
418 release_in_xmit(cp);
Andy Grover5c115592009-02-24 15:30:27 +0000419
Andy Grover2ad80992010-03-23 17:48:04 -0700420 /* Nuke any messages we decided not to retransmit. */
421 if (!list_empty(&to_be_dropped)) {
422 /* irqs on here, so we can put(), unlike above */
423 list_for_each_entry(rm, &to_be_dropped, m_conn_item)
424 rds_message_put(rm);
425 rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
426 }
427
Andy Groverfcc54502010-03-29 17:08:49 -0700428 /*
Zach Brown0f4b1c72010-06-04 14:41:41 -0700429 * Other senders can queue a message after we last test the send queue
430 * but before we clear RDS_IN_XMIT. In that case they'd back off and
431 * not try and send their newly queued message. We need to check the
432 * send queue after having cleared RDS_IN_XMIT so that their message
433 * doesn't get stuck on the send queue.
Andy Groverfcc54502010-03-29 17:08:49 -0700434 *
435 * If the transport cannot continue (i.e ret != 0), then it must
436 * call us when more room is available, such as from the tx
437 * completion handler.
Sowmini Varadhan443be0e2015-04-08 12:33:47 -0400438 *
439 * We have an extra generation check here so that if someone manages
440 * to jump in after our release_in_xmit, we'll see that they have done
441 * some work and we will skip our goto
Andy Groverfcc54502010-03-29 17:08:49 -0700442 */
443 if (ret == 0) {
Chris Mason9e29db02010-04-15 16:38:14 -0400444 smp_mb();
santosh.shilimkar@oracle.com0c484242015-08-22 15:45:27 -0700445 if ((test_bit(0, &conn->c_map_queued) ||
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700446 !list_empty(&cp->cp_send_queue)) &&
447 send_gen == cp->cp_send_gen) {
Andy Grover049ee3f2010-03-23 17:39:07 -0700448 rds_stats_inc(s_send_lock_queue_raced);
Santosh Shilimkar4bebdd72015-09-10 11:57:14 -0700449 if (batch_count < send_batch_count)
450 goto restart;
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -0700451 queue_delayed_work(rds_wq, &cp->cp_send_w, 1);
Andy Grover5c115592009-02-24 15:30:27 +0000452 }
Andy Grover5c115592009-02-24 15:30:27 +0000453 }
454out:
455 return ret;
456}
Santosh Shilimkar0c28c042015-09-06 02:18:51 -0400457EXPORT_SYMBOL_GPL(rds_send_xmit);
Andy Grover5c115592009-02-24 15:30:27 +0000458
459static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
460{
461 u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
462
463 assert_spin_locked(&rs->rs_lock);
464
465 BUG_ON(rs->rs_snd_bytes < len);
466 rs->rs_snd_bytes -= len;
467
468 if (rs->rs_snd_bytes == 0)
469 rds_stats_inc(s_send_queue_empty);
470}
471
472static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
473 is_acked_func is_acked)
474{
475 if (is_acked)
476 return is_acked(rm, ack);
477 return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
478}
479
480/*
Andy Grover5c115592009-02-24 15:30:27 +0000481 * This is pretty similar to what happens below in the ACK
482 * handling code - except that we call here as soon as we get
483 * the IB send completion on the RDMA op and the accompanying
484 * message.
485 */
486void rds_rdma_send_complete(struct rds_message *rm, int status)
487{
488 struct rds_sock *rs = NULL;
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800489 struct rm_rdma_op *ro;
Andy Grover5c115592009-02-24 15:30:27 +0000490 struct rds_notifier *notifier;
Andy Grover9de08642010-03-29 16:50:54 -0700491 unsigned long flags;
Andy Grover5c115592009-02-24 15:30:27 +0000492
Andy Grover9de08642010-03-29 16:50:54 -0700493 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000494
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800495 ro = &rm->rdma;
Joe Perchesf64f9e72009-11-29 16:55:45 -0800496 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800497 ro->op_active && ro->op_notify && ro->op_notifier) {
498 notifier = ro->op_notifier;
Andy Grover5c115592009-02-24 15:30:27 +0000499 rs = rm->m_rs;
500 sock_hold(rds_rs_to_sk(rs));
501
502 notifier->n_status = status;
503 spin_lock(&rs->rs_lock);
504 list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
505 spin_unlock(&rs->rs_lock);
506
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800507 ro->op_notifier = NULL;
Andy Grover5c115592009-02-24 15:30:27 +0000508 }
509
Andy Grover9de08642010-03-29 16:50:54 -0700510 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000511
512 if (rs) {
513 rds_wake_sk_sleep(rs);
514 sock_put(rds_rs_to_sk(rs));
515 }
516}
Andy Grover616b7572009-08-21 12:28:32 +0000517EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
Andy Grover5c115592009-02-24 15:30:27 +0000518
519/*
Andy Grover15133f62010-01-12 14:33:38 -0800520 * Just like above, except looks at atomic op
521 */
522void 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;
Andy Grovercf4b7382010-03-29 16:50:54 -0700527 unsigned long flags;
Andy Grover15133f62010-01-12 14:33:38 -0800528
Andy Grovercf4b7382010-03-29 16:50:54 -0700529 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover15133f62010-01-12 14:33:38 -0800530
531 ao = &rm->atomic;
532 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
533 && ao->op_active && ao->op_notify && ao->op_notifier) {
534 notifier = ao->op_notifier;
535 rs = rm->m_rs;
536 sock_hold(rds_rs_to_sk(rs));
537
538 notifier->n_status = status;
539 spin_lock(&rs->rs_lock);
540 list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
541 spin_unlock(&rs->rs_lock);
542
543 ao->op_notifier = NULL;
544 }
545
Andy Grovercf4b7382010-03-29 16:50:54 -0700546 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover15133f62010-01-12 14:33:38 -0800547
548 if (rs) {
549 rds_wake_sk_sleep(rs);
550 sock_put(rds_rs_to_sk(rs));
551 }
552}
553EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
554
555/*
Andy Grover5c115592009-02-24 15:30:27 +0000556 * This is the same as rds_rdma_send_complete except we
557 * don't do any locking - we have all the ingredients (message,
558 * socket, socket lock) and can just move the notifier.
559 */
560static inline void
Andy Grover940786e2010-02-19 18:04:58 -0800561__rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
Andy Grover5c115592009-02-24 15:30:27 +0000562{
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800563 struct rm_rdma_op *ro;
Andy Grover940786e2010-02-19 18:04:58 -0800564 struct rm_atomic_op *ao;
Andy Grover5c115592009-02-24 15:30:27 +0000565
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800566 ro = &rm->rdma;
567 if (ro->op_active && ro->op_notify && ro->op_notifier) {
568 ro->op_notifier->n_status = status;
569 list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
570 ro->op_notifier = NULL;
Andy Grover5c115592009-02-24 15:30:27 +0000571 }
572
Andy Grover940786e2010-02-19 18:04:58 -0800573 ao = &rm->atomic;
574 if (ao->op_active && ao->op_notify && ao->op_notifier) {
575 ao->op_notifier->n_status = status;
576 list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
577 ao->op_notifier = NULL;
578 }
579
Andy Grover5c115592009-02-24 15:30:27 +0000580 /* No need to wake the app - caller does this */
581}
582
583/*
Andy Grover5c115592009-02-24 15:30:27 +0000584 * This removes messages from the socket's list if they're on it. The list
585 * argument must be private to the caller, we must be able to modify it
586 * without locks. The messages must have a reference held for their
587 * position on the list. This function will drop that reference after
588 * removing the messages from the 'messages' list regardless of if it found
589 * the messages on the socket list or not.
590 */
stephen hemmingerff51bf82010-10-19 08:08:33 +0000591static void rds_send_remove_from_sock(struct list_head *messages, int status)
Andy Grover5c115592009-02-24 15:30:27 +0000592{
Andy Grover561c7df2010-03-11 13:50:06 +0000593 unsigned long flags;
Andy Grover5c115592009-02-24 15:30:27 +0000594 struct rds_sock *rs = NULL;
595 struct rds_message *rm;
596
Andy Grover5c115592009-02-24 15:30:27 +0000597 while (!list_empty(messages)) {
Andy Grover561c7df2010-03-11 13:50:06 +0000598 int was_on_sock = 0;
599
Andy Grover5c115592009-02-24 15:30:27 +0000600 rm = list_entry(messages->next, struct rds_message,
601 m_conn_item);
602 list_del_init(&rm->m_conn_item);
603
604 /*
605 * If we see this flag cleared then we're *sure* that someone
606 * else beat us to removing it from the sock. If we race
607 * with their flag update we'll get the lock and then really
608 * see that the flag has been cleared.
609 *
610 * The message spinlock makes sure nobody clears rm->m_rs
611 * while we're messing with it. It does not prevent the
612 * message from being removed from the socket, though.
613 */
Andy Grover561c7df2010-03-11 13:50:06 +0000614 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000615 if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
616 goto unlock_and_drop;
617
618 if (rs != rm->m_rs) {
619 if (rs) {
Andy Grover5c115592009-02-24 15:30:27 +0000620 rds_wake_sk_sleep(rs);
621 sock_put(rds_rs_to_sk(rs));
622 }
623 rs = rm->m_rs;
Herton R. Krzesinski593cbb32014-10-01 18:49:54 -0300624 if (rs)
625 sock_hold(rds_rs_to_sk(rs));
Andy Grover5c115592009-02-24 15:30:27 +0000626 }
Herton R. Krzesinski593cbb32014-10-01 18:49:54 -0300627 if (!rs)
628 goto unlock_and_drop;
Tina Yang048c15e2010-03-11 13:50:00 +0000629 spin_lock(&rs->rs_lock);
Andy Grover5c115592009-02-24 15:30:27 +0000630
631 if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800632 struct rm_rdma_op *ro = &rm->rdma;
Andy Grover5c115592009-02-24 15:30:27 +0000633 struct rds_notifier *notifier;
634
635 list_del_init(&rm->m_sock_item);
636 rds_send_sndbuf_remove(rs, rm);
637
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800638 if (ro->op_active && ro->op_notifier &&
639 (ro->op_notify || (ro->op_recverr && status))) {
640 notifier = ro->op_notifier;
Andy Grover5c115592009-02-24 15:30:27 +0000641 list_add_tail(&notifier->n_list,
642 &rs->rs_notify_queue);
643 if (!notifier->n_status)
644 notifier->n_status = status;
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800645 rm->rdma.op_notifier = NULL;
Andy Grover5c115592009-02-24 15:30:27 +0000646 }
Andy Grover561c7df2010-03-11 13:50:06 +0000647 was_on_sock = 1;
Andy Grover5c115592009-02-24 15:30:27 +0000648 rm->m_rs = NULL;
649 }
Tina Yang048c15e2010-03-11 13:50:00 +0000650 spin_unlock(&rs->rs_lock);
Andy Grover5c115592009-02-24 15:30:27 +0000651
652unlock_and_drop:
Andy Grover561c7df2010-03-11 13:50:06 +0000653 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000654 rds_message_put(rm);
Andy Grover561c7df2010-03-11 13:50:06 +0000655 if (was_on_sock)
656 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +0000657 }
658
659 if (rs) {
Andy Grover5c115592009-02-24 15:30:27 +0000660 rds_wake_sk_sleep(rs);
661 sock_put(rds_rs_to_sk(rs));
662 }
Andy Grover5c115592009-02-24 15:30:27 +0000663}
664
665/*
666 * Transports call here when they've determined that the receiver queued
667 * messages up to, and including, the given sequence number. Messages are
668 * moved to the retrans queue when rds_send_xmit picks them off the send
669 * queue. This means that in the TCP case, the message may not have been
670 * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
671 * checks the RDS_MSG_HAS_ACK_SEQ bit.
Andy Grover5c115592009-02-24 15:30:27 +0000672 */
Sowmini Varadhan5c3d2742016-06-13 09:44:31 -0700673void rds_send_path_drop_acked(struct rds_conn_path *cp, u64 ack,
674 is_acked_func is_acked)
Andy Grover5c115592009-02-24 15:30:27 +0000675{
676 struct rds_message *rm, *tmp;
677 unsigned long flags;
678 LIST_HEAD(list);
679
Sowmini Varadhan5c3d2742016-06-13 09:44:31 -0700680 spin_lock_irqsave(&cp->cp_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000681
Sowmini Varadhan5c3d2742016-06-13 09:44:31 -0700682 list_for_each_entry_safe(rm, tmp, &cp->cp_retrans, m_conn_item) {
Andy Grover5c115592009-02-24 15:30:27 +0000683 if (!rds_send_is_acked(rm, ack, is_acked))
684 break;
685
686 list_move(&rm->m_conn_item, &list);
687 clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
688 }
689
690 /* order flag updates with spin locks */
691 if (!list_empty(&list))
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100692 smp_mb__after_atomic();
Andy Grover5c115592009-02-24 15:30:27 +0000693
Sowmini Varadhan5c3d2742016-06-13 09:44:31 -0700694 spin_unlock_irqrestore(&cp->cp_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000695
696 /* now remove the messages from the sock list as needed */
697 rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
698}
Sowmini Varadhan5c3d2742016-06-13 09:44:31 -0700699EXPORT_SYMBOL_GPL(rds_send_path_drop_acked);
700
701void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
702 is_acked_func is_acked)
703{
704 WARN_ON(conn->c_trans->t_mp_capable);
705 rds_send_path_drop_acked(&conn->c_path[0], ack, is_acked);
706}
Andy Grover616b7572009-08-21 12:28:32 +0000707EXPORT_SYMBOL_GPL(rds_send_drop_acked);
Andy Grover5c115592009-02-24 15:30:27 +0000708
709void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
710{
711 struct rds_message *rm, *tmp;
712 struct rds_connection *conn;
Andy Grover7c82eaf2010-02-19 18:01:41 -0800713 unsigned long flags;
Andy Grover5c115592009-02-24 15:30:27 +0000714 LIST_HEAD(list);
Andy Grover5c115592009-02-24 15:30:27 +0000715
716 /* get all the messages we're dropping under the rs lock */
717 spin_lock_irqsave(&rs->rs_lock, flags);
718
719 list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
720 if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
721 dest->sin_port != rm->m_inc.i_hdr.h_dport))
722 continue;
723
Andy Grover5c115592009-02-24 15:30:27 +0000724 list_move(&rm->m_sock_item, &list);
725 rds_send_sndbuf_remove(rs, rm);
726 clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
Andy Grover5c115592009-02-24 15:30:27 +0000727 }
728
729 /* order flag updates with the rs lock */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100730 smp_mb__after_atomic();
Andy Grover5c115592009-02-24 15:30:27 +0000731
732 spin_unlock_irqrestore(&rs->rs_lock, flags);
733
Andy Grover7c82eaf2010-02-19 18:01:41 -0800734 if (list_empty(&list))
735 return;
Andy Grover5c115592009-02-24 15:30:27 +0000736
Andy Grover7c82eaf2010-02-19 18:01:41 -0800737 /* Remove the messages from the conn */
Andy Grover5c115592009-02-24 15:30:27 +0000738 list_for_each_entry(rm, &list, m_sock_item) {
Andy Grover7c82eaf2010-02-19 18:01:41 -0800739
740 conn = rm->m_inc.i_conn;
Andy Grover7c82eaf2010-02-19 18:01:41 -0800741
Andy Grover9de08642010-03-29 16:50:54 -0700742 spin_lock_irqsave(&conn->c_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800743 /*
744 * Maybe someone else beat us to removing rm from the conn.
745 * If we race with their flag update we'll get the lock and
746 * then really see that the flag has been cleared.
747 */
748 if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
749 spin_unlock_irqrestore(&conn->c_lock, flags);
Herton R. Krzesinski593cbb32014-10-01 18:49:54 -0300750 spin_lock_irqsave(&rm->m_rs_lock, flags);
751 rm->m_rs = NULL;
752 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800753 continue;
754 }
Andy Grover9de08642010-03-29 16:50:54 -0700755 list_del_init(&rm->m_conn_item);
756 spin_unlock_irqrestore(&conn->c_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800757
758 /*
759 * Couldn't grab m_rs_lock in top loop (lock ordering),
760 * but we can now.
761 */
Andy Grover9de08642010-03-29 16:50:54 -0700762 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800763
Tina Yang550a8002010-03-11 13:50:03 +0000764 spin_lock(&rs->rs_lock);
Andy Grover940786e2010-02-19 18:04:58 -0800765 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
Tina Yang550a8002010-03-11 13:50:03 +0000766 spin_unlock(&rs->rs_lock);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800767
Andy Grover5c115592009-02-24 15:30:27 +0000768 rm->m_rs = NULL;
Andy Grover9de08642010-03-29 16:50:54 -0700769 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000770
Andy Grover7c82eaf2010-02-19 18:01:41 -0800771 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +0000772 }
773
Andy Grover7c82eaf2010-02-19 18:01:41 -0800774 rds_wake_sk_sleep(rs);
Tina Yang550a8002010-03-11 13:50:03 +0000775
Andy Grover5c115592009-02-24 15:30:27 +0000776 while (!list_empty(&list)) {
777 rm = list_entry(list.next, struct rds_message, m_sock_item);
778 list_del_init(&rm->m_sock_item);
Andy Grover5c115592009-02-24 15:30:27 +0000779 rds_message_wait(rm);
santosh.shilimkar@oracle.comdfcec252015-08-22 15:45:33 -0700780
781 /* just in case the code above skipped this message
782 * because RDS_MSG_ON_CONN wasn't set, run it again here
783 * taking m_rs_lock is the only thing that keeps us
784 * from racing with ack processing.
785 */
786 spin_lock_irqsave(&rm->m_rs_lock, flags);
787
788 spin_lock(&rs->rs_lock);
789 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
790 spin_unlock(&rs->rs_lock);
791
792 rm->m_rs = NULL;
793 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
794
Andy Grover5c115592009-02-24 15:30:27 +0000795 rds_message_put(rm);
796 }
797}
798
799/*
800 * we only want this to fire once so we use the callers 'queued'. It's
801 * possible that another thread can race with us and remove the
802 * message from the flow with RDS_CANCEL_SENT_TO.
803 */
804static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700805 struct rds_conn_path *cp,
Andy Grover5c115592009-02-24 15:30:27 +0000806 struct rds_message *rm, __be16 sport,
807 __be16 dport, int *queued)
808{
809 unsigned long flags;
810 u32 len;
811
812 if (*queued)
813 goto out;
814
815 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
816
817 /* this is the only place which holds both the socket's rs_lock
818 * and the connection's c_lock */
819 spin_lock_irqsave(&rs->rs_lock, flags);
820
821 /*
822 * If there is a little space in sndbuf, we don't queue anything,
823 * and userspace gets -EAGAIN. But poll() indicates there's send
824 * room. This can lead to bad behavior (spinning) if snd_bytes isn't
825 * freed up by incoming acks. So we check the *old* value of
826 * rs_snd_bytes here to allow the last msg to exceed the buffer,
827 * and poll() now knows no more data can be sent.
828 */
829 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
830 rs->rs_snd_bytes += len;
831
832 /* let recv side know we are close to send space exhaustion.
833 * This is probably not the optimal way to do it, as this
834 * means we set the flag on *all* messages as soon as our
835 * throughput hits a certain threshold.
836 */
837 if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
838 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
839
840 list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
841 set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
842 rds_message_addref(rm);
843 rm->m_rs = rs;
844
845 /* The code ordering is a little weird, but we're
846 trying to minimize the time we hold c_lock */
847 rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
848 rm->m_inc.i_conn = conn;
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700849 rm->m_inc.i_conn_path = cp;
Andy Grover5c115592009-02-24 15:30:27 +0000850 rds_message_addref(rm);
851
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700852 spin_lock(&cp->cp_lock);
853 rm->m_inc.i_hdr.h_sequence = cpu_to_be64(cp->cp_next_tx_seq++);
854 list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
Andy Grover5c115592009-02-24 15:30:27 +0000855 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700856 spin_unlock(&cp->cp_lock);
Andy Grover5c115592009-02-24 15:30:27 +0000857
858 rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
859 rm, len, rs, rs->rs_snd_bytes,
860 (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
861
862 *queued = 1;
863 }
864
865 spin_unlock_irqrestore(&rs->rs_lock, flags);
866out:
867 return *queued;
868}
869
Andy Groverfc445082010-01-12 12:56:06 -0800870/*
871 * rds_message is getting to be quite complicated, and we'd like to allocate
872 * it all in one go. This figures out how big it needs to be up front.
873 */
874static int rds_rm_size(struct msghdr *msg, int data_len)
875{
Andy Groverff87e972010-01-12 14:13:15 -0800876 struct cmsghdr *cmsg;
Andy Groverfc445082010-01-12 12:56:06 -0800877 int size = 0;
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700878 int cmsg_groups = 0;
Andy Groverff87e972010-01-12 14:13:15 -0800879 int retval;
Andy Groverfc445082010-01-12 12:56:06 -0800880
Gu Zhengf95b4142014-12-11 11:22:04 +0800881 for_each_cmsghdr(cmsg, msg) {
Andy Groverff87e972010-01-12 14:13:15 -0800882 if (!CMSG_OK(msg, cmsg))
883 return -EINVAL;
884
885 if (cmsg->cmsg_level != SOL_RDS)
886 continue;
887
888 switch (cmsg->cmsg_type) {
889 case RDS_CMSG_RDMA_ARGS:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700890 cmsg_groups |= 1;
Andy Groverff87e972010-01-12 14:13:15 -0800891 retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
892 if (retval < 0)
893 return retval;
894 size += retval;
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700895
Andy Groverff87e972010-01-12 14:13:15 -0800896 break;
897
898 case RDS_CMSG_RDMA_DEST:
899 case RDS_CMSG_RDMA_MAP:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700900 cmsg_groups |= 2;
Andy Groverff87e972010-01-12 14:13:15 -0800901 /* these are valid but do no add any size */
902 break;
903
Andy Grover15133f62010-01-12 14:33:38 -0800904 case RDS_CMSG_ATOMIC_CSWP:
905 case RDS_CMSG_ATOMIC_FADD:
Andy Grover20c72bd2010-08-25 05:51:28 -0700906 case RDS_CMSG_MASKED_ATOMIC_CSWP:
907 case RDS_CMSG_MASKED_ATOMIC_FADD:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700908 cmsg_groups |= 1;
Andy Grover15133f62010-01-12 14:33:38 -0800909 size += sizeof(struct scatterlist);
910 break;
911
Andy Groverff87e972010-01-12 14:13:15 -0800912 default:
913 return -EINVAL;
914 }
915
916 }
917
918 size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
Andy Groverfc445082010-01-12 12:56:06 -0800919
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700920 /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
921 if (cmsg_groups == 3)
922 return -EINVAL;
923
Andy Groverfc445082010-01-12 12:56:06 -0800924 return size;
925}
926
Andy Grover5c115592009-02-24 15:30:27 +0000927static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
928 struct msghdr *msg, int *allocated_mr)
929{
930 struct cmsghdr *cmsg;
931 int ret = 0;
932
Gu Zhengf95b4142014-12-11 11:22:04 +0800933 for_each_cmsghdr(cmsg, msg) {
Andy Grover5c115592009-02-24 15:30:27 +0000934 if (!CMSG_OK(msg, cmsg))
935 return -EINVAL;
936
937 if (cmsg->cmsg_level != SOL_RDS)
938 continue;
939
940 /* As a side effect, RDMA_DEST and RDMA_MAP will set
Andy Grover15133f62010-01-12 14:33:38 -0800941 * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
Andy Grover5c115592009-02-24 15:30:27 +0000942 */
943 switch (cmsg->cmsg_type) {
944 case RDS_CMSG_RDMA_ARGS:
945 ret = rds_cmsg_rdma_args(rs, rm, cmsg);
946 break;
947
948 case RDS_CMSG_RDMA_DEST:
949 ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
950 break;
951
952 case RDS_CMSG_RDMA_MAP:
953 ret = rds_cmsg_rdma_map(rs, rm, cmsg);
954 if (!ret)
955 *allocated_mr = 1;
956 break;
Andy Grover15133f62010-01-12 14:33:38 -0800957 case RDS_CMSG_ATOMIC_CSWP:
958 case RDS_CMSG_ATOMIC_FADD:
Andy Grover20c72bd2010-08-25 05:51:28 -0700959 case RDS_CMSG_MASKED_ATOMIC_CSWP:
960 case RDS_CMSG_MASKED_ATOMIC_FADD:
Andy Grover15133f62010-01-12 14:33:38 -0800961 ret = rds_cmsg_atomic(rs, rm, cmsg);
962 break;
Andy Grover5c115592009-02-24 15:30:27 +0000963
964 default:
965 return -EINVAL;
966 }
967
968 if (ret)
969 break;
970 }
971
972 return ret;
973}
974
Ying Xue1b784142015-03-02 15:37:48 +0800975int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
Andy Grover5c115592009-02-24 15:30:27 +0000976{
977 struct sock *sk = sock->sk;
978 struct rds_sock *rs = rds_sk_to_rs(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100979 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
Andy Grover5c115592009-02-24 15:30:27 +0000980 __be32 daddr;
981 __be16 dport;
982 struct rds_message *rm = NULL;
983 struct rds_connection *conn;
984 int ret = 0;
985 int queued = 0, allocated_mr = 0;
986 int nonblock = msg->msg_flags & MSG_DONTWAIT;
Andy Grover1123fd72010-03-11 13:49:56 +0000987 long timeo = sock_sndtimeo(sk, nonblock);
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700988 struct rds_conn_path *cpath;
Andy Grover5c115592009-02-24 15:30:27 +0000989
990 /* Mirror Linux UDP mirror of BSD error message compatibility */
991 /* XXX: Perhaps MSG_MORE someday */
992 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
Andy Grover5c115592009-02-24 15:30:27 +0000993 ret = -EOPNOTSUPP;
994 goto out;
995 }
996
997 if (msg->msg_namelen) {
998 /* XXX fail non-unicast destination IPs? */
999 if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
1000 ret = -EINVAL;
1001 goto out;
1002 }
1003 daddr = usin->sin_addr.s_addr;
1004 dport = usin->sin_port;
1005 } else {
1006 /* We only care about consistency with ->connect() */
1007 lock_sock(sk);
1008 daddr = rs->rs_conn_addr;
1009 dport = rs->rs_conn_port;
1010 release_sock(sk);
1011 }
1012
Quentin Casasnovas8c7188b2015-11-24 17:13:21 -05001013 lock_sock(sk);
Andy Grover5c115592009-02-24 15:30:27 +00001014 if (daddr == 0 || rs->rs_bound_addr == 0) {
Quentin Casasnovas8c7188b2015-11-24 17:13:21 -05001015 release_sock(sk);
Andy Grover5c115592009-02-24 15:30:27 +00001016 ret = -ENOTCONN; /* XXX not a great errno */
1017 goto out;
1018 }
Quentin Casasnovas8c7188b2015-11-24 17:13:21 -05001019 release_sock(sk);
Andy Grover5c115592009-02-24 15:30:27 +00001020
Mukesh Kacker06e89412015-08-22 15:45:34 -07001021 if (payload_len > rds_sk_sndbuf(rs)) {
1022 ret = -EMSGSIZE;
1023 goto out;
1024 }
1025
Andy Groverfc445082010-01-12 12:56:06 -08001026 /* size of rm including all sgs */
1027 ret = rds_rm_size(msg, payload_len);
1028 if (ret < 0)
1029 goto out;
1030
1031 rm = rds_message_alloc(ret, GFP_KERNEL);
1032 if (!rm) {
1033 ret = -ENOMEM;
Andy Grover5c115592009-02-24 15:30:27 +00001034 goto out;
1035 }
1036
Andy Grover372cd7d2010-02-03 19:40:32 -08001037 /* Attach data to the rm */
1038 if (payload_len) {
1039 rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
Andy Groverd139ff02010-10-28 15:40:59 +00001040 if (!rm->data.op_sg) {
1041 ret = -ENOMEM;
1042 goto out;
1043 }
Al Viroc0371da2014-11-24 10:42:55 -05001044 ret = rds_message_copy_from_user(rm, &msg->msg_iter);
Andy Grover372cd7d2010-02-03 19:40:32 -08001045 if (ret)
1046 goto out;
1047 }
1048 rm->data.op_active = 1;
Andy Groverfc445082010-01-12 12:56:06 -08001049
Andy Grover5c115592009-02-24 15:30:27 +00001050 rm->m_daddr = daddr;
1051
Andy Grover5c115592009-02-24 15:30:27 +00001052 /* rds_conn_create has a spinlock that runs with IRQ off.
1053 * Caching the conn in the socket helps a lot. */
1054 if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
1055 conn = rs->rs_conn;
1056 else {
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -04001057 conn = rds_conn_create_outgoing(sock_net(sock->sk),
1058 rs->rs_bound_addr, daddr,
Andy Grover5c115592009-02-24 15:30:27 +00001059 rs->rs_transport,
1060 sock->sk->sk_allocation);
1061 if (IS_ERR(conn)) {
1062 ret = PTR_ERR(conn);
1063 goto out;
1064 }
1065 rs->rs_conn = conn;
1066 }
1067
Andy Grover49f69692009-04-09 14:09:41 +00001068 /* Parse any control messages the user may have included. */
1069 ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
1070 if (ret)
1071 goto out;
1072
Andy Grover2c3a5f92010-03-01 16:10:40 -08001073 if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
Manuel Zerpiescb0a6052011-06-16 02:09:57 +00001074 printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
Andy Groverf8b3aaf2010-03-01 14:11:53 -08001075 &rm->rdma, conn->c_trans->xmit_rdma);
Andy Grover15133f62010-01-12 14:33:38 -08001076 ret = -EOPNOTSUPP;
1077 goto out;
1078 }
1079
1080 if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
Manuel Zerpiescb0a6052011-06-16 02:09:57 +00001081 printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
Andy Grover15133f62010-01-12 14:33:38 -08001082 &rm->atomic, conn->c_trans->xmit_atomic);
Andy Grover5c115592009-02-24 15:30:27 +00001083 ret = -EOPNOTSUPP;
1084 goto out;
1085 }
1086
Zach Brownf3c68082010-05-24 13:14:36 -07001087 rds_conn_connect_if_down(conn);
Andy Grover5c115592009-02-24 15:30:27 +00001088
1089 ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
Andy Groverb98ba522010-03-11 13:50:04 +00001090 if (ret) {
1091 rs->rs_seen_congestion = 1;
Andy Grover5c115592009-02-24 15:30:27 +00001092 goto out;
Andy Groverb98ba522010-03-11 13:50:04 +00001093 }
Andy Grover5c115592009-02-24 15:30:27 +00001094
Sowmini Varadhan780a6d92016-06-13 09:44:33 -07001095 cpath = &conn->c_path[0];
1096
1097 while (!rds_send_queue_rm(rs, conn, cpath, rm, rs->rs_bound_port,
Andy Grover5c115592009-02-24 15:30:27 +00001098 dport, &queued)) {
1099 rds_stats_inc(s_send_queue_full);
Mukesh Kacker06e89412015-08-22 15:45:34 -07001100
Andy Grover5c115592009-02-24 15:30:27 +00001101 if (nonblock) {
1102 ret = -EAGAIN;
1103 goto out;
1104 }
1105
Eric Dumazetaa395142010-04-20 13:03:51 +00001106 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sowmini Varadhan780a6d92016-06-13 09:44:33 -07001107 rds_send_queue_rm(rs, conn, cpath, rm,
Andy Grover5c115592009-02-24 15:30:27 +00001108 rs->rs_bound_port,
1109 dport,
1110 &queued),
1111 timeo);
1112 rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
1113 if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
1114 continue;
1115
1116 ret = timeo;
1117 if (ret == 0)
1118 ret = -ETIMEDOUT;
1119 goto out;
1120 }
1121
1122 /*
1123 * By now we've committed to the send. We reuse rds_send_worker()
1124 * to retry sends in the rds thread if the transport asks us to.
1125 */
1126 rds_stats_inc(s_send_queued);
1127
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -07001128 ret = rds_send_xmit(cpath);
Santosh Shilimkardb6526d2015-09-11 15:44:29 -07001129 if (ret == -ENOMEM || ret == -EAGAIN)
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -07001130 queue_delayed_work(rds_wq, &cpath->cp_send_w, 1);
Andy Grover5c115592009-02-24 15:30:27 +00001131
1132 rds_message_put(rm);
1133 return payload_len;
1134
1135out:
1136 /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
1137 * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
1138 * or in any other way, we need to destroy the MR again */
1139 if (allocated_mr)
1140 rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
1141
1142 if (rm)
1143 rds_message_put(rm);
1144 return ret;
1145}
1146
1147/*
1148 * Reply to a ping packet.
1149 */
1150int
1151rds_send_pong(struct rds_connection *conn, __be16 dport)
1152{
1153 struct rds_message *rm;
1154 unsigned long flags;
1155 int ret = 0;
1156
1157 rm = rds_message_alloc(0, GFP_ATOMIC);
Andy Grover8690bfa2010-01-12 11:56:44 -08001158 if (!rm) {
Andy Grover5c115592009-02-24 15:30:27 +00001159 ret = -ENOMEM;
1160 goto out;
1161 }
1162
1163 rm->m_daddr = conn->c_faddr;
Andy Groveracfcd4d2010-03-31 18:56:25 -07001164 rm->data.op_active = 1;
Andy Grover5c115592009-02-24 15:30:27 +00001165
Zach Brownf3c68082010-05-24 13:14:36 -07001166 rds_conn_connect_if_down(conn);
Andy Grover5c115592009-02-24 15:30:27 +00001167
1168 ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
1169 if (ret)
1170 goto out;
1171
1172 spin_lock_irqsave(&conn->c_lock, flags);
1173 list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
1174 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
1175 rds_message_addref(rm);
1176 rm->m_inc.i_conn = conn;
1177
1178 rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
1179 conn->c_next_tx_seq);
1180 conn->c_next_tx_seq++;
1181 spin_unlock_irqrestore(&conn->c_lock, flags);
1182
1183 rds_stats_inc(s_send_queued);
1184 rds_stats_inc(s_send_pong);
1185
santosh.shilimkar@oracle.com7b4b0002015-10-16 22:13:21 -04001186 /* schedule the send work on rds_wq */
1187 queue_delayed_work(rds_wq, &conn->c_send_w, 1);
Andy Groveracfcd4d2010-03-31 18:56:25 -07001188
Andy Grover5c115592009-02-24 15:30:27 +00001189 rds_message_put(rm);
1190 return 0;
1191
1192out:
1193 if (rm)
1194 rds_message_put(rm);
1195 return ret;
1196}