[PATCH] 6pack persistence fix
[linux-2.6] / drivers / net / shaper.c
1 /*
2  *                      Simple traffic shaper for Linux NET3.
3  *
4  *      (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
5  *                              http://www.redhat.com
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *      
12  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide 
13  *      warranty for any of this software. This material is provided 
14  *      "AS-IS" and at no charge.       
15  *
16  *      
17  *      Algorithm:
18  *
19  *      Queue Frame:
20  *              Compute time length of frame at regulated speed
21  *              Add frame to queue at appropriate point
22  *              Adjust time length computation for followup frames
23  *              Any frame that falls outside of its boundaries is freed
24  *
25  *      We work to the following constants
26  *
27  *              SHAPER_QLEN     Maximum queued frames
28  *              SHAPER_LATENCY  Bounding latency on a frame. Leaving this latency
29  *                              window drops the frame. This stops us queueing 
30  *                              frames for a long time and confusing a remote
31  *                              host.
32  *              SHAPER_MAXSLIP  Maximum time a priority frame may jump forward.
33  *                              That bounds the penalty we will inflict on low
34  *                              priority traffic.
35  *              SHAPER_BURST    Time range we call "now" in order to reduce
36  *                              system load. The more we make this the burstier
37  *                              the behaviour, the better local performance you
38  *                              get through packet clustering on routers and the
39  *                              worse the remote end gets to judge rtts.
40  *
41  *      This is designed to handle lower speed links ( < 200K/second or so). We
42  *      run off a 100-150Hz base clock typically. This gives us a resolution at
43  *      200Kbit/second of about 2Kbit or 256 bytes. Above that our timer
44  *      resolution may start to cause much more burstiness in the traffic. We
45  *      could avoid a lot of that by calling kick_shaper() at the end of the 
46  *      tied device transmissions. If you run above about 100K second you 
47  *      may need to tune the supposed speed rate for the right values.
48  *
49  *      BUGS:
50  *              Downing the interface under the shaper before the shaper
51  *              will render your machine defunct. Don't for now shape over
52  *              PPP or SLIP therefore!
53  *              This will be fixed in BETA4
54  *
55  * Update History :
56  *
57  *              bh_atomic() SMP races fixes and rewritten the locking code to
58  *              be SMP safe and irq-mask friendly.
59  *              NOTE: we can't use start_bh_atomic() in kick_shaper()
60  *              because it's going to be recalled from an irq handler,
61  *              and synchronize_bh() is a nono if called from irq context.
62  *                                              1999  Andrea Arcangeli
63  *
64  *              Device statistics (tx_pakets, tx_bytes,
65  *              tx_drops: queue_over_time and collisions: max_queue_exceded)
66  *                               1999/06/18 Jordi Murgo <savage@apostols.org>
67  *
68  *              Use skb->cb for private data.
69  *                               2000/03 Andi Kleen
70  */
71  
72 #include <linux/config.h>
73 #include <linux/module.h>
74 #include <linux/kernel.h>
75 #include <linux/fcntl.h>
76 #include <linux/mm.h>
77 #include <linux/slab.h>
78 #include <linux/string.h>
79 #include <linux/errno.h>
80 #include <linux/netdevice.h>
81 #include <linux/etherdevice.h>
82 #include <linux/skbuff.h>
83 #include <linux/if_arp.h>
84 #include <linux/init.h>
85 #include <linux/if_shaper.h>
86
87 #include <net/dst.h>
88 #include <net/arp.h>
89
90 struct shaper_cb { 
91         unsigned long   shapeclock;             /* Time it should go out */
92         unsigned long   shapestamp;             /* Stamp for shaper    */
93         __u32           shapelatency;           /* Latency on frame */
94         __u32           shapelen;               /* Frame length in clocks */
95         __u16           shapepend;              /* Pending */
96 }; 
97 #define SHAPERCB(skb) ((struct shaper_cb *) ((skb)->cb))
98
99 static int sh_debug;            /* Debug flag */
100
101 #define SHAPER_BANNER   "CymruNet Traffic Shaper BETA 0.04 for Linux 2.1\n"
102
103 static void shaper_kick(struct shaper *sh);
104
105 /*
106  *      Compute clocks on a buffer
107  */
108   
109 static int shaper_clocks(struct shaper *shaper, struct sk_buff *skb)
110 {
111         int t=skb->len/shaper->bytespertick;
112         return t;
113 }
114
115 /*
116  *      Set the speed of a shaper. We compute this in bytes per tick since
117  *      thats how the machine wants to run. Quoted input is in bits per second
118  *      as is traditional (note not BAUD). We assume 8 bit bytes. 
119  */
120   
121 static void shaper_setspeed(struct shaper *shaper, int bitspersec)
122 {
123         shaper->bitspersec=bitspersec;
124         shaper->bytespertick=(bitspersec/HZ)/8;
125         if(!shaper->bytespertick)
126                 shaper->bytespertick++;
127 }
128
129 /*
130  *      Throw a frame at a shaper.
131  */
132   
133
134 static int shaper_start_xmit(struct sk_buff *skb, struct net_device *dev)
135 {
136         struct shaper *shaper = dev->priv;
137         struct sk_buff *ptr;
138   
139         spin_lock(&shaper->lock);
140         ptr=shaper->sendq.prev;
141         
142         /*
143          *      Set up our packet details
144          */
145          
146         SHAPERCB(skb)->shapelatency=0;
147         SHAPERCB(skb)->shapeclock=shaper->recovery;
148         if(time_before(SHAPERCB(skb)->shapeclock, jiffies))
149                 SHAPERCB(skb)->shapeclock=jiffies;
150         skb->priority=0;        /* short term bug fix */
151         SHAPERCB(skb)->shapestamp=jiffies;
152         
153         /*
154          *      Time slots for this packet.
155          */
156          
157         SHAPERCB(skb)->shapelen= shaper_clocks(shaper,skb);
158         
159 #ifdef SHAPER_COMPLEX /* and broken.. */
160
161         while(ptr && ptr!=(struct sk_buff *)&shaper->sendq)
162         {
163                 if(ptr->pri<skb->pri 
164                         && jiffies - SHAPERCB(ptr)->shapeclock < SHAPER_MAXSLIP)
165                 {
166                         struct sk_buff *tmp=ptr->prev;
167
168                         /*
169                          *      It goes before us therefore we slip the length
170                          *      of the new frame.
171                          */
172
173                         SHAPERCB(ptr)->shapeclock+=SHAPERCB(skb)->shapelen;
174                         SHAPERCB(ptr)->shapelatency+=SHAPERCB(skb)->shapelen;
175
176                         /*
177                          *      The packet may have slipped so far back it
178                          *      fell off.
179                          */
180                         if(SHAPERCB(ptr)->shapelatency > SHAPER_LATENCY)
181                         {
182                                 skb_unlink(ptr);
183                                 dev_kfree_skb(ptr);
184                         }
185                         ptr=tmp;
186                 }
187                 else
188                         break;
189         }
190         if(ptr==NULL || ptr==(struct sk_buff *)&shaper->sendq)
191                 skb_queue_head(&shaper->sendq,skb);
192         else
193         {
194                 struct sk_buff *tmp;
195                 /*
196                  *      Set the packet clock out time according to the
197                  *      frames ahead. Im sure a bit of thought could drop
198                  *      this loop.
199                  */
200                 for(tmp=skb_peek(&shaper->sendq); tmp!=NULL && tmp!=ptr; tmp=tmp->next)
201                         SHAPERCB(skb)->shapeclock+=tmp->shapelen;
202                 skb_append(ptr,skb);
203         }
204 #else
205         {
206                 struct sk_buff *tmp;
207                 /*
208                  *      Up our shape clock by the time pending on the queue
209                  *      (Should keep this in the shaper as a variable..)
210                  */
211                 for(tmp=skb_peek(&shaper->sendq); tmp!=NULL && 
212                         tmp!=(struct sk_buff *)&shaper->sendq; tmp=tmp->next)
213                         SHAPERCB(skb)->shapeclock+=SHAPERCB(tmp)->shapelen;
214                 /*
215                  *      Queue over time. Spill packet.
216                  */
217                 if(SHAPERCB(skb)->shapeclock-jiffies > SHAPER_LATENCY) {
218                         dev_kfree_skb(skb);
219                         shaper->stats.tx_dropped++;
220                 } else
221                         skb_queue_tail(&shaper->sendq, skb);
222         }
223 #endif  
224         if(sh_debug)
225                 printk("Frame queued.\n");
226         if(skb_queue_len(&shaper->sendq)>SHAPER_QLEN)
227         {
228                 ptr=skb_dequeue(&shaper->sendq);
229                 dev_kfree_skb(ptr);
230                 shaper->stats.collisions++;
231         }
232         shaper_kick(shaper);
233         spin_unlock(&shaper->lock);
234         return 0;
235 }
236
237 /*
238  *      Transmit from a shaper
239  */
240  
241 static void shaper_queue_xmit(struct shaper *shaper, struct sk_buff *skb)
242 {
243         struct sk_buff *newskb=skb_clone(skb, GFP_ATOMIC);
244         if(sh_debug)
245                 printk("Kick frame on %p\n",newskb);
246         if(newskb)
247         {
248                 newskb->dev=shaper->dev;
249                 newskb->priority=2;
250                 if(sh_debug)
251                         printk("Kick new frame to %s, %d\n",
252                                 shaper->dev->name,newskb->priority);
253                 dev_queue_xmit(newskb);
254
255                 shaper->stats.tx_bytes += skb->len;
256                 shaper->stats.tx_packets++;
257
258                 if(sh_debug)
259                         printk("Kicked new frame out.\n");
260                 dev_kfree_skb(skb);
261         }
262 }
263
264 /*
265  *      Timer handler for shaping clock
266  */
267  
268 static void shaper_timer(unsigned long data)
269 {
270         struct shaper *shaper = (struct shaper *)data;
271
272         spin_lock(&shaper->lock);
273         shaper_kick(shaper);
274         spin_unlock(&shaper->lock);
275 }
276
277 /*
278  *      Kick a shaper queue and try and do something sensible with the 
279  *      queue. 
280  */
281
282 static void shaper_kick(struct shaper *shaper)
283 {
284         struct sk_buff *skb;
285         
286         /*
287          *      Walk the list (may be empty)
288          */
289          
290         while((skb=skb_peek(&shaper->sendq))!=NULL)
291         {
292                 /*
293                  *      Each packet due to go out by now (within an error
294                  *      of SHAPER_BURST) gets kicked onto the link 
295                  */
296                  
297                 if(sh_debug)
298                         printk("Clock = %ld, jiffies = %ld\n", SHAPERCB(skb)->shapeclock, jiffies);
299                 if(time_before_eq(SHAPERCB(skb)->shapeclock, jiffies + SHAPER_BURST))
300                 {
301                         /*
302                          *      Pull the frame and get interrupts back on.
303                          */
304                          
305                         skb_unlink(skb);
306                         if (shaper->recovery < 
307                             SHAPERCB(skb)->shapeclock + SHAPERCB(skb)->shapelen)
308                                 shaper->recovery = SHAPERCB(skb)->shapeclock + SHAPERCB(skb)->shapelen;
309                         /*
310                          *      Pass on to the physical target device via
311                          *      our low level packet thrower.
312                          */
313                         
314                         SHAPERCB(skb)->shapepend=0;
315                         shaper_queue_xmit(shaper, skb); /* Fire */
316                 }
317                 else
318                         break;
319         }
320
321         /*
322          *      Next kick.
323          */
324          
325         if(skb!=NULL)
326                 mod_timer(&shaper->timer, SHAPERCB(skb)->shapeclock);
327 }
328
329
330 /*
331  *      Bring the interface up. We just disallow this until a 
332  *      bind.
333  */
334
335 static int shaper_open(struct net_device *dev)
336 {
337         struct shaper *shaper=dev->priv;
338         
339         /*
340          *      Can't open until attached.
341          *      Also can't open until speed is set, or we'll get
342          *      a division by zero.
343          */
344          
345         if(shaper->dev==NULL)
346                 return -ENODEV;
347         if(shaper->bitspersec==0)
348                 return -EINVAL;
349         return 0;
350 }
351
352 /*
353  *      Closing a shaper flushes the queues.
354  */
355  
356 static int shaper_close(struct net_device *dev)
357 {
358         struct shaper *shaper=dev->priv;
359         struct sk_buff *skb;
360
361         while ((skb = skb_dequeue(&shaper->sendq)) != NULL)
362                 dev_kfree_skb(skb);
363
364         spin_lock_bh(&shaper->lock);
365         shaper_kick(shaper);
366         spin_unlock_bh(&shaper->lock);
367
368         del_timer_sync(&shaper->timer);
369         return 0;
370 }
371
372 /*
373  *      Revectored calls. We alter the parameters and call the functions
374  *      for our attached device. This enables us to bandwidth allocate after
375  *      ARP and other resolutions and not before.
376  */
377
378 static struct net_device_stats *shaper_get_stats(struct net_device *dev)
379 {
380         struct shaper *sh=dev->priv;
381         return &sh->stats;
382 }
383
384 static int shaper_header(struct sk_buff *skb, struct net_device *dev, 
385         unsigned short type, void *daddr, void *saddr, unsigned len)
386 {
387         struct shaper *sh=dev->priv;
388         int v;
389         if(sh_debug)
390                 printk("Shaper header\n");
391         skb->dev=sh->dev;
392         v=sh->hard_header(skb,sh->dev,type,daddr,saddr,len);
393         skb->dev=dev;
394         return v;
395 }
396
397 static int shaper_rebuild_header(struct sk_buff *skb)
398 {
399         struct shaper *sh=skb->dev->priv;
400         struct net_device *dev=skb->dev;
401         int v;
402         if(sh_debug)
403                 printk("Shaper rebuild header\n");
404         skb->dev=sh->dev;
405         v=sh->rebuild_header(skb);
406         skb->dev=dev;
407         return v;
408 }
409
410 #if 0
411 static int shaper_cache(struct neighbour *neigh, struct hh_cache *hh)
412 {
413         struct shaper *sh=neigh->dev->priv;
414         struct net_device *tmp;
415         int ret;
416         if(sh_debug)
417                 printk("Shaper header cache bind\n");
418         tmp=neigh->dev;
419         neigh->dev=sh->dev;
420         ret=sh->hard_header_cache(neigh,hh);
421         neigh->dev=tmp;
422         return ret;
423 }
424
425 static void shaper_cache_update(struct hh_cache *hh, struct net_device *dev,
426         unsigned char *haddr)
427 {
428         struct shaper *sh=dev->priv;
429         if(sh_debug)
430                 printk("Shaper cache update\n");
431         sh->header_cache_update(hh, sh->dev, haddr);
432 }
433 #endif
434
435 #ifdef CONFIG_INET
436
437 static int shaper_neigh_setup(struct neighbour *n)
438 {
439 #ifdef CONFIG_INET
440         if (n->nud_state == NUD_NONE) {
441                 n->ops = &arp_broken_ops;
442                 n->output = n->ops->output;
443         }
444 #endif  
445         return 0;
446 }
447
448 static int shaper_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
449 {
450 #ifdef CONFIG_INET
451         if (p->tbl->family == AF_INET) {
452                 p->neigh_setup = shaper_neigh_setup;
453                 p->ucast_probes = 0;
454                 p->mcast_probes = 0;
455         }
456 #endif  
457         return 0;
458 }
459
460 #else /* !(CONFIG_INET) */
461
462 static int shaper_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
463 {
464         return 0;
465 }
466
467 #endif
468
469 static int shaper_attach(struct net_device *shdev, struct shaper *sh, struct net_device *dev)
470 {
471         sh->dev = dev;
472         sh->hard_start_xmit=dev->hard_start_xmit;
473         sh->get_stats=dev->get_stats;
474         if(dev->hard_header)
475         {
476                 sh->hard_header=dev->hard_header;
477                 shdev->hard_header = shaper_header;
478         }
479         else
480                 shdev->hard_header = NULL;
481                 
482         if(dev->rebuild_header)
483         {
484                 sh->rebuild_header      = dev->rebuild_header;
485                 shdev->rebuild_header   = shaper_rebuild_header;
486         }
487         else
488                 shdev->rebuild_header   = NULL;
489         
490 #if 0
491         if(dev->hard_header_cache)
492         {
493                 sh->hard_header_cache   = dev->hard_header_cache;
494                 shdev->hard_header_cache= shaper_cache;
495         }
496         else
497         {
498                 shdev->hard_header_cache= NULL;
499         }
500                         
501         if(dev->header_cache_update)
502         {
503                 sh->header_cache_update = dev->header_cache_update;
504                 shdev->header_cache_update = shaper_cache_update;
505         }
506         else
507                 shdev->header_cache_update= NULL;
508 #else
509         shdev->header_cache_update = NULL;
510         shdev->hard_header_cache = NULL;
511 #endif
512         shdev->neigh_setup = shaper_neigh_setup_dev;
513         
514         shdev->hard_header_len=dev->hard_header_len;
515         shdev->type=dev->type;
516         shdev->addr_len=dev->addr_len;
517         shdev->mtu=dev->mtu;
518         sh->bitspersec=0;
519         return 0;
520 }
521
522 static int shaper_ioctl(struct net_device *dev,  struct ifreq *ifr, int cmd)
523 {
524         struct shaperconf *ss= (struct shaperconf *)&ifr->ifr_ifru;
525         struct shaper *sh=dev->priv;
526         
527         if(ss->ss_cmd == SHAPER_SET_DEV || ss->ss_cmd == SHAPER_SET_SPEED)
528         {
529                 if(!capable(CAP_NET_ADMIN))
530                         return -EPERM;
531         }
532         
533         switch(ss->ss_cmd)
534         {
535                 case SHAPER_SET_DEV:
536                 {
537                         struct net_device *them=__dev_get_by_name(ss->ss_name);
538                         if(them==NULL)
539                                 return -ENODEV;
540                         if(sh->dev)
541                                 return -EBUSY;
542                         return shaper_attach(dev,dev->priv, them);
543                 }
544                 case SHAPER_GET_DEV:
545                         if(sh->dev==NULL)
546                                 return -ENODEV;
547                         strcpy(ss->ss_name, sh->dev->name);
548                         return 0;
549                 case SHAPER_SET_SPEED:
550                         shaper_setspeed(sh,ss->ss_speed);
551                         return 0;
552                 case SHAPER_GET_SPEED:
553                         ss->ss_speed=sh->bitspersec;
554                         return 0;
555                 default:
556                         return -EINVAL;
557         }
558 }
559
560 static void shaper_init_priv(struct net_device *dev)
561 {
562         struct shaper *sh = dev->priv;
563
564         skb_queue_head_init(&sh->sendq);
565         init_timer(&sh->timer);
566         sh->timer.function=shaper_timer;
567         sh->timer.data=(unsigned long)sh;
568         spin_lock_init(&sh->lock);
569 }
570
571 /*
572  *      Add a shaper device to the system
573  */
574  
575 static void __init shaper_setup(struct net_device *dev)
576 {
577         /*
578          *      Set up the shaper.
579          */
580
581         SET_MODULE_OWNER(dev);
582
583         shaper_init_priv(dev);
584
585         dev->open               = shaper_open;
586         dev->stop               = shaper_close;
587         dev->hard_start_xmit    = shaper_start_xmit;
588         dev->get_stats          = shaper_get_stats;
589         dev->set_multicast_list = NULL;
590         
591         /*
592          *      Intialise the packet queues
593          */
594          
595         /*
596          *      Handlers for when we attach to a device.
597          */
598
599         dev->hard_header        = shaper_header;
600         dev->rebuild_header     = shaper_rebuild_header;
601 #if 0
602         dev->hard_header_cache  = shaper_cache;
603         dev->header_cache_update= shaper_cache_update;
604 #endif
605         dev->neigh_setup        = shaper_neigh_setup_dev;
606         dev->do_ioctl           = shaper_ioctl;
607         dev->hard_header_len    = 0;
608         dev->type               = ARPHRD_ETHER; /* initially */
609         dev->set_mac_address    = NULL;
610         dev->mtu                = 1500;
611         dev->addr_len           = 0;
612         dev->tx_queue_len       = 10;
613         dev->flags              = 0;
614 }
615  
616 static int shapers = 1;
617 #ifdef MODULE
618
619 module_param(shapers, int, 0);
620 MODULE_PARM_DESC(shapers, "Traffic shaper: maximum number of shapers");
621
622 #else /* MODULE */
623
624 static int __init set_num_shapers(char *str)
625 {
626         shapers = simple_strtol(str, NULL, 0);
627         return 1;
628 }
629
630 __setup("shapers=", set_num_shapers);
631
632 #endif /* MODULE */
633
634 static struct net_device **devs;
635
636 static unsigned int shapers_registered = 0;
637
638 static int __init shaper_init(void)
639 {
640         int i;
641         size_t alloc_size;
642         struct net_device *dev;
643         char name[IFNAMSIZ];
644
645         if (shapers < 1)
646                 return -ENODEV;
647
648         alloc_size = sizeof(*dev) * shapers;
649         devs = kmalloc(alloc_size, GFP_KERNEL);
650         if (!devs)
651                 return -ENOMEM;
652         memset(devs, 0, alloc_size);
653
654         for (i = 0; i < shapers; i++) {
655
656                 snprintf(name, IFNAMSIZ, "shaper%d", i);
657                 dev = alloc_netdev(sizeof(struct shaper), name,
658                                    shaper_setup);
659                 if (!dev) 
660                         break;
661
662                 if (register_netdev(dev)) {
663                         free_netdev(dev);
664                         break;
665                 }
666
667                 devs[i] = dev;
668                 shapers_registered++;
669         }
670
671         if (!shapers_registered) {
672                 kfree(devs);
673                 devs = NULL;
674         }
675
676         return (shapers_registered ? 0 : -ENODEV);
677 }
678
679 static void __exit shaper_exit (void)
680 {
681         int i;
682
683         for (i = 0; i < shapers_registered; i++) {
684                 if (devs[i]) {
685                         unregister_netdev(devs[i]);
686                         free_netdev(devs[i]);
687                 }
688         }
689
690         kfree(devs);
691         devs = NULL;
692 }
693
694 module_init(shaper_init);
695 module_exit(shaper_exit);
696 MODULE_LICENSE("GPL");
697