/* * Copyright (c) 2006, 2019 Oracle and/or its affiliates. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *
*/ #include <linux/kernel.h> #include <linux/sched/clock.h> #include <linux/slab.h> #include <linux/pci.h> #include <linux/dma-mapping.h> #include <rdma/rdma_cm.h>
/* * The entire 'from' list, including the from element itself, is put on * to the tail of the 'to' list.
*/ staticvoid list_splice_entire_tail(struct list_head *from, struct list_head *to)
{ struct list_head *from_last = from->prev;
int rds_ib_recv_alloc_caches(struct rds_ib_connection *ic, gfp_t gfp)
{ int ret;
ret = rds_ib_recv_alloc_cache(&ic->i_cache_incs, gfp); if (!ret) {
ret = rds_ib_recv_alloc_cache(&ic->i_cache_frags, gfp); if (ret)
free_percpu(ic->i_cache_incs.percpu);
}
if (!ic->i_cache_incs.ready)
rds_ib_cache_xfer_to_ready(&ic->i_cache_incs); if (!ic->i_cache_frags.ready)
rds_ib_cache_xfer_to_ready(&ic->i_cache_frags);
/* * ibinc was taken from recv if recv contained the start of a message. * recvs that were continuations will still have this allocated.
*/ if (!recv->r_ibinc) {
recv->r_ibinc = rds_ib_refill_one_inc(ic, slab_mask); if (!recv->r_ibinc) goto out;
}
/* We don't use wait_on_bit()/wake_up_bit() because our waking is in a * hot path and finding waiters is very rare. We don't want to walk * the system-wide hashed waitqueue buckets in the fast path only to * almost never find waiters.
*/ if (waitqueue_active(&conn->c_waitq))
wake_up_all(&conn->c_waitq);
}
/* * This tries to allocate and post unused work requests after making sure that * they have all the allocations they need to queue received fragments into * sockets.
*/ void rds_ib_recv_refill(struct rds_connection *conn, int prefill, gfp_t gfp)
{ struct rds_ib_connection *ic = conn->c_transport_data; struct rds_ib_recv_work *recv; unsignedint posted = 0; int ret = 0; bool can_wait = !!(gfp & __GFP_DIRECT_RECLAIM); bool must_wake = false;
u32 pos;
/* the goal here is to just make sure that someone, somewhere * is posting buffers. If we can't get the refill lock, * let them do their thing
*/ if (!acquire_refill(conn)) return;
while ((prefill || rds_conn_up(conn)) &&
rds_ib_ring_alloc(&ic->i_recv_ring, 1, &pos)) { if (pos >= ic->i_recv_ring.w_nr) {
printk(KERN_NOTICE "Argh - ring alloc returned pos=%u\n",
pos); break;
}
recv = &ic->i_recvs[pos];
ret = rds_ib_recv_refill_one(conn, recv, gfp); if (ret) {
must_wake = true; break;
}
/* XXX when can this fail? */
ret = ib_post_recv(ic->i_cm_id->qp, &recv->r_wr, NULL); if (ret) {
rds_ib_conn_error(conn, "recv post on " "%pI6c returned %d, disconnecting and " "reconnecting\n", &conn->c_faddr,
ret); break;
}
/* We're doing flow control - update the window. */ if (ic->i_flowctl && posted)
rds_ib_advertise_credits(conn, posted);
if (ret)
rds_ib_ring_unalloc(&ic->i_recv_ring, 1);
release_refill(conn);
/* if we're called from the softirq handler, we'll be GFP_NOWAIT. * in this case the ring being low is going to lead to more interrupts * and we can safely let the softirq code take care of it unless the * ring is completely empty. * * if we're called from krdsd, we'll be GFP_KERNEL. In this case * we might have raced with the softirq code while we had the refill * lock held. Use rds_ib_ring_low() instead of ring_empty to decide * if we should requeue.
*/ if (rds_conn_up(conn) &&
(must_wake ||
(can_wait && rds_ib_ring_low(&ic->i_recv_ring)) ||
rds_ib_ring_empty(&ic->i_recv_ring))) {
queue_delayed_work(rds_wq, &conn->c_recv_w, 1);
} if (can_wait)
cond_resched();
}
/* * We want to recycle several types of recv allocations, like incs and frags. * To use this, the *_free() function passes in the ptr to a list_head within * the recyclee, as well as the cache to put it on. * * First, we put the memory on a percpu list. When this reaches a certain size, * We move it to an intermediate non-percpu list in a lockless manner, with some * xchg/compxchg wizardry. * * N.B. Instead of a list_head as the anchor, we use a single pointer, which can * be NULL and xchg'd. The list is actually empty when the pointer is NULL, and * list_empty() will return true with one element is actually present.
*/ staticvoid rds_ib_recv_cache_put(struct list_head *new_item, struct rds_ib_refill_cache *cache)
{ unsignedlong flags; struct list_head *old, *chpfirst;
local_irq_save(flags);
chpfirst = __this_cpu_read(cache->percpu->first); if (!chpfirst)
INIT_LIST_HEAD(new_item); else/* put on front */
list_add_tail(new_item, chpfirst);
if (__this_cpu_read(cache->percpu->count) < RDS_IB_RECYCLE_BATCH_COUNT) goto end;
/* * Return our per-cpu first list to the cache's xfer by atomically * grabbing the current xfer list, appending it to our per-cpu list, * and then atomically returning that entire list back to the * cache's xfer list as long as it's still empty.
*/ do {
old = xchg(&cache->xfer, NULL); if (old)
list_splice_entire_tail(old, chpfirst);
old = cmpxchg(&cache->xfer, NULL, chpfirst);
} while (old);
/* * You'd think that with reliable IB connections you wouldn't need to ack * messages that have been received. The problem is that IB hardware generates * an ack message before it has DMAed the message into memory. This creates a * potential message loss if the HCA is disabled for any reason between when it * sends the ack and before the message is DMAed and processed. This is only a * potential issue if another HCA is available for fail-over. * * When the remote host receives our ack they'll free the sent message from * their send queue. To decrease the latency of this we always send an ack * immediately after we've received messages. * * For simplicity, we only have one ack in flight at a time. This puts * pressure on senders to have deep enough send queues to absorb the latency of * a single ack frame being in flight. This might not be good enough. * * This is implemented by have a long-lived send_wr and sge which point to a * statically allocated ack frame. This ack wr does not fall under the ring * accounting that the tx and rx wrs do. The QP attribute specifically makes * room for it beyond the ring size. Send completion notices its special * wr_id and avoids working with the ring in that case.
*/ #ifndef KERNEL_HAS_ATOMIC64 void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq, int ack_required)
{ unsignedlong flags;
ret = ib_post_send(ic->i_cm_id->qp, &ic->i_ack_wr, NULL); if (unlikely(ret)) { /* Failed to send. Release the WR, and * force another ACK.
*/
clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
/* * There are 3 ways of getting acknowledgements to the peer: * 1. We call rds_ib_attempt_ack from the recv completion handler * to send an ACK-only frame. * However, there can be only one such frame in the send queue * at any time, so we may have to postpone it. * 2. When another (data) packet is transmitted while there's * an ACK in the queue, we piggyback the ACK sequence number * on the data packet. * 3. If the ACK WR is done sending, we get called from the * send queue completion handler, and check whether there's * another ACK pending (postponed because the WR was on the * queue). If so, we transmit it. * * We maintain 2 variables: * - i_ack_flags, which keeps track of whether the ACK WR * is currently in the send queue or not (IB_ACK_IN_FLIGHT) * - i_ack_next, which is the last sequence number we received * * Potentially, send queue and receive queue handlers can run concurrently. * It would be nice to not have to use a spinlock to synchronize things, * but the one problem that rules this out is that 64bit updates are * not atomic on all platforms. Things would be a lot simpler if * we had atomic64 or maybe cmpxchg64 everywhere. * * Reconnecting complicates this picture just slightly. When we * reconnect, we may be seeing duplicate packets. The peer * is retransmitting them, because it hasn't seen an ACK for * them. It is important that we ACK these. * * ACK mitigation adds a header flag "ACK_REQUIRED"; any packet with * this flag set *MUST* be acknowledged immediately.
*/
/* * When we get here, we're called from the recv queue handler. * Check whether we ought to transmit an ACK.
*/ void rds_ib_attempt_ack(struct rds_ib_connection *ic)
{ unsignedint adv_credits;
if (!test_bit(IB_ACK_REQUESTED, &ic->i_ack_flags)) return;
if (test_and_set_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags)) {
rds_ib_stats_inc(s_ib_ack_send_delayed); return;
}
/* Can we get a send credit? */ if (!rds_ib_send_grab_credits(ic, 1, &adv_credits, 0, RDS_MAX_ADV_CREDIT)) {
rds_ib_stats_inc(s_ib_tx_throttle);
clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags); return;
}
/* * We get here from the send completion handler, when the * adapter tells us the ACK frame was sent.
*/ void rds_ib_ack_send_complete(struct rds_ib_connection *ic)
{
clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
rds_ib_attempt_ack(ic);
}
/* * This is called by the regular xmit code when it wants to piggyback * an ACK on an outgoing frame.
*/
u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic)
{ if (test_and_clear_bit(IB_ACK_REQUESTED, &ic->i_ack_flags))
rds_ib_stats_inc(s_ib_ack_send_piggybacked); return rds_ib_get_ack(ic);
}
/* * It's kind of lame that we're copying from the posted receive pages into * long-lived bitmaps. We could have posted the bitmaps and rdma written into * them. But receiving new congestion bitmaps should be a *rare* event, so * hopefully we won't need to invest that complexity in making it more * efficient. By copying we can share a simpler core with TCP which has to * copy.
*/ staticvoid rds_ib_cong_recv(struct rds_connection *conn, struct rds_ib_incoming *ibinc)
{ struct rds_cong_map *map; unsignedint map_off; unsignedint map_page; struct rds_page_frag *frag; unsignedlong frag_off; unsignedlong to_copy; unsignedlong copied;
__le64 uncongested = 0; void *addr;
if (data_len < sizeof(struct rds_header)) {
rds_ib_conn_error(conn, "incoming message " "from %pI6c didn't include a " "header, disconnecting and " "reconnecting\n",
&conn->c_faddr); return;
}
data_len -= sizeof(struct rds_header);
ihdr = ic->i_recv_hdrs[recv - ic->i_recvs];
ib_dma_sync_single_for_cpu(ic->rds_ibdev->dev, dma_addr, sizeof(*ihdr), DMA_FROM_DEVICE); /* Validate the checksum. */ if (!rds_message_verify_checksum(ihdr)) {
rds_ib_conn_error(conn, "incoming message " "from %pI6c has corrupted header - " "forcing a reconnect\n",
&conn->c_faddr);
rds_stats_inc(s_recv_drop_bad_checksum); goto done;
}
/* Process the ACK sequence which comes with every packet */
state->ack_recv = be64_to_cpu(ihdr->h_ack);
state->ack_recv_valid = 1;
/* Process the credits update if there was one */ if (ihdr->h_credit)
rds_ib_send_add_credits(conn, ihdr->h_credit);
if (ihdr->h_sport == 0 && ihdr->h_dport == 0 && data_len == 0) { /* This is an ACK-only packet. The fact that it gets * special treatment here is that historically, ACKs * were rather special beasts.
*/
rds_ib_stats_inc(s_ib_ack_received);
/* * Usually the frags make their way on to incs and are then freed as * the inc is freed. We don't go that route, so we have to drop the * page ref ourselves. We can't just leave the page on the recv * because that confuses the dma mapping of pages and each recv's use * of a partial page. * * FIXME: Fold this into the code path below.
*/
rds_ib_frag_free(ic, recv->r_frag);
recv->r_frag = NULL; goto done;
}
/* * If we don't already have an inc on the connection then this * fragment has a header and starts a message.. copy its header * into the inc and save the inc so we can hang upcoming fragments * off its list.
*/ if (!ibinc) {
ibinc = recv->r_ibinc;
recv->r_ibinc = NULL;
ic->i_ibinc = ibinc;
/* Evaluate the ACK_REQUIRED flag *after* we received * the complete frame, and after bumping the next_rx
* sequence. */ if (hdr->h_flags & RDS_FLAG_ACK_REQUIRED) {
rds_stats_inc(s_recv_ack_required);
state->ack_required = 1;
}
/* Also process recvs in connecting state because it is possible * to get a recv completion _before_ the rdmacm ESTABLISHED * event is processed.
*/ if (wc->status == IB_WC_SUCCESS) {
rds_ib_process_recv(conn, recv, wc->byte_len, state);
} else { /* We expect errors as the qp is drained during shutdown */ if (rds_conn_up(conn) || rds_conn_connecting(conn))
rds_ib_conn_error(conn, "recv completion on <%pI6c,%pI6c, %d> had status %u (%s), vendor err 0x%x, disconnecting and reconnecting\n",
&conn->c_laddr, &conn->c_faddr,
conn->c_tos, wc->status,
ib_wc_status_msg(wc->status),
wc->vendor_err);
}
/* rds_ib_process_recv() doesn't always consume the frag, and * we might not have called it at all if the wc didn't indicate * success. We already unmapped the frag's pages, though, and * the following rds_ib_ring_free() call tells the refill path * that it will not find an allocated frag here. Make sure we * keep that promise by freeing a frag that's still on the ring.
*/ if (recv->r_frag) {
rds_ib_frag_free(ic, recv->r_frag);
recv->r_frag = NULL;
}
rds_ib_ring_free(&ic->i_recv_ring, 1);
/* If we ever end up with a really empty receive ring, we're * in deep trouble, as the sender will definitely see RNR
* timeouts. */ if (rds_ib_ring_empty(&ic->i_recv_ring))
rds_ib_stats_inc(s_ib_rx_ring_empty);
int rds_ib_recv_init(void)
{ struct sysinfo si; int ret = -ENOMEM;
/* Default to 30% of all available RAM for recv memory */
si_meminfo(&si);
rds_ib_sysctl_max_recv_allocation = si.totalram / 3 * PAGE_SIZE / RDS_FRAG_SIZE;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.