sch_teql.c 12 KB

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