Multiprotocol Label Switching(MPLS) Explained | by Mayank Tripathi | Towards Data Science

This blog is divided into two subparts, in the first part we will understand what exactly is MPLS and in the second we will see how we can craft an MPLS packet using Golang.
Multiprotocol label switching(MPLS) is not a new technology. It’s been around for several years. Many of you must be familiar with how the data is transmitted in the network from one place to another in the form of network packets. These packets contain the source IP address and the destination IP address. A packet is passed through several routers in between, these routers contain the routing table which provides the next-hop information to the packet and following it the packet finally reaches its destination. This is how the IP forwarding works. It is the layer 3 protocol.
MPLS forwarding is different from IP forwarding as instead of IP addresses the forwarding is done based on the label present on the packet and hence comes the name Label Switching. It is Multiprotocol as it supports multiple protocols such as Internet Protocol (IP), Asynchronous Transport Mode (ATM) and Frame Relay network protocols. We will later see how exactly is the MPLS forwarding is done.
If you are familiar with computer networks you must know that there are 5 layers in the TCP/IP model. The MPLS layer lies between layer 2 and 3 of the model ie the Data Link and the Network Layer. That’s why it is also known as 2.5 layer protocol or “shim” protocol.
The MPLS header is of 32 bits. It contains the following information:-
Label: The label field is of 20 bits, hence the label could take values from 0 to 2^20–1, or 1,048,575. However, the first 16 label values ie from 0 to 15 are exempted from normal use as they have a special meaning.Experimental(Exp): The three bits are reserved as experimental bits. They are used for Quality of Service(QoS).Bottom of Stack(BoS): A network packet can have more than one MPLS labels which are stacked one over another. To ensure which MPLS label is at the bottom of stack we have a BoS field which is of 1 bit. The bit is high (ie value 1) only when that particular label is at the bottom of the stack otherwise its value remains 0.Time to Live(TTL): The last 8 bits are used for Time to Live(TTL). This TTL has the same function as the TTL present in the IP header. Its value is simply decreased by 1 at each hop. The job of TTL is to avoid the packet being stuck in the network by discarding the packet if its value becomes zero.
Label: The label field is of 20 bits, hence the label could take values from 0 to 2^20–1, or 1,048,575. However, the first 16 label values ie from 0 to 15 are exempted from normal use as they have a special meaning.
Experimental(Exp): The three bits are reserved as experimental bits. They are used for Quality of Service(QoS).
Bottom of Stack(BoS): A network packet can have more than one MPLS labels which are stacked one over another. To ensure which MPLS label is at the bottom of stack we have a BoS field which is of 1 bit. The bit is high (ie value 1) only when that particular label is at the bottom of the stack otherwise its value remains 0.
Time to Live(TTL): The last 8 bits are used for Time to Live(TTL). This TTL has the same function as the TTL present in the IP header. Its value is simply decreased by 1 at each hop. The job of TTL is to avoid the packet being stuck in the network by discarding the packet if its value becomes zero.
MPLS network contains Label Switch Routers(LSR). These routers are capable of understanding MPLS labels and of receiving and transmitting the labeled packet.
There are three kinds of Label Switch Routers present in the MPLS Network:-
Ingress LSR: These routers are present at the beginning of the MPLS network. Their job is to receive unlabeled IP packet and push the label on top of it.Egress LSR: These routers are present at the end of the MPLS network. Their job is to pop the label from the incoming packet and forward the packet as an IP packet.Intermediate LSR: These routers are present in between the above two routers. Their job is to receive the labeled packet, swap the label of the packet and forward it to the next hop. They are responsible for the MPLS forwarding of the packet.
Ingress LSR: These routers are present at the beginning of the MPLS network. Their job is to receive unlabeled IP packet and push the label on top of it.
Egress LSR: These routers are present at the end of the MPLS network. Their job is to pop the label from the incoming packet and forward the packet as an IP packet.
Intermediate LSR: These routers are present in between the above two routers. Their job is to receive the labeled packet, swap the label of the packet and forward it to the next hop. They are responsible for the MPLS forwarding of the packet.
Ingress and Egress are also known as Edge Routers
Thus at any particular router, either of the three steps PUSH, POP or SWAP of the label takes place.
A label Switch Path(LSP) can be defined as the sequence of label switch routers(LSR) that transmit the packet within an MPLS network. Basically, LSP is a predefined path that the packet takes during the transmission. The first LSR in an LSP is an Ingress LSR, similarly the last LSR in an LSP is an Egress router followed by intermediate LSR’s in between.
The main takeaway from here is that in IP forwarding there is no fixed path that a packet must follow whereas in MPLS forwarding we predefine the path ie the LSP’s which must be followed during the packet transmission.
So now we can easily see how an MPLS packet is forwarded in the network combining the above concepts that we have learned.
When a network packet enters the MPLS network, the ingress router places a label on top of it. This label corresponds to a particular path that the packet needs to follow ie the LSP. Different LSP’s corresponds to different label stack. The label is distributed using different protocols such as Tag Distribution Protocol (TDP), Label Distribution Protocol(LDP) and Resource Reservation Protocol (RSVP).
As in IP forwarding, each router contains a routing table similarly in an MPLS network each LSR contains Label Forwarding Information Base(LFIB). This information base guides the LSR to swap the label with its corresponding outgoing label thus allowing the packet to transmit through the network. The major takeaway here is that the router needs only to see the label on top of the incoming packet, and it does not care about the IP address (source and destination) present inside the packet, thus allowing faster routing through the network.
Following is an example of the information present in an LFIB
At the end of the LSP, the Egress LSR pops the label of the packet and the packet is routed as a normal IP packet.
This is the high level view how MPLS forwarding takes place to learn more in depth you can refer to CISCO MPLS Fundamental book
Moving on to the next part, here we will see how we can craft MPLS packets using the Golang. Why Golang? because It is highly scalable, have built-in concurrency and great support for server-side programming and also has an awesome set of libraries. We will be using GoPacket library for crafting the packet.
So here comes the code
The code is self-explanatory, we are creating individual layers for IP, UDP, Payload, MPLS, and Ethernet and stacking these layer on top of each other in the following order and returning the byte object created.
If we execute the above code we will obtain the following output
c8b302c0b91bc8b302c0b91b88470001114045000023000000000011b6c7010101010101010107d00bb8000f2b157061796c6f616400000000000000
This is the output of packet formed, we can decode the packet using online tools such as https://hpd.gasmi.net/ or Wireshark.
Decoding the above packet gives the following result as expected.
The whole code is also available at my Github Repository
Thanks for reading this article.
For more such blogs, you can follow me, so that you get notified every time I come up with a new post.
Cheers!
Also, Let’s get connected on Twitter, Linkedin, Github.