--- /dev/null
+#!/usr/bin/env python
+
+"""
+ Traffic control interface for Linux tc
+ 2010, Razvan Deaconescu, razvan.deaconescu@cs.pub.ro
+"""
+
+import sys
+import sqlite3
+import os.path
+
+DEBUG = False
+
+class TrafficControl:
+ """
+ Basic class interface to Linux tc
+ """
+
+ def __init__(self, node_type):
+ self.node_type = node_type
+ self.upload_limit = -1
+ self.download_limit = -1
+
+ def config(self, host_address, host_port, host_iface,
+ vm_address = None, vm_port = -1, vm_iface = None):
+ self.host_address = host_address
+ self.host_port = host_port
+ self.host_iface = host_iface
+ self.vm_address = vm_address
+ self.vm_port = vm_port
+ self.vm_iface = vm_iface
+
+ """
+ Set upload limit. Argument in KB/s (kilobytes/s)
+ """
+ def set_upload_limit(self, upload_limit):
+ self.upload_limit = upload_limit
+
+ """
+ Set download limit. Argument in KB/s (kilobytes/s)
+ """
+ def set_download_limit(self, download_limit):
+ self.download_limit = download_limit
+
+ def get_upload_limit_commands(self):
+ upload_limit_commands = []
+ upload_limit_commands.append("tc qdisc add dev %s root handle 1: htb default 90" % (self.vm_iface))
+ upload_limit_commands.append("tc class add dev %s parent 1: classid 1:1 htb rate %dkb ceil %dkb" % (self.vm_iface, self.upload_limit, self.upload_limit))
+ upload_limit_commands.append("tc qdisc add dev %s parent 1:1 handle 10: sfq perturb 10" % (self.vm_iface))
+ upload_limit_commands.append("tc filter add dev %s parent 1:0 protocol ip u32 match ip dst %s match ip dport %d 0xffff classid 1:1" % (self.vm_iface, self.vm_address, self.vm_port))
+
+ return upload_limit_commands
+
+ def get_download_limit_commands(self):
+ download_limit_commands = []
+ download_limit_commands.append("tc qdisc add dev %s root handle 1: htb default 90" % (self.host_iface))
+ download_limit_commands.append("tc class add dev %s parent 1: classid 1:1 htb rate %dkb ceil %dkb" % (self.host_iface, self.download_limit, self.download_limit))
+ download_limit_commands.append("tc qdisc add dev %s parent 1:1 handle 10: sfq perturb 10" % (self.host_iface))
+ download_limit_commands.append("tc filter add dev %s parent 1:0 protocol ip u32 match ip src %s classid 1:1" % (self.host_iface, self.vm_address))
+
+ return download_limit_commands
+
+ def get_flush_commands(self):
+ flush_commands = []
+ flush_commands.append("tc qdisc del dev %s root handle 1:" % (self.host_iface))
+ flush_commands.append("tc qdisc del dev %s root handle 1:" % (self.vm_iface))
+
+ return flush_commands
+
+
+def print_commands(commands, header):
+ print "\n\t== %s ==\n" % (header)
+
+ for c in commands:
+ print c
+
+def main():
+
+ """
+ Test case
+ """
+
+ tc = TrafficControl("openvz")
+ tc.config("141.85.224.201", 10150, "eth0",
+ "172.16.10.0", 10150, "venet0")
+ tc.set_upload_limit(512)
+ tc.set_download_limit(256)
+
+ upload_limit_commands = tc.get_upload_limit_commands()
+ download_limit_commands = tc.get_download_limit_commands()
+ flush_commands = tc.get_flush_commands()
+
+ print_commands(upload_limit_commands, "upload limit commands")
+ print_commands(download_limit_commands, "download limit commands")
+ print_commands(flush_commands, "flush commands")
+
+if __name__ == "__main__":
+ sys.exit(main())