← Kỹ sư mạng← Network Engineer
Kỹ sư mạngNetwork Engineer19 Th7, 2026Jul 19, 202624 phút đọc21 min read

Định tuyến (Routing)Routing

Thuộc bộ kiến thức Network Engineer Roadmap.

Tổng quan

Routing là quá trình chọn đường đi cho traffic từ một mạng nguồn tới một mạng đích, đi qua một hoặc nhiều thiết bị trung gian. Trong khi switching di chuyển frame bên trong một broadcast domain Layer 2 dựa trên địa chỉ MAC, thì routing di chuyển packet giữa các mạng Layer 3 (các IP subnet) dựa trên địa chỉ IP. Thiết bị làm nhiệm vụ này là router: nó nhận packet trên một interface, quyết định packet nên đi tiếp về đâu, rồi forward ra một interface khác — lặp đi lặp lại, từng hop một, cho tới khi packet đến đích.

Bản chất của internet là một bài toán routing khổng lồ. Không có một tấm bản đồ duy nhất mô tả toàn bộ mạng; thay vào đó, hàng trăm nghìn router mỗi cái nắm một phần thông tin và phối hợp với nhau — thông qua các routing protocol — để dựng lên một tập đường đi nhất quán và không loop. Một packet rời laptop của bạn để tới server ở nửa kia địa cầu có thể đi qua 15–25 router, mỗi router tự đưa ra quyết định forward độc lập trong vài microsecond dựa hoàn toàn vào routing table cục bộ của nó.

Mỗi quyết định routing đều xoay quanh hai câu hỏi:

  1. Đích nằm ở đâu? Router so sánh địa chỉ IP đích của packet với các entry trong routing table (xem IP addressing và subnetting để hiểu cách địa chỉ và prefix hoạt động).
  2. Làm sao để tới đó? Entry khớp sẽ cho router biết địa chỉ next-hop và/hoặc interface đi ra.

Ghi chú này trình bày cách routing table hoạt động, sự khác nhau giữa static và dynamic routing, các họ routing protocol chính (distance-vector, link-state, path-vector), các protocol cụ thể bạn sẽ gặp trong thực tế (RIP, OSPF, EIGRP, BGP), và các công nghệ xây dựng trên nền routing (MPLS, VRF, và cloud route table).

Kiến thức nền tảng

Routing table

Mỗi router (và cả mỗi host — laptop của bạn cũng có) duy trì một routing table: một danh sách có thứ tự các mạng đích đã biết và cách tới từng mạng. Khi một packet đến, router tra bảng này để quyết định next-hop. Một entry tối thiểu gồm:

Bạn có thể xem trực tiếp:

# Linux
$ ip route show
default via 192.168.1.1 dev eth0 proto dhcp metric 100
10.10.0.0/16 via 192.168.1.254 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50
! Cisco IOS
Router# show ip route
Codes: C - connected, S - static, R - RIP, O - OSPF, D - EIGRP, B - BGP
       * - candidate default

C        192.168.1.0/24 is directly connected, GigabitEthernet0/0
O        10.10.0.0/16 [110/20] via 192.168.1.254, 00:15:22, GigabitEthernet0/0
S*       0.0.0.0/0 [1/0] via 192.168.1.1

Trong output Cisco, chữ cái ở cột trái là nguồn của route (C connected, O OSPF, S static). Cặp trong ngoặc [110/20][administrative distance / metric].

Longest-prefix match

Một IP đích có thể khớp cùng lúc nhiều entry trong routing table. Ví dụ, packet đi tới 10.1.2.5 có thể khớp tất cả các entry sau:

EntryĐộ dài prefixKhớp 10.1.2.5?
0.0.0.0/0 (default)0
10.0.0.0/88
10.1.0.0/1616
10.1.2.0/2424

Router luôn chọn entry cụ thể nhất — entry có prefix dài nhất (mask lớn nhất). Ở đây nó chọn 10.1.2.0/24. Đây là quy tắc forward nền tảng: longest-prefix match thắng. Nhờ đó router có thể mang một default route 0.0.0.0/0 làm “bắt tất cả”, đồng thời ghi đè nó bằng các route cụ thể hơn cho từng đích riêng.

Directly connected và remote network

Về bản chất, forward mang tính đệ quy: “để tới mạng X, gửi tới next-hop Y” — và bản thân router phải biết cách tới Y (thường vì Y nằm trên một subnet directly connected).

Administrative distance (AD)

Router có thể học cùng một đích từ nhiều nguồn — ví dụ một static route OSPF RIP cùng mô tả 10.10.0.0/16. Router không thể cài các entry mâu thuẫn, nên phải chọn một. Administrative distance xếp hạng mức độ tin cậy của nguồn, độc lập với metric. AD thấp hơn thắng. Chỉ sau khi AD đã chọn được nguồn thì metric của protocol mới dùng để phân định giữa các route cùng nguồn đó.

