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