input.c 29.9 KB
Newer Older
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 6 7
 * Written by David Howells (dhowells@redhat.com)
 */

8 9
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

10 11
#include "ar-internal.h"

12 13
static void rxrpc_proto_abort(struct rxrpc_call *call, rxrpc_seq_t seq,
			      enum rxrpc_abort_reason why)
14
{
15
	rxrpc_abort_call(call, seq, RX_PROTOCOL_ERROR, -EBADMSG, why);
16 17
}

18 19 20 21 22
/*
 * Do TCP-style congestion management [RFC 5681].
 */
static void rxrpc_congestion_management(struct rxrpc_call *call,
					struct sk_buff *skb,
23 24
					struct rxrpc_ack_summary *summary,
					rxrpc_serial_t acked_serial)
25 26 27 28 29 30 31
{
	enum rxrpc_congest_change change = rxrpc_cong_no_change;
	unsigned int cumulative_acks = call->cong_cumul_acks;
	unsigned int cwnd = call->cong_cwnd;
	bool resend = false;

	summary->flight_size =
32
		(call->tx_top - call->acks_hard_ack) - summary->nr_acks;
33 34 35 36 37 38

	if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
		summary->retrans_timeo = true;
		call->cong_ssthresh = max_t(unsigned int,
					    summary->flight_size / 2, 2);
		cwnd = 1;
39
		if (cwnd >= call->cong_ssthresh &&
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		    call->cong_mode == RXRPC_CALL_SLOW_START) {
			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
			call->cong_tstamp = skb->tstamp;
			cumulative_acks = 0;
		}
	}

	cumulative_acks += summary->nr_new_acks;
	if (cumulative_acks > 255)
		cumulative_acks = 255;

	summary->cwnd = call->cong_cwnd;
	summary->ssthresh = call->cong_ssthresh;
	summary->cumulative_acks = cumulative_acks;
	summary->dup_acks = call->cong_dup_acks;

	switch (call->cong_mode) {
	case RXRPC_CALL_SLOW_START:
58
		if (summary->saw_nacks)
59 60 61
			goto packet_loss_detected;
		if (summary->cumulative_acks > 0)
			cwnd += 1;
62
		if (cwnd >= call->cong_ssthresh) {
63 64 65 66 67 68
			call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
			call->cong_tstamp = skb->tstamp;
		}
		goto out;

	case RXRPC_CALL_CONGEST_AVOIDANCE:
69
		if (summary->saw_nacks)
70 71 72 73 74
			goto packet_loss_detected;

		/* We analyse the number of packets that get ACK'd per RTT
		 * period and increase the window if we managed to fill it.
		 */
75
		if (call->peer->rtt_count == 0)
76 77
			goto out;
		if (ktime_before(skb->tstamp,
78 79
				 ktime_add_us(call->cong_tstamp,
					      call->peer->srtt_us >> 3)))
80 81 82 83 84 85 86 87
			goto out_no_clear_ca;
		change = rxrpc_cong_rtt_window_end;
		call->cong_tstamp = skb->tstamp;
		if (cumulative_acks >= cwnd)
			cwnd++;
		goto out;

	case RXRPC_CALL_PACKET_LOSS:
88
		if (!summary->saw_nacks)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
			goto resume_normality;

		if (summary->new_low_nack) {
			change = rxrpc_cong_new_low_nack;
			call->cong_dup_acks = 1;
			if (call->cong_extra > 1)
				call->cong_extra = 1;
			goto send_extra_data;
		}

		call->cong_dup_acks++;
		if (call->cong_dup_acks < 3)
			goto send_extra_data;

		change = rxrpc_cong_begin_retransmission;
		call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
		call->cong_ssthresh = max_t(unsigned int,
					    summary->flight_size / 2, 2);
		cwnd = call->cong_ssthresh + 3;
		call->cong_extra = 0;
		call->cong_dup_acks = 0;
		resend = true;
		goto out;

	case RXRPC_CALL_FAST_RETRANSMIT:
		if (!summary->new_low_nack) {
			if (summary->nr_new_acks == 0)
				cwnd += 1;
			call->cong_dup_acks++;
			if (call->cong_dup_acks == 2) {
				change = rxrpc_cong_retransmit_again;
				call->cong_dup_acks = 0;
				resend = true;
			}
		} else {
			change = rxrpc_cong_progress;
			cwnd = call->cong_ssthresh;
126
			if (!summary->saw_nacks)
127 128 129 130 131 132 133 134 135 136 137 138 139 140
				goto resume_normality;
		}
		goto out;

	default:
		BUG();
		goto out;
	}

resume_normality:
	change = rxrpc_cong_cleared_nacks;
	call->cong_dup_acks = 0;
	call->cong_extra = 0;
	call->cong_tstamp = skb->tstamp;
141
	if (cwnd < call->cong_ssthresh)
142 143 144 145 146 147
		call->cong_mode = RXRPC_CALL_SLOW_START;
	else
		call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
