xref: /openbmc/linux/net/rxrpc/input.c (revision ad066c9b)
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 [RFC 5681].
31  */
32 static void rxrpc_congestion_management(struct rxrpc_call *call,
33 					struct sk_buff *skb,
34 					struct rxrpc_ack_summary *summary,
35 					rxrpc_serial_t acked_serial)
36 {
37 	enum rxrpc_congest_change change = rxrpc_cong_no_change;
38 	unsigned int cumulative_acks = call->cong_cumul_acks;
39 	unsigned int cwnd = call->cong_cwnd;
40 	bool resend = false;
41 
42 	summary->flight_size =
43 		(call->tx_top - call->acks_hard_ack) - summary->nr_acks;
44 
45 	if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
46 		summary->retrans_timeo = true;
47 		call->cong_ssthresh = max_t(unsigned int,
48 					    summary->flight_size / 2, 2);
49 		cwnd = 1;
50 		if (cwnd >= call->cong_ssthresh &&
51 		    call->cong_mode == RXRPC_CALL_SLOW_START) {
52 			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
53 			call->cong_tstamp = skb->tstamp;
54 			cumulative_acks = 0;
55 		}
56 	}
57 
58 	cumulative_acks += summary->nr_new_acks;
59 	if (cumulative_acks > 255)
60 		cumulative_acks = 255;
61 
62 	summary->cwnd = call->cong_cwnd;
63 	summary->ssthresh = call->cong_ssthresh;
64 	summary->cumulative_acks = cumulative_acks;
65 	summary->dup_acks = call->cong_dup_acks;
66 
67 	switch (call->cong_mode) {
68 	case RXRPC_CALL_SLOW_START:
69 		if (summary->saw_nacks)
70 			goto packet_loss_detected;
71 		if (summary->cumulative_acks > 0)
72 			cwnd += 1;
73 		if (cwnd >= call->cong_ssthresh) {
74 			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
75 			call->cong_tstamp = skb->tstamp;
76 		}
77 		goto out;
78 
79 	case RXRPC_CALL_CONGEST_AVOIDANCE:
80 		if (summary->saw_nacks)
81 			goto packet_loss_detected;
82 
83 		/* We analyse the number of packets that get ACK'd per RTT
84 		 * period and increase the window if we managed to fill it.
85 		 */
86 		if (call->peer->rtt_count == 0)
87 			goto out;
88 		if (ktime_before(skb->tstamp,
89 				 ktime_add_us(call->cong_tstamp,
90 					      call->peer->srtt_us >> 3)))
91 			goto out_no_clear_ca;
92 		change = rxrpc_cong_rtt_window_end;
93 		call->cong_tstamp = skb->tstamp;
94 		if (cumulative_acks >= cwnd)
95 			cwnd++;
96 		goto out;
97 
98 	case RXRPC_CALL_PACKET_LOSS:
99 		if (!summary->saw_nacks)
100 			goto resume_normality;
101 
102 		if (summary->new_low_nack) {
103 			change = rxrpc_cong_new_low_nack;
104 			call->cong_dup_acks = 1;
105 			if (call->cong_extra > 1)
106 				call->cong_extra = 1;
107 			goto send_extra_data;
108 		}
109 
110 		call->cong_dup_acks++;
111 		if (call->cong_dup_acks < 3)
112 			goto send_extra_data;
113 
114 		change = rxrpc_cong_begin_retransmission;
115 		call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
116 		call->cong_ssthresh = max_t(unsigned int,
117 					    summary->flight_size / 2, 2);
118 		cwnd = call->cong_ssthresh + 3;
119 		call->cong_extra = 0;
120 		call->cong_dup_acks = 0;
121 		resend = true;
122 		goto out;
123 
124 	case RXRPC_CALL_FAST_RETRANSMIT:
125 		if (!summary->new_low_nack) {
126 			if (summary->nr_new_acks == 0)
127 				cwnd += 1;
128 			call->cong_dup_acks++;
129 			if (call->cong_dup_acks == 2) {
130 				change = rxrpc_cong_retransmit_again;
131 				call->cong_dup_acks = 0;
132 				resend = true;
133 			}
134 		} else {
135 			change = rxrpc_cong_progress;
136 			cwnd = call->cong_ssthresh;
137 			if (!summary->saw_nacks)
138 				goto resume_normality;
139 		}
140 		goto out;
141 
142 	default:
143 		BUG();
144 		goto out;
145 	}
146 
147 resume_normality:
148 	change = rxrpc_cong_cleared_nacks;
149 	call->cong_dup_acks = 0;
150 	call->cong_extra = 0;
151 	call->cong_tstamp = skb->tstamp;
152 	if (cwnd < call->cong_ssthresh)
153 		call->cong_mode = RXRPC_CALL_SLOW_START;
154 	else
155 		call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
156 out:
157 	cumulative_acks = 0;
158 out_no_clear_ca:
159 	if (cwnd >= RXRPC_TX_MAX_WINDOW)
160 		cwnd = RXRPC_TX_MAX_WINDOW;
161 	call->cong_cwnd = cwnd;
162 	call->cong_cumul_acks = cumulative_acks;
163 	summary->mode = call->cong_mode;
164 	trace_rxrpc_congest(call, summary, acked_serial, change);
165 	if (resend)
166 		rxrpc_resend(call, skb);
167 	return;
168 
169 packet_loss_detected:
170 	change = rxrpc_cong_saw_nack;
171 	call->cong_mode = RXRPC_CALL_PACKET_LOSS;
172 	call->cong_dup_acks = 0;
173 	goto send_extra_data;
174 
175 send_extra_data:
176 	/* Send some previously unsent DATA if we have some to advance the ACK
177 	 * state.
178 	 */
179 	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
180 	    summary->nr_acks != call->tx_top - call->acks_hard_ack) {
181 		call->cong_extra++;
182 		wake_up(&call->waitq);
183 	}
184 	goto out_no_clear_ca;
185 }
186 
187 /*
188  * Degrade the congestion window if we haven't transmitted a packet for >1RTT.
189  */
190 void rxrpc_congestion_degrade(struct rxrpc_call *call)
191 {
192 	ktime_t rtt, now;
193 
194 	if (call->cong_mode != RXRPC_CALL_SLOW_START &&
195 	    call->cong_mode != RXRPC_CALL_CONGEST_AVOIDANCE)
196 		return;
197 	if (__rxrpc_call_state(call) == RXRPC_CALL_CLIENT_AWAIT_REPLY)
198 		return;
199 
200 	rtt = ns_to_ktime(call->peer->srtt_us * (1000 / 8));
201 	now = ktime_get_real();
202 	if (!ktime_before(ktime_add(call->tx_last_sent, rtt), now))
203 		return;
204 
205 	trace_rxrpc_reset_cwnd(call, now);
206 	rxrpc_inc_stat(call->rxnet, stat_tx_data_cwnd_reset);
207 	call->tx_last_sent = now;
208 	call->cong_mode = RXRPC_CALL_SLOW_START;
209 	call->cong_ssthresh = max_t(unsigned int, call->cong_ssthresh,
210 				    call->cong_cwnd * 3 / 4);
211 	call->cong_cwnd = max_t(unsigned int, call->cong_cwnd / 2, RXRPC_MIN_CWND);
212 }
213 
214 /*
215  * Apply a hard ACK by advancing the Tx window.
216  */
217 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
218 				   struct rxrpc_ack_summary *summary)
219 {
220 	struct rxrpc_txbuf *txb;
221 	bool rot_last = false;
222 
223 	list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
224 		if (before_eq(txb->seq, call->acks_hard_ack))
225 			continue;
226 		if (test_bit(RXRPC_TXBUF_LAST, &txb->flags)) {
227 			set_bit(RXRPC_CALL_TX_LAST, &call->flags);
228 			rot_last = true;
229 		}
230 		if (txb->seq == to)
231 			break;
232 	}
233 
234 	if (rot_last)
235 		set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
236 
237 	_enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
238 
239 	if (call->acks_lowest_nak == call->acks_hard_ack) {
240 		call->acks_lowest_nak = to;
241 	} else if (after(to, call->acks_lowest_nak)) {
242 		summary->new_low_nack = true;
243 		call->acks_lowest_nak = to;
244 	}
245 
246 	smp_store_release(&call->acks_hard_ack, to);
247 
248 	trace_rxrpc_txqueue(call, (rot_last ?
249 				   rxrpc_txqueue_rotate_last :
250 				   rxrpc_txqueue_rotate));
251 	wake_up(&call->waitq);
252 	return rot_last;
253 }
254 
255 /*
256  * End the transmission phase of a call.
257  *
258  * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
259  * or a final ACK packet.
260  */
261 static void rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
262 			       enum rxrpc_abort_reason abort_why)
263 {
264 	ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
265 
266 	if (unlikely(call->cong_last_nack)) {
267 		rxrpc_free_skb(call->cong_last_nack, rxrpc_skb_put_last_nack);
268 		call->cong_last_nack = NULL;
269 	}
270 
271 	switch (__rxrpc_call_state(call)) {
272 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
273 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
274 		if (reply_begun) {
275 			rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_RECV_REPLY);
276 			trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
277 			break;
278 		}
279 
280 		rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
281 		trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
282 		break;
283 
284 	case RXRPC_CALL_SERVER_AWAIT_ACK:
285 		rxrpc_call_completed(call);
286 		trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
287 		break;
288 
289 	default:
290 		kdebug("end_tx %s", rxrpc_call_states[__rxrpc_call_state(call)]);
291 		rxrpc_proto_abort(call, call->tx_top, abort_why);
292 		break;
293 	}
294 }
295 
296 /*
297  * Begin the reply reception phase of a call.
298  */
299 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
300 {
301 	struct rxrpc_ack_summary summary = { 0 };
302 	unsigned long now, timo;
303 	rxrpc_seq_t top = READ_ONCE(call->tx_top);
304 
305 	if (call->ackr_reason) {
306 		now = jiffies;
307 		timo = now + MAX_JIFFY_OFFSET;
308 
309 		WRITE_ONCE(call->delay_ack_at, timo);
310 		trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
311 	}
312 
313 	if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
314 		if (!rxrpc_rotate_tx_window(call, top, &summary)) {
315 			rxrpc_proto_abort(call, top, rxrpc_eproto_early_reply);
316 			return false;
317 		}
318 	}
319 
320 	rxrpc_end_tx_phase(call, true, rxrpc_eproto_unexpected_reply);
321 	return true;
322 }
323 
324 /*
325  * End the packet reception phase.
326  */
327 static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
328 {
329 	rxrpc_seq_t whigh = READ_ONCE(call->rx_highest_seq);
330 
331 	_enter("%d,%s", call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)]);
332 
333 	trace_rxrpc_receive(call, rxrpc_receive_end, 0, whigh);
334 
335 	switch (__rxrpc_call_state(call)) {
336 	case RXRPC_CALL_CLIENT_RECV_REPLY:
337 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_terminal_ack);
338 		rxrpc_call_completed(call);
339 		break;
340 
341 	case RXRPC_CALL_SERVER_RECV_REQUEST:
342 		rxrpc_set_call_state(call, RXRPC_CALL_SERVER_ACK_REQUEST);
343 		call->expect_req_by = jiffies + MAX_JIFFY_OFFSET;
344 		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_processing_op);
345 		break;
346 
347 	default:
348 		break;
349 	}
350 }
351 
352 static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
353 					  rxrpc_seq_t window, rxrpc_seq_t wtop)
354 {
355 	call->ackr_window = window;
356 	call->ackr_wtop = wtop;
357 }
358 
359 /*
360  * Push a DATA packet onto the Rx queue.
361  */
362 static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
363 				   rxrpc_seq_t window, rxrpc_seq_t wtop,
364 				   enum rxrpc_receive_trace why)
365 {
366 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
367 	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
368 
369 	__skb_queue_tail(&call->recvmsg_queue, skb);
370 	rxrpc_input_update_ack_window(call, window, wtop);
371 	trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
372 	if (last)
373 		rxrpc_end_rx_phase(call, sp->hdr.serial);
374 }
375 
376 /*
377  * Process a DATA packet.
378  */
379 static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb,
380 				 bool *_notify, rxrpc_serial_t *_ack_serial, int *_ack_reason)
381 {
382 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
383 	struct sk_buff *oos;
384 	rxrpc_serial_t serial = sp->hdr.serial;
385 	unsigned int sack = call->ackr_sack_base;
386 	rxrpc_seq_t window = call->ackr_window;
387 	rxrpc_seq_t wtop = call->ackr_wtop;
388 	rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
389 	rxrpc_seq_t seq = sp->hdr.seq;
390 	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
391 	int ack_reason = -1;
392 
393 	rxrpc_inc_stat(call->rxnet, stat_rx_data);
394 	if (sp->hdr.flags & RXRPC_REQUEST_ACK)
395 		rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
396 	if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
397 		rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
398 
399 	if (last) {
400 		if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
401 		    seq + 1 != wtop)
402 			return rxrpc_proto_abort(call, seq, rxrpc_eproto_different_last);
403 	} else {
404 		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
405 		    after_eq(seq, wtop)) {
406 			pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
407 				call->debug_id, seq, window, wtop, wlimit);
408 			return rxrpc_proto_abort(call, seq, rxrpc_eproto_data_after_last);
409 		}
410 	}
411 
412 	if (after(seq, call->rx_highest_seq))
413 		call->rx_highest_seq = seq;
414 
415 	trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
416 
417 	if (before(seq, window)) {
418 		ack_reason = RXRPC_ACK_DUPLICATE;
419 		goto send_ack;
420 	}
421 	if (after(seq, wlimit)) {
422 		ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
423 		goto send_ack;
424 	}
425 
426 	/* Queue the packet. */
427 	if (seq == window) {
428 		if (sp->hdr.flags & RXRPC_REQUEST_ACK)
429 			ack_reason = RXRPC_ACK_REQUESTED;
430 		/* Send an immediate ACK if we fill in a hole */
431 		else if (!skb_queue_empty(&call->rx_oos_queue))
432 			ack_reason = RXRPC_ACK_DELAY;
433 
434 		window++;
435 		if (after(window, wtop)) {
436 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_none);
437 			wtop = window;
438 		} else {
439 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_advance);
440 			sack = (sack + 1) % RXRPC_SACK_SIZE;
441 		}
442 
443 
444 		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg);
445 
446 		spin_lock(&call->recvmsg_queue.lock);
447 		rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
448 		*_notify = true;
449 
450 		while ((oos = skb_peek(&call->rx_oos_queue))) {
451 			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
452 
453 			if (after(osp->hdr.seq, window))
454 				break;
455 
456 			__skb_unlink(oos, &call->rx_oos_queue);
457 			last = osp->hdr.flags & RXRPC_LAST_PACKET;
458 			seq = osp->hdr.seq;
459 			call->ackr_sack_table[sack] = 0;
460 			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_fill);
461 			sack = (sack + 1) % RXRPC_SACK_SIZE;
462 
463 			window++;
464 			rxrpc_input_queue_data(call, oos, window, wtop,
465 					       rxrpc_receive_queue_oos);
466 		}
467 
468 		spin_unlock(&call->recvmsg_queue.lock);
469 
470 		call->ackr_sack_base = sack;
471 	} else {
472 		unsigned int slot;
473 
474 		ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
475 
476 		slot = seq - window;
477 		sack = (sack + slot) % RXRPC_SACK_SIZE;
478 
479 		if (call->ackr_sack_table[sack % RXRPC_SACK_SIZE]) {
480 			ack_reason = RXRPC_ACK_DUPLICATE;
481 			goto send_ack;
482 		}
483 
484 		call->ackr_sack_table[sack % RXRPC_SACK_SIZE] |= 1;
485 		trace_rxrpc_sack(call, seq, sack, rxrpc_sack_oos);
486 
487 		if (after(seq + 1, wtop)) {
488 			wtop = seq + 1;
489 			rxrpc_input_update_ack_window(call, window, wtop);
490 		}
491 
492 		skb_queue_walk(&call->rx_oos_queue, oos) {
493 			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
494 
495 			if (after(osp->hdr.seq, seq)) {
496 				rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
497 				__skb_queue_before(&call->rx_oos_queue, oos, skb);
498 				goto oos_queued;
499 			}
500 		}
501 
502 		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
503 		__skb_queue_tail(&call->rx_oos_queue, skb);
504 	oos_queued:
505 		trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
506 				    sp->hdr.serial, sp->hdr.seq);
507 	}
508 
509 send_ack:
510 	if (ack_reason >= 0) {
511 		if (rxrpc_ack_priority[ack_reason] > rxrpc_ack_priority[*_ack_reason]) {
512 			*_ack_serial = serial;
513 			*_ack_reason = ack_reason;
514 		} else if (rxrpc_ack_priority[ack_reason] == rxrpc_ack_priority[*_ack_reason] &&
515 			   ack_reason == RXRPC_ACK_REQUESTED) {
516 			*_ack_serial = serial;
517 			*_ack_reason = ack_reason;
518 		}
519 	}
520 }
521 
522 /*
523  * Split a jumbo packet and file the bits separately.
524  */
525 static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
526 {
527 	struct rxrpc_jumbo_header jhdr;
528 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
529 	struct sk_buff *jskb;
530 	rxrpc_serial_t ack_serial = 0;
531 	unsigned int offset = sizeof(struct rxrpc_wire_header);
532 	unsigned int len = skb->len - offset;
533 	bool notify = false;
534 	int ack_reason = 0;
535 
536 	while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
537 		if (len < RXRPC_JUMBO_SUBPKTLEN)
538 			goto protocol_error;
539 		if (sp->hdr.flags & RXRPC_LAST_PACKET)
540 			goto protocol_error;
541 		if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
542 				  &jhdr, sizeof(jhdr)) < 0)
543 			goto protocol_error;
544 
545 		jskb = skb_clone(skb, GFP_NOFS);
546 		if (!jskb) {
547 			kdebug("couldn't clone");
548 			return false;
549 		}
550 		rxrpc_new_skb(jskb, rxrpc_skb_new_jumbo_subpacket);
551 		jsp = rxrpc_skb(jskb);
552 		jsp->offset = offset;
553 		jsp->len = RXRPC_JUMBO_DATALEN;
554 		rxrpc_input_data_one(call, jskb, &notify, &ack_serial, &ack_reason);
555 		rxrpc_free_skb(jskb, rxrpc_skb_put_jumbo_subpacket);
556 
557 		sp->hdr.flags = jhdr.flags;
558 		sp->hdr._rsvd = ntohs(jhdr._rsvd);
559 		sp->hdr.seq++;
560 		sp->hdr.serial++;
561 		offset += RXRPC_JUMBO_SUBPKTLEN;
562 		len -= RXRPC_JUMBO_SUBPKTLEN;
563 	}
564 
565 	sp->offset = offset;
566 	sp->len    = len;
567 	rxrpc_input_data_one(call, skb, &notify, &ack_serial, &ack_reason);
568 
569 	if (ack_reason > 0) {
570 		rxrpc_send_ACK(call, ack_reason, ack_serial,
571 			       rxrpc_propose_ack_input_data);
572 	} else {
573 		call->ackr_nr_unacked++;
574 		rxrpc_propose_delay_ACK(call, sp->hdr.serial,
575 					rxrpc_propose_ack_input_data);
576 	}
577 	if (notify) {
578 		trace_rxrpc_notify_socket(call->debug_id, sp->hdr.serial);
579 		rxrpc_notify_socket(call);
580 	}
581 	return true;
582 
583 protocol_error:
584 	return false;
585 }
586 
587 /*
588  * Process a DATA packet, adding the packet to the Rx ring.  The caller's
589  * packet ref must be passed on or discarded.
590  */
591 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
592 {
593 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
594 	rxrpc_serial_t serial = sp->hdr.serial;
595 	rxrpc_seq_t seq0 = sp->hdr.seq;
596 
597 	_enter("{%x,%x,%x},{%u,%x}",
598 	       call->ackr_window, call->ackr_wtop, call->rx_highest_seq,
599 	       skb->len, seq0);
600 
601 	if (__rxrpc_call_is_complete(call))
602 		return;
603 
604 	switch (__rxrpc_call_state(call)) {
605 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
606 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
607 		/* Received data implicitly ACKs all of the request
608 		 * packets we sent when we're acting as a client.
609 		 */
610 		if (!rxrpc_receiving_reply(call))
611 			goto out_notify;
612 		break;
613 
614 	case RXRPC_CALL_SERVER_RECV_REQUEST: {
615 		unsigned long timo = READ_ONCE(call->next_req_timo);
616 		unsigned long now, expect_req_by;
617 
618 		if (timo) {
619 			now = jiffies;
620 			expect_req_by = now + timo;
621 			WRITE_ONCE(call->expect_req_by, expect_req_by);
622 			rxrpc_reduce_call_timer(call, expect_req_by, now,
623 						rxrpc_timer_set_for_idle);
624 		}
625 		break;
626 	}
627 
628 	default:
629 		break;
630 	}
631 
632 	if (!rxrpc_input_split_jumbo(call, skb)) {
633 		rxrpc_proto_abort(call, sp->hdr.seq, rxrpc_badmsg_bad_jumbo);
634 		goto out_notify;
635 	}
636 	return;
637 
638 out_notify:
639 	trace_rxrpc_notify_socket(call->debug_id, serial);
640 	rxrpc_notify_socket(call);
641 	_leave(" [queued]");
642 }
643 
644 /*
645  * See if there's a cached RTT probe to complete.
646  */
647 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
648 				     ktime_t resp_time,
649 				     rxrpc_serial_t acked_serial,
650 				     rxrpc_serial_t ack_serial,
651 				     enum rxrpc_rtt_rx_trace type)
652 {
653 	rxrpc_serial_t orig_serial;
654 	unsigned long avail;
655 	ktime_t sent_at;
656 	bool matched = false;
657 	int i;
658 
659 	avail = READ_ONCE(call->rtt_avail);
660 	smp_rmb(); /* Read avail bits before accessing data. */
661 
662 	for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
663 		if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
664 			continue;
665 
666 		sent_at = call->rtt_sent_at[i];
667 		orig_serial = call->rtt_serial[i];
668 
669 		if (orig_serial == acked_serial) {
670 			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
671 			smp_mb(); /* Read data before setting avail bit */
672 			set_bit(i, &call->rtt_avail);
673 			rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
674 					   sent_at, resp_time);
675 			matched = true;
676 		}
677 
678 		/* If a later serial is being acked, then mark this slot as
679 		 * being available.
680 		 */
681 		if (after(acked_serial, orig_serial)) {
682 			trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
683 					   orig_serial, acked_serial, 0, 0);
684 			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
685 			smp_wmb();
686 			set_bit(i, &call->rtt_avail);
687 		}
688 	}
689 
690 	if (!matched)
691 		trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
692 }
693 
694 /*
695  * Process the extra information that may be appended to an ACK packet
696  */
697 static void rxrpc_input_ack_trailer(struct rxrpc_call *call, struct sk_buff *skb,
698 				    struct rxrpc_acktrailer *trailer)
699 {
700 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
701 	struct rxrpc_peer *peer;
702 	unsigned int mtu;
703 	bool wake = false;
704 	u32 rwind = ntohl(trailer->rwind);
705 
706 	if (rwind > RXRPC_TX_MAX_WINDOW)
707 		rwind = RXRPC_TX_MAX_WINDOW;
708 	if (call->tx_winsize != rwind) {
709 		if (rwind > call->tx_winsize)
710 			wake = true;
711 		trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
712 		call->tx_winsize = rwind;
713 	}
714 
715 	mtu = min(ntohl(trailer->maxMTU), ntohl(trailer->ifMTU));
716 
717 	peer = call->peer;
718 	if (mtu < peer->maxdata) {
719 		spin_lock(&peer->lock);
720 		peer->maxdata = mtu;
721 		peer->mtu = mtu + peer->hdrsize;
722 		spin_unlock(&peer->lock);
723 	}
724 
725 	if (wake)
726 		wake_up(&call->waitq);
727 }
728 
729 /*
730  * Determine how many nacks from the previous ACK have now been satisfied.
731  */
732 static rxrpc_seq_t rxrpc_input_check_prev_ack(struct rxrpc_call *call,
733 					      struct rxrpc_ack_summary *summary,
734 					      rxrpc_seq_t seq)
735 {
736 	struct sk_buff *skb = call->cong_last_nack;
737 	struct rxrpc_ackpacket ack;
738 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
739 	unsigned int i, new_acks = 0, retained_nacks = 0;
740 	rxrpc_seq_t old_seq = sp->first_ack;
741 	u8 *acks = skb->data + sizeof(struct rxrpc_wire_header) + sizeof(ack);
742 
743 	if (after_eq(seq, old_seq + sp->nr_acks)) {
744 		summary->nr_new_acks += sp->nr_nacks;
745 		summary->nr_new_acks += seq - (old_seq + sp->nr_acks);
746 		summary->nr_retained_nacks = 0;
747 	} else if (seq == old_seq) {
748 		summary->nr_retained_nacks = sp->nr_nacks;
749 	} else {
750 		for (i = 0; i < sp->nr_acks; i++) {
751 			if (acks[i] == RXRPC_ACK_TYPE_NACK) {
752 				if (before(old_seq + i, seq))
753 					new_acks++;
754 				else
755 					retained_nacks++;
756 			}
757 		}
758 
759 		summary->nr_new_acks += new_acks;
760 		summary->nr_retained_nacks = retained_nacks;
761 	}
762 
763 	return old_seq + sp->nr_acks;
764 }
765 
766 /*
767  * Process individual soft ACKs.
768  *
769  * Each ACK in the array corresponds to one packet and can be either an ACK or
770  * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
771  * packets that lie beyond the end of the ACK list are scheduled for resend by
772  * the timer on the basis that the peer might just not have processed them at
773  * the time the ACK was sent.
774  */
775 static void rxrpc_input_soft_acks(struct rxrpc_call *call,
776 				  struct rxrpc_ack_summary *summary,
777 				  struct sk_buff *skb,
778 				  rxrpc_seq_t seq,
779 				  rxrpc_seq_t since)
780 {
781 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
782 	unsigned int i, old_nacks = 0;
783 	rxrpc_seq_t lowest_nak = seq + sp->nr_acks;
784 	u8 *acks = skb->data + sizeof(struct rxrpc_wire_header) + sizeof(struct rxrpc_ackpacket);
785 
786 	for (i = 0; i < sp->nr_acks; i++) {
787 		if (acks[i] == RXRPC_ACK_TYPE_ACK) {
788 			summary->nr_acks++;
789 			if (after_eq(seq, since))
790 				summary->nr_new_acks++;
791 		} else {
792 			summary->saw_nacks = true;
793 			if (before(seq, since)) {
794 				/* Overlap with previous ACK */
795 				old_nacks++;
796 			} else {
797 				summary->nr_new_nacks++;
798 				sp->nr_nacks++;
799 			}
800 
801 			if (before(seq, lowest_nak))
802 				lowest_nak = seq;
803 		}
804 		seq++;
805 	}
806 
807 	if (lowest_nak != call->acks_lowest_nak) {
808 		call->acks_lowest_nak = lowest_nak;
809 		summary->new_low_nack = true;
810 	}
811 
812 	/* We *can* have more nacks than we did - the peer is permitted to drop
813 	 * packets it has soft-acked and re-request them.  Further, it is
814 	 * possible for the nack distribution to change whilst the number of
815 	 * nacks stays the same or goes down.
816 	 */
817 	if (old_nacks < summary->nr_retained_nacks)
818 		summary->nr_new_acks += summary->nr_retained_nacks - old_nacks;
819 	summary->nr_retained_nacks = old_nacks;
820 }
821 
822 /*
823  * Return true if the ACK is valid - ie. it doesn't appear to have regressed
824  * with respect to the ack state conveyed by preceding ACKs.
825  */
826 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
827 			       rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
828 {
829 	rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
830 
831 	if (after(first_pkt, base))
832 		return true; /* The window advanced */
833 
834 	if (before(first_pkt, base))
835 		return false; /* firstPacket regressed */
836 
837 	if (after_eq(prev_pkt, call->acks_prev_seq))
838 		return true; /* previousPacket hasn't regressed. */
839 
840 	/* Some rx implementations put a serial number in previousPacket. */
841 	if (after_eq(prev_pkt, base + call->tx_winsize))
842 		return false;
843 	return true;
844 }
845 
846 /*
847  * Process an ACK packet.
848  *
849  * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
850  * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
851  *
852  * A hard-ACK means that a packet has been processed and may be discarded; a
853  * soft-ACK means that the packet may be discarded and retransmission
854  * requested.  A phase is complete when all packets are hard-ACK'd.
855  */
856 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
857 {
858 	struct rxrpc_ack_summary summary = { 0 };
859 	struct rxrpc_ackpacket ack;
860 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
861 	struct rxrpc_acktrailer trailer;
862 	rxrpc_serial_t ack_serial, acked_serial;
863 	rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt, since;
864 	int nr_acks, offset, ioffset;
865 
866 	_enter("");
867 
868 	offset = sizeof(struct rxrpc_wire_header);
869 	if (skb_copy_bits(skb, offset, &ack, sizeof(ack)) < 0)
870 		return rxrpc_proto_abort(call, 0, rxrpc_badmsg_short_ack);
871 	offset += sizeof(ack);
872 
873 	ack_serial = sp->hdr.serial;
874 	acked_serial = ntohl(ack.serial);
875 	first_soft_ack = ntohl(ack.firstPacket);
876 	prev_pkt = ntohl(ack.previousPacket);
877 	hard_ack = first_soft_ack - 1;
878 	nr_acks = ack.nAcks;
879 	sp->first_ack = first_soft_ack;
880 	sp->nr_acks = nr_acks;
881 	summary.ack_reason = (ack.reason < RXRPC_ACK__INVALID ?
882 			      ack.reason : RXRPC_ACK__INVALID);
883 
884 	trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
885 			   first_soft_ack, prev_pkt,
886 			   summary.ack_reason, nr_acks);
887 	rxrpc_inc_stat(call->rxnet, stat_rx_acks[ack.reason]);
888 
889 	if (acked_serial != 0) {
890 		switch (ack.reason) {
891 		case RXRPC_ACK_PING_RESPONSE:
892 			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
893 						 rxrpc_rtt_rx_ping_response);
894 			break;
895 		case RXRPC_ACK_REQUESTED:
896 			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
897 						 rxrpc_rtt_rx_requested_ack);
898 			break;
899 		default:
900 			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
901 						 rxrpc_rtt_rx_other_ack);
902 			break;
903 		}
904 	}
905 
906 	/* If we get an EXCEEDS_WINDOW ACK from the server, it probably
907 	 * indicates that the client address changed due to NAT.  The server
908 	 * lost the call because it switched to a different peer.
909 	 */
910 	if (unlikely(ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
911 	    first_soft_ack == 1 &&
912 	    prev_pkt == 0 &&
913 	    rxrpc_is_client_call(call)) {
914 		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
915 					  0, -ENETRESET);
916 		goto send_response;
917 	}
918 
919 	/* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
920 	 * indicate a change of address.  However, we can retransmit the call
921 	 * if we still have it buffered to the beginning.
922 	 */
923 	if (unlikely(ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
924 	    first_soft_ack == 1 &&
925 	    prev_pkt == 0 &&
926 	    call->acks_hard_ack == 0 &&
927 	    rxrpc_is_client_call(call)) {
928 		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
929 					  0, -ENETRESET);
930 		goto send_response;
931 	}
932 
933 	/* Discard any out-of-order or duplicate ACKs (outside lock). */
934 	if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
935 		trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
936 					   first_soft_ack, call->acks_first_seq,
937 					   prev_pkt, call->acks_prev_seq);
938 		goto send_response;
939 	}
940 
941 	trailer.maxMTU = 0;
942 	ioffset = offset + nr_acks + 3;
943 	if (skb->len >= ioffset + sizeof(trailer) &&
944 	    skb_copy_bits(skb, ioffset, &trailer, sizeof(trailer)) < 0)
945 		return rxrpc_proto_abort(call, 0, rxrpc_badmsg_short_ack_trailer);
946 
947 	if (nr_acks > 0)
948 		skb_condense(skb);
949 
950 	if (call->cong_last_nack) {
951 		since = rxrpc_input_check_prev_ack(call, &summary, first_soft_ack);
952 		rxrpc_free_skb(call->cong_last_nack, rxrpc_skb_put_last_nack);
953 		call->cong_last_nack = NULL;
954 	} else {
955 		summary.nr_new_acks = first_soft_ack - call->acks_first_seq;
956 		call->acks_lowest_nak = first_soft_ack + nr_acks;
957 		since = first_soft_ack;
958 	}
959 
960 	call->acks_latest_ts = skb->tstamp;
961 	call->acks_first_seq = first_soft_ack;
962 	call->acks_prev_seq = prev_pkt;
963 
964 	switch (ack.reason) {
965 	case RXRPC_ACK_PING:
966 		break;
967 	default:
968 		if (acked_serial && after(acked_serial, call->acks_highest_serial))
969 			call->acks_highest_serial = acked_serial;
970 		break;
971 	}
972 
973 	/* Parse rwind and mtu sizes if provided. */
974 	if (trailer.maxMTU)
975 		rxrpc_input_ack_trailer(call, skb, &trailer);
976 
977 	if (first_soft_ack == 0)
978 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_zero);
979 
980 	/* Ignore ACKs unless we are or have just been transmitting. */
981 	switch (__rxrpc_call_state(call)) {
982 	case RXRPC_CALL_CLIENT_SEND_REQUEST:
983 	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
984 	case RXRPC_CALL_SERVER_SEND_REPLY:
985 	case RXRPC_CALL_SERVER_AWAIT_ACK:
986 		break;
987 	default:
988 		goto send_response;
989 	}
990 
991 	if (before(hard_ack, call->acks_hard_ack) ||
992 	    after(hard_ack, call->tx_top))
993 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_outside_window);
994 	if (nr_acks > call->tx_top - hard_ack)
995 		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_sack_overflow);
996 
997 	if (after(hard_ack, call->acks_hard_ack)) {
998 		if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
999 			rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ack);
1000 			goto send_response;
1001 		}
1002 	}
1003 
1004 	if (nr_acks > 0) {
1005 		if (offset > (int)skb->len - nr_acks)
1006 			return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_short_sack);
1007 		rxrpc_input_soft_acks(call, &summary, skb, first_soft_ack, since);
1008 		rxrpc_get_skb(skb, rxrpc_skb_get_last_nack);
1009 		call->cong_last_nack = skb;
1010 	}
1011 
1012 	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
1013 	    summary.nr_acks == call->tx_top - hard_ack &&
1014 	    rxrpc_is_client_call(call))
1015 		rxrpc_propose_ping(call, ack_serial,
1016 				   rxrpc_propose_ack_ping_for_lost_reply);
1017 
1018 	rxrpc_congestion_management(call, skb, &summary, acked_serial);
1019 
1020 send_response:
1021 	if (ack.reason == RXRPC_ACK_PING)
1022 		rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
1023 			       rxrpc_propose_ack_respond_to_ping);
1024 	else if (sp->hdr.flags & RXRPC_REQUEST_ACK)
1025 		rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
1026 			       rxrpc_propose_ack_respond_to_ack);
1027 }
1028 
1029 /*
1030  * Process an ACKALL packet.
1031  */
1032 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1033 {
1034 	struct rxrpc_ack_summary summary = { 0 };
1035 
1036 	if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1037 		rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ackall);
1038 }
1039 
1040 /*
1041  * Process an ABORT packet directed at a call.
1042  */
1043 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1044 {
1045 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1046 
1047 	trace_rxrpc_rx_abort(call, sp->hdr.serial, skb->priority);
1048 
1049 	rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1050 				  skb->priority, -ECONNABORTED);
1051 }
1052 
1053 /*
1054  * Process an incoming call packet.
1055  */
1056 void rxrpc_input_call_packet(struct rxrpc_call *call, struct sk_buff *skb)
1057 {
1058 	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1059 	unsigned long timo;
1060 
1061 	_enter("%p,%p", call, skb);
1062 
1063 	if (sp->hdr.serviceId != call->dest_srx.srx_service)
1064 		call->dest_srx.srx_service = sp->hdr.serviceId;
1065 	if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
1066 		call->rx_serial = sp->hdr.serial;
1067 	if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
1068 		set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
1069 
1070 	timo = READ_ONCE(call->next_rx_timo);
1071 	if (timo) {
1072 		unsigned long now = jiffies, expect_rx_by;
1073 
1074 		expect_rx_by = now + timo;
1075 		WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1076 		rxrpc_reduce_call_timer(call, expect_rx_by, now,
1077 					rxrpc_timer_set_for_normal);
1078 	}
1079 
1080 	switch (sp->hdr.type) {
1081 	case RXRPC_PACKET_TYPE_DATA:
1082 		return rxrpc_input_data(call, skb);
1083 
1084 	case RXRPC_PACKET_TYPE_ACK:
1085 		return rxrpc_input_ack(call, skb);
1086 
1087 	case RXRPC_PACKET_TYPE_BUSY:
1088 		/* Just ignore BUSY packets from the server; the retry and
1089 		 * lifespan timers will take care of business.  BUSY packets
1090 		 * from the client don't make sense.
1091 		 */
1092 		return;
1093 
1094 	case RXRPC_PACKET_TYPE_ABORT:
1095 		return rxrpc_input_abort(call, skb);
1096 
1097 	case RXRPC_PACKET_TYPE_ACKALL:
1098 		return rxrpc_input_ackall(call, skb);
1099 
1100 	default:
1101 		break;
1102 	}
1103 }
1104 
1105 /*
1106  * Handle a new service call on a channel implicitly completing the preceding
1107  * call on that channel.  This does not apply to client conns.
1108  *
1109  * TODO: If callNumber > call_id + 1, renegotiate security.
1110  */
1111 void rxrpc_implicit_end_call(struct rxrpc_call *call, struct sk_buff *skb)
1112 {
1113 	switch (__rxrpc_call_state(call)) {
1114 	case RXRPC_CALL_SERVER_AWAIT_ACK:
1115 		rxrpc_call_completed(call);
1116 		fallthrough;
1117 	case RXRPC_CALL_COMPLETE:
1118 		break;
1119 	default:
1120 		rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ESHUTDOWN,
1121 				 rxrpc_eproto_improper_term);
1122 		trace_rxrpc_improper_term(call);
1123 		break;
1124 	}
1125 
1126 	rxrpc_input_call_event(call, skb);
1127 }
1128