xref: /openbmc/linux/net/rxrpc/input.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
296b2d69bSDavid Howells /* Processing of received RxRPC packets
38c3e34a4SDavid Howells  *
496b2d69bSDavid Howells  * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
58c3e34a4SDavid Howells  * Written by David Howells (dhowells@redhat.com)
68c3e34a4SDavid Howells  */
78c3e34a4SDavid Howells 
88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c3e34a4SDavid Howells 
108c3e34a4SDavid Howells #include "ar-internal.h"
118c3e34a4SDavid Howells 
121721afe2SDavid Howells /* Override priority when generating ACKs for received DATA */
131721afe2SDavid Howells static const u8 rxrpc_ack_priority[RXRPC_ACK__INVALID] = {
141721afe2SDavid Howells 	[RXRPC_ACK_IDLE]		= 1,
151721afe2SDavid Howells 	[RXRPC_ACK_DELAY]		= 2,
161721afe2SDavid Howells 	[RXRPC_ACK_REQUESTED]		= 3,
171721afe2SDavid Howells 	[RXRPC_ACK_DUPLICATE]		= 4,
181721afe2SDavid Howells 	[RXRPC_ACK_EXCEEDS_WINDOW]	= 5,
191721afe2SDavid Howells 	[RXRPC_ACK_NOSPACE]		= 6,
201721afe2SDavid Howells 	[RXRPC_ACK_OUT_OF_SEQUENCE]	= 7,
211721afe2SDavid Howells };
221721afe2SDavid Howells 
rxrpc_proto_abort(struct rxrpc_call * call,rxrpc_seq_t seq,enum rxrpc_abort_reason why)2357af281eSDavid Howells static void rxrpc_proto_abort(struct rxrpc_call *call, rxrpc_seq_t seq,
2457af281eSDavid Howells 			      enum rxrpc_abort_reason why)
258c3e34a4SDavid Howells {
2657af281eSDavid Howells 	rxrpc_abort_call(call, seq, RX_PROTOCOL_ERROR, -EBADMSG, why);
278c3e34a4SDavid Howells }
288c3e34a4SDavid Howells 
298c3e34a4SDavid Howells /*
3057494343SDavid Howells  * Do TCP-style congestion management [RFC 5681].
3157494343SDavid Howells  */
rxrpc_congestion_management(struct rxrpc_call * call,struct sk_buff * skb,struct rxrpc_ack_summary * summary,rxrpc_serial_t acked_serial)3257494343SDavid Howells static void rxrpc_congestion_management(struct rxrpc_call *call,
3357494343SDavid Howells 					struct sk_buff *skb,
34ed1e8679SDavid Howells 					struct rxrpc_ack_summary *summary,
35ed1e8679SDavid Howells 					rxrpc_serial_t acked_serial)
3657494343SDavid Howells {
3757494343SDavid Howells 	enum rxrpc_congest_change change = rxrpc_cong_no_change;
3857494343SDavid Howells 	unsigned int cumulative_acks = call->cong_cumul_acks;
3957494343SDavid Howells 	unsigned int cwnd = call->cong_cwnd;
4057494343SDavid Howells 	bool resend = false;
4157494343SDavid Howells 
4257494343SDavid Howells 	summary->flight_size =
43a4ea4c47SDavid Howells 		(call->tx_top - call->acks_hard_ack) - summary->nr_acks;
4457494343SDavid Howells 
4557494343SDavid Howells 	if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
4657494343SDavid Howells 		summary->retrans_timeo = true;
4757494343SDavid Howells 		call->cong_ssthresh = max_t(unsigned int,
4857494343SDavid Howells 					    summary->flight_size / 2, 2);
4957494343SDavid Howells 		cwnd = 1;
508782def2SDavid Howells 		if (cwnd >= call->cong_ssthresh &&
5157494343SDavid Howells 		    call->cong_mode == RXRPC_CALL_SLOW_START) {
5257494343SDavid Howells 			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
5357494343SDavid Howells 			call->cong_tstamp = skb->tstamp;
5457494343SDavid Howells 			cumulative_acks = 0;
5557494343SDavid Howells 		}
5657494343SDavid Howells 	}
5757494343SDavid Howells 
5857494343SDavid Howells 	cumulative_acks += summary->nr_new_acks;
5957494343SDavid Howells 	if (cumulative_acks > 255)
6057494343SDavid Howells 		cumulative_acks = 255;
6157494343SDavid Howells 
6257494343SDavid Howells 	summary->cwnd = call->cong_cwnd;
6357494343SDavid Howells 	summary->ssthresh = call->cong_ssthresh;
6457494343SDavid Howells 	summary->cumulative_acks = cumulative_acks;
6557494343SDavid Howells 	summary->dup_acks = call->cong_dup_acks;
6657494343SDavid Howells 
6757494343SDavid Howells 	switch (call->cong_mode) {
6857494343SDavid Howells 	case RXRPC_CALL_SLOW_START:
69d57a3a15SDavid Howells 		if (summary->saw_nacks)
7057494343SDavid Howells 			goto packet_loss_detected;
7157494343SDavid Howells 		if (summary->cumulative_acks > 0)
7257494343SDavid Howells 			cwnd += 1;
738782def2SDavid Howells 		if (cwnd >= call->cong_ssthresh) {
7457494343SDavid Howells 			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
7557494343SDavid Howells 			call->cong_tstamp = skb->tstamp;
7657494343SDavid Howells 		}
7757494343SDavid Howells 		goto out;
7857494343SDavid Howells 
7957494343SDavid Howells 	case RXRPC_CALL_CONGEST_AVOIDANCE:
80d57a3a15SDavid Howells 		if (summary->saw_nacks)
8157494343SDavid Howells 			goto packet_loss_detected;
8257494343SDavid Howells 
8357494343SDavid Howells 		/* We analyse the number of packets that get ACK'd per RTT
8457494343SDavid Howells 		 * period and increase the window if we managed to fill it.
8557494343SDavid Howells 		 */
86c410bf01SDavid Howells 		if (call->peer->rtt_count == 0)
8757494343SDavid Howells 			goto out;
8857494343SDavid Howells 		if (ktime_before(skb->tstamp,
89c410bf01SDavid Howells 				 ktime_add_us(call->cong_tstamp,
90c410bf01SDavid Howells 					      call->peer->srtt_us >> 3)))
9157494343SDavid Howells 			goto out_no_clear_ca;
9257494343SDavid Howells 		change = rxrpc_cong_rtt_window_end;
9357494343SDavid Howells 		call->cong_tstamp = skb->tstamp;
9457494343SDavid Howells 		if (cumulative_acks >= cwnd)
9557494343SDavid Howells 			cwnd++;
9657494343SDavid Howells 		goto out;
9757494343SDavid Howells 
9857494343SDavid Howells 	case RXRPC_CALL_PACKET_LOSS:
99d57a3a15SDavid Howells 		if (!summary->saw_nacks)
10057494343SDavid Howells 			goto resume_normality;
10157494343SDavid Howells 
10257494343SDavid Howells 		if (summary->new_low_nack) {
10357494343SDavid Howells 			change = rxrpc_cong_new_low_nack;
10457494343SDavid Howells 			call->cong_dup_acks = 1;
10557494343SDavid Howells 			if (call->cong_extra > 1)
10657494343SDavid Howells 				call->cong_extra = 1;
10757494343SDavid Howells 			goto send_extra_data;
10857494343SDavid Howells 		}
10957494343SDavid Howells 
11057494343SDavid Howells 		call->cong_dup_acks++;
11157494343SDavid Howells 		if (call->cong_dup_acks < 3)
11257494343SDavid Howells 			goto send_extra_data;
11357494343SDavid Howells 
11457494343SDavid Howells 		change = rxrpc_cong_begin_retransmission;
11557494343SDavid Howells 		call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
11657494343SDavid Howells 		call->cong_ssthresh = max_t(unsigned int,
11757494343SDavid Howells 					    summary->flight_size / 2, 2);
11857494343SDavid Howells 		cwnd = call->cong_ssthresh + 3;
11957494343SDavid Howells 		call->cong_extra = 0;
12057494343SDavid Howells 		call->cong_dup_acks = 0;
12157494343SDavid Howells 		resend = true;
12257494343SDavid Howells 		goto out;
12357494343SDavid Howells 
12457494343SDavid Howells 	case RXRPC_CALL_FAST_RETRANSMIT:
12557494343SDavid Howells 		if (!summary->new_low_nack) {
12657494343SDavid Howells 			if (summary->nr_new_acks == 0)
12757494343SDavid Howells 				cwnd += 1;
12857494343SDavid Howells 			call->cong_dup_acks++;
12957494343SDavid Howells 			if (call->cong_dup_acks == 2) {
13057494343SDavid Howells 				change = rxrpc_cong_retransmit_again;
13157494343SDavid Howells 				call->cong_dup_acks = 0;
13257494343SDavid Howells 				resend = true;
13357494343SDavid Howells 			}
13457494343SDavid Howells 		} else {
13557494343SDavid Howells 			change = rxrpc_cong_progress;
13657494343SDavid Howells 			cwnd = call->cong_ssthresh;
137d57a3a15SDavid Howells 			if (!summary->saw_nacks)
13857494343SDavid Howells 				goto resume_normality;
13957494343SDavid Howells 		}
14057494343SDavid Howells 		goto out;
14157494343SDavid Howells 
14257494343SDavid Howells 	default:
14357494343SDavid Howells 		BUG();
14457494343SDavid Howells 		goto out;
14557494343SDavid Howells 	}
14657494343SDavid Howells 
14757494343SDavid Howells resume_normality:
14857494343SDavid Howells 	change = rxrpc_cong_cleared_nacks;
14957494343SDavid Howells 	call->cong_dup_acks = 0;
15057494343SDavid Howells 	call->cong_extra = 0;
15157494343SDavid Howells 	call->cong_tstamp = skb->tstamp;
1528782def2SDavid Howells 	if (cwnd < call->cong_ssthresh)
15357494343SDavid Howells 		call->cong_mode = RXRPC_CALL_SLOW_START;
15457494343SDavid Howells 	else
15557494343SDavid Howells 		call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
15657494343SDavid Howells out:
15757494343SDavid Howells 	cumulative_acks = 0;
15857494343SDavid Howells out_no_clear_ca:
159a4ea4c47SDavid Howells 	if (cwnd >= RXRPC_TX_MAX_WINDOW)
160a4ea4c47SDavid Howells 		cwnd = RXRPC_TX_MAX_WINDOW;
16157494343SDavid Howells 	call->cong_cwnd = cwnd;
16257494343SDavid Howells 	call->cong_cumul_acks = cumulative_acks;
163024b2511SDavid Howells 	summary->mode = call->cong_mode;
164ed1e8679SDavid Howells 	trace_rxrpc_congest(call, summary, acked_serial, change);
1655e6ef4f1SDavid Howells 	if (resend)
1665e6ef4f1SDavid Howells 		rxrpc_resend(call, skb);
16757494343SDavid Howells 	return;
16857494343SDavid Howells 
16957494343SDavid Howells packet_loss_detected:
17057494343SDavid Howells 	change = rxrpc_cong_saw_nack;
17157494343SDavid Howells 	call->cong_mode = RXRPC_CALL_PACKET_LOSS;
17257494343SDavid Howells 	call->cong_dup_acks = 0;
17357494343SDavid Howells 	goto send_extra_data;
17457494343SDavid Howells 
17557494343SDavid Howells send_extra_data:
17657494343SDavid Howells 	/* Send some previously unsent DATA if we have some to advance the ACK
17757494343SDavid Howells 	 * state.
17857494343SDavid Howells 	 */
179a4ea4c47SDavid Howells 	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
180a4ea4c47SDavid Howells 	    summary->nr_acks != call->tx_top - call->acks_hard_ack) {
18157494343SDavid Howells 		call->cong_extra++;
18257494343SDavid Howells 		wake_up(&call->waitq);
18357494343SDavid Howells 	}
18457494343SDavid Howells 	goto out_no_clear_ca;
18557494343SDavid Howells }
18657494343SDavid Howells 
18757494343SDavid Howells /*
1885086d9a9SDavid Howells  * Degrade the congestion window if we haven't transmitted a packet for >1RTT.
1895086d9a9SDavid Howells  */
rxrpc_congestion_degrade(struct rxrpc_call * call)1905086d9a9SDavid Howells void rxrpc_congestion_degrade(struct rxrpc_call *call)
1915086d9a9SDavid Howells {
1925086d9a9SDavid Howells 	ktime_t rtt, now;
1935086d9a9SDavid Howells 
1945086d9a9SDavid Howells 	if (call->cong_mode != RXRPC_CALL_SLOW_START &&
1955086d9a9SDavid Howells 	    call->cong_mode != RXRPC_CALL_CONGEST_AVOIDANCE)
1965086d9a9SDavid Howells 		return;
19796b4059fSDavid Howells 	if (__rxrpc_call_state(call) == RXRPC_CALL_CLIENT_AWAIT_REPLY)
1985086d9a9SDavid Howells 		return;
1995086d9a9SDavid Howells 
2005086d9a9SDavid Howells 	rtt = ns_to_ktime(call->peer->srtt_us * (1000 / 8));
2015086d9a9SDavid Howells 	now = ktime_get_real();
2025086d9a9SDavid Howells 	if (!ktime_before(ktime_add(call->tx_last_sent, rtt), now))
2035086d9a9SDavid Howells 		return;
2045086d9a9SDavid Howells 
2055086d9a9SDavid Howells 	trace_rxrpc_reset_cwnd(call, now);
2065086d9a9SDavid Howells 	rxrpc_inc_stat(call->rxnet, stat_tx_data_cwnd_reset);
2075086d9a9SDavid Howells 	call->tx_last_sent = now;
2085086d9a9SDavid Howells 	call->cong_mode = RXRPC_CALL_SLOW_START;
2095086d9a9SDavid Howells 	call->cong_ssthresh = max_t(unsigned int, call->cong_ssthresh,
2105086d9a9SDavid Howells 				    call->cong_cwnd * 3 / 4);
2115086d9a9SDavid Howells 	call->cong_cwnd = max_t(unsigned int, call->cong_cwnd / 2, RXRPC_MIN_CWND);
2125086d9a9SDavid Howells }
2135086d9a9SDavid Howells 
2145086d9a9SDavid Howells /*
215248f219cSDavid Howells  * Apply a hard ACK by advancing the Tx window.
2168c3e34a4SDavid Howells  */
rxrpc_rotate_tx_window(struct rxrpc_call * call,rxrpc_seq_t to,struct rxrpc_ack_summary * summary)217c479d5f2SDavid Howells static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
21831a1b989SDavid Howells 				   struct rxrpc_ack_summary *summary)
2198c3e34a4SDavid Howells {
220a4ea4c47SDavid Howells 	struct rxrpc_txbuf *txb;
221c479d5f2SDavid Howells 	bool rot_last = false;
2228c3e34a4SDavid Howells 
223a4ea4c47SDavid Howells 	list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
224a4ea4c47SDavid Howells 		if (before_eq(txb->seq, call->acks_hard_ack))
225a4ea4c47SDavid Howells 			continue;
226a4ea4c47SDavid Howells 		if (test_bit(RXRPC_TXBUF_LAST, &txb->flags)) {
227a4ea4c47SDavid Howells 			set_bit(RXRPC_CALL_TX_LAST, &call->flags);
228a4ea4c47SDavid Howells 			rot_last = true;
229a4ea4c47SDavid Howells 		}
230a4ea4c47SDavid Howells 		if (txb->seq == to)
231a4ea4c47SDavid Howells 			break;
232a4ea4c47SDavid Howells 	}
233a4ea4c47SDavid Howells 
234a4ea4c47SDavid Howells 	if (rot_last)
235a4ea4c47SDavid Howells 		set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
236a4ea4c47SDavid Howells 
237a4ea4c47SDavid Howells 	_enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
238a4ea4c47SDavid Howells 
239a4ea4c47SDavid Howells 	if (call->acks_lowest_nak == call->acks_hard_ack) {
24031a1b989SDavid Howells 		call->acks_lowest_nak = to;
2411fc4fa2aSDavid Howells 	} else if (after(to, call->acks_lowest_nak)) {
24231a1b989SDavid Howells 		summary->new_low_nack = true;
24331a1b989SDavid Howells 		call->acks_lowest_nak = to;
24431a1b989SDavid Howells 	}
24531a1b989SDavid Howells 
246a4ea4c47SDavid Howells 	smp_store_release(&call->acks_hard_ack, to);
2478c3e34a4SDavid Howells 
248a4ea4c47SDavid Howells 	trace_rxrpc_txqueue(call, (rot_last ?
249a4ea4c47SDavid Howells 				   rxrpc_txqueue_rotate_last :
250a4ea4c47SDavid Howells 				   rxrpc_txqueue_rotate));
251bc4abfcfSDavid Howells 	wake_up(&call->waitq);
252c479d5f2SDavid Howells 	return rot_last;
2538c3e34a4SDavid Howells }
2548c3e34a4SDavid Howells 
2558c3e34a4SDavid Howells /*
256248f219cSDavid Howells  * End the transmission phase of a call.
257248f219cSDavid Howells  *
258248f219cSDavid Howells  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
259248f219cSDavid Howells  * or a final ACK packet.
2608c3e34a4SDavid Howells  */
rxrpc_end_tx_phase(struct rxrpc_call * call,bool reply_begun,enum rxrpc_abort_reason abort_why)26157af281eSDavid Howells static void rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
26257af281eSDavid Howells 			       enum rxrpc_abort_reason abort_why)
2638c3e34a4SDavid Howells {
26470790dbeSDavid Howells 	ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
265248f219cSDavid Howells 
266024b2511SDavid Howells 	if (unlikely(call->cong_last_nack)) {
267024b2511SDavid Howells 		rxrpc_free_skb(call->cong_last_nack, rxrpc_skb_put_last_nack);
268024b2511SDavid Howells 		call->cong_last_nack = NULL;
269024b2511SDavid Howells 	}
270024b2511SDavid Howells 
27196b4059fSDavid Howells 	switch (__rxrpc_call_state(call)) {
27270790dbeSDavid Howells 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
2738c3e34a4SDavid Howells 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
27496b4059fSDavid Howells 		if (reply_begun) {
27596b4059fSDavid Howells 			rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_RECV_REPLY);
27696b4059fSDavid Howells 			trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
27796b4059fSDavid Howells 			break;
27896b4059fSDavid Howells 		}
27996b4059fSDavid Howells 
28096b4059fSDavid Howells 		rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
28196b4059fSDavid Howells 		trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
282248f219cSDavid Howells 		break;
28370790dbeSDavid Howells 
284248f219cSDavid Howells 	case RXRPC_CALL_SERVER_AWAIT_ACK:
28596b4059fSDavid Howells 		rxrpc_call_completed(call);
28696b4059fSDavid Howells 		trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
287248f219cSDavid Howells 		break;
28870790dbeSDavid Howells 
28970790dbeSDavid Howells 	default:
29096b4059fSDavid Howells 		kdebug("end_tx %s", rxrpc_call_states[__rxrpc_call_state(call)]);
29157af281eSDavid Howells 		rxrpc_proto_abort(call, call->tx_top, abort_why);
29296b4059fSDavid Howells 		break;
29396b4059fSDavid Howells 	}
29470790dbeSDavid Howells }
29570790dbeSDavid Howells 
29670790dbeSDavid Howells /*
29770790dbeSDavid Howells  * Begin the reply reception phase of a call.
29870790dbeSDavid Howells  */
rxrpc_receiving_reply(struct rxrpc_call * call)29970790dbeSDavid Howells static bool rxrpc_receiving_reply(struct rxrpc_call *call)
30070790dbeSDavid Howells {
30131a1b989SDavid Howells 	struct rxrpc_ack_summary summary = { 0 };
302a158bdd3SDavid Howells 	unsigned long now, timo;
30370790dbeSDavid Howells 	rxrpc_seq_t top = READ_ONCE(call->tx_top);
30470790dbeSDavid Howells 
305dd7c1ee5SDavid Howells 	if (call->ackr_reason) {
306a158bdd3SDavid Howells 		now = jiffies;
307a158bdd3SDavid Howells 		timo = now + MAX_JIFFY_OFFSET;
30896b4059fSDavid Howells 
309530403d9SDavid Howells 		WRITE_ONCE(call->delay_ack_at, timo);
310a158bdd3SDavid Howells 		trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
311dd7c1ee5SDavid Howells 	}
312dd7c1ee5SDavid Howells 
31370790dbeSDavid Howells 	if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
314c479d5f2SDavid Howells 		if (!rxrpc_rotate_tx_window(call, top, &summary)) {
31557af281eSDavid Howells 			rxrpc_proto_abort(call, top, rxrpc_eproto_early_reply);
31670790dbeSDavid Howells 			return false;
31770790dbeSDavid Howells 		}
318c479d5f2SDavid Howells 	}
31957af281eSDavid Howells 
32057af281eSDavid Howells 	rxrpc_end_tx_phase(call, true, rxrpc_eproto_unexpected_reply);
32157af281eSDavid Howells 	return true;
322248f219cSDavid Howells }
323248f219cSDavid Howells 
32493368b6bSDavid Howells /*
32593368b6bSDavid Howells  * End the packet reception phase.
32693368b6bSDavid Howells  */
rxrpc_end_rx_phase(struct rxrpc_call * call,rxrpc_serial_t serial)32793368b6bSDavid Howells static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
32893368b6bSDavid Howells {
32993368b6bSDavid Howells 	rxrpc_seq_t whigh = READ_ONCE(call->rx_highest_seq);
33093368b6bSDavid Howells 
33196b4059fSDavid Howells 	_enter("%d,%s", call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)]);
33293368b6bSDavid Howells 
33393368b6bSDavid Howells 	trace_rxrpc_receive(call, rxrpc_receive_end, 0, whigh);
33493368b6bSDavid Howells 
33596b4059fSDavid Howells 	switch (__rxrpc_call_state(call)) {
33693368b6bSDavid Howells 	case RXRPC_CALL_CLIENT_RECV_REPLY:
33796b4059fSDavid Howells 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_terminal_ack);
33896b4059fSDavid Howells 		rxrpc_call_completed(call);
33993368b6bSDavid Howells 		break;
34093368b6bSDavid Howells 
34193368b6bSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST:
34296b4059fSDavid Howells 		rxrpc_set_call_state(call, RXRPC_CALL_SERVER_ACK_REQUEST);
34393368b6bSDavid Howells 		call->expect_req_by = jiffies + MAX_JIFFY_OFFSET;
34496b4059fSDavid Howells 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_processing_op);
34593368b6bSDavid Howells 		break;
34696b4059fSDavid Howells 
34793368b6bSDavid Howells 	default:
34893368b6bSDavid Howells 		break;
34993368b6bSDavid Howells 	}
35093368b6bSDavid Howells }
35193368b6bSDavid Howells 
rxrpc_input_update_ack_window(struct rxrpc_call * call,rxrpc_seq_t window,rxrpc_seq_t wtop)3525d7edbc9SDavid Howells static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
3535d7edbc9SDavid Howells 					  rxrpc_seq_t window, rxrpc_seq_t wtop)
3545d7edbc9SDavid Howells {
3555bbf9533SDavid Howells 	call->ackr_window = window;
3565bbf9533SDavid Howells 	call->ackr_wtop = wtop;
3575d7edbc9SDavid Howells }
3585d7edbc9SDavid Howells 
359248f219cSDavid Howells /*
3605d7edbc9SDavid Howells  * Push a DATA packet onto the Rx queue.
3615d7edbc9SDavid Howells  */
rxrpc_input_queue_data(struct rxrpc_call * call,struct sk_buff * skb,rxrpc_seq_t window,rxrpc_seq_t wtop,enum rxrpc_receive_trace why)3625d7edbc9SDavid Howells static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
3635d7edbc9SDavid Howells 				   rxrpc_seq_t window, rxrpc_seq_t wtop,
3645d7edbc9SDavid Howells 				   enum rxrpc_receive_trace why)
3655d7edbc9SDavid Howells {
3665d7edbc9SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3675d7edbc9SDavid Howells 	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
3685d7edbc9SDavid Howells 
3695d7edbc9SDavid Howells 	__skb_queue_tail(&call->recvmsg_queue, skb);
3705d7edbc9SDavid Howells 	rxrpc_input_update_ack_window(call, window, wtop);
3715d7edbc9SDavid Howells 	trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
37293368b6bSDavid Howells 	if (last)
37393368b6bSDavid Howells 		rxrpc_end_rx_phase(call, sp->hdr.serial);
3745d7edbc9SDavid Howells }
3755d7edbc9SDavid Howells 
3765d7edbc9SDavid Howells /*
3775d7edbc9SDavid Howells  * Process a DATA packet.
3788c3e34a4SDavid Howells  */
rxrpc_input_data_one(struct rxrpc_call * call,struct sk_buff * skb,bool * _notify,rxrpc_serial_t * _ack_serial,int * _ack_reason)3792d1faf7aSDavid Howells static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb,
3801721afe2SDavid Howells 				 bool *_notify, rxrpc_serial_t *_ack_serial, int *_ack_reason)
381248f219cSDavid Howells {
382248f219cSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
3835d7edbc9SDavid Howells 	struct sk_buff *oos;
384d4d02d8bSDavid Howells 	rxrpc_serial_t serial = sp->hdr.serial;
385f21e9348SDavid Howells 	unsigned int sack = call->ackr_sack_base;
3865bbf9533SDavid Howells 	rxrpc_seq_t window = call->ackr_window;
3875bbf9533SDavid Howells 	rxrpc_seq_t wtop = call->ackr_wtop;
3885d7edbc9SDavid Howells 	rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
3895d7edbc9SDavid Howells 	rxrpc_seq_t seq = sp->hdr.seq;
390d4d02d8bSDavid Howells 	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
3915d7edbc9SDavid Howells 	int ack_reason = -1;
392a158bdd3SDavid Howells 
393b0154246SDavid Howells 	rxrpc_inc_stat(call->rxnet, stat_rx_data);
394b0154246SDavid Howells 	if (sp->hdr.flags & RXRPC_REQUEST_ACK)
395b0154246SDavid Howells 		rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
396b0154246SDavid Howells 	if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
397b0154246SDavid Howells 		rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
398b0154246SDavid Howells 
399e2de6c40SDavid Howells 	if (last) {
4005d7edbc9SDavid Howells 		if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
40157af281eSDavid Howells 		    seq + 1 != wtop)
40257af281eSDavid Howells 			return rxrpc_proto_abort(call, seq, rxrpc_eproto_different_last);
403248f219cSDavid Howells 	} else {
404248f219cSDavid Howells 		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
4055d7edbc9SDavid Howells 		    after_eq(seq, wtop)) {
4065d7edbc9SDavid Howells 			pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
4075d7edbc9SDavid Howells 				call->debug_id, seq, window, wtop, wlimit);
40857af281eSDavid Howells 			return rxrpc_proto_abort(call, seq, rxrpc_eproto_data_after_last);
409c1e15b49SDavid Howells 		}
410248f219cSDavid Howells 	}
411248f219cSDavid Howells 
4125d7edbc9SDavid Howells 	if (after(seq, call->rx_highest_seq))
4135d7edbc9SDavid Howells 		call->rx_highest_seq = seq;
4145d7edbc9SDavid Howells 
415d4d02d8bSDavid Howells 	trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
416e2de6c40SDavid Howells 
4175d7edbc9SDavid Howells 	if (before(seq, window)) {
4185d7edbc9SDavid Howells 		ack_reason = RXRPC_ACK_DUPLICATE;
4195d7edbc9SDavid Howells 		goto send_ack;
4205d7edbc9SDavid Howells 	}
4215d7edbc9SDavid Howells 	if (after(seq, wlimit)) {
4225d7edbc9SDavid Howells 		ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
4235d7edbc9SDavid Howells 		goto send_ack;
424248f219cSDavid Howells 	}
425248f219cSDavid Howells 
4265d7edbc9SDavid Howells 	/* Queue the packet. */
4275d7edbc9SDavid Howells 	if (seq == window) {
4285d7edbc9SDavid Howells 		if (sp->hdr.flags & RXRPC_REQUEST_ACK)
4295d7edbc9SDavid Howells 			ack_reason = RXRPC_ACK_REQUESTED;
430a7056c5bSDavid Howells 		/* Send an immediate ACK if we fill in a hole */
4315d7edbc9SDavid Howells 		else if (!skb_queue_empty(&call->rx_oos_queue))
4325d7edbc9SDavid Howells 			ack_reason = RXRPC_ACK_DELAY;
433e2de6c40SDavid Howells 
4345d7edbc9SDavid Howells 		window++;
435f21e9348SDavid Howells 		if (after(window, wtop)) {
436f21e9348SDavid Howells 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_none);
4375d7edbc9SDavid Howells 			wtop = window;
438f21e9348SDavid Howells 		} else {
439f21e9348SDavid Howells 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_advance);
440f21e9348SDavid Howells 			sack = (sack + 1) % RXRPC_SACK_SIZE;
441f21e9348SDavid Howells 		}
442f21e9348SDavid Howells 
4435d7edbc9SDavid Howells 
4442d1faf7aSDavid Howells 		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg);
4452d1faf7aSDavid Howells 
4465d7edbc9SDavid Howells 		spin_lock(&call->recvmsg_queue.lock);
4475d7edbc9SDavid Howells 		rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
4482d1faf7aSDavid Howells 		*_notify = true;
449e2de6c40SDavid Howells 
4505d7edbc9SDavid Howells 		while ((oos = skb_peek(&call->rx_oos_queue))) {
4515d7edbc9SDavid Howells 			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
4525d7edbc9SDavid Howells 
4535d7edbc9SDavid Howells 			if (after(osp->hdr.seq, window))
4545d7edbc9SDavid Howells 				break;
4555d7edbc9SDavid Howells 
4565d7edbc9SDavid Howells 			__skb_unlink(oos, &call->rx_oos_queue);
4575d7edbc9SDavid Howells 			last = osp->hdr.flags & RXRPC_LAST_PACKET;
4585d7edbc9SDavid Howells 			seq = osp->hdr.seq;
459f21e9348SDavid Howells 			call->ackr_sack_table[sack] = 0;
460f21e9348SDavid Howells 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_fill);
461f21e9348SDavid Howells 			sack = (sack + 1) % RXRPC_SACK_SIZE;
4625d7edbc9SDavid Howells 
4635d7edbc9SDavid Howells 			window++;
4645d7edbc9SDavid Howells 			rxrpc_input_queue_data(call, oos, window, wtop,
4655d7edbc9SDavid Howells 					       rxrpc_receive_queue_oos);
4665d7edbc9SDavid Howells 		}
4675d7edbc9SDavid Howells 
4685d7edbc9SDavid Howells 		spin_unlock(&call->recvmsg_queue.lock);
4695d7edbc9SDavid Howells 
470f21e9348SDavid Howells 		call->ackr_sack_base = sack;
47158dc63c9SDavid Howells 	} else {
472f21e9348SDavid Howells 		unsigned int slot;
4735d7edbc9SDavid Howells 
4745d7edbc9SDavid Howells 		ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
4755d7edbc9SDavid Howells 
476f21e9348SDavid Howells 		slot = seq - window;
477f21e9348SDavid Howells 		sack = (sack + slot) % RXRPC_SACK_SIZE;
478f21e9348SDavid Howells 
479f21e9348SDavid Howells 		if (call->ackr_sack_table[sack % RXRPC_SACK_SIZE]) {
480f21e9348SDavid Howells 			ack_reason = RXRPC_ACK_DUPLICATE;
481f21e9348SDavid Howells 			goto send_ack;
48258dc63c9SDavid Howells 		}
483248f219cSDavid Howells 
484f21e9348SDavid Howells 		call->ackr_sack_table[sack % RXRPC_SACK_SIZE] |= 1;
485f21e9348SDavid Howells 		trace_rxrpc_sack(call, seq, sack, rxrpc_sack_oos);
486f21e9348SDavid Howells 
4875d7edbc9SDavid Howells 		if (after(seq + 1, wtop)) {
4885d7edbc9SDavid Howells 			wtop = seq + 1;
4895d7edbc9SDavid Howells 			rxrpc_input_update_ack_window(call, window, wtop);
490248f219cSDavid Howells 		}
49172f0c6fbSDavid Howells 
4925d7edbc9SDavid Howells 		skb_queue_walk(&call->rx_oos_queue, oos) {
4935d7edbc9SDavid Howells 			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
4945d7edbc9SDavid Howells 
4955d7edbc9SDavid Howells 			if (after(osp->hdr.seq, seq)) {
4962d1faf7aSDavid Howells 				rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
4975d7edbc9SDavid Howells 				__skb_queue_before(&call->rx_oos_queue, oos, skb);
4985d7edbc9SDavid Howells 				goto oos_queued;
4995d7edbc9SDavid Howells 			}
5005d7edbc9SDavid Howells 		}
5015d7edbc9SDavid Howells 
5022d1faf7aSDavid Howells 		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
5035d7edbc9SDavid Howells 		__skb_queue_tail(&call->rx_oos_queue, skb);
5045d7edbc9SDavid Howells 	oos_queued:
5055d7edbc9SDavid Howells 		trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
5065d7edbc9SDavid Howells 				    sp->hdr.serial, sp->hdr.seq);
5075d7edbc9SDavid Howells 	}
5085d7edbc9SDavid Howells 
5095d7edbc9SDavid Howells send_ack:
5101721afe2SDavid Howells 	if (ack_reason >= 0) {
5111721afe2SDavid Howells 		if (rxrpc_ack_priority[ack_reason] > rxrpc_ack_priority[*_ack_reason]) {
5121721afe2SDavid Howells 			*_ack_serial = serial;
5131721afe2SDavid Howells 			*_ack_reason = ack_reason;
5141721afe2SDavid Howells 		} else if (rxrpc_ack_priority[ack_reason] == rxrpc_ack_priority[*_ack_reason] &&
5151721afe2SDavid Howells 			   ack_reason == RXRPC_ACK_REQUESTED) {
5161721afe2SDavid Howells 			*_ack_serial = serial;
5171721afe2SDavid Howells 			*_ack_reason = ack_reason;
5181721afe2SDavid Howells 		}
5191721afe2SDavid Howells 	}
520d4d02d8bSDavid Howells }
521d4d02d8bSDavid Howells 
522d4d02d8bSDavid Howells /*
523d4d02d8bSDavid Howells  * Split a jumbo packet and file the bits separately.
524d4d02d8bSDavid Howells  */
rxrpc_input_split_jumbo(struct rxrpc_call * call,struct sk_buff * skb)525d4d02d8bSDavid Howells static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
526d4d02d8bSDavid Howells {
527d4d02d8bSDavid Howells 	struct rxrpc_jumbo_header jhdr;
528d4d02d8bSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
529d4d02d8bSDavid Howells 	struct sk_buff *jskb;
5301721afe2SDavid Howells 	rxrpc_serial_t ack_serial = 0;
531d4d02d8bSDavid Howells 	unsigned int offset = sizeof(struct rxrpc_wire_header);
532d4d02d8bSDavid Howells 	unsigned int len = skb->len - offset;
5332d1faf7aSDavid Howells 	bool notify = false;
5341721afe2SDavid Howells 	int ack_reason = 0;
535d4d02d8bSDavid Howells 
536d4d02d8bSDavid Howells 	while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
537d4d02d8bSDavid Howells 		if (len < RXRPC_JUMBO_SUBPKTLEN)
538d4d02d8bSDavid Howells 			goto protocol_error;
539d4d02d8bSDavid Howells 		if (sp->hdr.flags & RXRPC_LAST_PACKET)
540d4d02d8bSDavid Howells 			goto protocol_error;
541d4d02d8bSDavid Howells 		if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
542d4d02d8bSDavid Howells 				  &jhdr, sizeof(jhdr)) < 0)
543d4d02d8bSDavid Howells 			goto protocol_error;
544d4d02d8bSDavid Howells 
5455e6ef4f1SDavid Howells 		jskb = skb_clone(skb, GFP_NOFS);
546d4d02d8bSDavid Howells 		if (!jskb) {
547d4d02d8bSDavid Howells 			kdebug("couldn't clone");
548d4d02d8bSDavid Howells 			return false;
549d4d02d8bSDavid Howells 		}
5509a36a6bcSDavid Howells 		rxrpc_new_skb(jskb, rxrpc_skb_new_jumbo_subpacket);
551d4d02d8bSDavid Howells 		jsp = rxrpc_skb(jskb);
552d4d02d8bSDavid Howells 		jsp->offset = offset;
553d4d02d8bSDavid Howells 		jsp->len = RXRPC_JUMBO_DATALEN;
5541721afe2SDavid Howells 		rxrpc_input_data_one(call, jskb, &notify, &ack_serial, &ack_reason);
5552d1faf7aSDavid Howells 		rxrpc_free_skb(jskb, rxrpc_skb_put_jumbo_subpacket);
556d4d02d8bSDavid Howells 
557d4d02d8bSDavid Howells 		sp->hdr.flags = jhdr.flags;
558d4d02d8bSDavid Howells 		sp->hdr._rsvd = ntohs(jhdr._rsvd);
559d4d02d8bSDavid Howells 		sp->hdr.seq++;
560d4d02d8bSDavid Howells 		sp->hdr.serial++;
561d4d02d8bSDavid Howells 		offset += RXRPC_JUMBO_SUBPKTLEN;
562d4d02d8bSDavid Howells 		len -= RXRPC_JUMBO_SUBPKTLEN;
563d4d02d8bSDavid Howells 	}
564d4d02d8bSDavid Howells 
565d4d02d8bSDavid Howells 	sp->offset = offset;
566d4d02d8bSDavid Howells 	sp->len    = len;
5671721afe2SDavid Howells 	rxrpc_input_data_one(call, skb, &notify, &ack_serial, &ack_reason);
5681721afe2SDavid Howells 
5691721afe2SDavid Howells 	if (ack_reason > 0) {
5701721afe2SDavid Howells 		rxrpc_send_ACK(call, ack_reason, ack_serial,
5711721afe2SDavid Howells 			       rxrpc_propose_ack_input_data);
5721721afe2SDavid Howells 	} else {
5731721afe2SDavid Howells 		call->ackr_nr_unacked++;
5741721afe2SDavid Howells 		rxrpc_propose_delay_ACK(call, sp->hdr.serial,
5751721afe2SDavid Howells 					rxrpc_propose_ack_input_data);
5761721afe2SDavid Howells 	}
577*7770b221SDavid Howells 	if (notify && !test_bit(RXRPC_CALL_CONN_CHALLENGING, &call->flags)) {
5782d1faf7aSDavid Howells 		trace_rxrpc_notify_socket(call->debug_id, sp->hdr.serial);
5792d1faf7aSDavid Howells 		rxrpc_notify_socket(call);
5802d1faf7aSDavid Howells 	}
581d4d02d8bSDavid Howells 	return true;
582d4d02d8bSDavid Howells 
583d4d02d8bSDavid Howells protocol_error:
584d4d02d8bSDavid Howells 	return false;
585d4d02d8bSDavid Howells }
586d4d02d8bSDavid Howells 
587d4d02d8bSDavid Howells /*
588d4d02d8bSDavid Howells  * Process a DATA packet, adding the packet to the Rx ring.  The caller's
589d4d02d8bSDavid Howells  * packet ref must be passed on or discarded.
590d4d02d8bSDavid Howells  */
rxrpc_input_data(struct rxrpc_call * call,struct sk_buff * skb)591d4d02d8bSDavid Howells static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
592d4d02d8bSDavid Howells {
593d4d02d8bSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
594d4d02d8bSDavid Howells 	rxrpc_serial_t serial = sp->hdr.serial;
595d4d02d8bSDavid Howells 	rxrpc_seq_t seq0 = sp->hdr.seq;
596d4d02d8bSDavid Howells 
5975bbf9533SDavid Howells 	_enter("{%x,%x,%x},{%u,%x}",
5985bbf9533SDavid Howells 	       call->ackr_window, call->ackr_wtop, call->rx_highest_seq,
5995d7edbc9SDavid Howells 	       skb->len, seq0);
600d4d02d8bSDavid Howells 
60196b4059fSDavid Howells 	if (__rxrpc_call_is_complete(call))
602d4d02d8bSDavid Howells 		return;
603d4d02d8bSDavid Howells 
60496b4059fSDavid Howells 	switch (__rxrpc_call_state(call)) {
60596b4059fSDavid Howells 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
60696b4059fSDavid Howells 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
60796b4059fSDavid Howells 		/* Received data implicitly ACKs all of the request
60896b4059fSDavid Howells 		 * packets we sent when we're acting as a client.
60996b4059fSDavid Howells 		 */
61096b4059fSDavid Howells 		if (!rxrpc_receiving_reply(call))
61196b4059fSDavid Howells 			goto out_notify;
61296b4059fSDavid Howells 		break;
61396b4059fSDavid Howells 
61496b4059fSDavid Howells 	case RXRPC_CALL_SERVER_RECV_REQUEST: {
615d4d02d8bSDavid Howells 		unsigned long timo = READ_ONCE(call->next_req_timo);
616d4d02d8bSDavid Howells 		unsigned long now, expect_req_by;
617d4d02d8bSDavid Howells 
618d4d02d8bSDavid Howells 		if (timo) {
619d4d02d8bSDavid Howells 			now = jiffies;
620d4d02d8bSDavid Howells 			expect_req_by = now + timo;
621d4d02d8bSDavid Howells 			WRITE_ONCE(call->expect_req_by, expect_req_by);
622d4d02d8bSDavid Howells 			rxrpc_reduce_call_timer(call, expect_req_by, now,
623d4d02d8bSDavid Howells 						rxrpc_timer_set_for_idle);
624d4d02d8bSDavid Howells 		}
62596b4059fSDavid Howells 		break;
626d4d02d8bSDavid Howells 	}
627d4d02d8bSDavid Howells 
62896b4059fSDavid Howells 	default:
62996b4059fSDavid Howells 		break;
63096b4059fSDavid Howells 	}
631d4d02d8bSDavid Howells 
632d4d02d8bSDavid Howells 	if (!rxrpc_input_split_jumbo(call, skb)) {
63357af281eSDavid Howells 		rxrpc_proto_abort(call, sp->hdr.seq, rxrpc_badmsg_bad_jumbo);
6345e6ef4f1SDavid Howells 		goto out_notify;
635d4d02d8bSDavid Howells 	}
636c0783818SDavid Howells 	return;
637d4d02d8bSDavid Howells 
6385e6ef4f1SDavid Howells out_notify:
639d4d02d8bSDavid Howells 	trace_rxrpc_notify_socket(call->debug_id, serial);
640d4d02d8bSDavid Howells 	rxrpc_notify_socket(call);
641248f219cSDavid Howells 	_leave(" [queued]");
642248f219cSDavid Howells }
643248f219cSDavid Howells 
644248f219cSDavid Howells /*
6454700c4d8SDavid Howells  * See if there's a cached RTT probe to complete.
64650235c4bSDavid Howells  */
rxrpc_complete_rtt_probe(struct rxrpc_call * call,ktime_t resp_time,rxrpc_serial_t acked_serial,rxrpc_serial_t ack_serial,enum rxrpc_rtt_rx_trace type)6474700c4d8SDavid Howells static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
64850235c4bSDavid Howells 				     ktime_t resp_time,
6494700c4d8SDavid Howells 				     rxrpc_serial_t acked_serial,
6504700c4d8SDavid Howells 				     rxrpc_serial_t ack_serial,
6514700c4d8SDavid Howells 				     enum rxrpc_rtt_rx_trace type)
65250235c4bSDavid Howells {
6534700c4d8SDavid Howells 	rxrpc_serial_t orig_serial;
6544700c4d8SDavid Howells 	unsigned long avail;
65550235c4bSDavid Howells 	ktime_t sent_at;
6564700c4d8SDavid Howells 	bool matched = false;
6574700c4d8SDavid Howells 	int i;
65850235c4bSDavid Howells 
6594700c4d8SDavid Howells 	avail = READ_ONCE(call->rtt_avail);
6604700c4d8SDavid Howells 	smp_rmb(); /* Read avail bits before accessing data. */
6614700c4d8SDavid Howells 
6624700c4d8SDavid Howells 	for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
6634700c4d8SDavid Howells 		if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
66450235c4bSDavid Howells 			continue;
66550235c4bSDavid Howells 
6664700c4d8SDavid Howells 		sent_at = call->rtt_sent_at[i];
6674700c4d8SDavid Howells 		orig_serial = call->rtt_serial[i];
6684700c4d8SDavid Howells 
6694700c4d8SDavid Howells 		if (orig_serial == acked_serial) {
6704700c4d8SDavid Howells 			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
6714700c4d8SDavid Howells 			smp_mb(); /* Read data before setting avail bit */
6724700c4d8SDavid Howells 			set_bit(i, &call->rtt_avail);
6734700c4d8SDavid Howells 			rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
6744700c4d8SDavid Howells 					   sent_at, resp_time);
6754700c4d8SDavid Howells 			matched = true;
67650235c4bSDavid Howells 		}
677b604dd98SDavid Howells 
6784700c4d8SDavid Howells 		/* If a later serial is being acked, then mark this slot as
6794700c4d8SDavid Howells 		 * being available.
6804700c4d8SDavid Howells 		 */
6814700c4d8SDavid Howells 		if (after(acked_serial, orig_serial)) {
6824700c4d8SDavid Howells 			trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
6834700c4d8SDavid Howells 					   orig_serial, acked_serial, 0, 0);
6844700c4d8SDavid Howells 			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
6854700c4d8SDavid Howells 			smp_wmb();
6864700c4d8SDavid Howells 			set_bit(i, &call->rtt_avail);
6874700c4d8SDavid Howells 		}
6884700c4d8SDavid Howells 	}
68950235c4bSDavid Howells 
6904700c4d8SDavid Howells 	if (!matched)
6914700c4d8SDavid Howells 		trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
69250235c4bSDavid Howells }
69350235c4bSDavid Howells 
69450235c4bSDavid Howells /*
695248f219cSDavid Howells  * Process the extra information that may be appended to an ACK packet
696248f219cSDavid Howells  */
rxrpc_input_ack_trailer(struct rxrpc_call * call,struct sk_buff * skb,struct rxrpc_acktrailer * trailer)69759881e57SDavid Howells static void rxrpc_input_ack_trailer(struct rxrpc_call *call, struct sk_buff *skb,
69859881e57SDavid Howells 				    struct rxrpc_acktrailer *trailer)
699248f219cSDavid Howells {
700248f219cSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
701248f219cSDavid Howells 	struct rxrpc_peer *peer;
702248f219cSDavid Howells 	unsigned int mtu;
703702f2ac8SDavid Howells 	bool wake = false;
70459881e57SDavid Howells 	u32 rwind = ntohl(trailer->rwind);
705248f219cSDavid Howells 
706a4ea4c47SDavid Howells 	if (rwind > RXRPC_TX_MAX_WINDOW)
707a4ea4c47SDavid Howells 		rwind = RXRPC_TX_MAX_WINDOW;
708a2ad7c21SDavid Howells 	if (call->tx_winsize != rwind) {
709702f2ac8SDavid Howells 		if (rwind > call->tx_winsize)
710702f2ac8SDavid Howells 			wake = true;
711a2ad7c21SDavid Howells 		trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
71201fd0742SDavid Howells 		call->tx_winsize = rwind;
713702f2ac8SDavid Howells 	}
714702f2ac8SDavid Howells 
71559881e57SDavid Howells 	mtu = min(ntohl(trailer->maxMTU), ntohl(trailer->ifMTU));
716248f219cSDavid Howells 
717248f219cSDavid Howells 	peer = call->peer;
718248f219cSDavid Howells 	if (mtu < peer->maxdata) {
7193dd9c8b5SDavid Howells 		spin_lock(&peer->lock);
720248f219cSDavid Howells 		peer->maxdata = mtu;
721248f219cSDavid Howells 		peer->mtu = mtu + peer->hdrsize;
7223dd9c8b5SDavid Howells 		spin_unlock(&peer->lock);
723248f219cSDavid Howells 	}
724702f2ac8SDavid Howells 
725702f2ac8SDavid Howells 	if (wake)
726702f2ac8SDavid Howells 		wake_up(&call->waitq);
727248f219cSDavid Howells }
728248f219cSDavid Howells 
729248f219cSDavid Howells /*
730024b2511SDavid Howells  * Determine how many nacks from the previous ACK have now been satisfied.
731024b2511SDavid Howells  */
rxrpc_input_check_prev_ack(struct rxrpc_call * call,struct rxrpc_ack_summary * summary,rxrpc_seq_t seq)732024b2511SDavid Howells static rxrpc_seq_t rxrpc_input_check_prev_ack(struct rxrpc_call *call,
733024b2511SDavid Howells 					      struct rxrpc_ack_summary *summary,
734024b2511SDavid Howells 					      rxrpc_seq_t seq)
735024b2511SDavid Howells {
736024b2511SDavid Howells 	struct sk_buff *skb = call->cong_last_nack;
737024b2511SDavid Howells 	struct rxrpc_ackpacket ack;
738024b2511SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
739024b2511SDavid Howells 	unsigned int i, new_acks = 0, retained_nacks = 0;
740024b2511SDavid Howells 	rxrpc_seq_t old_seq = sp->first_ack;
741024b2511SDavid Howells 	u8 *acks = skb->data + sizeof(struct rxrpc_wire_header) + sizeof(ack);
742024b2511SDavid Howells 
743024b2511SDavid Howells 	if (after_eq(seq, old_seq + sp->nr_acks)) {
744024b2511SDavid Howells 		summary->nr_new_acks += sp->nr_nacks;
745024b2511SDavid Howells 		summary->nr_new_acks += seq - (old_seq + sp->nr_acks);
746024b2511SDavid Howells 		summary->nr_retained_nacks = 0;
747024b2511SDavid Howells 	} else if (seq == old_seq) {
748024b2511SDavid Howells 		summary->nr_retained_nacks = sp->nr_nacks;
749024b2511SDavid Howells 	} else {
750024b2511SDavid Howells 		for (i = 0; i < sp->nr_acks; i++) {
751024b2511SDavid Howells 			if (acks[i] == RXRPC_ACK_TYPE_NACK) {
752024b2511SDavid Howells 				if (before(old_seq + i, seq))
753024b2511SDavid Howells 					new_acks++;
754024b2511SDavid Howells 				else
755024b2511SDavid Howells 					retained_nacks++;
756024b2511SDavid Howells 			}
757024b2511SDavid Howells 		}
758024b2511SDavid Howells 
759024b2511SDavid Howells 		summary->nr_new_acks += new_acks;
760024b2511SDavid Howells 		summary->nr_retained_nacks = retained_nacks;
761024b2511SDavid Howells 	}
762024b2511SDavid Howells 
763024b2511SDavid Howells 	return old_seq + sp->nr_acks;
764024b2511SDavid Howells }
765024b2511SDavid Howells 
766024b2511SDavid Howells /*
767248f219cSDavid Howells  * Process individual soft ACKs.
768248f219cSDavid Howells  *
769248f219cSDavid Howells  * Each ACK in the array corresponds to one packet and can be either an ACK or
770248f219cSDavid Howells  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
771248f219cSDavid Howells  * packets that lie beyond the end of the ACK list are scheduled for resend by
772248f219cSDavid Howells  * the timer on the basis that the peer might just not have processed them at
773248f219cSDavid Howells  * the time the ACK was sent.
774248f219cSDavid Howells  */
rxrpc_input_soft_acks(struct rxrpc_call * call,struct rxrpc_ack_summary * summary,struct sk_buff * skb,rxrpc_seq_t seq,rxrpc_seq_t since)775024b2511SDavid Howells static void rxrpc_input_soft_acks(struct rxrpc_call *call,
776024b2511SDavid Howells 				  struct rxrpc_ack_summary *summary,
777024b2511SDavid Howells 				  struct sk_buff *skb,
778024b2511SDavid Howells 				  rxrpc_seq_t seq,
779024b2511SDavid Howells 				  rxrpc_seq_t since)
780248f219cSDavid Howells {
781024b2511SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
782024b2511SDavid Howells 	unsigned int i, old_nacks = 0;
783024b2511SDavid Howells 	rxrpc_seq_t lowest_nak = seq + sp->nr_acks;
784024b2511SDavid Howells 	u8 *acks = skb->data + sizeof(struct rxrpc_wire_header) + sizeof(struct rxrpc_ackpacket);
785248f219cSDavid Howells 
786024b2511SDavid Howells 	for (i = 0; i < sp->nr_acks; i++) {
787d57a3a15SDavid Howells 		if (acks[i] == RXRPC_ACK_TYPE_ACK) {
78831a1b989SDavid Howells 			summary->nr_acks++;
789024b2511SDavid Howells 			if (after_eq(seq, since))
79031a1b989SDavid Howells 				summary->nr_new_acks++;
791d57a3a15SDavid Howells 		} else {
792024b2511SDavid Howells 			summary->saw_nacks = true;
793024b2511SDavid Howells 			if (before(seq, since)) {
794024b2511SDavid Howells 				/* Overlap with previous ACK */
795024b2511SDavid Howells 				old_nacks++;
796024b2511SDavid Howells 			} else {
797024b2511SDavid Howells 				summary->nr_new_nacks++;
798024b2511SDavid Howells 				sp->nr_nacks++;
799024b2511SDavid Howells 			}
800024b2511SDavid Howells 
801024b2511SDavid Howells 			if (before(seq, lowest_nak))
802024b2511SDavid Howells 				lowest_nak = seq;
803024b2511SDavid Howells 		}
804024b2511SDavid Howells 		seq++;
805024b2511SDavid Howells 	}
806024b2511SDavid Howells 
807024b2511SDavid Howells 	if (lowest_nak != call->acks_lowest_nak) {
808024b2511SDavid Howells 		call->acks_lowest_nak = lowest_nak;
80931a1b989SDavid Howells 		summary->new_low_nack = true;
81031a1b989SDavid Howells 	}
811024b2511SDavid Howells 
812024b2511SDavid Howells 	/* We *can* have more nacks than we did - the peer is permitted to drop
813024b2511SDavid Howells 	 * packets it has soft-acked and re-request them.  Further, it is
814024b2511SDavid Howells 	 * possible for the nack distribution to change whilst the number of
815024b2511SDavid Howells 	 * nacks stays the same or goes down.
816024b2511SDavid Howells 	 */
817024b2511SDavid Howells 	if (old_nacks < summary->nr_retained_nacks)
818024b2511SDavid Howells 		summary->nr_new_acks += summary->nr_retained_nacks - old_nacks;
819024b2511SDavid Howells 	summary->nr_retained_nacks = old_nacks;
820248f219cSDavid Howells }
821248f219cSDavid Howells 
822248f219cSDavid Howells /*
823441fdee1SDavid Howells  * Return true if the ACK is valid - ie. it doesn't appear to have regressed
824441fdee1SDavid Howells  * with respect to the ack state conveyed by preceding ACKs.
825441fdee1SDavid Howells  */
rxrpc_is_ack_valid(struct rxrpc_call * call,rxrpc_seq_t first_pkt,rxrpc_seq_t prev_pkt)826441fdee1SDavid Howells static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
827441fdee1SDavid Howells 			       rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
828441fdee1SDavid Howells {
8298940ba3cSDavid Howells 	rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
830441fdee1SDavid Howells 
831441fdee1SDavid Howells 	if (after(first_pkt, base))
832441fdee1SDavid Howells 		return true; /* The window advanced */
833441fdee1SDavid Howells 
834441fdee1SDavid Howells 	if (before(first_pkt, base))
835441fdee1SDavid Howells 		return false; /* firstPacket regressed */
836441fdee1SDavid Howells 
8378940ba3cSDavid Howells 	if (after_eq(prev_pkt, call->acks_prev_seq))
838441fdee1SDavid Howells 		return true; /* previousPacket hasn't regressed. */
839441fdee1SDavid Howells 
840441fdee1SDavid Howells 	/* Some rx implementations put a serial number in previousPacket. */
841441fdee1SDavid Howells 	if (after_eq(prev_pkt, base + call->tx_winsize))
842441fdee1SDavid Howells 		return false;
843441fdee1SDavid Howells 	return true;
844441fdee1SDavid Howells }
845441fdee1SDavid Howells 
846441fdee1SDavid Howells /*
847248f219cSDavid Howells  * Process an ACK packet.
848248f219cSDavid Howells  *
849248f219cSDavid Howells  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
850248f219cSDavid Howells  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
851248f219cSDavid Howells  *
852248f219cSDavid Howells  * A hard-ACK means that a packet has been processed and may be discarded; a
853248f219cSDavid Howells  * soft-ACK means that the packet may be discarded and retransmission
854248f219cSDavid Howells  * requested.  A phase is complete when all packets are hard-ACK'd.
855248f219cSDavid Howells  */
rxrpc_input_ack(struct rxrpc_call * call,struct sk_buff * skb)856e8c3af6bSDavid Howells static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
857248f219cSDavid Howells {
85831a1b989SDavid Howells 	struct rxrpc_ack_summary summary = { 0 };
859248f219cSDavid Howells 	struct rxrpc_ackpacket ack;
860d57a3a15SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
86159881e57SDavid Howells 	struct rxrpc_acktrailer trailer;
86268528d93SDavid Howells 	rxrpc_serial_t ack_serial, acked_serial;
863024b2511SDavid Howells 	rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt, since;
864775e5b71SDavid Howells 	int nr_acks, offset, ioffset;
865248f219cSDavid Howells 
866248f219cSDavid Howells 	_enter("");
867248f219cSDavid Howells 
868775e5b71SDavid Howells 	offset = sizeof(struct rxrpc_wire_header);
8695e6ef4f1SDavid Howells 	if (skb_copy_bits(skb, offset, &ack, sizeof(ack)) < 0)
87057af281eSDavid Howells 		return rxrpc_proto_abort(call, 0, rxrpc_badmsg_short_ack);
871d57a3a15SDavid Howells 	offset += sizeof(ack);
872248f219cSDavid Howells 
87368528d93SDavid Howells 	ack_serial = sp->hdr.serial;
874d57a3a15SDavid Howells 	acked_serial = ntohl(ack.serial);
875d57a3a15SDavid Howells 	first_soft_ack = ntohl(ack.firstPacket);
876d57a3a15SDavid Howells 	prev_pkt = ntohl(ack.previousPacket);
877248f219cSDavid Howells 	hard_ack = first_soft_ack - 1;
878d57a3a15SDavid Howells 	nr_acks = ack.nAcks;
879024b2511SDavid Howells 	sp->first_ack = first_soft_ack;
880024b2511SDavid Howells 	sp->nr_acks = nr_acks;
881d57a3a15SDavid Howells 	summary.ack_reason = (ack.reason < RXRPC_ACK__INVALID ?
882d57a3a15SDavid Howells 			      ack.reason : RXRPC_ACK__INVALID);
883248f219cSDavid Howells 
88468528d93SDavid Howells 	trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
8851a2391c3SJeffrey Altman 			   first_soft_ack, prev_pkt,
886b1d9f7fdSDavid Howells 			   summary.ack_reason, nr_acks);
887d57a3a15SDavid Howells 	rxrpc_inc_stat(call->rxnet, stat_rx_acks[ack.reason]);
888ec71eb9aSDavid Howells 
889d299ab02SDavid Howells 	if (acked_serial != 0) {
890d57a3a15SDavid Howells 		switch (ack.reason) {
8914700c4d8SDavid Howells 		case RXRPC_ACK_PING_RESPONSE:
8924700c4d8SDavid Howells 			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
8934700c4d8SDavid Howells 						 rxrpc_rtt_rx_ping_response);
8944700c4d8SDavid Howells 			break;
8954700c4d8SDavid Howells 		case RXRPC_ACK_REQUESTED:
8964700c4d8SDavid Howells 			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
8974700c4d8SDavid Howells 						 rxrpc_rtt_rx_requested_ack);
8984700c4d8SDavid Howells 			break;
8994700c4d8SDavid Howells 		default:
9004700c4d8SDavid Howells 			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
901d299ab02SDavid Howells 						 rxrpc_rtt_rx_other_ack);
9024700c4d8SDavid Howells 			break;
9034700c4d8SDavid Howells 		}
904d299ab02SDavid Howells 	}
9058e83134dSDavid Howells 
906adc9613fSDavid Howells 	/* If we get an EXCEEDS_WINDOW ACK from the server, it probably
907adc9613fSDavid Howells 	 * indicates that the client address changed due to NAT.  The server
908adc9613fSDavid Howells 	 * lost the call because it switched to a different peer.
909adc9613fSDavid Howells 	 */
910d57a3a15SDavid Howells 	if (unlikely(ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
911adc9613fSDavid Howells 	    first_soft_ack == 1 &&
912adc9613fSDavid Howells 	    prev_pkt == 0 &&
913adc9613fSDavid Howells 	    rxrpc_is_client_call(call)) {
914adc9613fSDavid Howells 		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
915adc9613fSDavid Howells 					  0, -ENETRESET);
91628612371SDavid Howells 		goto send_response;
917adc9613fSDavid Howells 	}
918adc9613fSDavid Howells 
919adc9613fSDavid Howells 	/* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
920adc9613fSDavid Howells 	 * indicate a change of address.  However, we can retransmit the call
921adc9613fSDavid Howells 	 * if we still have it buffered to the beginning.
922adc9613fSDavid Howells 	 */
923d57a3a15SDavid Howells 	if (unlikely(ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
924adc9613fSDavid Howells 	    first_soft_ack == 1 &&
925adc9613fSDavid Howells 	    prev_pkt == 0 &&
926a4ea4c47SDavid Howells 	    call->acks_hard_ack == 0 &&
927adc9613fSDavid Howells 	    rxrpc_is_client_call(call)) {
928adc9613fSDavid Howells 		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
929adc9613fSDavid Howells 					  0, -ENETRESET);
93028612371SDavid Howells 		goto send_response;
931adc9613fSDavid Howells 	}
932adc9613fSDavid Howells 
9331a2391c3SJeffrey Altman 	/* Discard any out-of-order or duplicate ACKs (outside lock). */
934441fdee1SDavid Howells 	if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
93568528d93SDavid Howells 		trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
9368940ba3cSDavid Howells 					   first_soft_ack, call->acks_first_seq,
9378940ba3cSDavid Howells 					   prev_pkt, call->acks_prev_seq);
93828612371SDavid Howells 		goto send_response;
939d1f12947SDavid Howells 	}
940248f219cSDavid Howells 
94159881e57SDavid Howells 	trailer.maxMTU = 0;
942c1e15b49SDavid Howells 	ioffset = offset + nr_acks + 3;
94359881e57SDavid Howells 	if (skb->len >= ioffset + sizeof(trailer) &&
94459881e57SDavid Howells 	    skb_copy_bits(skb, ioffset, &trailer, sizeof(trailer)) < 0)
94559881e57SDavid Howells 		return rxrpc_proto_abort(call, 0, rxrpc_badmsg_short_ack_trailer);
946d57a3a15SDavid Howells 
947d57a3a15SDavid Howells 	if (nr_acks > 0)
948d57a3a15SDavid Howells 		skb_condense(skb);
949c1e15b49SDavid Howells 
950024b2511SDavid Howells 	if (call->cong_last_nack) {
951024b2511SDavid Howells 		since = rxrpc_input_check_prev_ack(call, &summary, first_soft_ack);
952024b2511SDavid Howells 		rxrpc_free_skb(call->cong_last_nack, rxrpc_skb_put_last_nack);
953024b2511SDavid Howells 		call->cong_last_nack = NULL;
954024b2511SDavid Howells 	} else {
955024b2511SDavid Howells 		summary.nr_new_acks = first_soft_ack - call->acks_first_seq;
956024b2511SDavid Howells 		call->acks_lowest_nak = first_soft_ack + nr_acks;
957024b2511SDavid Howells 		since = first_soft_ack;
958024b2511SDavid Howells 	}
959024b2511SDavid Howells 
960298bc15bSDavid Howells 	call->acks_latest_ts = skb->tstamp;
9618940ba3cSDavid Howells 	call->acks_first_seq = first_soft_ack;
9628940ba3cSDavid Howells 	call->acks_prev_seq = prev_pkt;
9631a2391c3SJeffrey Altman 
964d57a3a15SDavid Howells 	switch (ack.reason) {
965d57a3a15SDavid Howells 	case RXRPC_ACK_PING:
966d57a3a15SDavid Howells 		break;
967d57a3a15SDavid Howells 	default:
968024b2511SDavid Howells 		if (acked_serial && after(acked_serial, call->acks_highest_serial))
969589a0c1eSDavid Howells 			call->acks_highest_serial = acked_serial;
970d57a3a15SDavid Howells 		break;
971d57a3a15SDavid Howells 	}
972589a0c1eSDavid Howells 
973298bc15bSDavid Howells 	/* Parse rwind and mtu sizes if provided. */
97459881e57SDavid Howells 	if (trailer.maxMTU)
97559881e57SDavid Howells 		rxrpc_input_ack_trailer(call, skb, &trailer);
976248f219cSDavid Howells 
9775e6ef4f1SDavid Howells 	if (first_soft_ack == 0)
97857af281eSDavid Howells 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_zero);
979248f219cSDavid Howells 
980248f219cSDavid Howells 	/* Ignore ACKs unless we are or have just been transmitting. */
98196b4059fSDavid Howells 	switch (__rxrpc_call_state(call)) {
982248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
983248f219cSDavid Howells 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
984248f219cSDavid Howells 	case RXRPC_CALL_SERVER_SEND_REPLY:
985248f219cSDavid Howells 	case RXRPC_CALL_SERVER_AWAIT_ACK:
986248f219cSDavid Howells 		break;
987248f219cSDavid Howells 	default:
98828612371SDavid Howells 		goto send_response;
989248f219cSDavid Howells 	}
990248f219cSDavid Howells 
991a4ea4c47SDavid Howells 	if (before(hard_ack, call->acks_hard_ack) ||
9925e6ef4f1SDavid Howells 	    after(hard_ack, call->tx_top))
99357af281eSDavid Howells 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_outside_window);
9945e6ef4f1SDavid Howells 	if (nr_acks > call->tx_top - hard_ack)
99557af281eSDavid Howells 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_sack_overflow);
996248f219cSDavid Howells 
997a4ea4c47SDavid Howells 	if (after(hard_ack, call->acks_hard_ack)) {
998c479d5f2SDavid Howells 		if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
99957af281eSDavid Howells 			rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ack);
100028612371SDavid Howells 			goto send_response;
1001c479d5f2SDavid Howells 		}
1002c479d5f2SDavid Howells 	}
1003248f219cSDavid Howells 
1004248f219cSDavid Howells 	if (nr_acks > 0) {
10055e6ef4f1SDavid Howells 		if (offset > (int)skb->len - nr_acks)
100657af281eSDavid Howells 			return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_short_sack);
1007024b2511SDavid Howells 		rxrpc_input_soft_acks(call, &summary, skb, first_soft_ack, since);
1008024b2511SDavid Howells 		rxrpc_get_skb(skb, rxrpc_skb_get_last_nack);
1009024b2511SDavid Howells 		call->cong_last_nack = skb;
101070790dbeSDavid Howells 	}
101170790dbeSDavid Howells 
1012a4ea4c47SDavid Howells 	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
1013a9f312d9SDavid Howells 	    summary.nr_acks == call->tx_top - hard_ack &&
1014a9f312d9SDavid Howells 	    rxrpc_is_client_call(call))
101572f0c6fbSDavid Howells 		rxrpc_propose_ping(call, ack_serial,
10160d967960SDavid Howells 				   rxrpc_propose_ack_ping_for_lost_reply);
101757494343SDavid Howells 
1018c1e15b49SDavid Howells 	rxrpc_congestion_management(call, skb, &summary, acked_serial);
101928612371SDavid Howells 
102028612371SDavid Howells send_response:
102128612371SDavid Howells 	if (ack.reason == RXRPC_ACK_PING)
102228612371SDavid Howells 		rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
102328612371SDavid Howells 			       rxrpc_propose_ack_respond_to_ping);
102428612371SDavid Howells 	else if (sp->hdr.flags & RXRPC_REQUEST_ACK)
102528612371SDavid Howells 		rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
102628612371SDavid Howells 			       rxrpc_propose_ack_respond_to_ack);
102770790dbeSDavid Howells }
102870790dbeSDavid Howells 
1029248f219cSDavid Howells /*
1030248f219cSDavid Howells  * Process an ACKALL packet.
1031248f219cSDavid Howells  */
rxrpc_input_ackall(struct rxrpc_call * call,struct sk_buff * skb)1032248f219cSDavid Howells static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1033248f219cSDavid Howells {
103431a1b989SDavid Howells 	struct rxrpc_ack_summary summary = { 0 };
1035248f219cSDavid Howells 
1036c479d5f2SDavid Howells 	if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
103757af281eSDavid Howells 		rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ackall);
1038248f219cSDavid Howells }
1039248f219cSDavid Howells 
1040248f219cSDavid Howells /*
1041005ede28SDavid Howells  * Process an ABORT packet directed at a call.
1042248f219cSDavid Howells  */
rxrpc_input_abort(struct rxrpc_call * call,struct sk_buff * skb)1043248f219cSDavid Howells static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
10448c3e34a4SDavid Howells {
10458c3e34a4SDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1046248f219cSDavid Howells 
1047f14febd8SDavid Howells 	trace_rxrpc_rx_abort(call, sp->hdr.serial, skb->priority);
1048005ede28SDavid Howells 
10495ac0d622SDavid Howells 	rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1050f14febd8SDavid Howells 				  skb->priority, -ECONNABORTED);
1051248f219cSDavid Howells }
1052248f219cSDavid Howells 
1053248f219cSDavid Howells /*
1054248f219cSDavid Howells  * Process an incoming call packet.
1055248f219cSDavid Howells  */
rxrpc_input_call_packet(struct rxrpc_call * call,struct sk_buff * skb)10565e6ef4f1SDavid Howells void rxrpc_input_call_packet(struct rxrpc_call *call, struct sk_buff *skb)
1057248f219cSDavid Howells {
1058248f219cSDavid Howells 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1059a158bdd3SDavid Howells 	unsigned long timo;
10608c3e34a4SDavid Howells 
10618c3e34a4SDavid Howells 	_enter("%p,%p", call, skb);
10628c3e34a4SDavid Howells 
10635e6ef4f1SDavid Howells 	if (sp->hdr.serviceId != call->dest_srx.srx_service)
10645e6ef4f1SDavid Howells 		call->dest_srx.srx_service = sp->hdr.serviceId;
10655e6ef4f1SDavid Howells 	if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
10665e6ef4f1SDavid Howells 		call->rx_serial = sp->hdr.serial;
10675e6ef4f1SDavid Howells 	if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
10685e6ef4f1SDavid Howells 		set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
10695e6ef4f1SDavid Howells 
1070a158bdd3SDavid Howells 	timo = READ_ONCE(call->next_rx_timo);
1071a158bdd3SDavid Howells 	if (timo) {
1072a158bdd3SDavid Howells 		unsigned long now = jiffies, expect_rx_by;
1073a158bdd3SDavid Howells 
1074c54e43d7SDavid Howells 		expect_rx_by = now + timo;
1075a158bdd3SDavid Howells 		WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1076a158bdd3SDavid Howells 		rxrpc_reduce_call_timer(call, expect_rx_by, now,
1077a158bdd3SDavid Howells 					rxrpc_timer_set_for_normal);
1078a158bdd3SDavid Howells 	}
1079a158bdd3SDavid Howells 
10808c3e34a4SDavid Howells 	switch (sp->hdr.type) {
1081248f219cSDavid Howells 	case RXRPC_PACKET_TYPE_DATA:
108257af281eSDavid Howells 		return rxrpc_input_data(call, skb);
10838c3e34a4SDavid Howells 
1084248f219cSDavid Howells 	case RXRPC_PACKET_TYPE_ACK:
108557af281eSDavid Howells 		return rxrpc_input_ack(call, skb);
10868c3e34a4SDavid Howells 
10878c3e34a4SDavid Howells 	case RXRPC_PACKET_TYPE_BUSY:
1088248f219cSDavid Howells 		/* Just ignore BUSY packets from the server; the retry and
1089248f219cSDavid Howells 		 * lifespan timers will take care of business.  BUSY packets
1090248f219cSDavid Howells 		 * from the client don't make sense.
1091248f219cSDavid Howells 		 */
109257af281eSDavid Howells 		return;
10938c3e34a4SDavid Howells 
1094248f219cSDavid Howells 	case RXRPC_PACKET_TYPE_ABORT:
109557af281eSDavid Howells 		return rxrpc_input_abort(call, skb);
1096248f219cSDavid Howells 
1097248f219cSDavid Howells 	case RXRPC_PACKET_TYPE_ACKALL:
109857af281eSDavid Howells 		return rxrpc_input_ackall(call, skb);
10998c3e34a4SDavid Howells 
11008c3e34a4SDavid Howells 	default:
11018c3e34a4SDavid Howells 		break;
11028c3e34a4SDavid Howells 	}
11038c3e34a4SDavid Howells }
11048c3e34a4SDavid Howells 
11058c3e34a4SDavid Howells /*
1106c1e15b49SDavid Howells  * Handle a new service call on a channel implicitly completing the preceding
1107c1e15b49SDavid Howells  * call on that channel.  This does not apply to client conns.
1108b3156274SDavid Howells  *
1109b3156274SDavid Howells  * TODO: If callNumber > call_id + 1, renegotiate security.
1110b3156274SDavid Howells  */
rxrpc_implicit_end_call(struct rxrpc_call * call,struct sk_buff * skb)11115e6ef4f1SDavid Howells void rxrpc_implicit_end_call(struct rxrpc_call *call, struct sk_buff *skb)
1112b3156274SDavid Howells {
111396b4059fSDavid Howells 	switch (__rxrpc_call_state(call)) {
1114b3156274SDavid Howells 	case RXRPC_CALL_SERVER_AWAIT_ACK:
1115b3156274SDavid Howells 		rxrpc_call_completed(call);
1116df561f66SGustavo A. R. Silva 		fallthrough;
1117b3156274SDavid Howells 	case RXRPC_CALL_COMPLETE:
1118b3156274SDavid Howells 		break;
1119b3156274SDavid Howells 	default:
112057af281eSDavid Howells 		rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ESHUTDOWN,
112157af281eSDavid Howells 				 rxrpc_eproto_improper_term);
1122c1e15b49SDavid Howells 		trace_rxrpc_improper_term(call);
1123b3156274SDavid Howells 		break;
1124b3156274SDavid Howells 	}
1125b3156274SDavid Howells 
11265e6ef4f1SDavid Howells 	rxrpc_input_call_event(call, skb);
1127b3156274SDavid Howells }
1128