David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 1 | /* Client connection-specific management code. |
| 2 | * |
| 3 | * Copyright (C) 2016 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 Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/idr.h> |
| 16 | #include <linux/timer.h> |
| 17 | #include "ar-internal.h" |
| 18 | |
| 19 | /* |
| 20 | * We use machine-unique IDs for our client connections. |
| 21 | */ |
| 22 | DEFINE_IDR(rxrpc_client_conn_ids); |
| 23 | static DEFINE_SPINLOCK(rxrpc_conn_id_lock); |
| 24 | |
| 25 | /* |
| 26 | * Get a connection ID and epoch for a client connection from the global pool. |
| 27 | * The connection struct pointer is then recorded in the idr radix tree. The |
| 28 | * epoch is changed if this wraps. |
| 29 | * |
| 30 | * TODO: The IDR tree gets very expensive on memory if the connection IDs are |
| 31 | * widely scattered throughout the number space, so we shall need to retire |
| 32 | * connections that have, say, an ID more than four times the maximum number of |
| 33 | * client conns away from the current allocation point to try and keep the IDs |
| 34 | * concentrated. We will also need to retire connections from an old epoch. |
| 35 | */ |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 36 | static int rxrpc_get_client_connection_id(struct rxrpc_connection *conn, |
| 37 | gfp_t gfp) |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 38 | { |
| 39 | u32 epoch; |
| 40 | int id; |
| 41 | |
| 42 | _enter(""); |
| 43 | |
| 44 | idr_preload(gfp); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 45 | spin_lock(&rxrpc_conn_id_lock); |
| 46 | |
| 47 | epoch = rxrpc_epoch; |
| 48 | |
| 49 | /* We could use idr_alloc_cyclic() here, but we really need to know |
| 50 | * when the thing wraps so that we can advance the epoch. |
| 51 | */ |
| 52 | if (rxrpc_client_conn_ids.cur == 0) |
| 53 | rxrpc_client_conn_ids.cur = 1; |
| 54 | id = idr_alloc(&rxrpc_client_conn_ids, conn, |
| 55 | rxrpc_client_conn_ids.cur, 0x40000000, GFP_NOWAIT); |
| 56 | if (id < 0) { |
| 57 | if (id != -ENOSPC) |
| 58 | goto error; |
| 59 | id = idr_alloc(&rxrpc_client_conn_ids, conn, |
| 60 | 1, 0x40000000, GFP_NOWAIT); |
| 61 | if (id < 0) |
| 62 | goto error; |
| 63 | epoch++; |
| 64 | rxrpc_epoch = epoch; |
| 65 | } |
| 66 | rxrpc_client_conn_ids.cur = id + 1; |
| 67 | |
| 68 | spin_unlock(&rxrpc_conn_id_lock); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 69 | idr_preload_end(); |
| 70 | |
| 71 | conn->proto.epoch = epoch; |
| 72 | conn->proto.cid = id << RXRPC_CIDSHIFT; |
| 73 | set_bit(RXRPC_CONN_HAS_IDR, &conn->flags); |
| 74 | _leave(" [CID %x:%x]", epoch, conn->proto.cid); |
| 75 | return 0; |
| 76 | |
| 77 | error: |
| 78 | spin_unlock(&rxrpc_conn_id_lock); |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 79 | idr_preload_end(); |
| 80 | _leave(" = %d", id); |
| 81 | return id; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * Release a connection ID for a client connection from the global pool. |
| 86 | */ |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 87 | static void rxrpc_put_client_connection_id(struct rxrpc_connection *conn) |
David Howells | 4a3388c | 2016-04-04 14:00:37 +0100 | [diff] [blame] | 88 | { |
| 89 | if (test_bit(RXRPC_CONN_HAS_IDR, &conn->flags)) { |
| 90 | spin_lock(&rxrpc_conn_id_lock); |
| 91 | idr_remove(&rxrpc_client_conn_ids, |
| 92 | conn->proto.cid >> RXRPC_CIDSHIFT); |
| 93 | spin_unlock(&rxrpc_conn_id_lock); |
| 94 | } |
| 95 | } |
David Howells | eb9b9d2 | 2016-06-27 10:32:02 +0100 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Destroy the client connection ID tree. |
| 99 | */ |
| 100 | void rxrpc_destroy_client_conn_ids(void) |
| 101 | { |
| 102 | struct rxrpc_connection *conn; |
| 103 | int id; |
| 104 | |
| 105 | if (!idr_is_empty(&rxrpc_client_conn_ids)) { |
| 106 | idr_for_each_entry(&rxrpc_client_conn_ids, conn, id) { |
| 107 | pr_err("AF_RXRPC: Leaked client conn %p {%d}\n", |
| 108 | conn, atomic_read(&conn->usage)); |
| 109 | } |
| 110 | BUG(); |
| 111 | } |
| 112 | |
| 113 | idr_destroy(&rxrpc_client_conn_ids); |
| 114 | } |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Allocate a client connection. The caller must take care to clear any |
| 118 | * padding bytes in *cp. |
| 119 | */ |
| 120 | static struct rxrpc_connection * |
| 121 | rxrpc_alloc_client_connection(struct rxrpc_conn_parameters *cp, gfp_t gfp) |
| 122 | { |
| 123 | struct rxrpc_connection *conn; |
| 124 | int ret; |
| 125 | |
| 126 | _enter(""); |
| 127 | |
| 128 | conn = rxrpc_alloc_connection(gfp); |
| 129 | if (!conn) { |
| 130 | _leave(" = -ENOMEM"); |
| 131 | return ERR_PTR(-ENOMEM); |
| 132 | } |
| 133 | |
| 134 | conn->params = *cp; |
| 135 | conn->proto.local = cp->local; |
| 136 | conn->proto.epoch = rxrpc_epoch; |
| 137 | conn->proto.cid = 0; |
| 138 | conn->proto.in_clientflag = 0; |
| 139 | conn->proto.family = cp->peer->srx.transport.family; |
| 140 | conn->out_clientflag = RXRPC_CLIENT_INITIATED; |
| 141 | conn->state = RXRPC_CONN_CLIENT; |
| 142 | |
| 143 | switch (conn->proto.family) { |
| 144 | case AF_INET: |
| 145 | conn->proto.addr_size = sizeof(conn->proto.ipv4_addr); |
| 146 | conn->proto.ipv4_addr = cp->peer->srx.transport.sin.sin_addr; |
| 147 | conn->proto.port = cp->peer->srx.transport.sin.sin_port; |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | ret = rxrpc_get_client_connection_id(conn, gfp); |
| 152 | if (ret < 0) |
| 153 | goto error_0; |
| 154 | |
| 155 | ret = rxrpc_init_client_conn_security(conn); |
| 156 | if (ret < 0) |
| 157 | goto error_1; |
| 158 | |
| 159 | ret = conn->security->prime_packet_security(conn); |
| 160 | if (ret < 0) |
| 161 | goto error_2; |
| 162 | |
| 163 | write_lock(&rxrpc_connection_lock); |
| 164 | list_add_tail(&conn->link, &rxrpc_connections); |
| 165 | write_unlock(&rxrpc_connection_lock); |
| 166 | |
| 167 | /* We steal the caller's peer ref. */ |
| 168 | cp->peer = NULL; |
| 169 | rxrpc_get_local(conn->params.local); |
| 170 | key_get(conn->params.key); |
| 171 | |
| 172 | _leave(" = %p", conn); |
| 173 | return conn; |
| 174 | |
| 175 | error_2: |
| 176 | conn->security->clear(conn); |
| 177 | error_1: |
| 178 | rxrpc_put_client_connection_id(conn); |
| 179 | error_0: |
| 180 | kfree(conn); |
| 181 | _leave(" = %d", ret); |
| 182 | return ERR_PTR(ret); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * find a connection for a call |
| 187 | * - called in process context with IRQs enabled |
| 188 | */ |
| 189 | int rxrpc_connect_call(struct rxrpc_call *call, |
| 190 | struct rxrpc_conn_parameters *cp, |
| 191 | struct sockaddr_rxrpc *srx, |
| 192 | gfp_t gfp) |
| 193 | { |
| 194 | struct rxrpc_connection *conn, *candidate = NULL; |
| 195 | struct rxrpc_local *local = cp->local; |
| 196 | struct rb_node *p, **pp, *parent; |
| 197 | long diff; |
| 198 | int chan; |
| 199 | |
| 200 | DECLARE_WAITQUEUE(myself, current); |
| 201 | |
| 202 | _enter("{%d,%lx},", call->debug_id, call->user_call_ID); |
| 203 | |
| 204 | cp->peer = rxrpc_lookup_peer(cp->local, srx, gfp); |
| 205 | if (!cp->peer) |
| 206 | return -ENOMEM; |
| 207 | |
| 208 | if (!cp->exclusive) { |
| 209 | /* Search for a existing client connection unless this is going |
| 210 | * to be a connection that's used exclusively for a single call. |
| 211 | */ |
| 212 | _debug("search 1"); |
| 213 | spin_lock(&local->client_conns_lock); |
| 214 | p = local->client_conns.rb_node; |
| 215 | while (p) { |
| 216 | conn = rb_entry(p, struct rxrpc_connection, client_node); |
| 217 | |
| 218 | #define cmp(X) ((long)conn->params.X - (long)cp->X) |
| 219 | diff = (cmp(peer) ?: |
| 220 | cmp(key) ?: |
| 221 | cmp(security_level)); |
| 222 | if (diff < 0) |
| 223 | p = p->rb_left; |
| 224 | else if (diff > 0) |
| 225 | p = p->rb_right; |
| 226 | else |
| 227 | goto found_extant_conn; |
| 228 | } |
| 229 | spin_unlock(&local->client_conns_lock); |
| 230 | } |
| 231 | |
| 232 | /* We didn't find a connection or we want an exclusive one. */ |
| 233 | _debug("get new conn"); |
| 234 | candidate = rxrpc_alloc_client_connection(cp, gfp); |
| 235 | if (!candidate) { |
| 236 | _leave(" = -ENOMEM"); |
| 237 | return -ENOMEM; |
| 238 | } |
| 239 | |
| 240 | if (cp->exclusive) { |
| 241 | /* Assign the call on an exclusive connection to channel 0 and |
| 242 | * don't add the connection to the endpoint's shareable conn |
| 243 | * lookup tree. |
| 244 | */ |
| 245 | _debug("exclusive chan 0"); |
| 246 | conn = candidate; |
| 247 | atomic_set(&conn->avail_chans, RXRPC_MAXCALLS - 1); |
| 248 | spin_lock(&conn->channel_lock); |
| 249 | chan = 0; |
| 250 | goto found_channel; |
| 251 | } |
| 252 | |
| 253 | /* We need to redo the search before attempting to add a new connection |
| 254 | * lest we race with someone else adding a conflicting instance. |
| 255 | */ |
| 256 | _debug("search 2"); |
| 257 | spin_lock(&local->client_conns_lock); |
| 258 | |
| 259 | pp = &local->client_conns.rb_node; |
| 260 | parent = NULL; |
| 261 | while (*pp) { |
| 262 | parent = *pp; |
| 263 | conn = rb_entry(parent, struct rxrpc_connection, client_node); |
| 264 | |
| 265 | diff = (cmp(peer) ?: |
| 266 | cmp(key) ?: |
| 267 | cmp(security_level)); |
| 268 | if (diff < 0) |
| 269 | pp = &(*pp)->rb_left; |
| 270 | else if (diff > 0) |
| 271 | pp = &(*pp)->rb_right; |
| 272 | else |
| 273 | goto found_extant_conn; |
| 274 | } |
| 275 | |
| 276 | /* The second search also failed; simply add the new connection with |
| 277 | * the new call in channel 0. Note that we need to take the channel |
| 278 | * lock before dropping the client conn lock. |
| 279 | */ |
| 280 | _debug("new conn"); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 281 | set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags); |
| 282 | rb_link_node(&candidate->client_node, parent, pp); |
| 283 | rb_insert_color(&candidate->client_node, &local->client_conns); |
| 284 | attached: |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 285 | conn = candidate; |
| 286 | candidate = NULL; |
| 287 | |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 288 | atomic_set(&conn->avail_chans, RXRPC_MAXCALLS - 1); |
| 289 | spin_lock(&conn->channel_lock); |
| 290 | spin_unlock(&local->client_conns_lock); |
| 291 | chan = 0; |
| 292 | |
| 293 | found_channel: |
| 294 | _debug("found chan"); |
| 295 | call->conn = conn; |
| 296 | call->channel = chan; |
| 297 | call->epoch = conn->proto.epoch; |
| 298 | call->cid = conn->proto.cid | chan; |
| 299 | call->call_id = ++conn->channels[chan].call_counter; |
| 300 | conn->channels[chan].call_id = call->call_id; |
| 301 | rcu_assign_pointer(conn->channels[chan].call, call); |
| 302 | |
| 303 | _net("CONNECT call %d on conn %d", call->debug_id, conn->debug_id); |
| 304 | |
| 305 | spin_unlock(&conn->channel_lock); |
| 306 | rxrpc_put_peer(cp->peer); |
| 307 | cp->peer = NULL; |
| 308 | _leave(" = %p {u=%d}", conn, atomic_read(&conn->usage)); |
| 309 | return 0; |
| 310 | |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 311 | /* We found a potentially suitable connection already in existence. If |
| 312 | * we can reuse it (ie. its usage count hasn't been reduced to 0 by the |
| 313 | * reaper), discard any candidate we may have allocated, and try to get |
| 314 | * a channel on this one, otherwise we have to replace it. |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 315 | */ |
| 316 | found_extant_conn: |
| 317 | _debug("found conn"); |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 318 | if (!rxrpc_get_connection_maybe(conn)) { |
| 319 | set_bit(RXRPC_CONN_IN_CLIENT_CONNS, &candidate->flags); |
| 320 | rb_replace_node(&conn->client_node, |
| 321 | &candidate->client_node, |
| 322 | &local->client_conns); |
| 323 | clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags); |
| 324 | goto attached; |
| 325 | } |
| 326 | |
David Howells | c6d2b8d | 2016-04-04 14:00:40 +0100 | [diff] [blame] | 327 | spin_unlock(&local->client_conns_lock); |
| 328 | |
| 329 | rxrpc_put_connection(candidate); |
| 330 | |
| 331 | if (!atomic_add_unless(&conn->avail_chans, -1, 0)) { |
| 332 | if (!gfpflags_allow_blocking(gfp)) { |
| 333 | rxrpc_put_connection(conn); |
| 334 | _leave(" = -EAGAIN"); |
| 335 | return -EAGAIN; |
| 336 | } |
| 337 | |
| 338 | add_wait_queue(&conn->channel_wq, &myself); |
| 339 | for (;;) { |
| 340 | set_current_state(TASK_INTERRUPTIBLE); |
| 341 | if (atomic_add_unless(&conn->avail_chans, -1, 0)) |
| 342 | break; |
| 343 | if (signal_pending(current)) |
| 344 | goto interrupted; |
| 345 | schedule(); |
| 346 | } |
| 347 | remove_wait_queue(&conn->channel_wq, &myself); |
| 348 | __set_current_state(TASK_RUNNING); |
| 349 | } |
| 350 | |
| 351 | /* The connection allegedly now has a free channel and we can now |
| 352 | * attach the call to it. |
| 353 | */ |
| 354 | spin_lock(&conn->channel_lock); |
| 355 | |
| 356 | for (chan = 0; chan < RXRPC_MAXCALLS; chan++) |
| 357 | if (!conn->channels[chan].call) |
| 358 | goto found_channel; |
| 359 | BUG(); |
| 360 | |
| 361 | interrupted: |
| 362 | remove_wait_queue(&conn->channel_wq, &myself); |
| 363 | __set_current_state(TASK_RUNNING); |
| 364 | rxrpc_put_connection(conn); |
| 365 | rxrpc_put_peer(cp->peer); |
| 366 | cp->peer = NULL; |
| 367 | _leave(" = -ERESTARTSYS"); |
| 368 | return -ERESTARTSYS; |
| 369 | } |
David Howells | 001c112 | 2016-06-30 10:45:22 +0100 | [diff] [blame^] | 370 | |
| 371 | /* |
| 372 | * Remove a client connection from the local endpoint's tree, thereby removing |
| 373 | * it as a target for reuse for new client calls. |
| 374 | */ |
| 375 | void rxrpc_unpublish_client_conn(struct rxrpc_connection *conn) |
| 376 | { |
| 377 | struct rxrpc_local *local = conn->params.local; |
| 378 | |
| 379 | spin_lock(&local->client_conns_lock); |
| 380 | if (test_and_clear_bit(RXRPC_CONN_IN_CLIENT_CONNS, &conn->flags)) |
| 381 | rb_erase(&conn->client_node, &local->client_conns); |
| 382 | spin_unlock(&local->client_conns_lock); |
| 383 | |
| 384 | rxrpc_put_client_connection_id(conn); |
| 385 | } |