xref: /openbmc/linux/net/rxrpc/net_ns.c (revision 160b8e75)
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 
15 unsigned int rxrpc_net_id;
16 
17 static 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 
26 static 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 
35 /*
36  * Initialise a per-network namespace record.
37  */
38 static __net_init int rxrpc_init_net(struct net *net)
39 {
40 	struct rxrpc_net *rxnet = rxrpc_net(net);
41 	int ret;
42 
43 	rxnet->live = true;
44 	get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
45 	rxnet->epoch |= RXRPC_RANDOM_EPOCH;
46 
47 	INIT_LIST_HEAD(&rxnet->calls);
48 	rwlock_init(&rxnet->call_lock);
49 
50 	INIT_LIST_HEAD(&rxnet->conn_proc_list);
51 	INIT_LIST_HEAD(&rxnet->service_conns);
52 	rwlock_init(&rxnet->conn_lock);
53 	INIT_WORK(&rxnet->service_conn_reaper,
54 		  rxrpc_service_connection_reaper);
55 	timer_setup(&rxnet->service_conn_reap_timer,
56 		    rxrpc_service_conn_reap_timeout, 0);
57 
58 	rxnet->nr_client_conns = 0;
59 	rxnet->nr_active_client_conns = 0;
60 	rxnet->kill_all_client_conns = false;
61 	spin_lock_init(&rxnet->client_conn_cache_lock);
62 	spin_lock_init(&rxnet->client_conn_discard_lock);
63 	INIT_LIST_HEAD(&rxnet->waiting_client_conns);
64 	INIT_LIST_HEAD(&rxnet->active_client_conns);
65 	INIT_LIST_HEAD(&rxnet->idle_client_conns);
66 	INIT_WORK(&rxnet->client_conn_reaper,
67 		  rxrpc_discard_expired_client_conns);
68 	timer_setup(&rxnet->client_conn_reap_timer,
69 		    rxrpc_client_conn_reap_timeout, 0);
70 
71 	INIT_LIST_HEAD(&rxnet->local_endpoints);
72 	mutex_init(&rxnet->local_mutex);
73 	hash_init(rxnet->peer_hash);
74 	spin_lock_init(&rxnet->peer_hash_lock);
75 
76 	ret = -ENOMEM;
77 	rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
78 	if (!rxnet->proc_net)
79 		goto err_proc;
80 
81 	proc_create("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_fops);
82 	proc_create("conns", 0444, rxnet->proc_net, &rxrpc_connection_seq_fops);
83 	return 0;
84 
85 err_proc:
86 	rxnet->live = false;
87 	return ret;
88 }
89 
90 /*
91  * Clean up a per-network namespace record.
92  */
93 static __net_exit void rxrpc_exit_net(struct net *net)
94 {
95 	struct rxrpc_net *rxnet = rxrpc_net(net);
96 
97 	rxnet->live = false;
98 	rxrpc_destroy_all_calls(rxnet);
99 	rxrpc_destroy_all_connections(rxnet);
100 	rxrpc_destroy_all_locals(rxnet);
101 	proc_remove(rxnet->proc_net);
102 }
103 
104 struct pernet_operations rxrpc_net_ops = {
105 	.init	= rxrpc_init_net,
106 	.exit	= rxrpc_exit_net,
107 	.id	= &rxrpc_net_id,
108 	.size	= sizeof(struct rxrpc_net),
109 };
110