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

31 32 33 34 35
	_enter("{%ld,%ld,%ld:%ld}",
	       call->ack_at - now, call->resend_at - now, call->expire_at - now,
	       call->timer.expires - now);
	
	read_lock_bh(&call->state_lock);
36

37 38 39 40 41 42 43 44 45 46
	if (call->state < RXRPC_CALL_COMPLETE) {
		t = call->ack_at;
		if (time_before(call->resend_at, t))
			t = call->resend_at;
		if (time_before(call->expire_at, t))
			t = call->expire_at;
		if (!timer_pending(&call->timer) ||
		    time_before(t, call->timer.expires)) {
			_debug("set timer %ld", t - now);
			mod_timer(&call->timer, t);
47 48 49 50 51 52
		}
	}
	read_unlock_bh(&call->state_lock);
}

/*
53
 * propose an ACK be sent
54
 */
55 56 57
static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
				u16 skew, u32 serial, bool immediate,
				bool background)
58
{
59
	unsigned long now, ack_at, expiry = rxrpc_soft_ack_delay;
60 61
	s8 prior = rxrpc_ack_priority[ack_reason];

62 63
	_enter("{%d},%s,%%%x,%u",
	       call->debug_id, rxrpc_acks(ack_reason), serial, immediate);
64

65 66 67 68 69 70 71 72 73 74
	/* 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)) {
			call->ackr_serial = serial;
			call->ackr_skew = skew;
75
		}
76 77 78 79 80 81
		if (!immediate)
			return;
	} else if (prior > rxrpc_ack_priority[call->ackr_reason]) {
		call->ackr_reason = ack_reason;
		call->ackr_serial = serial;
		call->ackr_skew = skew;
82 83
	}

84 85 86 87 88 89 90
	switch (ack_reason) {
	case RXRPC_ACK_REQUESTED:
		if (rxrpc_requested_ack_delay < expiry)
			expiry = rxrpc_requested_ack_delay;
		if (serial == 1)
			immediate = false;
		break;
91

92 93 94 95
	case RXRPC_ACK_DELAY:
		if (rxrpc_soft_ack_delay < expiry)
			expiry = rxrpc_soft_ack_delay;
		break;
96

97
	case RXRPC_ACK_IDLE:
98
		if (rxrpc_idle_ack_delay < expiry)
99 100
			expiry = rxrpc_idle_ack_delay;
		break;
101

102 103 104
	default:
		immediate = true;
		break;
105 106
	}

107 108 109 110 111 112 113 114 115 116 117 118 119 120
	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;
			rxrpc_set_timer(call);
121 122 123 124 125
		}
	}
}

/*
126
 * propose an ACK be sent, locking the call structure
127
 */
128 129
void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
		       u16 skew, u32 serial, bool immediate, bool background)
130
{
131 132 133 134
	spin_lock_bh(&call->lock);
	__rxrpc_propose_ACK(call, ack_reason, skew, serial,
			    immediate, background);
	spin_unlock_bh(&call->lock);
135 136 137
}

/*
138
 * Perform retransmission of NAK'd and unack'd packets.
139
 */
