I have read a lot on how switches forward within LAN, but little about how switch talks to router.
When does a switch send packet to router ? Is it when it does not find recipient on the same LAN ?
Network Engineering Stack Exchange is a question and answer site for network engineers. It only takes a minute to sign up.Sign up to join this community
It kind of depends.
Normal primary operation for a switch is to forward frames from one host on the L2 domain to another host based on the destination MAC address. So in this sense, a switch will only forward frames to the router if the frame it received has the router's MAC address as the destination.
However if it is a managed switch, then it is also acting as a host on the network as well as a L2 device. So management traffic that needs to be routed (i.e. to hosts on a different subnet/network) will be sent by the switch to the router (assuming correct routing/default gateway are in place).
Based on the way you phrased your question, I think the important point to know is that the decision of whether the packet should go to the router or not is made by the switch, but by the sending host on that LAN.
The host (PC or router) determines that the destination is not on the local network, and that it needs to be forwarded to the router. If so, the host sets the destination MAC address to that of the router. The switch then forwards packets based on the MAC address. It doesn't know whether it's a router or something else.
Switches come in different flavours JavaDeveloper. Why do you need a switch? Because you want more than 2 hosts to share a common communication medium. If you have 2 PCs and they are not far away from each other you could connect an ethernet cable between them, assign IP addresses and start communicating. What if you have 15 PCs? Then you want to have a shared medium, a meeting ground where all are available.
Let's carry on with the same analogy. You plugged in your switch, did the cabling, employed an IP distribution mechanism and assigned each a 192.168.1.x/24 address. You will see that they can reach other. You can create a shared folder on one and send/receive files from others, you are connected.
Now imagine you wanted you and your neighbours' PCs to communicate, he uses a different subnet plus you don't have enough ethernet ports. You now need another mechanism that will route the traffic between you two. You found a router with two interfaces and connected a cable from your switch to one interface and another cable from your neighbours's switch to the oher interface. Why did you plug this cable to switches but not a printer? Because everybody is there and available. If you see the world from the router's point of view, I have 2 networks on each side, one speaks German, other one speaks French. In order to be able to let you 2 communicate it has to speak both languages. That's why on each router interface, you assign an IP address from each subnet to the correct side (yours will be 192.168.1.x/24). He is your virtual gate to your neighbour's house. If your network needs to communicate your neighbours, you have to knock on the virtual door. The address you just assigned at the router interface is your network's gateway to the other network which makes sense right? If you have a PC that only communicates with your printer, you don't need to assign him a gateway address why should you? He always stays at home.
When does a switch talk to router? When it needs to contact other adjacent or reachable networks.
(as other people wrote down, there are layer 3 switches thatt can be assigned IP addresses from other networks enabling you to meet everybody there but I won't talk about that. I wrote long, please don't get me wrong, there are other people who can make use of this thread)
Hope this helps.
A normal (layer 2) switch sends frames from one port (in) to another port (out) based on the destination MAC address in the frame. It wil search its MAC table
The host will package the packet/frame with the MAC address of the router, based on the knowledge of whether the desired endpoint is on the same subnetwork (using is mask). If it is not on the subnet or network segment, it will then forward the packet/frame to the default gateway on the segment (router), which will first go through the connected switch. The switch knowing that the MAC address is not connected to the device on the incoming interface will then query the CAM table and forward the frame out of the necessary interface if MAC info is present for the desired gateway (router). If not, then the frame will be multicast out of all ports with an arp requesting the desired mac address until it receives a response from the DGW (router), which will then begin the process of establishing a link and the necessary communication. That's it in a nutshell. Hopefully, I didn't miss any important details.