1 /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception 2 * 3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. 4 * Written by David Howells (dhowells@redhat.com) 5 * 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. 10 */ 11 12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 13 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> 20 #include <net/sock.h> 21 #include <net/af_rxrpc.h> 22 #include "ar-internal.h" 23 24 /* 25 * Set the timer 26 */ 27 void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why, 28 ktime_t now) 29 { 30 unsigned long t_j, now_j = jiffies; 31 ktime_t t; 32 bool queue = false; 33 34 read_lock_bh(&call->state_lock); 35 36 if (call->state < RXRPC_CALL_COMPLETE) { 37 t = call->expire_at; 38 if (!ktime_after(t, now)) 39 goto out; 40 41 if (!ktime_after(call->resend_at, now)) { 42 call->resend_at = call->expire_at; 43 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events)) 44 queue = true; 45 } else if (ktime_before(call->resend_at, t)) { 46 t = call->resend_at; 47 } 48 49 if (!ktime_after(call->ack_at, now)) { 50 call->ack_at = call->expire_at; 51 if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events)) 52 queue = true; 53 } else if (ktime_before(call->ack_at, t)) { 54 t = call->ack_at; 55 } 56 57 t_j = nsecs_to_jiffies(ktime_to_ns(ktime_sub(t, now))); 58 t_j += jiffies; 59 60 /* We have to make sure that the calculated jiffies value falls 61 * at or after the nsec value, or we may loop ceaselessly 62 * because the timer times out, but we haven't reached the nsec 63 * timeout yet. 64 */ 65 t_j++; 66 67 if (call->timer.expires != t_j || !timer_pending(&call->timer)) { 68 mod_timer(&call->timer, t_j); 69 trace_rxrpc_timer(call, why, now, now_j); 70 } 71 72 if (queue) 73 rxrpc_queue_call(call); 74 } 75 76 out: 77 read_unlock_bh(&call->state_lock); 78 } 79 80 /* 81 * propose an ACK be sent 82 */ 83 static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason, 84 u16 skew, u32 serial, bool immediate, 85 bool background, 86 enum rxrpc_propose_ack_trace why) 87 { 88 enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use; 89 unsigned int expiry = rxrpc_soft_ack_delay; 90 ktime_t now, ack_at; 91 s8 prior = rxrpc_ack_priority[ack_reason]; 92 93 /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial 94 * numbers, but we don't alter the timeout. 95 */ 96 _debug("prior %u %u vs %u %u", 97 ack_reason, prior, 98 call->ackr_reason, rxrpc_ack_priority[call->ackr_reason]); 99 if (ack_reason == call->ackr_reason) { 100 if (RXRPC_ACK_UPDATEABLE & (1 << ack_reason)) { 101 outcome = rxrpc_propose_ack_update; 102 call->ackr_serial = serial; 103 call->ackr_skew = skew; 104 } 105 if (!immediate) 106 goto trace; 107 } else if (prior > rxrpc_ack_priority[call->ackr_reason]) { 108 call->ackr_reason = ack_reason; 109 call->ackr_serial = serial; 110 call->ackr_skew = skew; 111 } else { 112 outcome = rxrpc_propose_ack_subsume; 113 } 114 115 switch (ack_reason) { 116 case RXRPC_ACK_REQUESTED: 117 if (rxrpc_requested_ack_delay < expiry) 118 expiry = rxrpc_requested_ack_delay; 119 if (serial == 1) 120 immediate = false; 121 break; 122 123 case RXRPC_ACK_DELAY: 124 if (rxrpc_soft_ack_delay < expiry) 125 expiry = rxrpc_soft_ack_delay; 126 break; 127 128 case RXRPC_ACK_PING: 129 case RXRPC_ACK_IDLE: 130 if (rxrpc_idle_ack_delay < expiry) 131 expiry = rxrpc_idle_ack_delay; 132 break; 133 134 default: 135 immediate = true; 136 break; 137 } 138 139 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) { 140 _debug("already scheduled"); 141 } else if (immediate || expiry == 0) { 142 _debug("immediate ACK %lx", call->events); 143 if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events) && 144 background) 145 rxrpc_queue_call(call); 146 } else { 147 now = ktime_get_real(); 148 ack_at = ktime_add_ms(now, expiry); 149 if (ktime_before(ack_at, call->ack_at)) { 150 call->ack_at = ack_at; 151 rxrpc_set_timer(call, rxrpc_timer_set_for_ack, now); 152 } 153 } 154 155 trace: 156 trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate, 157 background, outcome); 158 } 159 160 /* 161 * propose an ACK be sent, locking the call structure 162 */ 163 void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason, 164 u16 skew, u32 serial, bool immediate, bool background, 165 enum rxrpc_propose_ack_trace why) 166 { 167 spin_lock_bh(&call->lock); 168 __rxrpc_propose_ACK(call, ack_reason, skew, serial, 169 immediate, background, why); 170 spin_unlock_bh(&call->lock); 171 } 172 173 /* 174 * Handle congestion being detected by the retransmit timeout. 175 */ 176 static void rxrpc_congestion_timeout(struct rxrpc_call *call) 177 { 178 set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags); 179 } 180 181 /* 182 * Perform retransmission of NAK'd and unack'd packets. 183 */ 184 static void rxrpc_resend(struct rxrpc_call *call, ktime_t now) 185 { 186 struct rxrpc_skb_priv *sp; 187 struct sk_buff *skb; 188 rxrpc_seq_t cursor, seq, top; 189 ktime_t max_age, oldest, ack_ts; 190 int ix; 191 u8 annotation, anno_type, retrans = 0, unacked = 0; 192 193 _enter("{%d,%d}", call->tx_hard_ack, call->tx_top); 194 195 max_age = ktime_sub_ms(now, rxrpc_resend_timeout); 196 197 spin_lock_bh(&call->lock); 198 199 cursor = call->tx_hard_ack; 200 top = call->tx_top; 201 ASSERT(before_eq(cursor, top)); 202 if (cursor == top) 203 goto out_unlock; 204 205 /* Scan the packet list without dropping the lock and decide which of 206 * the packets in the Tx buffer we're going to resend and what the new 207 * resend timeout will be. 208 */ 209 oldest = now; 210 for (seq = cursor + 1; before_eq(seq, top); seq++) { 211 ix = seq & RXRPC_RXTX_BUFF_MASK; 212 annotation = call->rxtx_annotations[ix]; 213 anno_type = annotation & RXRPC_TX_ANNO_MASK; 214 annotation &= ~RXRPC_TX_ANNO_MASK; 215 if (anno_type == RXRPC_TX_ANNO_ACK) 216 continue; 217 218 skb = call->rxtx_buffer[ix]; 219 rxrpc_see_skb(skb, rxrpc_skb_tx_seen); 220 sp = rxrpc_skb(skb); 221 222 if (anno_type == RXRPC_TX_ANNO_UNACK) { 223 if (ktime_after(skb->tstamp, max_age)) { 224 if (ktime_before(skb->tstamp, oldest)) 225 oldest = skb->tstamp; 226 continue; 227 } 228 if (!(annotation & RXRPC_TX_ANNO_RESENT)) 229 unacked++; 230 } 231 232 /* Okay, we need to retransmit a packet. */ 233 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation; 234 retrans++; 235 trace_rxrpc_retransmit(call, seq, annotation | anno_type, 236 ktime_to_ns(ktime_sub(skb->tstamp, max_age))); 237 } 238 239 call->resend_at = ktime_add_ms(oldest, rxrpc_resend_timeout); 240 241 if (unacked) 242 rxrpc_congestion_timeout(call); 243 244 /* If there was nothing that needed retransmission then it's likely 245 * that an ACK got lost somewhere. Send a ping to find out instead of 246 * retransmitting data. 247 */ 248 if (!retrans) { 249 rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now); 250 spin_unlock_bh(&call->lock); 251 ack_ts = ktime_sub(now, call->acks_latest_ts); 252 if (ktime_to_ns(ack_ts) < call->peer->rtt) 253 goto out; 254 rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, false, 255 rxrpc_propose_ack_ping_for_lost_ack); 256 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK); 257 goto out; 258 } 259 260 /* Now go through the Tx window and perform the retransmissions. We 261 * have to drop the lock for each send. If an ACK comes in whilst the 262 * lock is dropped, it may clear some of the retransmission markers for 263 * packets that it soft-ACKs. 264 */ 265 for (seq = cursor + 1; before_eq(seq, top); seq++) { 266 ix = seq & RXRPC_RXTX_BUFF_MASK; 267 annotation = call->rxtx_annotations[ix]; 268 anno_type = annotation & RXRPC_TX_ANNO_MASK; 269 if (anno_type != RXRPC_TX_ANNO_RETRANS) 270 continue; 271 272 skb = call->rxtx_buffer[ix]; 273 rxrpc_get_skb(skb, rxrpc_skb_tx_got); 274 spin_unlock_bh(&call->lock); 275 276 if (rxrpc_send_data_packet(call, skb, true) < 0) { 277 rxrpc_free_skb(skb, rxrpc_skb_tx_freed); 278 return; 279 } 280 281 if (rxrpc_is_client_call(call)) 282 rxrpc_expose_client_call(call); 283 284 rxrpc_free_skb(skb, rxrpc_skb_tx_freed); 285 spin_lock_bh(&call->lock); 286 287 /* We need to clear the retransmit state, but there are two 288 * things we need to be aware of: A new ACK/NAK might have been 289 * received and the packet might have been hard-ACK'd (in which 290 * case it will no longer be in the buffer). 291 */ 292 if (after(seq, call->tx_hard_ack)) { 293 annotation = call->rxtx_annotations[ix]; 294 anno_type = annotation & RXRPC_TX_ANNO_MASK; 295 if (anno_type == RXRPC_TX_ANNO_RETRANS || 296 anno_type == RXRPC_TX_ANNO_NAK) { 297 annotation &= ~RXRPC_TX_ANNO_MASK; 298 annotation |= RXRPC_TX_ANNO_UNACK; 299 } 300 annotation |= RXRPC_TX_ANNO_RESENT; 301 call->rxtx_annotations[ix] = annotation; 302 } 303 304 if (after(call->tx_hard_ack, seq)) 305 seq = call->tx_hard_ack; 306 } 307 308 out_unlock: 309 spin_unlock_bh(&call->lock); 310 out: 311 _leave(""); 312 } 313 314 /* 315 * Handle retransmission and deferred ACK/abort generation. 316 */ 317 void rxrpc_process_call(struct work_struct *work) 318 { 319 struct rxrpc_call *call = 320 container_of(work, struct rxrpc_call, processor); 321 ktime_t now; 322 323 rxrpc_see_call(call); 324 325 //printk("\n--------------------\n"); 326 _enter("{%d,%s,%lx}", 327 call->debug_id, rxrpc_call_states[call->state], call->events); 328 329 recheck_state: 330 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) { 331 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT); 332 goto recheck_state; 333 } 334 335 if (call->state == RXRPC_CALL_COMPLETE) { 336 del_timer_sync(&call->timer); 337 goto out_put; 338 } 339 340 now = ktime_get_real(); 341 if (ktime_before(call->expire_at, now)) { 342 rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME); 343 set_bit(RXRPC_CALL_EV_ABORT, &call->events); 344 goto recheck_state; 345 } 346 347 if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events)) { 348 call->ack_at = call->expire_at; 349 if (call->ackr_reason) { 350 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK); 351 goto recheck_state; 352 } 353 } 354 355 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events)) { 356 rxrpc_resend(call, now); 357 goto recheck_state; 358 } 359 360 rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now); 361 362 /* other events may have been raised since we started checking */ 363 if (call->events && call->state < RXRPC_CALL_COMPLETE) { 364 __rxrpc_queue_call(call); 365 goto out; 366 } 367 368 out_put: 369 rxrpc_put_call(call, rxrpc_call_put); 370 out: 371 _leave(""); 372 } 373