call_event.c 11.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14 15 16 17
#include <linux/module.h>
#include <linux/circ_buf.h>
#include <linux/net.h>
#include <linux/skbuff.h>
18
#include <linux/slab.h>
19 20 21 22 23 24
#include <linux/udp.h>
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include "ar-internal.h"

/*
25
 * Set the timer
26
 */
27 28
void __rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why,
		       ktime_t now)
29
{
30 31
	unsigned long t_j, now_j = jiffies;
	ktime_t t;
32
	bool queue = false;
33

34
	if (call->state < RXRPC_CALL_COMPLETE) {
D
David Howells 已提交
35
		t = call->expire_at;
D
David Howells 已提交
36 37 38
		if (!ktime_after(t, now)) {
			trace_rxrpc_timer(call, why, now, now_j);
			queue = true;
D
David Howells 已提交
39
			goto out;
D
David Howells 已提交
40
		}
D
David Howells 已提交
41

42 43 44 45 46
		if (!ktime_after(call->resend_at, now)) {
			call->resend_at = call->expire_at;
			if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
				queue = true;
		} else if (ktime_before(call->resend_at, t)) {
47
			t = call->resend_at;
48
		}
D
David Howells 已提交
49

50 51 52 53 54
		if (!ktime_after(call->ack_at, now)) {
			call->ack_at = call->expire_at;
			if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
				queue = true;
		} else if (ktime_before(call->ack_at, t)) {
D
David Howells 已提交
55
			t = call->ack_at;
56
		}
D
David Howells 已提交
57

58 59 60 61 62 63 64 65
		if (!ktime_after(call->ping_at, now)) {
			call->ping_at = call->expire_at;
			if (!test_and_set_bit(RXRPC_CALL_EV_PING, &call->events))
				queue = true;
		} else if (ktime_before(call->ping_at, t)) {
			t = call->ping_at;
		}

66 67 68 69 70 71 72 73 74 75 76 77 78
		t_j = nsecs_to_jiffies(ktime_to_ns(ktime_sub(t, now)));
		t_j += jiffies;

		/* We have to make sure that the calculated jiffies value falls
		 * at or after the nsec value, or we may loop ceaselessly
		 * because the timer times out, but we haven't reached the nsec
		 * timeout yet.
		 */
		t_j++;

		if (call->timer.expires != t_j || !timer_pending(&call->timer)) {
			mod_timer(&call->timer, t_j);
			trace_rxrpc_timer(call, why, now, now_j);
79 80
		}
	}
D
David Howells 已提交
81 82

out:
D
David Howells 已提交
83 84
	if (queue)
		rxrpc_queue_call(call);
85 86 87 88 89 90 91 92 93 94
}

/*
 * Set the timer
 */
void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why,
		     ktime_t now)
{
	read_lock_bh(&call->state_lock);
	__rxrpc_set_timer(call, why, now);
95 96 97
	read_unlock_bh(&call->state_lock);
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*
 * Propose a PING ACK be sent.
 */
static void rxrpc_propose_ping(struct rxrpc_call *call,
			       bool immediate, bool background)
{
	if (immediate) {
		if (background &&
		    !test_and_set_bit(RXRPC_CALL_EV_PING, &call->events))
			rxrpc_queue_call(call);
	} else {
		ktime_t now = ktime_get_real();
		ktime_t ping_at = ktime_add_ms(now, rxrpc_idle_ack_delay);

		if (ktime_before(ping_at, call->ping_at)) {
			call->ping_at = ping_at;
			rxrpc_set_timer(call, rxrpc_timer_set_for_ping, now);
		}
	}
}

119
/*
120
 * propose an ACK be sent
121
 */
122 123
static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
				u16 skew, u32 serial, bool immediate,
124 125
				bool background,
				enum rxrpc_propose_ack_trace why)
