ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sched / sch_teql.c
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>
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>
19 #include <linux/mm.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
22 #include <linux/in.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>
31 #include <net/ip.h>
32 #include <net/route.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <net/pkt_sched.h>
36
37 /*
38    How to setup it.
39    ----------------
40
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.
44
45    # tc qdisc add dev eth0 root teql0
46    # tc qdisc add dev eth1 root teql0
47
48    That's all. Full PnP 8)
49
50    Applicability.
51    --------------
52
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.  */
66
67 struct teql_master
68 {
69         struct Qdisc_ops qops;
70         struct net_device *dev;
71         struct Qdisc *slaves;
72         struct list_head master_list;
73         struct net_device_stats stats;
74 };
75
76 struct teql_sched_data
77 {
78         struct Qdisc *next;
79         struct teql_master *m;
80         struct neighbour *ncache;
81         struct sk_buff_head q;
82 };
83
84 #define NEXT_SLAVE(q) (((struct teql_sched_data*)((q)->data))->next)
85
86 #define FMASK (IFF_BROADCAST|IFF_POINTOPOINT|IFF_BROADCAST)
87
88 /* "teql*" qdisc routines */
89
90 static int
91 teql_enqueue(struct sk_buff *skb, struct Qdisc* sch)
92 {
93         struct net_device *dev = sch->dev;
94         struct teql_sched_data *q = (struct teql_sched_data *)sch->data;
95
96         __skb_queue_tail(&q->q, skb);
97         if (q->q.qlen <= dev->tx_queue_len) {
98                 sch->stats.bytes += skb->len;
99                 sch->stats.packets++;
100                 return 0;
101         }
102
103         __skb_unlink(skb, &q->q);
104         kfree_skb(skb);
105         sch->stats.drops++;
106         return NET_XMIT_DROP;
107 }
108
109 static int
110 teql_requeue(struct sk_buff *skb, struct Qdisc* sch)
111 {
112         struct teql_sched_data *q = (struct teql_sched_data *)sch->data;
113
114         __skb_queue_head(&q->q, skb);
115         return 0;
116 }
117
118 static struct sk_buff *
119 teql_dequeue(struct Qdisc* sch)
120 {
121         struct teql_sched_data *dat = (struct teql_sched_data *)sch->data;
122         struct sk_buff *skb;
123
124         skb = __skb_dequeue(&dat->q);
125         if (skb == NULL) {
126                 struct net_device *m = dat->m->dev->qdisc->dev;
127                 if (m) {
128                         dat->m->slaves = sch;
129                         netif_wake_queue(m);
130                 }
131         }
132         sch->q.qlen = dat->q.qlen + dat->m->dev->qdisc->q.qlen;
133         return skb;
134 }
135
136 static __inline__ void
137 teql_neigh_release(struct neighbour *n)
138 {
139         if (n)
140                 neigh_release(n);
141 }
142
143 static void
144 teql_reset(struct Qdisc* sch)
145 {
146         struct teql_sched_data *dat = (struct teql_sched_data *)sch->data;
147
148         skb_queue_purge(&dat->q);
149         sch->q.qlen = 0;
150         teql_neigh_release(xchg(&dat->ncache, NULL));
151 }
152
153 static void
154 teql_destroy(struct Qdisc* sch)
155 {
156         struct Qdisc *q, *prev;
157         struct teql_sched_data *dat = (struct teql_sched_data *)sch->data;
158         struct teql_master *master = dat->m;
159
160         if ((prev = master->slaves) != NULL) {
161                 do {
162                         q = NEXT_SLAVE(prev);
163                         if (q == sch) {
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);
172                                         }
173                                 }
174                                 skb_queue_purge(&dat->q);
175                                 teql_neigh_release(xchg(&dat->ncache, NULL));
176                                 break;
177                         }
178                                 
179                 } while ((prev = q) != master->slaves);
180         }
181 }
182
183 static int teql_qdisc_init(struct Qdisc *sch, struct rtattr *opt)
184 {
185         struct net_device *dev = sch->dev;
186         struct teql_master *m = (struct teql_master*)sch->ops;
187         struct teql_sched_data *q = (struct teql_sched_data *)sch->data;
188
189         if (dev->hard_header_len > m->dev->hard_header_len)
190                 return -EINVAL;
191
192         if (m->dev == dev)
193                 return -ELOOP;
194
195         q->m = m;
196
197         skb_queue_head_init(&q->q);
198
199         if (m->slaves) {
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)
205                                 return -EINVAL;
206                 } else {
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;
215                 }
216                 q->next = NEXT_SLAVE(m->slaves);
217                 NEXT_SLAVE(m->slaves) = sch;
218         } else {
219                 q->next = sch;
220                 m->slaves = sch;
221                 m->dev->mtu = dev->mtu;
222                 m->dev->flags = (m->dev->flags&~FMASK)|(dev->flags&FMASK);
223         }
224         return 0;
225 }
226
227 /* "teql*" netdevice routines */
228
229 static int
230 __teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, struct net_device *dev)
231 {
232         struct teql_sched_data *q = (void*)dev->qdisc->data;
233         struct neighbour *mn = skb->dst->neighbour;
234         struct neighbour *n = q->ncache;
235
236         if (mn->tbl == NULL)
237                 return -EINVAL;
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);
241         } else {
242                 n = __neigh_lookup_errno(mn->tbl, mn->primary_key, dev);
243                 if (IS_ERR(n))
244                         return PTR_ERR(n);
245         }
246         if (neigh_event_send(n, skb_res) == 0) {
247                 int err;
248                 read_lock(&n->lock);
249                 err = dev->hard_header(skb, dev, ntohs(skb->protocol), n->ha, NULL, skb->len);
250                 read_unlock(&n->lock);
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
262 static __inline__ int
263 teql_resolve(struct sk_buff *skb, struct sk_buff *skb_res, struct net_device *dev)
264 {
265         if (dev->hard_header == NULL ||
266             skb->dst == NULL ||
267             skb->dst->neighbour == NULL)
268                 return 0;
269         return __teql_resolve(skb, skb_res, dev);
270 }
271
272 static int teql_master_xmit(struct sk_buff *skb, struct net_device *dev)
273 {
274         struct teql_master *master = (void*)dev->priv;
275         struct Qdisc *start, *q;
276         int busy;
277         int nores;
278         int len = skb->len;
279         struct sk_buff *skb_res = NULL;
280
281         start = master->slaves;
282
283 restart:
284         nores = 0;
285         busy = 0;
286
287         if ((q = start) == NULL)
288                 goto drop;
289
290         do {
291                 struct net_device *slave = q->dev;
292                 
293                 if (slave->qdisc_sleeping != q)
294                         continue;
295                 if (netif_queue_stopped(slave) || ! netif_running(slave)) {
296                         busy = 1;
297                         continue;
298                 }
299
300                 switch (teql_resolve(skb, skb_res, slave)) {
301                 case 0:
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;
312                                         return 0;
313                                 }
314                                 slave->xmit_lock_owner = -1;
315                                 spin_unlock(&slave->xmit_lock);
316                         }
317                         if (netif_queue_stopped(dev))
318                                 busy = 1;
319                         break;
320                 case 1:
321                         master->slaves = NEXT_SLAVE(q);
322                         return 0;
323                 default:
324                         nores = 1;
325                         break;
326                 }
327                 __skb_pull(skb, skb->nh.raw - skb->data);
328         } while ((q = NEXT_SLAVE(q)) != start);
329
330         if (nores && skb_res == NULL) {
331                 skb_res = skb;
332                 goto restart;
333         }
334
335         if (busy) {
336                 netif_stop_queue(dev);
337                 return 1;
338         }
339         master->stats.tx_errors++;
340
341 drop:
342         master->stats.tx_dropped++;
343         dev_kfree_skb(skb);
344         return 0;
345 }
346
347 static int teql_master_open(struct net_device *dev)
348 {
349         struct Qdisc * q;
350         struct teql_master *m = (void*)dev->priv;
351         int mtu = 0xFFFE;
352         unsigned flags = IFF_NOARP|IFF_MULTICAST;
353
354         if (m->slaves == NULL)
355                 return -EUNATCH;
356
357         flags = FMASK;
358
359         q = m->slaves;
360         do {
361                 struct net_device *slave = q->dev;
362
363                 if (slave == NULL)
364                         return -EUNATCH;
365
366                 if (slave->mtu < mtu)
367                         mtu = slave->mtu;
368                 if (slave->hard_header_len > LL_MAX_HEADER)
369                         return -EINVAL;
370
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.
374                  */
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);
382
383         m->dev->mtu = mtu;
384         m->dev->flags = (m->dev->flags&~FMASK) | flags;
385         netif_start_queue(m->dev);
386         return 0;
387 }
388
389 static int teql_master_close(struct net_device *dev)
390 {
391         netif_stop_queue(dev);
392         return 0;
393 }
394
395 static struct net_device_stats *teql_master_stats(struct net_device *dev)
396 {
397         struct teql_master *m = (void*)dev->priv;
398         return &m->stats;
399 }
400
401 static int teql_master_mtu(struct net_device *dev, int new_mtu)
402 {
403         struct teql_master *m = (void*)dev->priv;
404         struct Qdisc *q;
405
406         if (new_mtu < 68)
407                 return -EINVAL;
408
409         q = m->slaves;
410         if (q) {
411                 do {
412                         if (new_mtu > q->dev->mtu)
413                                 return -EINVAL;
414                 } while ((q=NEXT_SLAVE(q)) != m->slaves);
415         }
416
417         dev->mtu = new_mtu;
418         return 0;
419 }
420
421 static __init void teql_master_setup(struct net_device *dev)
422 {
423         struct teql_master *master = dev->priv;
424         struct Qdisc_ops *ops = &master->qops;
425
426         master->dev     = dev;
427         ops->priv_size  = sizeof(struct teql_sched_data);
428         
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;
436
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;
443         dev->mtu                = 1500;
444         dev->tx_queue_len       = 100;
445         dev->flags              = IFF_NOARP;
446         dev->hard_header_len    = LL_MAX_HEADER;
447         SET_MODULE_OWNER(dev);
448 }
449
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");
454
455 static int __init teql_init(void)
456 {
457         int i;
458         int err = -ENODEV;
459
460         for (i = 0; i < max_equalizers; i++) {
461                 struct net_device *dev;
462                 struct teql_master *master;
463
464                 dev = alloc_netdev(sizeof(struct teql_master),
465                                   "teql%d", teql_master_setup);
466                 if (!dev) {
467                         err = -ENOMEM;
468                         break;
469                 }
470
471                 if ((err = register_netdev(dev))) {
472                         free_netdev(dev);
473                         break;
474                 }
475
476                 master = dev->priv;
477
478                 strlcpy(master->qops.id, dev->name, IFNAMSIZ);
479                 err = register_qdisc(&master->qops);
480
481                 if (err) {
482                         unregister_netdev(dev);
483                         free_netdev(dev);
484                         break;
485                 }
486
487                 list_add_tail(&master->master_list, &master_dev_list);
488         }
489         return i ? 0 : err;
490 }
491
492 static void __exit teql_exit(void) 
493 {
494         struct teql_master *master, *nxt;
495
496         list_for_each_entry_safe(master, nxt, &master_dev_list, master_list) {
497
498                 list_del(&master->master_list);
499
500                 unregister_qdisc(&master->qops);
501                 unregister_netdev(master->dev);
502                 free_netdev(master->dev);
503         }
504 }
505
506 module_init(teql_init);
507 module_exit(teql_exit);
508
509 MODULE_LICENSE("GPL");