David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* RxRPC point-to-point transport session management |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/net.h> |
| 14 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 16 | #include <net/sock.h> |
| 17 | #include <net/af_rxrpc.h> |
| 18 | #include "ar-internal.h" |
| 19 | |
| 20 | static void rxrpc_transport_reaper(struct work_struct *work); |
| 21 | |
| 22 | static LIST_HEAD(rxrpc_transports); |
| 23 | static DEFINE_RWLOCK(rxrpc_transport_lock); |
| 24 | static unsigned long rxrpc_transport_timeout = 3600 * 24; |
| 25 | static DECLARE_DELAYED_WORK(rxrpc_transport_reap, rxrpc_transport_reaper); |
| 26 | |
| 27 | /* |
| 28 | * allocate a new transport session manager |
| 29 | */ |
| 30 | static struct rxrpc_transport *rxrpc_alloc_transport(struct rxrpc_local *local, |
| 31 | struct rxrpc_peer *peer, |
| 32 | gfp_t gfp) |
| 33 | { |
| 34 | struct rxrpc_transport *trans; |
| 35 | |
| 36 | _enter(""); |
| 37 | |
| 38 | trans = kzalloc(sizeof(struct rxrpc_transport), gfp); |
| 39 | if (trans) { |
| 40 | trans->local = local; |
| 41 | trans->peer = peer; |
| 42 | INIT_LIST_HEAD(&trans->link); |
| 43 | trans->bundles = RB_ROOT; |
| 44 | trans->client_conns = RB_ROOT; |
| 45 | trans->server_conns = RB_ROOT; |
| 46 | skb_queue_head_init(&trans->error_queue); |
| 47 | spin_lock_init(&trans->client_lock); |
| 48 | rwlock_init(&trans->conn_lock); |
| 49 | atomic_set(&trans->usage, 1); |
| 50 | trans->debug_id = atomic_inc_return(&rxrpc_debug_id); |
| 51 | |
| 52 | if (peer->srx.transport.family == AF_INET) { |
| 53 | switch (peer->srx.transport_type) { |
| 54 | case SOCK_DGRAM: |
| 55 | INIT_WORK(&trans->error_handler, |
| 56 | rxrpc_UDP_error_handler); |
| 57 | break; |
| 58 | default: |
| 59 | BUG(); |
| 60 | break; |
| 61 | } |
| 62 | } else { |
| 63 | BUG(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | _leave(" = %p", trans); |
| 68 | return trans; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * obtain a transport session for the nominated endpoints |
| 73 | */ |
| 74 | struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *local, |
| 75 | struct rxrpc_peer *peer, |
| 76 | gfp_t gfp) |
| 77 | { |
| 78 | struct rxrpc_transport *trans, *candidate; |
| 79 | const char *new = "old"; |
| 80 | int usage; |
| 81 | |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 82 | _enter("{%pI4+%hu},{%pI4+%hu},", |
| 83 | &local->srx.transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 84 | ntohs(local->srx.transport.sin.sin_port), |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 85 | &peer->srx.transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 86 | ntohs(peer->srx.transport.sin.sin_port)); |
| 87 | |
| 88 | /* search the transport list first */ |
| 89 | read_lock_bh(&rxrpc_transport_lock); |
| 90 | list_for_each_entry(trans, &rxrpc_transports, link) { |
| 91 | if (trans->local == local && trans->peer == peer) |
| 92 | goto found_extant_transport; |
| 93 | } |
| 94 | read_unlock_bh(&rxrpc_transport_lock); |
| 95 | |
| 96 | /* not yet present - create a candidate for a new record and then |
| 97 | * redo the search */ |
| 98 | candidate = rxrpc_alloc_transport(local, peer, gfp); |
| 99 | if (!candidate) { |
| 100 | _leave(" = -ENOMEM"); |
| 101 | return ERR_PTR(-ENOMEM); |
| 102 | } |
| 103 | |
| 104 | write_lock_bh(&rxrpc_transport_lock); |
| 105 | |
| 106 | list_for_each_entry(trans, &rxrpc_transports, link) { |
| 107 | if (trans->local == local && trans->peer == peer) |
| 108 | goto found_extant_second; |
| 109 | } |
| 110 | |
| 111 | /* we can now add the new candidate to the list */ |
| 112 | trans = candidate; |
| 113 | candidate = NULL; |
| 114 | |
| 115 | rxrpc_get_local(trans->local); |
| 116 | atomic_inc(&trans->peer->usage); |
| 117 | list_add_tail(&trans->link, &rxrpc_transports); |
| 118 | write_unlock_bh(&rxrpc_transport_lock); |
| 119 | new = "new"; |
| 120 | |
| 121 | success: |
| 122 | _net("TRANSPORT %s %d local %d -> peer %d", |
| 123 | new, |
| 124 | trans->debug_id, |
| 125 | trans->local->debug_id, |
| 126 | trans->peer->debug_id); |
| 127 | |
| 128 | _leave(" = %p {u=%d}", trans, atomic_read(&trans->usage)); |
| 129 | return trans; |
| 130 | |
| 131 | /* we found the transport in the list immediately */ |
| 132 | found_extant_transport: |
| 133 | usage = atomic_inc_return(&trans->usage); |
| 134 | read_unlock_bh(&rxrpc_transport_lock); |
| 135 | goto success; |
| 136 | |
| 137 | /* we found the transport on the second time through the list */ |
| 138 | found_extant_second: |
| 139 | usage = atomic_inc_return(&trans->usage); |
| 140 | write_unlock_bh(&rxrpc_transport_lock); |
| 141 | kfree(candidate); |
| 142 | goto success; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * find the transport connecting two endpoints |
| 147 | */ |
| 148 | struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *local, |
| 149 | struct rxrpc_peer *peer) |
| 150 | { |
| 151 | struct rxrpc_transport *trans; |
| 152 | |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 153 | _enter("{%pI4+%hu},{%pI4+%hu},", |
| 154 | &local->srx.transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 155 | ntohs(local->srx.transport.sin.sin_port), |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 156 | &peer->srx.transport.sin.sin_addr, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 157 | ntohs(peer->srx.transport.sin.sin_port)); |
| 158 | |
| 159 | /* search the transport list */ |
| 160 | read_lock_bh(&rxrpc_transport_lock); |
| 161 | |
| 162 | list_for_each_entry(trans, &rxrpc_transports, link) { |
| 163 | if (trans->local == local && trans->peer == peer) |
| 164 | goto found_extant_transport; |
| 165 | } |
| 166 | |
| 167 | read_unlock_bh(&rxrpc_transport_lock); |
| 168 | _leave(" = NULL"); |
| 169 | return NULL; |
| 170 | |
| 171 | found_extant_transport: |
| 172 | atomic_inc(&trans->usage); |
| 173 | read_unlock_bh(&rxrpc_transport_lock); |
| 174 | _leave(" = %p", trans); |
| 175 | return trans; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * release a transport session |
| 180 | */ |
| 181 | void rxrpc_put_transport(struct rxrpc_transport *trans) |
| 182 | { |
| 183 | _enter("%p{u=%d}", trans, atomic_read(&trans->usage)); |
| 184 | |
| 185 | ASSERTCMP(atomic_read(&trans->usage), >, 0); |
| 186 | |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 187 | trans->put_time = get_seconds(); |
Ilpo Järvinen | 50aab54 | 2008-05-02 16:20:10 -0700 | [diff] [blame] | 188 | if (unlikely(atomic_dec_and_test(&trans->usage))) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 189 | _debug("zombie"); |
| 190 | /* let the reaper determine the timeout to avoid a race with |
| 191 | * overextending the timeout if the reaper is running at the |
| 192 | * same time */ |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 193 | rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0); |
Ilpo Järvinen | 50aab54 | 2008-05-02 16:20:10 -0700 | [diff] [blame] | 194 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 195 | _leave(""); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * clean up a transport session |
| 200 | */ |
| 201 | static void rxrpc_cleanup_transport(struct rxrpc_transport *trans) |
| 202 | { |
| 203 | _net("DESTROY TRANS %d", trans->debug_id); |
| 204 | |
| 205 | rxrpc_purge_queue(&trans->error_queue); |
| 206 | |
| 207 | rxrpc_put_local(trans->local); |
| 208 | rxrpc_put_peer(trans->peer); |
| 209 | kfree(trans); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * reap dead transports that have passed their expiry date |
| 214 | */ |
| 215 | static void rxrpc_transport_reaper(struct work_struct *work) |
| 216 | { |
| 217 | struct rxrpc_transport *trans, *_p; |
| 218 | unsigned long now, earliest, reap_time; |
| 219 | |
| 220 | LIST_HEAD(graveyard); |
| 221 | |
| 222 | _enter(""); |
| 223 | |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 224 | now = get_seconds(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 225 | earliest = ULONG_MAX; |
| 226 | |
| 227 | /* extract all the transports that have been dead too long */ |
| 228 | write_lock_bh(&rxrpc_transport_lock); |
| 229 | list_for_each_entry_safe(trans, _p, &rxrpc_transports, link) { |
| 230 | _debug("reap TRANS %d { u=%d t=%ld }", |
| 231 | trans->debug_id, atomic_read(&trans->usage), |
| 232 | (long) now - (long) trans->put_time); |
| 233 | |
| 234 | if (likely(atomic_read(&trans->usage) > 0)) |
| 235 | continue; |
| 236 | |
| 237 | reap_time = trans->put_time + rxrpc_transport_timeout; |
| 238 | if (reap_time <= now) |
| 239 | list_move_tail(&trans->link, &graveyard); |
| 240 | else if (reap_time < earliest) |
| 241 | earliest = reap_time; |
| 242 | } |
| 243 | write_unlock_bh(&rxrpc_transport_lock); |
| 244 | |
| 245 | if (earliest != ULONG_MAX) { |
| 246 | _debug("reschedule reaper %ld", (long) earliest - now); |
| 247 | ASSERTCMP(earliest, >, now); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 248 | rxrpc_queue_delayed_work(&rxrpc_transport_reap, |
| 249 | (earliest - now) * HZ); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* then destroy all those pulled out */ |
| 253 | while (!list_empty(&graveyard)) { |
| 254 | trans = list_entry(graveyard.next, struct rxrpc_transport, |
| 255 | link); |
| 256 | list_del_init(&trans->link); |
| 257 | |
| 258 | ASSERTCMP(atomic_read(&trans->usage), ==, 0); |
| 259 | rxrpc_cleanup_transport(trans); |
| 260 | } |
| 261 | |
| 262 | _leave(""); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * preemptively destroy all the transport session records rather than waiting |
| 267 | * for them to time out |
| 268 | */ |
| 269 | void __exit rxrpc_destroy_all_transports(void) |
| 270 | { |
| 271 | _enter(""); |
| 272 | |
| 273 | rxrpc_transport_timeout = 0; |
| 274 | cancel_delayed_work(&rxrpc_transport_reap); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 275 | rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 276 | |
| 277 | _leave(""); |
| 278 | } |