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