blob: 417d80867c4f4a013465b3f9c22be04e49082b2f [file] [log] [blame]
David Howells2baec2c2017-05-24 17:02:32 +01001/* 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
David Howells3d18cbb2017-11-24 10:18:42 +000017static 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
David Howellsace45be2018-03-30 21:04:43 +010035static 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
David Howells2baec2c2017-05-24 17:02:32 +010044/*
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);
David Howellsace45be2018-03-30 21:04:43 +010050 int ret, i;
David Howells2baec2c2017-05-24 17:02:32 +010051
David Howellsf859ab62017-11-24 10:18:42 +000052 rxnet->live = true;
David Howells2baec2c2017-05-24 17:02:32 +010053 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);
David Howellsd3be4d22018-03-30 21:05:23 +010058 atomic_set(&rxnet->nr_calls, 1);
David Howells2baec2c2017-05-24 17:02:32 +010059
David Howells31f5f9a162018-03-30 21:05:33 +010060 atomic_set(&rxnet->nr_conns, 1);
David Howells2baec2c2017-05-24 17:02:32 +010061 INIT_LIST_HEAD(&rxnet->conn_proc_list);
62 INIT_LIST_HEAD(&rxnet->service_conns);
63 rwlock_init(&rxnet->conn_lock);
David Howells3d18cbb2017-11-24 10:18:42 +000064 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);
David Howells2baec2c2017-05-24 17:02:32 +010068
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);
David Howells3d18cbb2017-11-24 10:18:42 +000077 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);
David Howells2baec2c2017-05-24 17:02:32 +010081
82 INIT_LIST_HEAD(&rxnet->local_endpoints);
83 mutex_init(&rxnet->local_mutex);
David Howellsace45be2018-03-30 21:04:43 +010084
David Howells2baec2c2017-05-24 17:02:32 +010085 hash_init(rxnet->peer_hash);
86 spin_lock_init(&rxnet->peer_hash_lock);
David Howellsace45be2018-03-30 21:04:43 +010087 for (i = 0; i < ARRAY_SIZE(rxnet->peer_keepalive); i++)
David Howells330bdcf2018-08-08 11:30:02 +010088 INIT_LIST_HEAD(&rxnet->peer_keepalive[i]);
89 INIT_LIST_HEAD(&rxnet->peer_keepalive_new);
David Howellsace45be2018-03-30 21:04:43 +010090 timer_setup(&rxnet->peer_keepalive_timer,
91 rxrpc_peer_keepalive_timeout, 0);
92 INIT_WORK(&rxnet->peer_keepalive_work, rxrpc_peer_keepalive_worker);
David Howells330bdcf2018-08-08 11:30:02 +010093 rxnet->peer_keepalive_base = ktime_get_seconds();
David Howells2baec2c2017-05-24 17:02:32 +010094
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
Christoph Hellwigc3506372018-04-10 19:42:55 +0200100 proc_create_net("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_ops,
101 sizeof(struct seq_net_private));
102 proc_create_net("conns", 0444, rxnet->proc_net,
103 &rxrpc_connection_seq_ops,
104 sizeof(struct seq_net_private));
David Howells2baec2c2017-05-24 17:02:32 +0100105 return 0;
106
David Howells2baec2c2017-05-24 17:02:32 +0100107err_proc:
David Howellsf859ab62017-11-24 10:18:42 +0000108 rxnet->live = false;
David Howells2baec2c2017-05-24 17:02:32 +0100109 return ret;
110}
111
112/*
113 * Clean up a per-network namespace record.
114 */
115static __net_exit void rxrpc_exit_net(struct net *net)
116{
117 struct rxrpc_net *rxnet = rxrpc_net(net);
118
David Howellsf859ab62017-11-24 10:18:42 +0000119 rxnet->live = false;
David Howellsace45be2018-03-30 21:04:43 +0100120 del_timer_sync(&rxnet->peer_keepalive_timer);
121 cancel_work_sync(&rxnet->peer_keepalive_work);
David Howells2baec2c2017-05-24 17:02:32 +0100122 rxrpc_destroy_all_calls(rxnet);
123 rxrpc_destroy_all_connections(rxnet);
David Howells17226f12018-03-30 21:05:44 +0100124 rxrpc_destroy_all_peers(rxnet);
David Howells2baec2c2017-05-24 17:02:32 +0100125 rxrpc_destroy_all_locals(rxnet);
126 proc_remove(rxnet->proc_net);
127}
128
129struct pernet_operations rxrpc_net_ops = {
130 .init = rxrpc_init_net,
131 .exit = rxrpc_exit_net,
132 .id = &rxrpc_net_id,
133 .size = sizeof(struct rxrpc_net),
David Howells2baec2c2017-05-24 17:02:32 +0100134};