User avatar
thagrol
Posts: 2975
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Howto: ethernet gadget on Pi4B USB C

Mon Jul 15, 2019 3:04 pm

In case anyone wants to try it, here's how I got a 4B running as a USB ethenet gadget.

Firstly, this only works on the USB C port not on the USB 2 or USB 3 ports. Because of this you may have to find another way to get power into the 4B.
  1. Requirements:
    • Raspberry Pi 4B (any RAM size) and asociated cables, SD card etc.
    • USB A male to USB C male cable.
    • Another computer to act as the USB host. I've not tried it but I guess you could loop back to the USB 2/3 ports on the 4B
  2. Procedure:
    1. On the 4B, edit /boot/config.txt and add

      Code: Select all

      dtoverlay=dwc2
    2. On the 4B, edit /boot/cmdline.txt adding

      Code: Select all

       modules-load=dwc2,g_ether
    3. Reboot
    4. Log in to the 4B and run

      Code: Select all

      ifconfig -a
      You should see a new usb0 network interface.
    5. If you haven't already, connect to your USB host with the USB A to USB C cable. The USB host will show a new USB network adapter.
    6. Configure both enternet interfaces as required. DHCP, static IP addresses, bridging, routing, etc
  3. Gotchas:
    • MAC addresses used for each end of the USB link are randomly generated each time the g_ether module is started. This will not cause problems if using static IP addresses but will when using DCHP assigned one (especially if your DHCP server sets address based on the MAC address).

      Fixed MAC addresses can be set by appending this to /boot/cmdline.txt:

      Code: Select all

       g_ether.host_addr=<MAC addres> g_ether.dev_addr=<MAC addres>
      MAC addresses are expected to be unique to an interface.

      I have a script here: https://github.com/thagrol/usb-gadget that can generate suitable MAC addresses based on the serial number of the Pi it's running on. RUn as

      Code: Select all

      sudo ./set_id.py --test
      and copy the generated MAC addresses into /boot/cmdline.txt
    • The content of /boot/cmdline.txt must be on a single line with a linux line ending. It's best to edit it on a Pi with nano/vi/leafpad/etc rather than on windows.
    • From a network/ethernet point of view it doesn't matter which end of the link is running as a USB master or slave. You can have DHCP servers etc running on the 4B and have them available to the USB host.
    • The normal (USB A) ports on the 4B are unaffected by this.
    • If the 4B does not switch to device mode, this can be forced by changing your config.txt entry to

      Code: Select all

      dtoverlay=dwc2,dr_mode=peripheral
      and rebooting.
[Editied to fix a coupls of spelling errors and typos]
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

ejolson
Posts: 5217
Joined: Tue Mar 18, 2014 11:47 am

Re: Howto: ethernet gadget on Pi4B USB C

Mon Jul 15, 2019 10:47 pm

thagrol wrote:
Mon Jul 15, 2019 3:04 pm
In case anyone wants to try it, here's how I got a 4B running as a USB ethenet gadget.

Firstly, this only works on the USB C port not on the USB 2 or USB 3 ports. Because of this you may have to find another way to get power into the 4B.
  1. Requirements:
    • Raspberry Pi 4B (any RAM size) and asociated cables, SD card etc.
    • USB A male to USB C male cable.
    • Another computer to act as the USB host. I've not tried it but I guess you could loop back to the USB 2/3 ports on the 4B
  2. Procedure:
    1. On the 4B, edit /boot/config.txt and add

      Code: Select all

      dtoverlay=dwc2
    2. On the 4B, edit /boot/cmdline.txt adding

      Code: Select all

       modules-load=dwc2,g_ether
    3. Reboot
    4. Log in to the 4B and run

      Code: Select all

      ifconfig -a
      You should see a new usb0 network interface.
    5. If you haven't already, connect to your USB host with the USB A to USB C cable. The USB host will show a new USB network adapter.
    6. Configure both enternet interfaces as required. DHCP, static IP addresses, bridging, routing, etc
  3. Gotchas:
    • MAC addresses used for each end of the USB link are randomly generated each time the g_ether module is started. This will not cause problems if using static IP addresses but will when using DCHP assigned one (especially if your DHCP server sets address based on the MAC address).

      Fixed MAC addresses can be set by appending this to /boot/cmdline.txt:

      Code: Select all

       g_ether.host_addr=<MAC addres> g_ether.dev_addr=<MAC addres>
      MAC addresses are expected to be unique to an interface.

      I have a script here: https://github.com/thagrol/usb-gadget that can generate suitable MAC addresses based on the serial number of the Pi it's running on. RUn as

      Code: Select all

      sudo ./set_id.py --test
      and copy the generated MAC addresses into /boot/cmdline.txt
    • The content of /boot/cmdline.txt must be on a single line with a linux line ending. It's best to edit it on a Pi with nano/vi/leafpad/etc rather than on windows.
    • From a network/ethernet point of view it doesn't matter which end of the link is running as a USB master or slave. You can have DHCP servers etc running on the 4B and have them available to the USB host.
    • The normal (USB A) ports on the 4B are unaffected by this.
    • If the 4B does not switch to device mode, this can be forced by changing your config.txt entry to

      Code: Select all

      dtoverlay=dwc2,dr_mode=peripheral
      and rebooting.
[Editied to fix a coupls of spelling errors and typos]
Did you need to cut any power wires in the USB C cable to prevent backpowering from the Pi up to the host port?

User avatar
thagrol
Posts: 2975
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Howto: ethernet gadget on Pi4B USB C

Mon Jul 15, 2019 11:02 pm

ejolson wrote:
Mon Jul 15, 2019 10:47 pm
Did you need to cut any power wires in the USB C cable to prevent backpowering from the Pi up to the host port?
I haven't done so but see my post here: https://www.raspberrypi.org/forums/view ... 8#p1500930
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Notupus
Posts: 51
Joined: Tue Jun 19, 2018 9:57 am

Re: Howto: ethernet gadget on Pi4B USB C

Tue Jul 16, 2019 7:26 am

Can I do other USB gadgets such as a hid keyboard or a flash drive?

User avatar
thagrol
Posts: 2975
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Howto: ethernet gadget on Pi4B USB C

Tue Jul 16, 2019 11:49 am

Notupus wrote:
Tue Jul 16, 2019 7:26 am
Can I do other USB gadgets such as a hid keyboard or a flash drive?
I've not done so but I would expect them to work.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

dominikp
Posts: 12
Joined: Mon Dec 17, 2018 9:54 am

Re: Howto: ethernet gadget on Pi4B USB C

Tue Mar 24, 2020 11:51 am

Oh man, this topic is HUUUGE news to me because so far AFAIK it was possible only on RpiZ!
But two questions first ...
1. After enabling this, USB on RPI cant be used for things like storage right? Or can it?
2. Does bandwidth on USB3.0<->USB3.0 connectionon go full speed (or close to it) or the driver itself caps this somehow? Had anybody measured this ?

Im using RPi3 for turning on and off my small NAS computer (with GPIO pins, didnt like WoL solution). I would love to replace it with RPI4 and have backup drive 2.5" attached to it and do backups throught 5 times faster USB3.0 instead of ethernet. Also, bridging Ethernet with USB@eth on RPI would allow me to get rid of ethernet attached directly to my NAS.

But this would be possible as long as answers for my 2 questions are positive.

ejolson
Posts: 5217
Joined: Tue Mar 18, 2014 11:47 am

Re: Howto: ethernet gadget on Pi4B USB C

Tue Mar 24, 2020 2:25 pm

dominikp wrote:
Tue Mar 24, 2020 11:51 am
Oh man, this topic is HUUUGE news to me because so far AFAIK it was possible only on RpiZ!
But two questions first ...
1. After enabling this, USB on RPI cant be used for things like storage right? Or can it?
2. Does bandwidth on USB3.0<->USB3.0 connectionon go full speed (or close to it) or the driver itself caps this somehow? Had anybody measured this ?

Im using RPi3 for turning on and off my small NAS computer (with GPIO pins, didnt like WoL solution). I would love to replace it with RPI4 and have backup drive 2.5" attached to it and do backups throught 5 times faster USB3.0 instead of ethernet. Also, bridging Ethernet with USB@eth on RPI would allow me to get rid of ethernet attached directly to my NAS.

But this would be possible as long as answers for my 2 questions are positive.
The data lines on the USB-C port in the Pi 4B run at USB2 speeds. In practice this turns out to be more than 5 times slower than gigabit Ethernet.

User avatar
thagrol
Posts: 2975
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Howto: ethernet gadget on Pi4B USB C

Tue Mar 24, 2020 3:09 pm

dominikp wrote:
Tue Mar 24, 2020 11:51 am
Oh man, this topic is HUUUGE news to me because so far AFAIK it was possible only on RpiZ!
Nope. any model of Pi without a USB hub between the port and the SoC's built in controller can do it. That's zero, zeroW(H), A, A+, 3A+, 4B (via the USB C port only), and the CM series (though that depnds on the carrier board). Setup is slightly different on A series boards as they have to be forced into peripheral mode when the dwc2 module is loaded.
But two questions first ...
1. After enabling this, USB on RPI cant be used for things like storage right? Or can it?
Correct. The SoC can do either host or device but not both simultaneously. It's possible to hot swap between modes but that brings its own set of problems. The exception is the 4B but only because it has two USB controllers.
2. Does bandwidth on USB3.0<->USB3.0 connectionon go full speed (or close to it) or the driver itself caps this somehow? Had anybody measured this ?
It's only USB 2 so capped at around 480Mbps.

Im using RPi3 for turning on and off my small NAS computer (with GPIO pins, didnt like WoL solution). I would love to replace it with RPI4 and have backup drive 2.5" attached to it and do backups throught 5 times faster USB3.0 instead of ethernet. Also, bridging Ethernet with USB@eth on RPI would allow me to get rid of ethernet attached directly to my NAS.

But this would be possible as long as answers for my 2 questions are positive.
Not gonna work. Transfer speed is determined by the speed of the slowest link in the chain. A device with 10Mb ethernet will never see faster transfers than that regardless of the interface between the server and it's drives. Plus the device mode capable USB ports on all Pi are only USB 2.

If you want to try it, bridging the g_ether provided interface with eth0 works exactly same way as bridging any other ethernet interfaces. It's a networking issue not a USB one.

As for replacing your current setup with a 4B, just follow one of the many tutorials on building a Pi based NAS. You can do remote shutdown (via ssh for example) but remote start won't be possible without additional hardware.
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

dominikp
Posts: 12
Joined: Mon Dec 17, 2018 9:54 am

Re: Howto: ethernet gadget on Pi4B USB C

Tue Mar 24, 2020 5:00 pm

Oh this is so sad. Well, then maybe i will find another solution then.
thagrol wrote: You can do remote shutdown (via ssh for example) but remote start won't be possible without additional hardware.
From my RPI3 there are 4 wires going out from GPIO pins to PWR_SW and PWR_LED pins of my NAS motherboard (asus p8h61-i). Offcourse they are not connected directly but with transoptors and resistors.
I wrote simple bash scripts which are placed on RPI to set the GPIO pin connected to PWR_SW to HIGH for 1second. Second script is for checking (from GPIO connected to PWR_LED) whether machine is running or not. This is mainly for cron entry, to keep the machine up and not turn it on and off all the time.

So RPI3 is working 24/7 and turns my NAS on with crontab scripts at 7AM. Shutdown (11PM) as you said is done on NAS itself (to shutdown services gracefully). Shutdown scripts also checks first if there is any traffic on SAMBA port (i sometimes watch movies at night :P ) or there are backups going on. If there are, then it repeats checks every 10min until i finish watching movie and/or backup will finish.

If something hangs or i need to turn it on remotely i can log in to my RPI with SSH and use those scripts manually/

Previously i had WoL solution but it was not very reliable.

User avatar
thagrol
Posts: 2975
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Howto: ethernet gadget on Pi4B USB C

Tue Mar 24, 2020 6:06 pm

What I meant was that while you can shutdown a Pi remotely you'll need additional hardware to start one. My comment was in the context of switching to a 4B based NAS.

Sorry if it wasn't clear.

incidentally, last time I need to find an alternative to a PC's unreliable WoL support I did it with a Pi zero: https://github.com/thagrol/fakewake
Attempts to contact me outside of these forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Return to “Networking and servers”