OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [net/] [eql.c] - Blame information for rev 1275

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.