More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / eql.c
blobbfe053425de6868c3d31dabacdb3654f80c77374
1 /*
2 * Equalizer Load-balancer for serial network interfaces.
4 * (c) Copyright 1995 Simon "Guru Aleph-Null" Janes
5 * NCM: Network and Communications Management, Inc.
7 * (c) Copyright 2002 David S. Miller (davem@redhat.com)
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
12 * The author may be reached as simon@ncm.com, or C/O
13 * NCM
14 * Attn: Simon Janes
15 * 6803 Whittier Ave
16 * McLean VA 22101
17 * Phone: 1-703-847-0040 ext 103
21 * Sources:
22 * skeleton.c by Donald Becker.
23 * Inspirations:
24 * The Harried and Overworked Alan Cox
25 * Conspiracies:
26 * The Alan Cox and Mike McLagan plot to get someone else to do the code,
27 * which turned out to be me.
31 * $Log: eql.c,v $
32 * Revision 1.2 1996/04/11 17:51:52 guru
33 * Added one-line eql_remove_slave patch.
35 * Revision 1.1 1996/04/11 17:44:17 guru
36 * Initial revision
38 * Revision 3.13 1996/01/21 15:17:18 alan
39 * tx_queue_len changes.
40 * reformatted.
42 * Revision 3.12 1995/03/22 21:07:51 anarchy
43 * Added capable() checks on configuration.
44 * Moved header file.
46 * Revision 3.11 1995/01/19 23:14:31 guru
47 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
48 * (priority_Bps) + bytes_queued * 8;
50 * Revision 3.10 1995/01/19 23:07:53 guru
51 * back to
52 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
53 * (priority_Bps) + bytes_queued;
55 * Revision 3.9 1995/01/19 22:38:20 guru
56 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
57 * (priority_Bps) + bytes_queued * 4;
59 * Revision 3.8 1995/01/19 22:30:55 guru
60 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
61 * (priority_Bps) + bytes_queued * 2;
63 * Revision 3.7 1995/01/19 21:52:35 guru
64 * printk's trimmed out.
66 * Revision 3.6 1995/01/19 21:49:56 guru
67 * This is working pretty well. I gained 1 K/s in speed.. now it's just
68 * robustness and printk's to be diked out.
70 * Revision 3.5 1995/01/18 22:29:59 guru
71 * still crashes the kernel when the lock_wait thing is woken up.
73 * Revision 3.4 1995/01/18 21:59:47 guru
74 * Broken set-bit locking snapshot
76 * Revision 3.3 1995/01/17 22:09:18 guru
77 * infinite sleep in a lock somewhere..
79 * Revision 3.2 1995/01/15 16:46:06 guru
80 * Log trimmed of non-pertinent 1.x branch messages
82 * Revision 3.1 1995/01/15 14:41:45 guru
83 * New Scheduler and timer stuff...
85 * Revision 1.15 1995/01/15 14:29:02 guru
86 * Will make 1.14 (now 1.15) the 3.0 branch, and the 1.12 the 2.0 branch, the one
87 * with the dumber scheduler
89 * Revision 1.14 1995/01/15 02:37:08 guru
90 * shock.. the kept-new-versions could have zonked working
91 * stuff.. shudder
93 * Revision 1.13 1995/01/15 02:36:31 guru
94 * big changes
96 * scheduler was torn out and replaced with something smarter
98 * global names not prefixed with eql_ were renamed to protect
99 * against namespace collisions
101 * a few more abstract interfaces were added to facilitate any
102 * potential change of datastructure. the driver is still using
103 * a linked list of slaves. going to a heap would be a bit of
104 * an overkill.
106 * this compiles fine with no warnings.
108 * the locking mechanism and timer stuff must be written however,
109 * this version will not work otherwise
111 * Sorry, I had to rewrite most of this for 2.5.x -DaveM
114 #include <linux/module.h>
115 #include <linux/kernel.h>
116 #include <linux/init.h>
117 #include <linux/timer.h>
118 #include <linux/netdevice.h>
120 #include <linux/if.h>
121 #include <linux/if_arp.h>
122 #include <linux/if_eql.h>
124 #include <asm/uaccess.h>
126 static int eql_open(struct net_device *dev);
127 static int eql_close(struct net_device *dev);
128 static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
129 static int eql_slave_xmit(struct sk_buff *skb, struct net_device *dev);
130 static struct net_device_stats *eql_get_stats(struct net_device *dev);
132 #define eql_is_slave(dev) ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
133 #define eql_is_master(dev) ((dev->flags & IFF_MASTER) == IFF_MASTER)
135 static void eql_kill_one_slave(slave_t *slave);
137 static void eql_timer(unsigned long param)
139 equalizer_t *eql = (equalizer_t *) param;
140 struct list_head *this, *tmp, *head;
142 spin_lock_bh(&eql->queue.lock);
143 head = &eql->queue.all_slaves;
144 list_for_each_safe(this, tmp, head) {
145 slave_t *slave = list_entry(this, slave_t, list);
147 if ((slave->dev->flags & IFF_UP) == IFF_UP) {
148 slave->bytes_queued -= slave->priority_Bps;
149 if (slave->bytes_queued < 0)
150 slave->bytes_queued = 0;
151 } else {
152 eql_kill_one_slave(slave);
156 spin_unlock_bh(&eql->queue.lock);
158 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
159 add_timer(&eql->timer);
162 static char version[] __initdata =
163 "Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)\n";
165 static int __init eql_init(struct net_device *dev)
167 static unsigned int version_printed;
168 equalizer_t *eql;
170 SET_MODULE_OWNER(dev);
172 if (version_printed++ == 0)
173 printk(version);
175 dev->priv = kmalloc(sizeof (equalizer_t), GFP_KERNEL);
176 if (dev->priv == NULL)
177 return -ENOMEM;
178 memset(dev->priv, 0, sizeof (equalizer_t));
179 eql = dev->priv;
181 init_timer(&eql->timer);
182 eql->timer.data = (unsigned long) dev->priv;
183 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
184 eql->timer.function = eql_timer;
186 spin_lock_init(&eql->queue.lock);
187 INIT_LIST_HEAD(&eql->queue.all_slaves);
188 eql->queue.master_dev = dev;
190 dev->open = eql_open;
191 dev->stop = eql_close;
192 dev->do_ioctl = eql_ioctl;
193 dev->hard_start_xmit = eql_slave_xmit;
194 dev->get_stats = eql_get_stats;
197 * Now we undo some of the things that eth_setup does
198 * that we don't like
201 dev->mtu = EQL_DEFAULT_MTU; /* set to 576 in if_eql.h */
202 dev->flags = IFF_MASTER;
204 dev->type = ARPHRD_SLIP;
205 dev->tx_queue_len = 5; /* Hands them off fast */
207 return 0;
210 static int eql_open(struct net_device *dev)
212 equalizer_t *eql = dev->priv;
214 /* XXX We should force this off automatically for the user. */
215 printk(KERN_INFO "%s: remember to turn off Van-Jacobson compression on "
216 "your slave devices.\n", dev->name);
218 if (!list_empty(&eql->queue.all_slaves))
219 BUG();
221 eql->min_slaves = 1;
222 eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
224 add_timer(&eql->timer);
226 return 0;
229 static void eql_kill_one_slave(slave_t *slave)
231 list_del(&slave->list);
232 slave->dev->flags &= ~IFF_SLAVE;
233 dev_put(slave->dev);
234 kfree(slave);
237 static void eql_kill_slave_queue(slave_queue_t *queue)
239 struct list_head *head, *tmp, *this;
241 spin_lock_bh(&queue->lock);
243 head = &queue->all_slaves;
244 list_for_each_safe(this, tmp, head) {
245 slave_t *s = list_entry(this, slave_t, list);
247 eql_kill_one_slave(s);
248 queue->num_slaves--;
251 spin_unlock_bh(&queue->lock);
254 static int eql_close(struct net_device *dev)
256 equalizer_t *eql = dev->priv;
259 * The timer has to be stopped first before we start hacking away
260 * at the data structure it scans every so often...
263 del_timer_sync(&eql->timer);
265 eql_kill_slave_queue(&eql->queue);
267 return 0;
270 static int eql_enslave(struct net_device *dev, slaving_request_t *srq);
271 static int eql_emancipate(struct net_device *dev, slaving_request_t *srq);
273 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t *sc);
274 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t *sc);
276 static int eql_g_master_cfg(struct net_device *dev, master_config_t *mc);
277 static int eql_s_master_cfg(struct net_device *dev, master_config_t *mc);
279 static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
281 if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG &&
282 !capable(CAP_NET_ADMIN))
283 return -EPERM;
285 switch (cmd) {
286 case EQL_ENSLAVE:
287 return eql_enslave(dev,
288 (slaving_request_t *) ifr->ifr_data);
289 case EQL_EMANCIPATE:
290 return eql_emancipate(dev,
291 (slaving_request_t *) ifr->ifr_data);
292 case EQL_GETSLAVECFG:
293 return eql_g_slave_cfg(dev,
294 (slave_config_t *) ifr->ifr_data);
295 case EQL_SETSLAVECFG:
296 return eql_s_slave_cfg(dev,
297 (slave_config_t *) ifr->ifr_data);
298 case EQL_GETMASTRCFG:
299 return eql_g_master_cfg(dev,
300 (master_config_t *) ifr->ifr_data);
301 case EQL_SETMASTRCFG:
302 return eql_s_master_cfg(dev,
303 (master_config_t *) ifr->ifr_data);
304 default:
305 return -EOPNOTSUPP;
309 /* queue->lock must be held */
310 static slave_t *__eql_schedule_slaves(slave_queue_t *queue)
312 unsigned long best_load = ~0UL;
313 struct list_head *this, *tmp, *head;
314 slave_t *best_slave;
316 best_slave = NULL;
318 /* Make a pass to set the best slave. */
319 head = &queue->all_slaves;
320 list_for_each_safe(this, tmp, head) {
321 slave_t *slave = list_entry(this, slave_t, list);
322 unsigned long slave_load, bytes_queued, priority_Bps;
324 /* Go through the slave list once, updating best_slave
325 * whenever a new best_load is found.
327 bytes_queued = slave->bytes_queued;
328 priority_Bps = slave->priority_Bps;
329 if ((slave->dev->flags & IFF_UP) == IFF_UP) {
330 slave_load = (~0UL - (~0UL / 2)) -
331 (priority_Bps) + bytes_queued * 8;
333 if (slave_load < best_load) {
334 best_load = slave_load;
335 best_slave = slave;
337 } else {
338 /* We found a dead slave, kill it. */
339 eql_kill_one_slave(slave);
342 return best_slave;
345 static int eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
347 equalizer_t *eql = dev->priv;
348 slave_t *slave;
350 spin_lock(&eql->queue.lock);
352 slave = __eql_schedule_slaves(&eql->queue);
353 if (slave) {
354 struct net_device *slave_dev = slave->dev;
356 skb->dev = slave_dev;
357 skb->priority = 1;
358 slave->bytes_queued += skb->len;
359 dev_queue_xmit(skb);
360 eql->stats.tx_packets++;
361 } else {
362 eql->stats.tx_dropped++;
363 dev_kfree_skb(skb);
366 spin_unlock(&eql->queue.lock);
368 return 0;
371 static struct net_device_stats * eql_get_stats(struct net_device *dev)
373 equalizer_t *eql = dev->priv;
374 return &eql->stats;
378 * Private ioctl functions
381 /* queue->lock must be held */
382 static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
384 struct list_head *this, *head;
386 head = &queue->all_slaves;
387 list_for_each(this, head) {
388 slave_t *slave = list_entry(this, slave_t, list);
390 if (slave->dev == dev)
391 return slave;
394 return NULL;
397 static inline int eql_is_full(slave_queue_t *queue)
399 equalizer_t *eql = queue->master_dev->priv;
401 if (queue->num_slaves >= eql->max_slaves)
402 return 1;
403 return 0;
406 /* queue->lock must be held */
407 static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave)
409 if (!eql_is_full(queue)) {
410 slave_t *duplicate_slave = 0;
412 duplicate_slave = __eql_find_slave_dev(queue, slave->dev);
413 if (duplicate_slave != 0)
414 eql_kill_one_slave(duplicate_slave);
416 list_add(&slave->list, &queue->all_slaves);
417 queue->num_slaves++;
418 slave->dev->flags |= IFF_SLAVE;
420 return 0;
423 return -ENOSPC;
426 static int eql_enslave(struct net_device *master_dev, slaving_request_t *srqp)
428 struct net_device *slave_dev;
429 slaving_request_t srq;
431 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
432 return -EFAULT;
434 slave_dev = dev_get_by_name(srq.slave_name);
435 if (slave_dev) {
436 if ((master_dev->flags & IFF_UP) == IFF_UP) {
437 /* slave is not a master & not already a slave: */
438 if (!eql_is_master(slave_dev) &&
439 !eql_is_slave(slave_dev)) {
440 slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL);
441 equalizer_t *eql = master_dev->priv;
442 int ret;
444 if (!s) {
445 dev_put(slave_dev);
446 return -ENOMEM;
449 memset(s, 0, sizeof(*s));
450 s->dev = slave_dev;
451 s->priority = srq.priority;
452 s->priority_bps = srq.priority;
453 s->priority_Bps = srq.priority / 8;
455 spin_lock_bh(&eql->queue.lock);
456 ret = __eql_insert_slave(&eql->queue, s);
457 if (ret) {
458 dev_put(slave_dev);
459 kfree(s);
461 spin_unlock_bh(&eql->queue.lock);
463 return ret;
466 dev_put(slave_dev);
469 return -EINVAL;
472 static int eql_emancipate(struct net_device *master_dev, slaving_request_t *srqp)
474 equalizer_t *eql = master_dev->priv;
475 struct net_device *slave_dev;
476 slaving_request_t srq;
477 int ret;
479 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
480 return -EFAULT;
482 slave_dev = dev_get_by_name(srq.slave_name);
483 ret = -EINVAL;
484 if (slave_dev) {
485 spin_lock_bh(&eql->queue.lock);
487 if (eql_is_slave(slave_dev)) {
488 slave_t *slave = __eql_find_slave_dev(&eql->queue,
489 slave_dev);
491 if (slave) {
492 eql_kill_one_slave(slave);
493 ret = 0;
496 dev_put(slave_dev);
498 spin_unlock_bh(&eql->queue.lock);
501 return ret;
504 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t *scp)
506 equalizer_t *eql = dev->priv;
507 slave_t *slave;
508 struct net_device *slave_dev;
509 slave_config_t sc;
510 int ret;
512 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
513 return -EFAULT;
515 slave_dev = dev_get_by_name(sc.slave_name);
517 ret = -EINVAL;
519 spin_lock_bh(&eql->queue.lock);
520 if (eql_is_slave(slave_dev)) {
521 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
522 if (slave) {
523 sc.priority = slave->priority;
524 ret = 0;
527 spin_unlock_bh(&eql->queue.lock);
529 dev_put(slave_dev);
531 if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t)))
532 ret = -EFAULT;
534 return ret;
537 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t *scp)
539 slave_t *slave;
540 equalizer_t *eql;
541 struct net_device *slave_dev;
542 slave_config_t sc;
543 int ret;
545 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
546 return -EFAULT;
548 eql = dev->priv;
549 slave_dev = dev_get_by_name(sc.slave_name);
551 ret = -EINVAL;
553 spin_lock_bh(&eql->queue.lock);
554 if (eql_is_slave(slave_dev)) {
555 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
556 if (slave) {
557 slave->priority = sc.priority;
558 slave->priority_bps = sc.priority;
559 slave->priority_Bps = sc.priority / 8;
560 ret = 0;
563 spin_unlock_bh(&eql->queue.lock);
565 return ret;
568 static int eql_g_master_cfg(struct net_device *dev, master_config_t *mcp)
570 equalizer_t *eql;
571 master_config_t mc;
573 if (eql_is_master(dev)) {
574 eql = dev->priv;
575 mc.max_slaves = eql->max_slaves;
576 mc.min_slaves = eql->min_slaves;
577 if (copy_to_user(mcp, &mc, sizeof (master_config_t)))
578 return -EFAULT;
579 return 0;
581 return -EINVAL;
584 static int eql_s_master_cfg(struct net_device *dev, master_config_t *mcp)
586 equalizer_t *eql;
587 master_config_t mc;
589 if (copy_from_user(&mc, mcp, sizeof (master_config_t)))
590 return -EFAULT;
592 if (eql_is_master(dev)) {
593 eql = dev->priv;
594 eql->max_slaves = mc.max_slaves;
595 eql->min_slaves = mc.min_slaves;
596 return 0;
598 return -EINVAL;
601 static struct net_device dev_eql;
603 static int __init eql_init_module(void)
605 strcpy(dev_eql.name, "eql");
606 dev_eql.init = eql_init;
607 if (register_netdev(&dev_eql) != 0) {
608 printk("eql: register_netdev() returned non-zero.\n");
609 return -EIO;
611 return 0;
614 static void __exit eql_cleanup_module(void)
616 kfree(dev_eql.priv);
617 unregister_netdev(&dev_eql);
620 module_init(eql_init_module);
621 module_exit(eql_cleanup_module);
622 MODULE_LICENSE("GPL");