shaper.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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/module.h>
  72. #include <linux/kernel.h>
  73. #include <linux/fcntl.h>
  74. #include <linux/mm.h>
  75. #include <linux/slab.h>
  76. #include <linux/string.h>
  77. #include <linux/errno.h>
  78. #include <linux/netdevice.h>
  79. #include <linux/etherdevice.h>
  80. #include <linux/skbuff.h>
  81. #include <linux/if_arp.h>
  82. #include <linux/init.h>
  83. #include <linux/if_shaper.h>
  84. #include <linux/jiffies.h>
  85. #include <net/dst.h>
  86. #include <net/arp.h>
  87. #include <net/net_namespace.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. dev->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. dev->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->dev->stats.tx_bytes += skb->len;
  188. shaper->dev->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 int shaper_header(struct sk_buff *skb, struct net_device *dev,
  286. unsigned short type,
  287. const void *daddr, const void *saddr, unsigned len)
  288. {
  289. struct shaper *sh=dev->priv;
  290. int v;
  291. if(sh_debug)
  292. printk("Shaper header\n");
  293. skb->dev = sh->dev;
  294. v = dev_hard_header(skb, sh->dev, type, daddr, saddr, len);
  295. skb->dev = dev;
  296. return v;
  297. }
  298. static int shaper_rebuild_header(struct sk_buff *skb)
  299. {
  300. struct shaper *sh=skb->dev->priv;
  301. struct net_device *dev=skb->dev;
  302. int v;
  303. if(sh_debug)
  304. printk("Shaper rebuild header\n");
  305. skb->dev=sh->dev;
  306. v = sh->dev->header_ops->rebuild(skb);
  307. skb->dev=dev;
  308. return v;
  309. }
  310. #if 0
  311. static int shaper_cache(struct neighbour *neigh, struct hh_cache *hh)
  312. {
  313. struct shaper *sh=neigh->dev->priv;
  314. struct net_device *tmp;
  315. int ret;
  316. if(sh_debug)
  317. printk("Shaper header cache bind\n");
  318. tmp=neigh->dev;
  319. neigh->dev=sh->dev;
  320. ret=sh->hard_header_cache(neigh,hh);
  321. neigh->dev=tmp;
  322. return ret;
  323. }
  324. static void shaper_cache_update(struct hh_cache *hh, struct net_device *dev,
  325. unsigned char *haddr)
  326. {
  327. struct shaper *sh=dev->priv;
  328. if(sh_debug)
  329. printk("Shaper cache update\n");
  330. sh->header_cache_update(hh, sh->dev, haddr);
  331. }
  332. #endif
  333. #ifdef CONFIG_INET
  334. static int shaper_neigh_setup(struct neighbour *n)
  335. {
  336. #ifdef CONFIG_INET
  337. if (n->nud_state == NUD_NONE) {
  338. n->ops = &arp_broken_ops;
  339. n->output = n->ops->output;
  340. }
  341. #endif
  342. return 0;
  343. }
  344. static int shaper_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  345. {
  346. #ifdef CONFIG_INET
  347. if (p->tbl->family == AF_INET) {
  348. p->neigh_setup = shaper_neigh_setup;
  349. p->ucast_probes = 0;
  350. p->mcast_probes = 0;
  351. }
  352. #endif
  353. return 0;
  354. }
  355. #else /* !(CONFIG_INET) */
  356. static int shaper_neigh_setup_dev(struct net_device *dev, struct neigh_parms *p)
  357. {
  358. return 0;
  359. }
  360. #endif
  361. static const struct header_ops shaper_ops = {
  362. .create = shaper_header,
  363. .rebuild = shaper_rebuild_header,
  364. };
  365. static int shaper_attach(struct net_device *shdev, struct shaper *sh, struct net_device *dev)
  366. {
  367. sh->dev = dev;
  368. sh->get_stats=dev->get_stats;
  369. shdev->neigh_setup = shaper_neigh_setup_dev;
  370. shdev->hard_header_len=dev->hard_header_len;
  371. shdev->type=dev->type;
  372. shdev->addr_len=dev->addr_len;
  373. shdev->mtu=dev->mtu;
  374. sh->bitspersec=0;
  375. return 0;
  376. }
  377. static int shaper_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  378. {
  379. struct shaperconf *ss= (struct shaperconf *)&ifr->ifr_ifru;
  380. struct shaper *sh=dev->priv;
  381. if(ss->ss_cmd == SHAPER_SET_DEV || ss->ss_cmd == SHAPER_SET_SPEED)
  382. {
  383. if(!capable(CAP_NET_ADMIN))
  384. return -EPERM;
  385. }
  386. switch(ss->ss_cmd)
  387. {
  388. case SHAPER_SET_DEV:
  389. {
  390. struct net_device *them=__dev_get_by_name(&init_net, ss->ss_name);
  391. if(them==NULL)
  392. return -ENODEV;
  393. if(sh->dev)
  394. return -EBUSY;
  395. return shaper_attach(dev,dev->priv, them);
  396. }
  397. case SHAPER_GET_DEV:
  398. if(sh->dev==NULL)
  399. return -ENODEV;
  400. strcpy(ss->ss_name, sh->dev->name);
  401. return 0;
  402. case SHAPER_SET_SPEED:
  403. shaper_setspeed(sh,ss->ss_speed);
  404. return 0;
  405. case SHAPER_GET_SPEED:
  406. ss->ss_speed=sh->bitspersec;
  407. return 0;
  408. default:
  409. return -EINVAL;
  410. }
  411. }
  412. static void shaper_init_priv(struct net_device *dev)
  413. {
  414. struct shaper *sh = dev->priv;
  415. skb_queue_head_init(&sh->sendq);
  416. init_timer(&sh->timer);
  417. sh->timer.function=shaper_timer;
  418. sh->timer.data=(unsigned long)sh;
  419. spin_lock_init(&sh->lock);
  420. }
  421. /*
  422. * Add a shaper device to the system
  423. */
  424. static void __init shaper_setup(struct net_device *dev)
  425. {
  426. /*
  427. * Set up the shaper.
  428. */
  429. shaper_init_priv(dev);
  430. dev->open = shaper_open;
  431. dev->stop = shaper_close;
  432. dev->hard_start_xmit = shaper_start_xmit;
  433. dev->set_multicast_list = NULL;
  434. /*
  435. * Intialise the packet queues
  436. */
  437. /*
  438. * Handlers for when we attach to a device.
  439. */
  440. dev->neigh_setup = shaper_neigh_setup_dev;
  441. dev->do_ioctl = shaper_ioctl;
  442. dev->hard_header_len = 0;
  443. dev->type = ARPHRD_ETHER; /* initially */
  444. dev->set_mac_address = NULL;
  445. dev->mtu = 1500;
  446. dev->addr_len = 0;
  447. dev->tx_queue_len = 10;
  448. dev->flags = 0;
  449. }
  450. static int shapers = 1;
  451. #ifdef MODULE
  452. module_param(shapers, int, 0);
  453. MODULE_PARM_DESC(shapers, "Traffic shaper: maximum number of shapers");
  454. #else /* MODULE */
  455. static int __init set_num_shapers(char *str)
  456. {
  457. shapers = simple_strtol(str, NULL, 0);
  458. return 1;
  459. }
  460. __setup("shapers=", set_num_shapers);
  461. #endif /* MODULE */
  462. static struct net_device **devs;
  463. static unsigned int shapers_registered = 0;
  464. static int __init shaper_init(void)
  465. {
  466. int i;
  467. size_t alloc_size;
  468. struct net_device *dev;
  469. char name[IFNAMSIZ];
  470. if (shapers < 1)
  471. return -ENODEV;
  472. alloc_size = sizeof(*dev) * shapers;
  473. devs = kzalloc(alloc_size, GFP_KERNEL);
  474. if (!devs)
  475. return -ENOMEM;
  476. for (i = 0; i < shapers; i++) {
  477. snprintf(name, IFNAMSIZ, "shaper%d", i);
  478. dev = alloc_netdev(sizeof(struct shaper), name,
  479. shaper_setup);
  480. if (!dev)
  481. break;
  482. if (register_netdev(dev)) {
  483. free_netdev(dev);
  484. break;
  485. }
  486. devs[i] = dev;
  487. shapers_registered++;
  488. }
  489. if (!shapers_registered) {
  490. kfree(devs);
  491. devs = NULL;
  492. }
  493. return (shapers_registered ? 0 : -ENODEV);
  494. }
  495. static void __exit shaper_exit (void)
  496. {
  497. int i;
  498. for (i = 0; i < shapers_registered; i++) {
  499. if (devs[i]) {
  500. unregister_netdev(devs[i]);
  501. free_netdev(devs[i]);
  502. }
  503. }
  504. kfree(devs);
  505. devs = NULL;
  506. }
  507. module_init(shaper_init);
  508. module_exit(shaper_exit);
  509. MODULE_LICENSE("GPL");