shaper.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. #include <linux/config.h>
  72. #include <linux/module.h>
  73. #include <linux/kernel.h>
  74. #include <linux/fcntl.h>
  75. #include <linux/mm.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 <net/dst.h>
  86. #include <net/arp.h>
  87. struct shaper_cb {
  88. unsigned long shapeclock; /* Time it should go out */
  89. unsigned long shapestamp; /* Stamp for shaper */
  90. __u32 shapelatency; /* Latency on frame */
  91. __u32 shapelen; /* Frame length in clocks */
  92. __u16 shapepend; /* Pending */
  93. };
  94. #define SHAPERCB(skb) ((struct shaper_cb *) ((skb)->cb))
  95. static int sh_debug; /* Debug flag */
  96. #define SHAPER_BANNER "CymruNet Traffic Shaper BETA 0.04 for Linux 2.1\n"
  97. static void shaper_kick(struct shaper *sh);
  98. /*
  99. * Compute clocks on a buffer
  100. */
  101. static int shaper_clocks(struct shaper *shaper, struct sk_buff *skb)
  102. {
  103. int t=skb->len/shaper->bytespertick;
  104. return t;
  105. }
  106. /*
  107. * Set the speed of a shaper. We compute this in bytes per tick since
  108. * thats how the machine wants to run. Quoted input is in bits per second
  109. * as is traditional (note not BAUD). We assume 8 bit bytes.
  110. */
  111. static void shaper_setspeed(struct shaper *shaper, int bitspersec)
  112. {
  113. shaper->bitspersec=bitspersec;
  114. shaper->bytespertick=(bitspersec/HZ)/8;
  115. if(!shaper->bytespertick)
  116. shaper->bytespertick++;
  117. }
  118. /*
  119. * Throw a frame at a shaper.
  120. */
  121. static int shaper_start_xmit(struct sk_buff *skb, struct net_device *dev)
  122. {
  123. struct shaper *shaper = dev->priv;
  124. struct sk_buff *ptr;
  125. spin_lock(&shaper->lock);
  126. ptr=shaper->sendq.prev;
  127. /*
  128. * Set up our packet details
  129. */
  130. SHAPERCB(skb)->shapelatency=0;
  131. SHAPERCB(skb)->shapeclock=shaper->recovery;
  132. if(time_before(SHAPERCB(skb)->shapeclock, jiffies))
  133. SHAPERCB(skb)->shapeclock=jiffies;
  134. skb->priority=0; /* short term bug fix */
  135. SHAPERCB(skb)->shapestamp=jiffies;
  136. /*
  137. * Time slots for this packet.
  138. */
  139. SHAPERCB(skb)->shapelen= shaper_clocks(shaper,skb);
  140. #ifdef SHAPER_COMPLEX /* and broken.. */
  141. while(ptr && ptr!=(struct sk_buff *)&shaper->sendq)
  142. {
  143. if(ptr->pri<skb->pri
  144. && jiffies - SHAPERCB(ptr)->shapeclock < SHAPER_MAXSLIP)
  145. {
  146. struct sk_buff *tmp=ptr->prev;
  147. /*
  148. * It goes before us therefore we slip the length
  149. * of the new frame.
  150. */
  151. SHAPERCB(ptr)->shapeclock+=SHAPERCB(skb)->shapelen;
  152. SHAPERCB(ptr)->shapelatency+=SHAPERCB(skb)->shapelen;
  153. /*
  154. * The packet may have slipped so far back it
  155. * fell off.
  156. */
  157. if(SHAPERCB(ptr)->shapelatency > SHAPER_LATENCY)
  158. {
  159. skb_unlink(ptr);
  160. dev_kfree_skb(ptr);
  161. }
  162. ptr=tmp;
  163. }
  164. else
  165. break;
  166. }
  167. if(ptr==NULL || ptr==(struct sk_buff *)&shaper->sendq)
  168. skb_queue_head(&shaper->sendq,skb);
  169. else
  170. {
  171. struct sk_buff *tmp;
  172. /*
  173. * Set the packet clock out time according to the
  174. * frames ahead. Im sure a bit of thought could drop
  175. * this loop.
  176. */
  177. for(tmp=skb_peek(&shaper->sendq); tmp!=NULL && tmp!=ptr; tmp=tmp->next)
  178. SHAPERCB(skb)->shapeclock+=tmp->shapelen;
  179. skb_append(ptr,skb);
  180. }
  181. #else
  182. {
  183. struct sk_buff *tmp;
  184. /*
  185. * Up our shape clock by the time pending on the queue
  186. * (Should keep this in the shaper as a variable..)
  187. */
  188. for(tmp=skb_peek(&shaper->sendq); tmp!=NULL &&
  189. tmp!=(struct sk_buff *)&shaper->sendq; tmp=tmp->next)
  190. SHAPERCB(skb)->shapeclock+=SHAPERCB(tmp)->shapelen;
  191. /*
  192. * Queue over time. Spill packet.
  193. */
  194. if(SHAPERCB(skb)->shapeclock-jiffies > SHAPER_LATENCY) {
  195. dev_kfree_skb(skb);
  196. shaper->stats.tx_dropped++;
  197. } else
  198. skb_queue_tail(&shaper->sendq, skb);
  199. }
  200. #endif
  201. if(sh_debug)
  202. printk("Frame queued.\n");
  203. if(skb_queue_len(&shaper->sendq)>SHAPER_QLEN)
  204. {
  205. ptr=skb_dequeue(&shaper->sendq);
  206. dev_kfree_skb(ptr);
  207. shaper->stats.collisions++;
  208. }
  209. shaper_kick(shaper);
  210. spin_unlock(&shaper->lock);
  211. return 0;
  212. }
  213. /*
  214. * Transmit from a shaper
  215. */
  216. static void shaper_queue_xmit(struct shaper *shaper, struct sk_buff *skb)
  217. {
  218. struct sk_buff *newskb=skb_clone(skb, GFP_ATOMIC);
  219. if(sh_debug)
  220. printk("Kick frame on %p\n",newskb);
  221. if(newskb)
  222. {
  223. newskb->dev=shaper->dev;
  224. newskb->priority=2;
  225. if(sh_debug)
  226. printk("Kick new frame to %s, %d\n",
  227. shaper->dev->name,newskb->priority);
  228. dev_queue_xmit(newskb);
  229. shaper->stats.tx_bytes += skb->len;
  230. shaper->stats.tx_packets++;
  231. if(sh_debug)
  232. printk("Kicked new frame out.\n");
  233. dev_kfree_skb(skb);
  234. }
  235. }
  236. /*
  237. * Timer handler for shaping clock
  238. */
  239. static void shaper_timer(unsigned long data)
  240. {
  241. struct shaper *shaper = (struct shaper *)data;
  242. spin_lock(&shaper->lock);
  243. shaper_kick(shaper);
  244. spin_unlock(&shaper->lock);
  245. }
  246. /*
  247. * Kick a shaper queue and try and do something sensible with the
  248. * queue.
  249. */
  250. static void shaper_kick(struct shaper *shaper)
  251. {
  252. struct sk_buff *skb;
  253. /*
  254. * Walk the list (may be empty)
  255. */
  256. while((skb=skb_peek(&shaper->sendq))!=NULL)
  257. {
  258. /*
  259. * Each packet due to go out by now (within an error
  260. * of SHAPER_BURST) gets kicked onto the link
  261. */
  262. if(sh_debug)
  263. printk("Clock = %ld, jiffies = %ld\n", SHAPERCB(skb)->shapeclock, jiffies);
  264. if(time_before_eq(SHAPERCB(skb)->shapeclock, jiffies + SHAPER_BURST))
  265. {
  266. /*
  267. * Pull the frame and get interrupts back on.
  268. */
  269. skb_unlink(skb);
  270. if (shaper->recovery <
  271. SHAPERCB(skb)->shapeclock + SHAPERCB(skb)->shapelen)
  272. shaper->recovery = SHAPERCB(skb)->shapeclock + SHAPERCB(skb)->shapelen;
  273. /*
  274. * Pass on to the physical target device via
  275. * our low level packet thrower.
  276. */
  277. SHAPERCB(skb)->shapepend=0;
  278. shaper_queue_xmit(shaper, skb); /* Fire */
  279. }
  280. else
  281. break;
  282. }
  283. /*
  284. * Next kick.
  285. */
  286. if(skb!=NULL)
  287. mod_timer(&shaper->timer, SHAPERCB(skb)->shapeclock);
  288. }
  289. /*
  290. * Bring the interface up. We just disallow this until a
  291. * bind.
  292. */
  293. static int shaper_open(struct net_device *dev)
  294. {
  295. struct shaper *shaper=dev->priv;
  296. /*
  297. * Can't open until attached.
  298. * Also can't open until speed is set, or we'll get
  299. * a division by zero.
  300. */
  301. if(shaper->dev==NULL)
  302. return -ENODEV;
  303. if(shaper->bitspersec==0)
  304. return -EINVAL;
  305. return 0;
  306. }
  307. /*
  308. * Closing a shaper flushes the queues.
  309. */
  310. static int shaper_close(struct net_device *dev)
  311. {
  312. struct shaper *shaper=dev->priv;
  313. struct sk_buff *skb;
  314. while ((skb = skb_dequeue(&shaper->sendq)) != NULL)
  315. dev_kfree_skb(skb);
  316. spin_lock_bh(&shaper->lock);
  317. shaper_kick(shaper);
  318. spin_unlock_bh(&shaper->lock);
  319. del_timer_sync(&shaper->timer);
  320. return 0;
  321. }
  322. /*
  323. * Revectored calls. We alter the parameters and call the functions
  324. * for our attached device. This enables us to bandwidth allocate after
  325. * ARP and other resolutions and not before.
  326. */
  327. static struct net_device_stats *shaper_get_stats(struct net_device *dev)
  328. {
  329. struct shaper *sh=dev->priv;
  330. return &sh->stats;
  331. }
  332. static int shaper_header(struct sk_buff *skb, struct net_device *dev,
  333. unsigned short type, void *daddr, void *saddr, unsigned len)
  334. {
  335. struct shaper *sh=dev->priv;
  336. int v;
  337. if(sh_debug)
  338. printk("Shaper header\n");
  339. skb->dev=sh->dev;
  340. v=sh->hard_header(skb,sh->dev,type,daddr,saddr,len);
  341. skb->dev=dev;
  342. return v;
  343. }
  344. static int shaper_rebuild_header(struct sk_buff *skb)
  345. {
  346. struct shaper *sh=skb->dev->priv;
  347. struct net_device *dev=skb->dev;
  348. int v;
  349. if(sh_debug)
  350. printk("Shaper rebuild header\n");
  351. skb->dev=sh->dev;
  352. v=sh->rebuild_header(skb);
  353. skb->dev=dev;
  354. return v;
  355. }
  356. #if 0
  357. static int shaper_cache(struct neighbour *neigh, struct hh_cache *hh)
  358. {
  359. struct shaper *sh=neigh->dev->priv;
  360. struct net_device *tmp;
  361. int ret;
  362. if(sh_debug)
  363. printk("Shaper header cache bind\n");
  364. tmp=neigh->dev;
  365. neigh->dev=sh->dev;
  366. ret=sh->hard_header_cache(neigh,hh);
  367. neigh->dev=tmp;
  368. return ret;
  369. }
  370. static void shaper_cache_update(struct hh_cache *hh, struct net_device *dev,
  371. unsigned char *haddr)
  372. {
  373. struct shaper *sh=dev->priv;
  374. if(sh_debug)
  375. printk("Shaper cache update\n");
  376. sh->header_cache_update(hh, sh->dev, haddr);
  377. }
  378. #endif
  379. #ifdef CONFIG_INET
  380. static int shaper_neigh_setup(struct neighbour *n)
  381. {
  382. #ifdef CONFIG_INET
  383. if (n->nud_state == NUD_NONE) {
  384. n->ops = &arp_broken_ops;
  385. n->output = n->ops->output;
  386. }
  387. #endif
  388. return 0;
  389. }
  390. static int shaper_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  391. {
  392. #ifdef CONFIG_INET
  393. if (p->tbl->family == AF_INET) {
  394. p->neigh_setup = shaper_neigh_setup;
  395. p->ucast_probes = 0;
  396. p->mcast_probes = 0;
  397. }
  398. #endif
  399. return 0;
  400. }
  401. #else /* !(CONFIG_INET) */
  402. static int shaper_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  403. {
  404. return 0;
  405. }
  406. #endif
  407. static int shaper_attach(struct net_device *shdev, struct shaper *sh, struct net_device *dev)
  408. {
  409. sh->dev = dev;
  410. sh->hard_start_xmit=dev->hard_start_xmit;
  411. sh->get_stats=dev->get_stats;
  412. if(dev->hard_header)
  413. {
  414. sh->hard_header=dev->hard_header;
  415. shdev->hard_header = shaper_header;
  416. }
  417. else
  418. shdev->hard_header = NULL;
  419. if(dev->rebuild_header)
  420. {
  421. sh->rebuild_header = dev->rebuild_header;
  422. shdev->rebuild_header = shaper_rebuild_header;
  423. }
  424. else
  425. shdev->rebuild_header = NULL;
  426. #if 0
  427. if(dev->hard_header_cache)
  428. {
  429. sh->hard_header_cache = dev->hard_header_cache;
  430. shdev->hard_header_cache= shaper_cache;
  431. }
  432. else
  433. {
  434. shdev->hard_header_cache= NULL;
  435. }
  436. if(dev->header_cache_update)
  437. {
  438. sh->header_cache_update = dev->header_cache_update;
  439. shdev->header_cache_update = shaper_cache_update;
  440. }
  441. else
  442. shdev->header_cache_update= NULL;
  443. #else
  444. shdev->header_cache_update = NULL;
  445. shdev->hard_header_cache = NULL;
  446. #endif
  447. shdev->neigh_setup = shaper_neigh_setup_dev;
  448. shdev->hard_header_len=dev->hard_header_len;
  449. shdev->type=dev->type;
  450. shdev->addr_len=dev->addr_len;
  451. shdev->mtu=dev->mtu;
  452. sh->bitspersec=0;
  453. return 0;
  454. }
  455. static int shaper_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  456. {
  457. struct shaperconf *ss= (struct shaperconf *)&ifr->ifr_ifru;
  458. struct shaper *sh=dev->priv;
  459. if(ss->ss_cmd == SHAPER_SET_DEV || ss->ss_cmd == SHAPER_SET_SPEED)
  460. {
  461. if(!capable(CAP_NET_ADMIN))
  462. return -EPERM;
  463. }
  464. switch(ss->ss_cmd)
  465. {
  466. case SHAPER_SET_DEV:
  467. {
  468. struct net_device *them=__dev_get_by_name(ss->ss_name);
  469. if(them==NULL)
  470. return -ENODEV;
  471. if(sh->dev)
  472. return -EBUSY;
  473. return shaper_attach(dev,dev->priv, them);
  474. }
  475. case SHAPER_GET_DEV:
  476. if(sh->dev==NULL)
  477. return -ENODEV;
  478. strcpy(ss->ss_name, sh->dev->name);
  479. return 0;
  480. case SHAPER_SET_SPEED:
  481. shaper_setspeed(sh,ss->ss_speed);
  482. return 0;
  483. case SHAPER_GET_SPEED:
  484. ss->ss_speed=sh->bitspersec;
  485. return 0;
  486. default:
  487. return -EINVAL;
  488. }
  489. }
  490. static void shaper_init_priv(struct net_device *dev)
  491. {
  492. struct shaper *sh = dev->priv;
  493. skb_queue_head_init(&sh->sendq);
  494. init_timer(&sh->timer);
  495. sh->timer.function=shaper_timer;
  496. sh->timer.data=(unsigned long)sh;
  497. spin_lock_init(&sh->lock);
  498. }
  499. /*
  500. * Add a shaper device to the system
  501. */
  502. static void __init shaper_setup(struct net_device *dev)
  503. {
  504. /*
  505. * Set up the shaper.
  506. */
  507. SET_MODULE_OWNER(dev);
  508. shaper_init_priv(dev);
  509. dev->open = shaper_open;
  510. dev->stop = shaper_close;
  511. dev->hard_start_xmit = shaper_start_xmit;
  512. dev->get_stats = shaper_get_stats;
  513. dev->set_multicast_list = NULL;
  514. /*
  515. * Intialise the packet queues
  516. */
  517. /*
  518. * Handlers for when we attach to a device.
  519. */
  520. dev->hard_header = shaper_header;
  521. dev->rebuild_header = shaper_rebuild_header;
  522. #if 0
  523. dev->hard_header_cache = shaper_cache;
  524. dev->header_cache_update= shaper_cache_update;
  525. #endif
  526. dev->neigh_setup = shaper_neigh_setup_dev;
  527. dev->do_ioctl = shaper_ioctl;
  528. dev->hard_header_len = 0;
  529. dev->type = ARPHRD_ETHER; /* initially */
  530. dev->set_mac_address = NULL;
  531. dev->mtu = 1500;
  532. dev->addr_len = 0;
  533. dev->tx_queue_len = 10;
  534. dev->flags = 0;
  535. }
  536. static int shapers = 1;
  537. #ifdef MODULE
  538. module_param(shapers, int, 0);
  539. MODULE_PARM_DESC(shapers, "Traffic shaper: maximum number of shapers");
  540. #else /* MODULE */
  541. static int __init set_num_shapers(char *str)
  542. {
  543. shapers = simple_strtol(str, NULL, 0);
  544. return 1;
  545. }
  546. __setup("shapers=", set_num_shapers);
  547. #endif /* MODULE */
  548. static struct net_device **devs;
  549. static unsigned int shapers_registered = 0;
  550. static int __init shaper_init(void)
  551. {
  552. int i;
  553. size_t alloc_size;
  554. struct net_device *dev;
  555. char name[IFNAMSIZ];
  556. if (shapers < 1)
  557. return -ENODEV;
  558. alloc_size = sizeof(*dev) * shapers;
  559. devs = kmalloc(alloc_size, GFP_KERNEL);
  560. if (!devs)
  561. return -ENOMEM;
  562. memset(devs, 0, alloc_size);
  563. for (i = 0; i < shapers; i++) {
  564. snprintf(name, IFNAMSIZ, "shaper%d", i);
  565. dev = alloc_netdev(sizeof(struct shaper), name,
  566. shaper_setup);
  567. if (!dev)
  568. break;
  569. if (register_netdev(dev)) {
  570. free_netdev(dev);
  571. break;
  572. }
  573. devs[i] = dev;
  574. shapers_registered++;
  575. }
  576. if (!shapers_registered) {
  577. kfree(devs);
  578. devs = NULL;
  579. }
  580. return (shapers_registered ? 0 : -ENODEV);
  581. }
  582. static void __exit shaper_exit (void)
  583. {
  584. int i;
  585. for (i = 0; i < shapers_registered; i++) {
  586. if (devs[i]) {
  587. unregister_netdev(devs[i]);
  588. free_netdev(devs[i]);
  589. }
  590. }
  591. kfree(devs);
  592. devs = NULL;
  593. }
  594. module_init(shaper_init);
  595. module_exit(shaper_exit);
  596. MODULE_LICENSE("GPL");