Latest Linux kernel has supported getting active port's PHC from bond
directly. This would help topology like VLAN over bond to get PHC natively,
as VLAN also albe to get downlink's PHC.

To achieve this, the new hwtstamp flag is needed when passing hwtstamp_config
to kernel. Let's supply the flag first, and fall back without flag if user
run on old kernel.

We also need to update the PHC index in port_link_status() when there is
a RTNL event. The PHC info will be update in later port_change_phc().

Signed-off-by: Hangbin Liu <liuhang...@gmail.com>
---
v7: Move port_link_status() changes to a separate patch. And still call
    port_change_phc() via port_link_status() incase we miss the link down event.
v6: revert changes in clock_create(). Update ts_label by force and do
    interface_get_tsinfo() only once.
v5: no update
v4: define HWTSTAMP_FLAG_BONDED_PHC_INDEX in missing.h
    remove unneeded net_tstamp header files
v3: remove ifdef in clock_create() and FD_RTNL event.
v2: remove link state PHC_INDEX_CHANGED and use TS_LABEL_CHANGED only.
---
 clock.c   |  3 +++
 missing.h |  6 ++++++
 port.c    | 12 +++++++++---
 sk.c      |  5 +++++
 4 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/clock.c b/clock.c
index 7be021f..16ca6ed 100644
--- a/clock.c
+++ b/clock.c
@@ -1767,6 +1767,9 @@ int clock_switch_phc(struct clock *c, int phc_index)
        c->clkid = clkid;
        c->servo = servo;
        c->servo_state = SERVO_UNLOCKED;
+
+       pr_info("Switched to /dev/ptp%d as PTP clock", phc_index);
+
        return 0;
 }
 
diff --git a/missing.h b/missing.h
index 20f7193..4c7ac57 100644
--- a/missing.h
+++ b/missing.h
@@ -73,6 +73,12 @@ struct so_timestamping {
 };
 #endif
 
+#ifndef HWTSTAMP_FLAG_BONDED_PHC_INDEX
+enum {
+       HWTSTAMP_FLAG_BONDED_PHC_INDEX = (1<<0),
+};
+#endif
+
 #ifdef PTP_EXTTS_REQUEST2
 #define PTP_EXTTS_REQUEST_FAILED "PTP_EXTTS_REQUEST2 failed: %m"
 #else
diff --git a/port.c b/port.c
index 685232a..ce1ff33 100644
--- a/port.c
+++ b/port.c
@@ -2733,12 +2733,18 @@ void port_link_status(void *ctx, int linkup, int 
ts_index)
                pr_notice("%s: ts label changed to %s", p->log_name, ts_label);
        }
 
+       /* phc index may changed while ts_label keeps the same after failover.
+        * e.g. vlan over bond. Since the lower link changed, we still set
+        * the TS_LABEL_CHANGED flag.
+        */
+       interface_get_tsinfo(p->iface);
+       if (p->phc_index != interface_phc_index(p->iface))
+               p->link_status |= TS_LABEL_CHANGED;
+
        /* Both link down/up and change ts_label may change phc index. */
        if (p->link_status & LINK_UP &&
-           (p->link_status & LINK_STATE_CHANGED || p->link_status & 
TS_LABEL_CHANGED)) {
-               interface_get_tsinfo(p->iface);
+           (p->link_status & LINK_STATE_CHANGED || p->link_status & 
TS_LABEL_CHANGED))
                port_change_phc(p);
-       }
 
        /*
         * A port going down can affect the BMCA result.
diff --git a/sk.c b/sk.c
index b55d6b5..80075be 100644
--- a/sk.c
+++ b/sk.c
@@ -66,6 +66,11 @@ static int hwts_init(int fd, const char *device, int 
rx_filter,
 
        init_ifreq(&ifreq, &cfg, device);
 
+       cfg.flags = HWTSTAMP_FLAG_BONDED_PHC_INDEX;
+       /* Fall back without flag if user run new build on old kernel */
+       if (ioctl(fd, SIOCGHWTSTAMP, &ifreq) == -EINVAL)
+               init_ifreq(&ifreq, &cfg, device);
+
        switch (sk_hwts_filter_mode) {
        case HWTS_FILTER_CHECK:
                err = ioctl(fd, SIOCGHWTSTAMP, &ifreq);
-- 
2.35.1



_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to