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 basic graph terms node, edge, and weight. It also builds on the Bellman-Ford update equation from the previous chapter.
What a router sends
A distance-vector router sends each neighbor a list of pairs: a destination and the router's current cost to reach it. This list is the vector. It carries no topology and no path, only costs. A neighbor that receives it adds the cost of the link it arrived on and decides whether the route is better than what it already has.
Each router keeps a table that maps each destination to a cost and a next hop. Advertisements go out in two cases:
- Periodically, so that neighbors know the router is alive and their information is fresh.
- After a change in the table, to spread news quickly (a triggered update).
Applying one advertisement
The function below applies one neighbor's vector to the local table. It accepts a route in two cases. The first is a strictly lower cost. The second is any news from the current next hop, even if the cost went up. The second rule matters because the next hop is the only source that knows whether the path through it still works.
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 return value tells the caller whether to send a triggered update. The min caps the cost at INFINITY, so an unreachable destination stays at 16 and does not grow without bound.
Building the outgoing vector
A router does not have to send the same vector to every neighbor. The function below tailors it per neighbor. Routes learned from that neighbor are either omitted (split horizon) or advertised as unreachable (poison reverse).
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 outA worked exchange
Take routers A, B, and C in a line, with every link costing 1. A knows only itself. B receives A's vector {A: 0} and installs A: (1, A). B then sends C the vector {B: 0, A: 1}. C installs B: (1, B) and A: (2, B).
Information moves one hop per exchange. That is why the periodic timer controls how fast a distance-vector network learns.
Common errors
- Forgetting to add the link cost before comparing. The comparison must use the cost seen from the local router, not the neighbor's advertised cost.
- Ignoring increases from the current next hop. The router then keeps a stale, cheaper route and never learns that the path got worse.
- Sending the full vector on every change in a large network. This wastes bandwidth, and it is one reason RIP scales poorly.
Exercise
Router R has the table {X: (3, N1)}. Its link to neighbor N2 costs 1, and N2 advertises X at cost 1. Then N1 advertises X at cost 5 over a link of cost 1. What is R's entry for X after each message?
Show answer
After N2's message the new cost is 1 + 1 = 2, which is lower than 3, so the entry becomes (2, N2). N1's later message gives 1 + 5 = 6, but N1 is no longer the next hop and 6 is not lower than 2, so the entry stays (2, N2). If N1 had still been the next hop, the entry would have changed to (6, N1).