본문 바로가기

Linux

Linux Network Traffic Control

@markdown


# Linux Network Traffic Control


Ubuntu에서는 기본적으로 `tc` 유틸리티를 제공하고 있다. `tc`를 사용하면 대여폭 제한, 드랍, 복제와 같은 다양한 장애 상황을 시뮬레이션 할 수 있다. 단, `tc`는 나가는 트래픽에 대해서만 시뮬레이션을 지원한다.



# 예제1: tbf 사용


tc 는 traffic control 을 의미하며, qdisc 는 queue discipline 을 의미한다.


```

대상 네트워크 인터페이스: enp0s3

대여폭: 2Kbps

지연: 150ms

손실: 10%


sudo tc qdisc add dev enp0s3 root handle 1:0 netem delay 150ms loss 10%

sudo tc qdisc add dev enp0s3 parent 1:1 handle 10: tbf rate 2kbit buffer 1600 limit 3000

```


# 예제2: htb 사용


tc_set.sh

```

#!/bin/bash

if (( $(tc -s qdisc | grep netem | wc -l) ))

then

echo "traffic control is already enabled"

exit 1

fi

echo "Available Interfaces"

echo "===================="

ls --color=tty -1 /sys/class/net/

echo "Configuration example: Interface[enp3s0] Bandwidth[4Mbit] Delay[10ms] Loss[10%]"

echo -n "Enter Interface: " && read NIC

echo -n "Enter Bandwidth: " && read BANDWIDTH

echo -n "Enter Delay:     " && read DELAY

echo -n "Enter Loss:      " && read LOSS

if [[ -z "$NIC" ]]

then

echo "Error: Interface is not specified"

return 1

fi

if [[ -z "$BANDWIDTH" ]]

then

echo "Error: Bandwidth is not specified"

return 1

fi

if [[ -z "$DELAY" ]]

then

echo "Error: Delay is not specified"

return 1

fi

if [[ -z "$LOSS" ]]

then

echo "Error: Loss is not specified"

return 1

fi

sudo tc qdisc add dev "${NIC}" handle 1: root htb default 11

sudo tc class add dev "${NIC}" parent 1:1 classid 1:11 htb rate "${BANDWIDTH}"

sudo tc qdisc add dev "${NIC}" parent 1:11 handle 10: netem delay "${DELAY}" loss "${LOSS}"

echo "Traffic control is started"

```


tc_unset.sh


```

#!/bin/bash

NIC=$(tc -s qdisc | grep netem | grep -oP "dev \\w+ " | cut -f2 -d' ') 

sudo tc qdisc del dev "${NIC}" root

echo "Traffic control is stopped"

```

각 항목들은 선택적으로 적용 가능하다. 제한을 걸 항목에 대해서만 명령줄에 넣어주면 된다.

'Linux' 카테고리의 다른 글

Reverse SSH  (0) 2018.06.26
리눅스 파일시스템 계층 표준  (0) 2018.02.01
Boost Productivity with Z and Zsh on Ubuntu  (0) 2017.04.26
Linux Network Interface Name  (0) 2017.03.26
터미널에서 헥스덤프 만들기  (0) 2017.02.26