eql.c 14.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * Equalizer Load-balancer for serial network interfaces.
 *
 * (c) Copyright 1995 Simon "Guru Aleph-Null" Janes
 * NCM: Network and Communications Management, Inc.
 *
 * (c) Copyright 2002 David S. Miller (davem@redhat.com)
 *
 *	This software may be used and distributed according to the terms
 *	of the GNU General Public License, incorporated herein by reference.
11
 *
L
Linus Torvalds 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * The author may be reached as simon@ncm.com, or C/O
 *    NCM
 *    Attn: Simon Janes
 *    6803 Whittier Ave
 *    McLean VA 22101
 *    Phone: 1-703-847-0040 ext 103
 */

/*
 * Sources:
 *   skeleton.c by Donald Becker.
 * Inspirations:
 *   The Harried and Overworked Alan Cox
 * Conspiracies:
26
 *   The Alan Cox and Mike McLagan plot to get someone else to do the code,
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
 *   which turned out to be me.
 */

/*
 * $Log: eql.c,v $
 * Revision 1.2  1996/04/11 17:51:52  guru
 * Added one-line eql_remove_slave patch.
 *
 * Revision 1.1  1996/04/11 17:44:17  guru
 * Initial revision
 *
 * Revision 3.13  1996/01/21  15:17:18  alan
 * tx_queue_len changes.
 * reformatted.
 *
 * Revision 3.12  1995/03/22  21:07:51  anarchy
 * Added capable() checks on configuration.
 * Moved header file.
 *
 * Revision 3.11  1995/01/19  23:14:31  guru
 * 		      slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
 * 			(priority_Bps) + bytes_queued * 8;
 *
 * Revision 3.10  1995/01/19  23:07:53  guru
 * back to
 * 		      slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
 * 			(priority_Bps) + bytes_queued;
 *
 * Revision 3.9  1995/01/19  22:38:20  guru
 * 		      slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
 * 			(priority_Bps) + bytes_queued * 4;
 *
 * Revision 3.8  1995/01/19  22:30:55  guru
 *       slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
 * 			(priority_Bps) + bytes_queued * 2;
 *
 * Revision 3.7  1995/01/19  21:52:35  guru
 * printk's trimmed out.
 *
 * Revision 3.6  1995/01/19  21:49:56  guru
 * This is working pretty well. I gained 1 K/s in speed.. now it's just
 * robustness and printk's to be diked out.
 *
 * Revision 3.5  1995/01/18  22:29:59  guru
 * still crashes the kernel when the lock_wait thing is woken up.
 *
 * Revision 3.4  1995/01/18  21:59:47  guru
 * Broken set-bit locking snapshot
 *
 * Revision 3.3  1995/01/17  22:09:18  guru
 * infinite sleep in a lock somewhere..
 *
 * Revision 3.2  1995/01/15  16:46:06  guru
 * Log trimmed of non-pertinent 1.x branch messages
 *
 * Revision 3.1  1995/01/15  14:41:45  guru
 * New Scheduler and timer stuff...
 *
 * Revision 1.15  1995/01/15  14:29:02  guru
 * Will make 1.14 (now 1.15) the 3.0 branch, and the 1.12 the 2.0 branch, the one
 * with the dumber scheduler
 *
 * Revision 1.14  1995/01/15  02:37:08  guru
 * shock.. the kept-new-versions could have zonked working
 * stuff.. shudder
 *
 * Revision 1.13  1995/01/15  02:36:31  guru
 * big changes
 *
 * 	scheduler was torn out and replaced with something smarter
 *
 * 	global names not prefixed with eql_ were renamed to protect
 * 	against namespace collisions
 *
 * 	a few more abstract interfaces were added to facilitate any
 * 	potential change of datastructure.  the driver is still using
 * 	a linked list of slaves.  going to a heap would be a bit of
 * 	an overkill.
 *
 * 	this compiles fine with no warnings.
 *
 * 	the locking mechanism and timer stuff must be written however,
 * 	this version will not work otherwise
 *
 * Sorry, I had to rewrite most of this for 2.5.x -DaveM
 */

