[NET]: Basic network namespace infrastructure.
[linux-2.6/kmemtrace.git] / net / core / net_namespace.c
blobf259a9b6fdc1e03e83ce29931a720cd29b80141e
1 #include <linux/workqueue.h>
2 #include <linux/rtnetlink.h>
3 #include <linux/cache.h>
4 #include <linux/slab.h>
5 #include <linux/list.h>
6 #include <linux/delay.h>
7 #include <net/net_namespace.h>
9 /*
10 * Our network namespace constructor/destructor lists
13 static LIST_HEAD(pernet_list);
14 static struct list_head *first_device = &pernet_list;
15 static DEFINE_MUTEX(net_mutex);
17 static DEFINE_MUTEX(net_list_mutex);
18 LIST_HEAD(net_namespace_list);
20 static struct kmem_cache *net_cachep;
22 struct net init_net;
23 EXPORT_SYMBOL_GPL(init_net);
25 void net_lock(void)
27 mutex_lock(&net_list_mutex);
30 void net_unlock(void)
32 mutex_unlock(&net_list_mutex);
35 static struct net *net_alloc(void)
37 return kmem_cache_alloc(net_cachep, GFP_KERNEL);
40 static void net_free(struct net *net)
42 if (!net)
43 return;
45 if (unlikely(atomic_read(&net->use_count) != 0)) {
46 printk(KERN_EMERG "network namespace not free! Usage: %d\n",
47 atomic_read(&net->use_count));
48 return;
51 kmem_cache_free(net_cachep, net);
54 static void cleanup_net(struct work_struct *work)
56 struct pernet_operations *ops;
57 struct list_head *ptr;
58 struct net *net;
60 net = container_of(work, struct net, work);
62 mutex_lock(&net_mutex);
64 /* Don't let anyone else find us. */
65 net_lock();
66 list_del(&net->list);
67 net_unlock();
69 /* Run all of the network namespace exit methods */
70 list_for_each_prev(ptr, &pernet_list) {
71 ops = list_entry(ptr, struct pernet_operations, list);
72 if (ops->exit)
73 ops->exit(net);
76 mutex_unlock(&net_mutex);
78 /* Ensure there are no outstanding rcu callbacks using this
79 * network namespace.
81 rcu_barrier();
83 /* Finally it is safe to free my network namespace structure */
84 net_free(net);
88 void __put_net(struct net *net)
90 /* Cleanup the network namespace in process context */
91 INIT_WORK(&net->work, cleanup_net);
92 schedule_work(&net->work);
94 EXPORT_SYMBOL_GPL(__put_net);
97 * setup_net runs the initializers for the network namespace object.
99 static int setup_net(struct net *net)
101 /* Must be called with net_mutex held */
102 struct pernet_operations *ops;
103 struct list_head *ptr;
104 int error;
106 memset(net, 0, sizeof(struct net));
107 atomic_set(&net->count, 1);
108 atomic_set(&net->use_count, 0);
110 error = 0;
111 list_for_each(ptr, &pernet_list) {
112 ops = list_entry(ptr, struct pernet_operations, list);
113 if (ops->init) {
114 error = ops->init(net);
115 if (error < 0)
116 goto out_undo;
119 out:
120 return error;
121 out_undo:
122 /* Walk through the list backwards calling the exit functions
123 * for the pernet modules whose init functions did not fail.
125 for (ptr = ptr->prev; ptr != &pernet_list; ptr = ptr->prev) {
126 ops = list_entry(ptr, struct pernet_operations, list);
127 if (ops->exit)
128 ops->exit(net);
130 goto out;
133 static int __init net_ns_init(void)
135 int err;
137 printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
138 net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
139 SMP_CACHE_BYTES,
140 SLAB_PANIC, NULL);
141 mutex_lock(&net_mutex);
142 err = setup_net(&init_net);
144 net_lock();
145 list_add_tail(&init_net.list, &net_namespace_list);
146 net_unlock();
148 mutex_unlock(&net_mutex);
149 if (err)
150 panic("Could not setup the initial network namespace");
152 return 0;
155 pure_initcall(net_ns_init);
157 static int register_pernet_operations(struct list_head *list,
158 struct pernet_operations *ops)
160 struct net *net, *undo_net;
161 int error;
163 error = 0;
164 list_add_tail(&ops->list, list);
165 for_each_net(net) {
166 if (ops->init) {
167 error = ops->init(net);
168 if (error)
169 goto out_undo;
172 out:
173 return error;
175 out_undo:
176 /* If I have an error cleanup all namespaces I initialized */
177 list_del(&ops->list);
178 for_each_net(undo_net) {
179 if (undo_net == net)
180 goto undone;
181 if (ops->exit)
182 ops->exit(undo_net);
184 undone:
185 goto out;
188 static void unregister_pernet_operations(struct pernet_operations *ops)
190 struct net *net;
192 list_del(&ops->list);
193 for_each_net(net)
194 if (ops->exit)
195 ops->exit(net);
199 * register_pernet_subsys - register a network namespace subsystem
200 * @ops: pernet operations structure for the subsystem
202 * Register a subsystem which has init and exit functions
203 * that are called when network namespaces are created and
204 * destroyed respectively.
206 * When registered all network namespace init functions are
207 * called for every existing network namespace. Allowing kernel
208 * modules to have a race free view of the set of network namespaces.
210 * When a new network namespace is created all of the init
211 * methods are called in the order in which they were registered.
213 * When a network namespace is destroyed all of the exit methods
214 * are called in the reverse of the order with which they were
215 * registered.
217 int register_pernet_subsys(struct pernet_operations *ops)
219 int error;
220 mutex_lock(&net_mutex);
221 error = register_pernet_operations(first_device, ops);
222 mutex_unlock(&net_mutex);
223 return error;
225 EXPORT_SYMBOL_GPL(register_pernet_subsys);
228 * unregister_pernet_subsys - unregister a network namespace subsystem
229 * @ops: pernet operations structure to manipulate
231 * Remove the pernet operations structure from the list to be
232 * used when network namespaces are created or destoryed. In
233 * addition run the exit method for all existing network
234 * namespaces.
236 void unregister_pernet_subsys(struct pernet_operations *module)
238 mutex_lock(&net_mutex);
239 unregister_pernet_operations(module);
240 mutex_unlock(&net_mutex);
242 EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
245 * register_pernet_device - register a network namespace device
246 * @ops: pernet operations structure for the subsystem
248 * Register a device which has init and exit functions
249 * that are called when network namespaces are created and
250 * destroyed respectively.
252 * When registered all network namespace init functions are
253 * called for every existing network namespace. Allowing kernel
254 * modules to have a race free view of the set of network namespaces.
256 * When a new network namespace is created all of the init
257 * methods are called in the order in which they were registered.
259 * When a network namespace is destroyed all of the exit methods
260 * are called in the reverse of the order with which they were
261 * registered.
263 int register_pernet_device(struct pernet_operations *ops)
265 int error;
266 mutex_lock(&net_mutex);
267 error = register_pernet_operations(&pernet_list, ops);
268 if (!error && (first_device == &pernet_list))
269 first_device = &ops->list;
270 mutex_unlock(&net_mutex);
271 return error;
273 EXPORT_SYMBOL_GPL(register_pernet_device);
276 * unregister_pernet_device - unregister a network namespace netdevice
277 * @ops: pernet operations structure to manipulate
279 * Remove the pernet operations structure from the list to be
280 * used when network namespaces are created or destoryed. In
281 * addition run the exit method for all existing network
282 * namespaces.
284 void unregister_pernet_device(struct pernet_operations *ops)
286 mutex_lock(&net_mutex);
287 if (&ops->list == first_device)
288 first_device = first_device->next;
289 unregister_pernet_operations(ops);
290 mutex_unlock(&net_mutex);
292 EXPORT_SYMBOL_GPL(unregister_pernet_device);