1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC virtual connection handler, common bits.
4 * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include "ar-internal.h"
17 * Time till a connection expires after last use (in seconds).
19 unsigned int __read_mostly rxrpc_connection_expiry = 10 * 60;
20 unsigned int __read_mostly rxrpc_closed_conn_expiry = 10;
22 static void rxrpc_clean_up_connection(struct work_struct *work);
23 static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
24 unsigned long reap_at);
26 static void rxrpc_connection_timer(struct timer_list *timer)
28 struct rxrpc_connection *conn =
29 container_of(timer, struct rxrpc_connection, timer);
31 rxrpc_queue_conn(conn, rxrpc_conn_queue_timer);
35 * allocate a new connection
37 struct rxrpc_connection *rxrpc_alloc_connection(struct rxrpc_net *rxnet,
40 struct rxrpc_connection *conn;
44 conn = kzalloc(sizeof(struct rxrpc_connection), gfp);
46 INIT_LIST_HEAD(&conn->cache_link);
47 timer_setup(&conn->timer, &rxrpc_connection_timer, 0);
48 INIT_WORK(&conn->processor, rxrpc_process_connection);
49 INIT_WORK(&conn->destructor, rxrpc_clean_up_connection);
50 INIT_LIST_HEAD(&conn->proc_link);
51 INIT_LIST_HEAD(&conn->link);
52 skb_queue_head_init(&conn->rx_queue);
54 conn->security = &rxrpc_no_security;
55 spin_lock_init(&conn->state_lock);
56 conn->debug_id = atomic_inc_return(&rxrpc_debug_id);
57 conn->idle_timestamp = jiffies;
60 _leave(" = %p{%d}", conn, conn ? conn->debug_id : 0);
65 * Look up a connection in the cache by protocol parameters.
67 * If successful, a pointer to the connection is returned, but no ref is taken.
68 * NULL is returned if there is no match.
70 * When searching for a service call, if we find a peer but no connection, we
71 * return that through *_peer in case we need to create a new service call.
73 * The caller must be holding the RCU read lock.
75 struct rxrpc_connection *rxrpc_find_connection_rcu(struct rxrpc_local *local,
76 struct sockaddr_rxrpc *srx,
78 struct rxrpc_peer **_peer)
80 struct rxrpc_connection *conn;
81 struct rxrpc_conn_proto k;
82 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
83 struct rxrpc_peer *peer;
85 _enter(",%x", sp->hdr.cid & RXRPC_CIDMASK);
87 k.epoch = sp->hdr.epoch;
88 k.cid = sp->hdr.cid & RXRPC_CIDMASK;
90 if (rxrpc_to_server(sp)) {
91 /* We need to look up service connections by the full protocol
92 * parameter set. We look up the peer first as an intermediate
93 * step and then the connection from the peer's tree.
95 peer = rxrpc_lookup_peer_rcu(local, srx);
99 conn = rxrpc_find_service_conn_rcu(peer, skb);
100 if (!conn || refcount_read(&conn->ref) == 0)
102 _leave(" = %p", conn);
105 /* Look up client connections by connection ID alone as their
106 * IDs are unique for this machine.
108 conn = idr_find(&rxrpc_client_conn_ids, sp->hdr.cid >> RXRPC_CIDSHIFT);
109 if (!conn || refcount_read(&conn->ref) == 0) {
114 if (conn->proto.epoch != k.epoch ||
115 conn->local != local)
119 switch (srx->transport.family) {
121 if (peer->srx.transport.sin.sin_port !=
122 srx->transport.sin.sin_port ||
123 peer->srx.transport.sin.sin_addr.s_addr !=
124 srx->transport.sin.sin_addr.s_addr)
127 #ifdef CONFIG_AF_RXRPC_IPV6
129 if (peer->srx.transport.sin6.sin6_port !=
130 srx->transport.sin6.sin6_port ||
131 memcmp(&peer->srx.transport.sin6.sin6_addr,
132 &srx->transport.sin6.sin6_addr,
133 sizeof(struct in6_addr)) != 0)
141 _leave(" = %p", conn);
151 * Disconnect a call and clear any channel it occupies when that call
152 * terminates. The caller must hold the channel_lock and must release the
153 * call's ref on the connection.
155 void __rxrpc_disconnect_call(struct rxrpc_connection *conn,
156 struct rxrpc_call *call)
158 struct rxrpc_channel *chan =
159 &conn->channels[call->cid & RXRPC_CHANNELMASK];
161 _enter("%d,%x", conn->debug_id, call->cid);
163 if (rcu_access_pointer(chan->call) == call) {
164 /* Save the result of the call so that we can repeat it if necessary
165 * through the channel, whilst disposing of the actual call record.
167 trace_rxrpc_disconnect_call(call);
168 switch (call->completion) {
169 case RXRPC_CALL_SUCCEEDED:
170 chan->last_seq = call->rx_highest_seq;
171 chan->last_type = RXRPC_PACKET_TYPE_ACK;
173 case RXRPC_CALL_LOCALLY_ABORTED:
174 chan->last_abort = call->abort_code;
175 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
178 chan->last_abort = RX_CALL_DEAD;
179 chan->last_type = RXRPC_PACKET_TYPE_ABORT;
183 /* Sync with rxrpc_conn_retransmit(). */
185 chan->last_call = chan->call_id;
186 chan->call_id = chan->call_counter;
188 rcu_assign_pointer(chan->call, NULL);
195 * Disconnect a call and clear any channel it occupies when that call
198 void rxrpc_disconnect_call(struct rxrpc_call *call)
200 struct rxrpc_connection *conn = call->conn;
202 call->peer->cong_ssthresh = call->cong_ssthresh;
204 if (!hlist_unhashed(&call->error_link)) {
205 spin_lock(&call->peer->lock);
206 hlist_del_init(&call->error_link);
207 spin_unlock(&call->peer->lock);
210 if (rxrpc_is_client_call(call))
211 return rxrpc_disconnect_client_call(conn->bundle, call);
213 spin_lock(&conn->bundle->channel_lock);
214 __rxrpc_disconnect_call(conn, call);
215 spin_unlock(&conn->bundle->channel_lock);
217 set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
218 conn->idle_timestamp = jiffies;
219 if (atomic_dec_and_test(&conn->active))
220 rxrpc_set_service_reap_timer(conn->rxnet,
221 jiffies + rxrpc_connection_expiry);
225 * Queue a connection's work processor, getting a ref to pass to the work
228 void rxrpc_queue_conn(struct rxrpc_connection *conn, enum rxrpc_conn_trace why)
230 if (atomic_read(&conn->active) >= 0 &&
231 rxrpc_queue_work(&conn->processor))
232 rxrpc_see_connection(conn, why);
236 * Note the re-emergence of a connection.
238 void rxrpc_see_connection(struct rxrpc_connection *conn,
239 enum rxrpc_conn_trace why)
242 int r = refcount_read(&conn->ref);
244 trace_rxrpc_conn(conn->debug_id, r, why);
249 * Get a ref on a connection.
251 struct rxrpc_connection *rxrpc_get_connection(struct rxrpc_connection *conn,
252 enum rxrpc_conn_trace why)
256 __refcount_inc(&conn->ref, &r);
257 trace_rxrpc_conn(conn->debug_id, r + 1, why);
262 * Try to get a ref on a connection.
264 struct rxrpc_connection *
265 rxrpc_get_connection_maybe(struct rxrpc_connection *conn,
266 enum rxrpc_conn_trace why)
271 if (__refcount_inc_not_zero(&conn->ref, &r))
272 trace_rxrpc_conn(conn->debug_id, r + 1, why);
280 * Set the service connection reap timer.
282 static void rxrpc_set_service_reap_timer(struct rxrpc_net *rxnet,
283 unsigned long reap_at)
286 timer_reduce(&rxnet->service_conn_reap_timer, reap_at);
290 * destroy a virtual connection
292 static void rxrpc_rcu_free_connection(struct rcu_head *rcu)
294 struct rxrpc_connection *conn =
295 container_of(rcu, struct rxrpc_connection, rcu);
296 struct rxrpc_net *rxnet = conn->rxnet;
298 _enter("{%d,u=%d}", conn->debug_id, refcount_read(&conn->ref));
300 trace_rxrpc_conn(conn->debug_id, refcount_read(&conn->ref),
304 if (atomic_dec_and_test(&rxnet->nr_conns))
305 wake_up_var(&rxnet->nr_conns);
309 * Clean up a dead connection.
311 static void rxrpc_clean_up_connection(struct work_struct *work)
313 struct rxrpc_connection *conn =
314 container_of(work, struct rxrpc_connection, destructor);
315 struct rxrpc_net *rxnet = conn->rxnet;
317 ASSERT(!rcu_access_pointer(conn->channels[0].call) &&
318 !rcu_access_pointer(conn->channels[1].call) &&
319 !rcu_access_pointer(conn->channels[2].call) &&
320 !rcu_access_pointer(conn->channels[3].call));
321 ASSERT(list_empty(&conn->cache_link));
323 del_timer_sync(&conn->timer);
324 cancel_work_sync(&conn->processor); /* Processing may restart the timer */
325 del_timer_sync(&conn->timer);
327 write_lock(&rxnet->conn_lock);
328 list_del_init(&conn->proc_link);
329 write_unlock(&rxnet->conn_lock);
331 rxrpc_purge_queue(&conn->rx_queue);
333 rxrpc_kill_client_conn(conn);
335 conn->security->clear(conn);
337 rxrpc_put_bundle(conn->bundle, rxrpc_bundle_put_conn);
338 rxrpc_put_peer(conn->peer, rxrpc_peer_put_conn);
339 rxrpc_put_local(conn->local, rxrpc_local_put_kill_conn);
341 /* Drain the Rx queue. Note that even though we've unpublished, an
342 * incoming packet could still be being added to our Rx queue, so we
343 * will need to drain it again in the RCU cleanup handler.
345 rxrpc_purge_queue(&conn->rx_queue);
347 call_rcu(&conn->rcu, rxrpc_rcu_free_connection);
351 * Drop a ref on a connection.
353 void rxrpc_put_connection(struct rxrpc_connection *conn,
354 enum rxrpc_conn_trace why)
356 unsigned int debug_id;
363 debug_id = conn->debug_id;
364 dead = __refcount_dec_and_test(&conn->ref, &r);
365 trace_rxrpc_conn(debug_id, r - 1, why);
367 del_timer(&conn->timer);
368 cancel_work(&conn->processor);
370 if (in_softirq() || work_busy(&conn->processor) ||
371 timer_pending(&conn->timer))
372 /* Can't use the rxrpc workqueue as we need to cancel/flush
373 * something that may be running/waiting there.
375 schedule_work(&conn->destructor);
377 rxrpc_clean_up_connection(&conn->destructor);
382 * reap dead service connections
384 void rxrpc_service_connection_reaper(struct work_struct *work)
386 struct rxrpc_connection *conn, *_p;
387 struct rxrpc_net *rxnet =
388 container_of(work, struct rxrpc_net, service_conn_reaper);
389 unsigned long expire_at, earliest, idle_timestamp, now;
392 LIST_HEAD(graveyard);
397 earliest = now + MAX_JIFFY_OFFSET;
399 write_lock(&rxnet->conn_lock);
400 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
401 ASSERTCMP(atomic_read(&conn->active), >=, 0);
402 if (likely(atomic_read(&conn->active) > 0))
404 if (conn->state == RXRPC_CONN_SERVICE_PREALLOC)
407 if (rxnet->live && !conn->local->dead) {
408 idle_timestamp = READ_ONCE(conn->idle_timestamp);
409 expire_at = idle_timestamp + rxrpc_connection_expiry * HZ;
410 if (conn->local->service_closed)
411 expire_at = idle_timestamp + rxrpc_closed_conn_expiry * HZ;
413 _debug("reap CONN %d { a=%d,t=%ld }",
414 conn->debug_id, atomic_read(&conn->active),
415 (long)expire_at - (long)now);
417 if (time_before(now, expire_at)) {
418 if (time_before(expire_at, earliest))
419 earliest = expire_at;
424 /* The activity count sits at 0 whilst the conn is unused on
425 * the list; we reduce that to -1 to make the conn unavailable.
428 if (!atomic_try_cmpxchg(&conn->active, &active, -1))
430 rxrpc_see_connection(conn, rxrpc_conn_see_reap_service);
432 if (rxrpc_conn_is_client(conn))
435 rxrpc_unpublish_service_conn(conn);
437 list_move_tail(&conn->link, &graveyard);
439 write_unlock(&rxnet->conn_lock);
441 if (earliest != now + MAX_JIFFY_OFFSET) {
442 _debug("reschedule reaper %ld", (long)earliest - (long)now);
443 ASSERT(time_after(earliest, now));
444 rxrpc_set_service_reap_timer(rxnet, earliest);
447 while (!list_empty(&graveyard)) {
448 conn = list_entry(graveyard.next, struct rxrpc_connection,
450 list_del_init(&conn->link);
452 ASSERTCMP(atomic_read(&conn->active), ==, -1);
453 rxrpc_put_connection(conn, rxrpc_conn_put_service_reaped);
460 * preemptively destroy all the service connection records rather than
461 * waiting for them to time out
463 void rxrpc_destroy_all_connections(struct rxrpc_net *rxnet)
465 struct rxrpc_connection *conn, *_p;
470 atomic_dec(&rxnet->nr_conns);
471 rxrpc_destroy_all_client_connections(rxnet);
473 del_timer_sync(&rxnet->service_conn_reap_timer);
474 rxrpc_queue_work(&rxnet->service_conn_reaper);
475 flush_workqueue(rxrpc_workqueue);
477 write_lock(&rxnet->conn_lock);
478 list_for_each_entry_safe(conn, _p, &rxnet->service_conns, link) {
479 pr_err("AF_RXRPC: Leaked conn %p {%d}\n",
480 conn, refcount_read(&conn->ref));
483 write_unlock(&rxnet->conn_lock);
486 ASSERT(list_empty(&rxnet->conn_proc_list));
488 /* We need to wait for the connections to be destroyed by RCU as they
489 * pin things that we still need to get rid of.
491 wait_var_event(&rxnet->nr_conns, !atomic_read(&rxnet->nr_conns));