Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. #!/bin/bash
  2. # Full path to tc binary
  3.  
  4. TC=$(which tc)
  5.  
  6. #
  7. # NETWORK CONFIGURATION
  8. # interface - name of your interface device
  9. # interface_speed - speed in mbit of your $interface
  10. # ip - IP address of your server, change this if you don't want to use
  11. # the default catch all filters.
  12. #
  13. interface=eth0
  14. interface_speed=100mbit
  15. ip=4.1.2.3 # The IP address bound to the interface
  16.  
  17. # Define the upload and download speed limit, follow units can be
  18. # passed as a parameter:
  19. # kbps: Kilobytes per second
  20. # mbps: Megabytes per second
  21. # kbit: kilobits per second
  22. # mbit: megabits per second
  23. # bps: Bytes per second
  24. download_limit=512kbit
  25. upload_limit=10mbit
  26.  
  27.  
  28. # Filter options for limiting the intended interface.
  29. FILTER="$TC filter add dev $interface protocol ip parent 1: prio 1 u32"
  30.  
  31. #
  32. # This function starts the TC rules and limits the upload and download speed
  33. # per already configured earlier.
  34. #
  35.  
  36. function start_tc {
  37. tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"
  38. [ "$?" -gt "0" ] && tc qdisc del dev $interface root; sleep 1
  39.  
  40. # start the tc configuration
  41. $TC qdisc add dev $interface root handle 1: htb default 30
  42. $TC class add dev $interface parent 1: classid 1:1 htb rate $interface_speed burst 15k
  43.  
  44. $TC class add dev $interface parent 1:1 classid 1:10 htb rate $download_limit burst 15k
  45. $TC class add dev $interface parent 1:1 classid 1:20 htb rate $upload_limit burst 15k
  46.  
  47. $TC qdisc add dev $interface parent 1:10 handle 10: sfq perturb 10
  48. $TC qdisc add dev $interface parent 1:20 handle 20: sfq perturb 10
  49.  
  50. # Apply the filter rules
  51.  
  52. # Catch-all IP rules, which will set global limit on the server
  53. # for all IP addresses on the server.
  54. $FILTER match ip dst 0.0.0.0/0 flowid 1:10
  55. $FILTER match ip src 0.0.0.0/0 flowid 1:20
  56.  
  57. # If you want to limit the upload/download limit based on specific IP address
  58. # you can comment the above catch-all filter and uncomment these:
  59. #
  60. # $FILTER match ip dst $ip/32 flowid 1:10
  61. # $FILTER match ip src $ip/32 flowid 1:20
  62. }
  63.  
  64. #
  65. # Removes the network speed limiting and restores the default TC configuration
  66. #
  67. function stop_tc {
  68. tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"
  69. [ "$?" -gt "0" ] && tc qdisc del dev $interface root
  70. }
  71.  
  72. function show_status {
  73. $TC -s qdisc ls dev $interface
  74. }
  75. #
  76. # Display help
  77. #
  78. function display_help {
  79. echo "Usage: tc [OPTION]"
  80. echo -e "\tstart - Apply the tc limit"
  81. echo -e "\tstop - Remove the tc limit"
  82. echo -e "\tstatus - Show status"
  83. }
  84.  
  85. # Start
  86. if [ -z "$1" ]; then
  87. display_help
  88. elif [ "$1" == "start" ]; then
  89. start_tc
  90. elif [ "$1" == "stop" ]; then
  91. stop_tc
  92. elif [ "$1" == "status" ]; then
  93. show_status
  94. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement