Skip to content

neturils/ptpd: various improvements - #3789

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
wenquan2015:ptpd-improvements
Sep 18, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
wenquan2015:ptpd-improvements

Conversation

@wenquan2015

Copy link
Copy Markdown

Summary

Replace the sigqueue + shared-memory IPC mechanism in
ptpd_status() with a file-based approach that works across all
NuttX build modes (Flat, Protected, Kernel).

The old implementation passed a pointer to caller-owned memory via
sigqueue(SIGUSR1, sival_ptr), which only works in CONFIG_BUILD_FLAT
and returned -ENOTSUP for Protected/Kernel builds. The new approach:

  • Daemon side: on SIGUSR1, writes the ptpd_status_s struct to
    a temp file and atomically renames it to the status path. The
    dump flag is a simple bool checked in the main loop.
  • Client side (ptpd_status()): deletes any stale status file,
    sends kill(pid, SIGUSR1), polls for the file to appear (up to 3s),
    reads the struct back.

The atomic temp + rename pattern ensures readers never see a partial
write. The status file path is configurable via a new Kconfig option
NETUTILS_PTPD_STATUSFILE (default /tmp/ptpd.status).

  • Rewrite ptpd_status() — remove #ifdef CONFIG_BUILD_FLAT guard
  • Add NETUTILS_PTPD_STATUSFILE string config to Kconfig
  • Add #include <sys/stat.h> for O_CREAT mode bits

Impact

  • ptpd_status() now works in Protected and Kernel builds (previously
    returned -ENOTSUP).
  • The status file (/tmp/ptpd.status) is created on demand when
    SIGUSR1 is received; it does not exist unless status is queried.
  • No API signature changes — callers of ptpd_status(int pid, struct ptpd_status_s *) are unaffected.
  • New Kconfig: NETUTILS_PTPD_STATUSFILE (string, default
    /tmp/ptpd.status).

Testing

Build: sim:tcpblaster with CONFIG_NET_IGMP=y,
CONFIG_NETUTILS_PTPD=y, CONFIG_PTP_CLOCK=y,
CONFIG_CLOCK_ADJTIME=y added.

Host: Linux x86_64, GCC.

Runtime verification on NuttX sim — status query via file IPC:

nsh> ifup eth0
ifup eth0...OK
nsh> ptpd -S -i eth0 &
ptpd [10:100]
nsh> ptpd -t 10
PTPD (PID 10) status:

  • clock_source_valid: 0
  • last_clock_update: 1970-01-01T00:00:00.000000000
  • last_delta_ns: 0
  • last_adjtime_ns: 0
  • drift_ppb: 0
  • path_delay_ns: 0
  • last_received_multicast: 11 s ago
  • last_received_announce: 94 s ago
  • last_received_sync: 94 s ago
  • last_transmitted_sync: 0 s ago
  • last_transmitted_announce: 94 s ago
  • last_transmitted_delayresp: 94 s ago
  • last_transmitted_delayreq: 94 s ago

nsh> ptpd -d 10
Stopped ptpd

ptpd -t 10 successfully retrieves daemon status through the new
SIGUSR1 → status-file → read-back path. The daemon writes
/tmp/ptpd.status atomically; the client detects it within one poll
cycle (~100ms).

@wenquan2015

Copy link
Copy Markdown
Author

@xiaoxiang781216 @daniel-p-carvalho please review this pr

@daniel-p-carvalho

Copy link
Copy Markdown
Contributor

Hi @wenquan2015, thanks for tagging!

The changes look great — moving the IGMP join after SIOCGIFADDR and removing the CONFIG_BUILD_FLAT restriction via the atomic status file are solid improvements.

Just a heads up regarding PR #3782: it adds P2P delay support, which introduces the P2P multicast join (224.0.0.107) and a last_transmitted_pdelayreq field in struct ptpd_status_s.

Whichever PR is merged first, the rebase will be trivial to align both (placing the P2P join in your new post-SIOCGIFADDR block and dumping the new status field in ptp_dump_status_file()).

LGTM!

wenquan1 added 2 commits September 18, 2026 10:19
The IGMP multicast join (ipmsfilter) was previously called before
the interface address (interface_addr) was populated via SIOCGIFADDR.
This meant the IGMP join had to locate the network device without
a valid local address, which could fail or join on the wrong interface.

Move the multicast group subscription to after the interface address
is queried, and guard it with an AF_INET check since IGMP only applies
to IPv4. This ensures the IGMP join can always locate the correct
network device.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
Replace the sigqueue + shared-memory IPC mechanism in ptpd_status()
with a file-based approach:

  - Daemon side: on SIGUSR1, write a binary ptpd_status_s struct to
    a temp file and atomically rename it to the status path.
  - Client side: send kill(SIGUSR1), poll for the file to appear,
    then read the struct back.

This removes the CONFIG_BUILD_FLAT restriction (the old code returned
-ENOTSUP for Protected and Kernel builds) and avoids passing pointers
across address spaces via sigqueue.  The status file path is
configurable via NETUTILS_PTPD_STATUSFILE (default /tmp/ptpd.status).

The atomic temp + rename pattern ensures readers never see a partial
write.

Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
@jerpelea jerpelea changed the title Ptpd improvements neturils/ptpd: various improvements Sep 18, 2026
@xiaoxiang781216
xiaoxiang781216 merged commit e3e3933 into apache:master Sep 18, 2026
41 checks passed
daniel-p-carvalho added a commit to daniel-p-carvalho/nuttx-apps that referenced this pull request Sep 18, 2026
…tus()

PR apache#3789 replaced the in-memory sigqueue + shared memory IPC in ptpd_status()
with file-based IPC to support Protected and Kernel modes across address
spaces. However, on microcontrollers running CONFIG_BUILD_FLAT, a filesystem
or /tmp (TMPFS) is rarely mounted or available, causing ptpd_status() to fail
with -ETIMEDOUT (errno 110) because the status file cannot be created.

Retain the file-based IPC for !CONFIG_BUILD_FLAT (Protected and Kernel modes)
while restoring the zero-overhead in-memory sigqueue + semaphore IPC for
CONFIG_BUILD_FLAT. Both modes share the status serialization logic via
ptp_populate_status() and support all fields including P2P.

Assisted-by: Gemini:gemini-3.8-flash-medium
Signed-off-by: Daniel P. Carvalho <danieloak@gmail.com>
daniel-p-carvalho added a commit to daniel-p-carvalho/nuttx-apps that referenced this pull request Sep 20, 2026
…tus()

PR apache#3789 replaced the in-memory sigqueue + shared memory IPC in ptpd_status()
with file-based IPC to support Protected and Kernel modes across address
spaces. However, on microcontrollers running CONFIG_BUILD_FLAT, a filesystem
or /tmp (TMPFS) is rarely mounted or available, causing ptpd_status() to fail
with -ETIMEDOUT (errno 110) because the status file cannot be created.

Retain the file-based IPC for !CONFIG_BUILD_FLAT (Protected and Kernel modes)
while restoring the zero-overhead in-memory sigqueue + semaphore IPC for
CONFIG_BUILD_FLAT. Both modes share the status serialization logic via
ptp_populate_status() and support all fields including P2P.

Assisted-by: Gemini:gemini-3.8-flash-medium
Signed-off-by: Daniel P. Carvalho <danieloak@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants