Skip to content

Commit 44fac1c

Browse files
committed
unix: add PTP IOCTLs on Linux
The IOCTLs are used for interacting with PTP-specific functions of NIC and time card drivers. Description: https://netdevconf.info/0x18/docs/netdev-0x18-paper39-talk-slides/netdev-intro-ptp-api.pdf
1 parent a57fdb8 commit 44fac1c

21 files changed

+381
-0
lines changed

unix/ioctl_linux.go

+55
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,61 @@ func IoctlSetHwTstamp(fd int, ifname string, cfg *HwTstampConfig) error {
9999
return ioctlIfreqData(fd, SIOCSHWTSTAMP, &ifrd)
100100
}
101101

102+
// FdToClockID derives the clock ID from the file descriptor number
103+
// - see clock_gettime(3), FD_TO_CLOCKID macros. The resulting ID is
104+
// suitable for system calls like ClockGettime.
105+
func FdToClockID(fd int) int32 { return int32((int(^fd) << 3) | 3) }
106+
107+
// IoctlPtpClockGetcaps returns the description of a given PTP device.
108+
func IoctlPtpClockGetcaps(fd int) (*PtpClockCaps, error) {
109+
var value PtpClockCaps
110+
err := ioctlPtr(fd, PTP_CLOCK_GETCAPS2, unsafe.Pointer(&value))
111+
return &value, err
112+
}
113+
114+
// IoctlPtpSysOffsetPrecise returns a description of the clock
115+
// offset compared to the system clock.
116+
func IoctlPtpSysOffsetPrecise(fd int) (*PtpSysOffsetPrecise, error) {
117+
var value PtpSysOffsetPrecise
118+
err := ioctlPtr(fd, PTP_SYS_OFFSET_PRECISE2, unsafe.Pointer(&value))
119+
return &value, err
120+
}
121+
122+
// IoctlPtpSysOffsetExtended returns an extended description of the
123+
// clock offset compared to the system clock. The samples parameter
124+
// specifies the desired number of measurements.
125+
func IoctlPtpSysOffsetExtended(fd int, samples uint) (*PtpSysOffsetExtended, error) {
126+
value := PtpSysOffsetExtended{Samples: uint32(samples)}
127+
err := ioctlPtr(fd, PTP_SYS_OFFSET_EXTENDED2, unsafe.Pointer(&value))
128+
return &value, err
129+
}
130+
131+
// IoctlPtpPinGetfunc returns the configuration of the specified
132+
// I/O pin on given PTP device.
133+
func IoctlPtpPinGetfunc(fd int, index uint) (*PtpPinDesc, error) {
134+
value := PtpPinDesc{Index: uint32(index)}
135+
err := ioctlPtr(fd, PTP_PIN_GETFUNC2, unsafe.Pointer(&value))
136+
return &value, err
137+
}
138+
139+
// IoctlPtpPinSetfunc updates configuration of the specified PTP
140+
// I/O pin.
141+
func IoctlPtpPinSetfunc(fd int, pd *PtpPinDesc) error {
142+
return ioctlPtr(fd, PTP_PIN_SETFUNC2, unsafe.Pointer(pd))
143+
}
144+
145+
// IoctlPtpPeroutRequest configures the periodic output mode of the
146+
// PTP I/O pins.
147+
func IoctlPtpPeroutRequest(fd int, r *PtpPeroutRequest) error {
148+
return ioctlPtr(fd, PTP_PEROUT_REQUEST2, unsafe.Pointer(r))
149+
}
150+
151+
// IoctlPtpExttsRequest configures the external timestamping mode
152+
// of the PTP I/O pins.
153+
func IoctlPtpExttsRequest(fd int, r *PtpExttsRequest) error {
154+
return ioctlPtr(fd, PTP_EXTTS_REQUEST2, unsafe.Pointer(r))
155+
}
156+
102157
// IoctlGetWatchdogInfo fetches information about a watchdog device from the
103158
// Linux watchdog API. For more information, see:
104159
// https://www.kernel.org/doc/html/latest/watchdog/watchdog-api.html.

unix/linux/types.go