Nguồn routeAD mặc định (Cisco)
Connected interface0
Static route1
eBGP (external BGP)20
EIGRP (internal)90
OSPF110
RIP120
iBGP (internal BGP)200
Không tới được / floating dự phòng255 (không bao giờ cài)

Vì vậy một static route (AD 1) luôn thắng một route học từ OSPF (AD 110) cho cùng prefix, bất kể metric — vì bạn, người vận hành, được coi là biết rõ nhất.

Metric

Metric là giá trị mà một routing protocol dùng để so sánh các đường ứng viên của chính nó tới cùng một đích và chọn ra đường tốt nhất. Thấp hơn là tốt hơn. Mỗi protocol đo “tốt nhất” theo cách khác nhau:

Control plane và data plane

Khái niệm chính

Static routing

Static route là route bạn cấu hình bằng tay. Router không tự học, không tự thích nghi, cũng không tự rút nó — route tồn tại cho tới khi bạn xóa.

! Cisco IOS — tới 10.20.0.0/16 qua next-hop 192.0.2.2
Router(config)# ip route 10.20.0.0 255.255.0.0 192.0.2.2

! Default route — "mọi thứ còn lại" đi tới gateway của ISP
Router(config)# ip route 0.0.0.0 0.0.0.0 203.0.113.1
# Tương đương trên Linux
$ sudo ip route add 10.20.0.0/16 via 192.0.2.2
$ sudo ip route add default via 203.0.113.1

Default route (0.0.0.0/0, “quad-zero”) là entry longest-prefix cuối cùng dùng làm phương án dự phòng: nó khớp với mọi đích không được entry cụ thể hơn bao phủ. Các stub network và host thường chỉ mang đúng một default route trỏ về gateway của mình (“default gateway”), thay vì mang cả bảng đầy đủ.

Ưu điểm: không tốn overhead protocol, không tốn CPU/bandwidth cho update, hoàn toàn dự đoán được, an toàn hơn (không có route bị neighbor “tiêm” vào), và kiểm soát đường đi chính xác. Nhược điểm: không scale (mọi route trên mọi router phải nhập tay), không tự failover trừ khi kết hợp với tracking (ví dụ một floating static route có AD cao làm dự phòng), và dễ sai trong topology lớn hoặc hay thay đổi.

Khi nào dùng: mạng nhỏ, stub site có một lối ra duy nhất, default route hướng về ISP, và làm dự phòng. Dưới đây là một floating static route — nó chỉ được cài (AD 130) khi đường chính (học qua OSPF, AD 110) biến mất:

! Đường chính học qua OSPF (AD 110); backup này chỉ cài khi route OSPF mất
Router(config)# ip route 10.20.0.0 255.255.0.0 198.51.100.2 130

Dynamic routing

Dynamic routing dùng một routing protocol để router tự động khám phá các mạng, chia sẻ những gì mình biết cho neighbor, và tính lại đường khi topology thay đổi (một link chết, một router reboot). Thời gian để tất cả router cùng đồng thuận về một góc nhìn nhất quán mới sau một thay đổi gọi là convergence — một chỉ số chất lượng then chốt của mọi protocol.

Ưu điểm: scale được cho mạng lớn, tự thích nghi với sự cố, ít việc thủ công. Nhược điểm: tốn CPU, memory và bandwidth; tăng độ phức tạp; và cần bảo vệ protocol trước các update sai hoặc độc hại.

Hai họ IGP kinh điển khác nhau căn bản ở cái gì được chia sẻ và cách tính đường.

Khía cạnhDistance-vectorLink-state
Mỗi router biết gìChỉ biết khoảng cách/hướng do neighbor báo (“routing theo tin đồn”)Toàn bộ bản đồ topology của area
Quảng bá cái gìToàn bộ routing table của mình, cho neighbor trực tiếpCác link của chính nó (LSA), flood tới mọi router trong area
Thuật toánBellman-FordDijkstra (SPF — Shortest Path First)
ConvergenceChậm hơn; nguy cơ loop và count-to-infinityNhanh hơn; mỗi router tự tính độc lập
Tài nguyênCPU/memory thấpCPU/memory cao hơn (giữ cả topology)
Ví dụRIP, EIGRP (nâng cao)OSPF, IS-IS

Router distance-vector tin vào bản tóm tắt của neighbor; router link-state tự dựng bức tranh đầy đủ của mình và chạy Dijkstra để tìm đường ngắn nhất, nhờ đó tránh được hầu hết vấn đề loop và converge nhanh hơn.

IGP và EGP

Một autonomous system được định danh bằng một AS number (ASN) — một số duy nhất toàn cầu (16-bit 1–65535, hoặc 32-bit) cấp bởi registry khu vực (ví dụ AS15169 là Google). ISP của bạn có ASN; các doanh nghiệp lớn có multi-home cũng có.

