]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-wrr.patch
- mention apparmor in release
[packages/kernel.git] / kernel-wrr.patch
1 diff -urN linux-2.6.25.orig/include/linux/pkt_sched.h linux-2.6.25/include/linux/pkt_sched.h
2 --- linux-2.6.25.orig/include/linux/pkt_sched.h 2008-04-17 04:49:44.000000000 +0200
3 +++ linux-2.6.25/include/linux/pkt_sched.h      2008-04-24 16:19:03.000000000 +0200
4 @@ -23,6 +23,8 @@
5  
6  #define TC_PRIO_MAX                    15
7  
8 +#include <linux/if_ether.h>
9 +
10  /* Generic queue statistics, available for all the elements.
11     Particular schedulers may have also their private records.
12   */
13 @@ -482,4 +484,97 @@
14  
15  #define NETEM_DIST_SCALE       8192
16  
17 +
18 +/* WRR section */
19 +
20 +/*
21 + * A sub weight and of a class
22 + * All numbers are represented as parts of (2^64-1).
23 + */
24 +struct tc_wrr_class_weight {
25 +       __u64   val;    // Current value        (0 is not valid)
26 +       __u64   decr;   // Value pr bytes       (2^64-1 is not valid)
27 +       __u64   incr;   // Value pr seconds     (2^64-1 is not valid)
28 +       __u64   min;    // Minimal value        (0 is not valid)
29 +       __u64   max;    // Minimal value        (0 is not valid)
30 +       time_t  tim;    // The time where the above information was correct
31 +};
32 +
33 +/* Packet send when modifying a class */
34 +struct tc_wrr_class_modf {
35 +       /* Not-valid values are ignored */
36 +       struct tc_wrr_class_weight weight1;
37 +       struct tc_wrr_class_weight weight2;
38 +};
39 +
40 +/* Packet returned when quering a class */
41 +struct tc_wrr_class_stats {
42 +       char    used;                   /* If this is false the information below is invalid */
43 +       struct tc_wrr_class_modf class_modf;
44 +       unsigned char addr[ETH_ALEN];
45 +       char    usemac;                 /* True if addr is a MAC address, else it is an IP address
46 +                                          (this value is only for convience, it is always the same
47 +                                           value as in the qdisc) */
48 +       int     heappos;                /* Current heap position or 0 if not in heap */
49 +       __u64   penal_ls;               /* Penalty value in heap (ls) */
50 +       __u64   penal_ms;               /* Penalty value in heap (ms) */
51 +};
52 +
53 +/* Qdisc-wide penalty information (boolean values - 2 not valid) */
54 +struct tc_wrr_qdisc_weight {
55 +       signed char     weight_mode;    /* 0=No automatic change to weight
56 +                                          1=Decrease normally
57 +                                          2=Also multiply with number of machines
58 +                                          3=Instead multiply with priority divided
59 +                                            with priority of the other.
60 +                                          -1=no change */
61 +};
62 +
63 +/* Packet send when modifing a qdisc */
64 +struct tc_wrr_qdisc_modf {
65 +       /* Not-valid values are ignored */
66 +       struct tc_wrr_qdisc_weight weight1;
67 +       struct tc_wrr_qdisc_weight weight2;
68 +};
69 +
70 +/* Packet send when creating a qdisc */
71 +struct tc_wrr_qdisc_crt {
72 +       struct tc_wrr_qdisc_modf qdisc_modf;
73 +       char    srcaddr;        /* 1=lookup source, 0=lookup destination */
74 +       char    usemac;         /* 1=Classify on MAC addresses, 0=classify on IP */
75 +       char    usemasq;        /* 1=Classify based on masqgrading - only valid
76 +                                    if usemac is zero */
77 +       int     bands_max;      /* Maximal number of bands (i.e.: classes) */
78 +       int     proxy_maxconn;  /* If differnt from 0 then we support proxy remapping
79 +                                  of packets. And this is the number of maximal
80 +                                  concurrent proxy connections. */
81 +};
82 +
83 +/* Packet returned when quering a qdisc */
84 +struct tc_wrr_qdisc_stats {
85 +       struct tc_wrr_qdisc_crt qdisc_crt;
86 +       int     proxy_curconn;               
87 +       int     nodes_in_heap;  /* Current number of bands wanting to send something */
88 +       int     bands_cur;      /* Current number of bands used (i.e.: MAC/IP addresses seen) */
89 +       int     bands_reused;   /* Number of times this band has been reused. */
90 +       int     packets_requed; /* Number of times packets have been requeued. */
91 +       __u64   priosum;        /* Sum of priorities in heap where 1 is 2^32 */
92 +};
93 +
94 +struct tc_wrr_qdisc_modf_std {
95 +       char    proxy;          /* This indicates which of the tc_wrr_qdisc_modf structers this is. 0=This struct */
96 +       char    change_class;   /* Should we also change a class? */
97 +       struct tc_wrr_qdisc_modf qdisc_modf; /* Only valid if change_class is false */
98 +       unsigned char addr[ETH_ALEN]; /* Class to change (non-used bytes should be 0). Valid only of change_class is true */
99 +       struct tc_wrr_class_modf class_modf; /* The change */
100 +};
101 +
102 +/* Used for proxyremapping */
103 +struct tc_wrr_qdisc_modf_proxy {
104 +       char    proxy;          /* This indicates which of the tc_wrr_qdisc_modf structers this is. 1=This struct */
105 +       char    reset;          /* This is 1 if the proxyremap information should be reset */
106 +       int     changec;        /* changec is the number of elements in changes. */
107 +       long    changes[0];     /* This is an array of type ProxyRemapBlock */
108 +};
109 +
110  #endif
111 diff -urN linux-2.6.25.orig/include/linux/wrr.h linux-2.6.25/include/linux/wrr.h
112 --- linux-2.6.25.orig/include/linux/wrr.h       1970-01-01 01:00:00.000000000 +0100
113 +++ linux-2.6.25/include/linux/wrr.h    2008-04-24 16:19:05.000000000 +0200
114 @@ -0,0 +1,67 @@
115 +#ifndef WRR_H
116 +#define WRR_H
117 +
118 +/*
119 + * This describes the information that is written in proxyremap.log and which
120 + * are used in the communication between proxyremapserver and proxyremapclient.
121 + * Everything is in network order.
122 + */
123 +
124 +/* First this header is send */
125 +#define PROXY_WELCOME_LINE "ProxyRemap 1.02. This is a binary protocol.\r\n"
126 +
127 +/* 
128 + * Then this block is send every time a connection is opened or closed.
129 + * Note how it is alligned to use small space usage - arrays of this
130 + * structure are saved in many places.
131 + */
132 +typedef struct {
133 +       /* Server endpoint of connection */
134 +       unsigned saddr; 
135 +       unsigned short sport;
136 +
137 +       /* IP protocol for this connection (typically udp or tcp) */
138 +       unsigned char proto;
139 +
140 +       /* Is the connection opened or closed? */
141 +       unsigned char open;
142 +
143 +       /* Client the packets should be accounted to */
144 +       unsigned caddr;
145 +       unsigned char macaddr[6];       /* Might be 0. */
146 +
147 +       /* An informal two-charecter code from the proxyserver. Used for debugging. */
148 +       char proxyinfo[2];
149 +} ProxyRemapBlock;
150 +
151 +
152 +/*
153 + * This is common code for for handling the tables containing information about 
154 + * which proxyserver connections are associated with which machines..
155 + */
156 +
157 +/* Returns the number of bytes that should be available in the area
158 + * maintained by this module given the maximal number of concurrent 
159 + * connections. */
160 +int proxyGetMemSize(int max_connections);
161 +
162 +/* Initializes a memory area to use. There must be as many bytes
163 +   available as returned by getMemSize. */
164 +void proxyInitMem(void *data, int max_connections);
165 +
166 +/* Queries */
167 +int proxyGetCurConn(void *data);       /* Returns current number of connections */
168 +int proxyMaxCurConn(void *data);       /* Returns maximal number of connections */
169 +
170 +/* This is called to open and close conenctions. Returns -1 if
171 +   a protocol error occores (i.e.: If it is discovered) */
172 +int proxyConsumeBlock(void *data, ProxyRemapBlock *);
173 +
174 +/* Returns the RemapBlock associated with this connection or 0: */
175 +ProxyRemapBlock *proxyLookup(void *data, unsigned ipaddr, unsigned short port,
176 +                            char proto);
177 +
178 +/* Return the maximum number of connections */
179 +int proxyGetMaxConn(void *data);
180 +
181 +#endif
182 diff -urN linux-2.6.25.orig/MAINTAINERS linux-2.6.25/MAINTAINERS
183 --- linux-2.6.25.orig/MAINTAINERS       2008-04-17 04:49:44.000000000 +0200
184 +++ linux-2.6.25/MAINTAINERS    2008-04-24 16:19:05.000000000 +0200
185 @@ -4351,6 +4351,12 @@
186  W:     http://oops.ghostprotocols.net:81/blog
187  S:     Maintained
188  
189 +WRR NETWORK SCHEDULER
190 +P:     Rasmus Bøg Hansen
191 +M      moffe@zz9.dk
192 +W:     http://www.zz9.dk/wrr
193 +S:     Maintained
194 +
195  X.25 NETWORK LAYER
196  P:     Henner Eisen
197  M:     eis@baty.hanse.de
198 diff -urN linux-2.6.25.orig/net/sched/Kconfig linux-2.6.25/net/sched/Kconfig
199 --- linux-2.6.25.orig/net/sched/Kconfig 2008-04-17 04:49:44.000000000 +0200
200 +++ linux-2.6.25/net/sched/Kconfig      2008-04-24 16:19:05.000000000 +0200
201 @@ -117,6 +117,25 @@
202           sch_rr, so it will load sch_prio, although it is referred
203           to using sch_rr.
204  
205 +config NET_SCH_WRR
206 +       tristate "WRR packet scheduler"
207 +       depends on NET_SCHED && ( NF_CONNTRACK || !NF_CONNTRACK )
208 +       help
209 +         The weighted robin-robin scheduling algorithm directs network
210 +         connections to different real servers based on server weights
211 +         in a round-robin manner. Servers with higher weights receive
212 +         new connections first than those with less weights, and servers
213 +         with higher weights get more connections than those with less
214 +         weights and servers with equal weights get equal connections.
215 +
216 +         If you want masquerading (the "masq" option to the tc userspace
217 +         program) you need to enable connection tracking (IP_NF_CONNTRACK)
218 +         in the netfilter options.
219 +
220 +         If you want to compile it in kernel, say Y. If you want to compile
221 +         it as a module, say M here and read Documentation/modules.txt. The
222 +         module will be called sch_wrr. If unsure, say N.
223 +
224  config NET_SCH_RED
225         tristate "Random Early Detection (RED)"
226         ---help---
227 diff -urN linux-2.6.25.orig/net/sched/Makefile linux-2.6.25/net/sched/Makefile
228 --- linux-2.6.25.orig/net/sched/Makefile        2008-04-17 04:49:44.000000000 +0200
229 +++ linux-2.6.25/net/sched/Makefile     2008-04-24 16:19:06.000000000 +0200
230 @@ -0,0 +0,2 @@
231 +sch_wrr-objs = wrr.o wrr_proxydict.o
232 +obj-$(CONFIG_NET_SCH_WRR)      += sch_wrr.o
233 diff -urN linux-2.6.25.orig/net/sched/wrr.c linux-2.6.25/net/sched/wrr.c
234 --- linux-2.6.25.orig/net/sched/wrr.c   1970-01-01 01:00:00.000000000 +0100
235 +++ linux-2.6.25/net/sched/wrr.c        2008-04-24 16:19:07.000000000 +0200
236 @@ -0,0 +1,1385 @@
237 +/*-----------------------------------------------------------------------------
238 +Weighted Round Robin scheduler.
239 +  
240 +Written by Christian Worm Mortensen, cworm@it-c.dk.
241 +
242 +Introduction
243 +============
244 +This module implements a weighted round robin queue with build-in classifier.
245 +The classifier currently map each MAC or IP address (configurable either MAC
246 +or IP and either source or destination) to different classes. Each such class 
247 +is called a band. Whan using MAC addresses only bridged packets can be 
248 +classified other packets go to a default MAC address.
249 +
250 +Each band has a weight value, where 0<weight<=1. The bandwidth each band
251 +get is proportional to the weight as can be deduced from the next section.
252 +
253 +The queue
254 +=========
255 +Each band has a penalty value. Bands having something to sent are kept in
256 +a heap according to this value. The band with the lowest penalty value
257 +is in the root of the heap. The penalty value is a 128 bit number. Initially 
258 +no bands are in the heap.
259 +
260 +Two global 64 bit values counter_low_penal and couter_high_penal are initialized
261 +to 0 and to 2^63 respectively.
262 +
263 +Enqueing:
264 +  The packet is inserted in the queue for the band it belongs to. If the band 
265 +  is not in the heap it is inserted into it. In this case, the upper 64 bits 
266 +  of its penalty value is set to the same as for the root-band of the heap. 
267 +  If the heap is empty 0 is used. The lower 64 bit is set to couter_low_penal
268 +  and couter_low_penal is incremented by 1.
269 +  
270 +Dequing:
271 +  If the heap is empty we have nothing to send. 
272 +  
273 +  If the root band has a non-empty queue a packet is dequeued from that.
274 +  The upper 64 bit of the penalty value of the band is incremented by the 
275 +  packet size divided with the weight of the band. The lower 64 bit is set to 
276 +  couter_high_penal and couter_high_penal is incremented by 1.
277 +
278 +  If the root element for some reason has an  empty queue it is removed from 
279 +  the heap and we try to dequeue again.
280 +
281 +The effect of the heap and the upper 64 bit of the penalty values is to 
282 +implement a weighted round robin queue. The effect of counter_low_penal,
283 +counter_high_penal and the lower 64 bit of the penalty value is primarily to
284 +stabilize the queue and to give better quality of service to machines only 
285 +sending a packet now and then. For example machines which have a single 
286 +interactive connection such as telnet or simple text chatting.
287 +
288 +Setting weight
289 +==============
290 +The weight value can be changed dynamically by the queue itself. The weight 
291 +value and how it is changed is described by the two members weight1 and 
292 +weight2 which has type tc_wrr_class_weight and which are in each class. And 
293 +by the two integer value members of the qdisc called penalfact1 and penalfact2.
294 +The structure is defined as:
295 +
296 +  struct tc_wrr_class_weight {
297 +    // All are represented as parts of (2^64-1).
298 +    __u64 val;  // Current value                        (0 is not valid)
299 +    __u64 decr; // Value pr bytes                       (2^64-1 is not valid)
300 +    __u64 incr; // Value pr seconds                     (2^64-1 is not valid)
301 +    __u64 min;  // Minimal value                        (0 is not valid)
302 +    __u64 max;  // Minimal value                        (0 is not valid)
303 +
304 +    // The time where the above information was correct:
305 +    time_t tim;
306 +  };
307 +    
308 +The weight value used by the dequeue operations is calculated as 
309 +weight1.val*weight2.val. weight1 and weight2 and handled independently and in the 
310 +same way as will be described now.
311 +
312 +Every second, the val parameter is incremented by incr.
313 +
314 +Every time a packet is transmitted the value is increment by decr times
315 +the packet size. Depending on the value of the weight_mode parameter it
316 +is also mulitplied with other numbers. This makes it possible to give 
317 +penalty to machines transferring much data.
318 +
319 +-----------------------------------------------------------------------------*/
320 +
321 +#include <linux/autoconf.h>
322 +#include <linux/module.h>
323 +#include <asm/uaccess.h>
324 +#include <asm/system.h>
325 +#include <linux/bitops.h>
326 +#include <linux/types.h>
327 +#include <linux/kernel.h>
328 +#include <linux/vmalloc.h>
329 +#include <linux/sched.h>
330 +#include <linux/string.h>
331 +#include <linux/mm.h>
332 +#include <linux/socket.h>
333 +#include <linux/sockios.h>
334 +#include <linux/in.h>
335 +#include <linux/errno.h>
336 +#include <linux/interrupt.h>
337 +#include <linux/if_ether.h>
338 +#include <linux/inet.h>
339 +#include <linux/netdevice.h>
340 +#include <linux/etherdevice.h>
341 +#include <linux/notifier.h>
342 +#include <net/ip.h>
343 +#include <net/route.h>
344 +#include <linux/skbuff.h>
345 +#include <net/sock.h>
346 +#include <net/pkt_sched.h>
347 +#include <linux/if_arp.h>
348 +#include <linux/version.h>
349 +#include <linux/wrr.h>
350 +
351 +#define WRR_VER "051111"
352 +
353 +#define my_malloc(size) kmalloc(size,GFP_KERNEL)
354 +#define my_free(ptr)    kfree(ptr)
355 +
356 +#define LOCK_START sch_tree_lock(sch);
357 +#define LOCK_END   sch_tree_unlock(sch);
358 +#define ENQUEUE_SUCCESS 0
359 +#define ENQUEUE_FAIL    NET_XMIT_DROP
360 +
361 +#if defined CONFIG_IP_NF_CONNTRACK || defined CONFIG_IP_NF_CONNTRACK_MODULE || defined CONFIG_NF_CONNTRACK || defined CONFIG_NF_CONNTRACK_MODULE
362 +#include <net/netfilter/nf_conntrack.h>
363 +#define MASQ_SUPPORT
364 +#endif
365 +
366 +/* The penalty (priority) type */
367 +typedef u64 penalty_base_t;
368 +#define penalty_base_t_max ((penalty_base_t)-1)
369 +typedef struct penalty_t {
370 +       penalty_base_t ms;
371 +       penalty_base_t ls;
372 +} penalty_t;
373 +#define penalty_leq(a,b) (a.ms<b.ms || (a.ms==b.ms && a.ls<=b.ls))
374 +#define penalty_le(a,b)  (a.ms<b.ms || (a.ms==b.ms && a.ls<b.ls))
375 +static penalty_t penalty_max = { penalty_base_t_max, penalty_base_t_max };
376 +
377 +/*
378 + * A general heap.
379 + */
380 +
381 +struct heap;
382 +struct heap_element;
383 +
384 +/* Initializes an empty heap:
385 + *   he:   A pointer to an unintialized heap structure identifying the heap
386 + *   size: Maximal number of elements the heap can contain
387 + *   poll: An array of size "size" used by the heap.     
388 + */
389 +static void heap_init(struct heap *he, int size, struct heap_element *poll);
390 +
391 +/* Each element in the heap is identified by a user-assigned id which
392 + * should be a non negative integer less than the size argument
393 + * given to heap_init.
394 + */
395 +static void heap_insert(struct heap *, int id, penalty_t);
396 +static void heap_remove(struct heap *, int id);
397 +static void heap_set_penalty(struct heap *, int id, penalty_t);
398 +
399 +/* Retreviewing information */
400 +static char heap_empty(struct heap *);                         /* Heap empty? */
401 +static char heap_contains(struct heap *, int id);              /* Does heap contain 
402 +                                                                  the given id? */
403 +static int heap_root(struct heap *);                           /* Returns the id of the root */
404 +static penalty_t heap_get_penalty(struct heap *, int id);      /* Returns penalty
405 +                                                                  of root node */
406 +
407 +/*
408 + * Heap implementation
409 + */
410 +
411 +struct heap_element {
412 +       penalty_t penalty;
413 +       int id;                 /* The user-assigned id of this element */
414 +       int id2idx;             /* Maps from user-assigned ids to indices in root_1 */
415 +};
416 +
417 +struct heap {
418 +       struct heap_element *root_1;
419 +       int elements;
420 +};
421 +
422 +/* Heap implementation */
423 +static void heap_init(struct heap *h, int size, struct heap_element *poll)
424 +{
425 +       int i;
426 +
427 +       h->elements = 0;
428 +       h->root_1 = poll - 1;
429 +
430 +       for (i = 0; i < size; i++)
431 +               poll[i].id2idx = 0;
432 +};
433 +
434 +static char heap_empty(struct heap *h)
435 +{
436 +       return h->elements == 0;
437 +}
438 +
439 +static char heap_contains(struct heap *h, int id)
440 +{
441 +       return h->root_1[id + 1].id2idx != 0;
442 +}
443 +
444 +static int heap_root(struct heap *h)
445 +{
446 +       return h->root_1[1].id;
447 +}
448 +
449 +static penalty_t heap_get_penalty(struct heap *h, int id)
450 +{
451 +       return h->root_1[h->root_1[id + 1].id2idx].penalty;
452 +}
453 +
454 +static void heap_penalty_changed_internal(struct heap *h, int idx);
455 +
456 +static void heap_set_penalty(struct heap *h, int id, penalty_t p)
457 +{
458 +       int idx = h->root_1[id + 1].id2idx;
459 +       h->root_1[idx].penalty = p;
460 +       heap_penalty_changed_internal(h, idx);
461 +}
462 +
463 +static void heap_insert(struct heap *h, int id, penalty_t p)
464 +{
465 +       /* Insert at the end of the heap */
466 +       h->elements++;
467 +       h->root_1[h->elements].id = id;
468 +       h->root_1[h->elements].penalty = p;
469 +       h->root_1[id + 1].id2idx = h->elements;
470 +
471 +       /* And put it in the right position */
472 +       heap_penalty_changed_internal(h, h->elements);
473 +}
474 +
475 +static void heap_remove(struct heap *h, int id)
476 +{
477 +       int idx = h->root_1[id + 1].id2idx;
478 +       int mvid;
479 +       h->root_1[id + 1].id2idx = 0;
480 +
481 +       if (h->elements == idx) {
482 +               h->elements--;
483 +               return;
484 +       }
485 +
486 +       mvid = h->root_1[h->elements].id;
487 +       h->root_1[idx].id = mvid;
488 +       h->root_1[idx].penalty = h->root_1[h->elements].penalty;
489 +       h->root_1[mvid + 1].id2idx = idx;
490 +
491 +       h->elements--;
492 +       heap_penalty_changed_internal(h, idx);
493 +}
494 +
495 +static void heap_swap(struct heap *h, int idx0, int idx1)
496 +{
497 +       penalty_t tmp_p;
498 +       int tmp_id;
499 +       int id0, id1;
500 +
501 +       /* Simple content */
502 +       tmp_p = h->root_1[idx0].penalty;
503 +       tmp_id = h->root_1[idx0].id;
504 +       h->root_1[idx0].penalty = h->root_1[idx1].penalty;
505 +       h->root_1[idx0].id = h->root_1[idx1].id;
506 +       h->root_1[idx1].penalty = tmp_p;
507 +       h->root_1[idx1].id = tmp_id;
508 +
509 +       /* Update reverse pointers */
510 +       id0 = h->root_1[idx0].id;
511 +       id1 = h->root_1[idx1].id;
512 +       h->root_1[id0 + 1].id2idx = idx0;
513 +       h->root_1[id1 + 1].id2idx = idx1;
514 +}
515 +
516 +static void heap_penalty_changed_internal(struct heap *h, int cur)
517 +{
518 +       if (cur == 1
519 +           || penalty_leq(h->root_1[cur >> 1].penalty,
520 +                          h->root_1[cur].penalty)) {
521 +               /* We are in heap order upwards - so we should move the element down */
522 +               for (;;) {
523 +                       int nxt0 = cur << 1;
524 +                       int nxt1 = nxt0 + 1;
525 +                       penalty_t pen_c = h->root_1[cur].penalty;
526 +                       penalty_t pen_0 =
527 +                           nxt0 <=
528 +                           h->elements ? h->root_1[nxt0].penalty : penalty_max;
529 +                       penalty_t pen_1 =
530 +                           nxt1 <=
531 +                           h->elements ? h->root_1[nxt1].penalty : penalty_max;
532 +
533 +                       if (penalty_le(pen_0, pen_c)
534 +                           && penalty_leq(pen_0, pen_1)) {
535 +                               /* Swap with child 0 */
536 +                               heap_swap(h, cur, nxt0);
537 +                               cur = nxt0;
538 +                       } else if (penalty_le(pen_1, pen_c)) {
539 +                               /* Swap with child 1 */
540 +                               heap_swap(h, cur, nxt1);
541 +                               cur = nxt1;
542 +                       } else {
543 +                               /* Heap in heap order */
544 +                               return;
545 +                       }
546 +               }
547 +       } else {
548 +               /* We are not in heap order upwards (and thus we must be it downwards).
549 +                  We move up */
550 +               while (cur != 1) {      /* While not root */
551 +                       int nxt = cur >> 1;
552 +                       if (penalty_leq
553 +                           (h->root_1[nxt].penalty, h->root_1[cur].penalty))
554 +                               return;
555 +                       heap_swap(h, cur, nxt);
556 +                       cur = nxt;
557 +               }
558 +       }
559 +};
560 +
561 +/*
562 + * Classification based on MAC or IP adresses. Note that of historical reason
563 + * these are prefixed with mac_ since originally only MAC bases classification
564 + * was supported.
565 +
566 + * This code should be in a separate filter module - but it isn't.
567 + */
568 +
569 +
570 +/* Interface */
571 +
572 +struct mac_head;
573 +
574 +/* Initialices/destroys the structure we maintain.
575 +   Returns -1 on error */
576 +static int mac_init(struct mac_head *, int max_macs, char srcaddr,
577 +                   char usemac, char usemasq, void *proxyremap);
578 +static void mac_done(struct mac_head *);
579 +static void mac_reset(struct mac_head *);
580 +
581 +/* Classify a packet. Returns a number n where 0<=n<max_macs. Or -1 if
582 +   the packet should be dropped. */
583 +static int mac_classify(struct mac_head *, struct sk_buff *skb);
584 +
585 +/*
586 + * Implementation:
587 + */
588 +
589 +struct mac_addr {
590 +       unsigned char addr[ETH_ALEN];   /* Address of this band (last two are 0 on IP) */
591 +       unsigned long lastused;         /* Last time a packet was encountered */
592 +       int class;                      /* Classid of this band (0<=classid<max_macs) */
593 +};
594 +
595 +static int mac_compare(const void *a, const void *b)
596 +{
597 +       return memcmp(a, b, ETH_ALEN);
598 +}
599 +
600 +struct mac_head {
601 +       int mac_max;            /* Maximal number of MAC addresses/classes allowed */
602 +       int mac_cur;            /* Current number of MAC addresses/classes */
603 +       int mac_reused;         /* Number of times we have reused a class with a new address. */
604 +       u64 incr_time;
605 +       char srcaddr;           /* True if we classify on the source address of packets,
606 +                                  else we use destination address. */
607 +       char usemac;            /* If true we use mac, else we use IP */
608 +       char usemasq;           /* If true we try to demasqgrade */
609 +       struct mac_addr *macs;  /* Allocated mac_max elements, used max_cur */
610 +       char *cls2mac;          /* Mapping from classnumbers to addresses -
611 +                                  there is 6 bytes in each entry */
612 +
613 +       void *proxyremap;       /* Information on proxy remapping of data or 0 */
614 +};
615 +
616 +/* This is as the standard C library function with the same name: */
617 +static const void *bsearch(const void *key, const void *base, int nmemb,
618 +                          size_t size,
619 +                          int (*compare) (const void *, const void *))
620 +{
621 +       int m_idx;
622 +       const void *m_ptr;
623 +       int i;
624 +
625 +       if (nmemb <= 0)
626 +               return 0;
627 +
628 +       m_idx = nmemb >> 1;
629 +       m_ptr = ((const char *)base) + m_idx * size;
630 +
631 +       i = compare(key, m_ptr);
632 +       if (i < 0)              /* key is less */
633 +               return bsearch(key, base, m_idx, size, compare);
634 +       else if (i > 0)
635 +               return bsearch(key, ((const char *)m_ptr) + size,
636 +                              nmemb - m_idx - 1, size, compare);
637 +
638 +       return m_ptr;
639 +}
640 +
641 +static int mac_init(struct mac_head *h, int max_macs, char srcaddr,
642 +                   char usemac, char usemasq, void *proxyremap)
643 +{
644 +       h->mac_cur = 0;
645 +       h->mac_reused = 0;
646 +       h->incr_time = 0;
647 +       h->srcaddr = srcaddr;
648 +       h->usemac = usemac;
649 +       h->usemasq = usemasq;
650 +       h->mac_max = max_macs;
651 +       h->proxyremap = proxyremap;
652 +
653 +       h->macs = (struct mac_addr *)
654 +           my_malloc(sizeof(struct mac_addr) * max_macs);
655 +       h->cls2mac = (char *)my_malloc(6 * max_macs);
656 +       if (!h->macs || !h->cls2mac) {
657 +               if (h->macs)
658 +                       my_free(h->macs);
659 +               if (h->cls2mac)
660 +                       my_free(h->cls2mac);
661 +               return -1;
662 +       }
663 +       return 0;
664 +}
665 +
666 +static void mac_done(struct mac_head *h)
667 +{
668 +       my_free(h->macs);
669 +       my_free(h->cls2mac);
670 +}
671 +
672 +static void mac_reset(struct mac_head *h)
673 +{
674 +       h->mac_cur = 0;
675 +       h->mac_reused = 0;
676 +       h->incr_time = 0;
677 +}
678 +
679 +static int lookup_mac(struct mac_head *h, unsigned char *addr)
680 +{
681 +       int i;
682 +       int class;
683 +
684 +       /* First try to find the address in the table */
685 +       struct mac_addr *m = (struct mac_addr *)
686 +           bsearch(addr, h->macs, h->mac_cur, sizeof(struct mac_addr),
687 +                   mac_compare);
688 +       if (m) {
689 +               /* Found */
690 +               m->lastused = h->incr_time++;
691 +               return m->class;
692 +       }
693 +       /* Okay - the MAC adress was not in table */
694 +       if (h->mac_cur == h->mac_max) {
695 +               /* And the table is full - delete the oldest entry */
696 +
697 +               /* Find the oldest entry */
698 +               int lowidx = 0;
699 +               int i;
700 +               for (i = 1; i < h->mac_cur; i++)
701 +                       if (h->macs[i].lastused < h->macs[lowidx].lastused)
702 +                               lowidx = i;
703 +
704 +               class = h->macs[lowidx].class;
705 +
706 +               /* And delete it */
707 +               memmove(&h->macs[lowidx], &h->macs[lowidx + 1],
708 +                       (h->mac_cur - lowidx - 1) * sizeof(struct mac_addr));
709 +               h->mac_reused++;
710 +               h->mac_cur--;
711 +       } else {
712 +               class = h->mac_cur;
713 +       }
714 +
715 +       /* The table is not full - find the position we should put the address in */
716 +       for (i = 0; i < h->mac_cur; i++)
717 +               if (mac_compare(addr, &h->macs[i]) < 0)
718 +                       break;
719 +
720 +       /* We should insert at position i */
721 +       memmove(&h->macs[i + 1], &h->macs[i],
722 +               (h->mac_cur - i) * sizeof(struct mac_addr));
723 +       m = &h->macs[i];
724 +       memcpy(m->addr, addr, ETH_ALEN);
725 +       m->lastused = h->incr_time++;
726 +       m->class = class;
727 +       h->mac_cur++;
728 +
729 +       /* Finally update the cls2mac variabel */
730 +       memcpy(h->cls2mac + ETH_ALEN * class, addr, ETH_ALEN);
731 +
732 +       return m->class;
733 +}
734 +
735 +int valid_ip_checksum(struct iphdr *ip, int size)
736 +{
737 +       __u16 header_len = ip->ihl << 2;
738 +       __u16 c = 0;
739 +       __u16 *ipu = (u16 *) ip;
740 +       int a;
741 +
742 +       /* We require 4 bytes in the packet since we access the port numbers */
743 +       if ((size < header_len) || size < sizeof(struct iphdr) + 4)
744 +               return 0;
745 +
746 +       for (a = 0; a < (header_len >> 1); a++, ipu++) {
747 +               if (a != 5) {
748 +                       /* If not the checksum field */
749 +                       __u16 oldc = c;
750 +                       c += (*ipu);
751 +                       if (c < oldc)
752 +                               c++;
753 +               }
754 +       }
755 +
756 +       return ip->check == (__u16) ~ c;
757 +}
758 +
759 +static int mac_classify(struct mac_head *head, struct sk_buff *skb)
760 +{
761 +       /* We set this to the address we map to. In case we map to an IP
762 +          address the last two entries are set to 0. */
763 +       unsigned char addr[ETH_ALEN];
764 +
765 +       /* Used later for mac classification */
766 +       struct ethhdr *hdr;
767 +
768 +       /* This is the size of the network part of the packet */
769 +       int size = ((char *)skb->data + skb->len) - ((char *)ip_hdr(skb));
770 +
771 +       /* Set a default value for the address */
772 +       memset(addr, 0, ETH_ALEN);
773 +
774 +       /* Map IPv4 traffic to their correct addresses.
775 +          Everything (non-IPv4) else goes to a default address */
776 +       if (ntohs(skb->protocol) == ETH_P_IP) {
777 +
778 +               struct iphdr *iph = ip_hdr(skb);        /* This is the IP header */
779 +
780 +               const __u16 *portp = (__u16 *) & (((char *)iph)[iph->ihl * 4]); /* Port numbers*/
781 +               __u16 sport = portp[0];
782 +               __u16 dport = portp[1];
783 +
784 +               unsigned ipaddr;                        /* IP classification */
785 +               ProxyRemapBlock *prm;                   /* Proxyremapping */
786 +
787 +               /* IP packets must have valid checsum */
788 +               if (!valid_ip_checksum(ip_hdr(skb), size))
789 +                       return -1;
790 +
791 +               /* Set ipaddr */
792 +               if (head->srcaddr)
793 +                       ipaddr = iph->saddr;
794 +               else
795 +                       ipaddr = iph->daddr;
796 +
797 +#ifdef MASQ_SUPPORT
798 +               /* Update ipaddr if packet is masqgraded */
799 +               if (head->usemasq) {
800 +                       /* Thanks to Rusty Russell for help with the following code */
801 +                       enum ip_conntrack_info ctinfo;
802 +                       struct nf_conn *ct;
803 +                       ct = nf_ct_get(skb, &ctinfo);
804 +                       if (ct) {
805 +                               if (head->srcaddr)
806 +                                       ipaddr =
807 +                                           ct->tuplehash[CTINFO2DIR(ctinfo)].
808 +                                           tuple.src.u3.ip;
809 +                               else
810 +                                       ipaddr =
811 +                                           ct->tuplehash[CTINFO2DIR(ctinfo)].
812 +                                           tuple.dst.u3.ip;
813 +                       }
814 +               }
815 +#endif
816 +
817 +               /* Set prm (proxyremap) based on ipaddr */
818 +               prm = 0;
819 +               if (head->proxyremap) {
820 +                       if (head->srcaddr) {
821 +                               prm =
822 +                                   proxyLookup(head->proxyremap, ipaddr, sport,
823 +                                               ip_hdr(skb)->protocol);
824 +                       } else {
825 +                               prm =
826 +                                   proxyLookup(head->proxyremap, ipaddr, dport,
827 +                                               ip_hdr(skb)->protocol);
828 +                       }
829 +               }
830 +               /* And finally set the correct address */
831 +               if (prm) {
832 +                       /* This packet should be remapped */
833 +                       if (head->usemac)
834 +                               memcpy(addr, prm->macaddr, ETH_ALEN);
835 +                       else
836 +                               memcpy(addr, &prm->caddr, sizeof(unsigned));
837 +               } else {
838 +                       /* This packet should not be remapped */
839 +                       if (head->usemac) {
840 +                               /* Make sure we have a valid mac address */ 
841 +                               if (skb->dev &&
842 +                                   skb->dev->type == ARPHRD_ETHER &&
843 +                                   skb->len >= ETH_HLEN) {
844 +                                       hdr = (struct ethhdr*)skb->data;
845 +                                       if(head->srcaddr)
846 +                                               memcpy(addr,hdr->h_source, ETH_ALEN);
847 +                                       else
848 +                                               memcpy(addr,hdr->h_dest, ETH_ALEN);
849 +                               }
850 +                       } else {
851 +                               memcpy(addr, &ipaddr, 4);
852 +                       }
853 +               }
854 +       }
855 +
856 +       return lookup_mac(head, addr);
857 +}
858 +
859 +/*
860 + * The qdisc itself
861 + */
862 +
863 +/* Pr-class information */
864 +struct wrrc_sched_data {
865 +       struct Qdisc *que;                      /* The queue for this class */
866 +       struct tc_wrr_class_modf class_modf;    /* Information about the class */
867 +
868 +       /* For classes in the heap this is the priority value priosum
869 +          was updated with for this class */
870 +       u64 priosum_val;
871 +};
872 +
873 +/* Pr-qdisc information */
874 +struct wrr_sched_data {
875 +       struct heap h;                  /* A heap containing all the bands that will send something */
876 +       struct heap_element *poll;      /* bandc elements */
877 +
878 +       /* The sum of the prioities of the elements in the heap where
879 +          a priority of 1 is saved as 2^32 */
880 +       u64 priosum;
881 +
882 +       /* A class for each band */
883 +       struct wrrc_sched_data *bands;  /* bandc elements */
884 +
885 +       /* Information maintained by the proxydict module of 0 if we
886 +          have no proxy remapping */
887 +       void *proxydict;
888 +
889 +       /* Always incrementning counters, we always have that any value of
890 +          counter_low_penal < any value of counter_high_penal. */
891 +       penalty_base_t counter_low_penal;
892 +       penalty_base_t counter_high_penal;
893 +
894 +       struct tc_wrr_qdisc_modf qdisc_modf; /* Penalty updating */
895 +
896 +       int packets_requed;             /* Statistics */
897 +
898 +       struct mac_head filter;         /* The filter */
899 +       int bandc;                      /* Number of bands */
900 +};
901 +
902 +/* Priority handling.
903 + *   weight is in interval [0..2^32]
904 + *   priosum has whole numbers in the upper and fragments in the lower 32 bits. 
905 + */
906 +static void weight_transmit(struct tc_wrr_class_weight *p,
907 +                           struct tc_wrr_qdisc_weight q,
908 +                           unsigned heapsize,
909 +                           u64 priosum, u64 weight, unsigned size)
910 +{
911 +
912 +       unsigned long now = jiffies / HZ;
913 +
914 +       /* Penalty for transmitting */
915 +       u64 change, old;
916 +       u32 divisor;
917 +
918 +       change = 0;
919 +       switch (q.weight_mode) {
920 +       case 1:
921 +               change = p->decr * size;
922 +               break;
923 +       case 2:
924 +               change = p->decr * size * heapsize;
925 +               break;
926 +       case 3:         /* Note: 64 bit division is not always available */
927 +               divisor = (u32) (weight >> 16);
928 +               if (divisor <= 0)
929 +                       divisor = 1;
930 +               change = p->decr * size * (((u32) (priosum >> 16)) / divisor);
931 +               break;
932 +       }
933 +       old = p->val;
934 +       p->val -= change;
935 +       if (p->val > old || p->val < p->min)
936 +               p->val = p->min;
937 +
938 +       /* Credit for time went */
939 +       change = (now - p->tim) * p->incr;
940 +       p->tim = now;
941 +       old = p->val;
942 +       p->val += change;
943 +       if (p->val < old || p->val > p->max)
944 +               p->val = p->max;
945 +}
946 +
947 +static void weight_setdefault(struct tc_wrr_class_weight *p)
948 +{
949 +       p->val = (u64) - 1;
950 +       p->decr = 0;
951 +       p->incr = 0;
952 +       p->min = (u64) - 1;
953 +       p->max = (u64) - 1;
954 +       p->tim = jiffies / HZ;
955 +}
956 +
957 +static void weight_setvalue(struct tc_wrr_class_weight *dst,
958 +                           struct tc_wrr_class_weight *src)
959 +{
960 +       if (src->val != 0) {
961 +               dst->val = src->val;
962 +               dst->tim = jiffies / HZ;
963 +       }
964 +       if (src->min != 0)
965 +               dst->min = src->min;
966 +       if (src->max != 0)
967 +               dst->max = src->max;
968 +       if (src->decr != ((u64) - 1))
969 +               dst->decr = src->decr;
970 +       if (src->incr != ((u64) - 1))
971 +               dst->incr = src->incr;
972 +       if (dst->val < dst->min)
973 +               dst->val = dst->min;
974 +       if (dst->val > dst->max)
975 +               dst->val = dst->max;
976 +}
977 +
978 +static void wrr_destroy(struct Qdisc *sch)
979 +{
980 +       struct wrr_sched_data *q = qdisc_priv(sch);
981 +       int i;
982 +
983 +       /* Destroy our filter */
984 +       mac_done(&q->filter);
985 +
986 +       /* Destroy all our childre ques */
987 +       for (i = 0; i < q->bandc; i++)
988 +               qdisc_destroy(q->bands[i].que);
989 +
990 +       /* And free memory */
991 +       my_free(q->bands);
992 +       my_free(q->poll);
993 +       if (q->proxydict)
994 +               my_free(q->proxydict);
995 +}
996 +
997 +static int wrr_init(struct Qdisc *sch, struct rtattr *opt)
998 +{
999 +       struct wrr_sched_data *q = qdisc_priv(sch);
1000 +       int i, maciniterr;
1001 +       char crterr;
1002 +       struct tc_wrr_qdisc_crt *qopt;
1003 +
1004 +       /* Parse options */
1005 +       if (!opt)
1006 +               return -EINVAL; /* Options must be specified */
1007 +       if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
1008 +               return -EINVAL;
1009 +       qopt = RTA_DATA(opt);
1010 +
1011 +       if (qopt->bands_max > 8192 || qopt->bands_max < 2) {
1012 +               /* More than 8192 queues or less than 2? That cannot be true - it must be 
1013 +                  an error... */
1014 +               return -EINVAL;
1015 +       }
1016 +
1017 +       if (qopt->proxy_maxconn < 0 || qopt->proxy_maxconn > 20000) {
1018 +               /* More than this number of maximal concurrent connections is unrealistic */
1019 +               return -EINVAL;
1020 +       }
1021 +#ifndef MASQ_SUPPORT
1022 +       if (qopt->usemasq) {
1023 +               return -ENOSYS;
1024 +       }
1025 +#endif
1026 +       q->bandc = qopt->bands_max;
1027 +       q->qdisc_modf = qopt->qdisc_modf;
1028 +
1029 +       /* Create structures */
1030 +       q->poll = (struct heap_element *)
1031 +           my_malloc(sizeof(struct heap_element) * q->bandc);
1032 +       q->bands = (struct wrrc_sched_data *)
1033 +           my_malloc(sizeof(struct wrrc_sched_data) * q->bandc);
1034 +
1035 +       if (qopt->proxy_maxconn > 0) {
1036 +               q->proxydict = my_malloc(proxyGetMemSize(qopt->proxy_maxconn));
1037 +       } else {
1038 +               q->proxydict = 0;
1039 +       }
1040 +
1041 +       /* Init mac module */
1042 +       maciniterr = mac_init(&q->filter, qopt->bands_max, qopt->srcaddr,
1043 +                             qopt->usemac, qopt->usemasq, q->proxydict);
1044 +
1045 +       /* See if we got the memory we wanted */
1046 +       if (!q->poll || !q->bands ||
1047 +           (qopt->proxy_maxconn > 0 && !q->proxydict) || maciniterr < 0) {
1048 +               if (q->poll)
1049 +                       my_free(q->poll);
1050 +               if (q->bands)
1051 +                       my_free(q->bands);
1052 +               if (q->proxydict)
1053 +                       my_free(q->proxydict);
1054 +               if (maciniterr >= 0)
1055 +                       mac_done(&q->filter);
1056 +               return -ENOMEM;
1057 +       }
1058 +       /* Initialize proxy */
1059 +       if (q->proxydict)
1060 +               proxyInitMem(q->proxydict, qopt->proxy_maxconn);
1061 +       /* Initialize values */
1062 +       q->counter_low_penal = 0;
1063 +       q->counter_high_penal = penalty_base_t_max >> 1;
1064 +       q->packets_requed = 0;
1065 +
1066 +       /* Initialize empty heap */
1067 +       heap_init(&q->h, q->bandc, q->poll);
1068 +       q->priosum = 0;
1069 +
1070 +       /* Initialize each band */
1071 +       crterr = 0;
1072 +       for (i = 0; i < q->bandc; i++) {
1073 +               weight_setdefault(&q->bands[i].class_modf.weight1);
1074 +               weight_setdefault(&q->bands[i].class_modf.weight2);
1075 +               if (!crterr) {
1076 +                       struct Qdisc *child =
1077 +                           qdisc_create_dflt(sch->dev, &pfifo_qdisc_ops, sch->handle);
1078 +                       if (child)
1079 +                               q->bands[i].que = child;
1080 +                       else {
1081 +                               /* Queue couldn't be created :-( */
1082 +                               crterr = 1;
1083 +                       }
1084 +               }
1085 +               if (crterr)
1086 +                       q->bands[i].que = &noop_qdisc;
1087 +       }
1088 +
1089 +       if (crterr) {
1090 +               /* Destroy again */
1091 +               wrr_destroy(sch);
1092 +               return -ENOMEM;
1093 +       }
1094 +
1095 +       printk(KERN_DEBUG "sch_wrr: Initialized version " WRR_VER "\n");
1096 +
1097 +       return 0;
1098 +}
1099 +
1100 +static void wrr_reset(struct Qdisc *sch)
1101 +{
1102 +       struct wrr_sched_data *q = qdisc_priv(sch);
1103 +       int i;
1104 +
1105 +       /* Reset own values */
1106 +       q->counter_low_penal = 0;
1107 +       q->counter_high_penal = penalty_base_t_max >> 1;
1108 +       q->packets_requed = 0;
1109 +
1110 +       /* Reset filter */
1111 +       mac_reset(&q->filter);
1112 +
1113 +       /* Reinitialize heap */
1114 +       heap_init(&q->h, q->bandc, q->poll);
1115 +       q->priosum = 0;
1116 +
1117 +       /* Reset all bands */
1118 +       for (i = 0; i < q->bandc; i++) {
1119 +               weight_setdefault(&q->bands[i].class_modf.weight1);
1120 +               weight_setdefault(&q->bands[i].class_modf.weight2);
1121 +               qdisc_reset(q->bands[i].que);
1122 +       }
1123 +
1124 +       /* Reset proxy remapping information */
1125 +       if (q->proxydict)
1126 +               proxyInitMem(q->proxydict, proxyGetMaxConn(q->proxydict));
1127 +}
1128 +
1129 +static int wrr_enqueue(struct sk_buff *skb, struct Qdisc *sch)
1130 +{
1131 +       struct wrr_sched_data *q = qdisc_priv(sch);
1132 +       int retvalue = ENQUEUE_FAIL;
1133 +
1134 +       /* The packet is in skb */
1135 +       int band = mac_classify(&q->filter, skb);
1136 +
1137 +       if (band >= 0) {
1138 +               /* Enque packet for this band */
1139 +               struct Qdisc *qdisc = q->bands[band].que;
1140 +
1141 +               if ((retvalue = qdisc->enqueue(skb, qdisc)) == ENQUEUE_SUCCESS) {
1142 +                       /* Successful */
1143 +                       sch->bstats.bytes += skb->len;
1144 +                       sch->bstats.packets++;
1145 +                       sch->q.qlen++;
1146 +
1147 +                       /* Insert band into heap if not already there */
1148 +                       if (!heap_contains(&q->h, band)) {
1149 +                               penalty_t p;
1150 +                               if (!heap_empty(&q->h))
1151 +                                       p.ms =
1152 +                                           heap_get_penalty(&q->h,
1153 +                                                            heap_root(&q->h)).
1154 +                                           ms;
1155 +                               else
1156 +                                       p.ms = 0;
1157 +                               p.ls = q->counter_low_penal++;
1158 +                               heap_insert(&q->h, band, p);
1159 +                               q->bands[band].priosum_val =
1160 +                                   ((q->bands[band].class_modf.weight1.
1161 +                                     val >> 48) +
1162 +                                    1) *
1163 +                                   ((q->bands[band].class_modf.weight2.
1164 +                                     val >> 48) + 1);
1165 +                               q->priosum += q->bands[band].priosum_val;
1166 +                       }
1167 +               }
1168 +       } else {
1169 +               /* If we decide not to enque it seems like we also need to free the packet */
1170 +               kfree_skb(skb);
1171 +       }
1172 +
1173 +       if (retvalue != ENQUEUE_SUCCESS) {
1174 +               /* Packet not enqued */
1175 +               sch->qstats.drops++;
1176 +       }
1177 +
1178 +       return retvalue;
1179 +}
1180 +
1181 +static struct sk_buff *wrr_dequeue(struct Qdisc *sch)
1182 +{
1183 +       struct wrr_sched_data *q = qdisc_priv(sch);
1184 +       struct sk_buff *skb;
1185 +       int band;
1186 +       u64 weight, priosum;
1187 +       struct wrrc_sched_data *b;
1188 +
1189 +       /* Return if heap is empty */
1190 +       if (heap_empty(&q->h))
1191 +               return 0;
1192 +
1193 +       /* Find root element */
1194 +       band = heap_root(&q->h);
1195 +
1196 +       /* Find priority of this element in interval [1;2^32] */
1197 +       b = &q->bands[band];
1198 +
1199 +       /* weight is in interval [1;2^32] */
1200 +       weight = ((b->class_modf.weight1.val >> 48) + 1) * ((b->class_modf.weight2.val >> 48) + 1);
1201 +       priosum = q->priosum;
1202 +       q->priosum -= q->bands[band].priosum_val;
1203 +
1204 +       /* Dequeue the packet from the root */
1205 +       skb = q->bands[band].que->dequeue(q->bands[band].que);
1206 +
1207 +       if (skb) {
1208 +               /* There was a packet in this queue */
1209 +               unsigned adjlen;
1210 +               penalty_t p;
1211 +
1212 +               /* Find length of packet adjusted with priority */
1213 +               adjlen = (u32) (weight >> (32 - 16));
1214 +               if (adjlen == 0)
1215 +                       adjlen = 1;
1216 +               adjlen = (skb->len << 16) / adjlen;
1217 +
1218 +               /* Update penalty information for this class */
1219 +               weight_transmit(&b->class_modf.weight1, q->qdisc_modf.weight1,
1220 +                               q->h.elements, priosum, weight, skb->len);
1221 +               weight_transmit(&b->class_modf.weight2, q->qdisc_modf.weight2,
1222 +                               q->h.elements, priosum, weight, skb->len);
1223 +               q->bands[band].priosum_val =
1224 +                   ((b->class_modf.weight1.val >> 48) +
1225 +                    1) * ((b->class_modf.weight2.val >> 48) + 1);
1226 +               q->priosum += q->bands[band].priosum_val;
1227 +
1228 +               /* And update the class in the heap */
1229 +               p = heap_get_penalty(&q->h, band);
1230 +               p.ms += adjlen;
1231 +               p.ls = q->counter_high_penal++;
1232 +               heap_set_penalty(&q->h, band, p);
1233 +
1234 +               /* Return packet */
1235 +               sch->q.qlen--;
1236 +               return skb;
1237 +       }
1238 +       /* No packet - so machine should be removed from heap */
1239 +       heap_remove(&q->h, band);
1240 +
1241 +       return 0;
1242 +}
1243 +
1244 +static int wrr_requeue(struct sk_buff *skb, struct Qdisc *sch)
1245 +{
1246 +       struct wrr_sched_data *q = qdisc_priv(sch);
1247 +       struct Qdisc *qdisc;
1248 +       int ret;
1249 +
1250 +       /* Find band we took it from */
1251 +       int band = mac_classify(&q->filter, skb);
1252 +       if (band < 0) {
1253 +               /* Who should now free the pakcet? */
1254 +               printk(KERN_DEBUG
1255 +                      "sch_wrr: Oops - packet requeued could never have been queued.\n");
1256 +               sch->qstats.drops++;
1257 +               return ENQUEUE_FAIL;
1258 +       }
1259 +
1260 +       q->packets_requed++;
1261 +
1262 +       /* Try to requeue it on that machine */
1263 +       qdisc = q->bands[band].que;
1264 +
1265 +       if ((ret = qdisc->ops->requeue(skb, qdisc)) == ENQUEUE_SUCCESS) {
1266 +               /* On success */
1267 +               sch->q.qlen++;
1268 +               sch->qstats.requeues++;
1269 +
1270 +               /* We should restore priority information - but we don't
1271 +                *
1272 +                * p=heap_get_penalty(&q->h,band);
1273 +                * ...
1274 +                * heap_set_penalty(&q->h,band,p);
1275 +                */
1276 +
1277 +               return ENQUEUE_SUCCESS;
1278 +       } else {
1279 +               sch->qstats.drops++;
1280 +               return ret;
1281 +       }
1282 +}
1283 +
1284 +static unsigned int wrr_drop(struct Qdisc *sch)
1285 +{
1286 +       struct wrr_sched_data *q = qdisc_priv(sch);
1287 +
1288 +       /* Ugly... Drop button up in heap */
1289 +       int i;
1290 +
1291 +       for (i = q->h.elements; i >= 1; i--) {
1292 +               int band = q->h.root_1[i].id;
1293 +               if (q->bands[band].que->ops->drop(q->bands[band].que)) {
1294 +                       /* On success */
1295 +                       sch->q.qlen--;
1296 +                       sch->qstats.drops++;
1297 +                       return 1;
1298 +               }
1299 +       }
1300 +
1301 +       return 0;
1302 +}
1303 +
1304 +static int wrr_dump(struct Qdisc *sch, struct sk_buff *skb)
1305 +{
1306 +       struct wrr_sched_data *q = qdisc_priv(sch);
1307 +       unsigned char *b = skb->tail;
1308 +       struct tc_wrr_qdisc_stats opt;
1309 +
1310 +       opt.qdisc_crt.qdisc_modf = q->qdisc_modf;
1311 +       opt.qdisc_crt.srcaddr = q->filter.srcaddr;
1312 +       opt.qdisc_crt.usemac = q->filter.usemac;
1313 +       opt.qdisc_crt.usemasq = q->filter.usemasq;
1314 +       opt.qdisc_crt.bands_max = q->filter.mac_max;
1315 +       opt.nodes_in_heap = q->h.elements;
1316 +       opt.bands_cur = q->filter.mac_cur;
1317 +       opt.bands_reused = q->filter.mac_reused;
1318 +       opt.packets_requed = q->packets_requed;
1319 +       opt.priosum = q->priosum;
1320 +
1321 +       if (q->proxydict) {
1322 +               opt.qdisc_crt.proxy_maxconn = proxyGetMaxConn(q->proxydict);
1323 +               opt.proxy_curconn = proxyGetCurConn(q->proxydict);
1324 +       } else {
1325 +               opt.qdisc_crt.proxy_maxconn = 0;
1326 +               opt.proxy_curconn = 0;
1327 +       }
1328 +
1329 +       RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
1330 +       return skb->len;
1331 +
1332 +      rtattr_failure:          /* seems like RTA_PUT jump to this label */
1333 +       skb_trim(skb, b - skb->data);
1334 +       return -1;
1335 +}
1336 +
1337 +static int wrr_tune_std(struct Qdisc *sch, struct rtattr *opt)
1338 +{
1339 +       struct wrr_sched_data *q = qdisc_priv(sch);
1340 +       struct tc_wrr_qdisc_modf_std *qopt = RTA_DATA(opt);
1341 +
1342 +       if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)))
1343 +               return -EINVAL;
1344 +
1345 +       LOCK_START;
1346 +       
1347 +       if (qopt->change_class) {
1348 +               int idx = lookup_mac(&q->filter, qopt->addr);
1349 +               weight_setvalue
1350 +                   (&q->bands[idx].class_modf.weight1,
1351 +                    &qopt->class_modf.weight1);
1352 +               weight_setvalue(&q->bands[idx].class_modf.weight2,
1353 +                               &qopt->class_modf.weight2);
1354 +       } else {
1355 +               if (qopt->qdisc_modf.weight1.weight_mode != -1)
1356 +                       q->qdisc_modf.weight1.weight_mode =
1357 +                           qopt->qdisc_modf.weight1.weight_mode;
1358 +               if (qopt->qdisc_modf.weight2.weight_mode != -1)
1359 +                       q->qdisc_modf.weight2.weight_mode =
1360 +                           qopt->qdisc_modf.weight2.weight_mode;
1361 +       }
1362 +
1363 +       LOCK_END;
1364 +       return 0;
1365 +}
1366 +
1367 +static int wrr_tune_proxy(struct Qdisc *sch, struct rtattr *opt)
1368 +{
1369 +       struct wrr_sched_data *q = qdisc_priv(sch);
1370 +       struct tc_wrr_qdisc_modf_proxy *qopt = RTA_DATA(opt);
1371 +       int i;
1372 +
1373 +       /* Return if we are not configured with proxy support */
1374 +       if (!q->proxydict)
1375 +               return -ENOSYS;
1376 +
1377 +       /* Return if not enough data given */
1378 +       if (opt->rta_len < RTA_LENGTH(sizeof(*qopt)) ||
1379 +           opt->rta_len <
1380 +           RTA_LENGTH(sizeof(*qopt) + sizeof(ProxyRemapBlock) * qopt->changec))
1381 +               return -EINVAL;
1382 +
1383 +       LOCK_START;
1384 +
1385 +       if (qopt->reset) {
1386 +               proxyInitMem(q->proxydict, proxyGetMaxConn(q->proxydict));
1387 +       }
1388 +       /* Do all the changes */
1389 +       for (i = 0; i < qopt->changec; i++) {
1390 +               proxyConsumeBlock(q->proxydict,
1391 +                                 &((ProxyRemapBlock *) & qopt->changes)[i]);
1392 +       }
1393 +
1394 +       LOCK_END;
1395 +
1396 +       return 0;
1397 +}
1398 +
1399 +static int wrr_tune(struct Qdisc *sch, struct rtattr *opt)
1400 +{
1401 +       if (((struct tc_wrr_qdisc_modf_std *)RTA_DATA(opt))->proxy) {
1402 +               return wrr_tune_proxy(sch, opt);
1403 +       } else {
1404 +               return wrr_tune_std(sch, opt);
1405 +       }
1406 +}
1407 +
1408 +/*
1409 + * Classes.
1410 + *  External and internal IDs are equal. They are the band number plus 1.
1411 + */
1412 +
1413 +/* Replace a class with another */
1414 +static int wrr_graft(struct Qdisc *sch, unsigned long arg, struct Qdisc *new,
1415 +                    struct Qdisc **old)
1416 +{
1417 +       struct wrr_sched_data *q = qdisc_priv(sch);
1418 +       if (arg > q->bandc || arg == 0)
1419 +               return -EINVAL;
1420 +       arg--;
1421 +
1422 +       if (new == NULL)
1423 +               new = &noop_qdisc;
1424 +
1425 +       LOCK_START * old = q->bands[arg].que;
1426 +       q->bands[arg].que = new;
1427 +       qdisc_reset(*old);
1428 +       LOCK_END return 0;
1429 +}
1430 +
1431 +/* Returns the qdisc for a class */
1432 +static struct Qdisc *wrr_leaf(struct Qdisc *sch, unsigned long arg)
1433 +{
1434 +       struct wrr_sched_data *q = qdisc_priv(sch);
1435 +       if (arg > q->bandc || arg == 0)
1436 +               return NULL;
1437 +       arg--;
1438 +       return q->bands[arg].que;
1439 +}
1440 +
1441 +static unsigned long wrr_get(struct Qdisc *sch, u32 classid)
1442 +{
1443 +       struct wrr_sched_data *q = qdisc_priv(sch);
1444 +       unsigned long band = TC_H_MIN(classid);
1445 +       if (band > q->bandc || band == 0)
1446 +               return 0;
1447 +       return band;
1448 +}
1449 +
1450 +static void wrr_put(struct Qdisc *q, unsigned long cl)
1451 +{
1452 +       return;
1453 +}
1454 +
1455 +static int wrr_delete(struct Qdisc *sch, unsigned long cl)
1456 +{
1457 +       struct wrr_sched_data *q = qdisc_priv(sch);
1458 +       if (cl == 0 || cl > q->bandc)
1459 +               return -ENOENT;
1460 +       cl--;
1461 +       return 0;
1462 +}
1463 +
1464 +static int wrr_dump_class(struct Qdisc *sch, unsigned long cl,
1465 +                         struct sk_buff *skb, struct tcmsg *tcm)
1466 +{
1467 +       struct wrr_sched_data *q = qdisc_priv(sch);
1468 +       unsigned char *b = skb->tail;
1469 +       struct tc_wrr_class_stats opt;
1470 +
1471 +       /* Handle of this class */
1472 +       tcm->tcm_handle = sch->handle | cl;
1473 +
1474 +       if (cl == 0 || cl > q->bandc)
1475 +               goto rtattr_failure;
1476 +       cl--;
1477 +
1478 +       if ((cl < q->bandc) && q->bands[cl].que)
1479 +               tcm->tcm_info = q->bands[cl].que->handle;
1480 +
1481 +       if (cl >= q->filter.mac_cur) {
1482 +               /* Band is unused */
1483 +               memset(&opt, 0, sizeof(opt));
1484 +               opt.used = 0;
1485 +       } else {
1486 +               opt.used = 1;
1487 +               opt.class_modf.weight1 = q->bands[cl].class_modf.weight1;
1488 +               opt.class_modf.weight2 = q->bands[cl].class_modf.weight2;
1489 +               weight_transmit(&opt.class_modf.weight1, q->qdisc_modf.weight1,
1490 +                               0, 0, 0, 0);
1491 +               weight_transmit(&opt.class_modf.weight2, q->qdisc_modf.weight2,
1492 +                               0, 0, 0, 0);
1493 +               memcpy(opt.addr, q->filter.cls2mac + cl * ETH_ALEN, ETH_ALEN);
1494 +               opt.usemac = q->filter.usemac;
1495 +               opt.heappos = q->h.root_1[cl + 1].id2idx;
1496 +               if (opt.heappos != 0) {
1497 +                       /* Is in heap */
1498 +                       opt.penal_ls = heap_get_penalty(&q->h, cl).ls;
1499 +                       opt.penal_ms = heap_get_penalty(&q->h, cl).ms;
1500 +               } else {
1501 +                       opt.penal_ls = 0;
1502 +                       opt.penal_ms = 0;
1503 +               }
1504 +       }
1505 +
1506 +       /* Put quing information */
1507 +       RTA_PUT(skb, TCA_OPTIONS, sizeof(opt), &opt);
1508 +       return skb->len;
1509 +
1510 +      rtattr_failure:
1511 +       skb_trim(skb, b - skb->data);
1512 +       return -1;
1513 +}
1514 +
1515 +static int wrr_change(struct Qdisc *sch, u32 handle, u32 parent,
1516 +                     struct rtattr **tca, unsigned long *arg)
1517 +{
1518 +       unsigned long cl = *arg;
1519 +       struct wrr_sched_data *q = qdisc_priv(sch);
1520 +       struct rtattr *opt = tca[TCA_OPTIONS - 1];
1521 +       struct tc_wrr_class_modf *copt = RTA_DATA(opt);
1522 +
1523 +       if (cl == 0 || cl > q->bandc)
1524 +               return -EINVAL;
1525 +       cl--;
1526 +
1527 +       if (opt->rta_len < RTA_LENGTH(sizeof(*copt)))
1528 +               return -EINVAL;
1529 +
1530 +       LOCK_START;
1531 +
1532 +       weight_setvalue(&q->bands[cl].class_modf.weight1, &copt->weight1);
1533 +       weight_setvalue(&q->bands[cl].class_modf.weight2, &copt->weight2);
1534 +
1535 +       LOCK_END;
1536 +
1537 +       return 0;
1538 +}
1539 +
1540 +static void wrr_walk(struct Qdisc *sch, struct qdisc_walker *arg)
1541 +{
1542 +       struct wrr_sched_data *q = qdisc_priv(sch);
1543 +       int prio;
1544 +
1545 +       if (arg->stop)
1546 +               return;
1547 +
1548 +       for (prio = 1; prio <= q->bandc; prio++) {
1549 +               if (arg->count < arg->skip) {
1550 +                       arg->count++;
1551 +                       continue;
1552 +               }
1553 +               if (arg->fn(sch, prio, arg) < 0) {
1554 +                       arg->stop = 1;
1555 +                       break;
1556 +               }
1557 +               arg->count++;
1558 +       }
1559 +}
1560 +
1561 +static struct tcf_proto **wrr_find_tcf(struct Qdisc *sch, unsigned long cl)
1562 +{
1563 +       return NULL;
1564 +}
1565 +
1566 +static unsigned long wrr_bind(struct Qdisc *sch,
1567 +                             unsigned long parent, u32 classid)
1568 +{
1569 +       return wrr_get(sch, classid);
1570 +}
1571 +
1572 +/*
1573 + * General
1574 + */
1575 +
1576 +static struct Qdisc_class_ops wrr_class_ops = {
1577 +       .graft = wrr_graft,
1578 +       .leaf = wrr_leaf,
1579 +       .get = wrr_get,
1580 +       .put = wrr_put,
1581 +       .change = wrr_change,
1582 +       .delete = wrr_delete,
1583 +       .walk = wrr_walk,
1584 +       .tcf_chain = wrr_find_tcf,
1585 +       .bind_tcf = wrr_bind,
1586 +       .unbind_tcf = wrr_put,
1587 +       .dump = wrr_dump_class,
1588 +};
1589 +
1590 +static struct Qdisc_ops wrr_qdisc_ops = {
1591 +       .next = NULL,
1592 +       .cl_ops = &wrr_class_ops,
1593 +       .id = "wrr",
1594 +       .priv_size = sizeof(struct wrr_sched_data),
1595 +       .enqueue = wrr_enqueue,
1596 +       .dequeue = wrr_dequeue,
1597 +       .requeue = wrr_requeue,
1598 +       .drop = wrr_drop,
1599 +       .init = wrr_init,
1600 +       .reset = wrr_reset,
1601 +       .destroy = wrr_destroy,
1602 +       .change = wrr_tune,
1603 +       .dump = wrr_dump,
1604 +       .owner = THIS_MODULE,
1605 +};
1606 +
1607 +static int __init wrr_module_init(void)
1608 +{
1609 +       return register_qdisc(&wrr_qdisc_ops);
1610 +}
1611 +
1612 +static void __exit wrr_module_exit(void)
1613 +{
1614 +       unregister_qdisc(&wrr_qdisc_ops);
1615 +}
1616 +
1617 +module_init(wrr_module_init)
1618 +module_exit(wrr_module_exit)
1619 +
1620 +MODULE_LICENSE("GPL");
1621 +MODULE_AUTHOR("Christian Worm Mortensen");
1622 diff -urN linux-2.6.25.orig/net/sched/wrr_proxydict.c linux-2.6.25/net/sched/wrr_proxydict.c
1623 --- linux-2.6.25.orig/net/sched/wrr_proxydict.c 1970-01-01 01:00:00.000000000 +0100
1624 +++ linux-2.6.25/net/sched/wrr_proxydict.c      2008-04-24 16:19:07.000000000 +0200
1625 @@ -0,0 +1,156 @@
1626 +#ifndef __KERNEL__
1627 +#include <string.h>
1628 +#include <netinet/in.h>
1629 +#endif
1630 +
1631 +#include <linux/wrr.h>
1632 +
1633 +/* Proxy remapping part of WRR */
1634 +
1635 +/* Hash function */
1636 +#define hash_fnc(m,server,port,proto) \
1637 + (((proto)*7+(server)*13+(port)*5)%m->hash_size)
1638 +
1639 +/* Size of hash table given maximal number of connections */
1640 +#define hash_size_max_con(max_con) (2*(max_con))
1641 +
1642 +/* The memory area we maintain
1643 +
1644 +   Given a connection we map it by hash_fnc into hash_table. This gives an
1645 +   index in next which contains a -1 terminated linked list of connections
1646 +   mapping to that hash value.
1647 +
1648 +   The entries in next not allocated is also in linked list where
1649 +   the first free index is free_first.
1650 +*/
1651 +
1652 +typedef struct {
1653 +       int hash_size;
1654 +       int max_con;
1655 +       int cur_con;
1656 +
1657 +       int free_first;
1658 +
1659 +} proxy_memory;
1660 +
1661 +#define Memory(m)     ((proxy_memory*)m)
1662 +#define Hash_table(m) ((int*)(((char*)m)+sizeof(proxy_memory)))
1663 +#define Next(m)       ((int*)(((char*)m)+sizeof(proxy_memory)+     \
1664 +                       sizeof(int)*((proxy_memory*)m)->hash_size))
1665 +#define Info(m)       ((ProxyRemapBlock*)(((char*)m)+                          \
1666 +                                           sizeof(proxy_memory)+                     \
1667 +                                           sizeof(int)*((proxy_memory*)m)->hash_size+\
1668 +                                          sizeof(int)*((proxy_memory*)m)->max_con   \
1669 +                                         ))
1670 +
1671 +int proxyGetMemSize(int max_con)
1672 +{
1673 +       return sizeof(proxy_memory) +
1674 +           sizeof(int) * hash_size_max_con(max_con) +
1675 +           sizeof(int) * max_con + sizeof(ProxyRemapBlock) * max_con;
1676 +}
1677 +
1678 +void proxyInitMem(void *data, int max_con)
1679 +{
1680 +       proxy_memory *m = Memory(data);
1681 +       m->max_con = max_con;
1682 +       m->cur_con = 0;
1683 +       m->hash_size = hash_size_max_con(max_con);
1684 +
1685 +       {
1686 +               /* Get pointers */
1687 +               int *hash_table = Hash_table(data);
1688 +               int *next = Next(data);
1689 +               int i;
1690 +
1691 +               /* Init the hash table */
1692 +               for (i = 0; i < m->hash_size; i++)
1693 +                       hash_table[i] = -1;
1694 +
1695 +               /* Init the free-list */
1696 +               for (i = 0; i < m->max_con; i++)
1697 +                       next[i] = i + 1;
1698 +               m->free_first = 0;
1699 +       }
1700 +}
1701 +
1702 +int proxyGetCurConn(void *data)
1703 +{
1704 +       return Memory(data)->cur_con;
1705 +}
1706 +
1707 +int proxyGetMaxConn(void *data)
1708 +{
1709 +       return Memory(data)->max_con;
1710 +}
1711 +
1712 +ProxyRemapBlock *proxyLookup(void *data, unsigned ipaddr, unsigned short port,
1713 +                            char proto)
1714 +{
1715 +       proxy_memory *m = Memory(data);
1716 +       int *hash_table = Hash_table(m);
1717 +       int *next = Next(m);
1718 +       ProxyRemapBlock *info = Info(m);
1719 +       int i;
1720 +
1721 +       for (i = hash_table[hash_fnc(m, ipaddr, port, proto)]; i != -1;
1722 +            i = next[i]) {
1723 +               if (info[i].proto == proto && info[i].sport == port
1724 +                   && info[i].saddr == ipaddr)
1725 +                       return &info[i];
1726 +       }
1727 +
1728 +       return 0;
1729 +}
1730 +
1731 +int proxyConsumeBlock(void *data, ProxyRemapBlock * blk)
1732 +{
1733 +       proxy_memory *m = Memory(data);
1734 +       int *hash_table = Hash_table(m);
1735 +       int *next = Next(m);
1736 +       ProxyRemapBlock *info = Info(m);
1737 +       int hash = hash_fnc(m, blk->saddr, blk->sport, blk->proto);
1738 +       int foo;
1739 +
1740 +       if (blk->open) {
1741 +               if (m->cur_con == m->max_con)
1742 +                       return -1;
1743 +
1744 +               /* Insert the block at a free entry */
1745 +               info[m->free_first] = *blk;
1746 +               m->cur_con++;
1747 +
1748 +               foo = next[m->free_first];
1749 +
1750 +               /* And insert it in the hash tabel */
1751 +               next[m->free_first] = hash_table[hash];
1752 +               hash_table[hash] = m->free_first;
1753 +               m->free_first = foo;
1754 +       } else {
1755 +               int *toupdate;
1756 +
1757 +               /* Find the block */
1758 +               for (toupdate = &hash_table[hash];
1759 +                    *toupdate != -1; toupdate = &next[*toupdate]) {
1760 +                       if (info[*toupdate].proto == blk->proto &&
1761 +                           info[*toupdate].sport == blk->sport &&
1762 +                           info[*toupdate].saddr == blk->saddr)
1763 +                               break;
1764 +               }
1765 +               if (*toupdate == -1)
1766 +                       return -1;
1767 +
1768 +               foo = *toupdate;
1769 +
1770 +               /* Delete it from the hashing list */
1771 +               *toupdate = next[*toupdate];
1772 +
1773 +               /* And put it on the free list */
1774 +               next[foo] = m->free_first;
1775 +               m->free_first = foo;
1776 +
1777 +               m->cur_con--;
1778 +       }
1779 +
1780 +       return 0;
1781 +}
This page took 0.160344 seconds and 3 git commands to generate.