Tools & Debug¶
Cấu hình và quan sát với iproute2¶
ip -br link # trạng thái tất cả interface
ip -br addr # địa chỉ IP
ip route # bảng route
ip neigh # bảng ARP/NDP
ip -s link show eth0 # thống kê RX/TX, error, drop
ip monitor # theo dõi thay đổi qua netlink
Socket: ss¶
ss -tlnp # TCP đang listen, kèm tiến trình
ss -tunap # tất cả TCP/UDP
ss -ti # thông tin TCP nội bộ (rtt, cwnd, retrans)
Bắt gói: tcpdump¶
tcpdump -i eth0 -nn # bắt cơ bản
tcpdump -i eth0 -nn port 502 # theo port
tcpdump -i eth0 -nn host 192.168.1.10 and tcp
tcpdump -i eth0 -w /tmp/cap.pcap # ghi file, mở bằng Wireshark
tcpdump -i eth0 -e -nn vlan # hiện header L2 và VLAN
Trên thiết bị nhúng nên bắt bằng tcpdump -w rồi phân tích trên PC, và giới hạn dung lượng (-c, -C) để không đầy flash.
Đo băng thông: iperf3¶
iperf3 -s # phía server
iperf3 -c 192.168.1.10 -t 30 # TCP, 30 giây
iperf3 -c 192.168.1.10 -u -b 500M # UDP, 500 Mbit/s, xem loss và jitter
iperf3 -c 192.168.1.10 -P 4 # 4 luồng song song
Throughput thấp bất thường: kiểm tra CPU (top, softirq), offload (ethtool -k), tốc độ link thật (ethtool), và số lỗi/drop (ethtool -S).
Firewall: nftables¶
nft list ruleset
nft add table inet filter
nft add chain inet filter input '{ type filter hook input priority 0; policy drop; }'
nft add rule inet filter input ct state established,related accept
nft add rule inet filter input iif lo accept
nft add rule inet filter input tcp dport 22 accept
Thiết bị nhúng nên mặc định drop ở chain input và chỉ mở cổng cần thiết. Xem thêm SSH.
Quy trình debug khi mất kết nối¶
- L1/L2:
ethtool eth0cóLink detected: yes?dmesgcó lỗi PHY? Cáp, magnetics. - L2:
ip neighcó phân giải được MAC của gateway?tcpdump -ecó thấy ARP request/reply? - L3:
ip routeđúng chưa?pinggateway, rồipingđích. - L4:
ss -tlnpcó đang listen? firewallnft list rulesetcó chặn? - Hiệu năng:
ethtool -S,ip -s link,iperf3.