David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 | #include <linux/module.h> |
| 13 | #include <linux/circ_buf.h> |
| 14 | #include <linux/net.h> |
| 15 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 17 | #include <linux/udp.h> |
| 18 | #include <net/sock.h> |
| 19 | #include <net/af_rxrpc.h> |
| 20 | #include "ar-internal.h" |
| 21 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 22 | static unsigned int rxrpc_ack_defer = 1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 23 | |
Dan Carpenter | 08d4d21 | 2014-01-20 13:28:59 +0300 | [diff] [blame] | 24 | static const char *rxrpc_acks(u8 reason) |
| 25 | { |
| 26 | static const char *const str[] = { |
| 27 | "---", "REQ", "DUP", "OOS", "WIN", "MEM", "PNG", "PNR", "DLY", |
| 28 | "IDL", "-?-" |
| 29 | }; |
| 30 | |
| 31 | if (reason >= ARRAY_SIZE(str)) |
| 32 | reason = ARRAY_SIZE(str) - 1; |
| 33 | return str[reason]; |
| 34 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 35 | |
| 36 | static const s8 rxrpc_ack_priority[] = { |
| 37 | [0] = 0, |
| 38 | [RXRPC_ACK_DELAY] = 1, |
| 39 | [RXRPC_ACK_REQUESTED] = 2, |
| 40 | [RXRPC_ACK_IDLE] = 3, |
| 41 | [RXRPC_ACK_PING_RESPONSE] = 4, |
| 42 | [RXRPC_ACK_DUPLICATE] = 5, |
| 43 | [RXRPC_ACK_OUT_OF_SEQUENCE] = 6, |
| 44 | [RXRPC_ACK_EXCEEDS_WINDOW] = 7, |
| 45 | [RXRPC_ACK_NOSPACE] = 8, |
| 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * propose an ACK be sent |
| 50 | */ |
David Howells | 4e36a95 | 2009-09-16 00:01:13 -0700 | [diff] [blame] | 51 | void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 52 | __be32 serial, bool immediate) |
| 53 | { |
| 54 | unsigned long expiry; |
| 55 | s8 prior = rxrpc_ack_priority[ack_reason]; |
| 56 | |
| 57 | ASSERTCMP(prior, >, 0); |
| 58 | |
| 59 | _enter("{%d},%s,%%%x,%u", |
Dan Carpenter | 08d4d21 | 2014-01-20 13:28:59 +0300 | [diff] [blame] | 60 | call->debug_id, rxrpc_acks(ack_reason), ntohl(serial), |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 61 | immediate); |
| 62 | |
| 63 | if (prior < rxrpc_ack_priority[call->ackr_reason]) { |
| 64 | if (immediate) |
| 65 | goto cancel_timer; |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial |
| 70 | * numbers */ |
| 71 | if (prior == rxrpc_ack_priority[call->ackr_reason]) { |
| 72 | if (prior <= 4) |
| 73 | call->ackr_serial = serial; |
| 74 | if (immediate) |
| 75 | goto cancel_timer; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | call->ackr_reason = ack_reason; |
| 80 | call->ackr_serial = serial; |
| 81 | |
| 82 | switch (ack_reason) { |
| 83 | case RXRPC_ACK_DELAY: |
| 84 | _debug("run delay timer"); |
| 85 | call->ack_timer.expires = jiffies + rxrpc_ack_timeout * HZ; |
| 86 | add_timer(&call->ack_timer); |
| 87 | return; |
| 88 | |
| 89 | case RXRPC_ACK_IDLE: |
| 90 | if (!immediate) { |
| 91 | _debug("run defer timer"); |
| 92 | expiry = 1; |
| 93 | goto run_timer; |
| 94 | } |
| 95 | goto cancel_timer; |
| 96 | |
| 97 | case RXRPC_ACK_REQUESTED: |
| 98 | if (!rxrpc_ack_defer) |
| 99 | goto cancel_timer; |
| 100 | if (!immediate || serial == cpu_to_be32(1)) { |
| 101 | _debug("run defer timer"); |
| 102 | expiry = rxrpc_ack_defer; |
| 103 | goto run_timer; |
| 104 | } |
| 105 | |
| 106 | default: |
| 107 | _debug("immediate ACK"); |
| 108 | goto cancel_timer; |
| 109 | } |
| 110 | |
| 111 | run_timer: |
| 112 | expiry += jiffies; |
| 113 | if (!timer_pending(&call->ack_timer) || |
| 114 | time_after(call->ack_timer.expires, expiry)) |
| 115 | mod_timer(&call->ack_timer, expiry); |
| 116 | return; |
| 117 | |
| 118 | cancel_timer: |
| 119 | _debug("cancel timer %%%u", ntohl(serial)); |
| 120 | try_to_del_timer_sync(&call->ack_timer); |
| 121 | read_lock_bh(&call->state_lock); |
| 122 | if (call->state <= RXRPC_CALL_COMPLETE && |
| 123 | !test_and_set_bit(RXRPC_CALL_ACK, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 124 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 125 | read_unlock_bh(&call->state_lock); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * propose an ACK be sent, locking the call structure |
| 130 | */ |
David Howells | 4e36a95 | 2009-09-16 00:01:13 -0700 | [diff] [blame] | 131 | void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 132 | __be32 serial, bool immediate) |
| 133 | { |
| 134 | s8 prior = rxrpc_ack_priority[ack_reason]; |
| 135 | |
| 136 | if (prior > rxrpc_ack_priority[call->ackr_reason]) { |
| 137 | spin_lock_bh(&call->lock); |
| 138 | __rxrpc_propose_ACK(call, ack_reason, serial, immediate); |
| 139 | spin_unlock_bh(&call->lock); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * set the resend timer |
| 145 | */ |
| 146 | static void rxrpc_set_resend(struct rxrpc_call *call, u8 resend, |
| 147 | unsigned long resend_at) |
| 148 | { |
| 149 | read_lock_bh(&call->state_lock); |
| 150 | if (call->state >= RXRPC_CALL_COMPLETE) |
| 151 | resend = 0; |
| 152 | |
| 153 | if (resend & 1) { |
| 154 | _debug("SET RESEND"); |
| 155 | set_bit(RXRPC_CALL_RESEND, &call->events); |
| 156 | } |
| 157 | |
| 158 | if (resend & 2) { |
| 159 | _debug("MODIFY RESEND TIMER"); |
| 160 | set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
| 161 | mod_timer(&call->resend_timer, resend_at); |
| 162 | } else { |
| 163 | _debug("KILL RESEND TIMER"); |
| 164 | del_timer_sync(&call->resend_timer); |
| 165 | clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events); |
| 166 | clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
| 167 | } |
| 168 | read_unlock_bh(&call->state_lock); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * resend packets |
| 173 | */ |
| 174 | static void rxrpc_resend(struct rxrpc_call *call) |
| 175 | { |
| 176 | struct rxrpc_skb_priv *sp; |
| 177 | struct rxrpc_header *hdr; |
| 178 | struct sk_buff *txb; |
| 179 | unsigned long *p_txb, resend_at; |
| 180 | int loop, stop; |
| 181 | u8 resend; |
| 182 | |
| 183 | _enter("{%d,%d,%d,%d},", |
| 184 | call->acks_hard, call->acks_unacked, |
| 185 | atomic_read(&call->sequence), |
| 186 | CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz)); |
| 187 | |
| 188 | stop = 0; |
| 189 | resend = 0; |
| 190 | resend_at = 0; |
| 191 | |
| 192 | for (loop = call->acks_tail; |
| 193 | loop != call->acks_head || stop; |
| 194 | loop = (loop + 1) & (call->acks_winsz - 1) |
| 195 | ) { |
| 196 | p_txb = call->acks_window + loop; |
| 197 | smp_read_barrier_depends(); |
| 198 | if (*p_txb & 1) |
| 199 | continue; |
| 200 | |
| 201 | txb = (struct sk_buff *) *p_txb; |
| 202 | sp = rxrpc_skb(txb); |
| 203 | |
| 204 | if (sp->need_resend) { |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 205 | sp->need_resend = false; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 206 | |
| 207 | /* each Tx packet has a new serial number */ |
| 208 | sp->hdr.serial = |
| 209 | htonl(atomic_inc_return(&call->conn->serial)); |
| 210 | |
| 211 | hdr = (struct rxrpc_header *) txb->head; |
| 212 | hdr->serial = sp->hdr.serial; |
| 213 | |
| 214 | _proto("Tx DATA %%%u { #%d }", |
| 215 | ntohl(sp->hdr.serial), ntohl(sp->hdr.seq)); |
| 216 | if (rxrpc_send_packet(call->conn->trans, txb) < 0) { |
| 217 | stop = 0; |
| 218 | sp->resend_at = jiffies + 3; |
| 219 | } else { |
| 220 | sp->resend_at = |
| 221 | jiffies + rxrpc_resend_timeout * HZ; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (time_after_eq(jiffies + 1, sp->resend_at)) { |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 226 | sp->need_resend = true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 227 | resend |= 1; |
| 228 | } else if (resend & 2) { |
| 229 | if (time_before(sp->resend_at, resend_at)) |
| 230 | resend_at = sp->resend_at; |
| 231 | } else { |
| 232 | resend_at = sp->resend_at; |
| 233 | resend |= 2; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | rxrpc_set_resend(call, resend, resend_at); |
| 238 | _leave(""); |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * handle resend timer expiry |
| 243 | */ |
| 244 | static void rxrpc_resend_timer(struct rxrpc_call *call) |
| 245 | { |
| 246 | struct rxrpc_skb_priv *sp; |
| 247 | struct sk_buff *txb; |
| 248 | unsigned long *p_txb, resend_at; |
| 249 | int loop; |
| 250 | u8 resend; |
| 251 | |
| 252 | _enter("%d,%d,%d", |
| 253 | call->acks_tail, call->acks_unacked, call->acks_head); |
| 254 | |
David Howells | 3b5bac2 | 2010-08-04 02:34:17 +0000 | [diff] [blame] | 255 | if (call->state >= RXRPC_CALL_COMPLETE) |
| 256 | return; |
| 257 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 258 | resend = 0; |
| 259 | resend_at = 0; |
| 260 | |
| 261 | for (loop = call->acks_unacked; |
| 262 | loop != call->acks_head; |
| 263 | loop = (loop + 1) & (call->acks_winsz - 1) |
| 264 | ) { |
| 265 | p_txb = call->acks_window + loop; |
| 266 | smp_read_barrier_depends(); |
| 267 | txb = (struct sk_buff *) (*p_txb & ~1); |
| 268 | sp = rxrpc_skb(txb); |
| 269 | |
| 270 | ASSERT(!(*p_txb & 1)); |
| 271 | |
| 272 | if (sp->need_resend) { |
| 273 | ; |
| 274 | } else if (time_after_eq(jiffies + 1, sp->resend_at)) { |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 275 | sp->need_resend = true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 276 | resend |= 1; |
| 277 | } else if (resend & 2) { |
| 278 | if (time_before(sp->resend_at, resend_at)) |
| 279 | resend_at = sp->resend_at; |
| 280 | } else { |
| 281 | resend_at = sp->resend_at; |
| 282 | resend |= 2; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | rxrpc_set_resend(call, resend, resend_at); |
| 287 | _leave(""); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * process soft ACKs of our transmitted packets |
| 292 | * - these indicate packets the peer has or has not received, but hasn't yet |
| 293 | * given to the consumer, and so can still be discarded and re-requested |
| 294 | */ |
| 295 | static int rxrpc_process_soft_ACKs(struct rxrpc_call *call, |
| 296 | struct rxrpc_ackpacket *ack, |
| 297 | struct sk_buff *skb) |
| 298 | { |
| 299 | struct rxrpc_skb_priv *sp; |
| 300 | struct sk_buff *txb; |
| 301 | unsigned long *p_txb, resend_at; |
| 302 | int loop; |
| 303 | u8 sacks[RXRPC_MAXACKS], resend; |
| 304 | |
| 305 | _enter("{%d,%d},{%d},", |
| 306 | call->acks_hard, |
| 307 | CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz), |
| 308 | ack->nAcks); |
| 309 | |
| 310 | if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0) |
| 311 | goto protocol_error; |
| 312 | |
| 313 | resend = 0; |
| 314 | resend_at = 0; |
| 315 | for (loop = 0; loop < ack->nAcks; loop++) { |
| 316 | p_txb = call->acks_window; |
| 317 | p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1); |
| 318 | smp_read_barrier_depends(); |
| 319 | txb = (struct sk_buff *) (*p_txb & ~1); |
| 320 | sp = rxrpc_skb(txb); |
| 321 | |
| 322 | switch (sacks[loop]) { |
| 323 | case RXRPC_ACK_TYPE_ACK: |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 324 | sp->need_resend = false; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 325 | *p_txb |= 1; |
| 326 | break; |
| 327 | case RXRPC_ACK_TYPE_NACK: |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 328 | sp->need_resend = true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 329 | *p_txb &= ~1; |
| 330 | resend = 1; |
| 331 | break; |
| 332 | default: |
| 333 | _debug("Unsupported ACK type %d", sacks[loop]); |
| 334 | goto protocol_error; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | smp_mb(); |
| 339 | call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1); |
| 340 | |
| 341 | /* anything not explicitly ACK'd is implicitly NACK'd, but may just not |
| 342 | * have been received or processed yet by the far end */ |
| 343 | for (loop = call->acks_unacked; |
| 344 | loop != call->acks_head; |
| 345 | loop = (loop + 1) & (call->acks_winsz - 1) |
| 346 | ) { |
| 347 | p_txb = call->acks_window + loop; |
| 348 | smp_read_barrier_depends(); |
| 349 | txb = (struct sk_buff *) (*p_txb & ~1); |
| 350 | sp = rxrpc_skb(txb); |
| 351 | |
| 352 | if (*p_txb & 1) { |
| 353 | /* packet must have been discarded */ |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 354 | sp->need_resend = true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 355 | *p_txb &= ~1; |
| 356 | resend |= 1; |
| 357 | } else if (sp->need_resend) { |
| 358 | ; |
| 359 | } else if (time_after_eq(jiffies + 1, sp->resend_at)) { |
Rusty Russell | 3db1cd5 | 2011-12-19 13:56:45 +0000 | [diff] [blame] | 360 | sp->need_resend = true; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 361 | resend |= 1; |
| 362 | } else if (resend & 2) { |
| 363 | if (time_before(sp->resend_at, resend_at)) |
| 364 | resend_at = sp->resend_at; |
| 365 | } else { |
| 366 | resend_at = sp->resend_at; |
| 367 | resend |= 2; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | rxrpc_set_resend(call, resend, resend_at); |
| 372 | _leave(" = 0"); |
| 373 | return 0; |
| 374 | |
| 375 | protocol_error: |
| 376 | _leave(" = -EPROTO"); |
| 377 | return -EPROTO; |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * discard hard-ACK'd packets from the Tx window |
| 382 | */ |
| 383 | static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard) |
| 384 | { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 385 | unsigned long _skb; |
| 386 | int tail = call->acks_tail, old_tail; |
| 387 | int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz); |
| 388 | |
| 389 | _enter("{%u,%u},%u", call->acks_hard, win, hard); |
| 390 | |
| 391 | ASSERTCMP(hard - call->acks_hard, <=, win); |
| 392 | |
| 393 | while (call->acks_hard < hard) { |
| 394 | smp_read_barrier_depends(); |
| 395 | _skb = call->acks_window[tail] & ~1; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 396 | rxrpc_free_skb((struct sk_buff *) _skb); |
| 397 | old_tail = tail; |
| 398 | tail = (tail + 1) & (call->acks_winsz - 1); |
| 399 | call->acks_tail = tail; |
| 400 | if (call->acks_unacked == old_tail) |
| 401 | call->acks_unacked = tail; |
| 402 | call->acks_hard++; |
| 403 | } |
| 404 | |
| 405 | wake_up(&call->tx_waitq); |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * clear the Tx window in the event of a failure |
| 410 | */ |
| 411 | static void rxrpc_clear_tx_window(struct rxrpc_call *call) |
| 412 | { |
| 413 | rxrpc_rotate_tx_window(call, atomic_read(&call->sequence)); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * drain the out of sequence received packet queue into the packet Rx queue |
| 418 | */ |
| 419 | static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call) |
| 420 | { |
| 421 | struct rxrpc_skb_priv *sp; |
| 422 | struct sk_buff *skb; |
| 423 | bool terminal; |
| 424 | int ret; |
| 425 | |
| 426 | _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos); |
| 427 | |
| 428 | spin_lock_bh(&call->lock); |
| 429 | |
| 430 | ret = -ECONNRESET; |
| 431 | if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) |
| 432 | goto socket_unavailable; |
| 433 | |
| 434 | skb = skb_dequeue(&call->rx_oos_queue); |
| 435 | if (skb) { |
| 436 | sp = rxrpc_skb(skb); |
| 437 | |
| 438 | _debug("drain OOS packet %d [%d]", |
| 439 | ntohl(sp->hdr.seq), call->rx_first_oos); |
| 440 | |
| 441 | if (ntohl(sp->hdr.seq) != call->rx_first_oos) { |
| 442 | skb_queue_head(&call->rx_oos_queue, skb); |
| 443 | call->rx_first_oos = ntohl(rxrpc_skb(skb)->hdr.seq); |
| 444 | _debug("requeue %p {%u}", skb, call->rx_first_oos); |
| 445 | } else { |
| 446 | skb->mark = RXRPC_SKB_MARK_DATA; |
| 447 | terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) && |
| 448 | !(sp->hdr.flags & RXRPC_CLIENT_INITIATED)); |
| 449 | ret = rxrpc_queue_rcv_skb(call, skb, true, terminal); |
| 450 | BUG_ON(ret < 0); |
| 451 | _debug("drain #%u", call->rx_data_post); |
| 452 | call->rx_data_post++; |
| 453 | |
| 454 | /* find out what the next packet is */ |
| 455 | skb = skb_peek(&call->rx_oos_queue); |
| 456 | if (skb) |
| 457 | call->rx_first_oos = |
| 458 | ntohl(rxrpc_skb(skb)->hdr.seq); |
| 459 | else |
| 460 | call->rx_first_oos = 0; |
| 461 | _debug("peek %p {%u}", skb, call->rx_first_oos); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | ret = 0; |
| 466 | socket_unavailable: |
| 467 | spin_unlock_bh(&call->lock); |
| 468 | _leave(" = %d", ret); |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * insert an out of sequence packet into the buffer |
| 474 | */ |
| 475 | static void rxrpc_insert_oos_packet(struct rxrpc_call *call, |
| 476 | struct sk_buff *skb) |
| 477 | { |
| 478 | struct rxrpc_skb_priv *sp, *psp; |
| 479 | struct sk_buff *p; |
| 480 | u32 seq; |
| 481 | |
| 482 | sp = rxrpc_skb(skb); |
| 483 | seq = ntohl(sp->hdr.seq); |
| 484 | _enter(",,{%u}", seq); |
| 485 | |
| 486 | skb->destructor = rxrpc_packet_destructor; |
| 487 | ASSERTCMP(sp->call, ==, NULL); |
| 488 | sp->call = call; |
| 489 | rxrpc_get_call(call); |
| 490 | |
| 491 | /* insert into the buffer in sequence order */ |
| 492 | spin_lock_bh(&call->lock); |
| 493 | |
| 494 | skb_queue_walk(&call->rx_oos_queue, p) { |
| 495 | psp = rxrpc_skb(p); |
| 496 | if (ntohl(psp->hdr.seq) > seq) { |
| 497 | _debug("insert oos #%u before #%u", |
| 498 | seq, ntohl(psp->hdr.seq)); |
| 499 | skb_insert(p, skb, &call->rx_oos_queue); |
| 500 | goto inserted; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | _debug("append oos #%u", seq); |
| 505 | skb_queue_tail(&call->rx_oos_queue, skb); |
| 506 | inserted: |
| 507 | |
| 508 | /* we might now have a new front to the queue */ |
| 509 | if (call->rx_first_oos == 0 || seq < call->rx_first_oos) |
| 510 | call->rx_first_oos = seq; |
| 511 | |
| 512 | read_lock(&call->state_lock); |
| 513 | if (call->state < RXRPC_CALL_COMPLETE && |
| 514 | call->rx_data_post == call->rx_first_oos) { |
| 515 | _debug("drain rx oos now"); |
| 516 | set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events); |
| 517 | } |
| 518 | read_unlock(&call->state_lock); |
| 519 | |
| 520 | spin_unlock_bh(&call->lock); |
| 521 | _leave(" [stored #%u]", call->rx_first_oos); |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * clear the Tx window on final ACK reception |
| 526 | */ |
| 527 | static void rxrpc_zap_tx_window(struct rxrpc_call *call) |
| 528 | { |
| 529 | struct rxrpc_skb_priv *sp; |
| 530 | struct sk_buff *skb; |
| 531 | unsigned long _skb, *acks_window; |
David Howells | 4e36a95 | 2009-09-16 00:01:13 -0700 | [diff] [blame] | 532 | u8 winsz = call->acks_winsz; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 533 | int tail; |
| 534 | |
| 535 | acks_window = call->acks_window; |
| 536 | call->acks_window = NULL; |
| 537 | |
| 538 | while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) { |
| 539 | tail = call->acks_tail; |
| 540 | smp_read_barrier_depends(); |
| 541 | _skb = acks_window[tail] & ~1; |
| 542 | smp_mb(); |
| 543 | call->acks_tail = (call->acks_tail + 1) & (winsz - 1); |
| 544 | |
| 545 | skb = (struct sk_buff *) _skb; |
| 546 | sp = rxrpc_skb(skb); |
| 547 | _debug("+++ clear Tx %u", ntohl(sp->hdr.seq)); |
| 548 | rxrpc_free_skb(skb); |
| 549 | } |
| 550 | |
| 551 | kfree(acks_window); |
| 552 | } |
| 553 | |
| 554 | /* |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 555 | * process the extra information that may be appended to an ACK packet |
| 556 | */ |
| 557 | static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 558 | unsigned int latest, int nAcks) |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 559 | { |
| 560 | struct rxrpc_ackinfo ackinfo; |
| 561 | struct rxrpc_peer *peer; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 562 | unsigned int mtu; |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 563 | |
| 564 | if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) { |
| 565 | _leave(" [no ackinfo]"); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }", |
| 570 | latest, |
| 571 | ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU), |
| 572 | ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max)); |
| 573 | |
| 574 | mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU)); |
| 575 | |
| 576 | peer = call->conn->trans->peer; |
| 577 | if (mtu < peer->maxdata) { |
| 578 | spin_lock_bh(&peer->lock); |
| 579 | peer->maxdata = mtu; |
| 580 | peer->mtu = mtu + peer->hdrsize; |
| 581 | spin_unlock_bh(&peer->lock); |
| 582 | _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 587 | * process packets in the reception queue |
| 588 | */ |
| 589 | static int rxrpc_process_rx_queue(struct rxrpc_call *call, |
| 590 | u32 *_abort_code) |
| 591 | { |
| 592 | struct rxrpc_ackpacket ack; |
| 593 | struct rxrpc_skb_priv *sp; |
| 594 | struct sk_buff *skb; |
| 595 | bool post_ACK; |
| 596 | int latest; |
| 597 | u32 hard, tx; |
| 598 | |
| 599 | _enter(""); |
| 600 | |
| 601 | process_further: |
| 602 | skb = skb_dequeue(&call->rx_queue); |
| 603 | if (!skb) |
| 604 | return -EAGAIN; |
| 605 | |
| 606 | _net("deferred skb %p", skb); |
| 607 | |
| 608 | sp = rxrpc_skb(skb); |
| 609 | |
| 610 | _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state); |
| 611 | |
| 612 | post_ACK = false; |
| 613 | |
| 614 | switch (sp->hdr.type) { |
| 615 | /* data packets that wind up here have been received out of |
| 616 | * order, need security processing or are jumbo packets */ |
| 617 | case RXRPC_PACKET_TYPE_DATA: |
| 618 | _proto("OOSQ DATA %%%u { #%u }", |
| 619 | ntohl(sp->hdr.serial), ntohl(sp->hdr.seq)); |
| 620 | |
| 621 | /* secured packets must be verified and possibly decrypted */ |
| 622 | if (rxrpc_verify_packet(call, skb, _abort_code) < 0) |
| 623 | goto protocol_error; |
| 624 | |
| 625 | rxrpc_insert_oos_packet(call, skb); |
| 626 | goto process_further; |
| 627 | |
| 628 | /* partial ACK to process */ |
| 629 | case RXRPC_PACKET_TYPE_ACK: |
| 630 | if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) { |
| 631 | _debug("extraction failure"); |
| 632 | goto protocol_error; |
| 633 | } |
| 634 | if (!skb_pull(skb, sizeof(ack))) |
| 635 | BUG(); |
| 636 | |
| 637 | latest = ntohl(sp->hdr.serial); |
| 638 | hard = ntohl(ack.firstPacket); |
| 639 | tx = atomic_read(&call->sequence); |
| 640 | |
| 641 | _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }", |
| 642 | latest, |
| 643 | ntohs(ack.maxSkew), |
| 644 | hard, |
| 645 | ntohl(ack.previousPacket), |
| 646 | ntohl(ack.serial), |
Dan Carpenter | 08d4d21 | 2014-01-20 13:28:59 +0300 | [diff] [blame] | 647 | rxrpc_acks(ack.reason), |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 648 | ack.nAcks); |
| 649 | |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 650 | rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks); |
| 651 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 652 | if (ack.reason == RXRPC_ACK_PING) { |
| 653 | _proto("Rx ACK %%%u PING Request", latest); |
| 654 | rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE, |
| 655 | sp->hdr.serial, true); |
| 656 | } |
| 657 | |
| 658 | /* discard any out-of-order or duplicate ACKs */ |
| 659 | if (latest - call->acks_latest <= 0) { |
| 660 | _debug("discard ACK %d <= %d", |
| 661 | latest, call->acks_latest); |
| 662 | goto discard; |
| 663 | } |
| 664 | call->acks_latest = latest; |
| 665 | |
| 666 | if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST && |
| 667 | call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY && |
| 668 | call->state != RXRPC_CALL_SERVER_SEND_REPLY && |
| 669 | call->state != RXRPC_CALL_SERVER_AWAIT_ACK) |
| 670 | goto discard; |
| 671 | |
| 672 | _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state); |
| 673 | |
| 674 | if (hard > 0) { |
| 675 | if (hard - 1 > tx) { |
| 676 | _debug("hard-ACK'd packet %d not transmitted" |
| 677 | " (%d top)", |
| 678 | hard - 1, tx); |
| 679 | goto protocol_error; |
| 680 | } |
| 681 | |
| 682 | if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY || |
| 683 | call->state == RXRPC_CALL_SERVER_AWAIT_ACK) && |
| 684 | hard > tx) |
| 685 | goto all_acked; |
| 686 | |
| 687 | smp_rmb(); |
| 688 | rxrpc_rotate_tx_window(call, hard - 1); |
| 689 | } |
| 690 | |
| 691 | if (ack.nAcks > 0) { |
| 692 | if (hard - 1 + ack.nAcks > tx) { |
| 693 | _debug("soft-ACK'd packet %d+%d not" |
| 694 | " transmitted (%d top)", |
| 695 | hard - 1, ack.nAcks, tx); |
| 696 | goto protocol_error; |
| 697 | } |
| 698 | |
| 699 | if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0) |
| 700 | goto protocol_error; |
| 701 | } |
| 702 | goto discard; |
| 703 | |
| 704 | /* complete ACK to process */ |
| 705 | case RXRPC_PACKET_TYPE_ACKALL: |
| 706 | goto all_acked; |
| 707 | |
| 708 | /* abort and busy are handled elsewhere */ |
| 709 | case RXRPC_PACKET_TYPE_BUSY: |
| 710 | case RXRPC_PACKET_TYPE_ABORT: |
| 711 | BUG(); |
| 712 | |
| 713 | /* connection level events - also handled elsewhere */ |
| 714 | case RXRPC_PACKET_TYPE_CHALLENGE: |
| 715 | case RXRPC_PACKET_TYPE_RESPONSE: |
| 716 | case RXRPC_PACKET_TYPE_DEBUG: |
| 717 | BUG(); |
| 718 | } |
| 719 | |
| 720 | /* if we've had a hard ACK that covers all the packets we've sent, then |
| 721 | * that ends that phase of the operation */ |
| 722 | all_acked: |
| 723 | write_lock_bh(&call->state_lock); |
| 724 | _debug("ack all %d", call->state); |
| 725 | |
| 726 | switch (call->state) { |
| 727 | case RXRPC_CALL_CLIENT_AWAIT_REPLY: |
| 728 | call->state = RXRPC_CALL_CLIENT_RECV_REPLY; |
| 729 | break; |
| 730 | case RXRPC_CALL_SERVER_AWAIT_ACK: |
| 731 | _debug("srv complete"); |
| 732 | call->state = RXRPC_CALL_COMPLETE; |
| 733 | post_ACK = true; |
| 734 | break; |
| 735 | case RXRPC_CALL_CLIENT_SEND_REQUEST: |
| 736 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
| 737 | goto protocol_error_unlock; /* can't occur yet */ |
| 738 | default: |
| 739 | write_unlock_bh(&call->state_lock); |
| 740 | goto discard; /* assume packet left over from earlier phase */ |
| 741 | } |
| 742 | |
| 743 | write_unlock_bh(&call->state_lock); |
| 744 | |
| 745 | /* if all the packets we sent are hard-ACK'd, then we can discard |
| 746 | * whatever we've got left */ |
| 747 | _debug("clear Tx %d", |
| 748 | CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz)); |
| 749 | |
| 750 | del_timer_sync(&call->resend_timer); |
| 751 | clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
| 752 | clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events); |
| 753 | |
| 754 | if (call->acks_window) |
| 755 | rxrpc_zap_tx_window(call); |
| 756 | |
| 757 | if (post_ACK) { |
| 758 | /* post the final ACK message for userspace to pick up */ |
| 759 | _debug("post ACK"); |
| 760 | skb->mark = RXRPC_SKB_MARK_FINAL_ACK; |
| 761 | sp->call = call; |
| 762 | rxrpc_get_call(call); |
| 763 | spin_lock_bh(&call->lock); |
| 764 | if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0) |
| 765 | BUG(); |
| 766 | spin_unlock_bh(&call->lock); |
| 767 | goto process_further; |
| 768 | } |
| 769 | |
| 770 | discard: |
| 771 | rxrpc_free_skb(skb); |
| 772 | goto process_further; |
| 773 | |
| 774 | protocol_error_unlock: |
| 775 | write_unlock_bh(&call->state_lock); |
| 776 | protocol_error: |
| 777 | rxrpc_free_skb(skb); |
| 778 | _leave(" = -EPROTO"); |
| 779 | return -EPROTO; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * post a message to the socket Rx queue for recvmsg() to pick up |
| 784 | */ |
| 785 | static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error, |
| 786 | bool fatal) |
| 787 | { |
| 788 | struct rxrpc_skb_priv *sp; |
| 789 | struct sk_buff *skb; |
| 790 | int ret; |
| 791 | |
| 792 | _enter("{%d,%lx},%u,%u,%d", |
| 793 | call->debug_id, call->flags, mark, error, fatal); |
| 794 | |
| 795 | /* remove timers and things for fatal messages */ |
| 796 | if (fatal) { |
| 797 | del_timer_sync(&call->resend_timer); |
| 798 | del_timer_sync(&call->ack_timer); |
| 799 | clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
| 800 | } |
| 801 | |
| 802 | if (mark != RXRPC_SKB_MARK_NEW_CALL && |
| 803 | !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { |
| 804 | _leave("[no userid]"); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) { |
| 809 | skb = alloc_skb(0, GFP_NOFS); |
| 810 | if (!skb) |
| 811 | return -ENOMEM; |
| 812 | |
| 813 | rxrpc_new_skb(skb); |
| 814 | |
| 815 | skb->mark = mark; |
| 816 | |
| 817 | sp = rxrpc_skb(skb); |
| 818 | memset(sp, 0, sizeof(*sp)); |
| 819 | sp->error = error; |
| 820 | sp->call = call; |
| 821 | rxrpc_get_call(call); |
| 822 | |
| 823 | spin_lock_bh(&call->lock); |
| 824 | ret = rxrpc_queue_rcv_skb(call, skb, true, fatal); |
| 825 | spin_unlock_bh(&call->lock); |
Julia Lawall | 163e3cb | 2008-02-17 18:42:03 -0800 | [diff] [blame] | 826 | BUG_ON(ret < 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * handle background processing of incoming call packets and ACK / abort |
| 834 | * generation |
| 835 | */ |
| 836 | void rxrpc_process_call(struct work_struct *work) |
| 837 | { |
| 838 | struct rxrpc_call *call = |
| 839 | container_of(work, struct rxrpc_call, processor); |
| 840 | struct rxrpc_ackpacket ack; |
| 841 | struct rxrpc_ackinfo ackinfo; |
| 842 | struct rxrpc_header hdr; |
| 843 | struct msghdr msg; |
| 844 | struct kvec iov[5]; |
| 845 | unsigned long bits; |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 846 | __be32 data, pad; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 847 | size_t len; |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 848 | int genbit, loop, nbit, ioc, ret, mtu; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 849 | u32 abort_code = RX_PROTOCOL_ERROR; |
| 850 | u8 *acks = NULL; |
| 851 | |
| 852 | //printk("\n--------------------\n"); |
| 853 | _enter("{%d,%s,%lx} [%lu]", |
| 854 | call->debug_id, rxrpc_call_states[call->state], call->events, |
| 855 | (jiffies - call->creation_jif) / (HZ / 10)); |
| 856 | |
| 857 | if (test_and_set_bit(RXRPC_CALL_PROC_BUSY, &call->flags)) { |
| 858 | _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX"); |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | /* there's a good chance we're going to have to send a message, so set |
| 863 | * one up in advance */ |
| 864 | msg.msg_name = &call->conn->trans->peer->srx.transport.sin; |
| 865 | msg.msg_namelen = sizeof(call->conn->trans->peer->srx.transport.sin); |
| 866 | msg.msg_control = NULL; |
| 867 | msg.msg_controllen = 0; |
| 868 | msg.msg_flags = 0; |
| 869 | |
| 870 | hdr.epoch = call->conn->epoch; |
| 871 | hdr.cid = call->cid; |
| 872 | hdr.callNumber = call->call_id; |
| 873 | hdr.seq = 0; |
| 874 | hdr.type = RXRPC_PACKET_TYPE_ACK; |
| 875 | hdr.flags = call->conn->out_clientflag; |
| 876 | hdr.userStatus = 0; |
| 877 | hdr.securityIndex = call->conn->security_ix; |
| 878 | hdr._rsvd = 0; |
| 879 | hdr.serviceId = call->conn->service_id; |
| 880 | |
| 881 | memset(iov, 0, sizeof(iov)); |
| 882 | iov[0].iov_base = &hdr; |
| 883 | iov[0].iov_len = sizeof(hdr); |
| 884 | |
| 885 | /* deal with events of a final nature */ |
| 886 | if (test_bit(RXRPC_CALL_RELEASE, &call->events)) { |
| 887 | rxrpc_release_call(call); |
| 888 | clear_bit(RXRPC_CALL_RELEASE, &call->events); |
| 889 | } |
| 890 | |
| 891 | if (test_bit(RXRPC_CALL_RCVD_ERROR, &call->events)) { |
| 892 | int error; |
| 893 | |
| 894 | clear_bit(RXRPC_CALL_CONN_ABORT, &call->events); |
| 895 | clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events); |
| 896 | clear_bit(RXRPC_CALL_ABORT, &call->events); |
| 897 | |
| 898 | error = call->conn->trans->peer->net_error; |
| 899 | _debug("post net error %d", error); |
| 900 | |
| 901 | if (rxrpc_post_message(call, RXRPC_SKB_MARK_NET_ERROR, |
| 902 | error, true) < 0) |
| 903 | goto no_mem; |
| 904 | clear_bit(RXRPC_CALL_RCVD_ERROR, &call->events); |
| 905 | goto kill_ACKs; |
| 906 | } |
| 907 | |
| 908 | if (test_bit(RXRPC_CALL_CONN_ABORT, &call->events)) { |
| 909 | ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE); |
| 910 | |
| 911 | clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events); |
| 912 | clear_bit(RXRPC_CALL_ABORT, &call->events); |
| 913 | |
| 914 | _debug("post conn abort"); |
| 915 | |
| 916 | if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR, |
| 917 | call->conn->error, true) < 0) |
| 918 | goto no_mem; |
| 919 | clear_bit(RXRPC_CALL_CONN_ABORT, &call->events); |
| 920 | goto kill_ACKs; |
| 921 | } |
| 922 | |
| 923 | if (test_bit(RXRPC_CALL_REJECT_BUSY, &call->events)) { |
| 924 | hdr.type = RXRPC_PACKET_TYPE_BUSY; |
| 925 | genbit = RXRPC_CALL_REJECT_BUSY; |
| 926 | goto send_message; |
| 927 | } |
| 928 | |
| 929 | if (test_bit(RXRPC_CALL_ABORT, &call->events)) { |
| 930 | ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE); |
| 931 | |
| 932 | if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR, |
| 933 | ECONNABORTED, true) < 0) |
| 934 | goto no_mem; |
| 935 | hdr.type = RXRPC_PACKET_TYPE_ABORT; |
| 936 | data = htonl(call->abort_code); |
| 937 | iov[1].iov_base = &data; |
| 938 | iov[1].iov_len = sizeof(data); |
| 939 | genbit = RXRPC_CALL_ABORT; |
| 940 | goto send_message; |
| 941 | } |
| 942 | |
| 943 | if (test_bit(RXRPC_CALL_ACK_FINAL, &call->events)) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 944 | genbit = RXRPC_CALL_ACK_FINAL; |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 945 | |
| 946 | ack.bufferSpace = htons(8); |
| 947 | ack.maxSkew = 0; |
| 948 | ack.serial = 0; |
| 949 | ack.reason = RXRPC_ACK_IDLE; |
| 950 | ack.nAcks = 0; |
| 951 | call->ackr_reason = 0; |
| 952 | |
| 953 | spin_lock_bh(&call->lock); |
| 954 | ack.serial = call->ackr_serial; |
| 955 | ack.previousPacket = call->ackr_prev_seq; |
| 956 | ack.firstPacket = htonl(call->rx_data_eaten + 1); |
| 957 | spin_unlock_bh(&call->lock); |
| 958 | |
| 959 | pad = 0; |
| 960 | |
| 961 | iov[1].iov_base = &ack; |
| 962 | iov[1].iov_len = sizeof(ack); |
| 963 | iov[2].iov_base = &pad; |
| 964 | iov[2].iov_len = 3; |
| 965 | iov[3].iov_base = &ackinfo; |
| 966 | iov[3].iov_len = sizeof(ackinfo); |
| 967 | goto send_ACK; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | if (call->events & ((1 << RXRPC_CALL_RCVD_BUSY) | |
| 971 | (1 << RXRPC_CALL_RCVD_ABORT)) |
| 972 | ) { |
| 973 | u32 mark; |
| 974 | |
| 975 | if (test_bit(RXRPC_CALL_RCVD_ABORT, &call->events)) |
| 976 | mark = RXRPC_SKB_MARK_REMOTE_ABORT; |
| 977 | else |
| 978 | mark = RXRPC_SKB_MARK_BUSY; |
| 979 | |
| 980 | _debug("post abort/busy"); |
| 981 | rxrpc_clear_tx_window(call); |
| 982 | if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0) |
| 983 | goto no_mem; |
| 984 | |
| 985 | clear_bit(RXRPC_CALL_RCVD_BUSY, &call->events); |
| 986 | clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events); |
| 987 | goto kill_ACKs; |
| 988 | } |
| 989 | |
| 990 | if (test_and_clear_bit(RXRPC_CALL_RCVD_ACKALL, &call->events)) { |
| 991 | _debug("do implicit ackall"); |
| 992 | rxrpc_clear_tx_window(call); |
| 993 | } |
| 994 | |
| 995 | if (test_bit(RXRPC_CALL_LIFE_TIMER, &call->events)) { |
| 996 | write_lock_bh(&call->state_lock); |
| 997 | if (call->state <= RXRPC_CALL_COMPLETE) { |
| 998 | call->state = RXRPC_CALL_LOCALLY_ABORTED; |
| 999 | call->abort_code = RX_CALL_TIMEOUT; |
| 1000 | set_bit(RXRPC_CALL_ABORT, &call->events); |
| 1001 | } |
| 1002 | write_unlock_bh(&call->state_lock); |
| 1003 | |
| 1004 | _debug("post timeout"); |
| 1005 | if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR, |
| 1006 | ETIME, true) < 0) |
| 1007 | goto no_mem; |
| 1008 | |
| 1009 | clear_bit(RXRPC_CALL_LIFE_TIMER, &call->events); |
| 1010 | goto kill_ACKs; |
| 1011 | } |
| 1012 | |
| 1013 | /* deal with assorted inbound messages */ |
| 1014 | if (!skb_queue_empty(&call->rx_queue)) { |
| 1015 | switch (rxrpc_process_rx_queue(call, &abort_code)) { |
| 1016 | case 0: |
| 1017 | case -EAGAIN: |
| 1018 | break; |
| 1019 | case -ENOMEM: |
| 1020 | goto no_mem; |
| 1021 | case -EKEYEXPIRED: |
| 1022 | case -EKEYREJECTED: |
| 1023 | case -EPROTO: |
| 1024 | rxrpc_abort_call(call, abort_code); |
| 1025 | goto kill_ACKs; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | /* handle resending */ |
| 1030 | if (test_and_clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events)) |
| 1031 | rxrpc_resend_timer(call); |
| 1032 | if (test_and_clear_bit(RXRPC_CALL_RESEND, &call->events)) |
| 1033 | rxrpc_resend(call); |
| 1034 | |
| 1035 | /* consider sending an ordinary ACK */ |
| 1036 | if (test_bit(RXRPC_CALL_ACK, &call->events)) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1037 | _debug("send ACK: window: %d - %d { %lx }", |
| 1038 | call->rx_data_eaten, call->ackr_win_top, |
| 1039 | call->ackr_window[0]); |
| 1040 | |
| 1041 | if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST && |
| 1042 | call->ackr_reason != RXRPC_ACK_PING_RESPONSE) { |
| 1043 | /* ACK by sending reply DATA packet in this state */ |
| 1044 | clear_bit(RXRPC_CALL_ACK, &call->events); |
| 1045 | goto maybe_reschedule; |
| 1046 | } |
| 1047 | |
| 1048 | genbit = RXRPC_CALL_ACK; |
| 1049 | |
| 1050 | acks = kzalloc(call->ackr_win_top - call->rx_data_eaten, |
| 1051 | GFP_NOFS); |
| 1052 | if (!acks) |
| 1053 | goto no_mem; |
| 1054 | |
| 1055 | //hdr.flags = RXRPC_SLOW_START_OK; |
| 1056 | ack.bufferSpace = htons(8); |
| 1057 | ack.maxSkew = 0; |
| 1058 | ack.serial = 0; |
| 1059 | ack.reason = 0; |
| 1060 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1061 | spin_lock_bh(&call->lock); |
| 1062 | ack.reason = call->ackr_reason; |
| 1063 | ack.serial = call->ackr_serial; |
| 1064 | ack.previousPacket = call->ackr_prev_seq; |
| 1065 | ack.firstPacket = htonl(call->rx_data_eaten + 1); |
| 1066 | |
| 1067 | ack.nAcks = 0; |
| 1068 | for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) { |
| 1069 | nbit = loop * BITS_PER_LONG; |
| 1070 | for (bits = call->ackr_window[loop]; bits; bits >>= 1 |
| 1071 | ) { |
| 1072 | _debug("- l=%d n=%d b=%lx", loop, nbit, bits); |
| 1073 | if (bits & 1) { |
| 1074 | acks[nbit] = RXRPC_ACK_TYPE_ACK; |
| 1075 | ack.nAcks = nbit + 1; |
| 1076 | } |
| 1077 | nbit++; |
| 1078 | } |
| 1079 | } |
| 1080 | call->ackr_reason = 0; |
| 1081 | spin_unlock_bh(&call->lock); |
| 1082 | |
| 1083 | pad = 0; |
| 1084 | |
| 1085 | iov[1].iov_base = &ack; |
| 1086 | iov[1].iov_len = sizeof(ack); |
| 1087 | iov[2].iov_base = acks; |
| 1088 | iov[2].iov_len = ack.nAcks; |
| 1089 | iov[3].iov_base = &pad; |
| 1090 | iov[3].iov_len = 3; |
| 1091 | iov[4].iov_base = &ackinfo; |
| 1092 | iov[4].iov_len = sizeof(ackinfo); |
| 1093 | |
| 1094 | switch (ack.reason) { |
| 1095 | case RXRPC_ACK_REQUESTED: |
| 1096 | case RXRPC_ACK_DUPLICATE: |
| 1097 | case RXRPC_ACK_OUT_OF_SEQUENCE: |
| 1098 | case RXRPC_ACK_EXCEEDS_WINDOW: |
| 1099 | case RXRPC_ACK_NOSPACE: |
| 1100 | case RXRPC_ACK_PING: |
| 1101 | case RXRPC_ACK_PING_RESPONSE: |
| 1102 | goto send_ACK_with_skew; |
| 1103 | case RXRPC_ACK_DELAY: |
| 1104 | case RXRPC_ACK_IDLE: |
| 1105 | goto send_ACK; |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | /* handle completion of security negotiations on an incoming |
| 1110 | * connection */ |
| 1111 | if (test_and_clear_bit(RXRPC_CALL_SECURED, &call->events)) { |
| 1112 | _debug("secured"); |
| 1113 | spin_lock_bh(&call->lock); |
| 1114 | |
| 1115 | if (call->state == RXRPC_CALL_SERVER_SECURING) { |
| 1116 | _debug("securing"); |
| 1117 | write_lock(&call->conn->lock); |
| 1118 | if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && |
| 1119 | !test_bit(RXRPC_CALL_RELEASE, &call->events)) { |
| 1120 | _debug("not released"); |
| 1121 | call->state = RXRPC_CALL_SERVER_ACCEPTING; |
| 1122 | list_move_tail(&call->accept_link, |
| 1123 | &call->socket->acceptq); |
| 1124 | } |
| 1125 | write_unlock(&call->conn->lock); |
| 1126 | read_lock(&call->state_lock); |
| 1127 | if (call->state < RXRPC_CALL_COMPLETE) |
| 1128 | set_bit(RXRPC_CALL_POST_ACCEPT, &call->events); |
| 1129 | read_unlock(&call->state_lock); |
| 1130 | } |
| 1131 | |
| 1132 | spin_unlock_bh(&call->lock); |
| 1133 | if (!test_bit(RXRPC_CALL_POST_ACCEPT, &call->events)) |
| 1134 | goto maybe_reschedule; |
| 1135 | } |
| 1136 | |
| 1137 | /* post a notification of an acceptable connection to the app */ |
| 1138 | if (test_bit(RXRPC_CALL_POST_ACCEPT, &call->events)) { |
| 1139 | _debug("post accept"); |
| 1140 | if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL, |
| 1141 | 0, false) < 0) |
| 1142 | goto no_mem; |
| 1143 | clear_bit(RXRPC_CALL_POST_ACCEPT, &call->events); |
| 1144 | goto maybe_reschedule; |
| 1145 | } |
| 1146 | |
| 1147 | /* handle incoming call acceptance */ |
| 1148 | if (test_and_clear_bit(RXRPC_CALL_ACCEPTED, &call->events)) { |
| 1149 | _debug("accepted"); |
| 1150 | ASSERTCMP(call->rx_data_post, ==, 0); |
| 1151 | call->rx_data_post = 1; |
| 1152 | read_lock_bh(&call->state_lock); |
| 1153 | if (call->state < RXRPC_CALL_COMPLETE) |
| 1154 | set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events); |
| 1155 | read_unlock_bh(&call->state_lock); |
| 1156 | } |
| 1157 | |
| 1158 | /* drain the out of sequence received packet queue into the packet Rx |
| 1159 | * queue */ |
| 1160 | if (test_and_clear_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events)) { |
| 1161 | while (call->rx_data_post == call->rx_first_oos) |
| 1162 | if (rxrpc_drain_rx_oos_queue(call) < 0) |
| 1163 | break; |
| 1164 | goto maybe_reschedule; |
| 1165 | } |
| 1166 | |
| 1167 | /* other events may have been raised since we started checking */ |
| 1168 | goto maybe_reschedule; |
| 1169 | |
| 1170 | send_ACK_with_skew: |
| 1171 | ack.maxSkew = htons(atomic_read(&call->conn->hi_serial) - |
| 1172 | ntohl(ack.serial)); |
| 1173 | send_ACK: |
David Howells | 224711d | 2007-05-04 12:41:11 -0700 | [diff] [blame] | 1174 | mtu = call->conn->trans->peer->if_mtu; |
| 1175 | mtu -= call->conn->trans->peer->hdrsize; |
| 1176 | ackinfo.maxMTU = htonl(mtu); |
| 1177 | ackinfo.rwind = htonl(32); |
| 1178 | |
| 1179 | /* permit the peer to send us jumbo packets if it wants to */ |
| 1180 | ackinfo.rxMTU = htonl(5692); |
| 1181 | ackinfo.jumbo_max = htonl(4); |
| 1182 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1183 | hdr.serial = htonl(atomic_inc_return(&call->conn->serial)); |
| 1184 | _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }", |
| 1185 | ntohl(hdr.serial), |
| 1186 | ntohs(ack.maxSkew), |
| 1187 | ntohl(ack.firstPacket), |
| 1188 | ntohl(ack.previousPacket), |
| 1189 | ntohl(ack.serial), |
Dan Carpenter | 08d4d21 | 2014-01-20 13:28:59 +0300 | [diff] [blame] | 1190 | rxrpc_acks(ack.reason), |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1191 | ack.nAcks); |
| 1192 | |
| 1193 | del_timer_sync(&call->ack_timer); |
| 1194 | if (ack.nAcks > 0) |
| 1195 | set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags); |
| 1196 | goto send_message_2; |
| 1197 | |
| 1198 | send_message: |
| 1199 | _debug("send message"); |
| 1200 | |
| 1201 | hdr.serial = htonl(atomic_inc_return(&call->conn->serial)); |
| 1202 | _proto("Tx %s %%%u", rxrpc_pkts[hdr.type], ntohl(hdr.serial)); |
| 1203 | send_message_2: |
| 1204 | |
| 1205 | len = iov[0].iov_len; |
| 1206 | ioc = 1; |
| 1207 | if (iov[4].iov_len) { |
| 1208 | ioc = 5; |
| 1209 | len += iov[4].iov_len; |
| 1210 | len += iov[3].iov_len; |
| 1211 | len += iov[2].iov_len; |
| 1212 | len += iov[1].iov_len; |
| 1213 | } else if (iov[3].iov_len) { |
| 1214 | ioc = 4; |
| 1215 | len += iov[3].iov_len; |
| 1216 | len += iov[2].iov_len; |
| 1217 | len += iov[1].iov_len; |
| 1218 | } else if (iov[2].iov_len) { |
| 1219 | ioc = 3; |
| 1220 | len += iov[2].iov_len; |
| 1221 | len += iov[1].iov_len; |
| 1222 | } else if (iov[1].iov_len) { |
| 1223 | ioc = 2; |
| 1224 | len += iov[1].iov_len; |
| 1225 | } |
| 1226 | |
| 1227 | ret = kernel_sendmsg(call->conn->trans->local->socket, |
| 1228 | &msg, iov, ioc, len); |
| 1229 | if (ret < 0) { |
| 1230 | _debug("sendmsg failed: %d", ret); |
| 1231 | read_lock_bh(&call->state_lock); |
| 1232 | if (call->state < RXRPC_CALL_DEAD) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 1233 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1234 | read_unlock_bh(&call->state_lock); |
| 1235 | goto error; |
| 1236 | } |
| 1237 | |
| 1238 | switch (genbit) { |
| 1239 | case RXRPC_CALL_ABORT: |
| 1240 | clear_bit(genbit, &call->events); |
| 1241 | clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events); |
| 1242 | goto kill_ACKs; |
| 1243 | |
| 1244 | case RXRPC_CALL_ACK_FINAL: |
| 1245 | write_lock_bh(&call->state_lock); |
| 1246 | if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK) |
| 1247 | call->state = RXRPC_CALL_COMPLETE; |
| 1248 | write_unlock_bh(&call->state_lock); |
| 1249 | goto kill_ACKs; |
| 1250 | |
| 1251 | default: |
| 1252 | clear_bit(genbit, &call->events); |
| 1253 | switch (call->state) { |
| 1254 | case RXRPC_CALL_CLIENT_AWAIT_REPLY: |
| 1255 | case RXRPC_CALL_CLIENT_RECV_REPLY: |
| 1256 | case RXRPC_CALL_SERVER_RECV_REQUEST: |
| 1257 | case RXRPC_CALL_SERVER_ACK_REQUEST: |
| 1258 | _debug("start ACK timer"); |
| 1259 | rxrpc_propose_ACK(call, RXRPC_ACK_DELAY, |
| 1260 | call->ackr_serial, false); |
| 1261 | default: |
| 1262 | break; |
| 1263 | } |
| 1264 | goto maybe_reschedule; |
| 1265 | } |
| 1266 | |
| 1267 | kill_ACKs: |
| 1268 | del_timer_sync(&call->ack_timer); |
| 1269 | if (test_and_clear_bit(RXRPC_CALL_ACK_FINAL, &call->events)) |
| 1270 | rxrpc_put_call(call); |
| 1271 | clear_bit(RXRPC_CALL_ACK, &call->events); |
| 1272 | |
| 1273 | maybe_reschedule: |
| 1274 | if (call->events || !skb_queue_empty(&call->rx_queue)) { |
| 1275 | read_lock_bh(&call->state_lock); |
| 1276 | if (call->state < RXRPC_CALL_DEAD) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 1277 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1278 | read_unlock_bh(&call->state_lock); |
| 1279 | } |
| 1280 | |
| 1281 | /* don't leave aborted connections on the accept queue */ |
| 1282 | if (call->state >= RXRPC_CALL_COMPLETE && |
| 1283 | !list_empty(&call->accept_link)) { |
| 1284 | _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }", |
| 1285 | call, call->events, call->flags, |
| 1286 | ntohl(call->conn->cid)); |
| 1287 | |
| 1288 | read_lock_bh(&call->state_lock); |
| 1289 | if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) && |
| 1290 | !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 1291 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1292 | read_unlock_bh(&call->state_lock); |
| 1293 | } |
| 1294 | |
| 1295 | error: |
| 1296 | clear_bit(RXRPC_CALL_PROC_BUSY, &call->flags); |
| 1297 | kfree(acks); |
| 1298 | |
| 1299 | /* because we don't want two CPUs both processing the work item for one |
| 1300 | * call at the same time, we use a flag to note when it's busy; however |
| 1301 | * this means there's a race between clearing the flag and setting the |
| 1302 | * work pending bit and the work item being processed again */ |
| 1303 | if (call->events && !work_pending(&call->processor)) { |
| 1304 | _debug("jumpstart %x", ntohl(call->conn->cid)); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 1305 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | _leave(""); |
| 1309 | return; |
| 1310 | |
| 1311 | no_mem: |
| 1312 | _debug("out of memory"); |
| 1313 | goto maybe_reschedule; |
| 1314 | } |