Skip to content

Commit 248f219

Browse files
committed
rxrpc: Rewrite the data and ack handling code
Rewrite the data and ack handling code such that: (1) Parsing of received ACK and ABORT packets and the distribution and the filing of DATA packets happens entirely within the data_ready context called from the UDP socket. This allows us to process and discard ACK and ABORT packets much more quickly (they're no longer stashed on a queue for a background thread to process). (2) We avoid calling skb_clone(), pskb_pull() and pskb_trim(). We instead keep track of the offset and length of the content of each packet in the sk_buff metadata. This means we don't do any allocation in the receive path. (3) Jumbo DATA packet parsing is now done in data_ready context. Rather than cloning the packet once for each subpacket and pulling/trimming it, we file the packet multiple times with an annotation for each indicating which subpacket is there. From that we can directly calculate the offset and length. (4) A call's receive queue can be accessed without taking locks (memory barriers do have to be used, though). (5) Incoming calls are set up from preallocated resources and immediately made live. They can than have packets queued upon them and ACKs generated. If insufficient resources exist, DATA packet #1 is given a BUSY reply and other DATA packets are discarded). (6) sk_buffs no longer take a ref on their parent call. To make this work, the following changes are made: (1) Each call's receive buffer is now a circular buffer of sk_buff pointers (rxtx_buffer) rather than a number of sk_buff_heads spread between the call and the socket. This permits each sk_buff to be in the buffer multiple times. The receive buffer is reused for the transmit buffer. (2) A circular buffer of annotations (rxtx_annotations) is kept parallel to the data buffer. Transmission phase annotations indicate whether a buffered packet has been ACK'd or not and whether it needs retransmission. Receive phase annotations indicate whether a slot holds a whole packet or a jumbo subpacket and, if the latter, which subpacket. They also note whether the packet has been decrypted in place. (3) DATA packet window tracking is much simplified. Each phase has just two numbers representing the window (rx_hard_ack/rx_top and tx_hard_ack/tx_top). The hard_ack number is the sequence number before base of the window, representing the last packet the other side says it has consumed. hard_ack starts from 0 and the first packet is sequence number 1. The top number is the sequence number of the highest-numbered packet residing in the buffer. Packets between hard_ack+1 and top are soft-ACK'd to indicate they've been received, but not yet consumed. Four macros, before(), before_eq(), after() and after_eq() are added to compare sequence numbers within the window. This allows for the top of the window to wrap when the hard-ack sequence number gets close to the limit. Two flags, RXRPC_CALL_RX_LAST and RXRPC_CALL_TX_LAST, are added also to indicate when rx_top and tx_top point at the packets with the LAST_PACKET bit set, indicating the end of the phase. (4) Calls are queued on the socket 'receive queue' rather than packets. This means that we don't need have to invent dummy packets to queue to indicate abnormal/terminal states and we don't have to keep metadata packets (such as ABORTs) around (5) The offset and length of a (sub)packet's content are now passed to the verify_packet security op. This is currently expected to decrypt the packet in place and validate it. However, there's now nowhere to store the revised offset and length of the actual data within the decrypted blob (there may be a header and padding to skip) because an sk_buff may represent multiple packets, so a locate_data security op is added to retrieve these details from the sk_buff content when needed. (6) recvmsg() now has to handle jumbo subpackets, where each subpacket is individually secured and needs to be individually decrypted. The code to do this is broken out into rxrpc_recvmsg_data() and shared with the kernel API. It now iterates over the call's receive buffer rather than walking the socket receive queue. Additional changes: (1) The timers are condensed to a single timer that is set for the soonest of three timeouts (delayed ACK generation, DATA retransmission and call lifespan). (2) Transmission of ACK and ABORT packets is effected immediately from process-context socket ops/kernel API calls that cause them instead of them being punted off to a background work item. The data_ready handler still has to defer to the background, though. (3) A shutdown op is added to the AF_RXRPC socket so that the AFS filesystem can shut down the socket and flush its own work items before closing the socket to deal with any in-progress service calls. Future additional changes that will need to be considered: (1) Make sure that a call doesn't hog the front of the queue by receiving data from the network as fast as userspace is consuming it to the exclusion of other calls. (2) Transmit delayed ACKs from within recvmsg() when we've consumed sufficiently more packets to avoid the background work item needing to run. Signed-off-by: David Howells <dhowells@redhat.com>
1 parent 00e9071 commit 248f219

24 files changed

+1993
-3337
lines changed

fs/afs/rxrpc.c

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ static const struct afs_call_type afs_RXCMxxxx = {
5555
.abort_to_error = afs_abort_to_error,
5656
};
5757

58-
static void afs_collect_incoming_call(struct work_struct *);
5958
static void afs_charge_preallocation(struct work_struct *);
6059

61-
static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
6260
static DECLARE_WORK(afs_charge_preallocation_work, afs_charge_preallocation);
6361

6462
static int afs_wait_atomic_t(atomic_t *p)
@@ -143,6 +141,8 @@ void afs_close_socket(void)
143141
TASK_UNINTERRUPTIBLE);
144142
_debug("no outstanding calls");
145143

