1 /* net/sched/sch_teql.c "True" (or "trivial") link equalizer.
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.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 #include <linux/module.h>
12 #include <asm/uaccess.h>
13 #include <asm/system.h>
14 #include <asm/bitops.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/string.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.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>
32 #include <net/route.h>
33 #include <linux/skbuff.h>
35 #include <net/pkt_sched.h>
41 After loading this module you will find a new device teqlN
42 and new qdisc with the same name. To join a slave to the equalizer
43 you should just set this qdisc on a device f.e.
45 # tc qdisc add dev eth0 root teql0
46 # tc qdisc add dev eth1 root teql0
48 That's all. Full PnP 8)
53 1. Slave devices MUST be active devices, i.e., they must raise the tbusy
54 signal and generate EOI events. If you want to equalize virtual devices
55 like tunnels, use a normal eql device.
56 2. This device puts no limitations on physical slave characteristics
57 f.e. it will equalize 9600baud line and 100Mb ethernet perfectly :-)
58 Certainly, large difference in link speeds will make the resulting
59 eqalized link unusable, because of huge packet reordering.
60 I estimate an upper useful difference as ~10 times.
61 3. If the slave requires address resolution, only protocols using
62 neighbour cache (IPv4/IPv6) will work over the equalized link.
63 Other protocols are still allowed to use the slave device directly,
64 which will not break load balancing, though native slave
65 traffic will have the highest priority. */
69 struct Qdisc_ops qops;
70 struct net_device *dev;
72 struct list_head master_list;
73 struct net_device_stats stats;
76 struct teql_sched_data
79 struct teql_master *m;
80 struct neighbour *ncache;
81 struct sk_buff_head q;
84 #define NEXT_SLAVE(q) (((struct teql_sched_data*)qdisc_priv(q))->next)
86 #define FMASK (IFF_BROADCAST|IFF_POINTOPOINT|IFF_BROADCAST)
88 /* "teql*" qdisc routines */
91 teql_enqueue(struct sk_buff *skb, struct Qdisc* sch)
93 struct net_device *dev = sch->dev;
94 struct teql_sched_data *q = qdisc_priv(sch);
96 __skb_queue_tail(&q->q, skb);
97 if (q->q.qlen <= dev->tx_queue_len) {
98 sch->stats.bytes += skb->len;
103 __skb_unlink(skb, &q->q);
106 return NET_XMIT_DROP;
110 teql_requeue(struct sk_buff *skb, struct Qdisc* sch)
112 struct teql_sched_data *q = qdisc_priv(sch);
114 __skb_queue_head(&q->q, skb);
118 static struct sk_buff *
119 teql_dequeue(struct Qdisc* sch)
121 struct teql_sched_data *dat = qdisc_priv(sch);
124 skb = __skb_dequeue(&dat->q);
126 struct net_device *m = dat->m->dev->qdisc->dev;
128 dat->m->slaves = sch;
132 sch->q.qlen = dat->q.qlen + dat->m->dev->qdisc->q.qlen;
136 static __inline__ void
137 teql_neigh_release(struct neighbour *n)
144 teql_reset(struct Qdisc* sch)
146 struct teql_sched_data *dat = qdisc_priv(sch);
148 skb_queue_purge(&dat->q);
150 teql_neigh_release(xchg(&dat->ncache, NULL));
154 teql_destroy(struct Qdisc* sch)
156 struct Qdisc *q, *prev;
157 struct teql_sched_data *dat = qdisc_priv(sch);
158 struct teql_master *master = dat->m;
160 if ((prev = master->slaves) != NULL) {
162 q = NEXT_SLAVE(prev);
164 NEXT_SLAVE(prev) = NEXT_SLAVE(q);
165 if (q == master->slaves) {
166 master->slaves = NEXT_SLAVE(q);
167 if (q == master->slaves) {
168 master->slaves = NULL;
169 spin_lock_bh(&master->dev->queue_lock);
170 qdisc_reset(master->dev->qdisc);
171 spin_unlock_bh(&master->dev->queue_lock);
174 skb_queue_purge(&dat->q);
175 teql_neigh_release(xchg(&dat->ncache, NULL));
179 } while ((prev = q) != master->slaves);
183 static int teql_qdisc_init(struct Qdisc *sch, struct rtattr *opt)
185 struct net_device *dev = sch->dev;
186 struct teql_master *m = (struct teql_master*)sch->ops;
187 struct teql_sched_data *q = qdisc_priv(sch);
189 if (dev->hard_header_len > m->dev->hard_header_len)
197 skb_queue_head_init(&q->q);
200 if (m->dev->flags & IFF_UP) {
201 if ((m->dev->flags&IFF_POINTOPOINT && !(dev->flags&IFF_POINTOPOINT))
202 || (m->dev->flags&IFF_BROADCAST && !(dev->flags&IFF_BROADCAST))
203 || (m->dev->flags&IFF_MULTICAST && !(dev->flags&IFF_MULTICAST))
204 || dev->mtu < m->dev->mtu)
207 if (!(dev->flags&IFF_POINTOPOINT))
208 m->dev->flags &= ~IFF_POINTOPOINT;
209 if (!(dev->flags&IFF_BROADCAST))
210 m->dev->flags &= ~IFF_BROADCAST;
211 if (!(dev->flags&IFF_MULTICAST))
212 m->dev->flags &= ~IFF_MULTICAST;
213 if (dev->mtu < m->dev->mtu)
214 m->dev->mtu = dev->mtu;
216 q->next = NEXT_SLAVE(m->slaves);
217 NEXT_SLAVE(m->slaves) = sch;
221 m->dev->mtu = dev->mtu;
222 m->dev->flags = (m->dev->flags&~FMASK)|(dev->flags&FMASK);
227 /* "teql*" netdevice routines */
230 __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, struct net_device *dev)
232 struct teql_sched_data *q = qdisc_priv(dev->qdisc);
233 struct neighbour *mn = skb->dst->neighbour;
234 struct neighbour *n = q->ncache;
238 if (n && n->tbl == mn->tbl &&
239 memcmp(n->primary_key, mn->primary_key, mn->tbl->key_len) == 0) {
240 atomic_inc(&n->refcnt);
242 n = __neigh_lookup_errno(mn->tbl, mn->primary_key, dev);
246 if (neigh_event_send(n, skb_res) == 0) {
249 err = dev->hard_header(skb, dev, ntohs(skb->protocol), n->ha, NULL, skb->len);
250 read_unlock(&n->lock);
255 teql_neigh_release(xchg(&q->ncache, n));
259 return (skb_res == NULL) ? -EAGAIN : 1;
262 static __inline__ int
263 teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, struct net_device *dev)
265 if (dev->hard_header == NULL ||
267 skb->dst->neighbour == NULL)
269 return __teql_resolve(skb, skb_res, dev);
272 static int teql_master_xmit(struct sk_buff *skb, struct net_device *dev)
274 struct teql_master *master = (void*)dev->priv;
275 struct Qdisc *start, *q;
279 struct sk_buff *skb_res = NULL;
281 start = master->slaves;
287 if ((q = start) == NULL)
291 struct net_device *slave = q->dev;
293 if (slave->qdisc_sleeping != q)
295 if (netif_queue_stopped(slave) || ! netif_running(slave)) {
300 switch (teql_resolve(skb, skb_res, slave)) {
302 if (spin_trylock(&slave->xmit_lock)) {
303 slave->xmit_lock_owner = smp_processor_id();
304 if (!netif_queue_stopped(slave) &&
305 slave->hard_start_xmit(skb, slave) == 0) {
306 slave->xmit_lock_owner = -1;
307 spin_unlock(&slave->xmit_lock);
308 master->slaves = NEXT_SLAVE(q);
309 netif_wake_queue(dev);
310 master->stats.tx_packets++;
311 master->stats.tx_bytes += len;
314 slave->xmit_lock_owner = -1;
315 spin_unlock(&slave->xmit_lock);
317 if (netif_queue_stopped(dev))
321 master->slaves = NEXT_SLAVE(q);
327 __skb_pull(skb, skb->nh.raw - skb->data);
328 } while ((q = NEXT_SLAVE(q)) != start);
330 if (nores && skb_res == NULL) {
336 netif_stop_queue(dev);
339 master->stats.tx_errors++;
342 master->stats.tx_dropped++;
347 static int teql_master_open(struct net_device *dev)
350 struct teql_master *m = (void*)dev->priv;
352 unsigned flags = IFF_NOARP|IFF_MULTICAST;
354 if (m->slaves == NULL)
361 struct net_device *slave = q->dev;
366 if (slave->mtu < mtu)
368 if (slave->hard_header_len > LL_MAX_HEADER)
371 /* If all the slaves are BROADCAST, master is BROADCAST
372 If all the slaves are PtP, master is PtP
373 Otherwise, master is NBMA.
375 if (!(slave->flags&IFF_POINTOPOINT))
376 flags &= ~IFF_POINTOPOINT;
377 if (!(slave->flags&IFF_BROADCAST))
378 flags &= ~IFF_BROADCAST;
379 if (!(slave->flags&IFF_MULTICAST))
380 flags &= ~IFF_MULTICAST;
381 } while ((q = NEXT_SLAVE(q)) != m->slaves);
384 m->dev->flags = (m->dev->flags&~FMASK) | flags;
385 netif_start_queue(m->dev);
389 static int teql_master_close(struct net_device *dev)
391 netif_stop_queue(dev);
395 static struct net_device_stats *teql_master_stats(struct net_device *dev)
397 struct teql_master *m = (void*)dev->priv;
401 static int teql_master_mtu(struct net_device *dev, int new_mtu)
403 struct teql_master *m = (void*)dev->priv;
412 if (new_mtu > q->dev->mtu)
414 } while ((q=NEXT_SLAVE(q)) != m->slaves);
421 static __init void teql_master_setup(struct net_device *dev)
423 struct teql_master *master = dev->priv;
424 struct Qdisc_ops *ops = &master->qops;
427 ops->priv_size = sizeof(struct teql_sched_data);
429 ops->enqueue = teql_enqueue;
430 ops->dequeue = teql_dequeue;
431 ops->requeue = teql_requeue;
432 ops->init = teql_qdisc_init;
433 ops->reset = teql_reset;
434 ops->destroy = teql_destroy;
435 ops->owner = THIS_MODULE;
437 dev->open = teql_master_open;
438 dev->hard_start_xmit = teql_master_xmit;
439 dev->stop = teql_master_close;
440 dev->get_stats = teql_master_stats;
441 dev->change_mtu = teql_master_mtu;
442 dev->type = ARPHRD_VOID;
444 dev->tx_queue_len = 100;
445 dev->flags = IFF_NOARP;
446 dev->hard_header_len = LL_MAX_HEADER;
447 SET_MODULE_OWNER(dev);
450 static LIST_HEAD(master_dev_list);
451 static int max_equalizers = 1;
452 MODULE_PARM(max_equalizers, "i");
453 MODULE_PARM_DESC(max_equalizers, "Max number of link equalizers");
455 static int __init teql_init(void)
460 for (i = 0; i < max_equalizers; i++) {
461 struct net_device *dev;
462 struct teql_master *master;
464 dev = alloc_netdev(sizeof(struct teql_master),
465 "teql%d", teql_master_setup);
471 if ((err = register_netdev(dev))) {
478 strlcpy(master->qops.id, dev->name, IFNAMSIZ);
479 err = register_qdisc(&master->qops);
482 unregister_netdev(dev);
487 list_add_tail(&master->master_list, &master_dev_list);
492 static void __exit teql_exit(void)
494 struct teql_master *master, *nxt;
496 list_for_each_entry_safe(master, nxt, &master_dev_list, master_list) {
498 list_del(&master->master_list);
500 unregister_qdisc(&master->qops);
501 unregister_netdev(master->dev);
502 free_netdev(master->dev);
506 module_init(teql_init);
507 module_exit(teql_exit);
509 MODULE_LICENSE("GPL");