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