out:
	cumulative_acks = 0;
out_no_clear_ca:
148 149
	if (cwnd >= RXRPC_TX_MAX_WINDOW)
		cwnd = RXRPC_TX_MAX_WINDOW;
150 151
	call->cong_cwnd = cwnd;
	call->cong_cumul_acks = cumulative_acks;
152
	summary->mode = call->cong_mode;
153
	trace_rxrpc_congest(call, summary, acked_serial, change);
154 155
	if (resend)
		rxrpc_resend(call, skb);
156 157 158 159 160 161 162 163 164 165 166 167
	return;

packet_loss_detected:
	change = rxrpc_cong_saw_nack;
	call->cong_mode = RXRPC_CALL_PACKET_LOSS;
	call->cong_dup_acks = 0;
	goto send_extra_data;

send_extra_data:
	/* Send some previously unsent DATA if we have some to advance the ACK
	 * state.
	 */
168 169
	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
	    summary->nr_acks != call->tx_top - call->acks_hard_ack) {
170 171 172 173 174 175
		call->cong_extra++;
		wake_up(&call->waitq);
	}
	goto out_no_clear_ca;
}

176 177 178 179 180 181 182 183 184 185
/*
 * Degrade the congestion window if we haven't transmitted a packet for >1RTT.
 */
void rxrpc_congestion_degrade(struct rxrpc_call *call)
{
	ktime_t rtt, now;

	if (call->cong_mode != RXRPC_CALL_SLOW_START &&
	    call->cong_mode != RXRPC_CALL_CONGEST_AVOIDANCE)
		return;
186
	if (__rxrpc_call_state(call) == RXRPC_CALL_CLIENT_AWAIT_REPLY)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		return;

	rtt = ns_to_ktime(call->peer->srtt_us * (1000 / 8));
	now = ktime_get_real();
	if (!ktime_before(ktime_add(call->tx_last_sent, rtt), now))
		return;

	trace_rxrpc_reset_cwnd(call, now);
	rxrpc_inc_stat(call->rxnet, stat_tx_data_cwnd_reset);
	call->tx_last_sent = now;
	call->cong_mode = RXRPC_CALL_SLOW_START;
	call->cong_ssthresh = max_t(unsigned int, call->cong_ssthresh,
				    call->cong_cwnd * 3 / 4);
	call->cong_cwnd = max_t(unsigned int, call->cong_cwnd / 2, RXRPC_MIN_CWND);
}

203
/*
204
 * Apply a hard ACK by advancing the Tx window.
205
 */
206
static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
207
				   struct rxrpc_ack_summary *summary)
208
{
209
	struct rxrpc_txbuf *txb;
210
	bool rot_last = false;
211

212 213 214
	list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
		if (before_eq(txb->seq, call->acks_hard_ack))
			continue;
215
		if (txb->flags & RXRPC_LAST_PACKET) {
216
			set_bit(RXRPC_CALL_TX_LAST, &call->flags);
217 218
			rot_last = true;
		}
219 220
		if (txb->seq == to)
			break;
221
	}
222

223 224
	if (rot_last)
		set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
225

226
	_enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
227

228 229
	if (call->acks_lowest_nak == call->acks_hard_ack) {
		call->acks_lowest_nak = to;
230
	} else if (after(to, call->acks_lowest_nak)) {
231 232
		summary->new_low_nack = true;
		call->acks_lowest_nak = to;
233
	}
234

235 236 237 238 239 240
	smp_store_release(&call->acks_hard_ack, to);

	trace_rxrpc_txqueue(call, (rot_last ?
				   rxrpc_txqueue_rotate_last :
				   rxrpc_txqueue_rotate));
	wake_up(&call->waitq);
241
	return rot_last;
242
}
243

244 245 246 247 248 249
/*
 * End the transmission phase of a call.
 *
 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
 * or a final ACK packet.
 */
250 251
static void rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
			       enum rxrpc_abort_reason abort_why)
252
{
253
	ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
254

255 256 257
	call->resend_at = KTIME_MAX;
	trace_rxrpc_timer_can(call, rxrpc_timer_trace_resend);

258 259 260 261 262
	if (unlikely(call->cong_last_nack)) {
		rxrpc_free_skb(call->cong_last_nack, rxrpc_skb_put_last_nack);
		call->cong_last_nack = NULL;
	}

263
	switch (__rxrpc_call_state(call)) {
264
	case RXRPC_CALL_CLIENT_SEND_REQUEST:
265
	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
266 267 268 269 270 271 272 273
		if (reply_begun) {
			rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_RECV_REPLY);
			trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
			break;
		}

		rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
		trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
274
		break;
275

276
	case RXRPC_CALL_SERVER_AWAIT_ACK:
277 278
		rxrpc_call_completed(call);
		trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
279
		break;
280 281

	default:
282 283 284
		kdebug("end_tx %s", rxrpc_call_states[__rxrpc_call_state(call)]);
		rxrpc_proto_abort(call, call->tx_top, abort_why);
		break;
285
	}
