Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

If you were to design the network,

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > IT Information >

Share

Shulou(Shulou.com)11/24 Report--

This article comes from the official account of Wechat: low concurrency programming (ID:dibingfa). Author: flash.

You are a computer. Your name is A.

A long time ago, you were alone without being connected to any other computer.

Until one day, you want to establish communication with another computer B, so you each open a network port and connect with a network cable.

How can you "communicate" by connecting with a network cable? I can tell you about IO, interrupts, and buffers, but this is not a concern when studying the network.

If you struggle, either study how the operating system handles the network IO, or study how the packet is converted by the network card into an electrical signal, or just think of it as if there is a villain shooting in the computer.

Anyway, you are connected and can communicate.

One day on the first floor, a new partner C joined, but smart you soon found that each person can open two network ports, with a total of three network cables, connected to each other.

As more and more people join in, you find that there are too many net ports on your body, and the network lines are dense and chaotic. (in fact, a computer can't open so many Internet ports at all, so this kind of connection is only feasible in theory, so if I can't connect it, I use a red dotted line to express it. It's so rigorous. Haha)

So you invented an intermediate device, and you plugged all the network cables into this device, which was forwarded by this device, so that you could communicate with each other, essentially the same as before, except that the number of network ports and the number of network cables were reduced, and it was no longer so chaotic.

You call it a hub, it just doesn't have a brain to forward electrical signals to all exits (broadcasts) without any processing, you think it has no IQ, so it defines people in the physical layer.

Since it is forwarded to all the exits, how do the four BCDE machines know if the packet is sent to themselves?

First of all, you need to give a name to all the devices connected to the hub. Originally your name was ABCD, but now you need a more professional, globally unique name as a logo, and you call this higher-end name the MAC address.

Your MAC address is aa-aa-aa-aa-aa-aa, your partner b's MAC address is bb-bb-bb-bb-bb-bb, and so on, just don't repeat it.

In this way, when A sends a packet to B, all it has to do is to splice a piece of data with such a structure in the header.

B after receiving the data packet, according to the destination MAC address information of the header, determine that the data packet is indeed sent to himself, so he receives it.

After the other CDE receives the packet, according to the destination MAC address information of the header, it determines that the packet is not sent to itself, so it is discarded.

Although the hub makes the whole layout a lot cleaner, the message I sent to computer B is now sent to all the computers connected to the hub, which is neither secure nor save network resources.

It would be nice if the second layer made the hub smarter and sent it only to the computer that the target MAC address pointed to.

Although there is only a little more difference than a hub, it seems to be smart. You call this thing a switch. And because of this little bit of intelligence, you put it at another level, the data link layer.

As shown in the picture above, this is how you designed it.

A MAC address table is maintained inside the switch, recording the port on which each device with an MAC address is connected.

MAC address Port bb-bb-bb-bb-bb-bb1cc-cc-cc-cc-cc-cc3

Aa-aa-aa-aa-aa-aa4

Dd-dd-dd-dd-dd-dd5 if you still want to send a packet to B, construct the following data structure out of the network port.

When arriving at the switch, through the MAC address table maintained by the switch itself, it was found that the MAC address bb-bb-bb-bb-bb-bb of the target machine B was mapped to port 1, so the data was sent to B from port 1.

You give this small area of network that is transmitted in this way, called Ethernet.

Of course, at the beginning, the MAC address table was empty, how was it gradually set up?

If the MAC address table is empty, you send the following data to B

Because the packet enters the switch from port 4, the switch can record the first piece of data in the MAC address table:

MAC:aa-aa-aa-aa-aa-aa-aa

Port: 4

The switch sees that the destination MAC address (bb-bb-bb-bb-bb-bb) is not mapped in the address table, so it sends the packet to all ports, that is, to all machines.

After that, only machine B received the packet that was indeed sent to itself, so it responded and the response data entered the switch from port 1, so the switch updated the second data in the address table:

MAC:bb-bb-bb-bb-bb-bb

Port: 1

The process is as follows

After the continuous communication between the machines in the network, the switch finally sets up the MAC address table.

With the increase in the number of machines, there are not enough ports on the switch, but you are smart enough to find that as long as you connect multiple switches, this problem can be easily solved.

You don't need to design anything extra at all, you just need to follow the previous design and rules and follow the above wiring to complete the interconnection of all computers, so the rules of switch design are really ingenious. Think about why (for example, A sends data to F).

But you should note that the red line above is not a record in the MAC address table, but all the mappings of the four EFGH machines to the port (port 6) are recorded in the table.

Finally, the two switches will record the mapping records of all A ~ H machines respectively.

The switch on the left

MAC address Port bb-bb-bb-bb-bb-bb1cc-cc-cc-cc-cc-cc3

Aa-aa-aa-aa-aa-aa4

Dd-dd-dd-dd-dd-dd5

Ee-ee-ee-ee-ee-ee

six

Ff-ff-ff-ff-ff-ff

6gg-gg-gg-gg-gg-gg

six

Hh-hh-hh-hh-hh-hh

6 the switch on the right

MAC address Port bb-bb-bb-bb-bb-bb1cc-cc-cc-cc-cc-cc1

Aa-aa-aa-aa-aa-aa1

Dd-dd-dd-dd-dd-dd1

Ee-ee-ee-ee-ee-ee

two

Ff-ff-ff-ff-ff-ff

3gg-gg-gg-gg-gg-gg

four

Hh-hh-hh-hh-hh-hh

6 this is fine when there are only eight computers, even when there are only a few hundred computers, so the design of this kind of switch has been supported for a while.

But it is a pity that people are greedy animals. Soon, the number of computers will grow to thousands, tens of thousands, hundreds of thousands.

Layer 3 switches can no longer record such a large mapping relationship.

At this point, you use your brain, and you find that the root of the problem is that I don't know how many devices are constantly connected to the red network cable, which makes the address table bigger and bigger.

Can I connect that red network cable to a new device, which has its own independent MAC address just like a computer, and can also forward the packet for me at the same time?

This device is a router, its function is, as a separate device with an MAC address, and can help me to forward the packet, you set it at the network layer.

Note that each port of the router has a separate MAC address

Well, now in the MAC address table of the switch, you only need to add a mapping relationship between the MAC address ABAB and its port, and you can successfully transfer the packet to the router.

So how to do that, send it to C and D, or even to DEFGH....? All the packets are sent to the router first?

It is not difficult to think of the idea that if the MAC addresses of computers C and D have a common prefix, for example,

MAC address of C: FFFF-FFFF-CCCC

MAC address of D: FFFF-FFFF-DDDD

Then we can say that the target MAC address is FFFF-FFFF-?. At the beginning, all are sent to the router first.

Is this feasible? The answer is no.

Let's start with the structure of MAC addresses in reality. MAC addresses are also called physical addresses and hardware addresses. They are 48 bits long and are generally expressed in this way.

00-16-EA-AE-3C-40

It is an EPROM (a flash memory chip that can usually be erased and written by a program) that is burned on a network card when produced by network equipment manufacturers. The first 24 digits (00-16-EA) represent the number of the network hardware manufacturer, and the last 24 digits (AE-3C-40) are assigned by the manufacturer itself, generally indicating the serial number. As long as you don't change your MAC address, MAC address is unique in the world. Figuratively speaking, the MAC address is like the ID card number on the ID card, which is unique.

So if you want to indicate as above that the target MAC address is FFFF-FFFF-? At the beginning, if you uniformly send it to a group of devices from the router (which is actually the concept of a subnet), then you need to require all the devices under a subnet to buy a manufacturer's equipment, or you need to require the manufacturer to determine the MAC address in advance according to your planned subnet structure when the production network equipment burns the MAC address, and the network structure cannot be easily changed in the future.

This is obviously unrealistic.

So you invent a new address and give each machine a 32-bit number, such as:

11000000101010000000000000000001

You feel a little unclear, so divide it into four parts, connected by dots in the middle.

11000000.10101000.00000000.00000001

You still think it's not clear, so convert it to decimal.

192.168.0.1

Finally, you gave the address a loud name, the IP address. Now every computer has its own MAC address and its own IP address, but the IP address is on the software level and can be modified at any time, and the MAC address generally cannot be modified.

Such an IP address that can be modified at any time can be adjusted according to your planned network topology.

As shown in the figure above, if I want to send a packet to one of the ABCD devices, I can describe it like this, "send all the packets starting with the IP address of 192.168.0 to the router, and then how to forward it, give it to it!" Oh, clever.

After giving it to the router, how does the router accurately forward the packet to the designated device?

Don't worry. Let's take our time.

Let's first add our own IP address to each device in the above networking mode.

Now for the transmission between the two devices, in addition to adding the head of the data link layer, there is also a head of the network layer.

If A sends data to B, because they are directly connected to the switch, A can directly send out the following packets, in fact, the network layer does not play a role.

But if A sends data to C, A needs to hand it over to the router, and then the router transfers it to C. Because the lowest transmission still depends on Ethernet, the packet is divided into two segments.

The packets in this section of A ~ router are as follows:

The packet from router to C is as follows:

Well, I believe that careful readers will have a lot of questions about the two situations mentioned above (A-> BMaga A-> C). Let's expand them one by one.

When A sends a packet to C, how do you know if it should be forwarded through the router?

Answer: subnet

If the source IP is on the same subnet as the destination IP, the packet is sent directly through the switch.

If the source IP is not on the same subnet as the destination IP, leave it to the router to handle.

Okay, now we just need to figure out what it means to be in a subnet.

192.168.0.1 and 192.168.0.2 are on the same subnet

192.168.0.1 and 192.168.1.1 are in different subnets

These two are artificially prescribed by us, that is, we want to say that for 192.168.0.1:

What starts with 192.168.0.xxx is in one subnet, otherwise it is in a different subnet.

So for the computer, how to express this meaning? So people invented the concept of subnet mask.

If the subnet mask of a machine is 255.255.255.0

This means that the source IP and destination IP are respectively summed with this subnet mask, and the equality is on the same subnet, and the difference is on different subnets. it's as simple as that.

such as

A computer: 192.168.0.1 & 255.255.255.0 = 192.168.0.0

Computer B: 192.168.0.2 & 255.255.255.0 = 192.168.0.0

C computer: 192.168.1.1 & 255.255.255.0 = 192.168.1.0

D computer: 192.168.1.2 & 255.255.255.0 = 192.168.1.0

So An and B are on the same subnet, C and D are on the same subnet, but An and C are not on the same subnet, and D is not on the same subnet, and so on.

So if A sends a message to C and the IP address of An and C respectively & the subnet mask configured by A machine, and finds that it is not equal, then A thinks that C and he are not on the same subnet, so he sends the packet to the router and does not care how to forward it.

A how do you know which device is the router?

Answer: set the default gateway on A

The previous step A determines whether it should send the packet to the router by whether it is in the same subnet as C. what is the IP of the router?

In fact, it is not accurate to send it to the router, it should be said that A will send the packet to the default gateway.

For A, A can only send the packet directly to an IP under the same subnet, so sending it to the router or sending it to a computer doesn't matter to A, as long as the device has an IP address.

So the default gateway is an IP address that A configures in his computer so that when it is sent to machines with different subnets, it will be sent to this IP address.

That's all!

How does the router know where C is?

Answer: routing tabl

Now that A wants to send a packet to C, it can be successfully sent to the router. The last question is, how does the router know from which port the packet it receives should go out? to reach destination C directly (or indirectly).

The packet received by the router has a destination IP, that is, the IP address of C, and needs to be converted to which port it is going out from. It is easy to think that there should be a table, just like the MAC address table.

This table is called the routing table.

As for how this routing table comes out, there are many routing algorithms, which will not be expanded in this article, because I will not ~

Unlike the MAC address table, the routing table is not a clear one-to-one relationship. Let's take a look at the structure of a routing table.

Destination address subnet mask next hop port 192.168.0.0255.255.255.0

0192.168.0.254255.255.255.255

0192.168.1.0255.255.255.0

1192.168.1.254255.255.255.255

1 We learn a new representation. Because the subnet mask actually represents the first number of bits representing the network segment of the subnet, for example, 192.168.0.0 (255.255.255.0) can also be abbreviated to 192.168.0.0max 24.

Destination address next hop port 192.168.0.0Universe 24

0192.168.0.254/32

0192.168.1.0/24

1192.168.1.254/32

1 this is easy to understand. The routing table says that everything under the subnet of 192.168.0.xxx is forwarded to port 0, and everything under the subnet of 192.168.1.xxx is forwarded to port 1. The next hop column is not worth it yet, so let's ignore it.

In conjunction with the structure diagram (here the subnet mask and default gateway are all made up) in the diagram & clerical error, the result should be .0

What I just said is the IP layer, but the data link layer that sends the packet needs to know the MAC address, but what should I do if I only know the IP address?

Answer: arp

If you (A) don't know your partner B's MAC address at this time (in reality, we just assumed we knew it), and you only know its IP address, how can you send the packet to B exactly?

The answer is simple. At the network layer, I need to find the MAC address corresponding to the IP address, that is, in some way, to find the MAC address BBBB corresponding to 192.168.0.2.

This is the arp protocol, and computers An and B will also have an arp cache table that records the corresponding relationship between IP and MAC addresses.

IP address MAC address 192.168.0.2BBBB this table is empty at first. In order to know the MAC address of computer B (192.168.0.2), computer A will broadcast an arp request. After receiving the request, B will give An a response with its own MAC address. At this point, A updates its arp table.

In this way, through the continuous broadcast of arp requests, eventually all computers will update the complete arp cache table.

To sum up, there are only a few rules so far.

From the perspective of each node

Computer perspective:

First of all, I need to know my IP and the other person's IP.

Judge whether we are on the same subnet by the subnet mask

Get the mac address of the other party through arp on the same subnet and throw it directly.

If you are not in the same subnet, get the mac address of the default gateway through arp and throw it directly.

Switch perspective:

The packet I receive must have a destination MAC address

Check the mapping relationship through the MAC address table

If you find out, send it out from my designated port according to the mapping relationship.

If you can't find it, all the ports will be sent out.

Router perspective:

The packet I receive must have a destination IP address

Check the mapping relationship through the routing table

If you find it, it will be sent out from my designated port according to the mapping relationship (not in any subnet range, but also through the default gateway of its router).

If it is not found, a packet with an unreachable route is returned.

If you have a keen sense of smell, you should be able to feel the following sentence:

The network layer (IP protocol) does not have the function of transmitting packets, and the actual transmission of packets is entrusted to the data link layer (switches in Ethernet).

The three tables involved are

There is a MAC address table in the switch to map MAC addresses and its ports

The router has a routing table that maps the IP address (segment) and its port

Both computers and routers have arp cache tables for caching the mapping of IP and MAC addresses

How did these three forms come from?

The MAC address table is constantly improved through the continuous communication between the nodes in the Ethernet through the switch.

The routing table is gradually improved by various routing algorithms and manual configuration.

The arp cache table is gradually perfected through requests from the arp protocol.

Knowing the above, the process of how the two nodes on the network send data packets can be fully explained.

Then we will put on the last network topology diagram of this chapter, please get ready for battle!

At this point, router 1 is connected to router 2, so its routing table has the concept of the next address, so its routing table looks like this. If you match an entry with a next-hop address, you need to match again, find its port, and find the MAC address of the next-hop IP.

In other words, if you look around, you must eventually be able to map to a port number and then send the packet from that port number.

Destination address next hop port 192.168.0.0Universe 24

0192.168.0.254/32

0192.168.1.0/24

1192.168.1.254/32

1192.168.2.0/24192.168.100.5

192.168.100.0/24

2192.168.100.4/32

2 at this time, if A sends a data packet to F, can it get through? If so, what is the whole process?

Think for a minute.

Detailed process animation description:

Detailed process text description:

1. First, A (192.168.0.1) calculated through the subnet mask (255.255.255.0) that he was not on the same subnet as F (192.168.2.2), so he decided to send it to the default gateway (192.168.0.254).

2. A finds the MAC address of the default gateway 192.168.0.254 through ARP.

3. An encapsulates the source MAC address (AAAA) and gateway MAC address (ABAB) in the data link layer header, and then encapsulates the source IP address (192.168.0.1) and destination IP address (192.168.2.2) (please do not think that you are filling in the IP address of the default gateway. From beginning to end, the two IP addresses of this packet are unchanged, only the MAC address is constantly changing), and then send the packet.

4. After receiving the packet, switch 1 finds that the destination MAC address is ABAB and forwards it to router 1.

5. The packet arrives at router 1 and finds that its destination IP address is 192.168.2.2. Looking at its routing table, it is found that the address of the next hop is 192.168.100.5.

6. So at this time, router 1 needs to do two things. The first thing is to match the routing table again and find that the match is port 2, so it is encapsulated into the data link layer, and finally the packet is sent out from gate 2.

7. At this point, router 2 receives the packet and sees that its destination address is 192.168.2.2, queries its routing table, matches to port number 1, and is ready to send the packet out of gate 1.

8. But at this point, router 2 needs to know the MAC address of 192.168.2.2, so check its arp cache, find that its MAC address is FFFF, encapsulate it in the data link layer header, and send the packet out port 1.

9. Switch 3 received the packet, found that the destination MAC address was FFFF, queried its MAC address table, found that it should be out of its port 6, and sent the packet out port 6.

10. F finally received the packet! And found that the destination MAC address is himself, so he accepted the packet.

A more detailed and precise process:

If you want a more professional description of this process, you can reply to "network" in the official account "low concurrency programming" background and get the Cisco Packet Tracer source file that I simulate this process.

Each step of the packet transmission will have each layer of raw data, as well as a professional process description.

At the same time, on this basis, you can also design your own network topology and conduct a variety of experiments to deepen the understanding of the network transmission process.

After this, through the protocols of the physical layer, the data link layer and the network layer, and various network devices (network cables, hubs, switches, routers) designed according to these protocols, theoretically, as long as they have each other's IP address, they have connected two nodes anywhere on the earth.

This paper has been revised many times and deleted a lot of contents that affect the main process, in order to enable readers to grasp the real core ideas of the first three layers of network transmission. At the same time, the knowledge related to the network is also many and miscellaneous, I also have a lot of unclear places, you are very welcome to communicate with me and make progress together.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

IT Information

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report