jeste drobny bugfix a hotovo
[svn/Prometheus-QoS/.git] / prometheus.c
1 /* =============================================================== */
2 /* == Prometheus QoS - you can "steal fire" from your ISP == */
3 /* == "fair-per-IP" quality of service (QoS) utility == */
4 /* == requires Linux 2.4.x or 2.6.x with HTB support == */
5 /* == Copyright(C) 2005-2008 Michael Polak (xChaos) == */
6 /* == iptables-restore support Copyright(C) 2007-2008 ludva == */
7 /* == Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf == */
8 /* =============================================================== */
9
10 /* Modified: xChaos, 20080201
11 ludva, 20071227
12
13 Prometheus QoS is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2.1 of
16 the License, or (at your option) any later version.
17
18 Prometheus QoS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with Prometheus Qos; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
27 GNU General Public License is located in file COPYING */
28
29 #define STRLEN 256
30 #define FIRSTGROUPID 1024
31 #define FIRSTIPCLASS 2048
32 #undef DEBUG
33
34 #include "cll1-0.6.h"
35
36 const char *version="0.7.8"; /*0.7.9 will be last development, 0.8.0 first stable */
37
38 /* ======= Help screen is hopefuly self-documenting part of code :-) ======= */
39
40 void help(void)
41 {
42 puts("Command line switches:\n\
43 \n\
44 -?, --help this help screen\n\
45 -v, --version show version number of this utility and exit\n\
46 -c filename force alternative /etc/prometheus.conf filename\n\
47 -h filename force alternative /etc/hosts filename (overrides hosts keyword)\n\
48 -f just flush iptables and tc classes and exit (stop shaping)\n\
49 -9 emergency iptables flush (do not read data transfer statistics)\n\
50 -p just generate preview of data transfer statistics and exit\n\
51 -n no delay (overrides qos-free-delay keyword)\n\
52 -d dry run (preview tc and iptables commands on stdout)\n\
53 ");
54 }
55
56 /* === Configuraration file values defaults - stored in global variables ==== */
57
58 int filter_type=1; /*1 mark, 2 classify*/
59 char *mark="MARK";
60 char *mark_iptables="MARK --set-mark ";
61 int dry_run=0; /* preview - use puts() instead of system() */
62 char *config="/etc/prometheus/prometheus.conf"; /* main configuration file */
63 char *hosts="/etc/prometheus/hosts"; /* line bandwidth definition file */
64 char *tc="/sbin/tc"; /* requires tc with HTB support */
65 char *iptables="/sbin/iptables"; /* requires iptables utility */
66 char *iptablessave="/sbin/iptables-save"; /* new */
67 char *iptablesrestore="/sbin/iptables-restore"; /* new */
68 char *iptablesfile="/var/spool/prometheus.iptables"; /* new; file for iptables-restore*/
69 char *iptablespreamble="*mangle\n:PREROUTING ACCEPT [0:0]\n:POSTROUTING ACCEPT [0:0]\n:INPUT ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]";
70 FILE *iptables_file=NULL;
71 char *html="/var/www/traffic.html"; /* hall of fame filename */
72 char *preview="/var/www/preview.html"; /* hall of fame preview */
73 char *cmdlog="/var/log/prometheus"; /* command log filename */
74 char *credit="/var/run/prometheus.credit"; /* credit log file */
75 int enable_credit=1; /* enable credit file */
76 int use_credit=0; /* use credit file (if enabled)*/
77 char *log_dir="/var/www/logs/"; /* log directory pathname */
78 char *log_url="logs/"; /* log directory URL prefix */
79 char *title="Hall of Fame - Greatest Suckers"; /* hall of fame title */
80 int hall_of_fame=1; /* enable hall of fame */
81 char *lan="eth0"; /* LAN interface */
82 char *lan_medium="100Mbit"; /* 10Mbit/100Mbit ethernet */
83 char *wan="eth1"; /* WAN/ISP interface */
84 char *wan_medium="100Mbit"; /* 10Mbit/100Mbit ethernet */
85 char *qos_leaf="sfq perturb 5"; /* leaf discipline */
86 char *qos_free_zone=NULL; /* QoS free zone */
87 int qos_proxy=1; /* include proxy port to QoS */
88 int include_upload=1; /* upload+download=total traffic */
89 char *proxy_ip="192.168.1.1/32"; /* our IP with proxy port */
90 int proxy_port=3128; /* proxy port number */
91 long long int line=1024; /* WAN/ISP download in kbps */
92 long long int up=1024; /* WAN/ISP upload in kbps */
93 int free_min=32; /* minimum guaranted bandwidth for all undefined hosts */
94 int free_max=64; /* maximum allowed bandwidth for all undefined hosts */
95 int qos_free_delay=0; /* seconds to sleep before applying new QoS rules */
96 int digital_divide=2; /* controls digital divide weirdness ratio, 1...3 */
97 int max_nesting=3; /* maximum nesting of HTB clases, built-in maximum seems to be 4 */
98 int htb_r2q=1;
99 int burst=8; /* HTB burst (in kbits) */
100 int burst_main=64;
101 int burst_group=32;
102 int magic_priorities=8; /* number of priority levels (soft shaping) */
103 int magic_treshold=8; /* reduce ceil by X*magic_treshhold kbps (hard shaping) */
104 int keywordcount=0;
105
106 /* not yet implemented:
107 int fixed_packets=0; maximum number of pps per IP address (not class!)
108 int packet_limit=5; maximum number of pps to htn CEIL, not rate !!!
109 */
110 FILE *log_file=NULL;
111
112 char *kwd="via-prometheus"; /* /etc/hosts comment, eg. #qos-64-128 */
113
114 const int idxtable_treshold1=24; /* this is no longer configurable */
115 const int idxtable_treshold2=12; /* this is no longer configurable */
116 const int idxtable_bitmask1=3; /* this is no longer configurable */
117 const int idxtable_bitmask2=3; /* this is no longer configurable */
118
119
120 /* not yet implemented:
121 -s start shaping! (keep data transfer statistics - but apply shaping)\n\
122 -r just reload configuration (...and keep data transfer statistics)\n\
123 */
124
125 /* ==== This is C<<1 stuff - learn C<<1 first! http://cll1.arachne.cz ==== */
126
127 struct IP
128 {
129 char *addr;
130 char *name;
131 char *sharing;
132 int min;
133 int desired;
134 int max;
135 int mark;
136 int prio;
137 int fixedprio;
138 int group;
139 unsigned long long direct;
140 unsigned long long proxy;
141 unsigned long long upload;
142 unsigned long long traffic;
143 unsigned long long credit;
144 unsigned long pktsup;
145 unsigned long pktsdown;
146 struct Keyword *keyword;
147 list(IP);
148 } *ips=NULL, *ip, *sharedip;
149
150 struct Group
151 {
152 int min;
153 int count;
154 int desired;
155 int id;
156 list(Group);
157 } *groups=NULL, *group;
158
159 struct Index
160 {
161 char *addr;
162 char *id;
163 struct Index *parent;
164 int bitmask;
165 int children;
166 list(Index);
167 } *idxs=NULL, *idx, *metaindex;
168
169 struct Keyword
170 {
171 char *key;
172
173 int asymetry_ratio; /* ratio for ADSL-like upload */
174 int asymetry_fixed; /* fixed treshold for ADSL-like upload */
175 int data_limit; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
176 int data_prio; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
177 long fixed_limit; /* fixed data limit for setting lower HTB ceil */
178 long fixed_prio; /* fixed data lmit for setting lower HTB prio */
179 int reserve_min; /* bonus for nominal HTB rate bandwidth (in kbps) */
180 int reserve_max; /* malus for nominal HTB ceil (in kbps) */
181 // int divide_max; /* relative malus: new_ceil=rate+(old_ceil-rate)/divide_max */
182 // int htb_ceil_bonus_divide; /* relative bonus: new_ceil=old_ceil+old_ceil/htb_ceil_bonus_divide */
183 int default_prio; /* default HTB priority for this keyword */
184 char *html_color;
185 int ip_count;
186 char *leaf_discipline;
187
188 list(Keyword);
189 } *keyword,*defaultkeyword=NULL,*keywords=NULL;
190
191 /* Damned, this must be object oriented! This looks almost like constructor ;-) */
192
193 void TheIP(void)
194 {
195 create(ip,IP);
196 ip->name="";
197 ip->addr="";
198 ip->sharing=NULL;
199 ip->prio=1;
200 ip->fixedprio=0;
201 ip->mark=ip->min=ip->max=ip->desired=ip->credit=0;
202 ip->upload=ip->proxy=ip->direct=ip->traffic=0;
203 ip->pktsup=ip->pktsdown=0;
204 ip->keyword=keywords;
205 push(ip,ips);
206 }
207
208 /* ====== Iptables indexes are used to reduce complexity to log8(N) ===== */
209
210 char *very_ugly_ipv4_code(char *inip,int bitmask,int format_as_chainname)
211 {
212 /* warning: this function was debugged only for bitmask values 20,24,28 !!!*/
213 int dot=0,n;
214 char *ip,*outip,*outptr,*fmt;
215
216 duplicate(inip,ip);
217 /* debug printf("(%s,%d) -> ",ip,bitmask); */
218
219 if(ip && *ip && bitmask>=0 && bitmask<=32)
220 string(outip,strlen(ip)+10); /*fuck unicode? assertion: 10>strlen("_%d_%d") */
221 else
222 /* should never exit here */
223 return "undefined";
224 outptr=outip;
225 while(ip && *ip)
226 {
227 if(*ip=='.')
228 {
229 if(dot<(bitmask/8-1))
230 {
231 if(format_as_chainname)
232 *outptr='_';
233 else
234 *outptr='.';
235 outptr++;
236 dot++;
237 }
238 else
239 {
240 char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/
241 if(cutdot)*cutdot='\0';
242 if(format_as_chainname)
243 fmt="_%d_%d";
244 else
245 fmt=".%d";
246 if(bitmask%8)
247 n=atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8));
248 else
249 n=0;
250
251 /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */
252 sprintf(outptr,fmt,n,bitmask);
253 if(!format_as_chainname) while(bitmask<24)
254 {
255 strcat(outip,".0");
256 bitmask+=8;
257 }
258 /* debug printf("[%s]\n",outip); */
259 return outip;
260 }
261 }
262 else
263 {
264 *outptr=*ip;
265 outptr++;
266 }
267 ip++;
268 }
269 /*should never exit here*/
270 *outptr='\0';
271 return outip;
272 }
273
274 char *hash_id(char *ip,int bitmask)
275 { return very_ugly_ipv4_code(ip,bitmask,1); }
276
277 char *subnet_id(char *ip,int bitmask)
278 { return very_ugly_ipv4_code(ip,bitmask,0); }
279
280 /* ================= Let's parse configuration file here =================== */
281
282 void reject_config_and_exit(char *filename)
283 {
284 printf("Configuration file %s rejected - abnormal exit.",filename);
285 exit(-1);
286 }
287
288 void get_config(char *config_filename)
289 {
290 char *cnf="mark";
291
292 printf("Configured keywords: ");
293 parse(config_filename)
294 {
295 option("keyword",kwd);
296 if(kwd)
297 {
298 printf("%s ",kwd);
299
300 create(keyword,Keyword);
301 keyword->key=kwd;
302 keyword->asymetry_ratio=1; /* ratio for ADSL-like upload */
303 keyword->asymetry_fixed=0; /* fixed treshold for ADSL-like upload */
304 keyword->data_limit=8; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
305 keyword->data_prio=4; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
306 keyword->fixed_limit=0; /* fixed data limit for setting lower HTB ceil */
307 keyword->fixed_prio=0; /* fixed data limit for setting lower HTB prio */
308 keyword->reserve_min=8; /* bonus for nominal HTB rate bandwidth (in kbps) */
309 keyword->reserve_max=0; /* malus for nominal HTB ceil (in kbps) */
310 /* obsolete:
311 keyword->divide_max=0; relative malus: new_ceil=rate+(old_ceil-rate)/divide_max
312 keyword->htb_ceil_bonus_divide=0; relative bonus: new_ceil=old_ceil+old_ceil/htb_ceil_bonus_divide
313 */
314 keyword->default_prio=1;
315 keyword->html_color="000000";
316 keyword->ip_count=0;
317 keyword->leaf_discipline="";
318
319 push(keyword,keywords);
320 if(!defaultkeyword) defaultkeyword=keyword;
321 keywordcount++;
322
323 kwd=NULL;
324 }
325 else every(keyword,keywords)
326 {
327 int l=strlen(keyword->key);
328
329
330 if(!strncmp(keyword->key,_,l) && strlen(_)>l+2)
331 {
332 char *tmptr=_; /* <---- l+1 ----> */
333 _+=l+1; /* via-prometheus-asymetry-ratio, etc. */
334 ioption("asymetry-ratio",keyword->asymetry_ratio);
335 ioption("asymetry-treshold",keyword->asymetry_fixed);
336 ioption("magic-relative-limit",keyword->data_limit);
337 ioption("magic-relative-prio",keyword->data_prio);
338 loption("magic-fixed-limit",keyword->fixed_limit);
339 loption("magic-fixed-prio",keyword->fixed_prio);
340 ioption("htb-default-prio",keyword->default_prio);
341 ioption("htb-rate-bonus",keyword->reserve_min);
342 ioption("htb-ceil-malus",keyword->reserve_max);
343 /* obsolete:
344 ioption("htb-ceil-divide",keyword->divide_max);
345 ioption("htb-ceil-bonus-divide",keyword->htb_ceil_bonus_divide);
346 */
347 option("leaf-discipline",keyword->leaf_discipline);
348 option("html-color",keyword->html_color);
349 _=tmptr;
350
351 if(keyword->data_limit || keyword->fixed_limit ||
352 keyword->data_prio || keyword->fixed_prio)
353 use_credit=1;
354
355
356 }
357 }
358
359 option("tc",tc);
360 option("iptables",iptables);
361 option("iptables-save",iptablessave); /* new */
362 option("iptables-restore",iptablesrestore); /* new */
363 option("iptables-file",iptablesfile); /* new */
364 option("hosts",hosts);
365 option("lan-interface",lan);
366 option("wan-interface",wan);
367 option("lan-medium",lan_medium);
368 option("wan-medium",wan_medium);
369 lloption("wan-download",line);
370 lloption("wan-upload",up);
371 ioption("hall-of-fame-enable",hall_of_fame);
372 option("hall-of-fame-title",title);
373 option("hall-of-fame-filename",html);
374 option("hall-of-fame-preview",preview);
375 option("log-filename",cmdlog);
376 option("credit-filename",credit);
377 ioption("credit-enable",enable_credit);
378 option("log-traffic-directory",log_dir);
379 option("log-traffic-url-path",log_url);
380 option("qos-free-zone",qos_free_zone);
381 ioption("qos-free-delay",qos_free_delay);
382 ioption("qos-proxy-enable",qos_proxy);
383 option("qos-proxy-ip",proxy_ip);
384 option("htb-leaf-discipline",qos_leaf);
385 ioption("qos-proxy-port",proxy_port);
386 ioption("free-rate",free_min);
387 ioption("free-ceil",free_max);
388 ioption("htb-burst",burst);
389 ioption("htb-burst-main",burst_main);
390 ioption("htb-burst-group",burst_group);
391 ioption("htb-nesting-limit",max_nesting);
392 ioption("htb-r2q",htb_r2q);
393 ioption("magic-include-upload",include_upload);
394 ioption("magic-priorities",magic_priorities);
395 ioption("magic-treshold",magic_treshold);
396
397 option("filter-type", cnf);
398
399 /* not yet implemented:
400 ioption("magic-fixed-packets",fixed_packets);
401 ioption("magic-relative-packets",packet_limit);
402 */
403 }
404 fail
405 {
406 perror(config_filename);
407 puts("Warning - using built-in defaults instead ...");
408 }
409 done;
410 printf("\n");
411
412 /*leaf discipline for keywords*/
413 every(keyword,keywords)
414 {
415 if (!strcmpi(keyword->leaf_discipline, "")){
416 keyword->leaf_discipline = qos_leaf;
417 }
418 }
419
420 if (strcmpi(cnf, "mark")){
421 filter_type = 2;
422 mark = "CLASSIFY";
423 mark_iptables = "CLASSIFY --set-class 1:";
424 }else{
425 filter_type = 1;
426 mark = "MARK";
427 mark_iptables = "MARK --set-mark ";
428 }
429
430 /* are supplied values meaningful ?*/
431 if(line<=0 || up<=0)
432 {
433 puts("Illegal value of wan bandwidth: 0 kbps.");
434 reject_config_and_exit(config_filename);
435 }
436 }
437
438 /* ===================== traffic analyser - uses iptables ================ */
439
440 void get_traffic_statistics(void)
441 {
442 char *str,*cmd;
443 int downloadflag=0;
444
445 textfile(Pipe,str) *line,*lines=NULL;
446 string(str,STRLEN);
447 string(cmd,STRLEN);
448
449 sprintf(cmd,"%s -L -v -x -n -t mangle",iptables);
450 shell(cmd);
451 input(str,STRLEN)
452 {
453 create(line,Pipe);
454 line->str=str;
455 string(str,STRLEN);
456 append(line,lines);
457 }
458
459 every(line,lines)
460 {
461 int col, accept=0,proxyflag=0,valid=1,setchainname=0,commonflag=0;
462 unsigned long long traffic=0;
463 unsigned long pkts=0;
464 char *ipaddr=NULL,*ptr;
465
466 /* debug puts(line->str); */
467 valid_columns(ptr,line->str,' ',col)
468 if(valid) switch(col)
469 {
470 case 1: if(eq(ptr,"Chain"))
471 setchainname=1;
472 else if(eq(ptr,"pkts"))
473 valid=0;
474 else
475 sscanf(ptr,"%lu",&pkts);
476 break;
477 case 2: if(setchainname)
478 {
479 if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))
480 downloadflag=1;
481 else
482 if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))
483 downloadflag=0;
484
485 if(eq(ptr,"post_common") || eq(ptr,"forw_common"))
486 commonflag=1;
487 }
488 else
489 sscanf(ptr,"%Lu",&traffic); traffic+=(1<<19); traffic>>=20;
490 break;
491 case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)
492 accept=eq(ptr,mark);
493 /*if (filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/
494 break;
495 case 8: if(downloadflag)
496 {
497 if(strstr(proxy_ip,ptr))proxyflag=1;
498 }
499 else
500 ipaddr=ptr;
501 break;
502 case 9: if(downloadflag)ipaddr=ptr;break;
503 }
504
505 if(accept && traffic>0 && ipaddr)
506 {
507 if(proxyflag)printf("(proxy) ");
508 else if(!downloadflag) printf("(upload) ");
509 printf("IP %s: %Lu M (%ld pkts)\n", ipaddr, traffic, pkts);
510 find(ip,ips,eq(ip->addr,ipaddr));
511 else
512 {
513 TheIP();
514 ip->addr=ipaddr;
515 if(eq(ip->addr,"0.0.0.0/0"))
516 {
517 ip->name="(unregistered)";
518 ip->min=free_min;
519 ip->max=ip->desired=free_max;
520 }
521 }
522
523 if(downloadflag)
524 {
525 if(proxyflag)
526 ip->proxy=traffic;
527 else
528 ip->traffic+=traffic;
529 ip->direct=ip->traffic-ip->upload-ip->proxy;
530 ip->pktsdown=pkts;
531 }
532 else
533 {
534 ip->upload=traffic;
535 ip->pktsup=pkts;
536 if(include_upload)
537 ip->traffic+=traffic;
538 else
539 if(traffic>ip->traffic)
540 ip->traffic=traffic;
541 }
542 }
543 }
544
545
546 free(cmd);
547 }
548
549 /* ========== This function executes, logs OR ALSO prints command ========== */
550
551 void safe_run(char *cmd)
552 {
553 if(dry_run) printf("\n=>%s\n",cmd); else system(cmd);
554 if(log_file) fprintf(log_file,"%s\n",cmd);
555 }
556
557 void save_line(char *line)
558 {
559 fprintf(iptables_file,"%s\n",line);
560 }
561
562 void run_restore(void)
563 {
564 char *restor, *str;
565 string(restor,STRLEN);
566
567 /*-----------------------------------------------------------------*/
568 printf("Running %s <%s ...\n",iptablesrestore,iptablesfile);
569 /*-----------------------------------------------------------------*/
570
571 save_line("COMMIT");
572 fclose(iptables_file);
573 if(dry_run) {
574 parse(iptablesfile)
575 {
576 str=_;
577 printf("%s\n", str);
578 }done;
579 }else{
580 //sprintf(restor,"cat %s",iptablesfile); else
581 sprintf(restor,"%s <%s",iptablesrestore, iptablesfile);
582 system(restor);
583 };
584
585 free(restor);
586 }
587
588 /* == This function strips extra characters after IP address and stores it = */
589
590 void parse_ip(char *str)
591 {
592 char *ptr=str,*ipaddr=NULL,*ipname=NULL;;
593
594 while(*ptr && *ptr!=' ' && *ptr!=9)
595 ptr++;
596
597 *ptr=0;
598 ipaddr=str;
599 ptr++;
600 while(*ptr && (*ptr==' ' || *ptr==9))
601 ptr++;
602 ipname=ptr;
603 while(*ptr && *ptr!=' ' && *ptr!=9)
604 ptr++;
605 *ptr=0;
606
607 find(ip,ips,eq(ip->addr,ipaddr)); else TheIP();
608 ip->addr=ipaddr;
609 ip->name=ipname;
610 }
611
612 char *parse_datafile_line(char *str)
613 {
614 char *ptr=strchr(str,' ');
615
616 if(ptr)
617 {
618 *ptr=0;
619 ptr++;
620 return ptr;
621 }
622 else
623 return NULL;
624 }
625
626 /*-----------------------------------------------------------------*/
627 /* Are you looking for int main (int argc, char **argv) ? :-)) */
628 /*-----------------------------------------------------------------*/
629
630 program
631 {
632 int i=0;
633 FILE *f=NULL;
634 char *str, *ptr, *d;
635 char *substring;
636 int class_count=0,ip_count=0;
637 int parent=1;
638 int just_flush=0;
639 int nodelay=0;
640 int just_preview=0; /* preview - generate just stats */
641 char *chain_forward, *chain_postrouting;
642 char *althosts=NULL;
643
644 printf("\n\
645 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\
646 Version %s - Copyright (C)2005-2008 Michael Polak (xChaos)\n\
647 iptables-restore & burst tunning & classify modification 0.7d by Ludva\n\
648 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
649
650 /*----- Boring... we have to check command line options first: ----*/
651
652 arguments
653 {
654 argument("-c") { nextargument(config); }
655 argument("-h") { nextargument(althosts);}
656 argument("-d") { dry_run=1; }
657 argument("-f") { just_flush=1; }
658 argument("-9") { just_flush=9; }
659 argument("-p") { just_preview=1; }
660 argument("-n") { nodelay=1; }
661 argument("-?") { help(); exit(0); }
662 argument("--help") { help(); exit(0); }
663 argument("-v") { exit(0); }
664 argument("--version") { exit(0); }
665 }
666
667 if(dry_run)
668 puts("*** THIS IS JUST DRY RUN ! ***\n");
669
670 date(d); /* this is typical cll1.h macro */
671
672 /*-----------------------------------------------------------------*/
673 printf("Parsing configuration file %s ...\n", config);
674 /*-----------------------------------------------------------------*/
675 get_config(config);
676
677 if(althosts) hosts=althosts;
678
679 if(just_flush<9)
680 {
681 /*-----------------------------------------------------------------*/
682 puts("Parsing iptables verbose output ...");
683 /*-----------------------------------------------------------------*/
684 get_traffic_statistics();
685 }
686
687 /*-----------------------------------------------------------------*/
688 printf("Parsing class defintion file %s ...\n", hosts);
689 /*-----------------------------------------------------------------*/
690 int groupidx = FIRSTGROUPID;
691 parse(hosts)
692 {
693 str=_;
694
695 if(*str<'0' || *str>'9')
696 continue;
697
698 //Does this IP share QoS class with some other ?
699 substring=strstr(str,"sharing-");
700 if(substring)
701 {
702 substring+=8; //"sharing-"
703 parse_ip(str);
704 ip_count++;
705 ip->sharing=substring;
706 ip->keyword=defaultkeyword; /* settings for default keyword */
707 while(*substring && *substring!='\n')
708 substring++;
709 *substring=0;
710 }
711 else
712 {
713 //Do we have to create new QoS class for this IP ?
714
715 find(keyword,keywords,(substring=strstr(str,keyword->key)))
716 {
717 parse_ip(str);
718 ip_count++;
719 ip->keyword=keyword;
720 keyword->ip_count++;
721 ip->prio=keyword->default_prio;
722 substring+=strlen(keyword->key)+1;
723 ptr=substring;
724 while(*ptr && *ptr!='-')
725 ptr++;
726 if(*ptr=='-')
727 {
728 *ptr=0;
729 ip->max=ip->desired=atoi(ptr+1);
730 }
731 ip->min=atoi(substring);
732 if(ip->min<=0)
733 {
734 puts("Illegal value of minimum bandwidth: 0 kbps.");
735 reject_config_and_exit(hosts);
736 }
737 if(ip->max<=ip->min)
738 {
739 ip->fixedprio=1;
740 ip->max=ip->min+ip->keyword->reserve_min;
741 }
742 else
743 {
744 ip->max-=ip->keyword->reserve_max;
745
746 /*
747 if(ip->keyword->divide_max>1)
748 ip->max=ip->min+(ip->max-ip->min)/ip->keyword->divide_max;
749 if(ip->keyword->htb_ceil_bonus_divide>0)
750 ip->max+=ip->max/ip->keyword->htb_ceil_bonus_divide;
751 */
752 if(ip->max<ip->min)
753 ip->max=ip->min;
754 }
755 ip->mark=FIRSTIPCLASS+1+class_count++;
756
757 find(group,groups,group->min==ip->min)
758 {
759 group->count++;
760 group->desired+=ip->min;
761 ip->group = group->id;
762 }
763 else
764 {
765 create(group,Group);
766 group->min=ip->min;
767 group->id = groupidx++;
768 ip->group = group->id;
769
770 if(group->min<8) group->min=8;
771 /* Warning - this is maybe because of primitive tc namespace, can be fixed */
772 /* it is because class IDs are derived from min. bandwidth. - xCh */
773 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;
774
775 group->count=1;
776 group->desired=ip->min;
777 insert(group,groups,desc_order_by,min);
778 }
779 }//endif keyword-
780 }//endif sharing-
781 }
782 fail
783 {
784 perror(hosts);
785 exit(-1);
786 }
787 done;
788
789 /*-----------------------------------------------------------------*/
790 /* cll1.h - let's allocate brand new character buffer... */
791 /*-----------------------------------------------------------------*/
792 string(str,STRLEN);
793
794 /*-----------------------------------------------------------------*/
795 puts("Resolving shared connections ...");
796 /*-----------------------------------------------------------------*/
797 search(ip,ips,ip->sharing)
798 {
799 search(sharedip,ips,eq(sharedip->name,ip->sharing))
800 {
801 sharedip->traffic+=ip->traffic;
802 ip->traffic=0;
803 ip->mark=sharedip->mark;
804 break;
805 }
806 if(!sharedip)
807 printf("Unresolved shared connection: %s %s sharing-%s\n",ip->addr,ip->name,ip->sharing);
808 }
809
810 if(enable_credit && just_flush<9)
811 {
812 /*-----------------------------------------------------------------*/
813 printf("Parsing credit file %s ...\n", credit);
814 /*-----------------------------------------------------------------*/
815 parse(credit)
816 {
817 ptr=parse_datafile_line(_);
818 if(ptr)
819 {
820 find(ip,ips,eq(ip->addr,_))
821 sscanf(ptr,"%Lu",&(ip->credit));
822 }
823 }
824 done;
825 }
826
827 if(!just_preview)
828 {
829 /*-----------------------------------------------------------------*/
830 puts("Initializing iptables and tc classes ...");
831 /*-----------------------------------------------------------------*/
832
833 iptables_file=fopen(iptablesfile,"w");
834 if (iptables_file == NULL) {
835 puts("Cannot open iptablesfile!");
836 exit(-1);
837 }
838
839 log_file=fopen(cmdlog,"w");
840 if (log_file == NULL) {
841 puts("Cannot open logfile!");
842 exit(-1);
843 }
844
845 save_line(iptablespreamble);
846 run_restore();
847
848 sprintf(str,"%s qdisc del dev %s root 2>/dev/null",tc,lan);
849 safe_run(str);
850
851 sprintf(str,"%s qdisc del dev %s root 2>/dev/null",tc,wan);
852 safe_run(str);
853
854 iptables_file=fopen(iptablesfile,"w");
855 save_line(iptablespreamble);
856
857 if(qos_free_zone && *qos_free_zone!='0')
858 {
859 char *chain;
860
861 sprintf(str,"-A FORWARD -d %s -o %s -j ACCEPT", qos_free_zone, wan);
862 save_line(str);
863
864 if(qos_proxy)
865 {
866 save_line(":post_noproxy - [0:0]");
867 sprintf(str,"-A POSTROUTING -p ! tcp -o %s -j post_noproxy", lan);
868 save_line(str);
869 sprintf(str,"-A POSTROUTING -s ! %s -o %s -j post_noproxy", proxy_ip, lan);
870 save_line(str);
871 sprintf(str,"-A POSTROUTING -s %s -p tcp --sport ! %d -o %s -j post_noproxy", proxy_ip, proxy_port, lan);
872 save_line(str);
873
874 chain="post_noproxy";
875 }
876 else
877 chain="POSTROUTING";
878
879 sprintf(str,"-A %s -s %s -o %s -j ACCEPT", chain, qos_free_zone, lan);
880 save_line(str);
881 }
882
883 if(ip_count>idxtable_treshold1 && !just_flush)
884 {
885 int idxcount=0, bitmask=32-idxtable_bitmask1; /* default net mask: 255.255.255.240 */
886 char *subnet, *buf;
887 /*-----------------------------------------------------------------*/
888 printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count);
889 /*-----------------------------------------------------------------*/
890
891 save_line(":post_common - [0:0]");
892 save_line(":forw_common - [0:0]");
893
894 search(ip,ips,ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0"))
895 {
896 buf=hash_id(ip->addr,bitmask);
897 find(idx,idxs,eq(idx->id,buf))
898 idx->children++;
899 else
900 {
901 create(idx,Index);
902 idx->addr=ip->addr;
903 idx->id=buf;
904 idx->bitmask=bitmask;
905 idx->parent=NULL;
906 idx->children=0;
907 idxcount++;
908 push(idx,idxs);
909 }
910 }
911
912 /* brutal perfomance optimalization */
913 while(idxcount>idxtable_treshold2 && bitmask>2*idxtable_bitmask2)
914 {
915 bitmask-=idxtable_bitmask2;
916 idxcount=0;
917 search(idx,idxs,idx->parent==NULL)
918 {
919 buf=hash_id(idx->addr,bitmask);
920 find(metaindex,idxs,eq(metaindex->id,buf))
921 metaindex->children++;
922 else
923 {
924 create(metaindex,Index);
925 metaindex->addr=idx->addr;
926 metaindex->id=buf;
927 metaindex->bitmask=bitmask;
928 metaindex->parent=NULL;
929 metaindex->children=0;
930 idxcount++;
931 push(metaindex,idxs);
932 }
933 idx->parent=metaindex;
934 }
935 }
936
937 /* this should slightly optimize throughout ... */
938 sort(idx,idxs,desc_order_by,children);
939 sort(idx,idxs,order_by,bitmask);
940
941 i=0;
942 every(idx,idxs)
943 {
944 subnet=subnet_id(idx->addr,idx->bitmask);
945 printf("%d: %s/%d\n",++i,subnet,idx->bitmask);
946
947 sprintf(str,":post_%s - [0:0]", idx->id);
948 save_line(str);
949
950 sprintf(str,":forw_%s - [0:0]", idx->id);
951 save_line(str);
952
953 if(idx->parent)
954 {
955 string(buf,strlen(idx->parent->id)+6);
956 sprintf(buf,"post_%s",idx->parent->id);
957 }
958 else
959 buf="POSTROUTING";
960
961 sprintf(str,"-A %s -d %s/%d -o %s -j post_%s", buf, subnet, idx->bitmask, lan, idx->id);
962 save_line(str);
963
964 sprintf(str,"-A %s -d %s/%d -o %s -j post_common", buf, subnet, idx->bitmask, lan);
965 save_line(str);
966
967 if(idx->parent)
968 {
969 string(buf,strlen(idx->parent->id)+6);
970 sprintf(buf,"forw_%s",idx->parent->id);
971 }
972 else
973 buf="FORWARD";
974
975 sprintf(str,"-A %s -s %s/%d -o %s -j forw_%s", buf, subnet, idx->bitmask, wan, idx->id);
976 save_line(str);
977
978 sprintf(str,"-A %s -s %s/%d -o %s -j forw_common", buf, subnet, idx->bitmask, wan);
979 save_line(str);
980 }
981 printf("Total indexed iptables chains created: %d\n", i);
982
983 sprintf(str,"-A FORWARD -o %s -j forw_common", wan);
984 save_line(str);
985
986 sprintf(str,"-A POSTROUTING -o %s -j post_common", lan);
987 save_line(str);
988 }
989
990 }
991
992 if(just_flush)
993 {
994 fclose(iptables_file);
995 if (log_file) fclose(log_file);
996 puts("Just flushed iptables and tc classes - now exiting ...");
997 exit(0);
998 }
999
1000 if(!just_preview)
1001 {
1002 if(!dry_run && !nodelay && qos_free_delay)
1003 {
1004 printf("Flushed iptables and tc classes - now sleeping for %d seconds...\n",qos_free_delay);
1005 sleep(qos_free_delay);
1006 }
1007
1008 sprintf(str,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",tc,lan,htb_r2q);
1009 safe_run(str);
1010
1011 sprintf(str,"%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio 0",tc,lan,lan_medium,lan_medium,burst_main);
1012 safe_run(str);
1013
1014 sprintf(str,"%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio 0",tc,lan,line,line,burst_main);
1015 safe_run(str);
1016
1017 sprintf(str,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",tc,wan,htb_r2q);
1018 safe_run(str);
1019
1020 sprintf(str,"%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio 0",tc,wan,wan_medium,wan_medium,burst_main);
1021 safe_run(str);
1022
1023 sprintf(str,"%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio 0",tc,wan,up,up,burst_main);
1024 safe_run(str);
1025 }
1026
1027 /*-----------------------------------------------------------------*/
1028 puts("Locating suckers and generating root classes ...");
1029 /*-----------------------------------------------------------------*/
1030 sort(ip,ips,desc_order_by,traffic);
1031
1032
1033 /*-----------------------------------------------------------------*/
1034 /* sub-scope - local variables */
1035 {
1036 long long int rate=line;
1037 long long int max=line;
1038 int group_count=0;
1039 FILE *credit_file=NULL;
1040
1041 if(!just_preview && !dry_run && enable_credit) credit_file=fopen(credit,"w");
1042
1043 every(group,groups)
1044 {
1045 if(!just_preview)
1046 {
1047
1048 //download
1049 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio 1 #down desired %d",
1050 tc, lan, parent, group->id, rate, max, burst_group, group->desired);
1051 safe_run(str);
1052
1053 //upload
1054 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio 1 #up desired %d",
1055 tc, wan, parent, group->id, rate*up/line, max*up/line, burst_group, group->desired);
1056 safe_run(str);
1057 }
1058
1059 if(group_count++<max_nesting) parent=group->id;
1060
1061 rate-=digital_divide*group->min;
1062 if(rate<group->min)rate=group->min;
1063
1064 /*shaping of aggresive downloaders, with credit file support */
1065 if(use_credit)
1066 {
1067 int group_rate=group->min, priority_sequence=magic_priorities+1;
1068
1069 search(ip, ips, ip->min==group->min && ip->max>ip->min)
1070 {
1071 if( ip->keyword->data_limit && !ip->fixedprio &&
1072 ip->traffic>ip->credit+
1073 (ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20)) )
1074 {
1075 if(group_rate<ip->max) ip->max=group_rate;
1076 group_rate+=magic_treshold;
1077 ip->prio=magic_priorities+2;
1078 if(ip->prio<3) ip->prio=3;
1079 }
1080 else
1081 {
1082 if( ip->keyword->data_prio && !ip->fixedprio &&
1083 ip->traffic>ip->credit+
1084 (ip->min*ip->keyword->data_prio+(ip->keyword->fixed_prio<<20)) )
1085 {
1086 ip->prio=priority_sequence--;
1087 if(ip->prio<2) ip->prio=2;
1088 }
1089
1090 if(credit_file)
1091 {
1092 unsigned long long lcredit=0;
1093
1094 if((ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))>ip->traffic)
1095 lcredit=(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))-ip->traffic;
1096 fprintf(credit_file,"%s %Lu\n",ip->addr,lcredit);
1097 }
1098 }
1099 }
1100
1101 }
1102 }
1103 if(credit_file)fclose(credit_file);
1104 }
1105
1106 if(just_preview)
1107 {
1108 f=fopen(preview,"w");
1109 ptr=preview;
1110 }
1111 else if(!dry_run && !just_flush)
1112 {
1113 /*-----------------------------------------------------------------*/
1114 printf("Writing data transfer database ...\n");
1115 /*-----------------------------------------------------------------*/
1116 f=fopen("/var/run/prometheus.previous","w");
1117 if(f)
1118 {
1119 search(ip,ips,ip->traffic || ip->direct || ip->proxy ||ip->upload)
1120 fprintf(f,"%s %Lu %Lu %Lu %Lu\n",ip->addr,ip->traffic,ip->direct,ip->proxy,ip->upload);
1121 fclose(f);
1122 }
1123
1124 f=fopen(html,"w");
1125 ptr=html;
1126 }
1127
1128 if(f)
1129 {
1130 int total=0;
1131 int count=1;
1132 i=0;
1133
1134 /*-----------------------------------------------------------------*/
1135 printf("Sorting data and generating statistics page %s ...\n",ptr);
1136 /*-----------------------------------------------------------------*/
1137
1138 fputs("<table border>\n<tr><th align=\"right\">#</th><th align=\"right\">group</th><th align=\"right\">IPs</th><th align=\"right\">requested</th>\n",f);
1139 fprintf(f,"<th colspan=\"%d\">data limits</th>\n",keywordcount);
1140 fputs("</tr>\n",f);
1141 every(group,groups)
1142 {
1143 #ifdef DEBUG
1144 printf("%d k group: %d bandwidth requested: %d k\n",group->min,group->count,group->desired);
1145 #endif
1146 fprintf(f,"<tr><td align=\"right\">%d</td><td align=\"right\">%d k</td>",count,group->min);
1147 fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d k</td>",group->count,group->desired);
1148
1149 every(keyword,keywords)
1150 fprintf(f,"<td align=\"right\"><font color=\"#%s\">%d M</font></td>",keyword->html_color,group->min*keyword->data_limit);
1151
1152 i+=group->desired;
1153 total+=group->count;
1154 count++;
1155 }
1156 #ifdef DEBUG
1157 printf("Total groups: %d Total bandwidth requested: %d k\nAGGREGATION: 1/%d\n",count,i,i/line);
1158 #endif
1159 fprintf(f,"<tr><th colspan=\"2\" align=\"left\">Line %Ld k</td>",line);
1160 fprintf(f,"<th align=\"right\">%d</td><th align=\"right\">%d k</td>",total,i);
1161
1162 every(keyword,keywords)
1163 fprintf(f,"<th align=\"right\">%d IPs</th>",keyword->ip_count);
1164
1165 fprintf(f,"</tr><tr><th colspan=\"4\">Aggregation 1/%d</th>\n",(int)(0.5+i/line));
1166 fprintf(f,"<th colspan=\"%d\">%d traffic classes</th></tr>\n",keywordcount,total);
1167
1168 fputs("</table>\n",f);
1169 }
1170 else if(!dry_run && !just_flush)
1171 perror(html);
1172
1173 i=1;
1174 if(f)
1175 {
1176 unsigned long long total=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0;
1177 int active_classes=0;
1178 int colspan;
1179 FILE *iplog;
1180 struct Sum {unsigned long long l; int i; list(Sum);} *sum,*sums=NULL;
1181
1182 if(qos_proxy)
1183 colspan=12;
1184 else
1185 colspan=11;
1186
1187 fprintf(f,"<p><table border>\n<tr><th colspan=\"%d\">%s",colspan,title);
1188 fprintf(f," (%s)</th></tr>\n", d);
1189 fputs("<tr><td align=\"right\">#</td><td>hostname</td>\
1190 <td align=\"right\">credit</td>\
1191 <td align=\"right\">limit</td>\
1192 <td align=\"right\">total</td>\
1193 <td align=\"right\">direct</td>\n",f);
1194 if(qos_proxy)
1195 fputs("<td align=\"right\">proxy</td>\n",f);
1196 fputs("<td align=\"right\">upload</td>\
1197 <td align=\"right\">minimum</td>\
1198 <td align=\"right\">desired</td>\
1199 <td align=\"right\">maximum</td>\
1200 <td>prio</td></tr>\n",f);
1201
1202 every(ip,ips)
1203 {
1204 char *f1="", *f2="";
1205 if(ip->max<ip->desired)
1206 {
1207 f1="<font color=\"red\">";
1208 f2="</font>";
1209 }
1210 else if(ip->prio>1)
1211 {
1212 f1="<font color=\"brown\">";
1213 f2="</font>";
1214 }
1215
1216 #ifdef DEBUG
1217 printf("%03d. %-22s %10Lu (%d/%d)\n",i ,ip->name, ip->traffic, ip->min, ip->max);
1218 #endif
1219 fprintf(f,"<tr><td align=\"right\"><a name=\"%s\"></a>%d</td><td><a href=\"%s%s.log\">%s</a></td><td align=\"right\">%Lu M</td>\n",
1220 ip->name, i, log_url, ip->name, ip->name, ip->credit);
1221 fprintf(f,"<td align=\"right\"><font color=\"#%s\">%Lu M</font></td>",ip->keyword->html_color,ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20)));
1222 fprintf(f,"<td align=\"right\">%s%Lu M%s</td><td align=\"right\">%Lu M</td>\n", f1, ip->traffic, f2, ip->direct);
1223 if(qos_proxy)
1224 fprintf(f,"<td align=\"right\">%Lu M</td>\n", ip->proxy);
1225 fprintf(f,"<td align=\"right\">%Lu M</td>\n", ip->upload);
1226 fprintf(f,"<td align=\"right\">%d k</td><td align=\"right\">%d k</td><td align=\"right\">%s%d k%s</td><td>%s%d%s</td></tr>\n",ip->min,ip->desired,f1,ip->max,f2,f1,ip->prio,f2);
1227 total+=ip->traffic;
1228 total_direct+=ip->direct;
1229 total_proxy+=ip->proxy;
1230 total_upload+=ip->upload;
1231 if(ip->traffic>0)
1232 {
1233 active_classes++;
1234 tmp_sum+=ip->traffic;
1235 create(sum,Sum);
1236 sum->l=tmp_sum;
1237 sum->i=active_classes;
1238 insert(sum,sums,order_by,i);
1239 }
1240
1241 i++;
1242
1243 if(!just_preview)
1244 {
1245 sprintf(str,"%s%s.log",log_dir,ip->name);
1246 iplog=fopen(str,"a");
1247 if(iplog)
1248 {
1249 fprintf(iplog,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%s",time(NULL),ip->name,ip->traffic, ip->direct, ip->proxy, ip->upload,d);
1250 fclose(iplog);
1251 }
1252 }
1253
1254 }
1255 fprintf(f,"<tr><th colspan=\"4 \"align=\"left\">SUMMARY:</td>");
1256 fprintf(f,"<th align=\"right\">%Lu M</th>\
1257 <th align=\"right\">%Lu M</th>\n", total, total_direct);
1258 if(qos_proxy)
1259 fprintf(f,"<th align=\"right\">%Lu M</th>\n", total_proxy);
1260 fprintf(f,"<th align=\"right\">%Lu M</th>", total_upload);
1261 fputs("<td colspan=\"4\"></td></th>\n</table>\n",f);
1262
1263 if(active_classes>10)
1264 {
1265 fputs("<a name=\"erp\"></a><p><table border><tr><th colspan=\"5\">Enterprise Research and Planning (ERP)</th></tr>\n",f);
1266 fputs("<tr><td>Analytic category</td>\n",f);
1267 fputs("<td colspan=\"2\" align=\"center\">Active Classes</td><td colspan=\"2\" align=\"center\">Data transfers</td></tr>\n",f);
1268
1269 find(sum,sums,sum->l>=total/4)
1270 {
1271 fprintf(f,"<tr><td>Top 25%% of traffic</td>\n");
1272 fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1273 }
1274
1275 find(sum,sums,sum->i==10)
1276 {
1277 fprintf(f,"<tr><td>Top 10 downloaders</td>\n");
1278 fprintf(f,"<th align=\"right\">10</th><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1279 }
1280
1281 find(sum,sums,sum->l>=total/2)
1282 {
1283 fprintf(f,"<tr><td>Top 50%% of traffic</td>\n");
1284 fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1285 }
1286
1287 find(sum,sums,sum->l>=4*total/5)
1288 {
1289 fprintf(f,"<tr><td>Top 80%% of traffic</td>\n");
1290 fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1291 }
1292
1293 find (sum,sums,sum->i>=(active_classes+1)/5)
1294 {
1295 fprintf(f,"<tr><td>Top 20%% downloaders</td>\n");
1296 fprintf(f,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1297 }
1298
1299 find(sum,sums,sum->i>=(active_classes+1)/4)
1300 {
1301 fprintf(f,"<tr><td>Top 25%% downloaders</td>\n");
1302 fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1303 }
1304
1305 find(sum,sums,sum->i>=(active_classes+1)/2)
1306 {
1307 fprintf(f,"<tr><td>Top 50%% downloaders</td>\n");
1308 fprintf(f,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1309 }
1310
1311 find(sum,sums,sum->i>=4*(active_classes+1)/5)
1312 {
1313 fprintf(f,"<tr><td>Top 80%% downloaders</td>\n");
1314 fprintf(f,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total);
1315 }
1316
1317 fprintf(f,"<tr><td>All users, all traffic</td>\n");
1318 fprintf(f,"<th align=\"right\">%d</th><th align=\"right\">100 %%</th><th align=\"right\">%Lu M</th><th align=\"right\">100 %%</th></tr>\n",active_classes,total);
1319 fputs("</table>\n",f);
1320 }
1321 fprintf(f,"<small>Statistics generated by Prometheus QoS version %s<br>GPL+Copyright(C)2005-2008 Michael Polak, <a href=\"http://www.arachne.cz/\">Arachne Labs</a></small>\n",version);
1322 fclose(f);
1323 }
1324
1325 if(just_preview)
1326 {
1327 puts("Statistics preview generated (-p switch) - now exiting ...");
1328 exit(0);
1329 }
1330
1331 /*-----------------------------------------------------------------*/
1332 puts("Generating iptables and tc classes ...");
1333 /*-----------------------------------------------------------------*/
1334
1335 i=0;
1336 printf("%-22s %-15s mark\n","name","ip");
1337 search(ip,ips,ip->mark>0)
1338 {
1339
1340 if(idxs)
1341 {
1342 char *buf;
1343 duplicate(ip->addr,buf);
1344 buf=hash_id(ip->addr,32-idxtable_bitmask1);
1345
1346 string(chain_forward,6+strlen(buf));
1347 strcpy(chain_forward,"forw_");
1348 strcat(chain_forward,buf);
1349
1350 string(chain_postrouting,6+strlen(buf));
1351 strcpy(chain_postrouting,"post_");
1352 strcat(chain_postrouting,buf);
1353
1354 free(buf);
1355 }
1356 else
1357 {
1358 chain_forward="FORWARD";
1359 chain_postrouting="POSTROUTING";
1360 }
1361
1362 printf("%-22s %-16s %04d ", ip->name, ip->addr, ip->mark);
1363
1364 /* -------------------------------------------------------- mark download */
1365
1366 sprintf(str,"-A %s -d %s/32 -o %s -j %s%d",chain_postrouting,ip->addr,lan,mark_iptables,ip->mark);
1367 /*sprintf(str,"-A %s -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,ip->addr,lan,ip->mark);*/
1368 /* -m limit --limit 1/s */
1369 save_line(str);
1370
1371 if(qos_proxy)
1372 {
1373 sprintf(str,"-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j %s%d",chain_postrouting,proxy_ip,proxy_port,ip->addr,lan,mark_iptables,ip->mark);
1374 /*sprintf(str,"-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,proxy_ip,proxy_port,ip->addr,lan,ip->mark);*/
1375 save_line(str);
1376 }
1377
1378 sprintf(str,"-A %s -d %s/32 -o %s -j ACCEPT",chain_postrouting,ip->addr,lan);
1379 save_line(str);
1380
1381 /* -------------------------------------------------------- mark upload */
1382 sprintf(str,"-A %s -s %s/32 -o %s -j %s%d",chain_forward,ip->addr,wan,mark_iptables,ip->mark);
1383 /* sprintf(str,"-A %s -s %s/32 -o %s -j MARK --set-mark %d",chain_forward,ip->addr,wan,ip->mark);*/
1384 save_line(str);
1385
1386 sprintf(str,"-A %s -s %s/32 -o %s -j ACCEPT",chain_forward,ip->addr,wan);
1387 save_line(str);
1388
1389 if(ip->min)
1390 {
1391 /* -------------------------------------------------------- download class */
1392 printf("(down: %dk-%dk ", ip->min, ip->max);
1393
1394 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", tc, lan, ip->group, ip->mark,ip->min,ip->max, burst, ip->prio);
1395 safe_run(str);
1396
1397 if (strcmpi(ip->keyword->leaf_discipline, "none")){
1398 sprintf(str,"%s qdisc add dev %s parent 1:%d handle %d %s", tc, lan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/
1399 safe_run(str);
1400 }
1401
1402 if (filter_type == 1){
1403 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d", tc, lan, ip->mark, ip->mark);
1404 safe_run(str);
1405 }
1406
1407 /* -------------------------------------------------------- upload class */
1408 printf("up: %dk-%dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
1409 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed));
1410
1411 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1412 tc, wan, ip->group, ip->mark,
1413 (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
1414 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio);
1415 safe_run(str);
1416
1417 if (strcmpi(ip->keyword->leaf_discipline, "none")){
1418 sprintf(str,"%s qdisc add dev %s parent 1:%d handle %d %s",tc, wan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/
1419 safe_run(str);
1420 }
1421
1422 if (filter_type == 1){
1423 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",tc, wan, ip->mark, ip->mark);
1424 safe_run(str);
1425 }
1426 }
1427 else
1428 printf("(sharing %s)\n", ip->sharing);
1429 i++;
1430 }
1431
1432
1433 if(idxs)
1434 {
1435 chain_forward="forw_common";
1436 chain_postrouting="post_common";
1437 }
1438 else
1439 {
1440 chain_forward="FORWARD";
1441 chain_postrouting="POSTROUTING";
1442 }
1443
1444 /* -------------------------------------------------------- mark download */
1445
1446 if(qos_proxy)
1447 {
1448 sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j MARK --set-mark 3",chain_postrouting,proxy_ip,proxy_port,lan);
1449 save_line(str);
1450 sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j ACCEPT",chain_postrouting,proxy_ip,proxy_port,lan);
1451 save_line(str);
1452 }
1453 sprintf(str,"-A %s -o %s -j MARK --set-mark 3",chain_postrouting,lan);
1454 save_line(str);
1455 sprintf(str,"-A %s -o %s -j ACCEPT",chain_postrouting,lan);
1456 save_line(str);
1457
1458 /* -------------------------------------------------------- mark upload */
1459 sprintf(str,"-A %s -o %s -j MARK --set-mark 3",chain_forward,wan);
1460 save_line(str);
1461 sprintf(str,"-A %s -o %s -j ACCEPT",chain_forward,wan);
1462 save_line(str);
1463
1464 printf("Total IP count: %d\n", i);
1465
1466 /*-----------------------------------------------------------------*/
1467 puts("Generating free bandwith classes ...");
1468 /*-----------------------------------------------------------------*/
1469
1470 /* ---------------------------------------- tc - free bandwith shared class */
1471 sprintf(str,"%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio 2",tc,lan,parent,free_min,free_max,burst);
1472 safe_run(str);
1473
1474 sprintf(str,"%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio 2",tc,wan,parent,free_min,free_max,burst);
1475 safe_run(str);
1476
1477 /* tc SFQ */
1478 if (strcmpi(qos_leaf, "none")){
1479 sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc,lan,qos_leaf);
1480 safe_run(str);
1481
1482 sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc,wan,qos_leaf);
1483 safe_run(str);
1484 }
1485
1486 /* tc handle 1 fw flowid */
1487 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc,lan);
1488 safe_run(str);
1489
1490 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc,wan);
1491 safe_run(str);
1492
1493 run_restore();
1494
1495 if (log_file) fclose(log_file);
1496 return 0;
1497
1498 /* that's all folks, thank you for reading it all the way up to this point ;-) */
1499 /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */
1500 }
This page took 1.453391 seconds and 5 git commands to generate.