144+
flush_workqueue(afs_async_calls);
145+
kernel_sock_shutdown(afs_socket, SHUT_RDWR);
146146
flush_workqueue(afs_async_calls);
147147
sock_release(afs_socket);
148148

@@ -602,51 +602,6 @@ static void afs_process_async_call(struct work_struct *work)
602602
_leave("");
603603
}
604604

605-
/*
606-
* accept the backlog of incoming calls
607-
*/
608-
static void afs_collect_incoming_call(struct work_struct *work)
609-
{
610-
struct rxrpc_call *rxcall;
611-
struct afs_call *call = NULL;
612-
613-
_enter("");
614-
615-
do {
616-
if (!call) {
617-
call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
618-
if (!call) {
619-
rxrpc_kernel_reject_call(afs_socket);
620-
return;
621-
}
622-
623-
INIT_WORK(&call->async_work, afs_process_async_call);
624-
call->wait_mode = &afs_async_incoming_call;
625-
call->type = &afs_RXCMxxxx;
626-
init_waitqueue_head(&call->waitq);
627-
call->state = AFS_CALL_AWAIT_OP_ID;
628-
629-
_debug("CALL %p{%s} [%d]",
630-
call, call->type->name,
631-
atomic_read(&afs_outstanding_calls));
632-
atomic_inc(&afs_outstanding_calls);
633-
}
634-
635-
rxcall = rxrpc_kernel_accept_call(afs_socket,
636-
(unsigned long)call,
637-
afs_wake_up_async_call);
638-
if (!IS_ERR(rxcall)) {
639-
call->rxcall = rxcall;
640-
call->need_attention = true;
641-
queue_work(afs_async_calls, &call->async_work);
642-
call = NULL;
643-
}
644-
} while (!call);
645-
646-
if (call)
647-
afs_free_call(call);
648-
}
649-
650605
static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
651606
{
652607
struct afs_call *call = (struct afs_call *)user_call_ID;
@@ -704,7 +659,7 @@ static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
704659
static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
705660
unsigned long user_call_ID)
706661
{
707-
queue_work(afs_wq, &afs_collect_incoming_call_work);
662+
atomic_inc(&afs_outstanding_calls);
708663
queue_work(afs_wq, &afs_charge_preallocation_work);
709664
}
710665

include/net/af_rxrpc.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ int rxrpc_kernel_recv_data(struct socket *, struct rxrpc_call *,
4242
void rxrpc_kernel_abort_call(struct socket *, struct rxrpc_call *,
4343
u32, int, const char *);
4444
void rxrpc_kernel_end_call(struct socket *, struct rxrpc_call *);
45-
struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *, unsigned long,
46-
rxrpc_notify_rx_t);
47-
int rxrpc_kernel_reject_call(struct socket *);
4845
void rxrpc_kernel_get_peer(struct socket *, struct rxrpc_call *,
4946
struct sockaddr_rxrpc *);
5047
int rxrpc_kernel_charge_accept(struct socket *, rxrpc_notify_rx_t,

include/rxrpc/packet.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ struct rxrpc_ackpacket {
133133

134134
} __packed;
135135

136+
/* Some ACKs refer to specific packets and some are general and can be updated. */
137+
#define RXRPC_ACK_UPDATEABLE ((1 << RXRPC_ACK_REQUESTED) | \
138+
(1 << RXRPC_ACK_PING_RESPONSE) | \
139+
(1 << RXRPC_ACK_DELAY) | \
140+
(1 << RXRPC_ACK_IDLE))
141+
142+
136143
/*
137144
* ACK packets can have a further piece of information tagged on the end
138145
*/

net/rxrpc/af_rxrpc.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
155155
}
156156