114 115
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

116
#include <linux/capability.h>
L
Linus Torvalds 已提交
117 118 119
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
120
#include <linux/slab.h>
L
Linus Torvalds 已提交
121 122
#include <linux/timer.h>
#include <linux/netdevice.h>
123
#include <net/net_namespace.h>
L
Linus Torvalds 已提交
124 125 126 127

#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/if_eql.h>
E
Eric Dumazet 已提交
128
#include <linux/pkt_sched.h>
L
Linus Torvalds 已提交
129 130 131 132 133 134

#include <asm/uaccess.h>

static int eql_open(struct net_device *dev);
static int eql_close(struct net_device *dev);
static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
135
static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev);
L
Linus Torvalds 已提交
136 137 138 139

#define eql_is_slave(dev)	((dev->flags & IFF_SLAVE) == IFF_SLAVE)
#define eql_is_master(dev)	((dev->flags & IFF_MASTER) == IFF_MASTER)

140
static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave);
L
Linus Torvalds 已提交
141 142 143 144 145

static void eql_timer(unsigned long param)
{
	equalizer_t *eql = (equalizer_t *) param;
	struct list_head *this, *tmp, *head;
146

E
Eric Dumazet 已提交
147
	spin_lock(&eql->queue.lock);
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156
	head = &eql->queue.all_slaves;
	list_for_each_safe(this, tmp, head) {
		slave_t *slave = list_entry(this, slave_t, list);

		if ((slave->dev->flags & IFF_UP) == IFF_UP) {
			slave->bytes_queued -= slave->priority_Bps;
			if (slave->bytes_queued < 0)
				slave->bytes_queued = 0;
		} else {
157
			eql_kill_one_slave(&eql->queue, slave);
L
Linus Torvalds 已提交
158 159 160
		}

	}
E
Eric Dumazet 已提交
161
	spin_unlock(&eql->queue.lock);
L
Linus Torvalds 已提交
162 163 164 165 166

	eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
	add_timer(&eql->timer);
}

167
static const char version[] __initconst =
168
	"Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)";
L
Linus Torvalds 已提交
169

S
Stephen Hemminger 已提交
170 171 172 173 174 175 176
static const struct net_device_ops eql_netdev_ops = {
	.ndo_open	= eql_open,
	.ndo_stop	= eql_close,
	.ndo_do_ioctl	= eql_ioctl,
	.ndo_start_xmit	= eql_slave_xmit,
};

L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189
static void __init eql_setup(struct net_device *dev)
{
	equalizer_t *eql = netdev_priv(dev);

	init_timer(&eql->timer);
	eql->timer.data     	= (unsigned long) eql;
	eql->timer.expires  	= jiffies + EQL_DEFAULT_RESCHED_IVAL;
	eql->timer.function 	= eql_timer;

	spin_lock_init(&eql->queue.lock);
	INIT_LIST_HEAD(&eql->queue.all_slaves);
	eql->queue.master_dev	= dev;

S
Stephen Hemminger 已提交
190
	dev->netdev_ops		= &eql_netdev_ops;
191

L
Linus Torvalds 已提交
192 193
	/*
	 *	Now we undo some of the things that eth_setup does
194
	 * 	that we don't like
L
Linus Torvalds 已提交
195
	 */
196

L
Linus Torvalds 已提交
197 198 199 200 201
	dev->mtu        	= EQL_DEFAULT_MTU;	/* set to 576 in if_eql.h */
	dev->flags      	= IFF_MASTER;

	dev->type       	= ARPHRD_SLIP;
	dev->tx_queue_len 	= 5;		/* Hands them off fast */
202
	dev->priv_flags	       &= ~IFF_XMIT_DST_RELEASE;
L
Linus Torvalds 已提交
203 204 205 206 207 208 209
}

