VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / kernel / vserver / network.c
1 /*
2  *  linux/kernel/vserver/network.c
3  *
4  *  Virtual Server: Network Support
5  *
6  *  Copyright (C) 2003-2004  Herbert Pƶtzl
7  *
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  *
13  */
14
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>
21
22 #include <asm/errno.h>
23
24
25 /*      __alloc_nx_info()
26
27         * allocate an initialized nx_info struct
28         * doesn't make it visible (hash)                        */
29
30 static struct nx_info *__alloc_nx_info(nid_t nid)
31 {
32         struct nx_info *new = NULL;
33         
34         vxdprintk(VXD_CBIT(nid, 1), "alloc_nx_info(%d)*", nid);
35
36         /* would this benefit from a slab cache? */
37         new = kmalloc(sizeof(struct nx_info), GFP_KERNEL);
38         if (!new)
39                 return 0;
40         
41         memset (new, 0, sizeof(struct nx_info));
42         new->nx_id = nid;
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);
47
48         /* rest of init goes here */
49         
50         vxdprintk(VXD_CBIT(nid, 0),
51                 "alloc_nx_info() = %p", new);
52         return new;
53 }
54
55 /*      __dealloc_nx_info()
56
57         * final disposal of nx_info                             */
58
59 static void __dealloc_nx_info(struct nx_info *nxi)
60 {
61         vxdprintk(VXD_CBIT(nid, 0),
62                 "dealloc_nx_info(%p)", nxi);
63
64         nxi->nx_hlist.next = LIST_POISON1;
65         nxi->nx_id = -1;
66         
67         BUG_ON(atomic_read(&nxi->nx_usecnt));
68         BUG_ON(atomic_read(&nxi->nx_refcnt));
69
70         kfree(nxi);
71 }
72
73
74 /*      hash table for nx_info hash */
75
76 #define NX_HASH_SIZE    13
77
78 struct hlist_head nx_info_hash[NX_HASH_SIZE];
79
80 static spinlock_t nx_info_hash_lock = SPIN_LOCK_UNLOCKED;
81
82
83 static inline unsigned int __hashval(nid_t nid)
84 {
85         return (nid % NX_HASH_SIZE);
86 }
87
88
89
90 /*      __hash_nx_info()
91
92         * add the nxi to the global hash table
93         * requires the hash_lock to be held                     */
94
95 static inline void __hash_nx_info(struct nx_info *nxi)
96 {
97         struct hlist_head *head;
98         
99         vxdprintk(VXD_CBIT(nid, 4),
100                 "__hash_nx_info: %p[#%d]", nxi, nxi->nx_id);
101         get_nx_info(nxi);
102         head = &nx_info_hash[__hashval(nxi->nx_id)];
103         hlist_add_head_rcu(&nxi->nx_hlist, head);
104 }
105
106 /*      __unhash_nx_info()
107
108         * remove the nxi from the global hash table
109         * requires the hash_lock to be held                     */
110
111 static inline void __unhash_nx_info(struct nx_info *nxi)
112 {
113         vxdprintk(VXD_CBIT(nid, 4),
114                 "__unhash_nx_info: %p[#%d]", nxi, nxi->nx_id);
115         hlist_del_rcu(&nxi->nx_hlist);
116         put_nx_info(nxi);
117 }
118
119
120 /*      __lookup_nx_info()
121
122         * requires the rcu_read_lock()
123         * doesn't increment the nx_refcnt                       */
124
125 static inline struct nx_info *__lookup_nx_info(nid_t nid)
126 {
127         struct hlist_head *head = &nx_info_hash[__hashval(nid)];
128         struct hlist_node *pos;
129
130         hlist_for_each_rcu(pos, head) {
131                 struct nx_info *nxi =
132                         hlist_entry(pos, struct nx_info, nx_hlist);
133
134                 if (nxi->nx_id == nid) {
135                         return nxi;
136                 }
137         }
138         return NULL;
139 }
140
141
142 /*      __nx_dynamic_id()
143
144         * find unused dynamic nid
145         * requires the hash_lock to be held                     */
146
147 static inline nid_t __nx_dynamic_id(void)
148 {
149         static nid_t seq = MAX_N_CONTEXT;
150         nid_t barrier = seq;
151         
152         do {
153                 if (++seq > MAX_N_CONTEXT)
154                         seq = MIN_D_CONTEXT;
155                 if (!__lookup_nx_info(seq)) {
156                         vxdprintk(VXD_CBIT(nid, 4),
157                                 "__nx_dynamic_id: [#%d]", seq);
158                         return seq;
159                 }
160         } while (barrier != seq);
161         return 0;
162 }
163
164 /*      __loc_nx_info()
165
166         * locate or create the requested context
167         * get() it and if new hash it                           */
168
169 static struct nx_info * __loc_nx_info(int id, int *err)
170 {
171         struct nx_info *new, *nxi = NULL;
172         
173         vxdprintk(VXD_CBIT(nid, 1), "loc_nx_info(%d)*", id);
174
175         if (!(new = __alloc_nx_info(id))) {
176                 *err = -ENOMEM;
177                 return NULL;
178         }
179
180         spin_lock(&nx_info_hash_lock);
181
182         /* dynamic context requested */
183         if (id == NX_DYNAMIC_ID) {
184                 id = __nx_dynamic_id();
185                 if (!id) {
186                         printk(KERN_ERR "no dynamic context available.\n");
187                         goto out_unlock;
188                 }
189                 new->nx_id = id;
190         }
191         /* existing context requested */
192         else if ((nxi = __lookup_nx_info(id))) {
193                 /* context in setup is not available */
194                 if (nxi->nx_flags & VXF_STATE_SETUP) {
195                         vxdprintk(VXD_CBIT(nid, 0),
196                                 "loc_nx_info(%d) = %p (not available)", id, nxi);
197                         nxi = NULL;
198                         *err = -EBUSY;
199                 } else {
200                         vxdprintk(VXD_CBIT(nid, 0),
201                                 "loc_nx_info(%d) = %p (found)", id, nxi);
202                         get_nx_info(nxi);
203                         *err = 0;
204                 }
205                 goto out_unlock;
206         }
207
208         /* new context requested */
209         vxdprintk(VXD_CBIT(nid, 0),
210                 "loc_nx_info(%d) = %p (new)", id, new);
211         __hash_nx_info(get_nx_info(new));
212         nxi = new, new = NULL;
213         *err = 1;
214
215 out_unlock:
216         spin_unlock(&nx_info_hash_lock);
217         if (new)
218                 __dealloc_nx_info(new);
219         return nxi;
220 }
221
222
223
224 /*      exported stuff                                          */
225
226
227
228
229 void rcu_free_nx_info(struct rcu_head *head)
230 {
231         struct nx_info *nxi = container_of(head, struct nx_info, nx_rcu);
232         int usecnt, refcnt;
233
234         BUG_ON(!nxi || !head);
235
236         usecnt = atomic_read(&nxi->nx_usecnt);
237         BUG_ON(usecnt < 0);
238
239         refcnt = atomic_read(&nxi->nx_refcnt);
240         BUG_ON(refcnt < 0);
241
242         vxdprintk(VXD_CBIT(nid, 3),
243                 "rcu_free_nx_info(%p): uc=%d", nxi, usecnt);
244         if (!usecnt)
245                 __dealloc_nx_info(nxi);
246         else
247                 printk("!!! rcu didn't free\n");
248 }
249
250 void unhash_nx_info(struct nx_info *nxi)
251 {
252         spin_lock(&nx_info_hash_lock);
253         __unhash_nx_info(nxi);
254         spin_unlock(&nx_info_hash_lock);
255 }
256
257 /*      locate_nx_info()
258
259         * search for a nx_info and get() it                     
260         * negative id means current                             */
261
262 struct nx_info *locate_nx_info(int id)
263 {
264         struct nx_info *nxi;
265         
266         if (id < 0) {
267                 nxi = get_nx_info(current->nx_info);
268         } else {
269                 rcu_read_lock();
270                 nxi = get_nx_info(__lookup_nx_info(id));
271                 rcu_read_unlock();
272         }
273         return nxi;
274 }
275
276 /*      nx_info_is_hashed()
277
278         * verify that nid is still hashed                       */
279
280 int nx_info_is_hashed(nid_t nid)
281 {
282         int hashed;
283
284         rcu_read_lock();
285         hashed = (__lookup_nx_info(nid) != NULL);
286         rcu_read_unlock();
287         return hashed;
288 }
289
290 #ifdef  CONFIG_VSERVER_LEGACY
291
292 struct nx_info *locate_or_create_nx_info(int id)
293 {
294         int err;
295
296         return __loc_nx_info(id, &err);
297 }
298
299 struct nx_info *create_nx_info(void)
300 {
301         struct nx_info *new;
302         int err;
303         
304         vxdprintk(VXD_CBIT(nid, 5), "create_nx_info(%s)", "void");
305         if (!(new = __loc_nx_info(NX_DYNAMIC_ID, &err)))
306                 return NULL;
307         return new;
308 }
309
310
311 #endif
312
313 #ifdef  CONFIG_PROC_FS
314
315 #define hlist_for_each_rcu(pos, head) \
316         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1;}); \
317                 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
318
319 int get_nid_list(int index, unsigned int *nids, int size)
320 {
321         int hindex, nr_nids = 0;
322
323         rcu_read_lock();
324         for (hindex = 0; hindex < NX_HASH_SIZE; hindex++) {
325                 struct hlist_head *head = &nx_info_hash[hindex];
326                 struct hlist_node *pos;
327
328                 hlist_for_each_rcu(pos, head) {
329                         struct nx_info *nxi;
330
331                         if (--index > 0)
332                                 continue;
333
334                         nxi = hlist_entry(pos, struct nx_info, nx_hlist);
335                         nids[nr_nids] = nxi->nx_id;                     
336                         if (++nr_nids >= size)
337                                 goto out;
338                 }
339         }
340 out:
341         rcu_read_unlock();
342         return nr_nids;
343 }
344 #endif
345
346
347 /*
348  *      migrate task to new network
349  */
350
351 int nx_migrate_task(struct task_struct *p, struct nx_info *nxi)
352 {
353         struct nx_info *old_nxi;
354         int ret = 0;
355         
356         if (!p || !nxi)
357                 BUG();
358
359         vxdprintk(VXD_CBIT(nid, 5),
360                 "nx_migrate_task(%p,%p[#%d.%d.%d])",
361                 p, nxi, nxi->nx_id,
362                 atomic_read(&nxi->nx_usecnt),
363                 atomic_read(&nxi->nx_refcnt));
364
365         old_nxi = task_get_nx_info(p);
366         if (old_nxi == nxi)
367                 goto out;
368
369         task_lock(p);
370         /* should be handled in set_nx_info !! */
371         if (old_nxi)
372                 clr_nx_info(&p->nx_info);
373         set_nx_info(&p->nx_info, nxi);
374         p->nid = nxi->nx_id;
375         task_unlock(p);
376
377         /* obsoleted by clr/set */
378         // put_nx_info(old_nxi);
379 out:
380         put_nx_info(old_nxi);
381         return ret;
382 }
383
384
385 #include <linux/netdevice.h>
386 #include <linux/inetdevice.h>
387
388 static inline int __addr_in_nx_info(u32 addr, struct nx_info *nxi)
389 {
390         int i, nbip;
391
392         nbip = nxi->nbipv4;
393         for (i=0; i<nbip; i++)
394                 if (nxi->ipv4[i] == addr)
395                         return 1;
396         return 0;
397 }
398
399 int ifa_in_nx_info(struct in_ifaddr *ifa, struct nx_info *nxi)
400 {
401         if (nxi && ifa)
402                 return __addr_in_nx_info(ifa->ifa_address, nxi);
403         return 1;
404 }
405
406 int dev_in_nx_info(struct net_device *dev, struct nx_info *nxi)
407 {
408         struct in_device *in_dev = __in_dev_get(dev);
409         struct in_ifaddr **ifap = NULL;
410         struct in_ifaddr *ifa = NULL;
411
412         if (!nxi)
413                 return 1;
414         if (!in_dev)
415                 return 0;
416
417         for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
418                 ifap = &ifa->ifa_next) {
419                 if (__addr_in_nx_info(ifa->ifa_address, nxi))
420                         return 1;
421         }
422         return 0;
423 }
424
425
426
427
428 /* vserver syscall commands below here */
429
430 /* taks nid and nx_info functions */
431
432 #include <asm/uaccess.h>
433
434
435 int vc_task_nid(uint32_t id, void __user *data)
436 {
437         nid_t nid;
438
439         if (id) {
440                 struct task_struct *tsk;
441
442                 if (!vx_check(0, VX_ADMIN|VX_WATCH))
443                         return -EPERM;
444
445                 read_lock(&tasklist_lock);
446                 tsk = find_task_by_pid(id);
447                 nid = (tsk) ? tsk->nid : -ESRCH;
448                 read_unlock(&tasklist_lock);
449         }
450         else
451                 nid = current->nid;
452         return nid;
453 }
454
455
456 int vc_nx_info(uint32_t id, void __user *data)
457 {
458         struct nx_info *nxi;
459         struct vcmd_nx_info_v0 vc_data;
460
461         if (!vx_check(0, VX_ADMIN))
462                 return -ENOSYS;
463         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RESOURCE))
464                 return -EPERM;
465
466         nxi = locate_nx_info(id);
467         if (!nxi)
468                 return -ESRCH;
469
470         vc_data.nid = nxi->nx_id;
471         put_nx_info(nxi);
472
473         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
474                 return -EFAULT;
475         return 0;
476 }
477
478
479 /* network functions */
480
481 int vc_net_create(uint32_t nid, void __user *data)
482 {
483         // int ret = -ENOMEM;
484         struct nx_info *new_nxi;
485         int ret;
486
487         if (!capable(CAP_SYS_ADMIN))
488                 return -EPERM;
489
490         if ((nid >= MIN_D_CONTEXT) && (nid != VX_DYNAMIC_ID))
491                 return -EINVAL;
492
493         if (nid < 1)
494                 return -EINVAL;
495
496         new_nxi = __loc_nx_info(nid, &ret);
497         if (!new_nxi)
498                 return ret;
499         if (!(new_nxi->nx_flags & VXF_STATE_SETUP)) {
500                 ret = -EEXIST;
501                 goto out_put;
502         }
503
504         ret = new_nxi->nx_id;
505         nx_migrate_task(current, new_nxi);
506 out_put:
507         put_nx_info(new_nxi);
508         return ret;
509 }
510
511
512 int vc_net_migrate(uint32_t id, void __user *data)
513 {
514         struct nx_info *nxi;
515         
516         if (!capable(CAP_SYS_ADMIN))
517                 return -EPERM;
518
519         nxi = locate_nx_info(id);
520         if (!nxi)
521                 return -ESRCH;
522         nx_migrate_task(current, nxi);
523         put_nx_info(nxi);
524         return 0;
525 }
526
527 int vc_net_add(uint32_t id, void __user *data)
528 {
529         struct nx_info *nxi;
530         struct vcmd_net_nx_v0 vc_data;
531
532         if (!capable(CAP_SYS_ADMIN))
533                 return -EPERM;
534         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
535                 return -EFAULT;
536
537         nxi = locate_nx_info(id);
538         if (!nxi)
539                 return -ESRCH;
540
541         // add ip to net context here
542         put_nx_info(nxi);
543         return 0;
544 }
545
546 int vc_net_remove(uint32_t id, void __user *data)
547 {
548         struct nx_info *nxi;
549         struct vcmd_net_nx_v0 vc_data;
550
551         if (!capable(CAP_SYS_ADMIN))
552                 return -EPERM;
553         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
554                 return -EFAULT;
555
556         nxi = locate_nx_info(id);
557         if (!nxi)
558                 return -ESRCH;
559
560         // rem ip from net context here
561         put_nx_info(nxi);
562         return 0;
563 }
564
565
566
567 int vc_get_nflags(uint32_t id, void __user *data)
568 {
569         struct nx_info *nxi;
570         struct vcmd_net_flags_v0 vc_data;
571
572         if (!capable(CAP_SYS_ADMIN))
573                 return -EPERM;
574
575         nxi = locate_nx_info(id);
576         if (!nxi)
577                 return -ESRCH;
578
579         vc_data.flagword = nxi->nx_flags;
580
581         /* special STATE flag handling */
582         vc_data.mask = vx_mask_flags(~0UL, nxi->nx_flags, IPF_ONE_TIME);
583
584         put_nx_info(nxi);
585
586         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
587                 return -EFAULT;
588         return 0;
589 }
590
591 int vc_set_nflags(uint32_t id, void __user *data)
592 {
593         struct nx_info *nxi;
594         struct vcmd_net_flags_v0 vc_data;
595         uint64_t mask, trigger;
596
597         if (!capable(CAP_SYS_ADMIN))
598                 return -EPERM;
599         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
600                 return -EFAULT;
601
602         nxi = locate_nx_info(id);
603         if (!nxi)
604                 return -ESRCH;
605
606         /* special STATE flag handling */
607         mask = vx_mask_mask(vc_data.mask, nxi->nx_flags, IPF_ONE_TIME);
608         trigger = (mask & nxi->nx_flags) ^ (mask & vc_data.flagword);
609         // if (trigger & IPF_STATE_SETUP)
610
611         nxi->nx_flags = vx_mask_flags(nxi->nx_flags,
612                 vc_data.flagword, mask);
613         put_nx_info(nxi);
614         return 0;
615 }
616
617 int vc_get_ncaps(uint32_t id, void __user *data)
618 {
619         struct nx_info *nxi;
620         struct vcmd_net_caps_v0 vc_data;
621
622         if (!capable(CAP_SYS_ADMIN))
623                 return -EPERM;
624
625         nxi = locate_nx_info(id);
626         if (!nxi)
627                 return -ESRCH;
628
629         vc_data.ncaps = nxi->nx_ncaps;
630         vc_data.cmask = ~0UL;
631         put_nx_info(nxi);
632
633         if (copy_to_user (data, &vc_data, sizeof(vc_data)))
634                 return -EFAULT;
635         return 0;
636 }
637
638 int vc_set_ncaps(uint32_t id, void __user *data)
639 {
640         struct nx_info *nxi;
641         struct vcmd_net_caps_v0 vc_data;
642
643         if (!capable(CAP_SYS_ADMIN))
644                 return -EPERM;
645         if (copy_from_user (&vc_data, data, sizeof(vc_data)))
646                 return -EFAULT;
647
648         nxi = locate_nx_info(id);
649         if (!nxi)
650                 return -ESRCH;
651
652         nxi->nx_ncaps = vx_mask_flags(nxi->nx_ncaps,
653                 vc_data.ncaps, vc_data.cmask);
654         put_nx_info(nxi);
655         return 0;
656 }
657
658
659 #include <linux/module.h>
660
661 EXPORT_SYMBOL_GPL(rcu_free_nx_info);
662 EXPORT_SYMBOL_GPL(nx_info_hash_lock);
663 EXPORT_SYMBOL_GPL(unhash_nx_info);
664