2 * Simple traffic shaper for Linux NET3.
4 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
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.
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.
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
25 * We work to the following constants
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
32 * SHAPER_MAXSLIP Maximum time a priority frame may jump forward.
33 * That bounds the penalty we will inflict on low
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.
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.
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
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
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>
68 * Use skb->cb for private data.
72 #include <linux/module.h>
73 #include <linux/kernel.h>
74 #include <linux/fcntl.h>
76 #include <linux/slab.h>
77 #include <linux/string.h>
78 #include <linux/errno.h>
79 #include <linux/netdevice.h>
80 #include <linux/etherdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/if_arp.h>
83 #include <linux/init.h>
84 #include <linux/if_shaper.h>
85 #include <linux/jiffies.h>
89 #include <net/net_namespace.h>
92 unsigned long shapeclock
; /* Time it should go out */
93 unsigned long shapestamp
; /* Stamp for shaper */
94 __u32 shapelatency
; /* Latency on frame */
95 __u32 shapelen
; /* Frame length in clocks */
96 __u16 shapepend
; /* Pending */
98 #define SHAPERCB(skb) ((struct shaper_cb *) ((skb)->cb))
100 static int sh_debug
; /* Debug flag */
102 #define SHAPER_BANNER "CymruNet Traffic Shaper BETA 0.04 for Linux 2.1\n"
104 static void shaper_kick(struct shaper
*sh
);
107 * Compute clocks on a buffer
110 static int shaper_clocks(struct shaper
*shaper
, struct sk_buff
*skb
)
112 int t
=skb
->len
/shaper
->bytespertick
;
117 * Set the speed of a shaper. We compute this in bytes per tick since
118 * thats how the machine wants to run. Quoted input is in bits per second
119 * as is traditional (note not BAUD). We assume 8 bit bytes.
122 static void shaper_setspeed(struct shaper
*shaper
, int bitspersec
)
124 shaper
->bitspersec
=bitspersec
;
125 shaper
->bytespertick
=(bitspersec
/HZ
)/8;
126 if(!shaper
->bytespertick
)
127 shaper
->bytespertick
++;
131 * Throw a frame at a shaper.
135 static int shaper_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
137 struct shaper
*shaper
= dev
->priv
;
140 spin_lock(&shaper
->lock
);
141 ptr
=shaper
->sendq
.prev
;
144 * Set up our packet details
147 SHAPERCB(skb
)->shapelatency
=0;
148 SHAPERCB(skb
)->shapeclock
=shaper
->recovery
;
149 if(time_before(SHAPERCB(skb
)->shapeclock
, jiffies
))
150 SHAPERCB(skb
)->shapeclock
=jiffies
;
151 skb
->priority
=0; /* short term bug fix */
152 SHAPERCB(skb
)->shapestamp
=jiffies
;
155 * Time slots for this packet.
158 SHAPERCB(skb
)->shapelen
= shaper_clocks(shaper
,skb
);
163 * Up our shape clock by the time pending on the queue
164 * (Should keep this in the shaper as a variable..)
166 for(tmp
=skb_peek(&shaper
->sendq
); tmp
!=NULL
&&
167 tmp
!=(struct sk_buff
*)&shaper
->sendq
; tmp
=tmp
->next
)
168 SHAPERCB(skb
)->shapeclock
+=SHAPERCB(tmp
)->shapelen
;
170 * Queue over time. Spill packet.
172 if(time_after(SHAPERCB(skb
)->shapeclock
,jiffies
+ SHAPER_LATENCY
)) {
174 dev
->stats
.tx_dropped
++;
176 skb_queue_tail(&shaper
->sendq
, skb
);
180 printk("Frame queued.\n");
181 if(skb_queue_len(&shaper
->sendq
)>SHAPER_QLEN
)
183 ptr
=skb_dequeue(&shaper
->sendq
);
185 dev
->stats
.collisions
++;
188 spin_unlock(&shaper
->lock
);
193 * Transmit from a shaper
196 static void shaper_queue_xmit(struct shaper
*shaper
, struct sk_buff
*skb
)
198 struct sk_buff
*newskb
=skb_clone(skb
, GFP_ATOMIC
);
200 printk("Kick frame on %p\n",newskb
);
203 newskb
->dev
=shaper
->dev
;
206 printk("Kick new frame to %s, %d\n",
207 shaper
->dev
->name
,newskb
->priority
);
208 dev_queue_xmit(newskb
);
210 shaper
->dev
->stats
.tx_bytes
+= skb
->len
;
211 shaper
->dev
->stats
.tx_packets
++;
214 printk("Kicked new frame out.\n");
220 * Timer handler for shaping clock
223 static void shaper_timer(unsigned long data
)
225 struct shaper
*shaper
= (struct shaper
*)data
;
227 spin_lock(&shaper
->lock
);
229 spin_unlock(&shaper
->lock
);
233 * Kick a shaper queue and try and do something sensible with the
237 static void shaper_kick(struct shaper
*shaper
)
242 * Walk the list (may be empty)
245 while((skb
=skb_peek(&shaper
->sendq
))!=NULL
)
248 * Each packet due to go out by now (within an error
249 * of SHAPER_BURST) gets kicked onto the link
253 printk("Clock = %ld, jiffies = %ld\n", SHAPERCB(skb
)->shapeclock
, jiffies
);
254 if(time_before_eq(SHAPERCB(skb
)->shapeclock
, jiffies
+ SHAPER_BURST
))
257 * Pull the frame and get interrupts back on.
260 skb_unlink(skb
, &shaper
->sendq
);
261 if (shaper
->recovery
<
262 SHAPERCB(skb
)->shapeclock
+ SHAPERCB(skb
)->shapelen
)
263 shaper
->recovery
= SHAPERCB(skb
)->shapeclock
+ SHAPERCB(skb
)->shapelen
;
265 * Pass on to the physical target device via
266 * our low level packet thrower.
269 SHAPERCB(skb
)->shapepend
=0;
270 shaper_queue_xmit(shaper
, skb
); /* Fire */
281 mod_timer(&shaper
->timer
, SHAPERCB(skb
)->shapeclock
);
286 * Bring the interface up. We just disallow this until a
290 static int shaper_open(struct net_device
*dev
)
292 struct shaper
*shaper
=dev
->priv
;
295 * Can't open until attached.
296 * Also can't open until speed is set, or we'll get
297 * a division by zero.
300 if(shaper
->dev
==NULL
)
302 if(shaper
->bitspersec
==0)
308 * Closing a shaper flushes the queues.
311 static int shaper_close(struct net_device
*dev
)
313 struct shaper
*shaper
=dev
->priv
;
316 while ((skb
= skb_dequeue(&shaper
->sendq
)) != NULL
)
319 spin_lock_bh(&shaper
->lock
);
321 spin_unlock_bh(&shaper
->lock
);
323 del_timer_sync(&shaper
->timer
);
328 * Revectored calls. We alter the parameters and call the functions
329 * for our attached device. This enables us to bandwidth allocate after
330 * ARP and other resolutions and not before.
333 static int shaper_header(struct sk_buff
*skb
, struct net_device
*dev
,
335 const void *daddr
, const void *saddr
, unsigned len
)
337 struct shaper
*sh
=dev
->priv
;
340 printk("Shaper header\n");
342 v
= dev_hard_header(skb
, sh
->dev
, type
, daddr
, saddr
, len
);
347 static int shaper_rebuild_header(struct sk_buff
*skb
)
349 struct shaper
*sh
=skb
->dev
->priv
;
350 struct net_device
*dev
=skb
->dev
;
353 printk("Shaper rebuild header\n");
355 v
= sh
->dev
->header_ops
->rebuild(skb
);
361 static int shaper_cache(struct neighbour
*neigh
, struct hh_cache
*hh
)
363 struct shaper
*sh
=neigh
->dev
->priv
;
364 struct net_device
*tmp
;
367 printk("Shaper header cache bind\n");
370 ret
=sh
->hard_header_cache(neigh
,hh
);
375 static void shaper_cache_update(struct hh_cache
*hh
, struct net_device
*dev
,
376 unsigned char *haddr
)
378 struct shaper
*sh
=dev
->priv
;
380 printk("Shaper cache update\n");
381 sh
->header_cache_update(hh
, sh
->dev
, haddr
);
387 static int shaper_neigh_setup(struct neighbour
*n
)
390 if (n
->nud_state
== NUD_NONE
) {
391 n
->ops
= &arp_broken_ops
;
392 n
->output
= n
->ops
->output
;
398 static int shaper_neigh_setup_dev(struct net_device
*dev
, struct neigh_parms
*p
)
401 if (p
->tbl
->family
== AF_INET
) {
402 p
->neigh_setup
= shaper_neigh_setup
;
410 #else /* !(CONFIG_INET) */
412 static int shaper_neigh_setup_dev(struct net_device
*dev
, struct neigh_parms
*p
)
419 static const struct header_ops shaper_ops
= {
420 .create
= shaper_header
,
421 .rebuild
= shaper_rebuild_header
,
424 static int shaper_attach(struct net_device
*shdev
, struct shaper
*sh
, struct net_device
*dev
)
427 sh
->get_stats
=dev
->get_stats
;
429 shdev
->neigh_setup
= shaper_neigh_setup_dev
;
430 shdev
->hard_header_len
=dev
->hard_header_len
;
431 shdev
->type
=dev
->type
;
432 shdev
->addr_len
=dev
->addr_len
;
438 static int shaper_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
440 struct shaperconf
*ss
= (struct shaperconf
*)&ifr
->ifr_ifru
;
441 struct shaper
*sh
=dev
->priv
;
443 if(ss
->ss_cmd
== SHAPER_SET_DEV
|| ss
->ss_cmd
== SHAPER_SET_SPEED
)
445 if(!capable(CAP_NET_ADMIN
))
453 struct net_device
*them
=__dev_get_by_name(&init_net
, ss
->ss_name
);
458 return shaper_attach(dev
,dev
->priv
, them
);
463 strcpy(ss
->ss_name
, sh
->dev
->name
);
465 case SHAPER_SET_SPEED
:
466 shaper_setspeed(sh
,ss
->ss_speed
);
468 case SHAPER_GET_SPEED
:
469 ss
->ss_speed
=sh
->bitspersec
;
476 static void shaper_init_priv(struct net_device
*dev
)
478 struct shaper
*sh
= dev
->priv
;
480 skb_queue_head_init(&sh
->sendq
);
481 init_timer(&sh
->timer
);
482 sh
->timer
.function
=shaper_timer
;
483 sh
->timer
.data
=(unsigned long)sh
;
484 spin_lock_init(&sh
->lock
);
488 * Add a shaper device to the system
491 static void __init
shaper_setup(struct net_device
*dev
)
497 shaper_init_priv(dev
);
499 dev
->open
= shaper_open
;
500 dev
->stop
= shaper_close
;
501 dev
->hard_start_xmit
= shaper_start_xmit
;
502 dev
->set_multicast_list
= NULL
;
505 * Intialise the packet queues
509 * Handlers for when we attach to a device.
512 dev
->neigh_setup
= shaper_neigh_setup_dev
;
513 dev
->do_ioctl
= shaper_ioctl
;
514 dev
->hard_header_len
= 0;
515 dev
->type
= ARPHRD_ETHER
; /* initially */
516 dev
->set_mac_address
= NULL
;
519 dev
->tx_queue_len
= 10;
523 static int shapers
= 1;
526 module_param(shapers
, int, 0);
527 MODULE_PARM_DESC(shapers
, "Traffic shaper: maximum number of shapers");
531 static int __init
set_num_shapers(char *str
)
533 shapers
= simple_strtol(str
, NULL
, 0);
537 __setup("shapers=", set_num_shapers
);
541 static struct net_device
**devs
;
543 static unsigned int shapers_registered
= 0;
545 static int __init
shaper_init(void)
549 struct net_device
*dev
;
555 alloc_size
= sizeof(*dev
) * shapers
;
556 devs
= kzalloc(alloc_size
, GFP_KERNEL
);
560 for (i
= 0; i
< shapers
; i
++) {
562 snprintf(name
, IFNAMSIZ
, "shaper%d", i
);
563 dev
= alloc_netdev(sizeof(struct shaper
), name
,
568 if (register_netdev(dev
)) {
574 shapers_registered
++;
577 if (!shapers_registered
) {
582 return (shapers_registered
? 0 : -ENODEV
);
585 static void __exit
shaper_exit (void)
589 for (i
= 0; i
< shapers_registered
; i
++) {
591 unregister_netdev(devs
[i
]);
592 free_netdev(devs
[i
]);
600 module_init(shaper_init
);
601 module_exit(shaper_exit
);
602 MODULE_LICENSE("GPL");