+22
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ struct termios2 {
151151
#include <linux/openat2.h>
152152
#include <linux/perf_event.h>
153153
#include <linux/pps.h>
154+
#include <linux/ptp_clock.h>
154155
#include <linux/random.h>
155156
#include <linux/rtc.h>
156157
#include <linux/rtnetlink.h>
@@ -525,6 +526,15 @@ struct cachestat {
525526
__u64 nr_evicted;
526527
__u64 nr_recently_evicted;
527528
};
529+
530+
// the one defined in linux/ptp_clock.h has unions
531+
struct my_ptp_perout_request {
532+
struct ptp_clock_time startOrPhase; // start or phase
533+
struct ptp_clock_time period;
534+
unsigned int index;
535+
unsigned int flags;
536+
struct ptp_clock_time on;
537+
};
528538
*/
529539
import "C"
530540

@@ -4126,6 +4136,18 @@ const (
41264136
HWTSTAMP_TX_ONESTEP_SYNC = C.HWTSTAMP_TX_ONESTEP_SYNC
41274137
)
41284138

4139+
type (
4140+
PtpClockCaps C.struct_ptp_clock_caps
4141+
PtpClockTime C.struct_ptp_clock_time
4142+
PtpExttsEvent C.struct_ptp_extts_event
4143+
PtpExttsRequest C.struct_ptp_extts_request
4144+
PtpPeroutRequest C.struct_my_ptp_perout_request
4145+
PtpPinDesc C.struct_ptp_pin_desc
4146+
PtpSysOffset C.struct_ptp_sys_offset
4147+
PtpSysOffsetExtended C.struct_ptp_sys_offset_extended
4148+
PtpSysOffsetPrecise C.struct_ptp_sys_offset_precise
4149+
)
4150+
41294151
type (
41304152
HIDRawReportDescriptor C.struct_hidraw_report_descriptor
41314153
HIDRawDevInfo C.struct_hidraw_devinfo

unix/mkerrors.sh

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ struct ltchars {
266266
#include <linux/nsfs.h>
267267
#include <linux/perf_event.h>
268268
#include <linux/pps.h>
269+
#include <linux/ptp_clock.h>
269270
#include <linux/ptrace.h>
270271
#include <linux/random.h>
271272
#include <linux/reboot.h>
@@ -537,6 +538,7 @@ ccflags="$@"
537538
$2 ~ /^(AF|SOCK|SO|SOL|IPPROTO|IP|IPV6|TCP|MCAST|EVFILT|NOTE|SHUT|PROT|MAP|MREMAP|MFD|T?PACKET|MSG|SCM|MCL|DT|MADV|PR|LOCAL|TCPOPT|UDP)_/ ||
538539
$2 ~ /^NFC_(GENL|PROTO|COMM|RF|SE|DIRECTION|LLCP|SOCKPROTO)_/ ||
539540
$2 ~ /^NFC_.*_(MAX)?SIZE$/ ||
541+
$2 ~ /^PTP_/ ||
540542
$2 ~ /^RAW_PAYLOAD_/ ||
541543
$2 ~ /^[US]F_/ ||
542544
$2 ~ /^TP_STATUS_/ ||

unix/mkpost.go

+9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ func main() {
185185
b = bytes.Replace(b, s, newNames, 1)
186186
}
187187

188+
// Convert []int8 to []byte in PtpPinDesc
189+
ptpBytesRegex := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]u?int8`)
190+
ptpIoctlType := regexp.MustCompile(`PtpPinDesc\s+struct {[^}]*}`)
191+
ptpStructs := ptpIoctlType.FindAll(b, -1)
192+
for _, s := range ptpStructs {
193+
newNames := ptpBytesRegex.ReplaceAll(s, []byte("$1$2[$3]byte"))
194+
b = bytes.Replace(b, s, newNames, 1)
195+
}
196+
188197
// Convert []int8 to []byte in ctl_info ioctl interface
189198
convertCtlInfoName := regexp.MustCompile(`(Name)(\s+)\[(\d+)\]int8`)
190199
ctlInfoType := regexp.MustCompile(`type CtlInfo struct {[^}]*}`)

unix/zerrors_linux.go

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_386.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_amd64.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_arm.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_arm64.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_loong64.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_mips.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_mips64.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_mips64le.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_mipsle.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_ppc.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

unix/zerrors_linux_ppc64.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)