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/network.h>
18 #include <linux/ninline.h>
19 #include <linux/rcupdate.h>
21 #include <asm/errno.h>
26 * allocate an initialized nx_info struct
27 * doesn't make it visible (hash) */
29 static struct nx_info *__alloc_nx_info(nid_t nid)
31 struct nx_info *new = NULL;
33 nxdprintk("alloc_nx_info()\n");
35 /* would this benefit from a slab cache? */
36 new = kmalloc(sizeof(struct nx_info), GFP_KERNEL);
40 memset (new, 0, sizeof(struct nx_info));
42 INIT_RCU_HEAD(&new->nx_rcu);
43 INIT_HLIST_NODE(&new->nx_hlist);
44 atomic_set(&new->nx_refcnt, 0);
45 atomic_set(&new->nx_usecnt, 0);
47 /* rest of init goes here */
49 nxdprintk("alloc_nx_info() = %p\n", new);
53 /* __dealloc_nx_info()
55 * final disposal of nx_info */
57 static void __dealloc_nx_info(struct nx_info *nxi)
59 nxdprintk("dealloc_nx_info(%p)\n", nxi);
61 nxi->nx_hlist.next = LIST_POISON1;
64 BUG_ON(atomic_read(&nxi->nx_usecnt));
65 BUG_ON(atomic_read(&nxi->nx_refcnt));
71 /* hash table for nx_info hash */
73 #define NX_HASH_SIZE 13
75 struct hlist_head nx_info_hash[NX_HASH_SIZE];
77 static spinlock_t nx_info_hash_lock = SPIN_LOCK_UNLOCKED;
80 static inline unsigned int __hashval(nid_t nid)
82 return (nid % NX_HASH_SIZE);
89 * add the nxi to the global hash table
90 * requires the hash_lock to be held */
92 static inline void __hash_nx_info(struct nx_info *nxi)
94 struct hlist_head *head;
96 nxdprintk("__hash_nx_info: %p[#%d]\n", nxi, nxi->nx_id);
98 head = &nx_info_hash[__hashval(nxi->nx_id)];
99 hlist_add_head_rcu(&nxi->nx_hlist, head);
102 /* __unhash_nx_info()
104 * remove the nxi from the global hash table
105 * requires the hash_lock to be held */
107 static inline void __unhash_nx_info(struct nx_info *nxi)
109 nxdprintk("__unhash_nx_info: %p[#%d]\n", nxi, nxi->nx_id);
110 hlist_del_rcu(&nxi->nx_hlist);
115 /* __lookup_nx_info()
117 * requires the rcu_read_lock()
118 * doesn't increment the nx_refcnt */
120 static inline struct nx_info *__lookup_nx_info(nid_t nid)
122 struct hlist_head *head = &nx_info_hash[__hashval(nid)];
123 struct hlist_node *pos;
125 hlist_for_each(pos, head) {
126 struct nx_info *nxi =
127 hlist_entry(pos, struct nx_info, nx_hlist);
129 if (nxi->nx_id == nid) {
139 * find unused dynamic nid
140 * requires the hash_lock to be held */
142 static inline nid_t __nx_dynamic_id(void)
144 static nid_t seq = MAX_N_CONTEXT;
148 if (++seq > MAX_N_CONTEXT)
150 if (!__lookup_nx_info(seq))
152 } while (barrier != seq);
158 * locate or create the requested context
159 * get() it and if new hash it */
161 static struct nx_info * __loc_nx_info(int id, int *err)
163 struct nx_info *new, *nxi = NULL;
165 nxdprintk("loc_nx_info(%d)\n", id);
167 if (!(new = __alloc_nx_info(id))) {
172 spin_lock(&nx_info_hash_lock);
174 /* dynamic context requested */
175 if (id == NX_DYNAMIC_ID) {
176 id = __nx_dynamic_id();
178 printk(KERN_ERR "no dynamic context available.\n");
183 /* existing context requested */
184 else if ((nxi = __lookup_nx_info(id))) {
185 /* context in setup is not available */
186 if (nxi->nx_flags & VXF_STATE_SETUP) {
187 nxdprintk("loc_nx_info(%d) = %p (not available)\n", id, nxi);
191 nxdprintk("loc_nx_info(%d) = %p (found)\n", id, nxi);
198 /* new context requested */
199 nxdprintk("loc_nx_info(%d) = %p (new)\n", id, new);
200 __hash_nx_info(get_nx_info(new));
201 nxi = new, new = NULL;
205 spin_unlock(&nx_info_hash_lock);
207 __dealloc_nx_info(new);
218 void rcu_free_nx_info(void *obj)
220 struct nx_info *nxi = obj;
223 usecnt = atomic_read(&nxi->nx_usecnt);
226 refcnt = atomic_read(&nxi->nx_refcnt);
230 __dealloc_nx_info(nxi);
232 printk("!!! rcu didn't free\n");
235 void unhash_nx_info(struct nx_info *nxi)
237 spin_lock(&nx_info_hash_lock);
238 __unhash_nx_info(nxi);
239 spin_unlock(&nx_info_hash_lock);
244 * search for a nx_info and get() it
245 * negative id means current */
247 struct nx_info *locate_nx_info(int id)
252 nxi = get_nx_info(current->nx_info);
255 nxi = get_nx_info(__lookup_nx_info(id));
261 /* nx_info_is_hashed()
263 * verify that nid is still hashed */
265 int nx_info_is_hashed(nid_t nid)
270 hashed = (__lookup_nx_info(nid) != NULL);
275 #ifdef CONFIG_VSERVER_LEGACY
277 struct nx_info *locate_or_create_nx_info(int id)
281 return __loc_nx_info(id, &err);
284 struct nx_info *create_nx_info(void)
289 nxdprintk("create_nx_info()\n");
290 if (!(new = __loc_nx_info(NX_DYNAMIC_ID, &err)))
298 #ifdef CONFIG_PROC_FS
300 #define hlist_for_each_rcu(pos, head) \
301 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1;}); \
302 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
304 int get_nid_list(int index, unsigned int *nids, int size)
306 int hindex, nr_nids = 0;
309 for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
310 struct hlist_head *head = &nx_info_hash[hindex];
311 struct hlist_node *pos;
313 hlist_for_each_rcu(pos, head) {
319 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
320 nids[nr_nids] = nxi->nx_id;
321 if (++nr_nids >= size)
333 * migrate task to new network
336 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
338 struct nx_info *old_nxi = task_get_nx_info(p);
344 nxdprintk("nx_migrate_task(%p,%p[#%d.%d.%d])\n",
346 atomic_read(&nxi->nx_usecnt),
347 atomic_read(&nxi->nx_refcnt));
352 /* should be handled in set_nx_info !! */
354 clr_nx_info(&p->nx_info);
355 set_nx_info(&p->nx_info, nxi);
359 // put_nx_info(old_nxi);
361 put_nx_info(old_nxi);
366 #include <linux/netdevice.h>
367 #include <linux/inetdevice.h>
369 static inline int __addr_in_nx_info(u32 addr, struct nx_info *nxi)
374 for (i=0; i<nbip; i++)
375 if (nxi->ipv4[i] == addr)
380 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
383 return __addr_in_nx_info(ifa->ifa_address, nxi);
387 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
389 struct in_device *in_dev = __in_dev_get(dev);
390 struct in_ifaddr **ifap = NULL;
391 struct in_ifaddr *ifa = NULL;
398 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
399 ifap = &ifa->ifa_next) {
400 if (__addr_in_nx_info(ifa->ifa_address, nxi))
409 /* vserver syscall commands below here */
411 /* taks nid and nx_info functions */
413 #include <asm/uaccess.h>
416 int vc_task_nid(uint32_t id, void __user *data)
421 struct task_struct *tsk;
423 if (!vx_check(0, VX_ADMIN|VX_WATCH))
426 read_lock(&tasklist_lock);
427 tsk = find_task_by_pid(id);
428 nid = (tsk) ? tsk->nid : -ESRCH;
429 read_unlock(&tasklist_lock);
437 int vc_nx_info(uint32_t id, void __user *data)
440 struct vcmd_nx_info_v0 vc_data;
442 if (!vx_check(0, VX_ADMIN))
444 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
447 nxi = locate_nx_info(id);
451 vc_data.nid = nxi->nx_id;
454 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
460 /* network functions */
462 int vc_net_create(uint32_t nid, void __user *data)
464 // int ret = -ENOMEM;
465 struct nx_info *new_nxi;
468 if (!capable(CAP_SYS_ADMIN))
471 if ((nid >= MIN_D_CONTEXT) && (nid != VX_DYNAMIC_ID))
477 new_nxi = __loc_nx_info(nid, &ret);
480 if (!(new_nxi->nx_flags & VXF_STATE_SETUP)) {
485 ret = new_nxi->nx_id;
486 nx_migrate_task(current, new_nxi);
488 put_nx_info(new_nxi);
493 int vc_net_migrate(uint32_t id, void __user *data)
497 if (!capable(CAP_SYS_ADMIN))
500 nxi = locate_nx_info(id);
503 nx_migrate_task(current, nxi);
508 int vc_net_add(uint32_t id, void __user *data)
511 struct vcmd_net_nx_v0 vc_data;
513 if (!capable(CAP_SYS_ADMIN))
515 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
518 nxi = locate_nx_info(id);
522 // add ip to net context here
527 int vc_net_remove(uint32_t id, void __user *data)
530 struct vcmd_net_nx_v0 vc_data;
532 if (!capable(CAP_SYS_ADMIN))
534 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
537 nxi = locate_nx_info(id);
541 // rem ip from net context here
548 int vc_get_nflags(uint32_t id, void __user *data)
551 struct vcmd_net_flags_v0 vc_data;
553 if (!capable(CAP_SYS_ADMIN))
556 nxi = locate_nx_info(id);
560 vc_data.flagword = nxi->nx_flags;
562 /* special STATE flag handling */
563 vc_data.mask = vx_mask_flags(~0UL, nxi->nx_flags, IPF_ONE_TIME);
567 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
572 int vc_set_nflags(uint32_t id, void __user *data)
575 struct vcmd_net_flags_v0 vc_data;
576 uint64_t mask, trigger;
578 if (!capable(CAP_SYS_ADMIN))
580 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
583 nxi = locate_nx_info(id);
587 /* special STATE flag handling */
588 mask = vx_mask_mask(vc_data.mask, nxi->nx_flags, IPF_ONE_TIME);
589 trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
590 // if (trigger & IPF_STATE_SETUP)
592 nxi->nx_flags = vx_mask_flags(nxi->nx_flags,
593 vc_data.flagword, mask);
598 int vc_get_ncaps(uint32_t id, void __user *data)
601 struct vcmd_net_caps_v0 vc_data;
603 if (!capable(CAP_SYS_ADMIN))
606 nxi = locate_nx_info(id);
610 vc_data.ncaps = nxi->nx_ncaps;
611 vc_data.cmask = ~0UL;
614 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
619 int vc_set_ncaps(uint32_t id, void __user *data)
622 struct vcmd_net_caps_v0 vc_data;
624 if (!capable(CAP_SYS_ADMIN))
626 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
629 nxi = locate_nx_info(id);
633 nxi->nx_ncaps = vx_mask_flags(nxi->nx_ncaps,
634 vc_data.ncaps, vc_data.cmask);
640 #include <linux/module.h>
642 EXPORT_SYMBOL_GPL(rcu_free_nx_info);
643 EXPORT_SYMBOL_GPL(nx_info_hash_lock);