How do hosts communicate

How do hosts communicate

在开始讨论这个话题之前,我先介绍一下交换机以及路由器转发数据的基本行为。

  1. 交换机:交换机是二层网络设备,也就是说Mac地址是交换机转发数据的依据。在交换上有一张CAM表,用于存储Mac地址以及对应的端口号。当交换机收到数据包时,会检查包头的源目Mac地址,源Mac地址会被学习并存到CAM表中。同时将目的MAC地址与CAM表进行对比,如果找到了匹配条目,数据包会从对应端口发出;如果没有找到匹配条目, 数据包会从除接收端口以外的所有端口发出也就是广播。
  2. 路由器:路由器是三层网络设备,也就是说IP地址是路由器转发数据的依据。路由表存储在路由器中,当路由器收到数据包时,会拆开二层包头,查看三层包头的目的IP地址,并在路由表中根据最佳匹配原则进行递归查找。如果检查完毕依然没有路由可以匹配,数据包将被丢弃。

现在进入正题,主机A如何与主机B或者C通讯。如图所示,主机A,B,C已经配置了IP地址以及网关,路由器两个接口F1和F2配置为子网网关IP地址。假设所有设备都处于初始状态。

主机A试图与主机B通讯
• A知道B与他在同一个子网,所以会以自己的MAC地址为源,全F的MAC地址为目的发一个 ARP请求消息询问B的Mac地址。
• 交换机端口1收到此ARP消息后,首先会学习到主机A的MAC地址, 将此地址与对应的端口存入CAM表中。同时将此消息在除端口1以为的所有端口进行转发(广播)。
• 主机B收到此消息后发现目的是自己,会以自己的MAC地址为源,A的MAC地址为目的回复一个ARP答复消息。
• 交换机端口2收到此ARP答复消息后,同样会学习此MAC地址,并将消息从端口1转发。
• 主机A收到ARP答复消息后,就可以与主机B正常通讯了。

主机A试图与主机C通讯
• A知道C与他不在同一个子网,所以会以自己的MAC地址为源,全F的MAC地址为目的发一个 ARP请求消息询问网关的Mac地址。
• 交换机端口1收到此ARP消息后,首先会学习到主机A的MAC地址, 将此地址与对应的端口存入CAM表中。同时将此消息在除端口1以为的所有端口进行转发(广播)。
• 路由器接口1收到此消息后发现目的是自己,会以自己的MAC地址为源,A的MAC地址为目的回复一个ARP答复消息。
• 交换机同样会从端口3学习到路由器接口1的MAC地址,并从端口1将ARP答复转发给主机A。
• A此时为以自己的IP地址为源,C的IP地址为目的,以自己的MAC地址为源,以网关的MAC地址为目的组成包头向C发送消息。
• 由于交换机已经学习到此目的MAC地址,所以会从端口3转发此消息给路由器。
• 路由器收到此消息后,拆开数据包,根据路由表匹配出目的IP的转发接口为F2。
• 路由器以F2的MAC地址为源,C的MAC地址为目的,从新封装2层包头。与交换机不同的时,路由器将MAC地址信息存放在ARP表中。
• 至此,A与C可以正常通讯。

Let’s say host A is trying to communicate with host B and host C, IP address and Gateway has been configured on each host, and we presume all devices are at initialised status.

Before we start to talk about this, let me explain the basic behavior of switch when forwarding packets.

  1. Switch: we know switch is a layer 2 device, which means it transit packets based on Mac-address. Switch uses CAM table to store Mac-address in accordance with switch port where it learns about the address. When a packet is received, switch first check the designated destination Mac-address, and then compare with the CAM table. If a match is found, switch will only forward this packet through the designated port; if no match is found, switch will broadcast the packet from all its ports except the port where the packet is received.

  2. Router: router is a layer 3 device which means IP address is the determinant of how to forward packets. There is a routing table stored on router, when a packet is received, router will unpack it and check the destination IP address, and then compare with Routing table. Router will do recursive lookup to determine how to forward this packet, if recursive lookup is done and egress interface cannot be found, this packet will be dropped.

    Now, let’s talk about the communication host A is trying to make.

Host A is trying to communicate with Host B.
• A (192.168.1.100) knows that B is the in the same subnet with him, so A initiates an ARP request to ask for Mac-address of B (192.168.1.101) with the source Mac-address of A and destination Mac-address of FF:FF:FF:FF:FF:FF.
• This packet is received on switch port 1, switch will learn the source Mac-address of A and store it to CAM table in accordance with port 1. And broadcast this ARP request from all other ports except port 1.
• Host B received ARP request and find out the destination it is himself, and B replies this message with ARP reply with source Mac-address of B and destination Mac-address of A.
• Switch will also learn the source Mac-address of B and store it to CAM table in accordance with port 2 and forward this packet through port 1.
• After the ARP reply is received by Host A, A is able send message to the right destination Mac-address.

Host A is trying to communicate with Host C
• A (192.168.1.100) knows that C is in the different subnet with him, so A initiates an ARP request to ask for Mac-address of Gateway (192.168.1.1) with the source Mac-address of A and destination Mac-address of FF:FF:FF:FF:FF:FF.
• This packet is received on switch port 1, switch will learn the source Mac-address of A and store it to CAM table in accordance with port 1. And broadcast this ARP request from all other ports except port 1.
• Router interface F1 received ARP request and find out the destination it is himself, and router replies this message with ARP reply with source Mac-address of F1 and destination Mac-address of A.
• Switch will also learn the source Mac-address of Router interface F1 and store it to CAM table in accordance with port 3 and forward this packet through port 1.
• After the ARP reply is received by Host A, A will initiate packet to C with source IP address of A and destination IP address C, as well as source Mac-address of A and destination Mac-address of router interface F1.
• Since switch has learned the destination Mac-address, the packet will be forwarded through port 3 and router interface F1 will receive this packet.
• This packet in unpacked by router which will be determined to forward through F2 based on routing table.
• Router will re-pack this packet with source Mac-address of F2 and destination Mac-address of C. Unlike switch, router store mac-address at ARP table.
• Now, host A and C is able to communicate with each other.

猜你喜欢

转载自blog.51cto.com/13831525/2132673