Sub topic 5
The Bellman-Ford update equation
This chapter assumes you know what a routing table is, how longest-prefix match picks a route, and the graph terms node, edge, weight and shortest path. It also assumes you have read the previous chapter, which introduced distance-vector routing as "routing by rumor". Here we turn that idea into the one rule every distance-vector router applies.
The equation
Let D_x(y) be router x's current estimate of the cost to reach destination y. Let c(x,v) be the cost of the direct link from x to its neighbor v. The Bellman-Ford equation is:
D_x(y) = min over all neighbors v of [ c(x,v) + D_v(y) ]
D_x(x) = 0In words: to reach y, try each neighbor, add the cost to that neighbor to the cost the neighbor claims to have, and keep the smallest total. The neighbor that wins becomes the next hop for y.
The equation relies on one property of shortest paths. Any piece of a shortest path is itself a shortest path. If the best route from x to y goes through v, then the rest of that route is v's best route to y. So x never needs the full map. It only needs each neighbor's number for y.
A router does not evaluate this equation in one pass, because it never sees all its neighbors' numbers at once. Advertisements arrive one at a time, and each can change a neighbor's number. The router therefore applies the equation incrementally, one advertisement at a time.
From equation to update rule
The code below applies one neighbor's advertised vector to the local table. The table maps each destination to a pair (cost, next_hop).
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 has three decisions.
- Compute the candidate.
new = min(link_cost + adv, INFINITY). The cap keeps costs bounded. A route whose cost reaches 16 is treated as unreachable, as in RIP. - Unknown destination. If there is no entry yet, install the candidate unless it is already unreachable.
- Known destination. Replace the entry in one of two cases. The candidate is strictly cheaper, or the advertisement comes from the current next hop.
The second case is what separates a working implementation from a plain min. If the current next hop reports a worse cost, the router must believe it, even though the new number is higher. The old number was only ever a statement about that neighbor's path. Once the neighbor stops supporting it, the entry is stale.
A worked trace
Router A has two neighbors: B over a link of cost 1, and D over a link of cost 5. This script runs three updates and prints the table after each.
INFINITY = 16
def dv_update(me, link_cost, table, neighbor, advertised):
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 changed
table = {}
dv_update("A", 5, table, "D", {"D": 0, "C": 1})
print(table)
dv_update("A", 1, table, "B", {"B": 0, "C": 2, "D": 4})
print(table)
dv_update("A", 1, table, "B", {"B": 0, "C": 16, "D": 4})
print(table)The output is:
{'D': (5, 'D'), 'C': (6, 'D')}
{'D': (5, 'D'), 'C': (3, 'B'), 'B': (1, 'B')}
{'D': (5, 'D'), 'C': (16, 'B'), 'B': (1, 'B')}Step by step:
- D's advertisement installs D at cost 5 and C at cost 6, both through D.
- B advertises C at 2, so the candidate is 1 + 2 = 3. That beats 6, so C now goes through B. B itself is installed at cost 1. For D, the candidate is 1 + 4 = 5. It ties with the current cost and does not beat it, so the entry stays.
- B now advertises C as unreachable. B is C's current next hop, so A accepts the change even though the cost rises to 16. The entry for D is untouched.
Edge cases and gotchas
- Ties keep the existing route. The strict
<means a route does not flap between equal-cost neighbors. Equal-cost multipath needs separate handling. The table here stores only one next hop. - The table keeps the infinity entry. After step 3, C sits at cost 16 via B, although D could reach C at cost 6. Nothing in this function recovers that route. A real router waits for D's next periodic advertisement, which is one reason distance-vector protocols re-advertise even when nothing has changed.
- Link failure is not handled here.
link_costis the current cost of the link toneighbor. If the link goes down, the router must itself mark every route whose next hop is that neighbor as unreachable. It cannot wait for an advertisement that will never arrive. - Costs rise slowly. Accepting an increase from the current next hop is correct. But if the neighbor's own number was derived from your route, the two of you can raise each other's costs one step per exchange. That failure is count to infinity, covered in a later chapter.
- Advertisements are trusted. The equation uses
D_v(y)as given. A neighbor that advertises cost 0 for everything attracts all traffic. This property is why distance-vector protocols are called routing by rumor.
Why the update converges
If link costs stop changing, repeated application of the update gives each router the true shortest cost to each destination. After k rounds of exchanges, every router knows the best path of at most k hops. Shortest paths in a graph of n nodes have at most n − 1 hops, so the process settles within n − 1 rounds. Cost decreases, such as a new link, spread quickly. Cost increases spread slowly, which is the subject of the convergence chapters.
Exercise
Router A has a link of cost 2 to neighbor B and a link of cost 3 to neighbor C. Its table holds X: (7, C). B advertises X at cost 4. Then C advertises X at cost 9. Using dv_update, what is A's entry for X after each advertisement?
Show answer
After B's advertisement the candidate is 2 + 4 = 6, which is less than 7, so the entry becomes (6, B). After C's advertisement the candidate is 3 + 9 = 12. It is not cheaper than 6, and C is no longer the next hop, so the entry stays (6, B). Had the entry still pointed at C, the router would have accepted 12.