net_ns.c 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* rxrpc network namespace handling.
 *
 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

#include <linux/proc_fs.h>
#include "ar-internal.h"

unsigned int rxrpc_net_id;

D
David Howells 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
static void rxrpc_client_conn_reap_timeout(struct timer_list *timer)
{
	struct rxrpc_net *rxnet =
		container_of(timer, struct rxrpc_net, client_conn_reap_timer);

	if (rxnet->live)
		rxrpc_queue_work(&rxnet->client_conn_reaper);
}

static void rxrpc_service_conn_reap_timeout(struct timer_list *timer)
{
	struct rxrpc_net *rxnet =
		container_of(timer, struct rxrpc_net, service_conn_reap_timer);

	if (rxnet->live)
		rxrpc_queue_work(&rxnet->service_conn_reaper);
}

35 36 37 38 39 40 41 42 43
static void rxrpc_peer_keepalive_timeout(struct timer_list *timer)
{
	struct rxrpc_net *rxnet =
		container_of(timer, struct rxrpc_net, peer_keepalive_timer);

	if (rxnet->live)
		rxrpc_queue_work(&rxnet->peer_keepalive_work);
}

44 45 46 47 48 49
/*
 * Initialise a per-network namespace record.
 */
static __net_init int rxrpc_init_net(struct net *net)
{
	struct rxrpc_net *rxnet = rxrpc_net(net);
50
	int ret, i;
51

D
David Howells 已提交
52
	rxnet->live = true;
53 54 55 56 57
	get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
	rxnet->epoch |= RXRPC_RANDOM_EPOCH;

	INIT_LIST_HEAD(&rxnet->calls);
	rwlock_init(&rxnet->call_lock);
58
	atomic_set(&rxnet->nr_calls, 1);
59

60
	atomic_set(&rxnet->nr_conns, 1);
61 62 63
	INIT_LIST_HEAD(&rxnet->conn_proc_list);
	INIT_LIST_HEAD(&rxnet->service_conns);
	rwlock_init(&rxnet->conn_lock);
D
David Howells 已提交
64 65 66 67
	INIT_WORK(&rxnet->service_conn_reaper,
		  rxrpc_service_connection_reaper);
	timer_setup(&rxnet->service_conn_reap_timer,
		    rxrpc_service_conn_reap_timeout, 0);
68 69 70 71 72 73 74 75 76

	rxnet->nr_client_conns = 0;
	rxnet->nr_active_client_conns = 0;
	rxnet->kill_all_client_conns = false;
	spin_lock_init(&rxnet->client_conn_cache_lock);
	spin_lock_init(&rxnet->client_conn_discard_lock);
	INIT_LIST_HEAD(&rxnet->waiting_client_conns);
	INIT_LIST_HEAD(&rxnet->active_client_conns);
	INIT_LIST_HEAD(&rxnet->idle_client_conns);
D
David Howells 已提交
77 78 79 80
	INIT_WORK(&rxnet->client_conn_reaper,
		  rxrpc_discard_expired_client_conns);
	timer_setup(&rxnet->client_conn_reap_timer,
		    rxrpc_client_conn_reap_timeout, 0);
81 82 83

	INIT_LIST_HEAD(&rxnet->local_endpoints);
	mutex_init(&rxnet->local_mutex);
84

85 86
	hash_init(rxnet->peer_hash);
	spin_lock_init(&rxnet->peer_hash_lock);
87 88 89 90 91 92 93
	for (i = 0; i < ARRAY_SIZE(rxnet->peer_keepalive); i++)
		INIT_HLIST_HEAD(&rxnet->peer_keepalive[i]);
	INIT_HLIST_HEAD(&rxnet->peer_keepalive_new);
	timer_setup(&rxnet->peer_keepalive_timer,
		    rxrpc_peer_keepalive_timeout, 0);
	INIT_WORK(&rxnet->peer_keepalive_work, rxrpc_peer_keepalive_worker);
	rxnet->peer_keepalive_base = ktime_add(ktime_get_real(), NSEC_PER_SEC);
94 95 96 97 98 99 100 101 102 103 104

	ret = -ENOMEM;
	rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
	if (!rxnet->proc_net)
		goto err_proc;

	proc_create("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_fops);
	proc_create("conns", 0444, rxnet->proc_net, &rxrpc_connection_seq_fops);
	return 0;

err_proc:
D
David Howells 已提交
105
	rxnet->live = false;
106 107 108 109 110 111 112 113 114 115
	return ret;
}

/*
 * Clean up a per-network namespace record.
 */
static __net_exit void rxrpc_exit_net(struct net *net)
{
	struct rxrpc_net *rxnet = rxrpc_net(net);

D
David Howells 已提交
116
	rxnet->live = false;
117 118
	del_timer_sync(&rxnet->peer_keepalive_timer);
	cancel_work_sync(&rxnet->peer_keepalive_work);
119 120
	rxrpc_destroy_all_calls(rxnet);
	rxrpc_destroy_all_connections(rxnet);
121
	rxrpc_destroy_all_peers(rxnet);
122 123 124 125 126 127 128 129 130 131
	rxrpc_destroy_all_locals(rxnet);
	proc_remove(rxnet->proc_net);
}

struct pernet_operations rxrpc_net_ops = {
	.init	= rxrpc_init_net,
	.exit	= rxrpc_exit_net,
	.id	= &rxrpc_net_id,
	.size	= sizeof(struct rxrpc_net),
};