-
Notifications
You must be signed in to change notification settings - Fork 30
Handle non-directory entries in /sys/class/net #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the enhancement!
pkg/sysfsnet/interfaces_linux.go
Outdated
if !info.IsDir() { | ||
return "", nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we do this check earlier, and call readMACFromFile()
only if it's a dir?
Will this work?
diff --git a/pkg/sysfsnet/interfaces_linux.go b/pkg/sysfsnet/interfaces_linux.go
index f4a295f..138b788 100644
--- a/pkg/sysfsnet/interfaces_linux.go
+++ b/pkg/sysfsnet/interfaces_linux.go
@@ -39,6 +39,9 @@ func Interfaces() ([]Interface, error) {
}
for _, dentry := range dents {
+ if !dentry.IsDir() {
+ continue
+ }
hwText, err := readMACFromFile(filepath.Join(sysClassNet, dentry.Name(), "address"))
if os.IsNotExist(err) {
continue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is much cleaner - I'll make the change and test locally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks - looks like we are very close to merging this. Let me know if you need anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now with that change, I did have to call os.Stat() first becuase os.DirEntry(dentry) resolves to a symlink and IsDir()/FileInfo doesn't check the target
// os.Stat to follow symlinks and return the info of the target | ||
info, err := os.Stat(entryPath) | ||
if err != nil { | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should at least log err
as a warning before continue
. That may be other reasons why os.Stat(entryPath)
could fail, and we don't want it to always fail silently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ganawaj would you mind improving that?
pkg/sysfsnet/interfaces_test.go
Outdated
@@ -45,4 +50,9 @@ func TestInterfaces(t *testing.T) { | |||
if !reflect.DeepEqual(want, got) { | |||
t.Fatalf("want MACs=%v, got MACs=%v", want, got) | |||
} | |||
|
|||
// Ensure non-directory entry is ignored | |||
if _, exists := got[""]; exists { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think got[""]
is too broad. Can you add some test string to the bonding_masters
file and then make sure they aren't present in got
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, ensure the @ihcsim's suggestion could be addressed.
// os.Stat to follow symlinks and return the info of the target | ||
info, err := os.Stat(entryPath) | ||
if err != nil { | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ganawaj would you mind improving that?
@ganawaj Thanks again for your help on this PR. Since this one is quite important, I am going to cherry-pick your commits into another PR (#55), to make sure it gets in before our upcoming code freeze. Feel free to review it at #55. Let me know if you have any questions. @Vicente-Cheng @WebberHuang1118 PTAL at #55. Thanks. |
Sounds good - apologize for the delay on this. Had the commits a couple days ago after testing but hadn't pushed it yet - but if everything looks good feel free to close this one in favor of #55 |
pkg/sysfsnet/interfaces_linux.go
Outdated
@@ -23,7 +25,9 @@ func Interfaces() ([]Interface, error) { | |||
ifaces := make([]Interface, 0, 16) | |||
|
|||
readMACFromFile := func(s string) (string, error) { | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we remove the extra line break? Thanks.
pkg/sysfsnet/interfaces_linux.go
Outdated
f, err := os.Open(s) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we remove the extra line break? Thanks.
pkg/sysfsnet/interfaces_linux.go
Outdated
@@ -39,7 +43,23 @@ func Interfaces() ([]Interface, error) { | |||
} | |||
|
|||
for _, dentry := range dents { | |||
hwText, err := readMACFromFile(filepath.Join(sysClassNet, dentry.Name(), "address")) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we remove the extra line break? Thanks.
pkg/sysfsnet/interfaces_test.go
Outdated
} | ||
|
||
if !reflect.DeepEqual(want, got) { | ||
t.Fatalf("want MACs=%v, got MACs=%v", want, got) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can we remove the extra line break? Thanks.
Problem:
If non-directory entries are in /sys/class/net - the driver fails to enumerate interface macs.
Solution:
Before attempting to open /sys/class/net/[interface]/address, checks if /sys/class/net/[interface] is a directory. If it is not, ignore it and continue enumerating the interfaces
Related Issue:
harvester/harvester#7232
Test plan:
All test pass running
make ci
, built and pushed and tested in cluster with the following logs: