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"
21 * Time after last use at which transport record is cleaned up.
23 unsigned rxrpc_transport_expiry
= 3600 * 24;
25 static void rxrpc_transport_reaper(struct work_struct
*work
);
27 static LIST_HEAD(rxrpc_transports
);
28 static DEFINE_RWLOCK(rxrpc_transport_lock
);
29 static DECLARE_DELAYED_WORK(rxrpc_transport_reap
, rxrpc_transport_reaper
);
32 * allocate a new transport session manager
34 static struct rxrpc_transport
*rxrpc_alloc_transport(struct rxrpc_local
*local
,
35 struct rxrpc_peer
*peer
,
38 struct rxrpc_transport
*trans
;
42 trans
= kzalloc(sizeof(struct rxrpc_transport
), gfp
);
46 INIT_LIST_HEAD(&trans
->link
);
47 trans
->bundles
= RB_ROOT
;
48 trans
->client_conns
= RB_ROOT
;
49 trans
->server_conns
= RB_ROOT
;
50 skb_queue_head_init(&trans
->error_queue
);
51 spin_lock_init(&trans
->client_lock
);
52 rwlock_init(&trans
->conn_lock
);
53 atomic_set(&trans
->usage
, 1);
54 trans
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
56 if (peer
->srx
.transport
.family
== AF_INET
) {
57 switch (peer
->srx
.transport_type
) {
59 INIT_WORK(&trans
->error_handler
,
60 rxrpc_UDP_error_handler
);
71 _leave(" = %p", trans
);
76 * obtain a transport session for the nominated endpoints
78 struct rxrpc_transport
*rxrpc_get_transport(struct rxrpc_local
*local
,
79 struct rxrpc_peer
*peer
,
82 struct rxrpc_transport
*trans
, *candidate
;
83 const char *new = "old";
86 _enter("{%pI4+%hu},{%pI4+%hu},",
87 &local
->srx
.transport
.sin
.sin_addr
,
88 ntohs(local
->srx
.transport
.sin
.sin_port
),
89 &peer
->srx
.transport
.sin
.sin_addr
,
90 ntohs(peer
->srx
.transport
.sin
.sin_port
));
92 /* search the transport list first */
93 read_lock_bh(&rxrpc_transport_lock
);
94 list_for_each_entry(trans
, &rxrpc_transports
, link
) {
95 if (trans
->local
== local
&& trans
->peer
== peer
)
96 goto found_extant_transport
;
98 read_unlock_bh(&rxrpc_transport_lock
);
100 /* not yet present - create a candidate for a new record and then
102 candidate
= rxrpc_alloc_transport(local
, peer
, gfp
);
104 _leave(" = -ENOMEM");
105 return ERR_PTR(-ENOMEM
);
108 write_lock_bh(&rxrpc_transport_lock
);
110 list_for_each_entry(trans
, &rxrpc_transports
, link
) {
111 if (trans
->local
== local
&& trans
->peer
== peer
)
112 goto found_extant_second
;
115 /* we can now add the new candidate to the list */
118 usage
= atomic_read(&trans
->usage
);
120 rxrpc_get_local(trans
->local
);
121 atomic_inc(&trans
->peer
->usage
);
122 list_add_tail(&trans
->link
, &rxrpc_transports
);
123 write_unlock_bh(&rxrpc_transport_lock
);
127 _net("TRANSPORT %s %d local %d -> peer %d",
130 trans
->local
->debug_id
,
131 trans
->peer
->debug_id
);
133 _leave(" = %p {u=%d}", trans
, usage
);
136 /* we found the transport in the list immediately */
137 found_extant_transport
:
138 usage
= atomic_inc_return(&trans
->usage
);
139 read_unlock_bh(&rxrpc_transport_lock
);
142 /* we found the transport on the second time through the list */
144 usage
= atomic_inc_return(&trans
->usage
);
145 write_unlock_bh(&rxrpc_transport_lock
);
151 * find the transport connecting two endpoints
153 struct rxrpc_transport
*rxrpc_find_transport(struct rxrpc_local
*local
,
154 struct rxrpc_peer
*peer
)
156 struct rxrpc_transport
*trans
;
158 _enter("{%pI4+%hu},{%pI4+%hu},",
159 &local
->srx
.transport
.sin
.sin_addr
,
160 ntohs(local
->srx
.transport
.sin
.sin_port
),
161 &peer
->srx
.transport
.sin
.sin_addr
,
162 ntohs(peer
->srx
.transport
.sin
.sin_port
));
164 /* search the transport list */
165 read_lock_bh(&rxrpc_transport_lock
);
167 list_for_each_entry(trans
, &rxrpc_transports
, link
) {
168 if (trans
->local
== local
&& trans
->peer
== peer
)
169 goto found_extant_transport
;
172 read_unlock_bh(&rxrpc_transport_lock
);
176 found_extant_transport
:
177 atomic_inc(&trans
->usage
);
178 read_unlock_bh(&rxrpc_transport_lock
);
179 _leave(" = %p", trans
);
184 * release a transport session
186 void rxrpc_put_transport(struct rxrpc_transport
*trans
)
188 _enter("%p{u=%d}", trans
, atomic_read(&trans
->usage
));
190 ASSERTCMP(atomic_read(&trans
->usage
), >, 0);
192 trans
->put_time
= ktime_get_seconds();
193 if (unlikely(atomic_dec_and_test(&trans
->usage
))) {
195 /* let the reaper determine the timeout to avoid a race with
196 * overextending the timeout if the reaper is running at the
198 rxrpc_queue_delayed_work(&rxrpc_transport_reap
, 0);
204 * clean up a transport session
206 static void rxrpc_cleanup_transport(struct rxrpc_transport
*trans
)
208 _net("DESTROY TRANS %d", trans
->debug_id
);
210 rxrpc_purge_queue(&trans
->error_queue
);
212 rxrpc_put_local(trans
->local
);
213 rxrpc_put_peer(trans
->peer
);
218 * reap dead transports that have passed their expiry date
220 static void rxrpc_transport_reaper(struct work_struct
*work
)
222 struct rxrpc_transport
*trans
, *_p
;
223 unsigned long now
, earliest
, reap_time
;
225 LIST_HEAD(graveyard
);
229 now
= ktime_get_seconds();
230 earliest
= ULONG_MAX
;
232 /* extract all the transports that have been dead too long */
233 write_lock_bh(&rxrpc_transport_lock
);
234 list_for_each_entry_safe(trans
, _p
, &rxrpc_transports
, link
) {
235 _debug("reap TRANS %d { u=%d t=%ld }",
236 trans
->debug_id
, atomic_read(&trans
->usage
),
237 (long) now
- (long) trans
->put_time
);
239 if (likely(atomic_read(&trans
->usage
) > 0))
242 reap_time
= trans
->put_time
+ rxrpc_transport_expiry
;
243 if (reap_time
<= now
)
244 list_move_tail(&trans
->link
, &graveyard
);
245 else if (reap_time
< earliest
)
246 earliest
= reap_time
;
248 write_unlock_bh(&rxrpc_transport_lock
);
250 if (earliest
!= ULONG_MAX
) {
251 _debug("reschedule reaper %ld", (long) earliest
- now
);
252 ASSERTCMP(earliest
, >, now
);
253 rxrpc_queue_delayed_work(&rxrpc_transport_reap
,
254 (earliest
- now
) * HZ
);
257 /* then destroy all those pulled out */
258 while (!list_empty(&graveyard
)) {
259 trans
= list_entry(graveyard
.next
, struct rxrpc_transport
,
261 list_del_init(&trans
->link
);
263 ASSERTCMP(atomic_read(&trans
->usage
), ==, 0);
264 rxrpc_cleanup_transport(trans
);
271 * preemptively destroy all the transport session records rather than waiting
272 * for them to time out
274 void __exit
rxrpc_destroy_all_transports(void)
278 rxrpc_transport_expiry
= 0;
279 cancel_delayed_work(&rxrpc_transport_reap
);
280 rxrpc_queue_delayed_work(&rxrpc_transport_reap
, 0);