Hello everyone!
Next, let's continue to learn about routing.
Longest Match Principle
When forwarding data, a router needs to select the optimal route in the routing table. When a data packet arrives at a router, the router extracts the destination IP address of the packet, searches the routing table, and then performs the AND operation on the destination IP address of the packet and the mask field of an entry in the routing table. If the result of the AND operation is the same as the destination IP address of the entry in the routing table, the packet matches the destination IP address of the entry in the routing table. Otherwise, the packet does not match the destination IP address of the entry in the routing table. When all routing entries are matched, the router selects the entry with the longest mask.

As shown in the preceding figure, two entries in the routing table of RTA reach the destination network segment 10.1.1.0, and the next-hop addresses of the two entries are 20.1.1.2. If packets need to be forwarded to network segment 10.1.1.1, 10.1.1.0/30 complies with the longest match rule.
Routing Preference
A router can learn the routes to the same destination network through multiple protocols (including static routes). When these routes meet the longest match rule, the router must determine which route is preferred. Therefore, each routing protocol has a protocol priority. When multiple routes exist, the route discovered by the routing protocol with the highest priority becomes the optimal route and is added to the routing table.
As shown in the following figure, R3 and R1 run the RIP, and R3 establishes an adjacency through OSPF, R3 learns the route to 1.1.1.0/24 from both RIP and OSPF and adds the OSPF router to the routing table because the OSPF route has a lower priority than the RIP route.

Different manufacturers have different requirements for the priority of various routing protocols. The default priority of the HUAWEI Quidway router is shown in the table:
The smaller the value, the higher the preference.
In addition to direct routing, the preference of all dynamic routing protocols can be manually configured according to user needs. In addition, the priority of each static route can be different.
Route Metric
The metrics of the route indicate the cost of reaching the destination address.
Commonly used metrics are: hop count, bandwidth, delay, cost, load, reliability, etc.
As shown in the figure, OSPF is running and the cost (metric value) of the route is calculated based on the bandwidth. Therefore, the route with metric=1+1=2 is the optimal route to the destination, and its entry can be found in the routing table.

Where the route is unable to be distinguished by either the longest match value or preference, the cost metric is taken as the decision-maker in identifying the route that should be installed in the routing table.
Metric represents the length of a path to a destination network. Usually, the following factors will affect the routing metric.
Line delay, bandwidth, load, communication overhead, line reliability, hop count, and maximum transmission unit.
Hop count refers to the number of routers that arrive at the destination.
Hop count refers to the number of routers that arrive at the destination.
The smaller the Metric value, the more priority the routing is.
Different dynamic routing protocols choose one or more of these factors to calculate the metric. The metric is only meaningful in the same routing protocol, and the routing metric between different routing protocols is not comparable and there is no conversion relationship.
Next-Hop and Interface
When a router finds a matching routing table item, it needs to know the next-hop and out-interface to forward the data.

After receiving a packet, the router will check its destination IP address and then check the routing table. After finding out the matching routing item, the router will forward the packet according to the interface and the next-hop information indicated by the table item.
Autonomous Systems (AS)
Autonomous Systems (AS): A group of routers that are managed by the same organization and use the same routing policy.

An AS is a set of routers that share similar routing policies and run in a single management domain. An AS can be a set of routers that run a single IGP (internal Gateway Protocol) protocol, or a set of routers that run different routing protocols but belong to the same organization. In either case, the outside world regards the whole Autonomous System as an entity.
Each Autonomous System has a unique AS number, which is assigned by the Internet authorized authority IANA. Its basic idea is to distinguish different AS by different numbers. In this way, when the network administrator does not want his communication data to pass through an AS, this numbering method is very useful. For example, the network administrator's network can be fully accessible to an AS, but it may be managed by a competitor or lack sufficient security mechanism, it should be avoided. By using routing protocols and AS numbers, routers can determine the path and routing information exchange methods.
The numbering range of AS is 1 to 65535, of which 1 to 65411 are registered Internet numbers, and 65412 to 65535 are dedicated network numbers.
IGP & EGP

