Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[deliverable/linux.git] / drivers / net / eql.c
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 * (c) Copyright 2002 David S. Miller (davem@redhat.com)
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 *
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
18 */
19
20 /*
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.
28 */
29
30 /*
31 * $Log: eql.c,v $
32 * Revision 1.2 1996/04/11 17:51:52 guru
33 * Added one-line eql_remove_slave patch.
34 *
35 * Revision 1.1 1996/04/11 17:44:17 guru
36 * Initial revision
37 *
38 * Revision 3.13 1996/01/21 15:17:18 alan
39 * tx_queue_len changes.
40 * reformatted.
41 *
42 * Revision 3.12 1995/03/22 21:07:51 anarchy
43 * Added capable() checks on configuration.
44 * Moved header file.
45 *
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;
49 *
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;
54 *
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;
58 *
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;
62 *
63 * Revision 3.7 1995/01/19 21:52:35 guru
64 * printk's trimmed out.
65 *
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.
69 *
70 * Revision 3.5 1995/01/18 22:29:59 guru
71 * still crashes the kernel when the lock_wait thing is woken up.
72 *
73 * Revision 3.4 1995/01/18 21:59:47 guru
74 * Broken set-bit locking snapshot
75 *
76 * Revision 3.3 1995/01/17 22:09:18 guru
77 * infinite sleep in a lock somewhere..
78 *
79 * Revision 3.2 1995/01/15 16:46:06 guru
80 * Log trimmed of non-pertinent 1.x branch messages
81 *
82 * Revision 3.1 1995/01/15 14:41:45 guru
83 * New Scheduler and timer stuff...
84 *
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
88 *
89 * Revision 1.14 1995/01/15 02:37:08 guru
90 * shock.. the kept-new-versions could have zonked working
91 * stuff.. shudder
92 *
93 * Revision 1.13 1995/01/15 02:36:31 guru
94 * big changes
95 *
96 * scheduler was torn out and replaced with something smarter
97 *
98 * global names not prefixed with eql_ were renamed to protect
99 * against namespace collisions
100 *
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.
105 *
106 * this compiles fine with no warnings.
107 *
108 * the locking mechanism and timer stuff must be written however,
109 * this version will not work otherwise
110 *
111 * Sorry, I had to rewrite most of this for 2.5.x -DaveM
112 */
113
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>
119 #include <net/net_namespace.h>
120
121 #include <linux/if.h>
122 #include <linux/if_arp.h>
123 #include <linux/if_eql.h>
124
125 #include <asm/uaccess.h>
126
127 static int eql_open(struct net_device *dev);
128 static int eql_close(struct net_device *dev);
129 static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
130 static int eql_slave_xmit(struct sk_buff *skb, struct net_device *dev);
131
132 #define eql_is_slave(dev) ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
133 #define eql_is_master(dev) ((dev->flags & IFF_MASTER) == IFF_MASTER)
134
135 static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave);
136
137 static void eql_timer(unsigned long param)
138 {
139 equalizer_t *eql = (equalizer_t *) param;
140 struct list_head *this, *tmp, *head;
141
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);
146
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(&eql->queue, slave);
153 }
154
155 }
156 spin_unlock_bh(&eql->queue.lock);
157
158 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
159 add_timer(&eql->timer);
160 }
161
162 static char version[] __initdata =
163 "Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)\n";
164
165 static void __init eql_setup(struct net_device *dev)
166 {
167 equalizer_t *eql = netdev_priv(dev);
168
169 init_timer(&eql->timer);
170 eql->timer.data = (unsigned long) eql;
171 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
172 eql->timer.function = eql_timer;
173
174 spin_lock_init(&eql->queue.lock);
175 INIT_LIST_HEAD(&eql->queue.all_slaves);
176 eql->queue.master_dev = dev;
177
178 dev->open = eql_open;
179 dev->stop = eql_close;
180 dev->do_ioctl = eql_ioctl;
181 dev->hard_start_xmit = eql_slave_xmit;
182
183 /*
184 * Now we undo some of the things that eth_setup does
185 * that we don't like
186 */
187
188 dev->mtu = EQL_DEFAULT_MTU; /* set to 576 in if_eql.h */
189 dev->flags = IFF_MASTER;
190
191 dev->type = ARPHRD_SLIP;
192 dev->tx_queue_len = 5; /* Hands them off fast */
193 }
194
195 static int eql_open(struct net_device *dev)
196 {
197 equalizer_t *eql = netdev_priv(dev);
198
199 /* XXX We should force this off automatically for the user. */
200 printk(KERN_INFO "%s: remember to turn off Van-Jacobson compression on "
201 "your slave devices.\n", dev->name);
202
203 BUG_ON(!list_empty(&eql->queue.all_slaves));
204
205 eql->min_slaves = 1;
206 eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
207
208 add_timer(&eql->timer);
209
210 return 0;
211 }
212
213 static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave)
214 {
215 list_del(&slave->list);
216 queue->num_slaves--;
217 slave->dev->flags &= ~IFF_SLAVE;
218 dev_put(slave->dev);
219 kfree(slave);
220 }
221
222 static void eql_kill_slave_queue(slave_queue_t *queue)
223 {
224 struct list_head *head, *tmp, *this;
225
226 spin_lock_bh(&queue->lock);
227
228 head = &queue->all_slaves;
229 list_for_each_safe(this, tmp, head) {
230 slave_t *s = list_entry(this, slave_t, list);
231
232 eql_kill_one_slave(queue, s);
233 }
234
235 spin_unlock_bh(&queue->lock);
236 }
237
238 static int eql_close(struct net_device *dev)
239 {
240 equalizer_t *eql = netdev_priv(dev);
241
242 /*
243 * The timer has to be stopped first before we start hacking away
244 * at the data structure it scans every so often...
245 */
246
247 del_timer_sync(&eql->timer);
248
249 eql_kill_slave_queue(&eql->queue);
250
251 return 0;
252 }
253
254 static int eql_enslave(struct net_device *dev, slaving_request_t __user *srq);
255 static int eql_emancipate(struct net_device *dev, slaving_request_t __user *srq);
256
257 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
258 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
259
260 static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mc);
261 static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mc);
262
263 static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
264 {
265 if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG &&
266 !capable(CAP_NET_ADMIN))
267 return -EPERM;
268
269 switch (cmd) {
270 case EQL_ENSLAVE:
271 return eql_enslave(dev, ifr->ifr_data);
272 case EQL_EMANCIPATE:
273 return eql_emancipate(dev, ifr->ifr_data);
274 case EQL_GETSLAVECFG:
275 return eql_g_slave_cfg(dev, ifr->ifr_data);
276 case EQL_SETSLAVECFG:
277 return eql_s_slave_cfg(dev, ifr->ifr_data);
278 case EQL_GETMASTRCFG:
279 return eql_g_master_cfg(dev, ifr->ifr_data);
280 case EQL_SETMASTRCFG:
281 return eql_s_master_cfg(dev, ifr->ifr_data);
282 default:
283 return -EOPNOTSUPP;
284 };
285 }
286
287 /* queue->lock must be held */
288 static slave_t *__eql_schedule_slaves(slave_queue_t *queue)
289 {
290 unsigned long best_load = ~0UL;
291 struct list_head *this, *tmp, *head;
292 slave_t *best_slave;
293
294 best_slave = NULL;
295
296 /* Make a pass to set the best slave. */
297 head = &queue->all_slaves;
298 list_for_each_safe(this, tmp, head) {
299 slave_t *slave = list_entry(this, slave_t, list);
300 unsigned long slave_load, bytes_queued, priority_Bps;
301
302 /* Go through the slave list once, updating best_slave
303 * whenever a new best_load is found.
304 */
305 bytes_queued = slave->bytes_queued;
306 priority_Bps = slave->priority_Bps;
307 if ((slave->dev->flags & IFF_UP) == IFF_UP) {
308 slave_load = (~0UL - (~0UL / 2)) -
309 (priority_Bps) + bytes_queued * 8;
310
311 if (slave_load < best_load) {
312 best_load = slave_load;
313 best_slave = slave;
314 }
315 } else {
316 /* We found a dead slave, kill it. */
317 eql_kill_one_slave(queue, slave);
318 }
319 }
320 return best_slave;
321 }
322
323 static int eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
324 {
325 equalizer_t *eql = netdev_priv(dev);
326 slave_t *slave;
327
328 spin_lock(&eql->queue.lock);
329
330 slave = __eql_schedule_slaves(&eql->queue);
331 if (slave) {
332 struct net_device *slave_dev = slave->dev;
333
334 skb->dev = slave_dev;
335 skb->priority = 1;
336 slave->bytes_queued += skb->len;
337 dev_queue_xmit(skb);
338 dev->stats.tx_packets++;
339 } else {
340 dev->stats.tx_dropped++;
341 dev_kfree_skb(skb);
342 }
343
344 spin_unlock(&eql->queue.lock);
345
346 return 0;
347 }
348
349 /*
350 * Private ioctl functions
351 */
352
353 /* queue->lock must be held */
354 static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
355 {
356 struct list_head *this, *head;
357
358 head = &queue->all_slaves;
359 list_for_each(this, head) {
360 slave_t *slave = list_entry(this, slave_t, list);
361
362 if (slave->dev == dev)
363 return slave;
364 }
365
366 return NULL;
367 }
368
369 static inline int eql_is_full(slave_queue_t *queue)
370 {
371 equalizer_t *eql = netdev_priv(queue->master_dev);
372
373 if (queue->num_slaves >= eql->max_slaves)
374 return 1;
375 return 0;
376 }
377
378 /* queue->lock must be held */
379 static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave)
380 {
381 if (!eql_is_full(queue)) {
382 slave_t *duplicate_slave = NULL;
383
384 duplicate_slave = __eql_find_slave_dev(queue, slave->dev);
385 if (duplicate_slave)
386 eql_kill_one_slave(queue, duplicate_slave);
387
388 list_add(&slave->list, &queue->all_slaves);
389 queue->num_slaves++;
390 slave->dev->flags |= IFF_SLAVE;
391
392 return 0;
393 }
394
395 return -ENOSPC;
396 }
397
398 static int eql_enslave(struct net_device *master_dev, slaving_request_t __user *srqp)
399 {
400 struct net_device *slave_dev;
401 slaving_request_t srq;
402
403 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
404 return -EFAULT;
405
406 slave_dev = dev_get_by_name(&init_net, srq.slave_name);
407 if (slave_dev) {
408 if ((master_dev->flags & IFF_UP) == IFF_UP) {
409 /* slave is not a master & not already a slave: */
410 if (!eql_is_master(slave_dev) &&
411 !eql_is_slave(slave_dev)) {
412 slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL);
413 equalizer_t *eql = netdev_priv(master_dev);
414 int ret;
415
416 if (!s) {
417 dev_put(slave_dev);
418 return -ENOMEM;
419 }
420
421 memset(s, 0, sizeof(*s));
422 s->dev = slave_dev;
423 s->priority = srq.priority;
424 s->priority_bps = srq.priority;
425 s->priority_Bps = srq.priority / 8;
426
427 spin_lock_bh(&eql->queue.lock);
428 ret = __eql_insert_slave(&eql->queue, s);
429 if (ret) {
430 dev_put(slave_dev);
431 kfree(s);
432 }
433 spin_unlock_bh(&eql->queue.lock);
434
435 return ret;
436 }
437 }
438 dev_put(slave_dev);
439 }
440
441 return -EINVAL;
442 }
443
444 static int eql_emancipate(struct net_device *master_dev, slaving_request_t __user *srqp)
445 {
446 equalizer_t *eql = netdev_priv(master_dev);
447 struct net_device *slave_dev;
448 slaving_request_t srq;
449 int ret;
450
451 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
452 return -EFAULT;
453
454 slave_dev = dev_get_by_name(&init_net, srq.slave_name);
455 ret = -EINVAL;
456 if (slave_dev) {
457 spin_lock_bh(&eql->queue.lock);
458
459 if (eql_is_slave(slave_dev)) {
460 slave_t *slave = __eql_find_slave_dev(&eql->queue,
461 slave_dev);
462
463 if (slave) {
464 eql_kill_one_slave(&eql->queue, slave);
465 ret = 0;
466 }
467 }
468 dev_put(slave_dev);
469
470 spin_unlock_bh(&eql->queue.lock);
471 }
472
473 return ret;
474 }
475
476 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
477 {
478 equalizer_t *eql = netdev_priv(dev);
479 slave_t *slave;
480 struct net_device *slave_dev;
481 slave_config_t sc;
482 int ret;
483
484 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
485 return -EFAULT;
486
487 slave_dev = dev_get_by_name(&init_net, sc.slave_name);
488 if (!slave_dev)
489 return -ENODEV;
490
491 ret = -EINVAL;
492
493 spin_lock_bh(&eql->queue.lock);
494 if (eql_is_slave(slave_dev)) {
495 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
496 if (slave) {
497 sc.priority = slave->priority;
498 ret = 0;
499 }
500 }
501 spin_unlock_bh(&eql->queue.lock);
502
503 dev_put(slave_dev);
504
505 if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t)))
506 ret = -EFAULT;
507
508 return ret;
509 }
510
511 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
512 {
513 slave_t *slave;
514 equalizer_t *eql;
515 struct net_device *slave_dev;
516 slave_config_t sc;
517 int ret;
518
519 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
520 return -EFAULT;
521
522 slave_dev = dev_get_by_name(&init_net, sc.slave_name);
523 if (!slave_dev)
524 return -ENODEV;
525
526 ret = -EINVAL;
527
528 eql = netdev_priv(dev);
529 spin_lock_bh(&eql->queue.lock);
530 if (eql_is_slave(slave_dev)) {
531 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
532 if (slave) {
533 slave->priority = sc.priority;
534 slave->priority_bps = sc.priority;
535 slave->priority_Bps = sc.priority / 8;
536 ret = 0;
537 }
538 }
539 spin_unlock_bh(&eql->queue.lock);
540
541 return ret;
542 }
543
544 static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mcp)
545 {
546 equalizer_t *eql;
547 master_config_t mc;
548
549 if (eql_is_master(dev)) {
550 eql = netdev_priv(dev);
551 mc.max_slaves = eql->max_slaves;
552 mc.min_slaves = eql->min_slaves;
553 if (copy_to_user(mcp, &mc, sizeof (master_config_t)))
554 return -EFAULT;
555 return 0;
556 }
557 return -EINVAL;
558 }
559
560 static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mcp)
561 {
562 equalizer_t *eql;
563 master_config_t mc;
564
565 if (copy_from_user(&mc, mcp, sizeof (master_config_t)))
566 return -EFAULT;
567
568 if (eql_is_master(dev)) {
569 eql = netdev_priv(dev);
570 eql->max_slaves = mc.max_slaves;
571 eql->min_slaves = mc.min_slaves;
572 return 0;
573 }
574 return -EINVAL;
575 }
576
577 static struct net_device *dev_eql;
578
579 static int __init eql_init_module(void)
580 {
581 int err;
582
583 printk(version);
584
585 dev_eql = alloc_netdev(sizeof(equalizer_t), "eql", eql_setup);
586 if (!dev_eql)
587 return -ENOMEM;
588
589 err = register_netdev(dev_eql);
590 if (err)
591 free_netdev(dev_eql);
592 return err;
593 }
594
595 static void __exit eql_cleanup_module(void)
596 {
597 unregister_netdev(dev_eql);
598 free_netdev(dev_eql);
599 }
600
601 module_init(eql_init_module);
602 module_exit(eql_cleanup_module);
603 MODULE_LICENSE("GPL");
This page took 0.052453 seconds and 5 git commands to generate.