RDS: Move some variables around for consistency
[deliverable/linux.git] / net / rds / send.c
CommitLineData
5c115590
AG
1/*
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>
5a0e3ad6 34#include <linux/gfp.h>
5c115590
AG
35#include <net/sock.h>
36#include <linux/in.h>
37#include <linux/list.h>
38
39#include "rds.h"
5c115590
AG
40
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;
15133f6e 75 conn->c_xmit_atomic_sent = 0;
5b2366bd
AG
76 conn->c_xmit_rdma_sent = 0;
77 conn->c_xmit_data_sent = 0;
5c115590
AG
78
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
5b2366bd 141 * the connection doesn't make forward progress.
5c115590
AG
142 */
143 while (--send_quota) {
5c115590 144
5c115590 145 rm = conn->c_xmit_rm;
5c115590 146
5b2366bd
AG
147 /*
148 * If between sending messages, we can send a pending congestion
149 * map update.
150 *
151 * Transports either define a special xmit_cong_map function,
152 * or we allocate a cong_map message and treat it just like any
153 * other send.
5c115590 154 */
8690bfa1
AG
155 if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
156 if (conn->c_trans->xmit_cong_map) {
5b2366bd
AG
157 unsigned long map_offset = 0;
158 unsigned long map_bytes = sizeof(struct rds_header) +
5c115590 159 RDS_CONG_MAP_BYTES;
5c115590 160
5b2366bd
AG
161 while (map_bytes) {
162 ret = conn->c_trans->xmit_cong_map(conn, conn->c_lcong,
163 map_offset);
164 if (ret <= 0) {
165 /* too far down the rabbithole! */
166 mutex_unlock(&conn->c_send_lock);
167 rds_conn_error(conn, "Cong map xmit failed\n");
168 goto out;
169 }
170
171 map_offset += ret;
172 map_bytes -= ret;
173 }
174 } else {
175 /* send cong update like a normal rm */
176 rm = rds_cong_update_alloc(conn);
177 if (IS_ERR(rm)) {
178 ret = PTR_ERR(rm);
179 break;
180 }
181 rm->data.op_active = 1;
5c115590 182
5b2366bd
AG
183 conn->c_xmit_rm = rm;
184 }
5c115590
AG
185 }
186
187 /*
5b2366bd 188 * If not already working on one, grab the next message.
5c115590
AG
189 *
190 * c_xmit_rm holds a ref while we're sending this message down
191 * the connction. We can use this ref while holding the
192 * send_sem.. rds_send_reset() is serialized with it.
193 */
8690bfa1 194 if (!rm) {
5c115590
AG
195 unsigned int len;
196
197 spin_lock_irqsave(&conn->c_lock, flags);
198
199 if (!list_empty(&conn->c_send_queue)) {
200 rm = list_entry(conn->c_send_queue.next,
201 struct rds_message,
202 m_conn_item);
203 rds_message_addref(rm);
204
205 /*
206 * Move the message from the send queue to the retransmit
207 * list right away.
208 */
209 list_move_tail(&rm->m_conn_item, &conn->c_retrans);
210 }
211
212 spin_unlock_irqrestore(&conn->c_lock, flags);
213
8690bfa1 214 if (!rm) {
5c115590
AG
215 was_empty = 1;
216 break;
217 }
218
219 /* Unfortunately, the way Infiniband deals with
220 * RDMA to a bad MR key is by moving the entire
221 * queue pair to error state. We cold possibly
222 * recover from that, but right now we drop the
223 * connection.
224 * Therefore, we never retransmit messages with RDMA ops.
225 */
f8b3aaf2 226 if (rm->rdma.op_active &&
f64f9e71 227 test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) {
5c115590
AG
228 spin_lock_irqsave(&conn->c_lock, flags);
229 if (test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags))
230 list_move(&rm->m_conn_item, &to_be_dropped);
231 spin_unlock_irqrestore(&conn->c_lock, flags);
232 rds_message_put(rm);
233 continue;
234 }
235
236 /* Require an ACK every once in a while */
237 len = ntohl(rm->m_inc.i_hdr.h_len);
f64f9e71
JP
238 if (conn->c_unacked_packets == 0 ||
239 conn->c_unacked_bytes < len) {
5c115590
AG
240 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
241
242 conn->c_unacked_packets = rds_sysctl_max_unacked_packets;
243 conn->c_unacked_bytes = rds_sysctl_max_unacked_bytes;
244 rds_stats_inc(s_send_ack_required);
245 } else {
246 conn->c_unacked_bytes -= len;
247 conn->c_unacked_packets--;
248 }
249
250 conn->c_xmit_rm = rm;
251 }
252
15133f6e 253 if (rm->atomic.op_active && !conn->c_xmit_atomic_sent) {
241eef3e 254 ret = conn->c_trans->xmit_atomic(conn, rm);
15133f6e
AG
255 if (ret)
256 break;
257 conn->c_xmit_atomic_sent = 1;
258 /* The transport owns the mapped memory for now.
259 * You can't unmap it while it's on the send queue */
260 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
5b2366bd
AG
261
262 /*
263 * This is evil, muahaha.
264 * We permit 0-byte sends. (rds-ping depends on this.)
265 * BUT if there is an atomic op and no sent data,
266 * we turn off sending the header, to achieve
267 * "silent" atomics.
268 * But see below; RDMA op might toggle this back on!
269 */
270 if (rm->data.op_nents == 0)
271 rm->data.op_active = 0;
15133f6e
AG
272 }
273
5b2366bd 274 /* The transport either sends the whole rdma or none of it */
f8b3aaf2
AG
275 if (rm->rdma.op_active && !conn->c_xmit_rdma_sent) {
276 ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
5c115590
AG
277 if (ret)
278 break;
279 conn->c_xmit_rdma_sent = 1;
241eef3e
AG
280
281 /* rdmas need data sent, even if just the header */
282 rm->data.op_active = 1;
283
5c115590
AG
284 /* The transport owns the mapped memory for now.
285 * You can't unmap it while it's on the send queue */
286 set_bit(RDS_MSG_MAPPED, &rm->m_flags);
287 }
288
5b2366bd 289 if (rm->data.op_active && !conn->c_xmit_data_sent) {
5c115590
AG
290 ret = conn->c_trans->xmit(conn, rm,
291 conn->c_xmit_hdr_off,
292 conn->c_xmit_sg,
293 conn->c_xmit_data_off);
294 if (ret <= 0)
295 break;
296
297 if (conn->c_xmit_hdr_off < sizeof(struct rds_header)) {
298 tmp = min_t(int, ret,
299 sizeof(struct rds_header) -
300 conn->c_xmit_hdr_off);
301 conn->c_xmit_hdr_off += tmp;
302 ret -= tmp;
303 }
304
6c7cc6e4 305 sg = &rm->data.op_sg[conn->c_xmit_sg];
5c115590
AG
306 while (ret) {
307 tmp = min_t(int, ret, sg->length -
308 conn->c_xmit_data_off);
309 conn->c_xmit_data_off += tmp;
310 ret -= tmp;
311 if (conn->c_xmit_data_off == sg->length) {
312 conn->c_xmit_data_off = 0;
313 sg++;
314 conn->c_xmit_sg++;
315 BUG_ON(ret != 0 &&
6c7cc6e4 316 conn->c_xmit_sg == rm->data.op_nents);
5c115590
AG
317 }
318 }
5b2366bd
AG
319
320 if (conn->c_xmit_hdr_off == sizeof(struct rds_header) &&
321 (conn->c_xmit_sg == rm->data.op_nents))
322 conn->c_xmit_data_sent = 1;
323 }
324
325 /*
326 * A rm will only take multiple times through this loop
327 * if there is a data op. Thus, if the data is sent (or there was
328 * none), then we're done with the rm.
329 */
330 if (!rm->data.op_active || conn->c_xmit_data_sent) {
331 conn->c_xmit_rm = NULL;
332 conn->c_xmit_sg = 0;
333 conn->c_xmit_hdr_off = 0;
334 conn->c_xmit_data_off = 0;
335 conn->c_xmit_rdma_sent = 0;
336 conn->c_xmit_atomic_sent = 0;
337 conn->c_xmit_data_sent = 0;
338
339 rds_message_put(rm);
5c115590
AG
340 }
341 }
342
343 /* Nuke any messages we decided not to retransmit. */
344 if (!list_empty(&to_be_dropped))
345 rds_send_remove_from_sock(&to_be_dropped, RDS_RDMA_DROPPED);
346
347 if (conn->c_trans->xmit_complete)
348 conn->c_trans->xmit_complete(conn);
349
350 /*
351 * We might be racing with another sender who queued a message but
352 * backed off on noticing that we held the c_send_lock. If we check
353 * for queued messages after dropping the sem then either we'll
354 * see the queued message or the queuer will get the sem. If we
355 * notice the queued message then we trigger an immediate retry.
356 *
357 * We need to be careful only to do this when we stopped processing
358 * the send queue because it was empty. It's the only way we
359 * stop processing the loop when the transport hasn't taken
360 * responsibility for forward progress.
361 */
362 mutex_unlock(&conn->c_send_lock);
363
5b2366bd 364 if (send_quota == 0 && !was_empty) {
5c115590
AG
365 /* We exhausted the send quota, but there's work left to
366 * do. Return and (re-)schedule the send worker.
367 */
368 ret = -EAGAIN;
369 }
370
371 if (ret == 0 && was_empty) {
372 /* A simple bit test would be way faster than taking the
373 * spin lock */
374 spin_lock_irqsave(&conn->c_lock, flags);
375 if (!list_empty(&conn->c_send_queue)) {
376 rds_stats_inc(s_send_sem_queue_raced);
377 ret = -EAGAIN;
378 }
379 spin_unlock_irqrestore(&conn->c_lock, flags);
380 }
381out:
382 return ret;
383}
384
385static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
386{
387 u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
388
389 assert_spin_locked(&rs->rs_lock);
390
391 BUG_ON(rs->rs_snd_bytes < len);
392 rs->rs_snd_bytes -= len;
393
394 if (rs->rs_snd_bytes == 0)
395 rds_stats_inc(s_send_queue_empty);
396}
397
398static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
399 is_acked_func is_acked)
400{
401 if (is_acked)
402 return is_acked(rm, ack);
403 return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
404}
405
406/*
407 * Returns true if there are no messages on the send and retransmit queues
408 * which have a sequence number greater than or equal to the given sequence
409 * number.
410 */
411int rds_send_acked_before(struct rds_connection *conn, u64 seq)
412{
413 struct rds_message *rm, *tmp;
414 int ret = 1;
415
416 spin_lock(&conn->c_lock);
417
418 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
419 if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
420 ret = 0;
421 break;
422 }
423
424 list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
425 if (be64_to_cpu(rm->m_inc.i_hdr.h_sequence) < seq)
426 ret = 0;
427 break;
428 }
429
430 spin_unlock(&conn->c_lock);
431
432 return ret;
433}
434
435/*
436 * This is pretty similar to what happens below in the ACK
437 * handling code - except that we call here as soon as we get
438 * the IB send completion on the RDMA op and the accompanying
439 * message.
440 */
441void rds_rdma_send_complete(struct rds_message *rm, int status)
442{
443 struct rds_sock *rs = NULL;
f8b3aaf2 444 struct rm_rdma_op *ro;
5c115590 445 struct rds_notifier *notifier;
9de0864c 446 unsigned long flags;
5c115590 447
9de0864c 448 spin_lock_irqsave(&rm->m_rs_lock, flags);
5c115590 449
f8b3aaf2 450 ro = &rm->rdma;
f64f9e71 451 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
f8b3aaf2
AG
452 ro->op_active && ro->op_notify && ro->op_notifier) {
453 notifier = ro->op_notifier;
5c115590
AG
454 rs = rm->m_rs;
455 sock_hold(rds_rs_to_sk(rs));
456
457 notifier->n_status = status;
458 spin_lock(&rs->rs_lock);
459 list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
460 spin_unlock(&rs->rs_lock);
461
f8b3aaf2 462 ro->op_notifier = NULL;
5c115590
AG
463 }
464
9de0864c 465 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
5c115590
AG
466
467 if (rs) {
468 rds_wake_sk_sleep(rs);
469 sock_put(rds_rs_to_sk(rs));
470 }
471}
616b757a 472EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
5c115590 473
15133f6e
AG
474/*
475 * Just like above, except looks at atomic op
476 */
477void rds_atomic_send_complete(struct rds_message *rm, int status)
478{
479 struct rds_sock *rs = NULL;
480 struct rm_atomic_op *ao;
481 struct rds_notifier *notifier;
482
483 spin_lock(&rm->m_rs_lock);
484
485 ao = &rm->atomic;
486 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags)
487 && ao->op_active && ao->op_notify && ao->op_notifier) {
488 notifier = ao->op_notifier;
489 rs = rm->m_rs;
490 sock_hold(rds_rs_to_sk(rs));
491
492 notifier->n_status = status;
493 spin_lock(&rs->rs_lock);
494 list_add_tail(&notifier->n_list, &rs->rs_notify_queue);
495 spin_unlock(&rs->rs_lock);
496
497 ao->op_notifier = NULL;
498 }
499
500 spin_unlock(&rm->m_rs_lock);
501
502 if (rs) {
503 rds_wake_sk_sleep(rs);
504 sock_put(rds_rs_to_sk(rs));
505 }
506}
507EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
508
5c115590
AG
509/*
510 * This is the same as rds_rdma_send_complete except we
511 * don't do any locking - we have all the ingredients (message,
512 * socket, socket lock) and can just move the notifier.
513 */
514static inline void
940786eb 515__rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
5c115590 516{
f8b3aaf2 517 struct rm_rdma_op *ro;
940786eb 518 struct rm_atomic_op *ao;
5c115590 519
f8b3aaf2
AG
520 ro = &rm->rdma;
521 if (ro->op_active && ro->op_notify && ro->op_notifier) {
522 ro->op_notifier->n_status = status;
523 list_add_tail(&ro->op_notifier->n_list, &rs->rs_notify_queue);
524 ro->op_notifier = NULL;
5c115590
AG
525 }
526
940786eb
AG
527 ao = &rm->atomic;
528 if (ao->op_active && ao->op_notify && ao->op_notifier) {
529 ao->op_notifier->n_status = status;
530 list_add_tail(&ao->op_notifier->n_list, &rs->rs_notify_queue);
531 ao->op_notifier = NULL;
532 }
533
5c115590
AG
534 /* No need to wake the app - caller does this */
535}
536
537/*
538 * This is called from the IB send completion when we detect
539 * a RDMA operation that failed with remote access error.
540 * So speed is not an issue here.
541 */
542struct rds_message *rds_send_get_message(struct rds_connection *conn,
f8b3aaf2 543 struct rm_rdma_op *op)
5c115590
AG
544{
545 struct rds_message *rm, *tmp, *found = NULL;
546 unsigned long flags;
547
548 spin_lock_irqsave(&conn->c_lock, flags);
549
550 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
f8b3aaf2 551 if (&rm->rdma == op) {
5c115590
AG
552 atomic_inc(&rm->m_refcount);
553 found = rm;
554 goto out;
555 }
556 }
557
558 list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
f8b3aaf2 559 if (&rm->rdma == op) {
5c115590
AG
560 atomic_inc(&rm->m_refcount);
561 found = rm;
562 break;
563 }
564 }
565
566out:
567 spin_unlock_irqrestore(&conn->c_lock, flags);
568
569 return found;
570}
616b757a 571EXPORT_SYMBOL_GPL(rds_send_get_message);
5c115590
AG
572
573/*
574 * This removes messages from the socket's list if they're on it. The list
575 * argument must be private to the caller, we must be able to modify it
576 * without locks. The messages must have a reference held for their
577 * position on the list. This function will drop that reference after
578 * removing the messages from the 'messages' list regardless of if it found
579 * the messages on the socket list or not.
580 */
581void rds_send_remove_from_sock(struct list_head *messages, int status)
582{
561c7df6 583 unsigned long flags;
5c115590
AG
584 struct rds_sock *rs = NULL;
585 struct rds_message *rm;
586
5c115590 587 while (!list_empty(messages)) {
561c7df6
AG
588 int was_on_sock = 0;
589
5c115590
AG
590 rm = list_entry(messages->next, struct rds_message,
591 m_conn_item);
592 list_del_init(&rm->m_conn_item);
593
594 /*
595 * If we see this flag cleared then we're *sure* that someone
596 * else beat us to removing it from the sock. If we race
597 * with their flag update we'll get the lock and then really
598 * see that the flag has been cleared.
599 *
600 * The message spinlock makes sure nobody clears rm->m_rs
601 * while we're messing with it. It does not prevent the
602 * message from being removed from the socket, though.
603 */
561c7df6 604 spin_lock_irqsave(&rm->m_rs_lock, flags);
5c115590
AG
605 if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
606 goto unlock_and_drop;
607
608 if (rs != rm->m_rs) {
609 if (rs) {
5c115590
AG
610 rds_wake_sk_sleep(rs);
611 sock_put(rds_rs_to_sk(rs));
612 }
613 rs = rm->m_rs;
5c115590
AG
614 sock_hold(rds_rs_to_sk(rs));
615 }
048c15e6 616 spin_lock(&rs->rs_lock);
5c115590
AG
617
618 if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
f8b3aaf2 619 struct rm_rdma_op *ro = &rm->rdma;
5c115590
AG
620 struct rds_notifier *notifier;
621
622 list_del_init(&rm->m_sock_item);
623 rds_send_sndbuf_remove(rs, rm);
624
f8b3aaf2
AG
625 if (ro->op_active && ro->op_notifier &&
626 (ro->op_notify || (ro->op_recverr && status))) {
627 notifier = ro->op_notifier;
5c115590
AG
628 list_add_tail(&notifier->n_list,
629 &rs->rs_notify_queue);
630 if (!notifier->n_status)
631 notifier->n_status = status;
f8b3aaf2 632 rm->rdma.op_notifier = NULL;
5c115590 633 }
561c7df6 634 was_on_sock = 1;
5c115590
AG
635 rm->m_rs = NULL;
636 }
048c15e6 637 spin_unlock(&rs->rs_lock);
5c115590
AG
638
639unlock_and_drop:
561c7df6 640 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
5c115590 641 rds_message_put(rm);
561c7df6
AG
642 if (was_on_sock)
643 rds_message_put(rm);
5c115590
AG
644 }
645
646 if (rs) {
5c115590
AG
647 rds_wake_sk_sleep(rs);
648 sock_put(rds_rs_to_sk(rs));
649 }
5c115590
AG
650}
651
652/*
653 * Transports call here when they've determined that the receiver queued
654 * messages up to, and including, the given sequence number. Messages are
655 * moved to the retrans queue when rds_send_xmit picks them off the send
656 * queue. This means that in the TCP case, the message may not have been
657 * assigned the m_ack_seq yet - but that's fine as long as tcp_is_acked
658 * checks the RDS_MSG_HAS_ACK_SEQ bit.
659 *
660 * XXX It's not clear to me how this is safely serialized with socket
661 * destruction. Maybe it should bail if it sees SOCK_DEAD.
662 */
663void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
664 is_acked_func is_acked)
665{
666 struct rds_message *rm, *tmp;
667 unsigned long flags;
668 LIST_HEAD(list);
669
670 spin_lock_irqsave(&conn->c_lock, flags);
671
672 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
673 if (!rds_send_is_acked(rm, ack, is_acked))
674 break;
675
676 list_move(&rm->m_conn_item, &list);
677 clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
678 }
679
680 /* order flag updates with spin locks */
681 if (!list_empty(&list))
682 smp_mb__after_clear_bit();
683
684 spin_unlock_irqrestore(&conn->c_lock, flags);
685
686 /* now remove the messages from the sock list as needed */
687 rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
688}
616b757a 689EXPORT_SYMBOL_GPL(rds_send_drop_acked);
5c115590
AG
690
691void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
692{
693 struct rds_message *rm, *tmp;
694 struct rds_connection *conn;
7c82eaf0 695 unsigned long flags;
5c115590 696 LIST_HEAD(list);
5c115590
AG
697
698 /* get all the messages we're dropping under the rs lock */
699 spin_lock_irqsave(&rs->rs_lock, flags);
700
701 list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
702 if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
703 dest->sin_port != rm->m_inc.i_hdr.h_dport))
704 continue;
705
5c115590
AG
706 list_move(&rm->m_sock_item, &list);
707 rds_send_sndbuf_remove(rs, rm);
708 clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
5c115590
AG
709 }
710
711 /* order flag updates with the rs lock */
7c82eaf0 712 smp_mb__after_clear_bit();
5c115590
AG
713
714 spin_unlock_irqrestore(&rs->rs_lock, flags);
715
7c82eaf0
AG
716 if (list_empty(&list))
717 return;
5c115590 718
7c82eaf0 719 /* Remove the messages from the conn */
5c115590 720 list_for_each_entry(rm, &list, m_sock_item) {
7c82eaf0
AG
721
722 conn = rm->m_inc.i_conn;
5c115590 723
9de0864c 724 spin_lock_irqsave(&conn->c_lock, flags);
5c115590 725 /*
7c82eaf0
AG
726 * Maybe someone else beat us to removing rm from the conn.
727 * If we race with their flag update we'll get the lock and
728 * then really see that the flag has been cleared.
5c115590 729 */
7c82eaf0
AG
730 if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
731 spin_unlock_irqrestore(&conn->c_lock, flags);
5c115590 732 continue;
5c115590 733 }
9de0864c
AG
734 list_del_init(&rm->m_conn_item);
735 spin_unlock_irqrestore(&conn->c_lock, flags);
5c115590 736
7c82eaf0
AG
737 /*
738 * Couldn't grab m_rs_lock in top loop (lock ordering),
739 * but we can now.
740 */
9de0864c 741 spin_lock_irqsave(&rm->m_rs_lock, flags);
5c115590 742
7c82eaf0 743 spin_lock(&rs->rs_lock);
940786eb 744 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
7c82eaf0
AG
745 spin_unlock(&rs->rs_lock);
746
747 rm->m_rs = NULL;
9de0864c 748 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
7c82eaf0 749
7c82eaf0 750 rds_message_put(rm);
7c82eaf0 751 }
5c115590 752
7c82eaf0 753 rds_wake_sk_sleep(rs);
550a8002 754
5c115590
AG
755 while (!list_empty(&list)) {
756 rm = list_entry(list.next, struct rds_message, m_sock_item);
757 list_del_init(&rm->m_sock_item);
758
759 rds_message_wait(rm);
760 rds_message_put(rm);
761 }
762}
763
764/*
765 * we only want this to fire once so we use the callers 'queued'. It's
766 * possible that another thread can race with us and remove the
767 * message from the flow with RDS_CANCEL_SENT_TO.
768 */
769static int rds_send_queue_rm(struct rds_sock *rs, struct rds_connection *conn,
770 struct rds_message *rm, __be16 sport,
771 __be16 dport, int *queued)
772{
773 unsigned long flags;
774 u32 len;
775
776 if (*queued)
777 goto out;
778
779 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
780
781 /* this is the only place which holds both the socket's rs_lock
782 * and the connection's c_lock */
783 spin_lock_irqsave(&rs->rs_lock, flags);
784
785 /*
786 * If there is a little space in sndbuf, we don't queue anything,
787 * and userspace gets -EAGAIN. But poll() indicates there's send
788 * room. This can lead to bad behavior (spinning) if snd_bytes isn't
789 * freed up by incoming acks. So we check the *old* value of
790 * rs_snd_bytes here to allow the last msg to exceed the buffer,
791 * and poll() now knows no more data can be sent.
792 */
793 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
794 rs->rs_snd_bytes += len;
795
796 /* let recv side know we are close to send space exhaustion.
797 * This is probably not the optimal way to do it, as this
798 * means we set the flag on *all* messages as soon as our
799 * throughput hits a certain threshold.
800 */
801 if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
802 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
803
804 list_add_tail(&rm->m_sock_item, &rs->rs_send_queue);
805 set_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
806 rds_message_addref(rm);
807 rm->m_rs = rs;
808
809 /* The code ordering is a little weird, but we're
810 trying to minimize the time we hold c_lock */
811 rds_message_populate_header(&rm->m_inc.i_hdr, sport, dport, 0);
812 rm->m_inc.i_conn = conn;
813 rds_message_addref(rm);
814
815 spin_lock(&conn->c_lock);
816 rm->m_inc.i_hdr.h_sequence = cpu_to_be64(conn->c_next_tx_seq++);
817 list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
818 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
819 spin_unlock(&conn->c_lock);
820
821 rdsdebug("queued msg %p len %d, rs %p bytes %d seq %llu\n",
822 rm, len, rs, rs->rs_snd_bytes,
823 (unsigned long long)be64_to_cpu(rm->m_inc.i_hdr.h_sequence));
824
825 *queued = 1;
826 }
827
828 spin_unlock_irqrestore(&rs->rs_lock, flags);
829out:
830 return *queued;
831}
832
fc445084
AG
833/*
834 * rds_message is getting to be quite complicated, and we'd like to allocate
835 * it all in one go. This figures out how big it needs to be up front.
836 */
837static int rds_rm_size(struct msghdr *msg, int data_len)
838{
ff87e97a 839 struct cmsghdr *cmsg;
fc445084 840 int size = 0;
ff87e97a
AG
841 int retval;
842
843 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
844 if (!CMSG_OK(msg, cmsg))
845 return -EINVAL;
846
847 if (cmsg->cmsg_level != SOL_RDS)
848 continue;
849
850 switch (cmsg->cmsg_type) {
851 case RDS_CMSG_RDMA_ARGS:
852 retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
853 if (retval < 0)
854 return retval;
855 size += retval;
856 break;
857
858 case RDS_CMSG_RDMA_DEST:
859 case RDS_CMSG_RDMA_MAP:
860 /* these are valid but do no add any size */
861 break;
862
15133f6e
AG
863 case RDS_CMSG_ATOMIC_CSWP:
864 case RDS_CMSG_ATOMIC_FADD:
865 size += sizeof(struct scatterlist);
866 break;
867
ff87e97a
AG
868 default:
869 return -EINVAL;
870 }
871
872 }
fc445084 873
ff87e97a 874 size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
fc445084
AG
875
876 return size;
877}
878
5c115590
AG
879static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
880 struct msghdr *msg, int *allocated_mr)
881{
882 struct cmsghdr *cmsg;
883 int ret = 0;
884
885 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
886 if (!CMSG_OK(msg, cmsg))
887 return -EINVAL;
888
889 if (cmsg->cmsg_level != SOL_RDS)
890 continue;
891
892 /* As a side effect, RDMA_DEST and RDMA_MAP will set
15133f6e 893 * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
5c115590
AG
894 */
895 switch (cmsg->cmsg_type) {
896 case RDS_CMSG_RDMA_ARGS:
897 ret = rds_cmsg_rdma_args(rs, rm, cmsg);
898 break;
899
900 case RDS_CMSG_RDMA_DEST:
901 ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
902 break;
903
904 case RDS_CMSG_RDMA_MAP:
905 ret = rds_cmsg_rdma_map(rs, rm, cmsg);
906 if (!ret)
907 *allocated_mr = 1;
908 break;
15133f6e
AG
909 case RDS_CMSG_ATOMIC_CSWP:
910 case RDS_CMSG_ATOMIC_FADD:
911 ret = rds_cmsg_atomic(rs, rm, cmsg);
912 break;
5c115590
AG
913
914 default:
915 return -EINVAL;
916 }
917
918 if (ret)
919 break;
920 }
921
922 return ret;
923}
924
925int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
926 size_t payload_len)
927{
928 struct sock *sk = sock->sk;
929 struct rds_sock *rs = rds_sk_to_rs(sk);
930 struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
931 __be32 daddr;
932 __be16 dport;
933 struct rds_message *rm = NULL;
934 struct rds_connection *conn;
935 int ret = 0;
936 int queued = 0, allocated_mr = 0;
937 int nonblock = msg->msg_flags & MSG_DONTWAIT;
1123fd73 938 long timeo = sock_sndtimeo(sk, nonblock);
5c115590
AG
939
940 /* Mirror Linux UDP mirror of BSD error message compatibility */
941 /* XXX: Perhaps MSG_MORE someday */
942 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_CMSG_COMPAT)) {
943 printk(KERN_INFO "msg_flags 0x%08X\n", msg->msg_flags);
944 ret = -EOPNOTSUPP;
945 goto out;
946 }
947
948 if (msg->msg_namelen) {
949 /* XXX fail non-unicast destination IPs? */
950 if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
951 ret = -EINVAL;
952 goto out;
953 }
954 daddr = usin->sin_addr.s_addr;
955 dport = usin->sin_port;
956 } else {
957 /* We only care about consistency with ->connect() */
958 lock_sock(sk);
959 daddr = rs->rs_conn_addr;
960 dport = rs->rs_conn_port;
961 release_sock(sk);
962 }
963
964 /* racing with another thread binding seems ok here */
965 if (daddr == 0 || rs->rs_bound_addr == 0) {
966 ret = -ENOTCONN; /* XXX not a great errno */
967 goto out;
968 }
969
fc445084
AG
970 /* size of rm including all sgs */
971 ret = rds_rm_size(msg, payload_len);
972 if (ret < 0)
973 goto out;
974
975 rm = rds_message_alloc(ret, GFP_KERNEL);
976 if (!rm) {
977 ret = -ENOMEM;
5c115590
AG
978 goto out;
979 }
980
372cd7de
AG
981 /* Attach data to the rm */
982 if (payload_len) {
983 rm->data.op_sg = rds_message_alloc_sgs(rm, ceil(payload_len, PAGE_SIZE));
984 ret = rds_message_copy_from_user(rm, msg->msg_iov, payload_len);
985 if (ret)
986 goto out;
987 }
988 rm->data.op_active = 1;
fc445084 989
5c115590
AG
990 rm->m_daddr = daddr;
991
5c115590
AG
992 /* rds_conn_create has a spinlock that runs with IRQ off.
993 * Caching the conn in the socket helps a lot. */
994 if (rs->rs_conn && rs->rs_conn->c_faddr == daddr)
995 conn = rs->rs_conn;
996 else {
997 conn = rds_conn_create_outgoing(rs->rs_bound_addr, daddr,
998 rs->rs_transport,
999 sock->sk->sk_allocation);
1000 if (IS_ERR(conn)) {
1001 ret = PTR_ERR(conn);
1002 goto out;
1003 }
1004 rs->rs_conn = conn;
1005 }
1006
49f69691
AG
1007 /* Parse any control messages the user may have included. */
1008 ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
1009 if (ret)
1010 goto out;
1011
f8b3aaf2
AG
1012 if ((rm->m_rdma_cookie || rm->rdma.op_active) &&
1013 !conn->c_trans->xmit_rdma) {
5c115590
AG
1014 if (printk_ratelimit())
1015 printk(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
f8b3aaf2 1016 &rm->rdma, conn->c_trans->xmit_rdma);
15133f6e
AG
1017 ret = -EOPNOTSUPP;
1018 goto out;
1019 }
1020
1021 if (rm->atomic.op_active && !conn->c_trans->xmit_atomic) {
1022 if (printk_ratelimit())
1023 printk(KERN_NOTICE "atomic_op %p conn xmit_atomic %p\n",
1024 &rm->atomic, conn->c_trans->xmit_atomic);
5c115590
AG
1025 ret = -EOPNOTSUPP;
1026 goto out;
1027 }
1028
1029 /* If the connection is down, trigger a connect. We may
1030 * have scheduled a delayed reconnect however - in this case
1031 * we should not interfere.
1032 */
f64f9e71
JP
1033 if (rds_conn_state(conn) == RDS_CONN_DOWN &&
1034 !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
5c115590
AG
1035 queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
1036
1037 ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
b98ba52f
AG
1038 if (ret) {
1039 rs->rs_seen_congestion = 1;
5c115590 1040 goto out;
b98ba52f 1041 }
5c115590
AG
1042
1043 while (!rds_send_queue_rm(rs, conn, rm, rs->rs_bound_port,
1044 dport, &queued)) {
1045 rds_stats_inc(s_send_queue_full);
1046 /* XXX make sure this is reasonable */
1047 if (payload_len > rds_sk_sndbuf(rs)) {
1048 ret = -EMSGSIZE;
1049 goto out;
1050 }
1051 if (nonblock) {
1052 ret = -EAGAIN;
1053 goto out;
1054 }
1055
aa395145 1056 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
5c115590
AG
1057 rds_send_queue_rm(rs, conn, rm,
1058 rs->rs_bound_port,
1059 dport,
1060 &queued),
1061 timeo);
1062 rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
1063 if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
1064 continue;
1065
1066 ret = timeo;
1067 if (ret == 0)
1068 ret = -ETIMEDOUT;
1069 goto out;
1070 }
1071
1072 /*
1073 * By now we've committed to the send. We reuse rds_send_worker()
1074 * to retry sends in the rds thread if the transport asks us to.
1075 */
1076 rds_stats_inc(s_send_queued);
1077
1078 if (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags))
1079 rds_send_worker(&conn->c_send_w.work);
1080
1081 rds_message_put(rm);
1082 return payload_len;
1083
1084out:
1085 /* If the user included a RDMA_MAP cmsg, we allocated a MR on the fly.
1086 * If the sendmsg goes through, we keep the MR. If it fails with EAGAIN
1087 * or in any other way, we need to destroy the MR again */
1088 if (allocated_mr)
1089 rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
1090
1091 if (rm)
1092 rds_message_put(rm);
1093 return ret;
1094}
1095
1096/*
1097 * Reply to a ping packet.
1098 */
1099int
1100rds_send_pong(struct rds_connection *conn, __be16 dport)
1101{
1102 struct rds_message *rm;
1103 unsigned long flags;
1104 int ret = 0;
1105
1106 rm = rds_message_alloc(0, GFP_ATOMIC);
8690bfa1 1107 if (!rm) {
5c115590
AG
1108 ret = -ENOMEM;
1109 goto out;
1110 }
1111
1112 rm->m_daddr = conn->c_faddr;
1113
1114 /* If the connection is down, trigger a connect. We may
1115 * have scheduled a delayed reconnect however - in this case
1116 * we should not interfere.
1117 */
f64f9e71
JP
1118 if (rds_conn_state(conn) == RDS_CONN_DOWN &&
1119 !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
5c115590
AG
1120 queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
1121
1122 ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
1123 if (ret)
1124 goto out;
1125
1126 spin_lock_irqsave(&conn->c_lock, flags);
1127 list_add_tail(&rm->m_conn_item, &conn->c_send_queue);
1128 set_bit(RDS_MSG_ON_CONN, &rm->m_flags);
1129 rds_message_addref(rm);
1130 rm->m_inc.i_conn = conn;
1131
1132 rds_message_populate_header(&rm->m_inc.i_hdr, 0, dport,
1133 conn->c_next_tx_seq);
1134 conn->c_next_tx_seq++;
1135 spin_unlock_irqrestore(&conn->c_lock, flags);
1136
1137 rds_stats_inc(s_send_queued);
1138 rds_stats_inc(s_send_pong);
1139
1140 queue_delayed_work(rds_wq, &conn->c_send_w, 0);
1141 rds_message_put(rm);
1142 return 0;
1143
1144out:
1145 if (rm)
1146 rds_message_put(rm);
1147 return ret;
1148}
This page took 0.183893 seconds and 5 git commands to generate.