* [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp
@ 2026-12-03 17:44 Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 1/8] hsr: Add header_ops::parse_protocol Sebastian Andrzej Siewior
` (7 more replies)
0 siblings, 8 replies; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
I am trying to extend linuxptp to support PTP over a HSR network.
This is the kernel side of the changes. In short PTP over HSR sends its
packets to a multicast address and every node needs to forward the PTP
packet (SYNC and FOLLOW-UP for instance) within the HSR ring.
In order to achieve this, the HSR stack must not duplicate and forward
the PTP packets as it would do with other packets. The delay caused by
the duplication and forwarding adds overhead which in turn makes the
timing information within the PTP packet inaccurate.
The PTP stack needs to forward the packets so it is aware of the delay
and can update the PTP packet (the FOLLOW-UP) accordingly to the delay.
My current approach is to open the slave devices (eth0/ eth1) from
userland in order to receive the PTP packets. Sending happens from the
hsr0 device. The actual packet has an inline header prepended of type
struct hsr_inline_header. The size of the header is equivalent to
ethhdr. The header has a type (h_proto) at the same position as ethhdr
and expects it to be ETH_P_1588 as this extra meta information is only
relevant for PTP packets. It makes no sense to send PTP packets via the
HSR interface because it gets duplicated and the timestamp information
is lost so this should not break anything. As an additional safe guard
there is a magic value at h_source position. The value has '0xaf' at the
most significant byte which makes the address a locally administered
multicast address.
The header passes two information from userland: On which slave port
the packet has to be sent and does the HSR stack need to prepend a
header or not.
hsr_inline_header::{has_hsr_header = 1, req_tx_port = HSR_INLINE_HDR_PORT_A]
will send the packet on port PORT_A and it carries already a HSR tag.
hsr_inline_header::{has_hsr_header = 0, req_tx_port = HSR_INLINE_HDR_PORT_B]
will send the packet on port PORT_B and add system's HSR tag.
The header is skipped so that the remaining stack sees the actual data
and can send it as requested.
The PRP packets are sent directly via the SLAVE interface. The standard
mandates not add a PRP trailer (PRP, redundancy control trailer) to PTP
packets. There is not really a reason to use hsr interface.
HSR hardware offloading is optional. The driver needs to know if the
operating mode is HSR or PRP. In PRP mode it needs to check the ether
type and for ETH_P_1588 it must not perform any offloading.
In HSR mode, for ether-type ETH_P_1588 there must be no offloading. If
the ether-type is ETH_P_HSR there must be no offloading if the
encapsulated protocol is ETH_P_1588.
Driver with offloading support enabled need to be updated to handle
these cases correctly. Also their firmware may need to be updated if
cut-through forwarding is supported. This is not part of the series.
This has been tested in a pure software environment and in an HW-assisted
environment where the HW is able to duplicate and deduplicate packets
but does not do it for PTP packets.
It has not been tested within an environment where the HW is able to
forward the PTP packet and correctly update the timing information.
---
v13…v14: https://patch.msgid.link/20261130-hsr_ptp-v13-0-598326208fc5@linutronix.de
- Add a delay in hsr_ptp.sh after link set up, so that the asynchronous
linkwatch work comes through (sashiko).
- Add BUILD_BUG_ON(HSR_PT_SLAVE_x != HSR_INLINE_HDR_PORT_x) as safety
guard that none re-orders the enum (sashiko).
- Include linux/hsr_ptp.h instead uapi/ (sashiko).
v12…v13: https://patch.msgid.link/20261125-hsr_ptp-v12-0-9ddd2a73cb90@linutronix.de
- In hsr_ptp.sh, the explicit init of ns1-3 (in order to silence SC2154)
led to not creating random namespaces at all. This is now reverted and
shellcheck is told to ignore SC2154 instead.
v11…v12: https://patch.msgid.link/20261120-hsr_ptp-v11-0-264c88f4f1d0@linutronix.de
- Added tools/include/uapi/linux/hsr_ptp.h to HSR entry in MAINTAINERS
- Initialized the ns1-3 variables in hsr_ptp.sh to avoid triggering
SC2154. While at it, also dropped `ipv6' which is not used.
- Sashiko pointed out that hsr_ptp_parse_header() could accept frames
from remote hosts "OVS or nft fwd/dup" forwards this untrusted
traffic and it matches the custom header. While I still don't think it
is a sane setup, checking for skb->sk does not cost much.
v10…v11: https://patch.msgid.link/20261104-hsr_ptp-v10-8-2c4a38ad973c@linutronix.de
- Moved uapi/linux/hsr_ptp.h to hsr_device where it is used.
- Moved inlines header parsing into its own function
(hsr_ptp_parse_header())
- hsr_ptp_parse_header() now unconditionally does
skb_reset_mac_header(). sahshiko keeps complaining about the
conditional reset.
v9…v10: https://patch.msgid.link/20261014-hsr_ptp-v9-0-2d20a2a0f816@linutronix.de
- A few patches got their commit description updated/ corrected as
suggested by Daniel Machon.
- A few variables (local and uAPI) have been renamed to get alligned:
- tx_port -> req_tx_port
- hsr_hdr has_header has_foreign_header -> has_hsr_header
Suggested by Daniel Machon.
- hsr_forward_do() got two comments updated to be more clear what it
does. Suggested by Daniel Machon.
- sashiko
- noticed that skb_cow_head() needs also be adjusted by the size of
skb_metadata_len()
- hsr_dev_xmit() verifies that __pad0 and __pad1 of the supplied
hsr_inline_header is 0.
- hsr_dev_xmit() does skb_reset_mac_header() if the MAC header was not
yet set.
- Earlier sashiko pointed out that #4 does not consider VLAN headers
in hsr_handle_frame() while checking for PTP packets. I updated the
commit message saying that I don't check/ ignore VLAN because I have
no working setup and can't validate that the whole chain is working.
Now sashiko pointed out that this is not true (not checking VLAN)
because __netif_receive_skb_core() "unpacks" the VLAN header via
skb_vlan_untag(). This looks correct so I removed that part from the
commit message since it should the right thing here. However it is
still not verified.
- The selftest has been updated to verify that SRC-MAC and the HSR
sequence number is the supplied one if the HSR header was supplied.
v8…v9: https://patch.msgid.link/20261002-hsr_ptp-v8-0-60dabc07e554@linutronix.de
- Use skb_postpush_data_move() for the data move
- A few patches got their description updated so that the bot does
hopefully not complain about a few things that are meant to be (such
as skipping PRP).
v7…v8: https://patch.msgid.link/20260928-hsr_ptp-v7-0-d55d304d9a7e@linutronix.de
- Use __u8 instead of uint8_t, add header file for in hsr_ptp.h
- Add header file to if_hsr.h for struct ethhdr
- Remove the inline keyword from hsr_ptp_test.c
v6…v7: https://patch.msgid.link/20260923-hsr_ptp-v6-0-6ea07b3fb8a8@linutronix.de
- Change the logic in hsr_create_tagged_frame():
- Don't update ->csum_start (the cloning takes care of this)
- pskb_may_pull() does not need to include HSR_HLEN (it only copies
'movelen')
- Set network header to 'movelen' which takes VLAN into account
- Drop also PTP packets which were received on HSR_PT_INTERLINK (not
just HSR A/B).
- Move include HSR header to include/uapi/linux/hsr_ptp.h, so it is also
available in userland.
- selftests:
- Add hsr_ptp.sh alphabetically ordered to the Makefile
- Use "$0" in hsr_ptp.sh
- Drop unnused arguments in open_socket()
v5…v6: https://lore.kernel.org/r/20260527-hsr_ptp-v5-0-158a7633eac0@linutronix.de
- hsr_ptp_test: Let it wait up to 100ms for a packet. Otherwise it will
complain if the recevied packet is not already in socket.
- Use TEST_GEN_PROGS instead TEST_GEN_FILES, the test can not run
without additional arguments.
- Make the inline header mandatory for sending ETH_P_1588 packets via
the HSR device.
- Use kfree_skb() instead kfree() for the skb.
- Rephrase the commit message saying that skb_clone() shares
skb_shared_info and does not copy it.
v4…v5: https://lore.kernel.org/r/20260508-hsr_ptp-v4-1-aa19aa7c6a71@linutronix.de
- Split the patch into smaller pieces
- Added a test for the added inline header (which signals the port while
sending packets).
- Replaced __pskb_copy() with skb_clone() + skb_cow_head() in
hsr_create_tagged_frame() to preserve timestamp request.
v3…v4: https://lore.kernel.org/r/20260429-hsr_ptp-v3-1-afbf8f200f48@linutronix.de
- Removed skb extention. The information within HSR is passed via
struct hsr_frame_info. Driver with HSR-offloading capabilities need to
know the HSR mode (HSR or PRP) and parse the skb to decide what needs
to be done (whether to send on both ports and if adding a header is
needed).
v2…v3: https://patch.msgid.link/20260309-hsr_ptp-v2-0-798262aad3a4@linutronix.de
- Remove af_packet changes entirely.
- Add an internal header to pass additional information for HSR-PTP
packets.
- Remove PRP, userland will use slave devices directly.
- Drop all received PTP packets. Userland needs to use the slave device
for RX.
v1…v2: https://patch.msgid.link/20260204-hsr_ptp-v1-0-b421c69a77da@linutronix.de
- Added PRP support
- skb extention is used instead of extending struct skb_shared_info
- in af_packet
- packet_sendmsg_spkt() is no longer extended
- jump labels are used to avoid the overhead if there no socket that
is using this HSR extension.
---
Sebastian Andrzej Siewior (8):
hsr: Add header_ops::parse_protocol
hsr: Use skb_clone() while adding the HSR header
hsr: Add a magic header for sending PTP packets
hsr: Drop received PTP packets
hsr: Use the port and header information in hsr_forward_skb()
hsr: Assign a socket for cloned skbs
hsr: Move struct hsr_ethhdr to a global header
selftests: hsr: Add test for the inline PTP header on HSR
MAINTAINERS | 2 +
include/linux/if_hsr.h | 6 +
include/uapi/linux/hsr_ptp.h | 20 +
net/hsr/hsr_device.c | 86 ++++-
net/hsr/hsr_forward.c | 86 +++--
net/hsr/hsr_forward.h | 3 +-
net/hsr/hsr_framereg.h | 2 +
net/hsr/hsr_main.h | 5 -
net/hsr/hsr_slave.c | 34 +-
tools/include/uapi/linux/hsr_ptp.h | 20 +
tools/testing/selftests/net/hsr/.gitignore | 1 +
tools/testing/selftests/net/hsr/Makefile | 5 +
tools/testing/selftests/net/hsr/hsr_ptp.sh | 113 ++++++
tools/testing/selftests/net/hsr/hsr_ptp_test.c | 496 +++++++++++++++++++++++++
14 files changed, 830 insertions(+), 49 deletions(-)
---
base-commit: c8cbaa094f4f7cc4e4d5a1e3ea7be5dfb362dcbb
change-id: 20260204-hsr_ptp-1f6380f1d35f
Best regards,
--
Sebastian Andrzej Siewior <bigeasy@linutronix.de>
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH net-next v14 1/8] hsr: Add header_ops::parse_protocol
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 2/8] hsr: Use skb_clone() while adding the HSR header Sebastian Andrzej Siewior
` (6 subsequent siblings)
7 siblings, 0 replies; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
af_packet uses dev_parse_header_protocol() to assign skb::protocol.
The hsr stack does not assign a callback so the field is assigned to 0.
There is nothing known to be broken because this is missing. Having a
callback assigned makes it possible to use the protocol field later in
the series by packets which originate from the AF_PACKET interface.
The header used by the HSR stack corresponds to the ethernet header for
the "first" part so the protocol member is at the same position.
Use eth_header_parse_protocol() for the hsr_header_ops's parse_protocol
callback.
Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
net/hsr/hsr_device.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/net/hsr/hsr_device.c b/net/hsr/hsr_device.c
index fc512204600cd..59df647d2fd45 100644
--- a/net/hsr/hsr_device.c
+++ b/net/hsr/hsr_device.c
@@ -247,6 +247,7 @@ static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
static const struct header_ops hsr_header_ops = {
.create = eth_header,
.parse = eth_header_parse,
+ .parse_protocol = eth_header_parse_protocol,
};
static struct sk_buff *hsr_init_skb(struct hsr_port *master, int extra)
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH net-next v14 2/8] hsr: Use skb_clone() while adding the HSR header
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 1/8] hsr: Add header_ops::parse_protocol Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 3/8] hsr: Add a magic header for sending PTP packets Sebastian Andrzej Siewior
` (5 subsequent siblings)
7 siblings, 1 reply; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
hsr_create_tagged_frame() creates a new skb where the HSR header can be
prepended via __pskb_copy(). This does not copy the skb_shared_info part
of the original skb so any timestamp request is lost.
An alternative is to use skb_clone() where the skb_shared_info part is
shared and then skb_cow_head() to make the header part writeable. This
avoids touching manually the tx_flags and tskey members.
Using skb_clone() allows also return early (after the clone) in the
offloaded case so this can be unified a bit.
The clone case does not need manual adjustment of sk_buff::csum_start
because by using skb_cow_head() the head does not move relative to the
payload. Should the head need expansion then pskb_expand_head() will
move it and update sk_buff::csum_start accordingly. The clone also
preserves metadata (if available) so skb_postpush_data_move() is used
instead of memmove() to move both. Finally skb_reset_mac_len() is added
to set the new length of the header after expansion.
This is a preparation for PTP handling where timestamps are requested.
Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
net/hsr/hsr_forward.c | 30 ++++++++++++++++++------------
1 file changed, 18 insertions(+), 12 deletions(-)
diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
index 44f6d9c83180a..b183358207506 100644
--- a/net/hsr/hsr_forward.c
+++ b/net/hsr/hsr_forward.c
@@ -331,7 +331,6 @@ static struct sk_buff *hsr_fill_tag(struct sk_buff *skb,
struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
struct hsr_port *port)
{
- unsigned char *dst, *src;
struct sk_buff *skb;
int movelen;
@@ -342,33 +341,40 @@ struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
/* set the lane id properly */
hsr_set_path_id(frame, hsr_ethhdr, port);
return skb_clone(frame->skb_hsr, GFP_ATOMIC);
- } else if (port->dev->features & NETIF_F_HW_HSR_TAG_INS) {
- return skb_clone(frame->skb_std, GFP_ATOMIC);
}
- /* Create the new skb with enough headroom to fit the HSR tag */
- skb = __pskb_copy(frame->skb_std,
- skb_headroom(frame->skb_std) + HSR_HLEN, GFP_ATOMIC);
+ skb = skb_clone(frame->skb_std, GFP_ATOMIC);
if (!skb)
return NULL;
- skb_reset_mac_header(skb);
- if (skb->ip_summed == CHECKSUM_PARTIAL)
- skb->csum_start += HSR_HLEN;
+ if (port->dev->features & NETIF_F_HW_HSR_TAG_INS)
+ return skb;
+
+ /* Ensure the cloned skb has enough headroom to fit the HSR tag */
+ if (skb_cow_head(skb, skb_metadata_len(skb) + HSR_HLEN))
+ goto err;
movelen = ETH_HLEN;
if (frame->is_vlan)
movelen += VLAN_HLEN;
- src = skb_mac_header(skb);
- dst = skb_push(skb, HSR_HLEN);
- memmove(dst, src, movelen);
+ /* The entire header area needs to be linear */
+ if (!pskb_may_pull(skb, movelen))
+ goto err;
+
+ skb_push(skb, HSR_HLEN);
+ skb_postpush_data_move(skb, HSR_HLEN, movelen);
+ skb_set_network_header(skb, HSR_HLEN + movelen);
skb_reset_mac_header(skb);
+ skb_reset_mac_len(skb);
/* skb_put_padto free skb on error and hsr_fill_tag returns NULL in
* that case
*/
return hsr_fill_tag(skb, frame, port, port->hsr->prot_version);
+err:
+ kfree_skb(skb);
+ return NULL;
}
struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame,
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH net-next v14 3/8] hsr: Add a magic header for sending PTP packets
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 1/8] hsr: Add header_ops::parse_protocol Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 2/8] hsr: Use skb_clone() while adding the HSR header Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 4/8] hsr: Drop received " Sebastian Andrzej Siewior
` (4 subsequent siblings)
7 siblings, 1 reply; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
Sending PTP packets (ETH_P_1588) via the HSR stack is pointless in its
current shape because the requested PTP timestamp is not routed to the
sender. It also needs to be distinguished on which port the message
should be sent and whether or not a HSR header should be attached.
To pass this information, a custom header (struct hsr_inline_header) is
introduced. This header is expected and mandatory if the ether type is
ETH_P_1588. To avoid any wrong usage, there is a magic field to ensure
it really is the header.
The inline header is just prepended containing the port and header
parameter. After the header, a regular packet follows. It is ensured that
the inline header and the following ethernet or HSR header is linear
and can be accessed. Given that this information is important for how
PTP is used, PTP packets missing this information will be dropped.
The two retrieved parameters are passed to hsr_forward_skb() and will be
used later.
Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
MAINTAINERS | 1 +
include/uapi/linux/hsr_ptp.h | 20 +++++++++++
net/hsr/hsr_device.c | 85 +++++++++++++++++++++++++++++++++++++-------
net/hsr/hsr_forward.c | 3 +-
net/hsr/hsr_forward.h | 3 +-
net/hsr/hsr_slave.c | 4 +--
6 files changed, 99 insertions(+), 17 deletions(-)
diff --git a/MAINTAINERS b/MAINTAINERS
index 84cb98ef4c498..e7e4a7c41b4c5 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12307,6 +12307,7 @@ F: drivers/net/usb/hso.c
HSR NETWORK PROTOCOL
L: netdev@vger.kernel.org
S: Orphan
+F: include/uapi/linux/hsr_ptp.h
F: net/hsr/
F: tools/testing/selftests/net/hsr/
diff --git a/include/uapi/linux/hsr_ptp.h b/include/uapi/linux/hsr_ptp.h
new file mode 100644
index 0000000000000..95e905441ebdc
--- /dev/null
+++ b/include/uapi/linux/hsr_ptp.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
+#ifndef __UAPI_HSR_PTP_H
+#define __UAPI_HSR_PTP_H
+
+#include <linux/types.h>
+
+#define HSR_INLINE_HDR 0xaf485352
+#define HSR_INLINE_HDR_PORT_A 1
+#define HSR_INLINE_HDR_PORT_B 2
+
+struct hsr_inline_header {
+ __u8 req_tx_port;
+ __u8 has_hsr_header;
+ __be32 __pad0;
+ __be32 magic;
+ __be16 __pad1;
+ __be16 eth_type;
+} __attribute__ ((packed));
+
+#endif
diff --git a/net/hsr/hsr_device.c b/net/hsr/hsr_device.c
index 59df647d2fd45..252646e80f724 100644
--- a/net/hsr/hsr_device.c
+++ b/net/hsr/hsr_device.c
@@ -12,6 +12,8 @@
#include <linux/etherdevice.h>
#include <linux/rtnetlink.h>
#include <linux/pkt_sched.h>
+#include <linux/hsr_ptp.h>
+
#include "hsr_device.h"
#include "hsr_slave.h"
#include "hsr_framereg.h"
@@ -221,26 +223,83 @@ static netdev_features_t hsr_fix_features(struct net_device *dev,
return hsr_features_recompute(hsr, features);
}
+static int hsr_ptp_parse_header(struct sk_buff *skb, bool *has_hsr_header,
+ enum hsr_port_type *req_tx_port)
+{
+ struct hsr_inline_header *hsr_inline_hdr;
+ struct ethhdr *eth_hdr;
+ unsigned int hdr_len;
+
+ BUILD_BUG_ON(sizeof(struct hsr_inline_header) != sizeof(struct ethhdr));
+ BUILD_BUG_ON(HSR_PT_SLAVE_A != HSR_INLINE_HDR_PORT_A);
+ BUILD_BUG_ON(HSR_PT_SLAVE_B != HSR_INLINE_HDR_PORT_B);
+
+ skb_reset_mac_header(skb);
+ if (!skb->sk)
+ return -EINVAL;
+
+ /* need to access the magic header */
+ if (!pskb_may_pull(skb, sizeof(struct hsr_inline_header)))
+ return -EINVAL;
+
+ hsr_inline_hdr = (struct hsr_inline_header *)skb_mac_header(skb);
+ if (hsr_inline_hdr->magic != htonl(HSR_INLINE_HDR))
+ return -EINVAL;
+
+ *has_hsr_header = !!hsr_inline_hdr->has_hsr_header;
+ *req_tx_port = hsr_inline_hdr->req_tx_port;
+ if (*req_tx_port != HSR_PT_SLAVE_A && *req_tx_port != HSR_PT_SLAVE_B)
+ return -EINVAL;
+
+ if (hsr_inline_hdr->__pad0 != 0 || hsr_inline_hdr->__pad1 != 0)
+ return -EINVAL;
+
+ hdr_len = ETH_HLEN;
+ if (*has_hsr_header)
+ hdr_len += HSR_HLEN;
+
+ /* Ensure the real header after the stripped one can be
+ * accessed
+ */
+ if (!pskb_may_pull(skb, sizeof(struct hsr_inline_header) +
+ hdr_len))
+ return -EINVAL;
+
+ eth_hdr = skb_pull(skb, sizeof(struct hsr_inline_header));
+ skb_set_network_header(skb, hdr_len);
+ skb->protocol = eth_hdr->h_proto;
+ return 0;
+}
+
static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
{
+ enum hsr_port_type req_tx_port = HSR_PT_NONE;
struct hsr_priv *hsr = netdev_priv(dev);
+ bool has_hsr_header = false;
struct hsr_port *master;
rcu_read_lock();
master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
- if (master) {
- skb->dev = master->dev;
- skb_reset_mac_header(skb);
- skb_reset_mac_len(skb);
- spin_lock_bh(&hsr->seqnr_lock);
- hsr_forward_skb(skb, master);
- spin_unlock_bh(&hsr->seqnr_lock);
- } else {
- dev_core_stats_tx_dropped_inc(dev);
- dev_kfree_skb_any(skb);
- }
+ if (!master)
+ goto drop;
+
+ skb->dev = master->dev;
+ if (skb->protocol == htons(ETH_P_1588) &&
+ hsr_ptp_parse_header(skb, &has_hsr_header, &req_tx_port))
+ goto drop;
+
+ skb_reset_mac_header(skb);
+ skb_reset_mac_len(skb);
+ spin_lock_bh(&hsr->seqnr_lock);
+ hsr_forward_skb(skb, master, req_tx_port, has_hsr_header);
+ spin_unlock_bh(&hsr->seqnr_lock);
rcu_read_unlock();
+ return NETDEV_TX_OK;
+drop:
+ rcu_read_unlock();
+ dev_core_stats_tx_dropped_inc(dev);
+ dev_kfree_skb_any(skb);
return NETDEV_TX_OK;
}
@@ -362,7 +421,7 @@ static void send_hsr_supervision_frame(struct hsr_port *port,
return;
}
- hsr_forward_skb(skb, port);
+ hsr_forward_skb(skb, port, HSR_PT_NONE, false);
spin_unlock_bh(&hsr->seqnr_lock);
return;
}
@@ -430,7 +489,7 @@ static void send_prp_supervision_frame(struct hsr_port *master,
return;
}
- hsr_forward_skb(skb, master);
+ hsr_forward_skb(skb, master, HSR_PT_NONE, false);
spin_unlock_bh(&hsr->seqnr_lock);
}
diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
index b183358207506..f49d962337f8b 100644
--- a/net/hsr/hsr_forward.c
+++ b/net/hsr/hsr_forward.c
@@ -771,7 +771,8 @@ static int fill_frame_info(struct hsr_frame_info *frame,
}
/* Must be called holding rcu read lock (because of the port parameter) */
-void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port)
+void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port,
+ enum hsr_port_type req_tx_port, bool has_hsr_header)
{
struct hsr_frame_info frame;
diff --git a/net/hsr/hsr_forward.h b/net/hsr/hsr_forward.h
index 206636750b300..662dd369c7df2 100644
--- a/net/hsr/hsr_forward.h
+++ b/net/hsr/hsr_forward.h
@@ -13,7 +13,8 @@
#include <linux/netdevice.h>
#include "hsr_main.h"
-void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port);
+void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port,
+ enum hsr_port_type req_tx_port, bool has_hsr_header);
struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame,
struct hsr_port *port);
struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
diff --git a/net/hsr/hsr_slave.c b/net/hsr/hsr_slave.c
index a546f70f9cc8b..5274ba6dd36e6 100644
--- a/net/hsr/hsr_slave.c
+++ b/net/hsr/hsr_slave.c
@@ -78,10 +78,10 @@ static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
*/
if (port->type == HSR_PT_INTERLINK) {
spin_lock_bh(&hsr->seqnr_lock);
- hsr_forward_skb(skb, port);
+ hsr_forward_skb(skb, port, HSR_PT_NONE, false);
spin_unlock_bh(&hsr->seqnr_lock);
} else {
- hsr_forward_skb(skb, port);
+ hsr_forward_skb(skb, port, HSR_PT_NONE, false);
}
finish_consume:
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH net-next v14 4/8] hsr: Drop received PTP packets
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
` (2 preceding siblings ...)
2026-12-03 17:44 ` [PATCH net-next v14 3/8] hsr: Add a magic header for sending PTP packets Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 5/8] hsr: Use the port and header information in hsr_forward_skb() Sebastian Andrzej Siewior
` (3 subsequent siblings)
7 siblings, 0 replies; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
Receiving PTP packets via the HSR interface does not make sense. The HSR
stack will forward one copy to the user and ignore the duplicate from
the other port. The PTP stack is however interested in both copies since
they will have different content (due to different processing times
within the HSR ring) and different timestamp information which is not
forwarded at all.
Forwarding a PTP packet by the HSR stack is undesired because the
forwarding takes time, it is not accounted in the packet and this makes
the outgoing PTP packet inaccurate and therefore useless.
Drop all received PTP packets. For the PRP configuration it is the
ether type, for HSR configuration it is the encapsulated protocol, that
is checked. A PTP stack that is doing PTP over a HSR network needs to
retrieve the PTP packet on the original slave interface where it was
received. The PTP packet is dropped with RX_HANDLER_CONSUMED meaning it
won't be visible by packet sockets bounded to protocol ETH_P_1588. It
works for sockets bound to ETH_P_ALL which is what linuxptp is doing.
L3 packets (PTP over UDP) is not considered because it is not part of
the HSR specification.
Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
net/hsr/hsr_slave.c | 30 +++++++++++++++++++++++-------
1 file changed, 23 insertions(+), 7 deletions(-)
diff --git a/net/hsr/hsr_slave.c b/net/hsr/hsr_slave.c
index 5274ba6dd36e6..a5b47f016abd7 100644
--- a/net/hsr/hsr_slave.c
+++ b/net/hsr/hsr_slave.c
@@ -23,6 +23,7 @@ bool hsr_invalid_dan_ingress_frame(__be16 protocol)
static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
{
+ struct hsr_ethhdr *hsr_ethhdr;
struct sk_buff *skb = *pskb;
struct hsr_port *port;
struct hsr_priv *hsr;
@@ -44,8 +45,7 @@ static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
/* Directly kill frames sent by ourselves */
- kfree_skb(skb);
- goto finish_consume;
+ goto finish_free_consume;
}
/* For HSR, only tagged frames are expected (unless the device offloads
@@ -64,15 +64,28 @@ static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
skb_reset_mac_header(skb);
if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
protocol == htons(ETH_P_HSR)) {
- if (!pskb_may_pull(skb, ETH_HLEN + HSR_HLEN)) {
- kfree_skb(skb);
- goto finish_consume;
- }
+ if (!pskb_may_pull(skb, ETH_HLEN + HSR_HLEN))
+ goto finish_free_consume;
skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
}
skb_reset_mac_len(skb);
+ /* PTP packets are not supposed to be forwarded via HSR as-is. The
+ * latency introduced by forwarding renders the time information
+ * useless. Userland needs to capture the packet on the original
+ * interface instead of hsr.
+ */
+ if ((!hsr->prot_version && protocol == htons(ETH_P_PRP)) ||
+ protocol == htons(ETH_P_HSR)) {
+ hsr_ethhdr = (struct hsr_ethhdr *)skb_mac_header(skb);
+ if (hsr_ethhdr->hsr_tag.encap_proto == htons(ETH_P_1588))
+ goto finish_free_consume;
+ } else {
+ if (protocol == htons(ETH_P_1588))
+ goto finish_free_consume;
+ }
+
/* Only the frames received over the interlink port will assign a
* sequence number and require synchronisation vs other sender.
*/
@@ -84,7 +97,10 @@ static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
hsr_forward_skb(skb, port, HSR_PT_NONE, false);
}
-finish_consume:
+ return RX_HANDLER_CONSUMED;
+
+finish_free_consume:
+ kfree_skb(skb);
return RX_HANDLER_CONSUMED;
finish_pass:
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH net-next v14 5/8] hsr: Use the port and header information in hsr_forward_skb()
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
` (3 preceding siblings ...)
2026-12-03 17:44 ` [PATCH net-next v14 4/8] hsr: Drop received " Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 6/8] hsr: Assign a socket for cloned skbs Sebastian Andrzej Siewior
` (2 subsequent siblings)
7 siblings, 1 reply; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
The upper layer can submit a port on which the skb should be sent and
whether or not a HSR header should be added. Use this information while
forwarding the packet:
- If the port set, deliver it only on the requested port.
- If a header is already present then skip parts of the stack:
- The packet originates from the master port and we don't want to
assign a sequence number to it, it needs to preserve the original
one.
- The existing h_source must be used (and not replaced).
- The node should already exist in the node_db if "other" packets are
exchanged. If the node information is not yet existing then
creating it makes no sense because it is used for deduplication of
incoming packets and this is not used in the PTP case. If no other
packets are exchanged then the node_db will age and then removed.
This is only used in the pure software version. Hardware with any kind
of NETIF_F_HW_* offload support need to be addressed separately.
Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
net/hsr/hsr_forward.c | 47 +++++++++++++++++++++++++++++++++++++++--------
net/hsr/hsr_framereg.h | 2 ++
2 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
index f49d962337f8b..1d1d75ed8cd06 100644
--- a/net/hsr/hsr_forward.c
+++ b/net/hsr/hsr_forward.c
@@ -426,7 +426,7 @@ static void hsr_deliver_master(struct sk_buff *skb, struct net_device *dev,
static int hsr_xmit(struct sk_buff *skb, struct hsr_port *port,
struct hsr_frame_info *frame)
{
- if (frame->port_rcv->type == HSR_PT_MASTER) {
+ if (frame->port_rcv->type == HSR_PT_MASTER && !frame->has_hsr_header) {
hsr_addr_subst_dest(frame->node_src, skb, port);
/* Address substitution (IEC62439-3 pp 26, 50): replace mac
@@ -552,6 +552,7 @@ static void hsr_forward_do(struct hsr_frame_info *frame)
hsr_for_each_port(frame->port_rcv->hsr, port) {
struct hsr_priv *hsr = port->hsr;
+
/* Don't send frame back the way it came */
if (port == frame->port_rcv)
continue;
@@ -570,6 +571,20 @@ static void hsr_forward_do(struct hsr_frame_info *frame)
if ((port->dev->features & NETIF_F_HW_HSR_DUP) && sent)
continue;
+ /* If this frame has a specific outgoing port specified (by the
+ * sender) then send it on this port only.
+ */
+ if (frame->req_tx_port != HSR_PT_NONE &&
+ frame->req_tx_port != port->type)
+ continue;
+ /* If this TX frame already has a HSR header then forward the
+ * frame with it and don't add one.
+ */
+ if (frame->has_hsr_header && frame->skb_std) {
+ skb = skb_clone(frame->skb_std, GFP_ATOMIC);
+ goto inject_into_stack;
+ }
+
/* Don't send frame over port where it has been sent before.
* Also for SAN, this shouldn't be done.
*/
@@ -597,6 +612,7 @@ static void hsr_forward_do(struct hsr_frame_info *frame)
else
skb = hsr->proto_ops->get_untagged_frame(frame, port);
+inject_into_stack:
if (!skb) {
frame->port_rcv->dev->stats.rx_dropped++;
continue;
@@ -661,6 +677,13 @@ int hsr_fill_frame_info(__be16 proto, struct sk_buff *skb,
struct hsr_port *port = frame->port_rcv;
struct hsr_priv *hsr = port->hsr;
+ if (frame->has_hsr_header) {
+ frame->skb_std = skb;
+
+ WARN_ON_ONCE(port->type != HSR_PT_MASTER);
+ WARN_ON_ONCE(skb->mac_len < sizeof(struct hsr_ethhdr));
+ return 0;
+ }
/* HSRv0 supervisory frames double as a tag so treat them as tagged. */
if ((!hsr->prot_version && proto == htons(ETH_P_PRP)) ||
proto == htons(ETH_P_HSR)) {
@@ -702,7 +725,8 @@ int prp_fill_frame_info(__be16 proto, struct sk_buff *skb,
}
static int fill_frame_info(struct hsr_frame_info *frame,
- struct sk_buff *skb, struct hsr_port *port)
+ struct sk_buff *skb, struct hsr_port *port,
+ enum hsr_port_type req_tx_port, bool has_hsr_header)
{
struct hsr_priv *hsr = port->hsr;
struct hsr_vlan_ethhdr *vlan_hdr;
@@ -725,10 +749,16 @@ static int fill_frame_info(struct hsr_frame_info *frame,
if (port->type == HSR_PT_INTERLINK)
n_db = &hsr->proxy_node_db;
- frame->node_src = hsr_get_node(port, n_db, skb,
- frame->is_supervision, port->type);
- if (!frame->node_src)
- return -1; /* Unknown node and !is_supervision, or no mem */
+ frame->req_tx_port = req_tx_port;
+ frame->has_hsr_header = has_hsr_header;
+
+ if (!frame->has_hsr_header) {
+ frame->node_src = hsr_get_node(port, n_db, skb,
+ frame->is_supervision,
+ port->type);
+ if (!frame->node_src)
+ return -1; /* Unknown node and !is_supervision, or no mem */
+ }
ethhdr = (struct ethhdr *)skb_mac_header(skb);
frame->is_vlan = false;
@@ -777,10 +807,11 @@ void hsr_forward_skb(struct sk_buff *skb, struct hsr_port *port,
struct hsr_frame_info frame;
rcu_read_lock();
- if (fill_frame_info(&frame, skb, port) < 0)
+ if (fill_frame_info(&frame, skb, port, req_tx_port, has_hsr_header) < 0)
goto out_drop;
- hsr_register_frame_in(frame.node_src, port, frame.sequence_nr);
+ if (!frame.has_hsr_header)
+ hsr_register_frame_in(frame.node_src, port, frame.sequence_nr);
hsr_forward_do(&frame);
rcu_read_unlock();
/* Gets called for ingress frames as well as egress from master port.
diff --git a/net/hsr/hsr_framereg.h b/net/hsr/hsr_framereg.h
index 127a3fb64d5f0..dc13819d04098 100644
--- a/net/hsr/hsr_framereg.h
+++ b/net/hsr/hsr_framereg.h
@@ -29,6 +29,8 @@ struct hsr_frame_info {
bool is_from_san;
bool dst_in_node_db;
bool dst_in_proxy_node_db;
+ bool has_hsr_header;
+ enum hsr_port_type req_tx_port;
};
void hsr_del_self_node(struct hsr_priv *hsr);
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH net-next v14 6/8] hsr: Assign a socket for cloned skbs
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
` (4 preceding siblings ...)
2026-12-03 17:44 ` [PATCH net-next v14 5/8] hsr: Use the port and header information in hsr_forward_skb() Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 7/8] hsr: Move struct hsr_ethhdr to a global header Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 8/8] selftests: hsr: Add test for the inline PTP header on HSR Sebastian Andrzej Siewior
7 siblings, 1 reply; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
The cloned skb does not have the socket recorded of the original skb
because skb_clone() clears sk_buff::sk. The original skb is never
submitted but freed in hsr_forward_skb(). skb_tstamp_tx() will only
submit timestamp information to the sender if the socket is recorded and
the TS flags are available.
As of the recent changes everything is available except the socket and
the requested timestamp gets lost.
This does not consider PRP part of the stack. The specification says
that PTP-PRP packets do not have the RCT therefore these packets can be
sent directly from userland skipping the stack.
Assign the socket of the original skb to the clone so the timestamp is
forwarded to the user.
Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
net/hsr/hsr_forward.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
index 1d1d75ed8cd06..9cb3297a8640a 100644
--- a/net/hsr/hsr_forward.c
+++ b/net/hsr/hsr_forward.c
@@ -12,6 +12,7 @@
#include <linux/skbuff.h>
#include <linux/etherdevice.h>
#include <linux/if_vlan.h>
+#include <net/sock.h>
#include "hsr_main.h"
#include "hsr_framereg.h"
@@ -347,6 +348,9 @@ struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
if (!skb)
return NULL;
+ if (frame->req_tx_port != HSR_PT_NONE && frame->skb_std->sk)
+ skb_set_owner_w(skb, frame->skb_std->sk);
+
if (port->dev->features & NETIF_F_HW_HSR_TAG_INS)
return skb;
@@ -582,6 +586,8 @@ static void hsr_forward_do(struct hsr_frame_info *frame)
*/
if (frame->has_hsr_header && frame->skb_std) {
skb = skb_clone(frame->skb_std, GFP_ATOMIC);
+ if (skb && frame->skb_std->sk)
+ skb_set_owner_w(skb, frame->skb_std->sk);
goto inject_into_stack;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH net-next v14 7/8] hsr: Move struct hsr_ethhdr to a global header
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
` (5 preceding siblings ...)
2026-12-03 17:44 ` [PATCH net-next v14 6/8] hsr: Assign a socket for cloned skbs Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 8/8] selftests: hsr: Add test for the inline PTP header on HSR Sebastian Andrzej Siewior
7 siblings, 0 replies; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
In the offloading case the network driver needs to inspect the HSR
header to find out if the encapsulated type is a PTP packet where it
needs to suppress offloading of the packet.
Move the header so it can be included by the driver.
Reviewed-by: Daniel Machon <daniel.machon@microchip.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
include/linux/if_hsr.h | 6 ++++++
net/hsr/hsr_main.h | 5 -----
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/include/linux/if_hsr.h b/include/linux/if_hsr.h
index f4cf2dd36d193..d0773a60c4fd0 100644
--- a/include/linux/if_hsr.h
+++ b/include/linux/if_hsr.h
@@ -3,6 +3,7 @@
#define _LINUX_IF_HSR_H_
#include <linux/types.h>
+#include <linux/if_ether.h>
struct net_device;
@@ -38,6 +39,11 @@ struct hsr_tag {
#define HSR_HLEN 6
+struct hsr_ethhdr {
+ struct ethhdr ethhdr;
+ struct hsr_tag hsr_tag;
+} __packed;
+
#if IS_ENABLED(CONFIG_HSR)
extern bool is_hsr_master(struct net_device *dev);
extern int hsr_get_version(struct net_device *dev, enum hsr_version *ver);
diff --git a/net/hsr/hsr_main.h b/net/hsr/hsr_main.h
index 53e95bae0ee27..36eb82774000c 100644
--- a/net/hsr/hsr_main.h
+++ b/net/hsr/hsr_main.h
@@ -72,11 +72,6 @@ static inline void set_hsr_tag_LSDU_size(struct hsr_tag *ht, u16 LSDU_size)
0xF000) | (LSDU_size & 0x0FFF));
}
-struct hsr_ethhdr {
- struct ethhdr ethhdr;
- struct hsr_tag hsr_tag;
-} __packed;
-
struct hsr_vlan_ethhdr {
struct vlan_ethhdr vlanhdr;
struct hsr_tag hsr_tag;
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH net-next v14 8/8] selftests: hsr: Add test for the inline PTP header on HSR
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
` (6 preceding siblings ...)
2026-12-03 17:44 ` [PATCH net-next v14 7/8] hsr: Move struct hsr_ethhdr to a global header Sebastian Andrzej Siewior
@ 2026-12-03 17:44 ` Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
7 siblings, 1 reply; 14+ messages in thread
From: Sebastian Andrzej Siewior @ 2026-12-03 17:44 UTC (permalink / raw)
To: netdev
Cc: Sebastian Andrzej Siewior, (JC), Jayachandran, Andrew Lunn,
Danish Anwar, Daolin Qiu, David S. Miller, Felix Maurer,
Jakub Kicinski, Neelima Muralidharan, Paolo Abeni,
Praneeth Bajjuri, Pratheesh Gangadhar TK, Richard Cochran,
Simon Horman, Vignesh Raghavendra, Willem de Bruijn,
Daniel Machon, Eric Dumazet
This test verifies the inline header which is used by HSR stack with the
ether type ETH_P_1588.
The HSR stack needs to recognize this header, strip it and send the
requested packet only on the requested port.
This test needs a HSR device and the two slave devices passed. It will
will send a sample PTP packet with this inline header, requesting one of
the ports combining with and without the HSR header. A total of four
packets is sent.
The test checks both ports devices for packets and complains if the
packet was not observed on the expected port and/ or observed on the
other port. The received content of the data is compared against the
send data sample. If the HSR header was supplied it verifies the
sequence number and the SRC-MAC of the received header if it matches.
Suggested-by: Felix Maurer <fmaurer@redhat.com>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
MAINTAINERS | 1 +
tools/include/uapi/linux/hsr_ptp.h | 20 +
tools/testing/selftests/net/hsr/.gitignore | 1 +
tools/testing/selftests/net/hsr/Makefile | 5 +
tools/testing/selftests/net/hsr/hsr_ptp.sh | 113 ++++++
tools/testing/selftests/net/hsr/hsr_ptp_test.c | 496 +++++++++++++++++++++++++
6 files changed, 636 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index e7e4a7c41b4c5..a2f85fa68ee30 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -12309,6 +12309,7 @@ L: netdev@vger.kernel.org
S: Orphan
F: include/uapi/linux/hsr_ptp.h
F: net/hsr/
+F: tools/include/uapi/linux/hsr_ptp.h
F: tools/testing/selftests/net/hsr/
HT16K33 LED CONTROLLER DRIVER
diff --git a/tools/include/uapi/linux/hsr_ptp.h b/tools/include/uapi/linux/hsr_ptp.h
new file mode 100644
index 0000000000000..95e905441ebdc
--- /dev/null
+++ b/tools/include/uapi/linux/hsr_ptp.h
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
+#ifndef __UAPI_HSR_PTP_H
+#define __UAPI_HSR_PTP_H
+
+#include <linux/types.h>
+
+#define HSR_INLINE_HDR 0xaf485352
+#define HSR_INLINE_HDR_PORT_A 1
+#define HSR_INLINE_HDR_PORT_B 2
+
+struct hsr_inline_header {
+ __u8 req_tx_port;
+ __u8 has_hsr_header;
+ __be32 __pad0;
+ __be32 magic;
+ __be16 __pad1;
+ __be16 eth_type;
+} __attribute__ ((packed));
+
+#endif
diff --git a/tools/testing/selftests/net/hsr/.gitignore b/tools/testing/selftests/net/hsr/.gitignore
new file mode 100644
index 0000000000000..849eecb84c974
--- /dev/null
+++ b/tools/testing/selftests/net/hsr/.gitignore
@@ -0,0 +1 @@
+hsr_ptp_test
diff --git a/tools/testing/selftests/net/hsr/Makefile b/tools/testing/selftests/net/hsr/Makefile
index 2150e487ac7d7..512ccf61b6652 100644
--- a/tools/testing/selftests/net/hsr/Makefile
+++ b/tools/testing/selftests/net/hsr/Makefile
@@ -2,9 +2,12 @@
top_srcdir = ../../../../..
+CFLAGS += -Wall -Wl,--no-as-needed -O2 -g $(TOOLS_INCLUDES)
+
TEST_PROGS := \
hsr_ping.sh \
hsr_prp_redbox.sh \
+ hsr_ptp.sh \
hsr_redbox.sh \
link_faults.sh \
prp_ping.sh \
@@ -12,4 +15,6 @@ TEST_PROGS := \
TEST_FILES += hsr_common.sh
+TEST_GEN_FILES := hsr_ptp_test
+
include ../../lib.mk
diff --git a/tools/testing/selftests/net/hsr/hsr_ptp.sh b/tools/testing/selftests/net/hsr/hsr_ptp.sh
new file mode 100755
index 0000000000000..11866f1725f6a
--- /dev/null
+++ b/tools/testing/selftests/net/hsr/hsr_ptp.sh
@@ -0,0 +1,113 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+source ./hsr_common.sh
+
+optstring="h"
+usage() {
+ echo "Usage: $0"
+}
+
+while getopts "$optstring" option;do
+ case "$option" in
+ "h")
+ usage "$0"
+ exit 0
+ ;;
+ "?")
+ usage "$0"
+ exit 1
+ ;;
+esac
+done
+
+# shellcheck disable=SC2154
+# ns1…ns3 are set via setup_ns which is not recognized.
+
+setup_hsr_interfaces()
+{
+ local HSRv="$1"
+
+ echo "INFO: Preparing interfaces for HSRv${HSRv}."
+# Three HSR nodes. Each node has one link to each of its neighbour, two links in total.
+
+# ns1eth1 ----- ns2eth1
+# hsr1 hsr2
+# ns1eth2 ns2eth2
+# | |
+# ns3eth1 ns3eth2
+# \ /
+# hsr3
+#
+ # Interfaces
+ ip link add ns1eth1 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
+ ip link add ns1eth2 netns "$ns1" type veth peer name ns3eth1 netns "$ns3"
+ ip link add ns3eth2 netns "$ns3" type veth peer name ns2eth2 netns "$ns2"
+
+ # HSRv0/1
+ ip -net "$ns1" link add name hsr1 type hsr slave1 ns1eth1 \
+ slave2 ns1eth2 supervision 45 version "$HSRv" proto 0
+ ip -net "$ns2" link add name hsr2 type hsr slave1 ns2eth1 \
+ slave2 ns2eth2 supervision 45 version "$HSRv" proto 0
+ ip -net "$ns3" link add name hsr3 type hsr slave1 ns3eth1 \
+ slave2 ns3eth2 supervision 45 version "$HSRv" proto 0
+
+ # IP for HSR
+ ip -net "$ns1" addr add 100.64.0.1/24 dev hsr1
+ ip -net "$ns1" addr add dead:beef:0::1/64 dev hsr1 nodad
+ ip -net "$ns2" addr add 100.64.0.2/24 dev hsr2
+ ip -net "$ns2" addr add dead:beef:0::2/64 dev hsr2 nodad
+ ip -net "$ns3" addr add 100.64.0.3/24 dev hsr3
+ ip -net "$ns3" addr add dead:beef:0::3/64 dev hsr3 nodad
+
+ ip -net "$ns1" link set address 00:11:22:00:01:01 dev ns1eth1
+ ip -net "$ns1" link set address 00:11:22:00:01:02 dev ns1eth2
+
+ ip -net "$ns2" link set address 00:11:22:00:02:01 dev ns2eth1
+ ip -net "$ns2" link set address 00:11:22:00:02:02 dev ns2eth2
+
+ ip -net "$ns3" link set address 00:11:22:00:03:01 dev ns3eth1
+ ip -net "$ns3" link set address 00:11:22:00:03:02 dev ns3eth2
+
+ # All Links up
+ ip -net "$ns1" link set ns1eth1 up
+ ip -net "$ns1" link set ns1eth2 up
+ ip -net "$ns1" link set hsr1 up
+
+ ip -net "$ns2" link set ns2eth1 up
+ ip -net "$ns2" link set ns2eth2 up
+ ip -net "$ns2" link set hsr2 up
+
+ ip -net "$ns3" link set ns3eth1 up
+ ip -net "$ns3" link set ns3eth2 up
+ ip -net "$ns3" link set hsr3 up
+
+ # A safety delay to ensure the link is up
+ sleep 1
+}
+
+run_ptp_hdr_tests()
+{
+ echo "INFO: Running PTP-header tests."
+
+ ip netns exec "$ns1" ./hsr_ptp_test -H hsr1 -A ns1eth1 -B ns1eth2
+ ret=$?
+ stop_if_error "PTP header test failed (ns1)."
+
+ ip netns exec "$ns2" ./hsr_ptp_test -H hsr2 -A ns2eth1 -B ns2eth2
+ ret=$?
+ stop_if_error "PTP header test failed (ns2)."
+
+ ip netns exec "$ns3" ./hsr_ptp_test -H hsr3 -A ns3eth1 -B ns3eth2
+ ret=$?
+ stop_if_error "PTP header test failed (ns3)."
+}
+
+check_prerequisites
+trap cleanup_all_ns EXIT
+
+setup_ns ns1 ns2 ns3
+setup_hsr_interfaces 1
+run_ptp_hdr_tests
+
+exit $ret
diff --git a/tools/testing/selftests/net/hsr/hsr_ptp_test.c b/tools/testing/selftests/net/hsr/hsr_ptp_test.c
new file mode 100644
index 0000000000000..e2c2da56138ae
--- /dev/null
+++ b/tools/testing/selftests/net/hsr/hsr_ptp_test.c
@@ -0,0 +1,496 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Simple test to verify the usage of the inline header used for HSR with
+ * ether type ETH_P_1588.
+ * The inline header has to be stripped, the sent packet must only appear on the
+ * specified port and the interface needs to accept a foreign HSR header and
+ * prepend its own header.
+ *
+ * Socket handling inspired by raw.c from linuxptp.
+ *
+ */
+#include <poll.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <linux/types.h>
+#include <linux/hsr_ptp.h>
+#include <linux/if_ether.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <netpacket/packet.h>
+#include <sys/ioctl.h>
+
+#define MSEC_TO_NSEC(_x) (_x * 1000000)
+#define USEC_PER_SEC 1000000
+#define NSEC_PER_SEC 1000000000
+
+#ifndef __packed
+#define __packed __attribute__((packed))
+#endif
+
+#define MAC_LEN 6
+typedef uint8_t eth_addr[MAC_LEN];
+
+struct eth_hdr {
+ eth_addr dst;
+ eth_addr src;
+ uint16_t type;
+} __packed;
+
+struct hsr_hdr {
+ eth_addr dst;
+ eth_addr src;
+ uint16_t type;
+ uint16_t pathid_and_LSDU_size;
+ uint16_t sequence_nr;
+ uint16_t encap_type;
+} __packed;
+
+struct hsr_meta_header {
+ struct hsr_inline_header hsr_opt;
+ union {
+ struct hsr_hdr hsr_hdr;
+ struct eth_hdr eth_hdr;
+ };
+} __packed;
+
+static uint8_t ptp_packet[] = {
+ 0x00, 0x12, 0x00, 0x2c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x00, 0x01, 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
+
+static uint8_t p2p_dst_mac[MAC_LEN] = {
+ 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e
+};
+
+static uint8_t slave_mac_addr[MAC_LEN];
+static int32_t fd_slaveA = -1;
+static int32_t fd_slaveB = -1;
+static int32_t fd_hsr = -1;
+static uint16_t hsr_seq = 37;
+
+static void tsnorm(struct timespec *ts)
+{
+ while (ts->tv_nsec >= NSEC_PER_SEC) {
+ ts->tv_nsec -= NSEC_PER_SEC;
+ ts->tv_sec++;
+ }
+}
+
+static int64_t calcdiff(struct timespec t1, struct timespec t2)
+{
+ int64_t diff = USEC_PER_SEC * (long long)((int) t1.tv_sec - (int) t2.tv_sec);
+
+ diff += ((int) t1.tv_nsec - (int) t2.tv_nsec) / 1000;
+ return diff;
+}
+
+static int tsgreater(struct timespec *a, struct timespec *b)
+{
+ return ((a->tv_sec > b->tv_sec) ||
+ (a->tv_sec == b->tv_sec && a->tv_nsec > b->tv_nsec));
+}
+
+static int sk_interface_index(int fd, const char *name)
+{
+ struct ifreq ifreq;
+ int32_t err;
+
+ memset(&ifreq, 0, sizeof(ifreq));
+ strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
+ err = ioctl(fd, SIOCGIFINDEX, &ifreq);
+ if (err < 0) {
+ printf("ioctl SIOCGIFINDEX failed: %m\n");
+ return err;
+ }
+ return ifreq.ifr_ifindex;
+}
+
+static int open_socket(const char *name)
+{
+ struct sockaddr_ll addr;
+ int32_t fd, index;
+
+ fd = socket(AF_PACKET, SOCK_RAW, 0);
+ if (fd < 0) {
+ printf("socket failed: %m\n");
+ goto err;
+ }
+ index = sk_interface_index(fd, name);
+ if (index < 0)
+ goto err;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.sll_ifindex = index;
+ addr.sll_family = AF_PACKET;
+ addr.sll_protocol = htons(ETH_P_ALL);
+ if (bind(fd, (struct sockaddr *) &addr, sizeof(addr))) {
+ printf("bind failed: %m\n");
+ goto err;
+ }
+ if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))) {
+ printf("setsockopt SO_BINDTODEVICE failed: %m\n");
+ goto err;
+ }
+
+ return fd;
+err:
+ if (fd >= 0)
+ close(fd);
+ return -1;
+}
+
+static int sk_interface_macaddr(const char *name)
+{
+ struct ifreq ifreq;
+ int32_t err, fd;
+
+ memset(&ifreq, 0, sizeof(ifreq));
+ strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
+
+ fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if (fd < 0) {
+ printf("socket failed: %m\n");
+ return -1;
+ }
+
+ err = ioctl(fd, SIOCGIFHWADDR, &ifreq);
+ if (err < 0) {
+ printf("ioctl SIOCGIFHWADDR failed: %m\n");
+ close(fd);
+ return -1;
+ }
+
+ close(fd);
+
+ memcpy(slave_mac_addr, &ifreq.ifr_hwaddr.sa_data, MAC_LEN);
+ return 0;
+}
+
+static int raw_open(const char *hsr_dev, const char *slaveA_dev, const char *slaveB_dev)
+{
+
+ if (sk_interface_macaddr(slaveA_dev))
+ goto err;
+
+ fd_slaveA = open_socket(slaveA_dev);
+ if (fd_slaveA < 0)
+ goto err;
+
+ fd_slaveB = open_socket(slaveB_dev);
+ if (fd_slaveB < 0)
+ goto err;
+
+ fd_hsr = open_socket(hsr_dev);
+ if (fd_hsr < 0)
+ goto err;
+
+ return 0;
+
+err:
+ if (fd_slaveA >= 0)
+ close(fd_slaveA);
+ if (fd_slaveB >= 0)
+ close(fd_slaveB);
+ if (fd_hsr >= 0)
+ close(fd_hsr);
+
+ return -1;
+}
+
+static int sk_receive(int fd, void *buf, int buflen)
+{
+ struct iovec iov = { buf, buflen };
+ uint8_t control[256];
+ int32_t cnt = 0;
+ struct msghdr msg;
+
+ memset(control, 0, sizeof(control));
+ memset(&msg, 0, sizeof(msg));
+
+ msg.msg_iov = &iov;
+ msg.msg_iovlen = 1;
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
+
+ cnt = recvmsg(fd, &msg, MSG_DONTWAIT);
+ if (cnt < 0)
+ return 0;
+ return cnt;
+}
+
+static int raw_recv(int fd, bool fwd_hsr, void *sample, int sample_len)
+{
+ struct timespec now, end;
+ struct hsr_hdr *hsr_hdr;
+ uint8_t buf[1500];
+ int32_t cnt;
+
+ /* The packet is expected to arrive within 100ms. HSR will send hello
+ * packets so there an end time and poll() will wait until this point.
+ */
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ end = now;
+ end.tv_nsec += MSEC_TO_NSEC(100);
+ tsnorm(&end);
+again:
+
+ cnt = sk_receive(fd, buf, sizeof(buf));
+ if (cnt == 0) {
+ int64_t sleep_time;
+ struct pollfd pfd;
+ int ret;
+
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ if (tsgreater(&now, &end))
+ return 0;
+
+ sleep_time = calcdiff(end, now) / 1000;
+ if (sleep_time <= 0)
+ return 0;
+
+ pfd.fd = fd;
+ pfd.events = POLLIN;
+ ret = poll(&pfd, 1, sleep_time);
+ if (ret <= 0)
+ return ret;
+ goto again;
+ }
+
+ if (cnt < sizeof(struct hsr_hdr))
+ goto again;
+
+ hsr_hdr = (void *)buf;
+ /* Embedded magic packet passed? */
+ if (hsr_hdr->type == htons(ETH_P_1588)) {
+ struct hsr_inline_header *hsr_opt = (void *)buf;
+
+ if (hsr_opt->magic == htonl(HSR_INLINE_HDR)) {
+ printf("Error: Found HSR_INLINE_HDR\n");
+ return -1;
+ }
+ }
+ /* Assuming it is *our* network and nobody but the test here sends
+ * packets to p2p_dst_mac. Therefore any received packet needs to be
+ * ours and every mismatch is considered as error.
+ */
+ if (memcmp(hsr_hdr->dst, p2p_dst_mac, MAC_LEN))
+ goto again;
+
+ if (hsr_hdr->type != htons(ETH_P_HSR)) {
+ printf("Error: Unexpected ether type: 0x%x\n", ntohs(hsr_hdr->type));
+ return -1;
+ }
+
+ if (hsr_hdr->encap_type != htons(ETH_P_1588)) {
+ printf("Error: Unexpected encapsulated type: 0x%x\n",
+ htons(hsr_hdr->encap_type));
+ return -1;
+ }
+
+ if (fwd_hsr) {
+ if (hsr_hdr->sequence_nr != htons(hsr_seq - 1)) {
+ printf("Error: Packet has wrong seq nr, %d vs %d.\n",
+ ntohs(hsr_hdr->sequence_nr), hsr_seq - 1);
+ return -1;
+ }
+ /* Undo the SRC MAC change in pkt_send() */
+ hsr_hdr->src[3] ^= 0x21;
+ if (memcmp(hsr_hdr->src, slave_mac_addr, MAC_LEN)) {
+ printf("Error: Packet has wrong SRC-MAC\n");
+ return -1;
+ }
+ }
+
+ if (cnt < sizeof(struct hsr_hdr) + sample_len) {
+ printf("Error: Packet %d is too small for data check\n", cnt);
+ return -1;
+ }
+
+ if (!memcmp(&buf[sizeof(struct hsr_hdr)], sample, sample_len))
+ return 1;
+
+ printf("Error: Packet did not match the sample\n");
+ return -1;
+}
+
+static int recv_verify(int port, bool fwd_hsr, void *sample, int sample_len)
+{
+ int32_t error = 0;
+ int32_t ret;
+
+ ret = raw_recv(fd_slaveA, fwd_hsr, sample, sample_len);
+ if (ret < 0)
+ return ret;
+ if (port == HSR_INLINE_HDR_PORT_A && ret == 0) {
+ printf("Error: Missing packet on portA\n");
+ error = 1;
+ }
+ if (port == HSR_INLINE_HDR_PORT_B && ret == 1) {
+ printf("Error: Not expecting packet on portA\n");
+ error = 1;
+ }
+
+ ret = raw_recv(fd_slaveB, fwd_hsr, sample, sample_len);
+ if (ret < 0)
+ return ret;
+ if (port == HSR_INLINE_HDR_PORT_B && ret == 0) {
+ printf("Error: Missing packet on portB\n");
+ error = 1;
+ }
+ if (port == HSR_INLINE_HDR_PORT_A && ret == 1) {
+ printf("Error: Not expecting packet on portB\n");
+ error = 1;
+ }
+ return error;
+}
+
+static int32_t pkt_send(int port, bool hsr_hdr, void *data, int data_len)
+{
+ struct hsr_meta_header *hdr;
+ uint8_t packet[200];
+ ssize_t cnt;
+ size_t len;
+
+ memset(packet, 0, sizeof(packet));
+
+ len = sizeof(struct hsr_inline_header);
+ hdr = (struct hsr_meta_header *)packet;
+ memset(&hdr->hsr_opt, 0, sizeof(hdr->hsr_opt));
+ hdr->hsr_opt.magic = htonl(HSR_INLINE_HDR);
+ hdr->hsr_opt.eth_type = htons(ETH_P_1588);
+ if (port != HSR_INLINE_HDR_PORT_A && port != HSR_INLINE_HDR_PORT_B) {
+ printf("Wrong port requested\n");
+ return -1;
+ }
+ hdr->hsr_opt.req_tx_port = port;
+
+ if (hsr_hdr) {
+ uint16_t pathid_size;
+
+ len += sizeof(struct hsr_hdr);
+ memcpy(&packet[len], data, data_len);
+
+ hdr->hsr_opt.has_hsr_header = 1;
+
+ memcpy(&hdr->hsr_hdr.dst, p2p_dst_mac, MAC_LEN);
+ memcpy(&hdr->hsr_hdr.src, slave_mac_addr, MAC_LEN);
+ /* Flip a bit in SRC MAC addr so it does not look like hosts */
+ hdr->hsr_hdr.src[3] ^= 0x21;
+
+ hdr->hsr_hdr.type = htons(ETH_P_HSR);
+ hdr->hsr_hdr.sequence_nr = htons(hsr_seq++);
+ hdr->hsr_hdr.encap_type = htons(ETH_P_1588);
+
+ /* The resulting packet must be alteast 66 bytes */
+ if (data_len + sizeof(struct hsr_hdr) < 66)
+ data_len = 66 - sizeof(struct hsr_hdr);
+
+ pathid_size = data_len + sizeof(struct hsr_hdr) - sizeof(struct eth_hdr);
+ pathid_size |= (port - 1) << 12;
+
+ hdr->hsr_hdr.pathid_and_LSDU_size = htons(pathid_size);
+ } else {
+ len += sizeof(struct eth_hdr);
+ hdr->hsr_opt.has_hsr_header = 0;
+
+ memcpy(&hdr->eth_hdr.dst, p2p_dst_mac, MAC_LEN);
+ memcpy(&hdr->eth_hdr.src, slave_mac_addr, MAC_LEN);
+ hdr->eth_hdr.type = htons(ETH_P_1588);
+
+ memcpy(&packet[len], data, data_len);
+ }
+
+ cnt = send(fd_hsr, packet, len + data_len, 0);
+ if (cnt < 1)
+ return -1;
+
+ return cnt;
+}
+
+int main(int argc, char *argv[])
+{
+ char *slaveA = NULL, *slaveB = NULL, *hsr_dev = NULL;
+ char *msg_mode;
+ int opt;
+
+ while ((opt = getopt(argc, argv, "A:B:H:")) != -1) {
+ switch (opt) {
+ case 'A':
+ slaveA = strdup(optarg);
+ break;
+
+ case 'B':
+ slaveB = strdup(optarg);
+ break;
+
+ case 'H':
+ hsr_dev = strdup(optarg);
+ break;
+ default: /* '?' */
+ fprintf(stderr, "Usage: %s -A slaveA -B slaveB -H hsr_device\n",
+ argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ if (!slaveA || !slaveB || !hsr_dev) {
+ fprintf(stderr, "Missing network devices\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if (raw_open(hsr_dev, slaveA, slaveB) < 0)
+ return EXIT_FAILURE;
+
+ msg_mode = "PortA, no-hsr-header";
+ if (pkt_send(HSR_INLINE_HDR_PORT_A, false, ptp_packet, sizeof(ptp_packet)) < 0) {
+ printf("Sending failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+ if (recv_verify(HSR_INLINE_HDR_PORT_A, false, ptp_packet, sizeof(ptp_packet))) {
+ printf("Verify failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+
+ ptp_packet[16 + 4]++;
+ msg_mode = "PortB, no-hsr-header";
+ if (pkt_send(HSR_INLINE_HDR_PORT_B, false, ptp_packet, sizeof(ptp_packet)) < 0) {
+ printf("Sending failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+ if (recv_verify(HSR_INLINE_HDR_PORT_B, false, ptp_packet, sizeof(ptp_packet))) {
+ printf("Verify failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+
+ ptp_packet[16 + 4]++;
+ msg_mode = "PortA, hsr-header";
+ if (pkt_send(HSR_INLINE_HDR_PORT_A, true, ptp_packet, sizeof(ptp_packet)) < 0) {
+ printf("Sending failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+ if (recv_verify(HSR_INLINE_HDR_PORT_A, true, ptp_packet, sizeof(ptp_packet))) {
+ printf("Verify failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+
+ ptp_packet[16 + 4]++;
+ msg_mode = "PortB, hsr-header";
+ if (pkt_send(HSR_INLINE_HDR_PORT_B, true, ptp_packet, sizeof(ptp_packet)) < 0) {
+ printf("Sending failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+ if (recv_verify(HSR_INLINE_HDR_PORT_B, true, ptp_packet, sizeof(ptp_packet))) {
+ printf("Verify failed: %s\n", msg_mode);
+ return EXIT_FAILURE;
+ }
+
+ return EXIT_SUCCESS;
+}
--
2.55.0
^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH net-next v14 2/8] hsr: Use skb_clone() while adding the HSR header
2026-12-03 17:44 ` [PATCH net-next v14 2/8] hsr: Use skb_clone() while adding the HSR header Sebastian Andrzej Siewior
@ 2026-12-04 17:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04 17:55 UTC (permalink / raw)
To: bigeasy
Cc: netdev, j-rameshbabu, andrew+netdev, danishanwar, d-qiu, davem,
fmaurer, kuba, neelima, pabeni, praneeth, pratheesh,
richardcochran, horms, vigneshr, willemdebruijn.kernel,
daniel.machon, edumazet
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 (1) · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] hsr: hsr_create_tagged_frame() retains TX timestamp flags on
unowned clones
- [Low] prp_create_tagged_frame() (net/hsr/hsr_forward.c) still builds
tagged frames with skb_copy_expand().
Pre-existing issues:
- [Medium] This problem wasn't introduced by this patch, but
hsr_create_tagged_frame() returns a bare skb_clone() in two places…
--
hsr: Use skb_clone() while adding the HSR header
This commit replaces __pskb_copy() in hsr_create_tagged_frame() with
skb_clone() followed by skb_cow_head(). This keeps skb_shared_info, and any
timestamp request in it, on the HSR tagged frame. The NETIF_F_HW_HSR_TAG_INS
case now shares the same clone path, and skb_postpush_data_move() and
skb_reset_mac_len() move the header and metadata and update mac_len.
> diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
> index 44f6d9c83180a..b183358207506 100644
> --- a/net/hsr/hsr_forward.c
> +++ b/net/hsr/hsr_forward.c
[ ... ]
> @@ -342,33 +341,40 @@ struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
> /* set the lane id properly */
> hsr_set_path_id(frame, hsr_ethhdr, port);
> return skb_clone(frame->skb_hsr, GFP_ATOMIC);
> - } else if (port->dev->features & NETIF_F_HW_HSR_TAG_INS) {
> - return skb_clone(frame->skb_std, GFP_ATOMIC);
> }
>
> - /* Create the new skb with enough headroom to fit the HSR tag */
> - skb = __pskb_copy(frame->skb_std,
> - skb_headroom(frame->skb_std) + HSR_HLEN, GFP_ATOMIC);
> + skb = skb_clone(frame->skb_std, GFP_ATOMIC);
> if (!skb)
> return NULL;
> - skb_reset_mac_header(skb);
>
> - if (skb->ip_summed == CHECKSUM_PARTIAL)
> - skb->csum_start += HSR_HLEN;
> + if (port->dev->features & NETIF_F_HW_HSR_TAG_INS)
> + return skb;
[Severity: Medium]
Before this patch, __pskb_copy() went through skb_copy_header(), which
only carries over the gso fields of skb_shared_info. The software
tagged frames handed to the slaves therefore never had any SKBTX_* bits
set. With skb_clone() that changes.
__skb_clone() sets n->sk to NULL. Since frame->skb_std is still alive,
skb_header_cloned() is true here, so skb_cow_head() always ends up in
pskb_expand_head(). That copies the old skb_shared_info, tx_flags and
tskey included, into the new private one.
So the frames for SLAVE_A and SLAVE_B now reach the slave drivers with
SKBTX_HW_TSTAMP still set and skb->sk == NULL. Drivers such as igb only
look at the flag:
drivers/net/ethernet/intel/igb/igb_main.c:igb_xmit_frame_ring() {
...
if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
...
if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON &&
!test_and_set_bit_lock(__IGB_PTP_TX_IN_PROGRESS,
&adapter->state)) {
...
}
The timestamp they eventually report is then thrown away by the !sk
check in __skb_tstamp_tx(). Requesting SOF_TIMESTAMPING_TX_HARDWARE
needs no privileges, so any socket sending over hsr0 can now take the
single TX timestamp slot on both slaves for nothing. A ptp4l frame sent
at the same time can then be skipped (tx_hwtstamp_skipped).
"hsr: Assign a socket for cloned skbs" later in the series only calls
skb_set_owner_w() when frame->req_tx_port != HSR_PT_NONE.
hsr_dev_xmit() sets that only for ETH_P_1588 frames carrying the
HSR_INLINE_HDR magic header. All other traffic still ends up with this
combination at the end of the series.
This path already owns a private skb_shared_info after skb_cow_head().
Would it make sense to clear SKBTX_ANY_TSTAMP there when the clone has
no socket?
Also, the commit message says skb_shared_info is shared via skb_clone()
and skb_cow_head(). On this path pskb_expand_head() makes a private
copy of it instead. Could that sentence be reworded?
[Severity: Medium]
This isn't a bug introduced by this patch, but the clone returned here
still shares its head with frame->skb_std and with the clone made for the
other port. The same applies to skb_clone(frame->skb_hsr) above. Is that
safe, given what hsr_xmit() writes into the header afterwards?
When frame->port_rcv is the master, hsr_xmit() does:
net/hsr/hsr_forward.c:hsr_xmit() {
...
if (frame->port_rcv->type == HSR_PT_MASTER) {
hsr_addr_subst_dest(frame->node_src, skb, port);
...
ether_addr_copy(eth_hdr(skb)->h_source, port->dev->dev_addr);
}
...
}
hsr_addr_subst_dest() rewrites h_dest for only one of the ports:
net/hsr/hsr_framereg.c:hsr_addr_subst_dest() {
...
if (port->type != node_dst->addr_B_port)
return;
if (is_valid_ether_addr(node_dst->macaddress_B))
ether_addr_copy(eth_hdr(skb)->h_dest, node_dst->macaddress_B);
}
The h_source writes look harmless, because hsr_dev_finalize() gives both
slaves the same MAC address. The h_dest substitution is per port, though.
Take a slave with NETIF_F_HW_HSR_TAG_INS but without NETIF_F_HW_HSR_DUP,
and a unicast destination node with a valid macaddress_B on SLAVE_B. Could
port A's frame, still queued, go out with port B's macaddress_B as its
destination? Taps that share the head would also see the rewritten
addresses.
The comment above hsr_create_tagged_frame() says "Otherwise, create a
private frame especially tagged for 'port'", but only the non-offloaded
path provides that. Should these clones also get a private header before
they are returned?
[ ... ]
> /* skb_put_padto free skb on error and hsr_fill_tag returns NULL in
> * that case
> */
> return hsr_fill_tag(skb, frame, port, port->hsr->prot_version);
> +err:
> + kfree_skb(skb);
> + return NULL;
> }
>
> struct sk_buff *prp_create_tagged_frame(struct hsr_frame_info *frame,
[Severity: Low]
prp_create_tagged_frame() still builds its tagged frame with
skb_copy_expand():
skb = skb_copy_expand(frame->skb_std, skb_headroom(frame->skb_std),
skb_tailroom(frame->skb_std) + HSR_HLEN,
GFP_ATOMIC);
skb_copy_expand() ends in skb_copy_header(), which copies only gso_size,
gso_segs and gso_type from skb_shared_info. Doesn't that mean PRP frames
still lose the SKBTX_* tx_flags and tskey, the same loss this commit fixes
for HSR?
A later patch in the series, "hsr: Assign a socket for cloned skbs", says
PRP is left out on purpose:
"This does not consider PRP part of the stack. The specification says
that PTP-PRP packets do not have the RCT therefore these packets can be
sent directly from userland skipping the stack."
PRP also adds an RCT trailer rather than an HSR header, so it is outside
the scope of this patch. Could the commit message say that PRP is
intentionally left unchanged?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203-hsr_ptp-v14-0-1511ffd4c362%40linutronix.de
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net-next v14 3/8] hsr: Add a magic header for sending PTP packets
2026-12-03 17:44 ` [PATCH net-next v14 3/8] hsr: Add a magic header for sending PTP packets Sebastian Andrzej Siewior
@ 2026-12-04 17:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04 17:55 UTC (permalink / raw)
To: bigeasy
Cc: netdev, j-rameshbabu, andrew+netdev, danishanwar, d-qiu, davem,
fmaurer, kuba, neelima, pabeni, praneeth, pratheesh,
richardcochran, horms, vigneshr, willemdebruijn.kernel,
daniel.machon, edumazet
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 3 · Low: 0
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The new UAPI struct hsr_inline_header
(include/uapi/linux/hsr_ptp.h) is only partly validated, and nothing…
- [Medium] hsr: hsr_ptp_parse_header() breaks transport_header and
csum/GSO invariants
- [Medium] hsr: missing checksum update on skb_pull() in
hsr_ptp_parse_header()
--
hsr: Add a magic header for sending PTP packets
This commit adds a UAPI struct hsr_inline_header. It must be prepended to
ETH_P_1588 frames sent through the HSR master device.
hsr_ptp_parse_header() checks the magic and pad fields, extracts the
requested tx port and the has_hsr_header flag, and strips the inline
header before passing both values to hsr_forward_skb().
> diff --git a/include/uapi/linux/hsr_ptp.h b/include/uapi/linux/hsr_ptp.h
> new file mode 100644
> index 0000000000000..95e905441ebdc
> --- /dev/null
> +++ b/include/uapi/linux/hsr_ptp.h
[ ... ]
> +#define HSR_INLINE_HDR 0xaf485352
> +#define HSR_INLINE_HDR_PORT_A 1
> +#define HSR_INLINE_HDR_PORT_B 2
> +
> +struct hsr_inline_header {
> + __u8 req_tx_port;
> + __u8 has_hsr_header;
> + __be32 __pad0;
> + __be32 magic;
> + __be16 __pad1;
> + __be16 eth_type;
> +} __attribute__ ((packed));
[ ... ]
> diff --git a/net/hsr/hsr_device.c b/net/hsr/hsr_device.c
> index 59df647d2fd45..252646e80f724 100644
> --- a/net/hsr/hsr_device.c
> +++ b/net/hsr/hsr_device.c
[ ... ]
> @@ -221,26 +223,83 @@ static netdev_features_t hsr_fix_features(struct net_device *dev,
> return hsr_features_recompute(hsr, features);
> }
>
> +static int hsr_ptp_parse_header(struct sk_buff *skb, bool *has_hsr_header,
> + enum hsr_port_type *req_tx_port)
> +{
[ ... ]
> + hsr_inline_hdr = (struct hsr_inline_header *)skb_mac_header(skb);
> + if (hsr_inline_hdr->magic != htonl(HSR_INLINE_HDR))
> + return -EINVAL;
> +
> + *has_hsr_header = !!hsr_inline_hdr->has_hsr_header;
> + *req_tx_port = hsr_inline_hdr->req_tx_port;
> + if (*req_tx_port != HSR_PT_SLAVE_A && *req_tx_port != HSR_PT_SLAVE_B)
> + return -EINVAL;
> +
> + if (hsr_inline_hdr->__pad0 != 0 || hsr_inline_hdr->__pad1 != 0)
> + return -EINVAL;
> +
> + hdr_len = ETH_HLEN;
> + if (*has_hsr_header)
> + hdr_len += HSR_HLEN;
> +
> + /* Ensure the real header after the stripped one can be
> + * accessed
> + */
> + if (!pskb_may_pull(skb, sizeof(struct hsr_inline_header) +
> + hdr_len))
> + return -EINVAL;
> +
> + eth_hdr = skb_pull(skb, sizeof(struct hsr_inline_header));
> + skb_set_network_header(skb, hdr_len);
> + skb->protocol = eth_hdr->h_proto;
> + return 0;
> +}
[Severity: Medium]
The inline header is stripped with a plain skb_pull(), so the checksum
state of the skb is not adjusted. Suppose the skb is CHECKSUM_PARTIAL
and csum_start lies inside the 14 bytes being removed. Then
skb_checksum_start_offset() goes negative while ip_summed stays
CHECKSUM_PARTIAL.
virtio_net_hdr_to_skb() requires a csum_start of at least 20. However,
a tc egress action on the HSR master runs before hsr_dev_xmit() and can
shrink the offset further. For example, TCA_VLAN_ACT_POP_ETH calls
skb_eth_pop(), which uses skb_pull_rcsum(). That leaves an offset of 6
with CHECKSUM_PARTIAL intact. NETIF_F_HW_CSUM is set on the HSR master,
so validate_xmit_skb() does not resolve the checksum there. The pull
here then leaves an offset of -8.
Now the frame reaches a slave without checksum offload.
skb_checksum_help() compares the negative offset as unsigned against
skb_headlen(), dumps the skb and hits WARN_ONCE(). With panic_on_warn
that is a panic. A slave with NETIF_F_HW_CSUM instead gets the bogus
start offset passed straight into its driver.
Would skb_pull_rcsum() be enough here? For CHECKSUM_PARTIAL it only
downgrades the skb to CHECKSUM_NONE when the start offset goes
negative. On transmit that means nobody fills in the checksum. Should
hsr_ptp_parse_header() instead refuse CHECKSUM_PARTIAL skbs outright?
[Severity: Medium]
hsr_dev_setup() advertises NETIF_F_GSO_MASK and NETIF_F_HW_CSUM on the
HSR master, and hsr_features_recompute() keeps them. As a result,
validate_xmit_skb() passes CHECKSUM_PARTIAL and SKB_GSO_DODGY skbs to
hsr_dev_xmit() untouched. An AF_PACKET sender using PACKET_VNET_HDR can
set csum_start to 20, and skb_partial_csum_set() puts transport_header
at the same spot.
After the inline header is pulled here, network_header moves to 14 (or
20 with has_hsr_header) bytes past the new data pointer. Meanwhile
transport_header and csum_start stay where they were, so they now sit
8 or 14 bytes before network_header. Nothing rejects the offload state
or resets transport_header.
In that case skb_network_header_len() and skb_gso_network_seglen()
wrap to roughly 4 GiB, because both are unsigned. A later patch in this
series sends has_hsr_header frames straight to the slave with
dev_queue_xmit(). From then on, drivers such as enetc, mvneta and ena
program skb_network_header_len() into their TX descriptors for
CHECKSUM_PARTIAL frames. A tc egress program on the slave that uses
bpf_skb_check_mtu() with BPF_MTU_CHK_SEGS also sees the wrapped GSO
length.
PACKET_VNET_HDR is not needed to get a bad transport_header.
packet_parse_headers() probes the transport header of the ETH_P_1588
frame, so transport_header is already 14 bytes stale after the pull.
Is there any reason a PTP frame sent this way should carry checksum or
GSO offload state? If not, could hsr_ptp_parse_header() reject
skb->ip_summed == CHECKSUM_PARTIAL and skb_is_gso(skb), and reset
transport_header after the pull?
[Severity: Medium]
This patch makes struct hsr_inline_header part of the UAPI. Should
hsr_ptp_parse_header() be stricter about the values it accepts?
The pad fields are rejected when they are non-zero, but has_hsr_header
is read as:
*has_hsr_header = !!hsr_inline_hdr->has_hsr_header;
so any value from 1 to 255 means "has header". Could values above 1 be
rejected, so this byte can still carry flags later?
The eth_type field is never checked either. hsr_dev_xmit() decides
whether to parse the inline header from skb->protocol alone:
if (skb->protocol == htons(ETH_P_1588) &&
hsr_ptp_parse_header(skb, &has_hsr_header, &req_tx_port))
af_packet fills skb->protocol from offset 12 via
hsr_header_ops.parse_protocol only when the sender gave protocol 0 or
ETH_P_ALL. packet_snd() and tpacket_fill_skb() skip the assignment for
ETH_P_ALL.
So if sll_protocol is ETH_P_1588, the inline header is accepted no
matter what eth_type contains. If a different concrete protocol is
given, a well-formed inline header is sent on the wire unchanged as
the Ethernet header.
Should hsr_ptp_parse_header() also check that
hsr_inline_hdr->eth_type == htons(ETH_P_1588)?
What happens when has_hsr_header is set on a PRP_V1 device, or on an
HSR device when the inner frame is not ETH_P_HSR (or ETH_P_PRP for
HSRv0)?
PRP carries its redundancy information in an RCT trailer, not a
header, yet hdr_len becomes ETH_HLEN + HSR_HLEN. That gives
skb_set_network_header(skb, 20), and skb_reset_mac_len() in
hsr_dev_xmit() then sets mac_len to 20 for a plain 14 byte Ethernet
frame. Should has_hsr_header be checked against hsr->prot_version and
the inner h_proto?
This matters more later in the series. After "hsr: Use the port and
header information in hsr_forward_skb()", has_hsr_header=1 skips
hsr_get_node(), sequence numbering and tagging. hsr_forward_do() then
sends a clone of skb_std as is:
if (frame->has_hsr_header && frame->skb_std) {
skb = skb_clone(frame->skb_std, GFP_ATOMIC);
...
goto inject_into_stack;
}
so the flag is trusted without any check that the frame really carries
an HSR tag. hsr_ptp_parse_header() and hsr_ptp.h are unchanged at the
end of the series.
The new UAPI header also has no comments, and nothing under
Documentation/ describes the format. That includes the magic value,
the port encoding, the PRP behaviour, and the rule that the header is
mandatory for ETH_P_1588 frames sent through the HSR master. Could
this be documented, in hsr_ptp.h or under Documentation/?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203-hsr_ptp-v14-0-1511ffd4c362%40linutronix.de
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net-next v14 5/8] hsr: Use the port and header information in hsr_forward_skb()
2026-12-03 17:44 ` [PATCH net-next v14 5/8] hsr: Use the port and header information in hsr_forward_skb() Sebastian Andrzej Siewior
@ 2026-12-04 17:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04 17:55 UTC (permalink / raw)
To: bigeasy
Cc: netdev, j-rameshbabu, andrew+netdev, danishanwar, d-qiu, davem,
fmaurer, kuba, neelima, pabeni, praneeth, pratheesh,
richardcochran, horms, vigneshr, willemdebruijn.kernel,
daniel.machon, edumazet
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] The commit message says the new behaviour "is only used in the
pure software version" and that HW offload "need[s] to be addressed…
- [Low] Only hsr_fill_frame_info() (net/hsr/hsr_forward.c:680-686)
handles the new has_hsr_header flag.
--
hsr: Use the port and header information in hsr_forward_skb()
With this patch, hsr_forward_skb() honours an egress port and an existing
HSR header supplied by the upper layer. A frame with a requested port is
sent only on that port. A frame that already carries an HSR header is
cloned and sent unchanged, skipping sequence number assignment, source
address substitution and the node_db lookup.
> diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
> index f49d962337f8b..1d1d75ed8cd06 100644
> --- a/net/hsr/hsr_forward.c
> +++ b/net/hsr/hsr_forward.c
[ ... ]
> @@ -570,6 +571,20 @@ static void hsr_forward_do(struct hsr_frame_info *frame)
> if ((port->dev->features & NETIF_F_HW_HSR_DUP) && sent)
> continue;
>
> + /* If this frame has a specific outgoing port specified (by the
> + * sender) then send it on this port only.
> + */
> + if (frame->req_tx_port != HSR_PT_NONE &&
> + frame->req_tx_port != port->type)
> + continue;
> + /* If this TX frame already has a HSR header then forward the
> + * frame with it and don't add one.
> + */
> + if (frame->has_hsr_header && frame->skb_std) {
> + skb = skb_clone(frame->skb_std, GFP_ATOMIC);
> + goto inject_into_stack;
> + }
> +
[Severity: Medium]
The commit message says "This is only used in the pure software version",
but is that enforced anywhere in the code?
hsr_dev_xmit() parses the inline header for every ETH_P_1588 skb. It does
not look at hsr->fwd_offloaded or at the NETIF_F_HW_HSR_* features of the
slaves:
net/hsr/hsr_device.c:hsr_dev_xmit() {
...
if (skb->protocol == htons(ETH_P_1588) &&
hsr_ptp_parse_header(skb, &has_hsr_header, &req_tx_port))
goto drop;
...
hsr_forward_skb(skb, master, req_tx_port, has_hsr_header);
...
}
The req_tx_port filter above then leaves only one slave. On a DSA user
port with NETIF_F_HW_HSR_DUP, though, the tagger adds the other HSR port
back:
net/dsa/tag.h:dsa_xmit_port_mask() {
...
if (IS_ENABLED(CONFIG_HSR) &&
unlikely(dev->features & NETIF_F_HW_HSR_DUP)) {
...
dsa_hsr_foreach_port(other_dp, dp->ds, hsr_dev)
mask |= BIT(other_dp->index);
}
...
}
The full path is:
hsr_dev_xmit()->hsr_forward_skb()->hsr_forward_do()->hsr_xmit()->
dev_queue_xmit()->DSA tagger->dsa_xmit_port_mask()
Would a PTP frame requested for port A only then go out on both ring
ports, with a TX timestamp reported for only one of them?
Slaves with NETIF_F_HW_HSR_TAG_INS raise a similar question. A frame with
has_hsr_header set skips hsr_create_tagged_frame() and is handed to
hardware that may insert its own tag. This depends on the hardware, but
could the frame end up with two HSR tags?
Nothing in this commit or later in the series appears to reject the
inline header when offload is active. The follow-up commit "hsr: Move
struct hsr_ethhdr to a global header" only exports the header so drivers
can handle this later.
Should hsr_dev_xmit() reject the request when the slaves have HSR offload
features, rather than quietly giving it different semantics?
[ ... ]
> @@ -661,6 +677,13 @@ int hsr_fill_frame_info(__be16 proto, struct sk_buff *skb,
> struct hsr_port *port = frame->port_rcv;
> struct hsr_priv *hsr = port->hsr;
>
> + if (frame->has_hsr_header) {
> + frame->skb_std = skb;
> +
> + WARN_ON_ONCE(port->type != HSR_PT_MASTER);
> + WARN_ON_ONCE(skb->mac_len < sizeof(struct hsr_ethhdr));
> + return 0;
> + }
[Severity: Low]
Only the HSR fill_frame_info op handles has_hsr_header. What happens on a
PRP device? prp_fill_frame_info() has no matching branch:
net/hsr/hsr_forward.c:prp_fill_frame_info() {
struct prp_rct *rct = skb_get_PRP_rct(skb);
if (rct &&
prp_check_lsdu_size(skb, rct, frame->is_supervision)) {
frame->skb_hsr = NULL;
frame->skb_std = NULL;
frame->skb_prp = skb;
frame->sequence_nr = prp_get_skb_sequence_nr(rct);
return 0;
}
handle_std_frame(skb, frame);
...
}
hsr_dev_xmit() passes has_hsr_header down without checking
hsr->prot_version. fill_frame_info() also skips hsr_get_node() for these
frames whatever the protocol is. On PRP, the outcome therefore seems to
depend on the payload.
If the payload does not end in a valid RCT, handle_std_frame() uses up a
sequence number (hsr->sequence_nr++) that the transmitted frame never
carries. The clone shortcut in hsr_forward_do() then sends the frame
unmodified.
If the payload does end in a valid RCT, skb_std is NULL, so the
"frame->has_hsr_header && frame->skb_std" shortcut is skipped. The frame
goes through prp_register_frame_out()->prp_drop_frame()->
prp_create_tagged_frame(), and prp_set_lan_id() rewrites the trailer that
the user supplied.
This does not crash: node_src is NULL, but none of the PRP paths
dereference it. Still, the "header already present, send untouched"
behaviour described in the commit message only holds for HSR. The
follow-up commit "hsr: Assign a socket for cloned skbs" says PRP is not
considered, yet the PRP device still parses the inline header and acts on
it.
Should has_hsr_header be rejected for PRP devices, or should
prp_fill_frame_info() handle it the same way as hsr_fill_frame_info()?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203-hsr_ptp-v14-0-1511ffd4c362%40linutronix.de
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net-next v14 6/8] hsr: Assign a socket for cloned skbs
2026-12-03 17:44 ` [PATCH net-next v14 6/8] hsr: Assign a socket for cloned skbs Sebastian Andrzej Siewior
@ 2026-12-04 17:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04 17:55 UTC (permalink / raw)
To: bigeasy
Cc: netdev, j-rameshbabu, andrew+netdev, danishanwar, d-qiu, davem,
fmaurer, kuba, neelima, pabeni, praneeth, pratheesh,
richardcochran, horms, vigneshr, willemdebruijn.kernel,
daniel.machon, edumazet
Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 1
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] A PRP device now delivers a TX timestamp for a PTP frame only
when has_hsr_header=1; with has_hsr_header=0 it silently drops it.
- [Low] Both call sites added by this patch give clones a socket only
when the frame came through the new PTP inline header.
--
hsr: Assign a socket for cloned skbs
This patch gives skbs cloned in hsr_create_tagged_frame() and in the
has_hsr_header branch of hsr_forward_do() the socket of the original skb.
skb_clone() clears skb->sk and hsr_forward_skb() frees the original.
Without this change, skb_tstamp_tx() has no socket to report the requested
TX timestamp to.
> diff --git a/net/hsr/hsr_forward.c b/net/hsr/hsr_forward.c
> index 1d1d75ed8cd06..9cb3297a8640a 100644
> --- a/net/hsr/hsr_forward.c
> +++ b/net/hsr/hsr_forward.c
[ ... ]
> @@ -347,6 +348,9 @@ struct sk_buff *hsr_create_tagged_frame(struct hsr_frame_info *frame,
> if (!skb)
> return NULL;
>
> + if (frame->req_tx_port != HSR_PT_NONE && frame->skb_std->sk)
> + skb_set_owner_w(skb, frame->skb_std->sk);
> +
[Severity: Low]
This isn't a bug, but the subject ("hsr: Assign a socket for cloned
skbs") and the last paragraph ("Assign the socket of the original skb to
the clone") suggest that every HSR clone is now owned by a socket.
The socket is only assigned when the frame came in through the new PTP
inline header. Normal HSR traffic still gets clones with no owner. Could
the changelog mention that restriction?
The two sites also use different conditions. This one checks
frame->req_tx_port, while the hsr_forward_do() site below checks
frame->has_hsr_header. Both are meant to cover frames from the PTP inline
header.
That only works because hsr_ptp_parse_header() is the only place that sets
either field. It rejects frames without skb->sk, and it rejects any
req_tx_port other than HSR_PT_SLAVE_A or HSR_PT_SLAVE_B:
net/hsr/hsr_device.c:hsr_ptp_parse_header() {
...
if (!skb->sk)
return -EINVAL;
...
*has_hsr_header = !!hsr_inline_hdr->has_hsr_header;
*req_tx_port = hsr_inline_hdr->req_tx_port;
if (*req_tx_port != HSR_PT_SLAVE_A && *req_tx_port != HSR_PT_SLAVE_B)
return -EINVAL;
...
}
If a later change sets one of these fields without the other, the two
checks would no longer agree. Would a single shared condition be easier
to keep consistent?
> if (port->dev->features & NETIF_F_HW_HSR_TAG_INS)
> return skb;
>
[ ... ]
> @@ -582,6 +586,8 @@ static void hsr_forward_do(struct hsr_frame_info *frame)
> */
> if (frame->has_hsr_header && frame->skb_std) {
> skb = skb_clone(frame->skb_std, GFP_ATOMIC);
> + if (skb && frame->skb_std->sk)
> + skb_set_owner_w(skb, frame->skb_std->sk);
> goto inject_into_stack;
> }
[Severity: Medium]
The changelog says:
This does not consider PRP part of the stack. The specification says
that PTP-PRP packets do not have the RCT therefore these packets can be
sent directly from userland skipping the stack.
Is that accurate? This branch of hsr_forward_do() runs for both HSR and
PRP devices.
Neither hsr_dev_xmit() nor hsr_ptp_parse_header() checks
hsr->prot_version. A PRP device therefore accepts struct
hsr_inline_header with req_tx_port set to port A or B. For ETH_P_1588 it
also requires that header.
With has_hsr_header=1 on a PRP device, prp_fill_frame_info() finds no RCT
and calls handle_std_frame(). That sets skb_std, so this branch is taken.
The clone gets skb_set_owner_w() and the timestamp reaches the user.
With has_hsr_header=0 on the same device, hsr_forward_do() calls
hsr->proto_ops->create_tagged_frame(), which is prp_create_tagged_frame():
net/hsr/hsr_forward.c:prp_create_tagged_frame() {
...
} else if (port->dev->features & NETIF_F_HW_HSR_TAG_INS) {
return skb_clone(frame->skb_std, GFP_ATOMIC);
}
skb = skb_copy_expand(frame->skb_std, skb_headroom(frame->skb_std),
skb_tailroom(frame->skb_std) + HSR_HLEN,
GFP_ATOMIC);
return prp_fill_rct(skb, frame, port);
}
Neither of these paths assigns an owner. skb_tstamp_tx() then has no
socket, so the requested timestamp is silently lost. The frame also gets
an RCT appended, and the changelog says PTP-PRP frames should not carry
one.
After this patch, hsr_create_tagged_frame() and the shared has_hsr_header
branch both set the owner for req_tx_port frames.
prp_create_tagged_frame() is the only req_tx_port transmit path that
doesn't. On PRP, whether the timestamp arrives therefore depends on the
HSR-named has_hsr_header flag.
Should hsr_ptp_parse_header() reject the inline header on PRP_V1 devices?
Alternatively, should prp_create_tagged_frame() handle req_tx_port frames
the same way hsr_create_tagged_frame() now does? If neither, could the
changelog at least describe what happens on PRP?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203-hsr_ptp-v14-0-1511ffd4c362%40linutronix.de
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net-next v14 8/8] selftests: hsr: Add test for the inline PTP header on HSR
2026-12-03 17:44 ` [PATCH net-next v14 8/8] selftests: hsr: Add test for the inline PTP header on HSR Sebastian Andrzej Siewior
@ 2026-12-04 17:55 ` netdev-bot+sashiko
0 siblings, 0 replies; 14+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04 17:55 UTC (permalink / raw)
To: bigeasy
Cc: netdev, j-rameshbabu, andrew+netdev, danishanwar, d-qiu, davem,
fmaurer, kuba, neelima, pabeni, praneeth, pratheesh,
richardcochran, horms, vigneshr, willemdebruijn.kernel,
daniel.machon, edumazet
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The file header comment in
tools/testing/selftests/net/hsr/hsr_ptp_test.c (lines 2-11) says: "the…
- [Low] selftests: hsr: raw_recv() uses htons() instead of ntohs() for
encap_type
--
selftests: hsr: Add test for the inline PTP header on HSR
This adds a selftest for the inline PTP header that the HSR stack uses with
ether type ETH_P_1588. It sends four sample PTP packets on the HSR device,
each requesting port A or port B, with and without a user-supplied HSR
header. It then checks that each frame shows up only on the requested
slave port with the expected content.
> diff --git a/tools/testing/selftests/net/hsr/hsr_ptp_test.c b/tools/testing/selftests/net/hsr/hsr_ptp_test.c
> new file mode 100644
> index 0000000000000..e2c2da56138ae
> --- /dev/null
> +++ b/tools/testing/selftests/net/hsr/hsr_ptp_test.c
> @@ -0,0 +1,496 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Simple test to verify the usage of the inline header used for HSR with
> + * ether type ETH_P_1588.
> + * The inline header has to be stripped, the sent packet must only appear on the
> + * specified port and the interface needs to accept a foreign HSR header and
> + * prepend its own header.
[Severity: Low]
This isn't a bug, but does this comment describe what the stack actually
does? Read literally, it says the HSR device takes a frame that already has
a foreign HSR header and adds its own header on top.
hsr_forward_do() in net/hsr/hsr_forward.c sends the supplied header as is:
/* If this TX frame already has a HSR header then forward the
* frame with it and don't add one.
*/
if (frame->has_hsr_header && frame->skb_std) {
skb = skb_clone(frame->skb_std, GFP_ATOMIC);
...
goto inject_into_stack;
}
The fwd_hsr checks in raw_recv() expect the same thing. They only pass if
the sequence number and the bit-flipped source MAC written by pkt_send()
arrive unchanged:
if (fwd_hsr) {
if (hsr_hdr->sequence_nr != htons(hsr_seq - 1)) {
...
hsr_hdr->src[3] ^= 0x21;
if (memcmp(hsr_hdr->src, slave_mac_addr, MAC_LEN)) {
Is the comment meant to cover two separate cases? That would be: accept a
foreign HSR header when has_hsr_header is 1, and prepend the stack's own
header when has_hsr_header is 0. If so, could it be reworded to say that
explicitly?
[ ... ]
> @@ -0,0 +1,496 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Simple test to verify the usage of the inline header used for HSR with
> + * ether type ETH_P_1588.
> + * The inline header has to be stripped, the sent packet must only appear on the
> + * specified port and the interface needs to accept a foreign HSR header and
> + * prepend its own header.
> + *
> + * Socket handling inspired by raw.c from linuxptp.
> + *
> + */
> +#include <poll.h>
> +#include <stdbool.h>
> +#include <stdint.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <time.h>
> +#include <unistd.h>
> +
> +#include <linux/types.h>
> +#include <linux/hsr_ptp.h>
> +#include <linux/if_ether.h>
> +#include <net/if.h>
> +#include <netinet/in.h>
> +#include <netpacket/packet.h>
> +#include <sys/ioctl.h>
> +
> +#define MSEC_TO_NSEC(_x) (_x * 1000000)
> +#define USEC_PER_SEC 1000000
> +#define NSEC_PER_SEC 1000000000
> +
> +#ifndef __packed
> +#define __packed __attribute__((packed))
> +#endif
> +
> +#define MAC_LEN 6
> +typedef uint8_t eth_addr[MAC_LEN];
> +
> +struct eth_hdr {
> + eth_addr dst;
> + eth_addr src;
> + uint16_t type;
> +} __packed;
> +
> +struct hsr_hdr {
> + eth_addr dst;
> + eth_addr src;
> + uint16_t type;
> + uint16_t pathid_and_LSDU_size;
> + uint16_t sequence_nr;
> + uint16_t encap_type;
> +} __packed;
> +
> +struct hsr_meta_header {
> + struct hsr_inline_header hsr_opt;
> + union {
> + struct hsr_hdr hsr_hdr;
> + struct eth_hdr eth_hdr;
> + };
> +} __packed;
> +
> +static uint8_t ptp_packet[] = {
> + 0x00, 0x12, 0x00, 0x2c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> + 0x00, 0x00, 0x00, 0x00, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x00, 0x01, 0x00, 0x01,
> + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> +};
> +
> +static uint8_t p2p_dst_mac[MAC_LEN] = {
> + 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e
> +};
> +
> +static uint8_t slave_mac_addr[MAC_LEN];
> +static int32_t fd_slaveA = -1;
> +static int32_t fd_slaveB = -1;
> +static int32_t fd_hsr = -1;
> +static uint16_t hsr_seq = 37;
> +
> +static void tsnorm(struct timespec *ts)
> +{
> + while (ts->tv_nsec >= NSEC_PER_SEC) {
> + ts->tv_nsec -= NSEC_PER_SEC;
> + ts->tv_sec++;
> + }
> +}
> +
> +static int64_t calcdiff(struct timespec t1, struct timespec t2)
> +{
> + int64_t diff = USEC_PER_SEC * (long long)((int) t1.tv_sec - (int) t2.tv_sec);
> +
> + diff += ((int) t1.tv_nsec - (int) t2.tv_nsec) / 1000;
> + return diff;
> +}
> +
> +static int tsgreater(struct timespec *a, struct timespec *b)
> +{
> + return ((a->tv_sec > b->tv_sec) ||
> + (a->tv_sec == b->tv_sec && a->tv_nsec > b->tv_nsec));
> +}
> +
> +static int sk_interface_index(int fd, const char *name)
> +{
> + struct ifreq ifreq;
> + int32_t err;
> +
> + memset(&ifreq, 0, sizeof(ifreq));
> + strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
> + err = ioctl(fd, SIOCGIFINDEX, &ifreq);
> + if (err < 0) {
> + printf("ioctl SIOCGIFINDEX failed: %m\n");
> + return err;
> + }
> + return ifreq.ifr_ifindex;
> +}
> +
> +static int open_socket(const char *name)
> +{
> + struct sockaddr_ll addr;
> + int32_t fd, index;
> +
> + fd = socket(AF_PACKET, SOCK_RAW, 0);
> + if (fd < 0) {
> + printf("socket failed: %m\n");
> + goto err;
> + }
> + index = sk_interface_index(fd, name);
> + if (index < 0)
> + goto err;
> +
> + memset(&addr, 0, sizeof(addr));
> + addr.sll_ifindex = index;
> + addr.sll_family = AF_PACKET;
> + addr.sll_protocol = htons(ETH_P_ALL);
> + if (bind(fd, (struct sockaddr *) &addr, sizeof(addr))) {
> + printf("bind failed: %m\n");
> + goto err;
> + }
> + if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))) {
> + printf("setsockopt SO_BINDTODEVICE failed: %m\n");
> + goto err;
> + }
> +
> + return fd;
> +err:
> + if (fd >= 0)
> + close(fd);
> + return -1;
> +}
> +
> +static int sk_interface_macaddr(const char *name)
> +{
> + struct ifreq ifreq;
> + int32_t err, fd;
> +
> + memset(&ifreq, 0, sizeof(ifreq));
> + strncpy(ifreq.ifr_name, name, sizeof(ifreq.ifr_name) - 1);
> +
> + fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
> + if (fd < 0) {
> + printf("socket failed: %m\n");
> + return -1;
> + }
> +
> + err = ioctl(fd, SIOCGIFHWADDR, &ifreq);
> + if (err < 0) {
> + printf("ioctl SIOCGIFHWADDR failed: %m\n");
> + close(fd);
> + return -1;
> + }
> +
> + close(fd);
> +
> + memcpy(slave_mac_addr, &ifreq.ifr_hwaddr.sa_data, MAC_LEN);
> + return 0;
> +}
> +
> +static int raw_open(const char *hsr_dev, const char *slaveA_dev, const char *slaveB_dev)
> +{
> +
> + if (sk_interface_macaddr(slaveA_dev))
> + goto err;
> +
> + fd_slaveA = open_socket(slaveA_dev);
> + if (fd_slaveA < 0)
> + goto err;
> +
> + fd_slaveB = open_socket(slaveB_dev);
> + if (fd_slaveB < 0)
> + goto err;
> +
> + fd_hsr = open_socket(hsr_dev);
> + if (fd_hsr < 0)
> + goto err;
> +
> + return 0;
> +
> +err:
> + if (fd_slaveA >= 0)
> + close(fd_slaveA);
> + if (fd_slaveB >= 0)
> + close(fd_slaveB);
> + if (fd_hsr >= 0)
> + close(fd_hsr);
> +
> + return -1;
> +}
> +
> +static int sk_receive(int fd, void *buf, int buflen)
> +{
> + struct iovec iov = { buf, buflen };
> + uint8_t control[256];
> + int32_t cnt = 0;
> + struct msghdr msg;
> +
> + memset(control, 0, sizeof(control));
> + memset(&msg, 0, sizeof(msg));
> +
> + msg.msg_iov = &iov;
> + msg.msg_iovlen = 1;
> + msg.msg_control = control;
> + msg.msg_controllen = sizeof(control);
> +
> + cnt = recvmsg(fd, &msg, MSG_DONTWAIT);
> + if (cnt < 0)
> + return 0;
> + return cnt;
> +}
> +
> +static int raw_recv(int fd, bool fwd_hsr, void *sample, int sample_len)
> +{
> + struct timespec now, end;
> + struct hsr_hdr *hsr_hdr;
> + uint8_t buf[1500];
> + int32_t cnt;
> +
> + /* The packet is expected to arrive within 100ms. HSR will send hello
> + * packets so there an end time and poll() will wait until this point.
> + */
> + clock_gettime(CLOCK_MONOTONIC, &now);
> + end = now;
> + end.tv_nsec += MSEC_TO_NSEC(100);
> + tsnorm(&end);
> +again:
> +
> + cnt = sk_receive(fd, buf, sizeof(buf));
> + if (cnt == 0) {
> + int64_t sleep_time;
> + struct pollfd pfd;
> + int ret;
> +
> + clock_gettime(CLOCK_MONOTONIC, &now);
> + if (tsgreater(&now, &end))
> + return 0;
> +
> + sleep_time = calcdiff(end, now) / 1000;
> + if (sleep_time <= 0)
> + return 0;
> +
> + pfd.fd = fd;
> + pfd.events = POLLIN;
> + ret = poll(&pfd, 1, sleep_time);
> + if (ret <= 0)
> + return ret;
> + goto again;
> + }
> +
> + if (cnt < sizeof(struct hsr_hdr))
> + goto again;
> +
> + hsr_hdr = (void *)buf;
> + /* Embedded magic packet passed? */
> + if (hsr_hdr->type == htons(ETH_P_1588)) {
> + struct hsr_inline_header *hsr_opt = (void *)buf;
> +
> + if (hsr_opt->magic == htonl(HSR_INLINE_HDR)) {
> + printf("Error: Found HSR_INLINE_HDR\n");
> + return -1;
> + }
> + }
> + /* Assuming it is *our* network and nobody but the test here sends
> + * packets to p2p_dst_mac. Therefore any received packet needs to be
> + * ours and every mismatch is considered as error.
> + */
> + if (memcmp(hsr_hdr->dst, p2p_dst_mac, MAC_LEN))
> + goto again;
> +
> + if (hsr_hdr->type != htons(ETH_P_HSR)) {
> + printf("Error: Unexpected ether type: 0x%x\n", ntohs(hsr_hdr->type));
> + return -1;
> + }
> +
> + if (hsr_hdr->encap_type != htons(ETH_P_1588)) {
> + printf("Error: Unexpected encapsulated type: 0x%x\n",
> + htons(hsr_hdr->encap_type));
> + return -1;
> + }
> +
> + if (fwd_hsr) {
> + if (hsr_hdr->sequence_nr != htons(hsr_seq - 1)) {
> + printf("Error: Packet has wrong seq nr, %d vs %d.\n",
> + ntohs(hsr_hdr->sequence_nr), hsr_seq - 1);
> + return -1;
> + }
> + /* Undo the SRC MAC change in pkt_send() */
> + hsr_hdr->src[3] ^= 0x21;
> + if (memcmp(hsr_hdr->src, slave_mac_addr, MAC_LEN)) {
> + printf("Error: Packet has wrong SRC-MAC\n");
> + return -1;
> + }
> + }
> +
> + if (cnt < sizeof(struct hsr_hdr) + sample_len) {
> + printf("Error: Packet %d is too small for data check\n", cnt);
> + return -1;
> + }
> +
> + if (!memcmp(&buf[sizeof(struct hsr_hdr)], sample, sample_len))
> + return 1;
> +
> + printf("Error: Packet did not match the sample\n");
> + return -1;
> +}
> +
> +static int recv_verify(int port, bool fwd_hsr, void *sample, int sample_len)
> +{
> + int32_t error = 0;
> + int32_t ret;
> +
> + ret = raw_recv(fd_slaveA, fwd_hsr, sample, sample_len);
> + if (ret < 0)
> + return ret;
> + if (port == HSR_INLINE_HDR_PORT_A && ret == 0) {
> + printf("Error: Missing packet on portA\n");
> + error = 1;
> + }
> + if (port == HSR_INLINE_HDR_PORT_B && ret == 1) {
> + printf("Error: Not expecting packet on portA\n");
> + error = 1;
> + }
> +
> + ret = raw_recv(fd_slaveB, fwd_hsr, sample, sample_len);
> + if (ret < 0)
> + return ret;
> + if (port == HSR_INLINE_HDR_PORT_B && ret == 0) {
> + printf("Error: Missing packet on portB\n");
> + error = 1;
> + }
> + if (port == HSR_INLINE_HDR_PORT_A && ret == 1) {
> + printf("Error: Not expecting packet on portB\n");
> + error = 1;
> + }
> + return error;
> +}
> +
> +static int32_t pkt_send(int port, bool hsr_hdr, void *data, int data_len)
> +{
> + struct hsr_meta_header *hdr;
> + uint8_t packet[200];
> + ssize_t cnt;
> + size_t len;
> +
> + memset(packet, 0, sizeof(packet));
> +
> + len = sizeof(struct hsr_inline_header);
> + hdr = (struct hsr_meta_header *)packet;
> + memset(&hdr->hsr_opt, 0, sizeof(hdr->hsr_opt));
> + hdr->hsr_opt.magic = htonl(HSR_INLINE_HDR);
> + hdr->hsr_opt.eth_type = htons(ETH_P_1588);
> + if (port != HSR_INLINE_HDR_PORT_A && port != HSR_INLINE_HDR_PORT_B) {
> + printf("Wrong port requested\n");
> + return -1;
> + }
> + hdr->hsr_opt.req_tx_port = port;
> +
> + if (hsr_hdr) {
> + uint16_t pathid_size;
> +
> + len += sizeof(struct hsr_hdr);
> + memcpy(&packet[len], data, data_len);
> +
> + hdr->hsr_opt.has_hsr_header = 1;
> +
> + memcpy(&hdr->hsr_hdr.dst, p2p_dst_mac, MAC_LEN);
> + memcpy(&hdr->hsr_hdr.src, slave_mac_addr, MAC_LEN);
> + /* Flip a bit in SRC MAC addr so it does not look like hosts */
> + hdr->hsr_hdr.src[3] ^= 0x21;
> +
> + hdr->hsr_hdr.type = htons(ETH_P_HSR);
> + hdr->hsr_hdr.sequence_nr = htons(hsr_seq++);
> + hdr->hsr_hdr.encap_type = htons(ETH_P_1588);
> +
> + /* The resulting packet must be alteast 66 bytes */
> + if (data_len + sizeof(struct hsr_hdr) < 66)
> + data_len = 66 - sizeof(struct hsr_hdr);
> +
> + pathid_size = data_len + sizeof(struct hsr_hdr) - sizeof(struct eth_hdr);
> + pathid_size |= (port - 1) << 12;
> +
> + hdr->hsr_hdr.pathid_and_LSDU_size = htons(pathid_size);
> + } else {
> + len += sizeof(struct eth_hdr);
> + hdr->hsr_opt.has_hsr_header = 0;
> +
> + memcpy(&hdr->eth_hdr.dst, p2p_dst_mac, MAC_LEN);
> + memcpy(&hdr->eth_hdr.src, slave_mac_addr, MAC_LEN);
> + hdr->eth_hdr.type = htons(ETH_P_1588);
> +
> + memcpy(&packet[len], data, data_len);
> + }
> +
> + cnt = send(fd_hsr, packet, len + data_len, 0);
> + if (cnt < 1)
> + return -1;
> +
> + return cnt;
> +}
> +
> +int main(int argc, char *argv[])
> +{
> + char *slaveA = NULL, *slaveB = NULL, *hsr_dev = NULL;
> + char *msg_mode;
> + int opt;
> +
> + while ((opt = getopt(argc, argv, "A:B:H:")) != -1) {
> + switch (opt) {
> + case 'A':
> + slaveA = strdup(optarg);
> + break;
> +
> + case 'B':
> + slaveB = strdup(optarg);
> + break;
> +
> + case 'H':
> + hsr_dev = strdup(optarg);
> + break;
> + default: /* '?' */
> + fprintf(stderr, "Usage: %s -A slaveA -B slaveB -H hsr_device\n",
> + argv[0]);
> + exit(EXIT_FAILURE);
> + }
> + }
> +
> + if (!slaveA || !slaveB || !hsr_dev) {
> + fprintf(stderr, "Missing network devices\n");
> + exit(EXIT_FAILURE);
> + }
> +
> + if (raw_open(hsr_dev, slaveA, slaveB) < 0)
> + return EXIT_FAILURE;
> +
> + msg_mode = "PortA, no-hsr-header";
> + if (pkt_send(HSR_INLINE_HDR_PORT_A, false, ptp_packet, sizeof(ptp_packet)) < 0) {
> + printf("Sending failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> + if (recv_verify(HSR_INLINE_HDR_PORT_A, false, ptp_packet, sizeof(ptp_packet))) {
> + printf("Verify failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> +
> + ptp_packet[16 + 4]++;
> + msg_mode = "PortB, no-hsr-header";
> + if (pkt_send(HSR_INLINE_HDR_PORT_B, false, ptp_packet, sizeof(ptp_packet)) < 0) {
> + printf("Sending failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> + if (recv_verify(HSR_INLINE_HDR_PORT_B, false, ptp_packet, sizeof(ptp_packet))) {
> + printf("Verify failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> +
> + ptp_packet[16 + 4]++;
> + msg_mode = "PortA, hsr-header";
> + if (pkt_send(HSR_INLINE_HDR_PORT_A, true, ptp_packet, sizeof(ptp_packet)) < 0) {
> + printf("Sending failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> + if (recv_verify(HSR_INLINE_HDR_PORT_A, true, ptp_packet, sizeof(ptp_packet))) {
> + printf("Verify failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> +
> + ptp_packet[16 + 4]++;
> + msg_mode = "PortB, hsr-header";
> + if (pkt_send(HSR_INLINE_HDR_PORT_B, true, ptp_packet, sizeof(ptp_packet)) < 0) {
> + printf("Sending failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> + if (recv_verify(HSR_INLINE_HDR_PORT_B, true, ptp_packet, sizeof(ptp_packet))) {
> + printf("Verify failed: %s\n", msg_mode);
> + return EXIT_FAILURE;
> + }
> +
> + return EXIT_SUCCESS;
> +}
>
[Severity: Low]
This isn't a bug, but should this be ntohs() rather than htons()?
hsr_hdr->encap_type is read off the wire in network byte order. The
check just above compares it against htons(ETH_P_1588), so for the
printf() it needs converting to host order. The ether type error a few
lines earlier already does that with ntohs(hsr_hdr->type).
htons() and ntohs() do the same swap on both little and big endian
hosts, so the printed value is correct either way. Using ntohs() here
would match the neighbouring diagnostic and make the direction of the
conversion obvious to the next reader of raw_recv().
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203-hsr_ptp-v14-0-1511ffd4c362%40linutronix.de
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-12-04 17:55 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 17:44 [PATCH net-next v14 0/8] hsr: Allow to send packets on specific port and pass the timestamp Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 1/8] hsr: Add header_ops::parse_protocol Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 2/8] hsr: Use skb_clone() while adding the HSR header Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 3/8] hsr: Add a magic header for sending PTP packets Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 4/8] hsr: Drop received " Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 5/8] hsr: Use the port and header information in hsr_forward_skb() Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 6/8] hsr: Assign a socket for cloned skbs Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
2026-12-03 17:44 ` [PATCH net-next v14 7/8] hsr: Move struct hsr_ethhdr to a global header Sebastian Andrzej Siewior
2026-12-03 17:44 ` [PATCH net-next v14 8/8] selftests: hsr: Add test for the inline PTP header on HSR Sebastian Andrzej Siewior
2026-12-04 17:55 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox