Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* rxrpc network namespace handling.
3 *
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/proc_fs.h>
9#include "ar-internal.h"
10
11unsigned int rxrpc_net_id;
12
13static void rxrpc_service_conn_reap_timeout(struct timer_list *timer)
14{
15 struct rxrpc_net *rxnet =
16 container_of(timer, struct rxrpc_net, service_conn_reap_timer);
17
18 if (rxnet->live)
19 rxrpc_queue_work(&rxnet->service_conn_reaper);
20}
21
22static void rxrpc_peer_keepalive_timeout(struct timer_list *timer)
23{
24 struct rxrpc_net *rxnet =
25 container_of(timer, struct rxrpc_net, peer_keepalive_timer);
26
27 if (rxnet->live)
28 rxrpc_queue_work(&rxnet->peer_keepalive_work);
29}
30
31/*
32 * Initialise a per-network namespace record.
33 */
34static __net_init int rxrpc_init_net(struct net *net)
35{
36 struct rxrpc_net *rxnet = rxrpc_net(net);
37 int ret, i;
38
39 rxnet->live = true;
40 get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
41 rxnet->epoch |= RXRPC_RANDOM_EPOCH;
42
43 INIT_LIST_HEAD(&rxnet->calls);
44 spin_lock_init(&rxnet->call_lock);
45 atomic_set(&rxnet->nr_calls, 1);
46
47 atomic_set(&rxnet->nr_conns, 1);
48 INIT_LIST_HEAD(&rxnet->conn_proc_list);
49 INIT_LIST_HEAD(&rxnet->service_conns);
50 rwlock_init(&rxnet->conn_lock);
51 INIT_WORK(&rxnet->service_conn_reaper,
52 rxrpc_service_connection_reaper);
53 timer_setup(&rxnet->service_conn_reap_timer,
54 rxrpc_service_conn_reap_timeout, 0);
55
56 atomic_set(&rxnet->nr_client_conns, 0);
57
58 INIT_HLIST_HEAD(&rxnet->local_endpoints);
59 mutex_init(&rxnet->local_mutex);
60
61 hash_init(rxnet->peer_hash);
62 spin_lock_init(&rxnet->peer_hash_lock);
63 for (i = 0; i < ARRAY_SIZE(rxnet->peer_keepalive); i++)
64 INIT_LIST_HEAD(&rxnet->peer_keepalive[i]);
65 INIT_LIST_HEAD(&rxnet->peer_keepalive_new);
66 timer_setup(&rxnet->peer_keepalive_timer,
67 rxrpc_peer_keepalive_timeout, 0);
68 INIT_WORK(&rxnet->peer_keepalive_work, rxrpc_peer_keepalive_worker);
69 rxnet->peer_keepalive_base = ktime_get_seconds();
70
71 ret = -ENOMEM;
72 rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
73 if (!rxnet->proc_net)
74 goto err_proc;
75
76 proc_create_net("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_ops,
77 sizeof(struct seq_net_private));
78 proc_create_net("conns", 0444, rxnet->proc_net,
79 &rxrpc_connection_seq_ops,
80 sizeof(struct seq_net_private));
81 proc_create_net("peers", 0444, rxnet->proc_net,
82 &rxrpc_peer_seq_ops,
83 sizeof(struct seq_net_private));
84 proc_create_net("locals", 0444, rxnet->proc_net,
85 &rxrpc_local_seq_ops,
86 sizeof(struct seq_net_private));
87 proc_create_net_single_write("stats", S_IFREG | 0644, rxnet->proc_net,
88 rxrpc_stats_show, rxrpc_stats_clear, NULL);
89 return 0;
90
91err_proc:
92 rxnet->live = false;
93 return ret;
94}
95
96/*
97 * Clean up a per-network namespace record.
98 */
99static __net_exit void rxrpc_exit_net(struct net *net)
100{
101 struct rxrpc_net *rxnet = rxrpc_net(net);
102
103 rxnet->live = false;
104 del_timer_sync(&rxnet->peer_keepalive_timer);
105 cancel_work_sync(&rxnet->peer_keepalive_work);
106 /* Remove the timer again as the worker may have restarted it. */
107 del_timer_sync(&rxnet->peer_keepalive_timer);
108 rxrpc_destroy_all_calls(rxnet);
109 rxrpc_destroy_all_connections(rxnet);
110 rxrpc_destroy_all_peers(rxnet);
111 rxrpc_destroy_all_locals(rxnet);
112 proc_remove(rxnet->proc_net);
113}
114
115struct pernet_operations rxrpc_net_ops = {
116 .init = rxrpc_init_net,
117 .exit = rxrpc_exit_net,
118 .id = &rxrpc_net_id,
119 .size = sizeof(struct rxrpc_net),
120};
1/* rxrpc network namespace handling.
2 *
3 * Copyright (C) 2017 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 Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#include <linux/proc_fs.h>
13#include "ar-internal.h"
14
15unsigned int rxrpc_net_id;
16
17static void rxrpc_client_conn_reap_timeout(struct timer_list *timer)
18{
19 struct rxrpc_net *rxnet =
20 container_of(timer, struct rxrpc_net, client_conn_reap_timer);
21
22 if (rxnet->live)
23 rxrpc_queue_work(&rxnet->client_conn_reaper);
24}
25
26static void rxrpc_service_conn_reap_timeout(struct timer_list *timer)
27{
28 struct rxrpc_net *rxnet =
29 container_of(timer, struct rxrpc_net, service_conn_reap_timer);
30
31 if (rxnet->live)
32 rxrpc_queue_work(&rxnet->service_conn_reaper);
33}
34
35static void rxrpc_peer_keepalive_timeout(struct timer_list *timer)
36{
37 struct rxrpc_net *rxnet =
38 container_of(timer, struct rxrpc_net, peer_keepalive_timer);
39
40 if (rxnet->live)
41 rxrpc_queue_work(&rxnet->peer_keepalive_work);
42}
43
44/*
45 * Initialise a per-network namespace record.
46 */
47static __net_init int rxrpc_init_net(struct net *net)
48{
49 struct rxrpc_net *rxnet = rxrpc_net(net);
50 int ret, i;
51
52 rxnet->live = true;
53 get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
54 rxnet->epoch |= RXRPC_RANDOM_EPOCH;
55
56 INIT_LIST_HEAD(&rxnet->calls);
57 rwlock_init(&rxnet->call_lock);
58 atomic_set(&rxnet->nr_calls, 1);
59
60 atomic_set(&rxnet->nr_conns, 1);
61 INIT_LIST_HEAD(&rxnet->conn_proc_list);
62 INIT_LIST_HEAD(&rxnet->service_conns);
63 rwlock_init(&rxnet->conn_lock);
64 INIT_WORK(&rxnet->service_conn_reaper,
65 rxrpc_service_connection_reaper);
66 timer_setup(&rxnet->service_conn_reap_timer,
67 rxrpc_service_conn_reap_timeout, 0);
68
69 rxnet->nr_client_conns = 0;
70 rxnet->nr_active_client_conns = 0;
71 rxnet->kill_all_client_conns = false;
72 spin_lock_init(&rxnet->client_conn_cache_lock);
73 spin_lock_init(&rxnet->client_conn_discard_lock);
74 INIT_LIST_HEAD(&rxnet->waiting_client_conns);
75 INIT_LIST_HEAD(&rxnet->active_client_conns);
76 INIT_LIST_HEAD(&rxnet->idle_client_conns);
77 INIT_WORK(&rxnet->client_conn_reaper,
78 rxrpc_discard_expired_client_conns);
79 timer_setup(&rxnet->client_conn_reap_timer,
80 rxrpc_client_conn_reap_timeout, 0);
81
82 INIT_LIST_HEAD(&rxnet->local_endpoints);
83 mutex_init(&rxnet->local_mutex);
84
85 hash_init(rxnet->peer_hash);
86 spin_lock_init(&rxnet->peer_hash_lock);
87 for (i = 0; i < ARRAY_SIZE(rxnet->peer_keepalive); i++)
88 INIT_HLIST_HEAD(&rxnet->peer_keepalive[i]);
89 INIT_HLIST_HEAD(&rxnet->peer_keepalive_new);
90 timer_setup(&rxnet->peer_keepalive_timer,
91 rxrpc_peer_keepalive_timeout, 0);
92 INIT_WORK(&rxnet->peer_keepalive_work, rxrpc_peer_keepalive_worker);
93 rxnet->peer_keepalive_base = ktime_add(ktime_get_real(), NSEC_PER_SEC);
94
95 ret = -ENOMEM;
96 rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
97 if (!rxnet->proc_net)
98 goto err_proc;
99
100 proc_create("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_fops);
101 proc_create("conns", 0444, rxnet->proc_net, &rxrpc_connection_seq_fops);
102 return 0;
103
104err_proc:
105 rxnet->live = false;
106 return ret;
107}
108
109/*
110 * Clean up a per-network namespace record.
111 */
112static __net_exit void rxrpc_exit_net(struct net *net)
113{
114 struct rxrpc_net *rxnet = rxrpc_net(net);
115
116 rxnet->live = false;
117 del_timer_sync(&rxnet->peer_keepalive_timer);
118 cancel_work_sync(&rxnet->peer_keepalive_work);
119 rxrpc_destroy_all_calls(rxnet);
120 rxrpc_destroy_all_connections(rxnet);
121 rxrpc_destroy_all_peers(rxnet);
122 rxrpc_destroy_all_locals(rxnet);
123 proc_remove(rxnet->proc_net);
124}
125
126struct pernet_operations rxrpc_net_ops = {
127 .init = rxrpc_init_net,
128 .exit = rxrpc_exit_net,
129 .id = &rxrpc_net_id,
130 .size = sizeof(struct rxrpc_net),
131};