blob: 9c34fd204639260f0751c8c8a317e08311e94a48 [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;
Sowmini Varadhan01ff34e2016-06-13 09:44:35 -0700713 struct rds_conn_path *cp;
Andy Grover7c82eaf2010-02-19 18:01:41 -0800714 unsigned long flags;
Andy Grover5c115592009-02-24 15:30:27 +0000715 LIST_HEAD(list);
Andy Grover5c115592009-02-24 15:30:27 +0000716
717 /* get all the messages we're dropping under the rs lock */
718 spin_lock_irqsave(&rs->rs_lock, flags);
719
720 list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
721 if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
722 dest->sin_port != rm->m_inc.i_hdr.h_dport))
723 continue;
724
Andy Grover5c115592009-02-24 15:30:27 +0000725 list_move(&rm->m_sock_item, &list);
726 rds_send_sndbuf_remove(rs, rm);
727 clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
Andy Grover5c115592009-02-24 15:30:27 +0000728 }
729
730 /* order flag updates with the rs lock */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100731 smp_mb__after_atomic();
Andy Grover5c115592009-02-24 15:30:27 +0000732
733 spin_unlock_irqrestore(&rs->rs_lock, flags);
734
Andy Grover7c82eaf2010-02-19 18:01:41 -0800735 if (list_empty(&list))
736 return;
Andy Grover5c115592009-02-24 15:30:27 +0000737
Andy Grover7c82eaf2010-02-19 18:01:41 -0800738 /* Remove the messages from the conn */
Andy Grover5c115592009-02-24 15:30:27 +0000739 list_for_each_entry(rm, &list, m_sock_item) {
Andy Grover7c82eaf2010-02-19 18:01:41 -0800740
741 conn = rm->m_inc.i_conn;
Sowmini Varadhan01ff34e2016-06-13 09:44:35 -0700742 if (conn->c_trans->t_mp_capable)
743 cp = rm->m_inc.i_conn_path;
744 else
745 cp = &conn->c_path[0];
Andy Grover7c82eaf2010-02-19 18:01:41 -0800746
Sowmini Varadhan01ff34e2016-06-13 09:44:35 -0700747 spin_lock_irqsave(&cp->cp_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800748 /*
749 * Maybe someone else beat us to removing rm from the conn.
750 * If we race with their flag update we'll get the lock and
751 * then really see that the flag has been cleared.
752 */
753 if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
Sowmini Varadhan01ff34e2016-06-13 09:44:35 -0700754 spin_unlock_irqrestore(&cp->cp_lock, flags);
Herton R. Krzesinski593cbb32014-10-01 18:49:54 -0300755 spin_lock_irqsave(&rm->m_rs_lock, flags);
756 rm->m_rs = NULL;
757 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800758 continue;
759 }
Andy Grover9de08642010-03-29 16:50:54 -0700760 list_del_init(&rm->m_conn_item);
Sowmini Varadhan01ff34e2016-06-13 09:44:35 -0700761 spin_unlock_irqrestore(&cp->cp_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800762
763 /*
764 * Couldn't grab m_rs_lock in top loop (lock ordering),
765 * but we can now.
766 */
Andy Grover9de08642010-03-29 16:50:54 -0700767 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800768
Tina Yang550a8002010-03-11 13:50:03 +0000769 spin_lock(&rs->rs_lock);
Andy Grover940786e2010-02-19 18:04:58 -0800770 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
Tina Yang550a8002010-03-11 13:50:03 +0000771 spin_unlock(&rs->rs_lock);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800772
Andy Grover5c115592009-02-24 15:30:27 +0000773 rm->m_rs = NULL;
Andy Grover9de08642010-03-29 16:50:54 -0700774 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000775
Andy Grover7c82eaf2010-02-19 18:01:41 -0800776 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +0000777 }
778
Andy Grover7c82eaf2010-02-19 18:01:41 -0800779 rds_wake_sk_sleep(rs);
Tina Yang550a8002010-03-11 13:50:03 +0000780
Andy Grover5c115592009-02-24 15:30:27 +0000781 while (!list_empty(&list)) {
782 rm = list_entry(list.next, struct rds_message, m_sock_item);
783 list_del_init(&rm->m_sock_item);
Andy Grover5c115592009-02-24 15:30:27 +0000784 rds_message_wait(rm);
santosh.shilimkar@oracle.comdfcec252015-08-22 15:45:33 -0700785
786 /* just in case the code above skipped this message
787 * because RDS_MSG_ON_CONN wasn't set, run it again here
788 * taking m_rs_lock is the only thing that keeps us
789 * from racing with ack processing.
790 */
791 spin_lock_irqsave(&rm->m_rs_lock, flags);
792
793 spin_lock(&rs->rs_lock);
794 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
795 spin_unlock(&rs->rs_lock);
796
797 rm->m_rs = NULL;
798 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
799
Andy Grover5c115592009-02-24 15:30:27 +0000800 rds_message_put(rm);
801 }
802}
803
804/*
805 * we only want this to fire once so we use the callers 'queued'. It's
806 * possible that another thread can race with us and remove the
807 * message from the flow with RDS_CANCEL_SENT_TO.
808 */
809static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700810 struct rds_conn_path *cp,
Andy Grover5c115592009-02-24 15:30:27 +0000811 struct rds_message *rm, __be16 sport,
812 __be16 dport, int *queued)
813{
814 unsigned long flags;
815 u32 len;
816
817 if (*queued)
818 goto out;
819
820 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
821
822 /* this is the only place which holds both the socket's rs_lock
823 * and the connection's c_lock */
824 spin_lock_irqsave(&rs->rs_lock, flags);
825
826 /*
827 * If there is a little space in sndbuf, we don't queue anything,
828 * and userspace gets -EAGAIN. But poll() indicates there's send
829 * room. This can lead to bad behavior (spinning) if snd_bytes isn't
830 * freed up by incoming acks. So we check the *old* value of
831 * rs_snd_bytes here to allow the last msg to exceed the buffer,
832 * and poll() now knows no more data can be sent.
833 */
834 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
835 rs->rs_snd_bytes += len;
836
837 /* let recv side know we are close to send space exhaustion.
838 * This is probably not the optimal way to do it, as this
839 * means we set the flag on *all* messages as soon as our
840 * throughput hits a certain threshold.
841 */
842 if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
843 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
844
845 list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
846 set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
847 rds_message_addref(rm);
848 rm->m_rs = rs;
849
850 /* The code ordering is a little weird, but we're
851 trying to minimize the time we hold c_lock */
852 rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
853 rm->m_inc.i_conn = conn;
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700854 rm->m_inc.i_conn_path = cp;
Andy Grover5c115592009-02-24 15:30:27 +0000855 rds_message_addref(rm);
856
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700857 spin_lock(&cp->cp_lock);
858 rm->m_inc.i_hdr.h_sequence = cpu_to_be64(cp->cp_next_tx_seq++);
859 list_add_tail(&rm->m_conn_item, &cp->cp_send_queue);
Andy Grover5c115592009-02-24 15:30:27 +0000860 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700861 spin_unlock(&cp->cp_lock);
Andy Grover5c115592009-02-24 15:30:27 +0000862
863 rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
864 rm, len, rs, rs->rs_snd_bytes,
865 (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
866
867 *queued = 1;
868 }
869
870 spin_unlock_irqrestore(&rs->rs_lock, flags);
871out:
872 return *queued;
873}
874
Andy Groverfc445082010-01-12 12:56:06 -0800875/*
876 * rds_message is getting to be quite complicated, and we'd like to allocate
877 * it all in one go. This figures out how big it needs to be up front.
878 */
879static int rds_rm_size(struct msghdr *msg, int data_len)
880{
Andy Groverff87e972010-01-12 14:13:15 -0800881 struct cmsghdr *cmsg;
Andy Groverfc445082010-01-12 12:56:06 -0800882 int size = 0;
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700883 int cmsg_groups = 0;
Andy Groverff87e972010-01-12 14:13:15 -0800884 int retval;
Andy Groverfc445082010-01-12 12:56:06 -0800885
Gu Zhengf95b4142014-12-11 11:22:04 +0800886 for_each_cmsghdr(cmsg, msg) {
Andy Groverff87e972010-01-12 14:13:15 -0800887 if (!CMSG_OK(msg, cmsg))
888 return -EINVAL;
889
890 if (cmsg->cmsg_level != SOL_RDS)
891 continue;
892
893 switch (cmsg->cmsg_type) {
894 case RDS_CMSG_RDMA_ARGS:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700895 cmsg_groups |= 1;
Andy Groverff87e972010-01-12 14:13:15 -0800896 retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
897 if (retval < 0)
898 return retval;
899 size += retval;
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700900
Andy Groverff87e972010-01-12 14:13:15 -0800901 break;
902
903 case RDS_CMSG_RDMA_DEST:
904 case RDS_CMSG_RDMA_MAP:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700905 cmsg_groups |= 2;
Andy Groverff87e972010-01-12 14:13:15 -0800906 /* these are valid but do no add any size */
907 break;
908
Andy Grover15133f62010-01-12 14:33:38 -0800909 case RDS_CMSG_ATOMIC_CSWP:
910 case RDS_CMSG_ATOMIC_FADD:
Andy Grover20c72bd2010-08-25 05:51:28 -0700911 case RDS_CMSG_MASKED_ATOMIC_CSWP:
912 case RDS_CMSG_MASKED_ATOMIC_FADD:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700913 cmsg_groups |= 1;
Andy Grover15133f62010-01-12 14:33:38 -0800914 size += sizeof(struct scatterlist);
915 break;
916
Andy Groverff87e972010-01-12 14:13:15 -0800917 default:
918 return -EINVAL;
919 }
920
921 }
922
923 size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
Andy Groverfc445082010-01-12 12:56:06 -0800924
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700925 /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
926 if (cmsg_groups == 3)
927 return -EINVAL;
928
Andy Groverfc445082010-01-12 12:56:06 -0800929 return size;
930}
931
Andy Grover5c115592009-02-24 15:30:27 +0000932static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
933 struct msghdr *msg, int *allocated_mr)
934{
935 struct cmsghdr *cmsg;
936 int ret = 0;
937
Gu Zhengf95b4142014-12-11 11:22:04 +0800938 for_each_cmsghdr(cmsg, msg) {
Andy Grover5c115592009-02-24 15:30:27 +0000939 if (!CMSG_OK(msg, cmsg))
940 return -EINVAL;
941
942 if (cmsg->cmsg_level != SOL_RDS)
943 continue;
944
945 /* As a side effect, RDMA_DEST and RDMA_MAP will set
Andy Grover15133f62010-01-12 14:33:38 -0800946 * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
Andy Grover5c115592009-02-24 15:30:27 +0000947 */
948 switch (cmsg->cmsg_type) {
949 case RDS_CMSG_RDMA_ARGS:
950 ret = rds_cmsg_rdma_args(rs, rm, cmsg);
951 break;
952
953 case RDS_CMSG_RDMA_DEST:
954 ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
955 break;
956
957 case RDS_CMSG_RDMA_MAP:
958 ret = rds_cmsg_rdma_map(rs, rm, cmsg);
959 if (!ret)
960 *allocated_mr = 1;
961 break;
Andy Grover15133f62010-01-12 14:33:38 -0800962 case RDS_CMSG_ATOMIC_CSWP:
963 case RDS_CMSG_ATOMIC_FADD:
Andy Grover20c72bd2010-08-25 05:51:28 -0700964 case RDS_CMSG_MASKED_ATOMIC_CSWP:
965 case RDS_CMSG_MASKED_ATOMIC_FADD:
Andy Grover15133f62010-01-12 14:33:38 -0800966 ret = rds_cmsg_atomic(rs, rm, cmsg);
967 break;
Andy Grover5c115592009-02-24 15:30:27 +0000968
969 default:
970 return -EINVAL;
971 }
972
973 if (ret)
974 break;
975 }
976
977 return ret;
978}
979
Ying Xue1b784142015-03-02 15:37:48 +0800980int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
Andy Grover5c115592009-02-24 15:30:27 +0000981{
982 struct sock *sk = sock->sk;
983 struct rds_sock *rs = rds_sk_to_rs(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100984 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
Andy Grover5c115592009-02-24 15:30:27 +0000985 __be32 daddr;
986 __be16 dport;
987 struct rds_message *rm = NULL;
988 struct rds_connection *conn;
989 int ret = 0;
990 int queued = 0, allocated_mr = 0;
991 int nonblock = msg->msg_flags & MSG_DONTWAIT;
Andy Grover1123fd72010-03-11 13:49:56 +0000992 long timeo = sock_sndtimeo(sk, nonblock);
Sowmini Varadhan780a6d92016-06-13 09:44:33 -0700993 struct rds_conn_path *cpath;
Andy Grover5c115592009-02-24 15:30:27 +0000994
995 /* Mirror Linux UDP mirror of BSD error message compatibility */
996 /* XXX: Perhaps MSG_MORE someday */
997 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
Andy Grover5c115592009-02-24 15:30:27 +0000998 ret = -EOPNOTSUPP;
999 goto out;
1000 }
1001
1002 if (msg->msg_namelen) {
1003 /* XXX fail non-unicast destination IPs? */
1004 if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
1005 ret = -EINVAL;
1006 goto out;
1007 }
1008 daddr = usin->sin_addr.s_addr;
1009 dport = usin->sin_port;
1010 } else {
1011 /* We only care about consistency with ->connect() */
1012 lock_sock(sk);
1013 daddr = rs->rs_conn_addr;
1014 dport = rs->rs_conn_port;
1015 release_sock(sk);
1016 }
1017
Quentin Casasnovas8c7188b2015-11-24 17:13:21 -05001018 lock_sock(sk);
Andy Grover5c115592009-02-24 15:30:27 +00001019 if (daddr == 0 || rs->rs_bound_addr == 0) {
Quentin Casasnovas8c7188b2015-11-24 17:13:21 -05001020 release_sock(sk);
Andy Grover5c115592009-02-24 15:30:27 +00001021 ret = -ENOTCONN; /* XXX not a great errno */
1022 goto out;
1023 }
Quentin Casasnovas8c7188b2015-11-24 17:13:21 -05001024 release_sock(sk);
Andy Grover5c115592009-02-24 15:30:27 +00001025
Mukesh Kacker06e89412015-08-22 15:45:34 -07001026 if (payload_len > rds_sk_sndbuf(rs)) {
1027 ret = -EMSGSIZE;
1028 goto out;
1029 }
1030
Andy Groverfc445082010-01-12 12:56:06 -08001031 /* size of rm including all sgs */
1032 ret = rds_rm_size(msg, payload_len);
1033 if (ret < 0)
1034 goto out;
1035
1036 rm = rds_message_alloc(ret, GFP_KERNEL);
1037 if (!rm) {
1038 ret = -ENOMEM;
Andy Grover5c115592009-02-24 15:30:27 +00001039 goto out;
1040 }
1041
Andy Grover372cd7d2010-02-03 19:40:32 -08001042 /* Attach data to the rm */
1043 if (payload_len) {
1044 rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
Andy Groverd139ff02010-10-28 15:40:59 +00001045 if (!rm->data.op_sg) {
1046 ret = -ENOMEM;
1047 goto out;
1048 }
Al Viroc0371da2014-11-24 10:42:55 -05001049 ret = rds_message_copy_from_user(rm, &msg->msg_iter);
Andy Grover372cd7d2010-02-03 19:40:32 -08001050 if (ret)
1051 goto out;
1052 }
1053 rm->data.op_active = 1;
Andy Groverfc445082010-01-12 12:56:06 -08001054
Andy Grover5c115592009-02-24 15:30:27 +00001055 rm->m_daddr = daddr;
1056
Andy Grover5c115592009-02-24 15:30:27 +00001057 /* rds_conn_create has a spinlock that runs with IRQ off.
1058 * Caching the conn in the socket helps a lot. */
1059 if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
1060 conn = rs->rs_conn;
1061 else {
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -04001062 conn = rds_conn_create_outgoing(sock_net(sock->sk),
1063 rs->rs_bound_addr, daddr,
Andy Grover5c115592009-02-24 15:30:27 +00001064 rs->rs_transport,
1065 sock->sk->sk_allocation);
1066 if (IS_ERR(conn)) {
1067 ret = PTR_ERR(conn);
1068 goto out;
1069 }
1070 rs->rs_conn = conn;
1071 }
1072
Andy Grover49f69692009-04-09 14:09:41 +00001073 /* Parse any control messages the user may have included. */
1074 ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
1075 if (ret)
1076 goto out;
1077
Andy Grover2c3a5f92010-03-01 16:10:40 -08001078 if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
Manuel Zerpiescb0a6052011-06-16 02:09:57 +00001079 printk_ratelimited(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
Andy Groverf8b3aaf2010-03-01 14:11:53 -08001080 &rm->rdma, conn->c_trans->xmit_rdma);
Andy Grover15133f62010-01-12 14:33:38 -08001081 ret = -EOPNOTSUPP;
1082 goto out;
1083 }
1084
1085 if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
Manuel Zerpiescb0a6052011-06-16 02:09:57 +00001086 printk_ratelimited(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
Andy Grover15133f62010-01-12 14:33:38 -08001087 &rm->atomic, conn->c_trans->xmit_atomic);
Andy Grover5c115592009-02-24 15:30:27 +00001088 ret = -EOPNOTSUPP;
1089 goto out;
1090 }
1091
Zach Brownf3c68082010-05-24 13:14:36 -07001092 rds_conn_connect_if_down(conn);
Andy Grover5c115592009-02-24 15:30:27 +00001093
1094 ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
Andy Groverb98ba522010-03-11 13:50:04 +00001095 if (ret) {
1096 rs->rs_seen_congestion = 1;
Andy Grover5c115592009-02-24 15:30:27 +00001097 goto out;
Andy Groverb98ba522010-03-11 13:50:04 +00001098 }
Andy Grover5c115592009-02-24 15:30:27 +00001099
Sowmini Varadhan780a6d92016-06-13 09:44:33 -07001100 cpath = &conn->c_path[0];
1101
1102 while (!rds_send_queue_rm(rs, conn, cpath, rm, rs->rs_bound_port,
Andy Grover5c115592009-02-24 15:30:27 +00001103 dport, &queued)) {
1104 rds_stats_inc(s_send_queue_full);
Mukesh Kacker06e89412015-08-22 15:45:34 -07001105
Andy Grover5c115592009-02-24 15:30:27 +00001106 if (nonblock) {
1107 ret = -EAGAIN;
1108 goto out;
1109 }
1110
Eric Dumazetaa395142010-04-20 13:03:51 +00001111 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Sowmini Varadhan780a6d92016-06-13 09:44:33 -07001112 rds_send_queue_rm(rs, conn, cpath, rm,
Andy Grover5c115592009-02-24 15:30:27 +00001113 rs->rs_bound_port,
1114 dport,
1115 &queued),
1116 timeo);
1117 rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
1118 if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
1119 continue;
1120
1121 ret = timeo;
1122 if (ret == 0)
1123 ret = -ETIMEDOUT;
1124 goto out;
1125 }
1126
1127 /*
1128 * By now we've committed to the send. We reuse rds_send_worker()
1129 * to retry sends in the rds thread if the transport asks us to.
1130 */
1131 rds_stats_inc(s_send_queued);
1132
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -07001133 ret = rds_send_xmit(cpath);
Santosh Shilimkardb6526d2015-09-11 15:44:29 -07001134 if (ret == -ENOMEM || ret == -EAGAIN)
Sowmini Varadhan1f9ecd72016-06-13 09:44:34 -07001135 queue_delayed_work(rds_wq, &cpath->cp_send_w, 1);
Andy Grover5c115592009-02-24 15:30:27 +00001136
1137 rds_message_put(rm);
1138 return payload_len;
1139
1140out:
1141 /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
1142 * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
1143 * or in any other way, we need to destroy the MR again */
1144 if (allocated_mr)
1145 rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
1146
1147 if (rm)
1148 rds_message_put(rm);
1149 return ret;
1150}
1151
1152/*
1153 * Reply to a ping packet.
1154 */
1155int
1156rds_send_pong(struct rds_connection *conn, __be16 dport)
1157{
1158 struct rds_message *rm;
1159 unsigned long flags;
1160 int ret = 0;
1161
1162 rm = rds_message_alloc(0, GFP_ATOMIC);
Andy Grover8690bfa2010-01-12 11:56:44 -08001163 if (!rm) {
Andy Grover5c115592009-02-24 15:30:27 +00001164 ret = -ENOMEM;
1165 goto out;
1166 }
1167
1168 rm->m_daddr = conn->c_faddr;
Andy Groveracfcd4d2010-03-31 18:56:25 -07001169 rm->data.op_active = 1;
Andy Grover5c115592009-02-24 15:30:27 +00001170
Zach Brownf3c68082010-05-24 13:14:36 -07001171 rds_conn_connect_if_down(conn);
Andy Grover5c115592009-02-24 15:30:27 +00001172
1173 ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
1174 if (ret)
1175 goto out;
1176
1177 spin_lock_irqsave(&conn->c_lock, flags);
1178 list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
1179 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
1180 rds_message_addref(rm);
1181 rm->m_inc.i_conn = conn;
1182
1183 rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
1184 conn->c_next_tx_seq);
1185 conn->c_next_tx_seq++;
1186 spin_unlock_irqrestore(&conn->c_lock, flags);
1187
1188 rds_stats_inc(s_send_queued);
1189 rds_stats_inc(s_send_pong);
1190
santosh.shilimkar@oracle.com7b4b0002015-10-16 22:13:21 -04001191 /* schedule the send work on rds_wq */
1192 queue_delayed_work(rds_wq, &conn->c_send_w, 1);
Andy Groveracfcd4d2010-03-31 18:56:25 -07001193
Andy Grover5c115592009-02-24 15:30:27 +00001194 rds_message_put(rm);
1195 return 0;
1196
1197out:
1198 if (rm)
1199 rds_message_put(rm);
1200 return ret;
1201}