root/drivers/net/eql.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. eql_init
  2. eql_open
  3. eql_close
  4. eql_ioctl
  5. eql_slave_xmit
  6. eql_get_stats
  7. eql_header
  8. eql_rebuild_header
  9. eql_enslave
  10. eql_emancipate
  11. eql_g_slave_cfg
  12. eql_s_slave_cfg
  13. eql_g_master_cfg
  14. eql_s_master_cfg
  15. eql_is_slave
  16. eql_is_master
  17. eql_new_slave
  18. eql_delete_slave
  19. slave_Bps
  20. slave_bps
  21. eql_number_slaves
  22. eql_is_empty
  23. eql_is_full
  24. eql_new_slave_queue
  25. eql_delete_slave_queue
  26. eql_insert_slave
  27. eql_remove_slave
  28. eql_remove_slave_dev
  29. eql_best_slave_dev
  30. eql_best_slave
  31. eql_schedule_slaves
  32. eql_find_slave_dev
  33. eql_first_slave
  34. eql_next_slave
  35. eql_set_best_slave
  36. eql_timer
  37. init_module
  38. cleanup_module

   1 /*
   2  * Equalizer Load-balancer for serial network interfaces.
   3  *
   4  * (c) Copyright 1995 Simon "Guru Aleph-Null" Janes
   5  * NCM: Network and Communications Management, Inc.
   6  *
   7  *
   8  *      This software may be used and distributed according to the terms
   9  *      of the GNU Public License, incorporated herein by reference.
  10  * 
  11  * The author may be reached as simon@ncm.com, or C/O
  12  *    NCM
  13  *    Attn: Simon Janes
  14  *    6803 Whittier Ave
  15  *    McLean VA 22101
  16  *    Phone: 1-703-847-0040 ext 103
  17  */
  18 
  19 static const char *version = 
  20         "Equalizer1996: $Revision: 1.2 $ $Date: 1996/04/11 17:51:52 $ Simon Janes (simon@ncm.com)\n";
  21 
  22 /*
  23  * Sources:
  24  *   skeleton.c by Donald Becker.
  25  * Inspirations:
  26  *   The Harried and Overworked Alan Cox
  27  * Conspiracies:
  28  *   The Alan Cox and Mike McLagan plot to get someone else to do the code, 
  29  *   which turned out to be me.
  30  */
  31 
  32 /*
  33  * $Log: eql.c,v $
  34  * Revision 1.2  1996/04/11 17:51:52  guru
  35  * Added one-line eql_remove_slave patch.
  36  *
  37  * Revision 1.1  1996/04/11 17:44:17  guru
  38  * Initial revision
  39  *
  40  * Revision 3.13  1996/01/21  15:17:18  alan
  41  * tx_queue_len changes.
  42  * reformatted.
  43  *
  44  * Revision 3.12  1995/03/22  21:07:51  anarchy
  45  * Added suser() checks on configuration.
  46  * Moved header file.
  47  *
  48  * Revision 3.11  1995/01/19  23:14:31  guru
  49  *                    slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
  50  *                      (priority_Bps) + bytes_queued * 8;
  51  *
  52  * Revision 3.10  1995/01/19  23:07:53  guru
  53  * back to
  54  *                    slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
  55  *                      (priority_Bps) + bytes_queued;
  56  *
  57  * Revision 3.9  1995/01/19  22:38:20  guru
  58  *                    slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
  59  *                      (priority_Bps) + bytes_queued * 4;
  60  *
  61  * Revision 3.8  1995/01/19  22:30:55  guru
  62  *       slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
  63  *                      (priority_Bps) + bytes_queued * 2;
  64  *
  65  * Revision 3.7  1995/01/19  21:52:35  guru
  66  * printk's trimmed out.
  67  *
  68  * Revision 3.6  1995/01/19  21:49:56  guru
  69  * This is working pretty well. I gained 1 K/s in speed.. now it's just
  70  * robustness and printk's to be diked out.
  71  *
  72  * Revision 3.5  1995/01/18  22:29:59  guru
  73  * still crashes the kernel when the lock_wait thing is woken up.
  74  *
  75  * Revision 3.4  1995/01/18  21:59:47  guru
  76  * Broken set-bit locking snapshot
  77  *
  78  * Revision 3.3  1995/01/17  22:09:18  guru
  79  * infinite sleep in a lock somewhere..
  80  *
  81  * Revision 3.2  1995/01/15  16:46:06  guru
  82  * Log trimmed of non-pertinent 1.x branch messages
  83  *
  84  * Revision 3.1  1995/01/15  14:41:45  guru
  85  * New Scheduler and timer stuff...
  86  *
  87  * Revision 1.15  1995/01/15  14:29:02  guru
  88  * Will make 1.14 (now 1.15) the 3.0 branch, and the 1.12 the 2.0 branch, the one
  89  * with the dumber scheduler
  90  *
  91  * Revision 1.14  1995/01/15  02:37:08  guru
  92  * shock.. the kept-new-versions could have zonked working
  93  * stuff.. shudder
  94  *
  95  * Revision 1.13  1995/01/15  02:36:31  guru
  96  * big changes
  97  *
  98  *      scheduler was torn out and replaced with something smarter
  99  *
 100  *      global names not prefixed with eql_ were renamed to protect
 101  *      against namespace collisions
 102  *
 103  *      a few more abstract interfaces were added to facilitate any
 104  *      potential change of datastructure.  the driver is still using
 105  *      a linked list of slaves.  going to a heap would be a bit of
 106  *      an overkill.
 107  *
 108  *      this compiles fine with no warnings.
 109  *
 110  *      the locking mechanism and timer stuff must be written however,
 111  *      this version will not work otherwise
 112  *
 113  */
 114 
 115 #include <linux/module.h>
 116 
 117 #include <linux/kernel.h>
 118 #include <linux/sched.h>
 119 #include <linux/types.h>
 120 #include <linux/fcntl.h>
 121 #include <linux/interrupt.h>
 122 #include <linux/ptrace.h>
 123 #include <linux/ioport.h>
 124 #include <linux/in.h>
 125 #include <linux/malloc.h>
 126 #include <linux/string.h>
 127 #include <asm/system.h>
 128 #include <asm/bitops.h>
 129 #include <asm/io.h>
 130 #include <asm/dma.h>
 131 #include <linux/errno.h>              
 132 
 133 #include <linux/netdevice.h>
 134 #include <linux/if.h>
 135 #include <linux/if_arp.h>
 136 #include <linux/timer.h>
 137 
 138 #include <linux/if_eql.h>
 139 
 140 #ifndef EQL_DEBUG
 141 /* #undef EQL_DEBUG      -* print nothing at all, not even a boot-banner */
 142 /* #define EQL_DEBUG 1   -* print only the boot-banner */
 143 /* #define EQL_DEBUG 5   -* print major function entries */
 144 /* #define EQL_DEBUG 20  -* print subfunction entries */
 145 /* #define EQL_DEBUG 50  -* print utility entries */
 146 /* #define EQL_DEBUG 100 -* print voluminous function entries */
 147 #define EQL_DEBUG 1
 148 #endif
 149 static unsigned int eql_debug = EQL_DEBUG;
 150 
 151 int        eql_init(struct device *dev); /*  */
 152 static int eql_open(struct device *dev); /*  */
 153 static int eql_close(struct device *dev); /*  */
 154 static int eql_ioctl(struct device *dev, struct ifreq *ifr, int cmd); /*  */
 155 static int eql_slave_xmit(struct sk_buff *skb, struct device *dev); /*  */
 156 
 157 static struct enet_statistics *eql_get_stats(struct device *dev); /*  */
 158 static int eql_header(struct sk_buff *skb, struct device *dev, 
 159                       unsigned short type, void *daddr, void *saddr, 
 160                       unsigned len); /*  */
 161 static int eql_rebuild_header(void *buff, struct device *dev, 
 162                               unsigned long raddr, struct sk_buff *skb); /*  */
 163 
 164 /* ioctl() handlers
 165    ---------------- */
 166 static int eql_enslave(struct device *dev,  slaving_request_t *srq); /*  */
 167 static int eql_emancipate(struct device *dev, slaving_request_t *srq); /*  */
 168 
 169 static int eql_g_slave_cfg(struct device *dev, slave_config_t *sc); /*  */
 170 static int eql_s_slave_cfg(struct device *dev, slave_config_t *sc); /*  */
 171 
 172 static int eql_g_master_cfg(struct device *dev, master_config_t *mc); /*  */
 173 static int eql_s_master_cfg(struct device *dev, master_config_t *mc); /*  */
 174 
 175 static inline int eql_is_slave(struct device *dev); /*  */
 176 static inline int eql_is_master(struct device *dev); /*  */
 177 
 178 static slave_t *eql_new_slave(void); /*  */
 179 static void eql_delete_slave(slave_t *slave); /*  */
 180 
 181 /* static long eql_slave_priority(slave_t *slave); -*  */
 182 static inline int eql_number_slaves(slave_queue_t *queue); /*  */
 183 
 184 static inline int eql_is_empty(slave_queue_t *queue); /*  */
 185 static inline int eql_is_full(slave_queue_t *queue); /*  */
 186 
 187 static slave_queue_t *eql_new_slave_queue(struct device *dev); /*  */
 188 static void eql_delete_slave_queue(slave_queue_t *queue); /*  */
 189 
 190 static int eql_insert_slave(slave_queue_t *queue, slave_t *slave); /*  */
 191 static slave_t *eql_remove_slave(slave_queue_t *queue, slave_t *slave); /*  */
 192 
 193 /* static int eql_insert_slave_dev(slave_queue_t *queue, struct device *dev); -*  */
 194 static int eql_remove_slave_dev(slave_queue_t *queue, struct device *dev); /*  */
 195 
 196 static inline struct device *eql_best_slave_dev(slave_queue_t *queue); /*  */
 197 static inline slave_t *eql_best_slave(slave_queue_t *queue); /*  */
 198 static inline slave_t *eql_first_slave(slave_queue_t *queue); /*  */
 199 static inline slave_t *eql_next_slave(slave_queue_t *queue, slave_t *slave); /*  */
 200 
 201 static inline void eql_set_best_slave(slave_queue_t *queue, slave_t *slave); /*  */
 202 static inline void eql_schedule_slaves(slave_queue_t *queue); /*  */
 203 
 204 static slave_t *eql_find_slave_dev(slave_queue_t *queue, struct device *dev); /*  */
 205 
 206 /* static inline eql_lock_slave_queue(slave_queue_t *queue); -*  */
 207 /* static inline eql_unlock_slave_queue(slave_queue_t *queue); -*  */
 208 
 209 static void eql_timer(unsigned long param);     /*  */
 210 
 211 /* struct device * interface functions 
 212    ---------------------------------------------------------
 213    */
 214 
 215 int eql_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 216 {
 217         static unsigned version_printed = 0;
 218         /* static unsigned num_masters     = 0; */
 219         equalizer_t *eql = 0;
 220         int i;
 221 
 222         if ( version_printed++ == 0 && eql_debug > 0)
 223                 printk(version);
 224         /*
 225          *      Initialize the device structure. 
 226          */
 227         dev->priv = kmalloc (sizeof (equalizer_t), GFP_KERNEL);
 228         if (dev->priv == NULL)
 229                 return -ENOMEM;
 230         memset (dev->priv, 0, sizeof (equalizer_t));
 231         eql = (equalizer_t *) dev->priv;
 232 
 233         eql->stats = kmalloc (sizeof (struct enet_statistics), GFP_KERNEL);
 234         if (eql->stats == NULL) 
 235         {
 236                 kfree(dev->priv);
 237                 dev->priv = NULL;
 238                 return -ENOMEM;
 239         }
 240         memset (eql->stats, 0, sizeof (struct enet_statistics));
 241 
 242         init_timer (&eql->timer);
 243         eql->timer.data         = (unsigned long) dev->priv;
 244         eql->timer.expires      = jiffies+EQL_DEFAULT_RESCHED_IVAL;
 245         eql->timer.function     = &eql_timer;
 246         eql->timer_on           = 0;
 247 
 248         dev->open               = eql_open;
 249         dev->stop               = eql_close;
 250         dev->do_ioctl           = eql_ioctl;
 251         dev->hard_start_xmit    = eql_slave_xmit;
 252         dev->get_stats          = eql_get_stats;
 253   
 254         /*
 255          *      Fill in the fields of the device structure with 
 256          *      eql-generic values. This should be in a common 
 257          *      file instead of per-driver.  
 258          */
 259 
 260         for (i = 0; i < DEV_NUMBUFFS; i++)
 261                 skb_queue_head_init(&dev->buffs[i]);
 262 
 263         dev->hard_header        = eql_header; 
 264         dev->rebuild_header     = eql_rebuild_header;
 265 
 266         /*
 267          *      Now we undo some of the things that eth_setup does
 268          *      that we don't like 
 269          */
 270          
 271         dev->mtu                = EQL_DEFAULT_MTU;      /* set to 576 in eql.h */
 272         dev->flags              = IFF_MASTER;
 273 
 274         dev->family             = AF_INET;
 275         dev->pa_addr            = 0;
 276         dev->pa_brdaddr         = 0;
 277         dev->pa_mask            = 0;
 278         dev->pa_alen            = 4;
 279 
 280         dev->type               = ARPHRD_SLIP;
 281         dev->tx_queue_len       = 5;            /* Hands them off fast */
 282 
 283         return 0;
 284 }
 285 
 286 static int eql_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 287 {
 288         equalizer_t *eql = (equalizer_t *) dev->priv;
 289         slave_queue_t *new_queue;
 290 
 291 #ifdef EQL_DEBUG
 292         if (eql_debug >= 5)
 293                 printk ("%s: open\n", dev->name);
 294 #endif
 295 
 296         printk ("%s: remember to turn off Van-Jacobson compression on your slave devices.\n", dev->name);
 297 
 298         new_queue = eql_new_slave_queue (dev);
 299     
 300         if (new_queue != 0)
 301         {
 302                 new_queue->master_dev = dev;
 303                 eql->queue = new_queue;
 304                 eql->queue->lock = 0;
 305                 eql->min_slaves = 1;
 306                 eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
 307 
 308                 printk ("%s: adding timer\n", dev->name);
 309                 eql->timer_on = 1;
 310                 add_timer (&eql->timer);
 311 
 312                 MOD_INC_USE_COUNT;
 313                 return 0;
 314         }
 315         return 1;
 316 }
 317 
 318 
 319 static int eql_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 320 {
 321         equalizer_t *eql = (equalizer_t *) dev->priv;
 322 
 323 #ifdef EQL_DEBUG
 324         if ( eql_debug >= 5)
 325                 printk ("%s: close\n", dev->name);
 326 #endif
 327         /*
 328          *      The timer has to be stopped first before we start hacking away
 329          *      at the data structure it scans every so often... 
 330          */
 331 
 332 #ifdef EQL_DEBUG
 333         printk ("%s: stopping timer\n", dev->name);
 334 #endif  
 335         eql->timer_on = 0;
 336         del_timer (&eql->timer);
 337 
 338         eql_delete_slave_queue (eql->queue);
 339 
 340         MOD_DEC_USE_COUNT;
 341         return 0;
 342 }
 343 
 344 
 345 static int eql_ioctl(struct device *dev, struct ifreq *ifr, int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 346 {  
 347         if(!suser() && cmd!=EQL_GETMASTRCFG && cmd!=EQL_GETSLAVECFG)
 348                 return -EPERM;
 349         switch (cmd)
 350         {
 351                 case EQL_ENSLAVE:
 352                         return eql_enslave (dev, (slaving_request_t *) ifr->ifr_data);
 353                 case EQL_EMANCIPATE:
 354                         return eql_emancipate (dev, (slaving_request_t *) ifr->ifr_data);
 355                 case EQL_GETSLAVECFG:
 356                         return eql_g_slave_cfg (dev, (slave_config_t *) ifr->ifr_data);
 357                 case EQL_SETSLAVECFG:
 358                         return eql_s_slave_cfg (dev, (slave_config_t *) ifr->ifr_data);
 359                 case EQL_GETMASTRCFG:
 360                         return eql_g_master_cfg (dev, (master_config_t *) ifr->ifr_data);
 361                 case EQL_SETMASTRCFG:
 362                         return eql_s_master_cfg (dev, (master_config_t *) ifr->ifr_data);
 363                 default:
 364                         return -EOPNOTSUPP;
 365         }
 366 }
 367 
 368 
 369 static int eql_slave_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 370 {
 371         equalizer_t *eql = (equalizer_t *) dev->priv;
 372         struct device *slave_dev = 0;
 373         slave_t *slave;
 374 
 375         if (skb == NULL)
 376                 return 0;
 377 
 378         eql_schedule_slaves (eql->queue);
 379   
 380         slave_dev = eql_best_slave_dev (eql->queue);
 381         slave = eql_best_slave (eql->queue); 
 382 
 383         if ( slave_dev != 0 )
 384         {
 385 #ifdef EQL_DEBUG
 386                 if (eql_debug >= 100)
 387                         printk ("%s: %d slaves xmitng %ld B %s\n", 
 388                                 dev->name, eql_number_slaves (eql->queue), skb->len,
 389                                 slave_dev->name);
 390 #endif
 391                 dev_queue_xmit (skb, slave_dev, 1);
 392                 eql->stats->tx_packets++;
 393                 slave->bytes_queued += skb->len; 
 394         }
 395         else
 396         {
 397                 /*
 398                  *      The alternative for this is the return 1 and have
 399                  *      dev_queue_xmit just queue it up on the eql's queue. 
 400                  */
 401 
 402                 eql->stats->tx_dropped++;
 403                 dev_kfree_skb(skb, FREE_WRITE);
 404         }         
 405         return 0;
 406 }
 407 
 408 
 409 static struct enet_statistics * eql_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 410 {
 411         equalizer_t *eql = (equalizer_t *) dev->priv;
 412         return eql->stats;
 413 }
 414 
 415 
 416 static int  eql_header(struct sk_buff *skb, struct device *dev, 
     /* [previous][next][first][last][top][bottom][index][help] */
 417            unsigned short type, void *daddr, void *saddr, 
 418            unsigned len)
 419 {
 420         return 0;
 421 }
 422 
 423 
 424 static int eql_rebuild_header(void *buff, struct device *dev, 
     /* [previous][next][first][last][top][bottom][index][help] */
 425                    unsigned long raddr, struct sk_buff *skb)
 426 {
 427         return 0;
 428 }
 429 
 430 /*
 431  *      Private ioctl functions
 432  */
 433 
 434 static int eql_enslave(struct device *dev, slaving_request_t *srqp)
     /* [previous][next][first][last][top][bottom][index][help] */
 435 {
 436         struct device *master_dev;
 437         struct device *slave_dev;
 438         slaving_request_t srq;
 439         int err;
 440 
 441         err = verify_area(VERIFY_READ, (void *)srqp, sizeof (slaving_request_t));
 442         if (err)  
 443           {
 444 #ifdef EQL_DEBUG
 445         if (eql_debug >= 20)
 446                 printk ("EQL enslave: error detected by verify_area\n");
 447 #endif  
 448                 return err;
 449           }
 450         memcpy_fromfs (&srq, srqp, sizeof (slaving_request_t));
 451 
 452 #ifdef EQL_DEBUG
 453         if (eql_debug >= 20)
 454                 printk ("%s: enslave '%s' %ld bps\n", dev->name, 
 455                         srq.slave_name, srq.priority);
 456 #endif  
 457         master_dev = dev;               /* for "clarity" */
 458         slave_dev  = dev_get (srq.slave_name);
 459 
 460         if (master_dev != 0 && slave_dev != 0)
 461         {
 462                 if (! eql_is_master (slave_dev)  &&   /* slave is not a master */
 463                         ! eql_is_slave (slave_dev)      ) /* slave is not already a slave */
 464                 {
 465                         slave_t *s = eql_new_slave ();
 466                         equalizer_t *eql = (equalizer_t *) master_dev->priv;
 467                         s->dev = slave_dev;
 468                         s->priority = srq.priority;
 469                         s->priority_bps = srq.priority;
 470                         s->priority_Bps = srq.priority / 8;
 471                         slave_dev->flags |= IFF_SLAVE;
 472                         eql_insert_slave (eql->queue, s);
 473                         return 0;
 474                 }
 475 #ifdef EQL_DEBUG
 476         if (eql_debug >= 20)
 477                 printk ("EQL enslave: slave is master or slave is already slave\n");
 478 #endif  
 479 
 480                 return -EINVAL;
 481         }
 482 #ifdef EQL_DEBUG
 483         if (eql_debug >= 20)
 484                 printk ("EQL enslave: master or slave are NULL");
 485 #endif  
 486         return -EINVAL;
 487 }
 488 
 489 static int eql_emancipate(struct device *dev, slaving_request_t *srqp)
     /* [previous][next][first][last][top][bottom][index][help] */
 490 {
 491         struct device *master_dev;
 492         struct device *slave_dev;
 493         slaving_request_t srq;
 494         int err;
 495 
 496         err = verify_area(VERIFY_READ, (void *)srqp, sizeof (slaving_request_t));
 497         if (err) 
 498                 return err;
 499 
 500         memcpy_fromfs (&srq, srqp, sizeof (slaving_request_t));
 501 #ifdef EQL_DEBUG
 502         if (eql_debug >= 20)
 503                 printk ("%s: emancipate `%s`\n", dev->name, srq.slave_name);
 504 #endif
 505         master_dev = dev;               /* for "clarity" */
 506         slave_dev  = dev_get (srq.slave_name);
 507 
 508         if ( eql_is_slave (slave_dev) ) /* really is a slave */
 509         {
 510                 equalizer_t *eql = (equalizer_t *) master_dev->priv;
 511                 slave_dev->flags = slave_dev->flags & ~IFF_SLAVE;
 512                 eql_remove_slave_dev (eql->queue, slave_dev);
 513                 return 0;
 514         }
 515         return -EINVAL;
 516 }
 517 
 518 
 519 static int eql_g_slave_cfg(struct device *dev, slave_config_t *scp)
     /* [previous][next][first][last][top][bottom][index][help] */
 520 {
 521         slave_t *slave;
 522         equalizer_t *eql;
 523         struct device *slave_dev;
 524         slave_config_t sc;
 525         int err;
 526 
 527         err = verify_area(VERIFY_READ, (void *)scp, sizeof (slave_config_t));
 528         if (err) 
 529                 return err;
 530 
 531         memcpy_fromfs (&sc, scp, sizeof (slave_config_t));
 532 #ifdef EQL_DEBUG
 533         if (eql_debug >= 20)
 534                 printk ("%s: get config for slave `%s'\n", dev->name, sc.slave_name);
 535 #endif
 536         eql = (equalizer_t *) dev->priv;
 537         slave_dev = dev_get (sc.slave_name);
 538 
 539         if ( eql_is_slave (slave_dev) )
 540         {
 541                 slave = eql_find_slave_dev (eql->queue,  slave_dev);
 542                 if (slave != 0)
 543                 {
 544                         sc.priority = slave->priority;
 545                         err = verify_area(VERIFY_WRITE, (void *)scp, sizeof (slave_config_t));
 546                         if (err) 
 547                                 return err;
 548                         memcpy_tofs (scp, &sc, sizeof (slave_config_t));
 549                         return 0;
 550                 }
 551         }
 552         return -EINVAL;
 553 }
 554 
 555 
 556 static int eql_s_slave_cfg(struct device *dev, slave_config_t *scp)
     /* [previous][next][first][last][top][bottom][index][help] */
 557 {
 558         slave_t *slave;
 559         equalizer_t *eql;
 560         struct device *slave_dev;
 561         slave_config_t sc;
 562         int err;
 563 
 564         err = verify_area(VERIFY_READ, (void *)scp, sizeof (slave_config_t));
 565         if (err) 
 566                 return err;
 567 
 568 #ifdef EQL_DEBUG
 569         if (eql_debug >= 20)
 570                 printk ("%s: set config for slave `%s'\n", dev->name, sc.slave_name);
 571 #endif
 572   
 573         memcpy_fromfs (&sc, scp, sizeof (slave_config_t));
 574 
 575         eql = (equalizer_t *) dev->priv;
 576         slave_dev = dev_get (sc.slave_name);
 577 
 578         if ( eql_is_slave (slave_dev) )
 579         {
 580                 slave = eql_find_slave_dev (eql->queue, slave_dev);
 581                 if (slave != 0)
 582                 {
 583                         slave->priority = sc.priority;
 584                         slave->priority_bps = sc.priority;
 585                         slave->priority_Bps = sc.priority / 8;
 586                         return 0;
 587                 }
 588         }
 589         return -EINVAL;
 590 }
 591 
 592 
 593 static int eql_g_master_cfg(struct device *dev, master_config_t *mcp)
     /* [previous][next][first][last][top][bottom][index][help] */
 594 {
 595         equalizer_t *eql;
 596         master_config_t mc;
 597 
 598 #if EQL_DEBUG
 599         if (eql_debug >= 20)
 600                 printk ("%s: get master config\n", dev->name);
 601 #endif
 602 
 603         if ( eql_is_master (dev) )
 604         {
 605                 int err;
 606                 err = verify_area(VERIFY_WRITE, (void *)mcp, sizeof (master_config_t));
 607                 if (err) 
 608                         return err;
 609                 eql = (equalizer_t *) dev->priv;
 610                 mc.max_slaves = eql->max_slaves;
 611                 mc.min_slaves = eql->min_slaves;
 612                 memcpy_tofs (mcp, &mc, sizeof (master_config_t));
 613                 return 0;
 614         }
 615         return -EINVAL;
 616 }
 617 
 618 
 619 static int eql_s_master_cfg(struct device *dev, master_config_t *mcp)
     /* [previous][next][first][last][top][bottom][index][help] */
 620 {
 621         equalizer_t *eql;
 622         master_config_t mc;
 623         int err;
 624 
 625         err = verify_area(VERIFY_READ, (void *)mcp, sizeof (master_config_t));
 626         if (err)
 627                 return err;
 628 #if EQL_DEBUG
 629         if (eql_debug >= 20)
 630                 printk ("%s: set master config\n", dev->name);
 631 #endif
 632         memcpy_fromfs (&mc, mcp, sizeof (master_config_t));
 633         if ( eql_is_master (dev) )
 634         {
 635                 eql = (equalizer_t *) dev->priv;
 636                 eql->max_slaves = mc.max_slaves;
 637                 eql->min_slaves = mc.min_slaves;
 638                 return 0;
 639         }
 640         return -EINVAL;
 641 }
 642 
 643 /*
 644  *      Private device support functions
 645  */
 646 
 647 static inline int eql_is_slave(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 648 {
 649         if (dev)
 650         {
 651                 if ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
 652                         return 1;
 653         }
 654         return 0;
 655 }
 656 
 657 
 658 static inline int eql_is_master(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 659 {
 660         if (dev)
 661         {
 662                 if ((dev->flags & IFF_MASTER) == IFF_MASTER)
 663                 return 1;
 664         }
 665         return 0;
 666 }
 667 
 668 
 669 static slave_t *eql_new_slave(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 670 {
 671         slave_t *slave;
 672 
 673         slave = (slave_t *) kmalloc (sizeof (slave_t), GFP_KERNEL);
 674         if (slave)
 675         {
 676                 memset(slave, 0, sizeof (slave_t));
 677                 return slave;
 678         }
 679         return 0;
 680 }
 681 
 682 
 683 static void eql_delete_slave(slave_t *slave)
     /* [previous][next][first][last][top][bottom][index][help] */
 684 {
 685         kfree (slave);
 686 }
 687 
 688 
 689 #if 0                           /* not currently used, will be used
 690                                    when we really use a priority queue */
 691 static long slave_Bps(slave_t *slave)
     /* [previous][next][first][last][top][bottom][index][help] */
 692 {
 693         return (slave->priority_Bps);
 694 }
 695 
 696 static long slave_bps(slave_t *slave)
     /* [previous][next][first][last][top][bottom][index][help] */
 697 {
 698         return (slave->priority_bps);
 699 }
 700 
 701 #endif
 702 
 703 static inline int eql_number_slaves(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 704 {
 705         return queue->num_slaves;
 706 }
 707 
 708 static inline int eql_is_empty(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 709 {
 710         if (eql_number_slaves (queue) == 0)
 711                 return 1;
 712         return 0;
 713 }
 714 
 715 static inline int eql_is_full(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 716 {
 717         equalizer_t *eql = (equalizer_t *) queue->master_dev->priv;
 718 
 719         if (eql_number_slaves (queue) == eql->max_slaves)
 720                 return 1;
 721         return 0;
 722 }
 723 
 724 static slave_queue_t *eql_new_slave_queue(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 725 {
 726         slave_queue_t *queue;
 727         slave_t *head_slave;
 728         slave_t *tail_slave;
 729 
 730         queue = (slave_queue_t *) kmalloc (sizeof (slave_queue_t), GFP_KERNEL);
 731         if (queue == NULL)
 732                 return 0;
 733         memset (queue, 0, sizeof (slave_queue_t));
 734         head_slave = eql_new_slave ();
 735         tail_slave = eql_new_slave ();
 736 
 737         if ( head_slave != 0 &&
 738                 tail_slave != 0 )
 739         {
 740                 head_slave->next = tail_slave;
 741                 tail_slave->next = 0;
 742                 queue->head = head_slave;
 743                 queue->num_slaves = 0;
 744                 queue->master_dev = dev;
 745         }
 746         else
 747         {
 748                 if (head_slave)
 749                         kfree(head_slave);
 750                 if (tail_slave)
 751                         kfree(tail_slave);
 752                 kfree (queue);
 753                 return 0;
 754         }
 755         return queue;
 756 }
 757 
 758 
 759 static void eql_delete_slave_queue(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 760 { 
 761         slave_t *zapped;
 762         /*
 763          *      This should only be called when there isn't a
 764          *      timer running that scans the data periodically.. 
 765          *      dev_close stops the timer... 
 766          */
 767 
 768         while ( ! eql_is_empty (queue) )
 769         {
 770                 zapped = eql_remove_slave (queue, queue->head->next);
 771                 eql_delete_slave (zapped);
 772         }
 773         kfree (queue->head->next);
 774         kfree (queue->head);
 775         kfree (queue);
 776 }
 777 
 778 static int eql_insert_slave(slave_queue_t *queue, slave_t *slave)
     /* [previous][next][first][last][top][bottom][index][help] */
 779 {
 780         cli ();
 781 
 782         if ( ! eql_is_full (queue) )
 783         {
 784                 slave_t *duplicate_slave = 0;
 785                 duplicate_slave = eql_find_slave_dev (queue, slave->dev);
 786                 if (duplicate_slave != 0)
 787                 {
 788 /*                        printk ("%s: found a duplicate, killing it and replacing\n",
 789                                   queue->master_dev->name); */
 790                         eql_delete_slave (eql_remove_slave (queue, duplicate_slave));
 791                 }
 792                 slave->next = queue->head->next;
 793                 queue->head->next = slave;
 794                 queue->num_slaves++;
 795                 sti ();
 796                 return 0;
 797         }
 798         sti ();
 799         return 1;
 800 }
 801 
 802 
 803 static slave_t *eql_remove_slave(slave_queue_t *queue, slave_t *slave)
     /* [previous][next][first][last][top][bottom][index][help] */
 804 {
 805         slave_t *prev;
 806         slave_t *curr;
 807 
 808         cli ();
 809 
 810         prev = queue->head;
 811         curr = queue->head->next;
 812         while (curr != slave && 
 813                 curr->dev != 0 )
 814         {
 815 /*              printk ("%s: remove_slave; searching...\n", queue->master_dev->name); */
 816                 prev = curr;
 817                 curr = curr->next;
 818         }
 819 
 820         if (curr == slave)
 821         {
 822                 prev->next = curr->next;
 823                 queue->num_slaves--;
 824                 curr->dev->flags = curr->dev->flags & ~IFF_SLAVE;
 825                 sti();
 826                 return curr;
 827         }
 828         sti ();
 829         return 0;                       /* not found */
 830 }
 831 
 832 
 833 static int eql_remove_slave_dev(slave_queue_t *queue, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 834 {
 835         slave_t *prev;
 836         slave_t *curr;
 837         slave_t *target;
 838 
 839         target = eql_find_slave_dev (queue, dev);
 840 
 841         if (target != 0)
 842         {
 843                 cli ();
 844                 prev = queue->head;
 845                 curr = prev->next;
 846                 while (curr != target)
 847                 {
 848                         prev = curr;
 849                         curr = curr->next;
 850                 }
 851                 prev->next = curr->next;
 852                 queue->num_slaves--;
 853                 sti ();
 854                 eql_delete_slave (curr);
 855                 return 0;
 856         }
 857         return 1;
 858 }
 859 
 860 
 861 static inline struct device *eql_best_slave_dev(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 862 {
 863         if (queue->best_slave != 0)
 864         {
 865                 if (queue->best_slave->dev != 0)
 866                         return queue->best_slave->dev;
 867                 else
 868                         return 0;
 869         }
 870         else
 871                 return 0;
 872 }
 873 
 874 
 875 static inline slave_t *eql_best_slave(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 876 {
 877         return queue->best_slave;
 878 }
 879 
 880 static inline void eql_schedule_slaves(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 881 {
 882         struct device *master_dev = queue->master_dev;
 883         slave_t *best_slave = 0;
 884         slave_t *slave_corpse = 0;
 885 
 886 #ifdef EQL_DEBUG
 887         if (eql_debug >= 100)
 888                 printk ("%s: schedule %d slaves\n", 
 889                         master_dev->name, eql_number_slaves (queue));
 890 #endif
 891         if ( eql_is_empty (queue) )
 892         {
 893                 /*
 894                  *      No slaves to play with 
 895                  */
 896                 eql_set_best_slave (queue, (slave_t *) 0);
 897                 return;
 898         }
 899         else
 900         {               
 901                 /*
 902                  *      Make a pass to set the best slave 
 903                  */
 904                 unsigned long best_load = (unsigned long) ULONG_MAX;
 905                 slave_t *slave = 0;
 906                 int i;
 907 
 908                 cli ();
 909                 for (i = 1, slave = eql_first_slave (queue);
 910                         i <= eql_number_slaves (queue);
 911                         i++, slave = eql_next_slave (queue, slave))
 912                 {
 913                         /*
 914                          *      Go through the slave list once, updating best_slave 
 915                          *      whenever a new best_load is found, whenever a dead
 916                          *      slave is found, it is marked to be pulled out of the 
 917                          *      queue 
 918                          */
 919                 
 920                         unsigned long slave_load;
 921                         unsigned long bytes_queued; 
 922                         unsigned long priority_Bps; 
 923                 
 924                         if (slave != 0)
 925                         {
 926                                 bytes_queued = slave->bytes_queued;
 927                                 priority_Bps = slave->priority_Bps;    
 928                                 if ( slave->dev != 0)
 929                                 {
 930                                         if ((slave->dev->flags & IFF_UP) == IFF_UP )
 931                                         {
 932                                                 slave_load = (ULONG_MAX - (ULONG_MAX / 2)) - 
 933                                                         (priority_Bps) + bytes_queued * 8;
 934 
 935                                                 if (slave_load < best_load)
 936                                                 {
 937                                                         best_load = slave_load;
 938                                                         best_slave = slave;
 939                                                 }
 940                                         }
 941                                         else            /* we found a dead slave */
 942                                         {
 943                                                 /* 
 944                                                  *      We only bury one slave at a time, if more than
 945                                                  *      one slave dies, we will bury him on the next 
 946                                                  *      reschedule. slaves don't die all at once that 
 947                                                  *      much anyway 
 948                                                  */
 949                                                 slave_corpse = slave;
 950                                         }
 951                                 }
 952                         }
 953                 } /* for */
 954                 sti ();
 955                 eql_set_best_slave (queue, best_slave);
 956         } /* else */
 957         if (slave_corpse != 0)
 958         {
 959                 printk ("eql: scheduler found dead slave, burying...\n");
 960                 eql_delete_slave (eql_remove_slave (queue, slave_corpse));
 961         }
 962         return;
 963 }
 964 
 965 
 966 static slave_t * eql_find_slave_dev(slave_queue_t *queue, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 967 {
 968         slave_t *slave = 0;
 969         slave = eql_first_slave(queue);
 970 
 971         while (slave != 0 && slave->dev != dev && slave != 0)
 972         {
 973 #if 0
 974                 if (slave->dev != 0)
 975                         printk ("eql: find_slave_dev; looked at '%s'...\n", slave->dev->name);
 976                 else
 977                         printk ("eql: find_slave_dev; looked at nothing...\n");
 978 #endif
 979                 slave = slave->next;
 980         }
 981         return slave;
 982 }
 983 
 984 
 985 static inline slave_t *eql_first_slave(slave_queue_t *queue)
     /* [previous][next][first][last][top][bottom][index][help] */
 986 {
 987         return queue->head->next;
 988 }
 989 
 990 
 991 static inline slave_t *eql_next_slave(slave_queue_t *queue, slave_t *slave)
     /* [previous][next][first][last][top][bottom][index][help] */
 992 {
 993         return slave->next;
 994 }
 995 
 996 static inline void eql_set_best_slave(slave_queue_t *queue, slave_t *slave)
     /* [previous][next][first][last][top][bottom][index][help] */
 997 {
 998         queue->best_slave = slave;
 999 }
1000 
1001 static void eql_timer(unsigned long param)
     /* [previous][next][first][last][top][bottom][index][help] */
1002 {
1003         equalizer_t *eql = (equalizer_t *) param;
1004         slave_t *slave;
1005         slave_t *slave_corpse = 0;
1006         int i;
1007         
1008         if ( ! eql_is_empty (eql->queue) )
1009         {
1010                 cli ();
1011                 for (i = 1, slave = eql_first_slave (eql->queue);
1012                         i <= eql_number_slaves (eql->queue);
1013                         i++, slave = eql_next_slave (eql->queue, slave))
1014                 {
1015                         if (slave != 0)
1016                         {
1017                                 if ((slave->dev->flags & IFF_UP) == IFF_UP )
1018                                 {
1019                                         slave->bytes_queued -= slave->priority_Bps;
1020                                         if (slave->bytes_queued < 0)
1021                                                 slave->bytes_queued = 0;
1022                                 }
1023                                 else
1024                                         slave_corpse = slave;
1025                         }
1026                 }
1027                 sti ();
1028                 if (slave_corpse != 0)
1029                 {
1030                         printk ("eql: timer found dead slave, burying...\n");
1031                         eql_delete_slave (eql_remove_slave (eql->queue, slave_corpse));
1032                 }
1033         }
1034 
1035         if (eql->timer_on != 0) 
1036         {
1037                 eql->timer.expires = jiffies+EQL_DEFAULT_RESCHED_IVAL;
1038                 add_timer (&eql->timer);
1039         }
1040 }
1041 
1042 #ifdef MODULE
1043 static struct device dev_eql = 
1044 {
1045         "eql", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, eql_init 
1046 };
1047 
1048 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1049 {
1050         if (register_netdev(&dev_eql) != 0) {
1051                 printk("eql: register_netdev() returned non-zero.\n");
1052                 return -EIO;
1053         }
1054         return 0;
1055 }
1056 
1057 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1058 {
1059         unregister_netdev(&dev_eql);
1060 }
1061 #endif /* MODULE */
1062 
1063 /*
1064  * Local Variables: 
1065  * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c eql.c"
1066  * version-control: t
1067  * kept-new-versions: 20
1068  * End:
1069  */

/* [previous][next][first][last][top][bottom][index][help] */