当使用环回地址使用 TCP/IP 套接字进行 IPC 时,常见的网络堆栈是否会跳过将消息帧封装在较低级别的 PDU 中?
在某些环境(例如 Java)中,很自然地使用 TCP/IP 套接字通过“localhost”地址(IPv4 中的 127.0.0.1,或 IPv6 中的 ::1)在同一主机上的进程之间传递消息。 (因为Java倾向于不在其API中公开其他IPC机制)。
显然,这可能比通过管道传递消息的 IPC 或使用共享内存的 IPC 慢很多。
另一方面,如果 TCP/IP 网络堆栈意识到连接的两端都在环回接口上,则它可能能够进行相当多的优化,以便效率与使用管道不会有太大差异。
但是常见的操作系统(Windows、Linux)是否在其 TCP/IP 堆栈中实现了此类优化?
In some environments such as Java, it's natural to use TCP/IP sockets to pass messages between processes on the same host using the 'localhost' address (127.0.0.1 in IPv4, or ::1 in IPv6). (Because Java tends not to expose other IPC mechanisms in its API).
Clearly, this has the potential to be a lot slower than IPC via message passing over pipes, or IPC using shared-memory.
On the other hand, if the TCP/IP networking stack realised that both ends of the connection were on the loopback interface, it might be able to do a fair bit of optimisation so that the efficiency might not differ much from using pipes.
But do common operating systems (Windows, Linux) implement such optimisations in their TCP/IP stacks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。当收到发送到环回地址 (127.xxx) 的数据包/数据时,TCP/IP 的 IP 层使用环回路由将数据包路由到自身。
Yes. When a packet/data to a Loopback address(127.x.x.x) is received, the IP layer of the TCP/IP uses the Loopback route to route the packet to itself.
|| 255.0.0.0 || 127.0.0.1|| 127.0.0.1 || 1
将其路由到其自身后,在 TCP/UDP 层,借助协议控制块(每个连接数据结构),将识别相应的套接字及其所有者进程以传送数据包/数据。
最重要的是,数据链路层和物理层(OSI 模型)的所有任务都将被避免。
|| 255.0.0.0 || 127.0.0.1|| 127.0.0.1 || 1
After routing it to itsef, at TCP/UDP layer with the help of protocol control blocks(per connection data structure) the corresponding socket and its owner process will be identified to deliver the packet/data.
Bottom line, All the tasks at Data Link layers and Physical layers(of OSI Model) will be avoided.
取决于操作系统和所使用的配置。如果您要求默认行为,答案是肯定的。
这就是为什么您无法使用wireshark等工具来嗅探本地环回场景的原因。
[其他用户编辑]:其实这是可以的,你必须选择Loopback接口(使用Wireshark 3.4测试)
Depends upon the OS, and the configuration being used. The answer is yes if you are asking for default behavior.
This is the reason why you are not able to use tools like wireshark to sniff local loopback scenarios.
[Edit from another user]: Actually, this is possible, you have to choose the Loopback interface (Testing with Wireshark 3.4)
特别是在 Linux 中,当数据包在环回接口上传输时,内核会为每个数据包引发一个“软件”中断。从那时起,数据包接收与物理设备的数据包接收流程相同。因此,您的假设是正确的,即通过环回接口进行的通信会比替代 IPC 机制(例如 unix 套接字)慢得多。
我猜内核代码路径可以优化。例如,我们可以直接从环回接口的发送代码路径调用接收代码路径。
Specifically in linux when packets are transmitted on the loopback interface the kernel raises a "software" interrupt for each packet. From that point on the packet reception is identical to the packet reception flow for a physical device. So you are correct in your assumption that communication over the loopback interface would be much slower than alternate IPC mechanisms such as unix sockets.
I guess the kernel code path can be optimized. For e.g., we could call the receive code path directly from the transmit code path of loopback interface.