6

As I have noticed so far, there is often a switch in local area networks, and those switches route packets based on the MAC address , now if there were two switches connected to one pc in a local network, since I have not seen a MAC address set for the switch so far, how does the pc know which switch to send the packet to?

6
  • 7
    Maybe add a drawing of the topology of the network? In this comment you say a computer connected to two switches, but on Mokubai's answer you say the computer has a single network card. Commented May 22, 2021 at 14:37
  • I am a beginner, I do not have a specific topology, I was learning that this scenario came to my mind, whether it is possible for a PC with one NIC and two Ethernet ports and each port to be connected to a separate switch and all this set on one Be subnet? @SamuelYvon
    – Truth
    Commented May 22, 2021 at 16:11
  • 3
    Each ethernet port (afaik) requires a NIC. I feel like you might be mixing concepts. To be on a subnet, you require the IP layer, which means you know the MAC addresses of your peer. Commented May 22, 2021 at 16:57
  • 1
    @SamuelYvon, well, there's a bit of leeway with the terminology since the C in NIC can mean either Controller or Card. If we take NIC to mean a Network Interface Card, then two-port NICs definitely exist. Not that it matters at all for the question here how many cards the interfaces are spread over.
    – ilkkachu
    Commented May 22, 2021 at 20:08
  • @ilkkachu Yes, I mean the "network interface card".
    – Truth
    Commented May 23, 2021 at 5:31

4 Answers 4

26

It doesn't send it to a switch at all. It sends it out of whichever interface has the correct IP address and subnet or, if both are valid, whichever it believes is faster or has the fewest hops to the host.

If the network interfaces have different IP addresses and are segregated by subnet then that will effectively choose which interface the packets are sent out on.

Each interface can also be assigned a "metric" which basically states "this interface should be faster" and will be a lower number for faster interfaces. You can either let the operating system establish metrics, and therefore preferred interfaces, or you can set it yourself in the network interface properties.

The networking system in your OS might also use the TTL (Time To Live) information in TCP/IP packets to establish which link is shorter, but that is not a reliable indication of speed.

More intelligent systems may use load balancing and knowledge of recent delays on a link to adjust or override the interface metric on the fly.

9
  • I think that the OP was using the term route very loosely and was looking at an answer below the IP level.
    – davidgo
    Commented May 22, 2021 at 9:26
  • You are arguing about time, while I mean a computer with one network card between two routers (all of them in same subnet) and pinging one of the router, how does pc know which way to go? Does it hit ARP on both sides or is there a better way?
    – Truth
    Commented May 22, 2021 at 9:26
  • 1
    @Truth "a computer with one network card between two routers" doesn't make any sense. What "both sides"?
    – hobbs
    Commented May 22, 2021 at 20:29
  • 1
    @Truth How do you connect one network card to two switches?
    – gronostaj
    Commented May 23, 2021 at 8:45
  • 15
    @Truth You should be thinking in terms of network interfaces, not network cards. Two eth ports are two network interfaces. It doesn't matter if they are on a single PCB or two.
    – gronostaj
    Commented May 23, 2021 at 9:53
11

It depends what sort of a setup you have.

You could have two network interfaces on the computer, connected to two completely distinct networks. In that case, the packets would go out of the interface determined by IP routing and the system might or might not route traffic between the two networks.

E.g. here, anything going to 192.168.0.x would go out of eth0 and through switch #1. If you also have a default route via e.g. 192.168.0.1, everything else not going to 10.1.2.x would also go out of eth0 to the router.

+----------+ eth0              
|          | 192.168.0.10/24   +------------+     { other computers   }
| computer |-------------------| switch #1  |---- { in 192.168.0.0/24 }
|          |                   +------------+     {       ...         }
|          | eth1              
|          | 10.1.2.3/24       +------------+
|          |-------------------| switch #2  |---- ...
+----------+                   +------------+

The above is the same topology as the one you had in comments. With no connection between the two switches, having the same IP subnet in both interfaces would pretty much only make sense if the system acted as a bridge between the two networks, allowing all hosts on one side to reach the others. Individual hosts would be found the same way a switch finds them: by keeping track where a MAC address was seen, and flooding the traffic to all ports if the location of the destination MAC was not known.

Without bridging, there would be no straightforward way to know which network to use if an application wanted to connect to an address that exists in both places. It'd not be just about finding where a host with that address is located, but that there might be two of them.


On the other hand, if the two switches are connected externally (unlike above), it's be possible to have the two network interfaces connected to the two switches, all on the same layer 2 network and IP subnet, without bridging. This would be used for redundancy/fault tolerance or increased bandwidth.

That could be done with one IP address, if the OS handles failover between the interfaces. E.g. the Linux bonding driver can transparently fail over between two interfaces if one goes down. Traffic would then be sent along the link that's currently active.