286 287 288 289 290 291 292
}

/*
 * Begin the reply reception phase of a call.
 */
static bool rxrpc_receiving_reply(struct rxrpc_call *call)
{
293
	struct rxrpc_ack_summary summary = { 0 };
294 295
	rxrpc_seq_t top = READ_ONCE(call->tx_top);

296
	if (call->ackr_reason) {
297 298
		call->delay_ack_at = KTIME_MAX;
		trace_rxrpc_timer_can(call, rxrpc_timer_trace_delayed_ack);
299 300
	}

301
	if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
302
		if (!rxrpc_rotate_tx_window(call, top, &summary)) {
303
			rxrpc_proto_abort(call, top, rxrpc_eproto_early_reply);
304 305
			return false;
		}
306
	}
307 308 309

	rxrpc_end_tx_phase(call, true, rxrpc_eproto_unexpected_reply);
	return true;
310 311
}

312 313 314 315 316 317 318
/*
 * End the packet reception phase.
 */
static void rxrpc_end_rx_phase(struct rxrpc_call *call, rxrpc_serial_t serial)
{
	rxrpc_seq_t whigh = READ_ONCE(call->rx_highest_seq);

319
	_enter("%d,%s", call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)]);
320 321 322

	trace_rxrpc_receive(call, rxrpc_receive_end, 0, whigh);

323
	switch (__rxrpc_call_state(call)) {
324
	case RXRPC_CALL_CLIENT_RECV_REPLY:
325 326
		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_terminal_ack);
		rxrpc_call_completed(call);
327 328 329
		break;

	case RXRPC_CALL_SERVER_RECV_REQUEST:
330
		rxrpc_set_call_state(call, RXRPC_CALL_SERVER_ACK_REQUEST);
331
		call->expect_req_by = KTIME_MAX;
332
		rxrpc_propose_delay_ACK(call, serial, rxrpc_propose_ack_processing_op);
333
		break;
334

335 336 337 338 339
	default:
		break;
	}
}

340 341 342
static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
					  rxrpc_seq_t window, rxrpc_seq_t wtop)
{
343 344
	call->ackr_window = window;
	call->ackr_wtop = wtop;
345 346
}

347
/*
348 349 350 351 352 353 354 355 356 357 358 359
 * Push a DATA packet onto the Rx queue.
 */
static void 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)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;

	__skb_queue_tail(&call->recvmsg_queue, skb);
	rxrpc_input_update_ack_window(call, window, wtop);
	trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
360 361
	if (last)
		rxrpc_end_rx_phase(call, sp->hdr.serial);
362 363 364 365
}

/*
 * Process a DATA packet.
366
 */
367 368
static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb,
				 bool *_notify)
369 370
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
371
	struct sk_buff *oos;
372
	rxrpc_serial_t serial = sp->hdr.serial;
373
	unsigned int sack = call->ackr_sack_base;
374 375
	rxrpc_seq_t window = call->ackr_window;
	rxrpc_seq_t wtop = call->ackr_wtop;
376 377
	rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
	rxrpc_seq_t seq = sp->hdr.seq;
378
	bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
379
	int ack_reason = -1;
380

381 382 383 384 385
	rxrpc_inc_stat(call->rxnet, stat_rx_data);
	if (sp->hdr.flags & RXRPC_REQUEST_ACK)
		rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
	if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
		rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
386

387
	if (last) {
388
		if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
389 390
		    seq + 1 != wtop)
			return rxrpc_proto_abort(call, seq, rxrpc_eproto_different_last);
391 392
	} else {
		if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
393 394 395
		    after_eq(seq, wtop)) {
			pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
				call->debug_id, seq, window, wtop, wlimit);
396
			return rxrpc_proto_abort(call, seq, rxrpc_eproto_data_after_last);
397
		}
398
	}
399

400 401 402
	if (after(seq, call->rx_highest_seq))
		call->rx_highest_seq = seq;

403
	trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
404

405 406 407
	if (before(seq, window)) {
		ack_reason = RXRPC_ACK_DUPLICATE;
		goto send_ack;
408
	}
409 410 411
	if (after(seq, wlimit)) {
		ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
		goto send_ack;
412 413
	}

414 415 416 417 418 419 420
	/* Queue the packet. */
	if (seq == window) {
		if (sp->hdr.flags & RXRPC_REQUEST_ACK)
			ack_reason = RXRPC_ACK_REQUESTED;
		/* Send an immediate ACK if we fill in a hole */
		else if (!skb_queue_empty(&call->rx_oos_queue))
			ack_reason = RXRPC_ACK_DELAY;
421
		else
422
			call->ackr_nr_unacked++;
423

424
		window++;
425 426
		if (after(window, wtop)) {
			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_none);
427
			wtop = window;
428 429 430 431 432
		} else {
			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_advance);
			sack = (sack + 1) % RXRPC_SACK_SIZE;
		}

