xref: /linux/net/rxrpc/input.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Processing of received RxRPC packets
3  *
4  * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 
10 #include "ar-internal.h"
11 
12 /* Override priority when generating ACKs for received DATA */
13 static const u8 rxrpc_ack_priority[RXRPC_ACK__INVALID] = {
14 	[RXRPC_ACK_IDLE]		= 1,
15 	[RXRPC_ACK_DELAY]		= 2,
16 	[RXRPC_ACK_REQUESTED]		= 3,
17 	[RXRPC_ACK_DUPLICATE]		= 4,
18 	[RXRPC_ACK_EXCEEDS_WINDOW]	= 5,
19 	[RXRPC_ACK_NOSPACE]		= 6,
20 	[RXRPC_ACK_OUT_OF_SEQUENCE]	= 7,
21 };
22 
23 static void rxrpc_proto_abort(struct rxrpc_call *call, rxrpc_seq_t seq,
24 			      enum rxrpc_abort_reason why)
25 {
26 	rxrpc_abort_call(call, seq, RX_PROTOCOL_ERROR, -EBADMSG, why);
27 }
28 
29 /*
30  * Do TCP-style congestion management [RFC5681].
31  */
32 static void rxrpc_congestion_management(struct rxrpc_call *call,
33 					struct rxrpc_ack_summary *summary)
34 {
35 	summary->change = rxrpc_cong_no_change;
36 	summary->in_flight = rxrpc_tx_in_flight(call);
37 
38 	if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
39 		summary->retrans_timeo = true;
40 		call->cong_ssthresh = umax(summary->in_flight / 2, 2);
41 		call->cong_cwnd = 1;
42 		if (call->cong_cwnd >= call->cong_ssthresh &&
43 		    call->cong_ca_state == RXRPC_CA_SLOW_START) {
44 			call->cong_ca_state = RXRPC_CA_CONGEST_AVOIDANCE;
45 			call->cong_tstamp = call->acks_latest_ts;
46 			call->cong_cumul_acks = 0;
47 		}
48 	}
49 
50 	call->cong_cumul_acks += summary->nr_new_sacks;
51 	call->cong_cumul_acks += summary->nr_new_hacks;
52 	if (call->cong_cumul_acks > 255)
53 		call->cong_cumul_acks = 255;
54 
55 	switch (call->cong_ca_state) {
56 	case RXRPC_CA_SLOW_START:
57 		if (call->acks_nr_snacks > 0)
58 			goto packet_loss_detected;
59 		if (call->cong_cumul_acks > 0)
60 			call->cong_cwnd += 1;
61 		if (call->cong_cwnd >= call->cong_ssthresh) {
62 			call->cong_ca_state = RXRPC_CA_CONGEST_AVOIDANCE;
63 			call->cong_tstamp = call->acks_latest_ts;
64 		}
65 		goto out;
66 
67 	case RXRPC_CA_CONGEST_AVOIDANCE:
68 		if (call->acks_nr_snacks > 0)
69 			goto packet_loss_detected;
70 
71 		/* We analyse the number of packets that get ACK'd per RTT
72 		 * period and increase the window if we managed to fill it.
73 		 */
74 		if (call->rtt_count == 0)
75 			goto out;
76 		if (ktime_before(call->acks_latest_ts,
77 				 ktime_add_us(call->cong_tstamp,
78 					      call->srtt_us >> 3)))
79 			goto out_no_clear_ca;
80 		summary->change = rxrpc_cong_rtt_window_end;
81 		call->cong_tstamp = call->acks_latest_ts;
82 		if (call->cong_cumul_acks >= call->cong_cwnd)
83 			call->cong_cwnd++;
84 		goto out;
85 
86 	case RXRPC_CA_PACKET_LOSS:
87 		if (call->acks_nr_snacks == 0)
88 			goto resume_normality;
89 
90 		if (summary->new_low_snack) {
91 			summary->change = rxrpc_cong_new_low_nack;
92 			call->cong_dup_acks = 1;
93 			if (call->cong_extra > 1)
94 				call->cong_extra = 1;
95 			goto send_extra_data;
96 		}
97 
98 		call->cong_dup_acks++;
99 		if (call->cong_dup_acks < 3)
100 			goto send_extra_data;
101 
102 		summary->change = rxrpc_cong_begin_retransmission;
103 		call->cong_ca_state = RXRPC_CA_FAST_RETRANSMIT;
104 		call->cong_ssthresh = umax(summary->in_flight / 2, 2);
105 		call->cong_cwnd = call->cong_ssthresh + 3;
106 		call->cong_extra = 0;
107 		call->cong_dup_acks = 0;
108 		summary->need_retransmit = true;
109 		summary->in_fast_or_rto_recovery = true;
110 		goto out;
111 
112 	case RXRPC_CA_FAST_RETRANSMIT:
113 		rxrpc_tlp_init(call);
114 		summary->in_fast_or_rto_recovery = true;
115 		if (!summary->new_low_snack) {
116 			if (summary->nr_new_sacks == 0)
117 				call->cong_cwnd += 1;
118 			call->cong_dup_acks++;
119 			if (call->cong_dup_acks == 2) {
120 				summary->change = rxrpc_cong_retransmit_again;
121 				call->cong_dup_acks = 0;
122 				summary->need_retransmit = true;
123 			}
124 		} else {
125 			summary->change = rxrpc_cong_progress;
126 			call->cong_cwnd = call->cong_ssthresh;
127 			if (call->acks_nr_snacks == 0) {
128 				summary->exiting_fast_or_rto_recovery = true;
129 				goto resume_normality;
130 			}
131 		}
132 		goto out;
133 
134 	default:
135 		BUG();
136 		goto out;
137 	}
138 
139 resume_normality:
140 	summary->change = rxrpc_cong_cleared_nacks;
141 	call->cong_dup_acks = 0;
142 	call->cong_extra = 0;
143 	call->cong_tstamp = call->acks_latest_ts;
144 	if (call->cong_cwnd < call->cong_ssthresh)
145 		call->cong_ca_state = RXRPC_CA_SLOW_START;
146 	else
147 		call->cong_ca_state = RXRPC_CA_CONGEST_AVOIDANCE;
148 out:
149 	call->cong_cumul_acks = 0;
150 out_no_clear_ca:
151 	if (call->cong_cwnd >= RXRPC_TX_MAX_WINDOW)
152 		call->cong_cwnd = RXRPC_TX_MAX_WINDOW;
153 	trace_rxrpc_congest(call, summary);
154 	return;
155 
156 packet_loss_detected:
157 	summary->change = rxrpc_cong_saw_nack;
158 	call->cong_ca_state = RXRPC_CA_PACKET_LOSS;
159 	call->cong_dup_acks = 0;
160 	goto send_extra_data;
161 
162 send_extra_data:
163 	/* Send some previously unsent DATA if we have some to advance the ACK
164 	 * state.
165 	 */
166 	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
167 	    call->acks_nr_sacks != call->tx_top - call->tx_bottom) {
168 		call->cong_extra++;
169 		wake_up(&call->waitq);
170 	}
171 	goto out_no_clear_ca;
172 }
173 
174 /*
175  * Degrade the congestion window if we haven't transmitted a packet for >1RTT.
176  */
177 void rxrpc_congestion_degrade(struct rxrpc_call *call)
178 {
179 	ktime_t rtt, now, time_since;
180 
181 	if (call->cong_ca_state != RXRPC_CA_SLOW_START &&
182 	    call->cong_ca_state != RXRPC_CA_CONGEST_AVOIDANCE)
183 		return;
184 	if (__rxrpc_call_state(call) == RXRPC_CALL_CLIENT_AWAIT_REPLY)
185 		return;
186 
187 	rtt = ns_to_ktime(call->srtt_us * (NSEC_PER_USEC / 8));
188 	now = ktime_get_real();
189 	time_since = ktime_sub(now, call->tx_last_sent);
190 	if (ktime_before(time_since, rtt))
191 		return;
192 
193 	trace_rxrpc_reset_cwnd(call, time_since, rtt);
194 	rxrpc_inc_stat(call->rxnet, stat_tx_data_cwnd_reset);
195 	call->tx_last_sent = now;
196 	call->cong_ca_state = RXRPC_CA_SLOW_START;
197 	call->cong_ssthresh = umax(call->cong_ssthresh, call->cong_cwnd * 3 / 4);
198 	call->cong_cwnd = umax(call->cong_cwnd / 2, RXRPC_MIN_CWND);
199 }
200 
201 /*
202  * Add an RTT sample derived from an ACK'd DATA packet.
203  */
204 static void rxrpc_add_data_rtt_sample(struct rxrpc_call *call,
205 				      struct rxrpc_ack_summary *summary,
206 				      struct rxrpc_txqueue *tq,
207 				      int ix)
208 {
209 	ktime_t xmit_ts = ktime_add_us(tq->xmit_ts_base, tq->segment_xmit_ts[ix]);
210 
211 	rxrpc_call_add_rtt(call, rxrpc_rtt_rx_data_ack, -1,
212 			   summary->acked_serial, summary->ack_serial,
213 			   xmit_ts, call->acks_latest_ts);
214 	__clear_bit(ix, &tq->rtt_samples); /* Prevent repeat RTT sample */
215 }
216 
217 /*
218  * Apply a hard ACK by advancing the Tx window.
219  */
220 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
221 				   struct rxrpc_ack_summary *summary)
222 {
223 	struct rxrpc_txqueue *tq = call->tx_queue;
224 	rxrpc_seq_t seq = call->tx_bottom + 1;
225 	bool rot_last = false, trace = false;
226 
227 	_enter("%x,%x", call->tx_bottom, to);
228 
229 	trace_rxrpc_tx_rotate(call, seq, to);
230 	trace_rxrpc_tq(call, tq, seq, rxrpc_tq_rotate);
231 
232 	if (call->acks_lowest_nak == call->tx_bottom) {
233 		call->acks_lowest_nak = to;
234 	} else if (after(to, call->acks_lowest_nak)) {
235 		summary->new_low_snack = true;
236 		call->acks_lowest_nak = to;
237 	}
238 
239 	/* We may have a left over fully-consumed buffer at the front that we
240 	 * couldn't drop before (rotate_and_keep below).
241 	 */
242 	if (seq == call->tx_qbase + RXRPC_NR_TXQUEUE) {
243 		call->tx_qbase += RXRPC_NR_TXQUEUE;
244 		call->tx_queue = tq->next;
245 		trace_rxrpc_tq(call, tq, seq, rxrpc_tq_rotate_and_free);
246 		kfree(tq);
247 		tq = call->tx_queue;
248 	}
249 
250 	do {
251 		unsigned int ix = seq - call->tx_qbase;
252 
253 		_debug("tq=%x seq=%x i=%d f=%x", tq->qbase, seq, ix, tq->bufs[ix]->flags);
254 		if (tq->bufs[ix]->flags & RXRPC_LAST_PACKET) {
255 			set_bit(RXRPC_CALL_TX_LAST, &call->flags);
256 			rot_last = true;
257 		}
258 
259 		if (summary->acked_serial == tq->segment_serial[ix] &&
260 		    test_bit(ix, &tq->rtt_samples))
261 			rxrpc_add_data_rtt_sample(call, summary, tq, ix);
262 
263 		if (ix == tq->nr_reported_acks) {
264 			/* Packet directly hard ACK'd. */
265 			tq->nr_reported_acks++;
266 			rxrpc_input_rack_one(call, summary, tq, ix);
267 			if (seq == call->tlp_seq)
268 				summary->tlp_probe_acked = true;
269 			summary->nr_new_hacks++;
270 			__set_bit(ix, &tq->segment_acked);
271 			trace_rxrpc_rotate(call, tq, summary, seq, rxrpc_rotate_trace_hack);
272 		} else if (test_bit(ix, &tq->segment_acked)) {
273 			/* Soft ACK -> hard ACK. */
274 			call->acks_nr_sacks--;
275 			trace_rxrpc_rotate(call, tq, summary, seq, rxrpc_rotate_trace_sack);
276 		} else {
277 			/* Soft NAK -> hard ACK. */
278 			call->acks_nr_snacks--;
279 			rxrpc_input_rack_one(call, summary, tq, ix);
280 			if (seq == call->tlp_seq)
281 				summary->tlp_probe_acked = true;
282 			summary->nr_new_hacks++;
283 			__set_bit(ix, &tq->segment_acked);
284 			trace_rxrpc_rotate(call, tq, summary, seq, rxrpc_rotate_trace_snak);
285 		}
286 
287 		call->tx_nr_sent--;
288 		if (__test_and_clear_bit(ix, &tq->segment_lost))
289 			call->tx_nr_lost--;
290 		if (__test_and_clear_bit(ix, &tq->segment_retransmitted))
291 			call->tx_nr_resent--;
292 		__clear_bit(ix, &tq->ever_retransmitted);
293 
294 		rxrpc_put_txbuf(tq->bufs[ix], rxrpc_txbuf_put_rotated);
295 		tq->bufs[ix] = NULL;
296 
297 		WRITE_ONCE(call->tx_bottom, seq);
298 		trace_rxrpc_txqueue(call, (rot_last ?
299 					   rxrpc_txqueue_rotate_last :
300 					   rxrpc_txqueue_rotate));
301 
302 		seq++;
303 		trace = true;
304 		if (!(seq & RXRPC_TXQ_MASK)) {
305 			trace_rxrpc_rack_update(call, summary);
306 			trace = false;
307 			prefetch(tq->next);
308 			if (tq != call->tx_qtail) {
309 				call->tx_qbase += RXRPC_NR_TXQUEUE;
310 				call->tx_queue = tq->next;
311 				trace_rxrpc_tq(call, tq, seq, rxrpc_tq_rotate_and_free);
312 				kfree(tq);
313 				tq = call->tx_queue;
314 			} else {
315 				trace_rxrpc_tq(call, tq, seq, rxrpc_tq_rotate_and_keep);
316 				tq = NULL;
317 				break;
318 			}
319 		}
320 
321 	} while (before_eq(seq, to));
322 
323 	if (trace)
324 		trace_rxrpc_rack_update(call, summary);
325 
326 	if (rot_last) {
327 		set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
328 		if (tq) {
329 			trace_rxrpc_tq(call, tq, seq, rxrpc_tq_rotate_and_free);
330 			kfree(tq);
331 			call->tx_queue = NULL;
332 		}
333 	}
334 
335 	_debug("%x,%x,%x,%d", to, call->tx_bottom, call->tx_top, rot_last);
336 
337 	wake_up(&call->waitq);
338 	return rot_last;
339 }
340 
341 /*
342  * End the transmission phase of a call.
343  *
344  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
345  * or a final ACK packet.
346  */
347 static void rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
348 			       enum rxrpc_abort_reason abort_why)
349 {
350 	ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
351 
352 	call->rack_timer_mode = RXRPC_CALL_RACKTIMER_OFF;
353 	call->rack_timo_at = KTIME_MAX;
354 	trace_rxrpc_rack_timer(call, 0, false);
355 	trace_rxrpc_timer_can(call, rxrpc_timer_trace_rack_off + call->rack_timer_mode);
356 
357 	switch (__rxrpc_call_state(call)) {
358 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
359 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
360 		if (reply_begun) {
361 			rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_RECV_REPLY);
362 			trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
363 			break;
364 		}
365 
366 		rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
367 		trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
368 		break;
369 
370 	case RXRPC_CALL_SERVER_AWAIT_ACK:
371 		rxrpc_call_completed(call);
372 		trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
373 		break;
374 
375 	default:
376 		kdebug("end_tx %s", rxrpc_call_states[__rxrpc_call_state(call)]);
377 		rxrpc_proto_abort(call, call->tx_top, abort_why);
378 		break;
379 	}
380 }
381 
382 /*
383  * Begin the reply reception phase of a call.
384  */
385 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
386 {
387 	struct rxrpc_ack_summary summary = { 0 };
388 	rxrpc_seq_t top = READ_ONCE(call->tx_top);
389 
390 	if (call->ackr_reason) {
391 		call->delay_ack_at = KTIME_MAX;
392 		trace_rxrpc_timer_can(call, rxrpc_timer_trace_delayed_ack);
393 	}
394 
395 	if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
396 		if (!rxrpc_rotate_tx_window(call, top, &summary)) {
397 			rxrpc_proto_abort(call, top, rxrpc_eproto_early_reply);
398 			return false;
399 		}
400 	}
401 
402 	rxrpc_end_tx_phase(call, true, rxrpc_eproto_unexpected_reply);
403 	return true;
404 }
405 
406 /*
407  * End the packet reception phase.
408  */
409 static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
410 {
411 	rxrpc_seq_t whigh = READ_ONCE(call->rx_highest_seq);
412 
413 	_enter("%d,%s", call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)]);
414 
415 	trace_rxrpc_receive(call, rxrpc_receive_end, 0, whigh);
416 
417 	switch (__rxrpc_call_state(call)) {
418 	case RXRPC_CALL_CLIENT_RECV_REPLY:
419 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_terminal_ack);
420 		rxrpc_call_completed(call);
421 		break;
422 
423 	case RXRPC_CALL_SERVER_RECV_REQUEST:
424 		rxrpc_set_call_state(call, RXRPC_CALL_SERVER_ACK_REQUEST);
425 		call->expect_req_by = KTIME_MAX;
426 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_processing_op);
427 		break;
428 
429 	default:
430 		break;
431 	}
432 }
433 
434 static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
435 					  rxrpc_seq_t window, rxrpc_seq_t wtop)
436 {
437 	call->ackr_window = window;
438 	call->ackr_wtop = wtop;
439 }
440 
441 /*
442  * Push a DATA packet onto the Rx queue.
443  */
444 static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
445 				   rxrpc_seq_t window, rxrpc_seq_t wtop,
446 				   enum rxrpc_receive_trace why)
447 {
448 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
449 	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
450 
451 	skb_queue_tail(&call->recvmsg_queue, skb);
452 	rxrpc_input_update_ack_window(call, window, wtop);
453 	trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
454 	if (last)
455 		rxrpc_end_rx_phase(call, sp->hdr.serial);
456 }
457 
458 /*
459  * Process a DATA packet.
460  */
461 static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb,
462 				 bool *_notify, rxrpc_serial_t *_ack_serial, int *_ack_reason)
463 {
464 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
465 	struct sk_buff *oos;
466 	rxrpc_serial_t serial = sp->hdr.serial;
467 	unsigned int sack = call->ackr_sack_base;
468 	rxrpc_seq_t window = call->ackr_window;
469 	rxrpc_seq_t wtop = call->ackr_wtop;
470 	rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
471 	rxrpc_seq_t seq = sp->hdr.seq;
472 	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
473 	int ack_reason = -1;
474 
475 	rxrpc_inc_stat(call->rxnet, stat_rx_data);
476 	if (sp->hdr.flags & RXRPC_REQUEST_ACK)
477 		rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
478 	if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
479 		rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
480 
481 	if (last) {
482 		if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
483 		    seq + 1 != wtop)
484 			return rxrpc_proto_abort(call, seq, rxrpc_eproto_different_last);
485 	} else {
486 		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
487 		    after_eq(seq, wtop)) {
488 			pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
489 				call->debug_id, seq, window, wtop, wlimit);
490 			return rxrpc_proto_abort(call, seq, rxrpc_eproto_data_after_last);
491 		}
492 	}
493 
494 	if (after(seq, call->rx_highest_seq))
495 		call->rx_highest_seq = seq;
496 
497 	trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
498 
499 	if (before(seq, window)) {
500 		ack_reason = RXRPC_ACK_DUPLICATE;
501 		goto send_ack;
502 	}
503 	if (after(seq, wlimit)) {
504 		ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
505 		goto send_ack;
506 	}
507 
508 	/* Queue the packet. */
509 	if (seq == window) {
510 		if (sp->hdr.flags & RXRPC_REQUEST_ACK)
511 			ack_reason = RXRPC_ACK_REQUESTED;
512 		/* Send an immediate ACK if we fill in a hole */
513 		else if (!skb_queue_empty(&call->rx_oos_queue))
514 			ack_reason = RXRPC_ACK_DELAY;
515 
516 		window++;
517 		if (after(window, wtop)) {
518 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_none);
519 			wtop = window;
520 		} else {
521 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_advance);
522 			sack = (sack + 1) % RXRPC_SACK_SIZE;
523 		}
524 
525 
526 		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg);
527 
528 		rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
529 		*_notify = true;
530 
531 		while ((oos = skb_peek(&call->rx_oos_queue))) {
532 			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
533 
534 			if (after(osp->hdr.seq, window))
535 				break;
536 
537 			__skb_unlink(oos, &call->rx_oos_queue);
538 			last = osp->hdr.flags & RXRPC_LAST_PACKET;
539 			seq = osp->hdr.seq;
540 			call->ackr_sack_table[sack] = 0;
541 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_fill);
542 			sack = (sack + 1) % RXRPC_SACK_SIZE;
543 
544 			window++;
545 			rxrpc_input_queue_data(call, oos, window, wtop,
546 					       rxrpc_receive_queue_oos);
547 		}
548 
549 		call->ackr_sack_base = sack;
550 	} else {
551 		unsigned int slot;
552 
553 		ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
554 
555 		slot = seq - window;
556 		sack = (sack + slot) % RXRPC_SACK_SIZE;
557 
558 		if (call->ackr_sack_table[sack % RXRPC_SACK_SIZE]) {
559 			ack_reason = RXRPC_ACK_DUPLICATE;
560 			goto send_ack;
561 		}
562 
563 		call->ackr_sack_table[sack % RXRPC_SACK_SIZE] |= 1;
564 		trace_rxrpc_sack(call, seq, sack, rxrpc_sack_oos);
565 
566 		if (after(seq + 1, wtop)) {
567 			wtop = seq + 1;
568 			rxrpc_input_update_ack_window(call, window, wtop);
569 		}
570 
571 		skb_queue_walk(&call->rx_oos_queue, oos) {
572 			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
573 
574 			if (after(osp->hdr.seq, seq)) {
575 				rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
576 				__skb_queue_before(&call->rx_oos_queue, oos, skb);
577 				goto oos_queued;
578 			}
579 		}
580 
581 		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
582 		__skb_queue_tail(&call->rx_oos_queue, skb);
583 	oos_queued:
584 		trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
585 				    sp->hdr.serial, sp->hdr.seq);
586 	}
587 
588 send_ack:
589 	if (ack_reason >= 0) {
590 		if (rxrpc_ack_priority[ack_reason] > rxrpc_ack_priority[*_ack_reason]) {
591 			*_ack_serial = serial;
592 			*_ack_reason = ack_reason;
593 		} else if (rxrpc_ack_priority[ack_reason] == rxrpc_ack_priority[*_ack_reason] &&
594 			   ack_reason == RXRPC_ACK_REQUESTED) {
595 			*_ack_serial = serial;
596 			*_ack_reason = ack_reason;
597 		}
598 	}
599 }
600 
601 /*
602  * Split a jumbo packet and file the bits separately.
603  */
604 static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
605 {
606 	struct rxrpc_jumbo_header jhdr;
607 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
608 	struct sk_buff *jskb;
609 	rxrpc_serial_t ack_serial = 0;
610 	unsigned int offset = sizeof(struct rxrpc_wire_header);
611 	unsigned int len = skb->len - offset;
612 	bool notify = false;
613 	int ack_reason = 0, count = 1, stat_ix;
614 
615 	while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
616 		if (len < RXRPC_JUMBO_SUBPKTLEN)
617 			goto protocol_error;
618 		if (sp->hdr.flags & RXRPC_LAST_PACKET)
619 			goto protocol_error;
620 		if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
621 				  &jhdr, sizeof(jhdr)) < 0)
622 			goto protocol_error;
623 
624 		jskb = skb_clone(skb, GFP_NOFS);
625 		if (!jskb) {
626 			kdebug("couldn't clone");
627 			return false;
628 		}
629 		rxrpc_new_skb(jskb, rxrpc_skb_new_jumbo_subpacket);
630 		jsp = rxrpc_skb(jskb);
631 		jsp->offset = offset;
632 		jsp->len = RXRPC_JUMBO_DATALEN;
633 		rxrpc_input_data_one(call, jskb, &notify, &ack_serial, &ack_reason);
634 		rxrpc_free_skb(jskb, rxrpc_skb_put_jumbo_subpacket);
635 
636 		sp->hdr.flags = jhdr.flags;
637 		sp->hdr._rsvd = ntohs(jhdr._rsvd);
638 		sp->hdr.seq++;
639 		sp->hdr.serial++;
640 		offset += RXRPC_JUMBO_SUBPKTLEN;
641 		len -= RXRPC_JUMBO_SUBPKTLEN;
642 		count++;
643 	}
644 
645 	sp->offset = offset;
646 	sp->len    = len;
647 	rxrpc_input_data_one(call, skb, &notify, &ack_serial, &ack_reason);
648 
649 	stat_ix = umin(count, ARRAY_SIZE(call->rxnet->stat_rx_jumbo)) - 1;
650 	atomic_inc(&call->rxnet->stat_rx_jumbo[stat_ix]);
651 
652 	if (ack_reason > 0) {
653 		rxrpc_send_ACK(call, ack_reason, ack_serial,
654 			       rxrpc_propose_ack_input_data);
655 	} else {
656 		call->ackr_nr_unacked++;
657 		rxrpc_propose_delay_ACK(call, sp->hdr.serial,
658 					rxrpc_propose_ack_input_data);
659 	}
660 	if (notify) {
661 		trace_rxrpc_notify_socket(call->debug_id, sp->hdr.serial);
662 		rxrpc_notify_socket(call);
663 	}
664 	return true;
665 
666 protocol_error:
667 	return false;
668 }
669 
670 /*
671  * Process a DATA packet, adding the packet to the Rx ring.  The caller's
672  * packet ref must be passed on or discarded.
673  */
674 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
675 {
676 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
677 	rxrpc_serial_t serial = sp->hdr.serial;
678 	rxrpc_seq_t seq0 = sp->hdr.seq;
679 
680 	_enter("{%x,%x,%x},{%u,%x}",
681 	       call->ackr_window, call->ackr_wtop, call->rx_highest_seq,
682 	       skb->len, seq0);
683 
684 	if (__rxrpc_call_is_complete(call))
685 		return;
686 
687 	switch (__rxrpc_call_state(call)) {
688 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
689 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
690 		/* Received data implicitly ACKs all of the request
691 		 * packets we sent when we're acting as a client.
692 		 */
693 		if (!rxrpc_receiving_reply(call))
694 			goto out_notify;
695 		break;
696 
697 	case RXRPC_CALL_SERVER_RECV_REQUEST: {
698 		unsigned long timo = READ_ONCE(call->next_req_timo);
699 
700 		if (timo) {
701 			ktime_t delay = ms_to_ktime(timo);
702 
703 			call->expect_req_by = ktime_add(ktime_get_real(), delay);
704 			trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_idle);
705 		}
706 		break;
707 	}
708 
709 	default:
710 		break;
711 	}
712 
713 	if (!rxrpc_input_split_jumbo(call, skb)) {
714 		rxrpc_proto_abort(call, sp->hdr.seq, rxrpc_badmsg_bad_jumbo);
715 		goto out_notify;
716 	}
717 	return;
718 
719 out_notify:
720 	trace_rxrpc_notify_socket(call->debug_id, serial);
721 	rxrpc_notify_socket(call);
722 	_leave(" [queued]");
723 }
724 
725 /*
726  * See if there's a cached RTT probe to complete.
727  */
728 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
729 				     ktime_t resp_time,
730 				     rxrpc_serial_t acked_serial,
731 				     rxrpc_serial_t ack_serial,
732 				     enum rxrpc_rtt_rx_trace type)
733 {
734 	rxrpc_serial_t orig_serial;
735 	unsigned long avail;
736 	ktime_t sent_at;
737 	bool matched = false;
738 	int i;
739 
740 	avail = READ_ONCE(call->rtt_avail);
741 	smp_rmb(); /* Read avail bits before accessing data. */
742 
743 	for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
744 		if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
745 			continue;
746 
747 		sent_at = call->rtt_sent_at[i];
748 		orig_serial = call->rtt_serial[i];
749 
750 		if (orig_serial == acked_serial) {
751 			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
752 			smp_mb(); /* Read data before setting avail bit */
753 			set_bit(i, &call->rtt_avail);
754 			rxrpc_call_add_rtt(call, type, i, acked_serial, ack_serial,
755 					   sent_at, resp_time);
756 			matched = true;
757 		}
758 
759 		/* If a later serial is being acked, then mark this slot as
760 		 * being available.
761 		 */
762 		if (after(acked_serial, orig_serial)) {
763 			trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
764 					   orig_serial, acked_serial, 0, 0, 0);
765 			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
766 			smp_wmb();
767 			set_bit(i, &call->rtt_avail);
768 		}
769 	}
770 
771 	if (!matched)
772 		trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0, 0);
773 }
774 
775 /*
776  * Process the extra information that may be appended to an ACK packet
777  */
778 static void rxrpc_input_ack_trailer(struct rxrpc_call *call, struct sk_buff *skb,
779 				    struct rxrpc_acktrailer *trailer)
780 {
781 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
782 	struct rxrpc_peer *peer = call->peer;
783 	unsigned int max_data, capacity;
784 	bool wake = false;
785 	u32 max_mtu	= ntohl(trailer->maxMTU);
786 	//u32 if_mtu	= ntohl(trailer->ifMTU);
787 	u32 rwind	= ntohl(trailer->rwind);
788 	u32 jumbo_max	= ntohl(trailer->jumbo_max);
789 
790 	if (rwind > RXRPC_TX_MAX_WINDOW)
791 		rwind = RXRPC_TX_MAX_WINDOW;
792 	if (call->tx_winsize != rwind) {
793 		if (rwind > call->tx_winsize)
794 			wake = true;
795 		trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
796 		call->tx_winsize = rwind;
797 	}
798 
799 	max_mtu = clamp(max_mtu, 500, 65535);
800 	peer->ackr_max_data = max_mtu;
801 
802 	if (max_mtu < peer->max_data) {
803 		trace_rxrpc_pmtud_reduce(peer, sp->hdr.serial, max_mtu,
804 					 rxrpc_pmtud_reduce_ack);
805 		write_seqcount_begin(&peer->mtu_lock);
806 		peer->max_data = max_mtu;
807 		write_seqcount_end(&peer->mtu_lock);
808 	}
809 
810 	max_data = umin(max_mtu, peer->max_data);
811 	capacity = max_data;
812 	capacity += sizeof(struct rxrpc_jumbo_header); /* First subpacket has main hdr, not jumbo */
813 	capacity /= sizeof(struct rxrpc_jumbo_header) + RXRPC_JUMBO_DATALEN;
814 
815 	if (jumbo_max == 0) {
816 		/* The peer says it supports pmtu discovery */
817 		peer->ackr_adv_pmtud = true;
818 	} else {
819 		peer->ackr_adv_pmtud = false;
820 		capacity = clamp(capacity, 1, jumbo_max);
821 	}
822 
823 	call->tx_jumbo_max = capacity;
824 
825 	if (wake)
826 		wake_up(&call->waitq);
827 }
828 
829 #if defined(CONFIG_X86) && __GNUC__ && !defined(__clang__)
830 /* Clang doesn't support the %z constraint modifier */
831 #define shiftr_adv_rotr(shift_from, rotate_into) ({			\
832 			asm(" shr%z1 %1\n"				\
833 			    " inc %0\n"					\
834 			    " rcr%z2 %2\n"				\
835 			    : "+d"(shift_from), "+m"(*(shift_from)), "+rm"(rotate_into) \
836 			    );						\
837 		})
838 #else
839 #define shiftr_adv_rotr(shift_from, rotate_into) ({	\
840 			typeof(rotate_into) __bit0 = *(shift_from) & 1;	\
841 			*(shift_from) >>= 1;				\
842 			shift_from++;					\
843 			rotate_into >>= 1;				\
844 			rotate_into |= __bit0 << (sizeof(rotate_into) * 8 - 1); \
845 		})
846 #endif
847 
848 /*
849  * Deal with RTT samples from soft ACKs.
850  */
851 static void rxrpc_input_soft_rtt(struct rxrpc_call *call,
852 				 struct rxrpc_ack_summary *summary,
853 				 struct rxrpc_txqueue *tq)
854 {
855 	for (int ix = 0; ix < RXRPC_NR_TXQUEUE; ix++)
856 		if (summary->acked_serial == tq->segment_serial[ix])
857 			return rxrpc_add_data_rtt_sample(call, summary, tq, ix);
858 }
859 
860 /*
861  * Process a batch of soft ACKs specific to a transmission queue segment.
862  */
863 static void rxrpc_input_soft_ack_tq(struct rxrpc_call *call,
864 				    struct rxrpc_ack_summary *summary,
865 				    struct rxrpc_txqueue *tq,
866 				    unsigned long extracted_acks,
867 				    int nr_reported,
868 				    rxrpc_seq_t seq,
869 				    rxrpc_seq_t *lowest_nak)
870 {
871 	unsigned long old_reported = 0, flipped, new_acks = 0;
872 	unsigned long a_to_n, n_to_a = 0;
873 	int new, a, n;
874 
875 	if (tq->nr_reported_acks > 0)
876 		old_reported = ~0UL >> (RXRPC_NR_TXQUEUE - tq->nr_reported_acks);
877 
878 	_enter("{%x,%lx,%d},%lx,%d,%x",
879 	       tq->qbase, tq->segment_acked, tq->nr_reported_acks,
880 	       extracted_acks, nr_reported, seq);
881 
882 	_debug("[%x]", tq->qbase);
883 	_debug("tq    %16lx %u", tq->segment_acked, tq->nr_reported_acks);
884 	_debug("sack  %16lx %u", extracted_acks, nr_reported);
885 
886 	/* See how many previously logged ACKs/NAKs have flipped. */
887 	flipped = (tq->segment_acked ^ extracted_acks) & old_reported;
888 	if (flipped) {
889 		n_to_a = ~tq->segment_acked & flipped; /* Old NAK -> ACK */
890 		a_to_n =  tq->segment_acked & flipped; /* Old ACK -> NAK */
891 		a = hweight_long(n_to_a);
892 		n = hweight_long(a_to_n);
893 		_debug("flip  %16lx", flipped);
894 		_debug("ntoa  %16lx %d", n_to_a, a);
895 		_debug("aton  %16lx %d", a_to_n, n);
896 		call->acks_nr_sacks	+= a - n;
897 		call->acks_nr_snacks	+= n - a;
898 		summary->nr_new_sacks	+= a;
899 		summary->nr_new_snacks	+= n;
900 	}
901 
902 	/* See how many new ACKs/NAKs have been acquired. */
903 	new = nr_reported - tq->nr_reported_acks;
904 	if (new > 0) {
905 		new_acks = extracted_acks & ~old_reported;
906 		if (new_acks) {
907 			a = hweight_long(new_acks);
908 			n = new - a;
909 			_debug("new_a %16lx new=%d a=%d n=%d", new_acks, new, a, n);
910 			call->acks_nr_sacks	+= a;
911 			call->acks_nr_snacks	+= n;
912 			summary->nr_new_sacks	+= a;
913 			summary->nr_new_snacks	+= n;
914 		} else {
915 			call->acks_nr_snacks	+= new;
916 			summary->nr_new_snacks	+= new;
917 		}
918 	}
919 
920 	tq->nr_reported_acks = nr_reported;
921 	tq->segment_acked = extracted_acks;
922 	trace_rxrpc_apply_acks(call, tq);
923 
924 	if (extracted_acks != ~0UL) {
925 		rxrpc_seq_t lowest = seq + ffz(extracted_acks);
926 
927 		if (before(lowest, *lowest_nak))
928 			*lowest_nak = lowest;
929 	}
930 
931 	if (summary->acked_serial)
932 		rxrpc_input_soft_rtt(call, summary, tq);
933 
934 	new_acks |= n_to_a;
935 	if (new_acks)
936 		rxrpc_input_rack(call, summary, tq, new_acks);
937 
938 	if (call->tlp_serial &&
939 	    rxrpc_seq_in_txq(tq, call->tlp_seq) &&
940 	    test_bit(call->tlp_seq - tq->qbase, &new_acks))
941 		summary->tlp_probe_acked = true;
942 }
943 
944 /*
945  * Process individual soft ACKs.
946  *
947  * Each ACK in the array corresponds to one packet and can be either an ACK or
948  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
949  * packets that lie beyond the end of the ACK list are scheduled for resend by
950  * the timer on the basis that the peer might just not have processed them at
951  * the time the ACK was sent.
952  */
953 static void rxrpc_input_soft_acks(struct rxrpc_call *call,
954 				  struct rxrpc_ack_summary *summary,
955 				  struct sk_buff *skb)
956 {
957 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
958 	struct rxrpc_txqueue *tq = call->tx_queue;
959 	unsigned long extracted = ~0UL;
960 	unsigned int nr = 0;
961 	rxrpc_seq_t seq = call->acks_hard_ack + 1;
962 	rxrpc_seq_t lowest_nak = seq + sp->ack.nr_acks;
963 	u8 *acks = skb->data + sizeof(struct rxrpc_wire_header) + sizeof(struct rxrpc_ackpacket);
964 
965 	_enter("%x,%x,%u", tq->qbase, seq, sp->ack.nr_acks);
966 
967 	while (after(seq, tq->qbase + RXRPC_NR_TXQUEUE - 1))
968 		tq = tq->next;
969 
970 	for (unsigned int i = 0; i < sp->ack.nr_acks; i++) {
971 		/* Decant ACKs until we hit a txqueue boundary. */
972 		shiftr_adv_rotr(acks, extracted);
973 		if (i == 256) {
974 			acks -= i;
975 			i = 0;
976 		}
977 		seq++;
978 		nr++;
979 		if ((seq & RXRPC_TXQ_MASK) != 0)
980 			continue;
981 
982 		_debug("bound %16lx %u", extracted, nr);
983 
984 		rxrpc_input_soft_ack_tq(call, summary, tq, extracted, RXRPC_NR_TXQUEUE,
985 					seq - RXRPC_NR_TXQUEUE, &lowest_nak);
986 		extracted = ~0UL;
987 		nr = 0;
988 		tq = tq->next;
989 		prefetch(tq);
990 	}
991 
992 	if (nr) {
993 		unsigned int nr_reported = seq & RXRPC_TXQ_MASK;
994 
995 		extracted >>= RXRPC_NR_TXQUEUE - nr_reported;
996 		_debug("tail  %16lx %u", extracted, nr_reported);
997 		rxrpc_input_soft_ack_tq(call, summary, tq, extracted, nr_reported,
998 					seq & ~RXRPC_TXQ_MASK, &lowest_nak);
999 	}
1000 
1001 	/* We *can* have more nacks than we did - the peer is permitted to drop
1002 	 * packets it has soft-acked and re-request them.  Further, it is
1003 	 * possible for the nack distribution to change whilst the number of
1004 	 * nacks stays the same or goes down.
1005 	 */
1006 	if (lowest_nak != call->acks_lowest_nak) {
1007 		call->acks_lowest_nak = lowest_nak;
1008 		summary->new_low_snack = true;
1009 	}
1010 
1011 	_debug("summary A=%d+%d N=%d+%d",
1012 	       call->acks_nr_sacks,  summary->nr_new_sacks,
1013 	       call->acks_nr_snacks, summary->nr_new_snacks);
1014 }
1015 
1016 /*
1017  * Return true if the ACK is valid - ie. it doesn't appear to have regressed
1018  * with respect to the ack state conveyed by preceding ACKs.
1019  */
1020 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
1021 			       rxrpc_seq_t hard_ack, rxrpc_seq_t prev_pkt)
1022 {
1023 	rxrpc_seq_t base = READ_ONCE(call->acks_hard_ack);
1024 
1025 	if (after(hard_ack, base))
1026 		return true; /* The window advanced */
1027 
1028 	if (before(hard_ack, base))
1029 		return false; /* firstPacket regressed */
1030 
1031 	if (after_eq(prev_pkt, call->acks_prev_seq))
1032 		return true; /* previousPacket hasn't regressed. */
1033 
1034 	/* Some rx implementations put a serial number in previousPacket. */
1035 	if (after(prev_pkt, base + call->tx_winsize))
1036 		return false;
1037 	return true;
1038 }
1039 
1040 /*
1041  * Process an ACK packet.
1042  *
1043  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
1044  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
1045  *
1046  * A hard-ACK means that a packet has been processed and may be discarded; a
1047  * soft-ACK means that the packet may be discarded and retransmission
1048  * requested.  A phase is complete when all packets are hard-ACK'd.
1049  */
1050 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
1051 {
1052 	struct rxrpc_ack_summary summary = { 0 };
1053 	struct rxrpc_acktrailer trailer;
1054 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1055 	rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
1056 	int nr_acks, offset, ioffset;
1057 
1058 	_enter("");
1059 
1060 	offset = sizeof(struct rxrpc_wire_header) + sizeof(struct rxrpc_ackpacket);
1061 
1062 	summary.ack_serial	= sp->hdr.serial;
1063 	first_soft_ack		= sp->ack.first_ack;
1064 	prev_pkt		= sp->ack.prev_ack;
1065 	nr_acks			= sp->ack.nr_acks;
1066 	hard_ack		= first_soft_ack - 1;
1067 	summary.acked_serial	= sp->ack.acked_serial;
1068 	summary.ack_reason	= (sp->ack.reason < RXRPC_ACK__INVALID ?
1069 				   sp->ack.reason : RXRPC_ACK__INVALID);
1070 
1071 	trace_rxrpc_rx_ack(call, sp);
1072 	rxrpc_inc_stat(call->rxnet, stat_rx_acks[summary.ack_reason]);
1073 	prefetch(call->tx_queue);
1074 
1075 	/* If we get an EXCEEDS_WINDOW ACK from the server, it probably
1076 	 * indicates that the client address changed due to NAT.  The server
1077 	 * lost the call because it switched to a different peer.
1078 	 */
1079 	if (unlikely(summary.ack_reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
1080 	    hard_ack == 0 &&
1081 	    prev_pkt == 0 &&
1082 	    rxrpc_is_client_call(call)) {
1083 		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1084 					  0, -ENETRESET);
1085 		goto send_response;
1086 	}
1087 
1088 	/* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
1089 	 * indicate a change of address.  However, we can retransmit the call
1090 	 * if we still have it buffered to the beginning.
1091 	 */
1092 	if (unlikely(summary.ack_reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
1093 	    hard_ack == 0 &&
1094 	    prev_pkt == 0 &&
1095 	    call->tx_bottom == 0 &&
1096 	    rxrpc_is_client_call(call)) {
1097 		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1098 					  0, -ENETRESET);
1099 		goto send_response;
1100 	}
1101 
1102 	/* Discard any out-of-order or duplicate ACKs (outside lock). */
1103 	if (!rxrpc_is_ack_valid(call, hard_ack, prev_pkt)) {
1104 		trace_rxrpc_rx_discard_ack(call, summary.ack_serial, hard_ack, prev_pkt);
1105 		goto send_response; /* Still respond if requested. */
1106 	}
1107 
1108 	trailer.maxMTU = 0;
1109 	ioffset = offset + nr_acks + 3;
1110 	if (skb->len >= ioffset + sizeof(trailer) &&
1111 	    skb_copy_bits(skb, ioffset, &trailer, sizeof(trailer)) < 0)
1112 		return rxrpc_proto_abort(call, 0, rxrpc_badmsg_short_ack_trailer);
1113 
1114 	if (nr_acks > 0)
1115 		skb_condense(skb);
1116 
1117 	call->acks_latest_ts = ktime_get_real();
1118 	call->acks_hard_ack = hard_ack;
1119 	call->acks_prev_seq = prev_pkt;
1120 
1121 	if (summary.acked_serial) {
1122 		switch (summary.ack_reason) {
1123 		case RXRPC_ACK_PING_RESPONSE:
1124 			rxrpc_complete_rtt_probe(call, call->acks_latest_ts,
1125 						 summary.acked_serial, summary.ack_serial,
1126 						 rxrpc_rtt_rx_ping_response);
1127 			break;
1128 		default:
1129 			if (after(summary.acked_serial, call->acks_highest_serial))
1130 				call->acks_highest_serial = summary.acked_serial;
1131 			summary.rtt_sample_avail = true;
1132 			break;
1133 		}
1134 	}
1135 
1136 	/* Parse rwind and mtu sizes if provided. */
1137 	if (trailer.maxMTU)
1138 		rxrpc_input_ack_trailer(call, skb, &trailer);
1139 
1140 	if (hard_ack + 1 == 0)
1141 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_zero);
1142 
1143 	/* Ignore ACKs unless we are or have just been transmitting. */
1144 	switch (__rxrpc_call_state(call)) {
1145 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
1146 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1147 	case RXRPC_CALL_SERVER_SEND_REPLY:
1148 	case RXRPC_CALL_SERVER_AWAIT_ACK:
1149 		break;
1150 	default:
1151 		goto send_response;
1152 	}
1153 
1154 	if (before(hard_ack, call->tx_bottom) ||
1155 	    after(hard_ack, call->tx_top))
1156 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_outside_window);
1157 	if (nr_acks > call->tx_top - hard_ack)
1158 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_sack_overflow);
1159 
1160 	if (after(hard_ack, call->tx_bottom)) {
1161 		if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
1162 			rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ack);
1163 			goto send_response;
1164 		}
1165 	}
1166 
1167 	if (nr_acks > 0) {
1168 		if (offset > (int)skb->len - nr_acks)
1169 			return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_short_sack);
1170 		rxrpc_input_soft_acks(call, &summary, skb);
1171 	}
1172 
1173 	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
1174 	    call->acks_nr_sacks == call->tx_top - hard_ack &&
1175 	    rxrpc_is_client_call(call))
1176 		rxrpc_propose_ping(call, summary.ack_serial,
1177 				   rxrpc_propose_ack_ping_for_lost_reply);
1178 
1179 	/* Drive the congestion management algorithm first and then RACK-TLP as
1180 	 * the latter depends on the state/change in state in the former.
1181 	 */
1182 	rxrpc_congestion_management(call, &summary);
1183 	rxrpc_rack_detect_loss_and_arm_timer(call, &summary);
1184 	rxrpc_tlp_process_ack(call, &summary);
1185 	if (call->tlp_serial && after_eq(summary.acked_serial, call->tlp_serial))
1186 		call->tlp_serial = 0;
1187 
1188 send_response:
1189 	if (summary.ack_reason == RXRPC_ACK_PING)
1190 		rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, summary.ack_serial,
1191 			       rxrpc_propose_ack_respond_to_ping);
1192 	else if (sp->hdr.flags & RXRPC_REQUEST_ACK)
1193 		rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, summary.ack_serial,
1194 			       rxrpc_propose_ack_respond_to_ack);
1195 }
1196 
1197 /*
1198  * Process an ACKALL packet.
1199  */
1200 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1201 {
1202 	struct rxrpc_ack_summary summary = { 0 };
1203 
1204 	if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1205 		rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ackall);
1206 }
1207 
1208 /*
1209  * Process an ABORT packet directed at a call.
1210  */
1211 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1212 {
1213 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1214 
1215 	trace_rxrpc_rx_abort(call, sp->hdr.serial, skb->priority);
1216 
1217 	rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1218 				  skb->priority, -ECONNABORTED);
1219 }
1220 
1221 /*
1222  * Process an incoming call packet.
1223  */
1224 void rxrpc_input_call_packet(struct rxrpc_call *call, struct sk_buff *skb)
1225 {
1226 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1227 	unsigned long timo;
1228 
1229 	_enter("%p,%p", call, skb);
1230 
1231 	if (sp->hdr.serviceId != call->dest_srx.srx_service)
1232 		call->dest_srx.srx_service = sp->hdr.serviceId;
1233 	if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
1234 		call->rx_serial = sp->hdr.serial;
1235 	if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
1236 		set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
1237 
1238 	timo = READ_ONCE(call->next_rx_timo);
1239 	if (timo) {
1240 		ktime_t delay = ms_to_ktime(timo);
1241 
1242 		call->expect_rx_by = ktime_add(ktime_get_real(), delay);
1243 		trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_expect_rx);
1244 	}
1245 
1246 	switch (sp->hdr.type) {
1247 	case RXRPC_PACKET_TYPE_DATA:
1248 		return rxrpc_input_data(call, skb);
1249 
1250 	case RXRPC_PACKET_TYPE_ACK:
1251 		return rxrpc_input_ack(call, skb);
1252 
1253 	case RXRPC_PACKET_TYPE_BUSY:
1254 		/* Just ignore BUSY packets from the server; the retry and
1255 		 * lifespan timers will take care of business.  BUSY packets
1256 		 * from the client don't make sense.
1257 		 */
1258 		return;
1259 
1260 	case RXRPC_PACKET_TYPE_ABORT:
1261 		return rxrpc_input_abort(call, skb);
1262 
1263 	case RXRPC_PACKET_TYPE_ACKALL:
1264 		return rxrpc_input_ackall(call, skb);
1265 
1266 	default:
1267 		break;
1268 	}
1269 }
1270 
1271 /*
1272  * Handle a new service call on a channel implicitly completing the preceding
1273  * call on that channel.  This does not apply to client conns.
1274  *
1275  * TODO: If callNumber > call_id + 1, renegotiate security.
1276  */
1277 void rxrpc_implicit_end_call(struct rxrpc_call *call, struct sk_buff *skb)
1278 {
1279 	switch (__rxrpc_call_state(call)) {
1280 	case RXRPC_CALL_SERVER_AWAIT_ACK:
1281 		rxrpc_call_completed(call);
1282 		fallthrough;
1283 	case RXRPC_CALL_COMPLETE:
1284 		break;
1285 	default:
1286 		rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ESHUTDOWN,
1287 				 rxrpc_eproto_improper_term);
1288 		trace_rxrpc_improper_term(call);
1289 		break;
1290 	}
1291 
1292 	rxrpc_input_call_event(call);
1293 }
1294