xref: /openbmc/linux/net/sched/sch_teql.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /* net/sched/sch_teql.c	"True" (or "trivial") link equalizer.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/module.h>
81da177e4SLinus Torvalds #include <linux/types.h>
91da177e4SLinus Torvalds #include <linux/kernel.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/string.h>
121da177e4SLinus Torvalds #include <linux/errno.h>
1314c85021SArnaldo Carvalho de Melo #include <linux/if_arp.h>
141da177e4SLinus Torvalds #include <linux/netdevice.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/skbuff.h>
171da177e4SLinus Torvalds #include <linux/moduleparam.h>
180ba48053SPatrick McHardy #include <net/dst.h>
190ba48053SPatrick McHardy #include <net/neighbour.h>
201da177e4SLinus Torvalds #include <net/pkt_sched.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds    How to setup it.
241da177e4SLinus Torvalds    ----------------
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds    After loading this module you will find a new device teqlN
271da177e4SLinus Torvalds    and new qdisc with the same name. To join a slave to the equalizer
281da177e4SLinus Torvalds    you should just set this qdisc on a device f.e.
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds    # tc qdisc add dev eth0 root teql0
311da177e4SLinus Torvalds    # tc qdisc add dev eth1 root teql0
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds    That's all. Full PnP 8)
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds    Applicability.
361da177e4SLinus Torvalds    --------------
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds    1. Slave devices MUST be active devices, i.e., they must raise the tbusy
391da177e4SLinus Torvalds       signal and generate EOI events. If you want to equalize virtual devices
401da177e4SLinus Torvalds       like tunnels, use a normal eql device.
411da177e4SLinus Torvalds    2. This device puts no limitations on physical slave characteristics
421da177e4SLinus Torvalds       f.e. it will equalize 9600baud line and 100Mb ethernet perfectly :-)
431da177e4SLinus Torvalds       Certainly, large difference in link speeds will make the resulting
441da177e4SLinus Torvalds       eqalized link unusable, because of huge packet reordering.
451da177e4SLinus Torvalds       I estimate an upper useful difference as ~10 times.
461da177e4SLinus Torvalds    3. If the slave requires address resolution, only protocols using
471da177e4SLinus Torvalds       neighbour cache (IPv4/IPv6) will work over the equalized link.
481da177e4SLinus Torvalds       Other protocols are still allowed to use the slave device directly,
491da177e4SLinus Torvalds       which will not break load balancing, though native slave
501da177e4SLinus Torvalds       traffic will have the highest priority.  */
511da177e4SLinus Torvalds 
52cc7ec456SEric Dumazet struct teql_master {
531da177e4SLinus Torvalds 	struct Qdisc_ops qops;
541da177e4SLinus Torvalds 	struct net_device *dev;
551da177e4SLinus Torvalds 	struct Qdisc *slaves;
561da177e4SLinus Torvalds 	struct list_head master_list;
571ac9ad13SEric Dumazet 	unsigned long	tx_bytes;
581ac9ad13SEric Dumazet 	unsigned long	tx_packets;
591ac9ad13SEric Dumazet 	unsigned long	tx_errors;
601ac9ad13SEric Dumazet 	unsigned long	tx_dropped;
611da177e4SLinus Torvalds };
621da177e4SLinus Torvalds 
63cc7ec456SEric Dumazet struct teql_sched_data {
641da177e4SLinus Torvalds 	struct Qdisc *next;
651da177e4SLinus Torvalds 	struct teql_master *m;
661da177e4SLinus Torvalds 	struct sk_buff_head q;
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define NEXT_SLAVE(q) (((struct teql_sched_data *)qdisc_priv(q))->next)
701da177e4SLinus Torvalds 
71cc8fd14dSRoel Kluin #define FMASK (IFF_BROADCAST | IFF_POINTOPOINT)
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* "teql*" qdisc routines */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static int
teql_enqueue(struct sk_buff * skb,struct Qdisc * sch,struct sk_buff ** to_free)76520ac30fSEric Dumazet teql_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
771da177e4SLinus Torvalds {
785ce2d488SDavid S. Miller 	struct net_device *dev = qdisc_dev(sch);
791da177e4SLinus Torvalds 	struct teql_sched_data *q = qdisc_priv(sch);
801da177e4SLinus Torvalds 
814cd8c9e8SKrishna Kumar 	if (q->q.qlen < dev->tx_queue_len) {
821da177e4SLinus Torvalds 		__skb_queue_tail(&q->q, skb);
839871e50eSBen Greear 		return NET_XMIT_SUCCESS;
841da177e4SLinus Torvalds 	}
851da177e4SLinus Torvalds 
86520ac30fSEric Dumazet 	return qdisc_drop(skb, sch, to_free);
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds static struct sk_buff *
teql_dequeue(struct Qdisc * sch)901da177e4SLinus Torvalds teql_dequeue(struct Qdisc *sch)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	struct teql_sched_data *dat = qdisc_priv(sch);
93b0e1e646SDavid S. Miller 	struct netdev_queue *dat_queue;
941da177e4SLinus Torvalds 	struct sk_buff *skb;
9546e5da40SJohn Fastabend 	struct Qdisc *q;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	skb = __skb_dequeue(&dat->q);
98e8a0464cSDavid S. Miller 	dat_queue = netdev_get_tx_queue(dat->m->dev, 0);
9946e5da40SJohn Fastabend 	q = rcu_dereference_bh(dat_queue->qdisc);
10046e5da40SJohn Fastabend 
1011da177e4SLinus Torvalds 	if (skb == NULL) {
10246e5da40SJohn Fastabend 		struct net_device *m = qdisc_dev(q);
1031da177e4SLinus Torvalds 		if (m) {
1041da177e4SLinus Torvalds 			dat->m->slaves = sch;
1051da177e4SLinus Torvalds 			netif_wake_queue(m);
1061da177e4SLinus Torvalds 		}
1079190b3b3SEric Dumazet 	} else {
1089190b3b3SEric Dumazet 		qdisc_bstats_update(sch, skb);
1091da177e4SLinus Torvalds 	}
11046e5da40SJohn Fastabend 	sch->q.qlen = dat->q.qlen + q->q.qlen;
1111da177e4SLinus Torvalds 	return skb;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1148e3af978SJarek Poplawski static struct sk_buff *
teql_peek(struct Qdisc * sch)1158e3af978SJarek Poplawski teql_peek(struct Qdisc *sch)
1168e3af978SJarek Poplawski {
1178e3af978SJarek Poplawski 	/* teql is meant to be used as root qdisc */
1188e3af978SJarek Poplawski 	return NULL;
1198e3af978SJarek Poplawski }
1208e3af978SJarek Poplawski 
1211da177e4SLinus Torvalds static void
teql_reset(struct Qdisc * sch)1221da177e4SLinus Torvalds teql_reset(struct Qdisc *sch)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	struct teql_sched_data *dat = qdisc_priv(sch);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	skb_queue_purge(&dat->q);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static void
teql_destroy(struct Qdisc * sch)1301da177e4SLinus Torvalds teql_destroy(struct Qdisc *sch)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	struct Qdisc *q, *prev;
1331da177e4SLinus Torvalds 	struct teql_sched_data *dat = qdisc_priv(sch);
1341da177e4SLinus Torvalds 	struct teql_master *master = dat->m;
1351da177e4SLinus Torvalds 
1361ffbc7eaSPavel Tikhomirov 	if (!master)
1371ffbc7eaSPavel Tikhomirov 		return;
1381ffbc7eaSPavel Tikhomirov 
139cc7ec456SEric Dumazet 	prev = master->slaves;
140cc7ec456SEric Dumazet 	if (prev) {
1411da177e4SLinus Torvalds 		do {
1421da177e4SLinus Torvalds 			q = NEXT_SLAVE(prev);
1431da177e4SLinus Torvalds 			if (q == sch) {
1441da177e4SLinus Torvalds 				NEXT_SLAVE(prev) = NEXT_SLAVE(q);
1451da177e4SLinus Torvalds 				if (q == master->slaves) {
1461da177e4SLinus Torvalds 					master->slaves = NEXT_SLAVE(q);
1471da177e4SLinus Torvalds 					if (q == master->slaves) {
148e8a0464cSDavid S. Miller 						struct netdev_queue *txq;
14983874000SDavid S. Miller 						spinlock_t *root_lock;
150e8a0464cSDavid S. Miller 
151e8a0464cSDavid S. Miller 						txq = netdev_get_tx_queue(master->dev, 0);
1521da177e4SLinus Torvalds 						master->slaves = NULL;
15383874000SDavid S. Miller 
15446e5da40SJohn Fastabend 						root_lock = qdisc_root_sleeping_lock(rtnl_dereference(txq->qdisc));
15583874000SDavid S. Miller 						spin_lock_bh(root_lock);
15646e5da40SJohn Fastabend 						qdisc_reset(rtnl_dereference(txq->qdisc));
15783874000SDavid S. Miller 						spin_unlock_bh(root_lock);
1581da177e4SLinus Torvalds 					}
1591da177e4SLinus Torvalds 				}
1601da177e4SLinus Torvalds 				skb_queue_purge(&dat->q);
1611da177e4SLinus Torvalds 				break;
1621da177e4SLinus Torvalds 			}
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 		} while ((prev = q) != master->slaves);
1651da177e4SLinus Torvalds 	}
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
teql_qdisc_init(struct Qdisc * sch,struct nlattr * opt,struct netlink_ext_ack * extack)168e63d7dfdSAlexander Aring static int teql_qdisc_init(struct Qdisc *sch, struct nlattr *opt,
169e63d7dfdSAlexander Aring 			   struct netlink_ext_ack *extack)
1701da177e4SLinus Torvalds {
1715ce2d488SDavid S. Miller 	struct net_device *dev = qdisc_dev(sch);
1721da177e4SLinus Torvalds 	struct teql_master *m = (struct teql_master *)sch->ops;
1731da177e4SLinus Torvalds 	struct teql_sched_data *q = qdisc_priv(sch);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	if (dev->hard_header_len > m->dev->hard_header_len)
1761da177e4SLinus Torvalds 		return -EINVAL;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (m->dev == dev)
1791da177e4SLinus Torvalds 		return -ELOOP;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	q->m = m;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	skb_queue_head_init(&q->q);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	if (m->slaves) {
1861da177e4SLinus Torvalds 		if (m->dev->flags & IFF_UP) {
187f64f9e71SJoe Perches 			if ((m->dev->flags & IFF_POINTOPOINT &&
188f64f9e71SJoe Perches 			     !(dev->flags & IFF_POINTOPOINT)) ||
189f64f9e71SJoe Perches 			    (m->dev->flags & IFF_BROADCAST &&
190f64f9e71SJoe Perches 			     !(dev->flags & IFF_BROADCAST)) ||
191f64f9e71SJoe Perches 			    (m->dev->flags & IFF_MULTICAST &&
192f64f9e71SJoe Perches 			     !(dev->flags & IFF_MULTICAST)) ||
193f64f9e71SJoe Perches 			    dev->mtu < m->dev->mtu)
1941da177e4SLinus Torvalds 				return -EINVAL;
1951da177e4SLinus Torvalds 		} else {
1961da177e4SLinus Torvalds 			if (!(dev->flags&IFF_POINTOPOINT))
1971da177e4SLinus Torvalds 				m->dev->flags &= ~IFF_POINTOPOINT;
1981da177e4SLinus Torvalds 			if (!(dev->flags&IFF_BROADCAST))
1991da177e4SLinus Torvalds 				m->dev->flags &= ~IFF_BROADCAST;
2001da177e4SLinus Torvalds 			if (!(dev->flags&IFF_MULTICAST))
2011da177e4SLinus Torvalds 				m->dev->flags &= ~IFF_MULTICAST;
2021da177e4SLinus Torvalds 			if (dev->mtu < m->dev->mtu)
2031da177e4SLinus Torvalds 				m->dev->mtu = dev->mtu;
2041da177e4SLinus Torvalds 		}
2051da177e4SLinus Torvalds 		q->next = NEXT_SLAVE(m->slaves);
2061da177e4SLinus Torvalds 		NEXT_SLAVE(m->slaves) = sch;
2071da177e4SLinus Torvalds 	} else {
2081da177e4SLinus Torvalds 		q->next = sch;
2091da177e4SLinus Torvalds 		m->slaves = sch;
2101da177e4SLinus Torvalds 		m->dev->mtu = dev->mtu;
2111da177e4SLinus Torvalds 		m->dev->flags = (m->dev->flags&~FMASK)|(dev->flags&FMASK);
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 	return 0;
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static int
__teql_resolve(struct sk_buff * skb,struct sk_buff * skb_res,struct net_device * dev,struct netdev_queue * txq,struct dst_entry * dst)218f7e57044SEric Dumazet __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res,
219f7e57044SEric Dumazet 	       struct net_device *dev, struct netdev_queue *txq,
220dbedbe6dSDavid S. Miller 	       struct dst_entry *dst)
2211da177e4SLinus Torvalds {
222dbedbe6dSDavid S. Miller 	struct neighbour *n;
223dbedbe6dSDavid S. Miller 	int err = 0;
2241da177e4SLinus Torvalds 
225dbedbe6dSDavid S. Miller 	n = dst_neigh_lookup_skb(dst, skb);
226dbedbe6dSDavid S. Miller 	if (!n)
227dbedbe6dSDavid S. Miller 		return -ENOENT;
228dbedbe6dSDavid S. Miller 
229dbedbe6dSDavid S. Miller 	if (dst->dev != dev) {
230dbedbe6dSDavid S. Miller 		struct neighbour *mn;
231dbedbe6dSDavid S. Miller 
232dbedbe6dSDavid S. Miller 		mn = __neigh_lookup_errno(n->tbl, n->primary_key, dev);
233dbedbe6dSDavid S. Miller 		neigh_release(n);
234dbedbe6dSDavid S. Miller 		if (IS_ERR(mn))
235dbedbe6dSDavid S. Miller 			return PTR_ERR(mn);
236dbedbe6dSDavid S. Miller 		n = mn;
2371da177e4SLinus Torvalds 	}
238dbedbe6dSDavid S. Miller 
2391da177e4SLinus Torvalds 	if (neigh_event_send(n, skb_res) == 0) {
2401da177e4SLinus Torvalds 		int err;
2410ed8ddf4SEric Dumazet 		char haddr[MAX_ADDR_LEN];
2420c4e8581SStephen Hemminger 
2430ed8ddf4SEric Dumazet 		neigh_ha_snapshot(haddr, n, dev);
244d7bf2ebeSToke Høiland-Jørgensen 		err = dev_hard_header(skb, dev, ntohs(skb_protocol(skb, false)),
245d8b9605dSJiri Pirko 				      haddr, NULL, skb->len);
2460c4e8581SStephen Hemminger 
247dbedbe6dSDavid S. Miller 		if (err < 0)
248dbedbe6dSDavid S. Miller 			err = -EINVAL;
249dbedbe6dSDavid S. Miller 	} else {
250dbedbe6dSDavid S. Miller 		err = (skb_res == NULL) ? -EAGAIN : 1;
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds 	neigh_release(n);
253dbedbe6dSDavid S. Miller 	return err;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
teql_resolve(struct sk_buff * skb,struct sk_buff * skb_res,struct net_device * dev,struct netdev_queue * txq)2563b04dddeSStephen Hemminger static inline int teql_resolve(struct sk_buff *skb,
257f7e57044SEric Dumazet 			       struct sk_buff *skb_res,
258f7e57044SEric Dumazet 			       struct net_device *dev,
259f7e57044SEric Dumazet 			       struct netdev_queue *txq)
2601da177e4SLinus Torvalds {
261f7e57044SEric Dumazet 	struct dst_entry *dst = skb_dst(skb);
262f7e57044SEric Dumazet 	int res;
263f7e57044SEric Dumazet 
26446e5da40SJohn Fastabend 	if (rcu_access_pointer(txq->qdisc) == &noop_qdisc)
2654f9f8311SEvgeniy Polyakov 		return -ENODEV;
2664f9f8311SEvgeniy Polyakov 
267f7e57044SEric Dumazet 	if (!dev->header_ops || !dst)
2681da177e4SLinus Torvalds 		return 0;
269f7e57044SEric Dumazet 
270f7e57044SEric Dumazet 	rcu_read_lock();
271dbedbe6dSDavid S. Miller 	res = __teql_resolve(skb, skb_res, dev, txq, dst);
272f7e57044SEric Dumazet 	rcu_read_unlock();
273f7e57044SEric Dumazet 
274f7e57044SEric Dumazet 	return res;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
teql_master_xmit(struct sk_buff * skb,struct net_device * dev)2776fef4c0cSStephen Hemminger static netdev_tx_t teql_master_xmit(struct sk_buff *skb, struct net_device *dev)
2781da177e4SLinus Torvalds {
2792941a486SPatrick McHardy 	struct teql_master *master = netdev_priv(dev);
2801da177e4SLinus Torvalds 	struct Qdisc *start, *q;
2811da177e4SLinus Torvalds 	int busy;
2821da177e4SLinus Torvalds 	int nores;
2834e3ab47aSPavel Emelyanov 	int subq = skb_get_queue_mapping(skb);
2841da177e4SLinus Torvalds 	struct sk_buff *skb_res = NULL;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	start = master->slaves;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds restart:
2891da177e4SLinus Torvalds 	nores = 0;
2901da177e4SLinus Torvalds 	busy = 0;
2911da177e4SLinus Torvalds 
292cc7ec456SEric Dumazet 	q = start;
293cc7ec456SEric Dumazet 	if (!q)
2941da177e4SLinus Torvalds 		goto drop;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	do {
2975ce2d488SDavid S. Miller 		struct net_device *slave = qdisc_dev(q);
29861294e2eSStephen Hemminger 		struct netdev_queue *slave_txq = netdev_get_tx_queue(slave, 0);
2991da177e4SLinus Torvalds 
300*d636fc5dSEric Dumazet 		if (rcu_access_pointer(slave_txq->qdisc_sleeping) != q)
3011da177e4SLinus Torvalds 			continue;
30273466498STom Herbert 		if (netif_xmit_stopped(netdev_get_tx_queue(slave, subq)) ||
303f25f4e44SPeter P Waskiewicz Jr 		    !netif_running(slave)) {
3041da177e4SLinus Torvalds 			busy = 1;
3051da177e4SLinus Torvalds 			continue;
3061da177e4SLinus Torvalds 		}
3071da177e4SLinus Torvalds 
308f7e57044SEric Dumazet 		switch (teql_resolve(skb, skb_res, slave, slave_txq)) {
3091da177e4SLinus Torvalds 		case 0:
310c3f26a26SDavid S. Miller 			if (__netif_tx_trylock(slave_txq)) {
311c0f84d0dSEric Dumazet 				unsigned int length = qdisc_pkt_len(skb);
312c0f84d0dSEric Dumazet 
31373466498STom Herbert 				if (!netif_xmit_frozen_or_stopped(slave_txq) &&
314fa2dbdc2SDavid S. Miller 				    netdev_start_xmit(skb, slave, slave_txq, false) ==
315fa2dbdc2SDavid S. Miller 				    NETDEV_TX_OK) {
316c3f26a26SDavid S. Miller 					__netif_tx_unlock(slave_txq);
3171da177e4SLinus Torvalds 					master->slaves = NEXT_SLAVE(q);
3181da177e4SLinus Torvalds 					netif_wake_queue(dev);
3191ac9ad13SEric Dumazet 					master->tx_packets++;
3201ac9ad13SEric Dumazet 					master->tx_bytes += length;
3216ed10654SPatrick McHardy 					return NETDEV_TX_OK;
3221da177e4SLinus Torvalds 				}
323c3f26a26SDavid S. Miller 				__netif_tx_unlock(slave_txq);
3241da177e4SLinus Torvalds 			}
32573466498STom Herbert 			if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)))
3261da177e4SLinus Torvalds 				busy = 1;
3271da177e4SLinus Torvalds 			break;
3281da177e4SLinus Torvalds 		case 1:
3291da177e4SLinus Torvalds 			master->slaves = NEXT_SLAVE(q);
3306ed10654SPatrick McHardy 			return NETDEV_TX_OK;
3311da177e4SLinus Torvalds 		default:
3321da177e4SLinus Torvalds 			nores = 1;
3331da177e4SLinus Torvalds 			break;
3341da177e4SLinus Torvalds 		}
335bbe735e4SArnaldo Carvalho de Melo 		__skb_pull(skb, skb_network_offset(skb));
3361da177e4SLinus Torvalds 	} while ((q = NEXT_SLAVE(q)) != start);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (nores && skb_res == NULL) {
3391da177e4SLinus Torvalds 		skb_res = skb;
3401da177e4SLinus Torvalds 		goto restart;
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (busy) {
3441da177e4SLinus Torvalds 		netif_stop_queue(dev);
3455b548140SPatrick McHardy 		return NETDEV_TX_BUSY;
3461da177e4SLinus Torvalds 	}
3471ac9ad13SEric Dumazet 	master->tx_errors++;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds drop:
3501ac9ad13SEric Dumazet 	master->tx_dropped++;
3511da177e4SLinus Torvalds 	dev_kfree_skb(skb);
3526ed10654SPatrick McHardy 	return NETDEV_TX_OK;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
teql_master_open(struct net_device * dev)3551da177e4SLinus Torvalds static int teql_master_open(struct net_device *dev)
3561da177e4SLinus Torvalds {
3571da177e4SLinus Torvalds 	struct Qdisc *q;
3582941a486SPatrick McHardy 	struct teql_master *m = netdev_priv(dev);
3591da177e4SLinus Torvalds 	int mtu = 0xFFFE;
360cc7ec456SEric Dumazet 	unsigned int flags = IFF_NOARP | IFF_MULTICAST;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	if (m->slaves == NULL)
3631da177e4SLinus Torvalds 		return -EUNATCH;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	flags = FMASK;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	q = m->slaves;
3681da177e4SLinus Torvalds 	do {
3695ce2d488SDavid S. Miller 		struct net_device *slave = qdisc_dev(q);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		if (slave == NULL)
3721da177e4SLinus Torvalds 			return -EUNATCH;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		if (slave->mtu < mtu)
3751da177e4SLinus Torvalds 			mtu = slave->mtu;
3761da177e4SLinus Torvalds 		if (slave->hard_header_len > LL_MAX_HEADER)
3771da177e4SLinus Torvalds 			return -EINVAL;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		/* If all the slaves are BROADCAST, master is BROADCAST
3801da177e4SLinus Torvalds 		   If all the slaves are PtP, master is PtP
3811da177e4SLinus Torvalds 		   Otherwise, master is NBMA.
3821da177e4SLinus Torvalds 		 */
3831da177e4SLinus Torvalds 		if (!(slave->flags&IFF_POINTOPOINT))
3841da177e4SLinus Torvalds 			flags &= ~IFF_POINTOPOINT;
3851da177e4SLinus Torvalds 		if (!(slave->flags&IFF_BROADCAST))
3861da177e4SLinus Torvalds 			flags &= ~IFF_BROADCAST;
3871da177e4SLinus Torvalds 		if (!(slave->flags&IFF_MULTICAST))
3881da177e4SLinus Torvalds 			flags &= ~IFF_MULTICAST;
3891da177e4SLinus Torvalds 	} while ((q = NEXT_SLAVE(q)) != m->slaves);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	m->dev->mtu = mtu;
3921da177e4SLinus Torvalds 	m->dev->flags = (m->dev->flags&~FMASK) | flags;
3931da177e4SLinus Torvalds 	netif_start_queue(m->dev);
3941da177e4SLinus Torvalds 	return 0;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
teql_master_close(struct net_device * dev)3971da177e4SLinus Torvalds static int teql_master_close(struct net_device *dev)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	netif_stop_queue(dev);
4001da177e4SLinus Torvalds 	return 0;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
teql_master_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)403bc1f4470Sstephen hemminger static void teql_master_stats64(struct net_device *dev,
4041ac9ad13SEric Dumazet 				struct rtnl_link_stats64 *stats)
4051ac9ad13SEric Dumazet {
4061ac9ad13SEric Dumazet 	struct teql_master *m = netdev_priv(dev);
4071ac9ad13SEric Dumazet 
4081ac9ad13SEric Dumazet 	stats->tx_packets	= m->tx_packets;
4091ac9ad13SEric Dumazet 	stats->tx_bytes		= m->tx_bytes;
4101ac9ad13SEric Dumazet 	stats->tx_errors	= m->tx_errors;
4111ac9ad13SEric Dumazet 	stats->tx_dropped	= m->tx_dropped;
4121ac9ad13SEric Dumazet }
4131ac9ad13SEric Dumazet 
teql_master_mtu(struct net_device * dev,int new_mtu)4141da177e4SLinus Torvalds static int teql_master_mtu(struct net_device *dev, int new_mtu)
4151da177e4SLinus Torvalds {
4162941a486SPatrick McHardy 	struct teql_master *m = netdev_priv(dev);
4171da177e4SLinus Torvalds 	struct Qdisc *q;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	q = m->slaves;
4201da177e4SLinus Torvalds 	if (q) {
4211da177e4SLinus Torvalds 		do {
4225ce2d488SDavid S. Miller 			if (new_mtu > qdisc_dev(q)->mtu)
4231da177e4SLinus Torvalds 				return -EINVAL;
4241da177e4SLinus Torvalds 		} while ((q = NEXT_SLAVE(q)) != m->slaves);
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	dev->mtu = new_mtu;
4281da177e4SLinus Torvalds 	return 0;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
43161294e2eSStephen Hemminger static const struct net_device_ops teql_netdev_ops = {
43261294e2eSStephen Hemminger 	.ndo_open	= teql_master_open,
43361294e2eSStephen Hemminger 	.ndo_stop	= teql_master_close,
43461294e2eSStephen Hemminger 	.ndo_start_xmit	= teql_master_xmit,
4351ac9ad13SEric Dumazet 	.ndo_get_stats64 = teql_master_stats64,
43661294e2eSStephen Hemminger 	.ndo_change_mtu	= teql_master_mtu,
43761294e2eSStephen Hemminger };
43861294e2eSStephen Hemminger 
teql_master_setup(struct net_device * dev)4391da177e4SLinus Torvalds static __init void teql_master_setup(struct net_device *dev)
4401da177e4SLinus Torvalds {
4412941a486SPatrick McHardy 	struct teql_master *master = netdev_priv(dev);
4421da177e4SLinus Torvalds 	struct Qdisc_ops *ops = &master->qops;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	master->dev	= dev;
4451da177e4SLinus Torvalds 	ops->priv_size  = sizeof(struct teql_sched_data);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	ops->enqueue	=	teql_enqueue;
4481da177e4SLinus Torvalds 	ops->dequeue	=	teql_dequeue;
4498e3af978SJarek Poplawski 	ops->peek	=	teql_peek;
4501da177e4SLinus Torvalds 	ops->init	=	teql_qdisc_init;
4511da177e4SLinus Torvalds 	ops->reset	=	teql_reset;
4521da177e4SLinus Torvalds 	ops->destroy	=	teql_destroy;
4531da177e4SLinus Torvalds 	ops->owner	=	THIS_MODULE;
4541da177e4SLinus Torvalds 
45561294e2eSStephen Hemminger 	dev->netdev_ops =       &teql_netdev_ops;
4561da177e4SLinus Torvalds 	dev->type		= ARPHRD_VOID;
4571da177e4SLinus Torvalds 	dev->mtu		= 1500;
45891572088SJarod Wilson 	dev->min_mtu		= 68;
45991572088SJarod Wilson 	dev->max_mtu		= 65535;
4601da177e4SLinus Torvalds 	dev->tx_queue_len	= 100;
4611da177e4SLinus Torvalds 	dev->flags		= IFF_NOARP;
4621da177e4SLinus Torvalds 	dev->hard_header_len	= LL_MAX_HEADER;
46302875878SEric Dumazet 	netif_keep_dst(dev);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds static LIST_HEAD(master_dev_list);
4671da177e4SLinus Torvalds static int max_equalizers = 1;
4681da177e4SLinus Torvalds module_param(max_equalizers, int, 0);
4691da177e4SLinus Torvalds MODULE_PARM_DESC(max_equalizers, "Max number of link equalizers");
4701da177e4SLinus Torvalds 
teql_init(void)4711da177e4SLinus Torvalds static int __init teql_init(void)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	int i;
4741da177e4SLinus Torvalds 	int err = -ENODEV;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	for (i = 0; i < max_equalizers; i++) {
4771da177e4SLinus Torvalds 		struct net_device *dev;
4781da177e4SLinus Torvalds 		struct teql_master *master;
4791da177e4SLinus Torvalds 
480c835a677STom Gundersen 		dev = alloc_netdev(sizeof(struct teql_master), "teql%d",
481c835a677STom Gundersen 				   NET_NAME_UNKNOWN, teql_master_setup);
4821da177e4SLinus Torvalds 		if (!dev) {
4831da177e4SLinus Torvalds 			err = -ENOMEM;
4841da177e4SLinus Torvalds 			break;
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 		if ((err = register_netdev(dev))) {
4881da177e4SLinus Torvalds 			free_netdev(dev);
4891da177e4SLinus Torvalds 			break;
4901da177e4SLinus Torvalds 		}
4911da177e4SLinus Torvalds 
4922941a486SPatrick McHardy 		master = netdev_priv(dev);
4931da177e4SLinus Torvalds 
49492f24c6fSWolfram Sang 		strscpy(master->qops.id, dev->name, IFNAMSIZ);
4951da177e4SLinus Torvalds 		err = register_qdisc(&master->qops);
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 		if (err) {
4981da177e4SLinus Torvalds 			unregister_netdev(dev);
4991da177e4SLinus Torvalds 			free_netdev(dev);
5001da177e4SLinus Torvalds 			break;
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 		list_add_tail(&master->master_list, &master_dev_list);
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 	return i ? 0 : err;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
teql_exit(void)5081da177e4SLinus Torvalds static void __exit teql_exit(void)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	struct teql_master *master, *nxt;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	list_for_each_entry_safe(master, nxt, &master_dev_list, master_list) {
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 		list_del(&master->master_list);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		unregister_qdisc(&master->qops);
5171da177e4SLinus Torvalds 		unregister_netdev(master->dev);
5181da177e4SLinus Torvalds 		free_netdev(master->dev);
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds module_init(teql_init);
5231da177e4SLinus Torvalds module_exit(teql_exit);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
526