eql.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  112. #include <linux/capability.h>
  113. #include <linux/module.h>
  114. #include <linux/kernel.h>
  115. #include <linux/init.h>
  116. #include <linux/slab.h>
  117. #include <linux/timer.h>
  118. #include <linux/netdevice.h>
  119. #include <net/net_namespace.h>
  120. #include <linux/if.h>
  121. #include <linux/if_arp.h>
  122. #include <linux/if_eql.h>
  123. #include <linux/pkt_sched.h>
  124. #include <linux/uaccess.h>
  125. static int eql_open(struct net_device *dev);
  126. static int eql_close(struct net_device *dev);
  127. static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  128. static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev);
  129. #define eql_is_slave(dev) ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
  130. #define eql_is_master(dev) ((dev->flags & IFF_MASTER) == IFF_MASTER)
  131. static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave);
  132. static void eql_timer(struct timer_list *t)
  133. {
  134. equalizer_t *eql = from_timer(eql, t, timer);
  135. struct list_head *this, *tmp, *head;
  136. spin_lock(&eql->queue.lock);
  137. head = &eql->queue.all_slaves;
  138. list_for_each_safe(this, tmp, head) {
  139. slave_t *slave = list_entry(this, slave_t, list);
  140. if ((slave->dev->flags & IFF_UP) == IFF_UP) {
  141. slave->bytes_queued -= slave->priority_Bps;
  142. if (slave->bytes_queued < 0)
  143. slave->bytes_queued = 0;
  144. } else {
  145. eql_kill_one_slave(&eql->queue, slave);
  146. }
  147. }
  148. spin_unlock(&eql->queue.lock);
  149. eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
  150. add_timer(&eql->timer);
  151. }
  152. static const char version[] __initconst =
  153. "Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)";
  154. static const struct net_device_ops eql_netdev_ops = {
  155. .ndo_open = eql_open,
  156. .ndo_stop = eql_close,
  157. .ndo_do_ioctl = eql_ioctl,
  158. .ndo_start_xmit = eql_slave_xmit,
  159. };
  160. static void __init eql_setup(struct net_device *dev)
  161. {
  162. equalizer_t *eql = netdev_priv(dev);
  163. timer_setup(&eql->timer, eql_timer, 0);
  164. eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
  165. spin_lock_init(&eql->queue.lock);
  166. INIT_LIST_HEAD(&eql->queue.all_slaves);
  167. eql->queue.master_dev = dev;
  168. dev->netdev_ops = &eql_netdev_ops;
  169. /*
  170. * Now we undo some of the things that eth_setup does
  171. * that we don't like
  172. */
  173. dev->mtu = EQL_DEFAULT_MTU; /* set to 576 in if_eql.h */
  174. dev->flags = IFF_MASTER;
  175. dev->type = ARPHRD_SLIP;
  176. dev->tx_queue_len = 5; /* Hands them off fast */
  177. netif_keep_dst(dev);
  178. }
  179. static int eql_open(struct net_device *dev)
  180. {
  181. equalizer_t *eql = netdev_priv(dev);
  182. /* XXX We should force this off automatically for the user. */
  183. netdev_info(dev,
  184. "remember to turn off Van-Jacobson compression on your slave devices\n");
  185. BUG_ON(!list_empty(&eql->queue.all_slaves));
  186. eql->min_slaves = 1;
  187. eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
  188. add_timer(&eql->timer);
  189. return 0;
  190. }
  191. static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave)
  192. {
  193. list_del(&slave->list);
  194. queue->num_slaves--;
  195. slave->dev->flags &= ~IFF_SLAVE;
  196. dev_put(slave->dev);
  197. kfree(slave);
  198. }
  199. static void eql_kill_slave_queue(slave_queue_t *queue)
  200. {
  201. struct list_head *head, *tmp, *this;
  202. spin_lock_bh(&queue->lock);
  203. head = &queue->all_slaves;
  204. list_for_each_safe(this, tmp, head) {
  205. slave_t *s = list_entry(this, slave_t, list);
  206. eql_kill_one_slave(queue, s);
  207. }
  208. spin_unlock_bh(&queue->lock);
  209. }
  210. static int eql_close(struct net_device *dev)
  211. {
  212. equalizer_t *eql = netdev_priv(dev);
  213. /*
  214. * The timer has to be stopped first before we start hacking away
  215. * at the data structure it scans every so often...
  216. */
  217. del_timer_sync(&eql->timer);
  218. eql_kill_slave_queue(&eql->queue);
  219. return 0;
  220. }
  221. static int eql_enslave(struct net_device *dev, slaving_request_t __user *srq);
  222. static int eql_emancipate(struct net_device *dev, slaving_request_t __user *srq);
  223. static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
  224. static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
  225. static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mc);
  226. static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mc);
  227. static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  228. {
  229. if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG &&
  230. !capable(CAP_NET_ADMIN))
  231. return -EPERM;
  232. switch (cmd) {
  233. case EQL_ENSLAVE:
  234. return eql_enslave(dev, ifr->ifr_data);
  235. case EQL_EMANCIPATE:
  236. return eql_emancipate(dev, ifr->ifr_data);
  237. case EQL_GETSLAVECFG:
  238. return eql_g_slave_cfg(dev, ifr->ifr_data);
  239. case EQL_SETSLAVECFG:
  240. return eql_s_slave_cfg(dev, ifr->ifr_data);
  241. case EQL_GETMASTRCFG:
  242. return eql_g_master_cfg(dev, ifr->ifr_data);
  243. case EQL_SETMASTRCFG:
  244. return eql_s_master_cfg(dev, ifr->ifr_data);
  245. default:
  246. return -EOPNOTSUPP;
  247. }
  248. }
  249. /* queue->lock must be held */
  250. static slave_t *__eql_schedule_slaves(slave_queue_t *queue)
  251. {
  252. unsigned long best_load = ~0UL;
  253. struct list_head *this, *tmp, *head;
  254. slave_t *best_slave;
  255. best_slave = NULL;
  256. /* Make a pass to set the best slave. */
  257. head = &queue->all_slaves;
  258. list_for_each_safe(this, tmp, head) {
  259. slave_t *slave = list_entry(this, slave_t, list);
  260. unsigned long slave_load, bytes_queued, priority_Bps;
  261. /* Go through the slave list once, updating best_slave
  262. * whenever a new best_load is found.
  263. */
  264. bytes_queued = slave->bytes_queued;
  265. priority_Bps = slave->priority_Bps;
  266. if ((slave->dev->flags & IFF_UP) == IFF_UP) {
  267. slave_load = (~0UL - (~0UL / 2)) -
  268. (priority_Bps) + bytes_queued * 8;
  269. if (slave_load < best_load) {
  270. best_load = slave_load;
  271. best_slave = slave;
  272. }
  273. } else {
  274. /* We found a dead slave, kill it. */
  275. eql_kill_one_slave(queue, slave);
  276. }
  277. }
  278. return best_slave;
  279. }
  280. static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
  281. {
  282. equalizer_t *eql = netdev_priv(dev);
  283. slave_t *slave;
  284. spin_lock(&eql->queue.lock);
  285. slave = __eql_schedule_slaves(&eql->queue);
  286. if (slave) {
  287. struct net_device *slave_dev = slave->dev;
  288. skb->dev = slave_dev;
  289. skb->priority = TC_PRIO_FILLER;
  290. slave->bytes_queued += skb->len;
  291. dev_queue_xmit(skb);
  292. dev->stats.tx_packets++;
  293. } else {
  294. dev->stats.tx_dropped++;
  295. dev_kfree_skb(skb);
  296. }
  297. spin_unlock(&eql->queue.lock);
  298. return NETDEV_TX_OK;
  299. }
  300. /*
  301. * Private ioctl functions
  302. */
  303. /* queue->lock must be held */
  304. static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
  305. {
  306. struct list_head *this, *head;
  307. head = &queue->all_slaves;
  308. list_for_each(this, head) {
  309. slave_t *slave = list_entry(this, slave_t, list);
  310. if (slave->dev == dev)
  311. return slave;
  312. }
  313. return NULL;
  314. }
  315. static inline int eql_is_full(slave_queue_t *queue)
  316. {
  317. equalizer_t *eql = netdev_priv(queue->master_dev);
  318. if (queue->num_slaves >= eql->max_slaves)
  319. return 1;
  320. return 0;
  321. }
  322. /* queue->lock must be held */
  323. static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave)
  324. {
  325. if (!eql_is_full(queue)) {
  326. slave_t *duplicate_slave = NULL;
  327. duplicate_slave = __eql_find_slave_dev(queue, slave->dev);
  328. if (duplicate_slave)
  329. eql_kill_one_slave(queue, duplicate_slave);
  330. dev_hold(slave->dev);
  331. list_add(&slave->list, &queue->all_slaves);
  332. queue->num_slaves++;
  333. slave->dev->flags |= IFF_SLAVE;
  334. return 0;
  335. }
  336. return -ENOSPC;
  337. }
  338. static int eql_enslave(struct net_device *master_dev, slaving_request_t __user *srqp)
  339. {
  340. struct net_device *slave_dev;
  341. slaving_request_t srq;
  342. if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
  343. return -EFAULT;
  344. slave_dev = __dev_get_by_name(&init_net, srq.slave_name);
  345. if (!slave_dev)
  346. return -ENODEV;
  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) && !eql_is_slave(slave_dev)) {
  350. slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL);
  351. equalizer_t *eql = netdev_priv(master_dev);
  352. int ret;
  353. if (!s)
  354. return -ENOMEM;
  355. memset(s, 0, sizeof(*s));
  356. s->dev = slave_dev;
  357. s->priority = srq.priority;
  358. s->priority_bps = srq.priority;
  359. s->priority_Bps = srq.priority / 8;
  360. spin_lock_bh(&eql->queue.lock);
  361. ret = __eql_insert_slave(&eql->queue, s);
  362. if (ret)
  363. kfree(s);
  364. spin_unlock_bh(&eql->queue.lock);
  365. return ret;
  366. }
  367. }
  368. return -EINVAL;
  369. }
  370. static int eql_emancipate(struct net_device *master_dev, slaving_request_t __user *srqp)
  371. {
  372. equalizer_t *eql = netdev_priv(master_dev);
  373. struct net_device *slave_dev;
  374. slaving_request_t srq;
  375. int ret;
  376. if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
  377. return -EFAULT;
  378. slave_dev = __dev_get_by_name(&init_net, srq.slave_name);
  379. if (!slave_dev)
  380. return -ENODEV;
  381. ret = -EINVAL;
  382. spin_lock_bh(&eql->queue.lock);
  383. if (eql_is_slave(slave_dev)) {
  384. slave_t *slave = __eql_find_slave_dev(&eql->queue, slave_dev);
  385. if (slave) {
  386. eql_kill_one_slave(&eql->queue, slave);
  387. ret = 0;
  388. }
  389. }
  390. spin_unlock_bh(&eql->queue.lock);
  391. return ret;
  392. }
  393. static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
  394. {
  395. equalizer_t *eql = netdev_priv(dev);
  396. slave_t *slave;
  397. struct net_device *slave_dev;
  398. slave_config_t sc;
  399. int ret;
  400. if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
  401. return -EFAULT;
  402. slave_dev = __dev_get_by_name(&init_net, sc.slave_name);
  403. if (!slave_dev)
  404. return -ENODEV;
  405. ret = -EINVAL;
  406. spin_lock_bh(&eql->queue.lock);
  407. if (eql_is_slave(slave_dev)) {
  408. slave = __eql_find_slave_dev(&eql->queue, slave_dev);
  409. if (slave) {
  410. sc.priority = slave->priority;
  411. ret = 0;
  412. }
  413. }
  414. spin_unlock_bh(&eql->queue.lock);
  415. if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t)))
  416. ret = -EFAULT;
  417. return ret;
  418. }
  419. static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
  420. {
  421. slave_t *slave;
  422. equalizer_t *eql;
  423. struct net_device *slave_dev;
  424. slave_config_t sc;
  425. int ret;
  426. if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
  427. return -EFAULT;
  428. slave_dev = __dev_get_by_name(&init_net, sc.slave_name);
  429. if (!slave_dev)
  430. return -ENODEV;
  431. ret = -EINVAL;
  432. eql = netdev_priv(dev);
  433. spin_lock_bh(&eql->queue.lock);
  434. if (eql_is_slave(slave_dev)) {
  435. slave = __eql_find_slave_dev(&eql->queue, slave_dev);
  436. if (slave) {
  437. slave->priority = sc.priority;
  438. slave->priority_bps = sc.priority;
  439. slave->priority_Bps = sc.priority / 8;
  440. ret = 0;
  441. }
  442. }
  443. spin_unlock_bh(&eql->queue.lock);
  444. return ret;
  445. }
  446. static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mcp)
  447. {
  448. equalizer_t *eql;
  449. master_config_t mc;
  450. memset(&mc, 0, sizeof(master_config_t));
  451. if (eql_is_master(dev)) {
  452. eql = netdev_priv(dev);
  453. mc.max_slaves = eql->max_slaves;
  454. mc.min_slaves = eql->min_slaves;
  455. if (copy_to_user(mcp, &mc, sizeof (master_config_t)))
  456. return -EFAULT;
  457. return 0;
  458. }
  459. return -EINVAL;
  460. }
  461. static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mcp)
  462. {
  463. equalizer_t *eql;
  464. master_config_t mc;
  465. if (copy_from_user(&mc, mcp, sizeof (master_config_t)))
  466. return -EFAULT;
  467. if (eql_is_master(dev)) {
  468. eql = netdev_priv(dev);
  469. eql->max_slaves = mc.max_slaves;
  470. eql->min_slaves = mc.min_slaves;
  471. return 0;
  472. }
  473. return -EINVAL;
  474. }
  475. static struct net_device *dev_eql;
  476. static int __init eql_init_module(void)
  477. {
  478. int err;
  479. pr_info("%s\n", version);
  480. dev_eql = alloc_netdev(sizeof(equalizer_t), "eql", NET_NAME_UNKNOWN,
  481. eql_setup);
  482. if (!dev_eql)
  483. return -ENOMEM;
  484. err = register_netdev(dev_eql);
  485. if (err)
  486. free_netdev(dev_eql);
  487. return err;
  488. }
  489. static void __exit eql_cleanup_module(void)
  490. {
  491. unregister_netdev(dev_eql);
  492. free_netdev(dev_eql);
  493. }
  494. module_init(eql_init_module);
  495. module_exit(eql_cleanup_module);
  496. MODULE_LICENSE("GPL");