static int eql_open(struct net_device *dev)
{
	equalizer_t *eql = netdev_priv(dev);

	/* XXX We should force this off automatically for the user. */
210 211
	netdev_info(dev,
		    "remember to turn off Van-Jacobson compression on your slave devices\n");
L
Linus Torvalds 已提交
212

213
	BUG_ON(!list_empty(&eql->queue.all_slaves));
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221 222

	eql->min_slaves = 1;
	eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */

	add_timer(&eql->timer);

	return 0;
}

223
static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave)
L
Linus Torvalds 已提交
224 225
{
	list_del(&slave->list);
226
	queue->num_slaves--;
L
Linus Torvalds 已提交
227 228 229 230 231 232
	slave->dev->flags &= ~IFF_SLAVE;
	dev_put(slave->dev);
	kfree(slave);
}

static void eql_kill_slave_queue(slave_queue_t *queue)
233
{
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241
	struct list_head *head, *tmp, *this;

	spin_lock_bh(&queue->lock);

	head = &queue->all_slaves;
	list_for_each_safe(this, tmp, head) {
		slave_t *s = list_entry(this, slave_t, list);

242
		eql_kill_one_slave(queue, s);
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251 252 253
	}

	spin_unlock_bh(&queue->lock);
}

static int eql_close(struct net_device *dev)
{
	equalizer_t *eql = netdev_priv(dev);

	/*
	 *	The timer has to be stopped first before we start hacking away
254
	 *	at the data structure it scans every so often...
L
Linus Torvalds 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	 */

	del_timer_sync(&eql->timer);

	eql_kill_slave_queue(&eql->queue);

	return 0;
}

static int eql_enslave(struct net_device *dev,  slaving_request_t __user *srq);
static int eql_emancipate(struct net_device *dev, slaving_request_t __user *srq);

static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *sc);

static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mc);
static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mc);

static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
274
{
L
Linus Torvalds 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG &&
	    !capable(CAP_NET_ADMIN))
	  	return -EPERM;

	switch (cmd) {
		case EQL_ENSLAVE:
			return eql_enslave(dev, ifr->ifr_data);
		case EQL_EMANCIPATE:
			return eql_emancipate(dev, ifr->ifr_data);
		case EQL_GETSLAVECFG:
			return eql_g_slave_cfg(dev, ifr->ifr_data);
		case EQL_SETSLAVECFG:
			return eql_s_slave_cfg(dev, ifr->ifr_data);
		case EQL_GETMASTRCFG:
			return eql_g_master_cfg(dev, ifr->ifr_data);
		case EQL_SETMASTRCFG:
			return eql_s_master_cfg(dev, ifr->ifr_data);
		default:
			return -EOPNOTSUPP;
294
	}
L
Linus Torvalds 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
}

/* queue->lock must be held */
static slave_t *__eql_schedule_slaves(slave_queue_t *queue)
{
	unsigned long best_load = ~0UL;
	struct list_head *this, *tmp, *head;
	slave_t *best_slave;

	best_slave = NULL;

	/* Make a pass to set the best slave. */
	head = &queue->all_slaves;
	list_for_each_safe(this, tmp, head) {
		slave_t *slave = list_entry(this, slave_t, list);
310
		unsigned long slave_load, bytes_queued, priority_Bps;
L
Linus Torvalds 已提交
311 312 313 314 315

		/* Go through the slave list once, updating best_slave
		 * whenever a new best_load is found.
		 */
		bytes_queued = slave->bytes_queued;
316
		priority_Bps = slave->priority_Bps;
L
Linus Torvalds 已提交
317
		if ((slave->dev->flags & IFF_UP) == IFF_UP) {
318
			slave_load = (~0UL - (~0UL / 2)) -
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326
				(priority_Bps) + bytes_queued * 8;

			if (slave_load < best_load) {
				best_load = slave_load;
				best_slave = slave;
			}
		} else {
			/* We found a dead slave, kill it. */
327
			eql_kill_one_slave(queue, slave);
L
Linus Torvalds 已提交
328 329 330 331 332
		}
	}
	return best_slave;
}

