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