Sub topic 6
Exchanging vectors with neighbors
This chapter assumes you know what a routing table is, how longest-prefix match picks a route, and the Bellman-Ford update from the previous chapter. It covers the mechanics of the exchange itself: what a router sends, who receives it, how the receiver folds it into its table, and when the process stops.
What a vector contains
A distance vector is a list of pairs, one per destination: the destination and the sender's current cost to reach it. It does not contain paths or next hops. The receiver sees only "I can reach 10.2.0.0/16 at cost 3", with no idea how.
The vector is built from the sender's own routing table. Each router also adds an entry for itself at cost 0. That entry is how a neighbor learns the router exists at all.
Each router keeps three pieces of state per destination:
- the best known cost,
- the next hop (the neighbor the best route came through),
- the cost of the link to each neighbor, taken from interface configuration.
Receiving a vector
When a vector arrives from neighbor N over a link with cost c, the receiver treats every advertised cost adv as a candidate: c + adv. It then applies the rule below to each destination.
- If the destination is the receiver itself, ignore it.
- If the destination is unknown, install the route when the candidate cost is below the maximum metric.
- If the candidate is cheaper than the current route, replace the route.
- If the current route already uses N as its next hop, accept N's new cost even when it is higher.
Rule 4 is the one beginners miss. A router must believe its next hop, because that neighbor is the only source of information about the rest of the path. If N says the path got worse, the cost to the destination did get worse, and the table must follow.
INFINITY = 16
def dv_update(me, link_cost, table, neighbor, advertised):
"""table: dest -> (cost, next_hop). advertised: dest -> cost as sent by neighbor."""
changed = False
for dest, adv in advertised.items():
if dest == me:
continue
new = min(link_cost + adv, INFINITY)
cur = table.get(dest)
if cur is None:
if new < INFINITY:
table[dest] = (new, neighbor)
changed = True
elif new < cur[0] or cur[1] == neighbor:
if (new, neighbor) != cur:
table[dest] = (new, neighbor)
changed = True
return changedThe function returns whether anything changed. That return value matters, because a change is what triggers further advertisements.
Building the outgoing vector
The sender builds a separate vector for each neighbor. The only difference between vectors is how routes learned from that neighbor are treated. With plain split horizon they are left out. With poison reverse they are included at the maximum metric.
def advertise_to(me, neighbor, table, poison_reverse=True):
out = {me: 0}
for dest, (cost, next_hop) in table.items():
if next_hop == neighbor:
if poison_reverse:
out[dest] = INFINITY
else:
out[dest] = cost
return outThe reason is practical. A neighbor that is your next hop for a destination should never be told you can reach it through a path that goes via that same neighbor. The next chapters on loops show what goes wrong without this.
A three-router run
The following program connects A, B, and C in a line with link cost 1. Each round, every router sends a vector to every neighbor. All vectors are built before any table changes, which models a synchronized exchange. It reuses the two functions above, so add it to the same file.
links = {"A": {"B": 1}, "B": {"A": 1, "C": 1}, "C": {"B": 1}}
tables = {r: {} for r in links}
def exchange():
adverts = {(r, n): advertise_to(r, n, tables[r])
for r in links for n in links[r]}
changed = False
for (sender, receiver), vec in adverts.items():
if dv_update(receiver, links[receiver][sender],
tables[receiver], sender, vec):
changed = True
return changed
rounds = 0
while exchange():
rounds += 1
print(rounds)
for r in sorted(tables):
print(r, tables[r])Expected output:
2
A {'B': (1, 'B'), 'C': (2, 'B')}
B {'A': (1, 'A'), 'C': (1, 'C')}
C {'B': (1, 'B'), 'A': (2, 'B')}In round 1 every router learns its direct neighbors, because each vector contains the sender at cost 0. In round 2, B's vector lets A learn C at cost 2, and C learn A at cost 2. Round 3 changes nothing, so the loop stops. Information traveled one hop per round, so the number of rounds that change something is bounded by the network's diameter in hops.
When vectors are sent
Real protocols do not run in lockstep rounds. They combine three triggers:
- Periodic updates. RIP sends its full table every 30 seconds. This repairs lost packets and tells neighbors the sender is alive.
- Triggered updates. When a table changes, the router sends the change soon after, without waiting for the timer. This speeds up convergence, and RIP adds a short random delay to avoid synchronized bursts.
- Timeouts. If a neighbor's advertisements stop, the router eventually declares its routes invalid. RIP uses 180 seconds by default.
Message size and transport
A vector can be large. RIP carries at most 25 route entries per message, so a big table is split across several messages. RIP runs over UDP port 520 and version 2 sends to the multicast address 224.0.0.9. UDP provides no retransmission, so RIP relies on the next periodic update to repair a loss.
A single neighbor's advertisement is also unauthenticated unless you enable it. A router that accepts any vector can be fed false costs. In practice, enable RIPv2 authentication or restrict updates to the intended interfaces.
Common errors
- Forgetting rule 4. If a router accepts only cheaper routes, it keeps a stale route to a destination whose cost rose, and the route never gets corrected.
- Ignoring the link cost. Adding the neighbor's advertised cost without the cost of the link to that neighbor makes every destination look equally near.
- Not capping the metric. Without a maximum, a broken route's cost can grow without limit. The
min(..., INFINITY)in the code stops that growth at 16. - Treating the next hop as a path. The table stores only the neighbor. Two routers cannot compare paths or detect that a path passes through themselves.
Exercise
In the three-router run above, suppose C has the route A -> (2, 'B'), and B then advertises A at cost 5 to C over a link of cost 1. Does C change its table, and why?
Show answer
Yes. The candidate cost is 1 + 5 = 6. It is not cheaper than 2, but C's current next hop for A is B, and B is the neighbor that sent the advertisement. Rule 4 requires C to accept the new cost, so C's entry becomes (6, 'B').