net_namespace.h 3.0 KB

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