"How the Network is Connected" (2.2)

(6 messages) "How the network is connected" (2.1)_qq_38480311's blog-CSDN blog

This article is mainly based on chapters 2.5 and 2.6 of Chapter 2 of "How the Network is Connected".

Table of contents

Brief description: The main content of this article is the transceiver operation of Ethernet and the transceiver operation of UDP protocol.

Packet sending and receiving operation of IP and Ethernet

What is a package made of?

 How are packets forwarded?

The division of labor between routers and hubs? the difference?

The transmission process of IP packets

How are IP and MAC headers generated?

How to query MAC address? Use the ARP protocol

How is the packet converted into an optical signal? NIC mechanism

UDP protocol 

What is the biggest difference between the UDP protocol and the TCP protocol?

What scenarios are UDP suitable for?

Brief description: The main content of this article is the transceiver operation of Ethernet and the transceiver operation of UDP protocol.

The TCP module adds the TCP header to the packet, and entrusts the IP module to add the IP and MAC headers to the packet, and the IP packet is generated. After the IP packet is generated, the MAC module of the network card will take the packet out of the buffer, add a header and a start frame delimiter at the beginning, add a frame check sequence for error detection at the end, and then convert it into a general photoelectric signal ,Again. It is converted into a format that can be transmitted in the network cable by the PHY (MAU) module, and sent out through the network cable.

The packet will pass through the hub and router during the forwarding process. The router uses the IP protocol, and the hub uses the Ethernet or MAC protocol. During forwarding, the IP of the header will not change, but the MAC address of the header will change. Simply understand that IP is end-to-end, and the destination IP will not change, but the intermediate transit node device, that is, the MAC address will change. (Chapter 3 of hubs and routing will be explained in detail)

After arriving at the destination, the MAC module converts the signal into digital information from the beginning, checks the FCS (that is, checks whether the packet is correct), and then checks whether the MAC address matches, and discards if it does not match. The module then checks whether the IP address matches and whether it is fragmented (if the fragments and other packets are fully assembled), caches the data and waits for the application to call.

UDP is used to send short packets, without TCP receiving confirmation, window and other mechanisms, so there is no need to exchange control information before sending and receiving data, that is to say, there is no need to establish and disconnect the steps of the connection, it is only responsible for sending packets. It doesn't care about errors or packet loss.

Packet sending and receiving operation of IP and Ethernet

Structure of an IP packet

What is a package made of?

A packet is composed of two parts, header and data (Figure 2.14(a)). The header contains control information such as the destination address, which can be understood as the express delivery package; behind the header is the data that the entrusting party will send to the other party, which is equivalent to the goods in the express package.

 How are packets forwarded?

The forwarding device will judge where to send it next based on the information in the header . This process requires a table, and the result of the table lookup is "the packet with the destination address of ×××× should be sent to the line of ××××", then the forwarding device will send this packet to the number of ×××× line to go. Next, in the process of moving to the destination, the packet will reach the next forwarding device, and then be sent to the next forwarding device in the same way

The division of labor between routers and hubs? the difference?

There are two different forwarding devices, routers and hubs,  in the network , and they have their own division of labor when transmitting network packets.

(1) The router needs to use the IP header, transmit the packet according to the IP protocol, and judge the location of the next router according to the destination address. During the transmission of the packet, the destination IP address will not change.

(2) The hub needs to use the MAC header to transmit the packet according to the Ethernet protocol, and is responsible for transmitting the network packet to the next route in the subnet, and the target MAC address will change during the packet transmission.

The transmission process of IP packets

1. The sender writes the destination P address of the packet into the IP header, and the IP protocol can find the transmission direction of the packet according to this address, and thus find the location of the next router, which is router R1 in Figure 2.16. Next , the IP protocol will entrust the Ethernet protocol to transmit the packet. At this time, the IP protocol will look up the Ethernet address (MAC address) of the next router and write this address into the MAC header. In this way, the Ethernet protocol knows which router to send the packet to.

During the transmission of network packets (Figure 2.16①), the hub will pass through the hub, which is a device that works according to the Ethernet protocol. In order to judge where the packet should be transmitted next, there is a table (table for Ethernet protocol) in the hub , and the corresponding transmission direction can be found out according to the destination information recorded in the Ethernet header. There is only one hub in this picture. When there are multiple hubs, network packets will be transmitted through these hubs one by one in order.

