The idea: look up the address once, then follow labels

In a plain IP network every router reads the destination address and does a longest-prefix match on its routing table. MPLS (Multiprotocol Label Switching) does that lookup only once, at the edge. The first router puts a short label in front of the IP header. The routers in the core read only that label, find it by exact match in a small table, swap it for a new one and pass the packet on. The last router takes the label off and the packet is plain IP again.

Host A, PE1, P1, P2, PE2, Host B in a row. The packet is plain IP from Host A to PE1, carries label 24 from PE1 to P1, label 18 from P1 to P2, and is plain IP again from P2 to PE2 and to Host B. PE1 does a longest-prefix match and pushes 24, P1 swaps 24 to 18, P2 pops, PE2 does an IP lookup. Below, LDP label mappings 3, 18 and 24 flow from right to left.
Push at the ingress, swap in the core, pop one hop before the egress; the labels themselves were handed out from right to left by LDP.

The routers have names. The edge routers are label edge routers (LERs): PE1 is the ingress and PE2 the egress for this packet. The core routers P1 and P2 are label switching routers (LSRs). The chain of labels from PE1 to PE2 is a label switched path (LSP). (PE and P are the provider-edge and provider names that operators use.)

FEC and the push at the ingress

A FEC (forwarding equivalence class) is a group of packets that are forwarded the same way. Here a FEC is a destination prefix: every packet to 203.0.113.0/24 is in the same FEC. PE1 does one longest-prefix match, finds the FEC and pushes the label bound to it, 24. From then on nobody in the core looks at the IP address (Demo: one packet through the LSP).

The shim header

The label travels in a 4-byte label stack entry, also called the shim header, between the Ethernet header and the IP header. The Ethernet type changes from 0x0800 (IPv4) to 0x8847 (MPLS). The entry has four fields:

  • label, 20 bits. Values 0 to 15 are reserved: 0 is IPv4 explicit null, 3 is implicit null.
  • TC, 3 bits, traffic class for quality of service (it used to be called EXP). The page keeps it at 0.
  • S, 1 bit: 1 on the bottom entry of the stack, 0 on the others.
  • TTL, 8 bits. PE1 copies the IP TTL minus 1 into it; each LSR lowers it by 1; when the label is popped the value goes back into the IP header. So Host B sees TTL 60 after four routers, with or without labels.
Top: a 32-bit label stack entry with label (bits 0 to 19), TC (20 to 22), S (23) and TTL (24 to 31). Bottom: a VPN packet between PE1 and P1: Ethernet type 0x8847, label 25 with S 0, label 40 with S 1, IP header to 192.168.20.5, data. Label 25 is read by P1 and P2, label 40 only by PE2, and the IP header is never read in the core.
One 32-bit entry per label; a stack is several entries in a row, and the core reads only the top one.

LSRs swap labels with the LFIB

Each LSR has a LFIB (label forwarding information base): in-label → out-label and next hop. P1 reads label 24, finds in 24 → swap 18, to P2 and sends the packet on with 18. This is an exact match on one small number, not a search for the longest prefix. A label means something only on one link: 24 between PE1 and P1, 18 between P1 and P2, for the same FEC.

Penultimate hop popping

The egress PE2 has to do an IP lookup anyway, to find Host B. If it also had to look up a label first, it would do two lookups. So PE2 asks for label 3, implicit null. Label 3 never appears on the wire: it tells P2 "pop the label before you send to me". This is penultimate hop popping (PHP): the router before the last one removes the label, and PE2 gets plain IP.

PE2 can ask for label 0, explicit null, instead. Then P2 swaps to 0, and PE2 looks up label 0, pops it and then does the IP lookup: one extra lookup, but the TC bits survive to the last router (Demo: no PHP, explicit null).

LDP hands out labels hop by hop

Labels come from LDP, the Label Distribution Protocol. Neighbours find each other with Hello messages on UDP port 646 and then keep a session on TCP port 646. Each router picks its own labels for the FECs its IGP (for example OSPF) knows, and tells its neighbours with Label Mapping messages. The receiving router puts that label as the out-label of its LFIB entry. In the demo the mappings flow from the egress back: PE2 sends 3, P2 picks 18 and sends it, P1 picks 24 and sends it, PE1 binds the FEC to "push 24" (Demo: LDP hands out the labels).

Real LDP sends each mapping to every neighbour, not only the upstream one (downstream unsolicited), and most routers keep all of them (liberal retention), so a new path is ready at once when the IGP changes its mind.

Compared with hop-by-hop IP lookups

Without MPLS, each of the four routers reads the IP header and finds that 0.0.0.0/0, 203.0.0.0/16 and 203.0.113.0/24 all match 203.0.113.9; the longest wins (Demo: the same packet with plain IP lookups).

