Routing

The Problem
===========

How do we get packets from A to B?

Assumptions
===========

Hosts
- have interfaces on to a subnet

Subnets
- each host on a subnet has the ability to communicate with any other

Routers
- hosts can have multiple interfaces
- put these on different subnets
- then machine can relay packets from hosts on one of its subnets
  to hosts on the other 

So:
- can view the network as an arbitrary topology of subnets connected
  by routers

High Level Solution
===================

Addresses
- first clue given by the problem: need to be able to name A and B
- need some way to identify the interfaces that the hosts have
- assign them a number, for now assume it can be assigned randomly
  (but doesn't have to be)

Prefixes
- need some way to identify some subsection of the network
- maybe a subnet, or maybe a group of subnets
- assign "close" hosts addresses with the same "prefix"
- give the subnet/area a netmask, or prefix length, to indicate how much
  of the address all the hosts in the area have in common

Routing Tables
- so once you've got things addressed a simple and obvious way to 
  think about things pops up
- assume each node has a list of *all* the addresses,
  and where to send packets for that address
- an entry in the routing table will have an address, and then a destination
- the destination is either a directly-connected lower level address,
  or an IP router to send it to

Router Behavior
- receive packet
- look at destination address
- figure out where to send it to (local, other node on subnet)
- send it there
- gets repeated until it gets to the destination

* This approach is called Next Hop Routing, since each router only knows the
  next hop the packet is going to take, and does a look up based on the
  destination address
* (alterternative approaches would be to carry a multi-hop route in the packet,
  which could be placed either by the node or the router)

IPv4 Addressing
===============
- now we'll go more lower level, and explain how addresses actually work in
  IPv4

What Gets Addressed
- each destination on the Internet gets an address
- destinations are host interfaces, not hosts
- hosts can have multiple interfaces

Address Format
- 32 bits (4 bytes) long
- 2^32 = 4294967296 (4.2 billion) combinations
- commonly written in "dotted quads", but of course there are other reps
- ex: 18.70.0.158 (to base 2 by quad)
      /  |   \___\____________     (to base 10)
00010010 01000110 00000000 10011110 = 306577566 {address}

Expressing Prefixes
- use something called a netmask
- it's a left aligned bitmap
- sometimes see it written out as something like 255.255.0.0, but can just
  say "16 bit bitmask", or /16

Filling the Table
=================
(On Hosts)

Loopback address (one per machine) - hosts & routers
- for testing
- doesn't leave machine 
- 127.0.0.1
- uses loopback interface
- adds entry: 127.0.0.1 via lo0
  (or sometimes, 127/8)

Netmask (one per interface) - hosts & routers
- tells you what's local and what's not 
- notation for expressing netmask same as IP prefix
- what do you know - notation not the only thing the same - netmask
  implies routing information!
- adds entry: 18.70/16 via (interface names)

Default route (one per machine) - hosts & most routers
- matches as a last resort / if nothing else matches / least specific route
- typically represented by 0.0.0.0/0
- a small subset of routers (with a lot (eg: 64mb) of memory!) on the Internet
  don't have a default, since they know all the routes they need to
- either configured in machine (just like IP address) or use:

Router Discovery - RFC1256 - hosts
- if there's more than one choice for default, or if you don't want to
  fix the default router's IP address
- routers announce their existance via periodic multicast ICMP messages with
  a preference number 
- hosts use most preferred router as default
- not widely used

ICMP Redirects - RFC792
- if there's multiple routers, your default may not actually be the router
  that a packet for a particular destination ought to go through
- the router will still forward the packet to the right destination, but
  may also send you an ICMP redirect, telling you the router that the packet
  should have been sent to
- you can then add that to your routing table, and save yourself a hop
- how does the router know? it knows if it forwards the packet out the same
  interface it came in
- Only interesting between endhosts and their next-hop routers.

Routing protocols
=================
- hosts shouldn't really do this, but some do
- routers speak routing protocols with each other
- dynamic routing, adjustment during failure, optimal paths, policy routing,
  reduced administration
- Two main kinds: Internal (in an Autonomous System)
  and External (between ASes)
- Autonomous System / AS: set of routers under common admin control
- internal (optimization of path length): RIP, RIPv2, OSPF, ISIS
- external (policy): BGP
- {int-ext-route}

Static Routing
- the information that gets propagated in routing protocols needs to come
  from somewhere
- sometimes all of it gets picked up from describing the interfaces and
  netmasks, but sometimes you need to tell something about additional routes
  manually
- this is called static routing
  
Internal Routing
- amongst the routers of one AS
- routers share routing information with each other in order to find the
  "optimal" route
- "optimal" is with reference to a metric
- static routing
- distance vector (RIP, RIPv2) or link-state (OSPF, ISIS)

Distance vector (RIP/RIPv2)
- a.k.a. Bellman-Ford algorithm 
- metric is number of hops (distance)
- tell your neighbours about all the networks you know about, and how far
  away they are
- neighbours add their distance to you to the number, and keep if they
  don't have a route to the network, or your route is a lower cost route
- roughly O(N^3) computation
- Count to infinity
  - when a link breaks, sometimes an update will come around the other way,
    indicating there's still a path, which is slightly longer
  - this gets advertised back and forth, growing by one each time,
    until it gets to some maximum
  - sol'n (Split Horizon): don't announce route over link actually routing
    over
  - sol'n (Poison Reverse): even faster, announce it over that link with
    infinite metric
- can get transient loops, since slow update

Link state (OSPF, ISIS)
- tell neighbours about all the links you are connected to
- eventually all routers know about all links, and have a link-state
  database (matrix of metrics) [diagram]
- each router runs Dijkstra's Shortest Path algorithm to work out the
  optimal path to all destinations, and records the next hop
- roughly O(N^2) computation

External Routing (BGP - RFC1771)
- chooses routes acording to policy
- policies can be economic (only send routes to people who pay me),
  political (only send routes to universities), or technical (send this
  group of routes to an AS, since we're closest to them)

Addressing/Routing Dynamics
===========================

Characteristics:
  Routing Protocol Traffic
  - all of this exchanging of information that the routing protocols do
    takes place on the same wires as everything else
  - want to keep the amount of traffic low, so that bandwidth is available
    for real uses

  Stability
  - routing fluctuations can cause rapidly varying return trip times, 
    misordered packets, or even short outages / loops
  - these are all bad for end-to-end throughput

  Table Size
  - your router needs to have enough memory to store a table with all
    these addresses

  Shortest Path Computation
  - shortest path algorithms aren't cheap

  Address Availability
  - finite space of address

-> All of these argue for minimizing the number of routes/
   addresses in use

(Go back and explain why for each...)

Hierarchical Addressing
=======================

Aggregates
- provide a way to associate topologically-close groups of nodes
- within a group, nodes know the detailed location of hosts in the group
- outside of a group, nodes only know the way to the group
- basic unit of aggregation: the subnet, since all hosts on a subnet
  already sort-of know about each other
- other units: any cluster of subnets

Implementation
- prefixes
- that is, related addresses start with the same prefix
- "broad" routes for large groups of addresses will have short prefixes
- more specific routers for smaller groups will have longer prefixes
- not just a pure table lookup: now need to make "longest match"
- radix tree

Requirements
- topology needs to be hierarchical too
- the Internet is (mostly) hierarchical:
  national ISPs -> national POPs/regionals -> customers -> customers subnets
- assign addresses hierarchically!
- it's what you and the post office do:
  USA, Cambridge, Massachusetts Avenue, 77
- if someone asks you where a specific address is, you can always give them
  a general direction

Results
- nodes are *really* simple: 
  - need to know who they are
  - need to know what subnet they're on
  - everything else is just "out there", past a router on their subnet
- routers:
  - don't have to deal with every host
  - just subnets
  - or, groups of subnets i.e. "rest of the internet", "the company network"

Classfull Addressing
- What's all this Class A, B, and C business I always here about?
  - a bad idea
  - a way of encoding netmask/subnet information into the address
  - problems: inefficient allocation of address space, more routing load
  - almost gone
























