2 * linux/kernel/vserver/network.c
4 * Virtual Server: Network Support
6 * Copyright (C) 2003-2007 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
12 * V0.05 and back to locking again
13 * V0.06 changed vcmds to nxi arg
14 * V0.07 have __create claim() the nxi
18 #include <linux/slab.h>
19 #include <linux/rcupdate.h>
22 #include <asm/errno.h>
23 #include <linux/vserver/base.h>
24 #include <linux/vserver/network_cmd.h>
27 atomic_t nx_global_ctotal = ATOMIC_INIT(0);
28 atomic_t nx_global_cactive = ATOMIC_INIT(0);
33 * allocate an initialized nx_info struct
34 * doesn't make it visible (hash) */
36 static struct nx_info *__alloc_nx_info(nid_t nid)
38 struct nx_info *new = NULL;
40 vxdprintk(VXD_CBIT(nid, 1), "alloc_nx_info(%d)*", nid);
42 /* would this benefit from a slab cache? */
43 new = kmalloc(sizeof(struct nx_info), GFP_KERNEL);
47 memset (new, 0, sizeof(struct nx_info));
49 INIT_HLIST_NODE(&new->nx_hlist);
50 atomic_set(&new->nx_usecnt, 0);
51 atomic_set(&new->nx_tasks, 0);
54 new->nx_flags = NXF_INIT_SET;
56 /* rest of init goes here */
58 vxdprintk(VXD_CBIT(nid, 0),
59 "alloc_nx_info(%d) = %p", nid, new);
60 atomic_inc(&nx_global_ctotal);
64 /* __dealloc_nx_info()
66 * final disposal of nx_info */
68 static void __dealloc_nx_info(struct nx_info *nxi)
70 vxdprintk(VXD_CBIT(nid, 0),
71 "dealloc_nx_info(%p)", nxi);
73 nxi->nx_hlist.next = LIST_POISON1;
76 BUG_ON(atomic_read(&nxi->nx_usecnt));
77 BUG_ON(atomic_read(&nxi->nx_tasks));
79 nxi->nx_state |= NXS_RELEASED;
81 atomic_dec(&nx_global_ctotal);
84 static void __shutdown_nx_info(struct nx_info *nxi)
86 nxi->nx_state |= NXS_SHUTDOWN;
87 vs_net_change(nxi, VSC_NETDOWN);
92 void free_nx_info(struct nx_info *nxi)
94 /* context shutdown is mandatory */
95 BUG_ON(nxi->nx_state != NXS_SHUTDOWN);
97 /* context must not be hashed */
98 BUG_ON(nxi->nx_state & NXS_HASHED);
100 BUG_ON(atomic_read(&nxi->nx_usecnt));
101 BUG_ON(atomic_read(&nxi->nx_tasks));
103 __dealloc_nx_info(nxi);
107 /* hash table for nx_info hash */
109 #define NX_HASH_SIZE 13
111 struct hlist_head nx_info_hash[NX_HASH_SIZE];
113 static spinlock_t nx_info_hash_lock = SPIN_LOCK_UNLOCKED;
116 static inline unsigned int __hashval(nid_t nid)
118 return (nid % NX_HASH_SIZE);
125 * add the nxi to the global hash table
126 * requires the hash_lock to be held */
128 static inline void __hash_nx_info(struct nx_info *nxi)
130 struct hlist_head *head;
132 vxd_assert_lock(&nx_info_hash_lock);
133 vxdprintk(VXD_CBIT(nid, 4),
134 "__hash_nx_info: %p[#%d]", nxi, nxi->nx_id);
136 /* context must not be hashed */
137 BUG_ON(nx_info_state(nxi, NXS_HASHED));
139 nxi->nx_state |= NXS_HASHED;
140 head = &nx_info_hash[__hashval(nxi->nx_id)];
141 hlist_add_head(&nxi->nx_hlist, head);
142 atomic_inc(&nx_global_cactive);
145 /* __unhash_nx_info()
147 * remove the nxi from the global hash table
148 * requires the hash_lock to be held */
150 static inline void __unhash_nx_info(struct nx_info *nxi)
152 vxd_assert_lock(&nx_info_hash_lock);
153 vxdprintk(VXD_CBIT(nid, 4),
154 "__unhash_nx_info: %p[#%d.%d.%d]", nxi, nxi->nx_id,
155 atomic_read(&nxi->nx_usecnt), atomic_read(&nxi->nx_tasks));
157 /* context must be hashed */
158 BUG_ON(!nx_info_state(nxi, NXS_HASHED));
159 /* but without tasks */
160 BUG_ON(atomic_read(&nxi->nx_tasks));
162 nxi->nx_state &= ~NXS_HASHED;
163 hlist_del(&nxi->nx_hlist);
164 atomic_dec(&nx_global_cactive);
168 /* __lookup_nx_info()
170 * requires the hash_lock to be held
171 * doesn't increment the nx_refcnt */
173 static inline struct nx_info *__lookup_nx_info(nid_t nid)
175 struct hlist_head *head = &nx_info_hash[__hashval(nid)];
176 struct hlist_node *pos;
179 vxd_assert_lock(&nx_info_hash_lock);
180 hlist_for_each(pos, head) {
181 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
183 if (nxi->nx_id == nid)
188 vxdprintk(VXD_CBIT(nid, 0),
189 "__lookup_nx_info(#%u): %p[#%u]",
190 nid, nxi, nxi?nxi->nx_id:0);
197 * find unused dynamic nid
198 * requires the hash_lock to be held */
200 static inline nid_t __nx_dynamic_id(void)
202 static nid_t seq = MAX_N_CONTEXT;
205 vxd_assert_lock(&nx_info_hash_lock);
207 if (++seq > MAX_N_CONTEXT)
209 if (!__lookup_nx_info(seq)) {
210 vxdprintk(VXD_CBIT(nid, 4),
211 "__nx_dynamic_id: [#%d]", seq);
214 } while (barrier != seq);
218 /* __create_nx_info()
220 * create the requested context
221 * get(), claim() and hash it */
223 static struct nx_info * __create_nx_info(int id)
225 struct nx_info *new, *nxi = NULL;
227 vxdprintk(VXD_CBIT(nid, 1), "create_nx_info(%d)*", id);
229 if (!(new = __alloc_nx_info(id)))
230 return ERR_PTR(-ENOMEM);
232 /* required to make dynamic xids unique */
233 spin_lock(&nx_info_hash_lock);
235 /* dynamic context requested */
236 if (id == NX_DYNAMIC_ID) {
237 #ifdef CONFIG_VSERVER_DYNAMIC_IDS
238 id = __nx_dynamic_id();
240 printk(KERN_ERR "no dynamic context available.\n");
241 nxi = ERR_PTR(-EAGAIN);
246 printk(KERN_ERR "dynamic contexts disabled.\n");
247 nxi = ERR_PTR(-EINVAL);
251 /* static context requested */
252 else if ((nxi = __lookup_nx_info(id))) {
253 vxdprintk(VXD_CBIT(nid, 0),
254 "create_nx_info(%d) = %p (already there)", id, nxi);
255 if (nx_info_flags(nxi, NXF_STATE_SETUP, 0))
256 nxi = ERR_PTR(-EBUSY);
258 nxi = ERR_PTR(-EEXIST);
261 /* dynamic nid creation blocker */
262 else if (id >= MIN_D_CONTEXT) {
263 vxdprintk(VXD_CBIT(nid, 0),
264 "create_nx_info(%d) (dynamic rejected)", id);
265 nxi = ERR_PTR(-EINVAL);
270 vxdprintk(VXD_CBIT(nid, 0),
271 "create_nx_info(%d) = %p (new)", id, new);
272 claim_nx_info(new, NULL);
273 __hash_nx_info(get_nx_info(new));
274 nxi = new, new = NULL;
277 spin_unlock(&nx_info_hash_lock);
279 __dealloc_nx_info(new);
288 void unhash_nx_info(struct nx_info *nxi)
290 __shutdown_nx_info(nxi);
291 spin_lock(&nx_info_hash_lock);
292 __unhash_nx_info(nxi);
293 spin_unlock(&nx_info_hash_lock);
296 #ifdef CONFIG_VSERVER_LEGACYNET
298 struct nx_info *create_nx_info(void)
300 return __create_nx_info(NX_DYNAMIC_ID);
307 * search for a nx_info and get() it
308 * negative id means current */
310 struct nx_info *lookup_nx_info(int id)
312 struct nx_info *nxi = NULL;
315 nxi = get_nx_info(current->nx_info);
317 spin_lock(&nx_info_hash_lock);
318 nxi = get_nx_info(__lookup_nx_info(id));
319 spin_unlock(&nx_info_hash_lock);
326 * verify that nid is still hashed */
328 int nid_is_hashed(nid_t nid)
332 spin_lock(&nx_info_hash_lock);
333 hashed = (__lookup_nx_info(nid) != NULL);
334 spin_unlock(&nx_info_hash_lock);
339 #ifdef CONFIG_PROC_FS
343 * get a subset of hashed nids for proc
344 * assumes size is at least one */
346 int get_nid_list(int index, unsigned int *nids, int size)
348 int hindex, nr_nids = 0;
350 /* only show current and children */
351 if (!nx_check(0, VS_ADMIN|VS_WATCH)) {
354 nids[nr_nids] = nx_current_nid();
358 for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
359 struct hlist_head *head = &nx_info_hash[hindex];
360 struct hlist_node *pos;
362 spin_lock(&nx_info_hash_lock);
363 hlist_for_each(pos, head) {
369 nxi = hlist_entry(pos, struct nx_info, nx_hlist);
370 nids[nr_nids] = nxi->nx_id;
371 if (++nr_nids >= size) {
372 spin_unlock(&nx_info_hash_lock);
376 /* keep the lock time short */
377 spin_unlock(&nx_info_hash_lock);
386 * migrate task to new network
387 * gets nxi, puts old_nxi on change
390 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
392 struct nx_info *old_nxi;
398 vxdprintk(VXD_CBIT(nid, 5),
399 "nx_migrate_task(%p,%p[#%d.%d.%d])",
401 atomic_read(&nxi->nx_usecnt),
402 atomic_read(&nxi->nx_tasks));
404 if (nx_info_flags(nxi, NXF_INFO_PRIVATE, 0) &&
405 !nx_info_flags(nxi, NXF_STATE_SETUP, 0))
408 if (nx_info_state(nxi, NXS_SHUTDOWN))
411 /* maybe disallow this completely? */
412 old_nxi = task_get_nx_info(p);
418 clr_nx_info(&p->nx_info);
419 claim_nx_info(nxi, p);
420 set_nx_info(&p->nx_info, nxi);
424 vxdprintk(VXD_CBIT(nid, 5),
425 "moved task %p into nxi:%p[#%d]",
429 release_nx_info(old_nxi, p);
432 put_nx_info(old_nxi);
439 #include <linux/netdevice.h>
440 #include <linux/inetdevice.h>
442 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
448 return addr_in_nx_info(nxi, ifa->ifa_local);
451 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
453 struct in_device *in_dev;
454 struct in_ifaddr **ifap;
455 struct in_ifaddr *ifa;
463 in_dev = in_dev_get(dev);
467 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
468 ifap = &ifa->ifa_next) {
469 if (addr_in_nx_info(nxi, ifa->ifa_local)) {
480 * check if address is covered by socket
482 * sk: the socket to check against
483 * addr: the address in question (must be != 0)
485 static inline int __addr_in_socket(const struct sock *sk, uint32_t addr)
487 struct nx_info *nxi = sk->sk_nx_info;
488 uint32_t saddr = inet_rcv_saddr(sk);
490 vxdprintk(VXD_CBIT(net, 5),
491 "__addr_in_socket(%p,%d.%d.%d.%d) %p:%d.%d.%d.%d %p;%lx",
492 sk, VXD_QUAD(addr), nxi, VXD_QUAD(saddr), sk->sk_socket,
493 (sk->sk_socket?sk->sk_socket->flags:0));
496 /* direct address match */
497 return (saddr == addr);
499 /* match against nx_info */
500 return addr_in_nx_info(nxi, addr);
502 /* unrestricted any socket */
508 int nx_addr_conflict(struct nx_info *nxi, uint32_t addr, const struct sock *sk)
510 vxdprintk(VXD_CBIT(net, 2),
511 "nx_addr_conflict(%p,%p) %d.%d,%d.%d",
512 nxi, sk, VXD_QUAD(addr));
515 /* check real address */
516 return __addr_in_socket(sk, addr);
518 /* check against nx_info */
519 int i, n = nxi->nbipv4;
522 if (__addr_in_socket(sk, nxi->ipv4[i]))
526 /* check against any */
531 #endif /* CONFIG_INET */
533 void nx_set_persistent(struct nx_info *nxi)
535 vxdprintk(VXD_CBIT(nid, 6),
536 "nx_set_persistent(%p[#%d])", nxi, nxi->nx_id);
539 claim_nx_info(nxi, NULL);
542 void nx_clear_persistent(struct nx_info *nxi)
544 vxdprintk(VXD_CBIT(nid, 6),
545 "nx_clear_persistent(%p[#%d])", nxi, nxi->nx_id);
547 release_nx_info(nxi, NULL);
551 void nx_update_persistent(struct nx_info *nxi)
553 if (nx_info_flags(nxi, NXF_PERSISTENT, 0))
554 nx_set_persistent(nxi);
556 nx_clear_persistent(nxi);
559 /* vserver syscall commands below here */
561 /* taks nid and nx_info functions */
563 #include <asm/uaccess.h>
566 int vc_task_nid(uint32_t id, void __user *data)
571 struct task_struct *tsk;
573 if (!nx_check(0, VS_ADMIN|VS_WATCH))
576 read_lock(&tasklist_lock);
577 tsk = find_task_by_real_pid(id);
578 nid = (tsk) ? tsk->nid : -ESRCH;
579 read_unlock(&tasklist_lock);
582 nid = nx_current_nid();
587 int vc_nx_info(struct nx_info *nxi, void __user *data)
589 struct vcmd_nx_info_v0 vc_data;
591 vc_data.nid = nxi->nx_id;
593 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
599 /* network functions */
601 int vc_net_create(uint32_t nid, void __user *data)
603 struct vcmd_net_create vc_data = { .flagword = NXF_INIT_SET };
604 struct nx_info *new_nxi;
607 if (data && copy_from_user (&vc_data, data, sizeof(vc_data)))
610 if ((nid > MAX_S_CONTEXT) && (nid != NX_DYNAMIC_ID))
615 new_nxi = __create_nx_info(nid);
617 return PTR_ERR(new_nxi);
620 new_nxi->nx_flags = vc_data.flagword;
623 if (vs_net_change(new_nxi, VSC_NETUP))
626 ret = nx_migrate_task(current, new_nxi);
630 /* return context id on success */
631 ret = new_nxi->nx_id;
633 /* get a reference for persistent contexts */
634 if ((vc_data.flagword & NXF_PERSISTENT))
635 nx_set_persistent(new_nxi);
637 release_nx_info(new_nxi, NULL);
638 put_nx_info(new_nxi);
643 int vc_net_migrate(struct nx_info *nxi, void __user *data)
645 return nx_migrate_task(current, nxi);
648 int vc_net_add(struct nx_info *nxi, void __user *data)
650 struct vcmd_net_addr_v0 vc_data;
651 int index, pos, ret = 0;
653 if (data && copy_from_user (&vc_data, data, sizeof(vc_data)))
656 switch (vc_data.type) {
658 if ((vc_data.count < 1) || (vc_data.count > 4))
666 switch (vc_data.type) {
669 while ((index < vc_data.count) &&
670 ((pos = nxi->nbipv4) < NB_IPV4ROOT)) {
671 nxi->ipv4[pos] = vc_data.ip[index];
672 nxi->mask[pos] = vc_data.mask[index];
679 case NXA_TYPE_IPV4|NXA_MOD_BCAST:
680 nxi->v4_bcast = vc_data.ip[0];
691 int vc_net_remove(struct nx_info * nxi, void __user *data)
693 struct vcmd_net_addr_v0 vc_data;
695 if (data && copy_from_user (&vc_data, data, sizeof(vc_data)))
698 switch (vc_data.type) {
709 int vc_get_nflags(struct nx_info *nxi, void __user *data)
711 struct vcmd_net_flags_v0 vc_data;
713 vc_data.flagword = nxi->nx_flags;
715 /* special STATE flag handling */
716 vc_data.mask = vs_mask_flags(~0UL, nxi->nx_flags, NXF_ONE_TIME);
718 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
723 int vc_set_nflags(struct nx_info *nxi, void __user *data)
725 struct vcmd_net_flags_v0 vc_data;
726 uint64_t mask, trigger;
728 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
731 /* special STATE flag handling */
732 mask = vs_mask_mask(vc_data.mask, nxi->nx_flags, NXF_ONE_TIME);
733 trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
735 nxi->nx_flags = vs_mask_flags(nxi->nx_flags,
736 vc_data.flagword, mask);
737 if (trigger & NXF_PERSISTENT)
738 nx_update_persistent(nxi);
743 int vc_get_ncaps(struct nx_info *nxi, void __user *data)
745 struct vcmd_net_caps_v0 vc_data;
747 vc_data.ncaps = nxi->nx_ncaps;
748 vc_data.cmask = ~0UL;
750 if (copy_to_user (data, &vc_data, sizeof(vc_data)))
755 int vc_set_ncaps(struct nx_info *nxi, void __user *data)
757 struct vcmd_net_caps_v0 vc_data;
759 if (copy_from_user (&vc_data, data, sizeof(vc_data)))
762 nxi->nx_ncaps = vs_mask_flags(nxi->nx_ncaps,
763 vc_data.ncaps, vc_data.cmask);
768 #include <linux/module.h>
770 EXPORT_SYMBOL_GPL(free_nx_info);
771 EXPORT_SYMBOL_GPL(unhash_nx_info);