The function of a router is to route IP packets to the destination according to the destination address in the IP packet. In order to do this the router needs to build and maintain a routing table containing all the destination networks or sub networks available. In order for a router to build a routing table the router must be configured with a routing protocol, which is essentially a common language used by all the routers in the network so they can inform each other about available routes. Another option is to hard code a table by means of configuring static routes, as opposed to the dynamic routes determined through the use of the routing protocol.
Once a table is in place, the routing device then needs to be perform patch determination, which is done by examining the IP Packet and performing some simple mathematics on the 32 bit binary destination IP address. This will inform the router of the network that the IP address belongs to. The device then looks for a match for that network in the routing table and forwards the packet to the next hop router down the determined path. Other routing devices perform the same actions until the packet reaches the destination network.
An Ethernet switch has 3 basic functions which are:
Forwarding and Filtering of Ethernet Frames
A switch is programmed to read the incoming Ethernet frames and identify a source MAC Address with the port through which it arrived. The switch builds a table containing a mapping of device MAC Addresses and the ports to which they are associated. This means that when Ethernet frames come from other switches, routers or devices, a quick lookup in the MAC Address Table will allow the switch to deliver the incoming frames to through the correct port to the correct end user device.
The forwarding of frames is determined by the matching entry in the MAC Address Table, which will allow the switch to forward the frame correctly.
All Ethernet switches run a protocol known as STP (Spanning-Tree Protocol) which is designed to detect any loops to ensure that frames do not circulate without finding a destination. This can occur when there are multiple connections between switches, which allows for a failed connection, but can also create loops.
What is the difference between a Layer 2 switch and a Layer 3 switch? Well a Layer 2 switch operates essentially as we have described above, Layer 2 coming from the layer of the OSI Model or TCP/IP model at which Ethernet Frames operate. A Layer 3 switch is fundamentally a device that operates in the normal manner, but has the added benefit of a routing engine. It is essentially a switch and a router, the purpose of which is to allow the switch to make local routing decisions, particularly when VLANs (Virtual Local Area Networks) are being used. This allows switches to be able to connect to different networks or sub networks on a port by port basis.
So a router routes and a switch switches, unless it is a layer 3 switch when it can also route.
Routing and Switching forms the basis of a lot of our networking courses.