Next, the packet goes to the next router (Figure 2.16②). There is an IP protocol table in the router, and it can be found out which router should be sent to next according to this table and the destination information recorded in the IP header. In order to send the packet to the next router, we also need to find out the MAC address of the next router and record it in the MAC header , which can be understood as rewriting the MAC header [illustration]. In this way, the network packet is sent to the next node again.

Self-understanding: IP can be regarded as an end-to-end express delivery, that is, the starting point and the end point of the express delivery. Think of each router as a transit point for express delivery. During the transportation process, the sender and recipient, that is, the sending address and receiving address of the IP packet, are fixed and will not change , but the transit point will change. The address of the transfer point is identified by the MAC address. Every time a transfer is made, the next destination transfer point will change, so the MAC address will change.

How are IP and MAC headers generated?

At the beginning of sending the packet, the TCP module entrusts the IP module. The TCP module will add the TCP header at the beginning of the packet. The TCP module also needs to specify the IP address of the communication object, that is, it needs to write clearly "what content to send to whom", and then passed to the IP module. The IP module will add two types of headers, the IP header and the MAC header. Next, the encapsulated packet will be handed over to network hardware (network hardware can be understood as a network card, there are various types) ("②send" in Figure 2.17), such as Ethernet, wireless LAN, etc. After the package is delivered to the other party, the other party will respond. The returned packet will also be sent back through the forwarding device, and then we need to receive this packet. TCP will have different packets, but no matter whether the packets to be sent and received are control packets or data packets, the sending and receiving operations of IP for various types of packets are the same.

 

The IP address is not actually assigned to the computer, but to the network card, so when there are multiple network cards on the computer, each network card will have its own IP address. Multiple network cards are installed on many servers. At this time, a computer has multiple IP addresses. When filling in the IP address of the sender, it is necessary to determine which address should be filled in.

The beginning of the MAC header is the MAC address of the receiver and the sender. They have similar functions to the receiver and sender IP addresses in the IP header, except that the length of the IP address is 32 bits, while the MAC address is 48 bits.

How to query MAC address? Use the ARP protocol

ARP is to use broadcasting to ask all devices: "Who is this IP address of ××? Please tell me your MAC address." Then someone will answer: "This IP address is mine, and my MAC address is ×× ××."

Put the query results in a memory space called ARP cache for later use. When sending a packet, check the ARP cache first. If the MAC address of the other party has been saved in it, there is no need to send an ARP query, and the address in the ARP cache is used directly. When the MAC address of the other party does not exist in the ARP cache, send ARP query.

How is the packet converted into an optical signal? NIC mechanism

The network packets generated by IP are just a series of digital information stored in the memory. The digital information needs to be converted into electrical or optical signals before they can be transmitted on the network cable. That is to say, this is the real data transmission process.

It is the network card that is responsible for performing this operation, but the network card cannot work alone, and the network card driver is required to control the network card. The unique MAC address in the world is saved in the ROM of the network card, which is written when the network card is produced, and the MAC module can be set after reading out this value.

 Network card structure

After the network card driver obtains the packet from the IP module, it will copy it to the buffer in the network card, and then send a command to the MAC module to send the packet. The MAC module will take the packet out of the buffer, add a header and a start frame delimiter at the beginning, and add a frame check sequence for error detection at the end (Figure 2.24).

 The header is a string of bit sequences like 10101010... such that 1 and 0 appear alternately, with a length of 56 bits. Its function is to determine the timing of reading the packet. When these 1010 bit sequences are converted into electrical signals, a waveform like that shown in Figure 2.25 will be formed.

 When using electrical signals to express digital information, we need to make the two bits of 0 and 1 correspond to specific voltage and current respectively; by receiving and measuring the voltage and current changes in the signal, the values ​​of the two bits of 0 and 1 are restored. When the signal is continuously 1 or 0, the boundary between bits will disappear, and the clock signal can be superimposed to help determine the boundary between bits.

 The operation of sending signals is divided into two types, one is the half-duplex mode using the hub, and the other is the full-duplex [illustration] mode using the switch. In half-duplex mode, in order to avoid signal collision, it is first necessary to determine whether there are signals sent by other devices in the network cable. If there is, you need to wait for the signal to be transmitted, because if you send another set of signals when there is a signal, the two sets of signals will collide. First, the MAC module converts the digital information into an electrical signal per bit from the header , and then sends it out by the PHY, or the signal transceiver module called MAU [illustration]. Here, the rate at which digital information is converted into electrical signals is the transmission rate of the network. For example, if 10 Mbit of digital information is converted into electrical signals per second and sent, the rate is 10 Mbit/s.

