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