# lab11 tc qdisc 패턴 모음 (netem · tbf · htb)

# ─── 현재 상태 ───────────────────────────────────────────────
tc qdisc show                           # 전체
tc qdisc show dev eth0                  # 인터페이스 별
tc -s qdisc show dev eth0               # 통계 포함 (sent / drop / overlimits)
tc -d qdisc show dev eth0               # 상세 (파라미터)

# ─── netem (Network Emulator) ────────────────────────────────
# 지연
sudo tc qdisc add dev eth0 root netem delay 50ms
sudo tc qdisc add dev eth0 root netem delay 50ms 10ms                       # ±지터
sudo tc qdisc add dev eth0 root netem delay 50ms 10ms distribution normal   # 정규분포
sudo tc qdisc add dev eth0 root netem delay 50ms 10ms 25%                   # 상관

# 손실
sudo tc qdisc change dev eth0 root netem loss 1%
sudo tc qdisc change dev eth0 root netem loss random 1% 25%                 # 25% 는 상관
sudo tc qdisc change dev eth0 root netem loss state 1% 99% 100% 0%          # state model
sudo tc qdisc change dev eth0 root netem loss gemodel 1% 10% 70% 0.1%       # Gilbert-Elliott

# 중복 / 변조 / 재정렬
sudo tc qdisc change dev eth0 root netem duplicate 1%
sudo tc qdisc change dev eth0 root netem corrupt 0.1%
sudo tc qdisc change dev eth0 root netem reorder 25% 50%

# 조합
sudo tc qdisc add dev eth0 root netem delay 25ms 5ms distribution normal loss 0.1%

# ─── tbf (Token Bucket Filter) — 대역폭 제한 ─────────────────
sudo tc qdisc add dev eth0 root tbf rate 100mbit burst 32kbit latency 400ms
sudo tc qdisc change dev eth0 root tbf rate 1gbit burst 256kbit latency 200ms
sudo tc qdisc change dev eth0 root tbf rate 10mbit burst 16kbit latency 200ms

# ─── HTB (Hierarchical Token Bucket) — 클래스별 분배 ─────────
sudo tc qdisc add dev eth0 root handle 1: htb default 12
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit
sudo tc class add dev eth0 parent 1:1 classid 1:11 htb rate 30mbit ceil 100mbit
sudo tc class add dev eth0 parent 1:1 classid 1:12 htb rate 70mbit ceil 100mbit
# 필터: 192.168.1.10 의 트래픽은 1:11 로
sudo tc filter add dev eth0 protocol ip parent 1: prio 1 u32 match ip src 192.168.1.10 flowid 1:11

# ─── tbf + netem 조합 (대역폭 + 지연) ────────────────────────
sudo tc qdisc add dev eth0 root handle 1: tbf rate 100mbit burst 32kbit latency 400ms
sudo tc qdisc add dev eth0 parent 1:1 handle 10: netem delay 25ms 5ms

# ─── 원복 ────────────────────────────────────────────────────
sudo tc qdisc del dev eth0 root
sudo tc qdisc del dev eth0 ingress

# ─── ingress (들어오는 트래픽) ───────────────────────────────
sudo tc qdisc add dev eth0 handle ffff: ingress
sudo tc filter add dev eth0 parent ffff: u32 match ip protocol 1 0xff police rate 1mbit burst 10k drop

# ─── kernel module ───────────────────────────────────────────
sudo modprobe sch_netem
sudo modprobe sch_tbf
sudo modprobe sch_htb
lsmod | grep sch_

# ─── 현실 회선 프로파일 (delay JITTER loss) ──────────────────
# 서울↔부산        25ms  5ms  0.1%
# 서울↔도쿄        35ms 10ms  0.05%
# 서울↔미서부     130ms 20ms  0.2%
# 서울↔미동부     200ms 30ms  0.3%
# LTE 모바일       50ms 30ms  0.5%
# 저궤도 위성      30ms 20ms  1%
# 정지궤도 위성   550ms 50ms  0.5%
# 사내 LAN          1ms  0.5ms 0.001%
# WiFi (혼잡)      20ms 15ms  1%
