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  *
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: eql.c,v $
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.2.1 $ $Date: 1996/09/22 13:52:00 $ 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 
eql_init(struct net_device * dev)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 
eql_open(struct net_device * dev)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 
eql_close(struct net_device * dev)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 
eql_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)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 
eql_number_slaves(slave_queue_t * queue)338 static inline int eql_number_slaves(slave_queue_t *queue)
339 {
340 	return queue->num_slaves;
341 }
342 
343 
eql_is_empty(slave_queue_t * queue)344 static inline int eql_is_empty(slave_queue_t *queue)
345 {
346 	if (eql_number_slaves (queue) == 0)
347 		return 1;
348 	return 0;
349 }
350 
eql_is_full(slave_queue_t * queue)351 static inline int eql_is_full(slave_queue_t *queue)
352 {
353 	equalizer_t *eql = (equalizer_t *) queue->master_dev->priv;
354 
355 	if (eql_number_slaves (queue) == eql->max_slaves)
356 		return 1;
357 	return 0;
358 }
359 
eql_first_slave(slave_queue_t * queue)360 static inline slave_t *eql_first_slave(slave_queue_t *queue)
361 {
362 	return queue->head->next;
363 }
364 
365 
eql_next_slave(slave_queue_t * queue,slave_t * slave)366 static inline slave_t *eql_next_slave(slave_queue_t *queue, slave_t *slave)
367 {
368 	return slave->next;
369 }
370 
eql_set_best_slave(slave_queue_t * queue,slave_t * slave)371 static inline void eql_set_best_slave(slave_queue_t *queue, slave_t *slave)
372 {
373 	queue->best_slave = slave;
374 }
375 
eql_schedule_slaves(slave_queue_t * queue)376 static inline void eql_schedule_slaves(slave_queue_t *queue)
377 {
378 	struct net_device *master_dev = queue->master_dev;
379 	slave_t *best_slave = 0;
380 	slave_t *slave_corpse = 0;
381 
382 #ifdef EQL_DEBUG
383 	if (eql_debug >= 100)
384 		printk ("%s: schedule %d slaves\n",
385 			master_dev->name, eql_number_slaves (queue));
386 #endif
387 	if ( eql_is_empty (queue) )
388 	{
389 		/*
390 		 *	No slaves to play with
391 		 */
392 		eql_set_best_slave (queue, (slave_t *) 0);
393 		return;
394 	}
395 	else
396 	{
397 		/*
398 		 *	Make a pass to set the best slave
399 		 */
400 		unsigned long best_load = (unsigned long) ULONG_MAX;
401 		slave_t *slave = 0;
402 		unsigned long flags;
403 		int i;
404 
405 		save_flags(flags);
406 		cli ();
407 		for (i = 1, slave = eql_first_slave (queue);
408 			i <= eql_number_slaves (queue);
409 			i++, slave = eql_next_slave (queue, slave))
410 		{
411 			/*
412 			 *	Go through the slave list once, updating best_slave
413 			 *      whenever a new best_load is found, whenever a dead
414 			 *	slave is found, it is marked to be pulled out of the
415 			 *	queue
416 			 */
417 
418 			unsigned long slave_load;
419 			unsigned long bytes_queued;
420 			unsigned long priority_Bps;
421 
422 	  		if (slave != 0)
423 			{
424 				bytes_queued = slave->bytes_queued;
425 				priority_Bps = slave->priority_Bps;
426 				if ( slave->dev != 0)
427 				{
428 					if ((slave->dev->flags & IFF_UP) == IFF_UP )
429 					{
430 						slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
431 							(priority_Bps) + bytes_queued * 8;
432 
433 		      				if (slave_load < best_load)
434 						{
435 							best_load = slave_load;
436 							best_slave = slave;
437 						}
438 					}
439 					else		/* we found a dead slave */
440 					{
441 						/*
442 						 *	We only bury one slave at a time, if more than
443 						 *	one slave dies, we will bury him on the next
444 						 *	reschedule. slaves don't die all at once that
445 						 *	much anyway
446 						 */
447 						slave_corpse = slave;
448 					}
449 				}
450 			}
451 		} /* for */
452 		restore_flags(flags);
453 		eql_set_best_slave (queue, best_slave);
454 	} /* else */
455 	if (slave_corpse != 0)
456 	{
457 		printk ("eql: scheduler found dead slave, burying...\n");
458 		eql_delete_slave (eql_remove_slave (queue, slave_corpse));
459 	}
460 	return;
461 }
462 
463 
eql_best_slave_dev(slave_queue_t * queue)464 static inline struct net_device *eql_best_slave_dev(slave_queue_t *queue)
465 {
466 	if (queue->best_slave != 0)
467 	{
468 		if (queue->best_slave->dev != 0)
469 			return queue->best_slave->dev;
470 		else
471 			return 0;
472 	}
473 	else
474 		return 0;
475 }
476 
477 
eql_best_slave(slave_queue_t * queue)478 static inline slave_t *eql_best_slave(slave_queue_t *queue)
479 {
480 	return queue->best_slave;
481 }
482 
483 
eql_slave_xmit(struct sk_buff * skb,struct net_device * dev)484 static int eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
485 {
486 	equalizer_t *eql = (equalizer_t *) dev->priv;
487 	struct net_device *slave_dev = 0;
488 	slave_t *slave;
489 
490 	if (skb == NULL)
491 		return 0;
492 
493 	eql_schedule_slaves (eql->queue);
494 
495 	slave = eql_best_slave (eql->queue);
496 	slave_dev = slave ? slave->dev : 0;
497 
498 	if ( slave_dev != 0 )
499 	{
500 #ifdef EQL_DEBUG
501 		if (eql_debug >= 100)
502 			printk ("%s: %d slaves xmitng %d B %s\n",
503 				dev->name, eql_number_slaves (eql->queue), skb->len,
504 				slave_dev->name);
505 #endif
506 		skb->dev = slave_dev;
507 		skb->priority = 1;
508 		slave->bytes_queued += skb->len;
509 		dev_queue_xmit (skb);
510 		eql->stats->tx_packets++;
511 	}
512 	else
513 	{
514 		/*
515 		 *	The alternative for this is the return 1 and have
516 		 *	dev_queue_xmit just queue it up on the eql's queue.
517 		 */
518 
519 		eql->stats->tx_dropped++;
520 		dev_kfree_skb(skb);
521 	}
522 	return 0;
523 }
524 
525 
eql_get_stats(struct net_device * dev)526 static struct net_device_stats * eql_get_stats(struct net_device *dev)
527 {
528 	equalizer_t *eql = (equalizer_t *) dev->priv;
529 	return eql->stats;
530 }
531 
eql_is_slave(struct net_device * dev)532 static inline int eql_is_slave(struct net_device *dev)
533 {
534 	if (dev)
535 	{
536 		if ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
537 			return 1;
538 	}
539 	return 0;
540 }
541 
542 
eql_is_master(struct net_device * dev)543 static inline int eql_is_master(struct net_device *dev)
544 {
545 	if (dev)
546 	{
547 		if ((dev->flags & IFF_MASTER) == IFF_MASTER)
548 		return 1;
549 	}
550 	return 0;
551 }
552 
553 
554 /*
555  *	Private ioctl functions
556  */
557 
eql_enslave(struct net_device * dev,slaving_request_t * srqp)558 static int eql_enslave(struct net_device *dev, slaving_request_t *srqp)
559 {
560 	struct net_device *master_dev;
561 	struct net_device *slave_dev;
562 	slaving_request_t srq;
563 
564 	if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
565 	  {
566 #ifdef EQL_DEBUG
567 	if (eql_debug >= 20)
568 		printk ("EQL enslave: error detected by copy_from_user\n");
569 #endif
570 		return -EFAULT;
571 	  }
572 
573 #ifdef EQL_DEBUG
574 	if (eql_debug >= 20)
575 		printk ("%s: enslave '%s' %ld bps\n", dev->name,
576 			srq.slave_name, srq.priority);
577 #endif
578 	master_dev = dev;		/* for "clarity" */
579 	slave_dev  = __dev_get_by_name (srq.slave_name);
580 	if (!slave_dev)
581 		return -ENODEV;
582 
583 	if (master_dev != 0 && slave_dev != 0)
584 	{
585 		if ((master_dev->flags & IFF_UP) == IFF_UP)
586                 {
587 			/*slave is not a master & not already a slave:*/
588 			if (! eql_is_master (slave_dev)  &&
589 			    ! eql_is_slave (slave_dev) )
590 			{
591 				slave_t *s = eql_new_slave ();
592 				equalizer_t *eql =
593 					(equalizer_t *) master_dev->priv;
594 				if (!s)
595 					return -ENOMEM;
596 				s->dev = slave_dev;
597 				s->priority = srq.priority;
598 				s->priority_bps = srq.priority;
599 				s->priority_Bps = srq.priority / 8;
600 				slave_dev->flags |= IFF_SLAVE;
601 				eql_insert_slave (eql->queue, s);
602 				return 0;
603 			}
604 #ifdef EQL_DEBUG
605 			else if (eql_debug >= 20)
606 				printk ("EQL enslave: slave is master or slave is already slave\n");
607 #endif
608 		}
609 #ifdef EQL_DEBUG
610 		else if (eql_debug >= 20)
611 			printk ("EQL enslave: master device not up!\n");
612 #endif
613 	}
614 #ifdef EQL_DEBUG
615 	else if (eql_debug >= 20)
616 		printk ("EQL enslave: master or slave are NULL");
617 #endif
618 	return -EINVAL;
619 }
620 
eql_emancipate(struct net_device * dev,slaving_request_t * srqp)621 static int eql_emancipate(struct net_device *dev, slaving_request_t *srqp)
622 {
623 	struct net_device *master_dev;
624 	struct net_device *slave_dev;
625 	slaving_request_t srq;
626 
627 	if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
628 		return -EFAULT;
629 
630 #ifdef EQL_DEBUG
631 	if (eql_debug >= 20)
632 		printk ("%s: emancipate `%s`\n", dev->name, srq.slave_name);
633 #endif
634 	master_dev = dev;		/* for "clarity" */
635 	slave_dev  = __dev_get_by_name (srq.slave_name);
636 	if (!slave_dev)
637 		return -ENODEV;
638 
639 	if ( eql_is_slave (slave_dev) )	/* really is a slave */
640 	{
641 		equalizer_t *eql = (equalizer_t *) master_dev->priv;
642 		slave_dev->flags = slave_dev->flags & ~IFF_SLAVE;
643 		eql_remove_slave_dev (eql->queue, slave_dev);
644 		return 0;
645 	}
646 	return -EINVAL;
647 }
648 
649 
eql_g_slave_cfg(struct net_device * dev,slave_config_t * scp)650 static int eql_g_slave_cfg(struct net_device *dev, slave_config_t *scp)
651 {
652 	slave_t *slave;
653 	equalizer_t *eql;
654 	struct net_device *slave_dev;
655 	slave_config_t sc;
656 
657 	if (copy_from_user (&sc, scp, sizeof (slave_config_t)))
658 		return -EFAULT;
659 
660 #ifdef EQL_DEBUG
661 	if (eql_debug >= 20)
662 		printk ("%s: get config for slave `%s'\n", dev->name, sc.slave_name);
663 #endif
664 	eql = (equalizer_t *) dev->priv;
665 	slave_dev = __dev_get_by_name (sc.slave_name);
666 	if (!slave_dev)
667 		return -ENODEV;
668 
669 	if ( eql_is_slave (slave_dev) )
670 	{
671 		slave = eql_find_slave_dev (eql->queue,  slave_dev);
672 		if (slave != 0)
673 		{
674 			sc.priority = slave->priority;
675 			if (copy_to_user (scp, &sc, sizeof (slave_config_t)))
676 				return -EFAULT;
677 			return 0;
678 		}
679 	}
680 	return -EINVAL;
681 }
682 
683 
eql_s_slave_cfg(struct net_device * dev,slave_config_t * scp)684 static int eql_s_slave_cfg(struct net_device *dev, slave_config_t *scp)
685 {
686 	slave_t *slave;
687 	equalizer_t *eql;
688 	struct net_device *slave_dev;
689 	slave_config_t sc;
690 
691 	if (copy_from_user (&sc, scp, sizeof (slave_config_t)))
692 		return -EFAULT;
693 
694 #ifdef EQL_DEBUG
695 	if (eql_debug >= 20)
696 		printk ("%s: set config for slave `%s'\n", dev->name, sc.slave_name);
697 #endif
698 
699 
700 	eql = (equalizer_t *) dev->priv;
701 	slave_dev = __dev_get_by_name (sc.slave_name);
702 	if (!slave_dev)
703 		return -ENODEV;
704 
705 	if ( eql_is_slave (slave_dev) )
706 	{
707 		slave = eql_find_slave_dev (eql->queue, slave_dev);
708 		if (slave != 0)
709 		{
710 			slave->priority = sc.priority;
711 			slave->priority_bps = sc.priority;
712 			slave->priority_Bps = sc.priority / 8;
713 			return 0;
714 		}
715 	}
716 	return -EINVAL;
717 }
718 
719 
eql_g_master_cfg(struct net_device * dev,master_config_t * mcp)720 static int eql_g_master_cfg(struct net_device *dev, master_config_t *mcp)
721 {
722 	equalizer_t *eql;
723 	master_config_t mc;
724 
725 #if EQL_DEBUG
726 	if (eql_debug >= 20)
727 		printk ("%s: get master config\n", dev->name);
728 #endif
729 
730 	if ( eql_is_master (dev) )
731 	{
732 		eql = (equalizer_t *) dev->priv;
733 		mc.max_slaves = eql->max_slaves;
734 		mc.min_slaves = eql->min_slaves;
735 		if (copy_to_user (mcp, &mc, sizeof (master_config_t)))
736 			return -EFAULT;
737 		return 0;
738 	}
739 	return -EINVAL;
740 }
741 
742 
eql_s_master_cfg(struct net_device * dev,master_config_t * mcp)743 static int eql_s_master_cfg(struct net_device *dev, master_config_t *mcp)
744 {
745 	equalizer_t *eql;
746 	master_config_t mc;
747 
748 	if (copy_from_user (&mc, mcp, sizeof (master_config_t)))
749 		return -EFAULT;
750 #if EQL_DEBUG
751 	if (eql_debug >= 20)
752 		printk ("%s: set master config\n", dev->name);
753 #endif
754 	if ( eql_is_master (dev) )
755 	{
756 		eql = (equalizer_t *) dev->priv;
757 		eql->max_slaves = mc.max_slaves;
758 		eql->min_slaves = mc.min_slaves;
759 		return 0;
760 	}
761 	return -EINVAL;
762 }
763 
764 /*
765  *	Private device support functions
766  */
767 
eql_new_slave(void)768 static slave_t *eql_new_slave(void)
769 {
770 	slave_t *slave;
771 
772 	slave = (slave_t *) kmalloc (sizeof (slave_t), GFP_KERNEL);
773 	if (slave)
774 		memset(slave, 0, sizeof (slave_t));
775 	return slave;
776 }
777 
778 
eql_delete_slave(slave_t * slave)779 static void eql_delete_slave(slave_t *slave)
780 {
781 	kfree (slave);
782 }
783 
784 
785 #if 0				/* not currently used, will be used
786 				   when we really use a priority queue */
787 static long slave_Bps(slave_t *slave)
788 {
789 	return (slave->priority_Bps);
790 }
791 
792 static long slave_bps(slave_t *slave)
793 {
794 	return (slave->priority_bps);
795 }
796 
797 #endif
798 
eql_new_slave_queue(struct net_device * dev)799 static slave_queue_t *eql_new_slave_queue(struct net_device *dev)
800 {
801 	slave_queue_t *queue;
802 	slave_t *head_slave;
803 	slave_t *tail_slave;
804 
805 	queue = (slave_queue_t *) kmalloc (sizeof (slave_queue_t), GFP_KERNEL);
806 	if (!queue)
807 		goto err_out;
808 
809 	head_slave = eql_new_slave ();
810 	if (!head_slave)
811 		goto err_out_queue;
812 
813 	tail_slave = eql_new_slave ();
814 	if (!tail_slave)
815 		goto err_out_hs;
816 
817 	memset (queue, 0, sizeof (slave_queue_t));
818 
819 	head_slave->next = tail_slave;
820 	tail_slave->next = 0;
821 	queue->head = head_slave;
822 	queue->num_slaves = 0;
823 	queue->master_dev = dev;
824 	return queue;
825 
826 err_out_hs:
827 	kfree (head_slave);
828 err_out_queue:
829 	kfree (queue);
830 err_out:
831 	return NULL;
832 }
833 
834 
eql_delete_slave_queue(slave_queue_t * queue)835 static void eql_delete_slave_queue(slave_queue_t *queue)
836 {
837 	slave_t *zapped;
838 	/*
839 	 *	This should only be called when there isn't a
840 	 *	timer running that scans the data periodically..
841 	 *	dev_close stops the timer...
842 	 */
843 
844 	while ( ! eql_is_empty (queue) )
845 	{
846 		zapped = eql_remove_slave (queue, queue->head->next);
847 		eql_delete_slave (zapped);
848 	}
849 	kfree (queue->head->next);
850 	kfree (queue->head);
851 	kfree (queue);
852 }
853 
eql_insert_slave(slave_queue_t * queue,slave_t * slave)854 static int eql_insert_slave(slave_queue_t *queue, slave_t *slave)
855 {
856 	unsigned long flags;
857 
858 	save_flags(flags);
859 	cli ();
860 
861 	if ( ! eql_is_full (queue) )
862 	{
863 		slave_t *duplicate_slave = 0;
864 		duplicate_slave = eql_find_slave_dev (queue, slave->dev);
865 		if (duplicate_slave != 0)
866 		{
867 /*			  printk ("%s: found a duplicate, killing it and replacing\n",
868 				  queue->master_dev->name); */
869 			eql_delete_slave (eql_remove_slave (queue, duplicate_slave));
870 		}
871 		slave->next = queue->head->next;
872 		queue->head->next = slave;
873 		queue->num_slaves++;
874 		restore_flags(flags);
875 		return 0;
876 	}
877 	restore_flags(flags);
878 	return 1;
879 }
880 
881 
eql_remove_slave(slave_queue_t * queue,slave_t * slave)882 static slave_t *eql_remove_slave(slave_queue_t *queue, slave_t *slave)
883 {
884 	slave_t *prev;
885 	slave_t *curr;
886 	unsigned long flags;
887 
888 	save_flags(flags);
889 	cli ();
890 
891 	prev = queue->head;
892 	curr = queue->head->next;
893 	while (curr != slave &&
894 		curr->dev != 0 )
895 	{
896 /* 		printk ("%s: remove_slave; searching...\n", queue->master_dev->name); */
897 		prev = curr;
898 		curr = curr->next;
899 	}
900 
901 	if (curr == slave)
902 	{
903 		prev->next = curr->next;
904 		queue->num_slaves--;
905 		curr->dev->flags = curr->dev->flags & ~IFF_SLAVE;
906 		restore_flags(flags);
907 		return curr;
908 	}
909 	restore_flags(flags);
910 	return 0;			/* not found */
911 }
912 
913 
eql_remove_slave_dev(slave_queue_t * queue,struct net_device * dev)914 static int eql_remove_slave_dev(slave_queue_t *queue, struct net_device *dev)
915 {
916 	slave_t *prev;
917 	slave_t *curr;
918 	slave_t *target;
919 
920 	target = eql_find_slave_dev (queue, dev);
921 
922 	if (target != 0)
923 	{
924 		unsigned long flags;
925 
926 		save_flags(flags);
927 		cli ();
928 		prev = queue->head;
929 		curr = prev->next;
930 		while (curr != target)
931 		{
932 			prev = curr;
933 			curr = curr->next;
934 		}
935 		prev->next = curr->next;
936 		queue->num_slaves--;
937 		restore_flags(flags);
938 		eql_delete_slave (curr);
939 		return 0;
940 	}
941 	return 1;
942 }
943 
944 
eql_find_slave_dev(slave_queue_t * queue,struct net_device * dev)945 static slave_t * eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
946 {
947 	slave_t *slave = 0;
948 	slave = eql_first_slave(queue);
949 
950 	while (slave != 0 && slave->dev != dev && slave != 0)
951 	{
952 #if 0
953 		if (slave->dev != 0)
954 			printk ("eql: find_slave_dev; looked at '%s'...\n", slave->dev->name);
955 		else
956 			printk ("eql: find_slave_dev; looked at nothing...\n");
957 #endif
958 		slave = slave->next;
959 	}
960 	return slave;
961 }
962 
963 
eql_timer(unsigned long param)964 static void eql_timer(unsigned long param)
965 {
966 	equalizer_t *eql = (equalizer_t *) param;
967 	slave_t *slave;
968 	slave_t *slave_corpse = 0;
969 	int i;
970 	unsigned long flags;
971 
972 	if ( ! eql_is_empty (eql->queue) )
973 	{
974 		save_flags(flags);
975 		cli ();
976 		for (i = 1, slave = eql_first_slave (eql->queue);
977 			i <= eql_number_slaves (eql->queue);
978 			i++, slave = eql_next_slave (eql->queue, slave))
979 		{
980 			if (slave != 0)
981 			{
982 				if ((slave->dev->flags & IFF_UP) == IFF_UP )
983 				{
984 					slave->bytes_queued -= slave->priority_Bps;
985 					if (slave->bytes_queued < 0)
986 						slave->bytes_queued = 0;
987 				}
988 				else
989 					slave_corpse = slave;
990 			}
991 		}
992 		restore_flags(flags);
993 		if (slave_corpse != 0)
994 		{
995 			printk ("eql: timer found dead slave, burying...\n");
996 			eql_delete_slave (eql_remove_slave (eql->queue, slave_corpse));
997 		}
998 	}
999 
1000 	if (eql->timer_on != 0)
1001 	{
1002 		eql->timer.expires = jiffies+EQL_DEFAULT_RESCHED_IVAL;
1003 		add_timer (&eql->timer);
1004 	}
1005 }
1006 
1007 static struct net_device dev_eql;
1008 
eql_init_module(void)1009 static int __init eql_init_module(void)
1010 {
1011 	strcpy(dev_eql.name, "eql");
1012 	dev_eql.init = eql_init;
1013 	if (register_netdev(&dev_eql) != 0) {
1014 		printk("eql: register_netdev() returned non-zero.\n");
1015 		return -EIO;
1016 	}
1017 	return 0;
1018 }
1019 
eql_cleanup_module(void)1020 static void __exit eql_cleanup_module(void)
1021 {
1022 	kfree(((equalizer_t *)dev_eql.priv)->stats );
1023 	kfree(dev_eql.priv);
1024 	unregister_netdev(&dev_eql);
1025 }
1026 
1027 module_init(eql_init_module);
1028 module_exit(eql_cleanup_module);
1029 MODULE_LICENSE("GPL");
1030 
1031 /*
1032  * Local Variables:
1033  * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c eql.c"
1034  * version-control: t
1035  * kept-new-versions: 20
1036  * End:
1037  */
1038