This article is participating in the “Network protocol must know must know” essay contest.
The Web is a virtual thing that is largely invisible:
- It includes the application layer, transport layer, network layer, link layer, and physical layer from top to bottom.
- This article will cover the physical layer, link layer, and network layer in reverse order.
The physical layer
Is the network cable, is the optical fiber, is the signal transmission medium, is the physical medium.
- Computers exchange information through circuit signals
- Network cables or optical fibers generate 01 signals that computers can recognize
The link layer
In the physical, we know that
- Data exists in the communication medium as a stream
- The network cable is duplex. A cable can be sent from A to B, and B can be sent from A to A. If A keeps sending the cable, B cannot work.
We need to carve out some specifications to make this simple network work.
Data streams and frames
The first is packet splitting, which allows streams in one direction not to occupy the link forever.
Send by stream
The process described here looks like this:
- A first sending
- B intends to send
- B detects a collision and waits
- A has so much content to send that it never stops
- B waits for a long time until the link is not idle
Link frame transmission
So, the link layer does something like this:
-
A The data to be sent by the NIC is split into A frame according to the MTU
-
A sends A frame first
-
B wants to send, B detects a collision and waits
-
After A sends A frame, the link is idle
-
B can send
-
A continues to send the remaining frames after detecting idle
In this way, frames at the link layer solve the problem of link exclusivity.
Ethernet is the most widely used link-layer protocol.
Link layer basic elements
Thus, the basic elements of the link layer are shown as follows:
-
Suppose A transmits A piece of data to B: 0111001010101
-
This data is divided into frames
-
The Link layer of the Ethernet protocol, the most widely used link layer protocol, specifies a frame (MTU) size of 1500 bytes
-
If the number of data transmitted at a time exceeds the MTU, the link layer needs to split the data and then send the data to the physical layer
-
Common network adapters need to implement Ethernet protocols, including MTU size Settings
The link layer also standardizes MAC addresses, and each nic has a unique MAC address flag.
Link layer addressing tool – switch
The general host network card port is less, can be directly connected to the machine is very few, so the introduction of the switch.
The switch itself does not have a MAC address, but exists to facilitate frame forwarding.
As shown above:
- The nic also has a unique MAC address identifier, which is 48 bits long
- The switch has a large number of ports. Dozens of ports are common.
- The switch can cache and automatically record the relationship between ports and corresponding HOST MAC addresses
Frame format of the link layer
The link layer defines the MAC address specification, and you can see the frame format as follows:
- Playload is the content of the data to be sent, in this case 0111001010101, which is forwarded to the destination via DST MAC
- The frame size does not exceed the MTU value
The link layer constitutes the network worldtribal
Switches can also form a larger network through the hierarchical structure as shown in the following figure:
User A sends A packet to user C. If the currently connected interactive machine does not record the MAC address, the packet is forwarded to all other interactive machines.
A host’s MAC address is the equivalent of an unmarked ID card, traversed through the network by a connected switch.
Therefore, a subnet connected to a switch generally does not contain too many machines, equivalent to a small tribe.
The IP layer
It is difficult to form a large network world through the link layer, so protocol makers are starting to think of new solutions.
If you want to communicate among many, many small tribes, routing becomes very difficult, hence the IP layer.
Network extension
Tribe – village
The network world of switches is one by oneSmall tribes, our human development is not the best reference for the development of the network, as shown in the following figure:
- Multiple tribes (networks of switches) make up larger villages
- Villages can be identified by road signs
- These four tribes are represented by two binary digits, 00, 01, 10, and 11
Village – Larger area
The Web continues to evolve:
- The region here can also be represented by two binary digits for each contained village
- Continue to expand in this way, and eventually form a worldwide network.
IP Hierarchy
The web is big enough now, but the reality is that the web is denser and more complex than that.
Address and administrative level
The world is also big, and we need to find a person’s address, which can be expressed as an administrative level:
From bottom to top: group – village – township – town – county – district – city – province – country – planet
The IP address
Unlike in the real world, we use IP addresses to represent Internet addresses. IP has 32 bits, meaning there are up to 400 million unique IP addresses in the world.
The top administrative address is divided into a total of 10 layers, IP address although not so image, but also natural hierarchy.
- Each digit of the IP is meaningful, with some digit representing village-county-city
- A MAC address is like an ID card, which has no address
- So the IP address gives you a sense of where to go to find the corresponding host
ipv4
If an 8-bit IP address group is divided into four groups, it is called ipv4
The above address is an ipv4 address, 01101111.01111101.11111011.11110100 111.125.251.244.
The IP allocation
- Class A address: comes before the IP address8Bits represent the network ID, and the last 24 bits represent the host ID, with some exceptionsSpecial markThere are only about 200 such networks, most of which are allocated to COMPANIES in the United States. Many of the host addresses in the network are not used, causing a lot of waste
- Addresses starting with 127 in decimal notation: loopback addresses, such as ping itself 127.0.0.1,
- 127.0.0.1: This is the same address we use to call ourselves’ I ‘.
- 0.0.0.0 indicates the network itself
- Class B address: The first 16 bits of an IP address indicate the network ID and the last 16 bits indicate the host ID
-
- 192.168.1.1 indicates a private network
- Class C address: comes before the IP address24Bits indicate the network ID, and the last eight bits indicate the host ID. The number of available host addresses in the network is 256-2.
- All 0 host ID: indicates the network. For example, IP address 129.152.0.0 indicates a class B network whose NETWORK ID is 129.152
- All 1 Host ID: indicates the broadcast address
Private network
There are far more than 400 million computers in the world right now, and there are not enough IP addresses to go around. Add in the waste caused by all the compartmentalization mentioned above, and you’re running out of IP. So most of the time, for example in our country, many communities, companies and schools use private network solutions.
-
Your home network, your business network, and your OWN IP are all likely private Intranet addresses.
-
Bought a server in the cloud, probably with a public IP address.
As shown below:
Private Network Restrictions
case1
Because of the limitation of private network, the host in the Internet often needs to process the packet one more layer (compared with the public network, there is also one more layer conversion). As shown below, C1A opens the QQ chat program and chats with the family outside the school.
- When sending a packet, the egress router on the Intranet may split the packet into packet 2 (with its own public IP address and MAC address).
- When receiving a packet, the egress router on the Intranet converts packet 2 into packet 1 and forwards the packet to the host on the Intranet.
case2
Sometimes you need to access private addresses, just like accessing a server.
For P2P applications, everyone is a node on the network.
Because we are on a LAN, we need to use some network inversion techniques.
ipv6
Because IP is in a hurry, ipv6 specifications were developed later.
There are 128 bits in total, divided into 8 groups of 16 bits.
But it’s not very popular right now.
routing
The above is about the division of IP level, we can figure out that probably through this way to find another host in the network world.
In terms of implementation, the machines that implement the IP layer protocol maintain a routing table, which represents the network world that they can perceive.
You can run the route command to view the routing table on the host or router.
# routeKernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 9.134.64.1 0.0.0.0ug 0 0 0 eth1 9.0.0.0 9.134.64.1 255.0.0.0 UG 0 0 0 eth1 9.134.64.0 0.0.0.0 255.255.240.0 U 0 0 0 eth1 10.0.0.0 9.134.64.1 255.0.0.0 UG 00 0 eth1 100.64.0.0 9.134.64.1 255.192.0.0 UG 00 0 eth1 172.16.0.0 9.134.64.1 255.240.0.0 UG 00 0 eth1 192.168.0.0 9.134.64.1 255.255.0.0 UG 0 0 0 eth1 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0Copy the code
The meanings of the fields in the output result are as follows:
- Flags indicates the Flags of routes. The flags available and their meanings are: U indicates that the route is started, H indicates that the target is a host, and G indicates that the gateway is used
- Gateway Indicates the host name or IP address of the Gateway
- A subnet mask is a 32-bit address used to mask part of the IP address to distinguish the network id from the host IDIs the IP address on a LAN or a wan.
- Bit-by-bit “&” operations are performed on the Genmask and IP address of the packet, and if the packet is the same as Destination, it is forwarded to that Destination’s network
- Destination indicates the routeTarget networkWe can see that we have these in the example hereThe network can reach
- 9.0.0.0:1001000000000000000000000000 is A class A network, can contain A lot of (2 ^ 24) of the host address, through the gateway 9.134.64.1 can reach
- 9.134.64.0:1001100001100100000000000000 is a class B network, the current machine IP is located in the network
- 10.0.0.0 is also A class A address
- 100.64.0.0 is also a class B address and is accessible to the network
- 192.168.0.0 indicates the current LAN
- 192.168.10.0 indicates the docker network
- If other rules are not met, the default route is used and forwarded to the default public network.
- 0.0.0.0 indicates the public network
- Route selection is based on the longest match, so default is processed last, and mask 255.255.255.0 is processed first
conclusion
This paper describes how computers in the world are connected by describing the physical layer, link layer and network IP layer from bottom to top.
Step by step, layer by layer, the network moved from its primitive form to its modern form.