net_namespace.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 {
  11. atomic_t count; /* To decided when the network
  12. * namespace should be freed.
  13. */
  14. atomic_t use_count; /* To track references we
  15. * destroy on demand
  16. */
  17. struct list_head list; /* list of network namespaces */
  18. struct work_struct work; /* work struct for freeing */
  19. struct proc_dir_entry *proc_net;
  20. struct proc_dir_entry *proc_net_stat;
  21. struct proc_dir_entry *proc_net_root;
  22. struct list_head dev_base_head;
  23. struct hlist_head *dev_name_head;
  24. struct hlist_head *dev_index_head;
  25. };
  26. #ifdef CONFIG_NET
  27. /* Init's network namespace */
  28. extern struct net init_net;
  29. #define INIT_NET_NS(net_ns) .net_ns = &init_net,
  30. #else
  31. #define INIT_NET_NS(net_ns)
  32. #endif
  33. extern struct list_head net_namespace_list;
  34. extern void __put_net(struct net *net);
  35. static inline struct net *get_net(struct net *net)
  36. {
  37. atomic_inc(&net->count);
  38. return net;
  39. }
  40. static inline struct net *maybe_get_net(struct net *net)
  41. {
  42. /* Used when we know struct net exists but we
  43. * aren't guaranteed a previous reference count
  44. * exists. If the reference count is zero this
  45. * function fails and returns NULL.
  46. */
  47. if (!atomic_inc_not_zero(&net->count))
  48. net = NULL;
  49. return net;
  50. }
  51. static inline void put_net(struct net *net)
  52. {
  53. if (atomic_dec_and_test(&net->count))
  54. __put_net(net);
  55. }
  56. static inline struct net *hold_net(struct net *net)
  57. {
  58. atomic_inc(&net->use_count);
  59. return net;
  60. }
  61. static inline void release_net(struct net *net)
  62. {
  63. atomic_dec(&net->use_count);
  64. }
  65. extern void net_lock(void);
  66. extern void net_unlock(void);
  67. #define for_each_net(VAR) \
  68. list_for_each_entry(VAR, &net_namespace_list, list)
  69. struct pernet_operations {
  70. struct list_head list;
  71. int (*init)(struct net *net);
  72. void (*exit)(struct net *net);
  73. };
  74. extern int register_pernet_subsys(struct pernet_operations *);
  75. extern void unregister_pernet_subsys(struct pernet_operations *);
  76. extern int register_pernet_device(struct pernet_operations *);
  77. extern void unregister_pernet_device(struct pernet_operations *);
  78. #endif /* __NET_NET_NAMESPACE_H */