Speed meter network learning software development: data link layer

Preface, I also write data link layer failure, to learn all depends on this, Han Ligang teacher's class, absolutely interesting , and this is my reference Mu class network: network programming necessary foundation in the articles, all software, not very full, but I think for the software out enough . Please indicate wrong

1. Packaging Framing

"Frame" is the basic unit of the data link layer.

After a period of data transmission end network layer (data packets) to the data link layer, before and after the addition of specific datagrams labeled form a "frame", also referred to as "data frames."

Receiving end of the data link layer a "frame" According to a particular identification tag front.

Here Insert Picture Description

  • The frame header and the frame tail is specific control characters (bit stream)

2. Transparent transmission

Because the data is only the frame data to the receiving side will mark the first end of the frame removed, the frame data if there is such a specific bit stream to how to solve this presents a transparent transmission.

Here Insert Picture Description

"Transparent" in the computer often said: transparent to the user, referring to the (possible code or other functions) users do not need to know the specific implementation, users only need to use on the line. There are a variety of argument, it can be said package; for the network, the work of the physical layer is transparent to the data link layer, only needs to provide an interface to the data link layer on it. Is a thing actually exists, but do not appear to exist.

Not to be confused with the real world "transparent" transparent real-world meaning the opposite.

Transparent transmission: no matter what the message (frame data) can be transferred (ie control characters in the frame data), because the data link layer will solve such a problem in the data frame of EOT. So we just pass the message, do not care about the message that it contains EOT problem. This is the message there for us to deal with the problem EOT is transparent.

The data link layer can talk about exactly how to deal with (to understand): That includes front EOT of adding escape characters in the message.
Here Insert Picture Description
ESC: Escape character. The recipient is identified to EOT, first determine whether there is in front of EOT ESC, if any, then the frame is not the tail.

这跟我们编程中的“\”一样,比如要打印“\n”,而“\n”是换行符(规定的控制字符),那么可能需要转义:“\\n”,才打印“\n”。

3. 差错检测

物理层只管传输比特流,无法控制是否出错。所以数据链路层负责“差错检测”的工作。

差错检测有两种:奇偶校验码和循环冗余校验码CRC。

3.1 奇偶校验码

在比特流的尾部添加一位比特位来检测该比特流是否出错。

Here Insert Picture Description

但是,假设出现如下图的情况,那么在检测的时候就会成功,这就是该方法的缺陷:即在传输的过程可能会出现顺序错误但是计算出的结果还是跟携带的比特位一样,或者丢失了某些数据,最终形成的数据中计算出的结果也跟携带的比特位一样。

Here Insert Picture Description

所以就有循环冗余校验码CRC。

3.2 循环冗余校验码CRC

一种根据传输或保存的数据而产生固定位数校验码的方法。

学习它的前提知识需要知道模“2”除法:即二进制下的除法。与算术除法类似,但是该除法不借位,实际是 异或操作

异或有四条公式,用xor表示异或操作:

  • 0 xor 0 = 0
  • 0 xor 1 = 1
  • 1 xor 1 = 1
  • 1 xor 1 = 1

即只需要记住常说的一句话:异或操作,相同为0,不同为1

Here Insert Picture Description

循环冗余校验码CRC的生成主要有三个步骤:(看不懂没事,了解先,然后直接看例子)

  1. 选定一个用于校验的多项式G(x),多项式一般题目会给,并在数据尾部添加r个0。
  2. 将添加r个0后的数据,使用模“2”除法除以多项式的位串。
  3. 得到的余数填充在原数据r个0的位置得到可校验的位串。

例子1:对于生成多项式G(x)=x^4+x^3+x^2+1,计算报文110的可校验位串。(例子来自360百科)

1.根据用于校验的多项式G(x)来计算,并在数据尾部添加r个0:

Here Insert Picture Description

可以知道r就是最高阶数。

  1. 将添加r个0后的数据,使用模“2”除法除以多项式的位串。

Here Insert Picture Description

  1. 得到的余数填充在原数据r个0的位置得到可校验的位串。

Here Insert Picture Description

最终将该可校验的位串发送到接收方,接收方在数据链路层会根据多项式(双方协议所规定好的,我们不用理,了解)计算出位串,然后将发送来的可校验的位串除以多项式的位串,然后根据余数判断出错:

  • 假设余数为0,那么说明报文无错。
  • 假设余数不为0,那么说明报文有错。

Here Insert Picture Description

CRC的错误检测能力与位串的阶数r有关,即添加越多的0,那么检测能力越强。当r=1时,会退化为奇偶校验。

多项式不是随便自己想的,实际上多项式是双方约定的,即有协议来规定。根据下面的图:了解就行。

Here Insert Picture Description

4. MTU

MTU:最大传输单元MTU(Maximum Transmission Unit)

数据链路层的数据帧也不是无限大的,因为数据帧过大或过小都会影响传输的效率。以太网MUT一般为1500字节。

过小影响效率的解释:比如数据帧有1500字节都拆成1个字节分别发送,那么就要发送1500次。虽然发送时延减少了,但是需要重复1500次,那么就会影响传输效率。

路径MTU:

Here Insert Picture Description

MTU在网络层会涉及到。

问题:在路径MTU中如果当前发送帧为1500字节,可是下一个小型网络只能接受1000字节,会如何处理?

回答:路径MTU指的是发送方到接收方完整路径的最小MTU,这个MTU是可以动态变化的,如果确实发生上面的情况,则可以认为是数据链路层异常,数据会被直接丢弃掉。

5. 以太网协议

5.1 MAC地址

即物理地址、硬件地址。每个设备都拥有一个唯一的MAC地址。像身份证一样。MAC地址共48位(bit),使用十六进制表示。计算机的MAC地址由网卡决定,是不可改变的。集线器、路由器也有MAC地址。

在CMD中输入:ipconfig/all 就可以看到本机所有网络物理设备的MAC地址。

5.2 以太网协议

以太网(Ethernet)是一种使用广泛的局域网技术。是数据链路层中的协议。使用以太网可以完成相邻设备的数据帧传输。

Here Insert Picture Description

传输过程:

Here Insert Picture Description

如果是未知的情况:
Here Insert Picture Description

因为以太网协议只能解决相邻设备的数据传输,下图是需要跨节点传输,这需要网络层的知识来解决:

Here Insert Picture Description

6. 总结

  • Three important functions of the data link layer: a package framing, transparent transmission, error detection.
  • The data link layer header and trailer is added on the frame identifier "frame" is transmitted to the basic unit of data, will the network layer IP datagrams. The data is also transmitted to the physical layer frame header removed frame identifier and a tail portion. (Remember layer data transmission network of the sender is from top to bottom, and the opposite receiver).
  • The data link layer adds a MAC address in the IP datagram.
  • Causes and transparent transmission concept.
  • The data link layer only detection data is not correct . Data link layer data for errors directly discarded. There are two detection methods: Parity and cyclic redundancy check CRC. We know the calculation of CRC.
  • MTU and Ethernet protocols.
Published 65 original articles · won praise 51 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_41800884/article/details/104446759