7adff3cc6b493b5b9481a6284af7b6265e799b7d
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-2017 Michael Polak, Arachne Aerospace */
6 /* iptables-restore support Copyright(C) 2007-2008 ludva */
7 /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */
8 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
10 /* Modified by: xChaos, 20171106
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.
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.
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
27 GNU General Public License is located in file COPYING */
29 #include "cll1-0.6.2.h"
32 const char *version
= "0.9.0-b";
34 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35 /* Versions: 0.9.0 is development release, 1.0 will be "stable" */
36 /* Official Trac URL: https://dev.arachne.cz/svn/prometheus */
37 /* Official SVN URL: https://dev.arachne.cz/repos/prometheus */
38 /* BTC donations account: 19rriLx8vR19wGefPaMhakqnCYNYwjLvxq */
39 /* CZK donations account: 2900242944/2010 (transparent account) */
40 /* Warning: unofficial Github mirror is not supported by author! */
41 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
43 const char *stats_html_signature
= "<span class=\"small\">Statistics generated by Prometheus QoS version %s<br />GPL+Copyright(C)2005-2017 Michael Polak, <a target=\"_blank\" href=\"http://www.arachne.cz/\">Arachne Labs</a></span>\n";
48 /* ======= All path names are defined here (for RPM patch) ======= */
50 const char *tc
= "/sbin/tc"; /* requires tc with HTB support */
51 const char *iptables
= "/sbin/iptables"; /* requires iptables utility */
52 const char *ip6tables
= "/sbin/ip6tables"; /* requires iptables utility */
53 const char *iptablessave
= "/sbin/iptables-save"; /* not yet required */
54 const char *iptablesrestore
= "/sbin/iptables-restore"; /* requires iptables-restore */
55 const char *ip6tablessave
= "/sbin/ip6tables-save"; /* not yet required */
56 const char *ip6tablesrestore
= "/sbin/ip6tables-restore"; /* requires iptables-restore */
57 const char *ls
= "/bin/ls"; /* this is not user configurable :-) */
59 char *config
= "/etc/prometheus/prometheus.conf"; /* main configuration file */
60 char *hosts
= "/etc/prometheus/hosts"; /* per-IP bandwidth definition file */
61 char *macrosfile
= "/etc/prometheus/prometheus.macros"; /* rewrite rules for most common tariffs */
62 char *upstreamfile
= "/etc/prometheus/upstream.interfaces"; /* list of interfaces to manage */
63 char *downstreamfile
= "/etc/prometheus/downstream.interfaces"; /* list of interfaces to manage */
64 char *qosfreefile
= "/etc/prometheus/qosfree.interfaces"; /* list of interfaces to manage */
65 char *iptablesfile
= "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/
66 char *ip6tablesfile
= "/var/spool/prometheus.ip6tables"; /* temporary file for ip6tables-restore*/
67 char *credit
= "/var/lib/misc/prometheus.credit"; /* credit log file */
68 char *classmap
= "/var/lib/misc/prometheus.classes"; /* credit log file */
69 char *html
= "/var/www/traffic.html"; /* hall of fame - html version */
70 char *preview
= "/var/www/preview.html"; /* hall of fame preview - html version */
71 char *json_traffic
= "/var/www/logs/traffic.json"; /* hall of fame - json version */
72 char *json_preview
= "/var/www/logs/preview.json"; /* hall of fame preview - json version */
73 char *cmdlog
= "/var/log/prometheuslog"; /* command log filename */
74 char *log_dir
= "/var/www/logs/"; /* log directory pathname, ended with slash */
75 char *log_url
= "/logs/"; /* log directory relative URI prefix (partial URL) */
76 char *html_log_dir
= "/var/www/logs/html/";
78 char *jquery_url
= "http://code.jquery.com/jquery-latest.js";
79 char *lms_url
= "/lms/?m=customerinfo&id=";
80 int use_jquery_popups
= TRUE
;
81 int row_odd_even
= 0; /*<tr class="odd/even"> */
83 /* === Configuraration file values defaults - stored in global variables ==== */
85 int filter_type
= 1; /*1 mark, 2 classify*/
86 char *final_chain
= "DROP"; /* REJECT would be better, but it is impossible in mangle */
88 char *mark_iptables
= "MARK --set-mark ";
89 int dry_run
= FALSE
; /* preview - use puts() instead of system() */
90 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]";
91 char *ip6preamble
= "-A FORWARD -p ipv6-icmp -j ACCEPT\n-A POSTROUTING -p ipv6-icmp -j ACCEPT\n-A FORWARD -s fe80::/10 -j ACCEPT\n-A FORWARD -d ff00::/8 -j ACCEPT\n-A POSTROUTING -s fe80::/10 -j ACCEPT\n-A POSTROUTING -d ff00::/8 -j ACCEPT";
92 FILE *iptables_file
= NULL
;
93 FILE *ip6tables_file
= NULL
;
94 int enable_credit
= TRUE
; /* enable credit file */
95 int use_credit
= FALSE
; /* use credit file (if enabled)*/
96 char *title
= "Hall of Fame - Greatest Suckers"; /* hall of fame title */
97 int hall_of_fame
= TRUE
; /* enable hall of fame */
98 char *medium
= "1000Mbit"; /* 10Mbit/100Mbit ethernet */
99 //obsolete: char *lan = "eth0"; /* LAN interface */
100 //obsolete: char *lan_medium = "1000Mbit"; /* 10Mbit/100Mbit ethernet */
101 char *ip6prefix
= NULL
; /* Prefix for global /48 IPv6 subnet */
102 char *qos_leaf
= "sfq perturb 5"; /* leaf discipline */
103 char *qos_free_zone
= NULL
; /* QoS free zone */
104 /* int qos_proxy = TRUE; include proxy port to QoS */
105 int found_lmsid
= FALSE
; /* show links to users in LMS information system */
106 int include_upload
= TRUE
; /* upload+download=total traffic */
107 /* char *proxy_ip = "192.168.1.1/32"; our IP with proxy port */
108 /* int proxy_port = 3128; proxy port number */
109 //obsolete: long long int line = 1024; /* WAN/ISP download in kbps */
110 //obsolete: long long int up = 1024; /* WAN/ISP upload in kbps */
111 int free_min
= 256; /* minimum guaranted bandwidth for all undefined hosts */
112 int free_max
= 512; /* maximum allowed bandwidth for all undefined hosts */
113 int overlimit_min
= 256; /* minimum guaranted bandwidth for all undefined hosts */
114 int overlimit_max
= 512; /* maximum allowed bandwidth for all undefined hosts */
115 int qos_free_delay
= 0; /* seconds to sleep before applying new QoS rules */
116 int digital_divide
= 2; /* controls digital divide weirdness ratio, 1...3 */
117 int max_nesting
= 5; /* /include/uapi/linux/pkt_sched.h: #define TC_HTB_MAXDEPTH 8 [... - 3 parent classes] */
118 int htb_r2q
= 256; /* should work for leaf values 512 kbps to 8 Mbps */
119 int burst
= 8; /* HTB burst (in kbits) */
121 int burst_group
= 32;
122 int magic_treshold
= 8; /* reduce ceil by X*magic_treshhold kbps (hard shaping) */
123 int keywordcount
= 0;
126 FILE *log_file
= NULL
;
127 char *kwd
= "via-prometheus"; /* /etc/hosts comment, eg. #qos-64-128 */
129 const int highest_priority
= 0; /* highest HTB priority (HTB built-in value is 0) */
130 const int lowest_priority
= 7; /* lowest HTB priority /include/uapi/linux/pkt_sched.h: #define TC_HTB_NUMPRIO 8 */
131 const int idxtable_treshold1
= 24; /* this is no longer configurable */
132 const int idxtable_treshold2
= 12; /* this is no longer configurable */
133 const int idxtable_bitmask1
= 3; /* this is no longer configurable */
134 const int idxtable_bitmask2
= 3; /* this is no longer configurable */
136 struct IP
*ips
= NULL
, *networks
= NULL
, *ip
, *sharedip
;
137 struct Group
*groups
= NULL
, *group
;
138 struct Keyword
*keyword
, *defaultkeyword
=NULL
, *keywords
= NULL
;
139 struct Macro
*macro
, *macros
= NULL
;
140 struct Index
*idxs
= NULL
, *idx
, *metaindex
;
141 struct Interface
*interfaces
= NULL
, *interface
;
142 struct QosFreeInterface
*qosfreeinterfaces
= NULL
, *qosfreeinterface
;
145 #define OVERLIMIT_CLASS 4
148 /* implemented in help.c */
150 void get_traffic_statistics(const char *whichiptables
, int ipv6
);
151 /* implemented in parseiptables.c */
153 void parse_ip_log(int argc
, char **argv
);
154 /* implemented in parselog.c */
156 void parse_hosts(char *hosts
);
157 /* implemented in parsehosts.c */
159 void write_json_traffic(char *json
);
160 /* implemented in json.c */
162 void write_htmlandlogs(char *html
, char *d
, int total
, int just_preview
);
163 /* implemented in htmlandlogs.c */
165 void analyse_topology(char *traceroute
);
166 /* implemented in networks.c */
168 char *parse_datafile_line(char *str
);
169 /* implemented in utils.c */
171 time_t get_mtime(const char *path
);
172 /* implemented in utils.c */
174 const char *tr_odd_even(void)
176 row_odd_even
= 1 - row_odd_even
;
179 return "<tr class=\"even\">\n";
183 return "<tr class=\"odd\">\n";
188 /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */
190 char *index_id(char *ip
, int bitmask
);
191 /* function implemented in ipv4subnets.c */
193 char *subnet_id(char *ip
, int bitmask
);
194 /* function implemented in ipv4subnets.c */
196 char *index6_id(char *ip
, int bitmask
);
197 /* function implemented in ipv6subnets.c */
199 char *subnet6_id(char *ip
, int bitmask
);
200 /* function implemented in ipv6subnets.c */
202 /* ================= Let's parse configuration file here ================ */
204 void reject_config_and_exit(char *filename
)
206 printf("Configuration file %s rejected - abnormal exit.",filename
);
210 void get_config(char *config_filename
)
214 printf("Configured keywords: ");
215 parse(config_filename
)
217 option("keyword",kwd
);
222 create(keyword
,Keyword
);
224 keyword
->asymetry_ratio
= 1; /* ratio for ADSL-like upload */
225 keyword
->asymetry_fixed
= 0; /* fixed treshold for ADSL-like upload */
226 keyword
->data_limit
= 8; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
227 keyword
->data_prio
= 4; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
228 keyword
->fixed_limit
= 0; /* fixed data limit for setting lower HTB ceil */
229 keyword
->fixed_prio
= 0; /* fixed data limit for setting lower HTB prio */
230 keyword
->reserve_min
= 8; /* bonus for nominal HTB rate bandwidth (in kbps) */
231 keyword
->reserve_max
= 0; /* malus for nominal HTB ceil (in kbps) */
232 keyword
->default_prio
= highest_priority
+1;
233 keyword
->download_aggregation
= keyword
->upload_aggregation
= 0; /* disable by default */
234 keyword
->html_color
= "000000";
235 keyword
->ip_count
= 0;
236 keyword
->leaf_discipline
= "";
237 keyword
->allowed_avgmtu
= 0;
239 push(keyword
, keywords
);
242 defaultkeyword
= keyword
;
250 for_each(keyword
,keywords
)
252 int l
=strlen(keyword
->key
);
254 if(!strncmp(keyword
->key
,_
,l
) && strlen(_
)>l
+2)
256 char *tmptr
=_
; /* <---- l+1 ----> */
257 _
+=l
+1; /* via-prometheus-asymetry-ratio, etc. */
258 foption("asymetry-ratio", keyword
->asymetry_ratio
);
259 ioption("asymetry-treshold", keyword
->asymetry_fixed
);
260 ioption("magic-relative-limit", keyword
->data_limit
);
261 ioption("magic-relative-prio", keyword
->data_prio
);
262 loption("magic-fixed-limit", keyword
->fixed_limit
);
263 loption("magic-fixed-prio", keyword
->fixed_prio
);
264 ioption("htb-default-prio", keyword
->default_prio
);
265 ioption("htb-rate-bonus", keyword
->reserve_min
);
266 ioption("htb-ceil-malus", keyword
->reserve_max
);
267 ioption("download-aggregation", keyword
->download_aggregation
);
268 ioption("upload-aggregation", keyword
->upload_aggregation
);
269 option("leaf-discipline", keyword
->leaf_discipline
);
270 option("html-color", keyword
->html_color
);
271 ioption("allowed-avgmtu" ,keyword
->allowed_avgmtu
);
274 if(keyword
->data_limit
|| keyword
->fixed_limit
||
275 keyword
->data_prio
|| keyword
->fixed_prio
)
284 option("iptables",iptables
);
285 option("iptables-save",iptablessave
);
286 option("iptables-restore",iptablesrestore
);
287 option("ip6tables",ip6tables
);
288 option("ip6tables-save",ip6tablessave
);
289 option("ip6tables-restore",ip6tablesrestore
);
290 option("iptables-in-filename",iptablesfile
);
291 option("ip6tables-in-filename",ip6tablesfile
);
292 option("hosts",hosts
);
293 option("downstream-interfaces-list-filename",downstreamfile
);
294 option("upstream-interfaces-list-filename",upstreamfile
);
295 option("qos-free-interfaces-list-filename",qosfreefile
);
296 option("macros-filename",macrosfile
);
297 option("ip6-prefix",ip6prefix
);
298 option("medium",medium
);
299 ioption("hall-of-fame-enable",hall_of_fame
);
300 ioption("digital-divide-weirdness-ratio",digital_divide
);
301 option("hall-of-fame-title",title
);
302 option("hall-of-fame-filename",html
);
303 option("json-filename",json_traffic
);
304 option("hall-of-fame-preview",preview
);
305 option("json-preview",json_preview
);
306 option("log-filename",cmdlog
);
307 option("credit-filename",credit
);
308 option("classmap-filename",classmap
);
309 ioption("credit-enable",enable_credit
);
310 option("log-traffic-directory",log_dir
);
311 option("log-traffic-html-directory",html_log_dir
);
312 option("log-traffic-url-path",log_url
);
313 option("jquery-url",jquery_url
);
314 option("lms-url",lms_url
);
315 ioption("use-jquery-popups",use_jquery_popups
);
316 option("qos-free-zone",qos_free_zone
);
317 ioption("qos-free-delay",qos_free_delay
);
318 /* ioption("qos-proxy-enable",qos_proxy); */
319 /* option("qos-proxy-ip",proxy_ip);*/
320 option("htb-leaf-discipline",qos_leaf
);
321 /* ioption("qos-proxy-port",proxy_port); */
322 ioption("free-rate",free_min
);
323 ioption("free-ceil",free_max
);
324 ioption("overlimit-rate",overlimit_min
);
325 ioption("overlimit-ceil",overlimit_max
);
326 ioption("htb-burst",burst
);
327 ioption("htb-burst-main",burst_main
);
328 ioption("htb-burst-group",burst_group
);
329 ioption("htb-nesting-limit",max_nesting
);
330 ioption("htb-r2q",htb_r2q
);
331 ioption("magic-include-upload",include_upload
);
332 ioption("magic-treshold",magic_treshold
);
333 option("filter-type", cnf
);
334 /* not yet implemented:
335 ioption("magic-fixed-packets",fixed_packets);
336 ioption("magic-relative-packets",packet_limit);
341 perror(config_filename
);
342 puts("Warning - using built-in defaults instead ...");
344 done
; /* ugly macro end */
347 /* leaf discipline for keywords */
348 for_each(keyword
,keywords
)
350 if(!strcmpi(keyword
->leaf_discipline
, ""))
352 keyword
->leaf_discipline
= qos_leaf
;
356 if(strcmpi(cnf
, "mark"))
360 mark_iptables
= "CLASSIFY --set-class 1:";
366 mark_iptables
= "MARK --set-mark ";
371 /* ========== This function executes, logs OR ALSO prints command ========== */
373 void safe_run(char *cmd
)
377 printf("\n=>%s\n",cmd
);
385 fprintf(log_file
,"%s\n",cmd
);
389 void iptables_save_line(char *line
, int ipv6
)
393 fprintf(ip6tables_file
,"%s\n",line
);
397 fprintf(iptables_file
,"%s\n",line
);
405 void run_iptables_restore(void)
408 string(restor
, STRLEN
);
410 /*-----------------------------------------------------------------*/
411 printf("Running %s <%s ...\n", iptablesrestore
, iptablesfile
);
412 /*-----------------------------------------------------------------*/
414 iptables_save_line("COMMIT", IPv4
);
415 fclose(iptables_file
);
422 done
; /* ugly macro end */
425 sprintf(restor
,"%s <%s",iptablesrestore
, iptablesfile
);
430 /*-----------------------------------------------------------------*/
431 printf("Running %s <%s ...\n", ip6tablesrestore
, ip6tablesfile
);
432 /*-----------------------------------------------------------------*/
433 iptables_save_line("COMMIT", IPv6
);
434 fclose(ip6tables_file
);
441 done
; /* ugly macro end */
443 sprintf(restor
,"%s <%s",ip6tablesrestore
, ip6tablesfile
);
451 char *parse_datafile_line(char *str
);
452 time_t get_mtime(const char *path
);
454 /*-----------------------------------------------------------------*/
455 /* Are you looking for int main(int argc, char **argv) ? :-)) */
456 /*-----------------------------------------------------------------*/
460 int i
=0; /* just plain old Fortran style integer :-) */
461 FILE *f
=NULL
; /* everything is just stream of bytes... */
462 char *str
, *ptr
, *d
; /* LET A$=B$ :-) */
463 char *substring
, *limit_pkts
;
466 int just_networks
= FALSE
;
467 int just_flush
= FALSE
; /* deactivates all previous actions */
469 int just_preview
= FALSE
; /* preview - generate just stats */
470 int start_shaping
= FALSE
; /* apply FUP - requires classmap file */
471 int stop_shaping
= FALSE
; /* lift FUP - requires classmap file */
472 int reduce_ceil
= 0; /* allow only rate+(ceil-rate)/2, /4, etc. */
473 int just_logs
= FALSE
; /* just parse logs */
480 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\
481 Version %s - Copyright (C)2005-2017 Michael Polak, Arachne Labs\n\
482 iptables-restore & burst tunning & classify modification by Ludva\n\
483 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version
);
485 /*----- Boring... we have to check command line options first: ----*/
488 argument("-c") { nextargument(config
); }
489 argument("-h") { nextargument(althosts
);}
490 argument("-d") { run
=TRUE
; dry_run
=TRUE
; }
491 argument("-f") { run
=TRUE
; just_flush
=TRUE
; }
492 argument("-9") { run
=TRUE
; just_flush
=9; }
493 argument("-p") { run
=TRUE
; just_preview
=TRUE
; }
494 argument("-q") { run
=TRUE
; just_preview
=TRUE
; stop_shaping
=TRUE
; }
495 argument("-2") { run
=TRUE
; just_preview
=TRUE
; reduce_ceil
=2; }
496 argument("-4") { run
=TRUE
; just_preview
=TRUE
; reduce_ceil
=4; }
497 argument("-s") { run
=TRUE
; just_preview
=TRUE
; start_shaping
=TRUE
; }
498 argument("-r") { run
=TRUE
; }
499 argument("-n") { run
=TRUE
; nodelay
=TRUE
; }
500 argument("-a") { run
=TRUE
; just_networks
=TRUE
; }
501 argument("-l") { just_logs
=TRUE
; }
502 argument("-m") { just_logs
=TRUE
; }
503 argument("-y") { just_logs
=TRUE
; }
504 argument("-?") { help(); exit(0); }
505 argument("--help") { help(); exit(0); }
506 argument("-v") { exit(0); }
507 argument("--version") { exit(0); }
512 puts("*** THIS IS JUST DRY RUN ! ***\n");
515 date(d
); /* this is typical cll1.h macro - prints current date */
517 /*-----------------------------------------------------------------*/
518 printf("Parsing configuration file %s ...\n", config
);
519 /*-----------------------------------------------------------------*/
521 /*-----------------------------------------------------------------*/
522 printf("Parsing upstream interfaces list %s ...\n", upstreamfile
);
523 /*-----------------------------------------------------------------*/
526 ptr
= parse_datafile_line(_
);
529 create(interface
, Interface
);
531 interface
->speed
= (long long)atol(ptr
);
532 /* is supplied value meaningful ?*/
533 if(interface
->speed
<= 0)
535 printf("Illegal value of %s interface bandwidth.\n", interface
->name
);
536 reject_config_and_exit(upstreamfile
);
538 interface
->is_upstream
= TRUE
;
539 interface
->chain
= "FORWARD";
540 interface
->idxprefix
= "forw";
541 push(interface
, interfaces
);
542 printf("Upstream interface %s: medium %s capacity %ld kbps\n", interface
->name
, medium
, interface
->speed
);
545 done
; /* ugly macro end */
547 /*-----------------------------------------------------------------*/
548 printf("Parsing downstream interfaces list %s ...\n", downstreamfile
);
549 /*-----------------------------------------------------------------*/
550 parse(downstreamfile
)
552 ptr
= parse_datafile_line(_
);
555 create(interface
, Interface
);
557 interface
->speed
= (long long)atol(ptr
);
558 /* is supplied value meaningful ?*/
559 if(interface
->speed
<= 0)
561 printf("Illegal value of %s interface bandwidth.\n", interface
->name
);
562 reject_config_and_exit(downstreamfile
);
564 interface
->is_upstream
= FALSE
;
565 interface
->chain
= "POSTROUTING";
566 interface
->idxprefix
= "post";
567 push(interface
, interfaces
);
568 printf("Downstream interface %s: medium %s capacity %ld kbps\n", interface
->name
, medium
, interface
->speed
);
571 done
; /* ugly macro end */
575 parse_ip_log(argc
,argv
);
591 /*-----------------------------------------------------------------*/
592 puts("Parsing iptables verbose output ...");
593 /*-----------------------------------------------------------------*/
594 get_traffic_statistics(iptables
, FALSE
);
597 /*-----------------------------------------------------------------*/
598 puts("Parsing ip6tables verbose output ...");
599 /*-----------------------------------------------------------------*/
600 get_traffic_statistics(ip6tables
, TRUE
);
604 /*-----------------------------------------------------------------*/
605 /* cll1.h - let's allocate brand new character buffer... */
606 /*-----------------------------------------------------------------*/
608 string(limit_pkts
, STRLEN
);
610 /*-----------------------------------------------------------------*/
611 printf("Parsing qos free interfaces file %s ...\n", qosfreefile
);
612 /*-----------------------------------------------------------------*/
613 load(qosfreeinterface
, qosfreeinterfaces
,
614 qosfreefile
, QosFreeInterface
, name
);
616 /*-----------------------------------------------------------------*/
617 printf("Parsing macro definition file %s ...\n", macrosfile
);
618 /*-----------------------------------------------------------------*/
621 ptr
= parse_datafile_line(_
);
624 create(macro
, Macro
);
625 macro
->rewrite_from
= _
;
626 macro
->rewrite_to
= ptr
;
628 printf("%s -> %s\n", macro
->rewrite_from
, macro
->rewrite_to
);
631 done
; /* ugly macro end */
635 /*-----------------------------------------------------------------*/
636 printf("Parsing class defintion file %s ...\n", hosts
);
637 /*-----------------------------------------------------------------*/
640 //this was pretty dumb idea anyway...
643 analyse_topology("/usr/sbin/traceroute -n -m 10 -w 2 %s.%d");
648 /*-----------------------------------------------------------------*/
649 puts("Resolving shared connections ...");
650 /*-----------------------------------------------------------------*/
651 for_each(ip
, ips
) if(ip
->sharing
)
653 for_each(sharedip
, ips
) if(eq(sharedip
->name
, ip
->sharing
))
655 sharedip
->traffic
+= ip
->traffic
;
656 sharedip
->traffic_down
+= ip
->direct
;
657 sharedip
->traffic_up
+= ip
->upload
;
659 ip
->mark
= sharedip
->mark
;
660 ip
->lmsid
= sharedip
->lmsid
;
661 ip
->pps_limit
= sharedip
->pps_limit
; /* no other way to do this */
663 /* Ugly hack: append IPv4 addresses of sharedip to IPv6 uplinks */
664 ptr
= strchr(ip
->addr
, '+');
665 if(ptr
&& ptr
-ip
->addr
> 1 && !sharedip
->v6
)
668 concatenate(ip
->addr
, sharedip
->addr
, ptr
);
669 ip
->name
= ip
->addr
= ptr
;
670 ptr
= strchr(ip
->addr
, '.');
674 ptr
= strchr(ptr
, '.');
683 printf("Unresolved shared connection: %s %s sharing-%s\n",
684 ip
->addr
, ip
->name
, ip
->sharing
);
688 if(enable_credit
&& just_flush
<9)
690 /*-----------------------------------------------------------------*/
691 printf("Parsing credit file %s ...\n", credit
);
692 /*-----------------------------------------------------------------*/
695 ptr
= parse_datafile_line(_
);
698 if_exists(ip
,ips
,eq(ip
->addr
,_
))
700 sscanf(ptr
,"%Lu",&(ip
->credit
));
704 done
; /* ugly macro end */
710 /*-----------------------------------------------------------------*/
711 puts("Initializing iptables and tc classes ...");
712 /*-----------------------------------------------------------------*/
714 iptables_file
= fopen(iptablesfile
, "w");
715 if(iptables_file
== NULL
)
717 perror(iptablesfile
);
720 iptables_save_line(iptablespreamble
, IPv4
);
724 ip6tables_file
= fopen(ip6tablesfile
, "w");
725 if(ip6tables_file
== NULL
)
727 perror(ip6tablesfile
);
730 iptables_save_line(iptablespreamble
, IPv6
);
731 iptables_save_line(ip6preamble
, IPv6
);
734 run_iptables_restore();
736 log_file
= fopen(cmdlog
, "w");
743 for_each(interface
, interfaces
)
745 sprintf(str
,"%s qdisc del dev %s root 2>/dev/null", tc
, interface
->name
);
749 iptables_file
=fopen(iptablesfile
,"w");
750 iptables_save_line(iptablespreamble
, IPv4
);
753 ip6tables_file
=fopen(ip6tablesfile
,"w");
754 iptables_save_line(iptablespreamble
, IPv6
);
755 iptables_save_line(ip6preamble
, IPv6
);
758 if(qos_free_zone
&& *qos_free_zone
!='0') /* this is currently supported only for IPv4 */
760 for_each(interface
, interfaces
)
762 sprintf(str
,"-A %s -%c %s -o %s -j ACCEPT", interface
->chain
, (interface
->is_upstream
?'d':'s'), qos_free_zone
, interface
->name
);
763 iptables_save_line(str
, IPv4
);
767 for_each(qosfreeinterface
, qosfreeinterfaces
)
769 sprintf(str
,"-A FORWARD -i %s -j ACCEPT", qosfreeinterface
->name
);
770 iptables_save_line(str
, IPv4
);
771 iptables_save_line(str
, IPv6
);
772 sprintf(str
,"-A POSTROUTING -o %s -j ACCEPT", qosfreeinterface
->name
);
773 iptables_save_line(str
, IPv4
);
774 iptables_save_line(str
, IPv6
);
777 if(ip_count
> idxtable_treshold1
&& !just_flush
)
779 int idxcount
=0, bitmask
=32-idxtable_bitmask1
;
781 /*-----------------------------------------------------------------*/
782 printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count
);
783 /*-----------------------------------------------------------------*/
785 iptables_save_line(":post_common - [0:0]", IPv4
);
786 iptables_save_line(":forw_common - [0:0]", IPv4
);
789 iptables_save_line(":post_common - [0:0]", IPv6
);
790 iptables_save_line(":forw_common - [0:0]", IPv6
);
793 for_each(ip
,ips
) if(ip
->addr
&& *(ip
->addr
) && !eq(ip
->addr
,"0.0.0.0/0"))
797 buf
=index6_id(ip
->addr
,bitmask
+32);
801 buf
=index_id(ip
->addr
, bitmask
);
804 if_exists(idx
,idxs
,eq(idx
->id
,buf
))
811 idx
->addr
= ip
->addr
;
813 idx
->bitmask
= bitmask
+32*ip
->v6
;
822 /* brutal perfomance optimalization */
823 while(idxcount
> idxtable_treshold2
&& bitmask
> 2*idxtable_bitmask2
)
825 bitmask
-= idxtable_bitmask2
;
828 for_each(idx
,idxs
) if(idx
->parent
== NULL
)
832 buf
= index6_id(idx
->addr
, bitmask
+32);
836 buf
= index_id(idx
->addr
, bitmask
);
838 if_exists(metaindex
,idxs
,eq(metaindex
->id
,buf
))
840 metaindex
->children
++;
844 create(metaindex
,Index
);
845 metaindex
->addr
= idx
->addr
;
847 metaindex
->bitmask
= bitmask
+32*idx
->ipv6
;
848 metaindex
->parent
= NULL
;
849 metaindex
->children
= 0;
850 metaindex
->ipv6
= idx
->ipv6
;
852 push(metaindex
,idxs
);
854 idx
->parent
=metaindex
;
858 /* this should slightly optimize throughput ... */
859 sort(idx
,idxs
,desc_order_by
,children
);
860 sort(idx
,idxs
,order_by
,bitmask
);
867 subnet
=subnet6_id(idx
->addr
, idx
->bitmask
);
871 subnet
=subnet_id(idx
->addr
, idx
->bitmask
);
873 printf("%d: %s/%d\n", ++i
, subnet
, idx
->bitmask
);
875 sprintf(str
,":post_%s - [0:0]", idx
->id
);
876 iptables_save_line(str
, idx
->ipv6
);
878 sprintf(str
,":forw_%s - [0:0]", idx
->id
);
879 iptables_save_line(str
, idx
->ipv6
);
881 for_each(interface
, interfaces
)
885 string(buf
, strlen(idx
->parent
->id
)+6);
886 sprintf(buf
, "%s_%s", interface
->idxprefix
, idx
->parent
->id
);
890 buf
= interface
->chain
;
893 sprintf(str
, "-A %s -%c %s/%d -o %s -j %s_%s",
894 buf
, (interface
->is_upstream
?'s':'d'), subnet
, idx
->bitmask
, interface
->name
, interface
->idxprefix
, idx
->id
);
895 iptables_save_line(str
, idx
->ipv6
);
897 sprintf(str
, "-A %s -%c %s/%d -o %s -j %s_common",
898 buf
, (interface
->is_upstream
?'s':'d'), subnet
, idx
->bitmask
, interface
->name
, interface
->idxprefix
);
899 iptables_save_line(str
, idx
->ipv6
);
902 printf("Total indexed iptables chains created: %d\n", i
);
904 for_each(interface
, interfaces
)
906 sprintf(str
,"-A %s -o %s -j %s_common", interface
->chain
, interface
->name
, interface
->idxprefix
);
907 iptables_save_line(str
, IPv4
);
910 sprintf(str
,"-A %s -o %s -j %s_common", interface
->chain
, interface
->name
, interface
->idxprefix
);
911 iptables_save_line(str
, IPv6
);
919 fclose(iptables_file
);
924 puts("Just flushed iptables and tc classes - now exiting ...");
930 if(!dry_run
&& !nodelay
&& qos_free_delay
)
932 printf("Flushed iptables and tc classes - now sleeping for %d seconds...\n", qos_free_delay
);
933 sleep(qos_free_delay
);
936 for_each(interface
, interfaces
)
938 sprintf(str
, "%s qdisc add dev %s root handle 1: htb r2q %d default 1",
939 tc
, interface
->name
, htb_r2q
);
942 sprintf(str
, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",
943 tc
, interface
->name
, medium
, medium
, burst_main
, highest_priority
);
946 sprintf(str
, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",
947 tc
, interface
->name
, interface
->speed
, interface
->speed
, burst_main
, highest_priority
);
952 /*-----------------------------------------------------------------*/
953 puts("Locating heavy downloaders and generating root classes ...");
954 /*-----------------------------------------------------------------*/
955 sort(ip
,ips
,desc_order_by
,traffic
);
957 /*-----------------------------------------------------------------*/
958 for_each(interface
, interfaces
)
960 long long int rate
= interface
->speed
;
961 long long int max
= interface
->speed
;
963 //obsolete: FILE *credit_file = NULL;
965 //obsolete: if(!just_preview && !dry_run && enable_credit)
967 //obsolete: credit_file = fopen(credit,"w");
970 for_each(group
,groups
)
974 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",
975 tc
, interface
->name
, parent
, group
->id
, rate
, max
, burst_group
, highest_priority
+1, group
->desired
);
979 if(group_count
++ < max_nesting
)
984 rate
-= digital_divide
*group
->min
;
985 if(rate
< group
->min
)
990 /*shaping of aggresive downloaders, with credit file support */
994 int group_rate = group->min, priority_sequence = lowest_priority;
996 for_each(ip, ips) if(ip->min == group->min && ip->max > ip->min)
998 ip->realquota=ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20));
999 if( ip->keyword->data_limit
1000 and not ip->fixedprio
1001 and ip->traffic > ip->realquota )
1003 if(group_rate < ip->max)
1005 ip->max = group_rate;
1007 group_rate+=magic_treshold;
1008 ip->prio=lowest_priority;
1009 if(ip->prio<highest_priority+2)
1011 ip->prio=highest_priority+2;
1016 if( ip->keyword->data_prio
1018 && ( ip->traffic > ip->credit + (ip->min*ip->keyword->data_prio+(ip->keyword->fixed_prio<<20))) )
1020 ip->prio=priority_sequence--;
1021 if(ip->prio<highest_priority+1)
1023 ip->prio=highest_priority+1;
1029 unsigned long long lcredit=0;
1031 if((ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))>ip->traffic)
1033 lcredit=(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))-ip->traffic;
1035 fprintf(credit_file,"%s %Lu\n",ip->addr,lcredit);
1044 fclose(credit_file);
1050 if(start_shaping
|| stop_shaping
|| reduce_ceil
)
1052 time_t how_much_seconds
= time(NULL
) - get_mtime(classmap
); /* sice start of daily aggregation session */
1053 printf("Reading %s (%ld seconds old) and applying Fair Use Policy and Aggregation rules... \n", classmap
, how_much_seconds
);
1062 if_exists(ip
,ips
,eq(ip
->addr
,_
))
1064 int unshape_this_ip
= 0;
1065 long avg_mbps_down
= ip
->traffic_down
* 8 / how_much_seconds
;
1066 long avg_mbps_up
= ip
->traffic_up
* 8 / how_much_seconds
;
1067 int agreg
= 1, print_stats
= 1;
1069 if(ip
->keyword
->download_aggregation
)
1071 int min_mbps
= (ip
->min
/ip
->keyword
->download_aggregation
)>>10;
1077 if(min_mbps
<= avg_mbps_down
)
1079 unshape_this_ip
= 0;
1080 agreg
= (int)((float)(avg_mbps_down
+1)/min_mbps
+.5);
1082 ip
->pps_limit
/= agreg
;
1083 printf("Download aggregation 1:%d for %s (min: %lu Mbps avg: %ld Mbps)\n", agreg
, ip
->name
, min_mbps
, avg_mbps_down
);
1087 unshape_this_ip
= 1;
1090 else if(ip
->keyword
->upload_aggregation
)
1092 int min_mbps
= (ip
->min
/ip
->keyword
->upload_aggregation
)>>10;
1098 if(min_mbps
<= avg_mbps_up
)
1100 unshape_this_ip
= 0;
1101 agreg
= (int)((float)(avg_mbps_up
+1)/min_mbps
+.5);
1103 printf("Upload aggregation 1:%d for %s: (min: %lu Mbps avg: %ld Mbps)\n", agreg
, ip
->name
, min_mbps
, avg_mbps_up
);
1107 unshape_this_ip
= 1;
1112 unshape_this_ip
= 1;
1114 ip
->aggregated
= agreg
;
1115 ip
->mark
= atoi(ptr
);
1116 if(ip
->max
< ip
->desired
|| unshape_this_ip
|| reduce_ceil
) /* apply or disable FUP limit immediately.... */
1120 ip
->max
= ip
->desired
;
1121 if(stop_shaping
) /* all limits removed, but not printed with -s (start_shaping) switch */
1123 printf("Removing limit for %s (%s) ", ip
->name
, ip
->addr
);
1132 printf("Updating %s (%s) ", ip
->name
, ip
->addr
);
1135 ip
->max
= ip
->min
+ (ip
->desired
-ip
->min
)/reduce_ceil
;
1137 else if(ip
->max
< ip
->min
)
1142 for_each(interface
, interfaces
)
1144 if(!interface
->is_upstream
)
1148 printf("[down %s: %dk-%dk wants %d]", interface
->name
, ip
->min
, ip
->max
, ip
->desired
);
1150 sprintf(str
, "%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1151 tc
, interface
->name
, ip
->group
, ip
->mark
, ip
->min
, ip
->max
, burst
, ip
->prio
);
1158 printf("[up %s: %dk-%dk wants %dk]", interface
->name
, (int)((ip
->min
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
),
1159 (int)((ip
->desired
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
),
1160 (int)((ip
->desired
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
));
1162 sprintf(str
,"%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1163 tc
, interface
->name
, ip
->group
, ip
->mark
,
1164 (int)((ip
->min
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
),
1165 (int)((ip
->max
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
), burst
, ip
->prio
);
1180 puts("Warning - classmap file not fund, just generating preview ...");
1181 start_shaping
=FALSE
;
1184 done
; /* ugly macro end */
1187 json_traffic
=json_preview
;
1190 if(!dry_run
&& !just_flush
)
1192 /*-----------------------------------------------------------------*/
1193 printf("Writing json traffic overview %s ... ", json_traffic
);
1194 /*-----------------------------------------------------------------*/
1195 write_json_traffic(json_traffic
);
1197 /*-----------------------------------------------------------------*/
1198 printf("Writing statistics into HTML page %s ...\n", html
);
1199 /*-----------------------------------------------------------------*/
1200 write_htmlandlogs(html
, d
, total
, just_preview
);
1210 else if(reduce_ceil
)
1212 swchar
='0'+reduce_ceil
; /* -2, -4 */
1214 else if(stop_shaping
)
1219 printf("Statistics preview generated (-%c switch) - now exiting ...\n", swchar
);
1225 printf("%-22s %-15s mark\n","name","ip");
1228 printf("Writing %s", classmap
);
1229 f
= fopen(classmap
, "w");
1235 /*-----------------------------------------------------------------*/
1236 printf(" + generating iptables and tc classes ... ");
1237 /*-----------------------------------------------------------------*/
1239 for_each(ip
, ips
) if(ip
->mark
> 0)
1241 for_each(interface
, interfaces
)
1247 duplicate(ip
->addr
,buf
);
1250 buf
=index6_id(ip
->addr
,64-idxtable_bitmask1
);
1254 buf
=index_id(ip
->addr
,32-idxtable_bitmask1
);
1257 string(chain
, 6+strlen(buf
));
1258 sprintf(chain
, "%s_", interface
->idxprefix
);
1265 chain
= interface
->chain
;
1268 /* packet limits - this will be optional in future */
1271 sprintf(limit_pkts
, "-m limit --limit %d/s --limit-burst %d ",
1272 ip
->pps_limit
, ip
->pps_limit
);
1280 printf("%-22s %-16s %04d %d/s\n", ip
->name
, ip
->addr
, ip
->mark
, ip
->pps_limit
);
1283 /* ------------------------------------------------ iptables classify */
1284 sprintf(str
, "-A %s -%c %s/%d -o %s -j %s%d",
1285 chain
, (interface
->is_upstream
?'s':'d'), ip
->addr
, ip
->mask
,
1286 interface
->name
, mark_iptables
, ip
->mark
);
1287 iptables_save_line(str
, ip
->v6
);
1289 sprintf(str
, "-A %s -%c %s/%d -o %s %s-j ACCEPT",
1290 chain
, (interface
->is_upstream
?'s':'d'),ip
->addr
, ip
->mask
,
1291 interface
->name
, limit_pkts
);
1292 iptables_save_line(str
, ip
->v6
);
1294 if(*limit_pkts
) /* non-empty string?*/
1296 /* classify overlimit packets to separate overlimit class */
1297 sprintf(str
, "-A %s -%c %s/%d -o %s -j %s%d",
1298 chain
, (interface
->is_upstream
?'s':'d'), ip
->addr
, ip
->mask
,
1299 interface
->name
, mark_iptables
, OVERLIMIT_CLASS
);
1300 iptables_save_line(str
, ip
->v6
);
1302 sprintf(str
, "-A %s -%c %s/%d -o %s -j ACCEPT",
1303 chain
, (interface
->is_upstream
?'s':'d'), ip
->addr
, ip
->mask
,
1305 iptables_save_line(str
, ip
->v6
);
1310 //TODO - min and max should not exceed interface->speed
1312 /* -------------------------------------------------------- tc class */
1314 printf("[down: %dk-%dk]", ip
->min
, ip
->max
);
1317 sprintf(str
, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1318 tc
, interface
->name
, ip
->group
, ip
->mark
, ip
->min
, ip
->max
, burst
, ip
->prio
);
1321 if(strcmpi(ip
->keyword
->leaf_discipline
, "none"))
1323 sprintf(str
, "%s qdisc add dev %s parent 1:%d handle %d %s",
1324 tc
, interface
->name
, ip
->mark
, ip
->mark
, ip
->keyword
->leaf_discipline
); /*qos_leaf*/
1328 if(filter_type
== 1)
1330 sprintf(str
, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",
1331 tc
, interface
->name
, ip
->mark
, ip
->mark
);
1338 printf("(sharing %s)\n", ip
->sharing
);
1343 if(ip
->min
&& f
> 0)
1345 fprintf(f
, "%s %d\n", ip
->addr
, ip
->mark
);
1354 for_each(interface
, interfaces
)
1359 string(chain
, STRLEN
);
1360 sprintf(chain
, "%s_common", interface
->idxprefix
);
1364 chain
= interface
->chain
;
1369 final_chain
= "ACCEPT";
1371 sprintf(str
, "-A %s -o %s -j %s%d",
1372 chain
, interface
->name
, mark_iptables
, FREE_CLASS
);
1373 iptables_save_line(str
, IPv4
); /* only for IPv4 */
1376 sprintf(str
,"-A %s -o %s -j %s", chain
, interface
->name
, final_chain
);
1377 iptables_save_line(str
, IPv4
);
1380 sprintf(str
,"-A %s -o %s -j %s", chain
, interface
->name
, final_chain
);
1381 iptables_save_line(str
, IPv6
);
1384 if(free_min
) /* allocate free bandwith if it is not zero... */
1386 /*-----------------------------------------------------------------*/
1387 puts("Generating free bandwith class ...");
1388 /*-----------------------------------------------------------------*/
1389 sprintf(str
, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1390 tc
, interface
->name
, parent
, FREE_CLASS
, free_min
, free_max
,burst
, lowest_priority
);
1393 if(strcmpi(qos_leaf
, "none"))
1395 sprintf(str
,"%s qdisc add dev %s parent 1:%d handle %d %s", tc
, interface
->name
, FREE_CLASS
, FREE_CLASS
, qos_leaf
);
1398 /* tc handle 1 fw flowid */
1399 sprintf(str
,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d", tc
, interface
->name
, FREE_CLASS
, FREE_CLASS
);
1402 /*-----------------------------------------------------------------*/
1403 puts("Generating bandwith class for overlimit packets...");
1404 /*-----------------------------------------------------------------*/
1405 sprintf(str
, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1406 tc
, interface
->name
, parent
, OVERLIMIT_CLASS
, overlimit_min
, overlimit_max
, burst
, lowest_priority
);
1409 printf("Total IP count: %d\n", i
);
1410 run_iptables_restore();
1416 /* that's all folks, thank you for reading it all the way up to this point ;-) */
1417 /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */
This page took 2.891577 seconds and 4 git commands to generate.