X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=python%2Fbwlimit.py;h=58d3052787b83ac657c94d7e0a0412c264ef700e;hb=6b5db2f29ebe205319fc40f27a8cd745bfa29ff1;hp=073df7adba43b99695d43b8699bf0d3bf591c138;hpb=26eeac8583d6125e5335e337dd1cc9664037bc6e;p=util-vserver-pl.git diff --git a/python/bwlimit.py b/python/bwlimit.py index 073df7a..58d3052 100644 --- a/python/bwlimit.py +++ b/python/bwlimit.py @@ -66,7 +66,7 @@ verbose = 0 # bwmin should be small enough that it can be considered negligibly # slow compared to the hardware. 8 bits/second appears to be the # smallest value supported by tc. -bwmin = 8 +bwmin = 1000 # bwmax should be large enough that it can be considered at least as # fast as the hardware. @@ -154,24 +154,24 @@ default_share = 1 # be off by a small fraction. suffixes = { "": 1, - "bit": 1, - "kibit": 1024, - "kbit": 1000, - "mibit": 1024*1024, - "mbit": 1000000, - "gibit": 1024*1024*1024, - "gbit": 1000000000, - "tibit": 1024*1024*1024*1024, - "tbit": 1000000000000, - "bps": 8, - "kibps": 8*1024, - "kbps": 8000, - "mibps": 8*1024*1024, - "mbps": 8000000, - "gibps": 8*1024*1024*1024, - "gbps": 8000000000, - "tibps": 8*1024*1024*1024*1024, - "tbps": 8000000000000 + "bit": 1, + "kibit": 1024, + "kbit": 1000, + "mibit": 1024*1024, + "mbit": 1000000, + "gibit": 1024*1024*1024, + "gbit": 1000000000, + "tibit": 1024*1024*1024*1024, + "tbit": 1000000000000, + "bps": 8, + "kibps": 8*1024, + "kbps": 8000, + "mibps": 8*1024*1024, + "mbps": 8000000, + "gibps": 8*1024*1024*1024, + "gbps": 8000000000, + "tibps": 8*1024*1024*1024*1024, + "tbps": 8000000000000 } @@ -191,6 +191,24 @@ def get_tc_rate(s): else: return -1 +def format_bytes(bytes, si = True): + """ + Formats bytes into a string + """ + if si: + kilo = 1000. + else: + # Officially, a kibibyte + kilo = 1024. + + if bytes >= (kilo * kilo * kilo): + return "%.1f GB" % (bytes / (kilo * kilo * kilo)) + elif bytes >= 1000000: + return "%.1f MB" % (bytes / (kilo * kilo)) + elif bytes >= 1000: + return "%.1f KB" % (bytes / kilo) + else: + return "%.0f bytes" % bytes def format_tc_rate(rate): """ @@ -303,6 +321,16 @@ def tc(cmd): return run(TC + " " + cmd) +def stop(dev = dev): + ''' + Turn off all queing. Stops all slice HTBS and reverts to pfifo_fast (the default). + ''' + try: + for i in range(0,2): + tc("qdisc del dev %s root" % dev) + except: pass + + def init(dev = dev, bwcap = bwmax): """ (Re)initialize the bandwidth limits on this node @@ -573,22 +601,21 @@ def exempt_init(group_name, node_ips): (burst) cap. """ - # Clean up - iptables = "/sbin/iptables -t mangle %s POSTROUTING" - run(iptables % "-F") - run("/sbin/ipset -X " + group_name) - - # Create a hashed IP set of all of these destinations - lines = ["-N %s iphash" % group_name] - add_cmd = "-A %s " % group_name - lines += [(add_cmd + ip) for ip in node_ips] - lines += ["COMMIT"] - restore = "\n".join(lines) + "\n" - run("/sbin/ipset -R", restore) - - # Add rule to match on destination IP set - run((iptables + " -m set --set %s dst -j CLASSIFY --set-class 1:%x") % - ("-A", group_name, exempt_minor)) + # Check of set exists + set = run("/sbin/ipset -S " + group_name) + if set == None: + # Create a hashed IP set of all of these destinations + lines = ["-N %s iphash" % group_name] + add_cmd = "-A %s " % group_name + lines += [(add_cmd + ip) for ip in node_ips] + lines += ["COMMIT"] + restore = "\n".join(lines) + "\n" + run("/sbin/ipset -R", restore) + else: # set exists + # Check all hosts and add missing. + for nodeip in node_ips: + if not run("/sbin/ipset -T %s %s" % (group_name, nodeip)): + run("/sbin/ipset -A %s %s" % (group_name, nodeip)) def usage(): @@ -600,7 +627,7 @@ Usage: %s [OPTION]... [COMMAND] [ARGUMENT]... Options: - -d device Network interface (default: %s) + -d device Network interface (default: %s) -r rate Node bandwidth cap (default: %s) -q quantum Share multiplier (default: %d bytes) -n Print rates in numeric bits per second @@ -681,11 +708,11 @@ def main(): minexemptrate, maxexemptrate, bytes, exemptbytes) else: - print "%s %d %s %s %s %s %d %d" % \ + print "%s %d %s %s %s %s %s %s" % \ (slice, share, format_tc_rate(minrate), format_tc_rate(maxrate), format_tc_rate(minexemptrate), format_tc_rate(maxexemptrate), - bytes, exemptbytes) + format_bytes(bytes), format_bytes(exemptbytes)) elif len(argv) >= 2: # slice, ...