blob: 8a0647af5d95a4557644a190c767fce60a3c6a2b [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Andy Grover5c115592009-02-24 15:30:27 +000035#include <net/sock.h>
36#include <linux/in.h>
37#include <linux/list.h>
38
39#include "rds.h"
Andy Grover5c115592009-02-24 15:30:27 +000040
41/* When transmitting messages in rds_send_xmit, we need to emerge from
42 * time to time and briefly release the CPU. Otherwise the softlock watchdog
43 * will kick our shin.
44 * Also, it seems fairer to not let one busy connection stall all the
45 * others.
46 *
47 * send_batch_count is the number of times we'll loop in send_xmit. Setting
48 * it to 0 will restore the old behavior (where we looped until we had
49 * drained the queue).
50 */
51static int send_batch_count = 64;
52module_param(send_batch_count, int, 0444);
53MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
54
55/*
56 * Reset the send state. Caller must hold c_send_lock when calling here.
57 */
58void rds_send_reset(struct rds_connection *conn)
59{
60 struct rds_message *rm, *tmp;
61 unsigned long flags;
62
63 if (conn->c_xmit_rm) {
64 /* Tell the user the RDMA op is no longer mapped by the
65 * transport. This isn't entirely true (it's flushed out
66 * independently) but as the connection is down, there's
67 * no ongoing RDMA to/from that memory */
68 rds_message_unmapped(conn->c_xmit_rm);
69 rds_message_put(conn->c_xmit_rm);
70 conn->c_xmit_rm = NULL;
71 }
72 conn->c_xmit_sg = 0;
73 conn->c_xmit_hdr_off = 0;
74 conn->c_xmit_data_off = 0;
Andy Grover15133f62010-01-12 14:33:38 -080075 conn->c_xmit_atomic_sent = 0;
Andy Grover5b2366b2010-02-03 19:36:44 -080076 conn->c_xmit_rdma_sent = 0;
77 conn->c_xmit_data_sent = 0;
Andy Grover5c115592009-02-24 15:30:27 +000078
79 conn->c_map_queued = 0;
80
81 conn->c_unacked_packets = rds_sysctl_max_unacked_packets;
82 conn->c_unacked_bytes = rds_sysctl_max_unacked_bytes;
83
84 /* Mark messages as retransmissions, and move them to the send q */
85 spin_lock_irqsave(&conn->c_lock, flags);
86 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
87 set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
88 set_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags);
89 }
90 list_splice_init(&conn->c_retrans, &conn->c_send_queue);
91 spin_unlock_irqrestore(&conn->c_lock, flags);
92}
93
94/*
95 * We're making the concious trade-off here to only send one message
96 * down the connection at a time.
97 * Pro:
98 * - tx queueing is a simple fifo list
99 * - reassembly is optional and easily done by transports per conn
100 * - no per flow rx lookup at all, straight to the socket
101 * - less per-frag memory and wire overhead
102 * Con:
103 * - queued acks can be delayed behind large messages
104 * Depends:
105 * - small message latency is higher behind queued large messages
106 * - large message latency isn't starved by intervening small sends
107 */
108int rds_send_xmit(struct rds_connection *conn)
109{
110 struct rds_message *rm;
111 unsigned long flags;
112 unsigned int tmp;
113 unsigned int send_quota = send_batch_count;
114 struct scatterlist *sg;
115 int ret = 0;
116 int was_empty = 0;
117 LIST_HEAD(to_be_dropped);
118
119 /*
120 * sendmsg calls here after having queued its message on the send
121 * queue. We only have one task feeding the connection at a time. If
122 * another thread is already feeding the queue then we back off. This
123 * avoids blocking the caller and trading per-connection data between
124 * caches per message.
125 *
126 * The sem holder will issue a retry if they notice that someone queued
127 * a message after they stopped walking the send queue but before they
128 * dropped the sem.
129 */
130 if (!mutex_trylock(&conn->c_send_lock)) {
131 rds_stats_inc(s_send_sem_contention);
132 ret = -ENOMEM;
133 goto out;
134 }
135
136 if (conn->c_trans->xmit_prepare)
137 conn->c_trans->xmit_prepare(conn);
138
139 /*
140 * spin trying to push headers and data down the connection until
Andy Grover5b2366b2010-02-03 19:36:44 -0800141 * the connection doesn't make forward progress.
Andy Grover5c115592009-02-24 15:30:27 +0000142 */
143 while (--send_quota) {
Andy Grover5c115592009-02-24 15:30:27 +0000144
Andy Grover5c115592009-02-24 15:30:27 +0000145 rm = conn->c_xmit_rm;
Andy Grover5c115592009-02-24 15:30:27 +0000146
Andy Grover5b2366b2010-02-03 19:36:44 -0800147 /*
148 * If between sending messages, we can send a pending congestion
149 * map update.
Andy Grover5c115592009-02-24 15:30:27 +0000150 */
Andy Grover8690bfa2010-01-12 11:56:44 -0800151 if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
Andy Grover77dd5502010-03-22 15:22:04 -0700152 rm = rds_cong_update_alloc(conn);
153 if (IS_ERR(rm)) {
154 ret = PTR_ERR(rm);
155 break;
Andy Grover5b2366b2010-02-03 19:36:44 -0800156 }
Andy Grover77dd5502010-03-22 15:22:04 -0700157 rm->data.op_active = 1;
158
159 conn->c_xmit_rm = rm;
Andy Grover5c115592009-02-24 15:30:27 +0000160 }
161
162 /*
Andy Grover5b2366b2010-02-03 19:36:44 -0800163 * If not already working on one, grab the next message.
Andy Grover5c115592009-02-24 15:30:27 +0000164 *
165 * c_xmit_rm holds a ref while we're sending this message down
166 * the connction. We can use this ref while holding the
167 * send_sem.. rds_send_reset() is serialized with it.
168 */
Andy Grover8690bfa2010-01-12 11:56:44 -0800169 if (!rm) {
Andy Grover5c115592009-02-24 15:30:27 +0000170 unsigned int len;
171
172 spin_lock_irqsave(&conn->c_lock, flags);
173
174 if (!list_empty(&conn->c_send_queue)) {
175 rm = list_entry(conn->c_send_queue.next,
176 struct rds_message,
177 m_conn_item);
178 rds_message_addref(rm);
179
180 /*
181 * Move the message from the send queue to the retransmit
182 * list right away.
183 */
184 list_move_tail(&rm->m_conn_item, &conn->c_retrans);
185 }
186
187 spin_unlock_irqrestore(&conn->c_lock, flags);
188
Andy Grover8690bfa2010-01-12 11:56:44 -0800189 if (!rm) {
Andy Grover5c115592009-02-24 15:30:27 +0000190 was_empty = 1;
191 break;
192 }
193
194 /* Unfortunately, the way Infiniband deals with
195 * RDMA to a bad MR key is by moving the entire
196 * queue pair to error state. We cold possibly
197 * recover from that, but right now we drop the
198 * connection.
199 * Therefore, we never retransmit messages with RDMA ops.
200 */
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800201 if (rm->rdma.op_active &&
Joe Perchesf64f9e72009-11-29 16:55:45 -0800202 test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) {
Andy Grover5c115592009-02-24 15:30:27 +0000203 spin_lock_irqsave(&conn->c_lock, flags);
204 if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags))
205 list_move(&rm->m_conn_item, &to_be_dropped);
206 spin_unlock_irqrestore(&conn->c_lock, flags);
207 rds_message_put(rm);
208 continue;
209 }
210
211 /* Require an ACK every once in a while */
212 len = ntohl(rm->m_inc.i_hdr.h_len);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800213 if (conn->c_unacked_packets == 0 ||
214 conn->c_unacked_bytes < len) {
Andy Grover5c115592009-02-24 15:30:27 +0000215 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
216
217 conn->c_unacked_packets = rds_sysctl_max_unacked_packets;
218 conn->c_unacked_bytes = rds_sysctl_max_unacked_bytes;
219 rds_stats_inc(s_send_ack_required);
220 } else {
221 conn->c_unacked_bytes -= len;
222 conn->c_unacked_packets--;
223 }
224
225 conn->c_xmit_rm = rm;
226 }
227
Andy Grover2c3a5f92010-03-01 16:10:40 -0800228 /* The transport either sends the whole rdma or none of it */
229 if (rm->rdma.op_active && !conn->c_xmit_rdma_sent) {
Andy Groverff3d7d32010-03-01 14:03:09 -0800230 rm->m_final_op = &rm->rdma;
Andy Grover2c3a5f92010-03-01 16:10:40 -0800231 ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
232 if (ret)
233 break;
234 conn->c_xmit_rdma_sent = 1;
235
236 /* The transport owns the mapped memory for now.
237 * You can't unmap it while it's on the send queue */
238 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
239 }
240
Andy Grover15133f62010-01-12 14:33:38 -0800241 if (rm->atomic.op_active && !conn->c_xmit_atomic_sent) {
Andy Groverff3d7d32010-03-01 14:03:09 -0800242 rm->m_final_op = &rm->atomic;
243 ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
Andy Grover15133f62010-01-12 14:33:38 -0800244 if (ret)
245 break;
246 conn->c_xmit_atomic_sent = 1;
Andy Groverff3d7d32010-03-01 14:03:09 -0800247
Andy Grover15133f62010-01-12 14:33:38 -0800248 /* The transport owns the mapped memory for now.
249 * You can't unmap it while it's on the send queue */
250 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
251 }
252
Andy Grover2c3a5f92010-03-01 16:10:40 -0800253 /*
254 * A number of cases require an RDS header to be sent
255 * even if there is no data.
256 * We permit 0-byte sends; rds-ping depends on this.
257 * However, if there are exclusively attached silent ops,
258 * we skip the hdr/data send, to enable silent operation.
259 */
260 if (rm->data.op_nents == 0) {
261 int ops_present;
262 int all_ops_are_silent = 1;
Andy Grover241eef32010-01-19 21:25:26 -0800263
Andy Grover2c3a5f92010-03-01 16:10:40 -0800264 ops_present = (rm->atomic.op_active || rm->rdma.op_active);
265 if (rm->atomic.op_active && !rm->atomic.op_silent)
266 all_ops_are_silent = 0;
267 if (rm->rdma.op_active && !rm->rdma.op_silent)
268 all_ops_are_silent = 0;
Andy Grover241eef32010-01-19 21:25:26 -0800269
Andy Grover2c3a5f92010-03-01 16:10:40 -0800270 if (ops_present && all_ops_are_silent
271 && !rm->m_rdma_cookie)
272 rm->data.op_active = 0;
Andy Grover5c115592009-02-24 15:30:27 +0000273 }
274
Andy Grover5b2366b2010-02-03 19:36:44 -0800275 if (rm->data.op_active && !conn->c_xmit_data_sent) {
Andy Groverff3d7d32010-03-01 14:03:09 -0800276 rm->m_final_op = &rm->data;
Andy Grover5c115592009-02-24 15:30:27 +0000277 ret = conn->c_trans->xmit(conn, rm,
278 conn->c_xmit_hdr_off,
279 conn->c_xmit_sg,
280 conn->c_xmit_data_off);
281 if (ret <= 0)
282 break;
283
284 if (conn->c_xmit_hdr_off < sizeof(struct rds_header)) {
285 tmp = min_t(int, ret,
286 sizeof(struct rds_header) -
287 conn->c_xmit_hdr_off);
288 conn->c_xmit_hdr_off += tmp;
289 ret -= tmp;
290 }
291
Andy Grover6c7cc6e2010-01-27 18:04:18 -0800292 sg = &rm->data.op_sg[conn->c_xmit_sg];
Andy Grover5c115592009-02-24 15:30:27 +0000293 while (ret) {
294 tmp = min_t(int, ret, sg->length -
295 conn->c_xmit_data_off);
296 conn->c_xmit_data_off += tmp;
297 ret -= tmp;
298 if (conn->c_xmit_data_off == sg->length) {
299 conn->c_xmit_data_off = 0;
300 sg++;
301 conn->c_xmit_sg++;
302 BUG_ON(ret != 0 &&
Andy Grover6c7cc6e2010-01-27 18:04:18 -0800303 conn->c_xmit_sg == rm->data.op_nents);
Andy Grover5c115592009-02-24 15:30:27 +0000304 }
305 }
Andy Grover5b2366b2010-02-03 19:36:44 -0800306
307 if (conn->c_xmit_hdr_off == sizeof(struct rds_header) &&
308 (conn->c_xmit_sg == rm->data.op_nents))
309 conn->c_xmit_data_sent = 1;
310 }
311
312 /*
313 * A rm will only take multiple times through this loop
314 * if there is a data op. Thus, if the data is sent (or there was
315 * none), then we're done with the rm.
316 */
317 if (!rm->data.op_active || conn->c_xmit_data_sent) {
318 conn->c_xmit_rm = NULL;
319 conn->c_xmit_sg = 0;
320 conn->c_xmit_hdr_off = 0;
321 conn->c_xmit_data_off = 0;
322 conn->c_xmit_rdma_sent = 0;
323 conn->c_xmit_atomic_sent = 0;
324 conn->c_xmit_data_sent = 0;
325
326 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +0000327 }
328 }
329
330 /* Nuke any messages we decided not to retransmit. */
331 if (!list_empty(&to_be_dropped))
332 rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
333
334 if (conn->c_trans->xmit_complete)
335 conn->c_trans->xmit_complete(conn);
336
337 /*
338 * We might be racing with another sender who queued a message but
339 * backed off on noticing that we held the c_send_lock. If we check
340 * for queued messages after dropping the sem then either we'll
341 * see the queued message or the queuer will get the sem. If we
342 * notice the queued message then we trigger an immediate retry.
343 *
344 * We need to be careful only to do this when we stopped processing
345 * the send queue because it was empty. It's the only way we
346 * stop processing the loop when the transport hasn't taken
347 * responsibility for forward progress.
348 */
349 mutex_unlock(&conn->c_send_lock);
350
Andy Grover5b2366b2010-02-03 19:36:44 -0800351 if (send_quota == 0 && !was_empty) {
Andy Grover5c115592009-02-24 15:30:27 +0000352 /* We exhausted the send quota, but there's work left to
353 * do. Return and (re-)schedule the send worker.
354 */
355 ret = -EAGAIN;
356 }
357
358 if (ret == 0 && was_empty) {
359 /* A simple bit test would be way faster than taking the
360 * spin lock */
361 spin_lock_irqsave(&conn->c_lock, flags);
362 if (!list_empty(&conn->c_send_queue)) {
363 rds_stats_inc(s_send_sem_queue_raced);
364 ret = -EAGAIN;
365 }
366 spin_unlock_irqrestore(&conn->c_lock, flags);
367 }
368out:
369 return ret;
370}
371
372static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
373{
374 u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
375
376 assert_spin_locked(&rs->rs_lock);
377
378 BUG_ON(rs->rs_snd_bytes < len);
379 rs->rs_snd_bytes -= len;
380
381 if (rs->rs_snd_bytes == 0)
382 rds_stats_inc(s_send_queue_empty);
383}
384
385static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
386 is_acked_func is_acked)
387{
388 if (is_acked)
389 return is_acked(rm, ack);
390 return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
391}
392
393/*
394 * Returns true if there are no messages on the send and retransmit queues
395 * which have a sequence number greater than or equal to the given sequence
396 * number.
397 */
398int rds_send_acked_before(struct rds_connection *conn, u64 seq)
399{
400 struct rds_message *rm, *tmp;
401 int ret = 1;
402
403 spin_lock(&conn->c_lock);
404
405 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
406 if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
407 ret = 0;
408 break;
409 }
410
411 list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
412 if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
413 ret = 0;
414 break;
415 }
416
417 spin_unlock(&conn->c_lock);
418
419 return ret;
420}
421
422/*
423 * This is pretty similar to what happens below in the ACK
424 * handling code - except that we call here as soon as we get
425 * the IB send completion on the RDMA op and the accompanying
426 * message.
427 */
428void rds_rdma_send_complete(struct rds_message *rm, int status)
429{
430 struct rds_sock *rs = NULL;
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800431 struct rm_rdma_op *ro;
Andy Grover5c115592009-02-24 15:30:27 +0000432 struct rds_notifier *notifier;
Andy Grover9de08642010-03-29 16:50:54 -0700433 unsigned long flags;
Andy Grover5c115592009-02-24 15:30:27 +0000434
Andy Grover9de08642010-03-29 16:50:54 -0700435 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000436
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800437 ro = &rm->rdma;
Joe Perchesf64f9e72009-11-29 16:55:45 -0800438 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800439 ro->op_active && ro->op_notify && ro->op_notifier) {
440 notifier = ro->op_notifier;
Andy Grover5c115592009-02-24 15:30:27 +0000441 rs = rm->m_rs;
442 sock_hold(rds_rs_to_sk(rs));
443
444 notifier->n_status = status;
445 spin_lock(&rs->rs_lock);
446 list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
447 spin_unlock(&rs->rs_lock);
448
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800449 ro->op_notifier = NULL;
Andy Grover5c115592009-02-24 15:30:27 +0000450 }
451
Andy Grover9de08642010-03-29 16:50:54 -0700452 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000453
454 if (rs) {
455 rds_wake_sk_sleep(rs);
456 sock_put(rds_rs_to_sk(rs));
457 }
458}
Andy Grover616b7572009-08-21 12:28:32 +0000459EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
Andy Grover5c115592009-02-24 15:30:27 +0000460
461/*
Andy Grover15133f62010-01-12 14:33:38 -0800462 * Just like above, except looks at atomic op
463 */
464void rds_atomic_send_complete(struct rds_message *rm, int status)
465{
466 struct rds_sock *rs = NULL;
467 struct rm_atomic_op *ao;
468 struct rds_notifier *notifier;
469
470 spin_lock(&rm->m_rs_lock);
471
472 ao = &rm->atomic;
473 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
474 && ao->op_active && ao->op_notify && ao->op_notifier) {
475 notifier = ao->op_notifier;
476 rs = rm->m_rs;
477 sock_hold(rds_rs_to_sk(rs));
478
479 notifier->n_status = status;
480 spin_lock(&rs->rs_lock);
481 list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
482 spin_unlock(&rs->rs_lock);
483
484 ao->op_notifier = NULL;
485 }
486
487 spin_unlock(&rm->m_rs_lock);
488
489 if (rs) {
490 rds_wake_sk_sleep(rs);
491 sock_put(rds_rs_to_sk(rs));
492 }
493}
494EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
495
496/*
Andy Grover5c115592009-02-24 15:30:27 +0000497 * This is the same as rds_rdma_send_complete except we
498 * don't do any locking - we have all the ingredients (message,
499 * socket, socket lock) and can just move the notifier.
500 */
501static inline void
Andy Grover940786e2010-02-19 18:04:58 -0800502__rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
Andy Grover5c115592009-02-24 15:30:27 +0000503{
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800504 struct rm_rdma_op *ro;
Andy Grover940786e2010-02-19 18:04:58 -0800505 struct rm_atomic_op *ao;
Andy Grover5c115592009-02-24 15:30:27 +0000506
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800507 ro = &rm->rdma;
508 if (ro->op_active && ro->op_notify && ro->op_notifier) {
509 ro->op_notifier->n_status = status;
510 list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
511 ro->op_notifier = NULL;
Andy Grover5c115592009-02-24 15:30:27 +0000512 }
513
Andy Grover940786e2010-02-19 18:04:58 -0800514 ao = &rm->atomic;
515 if (ao->op_active && ao->op_notify && ao->op_notifier) {
516 ao->op_notifier->n_status = status;
517 list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
518 ao->op_notifier = NULL;
519 }
520
Andy Grover5c115592009-02-24 15:30:27 +0000521 /* No need to wake the app - caller does this */
522}
523
524/*
525 * This is called from the IB send completion when we detect
526 * a RDMA operation that failed with remote access error.
527 * So speed is not an issue here.
528 */
529struct rds_message *rds_send_get_message(struct rds_connection *conn,
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800530 struct rm_rdma_op *op)
Andy Grover5c115592009-02-24 15:30:27 +0000531{
532 struct rds_message *rm, *tmp, *found = NULL;
533 unsigned long flags;
534
535 spin_lock_irqsave(&conn->c_lock, flags);
536
537 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800538 if (&rm->rdma == op) {
Andy Grover5c115592009-02-24 15:30:27 +0000539 atomic_inc(&rm->m_refcount);
540 found = rm;
541 goto out;
542 }
543 }
544
545 list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800546 if (&rm->rdma == op) {
Andy Grover5c115592009-02-24 15:30:27 +0000547 atomic_inc(&rm->m_refcount);
548 found = rm;
549 break;
550 }
551 }
552
553out:
554 spin_unlock_irqrestore(&conn->c_lock, flags);
555
556 return found;
557}
Andy Grover616b7572009-08-21 12:28:32 +0000558EXPORT_SYMBOL_GPL(rds_send_get_message);
Andy Grover5c115592009-02-24 15:30:27 +0000559
560/*
561 * This removes messages from the socket's list if they're on it. The list
562 * argument must be private to the caller, we must be able to modify it
563 * without locks. The messages must have a reference held for their
564 * position on the list. This function will drop that reference after
565 * removing the messages from the 'messages' list regardless of if it found
566 * the messages on the socket list or not.
567 */
568void rds_send_remove_from_sock(struct list_head *messages, int status)
569{
Andy Grover561c7df2010-03-11 13:50:06 +0000570 unsigned long flags;
Andy Grover5c115592009-02-24 15:30:27 +0000571 struct rds_sock *rs = NULL;
572 struct rds_message *rm;
573
Andy Grover5c115592009-02-24 15:30:27 +0000574 while (!list_empty(messages)) {
Andy Grover561c7df2010-03-11 13:50:06 +0000575 int was_on_sock = 0;
576
Andy Grover5c115592009-02-24 15:30:27 +0000577 rm = list_entry(messages->next, struct rds_message,
578 m_conn_item);
579 list_del_init(&rm->m_conn_item);
580
581 /*
582 * If we see this flag cleared then we're *sure* that someone
583 * else beat us to removing it from the sock. If we race
584 * with their flag update we'll get the lock and then really
585 * see that the flag has been cleared.
586 *
587 * The message spinlock makes sure nobody clears rm->m_rs
588 * while we're messing with it. It does not prevent the
589 * message from being removed from the socket, though.
590 */
Andy Grover561c7df2010-03-11 13:50:06 +0000591 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000592 if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
593 goto unlock_and_drop;
594
595 if (rs != rm->m_rs) {
596 if (rs) {
Andy Grover5c115592009-02-24 15:30:27 +0000597 rds_wake_sk_sleep(rs);
598 sock_put(rds_rs_to_sk(rs));
599 }
600 rs = rm->m_rs;
Andy Grover5c115592009-02-24 15:30:27 +0000601 sock_hold(rds_rs_to_sk(rs));
602 }
Tina Yang048c15e2010-03-11 13:50:00 +0000603 spin_lock(&rs->rs_lock);
Andy Grover5c115592009-02-24 15:30:27 +0000604
605 if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800606 struct rm_rdma_op *ro = &rm->rdma;
Andy Grover5c115592009-02-24 15:30:27 +0000607 struct rds_notifier *notifier;
608
609 list_del_init(&rm->m_sock_item);
610 rds_send_sndbuf_remove(rs, rm);
611
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800612 if (ro->op_active && ro->op_notifier &&
613 (ro->op_notify || (ro->op_recverr && status))) {
614 notifier = ro->op_notifier;
Andy Grover5c115592009-02-24 15:30:27 +0000615 list_add_tail(&notifier->n_list,
616 &rs->rs_notify_queue);
617 if (!notifier->n_status)
618 notifier->n_status = status;
Andy Groverf8b3aaf2010-03-01 14:11:53 -0800619 rm->rdma.op_notifier = NULL;
Andy Grover5c115592009-02-24 15:30:27 +0000620 }
Andy Grover561c7df2010-03-11 13:50:06 +0000621 was_on_sock = 1;
Andy Grover5c115592009-02-24 15:30:27 +0000622 rm->m_rs = NULL;
623 }
Tina Yang048c15e2010-03-11 13:50:00 +0000624 spin_unlock(&rs->rs_lock);
Andy Grover5c115592009-02-24 15:30:27 +0000625
626unlock_and_drop:
Andy Grover561c7df2010-03-11 13:50:06 +0000627 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000628 rds_message_put(rm);
Andy Grover561c7df2010-03-11 13:50:06 +0000629 if (was_on_sock)
630 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +0000631 }
632
633 if (rs) {
Andy Grover5c115592009-02-24 15:30:27 +0000634 rds_wake_sk_sleep(rs);
635 sock_put(rds_rs_to_sk(rs));
636 }
Andy Grover5c115592009-02-24 15:30:27 +0000637}
638
639/*
640 * Transports call here when they've determined that the receiver queued
641 * messages up to, and including, the given sequence number. Messages are
642 * moved to the retrans queue when rds_send_xmit picks them off the send
643 * queue. This means that in the TCP case, the message may not have been
644 * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
645 * checks the RDS_MSG_HAS_ACK_SEQ bit.
646 *
647 * XXX It's not clear to me how this is safely serialized with socket
648 * destruction. Maybe it should bail if it sees SOCK_DEAD.
649 */
650void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
651 is_acked_func is_acked)
652{
653 struct rds_message *rm, *tmp;
654 unsigned long flags;
655 LIST_HEAD(list);
656
657 spin_lock_irqsave(&conn->c_lock, flags);
658
659 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
660 if (!rds_send_is_acked(rm, ack, is_acked))
661 break;
662
663 list_move(&rm->m_conn_item, &list);
664 clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
665 }
666
667 /* order flag updates with spin locks */
668 if (!list_empty(&list))
669 smp_mb__after_clear_bit();
670
671 spin_unlock_irqrestore(&conn->c_lock, flags);
672
673 /* now remove the messages from the sock list as needed */
674 rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
675}
Andy Grover616b7572009-08-21 12:28:32 +0000676EXPORT_SYMBOL_GPL(rds_send_drop_acked);
Andy Grover5c115592009-02-24 15:30:27 +0000677
678void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
679{
680 struct rds_message *rm, *tmp;
681 struct rds_connection *conn;
Andy Grover7c82eaf2010-02-19 18:01:41 -0800682 unsigned long flags;
Andy Grover5c115592009-02-24 15:30:27 +0000683 LIST_HEAD(list);
Andy Grover5c115592009-02-24 15:30:27 +0000684
685 /* get all the messages we're dropping under the rs lock */
686 spin_lock_irqsave(&rs->rs_lock, flags);
687
688 list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
689 if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
690 dest->sin_port != rm->m_inc.i_hdr.h_dport))
691 continue;
692
Andy Grover5c115592009-02-24 15:30:27 +0000693 list_move(&rm->m_sock_item, &list);
694 rds_send_sndbuf_remove(rs, rm);
695 clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
Andy Grover5c115592009-02-24 15:30:27 +0000696 }
697
698 /* order flag updates with the rs lock */
Andy Grover7c82eaf2010-02-19 18:01:41 -0800699 smp_mb__after_clear_bit();
Andy Grover5c115592009-02-24 15:30:27 +0000700
701 spin_unlock_irqrestore(&rs->rs_lock, flags);
702
Andy Grover7c82eaf2010-02-19 18:01:41 -0800703 if (list_empty(&list))
704 return;
Andy Grover5c115592009-02-24 15:30:27 +0000705
Andy Grover7c82eaf2010-02-19 18:01:41 -0800706 /* Remove the messages from the conn */
Andy Grover5c115592009-02-24 15:30:27 +0000707 list_for_each_entry(rm, &list, m_sock_item) {
Andy Grover7c82eaf2010-02-19 18:01:41 -0800708
709 conn = rm->m_inc.i_conn;
Andy Grover7c82eaf2010-02-19 18:01:41 -0800710
Andy Grover9de08642010-03-29 16:50:54 -0700711 spin_lock_irqsave(&conn->c_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800712 /*
713 * Maybe someone else beat us to removing rm from the conn.
714 * If we race with their flag update we'll get the lock and
715 * then really see that the flag has been cleared.
716 */
717 if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
718 spin_unlock_irqrestore(&conn->c_lock, flags);
719 continue;
720 }
Andy Grover9de08642010-03-29 16:50:54 -0700721 list_del_init(&rm->m_conn_item);
722 spin_unlock_irqrestore(&conn->c_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800723
724 /*
725 * Couldn't grab m_rs_lock in top loop (lock ordering),
726 * but we can now.
727 */
Andy Grover9de08642010-03-29 16:50:54 -0700728 spin_lock_irqsave(&rm->m_rs_lock, flags);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800729
Tina Yang550a8002010-03-11 13:50:03 +0000730 spin_lock(&rs->rs_lock);
Andy Grover940786e2010-02-19 18:04:58 -0800731 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
Tina Yang550a8002010-03-11 13:50:03 +0000732 spin_unlock(&rs->rs_lock);
Andy Grover7c82eaf2010-02-19 18:01:41 -0800733
Andy Grover5c115592009-02-24 15:30:27 +0000734 rm->m_rs = NULL;
Andy Grover9de08642010-03-29 16:50:54 -0700735 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
Andy Grover5c115592009-02-24 15:30:27 +0000736
Andy Grover7c82eaf2010-02-19 18:01:41 -0800737 rds_message_put(rm);
Andy Grover5c115592009-02-24 15:30:27 +0000738 }
739
Andy Grover7c82eaf2010-02-19 18:01:41 -0800740 rds_wake_sk_sleep(rs);
Tina Yang550a8002010-03-11 13:50:03 +0000741
Andy Grover5c115592009-02-24 15:30:27 +0000742 while (!list_empty(&list)) {
743 rm = list_entry(list.next, struct rds_message, m_sock_item);
744 list_del_init(&rm->m_sock_item);
745
746 rds_message_wait(rm);
747 rds_message_put(rm);
748 }
749}
750
751/*
752 * we only want this to fire once so we use the callers 'queued'. It's
753 * possible that another thread can race with us and remove the
754 * message from the flow with RDS_CANCEL_SENT_TO.
755 */
756static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
757 struct rds_message *rm, __be16 sport,
758 __be16 dport, int *queued)
759{
760 unsigned long flags;
761 u32 len;
762
763 if (*queued)
764 goto out;
765
766 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
767
768 /* this is the only place which holds both the socket's rs_lock
769 * and the connection's c_lock */
770 spin_lock_irqsave(&rs->rs_lock, flags);
771
772 /*
773 * If there is a little space in sndbuf, we don't queue anything,
774 * and userspace gets -EAGAIN. But poll() indicates there's send
775 * room. This can lead to bad behavior (spinning) if snd_bytes isn't
776 * freed up by incoming acks. So we check the *old* value of
777 * rs_snd_bytes here to allow the last msg to exceed the buffer,
778 * and poll() now knows no more data can be sent.
779 */
780 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
781 rs->rs_snd_bytes += len;
782
783 /* let recv side know we are close to send space exhaustion.
784 * This is probably not the optimal way to do it, as this
785 * means we set the flag on *all* messages as soon as our
786 * throughput hits a certain threshold.
787 */
788 if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
789 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
790
791 list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
792 set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
793 rds_message_addref(rm);
794 rm->m_rs = rs;
795
796 /* The code ordering is a little weird, but we're
797 trying to minimize the time we hold c_lock */
798 rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
799 rm->m_inc.i_conn = conn;
800 rds_message_addref(rm);
801
802 spin_lock(&conn->c_lock);
803 rm->m_inc.i_hdr.h_sequence = cpu_to_be64(conn->c_next_tx_seq++);
804 list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
805 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
806 spin_unlock(&conn->c_lock);
807
808 rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
809 rm, len, rs, rs->rs_snd_bytes,
810 (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
811
812 *queued = 1;
813 }
814
815 spin_unlock_irqrestore(&rs->rs_lock, flags);
816out:
817 return *queued;
818}
819
Andy Groverfc445082010-01-12 12:56:06 -0800820/*
821 * rds_message is getting to be quite complicated, and we'd like to allocate
822 * it all in one go. This figures out how big it needs to be up front.
823 */
824static int rds_rm_size(struct msghdr *msg, int data_len)
825{
Andy Groverff87e972010-01-12 14:13:15 -0800826 struct cmsghdr *cmsg;
Andy Groverfc445082010-01-12 12:56:06 -0800827 int size = 0;
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700828 int cmsg_groups = 0;
Andy Groverff87e972010-01-12 14:13:15 -0800829 int retval;
Andy Groverfc445082010-01-12 12:56:06 -0800830
Andy Groverff87e972010-01-12 14:13:15 -0800831 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
832 if (!CMSG_OK(msg, cmsg))
833 return -EINVAL;
834
835 if (cmsg->cmsg_level != SOL_RDS)
836 continue;
837
838 switch (cmsg->cmsg_type) {
839 case RDS_CMSG_RDMA_ARGS:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700840 cmsg_groups |= 1;
Andy Groverff87e972010-01-12 14:13:15 -0800841 retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
842 if (retval < 0)
843 return retval;
844 size += retval;
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700845
Andy Groverff87e972010-01-12 14:13:15 -0800846 break;
847
848 case RDS_CMSG_RDMA_DEST:
849 case RDS_CMSG_RDMA_MAP:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700850 cmsg_groups |= 2;
Andy Groverff87e972010-01-12 14:13:15 -0800851 /* these are valid but do no add any size */
852 break;
853
Andy Grover15133f62010-01-12 14:33:38 -0800854 case RDS_CMSG_ATOMIC_CSWP:
855 case RDS_CMSG_ATOMIC_FADD:
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700856 cmsg_groups |= 1;
Andy Grover15133f62010-01-12 14:33:38 -0800857 size += sizeof(struct scatterlist);
858 break;
859
Andy Groverff87e972010-01-12 14:13:15 -0800860 default:
861 return -EINVAL;
862 }
863
864 }
865
866 size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
Andy Groverfc445082010-01-12 12:56:06 -0800867
Andy Groveraa0a4ef2010-04-13 12:00:35 -0700868 /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
869 if (cmsg_groups == 3)
870 return -EINVAL;
871
Andy Groverfc445082010-01-12 12:56:06 -0800872 return size;
873}
874
Andy Grover5c115592009-02-24 15:30:27 +0000875static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
876 struct msghdr *msg, int *allocated_mr)
877{
878 struct cmsghdr *cmsg;
879 int ret = 0;
880
881 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
882 if (!CMSG_OK(msg, cmsg))
883 return -EINVAL;
884
885 if (cmsg->cmsg_level != SOL_RDS)
886 continue;
887
888 /* As a side effect, RDMA_DEST and RDMA_MAP will set
Andy Grover15133f62010-01-12 14:33:38 -0800889 * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
Andy Grover5c115592009-02-24 15:30:27 +0000890 */
891 switch (cmsg->cmsg_type) {
892 case RDS_CMSG_RDMA_ARGS:
893 ret = rds_cmsg_rdma_args(rs, rm, cmsg);
894 break;
895
896 case RDS_CMSG_RDMA_DEST:
897 ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
898 break;
899
900 case RDS_CMSG_RDMA_MAP:
901 ret = rds_cmsg_rdma_map(rs, rm, cmsg);
902 if (!ret)
903 *allocated_mr = 1;
904 break;
Andy Grover15133f62010-01-12 14:33:38 -0800905 case RDS_CMSG_ATOMIC_CSWP:
906 case RDS_CMSG_ATOMIC_FADD:
907 ret = rds_cmsg_atomic(rs, rm, cmsg);
908 break;
Andy Grover5c115592009-02-24 15:30:27 +0000909
910 default:
911 return -EINVAL;
912 }
913
914 if (ret)
915 break;
916 }
917
918 return ret;
919}
920
921int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
922 size_t payload_len)
923{
924 struct sock *sk = sock->sk;
925 struct rds_sock *rs = rds_sk_to_rs(sk);
926 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
927 __be32 daddr;
928 __be16 dport;
929 struct rds_message *rm = NULL;
930 struct rds_connection *conn;
931 int ret = 0;
932 int queued = 0, allocated_mr = 0;
933 int nonblock = msg->msg_flags & MSG_DONTWAIT;
Andy Grover1123fd72010-03-11 13:49:56 +0000934 long timeo = sock_sndtimeo(sk, nonblock);
Andy Grover5c115592009-02-24 15:30:27 +0000935
936 /* Mirror Linux UDP mirror of BSD error message compatibility */
937 /* XXX: Perhaps MSG_MORE someday */
938 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
939 printk(KERN_INFO "msg_flags 0x%08X\n", msg->msg_flags);
940 ret = -EOPNOTSUPP;
941 goto out;
942 }
943
944 if (msg->msg_namelen) {
945 /* XXX fail non-unicast destination IPs? */
946 if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
947 ret = -EINVAL;
948 goto out;
949 }
950 daddr = usin->sin_addr.s_addr;
951 dport = usin->sin_port;
952 } else {
953 /* We only care about consistency with ->connect() */
954 lock_sock(sk);
955 daddr = rs->rs_conn_addr;
956 dport = rs->rs_conn_port;
957 release_sock(sk);
958 }
959
960 /* racing with another thread binding seems ok here */
961 if (daddr == 0 || rs->rs_bound_addr == 0) {
962 ret = -ENOTCONN; /* XXX not a great errno */
963 goto out;
964 }
965
Andy Groverfc445082010-01-12 12:56:06 -0800966 /* size of rm including all sgs */
967 ret = rds_rm_size(msg, payload_len);
968 if (ret < 0)
969 goto out;
970
971 rm = rds_message_alloc(ret, GFP_KERNEL);
972 if (!rm) {
973 ret = -ENOMEM;
Andy Grover5c115592009-02-24 15:30:27 +0000974 goto out;
975 }
976
Andy Grover372cd7d2010-02-03 19:40:32 -0800977 /* Attach data to the rm */
978 if (payload_len) {
979 rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
980 ret = rds_message_copy_from_user(rm, msg->msg_iov, payload_len);
981 if (ret)
982 goto out;
983 }
984 rm->data.op_active = 1;
Andy Groverfc445082010-01-12 12:56:06 -0800985
Andy Grover5c115592009-02-24 15:30:27 +0000986 rm->m_daddr = daddr;
987
Andy Grover5c115592009-02-24 15:30:27 +0000988 /* rds_conn_create has a spinlock that runs with IRQ off.
989 * Caching the conn in the socket helps a lot. */
990 if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
991 conn = rs->rs_conn;
992 else {
993 conn = rds_conn_create_outgoing(rs->rs_bound_addr, daddr,
994 rs->rs_transport,
995 sock->sk->sk_allocation);
996 if (IS_ERR(conn)) {
997 ret = PTR_ERR(conn);
998 goto out;
999 }
1000 rs->rs_conn = conn;
1001 }
1002
Andy Grover49f69692009-04-09 14:09:41 +00001003 /* Parse any control messages the user may have included. */
1004 ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
1005 if (ret)
1006 goto out;
1007
Andy Grover2c3a5f92010-03-01 16:10:40 -08001008 if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
Andy Grover5c115592009-02-24 15:30:27 +00001009 if (printk_ratelimit())
1010 printk(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
Andy Groverf8b3aaf2010-03-01 14:11:53 -08001011 &rm->rdma, conn->c_trans->xmit_rdma);
Andy Grover15133f62010-01-12 14:33:38 -08001012 ret = -EOPNOTSUPP;
1013 goto out;
1014 }
1015
1016 if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
1017 if (printk_ratelimit())
1018 printk(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
1019 &rm->atomic, conn->c_trans->xmit_atomic);
Andy Grover5c115592009-02-24 15:30:27 +00001020 ret = -EOPNOTSUPP;
1021 goto out;
1022 }
1023
1024 /* If the connection is down, trigger a connect. We may
1025 * have scheduled a delayed reconnect however - in this case
1026 * we should not interfere.
1027 */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001028 if (rds_conn_state(conn) == RDS_CONN_DOWN &&
1029 !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
Andy Grover5c115592009-02-24 15:30:27 +00001030 queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
1031
1032 ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
Andy Groverb98ba522010-03-11 13:50:04 +00001033 if (ret) {
1034 rs->rs_seen_congestion = 1;
Andy Grover5c115592009-02-24 15:30:27 +00001035 goto out;
Andy Groverb98ba522010-03-11 13:50:04 +00001036 }
Andy Grover5c115592009-02-24 15:30:27 +00001037
1038 while (!rds_send_queue_rm(rs, conn, rm, rs->rs_bound_port,
1039 dport, &queued)) {
1040 rds_stats_inc(s_send_queue_full);
1041 /* XXX make sure this is reasonable */
1042 if (payload_len > rds_sk_sndbuf(rs)) {
1043 ret = -EMSGSIZE;
1044 goto out;
1045 }
1046 if (nonblock) {
1047 ret = -EAGAIN;
1048 goto out;
1049 }
1050
Eric Dumazetaa395142010-04-20 13:03:51 +00001051 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
Andy Grover5c115592009-02-24 15:30:27 +00001052 rds_send_queue_rm(rs, conn, rm,
1053 rs->rs_bound_port,
1054 dport,
1055 &queued),
1056 timeo);
1057 rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
1058 if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
1059 continue;
1060
1061 ret = timeo;
1062 if (ret == 0)
1063 ret = -ETIMEDOUT;
1064 goto out;
1065 }
1066
1067 /*
1068 * By now we've committed to the send. We reuse rds_send_worker()
1069 * to retry sends in the rds thread if the transport asks us to.
1070 */
1071 rds_stats_inc(s_send_queued);
1072
1073 if (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags))
1074 rds_send_worker(&conn->c_send_w.work);
1075
1076 rds_message_put(rm);
1077 return payload_len;
1078
1079out:
1080 /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
1081 * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
1082 * or in any other way, we need to destroy the MR again */
1083 if (allocated_mr)
1084 rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
1085
1086 if (rm)
1087 rds_message_put(rm);
1088 return ret;
1089}
1090
1091/*
1092 * Reply to a ping packet.
1093 */
1094int
1095rds_send_pong(struct rds_connection *conn, __be16 dport)
1096{
1097 struct rds_message *rm;
1098 unsigned long flags;
1099 int ret = 0;
1100
1101 rm = rds_message_alloc(0, GFP_ATOMIC);
Andy Grover8690bfa2010-01-12 11:56:44 -08001102 if (!rm) {
Andy Grover5c115592009-02-24 15:30:27 +00001103 ret = -ENOMEM;
1104 goto out;
1105 }
1106
1107 rm->m_daddr = conn->c_faddr;
1108
1109 /* If the connection is down, trigger a connect. We may
1110 * have scheduled a delayed reconnect however - in this case
1111 * we should not interfere.
1112 */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001113 if (rds_conn_state(conn) == RDS_CONN_DOWN &&
1114 !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
Andy Grover5c115592009-02-24 15:30:27 +00001115 queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
1116
1117 ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
1118 if (ret)
1119 goto out;
1120
1121 spin_lock_irqsave(&conn->c_lock, flags);
1122 list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
1123 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
1124 rds_message_addref(rm);
1125 rm->m_inc.i_conn = conn;
1126
1127 rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
1128 conn->c_next_tx_seq);
1129 conn->c_next_tx_seq++;
1130 spin_unlock_irqrestore(&conn->c_lock, flags);
1131
1132 rds_stats_inc(s_send_queued);
1133 rds_stats_inc(s_send_pong);
1134
1135 queue_delayed_work(rds_wq, &conn->c_send_w, 0);
1136 rds_message_put(rm);
1137 return 0;
1138
1139out:
1140 if (rm)
1141 rds_message_put(rm);
1142 return ret;
1143}