eql.c 15 KB

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