blob: 25bbc4cc8b1359f7b895f181dad227de088ed31d [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells2baec2c2017-05-24 17:02:32 +01002/* rxrpc network namespace handling.
3 *
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells2baec2c2017-05-24 17:02:32 +01006 */
7
8#include <linux/proc_fs.h>
9#include "ar-internal.h"
10
11unsigned int rxrpc_net_id;
12
David Howells3d18cbb2017-11-24 10:18:42 +000013static void rxrpc_client_conn_reap_timeout(struct timer_list *timer)
14{
15 struct rxrpc_net *rxnet =
16 container_of(timer, struct rxrpc_net, client_conn_reap_timer);
17
18 if (rxnet->live)
19 rxrpc_queue_work(&rxnet->client_conn_reaper);
20}
21
22static void rxrpc_service_conn_reap_timeout(struct timer_list *timer)
23{
24 struct rxrpc_net *rxnet =
25 container_of(timer, struct rxrpc_net, service_conn_reap_timer);
26
27 if (rxnet->live)
28 rxrpc_queue_work(&rxnet->service_conn_reaper);
29}
30
David Howellsace45be2018-03-30 21:04:43 +010031static void rxrpc_peer_keepalive_timeout(struct timer_list *timer)
32{
33 struct rxrpc_net *rxnet =
34 container_of(timer, struct rxrpc_net, peer_keepalive_timer);
35
36 if (rxnet->live)
37 rxrpc_queue_work(&rxnet->peer_keepalive_work);
38}
39
David Howells2baec2c2017-05-24 17:02:32 +010040/*
41 * Initialise a per-network namespace record.
42 */
43static __net_init int rxrpc_init_net(struct net *net)
44{
45 struct rxrpc_net *rxnet = rxrpc_net(net);
David Howellsace45be2018-03-30 21:04:43 +010046 int ret, i;
David Howells2baec2c2017-05-24 17:02:32 +010047
David Howellsf859ab62017-11-24 10:18:42 +000048 rxnet->live = true;
David Howells2baec2c2017-05-24 17:02:32 +010049 get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
50 rxnet->epoch |= RXRPC_RANDOM_EPOCH;
51
52 INIT_LIST_HEAD(&rxnet->calls);
53 rwlock_init(&rxnet->call_lock);
David Howellsd3be4d22018-03-30 21:05:23 +010054 atomic_set(&rxnet->nr_calls, 1);
David Howells2baec2c2017-05-24 17:02:32 +010055
David Howells31f5f9a162018-03-30 21:05:33 +010056 atomic_set(&rxnet->nr_conns, 1);
David Howells2baec2c2017-05-24 17:02:32 +010057 INIT_LIST_HEAD(&rxnet->conn_proc_list);
58 INIT_LIST_HEAD(&rxnet->service_conns);
59 rwlock_init(&rxnet->conn_lock);
David Howells3d18cbb2017-11-24 10:18:42 +000060 INIT_WORK(&rxnet->service_conn_reaper,
61 rxrpc_service_connection_reaper);
62 timer_setup(&rxnet->service_conn_reap_timer,
63 rxrpc_service_conn_reap_timeout, 0);
David Howells2baec2c2017-05-24 17:02:32 +010064
David Howells245500d2020-07-01 11:15:32 +010065 atomic_set(&rxnet->nr_client_conns, 0);
David Howells2baec2c2017-05-24 17:02:32 +010066 rxnet->kill_all_client_conns = false;
67 spin_lock_init(&rxnet->client_conn_cache_lock);
68 spin_lock_init(&rxnet->client_conn_discard_lock);
David Howells2baec2c2017-05-24 17:02:32 +010069 INIT_LIST_HEAD(&rxnet->idle_client_conns);
David Howells3d18cbb2017-11-24 10:18:42 +000070 INIT_WORK(&rxnet->client_conn_reaper,
71 rxrpc_discard_expired_client_conns);
72 timer_setup(&rxnet->client_conn_reap_timer,
73 rxrpc_client_conn_reap_timeout, 0);
David Howells2baec2c2017-05-24 17:02:32 +010074
75 INIT_LIST_HEAD(&rxnet->local_endpoints);
76 mutex_init(&rxnet->local_mutex);
David Howellsace45be2018-03-30 21:04:43 +010077
David Howells2baec2c2017-05-24 17:02:32 +010078 hash_init(rxnet->peer_hash);
79 spin_lock_init(&rxnet->peer_hash_lock);
David Howellsace45be2018-03-30 21:04:43 +010080 for (i = 0; i < ARRAY_SIZE(rxnet->peer_keepalive); i++)
David Howells330bdcf2018-08-08 11:30:02 +010081 INIT_LIST_HEAD(&rxnet->peer_keepalive[i]);
82 INIT_LIST_HEAD(&rxnet->peer_keepalive_new);
David Howellsace45be2018-03-30 21:04:43 +010083 timer_setup(&rxnet->peer_keepalive_timer,
84 rxrpc_peer_keepalive_timeout, 0);
85 INIT_WORK(&rxnet->peer_keepalive_work, rxrpc_peer_keepalive_worker);
David Howells330bdcf2018-08-08 11:30:02 +010086 rxnet->peer_keepalive_base = ktime_get_seconds();
David Howells2baec2c2017-05-24 17:02:32 +010087
88 ret = -ENOMEM;
89 rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
90 if (!rxnet->proc_net)
91 goto err_proc;
92
Christoph Hellwigc3506372018-04-10 19:42:55 +020093 proc_create_net("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_ops,
94 sizeof(struct seq_net_private));
95 proc_create_net("conns", 0444, rxnet->proc_net,
96 &rxrpc_connection_seq_ops,
97 sizeof(struct seq_net_private));
David Howellsbc0e7cf2018-10-15 11:31:03 +010098 proc_create_net("peers", 0444, rxnet->proc_net,
99 &rxrpc_peer_seq_ops,
100 sizeof(struct seq_net_private));
David Howells2baec2c2017-05-24 17:02:32 +0100101 return 0;
102
David Howells2baec2c2017-05-24 17:02:32 +0100103err_proc:
David Howellsf859ab62017-11-24 10:18:42 +0000104 rxnet->live = false;
David Howells2baec2c2017-05-24 17:02:32 +0100105 return ret;
106}
107
108/*
109 * Clean up a per-network namespace record.
110 */
111static __net_exit void rxrpc_exit_net(struct net *net)
112{
113 struct rxrpc_net *rxnet = rxrpc_net(net);
114
David Howellsf859ab62017-11-24 10:18:42 +0000115 rxnet->live = false;
David Howellsace45be2018-03-30 21:04:43 +0100116 del_timer_sync(&rxnet->peer_keepalive_timer);
117 cancel_work_sync(&rxnet->peer_keepalive_work);
David Howells2baec2c2017-05-24 17:02:32 +0100118 rxrpc_destroy_all_calls(rxnet);
119 rxrpc_destroy_all_connections(rxnet);
David Howells17226f12018-03-30 21:05:44 +0100120 rxrpc_destroy_all_peers(rxnet);
David Howells2baec2c2017-05-24 17:02:32 +0100121 rxrpc_destroy_all_locals(rxnet);
122 proc_remove(rxnet->proc_net);
123}
124
125struct pernet_operations rxrpc_net_ops = {
126 .init = rxrpc_init_net,
127 .exit = rxrpc_exit_net,
128 .id = &rxrpc_net_id,
129 .size = sizeof(struct rxrpc_net),
David Howells2baec2c2017-05-24 17:02:32 +0100130};