433

434 435
		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg);

436 437
		spin_lock(&call->recvmsg_queue.lock);
		rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
438
		*_notify = true;
439 440 441 442 443 444 445 446 447 448

		while ((oos = skb_peek(&call->rx_oos_queue))) {
			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);

			if (after(osp->hdr.seq, window))
				break;

			__skb_unlink(oos, &call->rx_oos_queue);
			last = osp->hdr.flags & RXRPC_LAST_PACKET;
			seq = osp->hdr.seq;
449 450 451
			call->ackr_sack_table[sack] = 0;
			trace_rxrpc_sack(call, seq, sack, rxrpc_sack_fill);
			sack = (sack + 1) % RXRPC_SACK_SIZE;
452 453 454

			window++;
			rxrpc_input_queue_data(call, oos, window, wtop,
455
					       rxrpc_receive_queue_oos);
456 457
		}

458
		spin_unlock(&call->recvmsg_queue.lock);
459

460
		call->ackr_sack_base = sack;
461
	} else {
462
		unsigned int slot;
463

464 465
		ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;

466 467 468 469 470 471
		slot = seq - window;
		sack = (sack + slot) % RXRPC_SACK_SIZE;

		if (call->ackr_sack_table[sack % RXRPC_SACK_SIZE]) {
			ack_reason = RXRPC_ACK_DUPLICATE;
			goto send_ack;
472 473
		}

474 475 476
		call->ackr_sack_table[sack % RXRPC_SACK_SIZE] |= 1;
		trace_rxrpc_sack(call, seq, sack, rxrpc_sack_oos);

477 478 479 480 481 482 483 484 485
		if (after(seq + 1, wtop)) {
			wtop = seq + 1;
			rxrpc_input_update_ack_window(call, window, wtop);
		}

		skb_queue_walk(&call->rx_oos_queue, oos) {
			struct rxrpc_skb_priv *osp = rxrpc_skb(oos);

			if (after(osp->hdr.seq, seq)) {
486
				rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
487 488 489
				__skb_queue_before(&call->rx_oos_queue, oos, skb);
				goto oos_queued;
			}
490
		}
491

492
		rxrpc_get_skb(skb, rxrpc_skb_get_to_recvmsg_oos);
493 494 495 496
		__skb_queue_tail(&call->rx_oos_queue, skb);
	oos_queued:
		trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
				    sp->hdr.serial, sp->hdr.seq);
497 498
	}

499 500 501
send_ack:
	if (ack_reason >= 0)
		rxrpc_send_ACK(call, ack_reason, serial,
502 503 504 505
			       rxrpc_propose_ack_input_data);
	else
		rxrpc_propose_delay_ACK(call, serial,
					rxrpc_propose_ack_input_data);
506 507 508
}

/*
509
 * Split a jumbo packet and file the bits separately.
510
 */
511
static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
512
{
513 514 515 516 517
	struct rxrpc_jumbo_header jhdr;
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
	struct sk_buff *jskb;
	unsigned int offset = sizeof(struct rxrpc_wire_header);
	unsigned int len = skb->len - offset;
518
	bool notify = false;
519

520 521 522 523 524 525 526 527 528
	while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
		if (len < RXRPC_JUMBO_SUBPKTLEN)
			goto protocol_error;
		if (sp->hdr.flags & RXRPC_LAST_PACKET)
			goto protocol_error;
		if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
				  &jhdr, sizeof(jhdr)) < 0)
			goto protocol_error;

529
		jskb = skb_clone(skb, GFP_NOFS);
530 531 532 533
		if (!jskb) {
			kdebug("couldn't clone");
			return false;
		}
534
		rxrpc_new_skb(jskb, rxrpc_skb_new_jumbo_subpacket);
535 536 537
		jsp = rxrpc_skb(jskb);
		jsp->offset = offset;
		jsp->len = RXRPC_JUMBO_DATALEN;
538 539
		rxrpc_input_data_one(call, jskb, &notify);
		rxrpc_free_skb(jskb, rxrpc_skb_put_jumbo_subpacket);
540 541 542 543 544 545 546

		sp->hdr.flags = jhdr.flags;
		sp->hdr._rsvd = ntohs(jhdr._rsvd);
		sp->hdr.seq++;
		sp->hdr.serial++;
		offset += RXRPC_JUMBO_SUBPKTLEN;
		len -= RXRPC_JUMBO_SUBPKTLEN;
547
	}
548 549 550

	sp->offset = offset;
	sp->len    = len;
551 552 553 554 555
	rxrpc_input_data_one(call, skb, &notify);
	if (notify) {
		trace_rxrpc_notify_socket(call->debug_id, sp->hdr.serial);
		rxrpc_notify_socket(call);
	}
556 557 558 559
	return true;

protocol_error:
	return false;
560
}
561

562
/*
563 564
 * Process a DATA packet, adding the packet to the Rx ring.  The caller's
 * packet ref must be passed on or discarded.
565
 */
