Usb to ethernet modules

Hi,
I’m working with two USB to Ethernet modules, and randomly i get their names switched upon device startup.
eth1 and eth2.
the problem is that i’m setting a static IP using nmcli to those NIC’s, and every time they switch, i have no connection on that NIC.
how do you advise to solve it?

Daniel

HI Daniel,
the best way to do this is by setting up udev rules to create name aliases for your devices.
Custom udev rules can be configured in config.json as explained here https://github.com/balena-os/meta-balena#udevrules .
The idea is to add rules that identify the usb devices - eg. by their serial number - and give them a unique name alias that you can be used to access them.
An udev rules that does this could look like this:
SUBSYSTEM=="net", ACTION=="add", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4ee2", NAME="hallo"```` The values for ATTRS{idVendor}andATTRS{idProduct}would have to be replaced with values and attributes that match your devices. You can find the attributes of your local USB devices by callinglsusbor going through the output ofdmesg```
Feel free to contact us if you need more advice…

thanks for the fast reply!
i dont quite understand what is the idVendor and idProduct.
attached image of lsusb

Hi Daniel,

In this case the first 16 bits (0424) is the vendor ID and the 16 bits after the colon are the product ID (2740 and 2744 respectively). In your case this will work, but I wanted to point out that if the two network devices are identical then adding these udev rules may not solve your issue.

Another angle to take is to create or edit the network manager connection files to specify the behaviour you want. You could either create specific connection files for each interface or just edit the default one to have the behaviour you want. I’d look at the stable-id option in the network manager documentation for inspiration. You can check our documentation for more information about configuring network options on balenaOS.

Good luck!
James.

Yes, my two adapters are the first two which are identical, i figured there will be something relatively easy to configure by mac address but couldn’t find any.
why adding two connection files will act differently? eth1 and eth2 will still keep alternating, no? or i misunderstood something?
i will take a look at stable-id

Luckily, they’re not identical - they have different product ID’s. Regardless, Thomas points out to me that you can also use the iSerial property to match the serial number.

they are identical…
Bus 001 Device 005: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
Bus 001 Device 004: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
serial number of what?

Oh sorry - I misread the lspci output.

Each USB device has it’s own serial number, so you can also match that in your udev rules by using iSerial. You can find these details by running lsusb -v.

thanks!
trying…

unfortunately not good, they also have the same iSerial.
the difference is the iMacAddress, can i use iMacAddress property in the udev rules?

here how lsusb -v looks like:
Bus 001 Device 006: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.10
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x0bda Realtek Semiconductor Corp.
idProduct 0x8153 RTL8153 Gigabit Ethernet Adapter
bcdDevice 30.00
iManufacturer 1 Realtek
iProduct 2 USB 10/100/1000 LAN
iSerial 6 000001
bNumConfigurations 2
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 39
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 350mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 3
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 255 Vendor Specific Subclass
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0002 1x 2 bytes
bInterval 8
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 80
bNumInterfaces 2
bConfigurationValue 2
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 350mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 2 Communications
bInterfaceSubClass 6 Ethernet Networking
bInterfaceProtocol 0
iInterface 5 CDC Communications Control
CDC Header:
bcdCDC 1.10
CDC Union:
bMasterInterface 0
bSlaveInterface 1
CDC Ethernet:
iMacAddress 3 00E04C681E1D
bmEthernetStatistics 0x00000000
wMaxSegmentSize 1514
wNumberMCFilters 0x0000
bNumberPowerFilters 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0010 1x 16 bytes
bInterval 8
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 10 CDC Data
bInterfaceSubClass 0
bInterfaceProtocol 0
iInterface 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 1
bNumEndpoints 2
bInterfaceClass 10 CDC Data
bInterfaceSubClass 0
bInterfaceProtocol 0
iInterface 4 Ethernet Data
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Binary Object Store Descriptor:
bLength 5
bDescriptorType 15
wTotalLength 12
bNumDeviceCaps 1
USB 2.0 Extension Device Capability:
bLength 7
bDescriptorType 16
bDevCapabilityType 2
bmAttributes 0x00000006
BESL Link Power Management (LPM) Supported
can’t get debug descriptor: Resource temporarily unavailable
Device Status: 0x0000
(Bus Powered)

Bus 001 Device 004: ID 0bda:8153 Realtek Semiconductor Corp. RTL8153 Gigabit Ethernet Adapter
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.10
bDeviceClass 0
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x0bda Realtek Semiconductor Corp.
idProduct 0x8153 RTL8153 Gigabit Ethernet Adapter
bcdDevice 30.00
iManufacturer 1 Realtek
iProduct 2 USB 10/100/1000 LAN
iSerial 6 000001
bNumConfigurations 2
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 39
bNumInterfaces 1
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 350mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 3
bInterfaceClass 255 Vendor Specific Class
bInterfaceSubClass 255 Vendor Specific Subclass
bInterfaceProtocol 0
iInterface 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0002 1x 2 bytes
bInterval 8
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 80
bNumInterfaces 2
bConfigurationValue 2
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 350mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 1
bInterfaceClass 2 Communications
bInterfaceSubClass 6 Ethernet Networking
bInterfaceProtocol 0
iInterface 5 CDC Communications Control
CDC Header:
bcdCDC 1.10
CDC Union:
bMasterInterface 0
bSlaveInterface 1
CDC Ethernet:
iMacAddress 3 00E04C688ACE
bmEthernetStatistics 0x00000000
wMaxSegmentSize 1514
wNumberMCFilters 0x0000
bNumberPowerFilters 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x83 EP 3 IN
bmAttributes 3
Transfer Type Interrupt
Synch Type None
Usage Type Data
wMaxPacketSize 0x0010 1x 16 bytes
bInterval 8
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 10 CDC Data
bInterfaceSubClass 0
bInterfaceProtocol 0
iInterface 0
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 1
bAlternateSetting 1
bNumEndpoints 2
bInterfaceClass 10 CDC Data
bInterfaceSubClass 0
bInterfaceProtocol 0
iInterface 4 Ethernet Data
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x81 EP 1 IN
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Endpoint Descriptor:
bLength 7
bDescriptorType 5
bEndpointAddress 0x02 EP 2 OUT
bmAttributes 2
Transfer Type Bulk
Synch Type None
Usage Type Data
wMaxPacketSize 0x0200 1x 512 bytes
bInterval 0
Binary Object Store Descriptor:
bLength 5
bDescriptorType 15
wTotalLength 12
bNumDeviceCaps 1
USB 2.0 Extension Device Capability:
bLength 7
bDescriptorType 16
bDevCapabilityType 2
bmAttributes 0x00000006
BESL Link Power Management (LPM) Supported
can’t get debug descriptor: Resource temporarily unavailable
Device Status: 0x0000
(Bus Powered)
Daniel

Any property should work.

hi, thank you for your help.
can you point me to a link that shows how to add custom udev rules?
i saw : https://github.com/balena-os/meta-balena#udevrules but i dont understand how i actually add a rule, i need to create a x.rules file in /etd/udev/rules.d ?
where can i find a nice tutorial for this?

thanks!

There’s two ways to do it - one is at the link you shared where you can write a rules file and place it in the device’s config.json (in the boot partition of the SD card), or if you’re using one of our balena base images then you can set the UDEV environment variable and put the rules files in your service container. There’s a great write up by one of our users on medium - in his case he’s using USB serial connections, but the concepts are similar.

thanks!
its a great post, so i’ve tried it and added a ethernet.rules file and copied to /mnt/udev/rules.d folder.
it doesnt work, i’m probably doing something wrong.

in my docker file the part looks like this:
ENV UDEV=on
COPY ethernet.rules /etc/udev/rules.d/ethernet.rules

the ethernet.rules looks like this:
SUBSYSTEM==“net”, ACTION==“add”, ATTRS{iMacAddress}==“00E04C681E1D”, NAME=“testudev”

Hi,
Given your container is privileged (otherwise you should see a message saying udev cannot be started in the service logs), I think the problem can be in how the rule is written. To debug this, I would start by adding a RUN option to see that the rule is triggered and try adjusting the rule to be less specific to see it’s fired. For instance,

SUBSYSTEM==“net”, ACTION==“add”,RUN+="/bin/sh -c 'echo net-device-added >> /udev-debug.log'"

And then, if that works (checking udev-debug.log in the container), making it more specific again.

thank you roman.
i’ve tried it with no success, nothing is written.
this is my ethernet.rules file:
SUBSYSTEM==“net”, ACTION==“add”,RUN+="/bin/sh -c ‘echo net-device-added >> /udev-debug.log’"

from what i read in the medium post (https://medium.com/swlh/hot-plugging-usb-devices-in-balenaos-5dca4b05ca7e) , the only time the udev rule was used is when he actually plugged the USB, when will the udev rule run in my case?
is it possible to change config.json on run time? so i can test the rules?

Hi there, are you able to try the rule again, with the correct quote characters. The rule we gave you used and instead of " (e.g.):

SUBSYSTEM=="net", ACTION=="add", RUN+="/bin/sh -c ‘echo net-device-added >> /udev-debug.log’"

In addition, running udevadm monitor — environment will give you a live output of udev events as you (un)plug devices.

thanks,
this is the output of “udevadm monitor — environment” when disconnecting and connecting the usb to ethernet dongle:

KERNEL[11566.238819] remove /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:1.0 (usb)
KERNEL[11566.239013] remove /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2 (usb)
KERNEL[11595.714787] add /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2 (usb)
KERNEL[11595.715329] add /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:2.0 (usb)
KERNEL[11595.716334] add /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:2.1 (usb)
KERNEL[11595.716525] remove /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:2.0 (usb)
KERNEL[11595.716592] remove /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:2.1 (usb)
KERNEL[11595.716653] add /devices/platform/32e50000.usb/ci_hdrc.1/usb1/1-1/1-1.2/1-1.2:1.0 (usb)

can you please advice? sounds like the rule is not net related its more usb related?
Found additional information here: Rename network interface
but its a bit different since my devices are USB that eventually turn into network cards