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