sch_teql.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* net/sched/sch_teql.c "True" (or "trivial") link equalizer.
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version
  6. * 2 of the License, or (at your option) any later version.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. */
  10. #include <linux/module.h>
  11. #include <asm/uaccess.h>
  12. #include <asm/system.h>
  13. #include <linux/bitops.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/string.h>
  18. #include <linux/mm.h>
  19. #include <linux/socket.h>
  20. #include <linux/sockios.h>
  21. #include <linux/in.h>
  22. #include <linux/errno.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/if_ether.h>
  26. #include <linux/inet.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/notifier.h>
  30. #include <linux/init.h>
  31. #include <net/ip.h>
  32. #include <net/route.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/moduleparam.h>
  35. #include <net/sock.h>
  36. #include <net/pkt_sched.h>
  37. /*
  38. How to setup it.
  39. ----------------
  40. After loading this module you will find a new device teqlN
  41. and new qdisc with the same name. To join a slave to the equalizer
  42. you should just set this qdisc on a device f.e.
  43. # tc qdisc add dev eth0 root teql0
  44. # tc qdisc add dev eth1 root teql0
  45. That's all. Full PnP 8)
  46. Applicability.
  47. --------------
  48. 1. Slave devices MUST be active devices, i.e., they must raise the tbusy
  49. signal and generate EOI events. If you want to equalize virtual devices
  50. like tunnels, use a normal eql device.
  51. 2. This device puts no limitations on physical slave characteristics
  52. f.e. it will equalize 9600baud line and 100Mb ethernet perfectly :-)
  53. Certainly, large difference in link speeds will make the resulting
  54. eqalized link unusable, because of huge packet reordering.
  55. I estimate an upper useful difference as ~10 times.
  56. 3. If the slave requires address resolution, only protocols using
  57. neighbour cache (IPv4/IPv6) will work over the equalized link.
  58. Other protocols are still allowed to use the slave device directly,
  59. which will not break load balancing, though native slave
  60. traffic will have the highest priority. */
  61. struct teql_master
  62. {
  63. struct Qdisc_ops qops;
  64. struct net_device *dev;
  65. struct Qdisc *slaves;
  66. struct list_head master_list;
  67. struct net_device_stats stats;
  68. };
  69. struct teql_sched_data
  70. {
  71. struct Qdisc *next;
  72. struct teql_master *m;
  73. struct neighbour *ncache;
  74. struct sk_buff_head q;
  75. };
  76. #define NEXT_SLAVE(q) (((struct teql_sched_data*)qdisc_priv(q))->next)
  77. #define FMASK (IFF_BROADCAST|IFF_POINTOPOINT|IFF_BROADCAST)
  78. /* "teql*" qdisc routines */
  79. static int
  80. teql_enqueue(struct sk_buff *skb, struct Qdisc* sch)
  81. {
  82. struct net_device *dev = sch->dev;
  83. struct teql_sched_data *q = qdisc_priv(sch);
  84. __skb_queue_tail(&q->q, skb);
  85. if (q->q.qlen <= dev->tx_queue_len) {
  86. sch->bstats.bytes += skb->len;
  87. sch->bstats.packets++;
  88. return 0;
  89. }
  90. __skb_unlink(skb, &q->q);
  91. kfree_skb(skb);
  92. sch->qstats.drops++;
  93. return NET_XMIT_DROP;
  94. }
  95. static int
  96. teql_requeue(struct sk_buff *skb, struct Qdisc* sch)
  97. {
  98. struct teql_sched_data *q = qdisc_priv(sch);
  99. __skb_queue_head(&q->q, skb);
  100. sch->qstats.requeues++;
  101. return 0;
  102. }
  103. static struct sk_buff *
  104. teql_dequeue(struct Qdisc* sch)
  105. {
  106. struct teql_sched_data *dat = qdisc_priv(sch);
  107. struct sk_buff *skb;
  108. skb = __skb_dequeue(&dat->q);
  109. if (skb == NULL) {
  110. struct net_device *m = dat->m->dev->qdisc->dev;
  111. if (m) {
  112. dat->m->slaves = sch;
  113. netif_wake_queue(m);
  114. }
  115. }
  116. sch->q.qlen = dat->q.qlen + dat->m->dev->qdisc->q.qlen;
  117. return skb;
  118. }
  119. static __inline__ void
  120. teql_neigh_release(struct neighbour *n)
  121. {
  122. if (n)
  123. neigh_release(n);
  124. }
  125. static void
  126. teql_reset(struct Qdisc* sch)
  127. {
  128. struct teql_sched_data *dat = qdisc_priv(sch);
  129. skb_queue_purge(&dat->q);
  130. sch->q.qlen = 0;
  131. teql_neigh_release(xchg(&dat->ncache, NULL));
  132. }
  133. static void
  134. teql_destroy(struct Qdisc* sch)
  135. {
  136. struct Qdisc *q, *prev;
  137. struct teql_sched_data *dat = qdisc_priv(sch);
  138. struct teql_master *master = dat->m;
  139. if ((prev = master->slaves) != NULL) {
  140. do {
  141. q = NEXT_SLAVE(prev);
  142. if (q == sch) {
  143. NEXT_SLAVE(prev) = NEXT_SLAVE(q);
  144. if (q == master->slaves) {
  145. master->slaves = NEXT_SLAVE(q);
  146. if (q == master->slaves) {
  147. master->slaves = NULL;
  148. spin_lock_bh(&master->dev->queue_lock);
  149. qdisc_reset(master->dev->qdisc);
  150. spin_unlock_bh(&master->dev->queue_lock);
  151. }
  152. }
  153. skb_queue_purge(&dat->q);
  154. teql_neigh_release(xchg(&dat->ncache, NULL));
  155. break;
  156. }
  157. } while ((prev = q) != master->slaves);
  158. }
  159. }
  160. static int teql_qdisc_init(struct Qdisc *sch, struct rtattr *opt)
  161. {
  162. struct net_device *dev = sch->dev;
  163. struct teql_master *m = (struct teql_master*)sch->ops;
  164. struct teql_sched_data *q = qdisc_priv(sch);
  165. if (dev->hard_header_len > m->dev->hard_header_len)
  166. return -EINVAL;
  167. if (m->dev == dev)
  168. return -ELOOP;
  169. q->m = m;
  170. skb_queue_head_init(&q->q);
  171. if (m->slaves) {
  172. if (m->dev->flags & IFF_UP) {
  173. if ((m->dev->flags&IFF_POINTOPOINT && !(dev->flags&IFF_POINTOPOINT))
  174. || (m->dev->flags&IFF_BROADCAST && !(dev->flags&IFF_BROADCAST))
  175. || (m->dev->flags&IFF_MULTICAST && !(dev->flags&IFF_MULTICAST))
  176. || dev->mtu < m->dev->mtu)
  177. return -EINVAL;
  178. } else {
  179. if (!(dev->flags&IFF_POINTOPOINT))
  180. m->dev->flags &= ~IFF_POINTOPOINT;
  181. if (!(dev->flags&IFF_BROADCAST))
  182. m->dev->flags &= ~IFF_BROADCAST;
  183. if (!(dev->flags&IFF_MULTICAST))
  184. m->dev->flags &= ~IFF_MULTICAST;
  185. if (dev->mtu < m->dev->mtu)
  186. m->dev->mtu = dev->mtu;
  187. }
  188. q->next = NEXT_SLAVE(m->slaves);
  189. NEXT_SLAVE(m->slaves) = sch;
  190. } else {
  191. q->next = sch;
  192. m->slaves = sch;
  193. m->dev->mtu = dev->mtu;
  194. m->dev->flags = (m->dev->flags&~FMASK)|(dev->flags&FMASK);
  195. }
  196. return 0;
  197. }
  198. /* "teql*" netdevice routines */
  199. static int
  200. __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, struct net_device *dev)
  201. {
  202. struct teql_sched_data *q = qdisc_priv(dev->qdisc);
  203. struct neighbour *mn = skb->dst->neighbour;
  204. struct neighbour *n = q->ncache;
  205. if (mn->tbl == NULL)
  206. return -EINVAL;
  207. if (n && n->tbl == mn->tbl &&
  208. memcmp(n->primary_key, mn->primary_key, mn->tbl->key_len) == 0) {
  209. atomic_inc(&n->refcnt);
  210. } else {
  211. n = __neigh_lookup_errno(mn->tbl, mn->primary_key, dev);
  212. if (IS_ERR(n))
  213. return PTR_ERR(n);
  214. }
  215. if (neigh_event_send(n, skb_res) == 0) {
  216. int err;
  217. read_lock(&n->lock);
  218. err = dev->hard_header(skb, dev, ntohs(skb->protocol), n->ha, NULL, skb->len);
  219. read_unlock(&n->lock);
  220. if (err < 0) {
  221. neigh_release(n);
  222. return -EINVAL;
  223. }
  224. teql_neigh_release(xchg(&q->ncache, n));
  225. return 0;
  226. }
  227. neigh_release(n);
  228. return (skb_res == NULL) ? -EAGAIN : 1;
  229. }
  230. static __inline__ int
  231. teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, struct net_device *dev)
  232. {
  233. if (dev->hard_header == NULL ||
  234. skb->dst == NULL ||
  235. skb->dst->neighbour == NULL)
  236. return 0;
  237. return __teql_resolve(skb, skb_res, dev);
  238. }
  239. static int teql_master_xmit(struct sk_buff *skb, struct net_device *dev)
  240. {
  241. struct teql_master *master = netdev_priv(dev);
  242. struct Qdisc *start, *q;
  243. int busy;
  244. int nores;
  245. int len = skb->len;
  246. struct sk_buff *skb_res = NULL;
  247. start = master->slaves;
  248. restart:
  249. nores = 0;
  250. busy = 0;
  251. if ((q = start) == NULL)
  252. goto drop;
  253. do {
  254. struct net_device *slave = q->dev;
  255. if (slave->qdisc_sleeping != q)
  256. continue;
  257. if (netif_queue_stopped(slave) || ! netif_running(slave)) {
  258. busy = 1;
  259. continue;
  260. }
  261. switch (teql_resolve(skb, skb_res, slave)) {
  262. case 0:
  263. if (spin_trylock(&slave->xmit_lock)) {
  264. slave->xmit_lock_owner = smp_processor_id();
  265. if (!netif_queue_stopped(slave) &&
  266. slave->hard_start_xmit(skb, slave) == 0) {
  267. slave->xmit_lock_owner = -1;
  268. spin_unlock(&slave->xmit_lock);
  269. master->slaves = NEXT_SLAVE(q);
  270. netif_wake_queue(dev);
  271. master->stats.tx_packets++;
  272. master->stats.tx_bytes += len;
  273. return 0;
  274. }
  275. slave->xmit_lock_owner = -1;
  276. spin_unlock(&slave->xmit_lock);
  277. }
  278. if (netif_queue_stopped(dev))
  279. busy = 1;
  280. break;
  281. case 1:
  282. master->slaves = NEXT_SLAVE(q);
  283. return 0;
  284. default:
  285. nores = 1;
  286. break;
  287. }
  288. __skb_pull(skb, skb->nh.raw - skb->data);
  289. } while ((q = NEXT_SLAVE(q)) != start);
  290. if (nores && skb_res == NULL) {
  291. skb_res = skb;
  292. goto restart;
  293. }
  294. if (busy) {
  295. netif_stop_queue(dev);
  296. return 1;
  297. }
  298. master->stats.tx_errors++;
  299. drop:
  300. master->stats.tx_dropped++;
  301. dev_kfree_skb(skb);
  302. return 0;
  303. }
  304. static int teql_master_open(struct net_device *dev)
  305. {
  306. struct Qdisc * q;
  307. struct teql_master *m = netdev_priv(dev);
  308. int mtu = 0xFFFE;
  309. unsigned flags = IFF_NOARP|IFF_MULTICAST;
  310. if (m->slaves == NULL)
  311. return -EUNATCH;
  312. flags = FMASK;
  313. q = m->slaves;
  314. do {
  315. struct net_device *slave = q->dev;
  316. if (slave == NULL)
  317. return -EUNATCH;
  318. if (slave->mtu < mtu)
  319. mtu = slave->mtu;
  320. if (slave->hard_header_len > LL_MAX_HEADER)
  321. return -EINVAL;
  322. /* If all the slaves are BROADCAST, master is BROADCAST
  323. If all the slaves are PtP, master is PtP
  324. Otherwise, master is NBMA.
  325. */
  326. if (!(slave->flags&IFF_POINTOPOINT))
  327. flags &= ~IFF_POINTOPOINT;
  328. if (!(slave->flags&IFF_BROADCAST))
  329. flags &= ~IFF_BROADCAST;
  330. if (!(slave->flags&IFF_MULTICAST))
  331. flags &= ~IFF_MULTICAST;
  332. } while ((q = NEXT_SLAVE(q)) != m->slaves);
  333. m->dev->mtu = mtu;
  334. m->dev->flags = (m->dev->flags&~FMASK) | flags;
  335. netif_start_queue(m->dev);
  336. return 0;
  337. }
  338. static int teql_master_close(struct net_device *dev)
  339. {
  340. netif_stop_queue(dev);
  341. return 0;
  342. }
  343. static struct net_device_stats *teql_master_stats(struct net_device *dev)
  344. {
  345. struct teql_master *m = netdev_priv(dev);
  346. return &m->stats;
  347. }
  348. static int teql_master_mtu(struct net_device *dev, int new_mtu)
  349. {
  350. struct teql_master *m = netdev_priv(dev);
  351. struct Qdisc *q;
  352. if (new_mtu < 68)
  353. return -EINVAL;
  354. q = m->slaves;
  355. if (q) {
  356. do {
  357. if (new_mtu > q->dev->mtu)
  358. return -EINVAL;
  359. } while ((q=NEXT_SLAVE(q)) != m->slaves);
  360. }
  361. dev->mtu = new_mtu;
  362. return 0;
  363. }
  364. static __init void teql_master_setup(struct net_device *dev)
  365. {
  366. struct teql_master *master = netdev_priv(dev);
  367. struct Qdisc_ops *ops = &master->qops;
  368. master->dev = dev;
  369. ops->priv_size = sizeof(struct teql_sched_data);
  370. ops->enqueue = teql_enqueue;
  371. ops->dequeue = teql_dequeue;
  372. ops->requeue = teql_requeue;
  373. ops->init = teql_qdisc_init;
  374. ops->reset = teql_reset;
  375. ops->destroy = teql_destroy;
  376. ops->owner = THIS_MODULE;
  377. dev->open = teql_master_open;
  378. dev->hard_start_xmit = teql_master_xmit;
  379. dev->stop = teql_master_close;
  380. dev->get_stats = teql_master_stats;
  381. dev->change_mtu = teql_master_mtu;
  382. dev->type = ARPHRD_VOID;
  383. dev->mtu = 1500;
  384. dev->tx_queue_len = 100;
  385. dev->flags = IFF_NOARP;
  386. dev->hard_header_len = LL_MAX_HEADER;
  387. SET_MODULE_OWNER(dev);
  388. }
  389. static LIST_HEAD(master_dev_list);
  390. static int max_equalizers = 1;
  391. module_param(max_equalizers, int, 0);
  392. MODULE_PARM_DESC(max_equalizers, "Max number of link equalizers");
  393. static int __init teql_init(void)
  394. {
  395. int i;
  396. int err = -ENODEV;
  397. for (i = 0; i < max_equalizers; i++) {
  398. struct net_device *dev;
  399. struct teql_master *master;
  400. dev = alloc_netdev(sizeof(struct teql_master),
  401. "teql%d", teql_master_setup);
  402. if (!dev) {
  403. err = -ENOMEM;
  404. break;
  405. }
  406. if ((err = register_netdev(dev))) {
  407. free_netdev(dev);
  408. break;
  409. }
  410. master = netdev_priv(dev);
  411. strlcpy(master->qops.id, dev->name, IFNAMSIZ);
  412. err = register_qdisc(&master->qops);
  413. if (err) {
  414. unregister_netdev(dev);
  415. free_netdev(dev);
  416. break;
  417. }
  418. list_add_tail(&master->master_list, &master_dev_list);
  419. }
  420. return i ? 0 : err;
  421. }
  422. static void __exit teql_exit(void)
  423. {
  424. struct teql_master *master, *nxt;
  425. list_for_each_entry_safe(master, nxt, &master_dev_list, master_list) {
  426. list_del(&master->master_list);
  427. unregister_qdisc(&master->qops);
  428. unregister_netdev(master->dev);
  429. free_netdev(master->dev);
  430. }
  431. }
  432. module_init(teql_init);
  433. module_exit(teql_exit);
  434. MODULE_LICENSE("GPL");