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