net_ns.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_service_conn_reap_timeout(struct timer_list *timer)
  11. {
  12. struct rxrpc_net *rxnet =
  13. container_of(timer, struct rxrpc_net, service_conn_reap_timer);
  14. if (rxnet->live)
  15. rxrpc_queue_work(&rxnet->service_conn_reaper);
  16. }
  17. static void rxrpc_peer_keepalive_timeout(struct timer_list *timer)
  18. {
  19. struct rxrpc_net *rxnet =
  20. container_of(timer, struct rxrpc_net, peer_keepalive_timer);
  21. if (rxnet->live)
  22. rxrpc_queue_work(&rxnet->peer_keepalive_work);
  23. }
  24. /*
  25. * Initialise a per-network namespace record.
  26. */
  27. static __net_init int rxrpc_init_net(struct net *net)
  28. {
  29. struct rxrpc_net *rxnet = rxrpc_net(net);
  30. int ret, i;
  31. rxnet->live = true;
  32. get_random_bytes(&rxnet->epoch, sizeof(rxnet->epoch));
  33. rxnet->epoch |= RXRPC_RANDOM_EPOCH;
  34. INIT_LIST_HEAD(&rxnet->calls);
  35. spin_lock_init(&rxnet->call_lock);
  36. atomic_set(&rxnet->nr_calls, 1);
  37. atomic_set(&rxnet->nr_conns, 1);
  38. INIT_LIST_HEAD(&rxnet->bundle_proc_list);
  39. INIT_LIST_HEAD(&rxnet->conn_proc_list);
  40. INIT_LIST_HEAD(&rxnet->service_conns);
  41. rwlock_init(&rxnet->conn_lock);
  42. INIT_WORK(&rxnet->service_conn_reaper,
  43. rxrpc_service_connection_reaper);
  44. timer_setup(&rxnet->service_conn_reap_timer,
  45. rxrpc_service_conn_reap_timeout, 0);
  46. atomic_set(&rxnet->nr_client_conns, 0);
  47. INIT_HLIST_HEAD(&rxnet->local_endpoints);
  48. mutex_init(&rxnet->local_mutex);
  49. hash_init(rxnet->peer_hash);
  50. spin_lock_init(&rxnet->peer_hash_lock);
  51. for (i = 0; i < ARRAY_SIZE(rxnet->peer_keepalive); i++)
  52. INIT_LIST_HEAD(&rxnet->peer_keepalive[i]);
  53. INIT_LIST_HEAD(&rxnet->peer_keepalive_new);
  54. timer_setup(&rxnet->peer_keepalive_timer,
  55. rxrpc_peer_keepalive_timeout, 0);
  56. INIT_WORK(&rxnet->peer_keepalive_work, rxrpc_peer_keepalive_worker);
  57. rxnet->peer_keepalive_base = ktime_get_seconds();
  58. ret = -ENOMEM;
  59. rxnet->proc_net = proc_net_mkdir(net, "rxrpc", net->proc_net);
  60. if (!rxnet->proc_net)
  61. goto err_proc;
  62. proc_create_net("calls", 0444, rxnet->proc_net, &rxrpc_call_seq_ops,
  63. sizeof(struct seq_net_private));
  64. proc_create_net("conns", 0444, rxnet->proc_net,
  65. &rxrpc_connection_seq_ops,
  66. sizeof(struct seq_net_private));
  67. proc_create_net("bundles", 0444, rxnet->proc_net,
  68. &rxrpc_bundle_seq_ops,
  69. sizeof(struct seq_net_private));
  70. proc_create_net("peers", 0444, rxnet->proc_net,
  71. &rxrpc_peer_seq_ops,
  72. sizeof(struct seq_net_private));
  73. proc_create_net("locals", 0444, rxnet->proc_net,
  74. &rxrpc_local_seq_ops,
  75. sizeof(struct seq_net_private));
  76. proc_create_net_single_write("stats", S_IFREG | 0644, rxnet->proc_net,
  77. rxrpc_stats_show, rxrpc_stats_clear, NULL);
  78. return 0;
  79. err_proc:
  80. rxnet->live = false;
  81. return ret;
  82. }
  83. /*
  84. * Clean up a per-network namespace record.
  85. */
  86. static __net_exit void rxrpc_exit_net(struct net *net)
  87. {
  88. struct rxrpc_net *rxnet = rxrpc_net(net);
  89. rxnet->live = false;
  90. del_timer_sync(&rxnet->peer_keepalive_timer);
  91. cancel_work_sync(&rxnet->peer_keepalive_work);
  92. /* Remove the timer again as the worker may have restarted it. */
  93. del_timer_sync(&rxnet->peer_keepalive_timer);
  94. rxrpc_destroy_all_calls(rxnet);
  95. rxrpc_destroy_all_connections(rxnet);
  96. rxrpc_destroy_all_peers(rxnet);
  97. rxrpc_destroy_all_locals(rxnet);
  98. proc_remove(rxnet->proc_net);
  99. }
  100. struct pernet_operations rxrpc_net_ops = {
  101. .init = rxrpc_init_net,
  102. .exit = rxrpc_exit_net,
  103. .id = &rxrpc_net_id,
  104. .size = sizeof(struct rxrpc_net),
  105. };