shaper.c 15 KB

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