566
static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
567 568
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
569 570
	rxrpc_serial_t serial = sp->hdr.serial;
	rxrpc_seq_t seq0 = sp->hdr.seq;
571

572 573
	_enter("{%x,%x,%x},{%u,%x}",
	       call->ackr_window, call->ackr_wtop, call->rx_highest_seq,
574
	       skb->len, seq0);
575

576
	if (__rxrpc_call_is_complete(call))
577
		return;
578

579 580 581 582 583 584 585 586 587 588 589
	switch (__rxrpc_call_state(call)) {
	case RXRPC_CALL_CLIENT_SEND_REQUEST:
	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
		/* Received data implicitly ACKs all of the request
		 * packets we sent when we're acting as a client.
		 */
		if (!rxrpc_receiving_reply(call))
			goto out_notify;
		break;

	case RXRPC_CALL_SERVER_RECV_REQUEST: {
David Howells's avatar
David Howells committed
590 591 592
		unsigned long timo = READ_ONCE(call->next_req_timo);

		if (timo) {
593 594 595 596
			ktime_t delay = ms_to_ktime(timo);

			call->expect_req_by = ktime_add(ktime_get_real(), delay);
			trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_idle);
David Howells's avatar
David Howells committed
597
		}
598
		break;
David Howells's avatar
David Howells committed
599 600
	}

601 602 603
	default:
		break;
	}
604

605
	if (!rxrpc_input_split_jumbo(call, skb)) {
606
		rxrpc_proto_abort(call, sp->hdr.seq, rxrpc_badmsg_bad_jumbo);
607
		goto out_notify;
608
	}
609
	return;
610

611
out_notify:
612 613
	trace_rxrpc_notify_socket(call->debug_id, serial);
	rxrpc_notify_socket(call);
614
	_leave(" [queued]");
615 616
}

617
/*
618
 * See if there's a cached RTT probe to complete.
619
 */
620 621 622 623 624
static void 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)
625
{
626 627
	rxrpc_serial_t orig_serial;
	unsigned long avail;
628
	ktime_t sent_at;
629 630
	bool matched = false;
	int i;
631

632 633
	avail = READ_ONCE(call->rtt_avail);
	smp_rmb(); /* Read avail bits before accessing data. */
634

635 636
	for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
		if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
637
			continue;
David Howells's avatar
David Howells committed
638

639 640 641 642 643 644 645
		sent_at = call->rtt_sent_at[i];
		orig_serial = call->rtt_serial[i];

		if (orig_serial == acked_serial) {
			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
			smp_mb(); /* Read data before setting avail bit */
			set_bit(i, &call->rtt_avail);
646 647
			rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
					   sent_at, resp_time);
648 649 650 651 652 653 654 655 656 657 658 659 660 661
			matched = true;
		}

		/* If a later serial is being acked, then mark this slot as
		 * being available.
		 */
		if (after(acked_serial, orig_serial)) {
			trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
					   orig_serial, acked_serial, 0, 0);
			clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
			smp_wmb();
			set_bit(i, &call->rtt_avail);
		}
	}
662

663 664
	if (!matched)
		trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
665 666
}

667
/*
668
 * Process the extra information that may be appended to an ACK packet
669
 */
670 671
static void rxrpc_input_ack_trailer(struct rxrpc_call *call, struct sk_buff *skb,
				    struct rxrpc_acktrailer *trailer)
672
{
673 674 675
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	struct rxrpc_peer *peer;
	unsigned int mtu;
676
	bool wake = false;
677
	u32 rwind = ntohl(trailer->rwind);
678

679 680
	if (rwind > RXRPC_TX_MAX_WINDOW)
		rwind = RXRPC_TX_MAX_WINDOW;
681 682 683
	if (call->tx_winsize != rwind) {
		if (rwind > call->tx_winsize)
			wake = true;
684
		trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
685 686 687
		call->tx_winsize = rwind;
	}

688 689
	if (call->cong_ssthresh > rwind)
		call->cong_ssthresh = rwind;
690

691
	mtu = min(ntohl(trailer->maxMTU), ntohl(trailer->ifMTU));
692 693 694

	peer = call->peer;
	if (mtu < peer->maxdata) {
695
		spin_lock(&peer->lock);
696 697
		peer->maxdata = mtu;
		peer->mtu = mtu + peer->hdrsize;
698
		spin_unlock(&peer->lock);
699
	}
700 701 702

	if (wake)
		wake_up(&call->waitq);
703
}
704

705 706 707 708 709 710 711 712 713 714
/*
 * Determine how many nacks from the previous ACK have now been satisfied.
 */
