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