X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=prometheus.c;h=7adff3cc6b493b5b9481a6284af7b6265e799b7d;hb=76b82eecc73f0495aea55f1ef5661fb336b53071;hp=d25ca51aa89b3baa0e9b62c193a56b141c937956;hpb=9f552e96b7c3ed1b5af6578252c0154491132a10;p=svn%2FPrometheus-QoS%2F.git diff --git a/prometheus.c b/prometheus.c index d25ca51..7adff3c 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-2015 Michael Polak, Arachne Aerospace */ +/* Copyright(C) 2005-2017 Michael Polak, Arachne Aerospace */ /* iptables-restore support Copyright(C) 2007-2008 ludva */ /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -/* Modified by: xChaos, 20171012 +/* Modified by: xChaos, 20171106 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-a"; +const char *version = "0.9.0-b"; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Versions: 0.9.0 is development release, 1.0 will be "stable" */ @@ -61,6 +61,7 @@ char *hosts = "/etc/prometheus/hosts"; /* per-IP bandwidth definition char *macrosfile = "/etc/prometheus/prometheus.macros"; /* rewrite rules for most common tariffs */ char *upstreamfile = "/etc/prometheus/upstream.interfaces"; /* list of interfaces to manage */ 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 *ip6tablesfile = "/var/spool/prometheus.ip6tables"; /* temporary file for ip6tables-restore*/ char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */ @@ -138,6 +139,7 @@ struct Keyword *keyword, *defaultkeyword=NULL, *keywords = NULL; struct Macro *macro, *macros = NULL; struct Index *idxs = NULL, *idx, *metaindex; struct Interface *interfaces = NULL, *interface; +struct QosFreeInterface *qosfreeinterfaces = NULL, *qosfreeinterface; #define FREE_CLASS 3 #define OVERLIMIT_CLASS 4 @@ -290,7 +292,8 @@ void get_config(char *config_filename) option("hosts",hosts); option("downstream-interfaces-list-filename",downstreamfile); option("upstream-interfaces-list-filename",upstreamfile); - option("macros-filename",upstreamfile); + option("qos-free-interfaces-list-filename",qosfreefile); + option("macros-filename",macrosfile); option("ip6-prefix",ip6prefix); option("medium",medium); ioption("hall-of-fame-enable",hall_of_fame); @@ -395,6 +398,10 @@ void iptables_save_line(char *line, int ipv6) } } +#define IPv4 FALSE +#define IPv6 TRUE + + void run_iptables_restore(void) { char *restor; @@ -404,7 +411,7 @@ void run_iptables_restore(void) printf("Running %s <%s ...\n", iptablesrestore, iptablesfile); /*-----------------------------------------------------------------*/ - iptables_save_line("COMMIT", FALSE); + iptables_save_line("COMMIT", IPv4); fclose(iptables_file); if(dry_run) { @@ -423,7 +430,7 @@ void run_iptables_restore(void) /*-----------------------------------------------------------------*/ printf("Running %s <%s ...\n", ip6tablesrestore, ip6tablesfile); /*-----------------------------------------------------------------*/ - iptables_save_line("COMMIT", TRUE); + iptables_save_line("COMMIT", IPv6); fclose(ip6tables_file); if(dry_run) { @@ -471,7 +478,7 @@ program printf("\n\ Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\ -Version %s - Copyright (C)2005-2015 Michael Polak, Arachne Labs\n\ +Version %s - Copyright (C)2005-2017 Michael Polak, Arachne Labs\n\ iptables-restore & burst tunning & classify modification by Ludva\n\ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); @@ -540,7 +547,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); /*-----------------------------------------------------------------*/ printf("Parsing downstream interfaces list %s ...\n", downstreamfile); /*-----------------------------------------------------------------*/ - parse(upstreamfile) + parse(downstreamfile) { ptr = parse_datafile_line(_); if(ptr) @@ -558,11 +565,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); interface->chain = "POSTROUTING"; interface->idxprefix = "post"; push(interface, interfaces); - printf("Upstream 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 */ - if(just_logs) { @@ -601,6 +607,12 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); string(str, STRLEN); string(limit_pkts, STRLEN); + /*-----------------------------------------------------------------*/ + printf("Parsing qos free interfaces file %s ...\n", qosfreefile); + /*-----------------------------------------------------------------*/ + load(qosfreeinterface, qosfreeinterfaces, + qosfreefile, QosFreeInterface, name); + /*-----------------------------------------------------------------*/ printf("Parsing macro definition file %s ...\n", macrosfile); /*-----------------------------------------------------------------*/ @@ -705,7 +717,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); perror(iptablesfile); exit(-1); } - iptables_save_line(iptablespreamble, FALSE); + iptables_save_line(iptablespreamble, IPv4); if(ip6prefix) { @@ -715,8 +727,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); perror(ip6tablesfile); exit(-1); } - iptables_save_line(iptablespreamble, TRUE); - iptables_save_line(ip6preamble, TRUE); + iptables_save_line(iptablespreamble, IPv6); + iptables_save_line(ip6preamble, IPv6); } run_iptables_restore(); @@ -735,23 +747,33 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } iptables_file=fopen(iptablesfile,"w"); - iptables_save_line(iptablespreamble, FALSE); + iptables_save_line(iptablespreamble, IPv4); if(ip6prefix) { ip6tables_file=fopen(ip6tablesfile,"w"); - iptables_save_line(iptablespreamble, TRUE); - iptables_save_line(ip6preamble, TRUE); + iptables_save_line(iptablespreamble, IPv6); + iptables_save_line(ip6preamble, IPv6); } if(qos_free_zone && *qos_free_zone!='0') /* this is currently supported only for IPv4 */ { for_each(interface, interfaces) { - sprintf(str,"-A %s -s %s -o %s -j ACCEPT", interface->chain, qos_free_zone, interface->name); - iptables_save_line(str, FALSE); + sprintf(str,"-A %s -%c %s -o %s -j ACCEPT", interface->chain, (interface->is_upstream?'d':'s'), qos_free_zone, interface->name); + iptables_save_line(str, IPv4); } } + for_each(qosfreeinterface, qosfreeinterfaces) + { + sprintf(str,"-A FORWARD -i %s -j ACCEPT", qosfreeinterface->name); + iptables_save_line(str, IPv4); + iptables_save_line(str, IPv6); + sprintf(str,"-A POSTROUTING -o %s -j ACCEPT", qosfreeinterface->name); + iptables_save_line(str, IPv4); + iptables_save_line(str, IPv6); + } + if(ip_count > idxtable_treshold1 && !just_flush) { int idxcount=0, bitmask=32-idxtable_bitmask1; @@ -760,12 +782,12 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count); /*-----------------------------------------------------------------*/ - iptables_save_line(":post_common - [0:0]", FALSE); - iptables_save_line(":forw_common - [0:0]", FALSE); + iptables_save_line(":post_common - [0:0]", IPv4); + iptables_save_line(":forw_common - [0:0]", IPv4); if(ip6prefix) { - iptables_save_line(":post_common - [0:0]", TRUE); - iptables_save_line(":forw_common - [0:0]", TRUE); + iptables_save_line(":post_common - [0:0]", IPv6); + iptables_save_line(":forw_common - [0:0]", IPv6); } for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0")) @@ -868,10 +890,12 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); buf = interface->chain; } - sprintf(str,"-A %s -d %s/%d -o %s -j %s_%s", buf, subnet, idx->bitmask, interface->name, interface->idxprefix, idx->id); + sprintf(str, "-A %s -%c %s/%d -o %s -j %s_%s", + buf, (interface->is_upstream?'s':'d'), subnet, idx->bitmask, interface->name, interface->idxprefix, idx->id); iptables_save_line(str, idx->ipv6); - sprintf(str,"-A %s -d %s/%d -o %s -j %s_common", buf, subnet, idx->bitmask, interface->name, interface->idxprefix); + sprintf(str, "-A %s -%c %s/%d -o %s -j %s_common", + buf, (interface->is_upstream?'s':'d'), subnet, idx->bitmask, interface->name, interface->idxprefix); iptables_save_line(str, idx->ipv6); } } @@ -880,11 +904,11 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); for_each(interface, interfaces) { sprintf(str,"-A %s -o %s -j %s_common", interface->chain, interface->name, interface->idxprefix); - iptables_save_line(str, FALSE); + iptables_save_line(str, IPv4); if(ip6prefix) { sprintf(str,"-A %s -o %s -j %s_common", interface->chain, interface->name, interface->idxprefix); - iptables_save_line(str, TRUE); + iptables_save_line(str, IPv6); } } } @@ -1257,24 +1281,29 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); #endif /* ------------------------------------------------ iptables classify */ - sprintf(str, "-A %s -d %s/%d -o %s -j %s%d", - chain, ip->addr, ip->mask, + sprintf(str, "-A %s -%c %s/%d -o %s -j %s%d", + chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask, interface->name, mark_iptables, ip->mark); iptables_save_line(str, ip->v6); - sprintf(str, "-A %s -d %s/%d -o %s %s-j ACCEPT", - chain, ip->addr, ip->mask, interface->name, limit_pkts); + sprintf(str, "-A %s -%c %s/%d -o %s %s-j ACCEPT", + chain, (interface->is_upstream?'s':'d'),ip->addr, ip->mask, + interface->name, limit_pkts); iptables_save_line(str, ip->v6); - /* classify overlimit packets to separate overlimit class */ - sprintf(str, "-A %s -d %s/%d -o %s -j %s%d", - chain, ip->addr, ip->mask, - interface->name, mark_iptables, OVERLIMIT_CLASS); - iptables_save_line(str, ip->v6); - - sprintf(str, "-A %s -d %s/%d -o %s -j ACCEPT", - chain, ip->addr, ip->mask, interface->name); - iptables_save_line(str, ip->v6); + if(*limit_pkts) /* non-empty string?*/ + { + /* classify overlimit packets to separate overlimit class */ + sprintf(str, "-A %s -%c %s/%d -o %s -j %s%d", + chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask, + interface->name, mark_iptables, OVERLIMIT_CLASS); + iptables_save_line(str, ip->v6); + + sprintf(str, "-A %s -%c %s/%d -o %s -j ACCEPT", + chain, (interface->is_upstream?'s':'d'), ip->addr, ip->mask, + interface->name); + iptables_save_line(str, ip->v6); + } if(ip->min) { @@ -1341,15 +1370,15 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); sprintf(str, "-A %s -o %s -j %s%d", chain, interface->name, mark_iptables, FREE_CLASS); - iptables_save_line(str, FALSE); /* only for IPv4 */ + iptables_save_line(str, IPv4); /* only for IPv4 */ } sprintf(str,"-A %s -o %s -j %s", chain, interface->name, final_chain); - iptables_save_line(str, FALSE); + iptables_save_line(str, IPv4); if(ip6prefix) { sprintf(str,"-A %s -o %s -j %s", chain, interface->name, final_chain); - iptables_save_line(str, TRUE); + iptables_save_line(str, IPv6); } if(free_min) /* allocate free bandwith if it is not zero... */ @@ -1369,14 +1398,13 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); /* tc handle 1 fw flowid */ 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); } + /*-----------------------------------------------------------------*/ + 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); } printf("Total IP count: %d\n", i); run_iptables_restore();