preface

The front has written several technical articles, every day liver to two or three in the morning, in the roommate look I became immortal, sleep late, but also get up early thief. Without further ado, unknowingly came to the data link layer, which is the bottom of the computer network model, learn this part of the knowledge of our later learning has a decisive role.

Here’s the structure of this article:

Ethernet frame


The data link layer receives IP datagrams from the network layer and encapsulates them so that IP datagrams can be transmitted on the data link layer. Like this, loaded IP datagrams are called Ethernet frames, or MAC frames. A MAC frame consists of the following important parts:

  • Destination MAC Address: The destination address of a MAC frame takes up 6 bytes and identifies the address of the target host.

  • Source MAC address: The same as the destination MAC address, the source MAC address is six bytes long and identifies the address of the source host.

  • Type: The type occupies two bytes and records the protocol 0X0800 used by the upper layer, indicating the IP protocol.

  • Data part: The data part is naturally the IP datagram from the upper layer.

  • FCS: FCS is 4 bytes long and is used for error detection. If a MAC frame has an error, it cannot be sent to the destination host.

Error detection


Why error detection?

Realistic communication links are not ideal. That is, a bit can go wrong in its transmission: a 1 can become a 0, and a 0 can become a 1. This is called a bit error. Over a period of time, the ratio of the transmitted Error bits to the total number of transmitted bits is called the Bit Error Rate (BER). Bit error rate (BER) is closely related to signal-to-noise ratio (SNR). It is impossible to reduce ber to zero in practical communication. Therefore, in order to ensure the reliability of data transmission, various error detection measures must be adopted in computer network data transmission.

It is inevitable that MAC frames will produce errors in the process of propagation. In the Ethernet frame section, we mentioned the error detection sequence FCS. According to FCS, we can know whether the MAC frame is wrong or lost during transmission.

We’ll talk about error detection later when we talk about the transport layer, so what’s the difference? To sum up, it can be summed up in one sentence:

  • The purpose of data link layer error detection is to achieve “incomparable error”.
  • The purpose of error detection at the transport layer is to achieve “transmission error free”. That is to compensate for frame loss, frame repetition, frame out of order.

There are two main error detection methods: parity check (PCC) and cyclic redundancy check (CRC). PCC is very simple and is not the focus of this article. The following is mainly about CRC cyclic redundancy check.

Cyclic redundancy check is a method that generates a fixed digit check code based on transmitted or saved data. It is mainly used to detect or verify errors that may occur after data is transmitted or saved. The resulting number is calculated and appended to the data before transmission or storage, and then checked by the receiver to see if the data has changed.

With CRC, we can calculate the FCS redundancy check code, which is at the end of the MAC frame. With FCS, we can tell if the MAC frame sent an error.

The adapter


When it comes to adapters, you can think of adapters in your life. For example, when we charge mobile phones, we need a power adapter. The power adapter is nothing more than a conversion function, or as a carrier, to achieve the transfer of energy. In fact, the same goes for adapters in computers. Consider this picture:

As we all know, data is transferred in serial mode through external media, while computers process internal instructions in parallel. How do you convert serial data into parallel data? This requires an adapter. The adapter acts as a bridge through which you can easily change the way data is transmitted.

CAM table


We all know about switches, which are multi-port Bridges that use MAC addresses to forward data at the data link layer. The switch class does not actually store a table, called a CAM table. This table records the MAC addresses of hosts and their corresponding interfaces. Take a look at the following graph:

There are three hosts A, B, and C connected to the switch. Initially, no information is stored in the CAM.

One day, host A (source MAC) wants to send A message to host B (destination MAC). At this time, the switch will check whether its CAM table stores the information of host A. Once it sees that there is no information of HOST A, the switch will write the information of HOST A into its CAM table. Now, the switch’s CAM table looks like this:

At this point, the CAM table of the switch already stores host A’s information, but host A wants to send A message to host B. What can I do about it? First, the exchange machine checks whether there is information about B in its CAM table, and if there is, it will directly forward the information to B. If it doesn’t exist, what then? After some hesitation, the switch had another idea. It broadcast the message from host A to host B to all the hosts connected to it. Host C also received the message, but host C checked the destination address and discarded the message. Host B receives the message, also checks the recipient (destination address), finds that the message is for itself, and accepts the message. The switch then updates its CAM table with a message:

In this way, the CAM table stores information about host A and host B. The next time host A wants to send A message to host B, the switch does not need to broadcast.

CSMA/CD protocol


The previous article talked about the computer network structure, here to tell the CSMA/CD protocol needs to know the bus structure of the computer network, do not know the friends can go to see: into the computer network: physical layer

The use of CSMA/CD has been fairly minimal so far, and its use is based on two premises:

  • It’s wired
  • Applied in 10M/100M half duplex wired network

Networks using CSMA/CD have the following three characteristics:

  • The network is a bus structure, all computers are connected to the same bus, at the same time, only one computer is allowed to send (or receive) messages, that is, use half duplex communication.
  • Carrier sense: the channel must be continuously monitored before and during transmission. Messages can be sent only when the channel is idle.
  • Collision detection: The host continuously checks the channel before sending a message. If two hosts send messages at the same time, the message transmission stops immediately. The backout algorithm is to wait a random period of time before sending a message.

To add the characteristics of the retreat algorithm:

  • Non-persistent CSMA: the line is busy, wait for a period of time, and then monitor; When not busy, send immediately; Conflicts are reduced and channel utilization is reduced
  • 1 adhere to the CSMA: line busy, continue to listen; When not busy, send immediately; Channel utilization rate increases and conflict increases.
  • P adhere to the CSMA: line busy, continue to listen; When not busy, send according to probability p, the other 1-p probability is to continue listening (p is a specified probability value).