Packet to Host BPrefix lookupsLabel lookupsTTL at Host B
Plain IP routing4 (every router)060
MPLS with PHP (label 3)2 (PE1, PE2)2 (P1, P2)60
MPLS with explicit null (label 0)23 (P1, P2, PE2)60
VPN, two labels2 (both in VRF blue)3 (P1, P2, PE2)60

Today's routers do longest-prefix matches in hardware just as fast, so speed is no longer the main reason for MPLS. The reasons now are what labels make possible: the core needs no routes for customers' addresses, an LSP can follow a path the IGP would not choose (traffic engineering), and one network can carry many separate VPNs.

A label stack: VPN label under the transport label

A packet can carry several labels. In a BGP/MPLS IP VPN, customer blue's site B uses 192.168.20.0/24, a private range that other customers may use too. PE2 picks a VPN label, 40, for blue's route and tells PE1 over MP-BGP. PE1 looks the packet up in blue's own table (a VRF), pushes 40 with S 1, then pushes the transport label 25 for PE2's loopback 10.255.0.4/32 on top, with S 0. P1 and P2 swap and pop only the top label; label 40 rides along unread. PE2 reads 40, knows the packet is blue's, and does the IP lookup in VRF blue (Demo: VPN label under the transport label).

See also How a Router Forwards a Packet for the longest-prefix match and TTL work that MPLS moves to the edge, OSPF Inside, BGP Between for where the routes that LDP labels come from, and How a VPN Works for a tunnel that encrypts instead of labelling.

What the page leaves out

LDP's own details (discovery, session setup, Label Withdraw and Release, ordered vs independent control), RSVP-TE and Segment Routing, which build LSPs another way, MP-BGP itself, per-prefix vs per-VRF VPN labels, the pipe TTL mode that hides the core from traceroute, TC-based queueing, load sharing over several LSPs, fast reroute, and MPLS on links other than Ethernet.

On the mind map

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeRouter forwardingRouter forwardinglongest prefix match picks the next hopMPLS label switchingpush at edge, swap in core, pop before lastVRRP / HSRP failoverVRRP / HSRP failoverBackup takes over the gateway in 3.6 sSDN and OpenFlowSDN and OpenFlowcontroller writes rules, switch looks upTracerouteTracerouteTTL 1, 2, 3: each hop sends Time ExceededNATNATmaps private IP:port to one public IPFTP active vs passiveFTP active vs passivePORT breaks behind NAT; PASV connects outIPv4 vs IPv6IPv4 vs IPv632 vs 128 bits; SLAAC, no NAT neededIPv6 Neighbor DiscoveryIPv6 Neighbor DiscoveryNS/NA to solicited-node group, not broadcastAnycast and BGPAnycast and BGPsame prefix announced from many sitesOSPF and BGP policyOSPF and BGP policyPolicy first, then path, then IGP costDistance vector vs link stateDistance vector vs link stateRIP shares distances; OSPF floods the mapSubnets and CIDRSubnets and CIDR/24 = mask: same subnet? ARP, else routerVLSM PlanningVLSM PlanningLargest first: no gaps, one summary routeRouter queues and AQMRouter queues and AQMFull buffers add delay; drop earlyDelay, loss, throughputDelay, loss, throughputSlowest link sets pace; full buffer dropsOpen the Networking mind mapNetworkingpackets hop between networksIP & routingRouter forwardingRouter forwardinglongest prefix match picks the next hopMPLS label switchingpush at edge, swap in core, pop before lastVRRP / HSRP failoverVRRP / HSRP failoverBackup takes over the gateway in 3.6 sSDN and OpenFlowSDN and OpenFlowcontroller writes rules, switch looks upTracerouteTracerouteTTL 1, 2, 3: each hop sends Time ExceededNATNATmaps private IP:port to one public IPFTP active vs passiveFTP active vs passivePORT breaks behind NAT; PASV connects outIPv4 vs IPv6IPv4 vs IPv632 vs 128 bits; SLAAC, no NAT neededIPv6 Neighbor DiscoveryIPv6 Neighbor DiscoveryNS/NA to solicited-node group, not broadcastAnycast and BGPAnycast and BGPsame prefix announced from many sitesOSPF and BGP policyOSPF and BGP policyPolicy first, then path, then IGP costDistance vector vs link stateDistance vector vs link stateRIP shares distances; OSPF floods the mapSubnets and CIDRSubnets and CIDR/24 = mask: same subnet? ARP, else routerVLSM PlanningVLSM PlanningLargest first: no gaps, one summary routeRouter queues and AQMRouter queues and AQMFull buffers add delay; drop earlyDelay, loss, throughputDelay, loss, throughputSlowest link sets pace; full buffer drops
This page on the Networking mind map · 83 pages in 10 branches · open it to see the whole map and test yourself in recall mode.