![通过硬件环回接口路由数据包,而不是](https://linux22.com/image/799588/%E9%80%9A%E8%BF%87%E7%A1%AC%E4%BB%B6%E7%8E%AF%E5%9B%9E%E6%8E%A5%E5%8F%A3%E8%B7%AF%E7%94%B1%E6%95%B0%E6%8D%AE%E5%8C%85%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF.png)
我的嵌入式Linux板有3个接口:
- eth0-用于所有出站流量
- eth1 - 硬件环回(流量离开主板,但立即返回同一端口)
- lo - 标准环回接口
ifconfig
揭示了以下内容:
eth1 Link encap:Ethernet HWaddr AA:BB:CC:DD:EE:FF
inet addr:169.254.1.1 Bcast:169.254.255.255 Mask:255.255.255.255
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Base address:0x8000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:14 errors:0 dropped:0 overruns:0 frame:0
TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1561 (1.5 KiB) TX bytes:1561 (1.5 KiB)
并且,route
收益为:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
169.254.1.1 * 255.255.255.255 UH 0 0 0 eth1
A.B.C.96 * 255.255.255.240 U 0 0 0 eth0
127.0.0.0 * 255.0.0.0 U 0 0 0 lo
default A.B.C.110 0.0.0.0 UG 0 0 0 eth0
我可以获取ping
eth1 接口的指定 IP,如下所示:
PING 169.254.1.1 (169.254.1.1): 56 data bytes
64 bytes from 169.254.1.1: seq=0 ttl=64 time=0.143 ms
64 bytes from 169.254.1.1: seq=1 ttl=64 time=0.067 ms
但,全部lo
根据ifconfig
报告的 RX/TX 计数器,数据包出现在接口上,而不是 eth1 上。
为什么?交通真的进入和离开eth1
端口,但被计入lo
接口?或者,流量真的全部通过吗lo
?
谢谢!
答案1
本地流量不通过以太网接口。从根本上讲,本地流量通过本地接口。操作系统不知道您的 eth1 接口有硬件环回。
答案2
Linux TCP/IP 协议栈非常灵活。请参阅:
# — let's add dummy IP-address to Wi-Fi NIC
# ip ad ad 11.1.2.3/24 dev wlan0
# — Now chage its scope from 'local' to 'link'
# ip ro replace 11.1.2.3 dev wlan0 scope link table local
…
# — with tcpdump we can see now that's traffic to that dummy
# ex-local IP-address actually tries to go out of Wi-Fi NIC:
00:15:22.807607 ARP, Request who-has 11.1.2.3 tell 10.0.0.7, length 28