2 * Copyright (c) 2006 Oracle. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/kernel.h>
34 #include <linux/gfp.h>
37 #include <linux/list.h>
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
44 * Also, it seems fairer to not let one busy connection stall all the
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
51 static int send_batch_count = 64;
52 module_param(send_batch_count, int, 0444);
53 MODULE_PARM_DESC(send_batch_count, " batch factor when working the send queue");
56 * Reset the send state. Caller must hold c_send_lock when calling here.
58 void rds_send_reset(struct rds_connection *conn)
60 struct rds_message *rm, *tmp;
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;
73 conn->c_xmit_hdr_off = 0;
74 conn->c_xmit_data_off = 0;
75 conn->c_xmit_atomic_sent = 0;
76 conn->c_xmit_rdma_sent = 0;
77 conn->c_xmit_data_sent = 0;
79 conn->c_map_queued = 0;
81 conn->c_unacked_packets = rds_sysctl_max_unacked_packets;
82 conn->c_unacked_bytes = rds_sysctl_max_unacked_bytes;
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);
90 list_splice_init(&conn->c_retrans, &conn->c_send_queue);
91 spin_unlock_irqrestore(&conn->c_lock, flags);
95 * We're making the concious trade-off here to only send one message
96 * down the connection at a time.
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
103 * - queued acks can be delayed behind large messages
105 * - small message latency is higher behind queued large messages
106 * - large message latency isn't starved by intervening small sends
108 int rds_send_xmit(struct rds_connection *conn)
110 struct rds_message *rm;
113 unsigned int send_quota = send_batch_count;
114 struct scatterlist *sg;
117 LIST_HEAD(to_be_dropped);
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.
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
130 if (!mutex_trylock(&conn->c_send_lock)) {
131 rds_stats_inc(s_send_sem_contention);
136 if (conn->c_trans->xmit_prepare)
137 conn->c_trans->xmit_prepare(conn);
140 * spin trying to push headers and data down the connection until
141 * the connection doesn't make forward progress.
143 while (--send_quota) {
145 rm = conn->c_xmit_rm;
148 * If between sending messages, we can send a pending congestion
151 if (!rm && test_and_clear_bit(0, &conn->c_map_queued)) {
152 rm = rds_cong_update_alloc(conn);
157 rm->data.op_active = 1;
159 conn->c_xmit_rm = rm;
163 * If not already working on one, grab the next message.
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.
172 spin_lock_irqsave(&conn->c_lock, flags);
174 if (!list_empty(&conn->c_send_queue)) {
175 rm = list_entry(conn->c_send_queue.next,
178 rds_message_addref(rm);
181 * Move the message from the send queue to the retransmit
184 list_move_tail(&rm->m_conn_item, &conn->c_retrans);
187 spin_unlock_irqrestore(&conn->c_lock, flags);
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
199 * Therefore, we never retransmit messages with RDMA ops.
201 if (rm->rdma.op_active &&
202 test_bit(RDS_MSG_RETRANSMITTED, &rm->m_flags)) {
203 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);
211 /* Require an ACK every once in a while */
212 len = ntohl(rm->m_inc.i_hdr.h_len);
213 if (conn->c_unacked_packets == 0 ||
214 conn->c_unacked_bytes < len) {
215 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
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);
221 conn->c_unacked_bytes -= len;
222 conn->c_unacked_packets--;
225 conn->c_xmit_rm = rm;
228 /* The transport either sends the whole rdma or none of it */
229 if (rm->rdma.op_active && !conn->c_xmit_rdma_sent) {
230 rm->m_final_op = &rm->rdma;
231 ret = conn->c_trans->xmit_rdma(conn, &rm->rdma);
234 conn->c_xmit_rdma_sent = 1;
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);
241 if (rm->atomic.op_active && !conn->c_xmit_atomic_sent) {
242 rm->m_final_op = &rm->atomic;
243 ret = conn->c_trans->xmit_atomic(conn, &rm->atomic);
246 conn->c_xmit_atomic_sent = 1;
248 /* 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);
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.
260 if (rm->data.op_nents == 0) {
262 int all_ops_are_silent = 1;
264 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;
270 if (ops_present && all_ops_are_silent
271 && !rm->m_rdma_cookie)
272 rm->data.op_active = 0;
275 if (rm->data.op_active && !conn->c_xmit_data_sent) {
276 rm->m_final_op = &rm->data;
277 ret = conn->c_trans->xmit(conn, rm,
278 conn->c_xmit_hdr_off,
280 conn->c_xmit_data_off);
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;
292 sg = &rm->data.op_sg[conn->c_xmit_sg];
294 tmp = min_t(int, ret, sg->length -
295 conn->c_xmit_data_off);
296 conn->c_xmit_data_off += tmp;
298 if (conn->c_xmit_data_off == sg->length) {
299 conn->c_xmit_data_off = 0;
303 conn->c_xmit_sg == rm->data.op_nents);
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;
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.
317 if (!rm->data.op_active || conn->c_xmit_data_sent) {
318 conn->c_xmit_rm = NULL;
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;
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);
334 if (conn->c_trans->xmit_complete)
335 conn->c_trans->xmit_complete(conn);
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.
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.
349 mutex_unlock(&conn->c_send_lock);
351 if (send_quota == 0 && !was_empty) {
352 /* We exhausted the send quota, but there's work left to
353 * do. Return and (re-)schedule the send worker.
358 if (ret == 0 && was_empty) {
359 /* A simple bit test would be way faster than taking the
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);
366 spin_unlock_irqrestore(&conn->c_lock, flags);
372 static void rds_send_sndbuf_remove(struct rds_sock *rs, struct rds_message *rm)
374 u32 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
376 assert_spin_locked(&rs->rs_lock);
378 BUG_ON(rs->rs_snd_bytes < len);
379 rs->rs_snd_bytes -= len;
381 if (rs->rs_snd_bytes == 0)
382 rds_stats_inc(s_send_queue_empty);
385 static inline int rds_send_is_acked(struct rds_message *rm, u64 ack,
386 is_acked_func is_acked)
389 return is_acked(rm, ack);
390 return be64_to_cpu(rm->m_inc.i_hdr.h_sequence) <= ack;
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
398 int rds_send_acked_before(struct rds_connection *conn, u64 seq)
400 struct rds_message *rm, *tmp;
403 spin_lock(&conn->c_lock);
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)
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)
417 spin_unlock(&conn->c_lock);
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
428 void rds_rdma_send_complete(struct rds_message *rm, int status)
430 struct rds_sock *rs = NULL;
431 struct rm_rdma_op *ro;
432 struct rds_notifier *notifier;
435 spin_lock_irqsave(&rm->m_rs_lock, flags);
438 if (test_bit(RDS_MSG_ON_SOCK, &rm->m_flags) &&
439 ro->op_active && ro->op_notify && ro->op_notifier) {
440 notifier = ro->op_notifier;
442 sock_hold(rds_rs_to_sk(rs));
444 notifier->n_status = status;
445 spin_lock(&rs->rs_lock);
446 list_add_tail(¬ifier->n_list, &rs->rs_notify_queue);
447 spin_unlock(&rs->rs_lock);
449 ro->op_notifier = NULL;
452 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
455 rds_wake_sk_sleep(rs);
456 sock_put(rds_rs_to_sk(rs));
459 EXPORT_SYMBOL_GPL(rds_rdma_send_complete);
462 * Just like above, except looks at atomic op
464 void rds_atomic_send_complete(struct rds_message *rm, int status)
466 struct rds_sock *rs = NULL;
467 struct rm_atomic_op *ao;
468 struct rds_notifier *notifier;
470 spin_lock(&rm->m_rs_lock);
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;
477 sock_hold(rds_rs_to_sk(rs));
479 notifier->n_status = status;
480 spin_lock(&rs->rs_lock);
481 list_add_tail(¬ifier->n_list, &rs->rs_notify_queue);
482 spin_unlock(&rs->rs_lock);
484 ao->op_notifier = NULL;
487 spin_unlock(&rm->m_rs_lock);
490 rds_wake_sk_sleep(rs);
491 sock_put(rds_rs_to_sk(rs));
494 EXPORT_SYMBOL_GPL(rds_atomic_send_complete);
497 * 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.
502 __rds_send_complete(struct rds_sock *rs, struct rds_message *rm, int status)
504 struct rm_rdma_op *ro;
505 struct rm_atomic_op *ao;
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;
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;
521 /* No need to wake the app - caller does this */
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.
529 struct rds_message *rds_send_get_message(struct rds_connection *conn,
530 struct rm_rdma_op *op)
532 struct rds_message *rm, *tmp, *found = NULL;
535 spin_lock_irqsave(&conn->c_lock, flags);
537 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
538 if (&rm->rdma == op) {
539 atomic_inc(&rm->m_refcount);
545 list_for_each_entry_safe(rm, tmp, &conn->c_send_queue, m_conn_item) {
546 if (&rm->rdma == op) {
547 atomic_inc(&rm->m_refcount);
554 spin_unlock_irqrestore(&conn->c_lock, flags);
558 EXPORT_SYMBOL_GPL(rds_send_get_message);
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.
568 void rds_send_remove_from_sock(struct list_head *messages, int status)
571 struct rds_sock *rs = NULL;
572 struct rds_message *rm;
574 while (!list_empty(messages)) {
577 rm = list_entry(messages->next, struct rds_message,
579 list_del_init(&rm->m_conn_item);
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.
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.
591 spin_lock_irqsave(&rm->m_rs_lock, flags);
592 if (!test_bit(RDS_MSG_ON_SOCK, &rm->m_flags))
593 goto unlock_and_drop;
595 if (rs != rm->m_rs) {
597 rds_wake_sk_sleep(rs);
598 sock_put(rds_rs_to_sk(rs));
601 sock_hold(rds_rs_to_sk(rs));
603 spin_lock(&rs->rs_lock);
605 if (test_and_clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags)) {
606 struct rm_rdma_op *ro = &rm->rdma;
607 struct rds_notifier *notifier;
609 list_del_init(&rm->m_sock_item);
610 rds_send_sndbuf_remove(rs, rm);
612 if (ro->op_active && ro->op_notifier &&
613 (ro->op_notify || (ro->op_recverr && status))) {
614 notifier = ro->op_notifier;
615 list_add_tail(¬ifier->n_list,
616 &rs->rs_notify_queue);
617 if (!notifier->n_status)
618 notifier->n_status = status;
619 rm->rdma.op_notifier = NULL;
624 spin_unlock(&rs->rs_lock);
627 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
634 rds_wake_sk_sleep(rs);
635 sock_put(rds_rs_to_sk(rs));
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.
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.
650 void rds_send_drop_acked(struct rds_connection *conn, u64 ack,
651 is_acked_func is_acked)
653 struct rds_message *rm, *tmp;
657 spin_lock_irqsave(&conn->c_lock, flags);
659 list_for_each_entry_safe(rm, tmp, &conn->c_retrans, m_conn_item) {
660 if (!rds_send_is_acked(rm, ack, is_acked))
663 list_move(&rm->m_conn_item, &list);
664 clear_bit(RDS_MSG_ON_CONN, &rm->m_flags);
667 /* order flag updates with spin locks */
668 if (!list_empty(&list))
669 smp_mb__after_clear_bit();
671 spin_unlock_irqrestore(&conn->c_lock, flags);
673 /* now remove the messages from the sock list as needed */
674 rds_send_remove_from_sock(&list, RDS_RDMA_SUCCESS);
676 EXPORT_SYMBOL_GPL(rds_send_drop_acked);
678 void rds_send_drop_to(struct rds_sock *rs, struct sockaddr_in *dest)
680 struct rds_message *rm, *tmp;
681 struct rds_connection *conn;
685 /* get all the messages we're dropping under the rs lock */
686 spin_lock_irqsave(&rs->rs_lock, flags);
688 list_for_each_entry_safe(rm, tmp, &rs->rs_send_queue, m_sock_item) {
689 if (dest && (dest->sin_addr.s_addr != rm->m_daddr ||
690 dest->sin_port != rm->m_inc.i_hdr.h_dport))
693 list_move(&rm->m_sock_item, &list);
694 rds_send_sndbuf_remove(rs, rm);
695 clear_bit(RDS_MSG_ON_SOCK, &rm->m_flags);
698 /* order flag updates with the rs lock */
699 smp_mb__after_clear_bit();
701 spin_unlock_irqrestore(&rs->rs_lock, flags);
703 if (list_empty(&list))
706 /* Remove the messages from the conn */
707 list_for_each_entry(rm, &list, m_sock_item) {
709 conn = rm->m_inc.i_conn;
711 spin_lock_irqsave(&conn->c_lock, flags);
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.
717 if (!test_and_clear_bit(RDS_MSG_ON_CONN, &rm->m_flags)) {
718 spin_unlock_irqrestore(&conn->c_lock, flags);
721 list_del_init(&rm->m_conn_item);
722 spin_unlock_irqrestore(&conn->c_lock, flags);
725 * Couldn't grab m_rs_lock in top loop (lock ordering),
728 spin_lock_irqsave(&rm->m_rs_lock, flags);
730 spin_lock(&rs->rs_lock);
731 __rds_send_complete(rs, rm, RDS_RDMA_CANCELED);
732 spin_unlock(&rs->rs_lock);
735 spin_unlock_irqrestore(&rm->m_rs_lock, flags);
740 rds_wake_sk_sleep(rs);
742 while (!list_empty(&list)) {
743 rm = list_entry(list.next, struct rds_message, m_sock_item);
744 list_del_init(&rm->m_sock_item);
746 rds_message_wait(rm);
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.
756 static 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)
766 len = be32_to_cpu(rm->m_inc.i_hdr.h_len);
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);
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.
780 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) {
781 rs->rs_snd_bytes += len;
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.
788 if (rs->rs_snd_bytes >= rds_sk_sndbuf(rs) / 2)
789 __set_bit(RDS_MSG_ACK_REQUIRED, &rm->m_flags);
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);
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);
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);
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));
815 spin_unlock_irqrestore(&rs->rs_lock, flags);
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.
824 static int rds_rm_size(struct msghdr *msg, int data_len)
826 struct cmsghdr *cmsg;
831 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
832 if (!CMSG_OK(msg, cmsg))
835 if (cmsg->cmsg_level != SOL_RDS)
838 switch (cmsg->cmsg_type) {
839 case RDS_CMSG_RDMA_ARGS:
841 retval = rds_rdma_extra_size(CMSG_DATA(cmsg));
848 case RDS_CMSG_RDMA_DEST:
849 case RDS_CMSG_RDMA_MAP:
851 /* these are valid but do no add any size */
854 case RDS_CMSG_ATOMIC_CSWP:
855 case RDS_CMSG_ATOMIC_FADD:
857 size += sizeof(struct scatterlist);
866 size += ceil(data_len, PAGE_SIZE) * sizeof(struct scatterlist);
868 /* Ensure (DEST, MAP) are never used with (ARGS, ATOMIC) */
869 if (cmsg_groups == 3)
875 static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
876 struct msghdr *msg, int *allocated_mr)
878 struct cmsghdr *cmsg;
881 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
882 if (!CMSG_OK(msg, cmsg))
885 if (cmsg->cmsg_level != SOL_RDS)
888 /* As a side effect, RDMA_DEST and RDMA_MAP will set
889 * rm->rdma.m_rdma_cookie and rm->rdma.m_rdma_mr.
891 switch (cmsg->cmsg_type) {
892 case RDS_CMSG_RDMA_ARGS:
893 ret = rds_cmsg_rdma_args(rs, rm, cmsg);
896 case RDS_CMSG_RDMA_DEST:
897 ret = rds_cmsg_rdma_dest(rs, rm, cmsg);
900 case RDS_CMSG_RDMA_MAP:
901 ret = rds_cmsg_rdma_map(rs, rm, cmsg);
905 case RDS_CMSG_ATOMIC_CSWP:
906 case RDS_CMSG_ATOMIC_FADD:
907 ret = rds_cmsg_atomic(rs, rm, cmsg);
921 int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
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;
929 struct rds_message *rm = NULL;
930 struct rds_connection *conn;
932 int queued = 0, allocated_mr = 0;
933 int nonblock = msg->msg_flags & MSG_DONTWAIT;
934 long timeo = sock_sndtimeo(sk, nonblock);
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);
944 if (msg->msg_namelen) {
945 /* XXX fail non-unicast destination IPs? */
946 if (msg->msg_namelen < sizeof(*usin) || usin->sin_family != AF_INET) {
950 daddr = usin->sin_addr.s_addr;
951 dport = usin->sin_port;
953 /* We only care about consistency with ->connect() */
955 daddr = rs->rs_conn_addr;
956 dport = rs->rs_conn_port;
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 */
966 /* size of rm including all sgs */
967 ret = rds_rm_size(msg, payload_len);
971 rm = rds_message_alloc(ret, GFP_KERNEL);
977 /* Attach data to the rm */
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);
984 rm->data.op_active = 1;
988 /* 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)
993 conn = rds_conn_create_outgoing(rs->rs_bound_addr, daddr,
995 sock->sk->sk_allocation);
1003 /* Parse any control messages the user may have included. */
1004 ret = rds_cmsg_send(rs, rm, msg, &allocated_mr);
1008 if (rm->rdma.op_active && !conn->c_trans->xmit_rdma) {
1009 if (printk_ratelimit())
1010 printk(KERN_NOTICE "rdma_op %p conn xmit_rdma %p\n",
1011 &rm->rdma, conn->c_trans->xmit_rdma);
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);
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.
1028 if (rds_conn_state(conn) == RDS_CONN_DOWN &&
1029 !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
1030 queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
1032 ret = rds_cong_wait(conn->c_fcong, dport, nonblock, rs);
1034 rs->rs_seen_congestion = 1;
1038 while (!rds_send_queue_rm(rs, conn, rm, rs->rs_bound_port,
1040 rds_stats_inc(s_send_queue_full);
1041 /* XXX make sure this is reasonable */
1042 if (payload_len > rds_sk_sndbuf(rs)) {
1051 timeo = wait_event_interruptible_timeout(*sk_sleep(sk),
1052 rds_send_queue_rm(rs, conn, rm,
1057 rdsdebug("sendmsg woke queued %d timeo %ld\n", queued, timeo);
1058 if (timeo > 0 || timeo == MAX_SCHEDULE_TIMEOUT)
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.
1071 rds_stats_inc(s_send_queued);
1073 if (!test_bit(RDS_LL_SEND_FULL, &conn->c_flags))
1074 rds_send_worker(&conn->c_send_w.work);
1076 rds_message_put(rm);
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 */
1084 rds_rdma_unuse(rs, rds_rdma_cookie_key(rm->m_rdma_cookie), 1);
1087 rds_message_put(rm);
1092 * Reply to a ping packet.
1095 rds_send_pong(struct rds_connection *conn, __be16 dport)
1097 struct rds_message *rm;
1098 unsigned long flags;
1101 rm = rds_message_alloc(0, GFP_ATOMIC);
1107 rm->m_daddr = conn->c_faddr;
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.
1113 if (rds_conn_state(conn) == RDS_CONN_DOWN &&
1114 !test_and_set_bit(RDS_RECONNECT_PENDING, &conn->c_flags))
1115 queue_delayed_work(rds_wq, &conn->c_conn_w, 0);
1117 ret = rds_cong_wait(conn->c_fcong, dport, 1, NULL);
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;
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);
1132 rds_stats_inc(s_send_queued);
1133 rds_stats_inc(s_send_pong);
1135 queue_delayed_work(rds_wq, &conn->c_send_w, 0);
1136 rds_message_put(rm);
1141 rds_message_put(rm);