According to the working area, routing protocols can be divided into IGP and EGP:
IGP(Interior Gateway Protocols):
RIP and IS-IS exchange routing information in the same AS. Both RIP and IS-IS are IGPs. IGP is used to discover and calculate routing information in an AS.
EGP(Exterior Gateway Protocols):
BGP is used to connect different ASs and exchange routing information between ASs. Routing policies and route filtering are used to control the transmission of routing information between ASs. An example of BGP is BGP.
Position of the Dynamic Routing Protocol in the Protocol Stack
All dynamic routing protocols are application layer protocols in the TCP/IP protocol stack. Different routing protocols, however, use different bottom-layer protocols.

OSPF directly encapsulates protocol packets into IP packets. The protocol number is 89. The IP protocol is an unreliable transmission protocol. Therefore, the reliability of OSPF transmission depends on the protocol.
BGP uses TCP as the transmission protocol, improving protocol reliability. The TCP port number is 179.
RIP uses UDP as the transmission protocol and port 520
Raw IP: The IP header is directly operated through a socket. If Raw IP is used, the IP protocol can be directly used without processing based on the transport layer. When Raw IP is used, a ping command can be used to send an ICMP request, or OSPF can be used for routing and addressing.
Interoperation Between Routing Protocols
To support multiple routing protocols on the same Internet, routing information must be shared among these different routing protocols. For example, routes learned from RIP may need to be imported to OSPF. This process of exchanging routing information between different routing protocols is called route import. Routes can be imported unidirectionally (for example, RIP is imported into OSPF) or bidirectionally (RIP and OSPF import routes from each other). Generally, the routers that import routes are located on the borders of different ASs or different routing domains.
Different routing protocols use different algorithms and may discover different routes. Therefore, the routing protocols need to share their discovery results. As mentioned above, the costs of different routing protocols are not comparable and cannot be converted. Therefore, when importing routes, you must reset the metric value of the imported routes or use the default metric value. The VRP supports the import-route command to import the routes discovered by one routing protocol to another routing protocol. Each routing protocol has its own route import mechanism.
Each routing protocol can advertise and learn only known routes of its own protocol.
A known route refers to a route that is discovered by a routing protocol running on an interface or in a routing table.
If need to know other routes, it needs to take operation (import-route).
Routing protocols import routes from each other to share routing information, but this brings some problems. The use of multiple routing protocols usually leads to complex network management and extra overheads. When a router sends the routing information learned from one AS back to the same AS, routing loops may occur. In addition, each routing protocol uses different metrics to determine the optimal route. Therefore, using imported route information to select a path may cause a sub-optimal route. Generally, routing protocols should not overlap (RIP and OSPF should be used in the same area). The networks that use different routing protocols should have clear boundaries. If more than one router is used as the route import point, import routes in only one direction to avoid routing loops and problems caused by inconsistent convergence time. If there is only one border router in a routing domain, bidirectional import can be used.
Route import means that the router searches the routing table for the route to be imported. If the router finds the route to be imported (such as static), the router advertises the route as a known route.
The most commonly used route is the imported static route and direct route. In some cases, the routes of other routing protocols need to be imported.
Route Self-loop
A routing loop occurs when a packet is sent from a router forwarded several times, and then returned to the original router. This is because the routing tables of some routers are incorrect. The possible Cause is that the static route is incorrectly configured or the dynamic routing protocol incorrectly calculates the route (although the probability of this case is low). When a routing loop occurs, packets are forwarded among several routers until the TTL is 0. This wastes network resources. Therefore, routing loops should be avoided.

Measures the performance indicators of routing protocols:
Correctness
Can be found correctly, and no self-loop occurs.
Fast convergence
When the topology of a network changes, routes in the autonomous system can be changed quickly.
Low cost
Minimum protocol overhead (memory, CPU, and network bandwidth)
Security
The protocol is not vulnerable to attacks and has a security mechanism.
Universality
Adapts to networks of various topologies and scales.
Comprehensive performance comparison of routing protocols: BGP, OSPF, and IS-IS
have no routing loops. RIP1 and RIP2 have routing loops.
Thanks for reading!