David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* RxRPC individual remote procedure call handling |
| 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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/circ_buf.h> |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 17 | #include <linux/hashtable.h> |
| 18 | #include <linux/spinlock_types.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 19 | #include <net/sock.h> |
| 20 | #include <net/af_rxrpc.h> |
| 21 | #include "ar-internal.h" |
| 22 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Maximum lifetime of a call (in jiffies). |
| 25 | */ |
David Howells | dad8aff | 2016-03-09 23:22:56 +0000 | [diff] [blame] | 26 | unsigned int rxrpc_max_call_lifetime = 60 * HZ; |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Time till dead call expires after last use (in jiffies). |
| 30 | */ |
David Howells | dad8aff | 2016-03-09 23:22:56 +0000 | [diff] [blame] | 31 | unsigned int rxrpc_dead_call_expiry = 2 * HZ; |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 32 | |
David Howells | 5b8848d | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 33 | const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = { |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 34 | [RXRPC_CALL_UNINITIALISED] = "Uninit", |
| 35 | [RXRPC_CALL_CLIENT_AWAIT_CONN] = "ClWtConn", |
David Howells | 1f8481d | 2007-05-22 16:14:24 -0700 | [diff] [blame] | 36 | [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq", |
| 37 | [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl", |
| 38 | [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl", |
| 39 | [RXRPC_CALL_CLIENT_FINAL_ACK] = "ClFnlACK", |
| 40 | [RXRPC_CALL_SERVER_SECURING] = "SvSecure", |
| 41 | [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept", |
| 42 | [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq", |
| 43 | [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq", |
| 44 | [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl", |
| 45 | [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK", |
| 46 | [RXRPC_CALL_COMPLETE] = "Complete", |
| 47 | [RXRPC_CALL_SERVER_BUSY] = "SvBusy ", |
| 48 | [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort", |
| 49 | [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort", |
| 50 | [RXRPC_CALL_NETWORK_ERROR] = "NetError", |
| 51 | [RXRPC_CALL_DEAD] = "Dead ", |
| 52 | }; |
| 53 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 54 | struct kmem_cache *rxrpc_call_jar; |
| 55 | LIST_HEAD(rxrpc_calls); |
| 56 | DEFINE_RWLOCK(rxrpc_call_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 57 | |
| 58 | static void rxrpc_destroy_call(struct work_struct *work); |
| 59 | static void rxrpc_call_life_expired(unsigned long _call); |
| 60 | static void rxrpc_dead_call_expired(unsigned long _call); |
| 61 | static void rxrpc_ack_time_expired(unsigned long _call); |
| 62 | static void rxrpc_resend_time_expired(unsigned long _call); |
| 63 | |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 64 | static DEFINE_SPINLOCK(rxrpc_call_hash_lock); |
| 65 | static DEFINE_HASHTABLE(rxrpc_call_hash, 10); |
| 66 | |
| 67 | /* |
| 68 | * Hash function for rxrpc_call_hash |
| 69 | */ |
| 70 | static unsigned long rxrpc_call_hashfunc( |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 71 | u8 in_clientflag, |
| 72 | u32 cid, |
| 73 | u32 call_id, |
| 74 | u32 epoch, |
| 75 | u16 service_id, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 76 | sa_family_t family, |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 77 | void *localptr, |
| 78 | unsigned int addr_size, |
| 79 | const u8 *peer_addr) |
| 80 | { |
| 81 | const u16 *p; |
| 82 | unsigned int i; |
| 83 | unsigned long key; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 84 | |
| 85 | _enter(""); |
| 86 | |
| 87 | key = (unsigned long)localptr; |
| 88 | /* We just want to add up the __be32 values, so forcing the |
| 89 | * cast should be okay. |
| 90 | */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 91 | key += epoch; |
| 92 | key += service_id; |
| 93 | key += call_id; |
| 94 | key += (cid & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT; |
| 95 | key += cid & RXRPC_CHANNELMASK; |
| 96 | key += in_clientflag; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 97 | key += family; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 98 | /* Step through the peer address in 16-bit portions for speed */ |
| 99 | for (i = 0, p = (const u16 *)peer_addr; i < addr_size >> 1; i++, p++) |
| 100 | key += *p; |
| 101 | _leave(" key = 0x%lx", key); |
| 102 | return key; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Add a call to the hashtable |
| 107 | */ |
| 108 | static void rxrpc_call_hash_add(struct rxrpc_call *call) |
| 109 | { |
| 110 | unsigned long key; |
| 111 | unsigned int addr_size = 0; |
| 112 | |
| 113 | _enter(""); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 114 | switch (call->family) { |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 115 | case AF_INET: |
| 116 | addr_size = sizeof(call->peer_ip.ipv4_addr); |
| 117 | break; |
| 118 | case AF_INET6: |
| 119 | addr_size = sizeof(call->peer_ip.ipv6_addr); |
| 120 | break; |
| 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | key = rxrpc_call_hashfunc(call->in_clientflag, call->cid, |
| 125 | call->call_id, call->epoch, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 126 | call->service_id, call->family, |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 127 | call->conn->params.local, addr_size, |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 128 | call->peer_ip.ipv6_addr); |
| 129 | /* Store the full key in the call */ |
| 130 | call->hash_key = key; |
| 131 | spin_lock(&rxrpc_call_hash_lock); |
| 132 | hash_add_rcu(rxrpc_call_hash, &call->hash_node, key); |
| 133 | spin_unlock(&rxrpc_call_hash_lock); |
| 134 | _leave(""); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Remove a call from the hashtable |
| 139 | */ |
| 140 | static void rxrpc_call_hash_del(struct rxrpc_call *call) |
| 141 | { |
| 142 | _enter(""); |
| 143 | spin_lock(&rxrpc_call_hash_lock); |
| 144 | hash_del_rcu(&call->hash_node); |
| 145 | spin_unlock(&rxrpc_call_hash_lock); |
| 146 | _leave(""); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Find a call in the hashtable and return it, or NULL if it |
| 151 | * isn't there. |
| 152 | */ |
| 153 | struct rxrpc_call *rxrpc_find_call_hash( |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 154 | struct rxrpc_host_header *hdr, |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 155 | void *localptr, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 156 | sa_family_t family, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 157 | const void *peer_addr) |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 158 | { |
| 159 | unsigned long key; |
| 160 | unsigned int addr_size = 0; |
| 161 | struct rxrpc_call *call = NULL; |
| 162 | struct rxrpc_call *ret = NULL; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 163 | u8 in_clientflag = hdr->flags & RXRPC_CLIENT_INITIATED; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 164 | |
| 165 | _enter(""); |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 166 | switch (family) { |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 167 | case AF_INET: |
| 168 | addr_size = sizeof(call->peer_ip.ipv4_addr); |
| 169 | break; |
| 170 | case AF_INET6: |
| 171 | addr_size = sizeof(call->peer_ip.ipv6_addr); |
| 172 | break; |
| 173 | default: |
| 174 | break; |
| 175 | } |
| 176 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 177 | key = rxrpc_call_hashfunc(in_clientflag, hdr->cid, hdr->callNumber, |
| 178 | hdr->epoch, hdr->serviceId, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 179 | family, localptr, addr_size, |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 180 | peer_addr); |
| 181 | hash_for_each_possible_rcu(rxrpc_call_hash, call, hash_node, key) { |
| 182 | if (call->hash_key == key && |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 183 | call->call_id == hdr->callNumber && |
| 184 | call->cid == hdr->cid && |
| 185 | call->in_clientflag == in_clientflag && |
| 186 | call->service_id == hdr->serviceId && |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 187 | call->family == family && |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 188 | call->local == localptr && |
| 189 | memcmp(call->peer_ip.ipv6_addr, peer_addr, |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 190 | addr_size) == 0 && |
| 191 | call->epoch == hdr->epoch) { |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 192 | ret = call; |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | _leave(" = %p", ret); |
| 197 | return ret; |
| 198 | } |
| 199 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 200 | /* |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 201 | * find an extant server call |
| 202 | * - called in process context with IRQs enabled |
| 203 | */ |
| 204 | struct rxrpc_call *rxrpc_find_call_by_user_ID(struct rxrpc_sock *rx, |
| 205 | unsigned long user_call_ID) |
| 206 | { |
| 207 | struct rxrpc_call *call; |
| 208 | struct rb_node *p; |
| 209 | |
| 210 | _enter("%p,%lx", rx, user_call_ID); |
| 211 | |
| 212 | read_lock(&rx->call_lock); |
| 213 | |
| 214 | p = rx->calls.rb_node; |
| 215 | while (p) { |
| 216 | call = rb_entry(p, struct rxrpc_call, sock_node); |
| 217 | |
| 218 | if (user_call_ID < call->user_call_ID) |
| 219 | p = p->rb_left; |
| 220 | else if (user_call_ID > call->user_call_ID) |
| 221 | p = p->rb_right; |
| 222 | else |
| 223 | goto found_extant_call; |
| 224 | } |
| 225 | |
| 226 | read_unlock(&rx->call_lock); |
| 227 | _leave(" = NULL"); |
| 228 | return NULL; |
| 229 | |
| 230 | found_extant_call: |
| 231 | rxrpc_get_call(call); |
| 232 | read_unlock(&rx->call_lock); |
| 233 | _leave(" = %p [%d]", call, atomic_read(&call->usage)); |
| 234 | return call; |
| 235 | } |
| 236 | |
| 237 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 238 | * allocate a new call |
| 239 | */ |
| 240 | static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp) |
| 241 | { |
| 242 | struct rxrpc_call *call; |
| 243 | |
| 244 | call = kmem_cache_zalloc(rxrpc_call_jar, gfp); |
| 245 | if (!call) |
| 246 | return NULL; |
| 247 | |
| 248 | call->acks_winsz = 16; |
| 249 | call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long), |
| 250 | gfp); |
| 251 | if (!call->acks_window) { |
| 252 | kmem_cache_free(rxrpc_call_jar, call); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | setup_timer(&call->lifetimer, &rxrpc_call_life_expired, |
| 257 | (unsigned long) call); |
| 258 | setup_timer(&call->deadspan, &rxrpc_dead_call_expired, |
| 259 | (unsigned long) call); |
| 260 | setup_timer(&call->ack_timer, &rxrpc_ack_time_expired, |
| 261 | (unsigned long) call); |
| 262 | setup_timer(&call->resend_timer, &rxrpc_resend_time_expired, |
| 263 | (unsigned long) call); |
| 264 | INIT_WORK(&call->destroyer, &rxrpc_destroy_call); |
| 265 | INIT_WORK(&call->processor, &rxrpc_process_call); |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 266 | INIT_LIST_HEAD(&call->link); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 267 | INIT_LIST_HEAD(&call->accept_link); |
| 268 | skb_queue_head_init(&call->rx_queue); |
| 269 | skb_queue_head_init(&call->rx_oos_queue); |
| 270 | init_waitqueue_head(&call->tx_waitq); |
| 271 | spin_lock_init(&call->lock); |
| 272 | rwlock_init(&call->state_lock); |
| 273 | atomic_set(&call->usage, 1); |
| 274 | call->debug_id = atomic_inc_return(&rxrpc_debug_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 275 | |
| 276 | memset(&call->sock_node, 0xed, sizeof(call->sock_node)); |
| 277 | |
| 278 | call->rx_data_expect = 1; |
| 279 | call->rx_data_eaten = 0; |
| 280 | call->rx_first_oos = 0; |
David Howells | 817913d | 2014-02-07 18:10:30 +0000 | [diff] [blame] | 281 | call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 282 | call->creation_jif = jiffies; |
| 283 | return call; |
| 284 | } |
| 285 | |
| 286 | /* |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 287 | * Allocate a new client call. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 288 | */ |
| 289 | static struct rxrpc_call *rxrpc_alloc_client_call( |
| 290 | struct rxrpc_sock *rx, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 291 | struct rxrpc_conn_parameters *cp, |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 292 | struct sockaddr_rxrpc *srx, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 293 | gfp_t gfp) |
| 294 | { |
| 295 | struct rxrpc_call *call; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 296 | |
| 297 | _enter(""); |
| 298 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 299 | ASSERT(rx->local != NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 300 | |
| 301 | call = rxrpc_alloc_call(gfp); |
| 302 | if (!call) |
| 303 | return ERR_PTR(-ENOMEM); |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 304 | call->state = RXRPC_CALL_CLIENT_AWAIT_CONN; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 305 | |
| 306 | sock_hold(&rx->sk); |
| 307 | call->socket = rx; |
| 308 | call->rx_data_post = 1; |
| 309 | |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 310 | /* Record copies of information for hashtable lookup */ |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 311 | call->family = rx->family; |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 312 | call->local = rx->local; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 313 | switch (call->family) { |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 314 | case AF_INET: |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 315 | call->peer_ip.ipv4_addr = srx->transport.sin.sin_addr.s_addr; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 316 | break; |
| 317 | case AF_INET6: |
| 318 | memcpy(call->peer_ip.ipv6_addr, |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 319 | srx->transport.sin6.sin6_addr.in6_u.u6_addr8, |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 320 | sizeof(call->peer_ip.ipv6_addr)); |
| 321 | break; |
| 322 | } |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 323 | |
| 324 | call->service_id = srx->srx_service; |
| 325 | call->in_clientflag = 0; |
| 326 | |
| 327 | _leave(" = %p", call); |
| 328 | return call; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * Begin client call. |
| 333 | */ |
| 334 | static int rxrpc_begin_client_call(struct rxrpc_call *call, |
| 335 | struct rxrpc_conn_parameters *cp, |
| 336 | struct rxrpc_transport *trans, |
| 337 | struct sockaddr_rxrpc *srx, |
| 338 | gfp_t gfp) |
| 339 | { |
| 340 | int ret; |
| 341 | |
| 342 | /* Set up or get a connection record and set the protocol parameters, |
| 343 | * including channel number and call ID. |
| 344 | */ |
| 345 | ret = rxrpc_connect_call(call, cp, trans, srx, gfp); |
| 346 | if (ret < 0) |
| 347 | return ret; |
| 348 | |
| 349 | call->state = RXRPC_CALL_CLIENT_SEND_REQUEST; |
| 350 | |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 351 | /* Add the new call to the hashtable */ |
| 352 | rxrpc_call_hash_add(call); |
| 353 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 354 | spin_lock(&call->conn->params.peer->lock); |
| 355 | hlist_add_head(&call->error_link, &call->conn->params.peer->error_targets); |
| 356 | spin_unlock(&call->conn->params.peer->lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 357 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 358 | call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 359 | add_timer(&call->lifetimer); |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 360 | return 0; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* |
| 364 | * set up a call for the given data |
| 365 | * - called in process context with IRQs enabled |
| 366 | */ |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 367 | struct rxrpc_call *rxrpc_new_client_call(struct rxrpc_sock *rx, |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 368 | struct rxrpc_conn_parameters *cp, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 369 | struct rxrpc_transport *trans, |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 370 | struct sockaddr_rxrpc *srx, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 371 | unsigned long user_call_ID, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 372 | gfp_t gfp) |
| 373 | { |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 374 | struct rxrpc_call *call, *xcall; |
| 375 | struct rb_node *parent, **pp; |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 376 | int ret; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 377 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 378 | _enter("%p,%lx", rx, user_call_ID); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 379 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 380 | call = rxrpc_alloc_client_call(rx, cp, srx, gfp); |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 381 | if (IS_ERR(call)) { |
| 382 | _leave(" = %ld", PTR_ERR(call)); |
| 383 | return call; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 384 | } |
| 385 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 386 | /* Publish the call, even though it is incompletely set up as yet */ |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 387 | call->user_call_ID = user_call_ID; |
| 388 | __set_bit(RXRPC_CALL_HAS_USERID, &call->flags); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 389 | |
| 390 | write_lock(&rx->call_lock); |
| 391 | |
| 392 | pp = &rx->calls.rb_node; |
| 393 | parent = NULL; |
| 394 | while (*pp) { |
| 395 | parent = *pp; |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 396 | xcall = rb_entry(parent, struct rxrpc_call, sock_node); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 397 | |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 398 | if (user_call_ID < xcall->user_call_ID) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 399 | pp = &(*pp)->rb_left; |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 400 | else if (user_call_ID > xcall->user_call_ID) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 401 | pp = &(*pp)->rb_right; |
| 402 | else |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 403 | goto found_user_ID_now_present; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 404 | } |
| 405 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 406 | rxrpc_get_call(call); |
| 407 | |
| 408 | rb_link_node(&call->sock_node, parent, pp); |
| 409 | rb_insert_color(&call->sock_node, &rx->calls); |
| 410 | write_unlock(&rx->call_lock); |
| 411 | |
| 412 | write_lock_bh(&rxrpc_call_lock); |
| 413 | list_add_tail(&call->link, &rxrpc_calls); |
| 414 | write_unlock_bh(&rxrpc_call_lock); |
| 415 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 416 | ret = rxrpc_begin_client_call(call, cp, trans, srx, gfp); |
| 417 | if (ret < 0) |
| 418 | goto error; |
| 419 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 420 | _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id); |
| 421 | |
| 422 | _leave(" = %p [new]", call); |
| 423 | return call; |
| 424 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 425 | error: |
| 426 | write_lock(&rx->call_lock); |
| 427 | rb_erase(&call->sock_node, &rx->calls); |
| 428 | write_unlock(&rx->call_lock); |
| 429 | rxrpc_put_call(call); |
| 430 | |
| 431 | write_lock_bh(&rxrpc_call_lock); |
| 432 | list_del(&call->link); |
| 433 | write_unlock_bh(&rxrpc_call_lock); |
| 434 | |
| 435 | rxrpc_put_call(call); |
| 436 | _leave(" = %d", ret); |
| 437 | return ERR_PTR(ret); |
| 438 | |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 439 | /* We unexpectedly found the user ID in the list after taking |
| 440 | * the call_lock. This shouldn't happen unless the user races |
| 441 | * with itself and tries to add the same user ID twice at the |
| 442 | * same time in different threads. |
| 443 | */ |
| 444 | found_user_ID_now_present: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 445 | write_unlock(&rx->call_lock); |
David Howells | 2341e07 | 2016-06-09 23:02:51 +0100 | [diff] [blame] | 446 | rxrpc_put_call(call); |
| 447 | _leave(" = -EEXIST [%p]", call); |
| 448 | return ERR_PTR(-EEXIST); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* |
| 452 | * set up an incoming call |
| 453 | * - called in process context with IRQs enabled |
| 454 | */ |
| 455 | struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx, |
| 456 | struct rxrpc_connection *conn, |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 457 | struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 458 | { |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 459 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 460 | struct rxrpc_call *call, *candidate; |
| 461 | struct rb_node **p, *parent; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 462 | u32 call_id; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 463 | |
David Howells | 843099c | 2016-04-07 17:23:37 +0100 | [diff] [blame] | 464 | _enter(",%d", conn->debug_id); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 465 | |
| 466 | ASSERT(rx != NULL); |
| 467 | |
David Howells | 843099c | 2016-04-07 17:23:37 +0100 | [diff] [blame] | 468 | candidate = rxrpc_alloc_call(GFP_NOIO); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 469 | if (!candidate) |
| 470 | return ERR_PTR(-EBUSY); |
| 471 | |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 472 | candidate->socket = rx; |
| 473 | candidate->conn = conn; |
| 474 | candidate->cid = sp->hdr.cid; |
| 475 | candidate->call_id = sp->hdr.callNumber; |
| 476 | candidate->channel = sp->hdr.cid & RXRPC_CHANNELMASK; |
| 477 | candidate->rx_data_post = 0; |
| 478 | candidate->state = RXRPC_CALL_SERVER_ACCEPTING; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 479 | if (conn->security_ix > 0) |
| 480 | candidate->state = RXRPC_CALL_SERVER_SECURING; |
| 481 | |
| 482 | write_lock_bh(&conn->lock); |
| 483 | |
| 484 | /* set the channel for this call */ |
| 485 | call = conn->channels[candidate->channel]; |
| 486 | _debug("channel[%u] is %p", candidate->channel, call); |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 487 | if (call && call->call_id == sp->hdr.callNumber) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 488 | /* already set; must've been a duplicate packet */ |
| 489 | _debug("extant call [%d]", call->state); |
| 490 | ASSERTCMP(call->conn, ==, conn); |
| 491 | |
| 492 | read_lock(&call->state_lock); |
| 493 | switch (call->state) { |
| 494 | case RXRPC_CALL_LOCALLY_ABORTED: |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 495 | if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 496 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 497 | case RXRPC_CALL_REMOTELY_ABORTED: |
| 498 | read_unlock(&call->state_lock); |
| 499 | goto aborted_call; |
| 500 | default: |
| 501 | rxrpc_get_call(call); |
| 502 | read_unlock(&call->state_lock); |
| 503 | goto extant_call; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (call) { |
| 508 | /* it seems the channel is still in use from the previous call |
| 509 | * - ditch the old binding if its call is now complete */ |
| 510 | _debug("CALL: %u { %s }", |
| 511 | call->debug_id, rxrpc_call_states[call->state]); |
| 512 | |
| 513 | if (call->state >= RXRPC_CALL_COMPLETE) { |
| 514 | conn->channels[call->channel] = NULL; |
| 515 | } else { |
| 516 | write_unlock_bh(&conn->lock); |
| 517 | kmem_cache_free(rxrpc_call_jar, candidate); |
| 518 | _leave(" = -EBUSY"); |
| 519 | return ERR_PTR(-EBUSY); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* check the call number isn't duplicate */ |
| 524 | _debug("check dup"); |
David Howells | 42886ff | 2016-06-16 13:31:07 +0100 | [diff] [blame] | 525 | call_id = sp->hdr.callNumber; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 526 | p = &conn->calls.rb_node; |
| 527 | parent = NULL; |
| 528 | while (*p) { |
| 529 | parent = *p; |
| 530 | call = rb_entry(parent, struct rxrpc_call, conn_node); |
| 531 | |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 532 | /* The tree is sorted in order of the __be32 value without |
| 533 | * turning it into host order. |
| 534 | */ |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 535 | if (call_id < call->call_id) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 536 | p = &(*p)->rb_left; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 537 | else if (call_id > call->call_id) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 538 | p = &(*p)->rb_right; |
| 539 | else |
| 540 | goto old_call; |
| 541 | } |
| 542 | |
| 543 | /* make the call available */ |
| 544 | _debug("new call"); |
| 545 | call = candidate; |
| 546 | candidate = NULL; |
| 547 | rb_link_node(&call->conn_node, parent, p); |
| 548 | rb_insert_color(&call->conn_node, &conn->calls); |
| 549 | conn->channels[call->channel] = call; |
| 550 | sock_hold(&rx->sk); |
David Howells | 5627cc8 | 2016-04-04 14:00:38 +0100 | [diff] [blame] | 551 | rxrpc_get_connection(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 552 | write_unlock_bh(&conn->lock); |
| 553 | |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 554 | spin_lock(&conn->params.peer->lock); |
| 555 | hlist_add_head(&call->error_link, &conn->params.peer->error_targets); |
| 556 | spin_unlock(&conn->params.peer->lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 557 | |
| 558 | write_lock_bh(&rxrpc_call_lock); |
| 559 | list_add_tail(&call->link, &rxrpc_calls); |
| 560 | write_unlock_bh(&rxrpc_call_lock); |
| 561 | |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 562 | /* Record copies of information for hashtable lookup */ |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 563 | call->family = rx->family; |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 564 | call->local = conn->params.local; |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 565 | switch (call->family) { |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 566 | case AF_INET: |
| 567 | call->peer_ip.ipv4_addr = |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 568 | conn->params.peer->srx.transport.sin.sin_addr.s_addr; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 569 | break; |
| 570 | case AF_INET6: |
| 571 | memcpy(call->peer_ip.ipv6_addr, |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 572 | conn->params.peer->srx.transport.sin6.sin6_addr.in6_u.u6_addr8, |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 573 | sizeof(call->peer_ip.ipv6_addr)); |
| 574 | break; |
| 575 | default: |
| 576 | break; |
| 577 | } |
David Howells | 19ffa01 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 578 | call->epoch = conn->proto.epoch; |
| 579 | call->service_id = conn->params.service_id; |
| 580 | call->in_clientflag = conn->proto.in_clientflag; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 581 | /* Add the new call to the hashtable */ |
| 582 | rxrpc_call_hash_add(call); |
| 583 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 584 | _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id); |
| 585 | |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 586 | call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 587 | add_timer(&call->lifetimer); |
| 588 | _leave(" = %p {%d} [new]", call, call->debug_id); |
| 589 | return call; |
| 590 | |
| 591 | extant_call: |
| 592 | write_unlock_bh(&conn->lock); |
| 593 | kmem_cache_free(rxrpc_call_jar, candidate); |
| 594 | _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1); |
| 595 | return call; |
| 596 | |
| 597 | aborted_call: |
| 598 | write_unlock_bh(&conn->lock); |
| 599 | kmem_cache_free(rxrpc_call_jar, candidate); |
| 600 | _leave(" = -ECONNABORTED"); |
| 601 | return ERR_PTR(-ECONNABORTED); |
| 602 | |
| 603 | old_call: |
| 604 | write_unlock_bh(&conn->lock); |
| 605 | kmem_cache_free(rxrpc_call_jar, candidate); |
| 606 | _leave(" = -ECONNRESET [old]"); |
| 607 | return ERR_PTR(-ECONNRESET); |
| 608 | } |
| 609 | |
| 610 | /* |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 611 | * detach a call from a socket and set up for release |
| 612 | */ |
| 613 | void rxrpc_release_call(struct rxrpc_call *call) |
| 614 | { |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 615 | struct rxrpc_connection *conn = call->conn; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 616 | struct rxrpc_sock *rx = call->socket; |
| 617 | |
| 618 | _enter("{%d,%d,%d,%d}", |
| 619 | call->debug_id, atomic_read(&call->usage), |
| 620 | atomic_read(&call->ackr_not_idle), |
| 621 | call->rx_first_oos); |
| 622 | |
| 623 | spin_lock_bh(&call->lock); |
| 624 | if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags)) |
| 625 | BUG(); |
| 626 | spin_unlock_bh(&call->lock); |
| 627 | |
| 628 | /* dissociate from the socket |
| 629 | * - the socket's ref on the call is passed to the death timer |
| 630 | */ |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 631 | _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 632 | |
| 633 | write_lock_bh(&rx->call_lock); |
| 634 | if (!list_empty(&call->accept_link)) { |
| 635 | _debug("unlinking once-pending call %p { e=%lx f=%lx }", |
| 636 | call, call->events, call->flags); |
| 637 | ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags)); |
| 638 | list_del_init(&call->accept_link); |
| 639 | sk_acceptq_removed(&rx->sk); |
| 640 | } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) { |
| 641 | rb_erase(&call->sock_node, &rx->calls); |
| 642 | memset(&call->sock_node, 0xdd, sizeof(call->sock_node)); |
| 643 | clear_bit(RXRPC_CALL_HAS_USERID, &call->flags); |
| 644 | } |
| 645 | write_unlock_bh(&rx->call_lock); |
| 646 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 647 | /* free up the channel for reuse */ |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 648 | spin_lock(&conn->channel_lock); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 649 | write_lock_bh(&conn->lock); |
| 650 | write_lock(&call->state_lock); |
| 651 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 652 | rxrpc_disconnect_call(call); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 653 | |
David Howells | 999b69f | 2016-06-17 15:42:35 +0100 | [diff] [blame^] | 654 | spin_unlock(&conn->channel_lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 655 | |
| 656 | if (call->state < RXRPC_CALL_COMPLETE && |
| 657 | call->state != RXRPC_CALL_CLIENT_FINAL_ACK) { |
| 658 | _debug("+++ ABORTING STATE %d +++\n", call->state); |
| 659 | call->state = RXRPC_CALL_LOCALLY_ABORTED; |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 660 | call->local_abort = RX_CALL_DEAD; |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 661 | set_bit(RXRPC_CALL_EV_ABORT, &call->events); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 662 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 663 | } |
| 664 | write_unlock(&call->state_lock); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 665 | write_unlock_bh(&conn->lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 666 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 667 | /* clean up the Rx queue */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 668 | if (!skb_queue_empty(&call->rx_queue) || |
| 669 | !skb_queue_empty(&call->rx_oos_queue)) { |
| 670 | struct rxrpc_skb_priv *sp; |
| 671 | struct sk_buff *skb; |
| 672 | |
| 673 | _debug("purge Rx queues"); |
| 674 | |
| 675 | spin_lock_bh(&call->lock); |
| 676 | while ((skb = skb_dequeue(&call->rx_queue)) || |
| 677 | (skb = skb_dequeue(&call->rx_oos_queue))) { |
| 678 | sp = rxrpc_skb(skb); |
| 679 | if (sp->call) { |
| 680 | ASSERTCMP(sp->call, ==, call); |
| 681 | rxrpc_put_call(call); |
| 682 | sp->call = NULL; |
| 683 | } |
| 684 | skb->destructor = NULL; |
| 685 | spin_unlock_bh(&call->lock); |
| 686 | |
| 687 | _debug("- zap %s %%%u #%u", |
| 688 | rxrpc_pkts[sp->hdr.type], |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 689 | sp->hdr.serial, sp->hdr.seq); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 690 | rxrpc_free_skb(skb); |
| 691 | spin_lock_bh(&call->lock); |
| 692 | } |
| 693 | spin_unlock_bh(&call->lock); |
| 694 | |
| 695 | ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE); |
| 696 | } |
| 697 | |
| 698 | del_timer_sync(&call->resend_timer); |
| 699 | del_timer_sync(&call->ack_timer); |
| 700 | del_timer_sync(&call->lifetimer); |
David Howells | 5873c08 | 2014-02-07 18:58:44 +0000 | [diff] [blame] | 701 | call->deadspan.expires = jiffies + rxrpc_dead_call_expiry; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 702 | add_timer(&call->deadspan); |
| 703 | |
| 704 | _leave(""); |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * handle a dead call being ready for reaping |
| 709 | */ |
| 710 | static void rxrpc_dead_call_expired(unsigned long _call) |
| 711 | { |
| 712 | struct rxrpc_call *call = (struct rxrpc_call *) _call; |
| 713 | |
| 714 | _enter("{%d}", call->debug_id); |
| 715 | |
| 716 | write_lock_bh(&call->state_lock); |
| 717 | call->state = RXRPC_CALL_DEAD; |
| 718 | write_unlock_bh(&call->state_lock); |
| 719 | rxrpc_put_call(call); |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * mark a call as to be released, aborting it if it's still in progress |
| 724 | * - called with softirqs disabled |
| 725 | */ |
| 726 | static void rxrpc_mark_call_released(struct rxrpc_call *call) |
| 727 | { |
| 728 | bool sched; |
| 729 | |
| 730 | write_lock(&call->state_lock); |
| 731 | if (call->state < RXRPC_CALL_DEAD) { |
| 732 | sched = false; |
| 733 | if (call->state < RXRPC_CALL_COMPLETE) { |
| 734 | _debug("abort call %p", call); |
| 735 | call->state = RXRPC_CALL_LOCALLY_ABORTED; |
David Howells | dc44b3a | 2016-04-07 17:23:30 +0100 | [diff] [blame] | 736 | call->local_abort = RX_CALL_DEAD; |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 737 | if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 738 | sched = true; |
| 739 | } |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 740 | if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 741 | sched = true; |
| 742 | if (sched) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 743 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 744 | } |
| 745 | write_unlock(&call->state_lock); |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * release all the calls associated with a socket |
| 750 | */ |
| 751 | void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx) |
| 752 | { |
| 753 | struct rxrpc_call *call; |
| 754 | struct rb_node *p; |
| 755 | |
| 756 | _enter("%p", rx); |
| 757 | |
| 758 | read_lock_bh(&rx->call_lock); |
| 759 | |
| 760 | /* mark all the calls as no longer wanting incoming packets */ |
| 761 | for (p = rb_first(&rx->calls); p; p = rb_next(p)) { |
| 762 | call = rb_entry(p, struct rxrpc_call, sock_node); |
| 763 | rxrpc_mark_call_released(call); |
| 764 | } |
| 765 | |
| 766 | /* kill the not-yet-accepted incoming calls */ |
| 767 | list_for_each_entry(call, &rx->secureq, accept_link) { |
| 768 | rxrpc_mark_call_released(call); |
| 769 | } |
| 770 | |
| 771 | list_for_each_entry(call, &rx->acceptq, accept_link) { |
| 772 | rxrpc_mark_call_released(call); |
| 773 | } |
| 774 | |
| 775 | read_unlock_bh(&rx->call_lock); |
| 776 | _leave(""); |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * release a call |
| 781 | */ |
| 782 | void __rxrpc_put_call(struct rxrpc_call *call) |
| 783 | { |
| 784 | ASSERT(call != NULL); |
| 785 | |
| 786 | _enter("%p{u=%d}", call, atomic_read(&call->usage)); |
| 787 | |
| 788 | ASSERTCMP(atomic_read(&call->usage), >, 0); |
| 789 | |
| 790 | if (atomic_dec_and_test(&call->usage)) { |
| 791 | _debug("call %d dead", call->debug_id); |
| 792 | ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 793 | rxrpc_queue_work(&call->destroyer); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 794 | } |
| 795 | _leave(""); |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * clean up a call |
| 800 | */ |
| 801 | static void rxrpc_cleanup_call(struct rxrpc_call *call) |
| 802 | { |
| 803 | _net("DESTROY CALL %d", call->debug_id); |
| 804 | |
| 805 | ASSERT(call->socket); |
| 806 | |
| 807 | memset(&call->sock_node, 0xcd, sizeof(call->sock_node)); |
| 808 | |
| 809 | del_timer_sync(&call->lifetimer); |
| 810 | del_timer_sync(&call->deadspan); |
| 811 | del_timer_sync(&call->ack_timer); |
| 812 | del_timer_sync(&call->resend_timer); |
| 813 | |
| 814 | ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags)); |
| 815 | ASSERTCMP(call->events, ==, 0); |
| 816 | if (work_pending(&call->processor)) { |
| 817 | _debug("defer destroy"); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 818 | rxrpc_queue_work(&call->destroyer); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | |
| 822 | if (call->conn) { |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 823 | spin_lock(&call->conn->params.peer->lock); |
David Howells | f66d749 | 2016-04-04 14:00:34 +0100 | [diff] [blame] | 824 | hlist_del_init(&call->error_link); |
David Howells | 85f3227 | 2016-04-04 14:00:36 +0100 | [diff] [blame] | 825 | spin_unlock(&call->conn->params.peer->lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 826 | |
| 827 | write_lock_bh(&call->conn->lock); |
| 828 | rb_erase(&call->conn_node, &call->conn->calls); |
| 829 | write_unlock_bh(&call->conn->lock); |
| 830 | rxrpc_put_connection(call->conn); |
| 831 | } |
| 832 | |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 833 | /* Remove the call from the hash */ |
| 834 | rxrpc_call_hash_del(call); |
| 835 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 836 | if (call->acks_window) { |
| 837 | _debug("kill Tx window %d", |
| 838 | CIRC_CNT(call->acks_head, call->acks_tail, |
| 839 | call->acks_winsz)); |
| 840 | smp_mb(); |
| 841 | while (CIRC_CNT(call->acks_head, call->acks_tail, |
| 842 | call->acks_winsz) > 0) { |
| 843 | struct rxrpc_skb_priv *sp; |
| 844 | unsigned long _skb; |
| 845 | |
| 846 | _skb = call->acks_window[call->acks_tail] & ~1; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 847 | sp = rxrpc_skb((struct sk_buff *)_skb); |
| 848 | _debug("+++ clear Tx %u", sp->hdr.seq); |
| 849 | rxrpc_free_skb((struct sk_buff *)_skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 850 | call->acks_tail = |
| 851 | (call->acks_tail + 1) & (call->acks_winsz - 1); |
| 852 | } |
| 853 | |
| 854 | kfree(call->acks_window); |
| 855 | } |
| 856 | |
| 857 | rxrpc_free_skb(call->tx_pending); |
| 858 | |
| 859 | rxrpc_purge_queue(&call->rx_queue); |
| 860 | ASSERT(skb_queue_empty(&call->rx_oos_queue)); |
| 861 | sock_put(&call->socket->sk); |
| 862 | kmem_cache_free(rxrpc_call_jar, call); |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | * destroy a call |
| 867 | */ |
| 868 | static void rxrpc_destroy_call(struct work_struct *work) |
| 869 | { |
| 870 | struct rxrpc_call *call = |
| 871 | container_of(work, struct rxrpc_call, destroyer); |
| 872 | |
| 873 | _enter("%p{%d,%d,%p}", |
| 874 | call, atomic_read(&call->usage), call->channel, call->conn); |
| 875 | |
| 876 | ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD); |
| 877 | |
| 878 | write_lock_bh(&rxrpc_call_lock); |
| 879 | list_del_init(&call->link); |
| 880 | write_unlock_bh(&rxrpc_call_lock); |
| 881 | |
| 882 | rxrpc_cleanup_call(call); |
| 883 | _leave(""); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * preemptively destroy all the call records from a transport endpoint rather |
| 888 | * than waiting for them to time out |
| 889 | */ |
| 890 | void __exit rxrpc_destroy_all_calls(void) |
| 891 | { |
| 892 | struct rxrpc_call *call; |
| 893 | |
| 894 | _enter(""); |
| 895 | write_lock_bh(&rxrpc_call_lock); |
| 896 | |
| 897 | while (!list_empty(&rxrpc_calls)) { |
| 898 | call = list_entry(rxrpc_calls.next, struct rxrpc_call, link); |
| 899 | _debug("Zapping call %p", call); |
| 900 | |
| 901 | list_del_init(&call->link); |
| 902 | |
| 903 | switch (atomic_read(&call->usage)) { |
| 904 | case 0: |
| 905 | ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD); |
| 906 | break; |
| 907 | case 1: |
| 908 | if (del_timer_sync(&call->deadspan) != 0 && |
| 909 | call->state != RXRPC_CALL_DEAD) |
| 910 | rxrpc_dead_call_expired((unsigned long) call); |
| 911 | if (call->state != RXRPC_CALL_DEAD) |
| 912 | break; |
| 913 | default: |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 914 | pr_err("Call %p still in use (%d,%d,%s,%lx,%lx)!\n", |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 915 | call, atomic_read(&call->usage), |
| 916 | atomic_read(&call->ackr_not_idle), |
| 917 | rxrpc_call_states[call->state], |
| 918 | call->flags, call->events); |
| 919 | if (!skb_queue_empty(&call->rx_queue)) |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 920 | pr_err("Rx queue occupied\n"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 921 | if (!skb_queue_empty(&call->rx_oos_queue)) |
Joe Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 922 | pr_err("OOS queue occupied\n"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 923 | break; |
| 924 | } |
| 925 | |
| 926 | write_unlock_bh(&rxrpc_call_lock); |
| 927 | cond_resched(); |
| 928 | write_lock_bh(&rxrpc_call_lock); |
| 929 | } |
| 930 | |
| 931 | write_unlock_bh(&rxrpc_call_lock); |
| 932 | _leave(""); |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * handle call lifetime being exceeded |
| 937 | */ |
| 938 | static void rxrpc_call_life_expired(unsigned long _call) |
| 939 | { |
| 940 | struct rxrpc_call *call = (struct rxrpc_call *) _call; |
| 941 | |
| 942 | if (call->state >= RXRPC_CALL_COMPLETE) |
| 943 | return; |
| 944 | |
| 945 | _enter("{%d}", call->debug_id); |
| 946 | read_lock_bh(&call->state_lock); |
| 947 | if (call->state < RXRPC_CALL_COMPLETE) { |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 948 | set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 949 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 950 | } |
| 951 | read_unlock_bh(&call->state_lock); |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * handle resend timer expiry |
David Howells | 3b5bac2 | 2010-08-04 02:34:17 +0000 | [diff] [blame] | 956 | * - may not take call->state_lock as this can deadlock against del_timer_sync() |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 957 | */ |
| 958 | static void rxrpc_resend_time_expired(unsigned long _call) |
| 959 | { |
| 960 | struct rxrpc_call *call = (struct rxrpc_call *) _call; |
| 961 | |
| 962 | _enter("{%d}", call->debug_id); |
| 963 | |
| 964 | if (call->state >= RXRPC_CALL_COMPLETE) |
| 965 | return; |
| 966 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 967 | clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags); |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 968 | if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 969 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /* |
| 973 | * handle ACK timer expiry |
| 974 | */ |
| 975 | static void rxrpc_ack_time_expired(unsigned long _call) |
| 976 | { |
| 977 | struct rxrpc_call *call = (struct rxrpc_call *) _call; |
| 978 | |
| 979 | _enter("{%d}", call->debug_id); |
| 980 | |
| 981 | if (call->state >= RXRPC_CALL_COMPLETE) |
| 982 | return; |
| 983 | |
| 984 | read_lock_bh(&call->state_lock); |
| 985 | if (call->state < RXRPC_CALL_COMPLETE && |
David Howells | 4c198ad | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 986 | !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events)) |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 987 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 988 | read_unlock_bh(&call->state_lock); |
| 989 | } |