Let’s focus on the data link layer. Without the data link layer, computer networks would not exist. It’s like a building without a foundation, a person without legs; Therefore, the knowledge of data link layer is also important, many partners only focus on TCP and IP these two protocols, this is a narrow thinking, need to be corrected in time, computer network is not only TCP and IP. Cxuan will talk to you about the data link layer in the computer.
Data link layer
The data link layer, according to the OSI seven-layer model, is the upper layer of the physical layer
The data link layer is a protocol layer that has many protocols. The data link layer is used to transmit data between nodes in the network segment across the physical layer. It usually refers to communication means such as Ethernet and wireless LAN. The data link layer provides the capability to transfer data between two entities of the network and error detection to correct errors that occur in the physical layer.
A key concept
In the data link layer, the link layer address has many different names: LAN address, physical address or MAC address. Since MAC address is the most popular term, we generally refer to the link layer address as the MAC address.
Let’s look at some of the key concepts of the data link layer
Packaged into the frame
Framing: Almost all link-layer protocols wrap each network layer datagram with a link layer before it is transmitted. The data link layer obtains data from the network layer and encapsulates it into frames. If the frame is too large, the data link layer will split the large frame into smaller frames, which can make transmission control and error detection more efficient.
A frame is the encapsulation of a sequence of 0 and 1.
A frame consists of Header, Payload Field, and Trailer. The network-layer datagram is encapsulated in the Payload Field. Each frame has a different structure depending on the physical medium. The composition of the frame is as follows
The main contents of the frame are as follows
The Frame head (Frame header)
: contains the source address and destination address of the frame.Payload Field
: It contains data and information to be passed.Trailer
: Contains error detection and error correction bits.Tag (Flag)
It marks the beginning and end of a frame.
The Flag bits are at the beginning and end of the frame, and two consecutive flags indicate the end and beginning of the frame
There are two main types of frames, fixed size and variable size.
Fixed-sized Framing
: indicates that the size of the frame is fixed, and the length of the frame acts as the boundary of the frame, so it does not require additional boundary bits to mark the beginning and end of the frame.Sized Framing
: indicates that each true size is not fixed, thus preserving other mechanisms to mark the end of one frame and the beginning of the next. Commonly used in lans, the two ways to define frame delimiters in variable-size frames areLength Field
: Uses the length field to determine the size of the frame. It is used for Ethernet (IEEE 802.3)End Delimiter
: is often used for token rings
Link to access
Link Access refers to the MAC protocol. Medium Access Control (MAC) defines frame transmission rules on links. As we know, The data link layer is The second layer of The OSI standard model. The data link layer can be subdivided into The Logical Link Control (LLC) layer and The Medium Access Control (MAC) layer.
The LLC layer, also known as the Logical control link layer, is mainly used for data transmission. It acts as the interface between the network layer and the media access Control (MAC) sub-layer of the data link layer. The main functions of the LLC layer are as follows
- The main function of LLC is to multiplex protocols at the MAC layer when sending and equally multiplex protocols when receiving.
- LLC provides skip flow and error control, such as data transfer between neighboring nodes called routers
A jump
. - It allows multipoint communication over computer networks.
MAC layer is responsible for the flow control and multiplexing of transmission media. Its main functions are as follows
- The MAC layer provides the physical layer abstraction for the upper layers of LLC and OSI networks.
- The MAC layer is responsible for sealing frames for transmission through physical media.
- The MAC layer is responsible for resolving source and destination addresses.
- The MAC layer is also responsible for performing conflict resolution and initiating retransmission in the event of a conflict.
- The MAC layer is responsible for generating the frame check sequence, which helps prevent transmission errors.
One of the key concepts in the MAC layer is the MAC address. MAC addresses are used to identify interconnected nodes on data links, as shown in the following figure
The MAC address is 48 bits long. If a NIC is used, the MAC address is usually burned into the ROM. Therefore, the MAC address of any network adapter is unique. The structure of the MAC address is as follows
The 3 to 24 digits in the MAC address indicate the vendor identifier. Each NIC vendor has a unique identifier. The 25-48 bits are used internally by the manufacturer to identify each network card. Therefore, you can guarantee that there will not be network cards with the same MAC address in the world.
There are exceptions to MAC addresses. For example, you can manually change the MAC address. However, there is no problem as long as the two MAC addresses belong to different data link layers.
Reliable delivery
The reliable delivery provided by the network layer refers more to end-to-end system delivery, while the reliable delivery provided by the data link layer refers more to single-end link node-to-node transmission. When the link layer protocol provides reliable delivery, it ensures error-free movement of each network layer datagram across the link layer. The link layer provides reliable delivery in a similar way to TCP, using acknowledgment and retransmission.
Reliable delivery of the link layer is typically used for error-prone links, such as wireless links, where the purpose is to correct an error-prone frame locally rather than forcing end-to-end data transfer through transport layer or application layer protocols. The delivery overhead of the link layer is not necessary for links with low error rates, such as fiber, coaxial, and twisted pair, and for this reason these links generally do not provide reliable delivery
Error detection and correction
Data at the link layer is sent in the form of frames. During transmission, the hardware at the link layer of the receiving node may incorrectly identify 1 as 0 and 0 as 1 due to signal interference or electromagnetic noise. There is no need to forward an errant datagram in this case, so many link layer protocols provide a mechanism to detect such bit errors. This is done by having the party node include error-detecting bits in the frame and the receiving node do error-checking.
The transport layer and the network layer implement error detection through Internet checksum, while the error detection at the link layer is usually more complex and implemented by hardware. Error correction is similar to error detection, the difference is that the receiver can not only detect the bit error in the frame, but also accurately determine the location of the error in the frame.
Error detection and correction techniques are mainly
- Parity check: It is mainly used for error detection and correction
- Checksum: This is a method used to verify the transport layer
- Cyclic redundancy check: this applies more to the link layer in adapters
Address mapping
Because there are network layer addresses (IP addresses) and data link layer addresses (MAC addresses), they need to be translated and mapped. This is what the ADDRESS resolution protocol does. For more information about the address resolution protocol, please refer to
Have a thorough understanding of ARP
Role of the data link layer
The protocols in the data link layer define the specifications for the transmission of data between two devices in an Internet network. The data link layer needs to take communication media as the transmission carrier, and the communication media includes infrared devices such as twisted-pair copper wire, optical fiber and radio wave. In the data distribution device there are switches, Bridges, Repeaters and other data transfer. Any device in the link layer is also called node, and the communication channel between adjacent nodes along the communication path is called link. In fact, both the link layer and the physical layer play a role in the transmission of data over the link layer. Because in the computer, information is transmitted in the form of 0 1 binary, but the actual link communication is carried out by the voltage level, the light flicker off and the frequency spectrum of the wave, so the role of the physical layer is to convert the binary into the information required by the link transmission for transmission. Data link layer transmissions are also not just a single sequence of zeros and ones; they are usually carried out in frames.
Now we know what the data link layer is about, so only theory can not, you have to have hard currency, that is, hardware, all the theory is inseparable from the support of hardware.
Hardware can be simply understood as communication media, there will be different kinds of information transmission in communication media, but in general can be summarized as two kinds: a shared media network, a non-shared media network, we are going to talk about these two types of communication.
Communication type classification
Shared media network
Shared media network is a network in which multiple devices share the same communication medium. Shared media networks mainly include Ethernet and Fiber Distributed Data Interface (FDDI).
Sharing means that multiple devices will use the same carrier channel to transmit and receive, a half-duplex design.
What is half duplex?
Half duplex means that data can be transmitted in both directions on a channel, but not at the same time. For example, if you can send a message to me, I can send a message to you, but you can send a message to me and I can send a message to you at the same time.
Since multiple devices share the same channel, there may be data contention problems caused by multiple data transfers to the same media. For this reason, shared media network has two media access control methods: contention and token passing.
contention
Contention is a data access mode that occurs on shared media, Carrier Sense Multiple Access (CSMA). In this access mode, each medium in the network will use a first-come, first-served way to occupy the carrier channel to send data. If multiple media send frames at the same time, conflicts are bound to occur, resulting in communication performance degradation and network congestion. Here’s how contention is handled
As shown in the figure above, if A wants to send data to C, media A will send data to C immediately after confirming that there are no other media around to send data to C, that is, after A certain period of time.
After receiving the data from A, each media resolves the MAC address in the PACKET to determine whether the packet is destined for itself. If not, the media discards the data.
The above method is used on some Ethernet, but other Ethernet uses CSMA’s modification, CSMA/CD. CSMA/CD requires each medium to check the link in advance for possible conflicts and release channels as early as possible. Its specific working principle is roughly as follows:
- Listen for data to flow over the carrier channel, and if not, any medium can send data.
- The medium checks for collisions and, in the event of a collision, discards the data and immediately releases the carrier channel.
- After data is abandoned, a period of time passes before the media is recontended.
Here is an improved version of CSMA/CD
The above process is Carrier Sense Multiple Access (CSMA). Firstly, the media will monitor whether there is data on the Carrier channel, if not send again, if there is, wait a period of time to send again.
The following is a schematic of CD(Collision Detection)
- After sending data -> the data is considered to have been sent normally if the voltage remains within the specified range.
- If the voltage exceeds a certain range during transmission, it is considered a data collision.
- When a conflict occurs, a blocking packet is sent, data is discarded, and the packet is sent again after a delay
Token ring
The second transmission mode of shared media network is token ring. Token ring, as the name implies, has a token like thing, and carries out token transmission around the ring. What is token? Think about it. That’s what we’re all about, isn’t it? So this token, can it be used as data?
In fact, in this transmission mode, token ring is transmitted as a special message, which is a way to control transmission. In the process of data transmission, token will be transmitted at the same time, and only the medium that obtains the token can transmit data. This approach has two advantages, namely
- Only the media that holds the token can transmit the data, thus ensuring that there are no packet conflicts.
- Each medium has equal access to tokens, which ensures that performance will not degrade even if the network is congested.
However, there are drawbacks to this token ring approach because only the media holding the token can send data, so utilization is not 100% even if the network is not too congested.
Below is a schematic of token passing
Initially, the token is located at medium A, where medium A has the ability to transmit data, and medium A passes the token to medium B.
B now holds the token, so medium B has the ability to send data.
This data will eventually be received by D, which will then set a flag bit for received data, and the data will continue to be sent down.
Token ring was a successful technology, especially in corporate environments, but has since been superseded by higher versions of Ethernet.
With shared networks behind us, let’s look at a wave of unshared networks
Unshared media network
If shared media network is shared media, then non-shared media network is not shared media, so how to communicate? In this way, each medium in the network is directly connected to a switch that forwards data frames. The sender and receiver do not share communication media, which means that media communicate directly with each other. This network transmission mode generally adopts full duplex communication.
Unshared media networks are suitable for virtual local area networks (vlans), but this communication method has a fatal weakness: if the switch fails, all computers connected to the switch cannot communicate.
The following is a communication diagram of a non-shared media network
As shown in the figure, host A sends A switch whose destination address is B and source address is A, and the switch is responsible for forwarding data to medium B, as shown in the figure below
Unshared network is a full-duplex communication mode in which each medium sends data and receives data from the switch.
Switch hub
Switching hub is a shared network communication medium, which uses coaxial cable as transmission medium. It is usually used in Ethernet. Switching hub is also called Ethernet switch.
Ports on an Ethernet switch forward data based on the MAC address of the media. The Forwarding Table is called a Forwarding Table, which records the MAC address of each media. Of course, we do not need to manually maintain the forwarding table, the exchange machine automatically maintain the forwarding table. The switch learns the MAC address of the media through which each packet passes, as shown in the following figure
Because host B does not know the MAC address of host A, the data sent by host A is broadcast to other hosts on the Ethernet through the switch. After receiving the data, host B sends A message back to host A.
After host B sends A message back to host A, the switch knows the MAC addresses of host A and host B, and the two sides communicate through their connected ports.
Because MAC addresses are not hierarchical, the number of records in the forwarding table is related to the number of all network devices. As the number of devices increases, the number of records in the forwarding table increases, and the search time increases gradually. Therefore, if multiple terminals need to be connected, divide the network into multiple data links and manage IP addresses by layer, similar to IP addresses.
In the process of network communication, the redundancy of network links or routing lines may lead to a closed loop, which is called a loop. Loop causes data packets to be repeatedly replicated on the network, and the network device cannot run properly due to heavy load. The impact may spread to the entire LAN, causing computers in the entire LAN to fail to use the network.
So how do you detect loops in the network?
Loop detection method
At present, there are two ways to detect loops, one is spanning tree, the other is source routing.
Spanning tree: Spanning tree means that each bridge must exchange spanning tree protocol unit (STP) packets with each other within 1-10 seconds to determine which interfaces are in use, thereby eliminating loops and immediately switching lines to unused ports in case of failure.
Source routing: Source routing is usually used to solve token loops. In this way, the source address of the data sent can be determined through which bridge to realize transmission, and the frame is written into the RIF, the bridge will send the RIF information to the target address, even if there is a loop in the bridge, the data frame will not be repeatedly forwarded.
Virtual LAN VLAN
In the process of network communication, the network load is too high and the communication performance deteriorates. In this case, the network load needs to be dispersed and the location of deployed network devices needs to be changed. Before the emergence of virtual LAN, administrators often need to manually change the topology of the network, such as changing the host network segment, hardware line transformation, but the use of virtual LAN, you can no longer do such a complex operation, only need to modify the network structure.
So what exactly is a virtual LAN?
As shown in the preceding figure, the switch divides multiple network segments by port to differentiate the transmission range of broadcast data and improve network security. However, the communication between two heterogeneous network segments can be realized only by using switches with routing function.
A switch port has two VLAN attributes, NAMELY, VLANID and VLAN Tag. Set the VLAN tag and allow the packets to pass through the VLAN respectively. Ports with different VLAN ids can allow each other to transmit VLAN tags. Build a VLAN.
Ethernet
Ethernet has been mentioned so much, so what is Ethernet?
There are many types of data link layer, including Ethernet, wireless communication, PPP, ATM, POS, FDDI, Token Ring, HDMI, etc., among which Ethernet is the most famous communication link.
At the beginning of Ethernet, it usually uses shared media connection with coaxial cable as transmission medium. This is also the first mode of Ethernet, which is called classic Ethernet. Now, with the increase in processing power and transmission speed of interconnected devices, terminals and switches are now connected, and this is a second method, called switched Ethernet. Ethernet uses the CSMA/CD bus technology, which we described earlier.
Ethernet frame format
A packet on an Ethernet link is called an Ethernet frame, and an Ethernet frame begins with a section called a Preamble, which consists of alternating zeros and ones. At the end of the leading code is a field called SFD(Start Frame Delimiter) with a value of 11. The lead code and SFD together occupy 8 bytes.
The last two bits of Ethernet are called SDF, and the last eight bits of IEEE802.3 are called SDF.
IEEE802.3 is a collection of standards developed by the Institute of Electrical and Electronics Engineers (IEEE).
This is the leading part of the Ethernet frame, and here is the body part of the Ethernet frame
There are two types of Ethernet frame frame format, one is Ethernet frame format, one is IEEE802.3 standard Ethernet frame format.
In the Ethernet frame format, the front end of the Ethernet frame is the Ethernet header, occupying a total of 14 bytes, including 6 bytes of target MAC address, 6 bytes of source MAC address and 2 bytes of upper layer protocol type, followed by the data part, occupying 46-1500 bytes. Finally, FCS(Frame Check Sequence) 4 bytes. FCS is used to check whether the frame is damaged, because the data may appear garbled bits due to noise interference in the communication process.
The format of IEEE802.3 Ethernet frames is different. Generally, the type field of Ethernet frames indicates the frame length in IEEE802.3. In addition, the LLC and SNAP fields are added.
The data link layer can be divided into two layers, media access control layer and logical link control layer
Media access is controlled based on the header information unique to different links such as Ethernet, while the logical link layer is controlled based on the frame header information common to different links such as Ethernet.
LLC and SNAP are the first messages of logical link control, so now you know what’s going on.
I have uploaded six PDFS by myself. After searching the public account of “Programmer Cxuan” on wechat, I reply to CXuan on the background and get all PDFS. These PDFS are as follows
Six PDF links