net_ns.c 3.8 KB

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