126
{
127
	enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
128 129
	unsigned int expiry = rxrpc_soft_ack_delay;
	ktime_t now, ack_at;
130 131
	s8 prior = rxrpc_ack_priority[ack_reason];

132 133 134 135 136 137 138 139
	/* Pings are handled specially because we don't want to accidentally
	 * lose a ping response by subsuming it into a ping.
	 */
	if (ack_reason == RXRPC_ACK_PING) {
		rxrpc_propose_ping(call, immediate, background);
		goto trace;
	}

140 141 142 143 144 145 146 147
	/* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
	 * numbers, but we don't alter the timeout.
	 */
	_debug("prior %u %u vs %u %u",
	       ack_reason, prior,
	       call->ackr_reason, rxrpc_ack_priority[call->ackr_reason]);
	if (ack_reason == call->ackr_reason) {
		if (RXRPC_ACK_UPDATEABLE & (1 << ack_reason)) {
148
			outcome = rxrpc_propose_ack_update;
149 150
			call->ackr_serial = serial;
			call->ackr_skew = skew;
151
		}
152
		if (!immediate)
153
			goto trace;
154 155 156 157
	} else if (prior > rxrpc_ack_priority[call->ackr_reason]) {
		call->ackr_reason = ack_reason;
		call->ackr_serial = serial;
		call->ackr_skew = skew;
158 159
	} else {
		outcome = rxrpc_propose_ack_subsume;
160 161
	}

162 163 164 165 166 167 168
	switch (ack_reason) {
	case RXRPC_ACK_REQUESTED:
		if (rxrpc_requested_ack_delay < expiry)
			expiry = rxrpc_requested_ack_delay;
		if (serial == 1)
			immediate = false;
		break;
169

170 171 172 173
	case RXRPC_ACK_DELAY:
		if (rxrpc_soft_ack_delay < expiry)
			expiry = rxrpc_soft_ack_delay;
		break;
174

175
	case RXRPC_ACK_IDLE:
176
		if (rxrpc_idle_ack_delay < expiry)
177 178
			expiry = rxrpc_idle_ack_delay;
		break;
179

180 181 182
	default:
		immediate = true;
		break;
183 184
	}

185 186 187 188 189 190 191 192
	if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
		_debug("already scheduled");
	} else if (immediate || expiry == 0) {
		_debug("immediate ACK %lx", call->events);
		if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events) &&
		    background)
			rxrpc_queue_call(call);
	} else {
193 194 195
		now = ktime_get_real();
		ack_at = ktime_add_ms(now, expiry);
		if (ktime_before(ack_at, call->ack_at)) {
196
			call->ack_at = ack_at;
197
			rxrpc_set_timer(call, rxrpc_timer_set_for_ack, now);
198 199
		}
	}
200 201 202 203

trace:
	trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
				background, outcome);
204 205 206
}

/*
207
 * propose an ACK be sent, locking the call structure
208
 */
209
void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
210 211
		       u16 skew, u32 serial, bool immediate, bool background,
		       enum rxrpc_propose_ack_trace why)
212
{
213 214
	spin_lock_bh(&call->lock);
	__rxrpc_propose_ACK(call, ack_reason, skew, serial,
215
			    immediate, background, why);
216
	spin_unlock_bh(&call->lock);
217 218
}

D
David Howells 已提交
219 220 221 222 223 224 225 226
/*
 * Handle congestion being detected by the retransmit timeout.
 */
static void rxrpc_congestion_timeout(struct rxrpc_call *call)
{
	set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
}

227
/*
228
 * Perform retransmission of NAK'd and unack'd packets.
229
 */
