net_namespace.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Operations on the network namespace
  3. */
  4. #ifndef __NET_NET_NAMESPACE_H
  5. #define __NET_NET_NAMESPACE_H
  6. #include <asm/atomic.h>
  7. #include <linux/workqueue.h>
  8. #include <linux/list.h>
  9. struct proc_dir_entry;
  10. struct net_device;
  11. struct sock;
  12. struct net {
  13. atomic_t count; /* To decided when the network
  14. * namespace should be freed.
  15. */
  16. atomic_t use_count; /* To track references we
  17. * destroy on demand
  18. */
  19. struct list_head list; /* list of network namespaces */
  20. struct work_struct work; /* work struct for freeing */
  21. struct proc_dir_entry *proc_net;
  22. struct proc_dir_entry *proc_net_stat;
  23. struct proc_dir_entry *proc_net_root;
  24. struct net_device *loopback_dev; /* The loopback */
  25. struct list_head dev_base_head;
  26. struct hlist_head *dev_name_head;
  27. struct hlist_head *dev_index_head;
  28. struct sock *rtnl; /* rtnetlink socket */
  29. };
  30. #ifdef CONFIG_NET
  31. /* Init's network namespace */
  32. extern struct net init_net;
  33. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  34. #else
  35. #define INIT_NET_NS(net_ns)
  36. #endif
  37. extern struct list_head net_namespace_list;
  38. #ifdef CONFIG_NET
  39. extern struct net *copy_net_ns(unsigned long flags, struct net *net_ns);
  40. #else
  41. static inline struct net *copy_net_ns(unsigned long flags, struct net *net_ns)
  42. {
  43. /* There is nothing to copy so this is a noop */
  44. return net_ns;
  45. }
  46. #endif
  47. #ifdef CONFIG_NET_NS
  48. extern void __put_net(struct net *net);
  49. static inline struct net *get_net(struct net *net)
  50. {
  51. atomic_inc(&net->count);
  52. return net;
  53. }
  54. static inline struct net *maybe_get_net(struct net *net)
  55. {
  56. /* Used when we know struct net exists but we
  57. * aren't guaranteed a previous reference count
  58. * exists. If the reference count is zero this
  59. * function fails and returns NULL.
  60. */
  61. if (!atomic_inc_not_zero(&net->count))
  62. net = NULL;
  63. return net;
  64. }
  65. static inline void put_net(struct net *net)
  66. {
  67. if (atomic_dec_and_test(&net->count))
  68. __put_net(net);
  69. }
  70. static inline struct net *hold_net(struct net *net)
  71. {
  72. atomic_inc(&net->use_count);
  73. return net;
  74. }
  75. static inline void release_net(struct net *net)
  76. {
  77. atomic_dec(&net->use_count);
  78. }
  79. #else
  80. static inline struct net *get_net(struct net *net)
  81. {
  82. return net;
  83. }
  84. static inline void put_net(struct net *net)
  85. {
  86. }
  87. static inline struct net *hold_net(struct net *net)
  88. {
  89. return net;
  90. }
  91. static inline void release_net(struct net *net)
  92. {
  93. }
  94. static inline struct net *maybe_get_net(struct net *net)
  95. {
  96. return net;
  97. }
  98. #endif
  99. #define for_each_net(VAR) \
  100. list_for_each_entry(VAR, &net_namespace_list, list)
  101. #ifdef CONFIG_NET_NS
  102. #define __net_init
  103. #define __net_exit
  104. #define __net_initdata
  105. #else
  106. #define __net_init __init
  107. #define __net_exit __exit_refok
  108. #define __net_initdata __initdata
  109. #endif
  110. struct pernet_operations {
  111. struct list_head list;
  112. int (*init)(struct net *net);
  113. void (*exit)(struct net *net);
  114. };
  115. extern int register_pernet_subsys(struct pernet_operations *);
  116. extern void unregister_pernet_subsys(struct pernet_operations *);
  117. extern int register_pernet_device(struct pernet_operations *);
  118. extern void unregister_pernet_device(struct pernet_operations *);
  119. #endif /* __NET_NET_NAMESPACE_H */