via-velocity: Give RX descriptors to the NIC later on open or MTU change
[linux-2.6/linux-2.6-openrd.git] / drivers / net / eql.c
blobf5b96cadeb2544994c4aadeeb45f30113c082116
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/capability.h>
115 #include <linux/module.h>
116 #include <linux/kernel.h>
117 #include <linux/init.h>
118 #include <linux/timer.h>
119 #include <linux/netdevice.h>
120 #include <net/net_namespace.h>
122 #include <linux/if.h>
123 #include <linux/if_arp.h>
124 #include <linux/if_eql.h>
126 #include <asm/uaccess.h>
128 static int eql_open(struct net_device *dev);
129 static int eql_close(struct net_device *dev);
130 static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
131 static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev);
133 #define eql_is_slave(dev) ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
134 #define eql_is_master(dev) ((dev->flags & IFF_MASTER) == IFF_MASTER)
136 static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave);
138 static void eql_timer(unsigned long param)
140 equalizer_t *eql = (equalizer_t *) param;
141 struct list_head *this, *tmp, *head;
143 spin_lock_bh(&eql->queue.lock);
144 head = &eql->queue.all_slaves;
145 list_for_each_safe(this, tmp, head) {
146 slave_t *slave = list_entry(this, slave_t, list);
148 if ((slave->dev->flags & IFF_UP) == IFF_UP) {
149 slave->bytes_queued -= slave->priority_Bps;
150 if (slave->bytes_queued < 0)
151 slave->bytes_queued = 0;
152 } else {
153 eql_kill_one_slave(&eql->queue, slave);
157 spin_unlock_bh(&eql->queue.lock);
159 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
160 add_timer(&eql->timer);
163 static const char version[] __initconst =
164 "Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)\n";
166 static const struct net_device_ops eql_netdev_ops = {
167 .ndo_open = eql_open,
168 .ndo_stop = eql_close,
169 .ndo_do_ioctl = eql_ioctl,
170 .ndo_start_xmit = eql_slave_xmit,
173 static void __init eql_setup(struct net_device *dev)
175 equalizer_t *eql = netdev_priv(dev);
177 init_timer(&eql->timer);
178 eql->timer.data = (unsigned long) eql;
179 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
180 eql->timer.function = eql_timer;
182 spin_lock_init(&eql->queue.lock);
183 INIT_LIST_HEAD(&eql->queue.all_slaves);
184 eql->queue.master_dev = dev;
186 dev->netdev_ops = &eql_netdev_ops;
189 * Now we undo some of the things that eth_setup does
190 * that we don't like
193 dev->mtu = EQL_DEFAULT_MTU; /* set to 576 in if_eql.h */
194 dev->flags = IFF_MASTER;
196 dev->type = ARPHRD_SLIP;
197 dev->tx_queue_len = 5; /* Hands them off fast */
198 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
201 static int eql_open(struct net_device *dev)
203 equalizer_t *eql = netdev_priv(dev);
205 /* XXX We should force this off automatically for the user. */
206 printk(KERN_INFO "%s: remember to turn off Van-Jacobson compression on "
207 "your slave devices.\n", dev->name);
209 BUG_ON(!list_empty(&eql->queue.all_slaves));
211 eql->min_slaves = 1;
212 eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
214 add_timer(&eql->timer);
216 return 0;
219 static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave)
221 list_del(&slave->list);
222 queue->num_slaves--;
223 slave->dev->flags &= ~IFF_SLAVE;
224 dev_put(slave->dev);
225 kfree(slave);
228 static void eql_kill_slave_queue(slave_queue_t *queue)
230 struct list_head *head, *tmp, *this;
232 spin_lock_bh(&queue->lock);
234 head = &queue->all_slaves;
235 list_for_each_safe(this, tmp, head) {
236 slave_t *s = list_entry(this, slave_t, list);
238 eql_kill_one_slave(queue, s);
241 spin_unlock_bh(&queue->lock);
244 static int eql_close(struct net_device *dev)
246 equalizer_t *eql = netdev_priv(dev);
249 * The timer has to be stopped first before we start hacking away
250 * at the data structure it scans every so often...
253 del_timer_sync(&eql->timer);
255 eql_kill_slave_queue(&eql->queue);
257 return 0;
260 static int eql_enslave(struct net_device *dev, slaving_request_t __user *srq);
261 static int eql_emancipate(struct net_device *dev, slaving_request_t __user *srq);
263 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
264 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
266 static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mc);
267 static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mc);
269 static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
271 if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG &&
272 !capable(CAP_NET_ADMIN))
273 return -EPERM;
275 switch (cmd) {
276 case EQL_ENSLAVE:
277 return eql_enslave(dev, ifr->ifr_data);
278 case EQL_EMANCIPATE:
279 return eql_emancipate(dev, ifr->ifr_data);
280 case EQL_GETSLAVECFG:
281 return eql_g_slave_cfg(dev, ifr->ifr_data);
282 case EQL_SETSLAVECFG:
283 return eql_s_slave_cfg(dev, ifr->ifr_data);
284 case EQL_GETMASTRCFG:
285 return eql_g_master_cfg(dev, ifr->ifr_data);
286 case EQL_SETMASTRCFG:
287 return eql_s_master_cfg(dev, ifr->ifr_data);
288 default:
289 return -EOPNOTSUPP;
293 /* queue->lock must be held */
294 static slave_t *__eql_schedule_slaves(slave_queue_t *queue)
296 unsigned long best_load = ~0UL;
297 struct list_head *this, *tmp, *head;
298 slave_t *best_slave;
300 best_slave = NULL;
302 /* Make a pass to set the best slave. */
303 head = &queue->all_slaves;
304 list_for_each_safe(this, tmp, head) {
305 slave_t *slave = list_entry(this, slave_t, list);
306 unsigned long slave_load, bytes_queued, priority_Bps;
308 /* Go through the slave list once, updating best_slave
309 * whenever a new best_load is found.
311 bytes_queued = slave->bytes_queued;
312 priority_Bps = slave->priority_Bps;
313 if ((slave->dev->flags & IFF_UP) == IFF_UP) {
314 slave_load = (~0UL - (~0UL / 2)) -
315 (priority_Bps) + bytes_queued * 8;
317 if (slave_load < best_load) {
318 best_load = slave_load;
319 best_slave = slave;
321 } else {
322 /* We found a dead slave, kill it. */
323 eql_kill_one_slave(queue, slave);
326 return best_slave;
329 static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
331 equalizer_t *eql = netdev_priv(dev);
332 slave_t *slave;
334 spin_lock(&eql->queue.lock);
336 slave = __eql_schedule_slaves(&eql->queue);
337 if (slave) {
338 struct net_device *slave_dev = slave->dev;
340 skb->dev = slave_dev;
341 skb->priority = 1;
342 slave->bytes_queued += skb->len;
343 dev_queue_xmit(skb);
344 dev->stats.tx_packets++;
345 } else {
346 dev->stats.tx_dropped++;
347 dev_kfree_skb(skb);
350 spin_unlock(&eql->queue.lock);
352 return NETDEV_TX_OK;
356 * Private ioctl functions
359 /* queue->lock must be held */
360 static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
362 struct list_head *this, *head;
364 head = &queue->all_slaves;
365 list_for_each(this, head) {
366 slave_t *slave = list_entry(this, slave_t, list);
368 if (slave->dev == dev)
369 return slave;
372 return NULL;
375 static inline int eql_is_full(slave_queue_t *queue)
377 equalizer_t *eql = netdev_priv(queue->master_dev);
379 if (queue->num_slaves >= eql->max_slaves)
380 return 1;
381 return 0;
384 /* queue->lock must be held */
385 static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave)
387 if (!eql_is_full(queue)) {
388 slave_t *duplicate_slave = NULL;
390 duplicate_slave = __eql_find_slave_dev(queue, slave->dev);
391 if (duplicate_slave)
392 eql_kill_one_slave(queue, duplicate_slave);
394 list_add(&slave->list, &queue->all_slaves);
395 queue->num_slaves++;
396 slave->dev->flags |= IFF_SLAVE;
398 return 0;
401 return -ENOSPC;
404 static int eql_enslave(struct net_device *master_dev, slaving_request_t __user *srqp)
406 struct net_device *slave_dev;
407 slaving_request_t srq;
409 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
410 return -EFAULT;
412 slave_dev = dev_get_by_name(&init_net, srq.slave_name);
413 if (slave_dev) {
414 if ((master_dev->flags & IFF_UP) == IFF_UP) {
415 /* slave is not a master & not already a slave: */
416 if (!eql_is_master(slave_dev) &&
417 !eql_is_slave(slave_dev)) {
418 slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL);
419 equalizer_t *eql = netdev_priv(master_dev);
420 int ret;
422 if (!s) {
423 dev_put(slave_dev);
424 return -ENOMEM;
427 memset(s, 0, sizeof(*s));
428 s->dev = slave_dev;
429 s->priority = srq.priority;
430 s->priority_bps = srq.priority;
431 s->priority_Bps = srq.priority / 8;
433 spin_lock_bh(&eql->queue.lock);
434 ret = __eql_insert_slave(&eql->queue, s);
435 if (ret) {
436 dev_put(slave_dev);
437 kfree(s);
439 spin_unlock_bh(&eql->queue.lock);
441 return ret;
444 dev_put(slave_dev);
447 return -EINVAL;
450 static int eql_emancipate(struct net_device *master_dev, slaving_request_t __user *srqp)
452 equalizer_t *eql = netdev_priv(master_dev);
453 struct net_device *slave_dev;
454 slaving_request_t srq;
455 int ret;
457 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
458 return -EFAULT;
460 slave_dev = dev_get_by_name(&init_net, srq.slave_name);
461 ret = -EINVAL;
462 if (slave_dev) {
463 spin_lock_bh(&eql->queue.lock);
465 if (eql_is_slave(slave_dev)) {
466 slave_t *slave = __eql_find_slave_dev(&eql->queue,
467 slave_dev);
469 if (slave) {
470 eql_kill_one_slave(&eql->queue, slave);
471 ret = 0;
474 dev_put(slave_dev);
476 spin_unlock_bh(&eql->queue.lock);
479 return ret;
482 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
484 equalizer_t *eql = netdev_priv(dev);
485 slave_t *slave;
486 struct net_device *slave_dev;
487 slave_config_t sc;
488 int ret;
490 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
491 return -EFAULT;
493 slave_dev = dev_get_by_name(&init_net, sc.slave_name);
494 if (!slave_dev)
495 return -ENODEV;
497 ret = -EINVAL;
499 spin_lock_bh(&eql->queue.lock);
500 if (eql_is_slave(slave_dev)) {
501 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
502 if (slave) {
503 sc.priority = slave->priority;
504 ret = 0;
507 spin_unlock_bh(&eql->queue.lock);
509 dev_put(slave_dev);
511 if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t)))
512 ret = -EFAULT;
514 return ret;
517 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
519 slave_t *slave;
520 equalizer_t *eql;
521 struct net_device *slave_dev;
522 slave_config_t sc;
523 int ret;
525 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
526 return -EFAULT;
528 slave_dev = dev_get_by_name(&init_net, sc.slave_name);
529 if (!slave_dev)
530 return -ENODEV;
532 ret = -EINVAL;
534 eql = netdev_priv(dev);
535 spin_lock_bh(&eql->queue.lock);
536 if (eql_is_slave(slave_dev)) {
537 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
538 if (slave) {
539 slave->priority = sc.priority;
540 slave->priority_bps = sc.priority;
541 slave->priority_Bps = sc.priority / 8;
542 ret = 0;
545 spin_unlock_bh(&eql->queue.lock);
547 dev_put(slave_dev);
549 return ret;
552 static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mcp)
554 equalizer_t *eql;
555 master_config_t mc;
557 if (eql_is_master(dev)) {
558 eql = netdev_priv(dev);
559 mc.max_slaves = eql->max_slaves;
560 mc.min_slaves = eql->min_slaves;
561 if (copy_to_user(mcp, &mc, sizeof (master_config_t)))
562 return -EFAULT;
563 return 0;
565 return -EINVAL;
568 static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mcp)
570 equalizer_t *eql;
571 master_config_t mc;
573 if (copy_from_user(&mc, mcp, sizeof (master_config_t)))
574 return -EFAULT;
576 if (eql_is_master(dev)) {
577 eql = netdev_priv(dev);
578 eql->max_slaves = mc.max_slaves;
579 eql->min_slaves = mc.min_slaves;
580 return 0;
582 return -EINVAL;
585 static struct net_device *dev_eql;
587 static int __init eql_init_module(void)
589 int err;
591 printk(version);
593 dev_eql = alloc_netdev(sizeof(equalizer_t), "eql", eql_setup);
594 if (!dev_eql)
595 return -ENOMEM;
597 err = register_netdev(dev_eql);
598 if (err)
599 free_netdev(dev_eql);
600 return err;
603 static void __exit eql_cleanup_module(void)
605 unregister_netdev(dev_eql);
606 free_netdev(dev_eql);
609 module_init(eql_init_module);
610 module_exit(eql_cleanup_module);
611 MODULE_LICENSE("GPL");