X-Git-Url: http://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=prometheus.c;h=8aacb50830c759fecc469588393d4d4cf98b0761;hp=7adff3cc6b493b5b9481a6284af7b6265e799b7d;hb=HEAD;hpb=4deb2d6f2b43232cf5e782ebb1deb9b0e041def9 diff --git a/prometheus.c b/prometheus.c index 7adff3c..8aacb50 100644 --- a/prometheus.c +++ b/prometheus.c @@ -2,12 +2,12 @@ /* Prometheus QoS - you can "steal fire" from your ISP */ /* "fair-per-IP" quality of service (QoS) utility */ /* requires Linux 2.4.x or 2.6.x with HTB support */ -/* Copyright(C) 2005-2017 Michael Polak, Arachne Aerospace */ +/* Copyright(C) 2005-2020 Michael Polak, Arachne Aerospace */ /* iptables-restore support Copyright(C) 2007-2008 ludva */ /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -/* Modified by: xChaos, 20171106 +/* Modified by: xChaos, 20231028 ludva, 20080415 Prometheus QoS is free software; you can redistribute it and/or @@ -29,7 +29,7 @@ #include "cll1-0.6.2.h" #include "ipstruct.h" -const char *version = "0.9.0-b"; +const char *version = "1.0.1"; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Versions: 0.9.0 is development release, 1.0 will be "stable" */ @@ -40,7 +40,7 @@ const char *version = "0.9.0-b"; /* Warning: unofficial Github mirror is not supported by author! */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2017 Michael Polak, Arachne Labs
\n"; +const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2020 Michael Polak, Arachne Labs
\n"; #define STRLEN 512 #undef DEBUG @@ -63,6 +63,7 @@ char *upstreamfile = "/etc/prometheus/upstream.interfaces"; /* list of interf char *downstreamfile = "/etc/prometheus/downstream.interfaces"; /* list of interfaces to manage */ char *qosfreefile = "/etc/prometheus/qosfree.interfaces"; /* list of interfaces to manage */ char *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/ +char *iptablesdump = "/var/spool/prometheus.iptables-dump"; /* temporary file for iptables -L -v -x -n -t mangle */ char *ip6tablesfile = "/var/spool/prometheus.ip6tables"; /* temporary file for ip6tables-restore*/ char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */ char *classmap = "/var/lib/misc/prometheus.classes"; /* credit log file */ @@ -86,7 +87,8 @@ int filter_type = 1; /*1 mark, 2 classify*/ char *final_chain = "DROP"; /* REJECT would be better, but it is impossible in mangle */ char *mark = "MARK"; char *mark_iptables = "MARK --set-mark "; -int dry_run = FALSE; /* preview - use puts() instead of system() */ +int dry_run = FALSE; /* preview commands - use puts() instead of system() */ +int skip_stats = FALSE; /* skip generating statistics */ 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]"; 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"; FILE *iptables_file = NULL; @@ -101,8 +103,10 @@ char *medium = "1000Mbit"; /* 10Mbit/100Mbit ethernet */ char *ip6prefix = NULL; /* Prefix for global /48 IPv6 subnet */ char *qos_leaf = "sfq perturb 5"; /* leaf discipline */ char *qos_free_zone = NULL; /* QoS free zone */ +char *qos_free_dst_ipset = NULL; /* QoS free zone - dst match ipset name, must be prepared outside prometheus */ +char *qos_free_src_ipset = NULL; /* QoS free zone - src match ipset name, must be prepared outside prometheus */ /* int qos_proxy = TRUE; include proxy port to QoS */ -int found_lmsid = FALSE; /* show links to users in LMS information system */ +int found_code = FALSE; /* show links to users in LMS information system */ int include_upload = TRUE; /* upload+download=total traffic */ /* char *proxy_ip = "192.168.1.1/32"; our IP with proxy port */ /* int proxy_port = 3128; proxy port number */ @@ -115,7 +119,8 @@ int overlimit_max = 512; /* maximum allowed bandwidth for all undefined hos int qos_free_delay = 0; /* seconds to sleep before applying new QoS rules */ int digital_divide = 2; /* controls digital divide weirdness ratio, 1...3 */ int max_nesting = 5; /* /include/uapi/linux/pkt_sched.h: #define TC_HTB_MAXDEPTH 8 [... - 3 parent classes] */ -int htb_r2q = 256; /* should work for leaf values 512 kbps to 8 Mbps */ +//obsolete: int htb_r2q = 256; /* should work for leaf values 512 kbps to 8 Mbps */ +int htb_quantum = 1514; /* MTU + ethernet header */ int burst = 8; /* HTB burst (in kbits) */ int burst_main = 64; int burst_group = 32; @@ -184,7 +189,6 @@ const char *tr_odd_even(void) } } - /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */ char *index_id(char *ip, int bitmask); @@ -288,6 +292,7 @@ void get_config(char *config_filename) option("ip6tables-save",ip6tablessave); option("ip6tables-restore",ip6tablesrestore); option("iptables-in-filename",iptablesfile); + option("iptables-dump-filename",iptablesdump); option("ip6tables-in-filename",ip6tablesfile); option("hosts",hosts); option("downstream-interfaces-list-filename",downstreamfile); @@ -314,6 +319,8 @@ void get_config(char *config_filename) option("lms-url",lms_url); ioption("use-jquery-popups",use_jquery_popups); option("qos-free-zone",qos_free_zone); + option("qos-free-dst-ipset",qos_free_dst_ipset); + option("qos-free-src-ipset",qos_free_src_ipset); ioption("qos-free-delay",qos_free_delay); /* ioption("qos-proxy-enable",qos_proxy); */ /* option("qos-proxy-ip",proxy_ip);*/ @@ -327,7 +334,7 @@ void get_config(char *config_filename) ioption("htb-burst-main",burst_main); ioption("htb-burst-group",burst_group); ioption("htb-nesting-limit",max_nesting); - ioption("htb-r2q",htb_r2q); + ioption("htb-quantum",htb_quantum); ioption("magic-include-upload",include_upload); ioption("magic-treshold",magic_treshold); option("filter-type", cnf); @@ -368,7 +375,7 @@ void get_config(char *config_filename) } -/* ========== This function executes, logs OR ALSO prints command ========== */ +/* ========== This function executes, logs or also prints command ========== */ void safe_run(char *cmd) { @@ -497,6 +504,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); argument("-s") { run=TRUE; just_preview=TRUE; start_shaping=TRUE; } argument("-r") { run=TRUE; } argument("-n") { run=TRUE; nodelay=TRUE; } + argument("-b") { run=TRUE; nodelay=TRUE; skip_stats=TRUE; } argument("-a") { run=TRUE; just_networks=TRUE; } argument("-l") { just_logs=TRUE; } argument("-m") { just_logs=TRUE; } @@ -539,7 +547,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); interface->chain = "FORWARD"; interface->idxprefix = "forw"; push(interface, interfaces); - printf("Upstream interface %s: medium %s capacity %ld kbps\n", interface->name, medium, interface->speed); + printf("Upstream interface %s: medium %s capacity %Ld kbps\n", interface->name, medium, interface->speed); } } done; /* ugly macro end */ @@ -565,7 +573,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); interface->chain = "POSTROUTING"; interface->idxprefix = "post"; push(interface, interfaces); - printf("Downstream interface %s: medium %s capacity %ld kbps\n", interface->name, medium, interface->speed); + printf("Downstream interface %s: medium %s capacity %Ld kbps\n", interface->name, medium, interface->speed); } } done; /* ugly macro end */ @@ -586,7 +594,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); hosts = althosts; } - if(just_flush<9) + if(just_flush<9 && !skip_stats) { /*-----------------------------------------------------------------*/ puts("Parsing iptables verbose output ..."); @@ -636,14 +644,34 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); printf("Parsing class defintion file %s ...\n", hosts); /*-----------------------------------------------------------------*/ parse_hosts(hosts); -/* - //this was pretty dumb idea anyway... - if(just_networks) + +#ifdef MONITORINGTRHU_CTU +//special hack only to generate certain required CSV statistics for www.ctu.cz (regulation body) +//not required for everyday use, requires special syntax sugar in hosts file, see parsehosts.c + for_each(technology, technologies) { - analyse_topology("/usr/sbin/traceroute -n -m 10 -w 2 %s.%d"); - exit(-1); - } -*/ + char *filename; + FILE *f; + string(filename, strlen(log_dir) + strlen(technology->filename) + 5); + strcpy(filename, log_dir); + strcat(filename, technology->filename); + strcat(filename, ".csv"); + /*-----------------------------------------------------------------*/ + printf("Writing report file %s ...\n", filename); + /*-----------------------------------------------------------------*/ + f = fopen(filename, "w"); + if(f) + { + for_each(ip, ips) if(eq(technology->filename, ip->technology_str)) + { + fprintf(f,"%s,%s,%d\n", ip->code, ip->ruian_id_str, ip->max); + } + fclose(f); + } + else + perror(filename); + } +#endif /*-----------------------------------------------------------------*/ puts("Resolving shared connections ..."); @@ -657,7 +685,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); sharedip->traffic_up += ip->upload; ip->traffic = 0; ip->mark = sharedip->mark; - ip->lmsid = sharedip->lmsid; + ip->code = sharedip->code; ip->pps_limit = sharedip->pps_limit; /* no other way to do this */ /* Ugly hack: append IPv4 addresses of sharedip to IPv6 uplinks */ @@ -755,7 +783,16 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); iptables_save_line(ip6preamble, IPv6); } - if(qos_free_zone && *qos_free_zone!='0') /* this is currently supported only for IPv4 */ + /* this doesn't seem to be way to go... about 2/3 of all packets have ACK flag set + maybe with --length 40:100 it would make some sense, but not enought + for_each(interface, interfaces) + { + sprintf(str,"-A %s -m tcp -p tcp --tcp-flags ACK ACK -o %s -j ACCEPT", interface->chain, interface->name); + iptables_save_line(str, IPv4); + } + */ + + if(qos_free_zone && *qos_free_zone != '0') /* this is currently supported only for IPv4 */ { for_each(interface, interfaces) { @@ -763,6 +800,24 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); iptables_save_line(str, IPv4); } } + + if(qos_free_dst_ipset && *qos_free_dst_ipset != '0') /* this is currently supported only for IPv4 */ + { + for_each(interface, interfaces) + { + sprintf(str,"-A %s -m set --match-set %s %s -o %s -j ACCEPT", interface->chain, qos_free_dst_ipset, (interface->is_upstream?"dst":"src"), interface->name); + iptables_save_line(str, IPv4); + } + } + + if(qos_free_src_ipset && *qos_free_src_ipset != '0') /* this is currently supported only for IPv4 */ + { + for_each(interface, interfaces) + { + sprintf(str,"-A %s -m set --match-set %s %s -o %s -j ACCEPT", interface->chain, qos_free_src_ipset, (interface->is_upstream?"src":"dst"), interface->name); + iptables_save_line(str, IPv4); + } + } for_each(qosfreeinterface, qosfreeinterfaces) { @@ -935,16 +990,16 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); for_each(interface, interfaces) { - sprintf(str, "%s qdisc add dev %s root handle 1: htb r2q %d default 1", - tc, interface->name, htb_r2q); + sprintf(str, "%s qdisc add dev %s root handle 1: htb default 1", + tc, interface->name); safe_run(str); - sprintf(str, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d", - tc, interface->name, medium, medium, burst_main, highest_priority); + sprintf(str, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d quantum %d", + tc, interface->name, medium, medium, burst_main, highest_priority, htb_quantum); safe_run(str); - sprintf(str, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d", - tc, interface->name, interface->speed, interface->speed, burst_main, highest_priority); + sprintf(str, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d quantum %d", + tc, interface->name, interface->speed, interface->speed, burst_main, highest_priority, htb_quantum); safe_run(str); } } @@ -971,8 +1026,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); { if(!just_preview) { - 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", - tc, interface->name, parent, group->id, rate, max, burst_group, highest_priority+1, group->desired); + sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d quantum %d #down desired %d", + tc, interface->name, parent, group->id, rate, max, burst_group, highest_priority+1, htb_quantum, group->desired); safe_run(str); } @@ -1080,7 +1135,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); agreg = (int)((float)(avg_mbps_down+1)/min_mbps+.5); ip->max /= agreg; ip->pps_limit /= agreg; - printf("Download aggregation 1:%d for %s (min: %lu Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_down); + printf("Download aggregation 1:%d for %s (min: %d Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_down); } else { @@ -1100,7 +1155,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); unshape_this_ip = 0; agreg = (int)((float)(avg_mbps_up+1)/min_mbps+.5); ip->max /= agreg; - printf("Upload aggregation 1:%d for %s: (min: %lu Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_up); + printf("Upload aggregation 1:%d for %s: (min: %d Mbps avg: %ld Mbps)\n", agreg, ip->name, min_mbps, avg_mbps_up); } else { @@ -1147,8 +1202,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); { printf("[down %s: %dk-%dk wants %d]", interface->name, ip->min, ip->max, ip->desired); } - sprintf(str, "%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", - tc, interface->name, ip->group, ip->mark, ip->min, ip->max, burst, ip->prio); + sprintf(str, "%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d quantum %d", + tc, interface->name, ip->group, ip->mark, ip->min, ip->max, burst, ip->prio, htb_quantum); safe_run(str); } else @@ -1159,10 +1214,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); (int)((ip->desired/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), (int)((ip->desired/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed)); } - sprintf(str,"%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", + sprintf(str,"%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d quantum %d", tc, interface->name, ip->group, ip->mark, (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), - (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio); + (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio, htb_quantum); safe_run(str); } } @@ -1187,7 +1242,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); json_traffic=json_preview; } - if(!dry_run && !just_flush) + if(!dry_run && !just_flush && !skip_stats) { /*-----------------------------------------------------------------*/ printf("Writing json traffic overview %s ... ", json_traffic); @@ -1198,6 +1253,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); printf("Writing statistics into HTML page %s ...\n", html); /*-----------------------------------------------------------------*/ write_htmlandlogs(html, d, total, just_preview); + printf("\n"); } if(just_preview) @@ -1233,7 +1289,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } /*-----------------------------------------------------------------*/ - printf(" + generating iptables and tc classes ... "); + printf(" + generating iptables and tc classes ... \n"); /*-----------------------------------------------------------------*/ for_each(ip, ips) if(ip->mark > 0) @@ -1314,8 +1370,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); printf("[down: %dk-%dk]", ip->min, ip->max); #endif - sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", - tc, interface->name, ip->group, ip->mark, ip->min, ip->max, burst, ip->prio); + sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d quantum %d", + tc, interface->name, ip->group, ip->mark, ip->min, ip->max, burst, ip->prio, htb_quantum); safe_run(str); if(strcmpi(ip->keyword->leaf_discipline, "none")) @@ -1344,10 +1400,11 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); { fprintf(f, "%s %d\n", ip->addr, ip->mark); } + printf("."); } if(f > 0) { - puts("done."); + puts(" done."); fclose(f); } @@ -1386,8 +1443,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); /*-----------------------------------------------------------------*/ puts("Generating free bandwith class ..."); /*-----------------------------------------------------------------*/ - sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", - tc, interface->name, parent, FREE_CLASS, free_min, free_max,burst, lowest_priority); + sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d quantum %d", + tc, interface->name, parent, FREE_CLASS, free_min, free_max,burst, lowest_priority, htb_quantum); safe_run(str); /* tc SFQ */ if(strcmpi(qos_leaf, "none")) @@ -1399,12 +1456,15 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); 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); safe_run(str); } - /*-----------------------------------------------------------------*/ - puts("Generating bandwith class for overlimit packets..."); - /*-----------------------------------------------------------------*/ - sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", - tc, interface->name, parent, OVERLIMIT_CLASS, overlimit_min, overlimit_max, burst, lowest_priority); - safe_run(str); + if(*limit_pkts) /* non-empty string?*/ + { + /*-----------------------------------------------------------------*/ + puts("Generating bandwith class for overlimit packets..."); + /*-----------------------------------------------------------------*/ + sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d quantum %d", + tc, interface->name, parent, OVERLIMIT_CLASS, overlimit_min, overlimit_max, burst, lowest_priority, htb_quantum); + safe_run(str); + } } printf("Total IP count: %d\n", i); run_iptables_restore();