PageRenderTime 131ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/net/sched/sch_teql.c

https://gitlab.com/deadnem/Singularity-DeYuS
C | 533 lines | 403 code | 86 blank | 44 comment | 75 complexity | 41c2d37c3e68771763e2c452f1117db6 MD5 | raw file
  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 sk_buff_head q;
  62. };
  63. #define NEXT_SLAVE(q) (((struct teql_sched_data *)qdisc_priv(q))->next)
  64. #define FMASK (IFF_BROADCAST | IFF_POINTOPOINT)
  65. /* "teql*" qdisc routines */
  66. static int
  67. teql_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  68. {
  69. struct net_device *dev = qdisc_dev(sch);
  70. struct teql_sched_data *q = qdisc_priv(sch);
  71. if (q->q.qlen < dev->tx_queue_len) {
  72. __skb_queue_tail(&q->q, skb);
  73. return NET_XMIT_SUCCESS;
  74. }
  75. return qdisc_drop(skb, sch);
  76. }
  77. static struct sk_buff *
  78. teql_dequeue(struct Qdisc *sch)
  79. {
  80. struct teql_sched_data *dat = qdisc_priv(sch);
  81. struct netdev_queue *dat_queue;
  82. struct sk_buff *skb;
  83. skb = __skb_dequeue(&dat->q);
  84. dat_queue = netdev_get_tx_queue(dat->m->dev, 0);
  85. if (skb == NULL) {
  86. struct net_device *m = qdisc_dev(dat_queue->qdisc);
  87. if (m) {
  88. dat->m->slaves = sch;
  89. netif_wake_queue(m);
  90. }
  91. } else {
  92. qdisc_bstats_update(sch, skb);
  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. }
  116. static void
  117. teql_destroy(struct Qdisc *sch)
  118. {
  119. struct Qdisc *q, *prev;
  120. struct teql_sched_data *dat = qdisc_priv(sch);
  121. struct teql_master *master = dat->m;
  122. prev = master->slaves;
  123. if (prev) {
  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. break;
  143. }
  144. } while ((prev = q) != master->slaves);
  145. }
  146. }
  147. static int teql_qdisc_init(struct Qdisc *sch, struct nlattr *opt)
  148. {
  149. struct net_device *dev = qdisc_dev(sch);
  150. struct teql_master *m = (struct teql_master *)sch->ops;
  151. struct teql_sched_data *q = qdisc_priv(sch);
  152. if (dev->hard_header_len > m->dev->hard_header_len)
  153. return -EINVAL;
  154. if (m->dev == dev)
  155. return -ELOOP;
  156. q->m = m;
  157. skb_queue_head_init(&q->q);
  158. if (m->slaves) {
  159. if (m->dev->flags & IFF_UP) {
  160. if ((m->dev->flags & IFF_POINTOPOINT &&
  161. !(dev->flags & IFF_POINTOPOINT)) ||
  162. (m->dev->flags & IFF_BROADCAST &&
  163. !(dev->flags & IFF_BROADCAST)) ||
  164. (m->dev->flags & IFF_MULTICAST &&
  165. !(dev->flags & IFF_MULTICAST)) ||
  166. dev->mtu < m->dev->mtu)
  167. return -EINVAL;
  168. } else {
  169. if (!(dev->flags&IFF_POINTOPOINT))
  170. m->dev->flags &= ~IFF_POINTOPOINT;
  171. if (!(dev->flags&IFF_BROADCAST))
  172. m->dev->flags &= ~IFF_BROADCAST;
  173. if (!(dev->flags&IFF_MULTICAST))
  174. m->dev->flags &= ~IFF_MULTICAST;
  175. if (dev->mtu < m->dev->mtu)
  176. m->dev->mtu = dev->mtu;
  177. }
  178. q->next = NEXT_SLAVE(m->slaves);
  179. NEXT_SLAVE(m->slaves) = sch;
  180. } else {
  181. q->next = sch;
  182. m->slaves = sch;
  183. m->dev->mtu = dev->mtu;
  184. m->dev->flags = (m->dev->flags&~FMASK)|(dev->flags&FMASK);
  185. }
  186. return 0;
  187. }
  188. static int
  189. __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res,
  190. struct net_device *dev, struct netdev_queue *txq,
  191. struct dst_entry *dst)
  192. {
  193. struct neighbour *n;
  194. int err = 0;
  195. n = dst_neigh_lookup_skb(dst, skb);
  196. if (!n)
  197. return -ENOENT;
  198. if (dst->dev != dev) {
  199. struct neighbour *mn;
  200. mn = __neigh_lookup_errno(n->tbl, n->primary_key, dev);
  201. neigh_release(n);
  202. if (IS_ERR(mn))
  203. return PTR_ERR(mn);
  204. n = mn;
  205. }
  206. if (neigh_event_send(n, skb_res) == 0) {
  207. int err;
  208. char haddr[MAX_ADDR_LEN];
  209. neigh_ha_snapshot(haddr, n, dev);
  210. err = dev_hard_header(skb, dev, ntohs(skb->protocol), haddr,
  211. NULL, skb->len);
  212. if (err < 0)
  213. err = -EINVAL;
  214. } else {
  215. err = (skb_res == NULL) ? -EAGAIN : 1;
  216. }
  217. neigh_release(n);
  218. return err;
  219. }
  220. static inline int teql_resolve(struct sk_buff *skb,
  221. struct sk_buff *skb_res,
  222. struct net_device *dev,
  223. struct netdev_queue *txq)
  224. {
  225. struct dst_entry *dst = skb_dst(skb);
  226. int res;
  227. if (txq->qdisc == &noop_qdisc)
  228. return -ENODEV;
  229. if (!dev->header_ops || !dst)
  230. return 0;
  231. rcu_read_lock();
  232. res = __teql_resolve(skb, skb_res, dev, txq, dst);
  233. rcu_read_unlock();
  234. return res;
  235. }
  236. static netdev_tx_t teql_master_xmit(struct sk_buff *skb, struct net_device *dev)
  237. {
  238. struct teql_master *master = netdev_priv(dev);
  239. struct Qdisc *start, *q;
  240. int busy;
  241. int nores;
  242. int subq = skb_get_queue_mapping(skb);
  243. struct sk_buff *skb_res = NULL;
  244. start = master->slaves;
  245. restart:
  246. nores = 0;
  247. busy = 0;
  248. q = start;
  249. if (!q)
  250. goto drop;
  251. do {
  252. struct net_device *slave = qdisc_dev(q);
  253. struct netdev_queue *slave_txq = netdev_get_tx_queue(slave, 0);
  254. const struct net_device_ops *slave_ops = slave->netdev_ops;
  255. if (slave_txq->qdisc_sleeping != q)
  256. continue;
  257. if (netif_xmit_stopped(netdev_get_tx_queue(slave, subq)) ||
  258. !netif_running(slave)) {
  259. busy = 1;
  260. continue;
  261. }
  262. switch (teql_resolve(skb, skb_res, slave, slave_txq)) {
  263. case 0:
  264. if (__netif_tx_trylock(slave_txq)) {
  265. unsigned int length = qdisc_pkt_len(skb);
  266. if (!netif_xmit_frozen_or_stopped(slave_txq) &&
  267. slave_ops->ndo_start_xmit(skb, slave) == NETDEV_TX_OK) {
  268. txq_trans_update(slave_txq);
  269. __netif_tx_unlock(slave_txq);
  270. master->slaves = NEXT_SLAVE(q);
  271. netif_wake_queue(dev);
  272. master->tx_packets++;
  273. master->tx_bytes += length;
  274. return NETDEV_TX_OK;
  275. }
  276. __netif_tx_unlock(slave_txq);
  277. }
  278. if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)))
  279. busy = 1;
  280. break;
  281. case 1:
  282. master->slaves = NEXT_SLAVE(q);
  283. return NETDEV_TX_OK;
  284. default:
  285. nores = 1;
  286. break;
  287. }
  288. __skb_pull(skb, skb_network_offset(skb));
  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 NETDEV_TX_BUSY;
  297. }
  298. master->tx_errors++;
  299. drop:
  300. master->tx_dropped++;
  301. dev_kfree_skb(skb);
  302. return NETDEV_TX_OK;
  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 int 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 = qdisc_dev(q);
  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 rtnl_link_stats64 *teql_master_stats64(struct net_device *dev,
  344. struct rtnl_link_stats64 *stats)
  345. {
  346. struct teql_master *m = netdev_priv(dev);
  347. stats->tx_packets = m->tx_packets;
  348. stats->tx_bytes = m->tx_bytes;
  349. stats->tx_errors = m->tx_errors;
  350. stats->tx_dropped = m->tx_dropped;
  351. return stats;
  352. }
  353. static int teql_master_mtu(struct net_device *dev, int new_mtu)
  354. {
  355. struct teql_master *m = netdev_priv(dev);
  356. struct Qdisc *q;
  357. if (new_mtu < 68)
  358. return -EINVAL;
  359. q = m->slaves;
  360. if (q) {
  361. do {
  362. if (new_mtu > qdisc_dev(q)->mtu)
  363. return -EINVAL;
  364. } while ((q = NEXT_SLAVE(q)) != m->slaves);
  365. }
  366. dev->mtu = new_mtu;
  367. return 0;
  368. }
  369. static const struct net_device_ops teql_netdev_ops = {
  370. .ndo_open = teql_master_open,
  371. .ndo_stop = teql_master_close,
  372. .ndo_start_xmit = teql_master_xmit,
  373. .ndo_get_stats64 = teql_master_stats64,
  374. .ndo_change_mtu = teql_master_mtu,
  375. };
  376. static __init void teql_master_setup(struct net_device *dev)
  377. {
  378. struct teql_master *master = netdev_priv(dev);
  379. struct Qdisc_ops *ops = &master->qops;
  380. master->dev = dev;
  381. ops->priv_size = sizeof(struct teql_sched_data);
  382. ops->enqueue = teql_enqueue;
  383. ops->dequeue = teql_dequeue;
  384. ops->peek = teql_peek;
  385. ops->init = teql_qdisc_init;
  386. ops->reset = teql_reset;
  387. ops->destroy = teql_destroy;
  388. ops->owner = THIS_MODULE;
  389. dev->netdev_ops = &teql_netdev_ops;
  390. dev->type = ARPHRD_VOID;
  391. dev->mtu = 1500;
  392. dev->tx_queue_len = 100;
  393. dev->flags = IFF_NOARP;
  394. dev->hard_header_len = LL_MAX_HEADER;
  395. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  396. }
  397. static LIST_HEAD(master_dev_list);
  398. static int max_equalizers = 1;
  399. module_param(max_equalizers, int, 0);
  400. MODULE_PARM_DESC(max_equalizers, "Max number of link equalizers");
  401. static int __init teql_init(void)
  402. {
  403. int i;
  404. int err = -ENODEV;
  405. for (i = 0; i < max_equalizers; i++) {
  406. struct net_device *dev;
  407. struct teql_master *master;
  408. dev = alloc_netdev(sizeof(struct teql_master),
  409. "teql%d", teql_master_setup);
  410. if (!dev) {
  411. err = -ENOMEM;
  412. break;
  413. }
  414. if ((err = register_netdev(dev))) {
  415. free_netdev(dev);
  416. break;
  417. }
  418. master = netdev_priv(dev);
  419. strlcpy(master->qops.id, dev->name, IFNAMSIZ);
  420. err = register_qdisc(&master->qops);
  421. if (err) {
  422. unregister_netdev(dev);
  423. free_netdev(dev);
  424. break;
  425. }
  426. list_add_tail(&master->master_list, &master_dev_list);
  427. }
  428. return i ? 0 : err;
  429. }
  430. static void __exit teql_exit(void)
  431. {
  432. struct teql_master *master, *nxt;
  433. list_for_each_entry_safe(master, nxt, &master_dev_list, master_list) {
  434. list_del(&master->master_list);
  435. unregister_qdisc(&master->qops);
  436. unregister_netdev(master->dev);
  437. free_netdev(master->dev);
  438. }
  439. }
  440. module_init(teql_init);
  441. module_exit(teql_exit);
  442. MODULE_LICENSE("GPL");