sch_teql.c 12 KB

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