2 * linux/kernel/vserver/network.c
4 * Virtual Server: Network Support
6 * Copyright (C) 2003-2004 Herbert Pƶtzl
8 * V0.01 broken out from vcontext V0.05
9 * V0.02 cleaned up implementation
10 * V0.03 added equiv nx commands
11 * V0.04 switch to RCU based hash
15 #include <linux/config.h>
16 #include <linux/slab.h>
17 #include <linux/vserver.h>
18 #include <linux/vs_base.h>
19 #include <linux/vs_network.h>
20 #include <linux/rcupdate.h>
22 #include <asm/errno.h>
27 * allocate an initialized nx_info struct
28 * doesn't make it visible (hash) */
30 static struct nx_info *__alloc_nx_info(nid_t nid)
32 struct nx_info *new = NULL;
34 nxdprintk("alloc_nx_info()\n");
36 /* would this benefit from a slab cache? */
37 new = kmalloc(sizeof(struct nx_info), GFP_KERNEL);
41 memset (new, 0, sizeof(struct nx_info));
43 INIT_RCU_HEAD(&new->nx_rcu);
44 INIT_HLIST_NODE(&new->nx_hlist);
45 atomic_set(&new->nx_refcnt, 0);
46 atomic_set(&new->nx_usecnt, 0);
48 /* rest of init goes here */
50 nxdprintk("alloc_nx_info() = %p\n", new);
54 /* __dealloc_nx_info()
56 * final disposal of nx_info */
58 static void __dealloc_nx_info(struct nx_info *nxi)
60 nxdprintk("dealloc_nx_info(%p)\n", nxi);
62 nxi->nx_hlist.next = LIST_POISON1;
65 BUG_ON(atomic_read(&nxi->nx_usecnt));
66 BUG_ON(atomic_read(&nxi->nx_refcnt));
72 /* hash table for nx_info hash */
74 #define NX_HASH_SIZE 13
76 struct hlist_head nx_info_hash[NX_HASH_SIZE];
78 static spinlock_t nx_info_hash_lock = SPIN_LOCK_UNLOCKED;
81 static inline unsigned int __hashval(nid_t nid)
83 return (nid % NX_HASH_SIZE);
90 * add the nxi to the global hash table
91 * requires the hash_lock to be held */
93 static inline void __hash_nx_info(struct nx_info *nxi)
95 struct hlist_head *head;
97 nxdprintk("__hash_nx_info: %p[#%d]\n", nxi, nxi->nx_id);
99 head = &nx_info_hash[__hashval(nxi->nx_id)];
100 hlist_add_head_rcu(&nxi->nx_hlist, head);
103 /* __unhash_nx_info()
105 * remove the nxi from the global hash table
106 * requires the hash_lock to be held */
108 static inline void __unhash_nx_info(struct nx_info *nxi)
110 nxdprintk("__unhash_nx_info: %p[#%d]\n", nxi, nxi->nx_id);
111 hlist_del_rcu(&nxi->nx_hlist);
116 /* __lookup_nx_info()
118 * requires the rcu_read_lock()
119 * doesn't increment the nx_refcnt */
121 static inline struct nx_info *__lookup_nx_info(nid_t nid)
123 struct hlist_head *head = &nx_info_hash[__hashval(nid)];
124 struct hlist_node *pos;
126 hlist_for_each_rcu(pos, head) {
127 struct nx_info *nxi =
128 hlist_entry(pos, struct nx_info, nx_hlist);
130 if (nxi->nx_id == nid) {
140 * find unused dynamic nid
141 * requires the hash_lock to be held */
143 static inline nid_t __nx_dynamic_id(void)
145 static nid_t seq = MAX_N_CONTEXT;
149 if (++seq > MAX_N_CONTEXT)
151 if (!__lookup_nx_info(seq))
153 } while (barrier != seq);
159 * locate or create the requested context
160 * get() it and if new hash it */
162 static struct nx_info * __loc_nx_info(int id, int *err)
164 struct nx_info *new, *nxi = NULL;
166 nxdprintk("loc_nx_info(%d)\n", id);
168 if (!(new = __alloc_nx_info(id))) {
173 spin_lock(&nx_info_hash_lock);
175 /* dynamic context requested */
176 if (id == NX_DYNAMIC_ID) {
177 id = __nx_dynamic_id();
179 printk(KERN_ERR "no dynamic context available.\n");
184 /* existing context requested */
185 else if ((nxi = __lookup_nx_info(id))) {
186 /* context in setup is not available */
187 if (nxi->nx_flags & VXF_STATE_SETUP) {
188 nxdprintk("loc_nx_info(%d) = %p (not available)\n", id, nxi);
192 nxdprintk("loc_nx_info(%d) = %p (found)\n", id, nxi);
199 /* new context requested */
200 nxdprintk("loc_nx_info(%d) = %p (new)\n", id, new);
201 __hash_nx_info(get_nx_info(new));
202 nxi = new, new = NULL;
206 spin_unlock(&nx_info_hash_lock);
208 __dealloc_nx_info(new);
219 void rcu_free_nx_info(struct rcu_head *head)
221 struct nx_info *nxi = container_of(head, struct nx_info, nx_rcu);
224 BUG_ON(!nxi || !head);
226 usecnt = atomic_read(&nxi->nx_usecnt);
229 refcnt = atomic_read(&nxi->nx_refcnt);
233 __dealloc_nx_info(nxi);
235 printk("!!! rcu didn't free\n");
238 void unhash_nx_info(struct nx_info *nxi)
240 spin_lock(&nx_info_hash_lock);
241 __unhash_nx_info(nxi);
242 spin_unlock(&nx_info_hash_lock);
247 * search for a nx_info and get() it
248 * negative id means current */
250 struct nx_info *locate_nx_info(int id)
255 nxi = get_nx_info(current->nx_info);
258 nxi = get_nx_info(__lookup_nx_info(id));
264 /* nx_info_is_hashed()
266 * verify that nid is still hashed */
268 int nx_info_is_hashed(nid_t nid)
273 hashed = (__lookup_nx_info(nid) != NULL);
278 #ifdef CONFIG_VSERVER_LEGACY
280 struct nx_info *locate_or_create_nx_info(int id)
284 return __loc_nx_info(id, &err);
287 struct nx_info *create_nx_info(void)
292 nxdprintk("create_nx_info()\n");
293 if (!(new = __loc_nx_info(NX_DYNAMIC_ID, &err)))
301 #ifdef CONFIG_PROC_FS
303 #define hlist_for_each_rcu(pos, head) \
304 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1;}); \
305 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
307 int get_nid_list(int index, unsigned int *nids, int size)
309 int hindex, nr_nids = 0;
312 for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
313 struct hlist_head *head = &nx_info_hash[hindex];
314 struct hlist_node *pos;
316 hlist_for_each_rcu(pos, head) {
322 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
323 nids[nr_nids] = nxi->nx_id;
324 if (++nr_nids >= size)
336 * migrate task to new network
339 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
341 struct nx_info *old_nxi;
347 nxdprintk("nx_migrate_task(%p,%p[#%d.%d.%d])\n",
349 atomic_read(&nxi->nx_usecnt),
350 atomic_read(&nxi->nx_refcnt));
352 old_nxi = task_get_nx_info(p);
357 /* should be handled in set_nx_info !! */
359 clr_nx_info(&p->nx_info);
360 set_nx_info(&p->nx_info, nxi);
364 /* obsoleted by clr/set */
365 // put_nx_info(old_nxi);
367 put_nx_info(old_nxi);
372 #include <linux/netdevice.h>
373 #include <linux/inetdevice.h>
375 static inline int __addr_in_nx_info(u32 addr, struct nx_info *nxi)
380 for (i=0; i<nbip; i++)
381 if (nxi->ipv4[i] == addr)
386 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
389 return __addr_in_nx_info(ifa->ifa_address, nxi);
393 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
395 struct in_device *in_dev = __in_dev_get(dev);
396 struct in_ifaddr **ifap = NULL;
397 struct in_ifaddr *ifa = NULL;
404 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
405 ifap = &ifa->ifa_next) {
406 if (__addr_in_nx_info(ifa->ifa_address, nxi))
415 /* vserver syscall commands below here */
417 /* taks nid and nx_info functions */
419 #include <asm/uaccess.h>
422 int vc_task_nid(uint32_t id, void __user *data)
427 struct task_struct *tsk;
429 if (!vx_check(0, VX_ADMIN|VX_WATCH))
432 read_lock(&tasklist_lock);
433 tsk = find_task_by_pid(id);
434 nid = (tsk) ? tsk->nid : -ESRCH;
435 read_unlock(&tasklist_lock);
443 int vc_nx_info(uint32_t id, void __user *data)
446 struct vcmd_nx_info_v0 vc_data;
448 if (!vx_check(0, VX_ADMIN))
450 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
453 nxi = locate_nx_info(id);
457 vc_data.nid = nxi->nx_id;
460 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
466 /* network functions */
468 int vc_net_create(uint32_t nid, void __user *data)
470 // int ret = -ENOMEM;
471 struct nx_info *new_nxi;
474 if (!capable(CAP_SYS_ADMIN))
477 if ((nid >= MIN_D_CONTEXT) && (nid != VX_DYNAMIC_ID))
483 new_nxi = __loc_nx_info(nid, &ret);
486 if (!(new_nxi->nx_flags & VXF_STATE_SETUP)) {
491 ret = new_nxi->nx_id;
492 nx_migrate_task(current, new_nxi);
494 put_nx_info(new_nxi);
499 int vc_net_migrate(uint32_t id, void __user *data)
503 if (!capable(CAP_SYS_ADMIN))
506 nxi = locate_nx_info(id);
509 nx_migrate_task(current, nxi);
514 int vc_net_add(uint32_t id, void __user *data)
517 struct vcmd_net_nx_v0 vc_data;
519 if (!capable(CAP_SYS_ADMIN))
521 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
524 nxi = locate_nx_info(id);
528 // add ip to net context here
533 int vc_net_remove(uint32_t id, void __user *data)
536 struct vcmd_net_nx_v0 vc_data;
538 if (!capable(CAP_SYS_ADMIN))
540 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
543 nxi = locate_nx_info(id);
547 // rem ip from net context here
554 int vc_get_nflags(uint32_t id, void __user *data)
557 struct vcmd_net_flags_v0 vc_data;
559 if (!capable(CAP_SYS_ADMIN))
562 nxi = locate_nx_info(id);
566 vc_data.flagword = nxi->nx_flags;
568 /* special STATE flag handling */
569 vc_data.mask = vx_mask_flags(~0UL, nxi->nx_flags, IPF_ONE_TIME);
573 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
578 int vc_set_nflags(uint32_t id, void __user *data)
581 struct vcmd_net_flags_v0 vc_data;
582 uint64_t mask, trigger;
584 if (!capable(CAP_SYS_ADMIN))
586 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
589 nxi = locate_nx_info(id);
593 /* special STATE flag handling */
594 mask = vx_mask_mask(vc_data.mask, nxi->nx_flags, IPF_ONE_TIME);
595 trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
596 // if (trigger & IPF_STATE_SETUP)
598 nxi->nx_flags = vx_mask_flags(nxi->nx_flags,
599 vc_data.flagword, mask);
604 int vc_get_ncaps(uint32_t id, void __user *data)
607 struct vcmd_net_caps_v0 vc_data;
609 if (!capable(CAP_SYS_ADMIN))
612 nxi = locate_nx_info(id);
616 vc_data.ncaps = nxi->nx_ncaps;
617 vc_data.cmask = ~0UL;
620 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
625 int vc_set_ncaps(uint32_t id, void __user *data)
628 struct vcmd_net_caps_v0 vc_data;
630 if (!capable(CAP_SYS_ADMIN))
632 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
635 nxi = locate_nx_info(id);
639 nxi->nx_ncaps = vx_mask_flags(nxi->nx_ncaps,
640 vc_data.ncaps, vc_data.cmask);
646 #include <linux/module.h>
648 EXPORT_SYMBOL_GPL(rcu_free_nx_info);
649 EXPORT_SYMBOL_GPL(nx_info_hash_lock);
650 EXPORT_SYMBOL_GPL(unhash_nx_info);