net_namespace.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. extern void __put_net(struct net *net);
  46. static inline struct net *get_net(struct net *net)
  47. {
  48. #ifdef CONFIG_NET
  49. atomic_inc(&net->count);
  50. #endif
  51. return net;
  52. }
  53. static inline struct net *maybe_get_net(struct net *net)
  54. {
  55. /* Used when we know struct net exists but we
  56. * aren't guaranteed a previous reference count
  57. * exists. If the reference count is zero this
  58. * function fails and returns NULL.
  59. */
  60. if (!atomic_inc_not_zero(&net->count))
  61. net = NULL;
  62. return net;
  63. }
  64. static inline void put_net(struct net *net)
  65. {
  66. #ifdef CONFIG_NET
  67. if (atomic_dec_and_test(&net->count))
  68. __put_net(net);
  69. #endif
  70. }
  71. static inline struct net *hold_net(struct net *net)
  72. {
  73. #ifdef CONFIG_NET
  74. atomic_inc(&net->use_count);
  75. #endif
  76. return net;
  77. }
  78. static inline void release_net(struct net *net)
  79. {
  80. #ifdef CONFIG_NET
  81. atomic_dec(&net->use_count);
  82. #endif
  83. }
  84. #define for_each_net(VAR) \
  85. list_for_each_entry(VAR, &net_namespace_list, list)
  86. #ifdef CONFIG_NET_NS
  87. #define __net_init
  88. #define __net_exit
  89. #define __net_initdata
  90. #else
  91. #define __net_init __init
  92. #define __net_exit __exit_refok
  93. #define __net_initdata __initdata
  94. #endif
  95. struct pernet_operations {
  96. struct list_head list;
  97. int (*init)(struct net *net);
  98. void (*exit)(struct net *net);
  99. };
  100. extern int register_pernet_subsys(struct pernet_operations *);
  101. extern void unregister_pernet_subsys(struct pernet_operations *);
  102. extern int register_pernet_device(struct pernet_operations *);
  103. extern void unregister_pernet_device(struct pernet_operations *);
  104. #endif /* __NET_NET_NAMESPACE_H */