1 /* RxRPC point-to-point transport session management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
20 static void rxrpc_transport_reaper(struct work_struct *work);
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);
28 * allocate a new transport session manager
30 static struct rxrpc_transport *rxrpc_alloc_transport(struct rxrpc_local *local,
31 struct rxrpc_peer *peer,
34 struct rxrpc_transport *trans;
38 trans = kzalloc(sizeof(struct rxrpc_transport), gfp);
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);
52 if (peer->srx.transport.family == AF_INET) {
53 switch (peer->srx.transport_type) {
55 INIT_WORK(&trans->error_handler,
56 rxrpc_UDP_error_handler);
67 _leave(" = %p", trans);
72 * obtain a transport session for the nominated endpoints
74 struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *local,
75 struct rxrpc_peer *peer,
78 struct rxrpc_transport *trans, *candidate;
79 const char *new = "old";
82 _enter("{%pI4+%hu},{%pI4+%hu},",
83 &local->srx.transport.sin.sin_addr,
84 ntohs(local->srx.transport.sin.sin_port),
85 &peer->srx.transport.sin.sin_addr,
86 ntohs(peer->srx.transport.sin.sin_port));
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;
94 read_unlock_bh(&rxrpc_transport_lock);
96 /* not yet present - create a candidate for a new record and then
98 candidate = rxrpc_alloc_transport(local, peer, gfp);
100 _leave(" = -ENOMEM");
101 return ERR_PTR(-ENOMEM);
104 write_lock_bh(&rxrpc_transport_lock);
106 list_for_each_entry(trans, &rxrpc_transports, link) {
107 if (trans->local == local && trans->peer == peer)
108 goto found_extant_second;
111 /* we can now add the new candidate to the list */
114 usage = atomic_read(&trans->usage);
116 rxrpc_get_local(trans->local);
117 atomic_inc(&trans->peer->usage);
118 list_add_tail(&trans->link, &rxrpc_transports);
119 write_unlock_bh(&rxrpc_transport_lock);
123 _net("TRANSPORT %s %d local %d -> peer %d",
126 trans->local->debug_id,
127 trans->peer->debug_id);
129 _leave(" = %p {u=%d}", trans, usage);
132 /* we found the transport in the list immediately */
133 found_extant_transport:
134 usage = atomic_inc_return(&trans->usage);
135 read_unlock_bh(&rxrpc_transport_lock);
138 /* we found the transport on the second time through the list */
140 usage = atomic_inc_return(&trans->usage);
141 write_unlock_bh(&rxrpc_transport_lock);
147 * find the transport connecting two endpoints
149 struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *local,
150 struct rxrpc_peer *peer)
152 struct rxrpc_transport *trans;
154 _enter("{%pI4+%hu},{%pI4+%hu},",
155 &local->srx.transport.sin.sin_addr,
156 ntohs(local->srx.transport.sin.sin_port),
157 &peer->srx.transport.sin.sin_addr,
158 ntohs(peer->srx.transport.sin.sin_port));
160 /* search the transport list */
161 read_lock_bh(&rxrpc_transport_lock);
163 list_for_each_entry(trans, &rxrpc_transports, link) {
164 if (trans->local == local && trans->peer == peer)
165 goto found_extant_transport;
168 read_unlock_bh(&rxrpc_transport_lock);
172 found_extant_transport:
173 atomic_inc(&trans->usage);
174 read_unlock_bh(&rxrpc_transport_lock);
175 _leave(" = %p", trans);
180 * release a transport session
182 void rxrpc_put_transport(struct rxrpc_transport *trans)
184 _enter("%p{u=%d}", trans, atomic_read(&trans->usage));
186 ASSERTCMP(atomic_read(&trans->usage), >, 0);
188 trans->put_time = get_seconds();
189 if (unlikely(atomic_dec_and_test(&trans->usage))) {
191 /* let the reaper determine the timeout to avoid a race with
192 * overextending the timeout if the reaper is running at the
194 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
200 * clean up a transport session
202 static void rxrpc_cleanup_transport(struct rxrpc_transport *trans)
204 _net("DESTROY TRANS %d", trans->debug_id);
206 rxrpc_purge_queue(&trans->error_queue);
208 rxrpc_put_local(trans->local);
209 rxrpc_put_peer(trans->peer);
214 * reap dead transports that have passed their expiry date
216 static void rxrpc_transport_reaper(struct work_struct *work)
218 struct rxrpc_transport *trans, *_p;
219 unsigned long now, earliest, reap_time;
221 LIST_HEAD(graveyard);
226 earliest = ULONG_MAX;
228 /* extract all the transports that have been dead too long */
229 write_lock_bh(&rxrpc_transport_lock);
230 list_for_each_entry_safe(trans, _p, &rxrpc_transports, link) {
231 _debug("reap TRANS %d { u=%d t=%ld }",
232 trans->debug_id, atomic_read(&trans->usage),
233 (long) now - (long) trans->put_time);
235 if (likely(atomic_read(&trans->usage) > 0))
238 reap_time = trans->put_time + rxrpc_transport_timeout;
239 if (reap_time <= now)
240 list_move_tail(&trans->link, &graveyard);
241 else if (reap_time < earliest)
242 earliest = reap_time;
244 write_unlock_bh(&rxrpc_transport_lock);
246 if (earliest != ULONG_MAX) {
247 _debug("reschedule reaper %ld", (long) earliest - now);
248 ASSERTCMP(earliest, >, now);
249 rxrpc_queue_delayed_work(&rxrpc_transport_reap,
250 (earliest - now) * HZ);
253 /* then destroy all those pulled out */
254 while (!list_empty(&graveyard)) {
255 trans = list_entry(graveyard.next, struct rxrpc_transport,
257 list_del_init(&trans->link);
259 ASSERTCMP(atomic_read(&trans->usage), ==, 0);
260 rxrpc_cleanup_transport(trans);
267 * preemptively destroy all the transport session records rather than waiting
268 * for them to time out
270 void __exit rxrpc_destroy_all_transports(void)
274 rxrpc_transport_timeout = 0;
275 cancel_delayed_work(&rxrpc_transport_reap);
276 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);