230
static void rxrpc_resend(struct rxrpc_call *call, ktime_t now)
231 232 233
{
	struct rxrpc_skb_priv *sp;
	struct sk_buff *skb;
234
	rxrpc_seq_t cursor, seq, top;
235
	ktime_t max_age, oldest, ack_ts;
236
	int ix;
D
David Howells 已提交
237
	u8 annotation, anno_type, retrans = 0, unacked = 0;
238

239
	_enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
240

241 242
	max_age = ktime_sub_ms(now, rxrpc_resend_timeout);

243 244
	spin_lock_bh(&call->lock);

245 246 247 248 249 250 251 252 253 254
	cursor = call->tx_hard_ack;
	top = call->tx_top;
	ASSERT(before_eq(cursor, top));
	if (cursor == top)
		goto out_unlock;

	/* Scan the packet list without dropping the lock and decide which of
	 * the packets in the Tx buffer we're going to resend and what the new
	 * resend timeout will be.
	 */
255
	oldest = now;
256
	for (seq = cursor + 1; before_eq(seq, top); seq++) {
257 258
		ix = seq & RXRPC_RXTX_BUFF_MASK;
		annotation = call->rxtx_annotations[ix];
D
David Howells 已提交
259 260 261
		anno_type = annotation & RXRPC_TX_ANNO_MASK;
		annotation &= ~RXRPC_TX_ANNO_MASK;
		if (anno_type == RXRPC_TX_ANNO_ACK)
262
			continue;
263

264
		skb = call->rxtx_buffer[ix];
D
David Howells 已提交
265
		rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
266 267
		sp = rxrpc_skb(skb);

D
David Howells 已提交
268
		if (anno_type == RXRPC_TX_ANNO_UNACK) {
269 270 271
			if (ktime_after(skb->tstamp, max_age)) {
				if (ktime_before(skb->tstamp, oldest))
					oldest = skb->tstamp;
272
				continue;
273
			}
D
David Howells 已提交
274 275
			if (!(annotation & RXRPC_TX_ANNO_RESENT))
				unacked++;
276 277
		}

278
		/* Okay, we need to retransmit a packet. */
D
David Howells 已提交
279
		call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
D
David Howells 已提交
280
		retrans++;
281 282
		trace_rxrpc_retransmit(call, seq, annotation | anno_type,
				       ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
283
	}
284

285
	call->resend_at = ktime_add_ms(oldest, rxrpc_resend_timeout);
286

D
David Howells 已提交
287 288 289 290 291 292 293 294
	if (unacked)
		rxrpc_congestion_timeout(call);

	/* If there was nothing that needed retransmission then it's likely
	 * that an ACK got lost somewhere.  Send a ping to find out instead of
	 * retransmitting data.
	 */
	if (!retrans) {
295
		rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
D
David Howells 已提交
296 297 298 299 300 301
		spin_unlock_bh(&call->lock);
		ack_ts = ktime_sub(now, call->acks_latest_ts);
		if (ktime_to_ns(ack_ts) < call->peer->rtt)
			goto out;
		rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, false,
				  rxrpc_propose_ack_ping_for_lost_ack);
302
		rxrpc_send_ack_packet(call, true);
D
David Howells 已提交
303 304 305
		goto out;
	}

306 307 308 309 310
	/* Now go through the Tx window and perform the retransmissions.  We
	 * have to drop the lock for each send.  If an ACK comes in whilst the
	 * lock is dropped, it may clear some of the retransmission markers for
	 * packets that it soft-ACKs.
	 */
311
	for (seq = cursor + 1; before_eq(seq, top); seq++) {
312 313
		ix = seq & RXRPC_RXTX_BUFF_MASK;
		annotation = call->rxtx_annotations[ix];
D
David Howells 已提交
314 315
		anno_type = annotation & RXRPC_TX_ANNO_MASK;
		if (anno_type != RXRPC_TX_ANNO_RETRANS)
316
			continue;
317

318
		skb = call->rxtx_buffer[ix];
D
David Howells 已提交
319
		rxrpc_get_skb(skb, rxrpc_skb_tx_got);
320 321
		spin_unlock_bh(&call->lock);

322
		if (rxrpc_send_data_packet(call, skb, true) < 0) {
D
David Howells 已提交
323
			rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
324 325
			return;
		}
326

327 328
		if (rxrpc_is_client_call(call))
			rxrpc_expose_client_call(call);
329

D
David Howells 已提交
330
		rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
331 332
		spin_lock_bh(&call->lock);

333 334 335 336 337
		/* We need to clear the retransmit state, but there are two
		 * things we need to be aware of: A new ACK/NAK might have been
		 * received and the packet might have been hard-ACK'd (in which
		 * case it will no longer be in the buffer).
		 */
D
David Howells 已提交
338 339 340 341 342 343 344 345 346 347 348
		if (after(seq, call->tx_hard_ack)) {
			annotation = call->rxtx_annotations[ix];
			anno_type = annotation & RXRPC_TX_ANNO_MASK;
			if (anno_type == RXRPC_TX_ANNO_RETRANS ||
			    anno_type == RXRPC_TX_ANNO_NAK) {
				annotation &= ~RXRPC_TX_ANNO_MASK;
				annotation |= RXRPC_TX_ANNO_UNACK;
			}
			annotation |= RXRPC_TX_ANNO_RESENT;
			call->rxtx_annotations[ix] = annotation;
		}
349 350 351

		if (after(call->tx_hard_ack, seq))
			seq = call->tx_hard_ack;
352
	}
353 354 355

out_unlock:
	spin_unlock_bh(&call->lock);
D
David Howells 已提交
356
out:
357
	_leave("");
358 359 360
}

/*
361
 * Handle retransmission and deferred ACK/abort generation.
362 363 364 365 366
 */
void rxrpc_process_call(struct work_struct *work)
{
	struct rxrpc_call *call =
		container_of(work, struct rxrpc_call, processor);
367
	ktime_t now;
368

D
David Howells 已提交
369 370
	rxrpc_see_call(call);

371
	//printk("\n--------------------\n");
372 373
	_enter("{%d,%s,%lx}",
	       call->debug_id, rxrpc_call_states[call->state], call->events);
374

375 376
recheck_state:
	if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
377
		rxrpc_send_abort_packet(call);
378
		goto recheck_state;
379 380
	}

381 382
	if (call->state == RXRPC_CALL_COMPLETE) {
		del_timer_sync(&call->timer);
D
David Howells 已提交
383
		rxrpc_notify_socket(call);
384
		goto out_put;
385 386
	}

387 388
	now = ktime_get_real();
	if (ktime_before(call->expire_at, now)) {
D
David Howells 已提交
389
		rxrpc_abort_call("EXP", call, 0, RX_USER_ABORT, -ETIME);
390
		set_bit(RXRPC_CALL_EV_ABORT, &call->events);
D
David Howells 已提交
391
		goto recheck_state;
392 393
	}

394
	if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events)) {
395
		if (call->ackr_reason) {
396
			rxrpc_send_ack_packet(call, false);
397
			goto recheck_state;
398 399 400
		}
	}

401 402 403 404 405
	if (test_and_clear_bit(RXRPC_CALL_EV_PING, &call->events)) {
		rxrpc_send_ack_packet(call, true);
		goto recheck_state;
	}

406
	if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events)) {
407
		rxrpc_resend(call, now);
408
		goto recheck_state;
409 410
	}

411
	rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
412 413

	/* other events may have been raised since we started checking */
414
	if (call->events && call->state < RXRPC_CALL_COMPLETE) {
415
		__rxrpc_queue_call(call);
416
		goto out;
417 418
	}

419 420 421
out_put:
	rxrpc_put_call(call, rxrpc_call_put);
out:
422 423
	_leave("");
}