net_ns.c 3.7 KB

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