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