Skip to content

Process sysfs/class/net/*/bonding#439

Open
bewing wants to merge 1 commit intoprometheus:masterfrom
bewing:sysfs_bonding
Open

Process sysfs/class/net/*/bonding#439
bewing wants to merge 1 commit intoprometheus:masterfrom
bewing:sysfs_bonding

Conversation

@bewing
Copy link
Copy Markdown

@bewing bewing commented Mar 21, 2022

As part of prometheus/node_exporter#1604, parse information from /sys/class/net/*/bonding*/* to provide information regarding current bonding state for bonding controllers and their devices.

I have some style/format questions that I would appreciate input on as part of the process:

  • There does not appear to be consistency on pointers vs values in many modules of this project. I notice that net_class.go uses pointers for integers, but not strings. Should I follow that, or is there a different preference from the maintainers? It would appear that using pointers would allow for nil values to indicate kernel-level support for a specific file (if the kernel doesn't have the file present, better to indicate with a nil pointer rather than a default empty string or integer?), but I'm curious what the consensus is in the project for indicating this
  • The kernel driver has many different sized ints (uint8, int64, uint64, etc) -- should I try to match what the current kernel defines these as, or just use uint64s as the worst case storage requirement?
  • The bonding driver has many files of the format <uint> <human readable value string> -- should this be implemented as constants in the module itself, or imported constants from a reference module (assuming one exists), or some other approach I haven't thought of?
  • Should I be using pointers to other NetClassIface structs to reference controllers and devices (IE, L34, L64 ?)
  • Should any special care/documentation be taken regarding fields that need CAP_NET_ADMIN to read?

@bewing bewing marked this pull request as draft March 21, 2022 22:33
@bewing bewing force-pushed the sysfs_bonding branch 6 times, most recently from 0a19628 to da42abf Compare March 25, 2022 20:37
@bewing bewing marked this pull request as ready for review March 25, 2022 20:38
@bewing bewing changed the title WIP: Process sysfs/class/net/*/bonding Process sysfs/class/net/*/bonding Mar 25, 2022
@discordianfish
Copy link
Copy Markdown
Member

There does not appear to be consistency on pointers vs values in many modules of this project. I notice that net_class.go uses pointers for integers, but not strings. Should I follow that, or is there a different preference from the maintainers? It would appear that using pointers would allow for nil values to indicate kernel-level support for a specific file (if the kernel doesn't have the file present, better to indicate with a nil pointer rather than a default empty string or integer?), but I'm curious what the consensus is in the project for indicating this

yes, we use pointers to distinguish between a null value and e.g non existing file as in your example. Other reasons would be larger structs where you want to avoid copying them when you pass them to functions.

The kernel driver has many different sized ints (uint8, int64, uint64, etc) -- should I try to match what the current kernel defines these as, or just use uint64s as the worst case storage requirement?

Yes we try to mimic the underlying sized ints

The bonding driver has many files of the format -- should this be implemented as constants in the module itself, or imported constants from a reference module (assuming one exists), or some other approach I haven't thought of?

Not sure what you mean by that.

Should I be using pointers to other NetClassIface structs to reference controllers and devices (IE, L34, L64 ?)

If you retrieved the data for them I see why not, but we probably shouldn't waste cycles on getting them if we don't need them in which case I'd just use some string? identifier.

Should any special care/documentation be taken regarding fields that need CAP_NET_ADMIN to read?

I don't think we have any convention for that.

@bewing
Copy link
Copy Markdown
Author

bewing commented Mar 30, 2022

The bonding driver has many files of the format -- should this be implemented as constants in the module itself, or imported constants from a reference module (assuming one exists), or some other approach I haven't thought of?

Not sure what you mean by that.

For example, sys/class/net/bond0/bonding/ad_select :

$ cat /sys/class/net/bond0/bonding/ad_select
stable 0

The actual underlying stored kernel value for ad_select is an integer, but bond_opt_get_val is used to resolve that integer 0 to the string "stable" via a constant lookup table bond_ad_select_tbl, and then both are present in the sysfs file. Should we return the integer, just the string, or a generic bond_options struct with both values?

@bewing bewing marked this pull request as draft April 1, 2022 15:42
@bewing
Copy link
Copy Markdown
Author

bewing commented Apr 1, 2022

I'm not able to run tests in node_exporter using this branch, so there's still more digging/dev work needed before this is mergable

Comment thread sysfs/net_class.go
@discordianfish
Copy link
Copy Markdown
Member

The actual underlying stored kernel value for ad_select is an integer, but bond_opt_get_val is used to resolve that integer 0 to the string "stable" via a constant lookup table bond_ad_select_tbl, and then both are present in the sysfs file. Should we return the integer, just the string, or a generic bond_options struct with both values?

I'd say just the string but I wonder why they expose both the string and the int in procfs and if we should take that decisiopn into account here..

bewing added a commit to bewing/node_exporter that referenced this pull request Apr 11, 2022
As part of change prometheus/procfs#439, update fixtures in
node_exporter to expose all referenced interfaces in /sys/class/net

This was naively accomplished by blindly copying eth0 data to other
physical interfaces, but that does not appear to have resulted in any
failing tests.  Once we begin to actually export bonding/LACP metrics,
additional modification of fixtures may be required.

Signed-off-by: Brandon Ewing <brandon.ewing@warningg.com>
@bewing
Copy link
Copy Markdown
Author

bewing commented Apr 22, 2026

Oh what a difference 4 years makes.

Updated branch, node_exporter tests no longer fail.

Can we re-touch on the questions I had in the initial PR, and see if we can get this to a mergable state?

$ git diff
diff --git a/go.mod b/go.mod
index 31d63c20..1bc7d351 100644
--- a/go.mod
+++ b/go.mod
@@ -31,6 +31,8 @@ require (
        howett.net/plist v1.0.1
 )

+replace github.com/prometheus/procfs => ../procfs
+
 require (
        cyphar.com/go-pathrs v0.2.2 // indirect
        github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
diff --git a/go.sum b/go.sum
index ae4bd585..25cfca5d 100644
--- a/go.sum
+++ b/go.sum
@@ -84,8 +84,6 @@ github.com/prometheus/common v0.67.5 h1:pIgK94WWlQt1WLwAC5j2ynLaBRDiinoAb86HZHTU
 github.com/prometheus/common v0.67.5/go.mod h1:SjE/0MzDEEAyrdr5Gqc6G+sXI67maCxzaT3A2+HqjUw=
 github.com/prometheus/exporter-toolkit v0.16.0 h1:xT/j7L2XKF+VJd6B4fpUw6xWabHrSmsUf6mYmFqyu0s=
 github.com/prometheus/exporter-toolkit v0.16.0/go.mod h1:d1EL8Z9674xQe/iWhwP2wDyCEoBPbXVeqDbqAUsgJWY=
-github.com/prometheus/procfs v0.20.1 h1:XwbrGOIplXW/AU3YhIhLODXMJYyC1isLFfYCsTEycfc=
-github.com/prometheus/procfs v0.20.1/go.mod h1:o9EMBZGRyvDrSPH1RqdxhojkuXstoe4UlK79eF5TGGo=
 github.com/safchain/ethtool v0.7.0 h1:rlJzfDetsVvT61uz8x1YIcFn12akMfuPulHtZjtb7Is=
 github.com/safchain/ethtool v0.7.0/go.mod h1:MenQKEjXdfkjD3mp2QdCk8B/hwvkrlOTm/FD4gTpFxQ=
 github.com/siebenmann/go-kstat v0.0.0-20210513183136-173c9b0a9973 h1:GfSdC6wKfTGcgCS7BtzF5694Amne1pGCSTY252WhlEY=

$ go clean -testcache
$ make test
>> running tests
go test -short -race ./...
ok      github.com/prometheus/node_exporter     1.107s
ok      github.com/prometheus/node_exporter/collector   1.602s
ok      github.com/prometheus/node_exporter/collector/utils     1.047s
?       github.com/prometheus/node_exporter/tools       [no test files]

@bewing bewing marked this pull request as ready for review April 22, 2026 18:54
@bewing
Copy link
Copy Markdown
Author

bewing commented Apr 22, 2026

I'd say just the string but I wonder why they expose both the string and the int in procfs and if we should take that decisiopn into account here..

It appears the pattern that was used in the Infiniband section was to include both:

State string // String representation from /sys/class/infiniband/<Name>/ports/<Port>/state
StateID uint // ID from /sys/class/infiniband/<Name>/ports/<Port>/state
PhysState string // String representation from /sys/class/infiniband/<Name>/ports/<Port>/phys_state
PhysStateID uint // String representation from /sys/class/infiniband/<Name>/ports/<Port>/phys_state

@SuperQ
Copy link
Copy Markdown
Member

SuperQ commented Apr 23, 2026

Thanks for coming back to working on this. Looks like there's some testing issues.

@bewing
Copy link
Copy Markdown
Author

bewing commented Apr 23, 2026

Thanks for coming back to working on this. Looks like there's some testing issues.

Pushed new commit with linting fixes.

Update net_class.go with new structures to handle bonding driver
information

Add new true/false values to internal.util.parse.ParseBool

Update AerCounters to ignore logical interfaces

References:
torvalds/linux/drivers/net/bonding/bond_sysfs.c
torvalds/linux/drivers/net/bonding/bond_sysfs_slave.c
torvalds/linux/include/net/bonding.h
torvalds/linux/include/net/bond_options.h
torvalds/linux/include/net/bond_3ad.h

Signed-off-by: Brandon Ewing <brandon.ewing@warningg.com>
@bewing
Copy link
Copy Markdown
Author

bewing commented Apr 23, 2026

Weird that gofmt didn't fix this for me.

$ golangci-lint run ./sysfs/
sysfs/net_class_test.go:23:1: File is not properly formatted (goimports)
        "github.com/google/go-cmp/cmp"
^
1 issues:
* goimports: 1

pushed new changeset after running golangci-lint.

@SuperQ
Copy link
Copy Markdown
Member

SuperQ commented Apr 23, 2026

Yea, we have some golangci-lint format tools here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants