Modeling the Internet from the scratch: Link-layer, LAN, Switch

Modeling the Internet

Although I took the CS network course when I’m an undergraduate and read the books related to networks, I still cannot see the whole picture of the network and still don’t understand very well how each layer interacts. So I decided to implement each layer one by one to deeply understand each layer and protocol.

vnet’ is the name of this hobby project. The main purpose of this project is to implement each layer in software and understand how each layer works interact. But I think after I built this, there are various ways to extends this. One thing is a developer can implement his/her own protocol and adapt to ‘vnet’. Then users who joined in ‘vnet’ could communicate with that protocol.

Link-layer Overview

So started from link-layer. The main concern of the link-layer is to transport data from one node to an adjacent node over a link.

In this post, the abstracted word node is referring to any device that runs a link-layer protocol. So this could be host, router, switch.

Link is communication channels that connect with adjacent nodes and in order to transfer data from source node to destination node, it must be moved over link.

The boundary between the host and the link is called an interface. The host typically has only a single link and the router has multiple interfaces, one for each of its links.

There are other important services for the link-layer. Those could be medium access control (MAC) for reliable delivery of data and error detection and correction on the bit level.

Most of the link-layer is implemented on hardware

link-layer on physical view
link-layer on physical view

Such services we listed above most of them are implemented on hardware especially on the network adapter. Like this, most of the protocol such as MAC and error detection is for a hardware problem. But I’m still want to see how on link-layer, each node could send data to an adjacent node over the link.

So I decided to abstract them all away! I assume that there’s no multiple access problem, no error on the bit level.

For abstracting all those things, defined interface. Card represents a network adapter and the interface will own it. In the project, the concrete type of Card implementing this interface use UDP for ease.

Define the link-layer network topology

To model our link-layer network, first, we need to define basic topology things. We can think that the link-layer network could be represented with the concept of “link” and its two “endpoint” and machine-thing has one or more “endpoint”.

With Transmitter we can send frame data to the link. And Interface extends this with the Endpoint interface. Also when we talked about the MAC address, the MAC address is assigned to each network interface and Host owns it. As a result, Interface has Address() a method that returns MAC address.

In the Host struct, there are several important components besides Interface.

  • NetHandler : With NetHandler, when Host receives frame from Interface , after doing its own business logic, we can pass the network-layer payload bytes to the network-layer.
  • FrameEncoder: When we want to send frame data to another node, we need to serialize frame data into bytes
  • FrameDecoder: When we receive frame data bytes from another node, we first need to deserialize it into frame data structure using FrameDecoder


There’s one important concept left when we talk about LAN: “switch”.

The switch has two key features. One is “filtering” which is a function that determines whether the frame it receives should be forwarded or just dropped. And the second one is “forwarding” which sends the frame it receives from one of the switch’s ports to the outgoing link.

All these features are done by a “switch table”. The switch table contains the following information:

  • MAC address
  • The switch port that leads toward that MAC address
  • The timestamp at which the entry was created.

So we could struct our switch and switch table like this:

Switch contains its own port list and switch table. Remember that switch do not have MAC addresses with their ports that connect to hosts and routers. So we need to model the port as such.

Port has its own id, not link-layer address so we need to register its own id. The concrete port instance is not initiated with its own id. The registerer such as a switch or other client assigns an id to each port then attached to the switch.

Filtering, Forwarding Algorithm

Then how the switch can filter and forward the frame to other hosts or router? Let’s take a simple scenario: A frame with destination address “62-FE-E7–11–89-A3” arrives at the switch on port with the id of “1”.

First, the switch updates its switch table with the following tuple data:

  • The MAC address in the frame’s source address
  • The interface from which the frame arrived
  • The current time.

Then there could be three possible scenarios:

  1. If there is no entry for MAC address “62-FE-E7–11–89-A3” in the table. In this case switch broadcast receiving frame to the ports which it has except the port where it receives the frame.
  2. If there is an entry for MAC address “62-FE-E7–11–89-A3” and port id “1”, in this case, because this means that the destination node is located on port “1”, there’s no need to forward the frame once again. As a result, the switch performs the filtering function by discarding the frame.
  3. If there is an entry for MAC address “62-FE-E7–11–89-A3” and port id is NOT “1” (let’s say the entry has port id “2”). In this case, the switch forwards the frame to port id “2”

This is the basic algorithm of how the switch filter and forward frame to the other nodes.

The switch deletes an entry in the table if no frames are receives with that address as a source address for some times. We call it an aging time. With this mechanism, we could handle the scenario when the host machine is replaced with another so that it has a different network adapter, MAC address, the switch could delete outdated data.

Also, we can see why the switch is “transparent”. It is because the switch is doing its job without the host or router having to know the switch link-layer address. So when we move on to the next level “network-layer”, we cannot see the presence of a switch.

Construct the link-layer network

So far, we’ve seen all the basic components of LAN from the view of the link-layer level. Let’s build a simple link-layer network.

sample link-layer network diagram
sample link-layer network diagram
  • In this network, there are two switches (“switch1”, “switch2”) and three hosts (“host1”, “host2”, “host3”).
  • On “switch1”, there are three ports, and each connected with “host1”, “host2”, “switch2”.
  • On “switch2”, there are two ports, and each connected with “switch1” and “host3”.

First, we need to setup five node.

construct the network — define node

Then we need to create the endpoint (interface or port) and attach it to its node: Interface for the host, the port for the switch.

And underneath, all the frame transmission is done by UDP we need to specify UDP port (4001–40008).

Each host has one interface with the variable name (intf1intf2intf3). And each switch has multiple ports with the variable name (sp11sp12sp13 for switch1, sp21sp22 for switch2)

construct the network — setup interface and attach it to node

Finally, we connect two endpoints with the link. To match with the diagram, we need to connect (intf1sp11), (intf2sp12), (sp21sp13) and (intf3sp22).

Then using these, we can simulate various cases of link-layer frame transmission tests.

construct the network — 3: attach the link to interfaces

You can find it on github

Notify of
Inline Feedbacks
View all comments