RIP — Routing Information Protocol

RIP là routing protocol động cổ và đơn giản nhất — một IGP distance-vector thuần túy.

Router(config)# router rip
Router(config-router)# version 2
Router(config-router)# no auto-summary
Router(config-router)# network 192.168.1.0
Router(config-router)# network 10.0.0.0

Hạn chế: trần 15 hop, convergence chậm, và metric hop count bỏ qua bandwidth (một đường 10 Gbps qua 3 hop lại thua đường 1 Mbps qua 2 hop). RIP chủ yếu chỉ còn tồn tại trong lab, mạng cực nhỏ, và thiết bị legacy. Các cơ chế chống loop mà nó dựa vào: split horizon, route poisoning, poison reverse, và hold-down timer.

OSPF — Open Shortest Path First

OSPF là IGP link-state chuẩn mở phổ biến nhất (RFC 2328 cho OSPFv2/IPv4, RFC 5340 cho OSPFv3/IPv6). Mọi router dựng một Link-State Database (LSDB) giống hệt nhau mô tả topology, rồi chạy thuật toán SPF của Dijkstra để tính cây đường ngắn nhất tới mọi đích.

Cách hoạt động:

  1. Router khám phá neighbor và hình thành adjacency bằng gói Hello.
  2. Mỗi router mô tả các link của mình trong các LSA (Link-State Advertisement) và flood chúng tới mọi router trong area.
  3. Mọi router ghép các LSA lại thành một LSDB giống hệt nhau.
  4. Mỗi router độc lập chạy SPF để dựng routing table của mình.

Cost (metric): cost của OSPF là reference-bandwidth / interface-bandwidth. Với reference mặc định 100 Mbps, link 100 Mbps = cost 1, link 10 Mbps = cost 10. Trên mạng hiện đại bạn nên tăng reference bandwidth (ví dụ lên 100000 cho 100 Gbps) để các link nhanh được phân biệt.

Area: OSPF scale bằng cách chia một domain lớn thành các area, tất cả nối vào một backbone area 0 trung tâm. Việc flood LSA được giới hạn trong một area; Area Border Router (ABR) tóm tắt route giữa các area. Nhờ đó LSDB nhỏ và SPF chạy nhẹ. Các loại area đặc biệt (stub, totally stubby, NSSA) còn giảm thêm số LSA đưa vào các area rìa.

DR/BDR: Trên một segment multi-access (như Ethernet có nhiều router) OSPF bầu một Designated Router (DR)Backup DR (BDR) để tránh việc mọi router phải hình thành full-mesh adjacency. Mọi router chỉ hình thành adjacency với DR/BDR, giảm việc flood LSA từ O(n²) xuống O(n). Bầu chọn theo OSPF priority cao nhất, rồi tới Router ID cao nhất.

Convergence: nhanh — thường dưới một giây tới vài giây — vì mỗi router đã giữ sẵn toàn bộ topology và chỉ cần chạy lại SPF khi một LSA thay đổi.

! Cisco IOS — OSPF một area
Router(config)# router ospf 1
Router(config-router)# router-id 1.1.1.1
Router(config-router)# auto-cost reference-bandwidth 100000
Router(config-router)# network 192.168.1.0 0.0.0.255 area 0
Router(config-router)# network 10.1.0.0 0.0.255.255 area 0
Router(config-router)# passive-interface GigabitEthernet0/2
!
! Kiểm tra
Router# show ip ospf neighbor
Router# show ip route ospf

Lưu ý wildcard mask (0.0.0.255, đảo ngược của subnet mask) và việc gán area tường minh. passive-interface dừng gửi Hello của OSPF trên các interface hướng về host (nơi không có router), giúp tăng bảo mật và giảm nhiễu.

EIGRP — Enhanced Interior Gateway Routing Protocol

EIGRP là protocol distance-vector nâng cao của Cisco (đã mở thành RFC 7868 dạng informational). Nó hành xử như distance-vector (học từ neighbor, không giữ topology đầy đủ) nhưng bổ sung tốc độ và tính không-loop kiểu link-state nhờ DUAL (Diffusing Update Algorithm).

! Cisco IOS — cấu hình named EIGRP
Router(config)# router eigrp CORP
Router(config-router)# address-family ipv4 unicast autonomous-system 100
Router(config-router-af)# network 192.168.1.0 0.0.0.255
Router(config-router-af)# network 10.1.0.0 0.0.255.255
!
Router# show ip eigrp neighbors
Router# show ip eigrp topology

EIGRP converge rất nhanh khi có feasible successor và cấu hình đơn giản hơn OSPF, nhưng (về mặt lịch sử) gắn với Cisco, nên phổ biến trong doanh nghiệp toàn Cisco và hiếm gặp ở môi trường đa hãng hoặc core của nhà cung cấp dịch vụ.