The MAC module of the network card generates a general-purpose signal, which is then converted into a format that can be transmitted in the network cable by the PHY (MAU) module, and sent out through the network cable. Ethernet does not confirm whether the signal sent by the other party has been received. According to the specifications of Ethernet, the network cable between two devices cannot exceed 100 meters.

In the process of sending signals, there is a very small possibility that multiple devices will perform sending operations at the same time. If there are other devices sending signals at the same time, those signals will come through the receiving line. In half-duplex mode using a hub, once this happens, the two sets of signals are superimposed on each other, making them indistinguishable. This is called a signal collision. In this case, there is no point in continuing to send the signal, so the send operation is terminated. In order to notify other devices that the current line has collided, a blocking signal will be sent for a period of time, and then all sending operations will be stopped.

receive return packet

In half-duplex Ethernet using a hub, a signal sent by one device reaches all devices connected to the hub. The PHY (MAU) module will convert the signal into a common format and send it to the MAC module, and the MAC module will convert the signal into digital information from scratch and store it in the buffer. The FCS also needs to be checked when the end of the signal is reached. If there is no problem with the FCS check, the next step is to check whether the MAC address of the receiver in the MAC header is consistent with the MAC address assigned to itself by the network card during initialization. The MAC module work is completed, and the network card will notify the computer that a packet has been received.

  • When the network card initiates an interrupt, the network card driver will be called automatically. After the network card driver is called by the interrupt handler, it will take out the received packet from the buffer of the network card, and judge the type of the protocol through the Ethernet type field in the MAC header.
  • Next, it is the turn of the IP module to start working. The first step is to check the IP header to confirm whether the format is correct. If there is no problem with the format, the next step is to check the IP address of the receiver. If it is wrong, the client computer is not responsible for forwarding the packet, and the IP module will notify the sender of the error through an ICMP message. If the receiver's IP address is correct, the packet will be received, and another work needs to be done at this time.
  • The IP protocol has a function called fragmentation (fragmentation simply understands that a large packet is divided into small packets). When a fragmented packet is received, the IP module will temporarily store it in the internal memory space, and then wait for the IP header. The packets with the same ID in all arrive because all fragments of the same packet have the same ID. The IP header also has a fragment offset (fragment offset) field, which indicates the position of the current fragment in the entire packet. According to this information, after all the fragments are received, they can be restored to the original package. This operation is called fragment reassembly
  • After the work of the IP module is completed, the TCP module will search for the corresponding socket according to the receiver and sender IP addresses in the IP header, and the receiver and sender port numbers in the TCP header. Find the socket, confirm the return packet, cache the data and wait for the application to fetch the data, and end.

UDP protocol 

What is the biggest difference between the UDP protocol and the TCP protocol?

TCP needs to send data to the other party efficiently and reliably. It needs to establish and disconnect the connection. It needs to confirm whether the other party has received the data we sent. If not, it needs to resend it.

UDP does not need to establish and disconnect the connection. The data of UDP is relatively small, and generally one packet can be loaded. Even if the sending fails, it is enough to resend this packet. After the data is sent, the other party will generally give a reply, as long as the reply data is regarded as a receipt confirmation, there is no need for a special reception confirmation packet.

What scenarios are UDP suitable for?

The operation of exchanging control information, such as DNS query, can basically be resolved within the size of a packet. In this scenario, UDP can be used instead of TCP.

When sending audio and video data. Audio and video data must be delivered within the specified time. Once the delivery is late, the playback opportunity will be missed, resulting in sound and image freezes. In these cases where there is no need to resend the data, or it is meaningless to resend it, it is more efficient to use UDP to send data.

Guess you like

Origin blog.csdn.net/qq_38480311/article/details/132033338