shaper.c 17 KB

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