BGP — Border Gateway Protocol

BGP (phiên bản 4, RFC 4271) là routing protocol của internet — EGP duy nhất còn dùng. Nó là protocol path-vector: thay vì một metric đơn giản, nó quảng bá cả AS-path (danh sách các autonomous system mà một route đi qua), vừa cho phép làm policy vừa chống loop (một router loại bỏ mọi route có AS-path đã chứa ASN của chính nó).

eBGP và iBGP:

eBGP (external)iBGP (internal)
PeerGiữa các AS khác nhauTrong cùng một AS
TTLThường trực tiếp kề nhau (TTL 1)Bất kỳ đâu trong AS
AS-pathPrepend ASN của mình khi quảng báKhông đổi AS-path
AD mặc định (Cisco)20200
Hành vi next-hopĐặt next-hop là chính nóGiữ nguyên next-hop (thường cần next-hop-self)
Chống loopAS-pathQuy tắc split-horizon → cần full mesh hoặc route reflector

Vì iBGP không quảng bá lại route học từ một iBGP peer sang một iBGP peer khác (để chống loop khi không đổi AS-path), một thiết kế iBGP ngây thơ đòi hỏi full mesh các phiên peer. Các AS lớn giải quyết bằng route reflector hoặc confederation.

Các path attribute chính của BGP (dùng để chọn đường tốt nhất, theo thứ tự quyết định của Cisco — cao nhất thắng trừ khi ghi chú khác):

  1. Weight (chỉ Cisco, cục bộ trên một router) — cao nhất thắng.
  2. Local Preference — cao nhất thắng; đặt trong AS của bạn để ưu tiên một điểm ra (ví dụ ưu tiên ISP rẻ hơn).
  3. Ưu tiên route khởi tạo cục bộ.
  4. Độ dài AS-path — ngắn nhất thắng (đi qua ít AS hơn).
  5. Kiểu Origin (IGP < EGP < Incomplete).
  6. MED (Multi-Exit Discriminator) — thấp nhất thắng; một gợi ý cho AS neighbor về điểm vào nào của AS bạn nên ưu tiên.
  7. eBGP hơn iBGP.
  8. IGP metric tới next-hop thấp nhất, rồi route cũ nhất, rồi router ID thấp nhất làm tiêu chí phá hòa.

Một mô hình tư duy hữu ích: Local Preference kiểm soát cách traffic rời khỏi AS của bạn (outbound), còn MEDAS-path prepending ảnh hưởng tới cách traffic đi vào AS của bạn (inbound).

! Cisco IOS — eBGP tới một ISP, cộng một iBGP peer
Router(config)# router bgp 65001
Router(config-router)# bgp router-id 10.0.0.1
!  neighbor eBGP thuộc AS của ISP
Router(config-router)# neighbor 203.0.113.1 remote-as 64500
!  neighbor iBGP trong AS của mình
Router(config-router)# neighbor 10.0.0.2 remote-as 65001
Router(config-router)# neighbor 10.0.0.2 next-hop-self
!  quảng bá prefix public của mình vào BGP
Router(config-router)# network 198.51.100.0 mask 255.255.255.0
!
!  ưu tiên link ISP cho traffic outbound qua local-pref
Router(config)# route-map PREFER-ISP permit 10
Router(config-route-map)# set local-preference 200
Router(config)# router bgp 65001
Router(config-router)# neighbor 203.0.113.1 route-map PREFER-ISP in
!
Router# show ip bgp summary
Router# show ip bgp

BGP được đánh giá cao vì tính ổn định và khả năng kiểm soát policy, chứ không phải tốc độ: nó không “flap” đường trên mỗi thay đổi nhỏ, và update mang theo các attribute phong phú để người vận hành cài đặt policy routing phức tạp (peering vs transit, traffic engineering, lọc prefix). Quy mô của nó cực lớn — bảng IPv4 toàn cầu vượt quá 900.000 prefix.

MPLS và MPLS VPN

MPLS (Multi-Protocol Label Switching) forward packet dựa trên các label ngắn, độ dài cố định thay vì tra IP đích ở mỗi hop. Tại rìa mạng, router ingress (LER — Label Edge Router) phân loại packet vào một FEC (Forwarding Equivalence Class) và push một label. Các router lõi (LSR — Label Switching Router) sau đó swap label và forward dọc theo một LSP (Label-Switched Path) đã lập sẵn — một phép tra nhanh dựa trên bảng — cho tới khi router egress pop label và forward theo IP bình thường. Label được phân phối bởi LDP hoặc bởi BGP hiểu MPLS / segment routing.

Vì sao nó quan trọng:

VRF — Virtual Routing and Forwarding

