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 int 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
->conn_idcounter
= peer
->srx
.srx_service
<< 16;
55 trans
->debug_id
= atomic_inc_return(&rxrpc_debug_id
);
57 if (peer
->srx
.transport
.family
== AF_INET
) {
58 switch (peer
->srx
.transport_type
) {
60 INIT_WORK(&trans
->error_handler
,
61 rxrpc_UDP_error_handler
);
72 _leave(" = %p", trans
);
77 * obtain a transport session for the nominated endpoints
79 struct rxrpc_transport
*rxrpc_get_transport(struct rxrpc_local
*local
,
80 struct rxrpc_peer
*peer
,
83 struct rxrpc_transport
*trans
, *candidate
;
84 const char *new = "old";
87 _enter("{%pI4+%hu},{%pI4+%hu},",
88 &local
->srx
.transport
.sin
.sin_addr
,
89 ntohs(local
->srx
.transport
.sin
.sin_port
),
90 &peer
->srx
.transport
.sin
.sin_addr
,
91 ntohs(peer
->srx
.transport
.sin
.sin_port
));
93 /* search the transport list first */
94 read_lock_bh(&rxrpc_transport_lock
);
95 list_for_each_entry(trans
, &rxrpc_transports
, link
) {
96 if (trans
->local
== local
&& trans
->peer
== peer
)
97 goto found_extant_transport
;
99 read_unlock_bh(&rxrpc_transport_lock
);
101 /* not yet present - create a candidate for a new record and then
103 candidate
= rxrpc_alloc_transport(local
, peer
, gfp
);
105 _leave(" = -ENOMEM");
106 return ERR_PTR(-ENOMEM
);
109 write_lock_bh(&rxrpc_transport_lock
);
111 list_for_each_entry(trans
, &rxrpc_transports
, link
) {
112 if (trans
->local
== local
&& trans
->peer
== peer
)
113 goto found_extant_second
;
116 /* we can now add the new candidate to the list */
119 usage
= atomic_read(&trans
->usage
);
121 rxrpc_get_local(trans
->local
);
122 atomic_inc(&trans
->peer
->usage
);
123 list_add_tail(&trans
->link
, &rxrpc_transports
);
124 write_unlock_bh(&rxrpc_transport_lock
);
128 _net("TRANSPORT %s %d local %d -> peer %d",
131 trans
->local
->debug_id
,
132 trans
->peer
->debug_id
);
134 _leave(" = %p {u=%d}", trans
, usage
);
137 /* we found the transport in the list immediately */
138 found_extant_transport
:
139 usage
= atomic_inc_return(&trans
->usage
);
140 read_unlock_bh(&rxrpc_transport_lock
);
143 /* we found the transport on the second time through the list */
145 usage
= atomic_inc_return(&trans
->usage
);
146 write_unlock_bh(&rxrpc_transport_lock
);
152 * find the transport connecting two endpoints
154 struct rxrpc_transport
*rxrpc_find_transport(struct rxrpc_local
*local
,
155 struct rxrpc_peer
*peer
)
157 struct rxrpc_transport
*trans
;
159 _enter("{%pI4+%hu},{%pI4+%hu},",
160 &local
->srx
.transport
.sin
.sin_addr
,
161 ntohs(local
->srx
.transport
.sin
.sin_port
),
162 &peer
->srx
.transport
.sin
.sin_addr
,
163 ntohs(peer
->srx
.transport
.sin
.sin_port
));
165 /* search the transport list */
166 read_lock_bh(&rxrpc_transport_lock
);
168 list_for_each_entry(trans
, &rxrpc_transports
, link
) {
169 if (trans
->local
== local
&& trans
->peer
== peer
)
170 goto found_extant_transport
;
173 read_unlock_bh(&rxrpc_transport_lock
);
177 found_extant_transport
:
178 atomic_inc(&trans
->usage
);
179 read_unlock_bh(&rxrpc_transport_lock
);
180 _leave(" = %p", trans
);
185 * release a transport session
187 void rxrpc_put_transport(struct rxrpc_transport
*trans
)
189 _enter("%p{u=%d}", trans
, atomic_read(&trans
->usage
));
191 ASSERTCMP(atomic_read(&trans
->usage
), >, 0);
193 trans
->put_time
= ktime_get_seconds();
194 if (unlikely(atomic_dec_and_test(&trans
->usage
))) {
196 /* let the reaper determine the timeout to avoid a race with
197 * overextending the timeout if the reaper is running at the
199 rxrpc_queue_delayed_work(&rxrpc_transport_reap
, 0);
205 * clean up a transport session
207 static void rxrpc_cleanup_transport(struct rxrpc_transport
*trans
)
209 _net("DESTROY TRANS %d", trans
->debug_id
);
211 rxrpc_purge_queue(&trans
->error_queue
);
213 rxrpc_put_local(trans
->local
);
214 rxrpc_put_peer(trans
->peer
);
219 * reap dead transports that have passed their expiry date
221 static void rxrpc_transport_reaper(struct work_struct
*work
)
223 struct rxrpc_transport
*trans
, *_p
;
224 unsigned long now
, earliest
, reap_time
;
226 LIST_HEAD(graveyard
);
230 now
= ktime_get_seconds();
231 earliest
= ULONG_MAX
;
233 /* extract all the transports that have been dead too long */
234 write_lock_bh(&rxrpc_transport_lock
);
235 list_for_each_entry_safe(trans
, _p
, &rxrpc_transports
, link
) {
236 _debug("reap TRANS %d { u=%d t=%ld }",
237 trans
->debug_id
, atomic_read(&trans
->usage
),
238 (long) now
- (long) trans
->put_time
);
240 if (likely(atomic_read(&trans
->usage
) > 0))
243 reap_time
= trans
->put_time
+ rxrpc_transport_expiry
;
244 if (reap_time
<= now
)
245 list_move_tail(&trans
->link
, &graveyard
);
246 else if (reap_time
< earliest
)
247 earliest
= reap_time
;
249 write_unlock_bh(&rxrpc_transport_lock
);
251 if (earliest
!= ULONG_MAX
) {
252 _debug("reschedule reaper %ld", (long) earliest
- now
);
253 ASSERTCMP(earliest
, >, now
);
254 rxrpc_queue_delayed_work(&rxrpc_transport_reap
,
255 (earliest
- now
) * HZ
);
258 /* then destroy all those pulled out */
259 while (!list_empty(&graveyard
)) {
260 trans
= list_entry(graveyard
.next
, struct rxrpc_transport
,
262 list_del_init(&trans
->link
);
264 ASSERTCMP(atomic_read(&trans
->usage
), ==, 0);
265 rxrpc_cleanup_transport(trans
);
272 * preemptively destroy all the transport session records rather than waiting
273 * for them to time out
275 void __exit
rxrpc_destroy_all_transports(void)
279 rxrpc_transport_expiry
= 0;
280 cancel_delayed_work(&rxrpc_transport_reap
);
281 rxrpc_queue_delayed_work(&rxrpc_transport_reap
, 0);