static rxrpc_seq_t rxrpc_input_check_prev_ack(struct rxrpc_call *call,
					      struct rxrpc_ack_summary *summary,
					      rxrpc_seq_t seq)
{
	struct sk_buff *skb = call->cong_last_nack;
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	unsigned int i, new_acks = 0, retained_nacks = 0;
715 716
	rxrpc_seq_t old_seq = sp->ack.first_ack;
	u8 *acks = skb->data + sizeof(struct rxrpc_wire_header) + sizeof(struct rxrpc_ackpacket);
717

718 719 720
	if (after_eq(seq, old_seq + sp->ack.nr_acks)) {
		summary->nr_new_acks += sp->ack.nr_nacks;
		summary->nr_new_acks += seq - (old_seq + sp->ack.nr_acks);
721 722
		summary->nr_retained_nacks = 0;
	} else if (seq == old_seq) {
723
		summary->nr_retained_nacks = sp->ack.nr_nacks;
724
	} else {
725
		for (i = 0; i < sp->ack.nr_acks; i++) {
726 727 728 729 730 731 732 733 734 735 736 737
			if (acks[i] == RXRPC_ACK_TYPE_NACK) {
				if (before(old_seq + i, seq))
					new_acks++;
				else
					retained_nacks++;
			}
		}

		summary->nr_new_acks += new_acks;
		summary->nr_retained_nacks = retained_nacks;
	}

738
	return old_seq + sp->ack.nr_acks;
739 740
}

741 742 743 744 745 746 747 748 749
/*
 * Process individual soft ACKs.
 *
 * Each ACK in the array corresponds to one packet and can be either an ACK or
 * a NAK.  If we get find an explicitly NAK'd packet we resend immediately;
 * packets that lie beyond the end of the ACK list are scheduled for resend by
 * the timer on the basis that the peer might just not have processed them at
 * the time the ACK was sent.
 */
750 751 752 753 754
static void 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)
755
{
756 757
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
	unsigned int i, old_nacks = 0;
758
	rxrpc_seq_t lowest_nak = seq + sp->ack.nr_acks;
759
	u8 *acks = skb->data + sizeof(struct rxrpc_wire_header) + sizeof(struct rxrpc_ackpacket);
760

761
	for (i = 0; i < sp->ack.nr_acks; i++) {
762
		if (acks[i] == RXRPC_ACK_TYPE_ACK) {
763
			summary->nr_acks++;
764 765
			if (after_eq(seq, since))
				summary->nr_new_acks++;
766 767
		} else {
			summary->saw_nacks = true;
768 769 770 771 772
			if (before(seq, since)) {
				/* Overlap with previous ACK */
				old_nacks++;
			} else {
				summary->nr_new_nacks++;
773
				sp->ack.nr_nacks++;
774 775 776 777
			}

			if (before(seq, lowest_nak))
				lowest_nak = seq;
778
		}
779 780 781 782 783 784
		seq++;
	}

	if (lowest_nak != call->acks_lowest_nak) {
		call->acks_lowest_nak = lowest_nak;
		summary->new_low_nack = true;
785
	}
786 787 788 789 790 791 792 793 794

	/* We *can* have more nacks than we did - the peer is permitted to drop
	 * packets it has soft-acked and re-request them.  Further, it is
	 * possible for the nack distribution to change whilst the number of
	 * nacks stays the same or goes down.
	 */
	if (old_nacks < summary->nr_retained_nacks)
		summary->nr_new_acks += summary->nr_retained_nacks - old_nacks;
	summary->nr_retained_nacks = old_nacks;
795 796
}

David Howells's avatar
David Howells committed
797 798 799 800 801 802 803
/*
 * Return true if the ACK is valid - ie. it doesn't appear to have regressed
 * with respect to the ack state conveyed by preceding ACKs.
 */
static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
			       rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
{
804
	rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
David Howells's avatar
David Howells committed
805 806 807 808 809 810 811

	if (after(first_pkt, base))
		return true; /* The window advanced */

	if (before(first_pkt, base))
		return false; /* firstPacket regressed */

812
	if (after_eq(prev_pkt, call->acks_prev_seq))
David Howells's avatar
David Howells committed
813 814 815 816 817 818 819 820
		return true; /* previousPacket hasn't regressed. */

	/* Some rx implementations put a serial number in previousPacket. */
	if (after_eq(prev_pkt, base + call->tx_winsize))
		return false;
	return true;
}

821
/*
822 823 824 825 826 827 828 829
 * Process an ACK packet.
 *
 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
 * in the ACK array.  Anything before that is hard-ACK'd and may be discarded.
 *
 * A hard-ACK means that a packet has been processed and may be discarded; a
 * soft-ACK means that the packet may be discarded and retransmission
 * requested.  A phase is complete when all packets are hard-ACK'd.
830
 */
