Skip to content

Commit

Permalink
time: import golang.org/cl/621375
Browse files Browse the repository at this point in the history
Summary:
https://go-review.googlesource.com/c/sys/+/621375: unix: add PTP IOCTLs on Linux

Import into the local shim so we can start adopting the code before the version is cut.

Differential Revision: D64695439
  • Loading branch information
yarikk authored and facebook-github-bot committed Oct 21, 2024
1 parent cd3b7ef commit 51b6cb0
Show file tree
Hide file tree
Showing 16 changed files with 427 additions and 29 deletions.
201 changes: 172 additions & 29 deletions phc/unix/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,149 @@ func ClockSettime(clockid int32, time *Timespec) (err error) {
return
}

// https://go-review.googlesource.com/c/sys/+/621375

type (
PtpClockCaps struct {
Max_adj int32

Check warning on line 125 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; struct field Max_adj should be MaxAdj (revive)
N_alarm int32

Check warning on line 126 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; struct field N_alarm should be NAlarm (revive)
N_ext_ts int32

Check warning on line 127 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; struct field N_ext_ts should be NExtTs (revive)
N_per_out int32

Check warning on line 128 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; struct field N_per_out should be NPerOut (revive)
Pps int32
N_pins int32

Check warning on line 130 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; struct field N_pins should be NPins (revive)
Cross_timestamping int32

Check warning on line 131 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; struct field Cross_timestamping should be CrossTimestamping (revive)
Adjust_phase int32

Check warning on line 132 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use underscores in Go names; struct field Adjust_phase should be AdjustPhase (revive)
Max_phase_adj int32
Rsv [11]int32
}
PtpClockTime struct {
Sec int64
Nsec uint32
Reserved uint32
}
PtpExttsEvent struct {
T PtpClockTime
Index uint32
Flags uint32
Rsv [2]uint32
}
PtpExttsRequest struct {
Index uint32
Flags uint32
Rsv [2]uint32
}
PtpPeroutRequest struct {
StartOrPhase PtpClockTime
Period PtpClockTime
Index uint32
Flags uint32
On PtpClockTime
}
PtpPinDesc struct {
Name [64]byte
Index uint32
Func uint32
Chan uint32
Rsv [5]uint32
}
PtpSysOffset struct {
Samples uint32
Rsv [3]uint32
Ts [51]PtpClockTime
}
PtpSysOffsetExtended struct {
Samples uint32
Rsv [3]uint32
Ts [25][3]PtpClockTime
}
PtpSysOffsetPrecise struct {
Device PtpClockTime
Realtime PtpClockTime
Monoraw PtpClockTime
Rsv [4]uint32
}
)

const (
PTP_CLK_MAGIC = '='
PTP_ENABLE_FEATURE = 0x1
PTP_EXTTS_EDGES = 0x6
PTP_EXTTS_EVENT_VALID = 0x1
PTP_EXTTS_V1_VALID_FLAGS = 0x7
PTP_EXTTS_VALID_FLAGS = 0x1f
PTP_EXT_OFFSET = 0x10
PTP_FALLING_EDGE = 0x4
PTP_MAX_SAMPLES = 0x19
PTP_PEROUT_DUTY_CYCLE = 0x2
PTP_PEROUT_ONE_SHOT = 0x1
PTP_PEROUT_PHASE = 0x4
PTP_PEROUT_V1_VALID_FLAGS = 0x0
PTP_PEROUT_VALID_FLAGS = 0x7
PTP_PIN_GETFUNC = 0xc0603d06
PTP_PIN_GETFUNC2 = 0xc0603d0f
PTP_RISING_EDGE = 0x2
PTP_STRICT_FLAGS = 0x8
PTP_SYS_OFFSET_EXTENDED = 0xc4c03d09
PTP_SYS_OFFSET_EXTENDED2 = 0xc4c03d12
PTP_SYS_OFFSET_PRECISE = 0xc0403d08
PTP_SYS_OFFSET_PRECISE2 = 0xc0403d11
)

// FdToClockID derives the clock ID from the file descriptor number
// - see clock_gettime(3), FD_TO_CLOCKID macros. The resulting ID is
// suitable for system calls like ClockGettime.
func FdToClockID(fd int) int32 { return int32((int(^fd) << 3) | 3) }