VRF cho phép một router vật lý duy nhất giữ nhiều routing table độc lập cùng lúc, như thể nó là nhiều router ảo. Mỗi VRF có routing table riêng, bộ interface riêng, và góc nhìn mạng riêng — traffic trong một VRF không thể tới VRF khác trừ khi bạn cố ý leak route giữa chúng.

Ứng dụng:

! Cisco IOS — định nghĩa một VRF và gán interface
Router(config)# vrf definition CUSTOMER-A
Router(config-vrf)# rd 65001:100
Router(config-vrf)# address-family ipv4
!
Router(config)# interface GigabitEthernet0/1
Router(config-if)# vrf forwarding CUSTOMER-A
Router(config-if)# ip address 10.1.1.1 255.255.255.0
!
Router# show ip route vrf CUSTOMER-A

Route distinguisher (RD) làm cho các prefix khách hàng vốn trùng nhau trở nên duy nhất toàn cầu bên trong MP-BGP.

Cloud routing

Mạng cloud định tuyến bằng route table gắn vào subnet thay vì bằng routing protocol chạy trên thiết bị vật lý. Trong AWS, một VPC route table ánh xạ CIDR đích tới target (internet gateway, NAT gateway, transit gateway, VPC peering, hay virtual private gateway); match cụ thể nhất (longest-prefix) thắng, đúng như trên router vật lý. Route propagation có thể tự động tiêm các route học qua VPN hoặc Direct Connect (qua BGP trên virtual gateway) vào route table. GCP và Azure theo cùng mô hình với tên gọi riêng. Chủ đề này được trình bày sâu trong ./16-cloud-networking.md.

So sánh các routing protocol

ProtocolLoạiMetricIGP/EGPChuẩnConvergenceUse case điển hình
Static— (thủ công)Chỉ ADTức thì (không thích nghi)Stub site, default route, dự phòng
RIPDistance-vectorHop count (tối đa 15)IGPRFC 2453 (v2)ChậmLegacy / mạng rất nhỏ
OSPFLink-stateCost (bandwidth)IGPRFC 2328Nhanh (dưới giây)Doanh nghiệp & core đa hãng
IS-ISLink-stateCostIGPISO 10589NhanhCore nhà cung cấp dịch vụ
EIGRPDistance-vector nâng caoBandwidth + delayIGPRFC 7868Rất nhanh (feasible successor)Doanh nghiệp toàn Cisco
BGPPath-vectorPath attributeEGP (và iBGP)RFC 4271Chậm (cố ý)Internet, inter-AS, policy WAN

Best Practices

Tài liệu tham khảo

Part of the Network Engineer Roadmap knowledge base.

Overview

Routing is the process of selecting a path for traffic to travel from a source network to a destination network across one or more intermediate devices. Where switching moves frames within a single Layer 2 broadcast domain using MAC addresses, routing moves packets between Layer 3 networks (IP subnets) using IP addresses. A router is the device that performs this job: it receives a packet on one interface, decides where it should go next, and forwards it out another interface — repeatedly, hop by hop, until the packet reaches its destination.

The internet is, at its heart, a colossal exercise in routing. There is no single map of the whole network; instead, hundreds of thousands of routers each hold a partial view and cooperate — through routing protocols — to build a consistent, loop-free set of paths. A packet leaving your laptop to reach a server on the other side of the planet may cross 15–25 routers, each making an independent forwarding decision in microseconds based purely on its local routing table.

Two questions sit at the core of every routing decision:

  1. Where is the destination? The router compares the packet’s destination IP against entries in its routing table (see IP addressing and subnetting for how addresses and prefixes work).
  2. How do I reach it? The matching entry tells the router the next-hop address and/or the outgoing interface.

This note covers how the routing table works, the difference between static and dynamic routing, the major routing protocol families (distance-vector, link-state, path-vector), the specific protocols you will meet in the field (RIP, OSPF, EIGRP, BGP), and the technologies built on top of routing (MPLS, VRF, and cloud route tables).

Fundamentals

The routing table

Every router (and every host — your laptop has one too) maintains a routing table: an ordered list of known destination networks and how to reach each one. When a packet arrives, the router consults this table to decide the next hop. A single entry contains, at minimum:

You can inspect it directly:

# Linux
$ ip route show
default via 192.168.1.1 dev eth0 proto dhcp metric 100
10.10.0.0/16 via 192.168.1.254 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50
! Cisco IOS
Router# show ip route
Codes: C - connected, S - static, R - RIP, O - OSPF, D - EIGRP, B - BGP
       * - candidate default

C        192.168.1.0/24 is directly connected, GigabitEthernet0/0
O        10.10.0.0/16 [110/20] via 192.168.1.254, 00:15:22, GigabitEthernet0/0
S*       0.0.0.0/0 [1/0] via 192.168.1.1

In the Cisco output the letter in the left column is the route source (C connected, O OSPF, S static). The bracketed pair [110/20] is [administrative distance / metric].

Longest-prefix match

