Skip to content

Commit

Permalink
phc: switch to use unix.IoctlGetEthtoolTsInfo (facebook#410)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#410

Drop the local syscall wrappers in favour of the upstreamed one.

See https://go-review.googlesource.com/c/sys/+/619335: unix: add IoctlGetEthtoolTsInfo on Linux.

Reviewed By: leoleovich

Differential Revision: D64325595
  • Loading branch information
yarikk authored and facebook-github-bot committed Oct 17, 2024
1 parent e789a56 commit 48dd038
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 43 deletions.
45 changes: 4 additions & 41 deletions phc/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"fmt"
"unsafe"

"github.com/facebook/time/phc/unix" // a temporary shim for "golang.org/x/sys/unix" until v0.27.0 is cut
"github.com/vtolstov/go-ioctl"
"golang.org/x/sys/unix"
)

// Missing from sys/unix package, defined in Linux include/uapi/linux/ptp_clock.h
Expand Down Expand Up @@ -55,25 +55,6 @@ var ioctlPTPPeroutRequest2 = ioctl.IOW(ptpClkMagic, 12, unsafe.Sizeof(PTPPeroutR
// ioctlExtTTSRequest2 is an IOCTL req corresponding to PTP_EXTTS_REQUEST2 in linux/ptp_clock.h
var ioctlExtTTSRequest2 = ioctl.IOW(ptpClkMagic, 11, unsafe.Sizeof(PTPExtTTSRequest{}))

// Ifreq is the request we send with SIOCETHTOOL IOCTL
// as per Linux kernel's include/uapi/linux/if.h
type Ifreq struct {
Name [unix.IFNAMSIZ]byte
Data uintptr
}

// EthtoolTSinfo holds a device's timestamping and PHC association
// as per Linux kernel's include/uapi/linux/ethtool.h
type EthtoolTSinfo struct {
Cmd uint32
SOtimestamping uint32
PHCIndex int32
TXTypes uint32
TXReserved [3]uint32
RXFilters uint32
RXReserved [3]uint32
}

// PTPSysOffsetExtended as defined in linux/ptp_clock.h
type PTPSysOffsetExtended struct {
NSamples uint32 /* Desired number of measurements. */
Expand Down Expand Up @@ -191,30 +172,12 @@ func (caps *PTPClockCaps) maxAdj() float64 {
return float64(caps.MaxAdj)
}

// IfaceInfo uses SIOCETHTOOL ioctl to get information for the give nic, i.e. eth0.
func IfaceInfo(iface string) (*EthtoolTSinfo, error) {
// IfaceInfo uses an ioctl to get information for the named nic, e.g. eth0.
func IfaceInfo(iface string) (*unix.EthtoolTsInfo, error) {
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, 0)
if err != nil {
return nil, fmt.Errorf("failed to create socket for ioctl: %w", err)
}
defer unix.Close(fd)
// this is what we want to be populated, but we need to provide Cmd first
data := &EthtoolTSinfo{
Cmd: unix.ETHTOOL_GET_TS_INFO,
}
// actual request we send
ifreq := &Ifreq{}
// set Name in the request
copy(ifreq.Name[:unix.IFNAMSIZ-1], iface)
// pointer to the data we need to be populated
ifreq.Data = uintptr(unsafe.Pointer(data))
_, _, errno := unix.Syscall(
unix.SYS_IOCTL, uintptr(fd),
uintptr(unix.SIOCETHTOOL),
uintptr(unsafe.Pointer(ifreq)),
)
if errno != 0 {
return nil, fmt.Errorf("failed get phc ID for %s: %w", iface, errno)
}
return data, nil
return unix.IoctlGetEthtoolTsInfo(fd, iface)
}
4 changes: 2 additions & 2 deletions phc/phc.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ func IfaceToPHCDevice(iface string) (string, error) {
if err != nil {
return "", fmt.Errorf("getting interface %s info: %w", iface, err)
}
if info.PHCIndex < 0 {
if info.Phc_index < 0 {
return "", fmt.Errorf("%s: no PHC support", iface)
}
return fmt.Sprintf("/dev/ptp%d", info.PHCIndex), nil
return fmt.Sprintf("/dev/ptp%d", info.Phc_index), nil
}

// Time returns time we got from network card
Expand Down

0 comments on commit 48dd038

Please sign in to comment.