831
static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
832
{
833
	struct rxrpc_ack_summary summary = { 0 };
834
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
835
	struct rxrpc_acktrailer trailer;
836
	rxrpc_serial_t ack_serial, acked_serial;
837
	rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt, since;
838
	int nr_acks, offset, ioffset;
839 840 841

	_enter("");

842 843 844 845 846 847 848 849 850 851
	offset = sizeof(struct rxrpc_wire_header) + sizeof(struct rxrpc_ackpacket);

	ack_serial	= sp->hdr.serial;
	acked_serial	= sp->ack.acked_serial;
	first_soft_ack	= sp->ack.first_ack;
	prev_pkt	= sp->ack.prev_ack;
	nr_acks		= sp->ack.nr_acks;
	hard_ack	= first_soft_ack - 1;
	summary.ack_reason = (sp->ack.reason < RXRPC_ACK__INVALID ?
			      sp->ack.reason : RXRPC_ACK__INVALID);
852

853
	trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
854
			   first_soft_ack, prev_pkt,
855
			   summary.ack_reason, nr_acks);
856
	rxrpc_inc_stat(call->rxnet, stat_rx_acks[summary.ack_reason]);
857

858
	if (acked_serial != 0) {
859
		switch (summary.ack_reason) {
860
		case RXRPC_ACK_PING_RESPONSE:
861
			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
862 863 864 865 866 867 868 869 870 871 872
						 rxrpc_rtt_rx_ping_response);
			break;
		case RXRPC_ACK_REQUESTED:
			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
						 rxrpc_rtt_rx_requested_ack);
			break;
		default:
			rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
						 rxrpc_rtt_rx_other_ack);
			break;
		}
873
	}
874

875 876 877 878
	/* If we get an EXCEEDS_WINDOW ACK from the server, it probably
	 * indicates that the client address changed due to NAT.  The server
	 * lost the call because it switched to a different peer.
	 */
879
	if (unlikely(summary.ack_reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
880 881 882 883 884
	    first_soft_ack == 1 &&
	    prev_pkt == 0 &&
	    rxrpc_is_client_call(call)) {
		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
					  0, -ENETRESET);
885
		goto send_response;
886 887 888 889 890 891
	}

	/* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
	 * indicate a change of address.  However, we can retransmit the call
	 * if we still have it buffered to the beginning.
	 */
892
	if (unlikely(summary.ack_reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
893 894
	    first_soft_ack == 1 &&
	    prev_pkt == 0 &&
895
	    call->acks_hard_ack == 0 &&
896 897 898
	    rxrpc_is_client_call(call)) {
		rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
					  0, -ENETRESET);
899
		goto send_response;
900 901
	}

902
	/* Discard any out-of-order or duplicate ACKs (outside lock). */
David Howells's avatar
David Howells committed
903
	if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
904
		trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
905 906
					   first_soft_ack, call->acks_first_seq,
					   prev_pkt, call->acks_prev_seq);
907
		goto send_response;
908
	}
909

910
	trailer.maxMTU = 0;
911
	ioffset = offset + nr_acks + 3;
912 913 914
	if (skb->len >= ioffset + sizeof(trailer) &&
	    skb_copy_bits(skb, ioffset, &trailer, sizeof(trailer)) < 0)
		return rxrpc_proto_abort(call, 0, rxrpc_badmsg_short_ack_trailer);
915 916 917

	if (nr_acks > 0)
		skb_condense(skb);
918

919 920 921 922 923 924 925 926 927 928
	if (call->cong_last_nack) {
		since = rxrpc_input_check_prev_ack(call, &summary, first_soft_ack);
		rxrpc_free_skb(call->cong_last_nack, rxrpc_skb_put_last_nack);
		call->cong_last_nack = NULL;
	} else {
		summary.nr_new_acks = first_soft_ack - call->acks_first_seq;
		call->acks_lowest_nak = first_soft_ack + nr_acks;
		since = first_soft_ack;
	}

929
	call->acks_latest_ts = skb->tstamp;
930 931
	call->acks_first_seq = first_soft_ack;
	call->acks_prev_seq = prev_pkt;
932

933
	switch (summary.ack_reason) {
934 935 936
	case RXRPC_ACK_PING:
		break;
	default:
937
		if (acked_serial && after(acked_serial, call->acks_highest_serial))
938 939 940
			call->acks_highest_serial = acked_serial;
		break;
	}
941

942
	/* Parse rwind and mtu sizes if provided. */
943 944
	if (trailer.maxMTU)
		rxrpc_input_ack_trailer(call, skb, &trailer);
945

946
	if (first_soft_ack == 0)
947
		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_zero);
948

949
	/* Ignore ACKs unless we are or have just been transmitting. */
950
	switch (__rxrpc_call_state(call)) {
951 952 953 954 955
	case RXRPC_CALL_CLIENT_SEND_REQUEST:
	case RXRPC_CALL_CLIENT_AWAIT_REPLY:
	case RXRPC_CALL_SERVER_SEND_REPLY:
	case RXRPC_CALL_SERVER_AWAIT_ACK:
		break;
956
	default:
957
		goto send_response;
958
	}
959

960
	if (before(hard_ack, call->acks_hard_ack) ||
961
	    after(hard_ack, call->tx_top))
962
		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_outside_window);
963
	if (nr_acks > call->tx_top - hard_ack)