A destination IP can match several routing-table entries at once. For example, a packet to 10.1.2.5 might match all of these:

EntryPrefix lengthMatches 10.1.2.5?
0.0.0.0/0 (default)0yes
10.0.0.0/88yes
10.1.0.0/1616yes
10.1.2.0/2424yes

The router always chooses the most specific match — the one with the longest prefix (largest mask). Here it picks 10.1.2.0/24. This is the fundamental forwarding rule: longest-prefix match wins. It lets a router carry a broad 0.0.0.0/0 default route as a catch-all while overriding it with more specific routes for particular destinations.

Directly connected vs remote networks

Forwarding is recursive in spirit: “to reach network X, send to next-hop Y” — and the router must itself know how to reach Y (usually because Y is on a directly connected subnet).

Administrative distance (AD)

A router may learn about the same destination from multiple sources — say, a static route and OSPF and RIP all describe 10.10.0.0/16. It cannot install conflicting entries, so it must pick one. Administrative distance ranks the trustworthiness of the source, independent of the metric. Lower AD wins. Only after AD selects a source does the protocol’s own metric break ties among routes from that same source.

Route sourceDefault AD (Cisco)
Connected interface0
Static route1
eBGP (external BGP)20
EIGRP (internal)90
OSPF110
RIP120
iBGP (internal BGP)200
Unreachable / floating fallback255 (never installed)

So a static route (AD 1) always beats an OSPF-learned route (AD 110) for the same prefix, regardless of metrics — because you, the operator, are assumed to know best.

Metric

The metric is a value a routing protocol uses to compare its own candidate paths to the same destination and pick the best one. Lower is better. Each protocol measures “best” differently:

Control plane vs data plane

Key Concepts

Static routing

A static route is a route you configure by hand. The router does not learn it, adapt it, or withdraw it — it stays until you remove it.

! Cisco IOS — reach 10.20.0.0/16 via next-hop 192.0.2.2
Router(config)# ip route 10.20.0.0 255.255.0.0 192.0.2.2

! Default route — "everything else" goes to the ISP gateway
Router(config)# ip route 0.0.0.0 0.0.0.0 203.0.113.1
# Linux equivalent
$ sudo ip route add 10.20.0.0/16 via 192.0.2.2
$ sudo ip route add default via 203.0.113.1

The default route (0.0.0.0/0, “quad-zero”) is the ultimate longest-prefix fallback: it matches any destination not covered by a more specific entry. Stub networks and hosts typically carry just one default route pointing at their gateway (“the default gateway”), rather than a full table.

Pros: no protocol overhead, no CPU/bandwidth for updates, fully predictable, more secure (no route injection from neighbors), and precise control over paths. Cons: does not scale (every route on every router by hand), no automatic failover unless combined with tracking (e.g. a floating static route with a high AD as backup), and error-prone in large or changing topologies.

When to use: small networks, stub sites with a single exit, default routes toward an ISP, and as backups. Below is a floating static route — it only installs (AD 5) if the primary path (via OSPF, AD 110… actually lower AD wins, so use AD higher than the primary’s) disappears:

! Primary learned via OSPF (AD 110); this backup installs only if OSPF route is gone
Router(config)# ip route 10.20.0.0 255.255.0.0 198.51.100.2 130

Dynamic routing

Dynamic routing uses a routing protocol so routers automatically discover networks, share what they know with neighbors, and recompute paths when the topology changes (a link fails, a router reboots). The time it takes all routers to agree on a new consistent view after a change is called convergence — a key quality metric for any protocol.

Pros: scales to large networks, adapts automatically to failures, less manual work. Cons: consumes CPU, memory, and bandwidth; adds complexity; and requires securing the protocol against bad or malicious updates.

The two classic families of IGP differ fundamentally in what they share and how they compute paths.

AspectDistance-vectorLink-state
What each router knowsOnly distances/directions reported by neighbors (“routing by rumor”)The full topology map of its area
What is advertisedIts entire routing table, to directly connected neighborsIts own links (LSAs), flooded to all routers in the area
AlgorithmBellman-FordDijkstra (SPF — Shortest Path First)
ConvergenceSlower; risk of loops and count-to-infinityFaster; each router computes independently
Resource useLow CPU/memoryHigher CPU/memory (holds full topology)
ExamplesRIP, EIGRP (advanced)OSPF, IS-IS

Distance-vector routers trust their neighbors’ summaries; link-state routers build their own complete picture and run Dijkstra to find shortest paths, which avoids most loop problems and converges faster.

IGP vs EGP

An autonomous system is identified by an AS number (ASN) — a globally unique number (16-bit 1–65535, or 32-bit) assigned by a regional registry (e.g. AS15169 is Google). Your ISP has an ASN; large enterprises that multi-home have one too.

RIP — Routing Information Protocol

RIP is the oldest, simplest dynamic routing protocol — a pure distance-vector IGP.

