Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* rxrpc network namespace handling.
0003  *
0004  * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/proc_fs.h>
0009 #include "ar-internal.h"
0010 
0011 unsigned int rxrpc_net_id;
0012 
0013 static void rxrpc_client_conn_reap_timeout(struct timer_list *timer)
0014 {
0015     struct rxrpc_net *rxnet =
0016         container_of(timer, struct rxrpc_net, client_conn_reap_timer);
0017 
0018     if (rxnet->live)
0019         rxrpc_queue_work(&rxnet->client_conn_reaper);
0020 }
0021 
0022 static void rxrpc_service_conn_reap_timeout(struct timer_list *timer)
0023 {
0024     struct rxrpc_net *rxnet =
0025         container_of(timer, struct rxrpc_net, service_conn_reap_timer);
0026 
0027     if (rxnet->live)
0028         rxrpc_queue_work(&rxnet->service_conn_reaper);
0029 }
0030 
0031 static void rxrpc_peer_keepalive_timeout(struct timer_list *timer)
0032 {
0033     struct rxrpc_net *rxnet =
0034         container_of(timer, struct rxrpc_net, peer_keepalive_timer);
0035 
0036     if (rxnet->live)
0037         rxrpc_queue_work(&rxnet->peer_keepalive_work);
0038 }
0039 
0040 /*
0041  * Initialise a per-network namespace record.
0042  */
0043 static __net_init int rxrpc_init_net(struct net *net)
0044 {
0045     struct rxrpc_net *rxnet = rxrpc_net(net);
0046     int ret, i;
0047 
0048     rxnet->live = true;
0049     get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
0050     rxnet->epoch |= RXRPC_RANDOM_EPOCH;
0051 
0052     INIT_LIST_HEAD(&rxnet->calls);
0053     spin_lock_init(&rxnet->call_lock);
0054     atomic_set(&rxnet->nr_calls, 1);
0055 
0056     atomic_set(&rxnet->nr_conns, 1);
0057     INIT_LIST_HEAD(&rxnet->conn_proc_list);
0058     INIT_LIST_HEAD(&rxnet->service_conns);
0059     rwlock_init(&rxnet->conn_lock);
0060     INIT_WORK(&rxnet->service_conn_reaper,
0061           rxrpc_service_connection_reaper);
0062     timer_setup(&rxnet->service_conn_reap_timer,
0063             rxrpc_service_conn_reap_timeout, 0);
0064 
0065     atomic_set(&rxnet->nr_client_conns, 0);
0066     rxnet->kill_all_client_conns = false;
0067     spin_lock_init(&rxnet->client_conn_cache_lock);
0068     spin_lock_init(&rxnet->client_conn_discard_lock);
0069     INIT_LIST_HEAD(&rxnet->idle_client_conns);
0070     INIT_WORK(&rxnet->client_conn_reaper,
0071           rxrpc_discard_expired_client_conns);
0072     timer_setup(&rxnet->client_conn_reap_timer,
0073             rxrpc_client_conn_reap_timeout, 0);
0074 
0075     INIT_HLIST_HEAD(&rxnet->local_endpoints);
0076     mutex_init(&rxnet->local_mutex);
0077 
0078     hash_init(rxnet->peer_hash);
0079     spin_lock_init(&rxnet->peer_hash_lock);
0080     for (i = 0; i < ARRAY_SIZE(rxnet->peer_keepalive); i++)
0081         INIT_LIST_HEAD(&rxnet->peer_keepalive[i]);
0082     INIT_LIST_HEAD(&rxnet->peer_keepalive_new);
0083     timer_setup(&rxnet->peer_keepalive_timer,
0084             rxrpc_peer_keepalive_timeout, 0);
0085     INIT_WORK(&rxnet->peer_keepalive_work, rxrpc_peer_keepalive_worker);
0086     rxnet->peer_keepalive_base = ktime_get_seconds();
0087 
0088     ret = -ENOMEM;
0089     rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
0090     if (!rxnet->proc_net)
0091         goto err_proc;
0092 
0093     proc_create_net("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_ops,
0094             sizeof(struct seq_net_private));
0095     proc_create_net("conns", 0444, rxnet->proc_net,
0096             &rxrpc_connection_seq_ops,
0097             sizeof(struct seq_net_private));
0098     proc_create_net("peers", 0444, rxnet->proc_net,
0099             &rxrpc_peer_seq_ops,
0100             sizeof(struct seq_net_private));
0101     proc_create_net("locals", 0444, rxnet->proc_net,
0102             &rxrpc_local_seq_ops,
0103             sizeof(struct seq_net_private));
0104     return 0;
0105 
0106 err_proc:
0107     rxnet->live = false;
0108     return ret;
0109 }
0110 
0111 /*
0112  * Clean up a per-network namespace record.
0113  */
0114 static __net_exit void rxrpc_exit_net(struct net *net)
0115 {
0116     struct rxrpc_net *rxnet = rxrpc_net(net);
0117 
0118     rxnet->live = false;
0119     del_timer_sync(&rxnet->peer_keepalive_timer);
0120     cancel_work_sync(&rxnet->peer_keepalive_work);
0121     /* Remove the timer again as the worker may have restarted it. */
0122     del_timer_sync(&rxnet->peer_keepalive_timer);
0123     rxrpc_destroy_all_calls(rxnet);
0124     rxrpc_destroy_all_connections(rxnet);
0125     rxrpc_destroy_all_peers(rxnet);
0126     rxrpc_destroy_all_locals(rxnet);
0127     proc_remove(rxnet->proc_net);
0128 }
0129 
0130 struct pernet_operations rxrpc_net_ops = {
0131     .init   = rxrpc_init_net,
0132     .exit   = rxrpc_exit_net,
0133     .id = &rxrpc_net_id,
0134     .size   = sizeof(struct rxrpc_net),
0135 };