333
static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343 344
{
	equalizer_t *eql = netdev_priv(dev);
	slave_t *slave;

	spin_lock(&eql->queue.lock);

	slave = __eql_schedule_slaves(&eql->queue);
	if (slave) {
		struct net_device *slave_dev = slave->dev;

		skb->dev = slave_dev;
E
Eric Dumazet 已提交
345
		skb->priority = TC_PRIO_FILLER;
346
		slave->bytes_queued += skb->len;
L
Linus Torvalds 已提交
347
		dev_queue_xmit(skb);
348
		dev->stats.tx_packets++;
L
Linus Torvalds 已提交
349
	} else {
350
		dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
351
		dev_kfree_skb(skb);
352
	}
L
Linus Torvalds 已提交
353 354 355

	spin_unlock(&eql->queue.lock);

356
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
}

/*
 *	Private ioctl functions
 */

/* queue->lock must be held */
static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
{
	struct list_head *this, *head;

	head = &queue->all_slaves;
	list_for_each(this, head) {
		slave_t *slave = list_entry(this, slave_t, list);

		if (slave->dev == dev)
			return slave;
	}

	return NULL;
}

static inline int eql_is_full(slave_queue_t *queue)
{
	equalizer_t *eql = netdev_priv(queue->master_dev);

	if (queue->num_slaves >= eql->max_slaves)
		return 1;
	return 0;
}

/* queue->lock must be held */
static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave)
{
	if (!eql_is_full(queue)) {
		slave_t *duplicate_slave = NULL;

		duplicate_slave = __eql_find_slave_dev(queue, slave->dev);
S
Stephen Hemminger 已提交
395
		if (duplicate_slave)
396
			eql_kill_one_slave(queue, duplicate_slave);
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

		list_add(&slave->list, &queue->all_slaves);
		queue->num_slaves++;
		slave->dev->flags |= IFF_SLAVE;

		return 0;
	}

	return -ENOSPC;
}

static int eql_enslave(struct net_device *master_dev, slaving_request_t __user *srqp)
{
	struct net_device *slave_dev;
	slaving_request_t srq;

	if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
		return -EFAULT;

416
	slave_dev  = dev_get_by_name(&init_net, srq.slave_name);
L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
	if (slave_dev) {
		if ((master_dev->flags & IFF_UP) == IFF_UP) {
			/* slave is not a master & not already a slave: */
			if (!eql_is_master(slave_dev) &&
			    !eql_is_slave(slave_dev)) {
				slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL);
				equalizer_t *eql = netdev_priv(master_dev);
				int ret;

				if (!s) {
					dev_put(slave_dev);
					return -ENOMEM;
				}

				memset(s, 0, sizeof(*s));
				s->dev = slave_dev;
				s->priority = srq.priority;
				s->priority_bps = srq.priority;
				s->priority_Bps = srq.priority / 8;

				spin_lock_bh(&eql->queue.lock);
				ret = __eql_insert_slave(&eql->queue, s);
				if (ret) {
					dev_put(slave_dev);
					kfree(s);
				}
				spin_unlock_bh(&eql->queue.lock);

				return ret;
			}
		}
		dev_put(slave_dev);
	}

	return -EINVAL;
}

