call_event.c 8.7 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
void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why)
28
{
29
	unsigned long t, now = jiffies;
30

31
	read_lock_bh(&call->state_lock);
32

33
	if (call->state < RXRPC_CALL_COMPLETE) {
D
David Howells 已提交
34 35 36 37 38 39
		t = call->expire_at;
		if (time_before_eq(t, now))
			goto out;

		if (time_after(call->resend_at, now) &&
		    time_before(call->resend_at, t))
40
			t = call->resend_at;
D
David Howells 已提交
41 42 43 44 45 46

		if (time_after(call->ack_at, now) &&
		    time_before(call->ack_at, t))
			t = call->ack_at;

		if (call->timer.expires != t || !timer_pending(&call->timer)) {
47
			mod_timer(&call->timer, t);
48
			trace_rxrpc_timer(call, why, now);
49 50
		}
	}
D
David Howells 已提交
51 52

out:
53 54 55 56
	read_unlock_bh(&call->state_lock);
}

/*
57
 * propose an ACK be sent
58
 */
59 60
static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
				u16 skew, u32 serial, bool immediate,
61 62
				bool background,
				enum rxrpc_propose_ack_trace why)
63
{
64
	enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
65
	unsigned long now, ack_at, expiry = rxrpc_soft_ack_delay;
66 67
	s8 prior = rxrpc_ack_priority[ack_reason];

68 69 70 71 72 73 74 75
	/* 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)) {
76
			outcome = rxrpc_propose_ack_update;
77 78
			call->ackr_serial = serial;
			call->ackr_skew = skew;
79
		}
80
		if (!immediate)
81
			goto trace;
82 83 84 85
	} else if (prior > rxrpc_ack_priority[call->ackr_reason]) {
		call->ackr_reason = ack_reason;
		call->ackr_serial = serial;
		call->ackr_skew = skew;
86 87
	} else {
		outcome = rxrpc_propose_ack_subsume;
88 89
	}

90 91 92 93 94 95 96
	switch (ack_reason) {
	case RXRPC_ACK_REQUESTED:
		if (rxrpc_requested_ack_delay < expiry)
			expiry = rxrpc_requested_ack_delay;
		if (serial == 1)
			immediate = false;
		break;
97

98 99 100 101
	case RXRPC_ACK_DELAY:
		if (rxrpc_soft_ack_delay < expiry)
			expiry = rxrpc_soft_ack_delay;
		break;
102

103
	case RXRPC_ACK_PING:
104
	case RXRPC_ACK_IDLE:
105
		if (rxrpc_idle_ack_delay < expiry)
106 107
			expiry = rxrpc_idle_ack_delay;
		break;
108

109 110 111
	default:
		immediate = true;
		break;
112 113
	}

114 115 116 117 118 119 120 121 122 123 124 125 126
	now = jiffies;
	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 {
		ack_at = now + expiry;
		_debug("deferred ACK %ld < %ld", expiry, call->ack_at - now);
		if (time_before(ack_at, call->ack_at)) {
			call->ack_at = ack_at;
127
			rxrpc_set_timer(call, rxrpc_timer_set_for_ack);
128 129
		}
	}
130 131 132 133

trace:
	trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
				background, outcome);
134 135 136
}

/*
137
 * propose an ACK be sent, locking the call structure
138
 */
139
void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
140 141
		       u16 skew, u32 serial, bool immediate, bool background,
		       enum rxrpc_propose_ack_trace why)
142
{
143 144
	spin_lock_bh(&call->lock);
	__rxrpc_propose_ACK(call, ack_reason, skew, serial,
145
			    immediate, background, why);
146
	spin_unlock_bh(&call->lock);
147 148 149
}

/*
150
 * Perform retransmission of NAK'd and unack'd packets.
151
 */
152
static void rxrpc_resend(struct rxrpc_call *call)
153 154 155
{
	struct rxrpc_skb_priv *sp;
	struct sk_buff *skb;
156
	rxrpc_seq_t cursor, seq, top;
157
	ktime_t now = ktime_get_real(), max_age, oldest,  resend_at;
158
	int ix;
D
David Howells 已提交
159
	u8 annotation, anno_type;
160

161
	_enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
162

163 164
	max_age = ktime_sub_ms(now, rxrpc_resend_timeout);

165 166
	spin_lock_bh(&call->lock);

167 168 169 170 171 172 173 174 175 176
	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.
	 */
177
	oldest = now;
178
	for (seq = cursor + 1; before_eq(seq, top); seq++) {
179 180
		ix = seq & RXRPC_RXTX_BUFF_MASK;
		annotation = call->rxtx_annotations[ix];
D
David Howells 已提交
181 182 183
		anno_type = annotation & RXRPC_TX_ANNO_MASK;
		annotation &= ~RXRPC_TX_ANNO_MASK;
		if (anno_type == RXRPC_TX_ANNO_ACK)
184
			continue;
185

186
		skb = call->rxtx_buffer[ix];
D
David Howells 已提交
187
		rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
188 189
		sp = rxrpc_skb(skb);

D
David Howells 已提交
190
		if (anno_type == RXRPC_TX_ANNO_UNACK) {
191 192 193
			if (ktime_after(skb->tstamp, max_age)) {
				if (ktime_before(skb->tstamp, oldest))
					oldest = skb->tstamp;
194
				continue;
195
			}
196 197
		}

198
		/* Okay, we need to retransmit a packet. */
D
David Howells 已提交
199
		call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
200 201
		trace_rxrpc_retransmit(call, seq, annotation | anno_type,
				       ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
202
	}
203

204 205 206 207 208 209 210 211
	resend_at = ktime_add_ms(oldest, rxrpc_resend_timeout);
	call->resend_at = jiffies +
		nsecs_to_jiffies(ktime_to_ns(ktime_sub(resend_at, now))) +
		1; /* We have to make sure that the calculated jiffies value
		    * falls at or after the nsec value, or we shall loop
		    * ceaselessly because the timer times out, but we haven't
		    * reached the nsec timeout yet.
		    */
212 213 214 215 216 217

	/* 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.
	 */
218
	for (seq = cursor + 1; before_eq(seq, top); seq++) {
219 220
		ix = seq & RXRPC_RXTX_BUFF_MASK;
		annotation = call->rxtx_annotations[ix];
D
David Howells 已提交
221 222
		anno_type = annotation & RXRPC_TX_ANNO_MASK;
		if (anno_type != RXRPC_TX_ANNO_RETRANS)
223
			continue;
224

225
		skb = call->rxtx_buffer[ix];
D
David Howells 已提交
226
		rxrpc_get_skb(skb, rxrpc_skb_tx_got);
227 228
		spin_unlock_bh(&call->lock);

229
		if (rxrpc_send_data_packet(call, skb) < 0) {
D
David Howells 已提交
230
			rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
231 232
			return;
		}
233

234 235
		if (rxrpc_is_client_call(call))
			rxrpc_expose_client_call(call);
236

D
David Howells 已提交
237
		rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
238 239
		spin_lock_bh(&call->lock);

240 241 242 243 244
		/* 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 已提交
245 246 247 248 249 250 251 252 253 254 255
		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;
		}
256 257 258

		if (after(call->tx_hard_ack, seq))
			seq = call->tx_hard_ack;
259
	}
260 261 262 263

out_unlock:
	spin_unlock_bh(&call->lock);
	_leave("");
264 265 266
}

/*
267
 * Handle retransmission and deferred ACK/abort generation.
268 269 270 271 272
 */
void rxrpc_process_call(struct work_struct *work)
{
	struct rxrpc_call *call =
		container_of(work, struct rxrpc_call, processor);
273
	unsigned long now;
274

D
David Howells 已提交
275 276
	rxrpc_see_call(call);

277
	//printk("\n--------------------\n");
278 279
	_enter("{%d,%s,%lx}",
	       call->debug_id, rxrpc_call_states[call->state], call->events);
280

281 282 283 284
recheck_state:
	if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
		rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
		goto recheck_state;
285 286
	}

287 288 289
	if (call->state == RXRPC_CALL_COMPLETE) {
		del_timer_sync(&call->timer);
		goto out_put;
290 291
	}

292 293
	now = jiffies;
	if (time_after_eq(now, call->expire_at)) {
294
		rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
295
		set_bit(RXRPC_CALL_EV_ABORT, &call->events);
296 297
	}

298 299 300 301 302 303
	if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events) ||
	    time_after_eq(now, call->ack_at)) {
		call->ack_at = call->expire_at;
		if (call->ackr_reason) {
			rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
			goto recheck_state;
304 305 306
		}
	}

307 308
	if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) ||
	    time_after_eq(now, call->resend_at)) {
309
		rxrpc_resend(call);
310
		goto recheck_state;
311 312
	}

313
	rxrpc_set_timer(call, rxrpc_timer_set_for_resend);
314 315

	/* other events may have been raised since we started checking */
316
	if (call->events && call->state < RXRPC_CALL_COMPLETE) {
317
		__rxrpc_queue_call(call);
318
		goto out;
319 320
	}

321 322 323
out_put:
	rxrpc_put_call(call, rxrpc_call_put);
out:
324 325
	_leave("");
}