Last active
May 29, 2018 08:27
-
-
Save ethercflow/3cb33d2034e93b789b4a34588bccbe01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import argparse | |
| import os | |
| import threading | |
| def uplink(ucycle, uspeed, ustep): | |
| os.system('sudo tc qd del dev eno1 root handle 1') | |
| os.system('sudo tc qdisc replace dev eno1 root handle 1: htb default 20') | |
| os.system('sudo tc class replace dev eno1 parent 1: classid 1:1 htb rate %dkbit prio 5' % uspeed) | |
| os.system('sudo tc class replace dev eno1 parent 1:1 classid 1:10 htb rate $[40*%d/100]kbit ceil $[95*%d/100]kbit prio 1' % (uspeed, uspeed)) | |
| os.system('sudo tc class replace dev eno1 parent 1:1 classid 1:20 htb rate $[40*%d/100]kbit ceil $[95*%d/100]kbit prio 2' % (uspeed, uspeed)) | |
| os.system('sudo tc class replace dev eno1 parent 1:1 classid 1:30 htb rate $[20*%d/100]kbit ceil $[90*%d/100]kbit prio 3' % (uspeed, uspeed)) | |
| os.system('sudo tc qdisc replace dev eno1 parent 1:10 handle 10: sfq perturb 10 quantum 6000') | |
| os.system('sudo tc qdisc replace dev eno1 parent 1:20 handle 20: sfq perturb 10 quantum 6000') | |
| os.system('sudo tc qdisc replace dev eno1 parent 1:30 handle 30: sfq perturb 10 quantum 6000') | |
| os.system('sudo tc filter replace dev eno1 parent 1: protocol ip prio 10 u32 match ip tos 0x10 0xff flowid 1:10') | |
| os.system('sudo tc filter replace dev eno1 parent 1: protocol ip prio 11 u32 match ip protocol 1 0xff flowid 1:10') | |
| os.system('sudo tc filter replace dev eno1 parent 1: protocol ip prio 12 u32 match ip protocol 6 0xff match u8 0x05 0x0f at 0 match u16 0x0000 0xffc0 at 2 flowid 1:10') | |
| os.system('sudo tc filter replace dev eno1 parent 1: protocol ip prio 18 u32 match ip dst 0.0.0.0/0 flowid 1:20') | |
| uspeed += ustep | |
| if uspeed <= 0: | |
| uspeed = 10 | |
| t = threading.Timer(ucycle, uplink, (ucycle, uspeed, ustep)) | |
| t.start() | |
| def downlink(dcycle, dspeed, dstep): | |
| os.system('sudo tc qdisc replace dev eno1 handle ffff: ingress') | |
| os.system('sudo tc filter replace dev eno1 parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0') | |
| os.system('sudo tc qdisc add dev ifb0 root handle 2: htb') | |
| os.system('sudo tc class replace dev ifb0 parent 2: classid 2:1 htb rate %skbit' % dspeed) | |
| dspeed += dstep | |
| if dspeed <= 0: | |
| dspeed = 10 | |
| t = threading.Timer(dcycle, downlink, (dcycle, dspeed, dstep)) | |
| t.start() | |
| def delay(lcycle, latency, lstep): | |
| os.system('sudo tc qdisc replace dev ifb0 parent 2:1 handle 11: netem delay %dms' % latency) | |
| os.system('sudo tc filter replace dev ifb0 protocol ip parent 2: prio 1 u32 match ip src 0.0.0.0/0 flowid 2:1') | |
| latency += lstep | |
| if latency < 0: | |
| latency = 0 | |
| t = threading.Timer(lcycle, delay, (lcycle, latency, lstep)) | |
| t.start() | |
| def parse_opts(): | |
| parser = argparse.ArgumentParser(prog='xiannet') | |
| parser.add_argument('--ubase', type = int, help='base uplink bandwidth') | |
| parser.add_argument('--ustep', type = int, help='base uplink bandwidth step') | |
| parser.add_argument('--dbase', type = int, help='base downlink bandwidth') | |
| parser.add_argument('--dstep', type = int, help='base downlink bandwidth step') | |
| parser.add_argument('--bcycle', type = int, help='bandwidth cycle') | |
| parser.add_argument('--latency', type = int, help='latency') | |
| parser.add_argument('--lstep', type = int, help='latency step') | |
| parser.add_argument('--lcycle', type = int, help='latency cycle') | |
| subparsers = parser.add_subparsers(help='sub-command help') | |
| parser_inc = subparsers.add_parser('inc', help='inc iodelay') | |
| parser_inc.set_defaults(func=inc) | |
| parser_dec = subparsers.add_parser('dec', help='dec iodelay') | |
| parser_dec.set_defaults(func=dec) | |
| return parser.parse_args() | |
| def inc(args): | |
| if args.ubase: | |
| uplink(args.bcycle, args.ubase, args.ustep) | |
| if args.dbase: | |
| downlink(args.bcycle, args.dbase, args.dstep) | |
| if args.latency: | |
| delay(args.lcycle, args.latency, args.lstep) | |
| def dec(ages): | |
| if args.ubase: | |
| uplink(args.bcycle, args.ubase, -args.ustep) | |
| if args.dbase: | |
| downlink(args.bcycle, args.dbase, -args.dstep) | |
| if args.latency: | |
| delay(args.lcycle, args.latency, -args.lstep) | |
| if __name__ == "__main__": | |
| args = parse_opts() | |
| args.func(args) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
此脚本控制北京两个机房与西安机房之间的网络带宽和延迟。
eg:
参数说明:
注意: