xref: /linux/drivers/net/eql.c (revision 10d024c1)
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 static struct net_device_stats *eql_get_stats(struct net_device *dev);
132 
133 #define eql_is_slave(dev)	((dev->flags & IFF_SLAVE) == IFF_SLAVE)
134 #define eql_is_master(dev)	((dev->flags & IFF_MASTER) == IFF_MASTER)
135 
136 static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave);
137 
138 static void eql_timer(unsigned long param)
139 {
140 	equalizer_t *eql = (equalizer_t *) param;
141 	struct list_head *this, *tmp, *head;
142 
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);
147 
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);
154 		}
155 
156 	}
157 	spin_unlock_bh(&eql->queue.lock);
158 
159 	eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
160 	add_timer(&eql->timer);
161 }
162 
163 static char version[] __initdata =
164 	"Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)\n";
165 
166 static void __init eql_setup(struct net_device *dev)
167 {
168 	equalizer_t *eql = netdev_priv(dev);
169 
170 	init_timer(&eql->timer);
171 	eql->timer.data     	= (unsigned long) eql;
172 	eql->timer.expires  	= jiffies + EQL_DEFAULT_RESCHED_IVAL;
173 	eql->timer.function 	= eql_timer;
174 
175 	spin_lock_init(&eql->queue.lock);
176 	INIT_LIST_HEAD(&eql->queue.all_slaves);
177 	eql->queue.master_dev	= dev;
178 
179 	dev->open		= eql_open;
180 	dev->stop		= eql_close;
181 	dev->do_ioctl		= eql_ioctl;
182 	dev->hard_start_xmit	= eql_slave_xmit;
183 	dev->get_stats		= eql_get_stats;
184 
185 	/*
186 	 *	Now we undo some of the things that eth_setup does
187 	 * 	that we don't like
188 	 */
189 
190 	dev->mtu        	= EQL_DEFAULT_MTU;	/* set to 576 in if_eql.h */
191 	dev->flags      	= IFF_MASTER;
192 
193 	dev->type       	= ARPHRD_SLIP;
194 	dev->tx_queue_len 	= 5;		/* Hands them off fast */
195 }
196 
197 static int eql_open(struct net_device *dev)
198 {
199 	equalizer_t *eql = netdev_priv(dev);
200 
201 	/* XXX We should force this off automatically for the user. */
202 	printk(KERN_INFO "%s: remember to turn off Van-Jacobson compression on "
203 	       "your slave devices.\n", dev->name);
204 
205 	BUG_ON(!list_empty(&eql->queue.all_slaves));
206 
207 	eql->min_slaves = 1;
208 	eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
209 
210 	add_timer(&eql->timer);
211 
212 	return 0;
213 }
214 
215 static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave)
216 {
217 	list_del(&slave->list);
218 	queue->num_slaves--;
219 	slave->dev->flags &= ~IFF_SLAVE;
220 	dev_put(slave->dev);
221 	kfree(slave);
222 }
223 
224 static void eql_kill_slave_queue(slave_queue_t *queue)
225 {
226 	struct list_head *head, *tmp, *this;
227 
228 	spin_lock_bh(&queue->lock);
229 
230 	head = &queue->all_slaves;
231 	list_for_each_safe(this, tmp, head) {
232 		slave_t *s = list_entry(this, slave_t, list);
233 
234 		eql_kill_one_slave(queue, s);
235 	}
236 
237 	spin_unlock_bh(&queue->lock);
238 }
239 
240 static int eql_close(struct net_device *dev)
241 {
242 	equalizer_t *eql = netdev_priv(dev);
243 
244 	/*
245 	 *	The timer has to be stopped first before we start hacking away
246 	 *	at the data structure it scans every so often...
247 	 */
248 
249 	del_timer_sync(&eql->timer);
250 
251 	eql_kill_slave_queue(&eql->queue);
252 
253 	return 0;
254 }
255 
256 static int eql_enslave(struct net_device *dev,  slaving_request_t __user *srq);
257 static int eql_emancipate(struct net_device *dev, slaving_request_t __user *srq);
258 
259 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
260 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
261 
262 static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mc);
263 static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mc);
264 
265 static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
266 {
267 	if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG &&
268 	    !capable(CAP_NET_ADMIN))
269 	  	return -EPERM;
270 
271 	switch (cmd) {
272 		case EQL_ENSLAVE:
273 			return eql_enslave(dev, ifr->ifr_data);
274 		case EQL_EMANCIPATE:
275 			return eql_emancipate(dev, ifr->ifr_data);
276 		case EQL_GETSLAVECFG:
277 			return eql_g_slave_cfg(dev, ifr->ifr_data);
278 		case EQL_SETSLAVECFG:
279 			return eql_s_slave_cfg(dev, ifr->ifr_data);
280 		case EQL_GETMASTRCFG:
281 			return eql_g_master_cfg(dev, ifr->ifr_data);
282 		case EQL_SETMASTRCFG:
283 			return eql_s_master_cfg(dev, ifr->ifr_data);
284 		default:
285 			return -EOPNOTSUPP;
286 	};
287 }
288 
289 /* queue->lock must be held */
290 static slave_t *__eql_schedule_slaves(slave_queue_t *queue)
291 {
292 	unsigned long best_load = ~0UL;
293 	struct list_head *this, *tmp, *head;
294 	slave_t *best_slave;
295 
296 	best_slave = NULL;
297 
298 	/* Make a pass to set the best slave. */
299 	head = &queue->all_slaves;
300 	list_for_each_safe(this, tmp, head) {
301 		slave_t *slave = list_entry(this, slave_t, list);
302 		unsigned long slave_load, bytes_queued, priority_Bps;
303 
304 		/* Go through the slave list once, updating best_slave
305 		 * whenever a new best_load is found.
306 		 */
307 		bytes_queued = slave->bytes_queued;
308 		priority_Bps = slave->priority_Bps;
309 		if ((slave->dev->flags & IFF_UP) == IFF_UP) {
310 			slave_load = (~0UL - (~0UL / 2)) -
311 				(priority_Bps) + bytes_queued * 8;
312 
313 			if (slave_load < best_load) {
314 				best_load = slave_load;
315 				best_slave = slave;
316 			}
317 		} else {
318 			/* We found a dead slave, kill it. */
319 			eql_kill_one_slave(queue, slave);
320 		}
321 	}
322 	return best_slave;
323 }
324 
325 static int eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
326 {
327 	equalizer_t *eql = netdev_priv(dev);
328 	slave_t *slave;
329 
330 	spin_lock(&eql->queue.lock);
331 
332 	slave = __eql_schedule_slaves(&eql->queue);
333 	if (slave) {
334 		struct net_device *slave_dev = slave->dev;
335 
336 		skb->dev = slave_dev;
337 		skb->priority = 1;
338 		slave->bytes_queued += skb->len;
339 		dev_queue_xmit(skb);
340 		eql->stats.tx_packets++;
341 	} else {
342 		eql->stats.tx_dropped++;
343 		dev_kfree_skb(skb);
344 	}
345 
346 	spin_unlock(&eql->queue.lock);
347 
348 	return 0;
349 }
350 
351 static struct net_device_stats * eql_get_stats(struct net_device *dev)
352 {
353 	equalizer_t *eql = netdev_priv(dev);
354 	return &eql->stats;
355 }
356 
357 /*
358  *	Private ioctl functions
359  */
360 
361 /* queue->lock must be held */
362 static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
363 {
364 	struct list_head *this, *head;
365 
366 	head = &queue->all_slaves;
367 	list_for_each(this, head) {
368 		slave_t *slave = list_entry(this, slave_t, list);
369 
370 		if (slave->dev == dev)
371 			return slave;
372 	}
373 
374 	return NULL;
375 }
376 
377 static inline int eql_is_full(slave_queue_t *queue)
378 {
379 	equalizer_t *eql = netdev_priv(queue->master_dev);
380 
381 	if (queue->num_slaves >= eql->max_slaves)
382 		return 1;
383 	return 0;
384 }
385 
386 /* queue->lock must be held */
387 static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave)
388 {
389 	if (!eql_is_full(queue)) {
390 		slave_t *duplicate_slave = NULL;
391 
392 		duplicate_slave = __eql_find_slave_dev(queue, slave->dev);
393 		if (duplicate_slave)
394 			eql_kill_one_slave(queue, duplicate_slave);
395 
396 		list_add(&slave->list, &queue->all_slaves);
397 		queue->num_slaves++;
398 		slave->dev->flags |= IFF_SLAVE;
399 
400 		return 0;
401 	}
402 
403 	return -ENOSPC;
404 }
405 
406 static int eql_enslave(struct net_device *master_dev, slaving_request_t __user *srqp)
407 {
408 	struct net_device *slave_dev;
409 	slaving_request_t srq;
410 
411 	if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
412 		return -EFAULT;
413 
414 	slave_dev  = dev_get_by_name(&init_net, srq.slave_name);
415 	if (slave_dev) {
416 		if ((master_dev->flags & IFF_UP) == IFF_UP) {
417 			/* slave is not a master & not already a slave: */
418 			if (!eql_is_master(slave_dev) &&
419 			    !eql_is_slave(slave_dev)) {
420 				slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL);
421 				equalizer_t *eql = netdev_priv(master_dev);
422 				int ret;
423 
424 				if (!s) {
425 					dev_put(slave_dev);
426 					return -ENOMEM;
427 				}
428 
429 				memset(s, 0, sizeof(*s));
430 				s->dev = slave_dev;
431 				s->priority = srq.priority;
432 				s->priority_bps = srq.priority;
433 				s->priority_Bps = srq.priority / 8;
434 
435 				spin_lock_bh(&eql->queue.lock);
436 				ret = __eql_insert_slave(&eql->queue, s);
437 				if (ret) {
438 					dev_put(slave_dev);
439 					kfree(s);
440 				}
441 				spin_unlock_bh(&eql->queue.lock);
442 
443 				return ret;
444 			}
445 		}
446 		dev_put(slave_dev);
447 	}
448 
449 	return -EINVAL;
450 }
451 
452 static int eql_emancipate(struct net_device *master_dev, slaving_request_t __user *srqp)
453 {
454 	equalizer_t *eql = netdev_priv(master_dev);
455 	struct net_device *slave_dev;
456 	slaving_request_t srq;
457 	int ret;
458 
459 	if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
460 		return -EFAULT;
461 
462 	slave_dev = dev_get_by_name(&init_net, srq.slave_name);
463 	ret = -EINVAL;
464 	if (slave_dev) {
465 		spin_lock_bh(&eql->queue.lock);
466 
467 		if (eql_is_slave(slave_dev)) {
468 			slave_t *slave = __eql_find_slave_dev(&eql->queue,
469 							      slave_dev);
470 
471 			if (slave) {
472 				eql_kill_one_slave(&eql->queue, slave);
473 				ret = 0;
474 			}
475 		}
476 		dev_put(slave_dev);
477 
478 		spin_unlock_bh(&eql->queue.lock);
479 	}
480 
481 	return ret;
482 }
483 
484 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
485 {
486 	equalizer_t *eql = netdev_priv(dev);
487 	slave_t *slave;
488 	struct net_device *slave_dev;
489 	slave_config_t sc;
490 	int ret;
491 
492 	if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
493 		return -EFAULT;
494 
495 	slave_dev = dev_get_by_name(&init_net, sc.slave_name);
496 	if (!slave_dev)
497 		return -ENODEV;
498 
499 	ret = -EINVAL;
500 
501 	spin_lock_bh(&eql->queue.lock);
502 	if (eql_is_slave(slave_dev)) {
503 		slave = __eql_find_slave_dev(&eql->queue, slave_dev);
504 		if (slave) {
505 			sc.priority = slave->priority;
506 			ret = 0;
507 		}
508 	}
509 	spin_unlock_bh(&eql->queue.lock);
510 
511 	dev_put(slave_dev);
512 
513 	if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t)))
514 		ret = -EFAULT;
515 
516 	return ret;
517 }
518 
519 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
520 {
521 	slave_t *slave;
522 	equalizer_t *eql;
523 	struct net_device *slave_dev;
524 	slave_config_t sc;
525 	int ret;
526 
527 	if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
528 		return -EFAULT;
529 
530 	slave_dev = dev_get_by_name(&init_net, sc.slave_name);
531 	if (!slave_dev)
532 		return -ENODEV;
533 
534 	ret = -EINVAL;
535 
536 	eql = netdev_priv(dev);
537 	spin_lock_bh(&eql->queue.lock);
538 	if (eql_is_slave(slave_dev)) {
539 		slave = __eql_find_slave_dev(&eql->queue, slave_dev);
540 		if (slave) {
541 			slave->priority = sc.priority;
542 			slave->priority_bps = sc.priority;
543 			slave->priority_Bps = sc.priority / 8;
544 			ret = 0;
545 		}
546 	}
547 	spin_unlock_bh(&eql->queue.lock);
548 
549 	return ret;
550 }
551 
552 static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mcp)
553 {
554 	equalizer_t *eql;
555 	master_config_t mc;
556 
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;
564 	}
565 	return -EINVAL;
566 }
567 
568 static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mcp)
569 {
570 	equalizer_t *eql;
571 	master_config_t mc;
572 
573 	if (copy_from_user(&mc, mcp, sizeof (master_config_t)))
574 		return -EFAULT;
575 
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;
581 	}
582 	return -EINVAL;
583 }
584 
585 static struct net_device *dev_eql;
586 
587 static int __init eql_init_module(void)
588 {
589 	int err;
590 
591 	printk(version);
592 
593 	dev_eql = alloc_netdev(sizeof(equalizer_t), "eql", eql_setup);
594 	if (!dev_eql)
595 		return -ENOMEM;
596 
597 	err = register_netdev(dev_eql);
598 	if (err)
599 		free_netdev(dev_eql);
600 	return err;
601 }
602 
603 static void __exit eql_cleanup_module(void)
604 {
605 	unregister_netdev(dev_eql);
606 	free_netdev(dev_eql);
607 }
608 
609 module_init(eql_init_module);
610 module_exit(eql_cleanup_module);
611 MODULE_LICENSE("GPL");
612