Can switches do VLAN routing? [closed]

I have been learning about VLANs and I am wondering if switch can be used for communication between two VLANs.

Since switches support trunk links and already know the destination/source (VID) of the frame could they be used to send the frame from one VLAN to the other?

vlan network example

For example in this image above I was wondering why the router is needed if this switch already receives the VID of each VLAN. Why can't switch forward the frame to the targeted VLAN but needs the router to do it thus prolonging the transfer time of the frame to the router and back?


Solution 1:

L2 switches cannot do Inter-VLAN routing, since they only take care of L2 network stuff, that is, forward frames between links. If it is a L3 switch, then it can do IP protocol routing between VLANs.

Solution 2:

VLANs are used to create different subnets which means different broadcast domains. You need to have either a router or a Layer-3 switch in order to forward traffic between different VLANs (different subnets).

Solution 3:

Routing is a layer 3 function, while VLAN only concern Layer 2.

When a computer A want to send data to another host B, using the IP protocol it will first check if B is in the same network as himself (comparing the network portion of its own address and the destination IP address).

  • If B IP address is in the same network, A will then perform an ARP request to find the MAC address of B.
    If B is not alive or not in the same VLAN, the ARP request will not have an answer and A will not send anything else (the communication fail).

  • If B IP address belong to another IP network, then A will lookup in its routing table, found the IP address of the router C (usually its default gateway) and will send the packet to C (once again performing an ARP request to find C MAC address).

So if you don't have a router with an interface in both network you cannot have communication between two hosts in two separates IP networks.

Even if there's some kind of bridges between two VLAN (unusual but possible), or the two hosts are in the same VLAN but have IP address in different IP networks, the communication is not possible without a router, since A will not try to send a frame to B (because it's IP configuration tell it it needs a router).

Now, as other answers pointed out, if your switch is a Layer 3 switch, that means you have in one box both a switch and a router. If configured correctly, the router function of you switch will route between the different VLANs.
The main advantage of Layer 3 switches (opposed to a separate router) is that you can route between different VLANs at full wire speed (I.E. as fast as the switching process).