Skip to content

Commit 3743a3a

Browse files
committed
unix: add IoctlGetEthtoolTsInfo
The function fetches ethtool timestamping and PHC association for a network interface. Its primary usage is to query the mapping between the interface and its corresponding PTP clock number in /dev/ptp𝑛.
1 parent 7143f4a commit 3743a3a

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

unix/ioctl_linux.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ func IoctlGetEthtoolDrvinfo(fd int, ifname string) (*EthtoolDrvinfo, error) {
5858
return &value, err
5959
}
6060

61+
// IoctlGetEthtoolTsInfo fetches ethtool timestamping and PHC
62+
// association for the network device specified by ifname.
63+
func IoctlGetEthtoolTsInfo(fd int, ifname string) (*EthtoolTsInfo, error) {
64+
ifr, err := NewIfreq(ifname)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
value := EthtoolTsInfo{Cmd: ETHTOOL_GET_TS_INFO}
70+
ifrd := ifr.withData(unsafe.Pointer(&value))
71+
72+
err = ioctlIfreqData(fd, SIOCETHTOOL, &ifrd)
73+
return &value, err
74+
}
75+
6176
// IoctlGetWatchdogInfo fetches information about a watchdog device from the
6277
// Linux watchdog API. For more information, see:
6378
// https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.

unix/linux/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,6 +4090,8 @@ const SPEED_UNKNOWN = C.SPEED_UNKNOWN
40904090

40914091
type EthtoolDrvinfo C.struct_ethtool_drvinfo
40924092

4093+
type EthtoolTsInfo C.struct_ethtool_ts_info
4094+
40934095
type (
40944096
HIDRawReportDescriptor C.struct_hidraw_report_descriptor
40954097
HIDRawDevInfo C.struct_hidraw_devinfo

unix/syscall_linux_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,44 @@ func TestIoctlGetEthtoolDrvinfo(t *testing.T) {
6868
}
6969
}
7070

71+
func TestIoctlGetEthtoolTsInfo(t *testing.T) {
72+
if runtime.GOOS == "android" {
73+
t.Skip("ethtool driver info is not available on android, skipping test")
74+
}
75+
76+
s, err := unix.Socket(unix.AF_INET, unix.SOCK_STREAM, 0)
77+
if err != nil {
78+
t.Fatalf("failed to open socket: %v", err)
79+
}
80+
defer unix.Close(s)
81+
82+
ifis, err := net.Interfaces()
83+
if err != nil {
84+
t.Fatalf("failed to get network interfaces: %v", err)
85+
}
86+
87+
// Print the interface name and associated PHC information for each
88+
// network interface supported by ethtool.
89+
for _, ifi := range ifis {
90+
tsi, err := unix.IoctlGetEthtoolTsInfo(s, ifi.Name)
91+
if err != nil {
92+
if err == unix.EOPNOTSUPP {
93+
continue
94+
}
95+
96+
if err == unix.EBUSY {
97+
// See https://go.dev/issues/67350
98+
t.Logf("%s: ethtool driver busy, possible kernel bug", ifi.Name)
99+
continue
100+
}
101+
102+
t.Fatalf("failed to get ethtool PHC info for %q: %v", ifi.Name, err)
103+
}
104+
105+
t.Logf("%s: ptp%d", ifi.Name, tsi.Phc_index)
106+
}
107+
}
108+
71109
func TestIoctlGetInt(t *testing.T) {
72110
f, err := os.Open("/dev/random")
73111
if err != nil {

unix/ztypes_linux.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)