Check failure on line 212 in phc/unix/linux.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)

// IoctlPtpClockGetcaps returns the description of a given PTP device.
func IoctlPtpClockGetcaps(fd int) (*PtpClockCaps, error) {
var value PtpClockCaps
err := ioctlPtr(fd, PTP_CLOCK_GETCAPS2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpSysOffsetPrecise returns a description of the clock
// offset compared to the system clock.
func IoctlPtpSysOffsetPrecise(fd int) (*PtpSysOffsetPrecise, error) {
var value PtpSysOffsetPrecise
err := ioctlPtr(fd, PTP_SYS_OFFSET_PRECISE2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpSysOffsetExtended returns an extended description of the
// clock offset compared to the system clock. The samples parameter
// specifies the desired number of measurements.
func IoctlPtpSysOffsetExtended(fd int, samples uint) (*PtpSysOffsetExtended, error) {
value := PtpSysOffsetExtended{Samples: uint32(samples)}
err := ioctlPtr(fd, PTP_SYS_OFFSET_EXTENDED2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpPinGetfunc returns the configuration of the specified
// I/O pin on given PTP device.
func IoctlPtpPinGetfunc(fd int, index uint) (*PtpPinDesc, error) {
value := PtpPinDesc{Index: uint32(index)}
err := ioctlPtr(fd, PTP_PIN_GETFUNC2, unsafe.Pointer(&value))
return &value, err
}

// IoctlPtpPinSetfunc updates configuration of the specified PTP
// I/O pin.
func IoctlPtpPinSetfunc(fd int, pd *PtpPinDesc) error {
return ioctlPtr(fd, PTP_PIN_SETFUNC2, unsafe.Pointer(pd))
}

// IoctlPtpPeroutRequest configures the periodic output mode of the
// PTP I/O pins.
func IoctlPtpPeroutRequest(fd int, r *PtpPeroutRequest) error {
return ioctlPtr(fd, PTP_PEROUT_REQUEST2, unsafe.Pointer(r))
}

// IoctlPtpExttsRequest configures the external timestamping mode
// of the PTP I/O pins.
func IoctlPtpExttsRequest(fd int, r *PtpExttsRequest) error {
return ioctlPtr(fd, PTP_EXTTS_REQUEST2, unsafe.Pointer(r))
}

// bridging to upstream

type Cmsghdr = unix.Cmsghdr
Expand Down Expand Up @@ -147,36 +290,36 @@ func TimeToTimespec(t time.Time) (Timespec, error) { return unix.TimeToT
func Uname(s *Utsname) error { return unix.Uname(s) }

const (
AF_INET = unix.AF_INET //nolint:revive
EAGAIN = unix.EAGAIN //nolint:revive
EINVAL = unix.EINVAL //nolint:revive
ENOENT = unix.ENOENT //nolint:revive
ENOTSUP = unix.ENOTSUP //nolint:revive
ETHTOOL_GET_TS_INFO = unix.ETHTOOL_GET_TS_INFO //nolint:revive
IFNAMSIZ = unix.IFNAMSIZ //nolint:revive
MSG_ERRQUEUE = unix.MSG_ERRQUEUE //nolint:revive
POLLERR = unix.POLLERR //nolint:revive
SIOCETHTOOL = unix.SIOCETHTOOL //nolint:revive
SIOCGHWTSTAMP = unix.SIOCGHWTSTAMP //nolint:revive
SIOCSHWTSTAMP = unix.SIOCSHWTSTAMP //nolint:revive
SizeofPtr = unix.SizeofPtr
SizeofSockaddrInet4 = unix.SizeofSockaddrInet4
SOCK_DGRAM = unix.SOCK_DGRAM //nolint:revive
SOF_TIMESTAMPING_OPT_TSONLY = unix.SOF_TIMESTAMPING_OPT_TSONLY //nolint:revive
AF_INET = unix.AF_INET //nolint:revive
EAGAIN = unix.EAGAIN //nolint:revive
EINVAL = unix.EINVAL //nolint:revive
ENOENT = unix.ENOENT //nolint:revive
ENOTSUP = unix.ENOTSUP //nolint:revive
ETHTOOL_GET_TS_INFO = unix.ETHTOOL_GET_TS_INFO //nolint:revive
IFNAMSIZ = unix.IFNAMSIZ //nolint:revive
MSG_ERRQUEUE = unix.MSG_ERRQUEUE //nolint:revive
POLLERR = unix.POLLERR //nolint:revive
SIOCETHTOOL = unix.SIOCETHTOOL //nolint:revive
SIOCGHWTSTAMP = unix.SIOCGHWTSTAMP //nolint:revive
SIOCSHWTSTAMP = unix.SIOCSHWTSTAMP //nolint:revive
SizeofPtr = unix.SizeofPtr
SizeofSockaddrInet4 = unix.SizeofSockaddrInet4
SOCK_DGRAM = unix.SOCK_DGRAM //nolint:revive
SOF_TIMESTAMPING_OPT_TSONLY = unix.SOF_TIMESTAMPING_OPT_TSONLY //nolint:revive
SOF_TIMESTAMPING_RAW_HARDWARE = unix.SOF_TIMESTAMPING_RAW_HARDWARE //nolint:revive
SOF_TIMESTAMPING_RX_HARDWARE = unix.SOF_TIMESTAMPING_RX_HARDWARE //nolint:revive
SOF_TIMESTAMPING_RX_SOFTWARE = unix.SOF_TIMESTAMPING_RX_SOFTWARE //nolint:revive
SOF_TIMESTAMPING_SOFTWARE = unix.SOF_TIMESTAMPING_SOFTWARE //nolint:revive
SOF_TIMESTAMPING_TX_HARDWARE = unix.SOF_TIMESTAMPING_TX_HARDWARE //nolint:revive
SOF_TIMESTAMPING_TX_SOFTWARE = unix.SOF_TIMESTAMPING_TX_SOFTWARE //nolint:revive
SOL_SOCKET = unix.SOL_SOCKET //nolint:revive
SO_SELECT_ERR_QUEUE = unix.SO_SELECT_ERR_QUEUE //nolint:revive
SO_TIMESTAMPING_NEW = unix.SO_TIMESTAMPING_NEW //nolint:revive
SO_TIMESTAMPING = unix.SO_TIMESTAMPING //nolint:revive
SYS_CLOCK_SETTIME = unix.SYS_CLOCK_SETTIME //nolint:revive
SYS_IOCTL = unix.SYS_IOCTL //nolint:revive
SYS_RECVMSG = unix.SYS_RECVMSG //nolint:revive
TIME_OK = unix.TIME_OK //nolint:revive
SOF_TIMESTAMPING_RX_HARDWARE = unix.SOF_TIMESTAMPING_RX_HARDWARE //nolint:revive
SOF_TIMESTAMPING_RX_SOFTWARE = unix.SOF_TIMESTAMPING_RX_SOFTWARE //nolint:revive
SOF_TIMESTAMPING_SOFTWARE = unix.SOF_TIMESTAMPING_SOFTWARE //nolint:revive
SOF_TIMESTAMPING_TX_HARDWARE = unix.SOF_TIMESTAMPING_TX_HARDWARE //nolint:revive
SOF_TIMESTAMPING_TX_SOFTWARE = unix.SOF_TIMESTAMPING_TX_SOFTWARE //nolint:revive
SOL_SOCKET = unix.SOL_SOCKET //nolint:revive
SO_SELECT_ERR_QUEUE = unix.SO_SELECT_ERR_QUEUE //nolint:revive
SO_TIMESTAMPING_NEW = unix.SO_TIMESTAMPING_NEW //nolint:revive
SO_TIMESTAMPING = unix.SO_TIMESTAMPING //nolint:revive
SYS_CLOCK_SETTIME = unix.SYS_CLOCK_SETTIME //nolint:revive
SYS_IOCTL = unix.SYS_IOCTL //nolint:revive
SYS_RECVMSG = unix.SYS_RECVMSG //nolint:revive
TIME_OK = unix.TIME_OK //nolint:revive
)

var (
Expand Down
17 changes: 17 additions & 0 deletions phc/unix/linux_386.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [16]byte
}

const (
PTP_CLOCK_GETCAPS = 0x80503d01
PTP_CLOCK_GETCAPS2 = 0x80503d0a
PTP_ENABLE_PPS = 0x40043d04
PTP_ENABLE_PPS2 = 0x40043d0d
PTP_EXTTS_REQUEST = 0x40103d02
PTP_EXTTS_REQUEST2 = 0x40103d0b
PTP_MASK_CLEAR_ALL = 0x3d13
PTP_MASK_EN_SINGLE = 0x40043d14
PTP_PEROUT_REQUEST = 0x40383d03
PTP_PEROUT_REQUEST2 = 0x40383d0c
PTP_PIN_SETFUNC = 0x40603d07
PTP_PIN_SETFUNC2 = 0x40603d10
PTP_SYS_OFFSET = 0x43403d05
PTP_SYS_OFFSET2 = 0x43403d0e
)
17 changes: 17 additions & 0 deletions phc/unix/linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [24]byte
}

const (
PTP_CLOCK_GETCAPS = 0x80503d01

Check warning on line 27 in phc/unix/linux_amd64.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use ALL_CAPS in Go names; use CamelCase (revive)
PTP_CLOCK_GETCAPS2 = 0x80503d0a

Check warning on line 28 in phc/unix/linux_amd64.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use ALL_CAPS in Go names; use CamelCase (revive)
PTP_ENABLE_PPS = 0x40043d04

Check warning on line 29 in phc/unix/linux_amd64.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use ALL_CAPS in Go names; use CamelCase (revive)
PTP_ENABLE_PPS2 = 0x40043d0d
PTP_EXTTS_REQUEST = 0x40103d02
PTP_EXTTS_REQUEST2 = 0x40103d0b
PTP_MASK_CLEAR_ALL = 0x3d13
PTP_MASK_EN_SINGLE = 0x40043d14
PTP_PEROUT_REQUEST = 0x40383d03
PTP_PEROUT_REQUEST2 = 0x40383d0c
PTP_PIN_SETFUNC = 0x40603d07
PTP_PIN_SETFUNC2 = 0x40603d10
PTP_SYS_OFFSET = 0x43403d05
PTP_SYS_OFFSET2 = 0x43403d0e
)
17 changes: 17 additions & 0 deletions phc/unix/linux_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [16]byte
}

const (
PTP_CLOCK_GETCAPS = 0x80503d01
PTP_CLOCK_GETCAPS2 = 0x80503d0a
PTP_ENABLE_PPS = 0x40043d04
PTP_ENABLE_PPS2 = 0x40043d0d
PTP_EXTTS_REQUEST = 0x40103d02
PTP_EXTTS_REQUEST2 = 0x40103d0b
PTP_MASK_CLEAR_ALL = 0x3d13
PTP_MASK_EN_SINGLE = 0x40043d14
PTP_PEROUT_REQUEST = 0x40383d03
PTP_PEROUT_REQUEST2 = 0x40383d0c
PTP_PIN_SETFUNC = 0x40603d07
PTP_PIN_SETFUNC2 = 0x40603d10
PTP_SYS_OFFSET = 0x43403d05
PTP_SYS_OFFSET2 = 0x43403d0e
)
17 changes: 17 additions & 0 deletions phc/unix/linux_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [24]byte
}

const (
PTP_CLOCK_GETCAPS = 0x80503d01
PTP_CLOCK_GETCAPS2 = 0x80503d0a
PTP_ENABLE_PPS = 0x40043d04
PTP_ENABLE_PPS2 = 0x40043d0d
PTP_EXTTS_REQUEST = 0x40103d02
PTP_EXTTS_REQUEST2 = 0x40103d0b
PTP_MASK_CLEAR_ALL = 0x3d13
PTP_MASK_EN_SINGLE = 0x40043d14
PTP_PEROUT_REQUEST = 0x40383d03
PTP_PEROUT_REQUEST2 = 0x40383d0c
PTP_PIN_SETFUNC = 0x40603d07
PTP_PIN_SETFUNC2 = 0x40603d10
PTP_SYS_OFFSET = 0x43403d05
PTP_SYS_OFFSET2 = 0x43403d0e
)
17 changes: 17 additions & 0 deletions phc/unix/linux_loong64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [24]byte
}

const (
PTP_CLOCK_GETCAPS = 0x80503d01
PTP_CLOCK_GETCAPS2 = 0x80503d0a
PTP_ENABLE_PPS = 0x40043d04
PTP_ENABLE_PPS2 = 0x40043d0d
PTP_EXTTS_REQUEST = 0x40103d02
PTP_EXTTS_REQUEST2 = 0x40103d0b
PTP_MASK_CLEAR_ALL = 0x3d13
PTP_MASK_EN_SINGLE = 0x40043d14
PTP_PEROUT_REQUEST = 0x40383d03
PTP_PEROUT_REQUEST2 = 0x40383d0c
PTP_PIN_SETFUNC = 0x40603d07
PTP_PIN_SETFUNC2 = 0x40603d10
PTP_SYS_OFFSET = 0x43403d05
PTP_SYS_OFFSET2 = 0x43403d0e
)
17 changes: 17 additions & 0 deletions phc/unix/linux_mips.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [16]byte
}

const (
PTP_CLOCK_GETCAPS = 0x40503d01
PTP_CLOCK_GETCAPS2 = 0x40503d0a
PTP_ENABLE_PPS = 0x80043d04
PTP_ENABLE_PPS2 = 0x80043d0d
PTP_EXTTS_REQUEST = 0x80103d02
PTP_EXTTS_REQUEST2 = 0x80103d0b
PTP_MASK_CLEAR_ALL = 0x20003d13
PTP_MASK_EN_SINGLE = 0x80043d14
PTP_PEROUT_REQUEST = 0x80383d03
PTP_PEROUT_REQUEST2 = 0x80383d0c
PTP_PIN_SETFUNC = 0x80603d07
PTP_PIN_SETFUNC2 = 0x80603d10
PTP_SYS_OFFSET = 0x83403d05
PTP_SYS_OFFSET2 = 0x83403d0e
)
17 changes: 17 additions & 0 deletions phc/unix/linux_mips64.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [24]byte
}

const (
PTP_CLOCK_GETCAPS = 0x40503d01
PTP_CLOCK_GETCAPS2 = 0x40503d0a
PTP_ENABLE_PPS = 0x80043d04
PTP_ENABLE_PPS2 = 0x80043d0d
PTP_EXTTS_REQUEST = 0x80103d02
PTP_EXTTS_REQUEST2 = 0x80103d0b
PTP_MASK_CLEAR_ALL = 0x20003d13
PTP_MASK_EN_SINGLE = 0x80043d14
PTP_PEROUT_REQUEST = 0x80383d03
PTP_PEROUT_REQUEST2 = 0x80383d0c
PTP_PIN_SETFUNC = 0x80603d07
PTP_PIN_SETFUNC2 = 0x80603d10
PTP_SYS_OFFSET = 0x83403d05
PTP_SYS_OFFSET2 = 0x83403d0e
)
17 changes: 17 additions & 0 deletions phc/unix/linux_mips64le.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type ifreq struct {
Ifrn [16]byte
Ifru [24]byte
}

const (
PTP_CLOCK_GETCAPS = 0x40503d01
PTP_CLOCK_GETCAPS2 = 0x40503d0a
PTP_ENABLE_PPS = 0x80043d04
PTP_ENABLE_PPS2 = 0x80043d0d
PTP_EXTTS_REQUEST = 0x80103d02
PTP_EXTTS_REQUEST2 = 0x80103d0b
PTP_MASK_CLEAR_ALL = 0x20003d13
PTP_MASK_EN_SINGLE = 0x80043d14
PTP_PEROUT_REQUEST = 0x80383d03
PTP_PEROUT_REQUEST2 = 0x80383d0c
PTP_PIN_SETFUNC = 0x80603d07
PTP_PIN_SETFUNC2 = 0x80603d10
PTP_SYS_OFFSET = 0x83403d05
PTP_SYS_OFFSET2 = 0x83403d0e
)
Loading

0 comments on commit 51b6cb0

Please sign in to comment.