UNIX/Redhat

[스크랩] 네트워크 bandwidth 제한 - ethtool , tc

99iberty 2014. 1. 7. 17:10

http://www.xelloss.pe.kr/258

 

 

서버 호스팅을 받아보면 알겠지만 국내에서는 이미 1Gbps 은 기본이고, 10Gbps 네트워크에 물려서 마음껏 쓸 수 있도록 해 주고 있습니다만 기본 서비스 대역이 10Mbps를 넘어가게 되면 그때부터는 과금이 눈덩이처럼 불어나게 되어 있습니다.


회사에서야 매출을 위해서 그 정도의 투자는 문제가 없지만 개인이 개발 혹은 특수한 목적으로 서버 호스팅을 받는 것이라면 이는 문제가 심각하게 되지요.


그렇다고 호스팅 회사에서 대역폭을 강제로 10Mbps로 줄여주거나 하지 않는 추세이기 때문에 이제는 스스로 알아서 대역폭을 줄이는 방법 외에는 없게 됩니다.


이러한 환경을 기본으로 해서 네트워크 대역폭을 제한하는 방법으로 제가 실제로 사용한 2가지 방법을 공유코자 합니다.




● 간단하게 10Mbps 로 네트워크 대역폭을 제한하기 - ethtool


Linux에는 네트워크 카드 정보를 확인하거나 설정하는 용도로 쓰이는 ethtool 이라는 명령어가 있습니다.

간단히 실행해 보면 아래와 같이 나옵니다.


[root@test ~]# ethtool eth0

Settings for eth0:

Supported ports: [ TP ]

Supported link modes: 10baseT/Half 10baseT/Full

100baseT/Half 100baseT/Full

1000baseT/Full

Supports auto-negotiation: Yes

Advertised link modes: 10baseT/Half 10baseT/Full

100baseT/Half 100baseT/Full

1000baseT/Full

Advertised auto-negotiation: Yes

Speed: 1000Mb/s

Duplex: Full

Port: Twisted Pair

PHYAD: 1

Transceiver: internal

Auto-negotiation: on

Supports Wake-on: g

Wake-on: g

Link detected: yes


많은 정보가 출력이 됩니다만 우리가 눈여겨 보아야 할 부분은 Supported link modes로 해당 랜카드가 지원할 수 있는 속도 모드라고 보시면 됩니다.


랜카드의 경우 아무리 속도가 빠른 모델이라 하더라도 하위 호환성을 위해서 대걔 10Mbps 부터 지원을 하게 되는데 지금 장착된 랜카드의 경우는 10Mbps, 100Mbps, 1000Mbps를 모두 지원합니다.


Half와 Full 모드가 있기는 하지만 Half모드는 특수한 용도로 사용할뿐 대부분은 Full모드를 사용하니 신경 안쓰셔도 될 것 같습니다.


앞서 이야기 드린 하위호환성을 지원한다고 하였는데요, 바로 이 부분을 이용해서 네트워크 대역폭을 강제로 제한할 수 있습니다.


즉 ethtool 명령어를 통해서 1000Mbps 랜카드를 10Mbps 랜카드 호환모드로 강제 설정하게 하여 대역폭을 10Mbps만 사용하도록 하는 것이지요.


사용법은 아래와 같습니다.


/sbin/ethtool -s eth1 speed 10 duplex full autoneg on


그리고 설정된 값을 보면 이와 같이 나오지요.



root@test:/root# ethtool eth1

Settings for eth1:

Supported ports: [ TP ]

Supported link modes: 10baseT/Half 10baseT/Full

100baseT/Half 100baseT/Full

1000baseT/Full

Supported pause frame use: No

Supports auto-negotiation: Yes

Advertised link modes: 10baseT/Full

Advertised pause frame use: No

Advertised auto-negotiation: Yes

Speed: 10Mb/s

Duplex: Full

Port: Twisted Pair

PHYAD: 1

Transceiver: internal

Auto-negotiation: on

MDI-X: on

Supports Wake-on: pumbg

Wake-on: g

Current message level: 0x00000001 (1)

drv

Link detected: yes


Speed를 보시면 10Mbps로 강제 제한이 된 걸 보실 수 있습니다.

매번 이 명령어를 넣기는 불편하니 /etc/rc.local 에 아래처럼 추가합니다.


/sbin/ethtool -s eth1 speed 10 duplex full autoneg on > /dev/null


상당히 간단하지만 굉장히 효과가 좋은 네트워크 대역폭 제한 방법으로 ethtool을 추천해 드립니다.




● 조금 복잡하지만 내 마음대로 속도를 제안하자 - tc


특수한 경우로 인해서 서버 몇 대를 각각 네트워크 대역폭을 25Mbps 이하로 제안해야 했습니다. 헌데 ethtool은 25Mbps 처럼 임의로 제한을 할 수 없어서 방법을 찾게 되었는데 tc라는 명령어를 통해서 가능하다는 것을 알게 되었습니다.


ethtool과 달리 tc라는 명령어는 사용법이 낮설어서 조금 힘들었는데 검색하다 보니 저만 그런게 아니었나봅니다.

tc를 간단히 사용할 수 있도록 스크립트를 쓰고 있더군요.


출처 : http://www.topwebhosts.org/tools/traffic-control.php



#!/bin/bash

#

# tc uses the following units when passed as a parameter.

# kbps: Kilobytes per second

# mbps: Megabytes per second

# kbit: Kilobits per second

# mbit: Megabits per second

# bps: Bytes per second

# Amounts of data can be specified in:

# kb or k: Kilobytes

# mb or m: Megabytes

# mbit: Megabits

# kbit: Kilobits

# To get the byte figure from bits, divide the number by 8 bit

#


#

# Name of the traffic control command.

TC=/sbin/tc


# The network interface we're planning on limiting bandwidth.

IF=eth0 # Interface


# Download limit (in mega bits)

DNLD=1mbit # DOWNLOAD Limit


# Upload limit (in mega bits)

UPLD=1mbit # UPLOAD Limit


# IP address of the machine we are controlling

IP=216.3.128.12 # Host IP


# Filter options for limiting the intended interface.

U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"


start() {


# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.

# For detailed configuration options, please consult Linux man

# page.


$TC qdisc add dev $IF root handle 1: htb default 30

$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD

$TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD

$U32 match ip dst $IP/32 flowid 1:1

$U32 match ip src $IP/32 flowid 1:2


# The first line creates the root qdisc, and the next two lines

# create two child qdisc that are to be used to shape download

# and upload bandwidth.

#

# The 4th and 5th line creates the filter to match the interface.

# The 'dst' IP address is used to limit download speed, and the

# 'src' IP address is used to limit upload speed.


}


stop() {


# Stop the bandwidth shaping.

$TC qdisc del dev $IF root


}


restart() {


# Self-explanatory.

stop

sleep 1

start


}


show() {


# Display status of traffic control status.

$TC -s qdisc ls dev $IF


}


case "$1" in


start)


echo -n "Starting bandwidth shaping: "

start

echo "done"

;;


stop)


echo -n "Stopping bandwidth shaping: "

stop

echo "done"

;;


restart)


echo -n "Restarting bandwidth shaping: "

restart

echo "done"

;;


show)


echo "Bandwidth shaping status for $IF:"

show

echo ""

;;


*)


pwd=$(pwd)

echo "Usage: tc.bash {start|stop|restart|show}"

;;


esac


exit 0


설정할 부부은 단 4군데입니다.


IF : 제한 하고자 하는 자신의 랜카드를 선택합니다. 특별한 경우가 아니면 대부분 eth0 입니다.

DNLD : 다운로드 제한으로 단위는 mbit 입니다.

UPLD : 업르도 제한으로 단위는 mbit 입니다.

IP : IF에 설정된 IP를 넣으면 됩니다.


해당 스크립트를 저장 및 실행 권한을 주고 tc.sh start 하면 적용 tc.sh stop 하면 종료가 됩니다.


저의 경우는 DNLD와 UPLD를 22mbit 로 했는데 이유는 ethtool과 달리 트래픽 대역을 넘을 수도 있다고 판단을 했기 때문입니다.





36Mbps까지 치솟던 네트워크 대역폭이 tc를 이용한 제한을 걸었더니 19시 40분부터 위와 같이 네트워크 대역폭이 잘리면서 대역폭 제한이 잘 되는 것을 확인할 수 있었습니다.




ethtool과 tc 중 선택은 자유이지만 대부분의 국내 호스팅처럼 10Mbps 제한일 경우 편하고, 단순하면서 강력하게 제한이 되는 ethtool를 사용하시기를 추천해 드립니다.