25.6. tc-c-ctinfo 유틸리티를 사용하여 패킷의 속도 제한 구성
속도 제한을 사용하면 네트워크 트래픽을 제한하고, 네트워크에서 리소스 소진을 방지하고, 특정 시간 프레임에서 반복적인 패킷 요청을 제한하여 서버의 부하를 줄일 수 있습니다.
connection tracking 항목은 Netfilter 마크 및 연결 정보를 저장합니다. 라우터가 방화벽에서 패킷을 전달하면 라우터는 패킷에서 연결 추적 항목을 제거하거나 수정합니다. 연결 추적 정보(ctinfo) 모듈은 연결 추적 마크에서 다양한 필드로 데이터를 검색합니다. 이 커널 모듈은 소켓 버퍼(skb) 마크 메타데이터 필드에 복사하여 Netfilter 표시를 유지합니다.
사전 요구 사항
-
iperf3유틸리티는 서버 및 클라이언트에 설치됩니다.
프로세스
서버에서 다음 단계를 수행합니다.
네트워크 인터페이스에 가상 링크를 추가합니다.
# ip link add name ifb4eth0 numtxqueues 48 numrxqueues 48 type ifb이 명령에는 다음 매개변수가 있습니다.
name ifb4eth0- 새 가상 장치 인터페이스를 설정합니다.
numtxqueues 48- 전송 대기열 수를 설정합니다.
numrxqueues 48- 수신 대기열 수를 설정합니다.
유형 ifb- 새 장치의 유형을 설정합니다.
인터페이스 상태를 변경합니다.
# ip link set dev ifb4eth0 up물리적 네트워크 인터페이스에
qdisc속성을 추가하고 들어오는 트래픽에 적용합니다.# tc qdisc add dev enp1s0 handle ffff: ingresshandle ffff:옵션에서handle매개 변수는 주요 숫자ffff:를 기본 값으로enp1s0물리적 네트워크 인터페이스에서 클래스별qdisc에 할당합니다. 여기서qdisc는 트래픽 제어를 분석하기 위한 큐링 주체 매개 변수입니다.ip프로토콜의 물리적 인터페이스에 필터를 추가하여 패킷을 분류합니다.# tc filter add dev enp1s0 parent ffff: protocol ip u32 match u32 0 0 action ctinfo cpmark 100 action mirred egress redirect dev ifb4eth0이 명령에는 다음 속성이 있습니다.
상위 ffff:-
주요 번호
ffff:를 상위qdisc로 설정합니다. u32 match u32 0-
u32패턴의 IP 헤더와 일치하도록u32필터를 설정합니다. 첫 번째0은 IP 헤더의 두 번째 바이트를 나타내며 다른0은 일치하는 비트를 알리는 마스크 일치를 나타냅니다. 작업 ctinfo- 연결 추적 마크에서 다양한 필드로 데이터를 검색하는 작업을 설정합니다.
cpmark 100-
연결 추적 마크(connmark)
100을 패킷 IP 헤더 필드에 복사합니다. 작업 mirred egress redirect dev ifb4eth0-
수신된 패킷을
ifb4eth0대상 인터페이스로 리디렉션하려면작업을mirred로 설정합니다.
인터페이스에 클래스
qdisc를 추가합니다.# tc qdisc add dev ifb4eth0 root handle 1: htb default 1000이 명령은 메이저 번호
1을 rootqdisc로 설정하고 마이너 ID1000의 클래스qdisc와 함께htb계층 구조 토큰 버킷을 사용합니다.인터페이스의 트래픽을 2Mbit/s의 상한이 있는 1Mbit/s로 제한합니다.
# tc class add dev ifb4eth0 parent 1:1 classid 1:100 htb ceil 2mbit rate 1mbit prio 100이 명령에는 다음 매개변수가 있습니다.
상위 1:1-
classid를1로 설정하고root를1로 설정합니다. classID 1:100-
classid를1:100으로 설정합니다. 여기서1은 상위qdisc의 수이고100은 상위qdisc의 클래스 수입니다. HTB ceil 2mbit-
htbclassfulqdisc는ceilrate limit으로2 Mbit/s의 상한 대역폭을 허용합니다.
클래스리스
qdisc의 Stochastic Fairness Queuing (sfq)를 적용하여 큐 알고리즘 전달을 줄이기 위해60초의 시간 간격과 상호 작용합니다.# tc qdisc add dev ifb4eth0 parent 1:100 sfq perturb 60인터페이스에 방화벽 표시(
fw) 필터를 추가합니다.# tc filter add dev ifb4eth0 parent 1:0 protocol ip prio 100 handle 100 fw classid 1:100연결 마크(
CONNMARK)에서 패킷 메타 표시를 복원합니다.# nft add rule ip mangle PREROUTING counter meta mark set ct mark이 명령에서
nft유틸리티에는 패킷 마크를CONNMARK로 교체하기 전에 들어오는 패킷을 변경하는PREROUTING체인 규칙 사양이 있는mangle테이블이 있습니다.nft테이블 및 체인이 없는 경우 테이블을 생성하고 체인 규칙을 추가합니다.# nft add table ip mangle # nft add chain ip mangle PREROUTING {type filter hook prerouting priority mangle \;}지정된 대상 주소
192.0.2.3에서 수신되는tcp패킷에 메타 표시를 설정합니다.# nft add rule ip mangle PREROUTING ip daddr 192.0.2.3 counter meta mark set 0x64패킷 표시를 연결 마크에 저장합니다.
# nft add rule ip mangle PREROUTING counter ct mark set mark-s매개변수와 서버를 사용하여 시스템에서iperf3유틸리티를 서버로 실행한 다음 클라이언트 연결의 응답을 기다립니다.# iperf3 -s
클라이언트에서
iperf3을 클라이언트로 실행하고 주기적인 HTTP 요청 응답 타임스탬프를 위해 IP 주소192.0.2.3에서 수신 대기하는 서버에 연결합니다.# iperf3 -c 192.0.2.3 | tee rate192.0.2.3은 서버의 IP 주소이며192.0.2.4는 클라이언트의 IP 주소입니다.Ctrl+C 를 눌러 서버에서
iperf3유틸리티를 종료합니다.Accepted connection from 192.0.2.4, port 52128 [5] local 192.0.2.3 port 5201 connected to 192.0.2.4 port 52130 [ID] Interval Transfer Bitrate [5] 0.00-1.00 sec 119 KBytes 973 Kbits/sec [5] 1.00-2.00 sec 116 KBytes 950 Kbits/sec ... [ID] Interval Transfer Bitrate [5] 0.00-14.81 sec 1.51 MBytes 853 Kbits/sec receiver iperf3: interrupt - the server has terminatedCtrl+C:을 눌러 클라이언트에서
iperf3유틸리티를 종료합니다.Connecting to host 192.0.2.3, port 5201 [5] local 192.0.2.4 port 52130 connected to 192.0.2.3 port 5201 [ID] Interval Transfer Bitrate Retr Cwnd [5] 0.00-1.00 sec 481 KBytes 3.94 Mbits/sec 0 76.4 KBytes [5] 1.00-2.00 sec 223 KBytes 1.83 Mbits/sec 0 82.0 KBytes ... [ID] Interval Transfer Bitrate Retr [5] 0.00-14.00 sec 3.92 MBytes 2.35 Mbits/sec 32 sender [5] 0.00-14.00 sec 0.00 Bytes 0.00 bits/sec receiver iperf3: error - the server has terminated
검증
인터페이스에서
htb및sfq클래스의 패킷 수에 대한 통계를 표시합니다.# tc -s qdisc show dev ifb4eth0 qdisc htb 1: root ... Sent 26611455 bytes 3054 pkt (dropped 76, overlimits 4887 requeues 0) ... qdisc sfq 8001: parent ... Sent 26535030 bytes 2296 pkt (dropped 76, overlimits 0 requeues 0) ...mirred및ctinfo작업에 대한 패킷 수의 통계를 표시합니다.# tc -s filter show dev enp1s0 ingress filter parent ffff: protocol ip pref 49152 u32 chain 0 filter parent ffff: protocol ip pref 49152 u32 chain 0 fh 800: ht divisor 1 filter parent ffff: protocol ip pref 49152 u32 chain 0 fh 800::800 order 2048 key ht 800 bkt 0 terminal flowid not_in_hw (rule hit 8075 success 8075) match 00000000/00000000 at 0 (success 8075 ) action order 1: ctinfo zone 0 pipe index 1 ref 1 bind 1 cpmark 0x00000064 installed 3105 sec firstused 3105 sec DSCP set 0 error 0 CPMARK set 7712 Action statistics: Sent 25891504 bytes 3137 pkt (dropped 0, overlimits 0 requeues 0) backlog 0b 0p requeues 0 action order 2: mirred (Egress Redirect to device ifb4eth0) stolen index 1 ref 1 bind 1 installed 3105 sec firstused 3105 sec Action statistics: Sent 25891504 bytes 3137 pkt (dropped 0, overlimits 61 requeues 0) backlog 0b 0p requeues 0htbrate-limiter 및 해당 구성의 통계를 표시합니다.# tc -s class show dev ifb4eth0 class htb 1:100 root leaf 8001: prio 7 rate 1Mbit ceil 2Mbit burst 1600b cburst 1600b Sent 26541716 bytes 2373 pkt (dropped 61, overlimits 4887 requeues 0) backlog 0b 0p requeues 0 lended: 7248 borrowed: 0 giants: 0 tokens: 187250 ctokens: 93625