964
		return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_sack_overflow);
965

966
	if (after(hard_ack, call->acks_hard_ack)) {
967
		if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
968
			rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ack);
969
			goto send_response;
970 971
		}
	}
972

973
	if (nr_acks > 0) {
974
		if (offset > (int)skb->len - nr_acks)
975
			return rxrpc_proto_abort(call, 0, rxrpc_eproto_ackr_short_sack);
976 977 978
		rxrpc_input_soft_acks(call, &summary, skb, first_soft_ack, since);
		rxrpc_get_skb(skb, rxrpc_skb_get_last_nack);
		call->cong_last_nack = skb;
979 980
	}

981
	if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
982 983
	    summary.nr_acks == call->tx_top - hard_ack &&
	    rxrpc_is_client_call(call))
984 985
		rxrpc_propose_ping(call, ack_serial,
				   rxrpc_propose_ack_ping_for_lost_reply);
986

987
	rxrpc_congestion_management(call, skb, &summary, acked_serial);
988 989

send_response:
990
	if (summary.ack_reason == RXRPC_ACK_PING)
991 992 993 994 995
		rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
			       rxrpc_propose_ack_respond_to_ping);
	else if (sp->hdr.flags & RXRPC_REQUEST_ACK)
		rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
			       rxrpc_propose_ack_respond_to_ack);
996 997 998
}

/*
999
 * Process an ACKALL packet.
1000
 */
1001
static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1002
{
1003
	struct rxrpc_ack_summary summary = { 0 };
1004

1005
	if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1006
		rxrpc_end_tx_phase(call, false, rxrpc_eproto_unexpected_ackall);
1007
}
1008

1009
/*
1010
 * Process an ABORT packet directed at a call.
1011 1012 1013 1014
 */
static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1015

1016
	trace_rxrpc_rx_abort(call, sp->hdr.serial, skb->priority);
1017

1018
	rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1019
				  skb->priority, -ECONNABORTED);
1020 1021 1022
}

/*
1023
 * Process an incoming call packet.
1024
 */
1025
void rxrpc_input_call_packet(struct rxrpc_call *call, struct sk_buff *skb)
1026
{
1027
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells's avatar
David Howells committed
1028
	unsigned long timo;
1029

1030
	_enter("%p,%p", call, skb);
1031

1032 1033 1034 1035 1036 1037 1038
	if (sp->hdr.serviceId != call->dest_srx.srx_service)
		call->dest_srx.srx_service = sp->hdr.serviceId;
	if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
		call->rx_serial = sp->hdr.serial;
	if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
		set_bit(RXRPC_CALL_RX_HEARD, &call->flags);

David Howells's avatar
David Howells committed
1039 1040
	timo = READ_ONCE(call->next_rx_timo);
	if (timo) {
1041
		ktime_t delay = ms_to_ktime(timo);
David Howells's avatar
David Howells committed
1042

1043 1044
		call->expect_rx_by = ktime_add(ktime_get_real(), delay);
		trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_expect_rx);
David Howells's avatar
David Howells committed
1045
	}
David Howells's avatar
David Howells committed
1046

1047 1048
	switch (sp->hdr.type) {
	case RXRPC_PACKET_TYPE_DATA:
1049
		return rxrpc_input_data(call, skb);
1050

1051
	case RXRPC_PACKET_TYPE_ACK:
1052
		return rxrpc_input_ack(call, skb);
1053

1054 1055 1056 1057 1058
	case RXRPC_PACKET_TYPE_BUSY:
		/* Just ignore BUSY packets from the server; the retry and
		 * lifespan timers will take care of business.  BUSY packets
		 * from the client don't make sense.
		 */
1059
		return;
1060

1061
	case RXRPC_PACKET_TYPE_ABORT:
1062
		return rxrpc_input_abort(call, skb);
1063

1064
	case RXRPC_PACKET_TYPE_ACKALL:
1065
		return rxrpc_input_ackall(call, skb);
1066

1067 1068
	default:
		break;
1069 1070 1071
	}
}

1072
/*
1073 1074
 * Handle a new service call on a channel implicitly completing the preceding
 * call on that channel.  This does not apply to client conns.
1075 1076 1077
 *
 * TODO: If callNumber > call_id + 1, renegotiate security.
 */
1078
void rxrpc_implicit_end_call(struct rxrpc_call *call, struct sk_buff *skb)
1079
{
1080
	switch (__rxrpc_call_state(call)) {
1081 1082
	case RXRPC_CALL_SERVER_AWAIT_ACK:
		rxrpc_call_completed(call);
1083
		fallthrough;
1084 1085 1086
	case RXRPC_CALL_COMPLETE:
		break;
	default:
1087 1088
		rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ESHUTDOWN,
				 rxrpc_eproto_improper_term);
1089
		trace_rxrpc_improper_term(call);
1090 1091 1092
		break;
	}

1093
	rxrpc_input_call_event(call, skb);
1094
}