157157
if (rx->srx.srx_service) {
158-
write_lock_bh(&local->services_lock);
158+
write_lock(&local->services_lock);
159159
hlist_for_each_entry(prx, &local->services, listen_link) {
160160
if (prx->srx.srx_service == rx->srx.srx_service)
161161
goto service_in_use;
162162
}
163163

164164
rx->local = local;
165165
hlist_add_head_rcu(&rx->listen_link, &local->services);
166-
write_unlock_bh(&local->services_lock);
166+
write_unlock(&local->services_lock);
167167

168168
rx->sk.sk_state = RXRPC_SERVER_BOUND;
169169
} else {
@@ -176,7 +176,7 @@ static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
176176
return 0;
177177

178178
service_in_use:
179-
write_unlock_bh(&local->services_lock);
179+
write_unlock(&local->services_lock);
180180
rxrpc_put_local(local);
181181
ret = -EADDRINUSE;
182182
error_unlock:
@@ -515,15 +515,16 @@ static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
515515
static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
516516
poll_table *wait)
517517
{
518-
unsigned int mask;
519518
struct sock *sk = sock->sk;
519+
struct rxrpc_sock *rx = rxrpc_sk(sk);
520+
unsigned int mask;
520521

521522
sock_poll_wait(file, sk_sleep(sk), wait);
522523
mask = 0;
523524

524525
/* the socket is readable if there are any messages waiting on the Rx
525526
* queue */
526-
if (!skb_queue_empty(&sk->sk_receive_queue))
527+
if (!list_empty(&rx->recvmsg_q))
527528
mask |= POLLIN | POLLRDNORM;
528529

529530
/* the socket is writable if there is space to add new data to the
@@ -575,15 +576,51 @@ static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
575576
rx->calls = RB_ROOT;
576577

577578
INIT_HLIST_NODE(&rx->listen_link);
578-
INIT_LIST_HEAD(&rx->secureq);
579-
INIT_LIST_HEAD(&rx->acceptq);
579+
spin_lock_init(&rx->incoming_lock);
580+
INIT_LIST_HEAD(&rx->sock_calls);
581+
INIT_LIST_HEAD(&rx->to_be_accepted);
582+
INIT_LIST_HEAD(&rx->recvmsg_q);
583+
rwlock_init(&rx->recvmsg_lock);
580584
rwlock_init(&rx->call_lock);
581585
memset(&rx->srx, 0, sizeof(rx->srx));
582586

583587
_leave(" = 0 [%p]", rx);
584588
return 0;
585589
}
586590

591+
/*
592+
* Kill all the calls on a socket and shut it down.
593+
*/
594+
static int rxrpc_shutdown(struct socket *sock, int flags)
595+
{
596+
struct sock *sk = sock->sk;
597+
struct rxrpc_sock *rx = rxrpc_sk(sk);
598+
int ret = 0;
599+
600+
_enter("%p,%d", sk, flags);
601+
602+
if (flags != SHUT_RDWR)
603+
return -EOPNOTSUPP;
604+
if (sk->sk_state == RXRPC_CLOSE)
605+
return -ESHUTDOWN;
606+
607+
lock_sock(sk);
608+
609+
spin_lock_bh(&sk->sk_receive_queue.lock);
610+
if (sk->sk_state < RXRPC_CLOSE) {
611+
sk->sk_state = RXRPC_CLOSE;
612+
sk->sk_shutdown = SHUTDOWN_MASK;
613+
} else {
614+
ret = -ESHUTDOWN;
615+
}
616+
spin_unlock_bh(&sk->sk_receive_queue.lock);
617+
618+
rxrpc_discard_prealloc(rx);
619+
620+
release_sock(sk);
621+
return ret;
622+
}
623+
587624
/*
588625
* RxRPC socket destructor
589626
*/
@@ -623,9 +660,9 @@ static int rxrpc_release_sock(struct sock *sk)
623660
ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
624661

625662
if (!hlist_unhashed(&rx->listen_link)) {
626-
write_lock_bh(&rx->local->services_lock);
663+
write_lock(&rx->local->services_lock);
627664
hlist_del_rcu(&rx->listen_link);
628-
write_unlock_bh(&rx->local->services_lock);
665+
write_unlock(&rx->local->services_lock);
629666
}
630667

631668
/* try to flush out this socket */
@@ -678,7 +715,7 @@ static const struct proto_ops rxrpc_rpc_ops = {
678715
.poll = rxrpc_poll,
679716
.ioctl = sock_no_ioctl,
680717
.listen = rxrpc_listen,
681-
.shutdown = sock_no_shutdown,
718+
.shutdown = rxrpc_shutdown,
682719
.setsockopt = rxrpc_setsockopt,
683720
.getsockopt = sock_no_getsockopt,
684721
.sendmsg = rxrpc_sendmsg,

0 commit comments

Comments
 (0)