rxrpc: Use the peer record to distribute network errors
[deliverable/linux.git] / net / rxrpc / transport.c
CommitLineData
17926a79
DH
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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
14#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
5a0e3ad6 17#include <linux/slab.h>
17926a79
DH
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
5873c083
DH
22/*
23 * Time after last use at which transport record is cleaned up.
24 */
dad8aff7 25unsigned int rxrpc_transport_expiry = 3600 * 24;
5873c083 26
17926a79
DH
27static void rxrpc_transport_reaper(struct work_struct *work);
28
29static LIST_HEAD(rxrpc_transports);
30static DEFINE_RWLOCK(rxrpc_transport_lock);
17926a79
DH
31static DECLARE_DELAYED_WORK(rxrpc_transport_reap, rxrpc_transport_reaper);
32
33/*
34 * allocate a new transport session manager
35 */
36static struct rxrpc_transport *rxrpc_alloc_transport(struct rxrpc_local *local,
37 struct rxrpc_peer *peer,
38 gfp_t gfp)
39{
40 struct rxrpc_transport *trans;
41
42 _enter("");
43
44 trans = kzalloc(sizeof(struct rxrpc_transport), gfp);
45 if (trans) {
46 trans->local = local;
47 trans->peer = peer;
48 INIT_LIST_HEAD(&trans->link);
49 trans->bundles = RB_ROOT;
50 trans->client_conns = RB_ROOT;
51 trans->server_conns = RB_ROOT;
17926a79
DH
52 spin_lock_init(&trans->client_lock);
53 rwlock_init(&trans->conn_lock);
54 atomic_set(&trans->usage, 1);
0d12f8a4 55 trans->conn_idcounter = peer->srx.srx_service << 16;
17926a79 56 trans->debug_id = atomic_inc_return(&rxrpc_debug_id);
17926a79
DH
57 }
58
59 _leave(" = %p", trans);
60 return trans;
61}
62
63/*
64 * obtain a transport session for the nominated endpoints
65 */
66struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *local,
67 struct rxrpc_peer *peer,
68 gfp_t gfp)
69{
70 struct rxrpc_transport *trans, *candidate;
71 const char *new = "old";
72 int usage;
73
21454aaa
HH
74 _enter("{%pI4+%hu},{%pI4+%hu},",
75 &local->srx.transport.sin.sin_addr,
17926a79 76 ntohs(local->srx.transport.sin.sin_port),
21454aaa 77 &peer->srx.transport.sin.sin_addr,
17926a79
DH
78 ntohs(peer->srx.transport.sin.sin_port));
79
80 /* search the transport list first */
81 read_lock_bh(&rxrpc_transport_lock);
82 list_for_each_entry(trans, &rxrpc_transports, link) {
83 if (trans->local == local && trans->peer == peer)
84 goto found_extant_transport;
85 }
86 read_unlock_bh(&rxrpc_transport_lock);
87
88 /* not yet present - create a candidate for a new record and then
89 * redo the search */
90 candidate = rxrpc_alloc_transport(local, peer, gfp);
91 if (!candidate) {
92 _leave(" = -ENOMEM");
93 return ERR_PTR(-ENOMEM);
94 }
95
96 write_lock_bh(&rxrpc_transport_lock);
97
98 list_for_each_entry(trans, &rxrpc_transports, link) {
99 if (trans->local == local && trans->peer == peer)
100 goto found_extant_second;
101 }
102
103 /* we can now add the new candidate to the list */
104 trans = candidate;
105 candidate = NULL;
ed84cadb 106 usage = atomic_read(&trans->usage);
17926a79
DH
107
108 rxrpc_get_local(trans->local);
be6e6707 109 rxrpc_get_peer(trans->peer);
17926a79
DH
110 list_add_tail(&trans->link, &rxrpc_transports);
111 write_unlock_bh(&rxrpc_transport_lock);
112 new = "new";
113
114success:
115 _net("TRANSPORT %s %d local %d -> peer %d",
116 new,
117 trans->debug_id,
118 trans->local->debug_id,
119 trans->peer->debug_id);
120
ed84cadb 121 _leave(" = %p {u=%d}", trans, usage);
17926a79
DH
122 return trans;
123
124 /* we found the transport in the list immediately */
125found_extant_transport:
126 usage = atomic_inc_return(&trans->usage);
127 read_unlock_bh(&rxrpc_transport_lock);
128 goto success;
129
130 /* we found the transport on the second time through the list */
131found_extant_second:
132 usage = atomic_inc_return(&trans->usage);
133 write_unlock_bh(&rxrpc_transport_lock);
134 kfree(candidate);
135 goto success;
136}
137
138/*
139 * find the transport connecting two endpoints
140 */
141struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *local,
142 struct rxrpc_peer *peer)
143{
144 struct rxrpc_transport *trans;
145
21454aaa
HH
146 _enter("{%pI4+%hu},{%pI4+%hu},",
147 &local->srx.transport.sin.sin_addr,
17926a79 148 ntohs(local->srx.transport.sin.sin_port),
21454aaa 149 &peer->srx.transport.sin.sin_addr,
17926a79
DH
150 ntohs(peer->srx.transport.sin.sin_port));
151
152 /* search the transport list */
153 read_lock_bh(&rxrpc_transport_lock);
154
155 list_for_each_entry(trans, &rxrpc_transports, link) {
156 if (trans->local == local && trans->peer == peer)
157 goto found_extant_transport;
158 }
159
160 read_unlock_bh(&rxrpc_transport_lock);
161 _leave(" = NULL");
162 return NULL;
163
164found_extant_transport:
165 atomic_inc(&trans->usage);
166 read_unlock_bh(&rxrpc_transport_lock);
167 _leave(" = %p", trans);
168 return trans;
169}
170
171/*
172 * release a transport session
173 */
174void rxrpc_put_transport(struct rxrpc_transport *trans)
175{
176 _enter("%p{u=%d}", trans, atomic_read(&trans->usage));
177
178 ASSERTCMP(atomic_read(&trans->usage), >, 0);
179
22a3f9a2 180 trans->put_time = ktime_get_seconds();
50aab54f 181 if (unlikely(atomic_dec_and_test(&trans->usage))) {
17926a79
DH
182 _debug("zombie");
183 /* let the reaper determine the timeout to avoid a race with
184 * overextending the timeout if the reaper is running at the
185 * same time */
651350d1 186 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
50aab54f 187 }
17926a79
DH
188 _leave("");
189}
190
191/*
192 * clean up a transport session
193 */
194static void rxrpc_cleanup_transport(struct rxrpc_transport *trans)
195{
196 _net("DESTROY TRANS %d", trans->debug_id);
197
17926a79
DH
198 rxrpc_put_local(trans->local);
199 rxrpc_put_peer(trans->peer);
200 kfree(trans);
201}
202
203/*
204 * reap dead transports that have passed their expiry date
205 */
206static void rxrpc_transport_reaper(struct work_struct *work)
207{
208 struct rxrpc_transport *trans, *_p;
209 unsigned long now, earliest, reap_time;
210
211 LIST_HEAD(graveyard);
212
213 _enter("");
214
22a3f9a2 215 now = ktime_get_seconds();
17926a79
DH
216 earliest = ULONG_MAX;
217
218 /* extract all the transports that have been dead too long */
219 write_lock_bh(&rxrpc_transport_lock);
220 list_for_each_entry_safe(trans, _p, &rxrpc_transports, link) {
221 _debug("reap TRANS %d { u=%d t=%ld }",
222 trans->debug_id, atomic_read(&trans->usage),
223 (long) now - (long) trans->put_time);
224
225 if (likely(atomic_read(&trans->usage) > 0))
226 continue;
227
5873c083 228 reap_time = trans->put_time + rxrpc_transport_expiry;
17926a79
DH
229 if (reap_time <= now)
230 list_move_tail(&trans->link, &graveyard);
231 else if (reap_time < earliest)
232 earliest = reap_time;
233 }
234 write_unlock_bh(&rxrpc_transport_lock);
235
236 if (earliest != ULONG_MAX) {
237 _debug("reschedule reaper %ld", (long) earliest - now);
238 ASSERTCMP(earliest, >, now);
651350d1
DH
239 rxrpc_queue_delayed_work(&rxrpc_transport_reap,
240 (earliest - now) * HZ);
17926a79
DH
241 }
242
243 /* then destroy all those pulled out */
244 while (!list_empty(&graveyard)) {
245 trans = list_entry(graveyard.next, struct rxrpc_transport,
246 link);
247 list_del_init(&trans->link);
248
249 ASSERTCMP(atomic_read(&trans->usage), ==, 0);
250 rxrpc_cleanup_transport(trans);
251 }
252
253 _leave("");
254}
255
256/*
257 * preemptively destroy all the transport session records rather than waiting
258 * for them to time out
259 */
260void __exit rxrpc_destroy_all_transports(void)
261{
262 _enter("");
263
5873c083 264 rxrpc_transport_expiry = 0;
17926a79 265 cancel_delayed_work(&rxrpc_transport_reap);
651350d1 266 rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
17926a79
DH
267
268 _leave("");
269}
This page took 0.651491 seconds and 5 git commands to generate.