Router(config)# router rip
Router(config-router)# version 2
Router(config-router)# no auto-summary
Router(config-router)# network 192.168.1.0
Router(config-router)# network 10.0.0.0

Limitations: the 15-hop ceiling, slow convergence, and a hop-count metric that ignores bandwidth (a 10 Gbps 3-hop path loses to a 1 Mbps 2-hop path). RIP survives mostly in labs, tiny networks, and legacy gear. Loop-prevention mechanisms it relies on: split horizon, route poisoning, poison reverse, and hold-down timers.

OSPF — Open Shortest Path First

OSPF is the dominant open-standard link-state IGP (RFC 2328 for OSPFv2/IPv4, RFC 5340 for OSPFv3/IPv6). Every router builds an identical Link-State Database (LSDB) describing the topology, then runs Dijkstra’s SPF algorithm to compute the shortest-path tree to every destination.

How it works:

  1. Routers discover neighbors and form adjacencies using Hello packets.
  2. Each router describes its links in LSAs (Link-State Advertisements) and floods them to all routers in the area.
  3. Every router assembles the LSAs into an identical LSDB.
  4. Each router independently runs SPF to build its routing table.

Cost (metric): OSPF cost is reference-bandwidth / interface-bandwidth. With the default reference of 100 Mbps, a 100 Mbps link = cost 1, a 10 Mbps link = cost 10. On modern networks you should raise the reference bandwidth (e.g. to 100000 for 100 Gbps) so fast links are distinguished.

Areas: OSPF scales by dividing a large domain into areas, all connected to a central backbone area 0. LSA flooding is contained within an area; Area Border Routers (ABRs) summarize between areas. This keeps LSDBs small and SPF runs cheap. Special area types (stub, totally stubby, NSSA) further reduce the LSAs injected into edge areas.

DR/BDR: On a multi-access segment (like Ethernet with many routers) OSPF elects a Designated Router (DR) and Backup DR (BDR) to avoid every router forming a full mesh of adjacencies. All routers form adjacency with the DR/BDR only, which reduces LSA flooding from O(n²) to O(n). Election is by highest OSPF priority, then highest Router ID.

Convergence: fast — typically sub-second to a few seconds — because each router already holds the full topology and only needs to re-run SPF when an LSA changes.

! Cisco IOS — OSPF single area
Router(config)# router ospf 1
Router(config-router)# router-id 1.1.1.1
Router(config-router)# auto-cost reference-bandwidth 100000
Router(config-router)# network 192.168.1.0 0.0.0.255 area 0
Router(config-router)# network 10.1.0.0 0.0.255.255 area 0
Router(config-router)# passive-interface GigabitEthernet0/2
!
! Verify
Router# show ip ospf neighbor
Router# show ip route ospf

Note the wildcard mask (0.0.0.255, the inverse of a subnet mask) and the explicit area assignment. passive-interface stops OSPF Hellos on interfaces facing hosts (no routers there), improving security and reducing noise.

EIGRP — Enhanced Interior Gateway Routing Protocol

EIGRP is Cisco’s advanced distance-vector protocol (opened as informational RFC 7868). It behaves like distance-vector (it learns from neighbors, not a full topology) but adds link-state-like speed and loop-freedom via the DUAL (Diffusing Update Algorithm).

! Cisco IOS — named EIGRP configuration
Router(config)# router eigrp CORP
Router(config-router)# address-family ipv4 unicast autonomous-system 100
Router(config-router-af)# network 192.168.1.0 0.0.0.255
Router(config-router-af)# network 10.1.0.0 0.0.255.255
!
Router# show ip eigrp neighbors
Router# show ip eigrp topology

EIGRP converges very fast when feasible successors are available and is simpler to configure than OSPF, but it is (historically) Cisco-centric, so it is common in all-Cisco enterprises and rare in multi-vendor or service-provider cores.

BGP — Border Gateway Protocol

BGP (version 4, RFC 4271) is the routing protocol of the internet — the only EGP in use. It is a path-vector protocol: instead of a simple metric, it advertises the full AS-path (the list of autonomous systems a route traverses), which both enables policy and prevents loops (a router rejects any route whose AS-path already contains its own ASN).

eBGP vs iBGP:

eBGP (external)iBGP (internal)
PeersBetween different ASesWithin the same AS
TTLTypically directly connected (TTL 1)Anywhere in the AS
AS-pathPrepends own ASN on advertiseDoes not change AS-path
Default AD (Cisco)20200
Next-hop behaviorSets next-hop to itselfNext-hop unchanged (often needs next-hop-self)
Loop preventionAS-pathSplit-horizon rule → needs full mesh or route reflectors

Because iBGP does not re-advertise routes learned from one iBGP peer to another (to prevent loops without an AS-path change), a naive iBGP design requires a full mesh of peerings. Large ASes solve this with route reflectors or confederations.

