3

I know that I could achieve the desired result with an Ethernet switch, but I'm curious if I can get this with some cheaper solution.

The idea is as follows:

  • Rapsberry Pi has two Ethernet ports - one is the built-in port and the other will be some cheap USB-Ethernet adapter

  • one of the ports is connected directly to my local router and receives a DHCP IP address. Let's say it is 192.168.1.2.

  • the other port will be attached to some other device. I need this device to think that it is attached directly to the same router and to receive a DHCP IP address from the router. Let's say, the address is 192.168.1.3.

Essentially, I'd like RPi to behave like an Ethernet switch, which requests one IP for itself and then routes other requests from/to the other device.

Here is an image which explains how I'd do it with a switch:

RPi with some device and a switch

And here is how I'd like it to work:

RPi with some device and an Ethernet dongle

Now imagine that I have five (or more) such pairs "RPi plus Device X" and you'll understand why I would want to get rid of the switch, if possible - I could buy five USB-Ethernet dongles instead of five 3 port switches, which would require separate power supply and two additional wires.

Are there any Linux configuration options to make this setup to work with RPi's Ethernet port and an Ethernet dongle?

migrated from raspberrypi.stackexchange.com Jan 28 '15 at 14:09

This question came from our site for users and developers of hardware and software for Raspberry Pi.

2

You should be able to achieve this by setting up a bridge between your two network interfaces.

Assuming your integrated ethernet interface is eth0 and that the on via the USB adapter is eth1, a configuration like this in /etc/network/interfaces should work (provided you've installed the bridge-utils package and that your kernel supports it):

auto lo br0
iface lo inet loopback

iface eth0 inet manual

iface eth1 inet manual

iface br0 inet dhcp
    bridge_ports eth0 eth1

If you run ifconfig, you should then see a br0 interface (probably with the MAC address of eth0 if I remember well). This one will effectively be the merge interface of your RPi and get its IP address from your router.

The traffic coming from your other device should flow through the bridge, and it should get its IP address too. If the traffic doesn't go through, you may need to allow it via iptables. You may also be interested in this article.

(As a side note, this wouldn't necessarily work with wireless LAN adapters, since many cannot use multiple MAC addresses at the same time.)

  • Not totally related, but I know my vpn that uses bridge requires iptables for vpn in /proc/sys/net/ipv4/conf/*; do echo 0 > $vpn/accept_redirects; echo 0 > $vpn/send_redirects; done iptables -A FORWARD -i br0 -o br0 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A FORWARD -i br0 -o br0 -j ACCEPT # End VPN Settings – geoffmcc Jan 28 '15 at 21:59
  • Thanks, I'll give it a try. The article with DHCP information made me thinking, though - if I have multiple pairs with "RPi+DeviceX" connected to a single router, is it still safe to turn the 30 second delay off, or my setup counts as complicated case with multiple bridges and it might have issues if the delay is turned off? – JustAMartin Jan 29 '15 at 7:50
  • I don't think this should be an issue, as long as the devices don't share the same MAC addresses (which they shouldn't anyway). – Bruno Jan 29 '15 at 7:55
  • Thanks, I tried it today, it works. I added also bridge_stp off bridge_fd 0 bridge_waitport 0 but not sure if they are needed. – JustAMartin Feb 6 '15 at 20:13
  • The only problem is that my bridge br0 disappears as soon as I detach any of the Ethernet cables and after I attach them back, I have to do ifup br0 to make it work again. Is there any simple way to make my br0 to survive disconnects? – JustAMartin Feb 6 '15 at 21:26
1

Your best bet is to set the Pi with a static ip.

From there you want to install a dhcp server

 apt-get install isc-dhcp-server 

You then edit the configuration file at

 /etc/conf.d/dhcp 

which tells the dhcp server which interface on which to listen with this line:

 DHCP4_ARGS="... eth0" 

You then start the server daemon with:

 sudo /etc/rc.d/dhcp4 start 

Now any clients that broadcast for an IP connected to eth0 will be managed by the dhcp server.

You may need to swap eth0 with eth1 to meet your needs

  • Thanks, but there is one problem with this approach - now I'll have two DHCP servers - one on the local router and another one on the Pi. I actually wanted to have only one DHCP server on the router and make RPi capable to not only receive its IP from the router, but somehow make the router see the physical device attached to another Ethernet port on RPi. I guess, I'll draw a diagram to make it more clear. – JustAMartin Jan 28 '15 at 20:04
  • 1
    @martin okay, I get what your looking for. First thing I can think of is to bridge the two adapters, but I'm not 100% sure devices connected to eth1 will show in router. Gonna do some more looking. Plus I have a pi. I don't have another eth adapter, but I should be able to bridge eth0 and wlan0 and then see if I connect something to eth0 if it will show on router. Kinda backwards from what your trying to do, but I don't have usb eth – geoffmcc Jan 28 '15 at 21:35
1

Be aware that the Pi USB+Ethernet comes from a single dumb on-board chip that connects to a SINGLE port on the Pi GPU = i.e. the USB Ethernet dongle will NOT just 'link direct' to built in Ethernet

Thus, when you attempt transfer data from one interface to the other, throughput is crippled as each byte has to be read by the CPU, wait whilst the CPU tells the GPU to select the other interface (on the external chip) after which the CPU can write the data back to GPU and out onto the other interface.

Of course it's more efficient to read & write 'blocks' of data at a time, however whilst the Pi is sending a block of data out on one interface, the other is disconnected so anything coming in will be 'dropped' and have to be retransmitted ...

Depending on how the Ethernet & USB 'dongle' drivers are coded, chances are your 100mbs Ethernet will be reduced to way less than 10 mbs (I've seen some reports in the kbs range)

  • Thanks for the info, yes, I considered this issue. But for my case expected transfer rates between both devices will be less than 1 mbs. I already tried the bridge and it was working stable enough. – JustAMartin Feb 15 '15 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.