140
static void rxrpc_resend(struct rxrpc_call *call)
141
{
142
	struct rxrpc_wire_header *whdr;
143 144
	struct rxrpc_skb_priv *sp;
	struct sk_buff *skb;
145 146 147 148
	rxrpc_seq_t cursor, seq, top;
	unsigned long resend_at, now;
	int ix;
	u8 annotation;
149

150
	_enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
151 152 153

	spin_lock_bh(&call->lock);

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	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.
	 */
	now = jiffies;
	resend_at = now + rxrpc_resend_timeout;
	seq = cursor + 1;
	do {
		ix = seq & RXRPC_RXTX_BUFF_MASK;
		annotation = call->rxtx_annotations[ix];
		if (annotation == RXRPC_TX_ANNO_ACK)
			continue;
172

173
		skb = call->rxtx_buffer[ix];
174
		rxrpc_see_skb(skb);
175 176
		sp = rxrpc_skb(skb);

177 178 179 180 181
		if (annotation == RXRPC_TX_ANNO_UNACK) {
			if (time_after(sp->resend_at, now)) {
				if (time_before(sp->resend_at, resend_at))
					resend_at = sp->resend_at;
				continue;
182
			}
183 184
		}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
		/* Okay, we need to retransmit a packet. */
		call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS;
		seq++;
	} while (before_eq(seq, top));

	call->resend_at = resend_at;

	/* 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.
	 */
	seq = cursor + 1;
	do {
		ix = seq & RXRPC_RXTX_BUFF_MASK;
		annotation = call->rxtx_annotations[ix];
		if (annotation != RXRPC_TX_ANNO_RETRANS)
			continue;
203

204 205
		skb = call->rxtx_buffer[ix];
		rxrpc_get_skb(skb);
206
		spin_unlock_bh(&call->lock);
207
		sp = rxrpc_skb(skb);
208

209 210
		/* Each Tx packet needs a new serial number */
		sp->hdr.serial = atomic_inc_return(&call->conn->serial);
211

212 213
		whdr = (struct rxrpc_wire_header *)skb->head;
		whdr->serial = htonl(sp->hdr.serial);
214

215 216 217 218 219
		if (rxrpc_send_data_packet(call->conn, skb) < 0) {
			call->resend_at = now + 2;
			rxrpc_free_skb(skb);
			return;
		}
220

221 222 223
		if (rxrpc_is_client_call(call))
			rxrpc_expose_client_call(call);
		sp->resend_at = now + rxrpc_resend_timeout;
224

225
		rxrpc_free_skb(skb);
226 227
		spin_lock_bh(&call->lock);

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		/* 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).
		 */
		if (after(seq, call->tx_hard_ack) &&
		    (call->rxtx_annotations[ix] == RXRPC_TX_ANNO_RETRANS ||
		     call->rxtx_annotations[ix] == RXRPC_TX_ANNO_NAK))
			call->rxtx_annotations[ix] = RXRPC_TX_ANNO_UNACK;

		if (after(call->tx_hard_ack, seq))
			seq = call->tx_hard_ack;
		seq++;
	} while (before_eq(seq, top));

out_unlock:
	spin_unlock_bh(&call->lock);
	_leave("");
246 247 248
}

/*
249
 * Handle retransmission and deferred ACK/abort generation.
250 251 252 253 254
 */
void rxrpc_process_call(struct work_struct *work)
{
	struct rxrpc_call *call =
		container_of(work, struct rxrpc_call, processor);
255
	unsigned long now;
256

D
David Howells 已提交
257 258
	rxrpc_see_call(call);

259
	//printk("\n--------------------\n");
260 261
	_enter("{%d,%s,%lx}",
	       call->debug_id, rxrpc_call_states[call->state], call->events);
262

263 264 265 266
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;
267 268
	}

269 270 271
	if (call->state == RXRPC_CALL_COMPLETE) {
		del_timer_sync(&call->timer);
		goto out_put;
272 273
	}

274 275
	now = jiffies;
	if (time_after_eq(now, call->expire_at)) {
276
		rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
277
		set_bit(RXRPC_CALL_EV_ABORT, &call->events);
278 279
	}

280 281 282 283 284 285
	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;
286 287 288
		}
	}

289 290
	if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) ||
	    time_after_eq(now, call->resend_at)) {
291
		rxrpc_resend(call);
292
		goto recheck_state;
293 294
	}

295
	rxrpc_set_timer(call);
296 297

	/* other events may have been raised since we started checking */
298
	if (call->events && call->state < RXRPC_CALL_COMPLETE) {
299
		__rxrpc_queue_call(call);
300
		goto out;
301 302
	}

303 304 305
out_put:
	rxrpc_put_call(call, rxrpc_call_put);
out:
306 307
	_leave("");
}