Key BGP path attributes (used to select the best path, in Cisco’s decision order — highest wins unless noted):

  1. Weight (Cisco-only, local to one router) — highest wins.
  2. Local Preference — highest wins; set within your AS to prefer an exit point (e.g. prefer the cheaper ISP).
  3. Locally originated routes preferred.
  4. AS-path length — shortest wins (fewer ASes to cross).
  5. Origin type (IGP < EGP < Incomplete).
  6. MED (Multi-Exit Discriminator) — lowest wins; a hint to a neighbor AS about which entry point into your AS to prefer.
  7. eBGP over iBGP.
  8. Lowest IGP metric to the next-hop, then oldest route, then lowest router ID as tiebreakers.

A useful mental model: Local Preference controls how traffic leaves your AS (outbound), while MED and AS-path prepending influence how traffic enters your AS (inbound).

! Cisco IOS — eBGP to an ISP, plus one iBGP peer
Router(config)# router bgp 65001
Router(config-router)# bgp router-id 10.0.0.1
!  eBGP neighbor in the ISP's AS
Router(config-router)# neighbor 203.0.113.1 remote-as 64500
!  iBGP neighbor inside our own AS
Router(config-router)# neighbor 10.0.0.2 remote-as 65001
Router(config-router)# neighbor 10.0.0.2 next-hop-self
!  advertise our public prefix into BGP
Router(config-router)# network 198.51.100.0 mask 255.255.255.0
!
!  prefer the ISP link for outbound traffic via local-pref
Router(config)# route-map PREFER-ISP permit 10
Router(config-route-map)# set local-preference 200
Router(config)# router bgp 65001
Router(config-router)# neighbor 203.0.113.1 route-map PREFER-ISP in
!
Router# show ip bgp summary
Router# show ip bgp

BGP is prized for stability and policy control, not speed: it does not flap paths on every minor change, and updates carry rich attributes so operators can implement complex routing policy (peering vs transit, traffic engineering, prefix filtering). Its scale is immense — the global IPv4 table exceeds 900,000 prefixes.

MPLS and MPLS VPNs

MPLS (Multi-Protocol Label Switching) forwards packets based on short, fixed-length labels rather than by looking up the destination IP at every hop. At the network edge, an ingress router (LER — Label Edge Router) classifies a packet into a FEC (Forwarding Equivalence Class) and pushes a label. Core routers (LSRs — Label Switching Routers) then swap labels and forward along a pre-established LSP (Label-Switched Path) — a fast, table-driven lookup — until the egress router pops the label and forwards natively. Labels are distributed by LDP or by MPLS-aware BGP/segment routing.

Why it matters:

VRF — Virtual Routing and Forwarding

VRF lets a single physical router hold multiple independent routing tables at once, as if it were several virtual routers. Each VRF has its own routing table, its own set of interfaces, and its own view of the network — traffic in one VRF cannot reach another unless you explicitly leak routes between them.

Uses:

! Cisco IOS — define a VRF and bind an interface
Router(config)# vrf definition CUSTOMER-A
Router(config-vrf)# rd 65001:100
Router(config-vrf)# address-family ipv4
!
Router(config)# interface GigabitEthernet0/1
Router(config-if)# vrf forwarding CUSTOMER-A
Router(config-if)# ip address 10.1.1.1 255.255.255.0
!
Router# show ip route vrf CUSTOMER-A

The route distinguisher (RD) makes otherwise-overlapping customer prefixes globally unique inside MP-BGP.

Cloud routing

Cloud networks route with route tables attached to subnets rather than routing protocols on physical boxes. In AWS a VPC route table maps destination CIDRs to targets (an internet gateway, NAT gateway, transit gateway, VPC peering connection, or virtual private gateway); the most specific (longest-prefix) match wins, exactly as on a physical router. Route propagation can automatically inject routes learned over a VPN or Direct Connect (via BGP on a virtual gateway) into the route table. GCP and Azure follow the same model with their own naming. This is covered in depth in ./16-cloud-networking.md.

Routing protocol comparison

ProtocolTypeMetricIGP/EGPStandardConvergenceTypical use case
Static— (manual)AD onlyInstant (no adaptation)Stub sites, default routes, backups
RIPDistance-vectorHop count (max 15)IGPRFC 2453 (v2)SlowLegacy / very small nets
OSPFLink-stateCost (bandwidth)IGPRFC 2328Fast (sub-second)Enterprise & multi-vendor cores
IS-ISLink-stateCostIGPISO 10589FastService-provider cores
EIGRPAdvanced distance-vectorBandwidth + delayIGPRFC 7868Very fast (feasible successor)All-Cisco enterprises
BGPPath-vectorPath attributesEGP (and iBGP)RFC 4271Slow (by design)Internet, inter-AS, WAN policy

Best Practices

References