static int eql_emancipate(struct net_device *master_dev, slaving_request_t __user *srqp)
{
	equalizer_t *eql = netdev_priv(master_dev);
	struct net_device *slave_dev;
	slaving_request_t srq;
	int ret;

	if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
		return -EFAULT;

464
	slave_dev = dev_get_by_name(&init_net, srq.slave_name);
L
Linus Torvalds 已提交
465 466 467 468 469 470 471 472 473
	ret = -EINVAL;
	if (slave_dev) {
		spin_lock_bh(&eql->queue.lock);

		if (eql_is_slave(slave_dev)) {
			slave_t *slave = __eql_find_slave_dev(&eql->queue,
							      slave_dev);

			if (slave) {
474
				eql_kill_one_slave(&eql->queue, slave);
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
				ret = 0;
			}
		}
		dev_put(slave_dev);

		spin_unlock_bh(&eql->queue.lock);
	}

	return ret;
}

static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
{
	equalizer_t *eql = netdev_priv(dev);
	slave_t *slave;
	struct net_device *slave_dev;
	slave_config_t sc;
	int ret;

	if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
		return -EFAULT;

497
	slave_dev = dev_get_by_name(&init_net, sc.slave_name);
L
Linus Torvalds 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	if (!slave_dev)
		return -ENODEV;

	ret = -EINVAL;

	spin_lock_bh(&eql->queue.lock);
	if (eql_is_slave(slave_dev)) {
		slave = __eql_find_slave_dev(&eql->queue, slave_dev);
		if (slave) {
			sc.priority = slave->priority;
			ret = 0;
		}
	}
	spin_unlock_bh(&eql->queue.lock);

	dev_put(slave_dev);

	if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t)))
		ret = -EFAULT;

	return ret;
}

static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
{
	slave_t *slave;
	equalizer_t *eql;
	struct net_device *slave_dev;
	slave_config_t sc;
	int ret;

	if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
		return -EFAULT;

532
	slave_dev = dev_get_by_name(&init_net, sc.slave_name);
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	if (!slave_dev)
		return -ENODEV;

	ret = -EINVAL;

	eql = netdev_priv(dev);
	spin_lock_bh(&eql->queue.lock);
	if (eql_is_slave(slave_dev)) {
		slave = __eql_find_slave_dev(&eql->queue, slave_dev);
		if (slave) {
			slave->priority = sc.priority;
			slave->priority_bps = sc.priority;
			slave->priority_Bps = sc.priority / 8;
			ret = 0;
		}
	}
	spin_unlock_bh(&eql->queue.lock);

551 552
	dev_put(slave_dev);

L
Linus Torvalds 已提交
553 554 555 556 557 558 559 560
	return ret;
}

static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mcp)
{
	equalizer_t *eql;
	master_config_t mc;

561 562
	memset(&mc, 0, sizeof(master_config_t));

L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	if (eql_is_master(dev)) {
		eql = netdev_priv(dev);
		mc.max_slaves = eql->max_slaves;
		mc.min_slaves = eql->min_slaves;
		if (copy_to_user(mcp, &mc, sizeof (master_config_t)))
			return -EFAULT;
		return 0;
	}
	return -EINVAL;
}

static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mcp)
{
	equalizer_t *eql;
	master_config_t mc;

	if (copy_from_user(&mc, mcp, sizeof (master_config_t)))
		return -EFAULT;

	if (eql_is_master(dev)) {
		eql = netdev_priv(dev);
		eql->max_slaves = mc.max_slaves;
		eql->min_slaves = mc.min_slaves;
		return 0;
	}
	return -EINVAL;
}

static struct net_device *dev_eql;

static int __init eql_init_module(void)
{
	int err;

597
	pr_info("%s\n", version);
L
Linus Torvalds 已提交
598 599 600 601 602 603

	dev_eql = alloc_netdev(sizeof(equalizer_t), "eql", eql_setup);
	if (!dev_eql)
		return -ENOMEM;

	err = register_netdev(dev_eql);
604
	if (err)
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617
		free_netdev(dev_eql);
	return err;
}

static void __exit eql_cleanup_module(void)
{
	unregister_netdev(dev_eql);
	free_netdev(dev_eql);
}

module_init(eql_init_module);
module_exit(eql_cleanup_module);
MODULE_LICENSE("GPL");