Skip to content

AlexLy1/Routing-algorithms-for-router-and-switch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Routing-algorithms-for-router-and-switch

In this project, I am using Switchyard library to support my code. Switchyard is a Python library designed for learning about network behaviour and prototyping network protocols. It takes the place of an operating system's network stack and allows the user to define the behaviour between physical and application layers.

Routing Algorithm for Router

My program was mainly done in network_layer.py and some others in link_layer.py. The objective is to code up an IP-MAC address mapping table for the router, so that whenever a packet comes in, the router knows where to send the packet forward.

IP-MAC address table is stored in network layer used to find the corresponding destination MAC address by a given destination IP address. In the network layer, it basically has two functionalities, forwarding a packet and receiving a packet. These two functionalities operating in different way depends on what IP protocol is used, IPv4 or IPv6, so that IPv4 and IPv6 have different process to establish the IP-MAC mapping table.

For IPv4 sending packet, when the network gets a packet data from transport layer and needs to forward the packet to the next link layer, it needs to know what the destination MAC address is. Firstly, it will look up the IP-MAC mapping table stored in the network layer, to see if the MAC address is known. If the MAC address is known, then it will straightly forward the packet data with this MAC address to the next layer. But if the MAC address is unknown, then it has to broadcast a message to all nodes with the given IP address using ARP request, in order to gain the destination MAC address before it can forward the packet to the next link layer.

For doing IPv4 ARP request, the network needs to create a new ARP packet with special target hardware address FF:FF:FF:FF:FF:FF, and also specifies the packet type which is ARP request type. By broadcasting this ARP packet, every node in the network can receive this request and if a node has the IP address specified in the request, it will send back an ARP reply packet back to the sender containing its own MAC address.

For IPv4 receiving packet, once when a network receives a packet containing an ARP header, the network knows that a new entry can be added into its IP- MAC mapping table. The network will retrieve the source IP and MAC addresses from the ARP packet and see if this IP-MAC pair is already in its table, if not, this new pair will be added. After this, the network checks if this is the ARP request packet for itself, by checking if the target MAC address in the packet matches its own MAC address. If they are matched, the network will send an ARP reply packet back to the sender and if not, the packet will be discarded.

For IPv6 sending packet, the network doesn’t use ARP request but instead it uses NDP to find the destination MAC address. If IPv6 is aiming to send a normal packet, it needs to construct the packet with UDP header. But if IPv6 is aiming to send an NDP packet, it needs to construct the packet with ICMPv6 header. NDP packet has ICMPv6 header with solicitation specified. Solicitation specifies the icmptype, which is NeighborSolicitation, and it contains the target IP address and the sender’s MAC address. NDP packet then will be sent by the network with FF:FF:FF:FF:FF:FF broadcast address.

For IPv6 receiving packet, if the network receives a packet with UDP header (Normal packet), it will just forward the packet to transport layer, but if the packet is with ICMPv6 header, it knows that the packet is the one from broadcasting. For those packets from broadcasting, the network will retrieve the source IP and MAC addresses from the packet and check if this IP-MAC address pair is already in the mapping table and if it is not in the table, new pair is added. After this, the network will check if this solicitation is for itself, by checking if the target IP address in the packet matches its own IP address. If they are matched, IPv6 will generate an advertisement containing its own MAC address and send this ad back to the sender’s network. After this process, IPv6 network is able to know the destination address before it sends the packet.

Routing Algorithm for Switch

The objective of part B is to establish a mapping table that maps MAC address to correct switch interface, so that when switch receives a packet, it knows which interface that it should forward the packet to.

Firstly, I created a dictionary to store the MAC-INTERFACE mappings. The dictionary has key MAC address and two values, INTERFACE and TTL. Each time when there is a new packet coming into the switch, the switch will retrieve the source MAC address from the packet and pair the address with the input interface together and store into the dictionary. This is the learning part of the switch.

After having a learning table in the switch, forwarding packet has two situations. The first one is that the destination interface can be found from the table, so that the switch just needs to simply forward the packet to that interface. But another on is that the destination interface is unknown, so that the switch needs to do flooding that sends the packet to all interfaces expect the one that the packet came from.

For implementing TTL, I had a TTL attribute for each entry in the table. Each time when a packet comes into the switch and queries interface from the table, all entries in the table will update its own TTL. The update has two situations, the first one is resetting the TTL back to the original time if this entry gets used, the second one is to reduce the TTL for those unused entry in the table. Once an entry in the table has 0 TTL, it will be removed from the table.

For implementing limited size for the table, I set a global variable in the implementation representing the size of the table. Each time when a new entry is aiming to be added into the table, the table will first check if there is enough space. If space is not enough, the table will find out the entry with the oldest TTL and remove this oldest entry from the table in order to give space for the new entry.

The rule that I used to create space for new entry added to table is just a reasonable solution, and it’s possible to have two entry having the same oldest TTL, so in this case, the solution can be improved by combining the use of other solutions. For example, adding one more rule that when tie entries found, observe the amount of network traffic in terms of number of bytes for these two entries and discard the entry with less network traffic.

About

Implemented routing algorithms of routers and switches in the network using Python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages