Skip to contentSkip to conversation

bpf: Support RX/TX HW timestamp proxy.

PATCH Superseded Kuniyuki Iwashima 5 participants 19 messages Jun 13

Subsystems (10)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: Support RX/TX HW timestamp proxy.

We have some hosts where packets come from special hardware and are provided directly to userspace, bypassing the kernel networking stack.

When standard socket applications are run on these hosts, a userspace proxy is required to mediate traffic between the hardware and the applications.

            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     ^                               ^
  userspace   |     |                               |
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|  virtual interface  |<---'
  kernel     | |   skb   +---------------------+
  -----------| |-----------------------------------------------
              |
              v
       +------------+
       |  hardware  |
       +------------+

However, even though the hardware fully supports timestamping, the HW timestamps are not directly accessible to the socket applications because the skb is consumed/injected by the proxy.

This series extends ethtool and adds BPF kfuncs to transparently support HW timestamp on such a setup.

Patch 1 is pure net-next patch to advertise fake timestamping capability on virtual interfaces (e.g. ipvlan, geneve, etc).

Patch 2 is misc cleanup.

Patch 3 & 4 add kfunc to proxy RX/TX hwtstamp.

Patch 5 is selftest to demonstrate how it works.

Note the test requires this iproute2 commit:
https://git.kernel.org/pub/scm/network/iproute2/iproute2-next.git/commit/?id=c9a9f12aa619288fd3d4e16bc4b3c73b655a4efe

Changes:
v2:
* Patch 3:
* Remove __packed and use unnamed bit-field in struct bpf_hwtstamp (Alexei Starovoitov)
* Use skb_unclone() instead of skb_heaeder_clone() (Sashiko)

* Patch 4
* Remove __packed and use unnamed bit-field in struct bpf_tx_tstamp_cmpl (Alexei Starovoitov)
* Check !skb_at_tc_ingress() in bpf_skb_complete_tx_tstamp() (Sashiko)
* Use skb_unclone() instead of skb_heaeder_clone() (Sashiko)

* Patch 5
* Use scm_timestamping64 (Sashiko)
* Correct saw_tskey check (Sashiko)
* Fix retval check for bpf_skb_get_tunnel_opt() with (int) cast (Sashiko)
* bpf-style comment

v1: https://lore.kernel.org/bpf/[email protected]/

Kuniyuki Iwashima (5):
ethtool: Introduce ETHTOOL_MSG_TSINFO_SET for virtual interfaces.
bpf: Rename bpf_kfunc_set_tcp_reqsk to bpf_kfunc_set_sched_cls.
bpf: Add bpf_skb_set_hwtstamp().
bpf: Add kfunc to proxy TX HW Timestamp.
selftest: bpf: Add test for hwtstamp proxy.

 Documentation/netlink/specs/ethtool.yaml      |  13 +
 include/linux/filter.h                        |   2 +
 include/linux/netdevice.h                     |  11 +
 include/linux/skbuff.h                        |  13 +
 include/net/tcx.h                             |   1 +
 include/uapi/linux/bpf.h                      |   1 +
 .../uapi/linux/ethtool_netlink_generated.h    |   1 +
 include/uapi/linux/pkt_cls.h                  |   3 +-
 kernel/bpf/verifier.c                         |  13 +-
 net/core/dev.c                                |  39 ++
 net/core/dev_ioctl.c                          |  31 +-
 net/core/filter.c                             |  98 ++-
 net/ethtool/common.c                          |   4 +
 net/ethtool/netlink.c                         |   8 +
 net/ethtool/netlink.h                         |   1 +
 net/ethtool/tsconfig.c                        |   7 +-
 net/ethtool/tsinfo.c                          | 123 +++-
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  10 +
 .../selftests/bpf/prog_tests/proxy_hwtstamp.c | 588 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_tracing_net.h     |   1 +
 .../selftests/bpf/progs/proxy_hwtstamp.c      | 236 +++++++
 21 files changed, 1186 insertions(+), 18 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/proxy_hwtstamp.c
 create mode 100644 tools/testing/selftests/bpf/progs/proxy_hwtstamp.c
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#1 lore raw

PATCH v2 1/5 ethtool: Introduce ETHTOOL_MSG_TSINFO_SET for virtual interfaces.

Before enabling SO_TIMESTAMPING, applications typically try to enable hardware timestamping on network interfaces via SIOCSHWTSTAMP (or ETHTOOL_MSG_TSCONFIG_SET).

The timestamping capability on an interface can be checked via ETHTOOL_MSG_TSINFO_GET:

  # ethtool -T eth0
  Time stamping parameters for eth0:
  Capabilities:
  	hardware-transmit
  	software-transmit
  	hardware-receive
  	software-receive
  	software-system-clock
  	hardware-raw-clock
  PTP Hardware Clock: none
  Hardware Transmit Timestamp Modes:
  	off
  	on
  Hardware Receive Filter Modes:
  	none
  	all

These operations rely on the driver implementing two callbacks, dev->netdev_ops->ndo_hwtstamp_{get,set}().

However, among all virtual network interfaces, only bond and macvlan currently implement them.

As a result, most virtual interfaces cannot advertise the capabilities of their underlying devices:

  # ip link add ipvl0 link eth0 type ipvlan mode l2 bridge
  # ethtool -T ipvl0
  Time stamping parameters for ipvl0:
  Capabilities:
  	software-receive
  	software-system-clock
  PTP Hardware Clock: none
  Hardware Transmit Timestamp Modes: none
  Hardware Receive Filter Modes: none

While these callbacks could be implemented in each virtual interface, this approach is limited to those directly linked to a physical device.

Not all virtual interfaces are tied to real hardware; for instance, packets from UDP tunnel devices eventually pass through physical devices and can be hardware-timestamped there.

Let's allow configuring the hardware timestamping capability on virtual interfaces via ETHTOOL_MSG_TSINFO_SET.

Note that SOF_TIMESTAMPING_RX_SOFTWARE and SOF_TIMESTAMPING_SOFTWARE are automatically added since __ethtool_get_ts_info() and ethnl_tsinfo_end_dump() report them for all devices.

By configuring this capability, ioctl(SIOCSHWTSTAMP) (hwstamp_ctl below) can enable TX/RX hardware timestamping successfully:

  # ./tools/net/ynl/pyynl/cli.py \
      --spec Documentation/netlink/specs/ethtool.yaml \
      --do tsinfo-set \
      --json '{"header": {"dev-index": 6},
               "timestamping": {"nomask": true, "bits": {
                   "bit": [{"name": "hardware-transmit"},
                           {"name": "software-transmit"},
                           {"name": "hardware-receive"}]
               }},
               "tx-types": {"nomask": true, "bits": {
                   "bit": [{"name": "off"}, {"name": "on"}]
               }},
               "rx-filters": {"nomask": true, "bits": {
                   "bit" : [{"name": "none"}, {"name": "all"}]
               }}}'
  # ethtool -T ipvl0
  Time stamping parameters for ipvl0:
  Capabilities:
  	hardware-transmit
  	software-transmit
  	hardware-receive
  	software-receive
  	software-system-clock
  PTP Hardware Clock: none
  Hardware Transmit Timestamp Modes:
  	off
  	on
  Hardware Receive Filter Modes:
  	none
  	all
  # hwstamp_ctl -i ipvl0 -t 1 -r 1
  current settings:
  tx_type 0
  rx_filter 0
  new settings:
  tx_type 1
  rx_filter 1
---
 Documentation/netlink/specs/ethtool.yaml      |  13 ++
 include/linux/netdevice.h                     |  11 ++
 .../uapi/linux/ethtool_netlink_generated.h    |   1 +
 net/core/dev_ioctl.c                          |  31 ++++-
 net/ethtool/common.c                          |   4 +
 net/ethtool/netlink.c                         |   8 ++
 net/ethtool/netlink.h                         |   1 +
 net/ethtool/tsconfig.c                        |   7 +-
 net/ethtool/tsinfo.c                          | 123 +++++++++++++++++-
 9 files changed, 188 insertions(+), 11 deletions(-)
Documentation/netlink/specs/ethtool.yaml +13 -0
index 5dd4d1b5d94b..2dace12c8b4d 100644
@@ -2859,6 +2859,19 @@ operations:
             - worst-channel
             - link
       dump: *mse-get-op
+    -
+      name: tsinfo-set
+      doc: Set tsinfo params.
+
+      attribute-set: tsinfo
+
+      do: &tsinfo-set-op
+        request:
+          attributes:
+            - header
+            - timestamping
+            - tx-types
+            - rx-filters
 
 mcast-groups:
   list:
include/linux/netdevice.h +11 -0
index 74507c006490..2693161d4168 100644
@@ -2087,6 +2087,7 @@ enum netdev_reg_state {
  *				offload capabilities of the device
  *	@udp_tunnel_nic:	UDP tunnel offload state
  *	@ethtool:	ethtool related state
+ *	@tsinfo:	HW timestamping capability for virtual devices
  *	@xdp_state:		stores info on attached XDP BPF programs
  *
  *	@nested_level:	Used as a parameter of spin_lock_nested() of
@@ -2509,6 +2510,16 @@ struct net_device {
 	 */
 	struct netdev_config	*cfg_pending;
 	struct ethtool_netdev_state *ethtool;
+	struct {
+		struct {
+			u32	tx_type;
+			u32	rx_filter;
+		} cfg;
+		u32		so_timestamping;
+		u32		tx_types;
+		u32		rx_filters;
+		bool		enabled;
+	} tsinfo;
 
 	/* protected by rtnl_lock */
 	struct bpf_xdp_entity	xdp_state[__MAX_XDP_MODE];
include/uapi/linux/ethtool_netlink_generated.h +1 -0
index 8134baf7860f..5aaa3bf9a073 100644
@@ -893,6 +893,7 @@ enum {
 	ETHTOOL_MSG_RSS_CREATE_ACT,
 	ETHTOOL_MSG_RSS_DELETE_ACT,
 	ETHTOOL_MSG_MSE_GET,
+	ETHTOOL_MSG_TSINFO_SET,
 
 	__ETHTOOL_MSG_USER_CNT,
 	ETHTOOL_MSG_USER_MAX = (__ETHTOOL_MSG_USER_CNT - 1)
net/core/dev_ioctl.c +27 -4
index f3979b276090..3ecc57fd75c9 100644
@@ -260,6 +260,15 @@ int dev_get_hwtstamp_phylib(struct net_device *dev,
 {
 	struct hwtstamp_provider *hwprov;
 
+	if (!dev->netdev_ops->ndo_hwtstamp_get) {
+		if (!dev->tsinfo.enabled)
+			return -EOPNOTSUPP;
+
+		cfg->rx_filter = dev->tsinfo.cfg.rx_filter;
+		cfg->tx_type = dev->tsinfo.cfg.tx_type;
+		return 0;
+	}
+
 	hwprov = rtnl_dereference(dev->hwprov);
 	if (hwprov) {
 		cfg->qualifier = hwprov->desc.qualifier;
@@ -286,7 +295,7 @@ static int dev_get_hwtstamp(struct net_device *dev, struct ifreq *ifr)
 	struct hwtstamp_config cfg;
 	int err;
 
-	if (!ops->ndo_hwtstamp_get)
+	if (!ops->ndo_hwtstamp_get && !dev->tsinfo.enabled)
 		return -EOPNOTSUPP;
 
 	if (!netif_device_present(dev))
@@ -337,6 +346,20 @@ int dev_set_hwtstamp_phylib(struct net_device *dev,
 	bool phy_ts;
 	int err;
 
+	if (!ops->ndo_hwtstamp_set) {
+		if (!dev->tsinfo.enabled ||
+		    !(dev->tsinfo.tx_types & BIT(cfg->tx_type)) ||
+		    !(dev->tsinfo.rx_filters & BIT(cfg->rx_filter)))
+			return -EOPNOTSUPP;
+
+		if (cfg->flags)
+			return -EINVAL;
+
+		dev->tsinfo.cfg.tx_type = cfg->tx_type;
+		dev->tsinfo.cfg.rx_filter = cfg->rx_filter;
+		return 0;
+	}
+
 	hwprov = rtnl_dereference(dev->hwprov);
 	if (hwprov) {
 		if (hwprov->source == HWTSTAMP_SOURCE_PHYLIB &&
@@ -413,7 +436,7 @@ static int dev_set_hwtstamp(struct net_device *dev, struct ifreq *ifr)
 		return err;
 	}
 
-	if (!ops->ndo_hwtstamp_set)
+	if (!ops->ndo_hwtstamp_set && !dev->tsinfo.enabled)
 		return -EOPNOTSUPP;
 
 	if (!netif_device_present(dev))
@@ -447,7 +470,7 @@ int generic_hwtstamp_get_lower(struct net_device *dev,
 	if (!netif_device_present(dev))
 		return -ENODEV;
 
-	if (!ops->ndo_hwtstamp_get)
+	if (!ops->ndo_hwtstamp_get && !dev->tsinfo.enabled)
 		return -EOPNOTSUPP;
 
 	netdev_lock_ops(dev);
@@ -468,7 +491,7 @@ int generic_hwtstamp_set_lower(struct net_device *dev,
 	if (!netif_device_present(dev))
 		return -ENODEV;
 
-	if (!ops->ndo_hwtstamp_set)
+	if (!ops->ndo_hwtstamp_set && !dev->tsinfo.enabled)
 		return -EOPNOTSUPP;
 
 	netdev_lock_ops(dev);
net/ethtool/common.c +4 -0
index 84ec88dee05c..bb0c02d92a9b 100644
@@ -1090,6 +1090,10 @@ int __ethtool_get_ts_info(struct net_device *dev,
 			err = ops->get_ts_info(dev, info);
 			if (!err && info->phc_index >= 0)
 				info->phc_source = HWTSTAMP_SOURCE_NETDEV;
+		} else if (dev->tsinfo.enabled) {
+			info->so_timestamping = dev->tsinfo.so_timestamping;
+			info->tx_types = dev->tsinfo.tx_types;
+			info->rx_filters = dev->tsinfo.rx_filters;
 		}
 
 		info->so_timestamping |= SOF_TIMESTAMPING_RX_SOFTWARE |
net/ethtool/netlink.c +8 -0
index 25e22c48060a..07d1a010b1cc 100644
@@ -422,6 +422,7 @@ ethnl_default_requests[__ETHTOOL_MSG_USER_CNT] = {
 	[ETHTOOL_MSG_TSCONFIG_SET]	= &ethnl_tsconfig_request_ops,
 	[ETHTOOL_MSG_PHY_GET]		= &ethnl_phy_request_ops,
 	[ETHTOOL_MSG_MSE_GET]		= &ethnl_mse_request_ops,
+	[ETHTOOL_MSG_TSINFO_SET]	= &ethnl_tsinfo_request_ops,
 };
 
 static struct ethnl_dump_ctx *ethnl_dump_context(struct netlink_callback *cb)
@@ -1548,6 +1549,13 @@ static const struct genl_ops ethtool_genl_ops[] = {
 		.policy = ethnl_mse_get_policy,
 		.maxattr = ARRAY_SIZE(ethnl_mse_get_policy) - 1,
 	},
+	{
+		.cmd	= ETHTOOL_MSG_TSINFO_SET,
+		.flags	= GENL_UNS_ADMIN_PERM,
+		.doit	= ethnl_default_set_doit,
+		.policy = ethnl_tsinfo_set_policy,
+		.maxattr = ARRAY_SIZE(ethnl_tsinfo_set_policy) - 1,
+	},
 };
 
 static const struct genl_multicast_group ethtool_nl_mcgrps[] = {
net/ethtool/netlink.h +1 -0
index 674c9c19529b..7c2a350f8ba4 100644
@@ -502,6 +502,7 @@ extern const struct nla_policy ethnl_phy_get_policy[ETHTOOL_A_PHY_HEADER + 1];
 extern const struct nla_policy ethnl_tsconfig_get_policy[ETHTOOL_A_TSCONFIG_HEADER + 1];
 extern const struct nla_policy ethnl_tsconfig_set_policy[ETHTOOL_A_TSCONFIG_MAX + 1];
 extern const struct nla_policy ethnl_mse_get_policy[ETHTOOL_A_MSE_HEADER + 1];
+extern const struct nla_policy ethnl_tsinfo_set_policy[ETHTOOL_A_TSINFO_MAX + 1];
 
 int ethnl_set_features(struct sk_buff *skb, struct genl_info *info);
 int ethnl_act_cable_test(struct sk_buff *skb, struct genl_info *info);
net/ethtool/tsconfig.c +4 -3
index 664c3fe49b5b..9f387339fc8e 100644
@@ -41,7 +41,7 @@ static int tsconfig_prepare_data(const struct ethnl_req_info *req_base,
 	struct kernel_hwtstamp_config cfg = {};
 	int ret;
 
-	if (!dev->netdev_ops->ndo_hwtstamp_get)
+	if (!dev->netdev_ops->ndo_hwtstamp_get && !dev->tsinfo.enabled)
 		return -EOPNOTSUPP;
 
 	ret = ethnl_ops_begin(dev);
@@ -61,7 +61,7 @@ static int tsconfig_prepare_data(const struct ethnl_req_info *req_base,
 	if (hwprov) {
 		data->hwprov_desc.index = hwprov->desc.index;
 		data->hwprov_desc.qualifier = hwprov->desc.qualifier;
-	} else {
+	} else if (!dev->tsinfo.enabled) {
 		struct kernel_ethtool_ts_info ts_info = {};
 
 		ts_info.phc_index = -1;
@@ -252,7 +252,8 @@ static int ethnl_set_tsconfig_validate(struct ethnl_req_info *req_base,
 {
 	const struct net_device_ops *ops = req_base->dev->netdev_ops;
 
-	if (!ops->ndo_hwtstamp_set || !ops->ndo_hwtstamp_get)
+	if ((!ops->ndo_hwtstamp_set || !ops->ndo_hwtstamp_get) &&
+	    !READ_ONCE(req_base->dev->tsinfo.enabled))
 		return -EOPNOTSUPP;
 
 	return 1;
net/ethtool/tsinfo.c +119 -4

159 lines not on this page · raw message

Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#2 lore raw

Re: [PATCH v2 bpf-next/net 1/5] ethtool: Introduce ETHTOOL_MSG_TSINFO_SET for virtual interfaces.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Data race on dev->tsinfo fields between the writer (ethnl_tsinfo_set) and lockless readers…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Data race on dev->tsinfo fields between the writer (ethnl_tsinfo_set) and lockless readers (__ethtool_get_ts_info).
- [Low] TSINFO_SET netlink policy improperly allows the ETHTOOL_FLAG_STATS flag by copying the GET policy. --

commit 8f988b32029a0a0b4879e0ac83015bac45af1444
Author: Kuniyuki Iwashima <[email protected]>

ethtool: Introduce ETHTOOL_MSG_TSINFO_SET for virtual interfaces.

This patch allows configuring hardware timestamping capabilities on virtual interfaces via ETHTOOL_MSG_TSINFO_SET, as they cannot currently advertise the capabilities of their underlying devices.

Quoted 20 lines
net/ethtool/common.c +4 -0
index 84ec88dee05c5..bb0c02d92a9b5 100644
@@ -1090,6 +1090,10 @@ int __ethtool_get_ts_info(struct net_device *dev,
 			err = ops->get_ts_info(dev, info);
 			if (!err && info->phc_index >= 0)
 				info->phc_source = HWTSTAMP_SOURCE_NETDEV;
+		} else if (dev->tsinfo.enabled) {
+			info->so_timestamping = dev->tsinfo.so_timestamping;
+			info->tx_types = dev->tsinfo.tx_types;
+			info->rx_filters = dev->tsinfo.rx_filters;
 		}
 
 		info->so_timestamping |= SOF_TIMESTAMPING_RX_SOFTWARE |
net/ethtool/tsinfo.c +0 -0
index 14bf01e3b55cd..c9078aa4a897e 100644

net/ethtool/tsinfo.c const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_MAX + 1] = { · line 43

Quoted 6 lines
net/ethtool/tsinfo.c +3 -0
@@ -38,6 +38,14 @@ const struct nla_policy ethnl_tsinfo_get_policy[ETHTOOL_A_TSINFO_MAX + 1] = {
 		NLA_POLICY_NESTED(ethnl_ts_hwtst_prov_policy),
 };
 
+const struct nla_policy ethnl_tsinfo_set_policy[ETHTOOL_A_TSINFO_MAX + 1] = {
+	[ETHTOOL_A_TSINFO_HEADER]		=
+		NLA_POLICY_NESTED(ethnl_header_policy_stats),

[Severity: Low]
Does this unintentionally allow the ETHTOOL_FLAG_STATS flag in SET operations?

The ethnl_header_policy_stats policy allows users to request statistics, which is normally only applicable to GET requests. Other SET operations in the ethtool family appear to use ethnl_header_policy to enforce strict validation.

Quoted 4 lines
net/ethtool/tsinfo.c +4 -0
+	[ETHTOOL_A_TSINFO_TIMESTAMPING]		= { .type = NLA_NESTED },
+	[ETHTOOL_A_TSINFO_TX_TYPES]		= { .type = NLA_NESTED },
+	[ETHTOOL_A_TSINFO_RX_FILTERS]		= { .type = NLA_NESTED },
+};

[ ... ]

Quoted 3 lines
net/ethtool/tsinfo.c +3 -0
+static int ethnl_tsinfo_set(struct ethnl_req_info *req_base,
+			    struct genl_info *info)
+{

[ ... ]

net/ethtool/tsinfo.c int ethnl_tsinfo_done(struct netlink_callback *cb) · line 646

Quoted 4 lines
net/ethtool/tsinfo.c +4 -0
+	if (!ts_info.so_timestamping &&
+	    !ts_info.tx_types && !ts_info.rx_filters) {
+		WRITE_ONCE(dev->tsinfo.enabled, false);
+		memset(&dev->tsinfo, 0, offsetof(typeof(dev->tsinfo), enabled));

[Severity: High]
Is there a data race between lockless readers and this writer?

In __ethtool_get_ts_info(), plain C reads of the dev->tsinfo fields are performed while holding only the RCU read lock.

If ethnl_tsinfo_set() concurrently updates or clears dev->tsinfo using a non-atomic memset, could the reader observe torn reads or garbage zeroes, resulting in incorrect capabilities being reported to the socket layer?

Quoted 2 lines
net/ethtool/tsinfo.c +2 -0
+		goto out;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 bpf-next/net 1/5] ethtool: Introduce ETHTOOL_MSG_TSINFO_SET for virtual interfaces.

In reply to Kuniyuki Iwashima
Quoted 47 lines · Kuniyuki Iwashima

Before enabling SO_TIMESTAMPING, applications typically try to enable hardware timestamping on network interfaces via SIOCSHWTSTAMP (or ETHTOOL_MSG_TSCONFIG_SET).

The timestamping capability on an interface can be checked via ETHTOOL_MSG_TSINFO_GET:

  # ethtool -T eth0
  Time stamping parameters for eth0:
  Capabilities:
  	hardware-transmit
  	software-transmit
  	hardware-receive
  	software-receive
  	software-system-clock
  	hardware-raw-clock
  PTP Hardware Clock: none
  Hardware Transmit Timestamp Modes:
  	off
  	on
  Hardware Receive Filter Modes:
  	none
  	all

These operations rely on the driver implementing two callbacks, dev->netdev_ops->ndo_hwtstamp_{get,set}().

However, among all virtual network interfaces, only bond and macvlan currently implement them.

As a result, most virtual interfaces cannot advertise the capabilities of their underlying devices:

  # ip link add ipvl0 link eth0 type ipvlan mode l2 bridge
  # ethtool -T ipvl0
  Time stamping parameters for ipvl0:
  Capabilities:
  	software-receive
  	software-system-clock
  PTP Hardware Clock: none
  Hardware Transmit Timestamp Modes: none
  Hardware Receive Filter Modes: none

While these callbacks could be implemented in each virtual interface, this approach is limited to those directly linked to a physical device.

Quoted 6 lines

Not all virtual interfaces are tied to real hardware; for instance, packets from UDP tunnel devices eventually pass through physical devices and can be hardware-timestamped there.

Let's allow configuring the hardware timestamping capability on virtual interfaces via ETHTOOL_MSG_TSINFO_SET.

I don't have a lot of state on this, but when adding xdp hw timestamping I remember fighting a lot with those apis. Now we are adding another one, but (seemingly?) for sw devices. Can you explain a bit more about why can't existing SIOCGHWTSTAMP (or ETHTOOL_MSG_TSCONFIG_SET) fallback to your new dev->tsinfo.enabled codepaths for the devices that don't implement the ops?

#4 lore raw

Re: [PATCH v2 bpf-next/net 1/5] ethtool: Introduce ETHTOOL_MSG_TSINFO_SET for virtual interfaces.

Quoted 56 lines · Stanislav Fomichev
Quoted 47 lines · Kuniyuki Iwashima

Before enabling SO_TIMESTAMPING, applications typically try to enable hardware timestamping on network interfaces via SIOCSHWTSTAMP (or ETHTOOL_MSG_TSCONFIG_SET).

The timestamping capability on an interface can be checked via ETHTOOL_MSG_TSINFO_GET:

  # ethtool -T eth0
  Time stamping parameters for eth0:
  Capabilities:
      hardware-transmit
      software-transmit
      hardware-receive
      software-receive
      software-system-clock
      hardware-raw-clock
  PTP Hardware Clock: none
  Hardware Transmit Timestamp Modes:
      off
      on
  Hardware Receive Filter Modes:
      none
      all

These operations rely on the driver implementing two callbacks, dev->netdev_ops->ndo_hwtstamp_{get,set}().

However, among all virtual network interfaces, only bond and macvlan currently implement them.

As a result, most virtual interfaces cannot advertise the capabilities of their underlying devices:

  # ip link add ipvl0 link eth0 type ipvlan mode l2 bridge
  # ethtool -T ipvl0
  Time stamping parameters for ipvl0:
  Capabilities:
      software-receive
      software-system-clock
  PTP Hardware Clock: none
  Hardware Transmit Timestamp Modes: none
  Hardware Receive Filter Modes: none

While these callbacks could be implemented in each virtual interface, this approach is limited to those directly linked to a physical device.

Quoted 6 lines

Not all virtual interfaces are tied to real hardware; for instance, packets from UDP tunnel devices eventually pass through physical devices and can be hardware-timestamped there.

Let's allow configuring the hardware timestamping capability on virtual interfaces via ETHTOOL_MSG_TSINFO_SET.

Quoted 6 lines

I don't have a lot of state on this, but when adding xdp hw timestamping I remember fighting a lot with those apis. Now we are adding another one, but (seemingly?) for sw devices. Can you explain a bit more about why can't existing SIOCGHWTSTAMP (or ETHTOOL_MSG_TSCONFIG_SET) fallback to your new dev->tsinfo.enabled codepaths for the devices that don't implement the ops?

Simply because we mainly need this for ipvlan and geneve so limited the condition to cover such sw devices only.

If needed, we can loosen the limitation and support sw devices
w/ ops (bonding/macvlan) and even hw devices too.

#5 lore raw

PATCH v2 2/5 bpf: Rename bpf_kfunc_set_tcp_reqsk to bpf_kfunc_set_sched_cls.

In reply to Kuniyuki Iwashima

Currently bpf_kfunc_set_tcp_reqsk is registered for BPF_PROG_TYPE_SCHED_CLS.

We will add more kfuncs there, but the name is too specific.

Let's rename it to bpf_kfunc_set_sched_cls.

---
 net/core/filter.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/core/filter.c +5 -5
index 80439767e0ee..acdc66aa4f27 100644
@@ -12498,9 +12498,9 @@ BTF_KFUNCS_START(bpf_kfunc_check_set_sock_addr)
 BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path)
 BTF_KFUNCS_END(bpf_kfunc_check_set_sock_addr)
 
-BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk)
+BTF_KFUNCS_START(bpf_kfunc_check_set_sched_cls)
 BTF_ID_FLAGS(func, bpf_sk_assign_tcp_reqsk)
-BTF_KFUNCS_END(bpf_kfunc_check_set_tcp_reqsk)
+BTF_KFUNCS_END(bpf_kfunc_check_set_sched_cls)
 
 BTF_KFUNCS_START(bpf_kfunc_check_set_sock_ops)
 BTF_ID_FLAGS(func, bpf_sock_ops_enable_tx_tstamp)
@@ -12526,9 +12526,9 @@ static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = {
 	.set = &bpf_kfunc_check_set_sock_addr,
 };
 
-static const struct btf_kfunc_id_set bpf_kfunc_set_tcp_reqsk = {
+static const struct btf_kfunc_id_set bpf_kfunc_set_sched_cls = {
 	.owner = THIS_MODULE,
-	.set = &bpf_kfunc_check_set_tcp_reqsk,
+	.set = &bpf_kfunc_check_set_sched_cls,
 };
 
 static const struct btf_kfunc_id_set bpf_kfunc_set_sock_ops = {
@@ -12556,7 +12556,7 @@ static int __init bpf_kfunc_init(void)
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
 	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
 					       &bpf_kfunc_set_sock_addr);
-	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_tcp_reqsk);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_sched_cls);
 	return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SOCK_OPS, &bpf_kfunc_set_sock_ops);
 }
 late_initcall(bpf_kfunc_init);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#6 lore raw

PATCH v2 3/5 bpf: Add bpf_skb_set_hwtstamp().

In reply to Kuniyuki Iwashima

We have some hosts where packets come from special hardware and are provided directly to userspace, bypassing the kernel networking stack.

When standard socket applications are run on these hosts, a userspace proxy is required to mediate traffic between the hardware and the applications.

            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     ^                               ^
  userspace   |     |                               |
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|  virtual interface  |<---'
  kernel     | |   skb   +---------------------+
  -----------| |-----------------------------------------------
              |
              v
       +------------+
       |  hardware  |
       +------------+

However, even though the hardware fully supports timestamping, the HW timestamps are not directly accessible to the socket applications because the skb is consumed/injected by the proxy.

For RX flow, let's add a kfunc to update skb_hwtstamps(skb)->hwtstamp at tc/ingress.

With this kfunc, the proxy can carry the RX hardware timestamp via encapsulated packets (e.g. in GENEVE option) and BPF prog can extract it into skb_hwtstamps(skb)->hwtstamp at tc/ingress of the virtual interface above.

            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     | encap packet                  ^ recv payload
  userspace   |     |  w/ RX hwtstamp               |  w/ RX hwtstamp
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|       geneve0       |----'
  kernel     | |   skb   +---------------------+
             | |             |             ^
             | |             v             |
             | |          +------------------+  extract RX hwtstamp
             | |          |  BPF@tc/ingress  |   and set it to skb
             | |          +------------------+
  -----------| |-----------------------------------------------
              |
              | RX packet w/ RX hwtstamp
       +------------+
       |  hardware  |
       +------------+

This allows transparently proxying RX hardware timestamps to the socket applications via SCM_TIMESTAMPING.

Note that bpf_skb_set_hwtstamp() calls skb_unclone() and bpf_compute_data_pointers(), so it is marked as a packet-changing kfunc.

---
v2:
* Remove __packed and use unnamed bit-field in struct bpf_hwtstamp (Alexei Starovoitov)
* Use skb_unclone() instead of skb_heaeder_clone() (Sashiko)

---
 include/linux/skbuff.h |  5 +++++
 kernel/bpf/verifier.c  |  9 ++++++++-
 net/core/filter.c      | 25 +++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 1 deletion(-)
include/linux/skbuff.h +5 -0
index 115db8c44db2..0f62f7cd22e1 100644
@@ -4701,6 +4701,11 @@ static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
 
 #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
 
+struct bpf_hwtstamp {
+	ktime_t hwtstamp;
+	u64 :64;
+};
+
 /**
  * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
  *
kernel/bpf/verifier.c +8 -1
index 7fb88e1cd7c4..6b23577d001a 100644
@@ -11191,6 +11191,7 @@ enum special_kfunc_type {
 	KF_bpf_session_is_return,
 	KF_bpf_stream_vprintk,
 	KF_bpf_stream_print_stack,
+	KF_bpf_skb_set_hwtstamp,
 };
 
 BTF_ID_LIST(special_kfunc_list)
@@ -11283,6 +11284,11 @@ BTF_ID_UNUSED
 #endif
 BTF_ID(func, bpf_stream_vprintk)
 BTF_ID(func, bpf_stream_print_stack)
+#ifdef CONFIG_NET
+BTF_ID(func, bpf_skb_set_hwtstamp)
+#else
+BTF_ID_UNUSED
+#endif
 
 static bool is_bpf_obj_new_kfunc(u32 func_id)
 {
@@ -11364,7 +11370,8 @@ static bool is_kfunc_bpf_preempt_enable(struct bpf_kfunc_call_arg_meta *meta)
 
 bool bpf_is_kfunc_pkt_changing(struct bpf_kfunc_call_arg_meta *meta)
 {
-	return meta->func_id == special_kfunc_list[KF_bpf_xdp_pull_data];
+	return meta->func_id == special_kfunc_list[KF_bpf_xdp_pull_data] ||
+	       meta->func_id == special_kfunc_list[KF_bpf_skb_set_hwtstamp];
 }
 
 static enum kfunc_ptr_arg_type
net/core/filter.c +25 -0
index acdc66aa4f27..bee1e7eee5ea 100644
@@ -12372,6 +12372,30 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
 	return 0;
 }
 
+__bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s,
+				     struct bpf_hwtstamp *attrs, int attrs__sz)
+{
+	int defined_sz = offsetofend(struct bpf_hwtstamp, hwtstamp);
+	struct sk_buff *skb = (struct sk_buff *)s;
+
+	if (attrs__sz != sizeof(*attrs) ||
+	    memchr_inv((char *)attrs + defined_sz, 0, sizeof(u64)))
+		return -EINVAL;
+
+	if (!skb_at_tc_ingress(skb))
+		return -EINVAL;
+
+	if (skb_unclone(skb, GFP_ATOMIC))
+		return -ENOMEM;
+
+	skb_clear_tstamp(skb);
+	skb_hwtstamps(skb)->hwtstamp = attrs->hwtstamp;
+
+	bpf_compute_data_pointers(skb);
+
+	return 0;
+}
+
 /**
  * bpf_xdp_pull_data() - Pull in non-linear xdp data.
  * @x: &xdp_md associated with the XDP buffer
@@ -12500,6 +12524,7 @@ BTF_KFUNCS_END(bpf_kfunc_check_set_sock_addr)
 
 BTF_KFUNCS_START(bpf_kfunc_check_set_sched_cls)
 BTF_ID_FLAGS(func, bpf_sk_assign_tcp_reqsk)
+BTF_ID_FLAGS(func, bpf_skb_set_hwtstamp)
 BTF_KFUNCS_END(bpf_kfunc_check_set_sched_cls)
 
 BTF_KFUNCS_START(bpf_kfunc_check_set_sock_ops)
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#7 lore raw

Re: [PATCH v2 bpf-next/net 3/5] bpf: Add bpf_skb_set_hwtstamp().

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Potential use-after-free read of `attrs->hwtstamp` if the BPF program passes a pointer to the…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Potential use-after-free read of attrs->hwtstamp if the BPF program passes a pointer to the packet payload. --

commit 41caec1764c831fb81388c5eab46d122318a01be
Author: Kuniyuki Iwashima <[email protected]>

bpf: Add bpf_skb_set_hwtstamp().

This commit adds a new kfunc, bpf_skb_set_hwtstamp(), to allow BPF programs at TC ingress to update the hardware timestamp of an skb. This enables user space proxies to encapsulate and pass hardware timestamps to socket applications bypassing the kernel networking stack.

Quoted 4 lines
net/core/filter.c +0 -0
index acdc66aa4f27c..bee1e7eee5eae 100644

[ ... ]

Quoted 12 lines
net/core/filter.c +8 -0
@@ -12372,6 +12372,30 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
 	return 0;
 }
 
+__bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s,
+				     struct bpf_hwtstamp *attrs, int attrs__sz)
+{
+	int defined_sz = offsetofend(struct bpf_hwtstamp, hwtstamp);
+	struct sk_buff *skb = (struct sk_buff *)s;
+
+	if (attrs__sz != sizeof(*attrs) ||
+	    memchr_inv((char *)attrs + defined_sz, 0, sizeof(u64)))

net/core/filter.c __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops, · line 12392

Quoted 10 lines
net/core/filter.c +10 -0
+		return -EINVAL;
+
+	if (!skb_at_tc_ingress(skb))
+		return -EINVAL;
+
+	if (skb_unclone(skb, GFP_ATOMIC))
+		return -ENOMEM;
+
+	skb_clear_tstamp(skb);
+	skb_hwtstamps(skb)->hwtstamp = attrs->hwtstamp;

[Severity: High]
Could this result in a use-after-free if the BPF program passes a pointer to the packet payload as the attrs argument?

The BPF verifier allows passing a packet pointer to a kfunc argument that has an associated size parameter (attrs__sz). If the attrs pointer points into the packet's payload (skb->data) and the skb happens to be cloned, skb_unclone() will invoke pskb_expand_head().

This reallocation frees the original packet buffer, meaning the attrs pointer becomes dangling. The subsequent read of attrs->hwtstamp would then dereference freed memory.

Would it be safer to read the attrs->hwtstamp value into a local variable before calling skb_unclone()?

Quoted 5 lines
net/core/filter.c +5 -0
+
+	bpf_compute_data_pointers(skb);
+
+	return 0;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#8 lore raw

Re: [PATCH v2 bpf-next/net 3/5] bpf: Add bpf_skb_set_hwtstamp().

Quoted 38 lines · On Fri, Jun 12, 2026 at 6:12 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Potential use-after-free read of attrs->hwtstamp if the BPF program passes a pointer to the packet payload. --

commit 41caec1764c831fb81388c5eab46d122318a01be
Author: Kuniyuki Iwashima <[email protected]>

bpf: Add bpf_skb_set_hwtstamp().

This commit adds a new kfunc, bpf_skb_set_hwtstamp(), to allow BPF programs at TC ingress to update the hardware timestamp of an skb. This enables user space proxies to encapsulate and pass hardware timestamps to socket applications bypassing the kernel networking stack.

Quoted 4 lines
net/core/filter.c +0 -0
index acdc66aa4f27c..bee1e7eee5eae 100644

[ ... ]

Quoted 18 lines
net/core/filter.c +14 -0
@@ -12372,6 +12372,30 @@ __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
      return 0;
 }
 
+__bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s,
+                                  struct bpf_hwtstamp *attrs, int attrs__sz)
+{
+     int defined_sz = offsetofend(struct bpf_hwtstamp, hwtstamp);
+     struct sk_buff *skb = (struct sk_buff *)s;
+
+     if (attrs__sz != sizeof(*attrs) ||
+         memchr_inv((char *)attrs + defined_sz, 0, sizeof(u64)))
+             return -EINVAL;
+
+     if (!skb_at_tc_ingress(skb))
+             return -EINVAL;
+
+     if (skb_unclone(skb, GFP_ATOMIC))
Quoted 8 lines

net/core/filter.c __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops, · line 12392

Quoted 4 lines
net/core/filter.c +4 -0
+             return -ENOMEM;
+
+     skb_clear_tstamp(skb);
+     skb_hwtstamps(skb)->hwtstamp = attrs->hwtstamp;

[Severity: High]
Could this result in a use-after-free if the BPF program passes a pointer to the packet payload as the attrs argument?

This requires bpf_skb_store_bytes() to build attrs on skb, which does not make sense. Passing sane local variable is bpf prog responsibility.

Quoted 21 lines

The BPF verifier allows passing a packet pointer to a kfunc argument that has an associated size parameter (attrs__sz). If the attrs pointer points into the packet's payload (skb->data) and the skb happens to be cloned, skb_unclone() will invoke pskb_expand_head().

This reallocation frees the original packet buffer, meaning the attrs pointer becomes dangling. The subsequent read of attrs->hwtstamp would then dereference freed memory.

Would it be safer to read the attrs->hwtstamp value into a local variable before calling skb_unclone()?

net/core/filter.c __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops, · line 12397

Quoted 5 lines
net/core/filter.c +5 -0
+
+     bpf_compute_data_pointers(skb);
+
+     return 0;
+}

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

#9 lore raw

PATCH v2 4/5 bpf: Add kfunc to proxy TX HW Timestamp.

In reply to Kuniyuki Iwashima

In the setup mentioned in the previous patch, it is impossible for socket applications to get TX hardware timestamps via SCM_TIMESTAMPING.

To proxy TX hardware timestamp, let's add two kfuncs:

* bpf_skb_scrub_tx_tstamp() : scrub skb_shinfo(skb)->tx_flags
* bpf_skb_complete_tx_tstamp() : enqueue skb to sk->sk_error_queue

The key idea is to regenerate an skb that contains all the information required for the TX timestamp, identical to the original skb.

Here is how it works:

When the socket application sends a packet, BPF prog at tc/egress checks skb_shinfo()->tx_flags. If it has SKBTX_HW_TSTAMP_NOBPF, BPF prog scrub the value by bpf_skb_scrub_tx_tstamp() and inserts a GENEVE option to signal that the packet wants TX HW timestamp.

The proxy decapsulates and forwards the packet to the hardware, and if it has GENEVE option, the proxy keeps the original packet until TX completion.

            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              |     ^ decap packet and              |
  userspace   |     |  keep it till TX cmpl         |
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `----|       geneve0       |<---'
  kernel     | |   skb   +---------------------+
             | |             ^             |
             | |             |             v
             | |          +------------------+  check skb_shinfo()->tx_flags
             | |          |  BPF@tc/egress   |    and insert a GENEVE option
             | |          +------------------+
  -----------| |-----------------------------------------------
              |
              v
       +------------+
       |  hardware  |
       +------------+

Once the proxy gets TX hwtstamp, encapsulate the original packet with TX hwtstamp embedded in GENEVE option, and sends it to the GENEVE device.

At tc@ingress, BPF extracts the TX hwtstamp and sets it to skb. Then, it looks up the sender socket, assigns it to skb->sk, calls bpf_skb_complete_tx_tstamp(), and returns TCX_ERRQUEUE to put the skb to skb->sk->sk_error_queue.

            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     | encap packet                  ^ get TX hwtstamp by
  userspace   |     |  w/ TX hwtstamp               |  recvmsg(MSG_ERRQUEUE)
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|       geneve0       |    |
  kernel     | |   skb   +---------------------+    |
             | |             |              ________'
             | |             v             |    extract TX hwtstamp to skb
             | |          +------------------+   and look up the sender sk
             | |          |  BPF@tc/ingress  |   and enqueue skb to its
             | |          +------------------+    sk->sk_error_queue
  -----------| |-----------------------------------------------
              |
              | TX completion w/ TX hwtstamp
       +------------+
       |  hardware  |
       +------------+

This provides transparent TX HW timestamp support, and the socket application can finally receive it via recvmsg(MSG_ERRQUEUE).

Note that struct bpf_tx_tstamp_cmpl needs network_offset and payload_offset so that

1. ip_cmsg_recv() and ipv6_recv_error() can correctly parse the IPv4/IPv6 header for some control messages

2. applications can receive the original payload

---
v2:
* Remove __packed and use unnamed bit-field in struct bpf_tx_tstamp_cmpl (Alexei Starovoitov)
* Check !skb_at_tc_ingress() in bpf_skb_complete_tx_tstamp() (Sashiko)
* Use skb_unclone() instead of skb_heaeder_clone() (Sashiko)

---
 include/linux/filter.h       |  2 ++
 include/linux/skbuff.h       |  8 +++++
 include/net/tcx.h            |  1 +
 include/uapi/linux/bpf.h     |  1 +
 include/uapi/linux/pkt_cls.h |  3 +-
 kernel/bpf/verifier.c        |  6 +++-
 net/core/dev.c               | 39 ++++++++++++++++++++++
 net/core/filter.c            | 63 ++++++++++++++++++++++++++++++++++++
 8 files changed, 121 insertions(+), 2 deletions(-)
include/linux/filter.h +2 -0
index 88a241aac36a..59097bfd8522 100644
@@ -770,6 +770,7 @@ struct bpf_nh_params {
 #define BPF_RI_F_CPU_MAP_INIT	BIT(2)
 #define BPF_RI_F_DEV_MAP_INIT	BIT(3)
 #define BPF_RI_F_XSK_MAP_INIT	BIT(4)
+#define BPF_RI_F_TX_TS_CMPL	BIT(5)
 
 struct bpf_redirect_info {
 	u64 tgt_index;
@@ -780,6 +781,7 @@ struct bpf_redirect_info {
 	enum bpf_map_type map_type;
 	struct bpf_nh_params nh;
 	u32 kern_flags;
+	struct bpf_tx_tstamp_cmpl txtscmpl;
 };
 
 struct bpf_net_context {
include/linux/skbuff.h +8 -0
index 0f62f7cd22e1..9709e3389b13 100644
@@ -4706,6 +4706,14 @@ struct bpf_hwtstamp {
 	u64 :64;
 };
 
+struct bpf_tx_tstamp_cmpl {
+	u32 tskey;
+	__be16 protocol;
+	u16 network_offset;
+	u16 payload_offset;
+	u16 :16;
+};
+
 /**
  * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
  *
include/net/tcx.h +1 -0
index 23a61af13547..052e751d907e 100644
@@ -151,6 +151,7 @@ static inline enum tcx_action_base tcx_action_code(struct sk_buff *skb,
 		fallthrough;
 	case TCX_DROP:
 	case TCX_REDIRECT:
+	case TCX_ERRQUEUE:
 		return code;
 	case TCX_NEXT:
 	default:
include/uapi/linux/bpf.h +1 -0
index 552bc5d9afbd..60950aa583aa 100644
@@ -6532,6 +6532,7 @@ enum tcx_action_base {
 	TCX_PASS	= 0,
 	TCX_DROP	= 2,
 	TCX_REDIRECT	= 7,
+	TCX_ERRQUEUE	= 9,
 };
 
 struct bpf_xdp_sock {
include/uapi/linux/pkt_cls.h +2 -1
index 28d94b11d1aa..337f1bdbabb6 100644
@@ -76,7 +76,8 @@ enum {
 				   * the skb and act like everything
 				   * is alright.
 				   */
-#define TC_ACT_VALUE_MAX	TC_ACT_TRAP
+#define TC_ACT_ERRQUEUE		9
+#define TC_ACT_VALUE_MAX	TC_ACT_ERRQUEUE
 
 /* There is a special kind of actions called "extended actions",
  * which need a value parameter. These have a local opcode located in
kernel/bpf/verifier.c +5 -1
index 6b23577d001a..5451a19847ec 100644
@@ -11192,6 +11192,7 @@ enum special_kfunc_type {
 	KF_bpf_stream_vprintk,
 	KF_bpf_stream_print_stack,
 	KF_bpf_skb_set_hwtstamp,
+	KF_bpf_skb_scrub_tx_tstamp,
 };
 
 BTF_ID_LIST(special_kfunc_list)
@@ -11286,8 +11287,10 @@ BTF_ID(func, bpf_stream_vprintk)
 BTF_ID(func, bpf_stream_print_stack)
 #ifdef CONFIG_NET
 BTF_ID(func, bpf_skb_set_hwtstamp)
+BTF_ID(func, bpf_skb_scrub_tx_tstamp)
 #else
 BTF_ID_UNUSED
+BTF_ID_UNUSED
 #endif
 
 static bool is_bpf_obj_new_kfunc(u32 func_id)
@@ -11371,7 +11374,8 @@ static bool is_kfunc_bpf_preempt_enable(struct bpf_kfunc_call_arg_meta *meta)
 bool bpf_is_kfunc_pkt_changing(struct bpf_kfunc_call_arg_meta *meta)
 {
 	return meta->func_id == special_kfunc_list[KF_bpf_xdp_pull_data] ||
-	       meta->func_id == special_kfunc_list[KF_bpf_skb_set_hwtstamp];
+	       meta->func_id == special_kfunc_list[KF_bpf_skb_set_hwtstamp] ||
+	       meta->func_id == special_kfunc_list[KF_bpf_skb_scrub_tx_tstamp];
 }
 
 static enum kfunc_ptr_arg_type
net/core/dev.c +39 -0
index 1ecd5691992e..f8a8cc85d47d 100644
@@ -4457,6 +4457,41 @@ tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
 	return tcx_action_code(skb, ret);
 }
 
+static int skb_do_completion(struct sk_buff *skb)
+{
+	enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_INGRESS;
+	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
+	struct bpf_tx_tstamp_cmpl *txtscmpl;
+
+	if (!(ri->kern_flags & BPF_RI_F_TX_TS_CMPL))
+		goto drop;
+
+	if (skb_unclone(skb, GFP_ATOMIC))
+		goto drop;
+
+	__skb_push(skb, skb->mac_len);
+
+	txtscmpl = &ri->txtscmpl;
+
+	drop_reason = pskb_may_pull_reason(skb, txtscmpl->payload_offset);
+	if (drop_reason)
+		goto drop;
+
+	skb->protocol = txtscmpl->protocol;
+	skb_set_network_header(skb, txtscmpl->network_offset);
+	__skb_pull(skb, txtscmpl->payload_offset);
+
+	skb_shinfo(skb)->tskey = txtscmpl->tskey;
+	skb_shinfo(skb)->tx_flags = SKBTX_HW_TSTAMP_NOBPF;
+	__skb_tstamp_tx(skb, NULL, skb_hwtstamps(skb), skb->sk, SCM_TSTAMP_SND);
+
+	consume_skb(skb);
+	return NET_RX_SUCCESS;
+drop:
+	kfree_skb_reason(skb, drop_reason);
+	return NET_RX_DROP;
+}
+
 static __always_inline struct sk_buff *
 sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
 		   struct net_device *orig_dev, bool *another)
@@ -4505,6 +4540,10 @@ sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
 		*ret = NET_RX_DROP;
 		bpf_net_ctx_clear(bpf_net_ctx);
 		return NULL;
+	case TC_ACT_ERRQUEUE:
+		*ret = skb_do_completion(skb);
+		bpf_net_ctx_clear(bpf_net_ctx);
+		return NULL;
 	/* used by tc_run */
 	case TC_ACT_STOLEN:
 	case TC_ACT_QUEUED:
net/core/filter.c +63 -0

75 lines not on this page · raw message

Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#10 lore raw

Re: [PATCH v2 bpf-next/net 4/5] bpf: Add kfunc to proxy TX HW Timestamp.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Incompatible `payload_offset` semantics and bounds checking between `tcx`, `cls_bpf`, and…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incompatible payload_offset semantics and bounds checking between tcx, cls_bpf, and bpf_skb_complete_tx_tstamp() leads to false rejections or packet corruption.
- [High] Blindly clearing skb_shinfo(skb)->tx_flags destroys the SKBTX_DEV_ZEROCOPY flag, causing a permanent memory leak and application hang.
--

commit 07c148630f8e93ce45f5ce41dcd9ee675f05a799
Author: Kuniyuki Iwashima <[email protected]>

bpf: Add kfunc to proxy TX HW Timestamp.

The commit adds two kfuncs, bpf_skb_scrub_tx_tstamp() and bpf_skb_complete_tx_tstamp(), to proxy TX hardware timestamps. This allows socket applications to receive TX hardware timestamps via SCM_TIMESTAMPING.

Quoted 24 lines
net/core/dev.c +16 -0
index 0c6c270d9f7d1..a45cdf38616ac 100644
@@ -4456,6 +4456,41 @@ tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
 	return tcx_action_code(skb, ret);
 }
 
+static int skb_do_completion(struct sk_buff *skb)
+{
+	enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_INGRESS;
+	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
+	struct bpf_tx_tstamp_cmpl *txtscmpl;
+
+	if (!(ri->kern_flags & BPF_RI_F_TX_TS_CMPL))
+		goto drop;
+
+	if (skb_unclone(skb, GFP_ATOMIC))
+		goto drop;
+
+	__skb_push(skb, skb->mac_len);
+
+	txtscmpl = &ri->txtscmpl;
+

net/core/dev.c tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, · line 4484

Quoted 10 lines
net/core/dev.c +10 -0
+	drop_reason = pskb_may_pull_reason(skb, txtscmpl->payload_offset);
+	if (drop_reason)
+		goto drop;
+
+	skb->protocol = txtscmpl->protocol;
+	skb_set_network_header(skb, txtscmpl->network_offset);
+	__skb_pull(skb, txtscmpl->payload_offset);
+
+	skb_shinfo(skb)->tskey = txtscmpl->tskey;
+	skb_shinfo(skb)->tx_flags = SKBTX_HW_TSTAMP_NOBPF;

[Severity: High]
Does this direct assignment cause a memory leak if the packet was sent with MSG_ZEROCOPY?

If the user application sends a packet with MSG_ZEROCOPY, tx_flags contains SKBTX_DEV_ZEROCOPY and a reference to ubuf_info is held. Blindly overwriting tx_flags here erases SKBTX_DEV_ZEROCOPY. This prevents skb_release_data() from calling skb_zcopy_clear() when the SKB is freed, leaking the ubuf_info reference and causing the userspace socket to hang indefinitely waiting for the completion notification.

Can we mask out only the timestamp flags or preserve SKBTX_DEV_ZEROCOPY?

Quoted 4 lines
net/core/dev.c +4 -0
+	__skb_tstamp_tx(skb, NULL, skb_hwtstamps(skb), skb->sk, SCM_TSTAMP_SND);
+
+	consume_skb(skb);
+	return NET_RX_SUCCESS;

[ ... ]

Quoted 9 lines
net/core/filter.c +1 -0
index bee1e7eee5eae..fce2d24aad487 100644
@@ -12396,6 +12396,67 @@ __bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s,
 	return 0;
 }
 
+__bpf_kfunc int bpf_skb_scrub_tx_tstamp(struct __sk_buff *s)

net/core/filter.c __bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s, · line 12409

Quoted 10 lines
net/core/filter.c +10 -0
+{
+	struct sk_buff *skb = (struct sk_buff *)s;
+
+	if (skb_at_tc_ingress(skb))
+		return -EINVAL;
+
+	if (skb_unclone(skb, GFP_ATOMIC))
+		return -ENOMEM;
+
+	skb_shinfo(skb)->tx_flags = 0;

[Severity: High]
Does clearing tx_flags here also destroy SKBTX_DEV_ZEROCOPY?

Similar to skb_do_completion(), if a user application sends a packet with MSG_ZEROCOPY, blindly clearing tx_flags to 0 erases SKBTX_DEV_ZEROCOPY. When the SKB is ultimately freed after transmission, skb_release_data() will not call skb_zcopy_clear(), leading to a leaked ubuf_info reference and hanging the userspace socket waiting for completion.

Quoted 17 lines
net/core/filter.c +17 -0
+
+	bpf_compute_data_pointers(skb);
+
+	return 0;
+}
+
+__bpf_kfunc int bpf_skb_complete_tx_tstamp(struct __sk_buff *s,
+					   struct bpf_tx_tstamp_cmpl *attrs,
+					   int attrs__sz)
+{
+	int defined_sz = offsetofend(struct bpf_tx_tstamp_cmpl, payload_offset);
+	struct sk_buff *skb = (struct sk_buff *)s;
+	struct bpf_redirect_info *ri;
+	struct sock *sk = skb->sk;
+	s32 delta;
+
+	if (attrs__sz != sizeof(*attrs) ||

net/core/filter.c __bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s, · line 12436

Quoted 10 lines
net/core/filter.c +10 -0
+	    memchr_inv((char *)attrs + defined_sz, 0, sizeof(u16)))
+		return -EINVAL;
+
+	if (!sk || !sk_fullsock(sk))
+		return -EINVAL;
+
+	if (!skb_at_tc_ingress(skb))
+		return -EINVAL;
+
+	if (attrs->payload_offset > skb->len)

[Severity: High]
Are there incompatible payload_offset semantics between here and legacy TC?

In skb_do_completion(), skb->mac_len is unconditionally pushed before pulling txtscmpl->payload_offset:

	__skb_push(skb, skb->mac_len);
	txtscmpl = &ri->txtscmpl;
	drop_reason = pskb_may_pull_reason(skb, txtscmpl->payload_offset);

This structurally requires payload_offset to be relative to the MAC header. However, for BPF programs attached via legacy TC (cls_bpf), the MAC header is not pushed prior to ingress execution, meaning skb->len excludes mac_len.

If a MAC-relative offset is provided, it will exceed skb->len for small payloads (e.g., empty TCP ACKs where mac_len + offset > total_len - mac_len), causing a false -EINVAL rejection.

Conversely, if a BPF program compensates by sending a network-relative offset to pass this validation, it will cause skb_do_completion() to pull from the MAC header incorrectly, corrupting the packet.

Quoted 3 lines
net/core/filter.c +3 -0
+		return -EINVAL;
+
+	delta = attrs->payload_offset - attrs->network_offset;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#11 lore raw

Re: [PATCH v2 bpf-next/net 4/5] bpf: Add kfunc to proxy TX HW Timestamp.

Quoted 54 lines · On Fri, Jun 12, 2026 at 6:20 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Incompatible payload_offset semantics and bounds checking between tcx, cls_bpf, and bpf_skb_complete_tx_tstamp() leads to false rejections or packet corruption.
- [High] Blindly clearing skb_shinfo(skb)->tx_flags destroys the SKBTX_DEV_ZEROCOPY flag, causing a permanent memory leak and application hang.
--

commit 07c148630f8e93ce45f5ce41dcd9ee675f05a799
Author: Kuniyuki Iwashima <[email protected]>

bpf: Add kfunc to proxy TX HW Timestamp.

The commit adds two kfuncs, bpf_skb_scrub_tx_tstamp() and bpf_skb_complete_tx_tstamp(), to proxy TX hardware timestamps. This allows socket applications to receive TX hardware timestamps via SCM_TIMESTAMPING.

Quoted 24 lines
net/core/dev.c +16 -0
index 0c6c270d9f7d1..a45cdf38616ac 100644
@@ -4456,6 +4456,41 @@ tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb,
      return tcx_action_code(skb, ret);
 }
 
+static int skb_do_completion(struct sk_buff *skb)
+{
+     enum skb_drop_reason drop_reason = SKB_DROP_REASON_TC_INGRESS;
+     struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
+     struct bpf_tx_tstamp_cmpl *txtscmpl;
+
+     if (!(ri->kern_flags & BPF_RI_F_TX_TS_CMPL))
+             goto drop;
+
+     if (skb_unclone(skb, GFP_ATOMIC))
+             goto drop;
+
+     __skb_push(skb, skb->mac_len);
+
+     txtscmpl = &ri->txtscmpl;
+

net/core/dev.c tcx_run(const struct bpf_mprog_entry *entry, struct sk_buff *skb, · line 4484

Quoted 10 lines
net/core/dev.c +10 -0
+     drop_reason = pskb_may_pull_reason(skb, txtscmpl->payload_offset);
+     if (drop_reason)
+             goto drop;
+
+     skb->protocol = txtscmpl->protocol;
+     skb_set_network_header(skb, txtscmpl->network_offset);
+     __skb_pull(skb, txtscmpl->payload_offset);
+
+     skb_shinfo(skb)->tskey = txtscmpl->tskey;
+     skb_shinfo(skb)->tx_flags = SKBTX_HW_TSTAMP_NOBPF;

[Severity: High]
Does this direct assignment cause a memory leak if the packet was sent with MSG_ZEROCOPY?

Quoted 2 lines

If the user application sends a packet with MSG_ZEROCOPY, tx_flags contains SKBTX_DEV_ZEROCOPY

This is pure hallucination. No such flag exists.

Quoted 23 lines

and a reference to ubuf_info is held. Blindly overwriting tx_flags here erases SKBTX_DEV_ZEROCOPY. This prevents skb_release_data() from calling skb_zcopy_clear() when the SKB is freed, leaking the ubuf_info reference and causing the userspace socket to hang indefinitely waiting for the completion notification.

Can we mask out only the timestamp flags or preserve SKBTX_DEV_ZEROCOPY?

Quoted 4 lines
net/core/dev.c +4 -0
+     __skb_tstamp_tx(skb, NULL, skb_hwtstamps(skb), skb->sk, SCM_TSTAMP_SND);
+
+     consume_skb(skb);
+     return NET_RX_SUCCESS;

[ ... ]

Quoted 8 lines
net/core/filter.c +0 -0
index bee1e7eee5eae..fce2d24aad487 100644
@@ -12396,6 +12396,67 @@ __bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s,
      return 0;
 }
 
Quoted 14 lines

net/core/filter.c __bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s, · line 12409

Quoted 11 lines
net/core/filter.c +11 -0
+__bpf_kfunc int bpf_skb_scrub_tx_tstamp(struct __sk_buff *s)
+{
+     struct sk_buff *skb = (struct sk_buff *)s;
+
+     if (skb_at_tc_ingress(skb))
+             return -EINVAL;
+
+     if (skb_unclone(skb, GFP_ATOMIC))
+             return -ENOMEM;
+
+     skb_shinfo(skb)->tx_flags = 0;

[Severity: High]
Does clearing tx_flags here also destroy SKBTX_DEV_ZEROCOPY?

Same here.

Quoted 29 lines

Similar to skb_do_completion(), if a user application sends a packet with MSG_ZEROCOPY, blindly clearing tx_flags to 0 erases SKBTX_DEV_ZEROCOPY. When the SKB is ultimately freed after transmission, skb_release_data() will not call skb_zcopy_clear(), leading to a leaked ubuf_info reference and hanging the userspace socket waiting for completion.

Quoted 22 lines
net/core/filter.c +22 -0
+
+     bpf_compute_data_pointers(skb);
+
+     return 0;
+}
+
+__bpf_kfunc int bpf_skb_complete_tx_tstamp(struct __sk_buff *s,
+                                        struct bpf_tx_tstamp_cmpl *attrs,
+                                        int attrs__sz)
+{
+     int defined_sz = offsetofend(struct bpf_tx_tstamp_cmpl, payload_offset);
+     struct sk_buff *skb = (struct sk_buff *)s;
+     struct bpf_redirect_info *ri;
+     struct sock *sk = skb->sk;
+     s32 delta;
+
+     if (attrs__sz != sizeof(*attrs) ||
+         memchr_inv((char *)attrs + defined_sz, 0, sizeof(u16)))
+             return -EINVAL;
+
+     if (!sk || !sk_fullsock(sk))
+             return -EINVAL;
Quoted 8 lines

net/core/filter.c __bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s, · line 12436

Quoted 5 lines
net/core/filter.c +5 -0
+
+     if (!skb_at_tc_ingress(skb))
+             return -EINVAL;
+
+     if (attrs->payload_offset > skb->len)

[Severity: High]
Are there incompatible payload_offset semantics between here and legacy TC?

No, both are offsets from the ethhdr.

Quoted 26 lines

In skb_do_completion(), skb->mac_len is unconditionally pushed before pulling txtscmpl->payload_offset:

        __skb_push(skb, skb->mac_len);
        txtscmpl = &ri->txtscmpl;
        drop_reason = pskb_may_pull_reason(skb, txtscmpl->payload_offset);

This structurally requires payload_offset to be relative to the MAC header. However, for BPF programs attached via legacy TC (cls_bpf), the MAC header is not pushed prior to ingress execution, meaning skb->len excludes mac_len.

If a MAC-relative offset is provided, it will exceed skb->len for small payloads (e.g., empty TCP ACKs where mac_len + offset > total_len - mac_len), causing a false -EINVAL rejection.

Conversely, if a BPF program compensates by sending a network-relative offset to pass this validation, it will cause skb_do_completion() to pull from the MAC header incorrectly, corrupting the packet.

net/core/filter.c __bpf_kfunc int bpf_skb_set_hwtstamp(struct __sk_buff *s, · line 12439

Quoted 3 lines
net/core/filter.c +3 -0
+             return -EINVAL;
+
+     delta = attrs->payload_offset - attrs->network_offset;

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4

#12 lore raw

PATCH v2 5/5 selftest: bpf: Add test for hwtstamp proxy.

In reply to Kuniyuki Iwashima

This selftest simulates the hardware timestamp proxy scenario mentioned in the previous commits using two UDP sockets.

Here, app_fd represents a standard socket application, and proxy_fd simulates a userspace proxy that receives and injects encapsulated packets from/to app_fd via a GENEVE device (geneve0).

TX:
1. app_fd sends data w/ SCM_TS_OPT_ID
2. BPF prog hooks at tc/egress of geneve0
3. BPF inserts the GENEVE option with Type 0x1 to save SCM_TS_OPT_ID
4. proxy_fd receives the encapsulated packet
5. proxy changes the option Type to 0x2 and sets TX hwtstamp
6. proxy sends it back to geneve0
7. BPF prog hooks at tc/ingress of geneve0
8. BPF extracts TX hwtstamp into skb
9. BPF looks up the app_fd socket
10. BPF enqueues skb to app_fd's sk->sk_error_queue
11. app_fd receives TX hwtstamp and verifies the value

RX:
12. proxy_fd generates RX packet from TX packet
by swapping src/dst in each header
13. proxy changes the option Type to 0x3 and sets RX hwtstamp
14. proxy sends the encapsulated packet to geneve0
15. BPF prog hooks at tc/ingress of geneve0
16. BPF extracts RX hwtstamp into skb
17. app_fd receives RX hwtstamp and verifies the value

The GENEVE TLV option is structured as follows:

0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1

   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          Option Class         |      Type     |0|0|0| Length  |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                                                               |
   +                     HW Timestamp  (8 bytes)                   +
   |                                                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                     Timestamp key (4 bytes)                   |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Type:
- 0x1: TX packet
- 0x2: TX completion packet w/ TX hwtstamp
- 0x3: RX packet w/ RX hwtstamp

---
v2:
* Use scm_timestamping64 (Sashiko)
* Correct saw_tskey check (Sashiko)
* Fix retval check for bpf_skb_get_tunnel_opt() with (int) cast (Sashiko)
* bpf-style comment

---
 tools/testing/selftests/bpf/bpf_kfuncs.h      |  10 +
 .../selftests/bpf/prog_tests/proxy_hwtstamp.c | 588 ++++++++++++++++++
 .../selftests/bpf/progs/bpf_tracing_net.h     |   1 +
 .../selftests/bpf/progs/proxy_hwtstamp.c      | 236 +++++++
 4 files changed, 835 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/proxy_hwtstamp.c
 create mode 100644 tools/testing/selftests/bpf/progs/proxy_hwtstamp.c
tools/testing/selftests/bpf/bpf_kfuncs.h +10 -0
index 7dad01439391..8d119b10ed0d 100644
@@ -92,4 +92,14 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str,
 				const struct bpf_dynptr *value_p, int flags) __ksym __weak;
 extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak;
 
+extern int bpf_skb_scrub_tx_tstamp(struct __sk_buff *s) __ksym __weak;
+
+struct bpf_hwtstamp;
+extern int bpf_skb_set_hwtstamp(struct __sk_buff *s,
+				struct bpf_hwtstamp *attrs, int attrs__sz) __ksym __weak;
+
+struct bpf_tx_tstamp_cmpl;
+extern int bpf_skb_complete_tx_tstamp(struct __sk_buff *s,
+				      struct bpf_tx_tstamp_cmpl *attrs,
+				      int attrs__sz) __ksym __weak;
 #endif
tools/testing/selftests/bpf/prog_tests/proxy_hwtstamp.c new file +588 -0

588 lines not on this page · raw message

tools/testing/selftests/bpf/progs/bpf_tracing_net.h +1 -0
index d8dacef37c16..77a88dc20a64 100644
@@ -73,6 +73,7 @@
 #define ETH_P_IPV6		0x86DD
 
 #define NEXTHDR_TCP		6
+#define NEXTHDR_UDP		17
 
 #define TCPOPT_NOP		1
 #define TCPOPT_EOL		0
tools/testing/selftests/bpf/progs/proxy_hwtstamp.c new file +236 -0

236 lines not on this page · raw message

Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#13 lore raw

Re: [PATCH v2 bpf-next/net 5/5] selftest: bpf: Add test for hwtstamp proxy.

Quoted 18 lines · Kuniyuki Iwashima
tools/testing/selftests/bpf/bpf_kfuncs.h +10 -0
index 7dad01439391..8d119b10ed0d 100644
@@ -92,4 +92,14 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str,
 				const struct bpf_dynptr *value_p, int flags) __ksym __weak;
 extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak;
 
+extern int bpf_skb_scrub_tx_tstamp(struct __sk_buff *s) __ksym __weak;
+
+struct bpf_hwtstamp;
+extern int bpf_skb_set_hwtstamp(struct __sk_buff *s,
+				struct bpf_hwtstamp *attrs, int attrs__sz) __ksym __weak;
+
+struct bpf_tx_tstamp_cmpl;
+extern int bpf_skb_complete_tx_tstamp(struct __sk_buff *s,
+				      struct bpf_tx_tstamp_cmpl *attrs,
+				      int attrs__sz) __ksym __weak;

...

tools/testing/selftests/bpf/progs/proxy_hwtstamp.c line 4

Quoted 10 lines
tools/testing/selftests/bpf/progs/proxy_hwtstamp.c new file +4 -0
new file mode 100644
index 000000000000..e13963f2393e
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright 2026 Google LLC */
+
+#include "vmlinux.h"

Please update your pahole. It must be old, since you had the need to add kfuncs defs into bpf_kfuncs.h. pahole was improved couple years ago and all kfuncs now in vmlinux.h automatically.

Overall, the set looks good to me, but I would like to see an ack for networking bits from somebody.

bpf-next/net was the right subj to use, but this is very late into release cycle, so I prefer to land it to bpf-next/master and send together with main bpf-next PR.

Kuba,
is is ok with you? thoughts on patchset overall?

#14 lore raw

Re: [PATCH v2 bpf-next/net 5/5] selftest: bpf: Add test for hwtstamp proxy.

Quoted 23 lines · Alexei Starovoitov
Quoted 18 lines · Kuniyuki Iwashima
tools/testing/selftests/bpf/bpf_kfuncs.h +10 -0
index 7dad01439391..8d119b10ed0d 100644
@@ -92,4 +92,14 @@ extern int bpf_set_dentry_xattr(struct dentry *dentry, const char *name__str,
                              const struct bpf_dynptr *value_p, int flags) __ksym __weak;
 extern int bpf_remove_dentry_xattr(struct dentry *dentry, const char *name__str) __ksym __weak;
 
+extern int bpf_skb_scrub_tx_tstamp(struct __sk_buff *s) __ksym __weak;
+
+struct bpf_hwtstamp;
+extern int bpf_skb_set_hwtstamp(struct __sk_buff *s,
+                             struct bpf_hwtstamp *attrs, int attrs__sz) __ksym __weak;
+
+struct bpf_tx_tstamp_cmpl;
+extern int bpf_skb_complete_tx_tstamp(struct __sk_buff *s,
+                                   struct bpf_tx_tstamp_cmpl *attrs,
+                                   int attrs__sz) __ksym __weak;

...

Quoted 15 lines

tools/testing/selftests/bpf/progs/proxy_hwtstamp.c line 4

Quoted 10 lines
tools/testing/selftests/bpf/progs/proxy_hwtstamp.c new file +4 -0
new file mode 100644
index 000000000000..e13963f2393e
@@ -0,0 +1,236 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright 2026 Google LLC */
+
+#include "vmlinux.h"

Please update your pahole. It must be old, since you had the need to add kfuncs defs into bpf_kfuncs.h. pahole was improved couple years ago and all kfuncs now in vmlinux.h automatically.

Good to know this. I was able to build selftest without bpf_kfunc.h change with the latest pahole.

Quoted 7 lines

Overall, the set looks good to me, but I would like to see an ack for networking bits from somebody.

bpf-next/net was the right subj to use, but this is very late into release cycle, so I prefer to land it to bpf-next/master and send together with main bpf-next PR.

I'll repost to bpf-next without patch 1.

It's okay to drop patch 1 for now (since SO_TIMESTAMPING is not tied to device capability, and while it's not ideal, we can change the application for a while), and I can explore possible options in the next cycle in net-next.

maintainer
#15 lore raw
In reply to Kuniyuki Iwashima
Quoted 4 lines · On Sat, 13 Jun 2026 00:59:57 +0000 Kuniyuki Iwashima wrote:

When standard socket applications are run on these hosts, a userspace proxy is required to mediate traffic between the hardware and the applications.

Quoted 15 lines
            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     ^                               ^
  userspace   |     |                               |
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|  virtual interface  |<---'
  kernel     | |   skb   +---------------------+
  -----------| |-----------------------------------------------
              |
              v
       +------------+
       |  hardware  |
       +------------+

The first patch looks kinda nonsensical but then I saw this diagram. Looks like you're vibe coding an integration that makes it easier to treat netdev as a slow path for a user networking stack. Please tell me if I'm missing anything otherwise add my nack if you repost.

#16 lore raw
Quoted 22 lines · Jakub Kicinski
Quoted 4 lines · On Sat, 13 Jun 2026 00:59:57 +0000 Kuniyuki Iwashima wrote:

When standard socket applications are run on these hosts, a userspace proxy is required to mediate traffic between the hardware and the applications.

Quoted 15 lines
            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     ^                               ^
  userspace   |     |                               |
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|  virtual interface  |<---'
  kernel     | |   skb   +---------------------+
  -----------| |-----------------------------------------------
              |
              v
       +------------+
       |  hardware  |
       +------------+
Quoted 5 lines

The first patch looks kinda nonsensical but then I saw this diagram. Looks like you're vibe coding an integration that makes it easier to treat netdev as a slow path for a user networking stack. Please tell me if I'm missing anything otherwise add my nack if you repost.

Hmm, what would be a better way to tell users that HW TS is available on tunnel devices ?

Other options were 1) add attribute to tie the tunnel to a physical device and use its ndo_hwtstamp_set/get, or 2) add bpf retval hook in the path like update_socket_protocol().

Or do you think applications should handle ENOTSUP by ioctl as soft error and always set SOF_TIMESTAMPING_XXX_HARDWARE since the underlying physical device may support it ?

Anyway, I'm happy to drop patch 1 for now and explore options.

(Believe it or not, I haven't let AI write code because I don't
want AI to take over the most fun part.)

maintainer
#17 lore raw
Quoted 28 lines · On Sat, 13 Jun 2026 14:43:46 -0700 Kuniyuki Iwashima wrote:
Quoted 21 lines · Jakub Kicinski
Quoted 4 lines · On Sat, 13 Jun 2026 00:59:57 +0000 Kuniyuki Iwashima wrote:

When standard socket applications are run on these hosts, a userspace proxy is required to mediate traffic between the hardware and the applications.

Quoted 15 lines
            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     ^                               ^
  userspace   |     |                               |
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|  virtual interface  |<---'
  kernel     | |   skb   +---------------------+
  -----------| |-----------------------------------------------
              |
              v
       +------------+
       |  hardware  |
       +------------+  
Quoted 5 lines

The first patch looks kinda nonsensical but then I saw this diagram. Looks like you're vibe coding an integration that makes it easier to treat netdev as a slow path for a user networking stack. Please tell me if I'm missing anything otherwise add my nack if you repost.

Quoted 6 lines

Hmm, what would be a better way to tell users that HW TS is available on tunnel devices ?

Other options were 1) add attribute to tie the tunnel to a physical device and use its ndo_hwtstamp_set/get, or 2) add bpf retval hook in the path like update_socket_protocol().

Can you explain the use case for HW timestamps on tunnels? I see you have some diagrams in patches 3 and 4 but the motivation isn't really explained, and we have 250 patches in the queue still. Not much time to play detective right now :/

Quoted 3 lines

Or do you think applications should handle ENOTSUP by ioctl as soft error and always set SOF_TIMESTAMPING_XXX_HARDWARE since the underlying physical device may support it ?

And explain the deployment model and API semantics you have in mind.

Quoted 1 line

Anyway, I'm happy to drop patch 1 for now and explore options.

Not patch 1, all of it. I get the feeling y'all are sitting on a pile of Swift related code that needs to be made upstreamable. Odd series like this make me think it's never going to happen.

Quoted 2 lines

(Believe it or not, I haven't let AI write code because I don't
want AI to take over the most fun part.)

#18 lore raw
Quoted 33 lines · Jakub Kicinski
Quoted 31 lines · On Sat, 13 Jun 2026 14:43:46 -0700 Kuniyuki Iwashima wrote:
Quoted 21 lines · Jakub Kicinski
Quoted 4 lines · On Sat, 13 Jun 2026 00:59:57 +0000 Kuniyuki Iwashima wrote:

When standard socket applications are run on these hosts, a userspace proxy is required to mediate traffic between the hardware and the applications.

Quoted 15 lines
            +---------+                 +----------------------+
            |  proxy  |                 |  socket application  |
            +---------+                 +----------------------+
              ^     ^                               ^
  userspace   |     |                               |
  -----------| |-----------------------------------------------
             | |    |    +---------------------+    | skb
             | |    `--->|  virtual interface  |<---'
  kernel     | |   skb   +---------------------+
  -----------| |-----------------------------------------------
              |
              v
       +------------+
       |  hardware  |
       +------------+
Quoted 5 lines

The first patch looks kinda nonsensical but then I saw this diagram. Looks like you're vibe coding an integration that makes it easier to treat netdev as a slow path for a user networking stack. Please tell me if I'm missing anything otherwise add my nack if you repost.

Hmm, what would be a better way to tell users that HW TS is available on tunnel devices ?

Quoted 8 lines
Quoted 3 lines

Other options were 1) add attribute to tie the tunnel to a physical device and use its ndo_hwtstamp_set/get, or 2) add bpf retval hook in the path like update_socket_protocol().

Can you explain the use case for HW timestamps on tunnels? I see you have some diagrams in patches 3 and 4 but the motivation isn't really explained, and we have 250 patches in the queue still. Not much time to play detective right now :/

To be clear, this series is not related to Swift.

Our RPC library capture timestamps as many places as possible so that we can visualise the RPC calls and know/debug where the latency comes from: application, library, kernel, network.

This is not specific to tunnels and applied to all workloads running on container w/ virtual devices and init_net w/ physical devices.

Quoted 6 lines
Quoted 4 lines

Or do you think applications should handle ENOTSUP by ioctl as soft error and always set SOF_TIMESTAMPING_XXX_HARDWARE since the underlying physical device may support it ?

And explain the deployment model and API semantics you have in mind.

The application first calls ioctl(SIOCSHWTSTAMP) and only when hwtstamp is supported, it uses SOF_TIMESTAMPING_XXX_HARDARE.

The doc mentions ioctl(SIOCSHWTSTAMP) may overwrite the configuration and ioctl(SIOCGHWTSTAMP) is not always implemented.
https://docs.kernel.org/networking/timestamping.html

But it does not say ioctl(SIOCSHWTSTAMP) may fail although SOF_TIMESTAMPING_XXX_HARDARE could be supported.

Quoted 9 lines
Quoted 2 lines

Anyway, I'm happy to drop patch 1 for now and explore options.

Not patch 1, all of it. I get the feeling y'all are sitting on a pile of Swift related code that needs to be made upstreamable. Odd series like this make me think it's never going to happen.

Quoted 2 lines

(Believe it or not, I haven't let AI write code because I don't
want AI to take over the most fun part.)