1 /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/circ_buf.h>
16 #include <linux/net.h>
17 #include <linux/skbuff.h>
18 #include <linux/slab.h>
19 #include <linux/udp.h>
21 #include <net/af_rxrpc.h>
22 #include "ar-internal.h"
27 static void rxrpc_set_timer(struct rxrpc_call
*call
)
29 unsigned long t
, now
= jiffies
;
31 _enter("{%ld,%ld,%ld:%ld}",
32 call
->ack_at
- now
, call
->resend_at
- now
, call
->expire_at
- now
,
33 call
->timer
.expires
- now
);
35 read_lock_bh(&call
->state_lock
);
37 if (call
->state
< RXRPC_CALL_COMPLETE
) {
39 if (time_before(call
->resend_at
, t
))
41 if (time_before(call
->expire_at
, t
))
43 if (!timer_pending(&call
->timer
) ||
44 time_before(t
, call
->timer
.expires
)) {
45 _debug("set timer %ld", t
- now
);
46 mod_timer(&call
->timer
, t
);
49 read_unlock_bh(&call
->state_lock
);
53 * propose an ACK be sent
55 static void __rxrpc_propose_ACK(struct rxrpc_call
*call
, u8 ack_reason
,
56 u16 skew
, u32 serial
, bool immediate
,
59 unsigned long now
, ack_at
, expiry
= rxrpc_soft_ack_delay
;
60 s8 prior
= rxrpc_ack_priority
[ack_reason
];
62 _enter("{%d},%s,%%%x,%u",
63 call
->debug_id
, rxrpc_acks(ack_reason
), serial
, immediate
);
65 /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
66 * numbers, but we don't alter the timeout.
68 _debug("prior %u %u vs %u %u",
70 call
->ackr_reason
, rxrpc_ack_priority
[call
->ackr_reason
]);
71 if (ack_reason
== call
->ackr_reason
) {
72 if (RXRPC_ACK_UPDATEABLE
& (1 << ack_reason
)) {
73 call
->ackr_serial
= serial
;
74 call
->ackr_skew
= skew
;
78 } else if (prior
> rxrpc_ack_priority
[call
->ackr_reason
]) {
79 call
->ackr_reason
= ack_reason
;
80 call
->ackr_serial
= serial
;
81 call
->ackr_skew
= skew
;
85 case RXRPC_ACK_REQUESTED
:
86 if (rxrpc_requested_ack_delay
< expiry
)
87 expiry
= rxrpc_requested_ack_delay
;
93 if (rxrpc_soft_ack_delay
< expiry
)
94 expiry
= rxrpc_soft_ack_delay
;
98 if (rxrpc_idle_ack_delay
< expiry
)
99 expiry
= rxrpc_idle_ack_delay
;
108 if (test_bit(RXRPC_CALL_EV_ACK
, &call
->events
)) {
109 _debug("already scheduled");
110 } else if (immediate
|| expiry
== 0) {
111 _debug("immediate ACK %lx", call
->events
);
112 if (!test_and_set_bit(RXRPC_CALL_EV_ACK
, &call
->events
) &&
114 rxrpc_queue_call(call
);
116 ack_at
= now
+ expiry
;
117 _debug("deferred ACK %ld < %ld", expiry
, call
->ack_at
- now
);
118 if (time_before(ack_at
, call
->ack_at
)) {
119 call
->ack_at
= ack_at
;
120 rxrpc_set_timer(call
);
126 * propose an ACK be sent, locking the call structure
128 void rxrpc_propose_ACK(struct rxrpc_call
*call
, u8 ack_reason
,
129 u16 skew
, u32 serial
, bool immediate
, bool background
)
131 spin_lock_bh(&call
->lock
);
132 __rxrpc_propose_ACK(call
, ack_reason
, skew
, serial
,
133 immediate
, background
);
134 spin_unlock_bh(&call
->lock
);
138 * Perform retransmission of NAK'd and unack'd packets.
140 static void rxrpc_resend(struct rxrpc_call
*call
)
142 struct rxrpc_skb_priv
*sp
;
144 rxrpc_seq_t cursor
, seq
, top
;
145 ktime_t now
= ktime_get_real(), max_age
, oldest
, resend_at
;
147 u8 annotation
, anno_type
;
149 _enter("{%d,%d}", call
->tx_hard_ack
, call
->tx_top
);
151 max_age
= ktime_sub_ms(now
, rxrpc_resend_timeout
);
153 spin_lock_bh(&call
->lock
);
155 cursor
= call
->tx_hard_ack
;
157 ASSERT(before_eq(cursor
, top
));
161 /* Scan the packet list without dropping the lock and decide which of
162 * the packets in the Tx buffer we're going to resend and what the new
163 * resend timeout will be.
166 for (seq
= cursor
+ 1; before_eq(seq
, top
); seq
++) {
167 ix
= seq
& RXRPC_RXTX_BUFF_MASK
;
168 annotation
= call
->rxtx_annotations
[ix
];
169 anno_type
= annotation
& RXRPC_TX_ANNO_MASK
;
170 annotation
&= ~RXRPC_TX_ANNO_MASK
;
171 if (anno_type
== RXRPC_TX_ANNO_ACK
)
174 skb
= call
->rxtx_buffer
[ix
];
175 rxrpc_see_skb(skb
, rxrpc_skb_tx_seen
);
178 if (anno_type
== RXRPC_TX_ANNO_UNACK
) {
179 if (ktime_after(skb
->tstamp
, max_age
)) {
180 if (ktime_before(skb
->tstamp
, oldest
))
181 oldest
= skb
->tstamp
;
186 /* Okay, we need to retransmit a packet. */
187 call
->rxtx_annotations
[ix
] = RXRPC_TX_ANNO_RETRANS
| annotation
;
190 resend_at
= ktime_sub(ktime_add_ns(oldest
, rxrpc_resend_timeout
), now
);
191 call
->resend_at
= jiffies
+ nsecs_to_jiffies(ktime_to_ns(resend_at
));
193 /* Now go through the Tx window and perform the retransmissions. We
194 * have to drop the lock for each send. If an ACK comes in whilst the
195 * lock is dropped, it may clear some of the retransmission markers for
196 * packets that it soft-ACKs.
198 for (seq
= cursor
+ 1; before_eq(seq
, top
); seq
++) {
199 ix
= seq
& RXRPC_RXTX_BUFF_MASK
;
200 annotation
= call
->rxtx_annotations
[ix
];
201 anno_type
= annotation
& RXRPC_TX_ANNO_MASK
;
202 if (anno_type
!= RXRPC_TX_ANNO_RETRANS
)
205 skb
= call
->rxtx_buffer
[ix
];
206 rxrpc_get_skb(skb
, rxrpc_skb_tx_got
);
207 spin_unlock_bh(&call
->lock
);
209 if (rxrpc_send_data_packet(call
, skb
) < 0) {
210 rxrpc_free_skb(skb
, rxrpc_skb_tx_freed
);
214 if (rxrpc_is_client_call(call
))
215 rxrpc_expose_client_call(call
);
217 rxrpc_free_skb(skb
, rxrpc_skb_tx_freed
);
218 spin_lock_bh(&call
->lock
);
220 /* We need to clear the retransmit state, but there are two
221 * things we need to be aware of: A new ACK/NAK might have been
222 * received and the packet might have been hard-ACK'd (in which
223 * case it will no longer be in the buffer).
225 if (after(seq
, call
->tx_hard_ack
)) {
226 annotation
= call
->rxtx_annotations
[ix
];
227 anno_type
= annotation
& RXRPC_TX_ANNO_MASK
;
228 if (anno_type
== RXRPC_TX_ANNO_RETRANS
||
229 anno_type
== RXRPC_TX_ANNO_NAK
) {
230 annotation
&= ~RXRPC_TX_ANNO_MASK
;
231 annotation
|= RXRPC_TX_ANNO_UNACK
;
233 annotation
|= RXRPC_TX_ANNO_RESENT
;
234 call
->rxtx_annotations
[ix
] = annotation
;
237 if (after(call
->tx_hard_ack
, seq
))
238 seq
= call
->tx_hard_ack
;
242 spin_unlock_bh(&call
->lock
);
247 * Handle retransmission and deferred ACK/abort generation.
249 void rxrpc_process_call(struct work_struct
*work
)
251 struct rxrpc_call
*call
=
252 container_of(work
, struct rxrpc_call
, processor
);
255 rxrpc_see_call(call
);
257 //printk("\n--------------------\n");
258 _enter("{%d,%s,%lx}",
259 call
->debug_id
, rxrpc_call_states
[call
->state
], call
->events
);
262 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT
, &call
->events
)) {
263 rxrpc_send_call_packet(call
, RXRPC_PACKET_TYPE_ABORT
);
267 if (call
->state
== RXRPC_CALL_COMPLETE
) {
268 del_timer_sync(&call
->timer
);
273 if (time_after_eq(now
, call
->expire_at
)) {
274 rxrpc_abort_call("EXP", call
, 0, RX_CALL_TIMEOUT
, ETIME
);
275 set_bit(RXRPC_CALL_EV_ABORT
, &call
->events
);
278 if (test_and_clear_bit(RXRPC_CALL_EV_ACK
, &call
->events
) ||
279 time_after_eq(now
, call
->ack_at
)) {
280 call
->ack_at
= call
->expire_at
;
281 if (call
->ackr_reason
) {
282 rxrpc_send_call_packet(call
, RXRPC_PACKET_TYPE_ACK
);
287 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND
, &call
->events
) ||
288 time_after_eq(now
, call
->resend_at
)) {
293 rxrpc_set_timer(call
);
295 /* other events may have been raised since we started checking */
296 if (call
->events
&& call
->state
< RXRPC_CALL_COMPLETE
) {
297 __rxrpc_queue_call(call
);
302 rxrpc_put_call(call
, rxrpc_call_put
);