Load balancing is not usually possible here if the two switches are independent. But if they're part of a single logical platform that supports it, you could run LACP over the two links, transparently balancing traffic over the two links. With LACP, the traffic is split based on hashing the source and destination addresses.

bond0: eth0 + eth1, 192.168.0.10/24 
+----------+               
|          | eth0 (bonded)     +------------+     { other computers   }
| computer |-------------------| switch #1  |---- { in 192.168.0.0/24 }
|          |                   +------------+     {       ...         }
|          |                         |               |
|          | eth1 (bonded)     +------------+        |
|          |-------------------| switch #2  |--------+
+----------+                   +------------+

It could also be done with two IP addresses, with the application(s) dealing with traffic distribution. This would be similar to just having two independent hosts on the network. (It may require policy-based routing to have the source IP address influence the outgoing interface used.)

+----------+ eth0              
|          | 192.168.0.10/24   +------------+     { other computers   }
| computer |-------------------| switch #1  |---- { in 192.168.0.0/24 }
|          |                   +------------+     {       ...         }
|          | eth1                    |               |
|          | 192.168.0.11/24   +------------+        |
|          |-------------------| switch #2  |--------+
+----------+                   +------------+

Note that while switches have MAC addresses, they're only used for control functions, and are not relevant for data traffic. Switches do keep a table of the host MAC addresses they've seen in each port, and use that to forward data frames to the right places. And the hosts just have routing entries saying "traffic to network N goes out of interface I", or "traffic to network M gets sent to router R via interface J", or routing entries that also take the source address into account. The host will have to use ARP or similar to figure out the destination MAC address, but that's the MAC address of the destination host, not the switch.

Also, "routing" is a network/IP/layer 3 function, not something a switch does. Switches "forward" frames, or maybe just "switch" them. (I'd happily use the latter, though perhaps some might disagree.)

It doesn't matter here if those two network interfaces are on a single (PCI / PCIe) card, on two cards, integrated on the motherboard, or connected via USB or whatever. Just that they're complete independent interfaces, and not just alternate physical connectors for the same actual interface. E.g. many switches have ports that have both a copper connector and an SFP socket, but only one works at a time. And in times of yore, a 10 Mb/s network card often had connectors for twisted pair, coaxial thin Ethernet and the D-15 AUI as alternatives.

1
  • 2
    +1 for ASCII diagram Commented May 23, 2021 at 11:53
2

In this answer I take into account additional details given in the comments:

  1. the topology: (router--------------switch----------PC-----------switch----------router)

  2. both PC's interfaces are configured in the same subnet

This is wrong.

What happens in this faulty configuration (in Linux)?

The routing table has two entries for the same subnet, one for each interface and source ip address. E.g.:

# ip route show
10.0.0.0/24 dev eth0  proto kernel  scope link  src 10.0.0.1
10.0.0.0/24 dev eth1  proto kernel  scope link  src 10.0.0.2

When you try to ping some address in that subnet only the first interface is used. Thus, you cannot reach the second router.

0

The PC learns which MAC addresses correspond to an IP address by broadcasting an ARP message. The target should respond I have IP and my MAC address is this. The PC will then keep an ARP table with entries that map IP, MAC and interface the device responded.

The PC is not bothered how the message arrives at the end point as long as some device will switch the packet to your destination.

Most switches have a MAC address and keep ARP tables. They will forward the ARP requests to all connected devices and they will also remember where the reponse comes from. Dumb hub's would typically not have memory and would just keep broadcasting the messages in all directions.

3
  • Most switches don't keep ARP tables (except for control plane functions). Routers would. ARP isn't necessary for knowing where to pass the layer 2 frames to, the MAC address is enough for that. Similarly, switches don't forward ARP requests in any special way, they're just treated like any regular layer 2 frame. Unless of course the switch is more than a simple switch, and e.g. runs something like Dynamic ARP Inspection (or the same with another name). Even then, it wouldn't keep an "ARP table" in the usual sense.
    – ilkkachu
    Commented May 23, 2021 at 10:19
  • switches swith ethernet based on mac addresses, they don't need to keep tables, but good ones do. routers route IP packets and to do that they also need to keep track of end devices. I never said switches treat arp specially, I only said they forward them. I answered, because none of the other answers including yours mentions ARP, until you edited yours. And that is the key answer to the question. LACP is completely optional
    – sleepyhead
    Commented May 23, 2021 at 11:07
  • Yep, I looked at it from a step higher at first. ARP is so automatic it's simple to ignore, and in any case, the upper levels get to decide which interface to use and send the ARP request out on. (Though ok, I suppose some system could just send an ARP request out on multiple interfaces and see which one gives an answer, but that sounds odd somehow.) But my point was that switches keep tables of MAC addresses (MAC<>port bindings), not ARP tables (which would be IP<>MAC bindings). Hubs that broadcast every frame on all ports are so rare by now they can probably be ignored.
    – ilkkachu
    Commented May 23, 2021 at 11:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .