Skip to content

Commit 0ebf3ef

Browse files
author
John Lam
committed
Enhance device index detection with sysfs-based approach
Improves network interface device index detection reliability by: - Adding a primary method that reads device index directly from sysfs - Maintaining the name-based parsing as a fallback mechanism - Adding logging to indicate which method was used This makes the interface detection more robust across different system configurations where sysfs information is available, reducing reliance on interface naming conventions.
1 parent 9c00fe9 commit 0ebf3ef

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

pkg/eni-manager/network/manager.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,14 @@ func (m *Manager) getPCIAddressForInterface(ifaceName string) (string, error) {
267267
}
268268

269269
func (m *Manager) getDeviceIndexForInterface(ifaceName string) (int, error) {
270-
// Try to extract device index from interface name
271-
// This is a simplified approach - in practice, this would use more sophisticated mapping
270+
// Method 1: Try to read device index from sysfs (most reliable)
271+
if deviceIndex, err := m.getDeviceIndexFromSysfs(ifaceName); err == nil {
272+
log.Printf("Device index for %s from sysfs: %d", ifaceName, deviceIndex)
273+
return deviceIndex, nil
274+
}
275+
276+
// Method 2: Fallback to interface name parsing
277+
log.Printf("Sysfs unavailable for %s, using name-based calculation", ifaceName)
272278

273279
// For eth interfaces (eth0, eth1, etc.)
274280
if strings.HasPrefix(ifaceName, "eth") {
@@ -290,6 +296,25 @@ func (m *Manager) getDeviceIndexForInterface(ifaceName string) (int, error) {
290296
return 0, fmt.Errorf("could not determine device index for interface %s", ifaceName)
291297
}
292298

299+
// getDeviceIndexFromSysfs reads the device index directly from sysfs
300+
func (m *Manager) getDeviceIndexFromSysfs(ifaceName string) (int, error) {
301+
// Try multiple sysfs paths for device index
302+
paths := []string{
303+
fmt.Sprintf("/sys/class/net/%s/device/device_index", ifaceName),
304+
fmt.Sprintf("/sys/class/net/%s/dev_id", ifaceName),
305+
}
306+
307+
for _, path := range paths {
308+
if data, err := os.ReadFile(path); err == nil {
309+
if deviceIndex, err := strconv.Atoi(strings.TrimSpace(string(data))); err == nil {
310+
return deviceIndex, nil
311+
}
312+
}
313+
}
314+
315+
return 0, fmt.Errorf("device index not found in sysfs for interface %s", ifaceName)
316+
}
317+
293318
func (m *Manager) bringUpInterfaceWithIP(ifaceName string) error {
294319
cmd := exec.Command("ip", "link", "set", "dev", ifaceName, "up")
295320
output, err := cmd.CombinedOutput()

0 commit comments

Comments
 (0)