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(void *obj)
221 struct nx_info *nxi = obj;
224 usecnt = atomic_read(&nxi->nx_usecnt);
227 refcnt = atomic_read(&nxi->nx_refcnt);
231 __dealloc_nx_info(nxi);
233 printk("!!! rcu didn't free\n");
236 void unhash_nx_info(struct nx_info *nxi)
238 spin_lock(&nx_info_hash_lock);
239 __unhash_nx_info(nxi);
240 spin_unlock(&nx_info_hash_lock);
245 * search for a nx_info and get() it
246 * negative id means current */
248 struct nx_info *locate_nx_info(int id)
253 nxi = get_nx_info(current->nx_info);
256 nxi = get_nx_info(__lookup_nx_info(id));
262 /* nx_info_is_hashed()
264 * verify that nid is still hashed */
266 int nx_info_is_hashed(nid_t nid)
271 hashed = (__lookup_nx_info(nid) != NULL);
276 #ifdef CONFIG_VSERVER_LEGACY
278 struct nx_info *locate_or_create_nx_info(int id)
282 return __loc_nx_info(id, &err);
285 struct nx_info *create_nx_info(void)
290 nxdprintk("create_nx_info()\n");
291 if (!(new = __loc_nx_info(NX_DYNAMIC_ID, &err)))
299 #ifdef CONFIG_PROC_FS
301 #define hlist_for_each_rcu(pos, head) \
302 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1;}); \
303 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
305 int get_nid_list(int index, unsigned int *nids, int size)
307 int hindex, nr_nids = 0;
310 for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
311 struct hlist_head *head = &nx_info_hash[hindex];
312 struct hlist_node *pos;
314 hlist_for_each_rcu(pos, head) {
320 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
321 nids[nr_nids] = nxi->nx_id;
322 if (++nr_nids >= size)
334 * migrate task to new network
337 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
339 struct nx_info *old_nxi;
345 nxdprintk("nx_migrate_task(%p,%p[#%d.%d.%d])\n",
347 atomic_read(&nxi->nx_usecnt),
348 atomic_read(&nxi->nx_refcnt));
350 old_nxi = task_get_nx_info(p);
355 /* should be handled in set_nx_info !! */
357 clr_nx_info(&p->nx_info);
358 set_nx_info(&p->nx_info, nxi);
362 /* obsoleted by clr/set */
363 // put_nx_info(old_nxi);
365 put_nx_info(old_nxi);
370 #include <linux/netdevice.h>
371 #include <linux/inetdevice.h>
373 static inline int __addr_in_nx_info(u32 addr, struct nx_info *nxi)
378 for (i=0; i<nbip; i++)
379 if (nxi->ipv4[i] == addr)
384 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
387 return __addr_in_nx_info(ifa->ifa_address, nxi);
391 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
393 struct in_device *in_dev = __in_dev_get(dev);
394 struct in_ifaddr **ifap = NULL;
395 struct in_ifaddr *ifa = NULL;
402 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
403 ifap = &ifa->ifa_next) {
404 if (__addr_in_nx_info(ifa->ifa_address, nxi))
413 /* vserver syscall commands below here */
415 /* taks nid and nx_info functions */
417 #include <asm/uaccess.h>
420 int vc_task_nid(uint32_t id, void __user *data)
425 struct task_struct *tsk;
427 if (!vx_check(0, VX_ADMIN|VX_WATCH))
430 read_lock(&tasklist_lock);
431 tsk = find_task_by_pid(id);
432 nid = (tsk) ? tsk->nid : -ESRCH;
433 read_unlock(&tasklist_lock);
441 int vc_nx_info(uint32_t id, void __user *data)
444 struct vcmd_nx_info_v0 vc_data;
446 if (!vx_check(0, VX_ADMIN))
448 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
451 nxi = locate_nx_info(id);
455 vc_data.nid = nxi->nx_id;
458 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
464 /* network functions */
466 int vc_net_create(uint32_t nid, void __user *data)
468 // int ret = -ENOMEM;
469 struct nx_info *new_nxi;
472 if (!capable(CAP_SYS_ADMIN))
475 if ((nid >= MIN_D_CONTEXT) && (nid != VX_DYNAMIC_ID))
481 new_nxi = __loc_nx_info(nid, &ret);
484 if (!(new_nxi->nx_flags & VXF_STATE_SETUP)) {
489 ret = new_nxi->nx_id;
490 nx_migrate_task(current, new_nxi);
492 put_nx_info(new_nxi);
497 int vc_net_migrate(uint32_t id, void __user *data)
501 if (!capable(CAP_SYS_ADMIN))
504 nxi = locate_nx_info(id);
507 nx_migrate_task(current, nxi);
512 int vc_net_add(uint32_t id, void __user *data)
515 struct vcmd_net_nx_v0 vc_data;
517 if (!capable(CAP_SYS_ADMIN))
519 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
522 nxi = locate_nx_info(id);
526 // add ip to net context here
531 int vc_net_remove(uint32_t id, void __user *data)
534 struct vcmd_net_nx_v0 vc_data;
536 if (!capable(CAP_SYS_ADMIN))
538 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
541 nxi = locate_nx_info(id);
545 // rem ip from net context here
552 int vc_get_nflags(uint32_t id, void __user *data)
555 struct vcmd_net_flags_v0 vc_data;
557 if (!capable(CAP_SYS_ADMIN))
560 nxi = locate_nx_info(id);
564 vc_data.flagword = nxi->nx_flags;
566 /* special STATE flag handling */
567 vc_data.mask = vx_mask_flags(~0UL, nxi->nx_flags, IPF_ONE_TIME);
571 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
576 int vc_set_nflags(uint32_t id, void __user *data)
579 struct vcmd_net_flags_v0 vc_data;
580 uint64_t mask, trigger;
582 if (!capable(CAP_SYS_ADMIN))
584 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
587 nxi = locate_nx_info(id);
591 /* special STATE flag handling */
592 mask = vx_mask_mask(vc_data.mask, nxi->nx_flags, IPF_ONE_TIME);
593 trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
594 // if (trigger & IPF_STATE_SETUP)
596 nxi->nx_flags = vx_mask_flags(nxi->nx_flags,
597 vc_data.flagword, mask);
602 int vc_get_ncaps(uint32_t id, void __user *data)
605 struct vcmd_net_caps_v0 vc_data;
607 if (!capable(CAP_SYS_ADMIN))
610 nxi = locate_nx_info(id);
614 vc_data.ncaps = nxi->nx_ncaps;
615 vc_data.cmask = ~0UL;
618 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
623 int vc_set_ncaps(uint32_t id, void __user *data)
626 struct vcmd_net_caps_v0 vc_data;
628 if (!capable(CAP_SYS_ADMIN))
630 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
633 nxi = locate_nx_info(id);
637 nxi->nx_ncaps = vx_mask_flags(nxi->nx_ncaps,
638 vc_data.ncaps, vc_data.cmask);
644 #include <linux/module.h>
646 EXPORT_SYMBOL_GPL(rcu_free_nx_info);
647 EXPORT_SYMBOL_GPL(nx_info_hash_lock);
648 EXPORT_SYMBOL_GPL(unhash_nx_info);