Skip to contentSkip to conversation

Add NDOs for hardware timestamp get/set

RFC PATCH Superseded Maxim Georgiev 8 participants 16 messages Apr 2, 2023 – Apr 5, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 Add NDOs for hardware timestamp get/set

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

This patch is sent out as RFC.
It still pending on basic testing. Implementing ndo_hwtstamp_get/set in netdevsim driver should allow manual testing of the request translation logic. Also is there a way to automate this testing?

---
Changes in v2:
- Introduced kernel_hwtstamp_config structure
- Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters
- Reodered function variable declarations in dev_hwtstamp()
- Refactored error handling logic in dev_hwtstamp()
- Split dev_hwtstamp() into GET and SET versions
- Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter

---
 drivers/net/ethernet/intel/e1000e/netdev.c | 39 ++++++-----
 drivers/net/netdevsim/netdev.c             | 26 +++++++
 drivers/net/netdevsim/netdevsim.h          |  1 +
 include/linux/netdevice.h                  | 21 ++++++
 include/uapi/linux/net_tstamp.h            | 15 ++++
 net/core/dev_ioctl.c                       | 81 ++++++++++++++++++----
 6 files changed, 149 insertions(+), 34 deletions(-)
drivers/net/ethernet/intel/e1000e/netdev.c +20 -19
index 6f5c16aebcbf..5b98f7257c77 100644
@@ -6161,7 +6161,9 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
 /**
  * e1000e_hwtstamp_set - control hardware time stamping
  * @netdev: network interface device structure
- * @ifr: interface request
+ * @config: hwtstamp_config structure containing request parameters
+ * @kernel_config: kernel version of config parameter structure.
+ * @extack: netlink request parameters.
  *
  * Outgoing time stamping can be enabled and disabled. Play nice and
  * disable it when requested, although it shouldn't cause any overhead
@@ -6174,20 +6176,19 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  * specified. Matching the kind of event packet is not supported, with the
  * exception of "all V2 events regardless of level 2 or 4".
  **/
-static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_set(struct net_device *netdev,
+			       struct hwtstamp_config *config,
+			       struct kernel_hwtstamp_config *kernel_config,
+			       struct netlink_ext_ack *extack)
 {
 	struct e1000_adapter *adapter = netdev_priv(netdev);
-	struct hwtstamp_config config;
 	int ret_val;
 
-	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-		return -EFAULT;
-
-	ret_val = e1000e_config_hwtstamp(adapter, &config);
+	ret_val = e1000e_config_hwtstamp(adapter, config);
 	if (ret_val)
 		return ret_val;
 
-	switch (config.rx_filter) {
+	switch (config->rx_filter) {
 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -6199,22 +6200,24 @@ static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
 		 * by hardware so notify the caller the requested packets plus
 		 * some others are time stamped.
 		 */
-		config.rx_filter = HWTSTAMP_FILTER_SOME;
+		config->rx_filter = HWTSTAMP_FILTER_SOME;
 		break;
 	default:
 		break;
 	}
-
-	return copy_to_user(ifr->ifr_data, &config,
-			    sizeof(config)) ? -EFAULT : 0;
+	return ret_val;
 }
 
-static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_get(struct net_device *netdev,
+			       struct hwtstamp_config *config,
+			       struct kernel_hwtstamp_config *kernel_config,
+			       struct netlink_ext_ack *extack)
 {
 	struct e1000_adapter *adapter = netdev_priv(netdev);
 
-	return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
-			    sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
+	memcpy(config, &adapter->hwtstamp_config,
+	       sizeof(adapter->hwtstamp_config));
+	return 0;
 }
 
 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
@@ -6224,10 +6227,6 @@ static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
 	case SIOCGMIIREG:
 	case SIOCSMIIREG:
 		return e1000_mii_ioctl(netdev, ifr, cmd);
-	case SIOCSHWTSTAMP:
-		return e1000e_hwtstamp_set(netdev, ifr);
-	case SIOCGHWTSTAMP:
-		return e1000e_hwtstamp_get(netdev, ifr);
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
 	.ndo_set_features = e1000_set_features,
 	.ndo_fix_features = e1000_fix_features,
 	.ndo_features_check	= passthru_features_check,
+	.ndo_hwtstamp_get	= e1000e_hwtstamp_get,
+	.ndo_hwtstamp_set	= e1000e_hwtstamp_set,
 };
 
 /**
drivers/net/netdevsim/netdev.c +26 -0
index 35fa1ca98671..502715c6e9e1 100644
@@ -238,6 +238,30 @@ nsim_set_features(struct net_device *dev, netdev_features_t features)
 	return 0;
 }
 
+static int
+nsim_hwtstamp_get(struct net_device *dev,
+		  struct hwtstamp_config *config,
+		  struct kernel_hwtstamp_config *kernel_config,
+		  struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	memcpy(config, &ns->hw_tstamp_config, sizeof(ns->hw_tstamp_config));
+	return 0;
+}
+
+static int
+nsim_hwtstamp_ges(struct net_device *dev,
+		  struct hwtstamp_config *config,
+		  struct kernel_hwtstamp_config *kernel_config,
+		  struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	memcpy(&ns->hw_tstamp_config, config, sizeof(ns->hw_tstamp_config));
+	return 0;
+}
+
 static const struct net_device_ops nsim_netdev_ops = {
 	.ndo_start_xmit		= nsim_start_xmit,
 	.ndo_set_rx_mode	= nsim_set_rx_mode,
@@ -256,6 +280,8 @@ static const struct net_device_ops nsim_netdev_ops = {
 	.ndo_setup_tc		= nsim_setup_tc,
 	.ndo_set_features	= nsim_set_features,
 	.ndo_bpf		= nsim_bpf,
+	.ndo_hwtstamp_get	= nsim_hwtstamp_get,
+	.ndo_hwtstamp_set	= nsim_hwtstamp_get,
 };
 
 static const struct net_device_ops nsim_vf_netdev_ops = {
drivers/net/netdevsim/netdevsim.h +1 -0
index 7d8ed8d8df5c..c6efd2383552 100644
@@ -102,6 +102,7 @@ struct netdevsim {
 	} udp_ports;
 
 	struct nsim_ethtool ethtool;
+	struct hwtstamp_config hw_tstamp_config;
 };
 
 struct netdevsim *
include/linux/netdevice.h +21 -0
index c8c634091a65..078c9284930a 100644
@@ -57,6 +57,8 @@
 struct netpoll_info;
 struct device;
 struct ethtool_ops;
+struct hwtstamp_config;
+struct kernel_hwtstamp_config;
 struct phy_device;
 struct dsa_port;
 struct ip_tunnel_parm;
@@ -1411,6 +1413,17 @@ struct netdev_net_notifier {
  *	Get hardware timestamp based on normal/adjustable time or free running
  *	cycle counter. This function is required if physical clock supports a
  *	free running cycle counter.
+ *	int (*ndo_hwtstamp_get)(struct net_device *dev,
+ *				struct hwtstamp_config *config,
+ *				struct kernel_hwtstamp_config *kernel_config,
+ *				struct netlink_ext_ack *extack);
+ *	Get hardware timestamping parameters currently configured  for NIC
+ *	device.
+ *	int (*ndo_hwtstamp_set)(struct net_device *dev,
+ *				struct hwtstamp_config *config,
+ *				struct kernel_hwtstamp_config *kernel_config,
+ *				struct netlink_ext_ack *extack);
+ *	Set hardware timestamping parameters for NIC device.
  */
 struct net_device_ops {
 	int			(*ndo_init)(struct net_device *dev);
@@ -1645,6 +1658,14 @@ struct net_device_ops {
 	ktime_t			(*ndo_get_tstamp)(struct net_device *dev,
 						  const struct skb_shared_hwtstamps *hwtstamps,
 						  bool cycles);
+	int			(*ndo_hwtstamp_get)(struct net_device *dev,
+						    struct hwtstamp_config *config,
+						    struct kernel_hwtstamp_config *kernel_config,
+						    struct netlink_ext_ack *extack);
+	int			(*ndo_hwtstamp_set)(struct net_device *dev,
+						    struct hwtstamp_config *config,
+						    struct kernel_hwtstamp_config *kernel_config,
+						    struct netlink_ext_ack *extack);
 };
 
 struct xdp_metadata_ops {
include/uapi/linux/net_tstamp.h +15 -0
index a2c66b3d7f0f..547f73beb479 100644
@@ -79,6 +79,21 @@ struct hwtstamp_config {
 	int rx_filter;
 };
 
+/**
+ * struct kernel_hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
+ *
+ * @dummy:	a placeholder field added to work around empty struct language
+ *		restriction
+ *
+ * This structure passed as a parameter to NDO methods called in
+ * response to SIOCGHWTSTAMP and SIOCSHWTSTAMP IOCTLs.
+ * The structure is effectively empty for now. Before adding new fields
+ * to the structure "dummy" placeholder field should be removed.
+ */
+struct kernel_hwtstamp_config {
+	u8 dummy;
+};
+
 /* possible values for hwtstamp_config->flags */
 enum hwtstamp_flags {
 	/*
net/core/dev_ioctl.c +66 -15

114 lines not on this page · raw message

Signature · 1 line
2.39.2
#1 lore raw
reproduce (this is a W=1 build):

Hi Maxim,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Maxim-Georgiev/Add-NDOs-for-hardware-timestamp-get-set/20230402-222504
patch link:    https://lore.kernel.org/r/20230402142435.47105-1-glipus%40gmail.com
patch subject: [PATCH net-next RFC v2] Add NDOs for hardware timestamp get/set
config: x86_64-randconfig-a004 (https://download.01.org/0day-ci/archive/20230402/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/9c768404248f059870f899ce676098d83bacdf18
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Maxim-Georgiev/Add-NDOs-for-hardware-timestamp-get-set/20230402-222504
        git checkout 9c768404248f059870f899ce676098d83bacdf18
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=x86_64 olddefconfig
        make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from <command-line>:

>> ./usr/include/linux/net_tstamp.h:94:9: error: unknown type name 'u8'
      94 |         u8 dummy;
         |         ^~
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#2 lore raw
In reply to Maxim Georgiev
reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross #…

Hi Maxim,

[FYI, it's a private test report for your RFC patch.] [auto build test ERROR on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Maxim-Georgiev/Add-NDOs-for-hardware-timestamp-get-set/20230402-222504
patch link:    https://lore.kernel.org/r/20230402142435.47105-1-glipus%40gmail.com
patch subject: [PATCH net-next RFC v2] Add NDOs for hardware timestamp get/set
config: i386-randconfig-a006 (https://download.01.org/0day-ci/archive/20230403/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/9c768404248f059870f899ce676098d83bacdf18
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Maxim-Georgiev/Add-NDOs-for-hardware-timestamp-get-set/20230402-222504 git checkout 9c768404248f059870f899ce676098d83bacdf18
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from <built-in>:1:

>> ./usr/include/linux/net_tstamp.h:94:2: error: unknown type name 'u8'
           u8 dummy;
           ^

1 error generated.

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#3 lore raw
In reply to Maxim Georgiev
Quoted 3 lines · Maxim Georgiev

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation

dirivers -> drivers

Quoted 5 lines

between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set

functiuon -> function

Quoted 35 lines

functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

This patch is sent out as RFC.
It still pending on basic testing. Implementing ndo_hwtstamp_get/set in netdevsim driver should allow manual testing of the request translation logic. Also is there a way to automate this testing?

---
Changes in v2:
- Introduced kernel_hwtstamp_config structure
- Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters
- Reodered function variable declarations in dev_hwtstamp()
- Refactored error handling logic in dev_hwtstamp()
- Split dev_hwtstamp() into GET and SET versions
- Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter

---
  drivers/net/ethernet/intel/e1000e/netdev.c | 39 ++++++-----
  drivers/net/netdevsim/netdev.c             | 26 +++++++
  drivers/net/netdevsim/netdevsim.h          |  1 +
  include/linux/netdevice.h                  | 21 ++++++
  include/uapi/linux/net_tstamp.h            | 15 ++++
  net/core/dev_ioctl.c                       | 81 ++++++++++++++++++----
  6 files changed, 149 insertions(+), 34 deletions(-)
drivers/net/ethernet/intel/e1000e/netdev.c +0 -0
index 6f5c16aebcbf..5b98f7257c77 100644

drivers/net/ethernet/intel/e1000e/netdev.c static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, · line 6166

Quoted 7 lines
drivers/net/ethernet/intel/e1000e/netdev.c +3 -1
@@ -6161,7 +6161,9 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  /**
   * e1000e_hwtstamp_set - control hardware time stamping
   * @netdev: network interface device structure
- * @ifr: interface request
+ * @config: hwtstamp_config structure containing request parameters
+ * @kernel_config: kernel version of config parameter structure.
+ * @extack: netlink request parameters.

'.' at end of parameter line or not? You mixed it.

Quoted 34 lines
drivers/net/ethernet/intel/e1000e/netdev.c +6 -7
   *
   * Outgoing time stamping can be enabled and disabled. Play nice and
   * disable it when requested, although it shouldn't cause any overhead
@@ -6174,20 +6176,19 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
   * specified. Matching the kind of event packet is not supported, with the
   * exception of "all V2 events regardless of level 2 or 4".
   **/
-static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_set(struct net_device *netdev,
+			       struct hwtstamp_config *config,
+			       struct kernel_hwtstamp_config *kernel_config,
+			       struct netlink_ext_ack *extack)
  {
  	struct e1000_adapter *adapter = netdev_priv(netdev);
-	struct hwtstamp_config config;
  	int ret_val;
 
-	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-		return -EFAULT;
-
-	ret_val = e1000e_config_hwtstamp(adapter, &config);
+	ret_val = e1000e_config_hwtstamp(adapter, config);
  	if (ret_val)
  		return ret_val;
 
-	switch (config.rx_filter) {
+	switch (config->rx_filter) {
  	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  	case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -6199,22 +6200,24 @@ static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
  		 * by hardware so notify the caller the requested packets plus
  		 * some others are time stamped.
  		 */

drivers/net/ethernet/intel/e1000e/netdev.c static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr) · line 6208

Quoted 10 lines
drivers/net/ethernet/intel/e1000e/netdev.c +2 -4
-		config.rx_filter = HWTSTAMP_FILTER_SOME;
+		config->rx_filter = HWTSTAMP_FILTER_SOME;
  		break;
  	default:
  		break;
  	}
-
-	return copy_to_user(ifr->ifr_data, &config,
-			    sizeof(config)) ? -EFAULT : 0;
+	return ret_val;

I would expect 'return 0;'.

Quoted 50 lines
drivers/net/ethernet/intel/e1000e/netdev.c +9 -7
  }
 
-static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_get(struct net_device *netdev,
+			       struct hwtstamp_config *config,
+			       struct kernel_hwtstamp_config *kernel_config,
+			       struct netlink_ext_ack *extack)
  {
  	struct e1000_adapter *adapter = netdev_priv(netdev);
 
-	return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
-			    sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
+	memcpy(config, &adapter->hwtstamp_config,
+	       sizeof(adapter->hwtstamp_config));
+	return 0;
  }
 
  static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
@@ -6224,10 +6227,6 @@ static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  	case SIOCGMIIREG:
  	case SIOCSMIIREG:
  		return e1000_mii_ioctl(netdev, ifr, cmd);
-	case SIOCSHWTSTAMP:
-		return e1000e_hwtstamp_set(netdev, ifr);
-	case SIOCGHWTSTAMP:
-		return e1000e_hwtstamp_get(netdev, ifr);
  	default:
  		return -EOPNOTSUPP;
  	}
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
  	.ndo_set_features = e1000_set_features,
  	.ndo_fix_features = e1000_fix_features,
  	.ndo_features_check	= passthru_features_check,
+	.ndo_hwtstamp_get	= e1000e_hwtstamp_get,
+	.ndo_hwtstamp_set	= e1000e_hwtstamp_set,
  };
 
  /**
drivers/net/netdevsim/netdev.c +4 -0
index 35fa1ca98671..502715c6e9e1 100644
@@ -238,6 +238,30 @@ nsim_set_features(struct net_device *dev, netdev_features_t features)
  	return 0;
  }
 
+static int
+nsim_hwtstamp_get(struct net_device *dev,
+		  struct hwtstamp_config *config,
+		  struct kernel_hwtstamp_config *kernel_config,

drivers/net/netdevsim/netdev.c nsim_set_features(struct net_device *dev, netdev_features_t features) · line 254

Quoted 10 lines
drivers/net/netdevsim/netdev.c +10 -0
+		  struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	memcpy(config, &ns->hw_tstamp_config, sizeof(ns->hw_tstamp_config));
+	return 0;
+}
+
+static int
+nsim_hwtstamp_ges(struct net_device *dev,

nsim_hwtstamp_ges -> nsim_hwtstamp_get

Quoted 14 lines
drivers/net/netdevsim/netdev.c +10 -0
+		  struct hwtstamp_config *config,
+		  struct kernel_hwtstamp_config *kernel_config,
+		  struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	memcpy(&ns->hw_tstamp_config, config, sizeof(ns->hw_tstamp_config));
+	return 0;
+}
+
  static const struct net_device_ops nsim_netdev_ops = {
  	.ndo_start_xmit		= nsim_start_xmit,
  	.ndo_set_rx_mode	= nsim_set_rx_mode,

drivers/net/netdevsim/netdev.c static const struct net_device_ops nsim_netdev_ops = { · line 284

Quoted 5 lines
drivers/net/netdevsim/netdev.c +2 -0
@@ -256,6 +280,8 @@ static const struct net_device_ops nsim_netdev_ops = {
  	.ndo_setup_tc		= nsim_setup_tc,
  	.ndo_set_features	= nsim_set_features,
  	.ndo_bpf		= nsim_bpf,
+	.ndo_hwtstamp_get	= nsim_hwtstamp_get,
+	.ndo_hwtstamp_set	= nsim_hwtstamp_get,

nsim_hwtstamp_get -> nsim_hwtstamp_set

Quoted 29 lines
drivers/net/netdevsim/netdev.c +0 -0
  };
 
  static const struct net_device_ops nsim_vf_netdev_ops = {
drivers/net/netdevsim/netdevsim.h +1 -0
index 7d8ed8d8df5c..c6efd2383552 100644
@@ -102,6 +102,7 @@ struct netdevsim {
  	} udp_ports;
 
  	struct nsim_ethtool ethtool;
+	struct hwtstamp_config hw_tstamp_config;
  };
 
  struct netdevsim *
include/linux/netdevice.h +2 -0
index c8c634091a65..078c9284930a 100644
@@ -57,6 +57,8 @@
  struct netpoll_info;
  struct device;
  struct ethtool_ops;
+struct hwtstamp_config;
+struct kernel_hwtstamp_config;
  struct phy_device;
  struct dsa_port;
  struct ip_tunnel_parm;

include/linux/netdevice.h struct netdev_net_notifier { · line 1420

Quoted 8 lines
include/linux/netdevice.h +5 -0
@@ -1411,6 +1413,17 @@ struct netdev_net_notifier {
   *	Get hardware timestamp based on normal/adjustable time or free running
   *	cycle counter. This function is required if physical clock supports a
   *	free running cycle counter.
+ *	int (*ndo_hwtstamp_get)(struct net_device *dev,
+ *				struct hwtstamp_config *config,
+ *				struct kernel_hwtstamp_config *kernel_config,
+ *				struct netlink_ext_ack *extack);
+ *	Get hardware timestamping parameters currently configured  for NIC

'configured for' -> 'configured for'

Quoted 172 lines
include/linux/netdevice.h +14 -0
+ *	device.
+ *	int (*ndo_hwtstamp_set)(struct net_device *dev,
+ *				struct hwtstamp_config *config,
+ *				struct kernel_hwtstamp_config *kernel_config,
+ *				struct netlink_ext_ack *extack);
+ *	Set hardware timestamping parameters for NIC device.
   */
  struct net_device_ops {
  	int			(*ndo_init)(struct net_device *dev);
@@ -1645,6 +1658,14 @@ struct net_device_ops {
  	ktime_t			(*ndo_get_tstamp)(struct net_device *dev,
  						  const struct skb_shared_hwtstamps *hwtstamps,
  						  bool cycles);
+	int			(*ndo_hwtstamp_get)(struct net_device *dev,
+						    struct hwtstamp_config *config,
+						    struct kernel_hwtstamp_config *kernel_config,
+						    struct netlink_ext_ack *extack);
+	int			(*ndo_hwtstamp_set)(struct net_device *dev,
+						    struct hwtstamp_config *config,
+						    struct kernel_hwtstamp_config *kernel_config,
+						    struct netlink_ext_ack *extack);
  };
 
  struct xdp_metadata_ops {
include/uapi/linux/net_tstamp.h +15 -0
index a2c66b3d7f0f..547f73beb479 100644
@@ -79,6 +79,21 @@ struct hwtstamp_config {
  	int rx_filter;
  };
 
+/**
+ * struct kernel_hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
+ *
+ * @dummy:	a placeholder field added to work around empty struct language
+ *		restriction
+ *
+ * This structure passed as a parameter to NDO methods called in
+ * response to SIOCGHWTSTAMP and SIOCSHWTSTAMP IOCTLs.
+ * The structure is effectively empty for now. Before adding new fields
+ * to the structure "dummy" placeholder field should be removed.
+ */
+struct kernel_hwtstamp_config {
+	u8 dummy;
+};
+
  /* possible values for hwtstamp_config->flags */
  enum hwtstamp_flags {
  	/*
net/core/dev_ioctl.c +66 -15
index 846669426236..c145afb3f77e 100644
@@ -183,22 +183,18 @@ static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cm
  	return err;
  }
 
-static int net_hwtstamp_validate(struct ifreq *ifr)
+static int net_hwtstamp_validate(struct hwtstamp_config *cfg)
  {
-	struct hwtstamp_config cfg;
  	enum hwtstamp_tx_types tx_type;
  	enum hwtstamp_rx_filters rx_filter;
  	int tx_type_valid = 0;
  	int rx_filter_valid = 0;
 
-	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-		return -EFAULT;
-
-	if (cfg.flags & ~HWTSTAMP_FLAG_MASK)
+	if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
  		return -EINVAL;
 
-	tx_type = cfg.tx_type;
-	rx_filter = cfg.rx_filter;
+	tx_type = cfg->tx_type;
+	rx_filter = cfg->rx_filter;
 
  	switch (tx_type) {
  	case HWTSTAMP_TX_OFF:
@@ -277,6 +273,63 @@ static int dev_siocbond(struct net_device *dev,
  	return -EOPNOTSUPP;
  }
 
+static int dev_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
+{
+	const struct net_device_ops *ops = dev->netdev_ops;
+	struct hwtstamp_config config;
+	int err;
+
+	err = dsa_ndo_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+	if (err == 0 || err != -EOPNOTSUPP)
+		return err;
+
+	if (!ops->ndo_hwtstamp_get)
+		return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+
+	if (!netif_device_present(dev))
+		return -ENODEV;
+
+	err = ops->ndo_hwtstamp_get(dev, &config, NULL, NULL);
+	if (err)
+		return err;
+
+	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+		return -EFAULT;
+	return 0;
+}
+
+static int dev_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
+{
+	const struct net_device_ops *ops = dev->netdev_ops;
+	struct hwtstamp_config config;
+	int err;
+
+	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
+		return -EFAULT;
+
+	err = net_hwtstamp_validate(&config);
+	if (err)
+		return err;
+
+	err = dsa_ndo_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+	if (err == 0 || err != -EOPNOTSUPP)
+		return err;
+
+	if (!ops->ndo_hwtstamp_set)
+		return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+
+	if (!netif_device_present(dev))
+		return -ENODEV;
+
+	err = ops->ndo_hwtstamp_set(dev, &config, NULL, NULL);
+	if (err)
+		return err;
+
+	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+		return -EFAULT;
+	return 0;
+}
+
  static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
  			      void __user *data, unsigned int cmd)
  {
@@ -391,11 +444,11 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
  		rtnl_lock();
  		return err;
 
+	case SIOCGHWTSTAMP:
+		return dev_hwtstamp_get(dev, ifr);
+
  	case SIOCSHWTSTAMP:
-		err = net_hwtstamp_validate(ifr);
-		if (err)
-			return err;
-		fallthrough;
+		return dev_hwtstamp_set(dev, ifr);
 
  	/*
  	 *	Unknown or private ioctl
@@ -407,9 +460,7 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
 
  		if (cmd == SIOCGMIIPHY ||
  		    cmd == SIOCGMIIREG ||
-		    cmd == SIOCSMIIREG ||
-		    cmd == SIOCSHWTSTAMP ||
-		    cmd == SIOCGHWTSTAMP) {
+		    cmd == SIOCSMIIREG) {
  			err = dev_eth_ioctl(dev, ifr, cmd);
  		} else if (cmd == SIOCBONDENSLAVE ||
  		    cmd == SIOCBONDRELEASE ||
#4 lore raw
Quoted 7 lines · Gerhard Engleder
Quoted 3 lines · Maxim Georgiev

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation

dirivers -> drivers

Thank you for catching it!

Quoted 8 lines
Quoted 6 lines

between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set

functiuon -> function

My bad. Thank you for catching it!

Quoted 31 lines
Quoted 31 lines

functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

This patch is sent out as RFC.
It still pending on basic testing. Implementing ndo_hwtstamp_get/set in netdevsim driver should allow manual testing of the request translation logic. Also is there a way to automate this testing?

---
Changes in v2:
- Introduced kernel_hwtstamp_config structure
- Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters
- Reodered function variable declarations in dev_hwtstamp()
- Refactored error handling logic in dev_hwtstamp()
- Split dev_hwtstamp() into GET and SET versions
- Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter

---
  drivers/net/ethernet/intel/e1000e/netdev.c | 39 ++++++-----
  drivers/net/netdevsim/netdev.c             | 26 +++++++
  drivers/net/netdevsim/netdevsim.h          |  1 +
  include/linux/netdevice.h                  | 21 ++++++
  include/uapi/linux/net_tstamp.h            | 15 ++++
  net/core/dev_ioctl.c                       | 81 ++++++++++++++++++----
  6 files changed, 149 insertions(+), 34 deletions(-)
Quoted 14 lines

drivers/net/ethernet/intel/e1000e/netdev.c static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, · line 6166

Quoted 12 lines
drivers/net/ethernet/intel/e1000e/netdev.c +3 -1
index 6f5c16aebcbf..5b98f7257c77 100644
@@ -6161,7 +6161,9 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  /**
   * e1000e_hwtstamp_set - control hardware time stamping
   * @netdev: network interface device structure
- * @ifr: interface request
+ * @config: hwtstamp_config structure containing request parameters
+ * @kernel_config: kernel version of config parameter structure.
+ * @extack: netlink request parameters.

'.' at end of parameter line or not? You mixed it.

What was I thinking!
Let me remove the periods since the original parameter descriptions didn't have them.

Quoted 39 lines
Quoted 39 lines
drivers/net/ethernet/intel/e1000e/netdev.c +7 -8
   *
   * Outgoing time stamping can be enabled and disabled. Play nice and
   * disable it when requested, although it shouldn't cause any overhead
@@ -6174,20 +6176,19 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
   * specified. Matching the kind of event packet is not supported, with the
   * exception of "all V2 events regardless of level 2 or 4".
   **/
-static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_set(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
  {
      struct e1000_adapter *adapter = netdev_priv(netdev);
-     struct hwtstamp_config config;
      int ret_val;
 
-     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-             return -EFAULT;
-
-     ret_val = e1000e_config_hwtstamp(adapter, &config);
+     ret_val = e1000e_config_hwtstamp(adapter, config);
      if (ret_val)
              return ret_val;
 
-     switch (config.rx_filter) {
+     switch (config->rx_filter) {
      case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
      case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
      case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -6199,22 +6200,24 @@ static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
               * by hardware so notify the caller the requested packets plus
               * some others are time stamped.
               */
-             config.rx_filter = HWTSTAMP_FILTER_SOME;
+             config->rx_filter = HWTSTAMP_FILTER_SOME;
              break;
      default:
Quoted 8 lines

drivers/net/ethernet/intel/e1000e/netdev.c static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr) · line 6208

Quoted 6 lines
drivers/net/ethernet/intel/e1000e/netdev.c +1 -3
              break;
      }
-
-     return copy_to_user(ifr->ifr_data, &config,
-                         sizeof(config)) ? -EFAULT : 0;
+     return ret_val;

I would expect 'return 0;'.

Makes perfect sense. Let me fix that.

Quoted 47 lines
Quoted 47 lines
drivers/net/ethernet/intel/e1000e/netdev.c +9 -7
  }
 
-static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_get(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
  {
      struct e1000_adapter *adapter = netdev_priv(netdev);
 
-     return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
-                         sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
+     memcpy(config, &adapter->hwtstamp_config,
+            sizeof(adapter->hwtstamp_config));
+     return 0;
  }
 
  static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
@@ -6224,10 +6227,6 @@ static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
      case SIOCGMIIREG:
      case SIOCSMIIREG:
              return e1000_mii_ioctl(netdev, ifr, cmd);
-     case SIOCSHWTSTAMP:
-             return e1000e_hwtstamp_set(netdev, ifr);
-     case SIOCGHWTSTAMP:
-             return e1000e_hwtstamp_get(netdev, ifr);
      default:
              return -EOPNOTSUPP;
      }
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
      .ndo_set_features = e1000_set_features,
      .ndo_fix_features = e1000_fix_features,
      .ndo_features_check     = passthru_features_check,
+     .ndo_hwtstamp_get       = e1000e_hwtstamp_get,
+     .ndo_hwtstamp_set       = e1000e_hwtstamp_set,
  };
 
  /**
drivers/net/netdevsim/netdev.c +0 -0
index 35fa1ca98671..502715c6e9e1 100644
@@ -238,6 +238,30 @@ nsim_set_features(struct net_device *dev, netdev_features_t features)
      return 0;
  }
 
Quoted 16 lines
Quoted 4 lines
drivers/net/netdevsim/netdev.c +4 -0
+static int
+nsim_hwtstamp_get(struct net_device *dev,
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,

drivers/net/netdevsim/netdev.c nsim_set_features(struct net_device *dev, netdev_features_t features) · line 254

Quoted 10 lines
drivers/net/netdevsim/netdev.c +10 -0
+               struct netlink_ext_ack *extack)
+{
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(config, &ns->hw_tstamp_config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}
+
+static int
+nsim_hwtstamp_ges(struct net_device *dev,

nsim_hwtstamp_ges -> nsim_hwtstamp_get

This was intended to be nsim_hwtstamp_set(). But nevertheless I got it wrong. Fixing it...

Quoted 14 lines
Quoted 14 lines
drivers/net/netdevsim/netdev.c +10 -0
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,
+               struct netlink_ext_ack *extack)
+{
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(&ns->hw_tstamp_config, config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}
+
  static const struct net_device_ops nsim_netdev_ops = {
      .ndo_start_xmit         = nsim_start_xmit,
      .ndo_set_rx_mode        = nsim_set_rx_mode,
Quoted 8 lines

drivers/net/netdevsim/netdev.c static const struct net_device_ops nsim_netdev_ops = { · line 284

Quoted 6 lines
drivers/net/netdevsim/netdev.c +2 -0
@@ -256,6 +280,8 @@ static const struct net_device_ops nsim_netdev_ops = {
      .ndo_setup_tc           = nsim_setup_tc,
      .ndo_set_features       = nsim_set_features,
      .ndo_bpf                = nsim_bpf,
+     .ndo_hwtstamp_get       = nsim_hwtstamp_get,
+     .ndo_hwtstamp_set       = nsim_hwtstamp_get,

nsim_hwtstamp_get -> nsim_hwtstamp_set

Aha, this one masked the previous error you caught. Thank you for pointing it out!

Quoted 32 lines
Quoted 32 lines
drivers/net/netdevsim/netdev.c +0 -0
  };
 
  static const struct net_device_ops nsim_vf_netdev_ops = {
drivers/net/netdevsim/netdevsim.h +1 -0
index 7d8ed8d8df5c..c6efd2383552 100644
@@ -102,6 +102,7 @@ struct netdevsim {
      } udp_ports;
 
      struct nsim_ethtool ethtool;
+     struct hwtstamp_config hw_tstamp_config;
  };
 
  struct netdevsim *
include/linux/netdevice.h +2 -0
index c8c634091a65..078c9284930a 100644
@@ -57,6 +57,8 @@
  struct netpoll_info;
  struct device;
  struct ethtool_ops;
+struct hwtstamp_config;
+struct kernel_hwtstamp_config;
  struct phy_device;
  struct dsa_port;
  struct ip_tunnel_parm;
@@ -1411,6 +1413,17 @@ struct netdev_net_notifier {
   *  Get hardware timestamp based on normal/adjustable time or free running
   *  cycle counter. This function is required if physical clock supports a
Quoted 8 lines

include/linux/netdevice.h struct netdev_net_notifier { · line 1420

Quoted 6 lines
include/linux/netdevice.h +5 -0
   *  free running cycle counter.
+ *   int (*ndo_hwtstamp_get)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Get hardware timestamping parameters currently configured  for NIC

'configured for' -> 'configured for'

Removing this double whitespace. Thank you!

Quoted 164 lines
Quoted 164 lines
include/linux/netdevice.h +14 -0
+ *   device.
+ *   int (*ndo_hwtstamp_set)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Set hardware timestamping parameters for NIC device.
   */
  struct net_device_ops {
      int                     (*ndo_init)(struct net_device *dev);
@@ -1645,6 +1658,14 @@ struct net_device_ops {
      ktime_t                 (*ndo_get_tstamp)(struct net_device *dev,
                                                const struct skb_shared_hwtstamps *hwtstamps,
                                                bool cycles);
+     int                     (*ndo_hwtstamp_get)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
+     int                     (*ndo_hwtstamp_set)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
  };
 
  struct xdp_metadata_ops {
include/uapi/linux/net_tstamp.h +15 -0
index a2c66b3d7f0f..547f73beb479 100644
@@ -79,6 +79,21 @@ struct hwtstamp_config {
      int rx_filter;
  };
 
+/**
+ * struct kernel_hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
+ *
+ * @dummy:   a placeholder field added to work around empty struct language
+ *           restriction
+ *
+ * This structure passed as a parameter to NDO methods called in
+ * response to SIOCGHWTSTAMP and SIOCSHWTSTAMP IOCTLs.
+ * The structure is effectively empty for now. Before adding new fields
+ * to the structure "dummy" placeholder field should be removed.
+ */
+struct kernel_hwtstamp_config {
+     u8 dummy;
+};
+
  /* possible values for hwtstamp_config->flags */
  enum hwtstamp_flags {
      /*
net/core/dev_ioctl.c +65 -12
index 846669426236..c145afb3f77e 100644
@@ -183,22 +183,18 @@ static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cm
      return err;
  }
 
-static int net_hwtstamp_validate(struct ifreq *ifr)
+static int net_hwtstamp_validate(struct hwtstamp_config *cfg)
  {
-     struct hwtstamp_config cfg;
      enum hwtstamp_tx_types tx_type;
      enum hwtstamp_rx_filters rx_filter;
      int tx_type_valid = 0;
      int rx_filter_valid = 0;
 
-     if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-             return -EFAULT;
-
-     if (cfg.flags & ~HWTSTAMP_FLAG_MASK)
+     if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
              return -EINVAL;
 
-     tx_type = cfg.tx_type;
-     rx_filter = cfg.rx_filter;
+     tx_type = cfg->tx_type;
+     rx_filter = cfg->rx_filter;
 
      switch (tx_type) {
      case HWTSTAMP_TX_OFF:
@@ -277,6 +273,63 @@ static int dev_siocbond(struct net_device *dev,
      return -EOPNOTSUPP;
  }
 
+static int dev_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_get)
+             return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_get(dev, &config, NULL, NULL);
+     if (err)
+             return err;
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
+static int dev_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
+             return -EFAULT;
+
+     err = net_hwtstamp_validate(&config);
+     if (err)
+             return err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_set)
+             return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_set(dev, &config, NULL, NULL);
+     if (err)
+             return err;
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
  static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
                            void __user *data, unsigned int cmd)
  {
@@ -391,11 +444,11 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
              rtnl_lock();
              return err;
 
+     case SIOCGHWTSTAMP:
+             return dev_hwtstamp_get(dev, ifr);
+
      case SIOCSHWTSTAMP:
-             err = net_hwtstamp_validate(ifr);
-             if (err)
-                     return err;
-             fallthrough;
+             return dev_hwtstamp_set(dev, ifr);
 
      /*
       *      Unknown or private ioctl
@@ -407,9 +460,7 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
 
Quoted 9 lines

net/core/dev_ioctl.c static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data, · line 466

Quoted 9 lines
net/core/dev_ioctl.c +1 -3
              if (cmd == SIOCGMIIPHY ||
                  cmd == SIOCGMIIREG ||
-                 cmd == SIOCSMIIREG ||
-                 cmd == SIOCSHWTSTAMP ||
-                 cmd == SIOCGHWTSTAMP) {
+                 cmd == SIOCSMIIREG) {
                      err = dev_eth_ioctl(dev, ifr, cmd);
              } else if (cmd == SIOCBONDENSLAVE ||
                  cmd == SIOCBONDRELEASE ||

Gerhard, thank you for reviewing the patch and highlighting all these errors! I'll incorporate the fixes to ver 3 of the patch.

#5 lore raw
In reply to Maxim Georgiev
Quoted 12 lines · Maxim Georgiev

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

Quoted 2 lines

This patch is sent out as RFC.
It still pending on basic testing.

Who should do that testing? Do you have any NIC with the hardware timestamping capability?

Quoted 3 lines

Implementing ndo_hwtstamp_get/set in netdevsim driver should allow manual testing of the request translation logic. Also is there a way to automate this testing?

Automatic testing would require manual testing as a first step, to iron out the procedure, right?

The netdevsim driver should be testable by anyone using 'echo "0 1" > /sys/bus/netdevsim/new_device', and then 'hwstamp_ctl -i eni0np1 -t 1' (or 'testptp', or 'ptp4l', or whatever). Have you tried doing at least that, did it work?

Quoted 108 lines

---
Changes in v2:
- Introduced kernel_hwtstamp_config structure
- Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters
- Reodered function variable declarations in dev_hwtstamp()
- Refactored error handling logic in dev_hwtstamp()
- Split dev_hwtstamp() into GET and SET versions
- Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter

---
 drivers/net/ethernet/intel/e1000e/netdev.c | 39 ++++++-----
 drivers/net/netdevsim/netdev.c             | 26 +++++++
 drivers/net/netdevsim/netdevsim.h          |  1 +
 include/linux/netdevice.h                  | 21 ++++++
 include/uapi/linux/net_tstamp.h            | 15 ++++
 net/core/dev_ioctl.c                       | 81 ++++++++++++++++++----
 6 files changed, 149 insertions(+), 34 deletions(-)
drivers/net/ethernet/intel/e1000e/netdev.c +18 -19
index 6f5c16aebcbf..5b98f7257c77 100644
@@ -6161,7 +6161,9 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
 /**
  * e1000e_hwtstamp_set - control hardware time stamping
  * @netdev: network interface device structure
- * @ifr: interface request
+ * @config: hwtstamp_config structure containing request parameters
+ * @kernel_config: kernel version of config parameter structure.
+ * @extack: netlink request parameters.
  *
  * Outgoing time stamping can be enabled and disabled. Play nice and
  * disable it when requested, although it shouldn't cause any overhead
@@ -6174,20 +6176,19 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  * specified. Matching the kind of event packet is not supported, with the
  * exception of "all V2 events regardless of level 2 or 4".
  **/
-static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_set(struct net_device *netdev,
+			       struct hwtstamp_config *config,
+			       struct kernel_hwtstamp_config *kernel_config,
+			       struct netlink_ext_ack *extack)
 {
 	struct e1000_adapter *adapter = netdev_priv(netdev);
-	struct hwtstamp_config config;
 	int ret_val;
 
-	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-		return -EFAULT;
-
-	ret_val = e1000e_config_hwtstamp(adapter, &config);
+	ret_val = e1000e_config_hwtstamp(adapter, config);
 	if (ret_val)
 		return ret_val;
 
-	switch (config.rx_filter) {
+	switch (config->rx_filter) {
 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -6199,22 +6200,24 @@ static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
 		 * by hardware so notify the caller the requested packets plus
 		 * some others are time stamped.
 		 */
-		config.rx_filter = HWTSTAMP_FILTER_SOME;
+		config->rx_filter = HWTSTAMP_FILTER_SOME;
 		break;
 	default:
 		break;
 	}
-
-	return copy_to_user(ifr->ifr_data, &config,
-			    sizeof(config)) ? -EFAULT : 0;
+	return ret_val;
 }
 
-static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_get(struct net_device *netdev,
+			       struct hwtstamp_config *config,
+			       struct kernel_hwtstamp_config *kernel_config,
+			       struct netlink_ext_ack *extack)
 {
 	struct e1000_adapter *adapter = netdev_priv(netdev);
 
-	return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
-			    sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
+	memcpy(config, &adapter->hwtstamp_config,
+	       sizeof(adapter->hwtstamp_config));
+	return 0;
 }
 
 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
@@ -6224,10 +6227,6 @@ static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
 	case SIOCGMIIREG:
 	case SIOCSMIIREG:
 		return e1000_mii_ioctl(netdev, ifr, cmd);
-	case SIOCSHWTSTAMP:
-		return e1000e_hwtstamp_set(netdev, ifr);
-	case SIOCGHWTSTAMP:
-		return e1000e_hwtstamp_get(netdev, ifr);
 	default:
 		return -EOPNOTSUPP;
 	}

drivers/net/ethernet/intel/e1000e/netdev.c static const struct net_device_ops e1000e_netdev_ops = { · line 7369

Quoted 6 lines
drivers/net/ethernet/intel/e1000e/netdev.c +2 -0
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
 	.ndo_set_features = e1000_set_features,
 	.ndo_fix_features = e1000_fix_features,
 	.ndo_features_check	= passthru_features_check,
+	.ndo_hwtstamp_get	= e1000e_hwtstamp_get,
+	.ndo_hwtstamp_set	= e1000e_hwtstamp_set,
 };

The conversion per se looks almost in line with what I was expecting to see, except for the comments. I guess you can convert a single driver first (e1000 seems fine), to get the API merged, then more people could work in parallel?

Or do you want netdevsim to cover hardware timestamping from the beginning too, Jakub?

Quoted 23 lines
drivers/net/ethernet/intel/e1000e/netdev.c +0 -0
 /**
drivers/net/netdevsim/netdev.c +13 -0
index 35fa1ca98671..502715c6e9e1 100644
@@ -238,6 +238,30 @@ nsim_set_features(struct net_device *dev, netdev_features_t features)
 	return 0;
 }
 
+static int
+nsim_hwtstamp_get(struct net_device *dev,
+		  struct hwtstamp_config *config,
+		  struct kernel_hwtstamp_config *kernel_config,
+		  struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	memcpy(config, &ns->hw_tstamp_config, sizeof(ns->hw_tstamp_config));
+	return 0;
+}
+
+static int

drivers/net/netdevsim/netdev.c nsim_set_features(struct net_device *dev, netdev_features_t features) · line 263

Quoted 10 lines
drivers/net/netdevsim/netdev.c +10 -0
+nsim_hwtstamp_ges(struct net_device *dev,
+		  struct hwtstamp_config *config,
+		  struct kernel_hwtstamp_config *kernel_config,
+		  struct netlink_ext_ack *extack)
+{
+	struct netdevsim *ns = netdev_priv(dev);
+
+	memcpy(&ns->hw_tstamp_config, config, sizeof(ns->hw_tstamp_config));
+	return 0;
+}

Please keep conversion patches separate from patches which add new functionality.

Also, does the netdevsim portion even do something useful? Wouldn't you need to implement ethtool_ops :: get_ts_info() in order for user space to know that there is a PHC, and that the device supports hardware timestamping?

Quoted 43 lines
drivers/net/netdevsim/netdev.c +3 -0
+
 static const struct net_device_ops nsim_netdev_ops = {
 	.ndo_start_xmit		= nsim_start_xmit,
 	.ndo_set_rx_mode	= nsim_set_rx_mode,
@@ -256,6 +280,8 @@ static const struct net_device_ops nsim_netdev_ops = {
 	.ndo_setup_tc		= nsim_setup_tc,
 	.ndo_set_features	= nsim_set_features,
 	.ndo_bpf		= nsim_bpf,
+	.ndo_hwtstamp_get	= nsim_hwtstamp_get,
+	.ndo_hwtstamp_set	= nsim_hwtstamp_get,
 };
 
 static const struct net_device_ops nsim_vf_netdev_ops = {
drivers/net/netdevsim/netdevsim.h +1 -0
index 7d8ed8d8df5c..c6efd2383552 100644
@@ -102,6 +102,7 @@ struct netdevsim {
 	} udp_ports;
 
 	struct nsim_ethtool ethtool;
+	struct hwtstamp_config hw_tstamp_config;
 };
 
 struct netdevsim *
include/linux/netdevice.h +3 -0
index c8c634091a65..078c9284930a 100644
@@ -57,6 +57,8 @@
 struct netpoll_info;
 struct device;
 struct ethtool_ops;
+struct hwtstamp_config;
+struct kernel_hwtstamp_config;
 struct phy_device;
 struct dsa_port;
 struct ip_tunnel_parm;
@@ -1411,6 +1413,17 @@ struct netdev_net_notifier {
  *	Get hardware timestamp based on normal/adjustable time or free running
  *	cycle counter. This function is required if physical clock supports a
  *	free running cycle counter.
+ *	int (*ndo_hwtstamp_get)(struct net_device *dev,

include/linux/netdevice.h struct netdev_net_notifier { · line 1426

Quoted 10 lines
include/linux/netdevice.h +10 -0
+ *				struct hwtstamp_config *config,
+ *				struct kernel_hwtstamp_config *kernel_config,
+ *				struct netlink_ext_ack *extack);
+ *	Get hardware timestamping parameters currently configured  for NIC
+ *	device.
+ *	int (*ndo_hwtstamp_set)(struct net_device *dev,
+ *				struct hwtstamp_config *config,
+ *				struct kernel_hwtstamp_config *kernel_config,
+ *				struct netlink_ext_ack *extack);
+ *	Set hardware timestamping parameters for NIC device.

I would expect that in the next version, you only pass struct kernel_hwtstamp_config * to these and not struct hwtstamp_config *, since the former was merged in a form that contains all that struct hwtstamp_config does, right?

Quoted 30 lines
include/linux/netdevice.h +8 -0
  */
 struct net_device_ops {
 	int			(*ndo_init)(struct net_device *dev);
@@ -1645,6 +1658,14 @@ struct net_device_ops {
 	ktime_t			(*ndo_get_tstamp)(struct net_device *dev,
 						  const struct skb_shared_hwtstamps *hwtstamps,
 						  bool cycles);
+	int			(*ndo_hwtstamp_get)(struct net_device *dev,
+						    struct hwtstamp_config *config,
+						    struct kernel_hwtstamp_config *kernel_config,
+						    struct netlink_ext_ack *extack);
+	int			(*ndo_hwtstamp_set)(struct net_device *dev,
+						    struct hwtstamp_config *config,
+						    struct kernel_hwtstamp_config *kernel_config,
+						    struct netlink_ext_ack *extack);
 };
 
 struct xdp_metadata_ops {
include/uapi/linux/net_tstamp.h +4 -0
index a2c66b3d7f0f..547f73beb479 100644
@@ -79,6 +79,21 @@ struct hwtstamp_config {
 	int rx_filter;
 };
 
+/**
+ * struct kernel_hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
+ *
+ * @dummy:	a placeholder field added to work around empty struct language

include/uapi/linux/net_tstamp.h struct hwtstamp_config { · line 95

Quoted 10 lines
include/uapi/linux/net_tstamp.h +10 -0
+ *		restriction
+ *
+ * This structure passed as a parameter to NDO methods called in
+ * response to SIOCGHWTSTAMP and SIOCSHWTSTAMP IOCTLs.
+ * The structure is effectively empty for now. Before adding new fields
+ * to the structure "dummy" placeholder field should be removed.
+ */
+struct kernel_hwtstamp_config {
+	u8 dummy;
+};

In include/uapi? No-no. That's exported to user space, which contradicts the whole point of having a structure visible just to the kernel.

See net-next commit c4bffeaa8d50 ("net: add struct kernel_hwtstamp_config and make net_hwtstamp_validate() use it") by the way.

Quoted 80 lines
include/uapi/linux/net_tstamp.h +1 -0
+
 /* possible values for hwtstamp_config->flags */
 enum hwtstamp_flags {
 	/*
net/core/dev_ioctl.c +45 -8
index 846669426236..c145afb3f77e 100644
@@ -183,22 +183,18 @@ static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cm
 	return err;
 }
 
-static int net_hwtstamp_validate(struct ifreq *ifr)
+static int net_hwtstamp_validate(struct hwtstamp_config *cfg)
 {
-	struct hwtstamp_config cfg;
 	enum hwtstamp_tx_types tx_type;
 	enum hwtstamp_rx_filters rx_filter;
 	int tx_type_valid = 0;
 	int rx_filter_valid = 0;
 
-	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-		return -EFAULT;
-
-	if (cfg.flags & ~HWTSTAMP_FLAG_MASK)
+	if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
 		return -EINVAL;
 
-	tx_type = cfg.tx_type;
-	rx_filter = cfg.rx_filter;
+	tx_type = cfg->tx_type;
+	rx_filter = cfg->rx_filter;
 
 	switch (tx_type) {
 	case HWTSTAMP_TX_OFF:
@@ -277,6 +273,63 @@ static int dev_siocbond(struct net_device *dev,
 	return -EOPNOTSUPP;
 }
 
+static int dev_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
+{
+	const struct net_device_ops *ops = dev->netdev_ops;
+	struct hwtstamp_config config;
+	int err;
+
+	err = dsa_ndo_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+	if (err == 0 || err != -EOPNOTSUPP)
+		return err;
+
+	if (!ops->ndo_hwtstamp_get)
+		return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+
+	if (!netif_device_present(dev))
+		return -ENODEV;
+
+	err = ops->ndo_hwtstamp_get(dev, &config, NULL, NULL);
+	if (err)
+		return err;
+
+	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+		return -EFAULT;
+	return 0;
+}
+
+static int dev_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
+{
+	const struct net_device_ops *ops = dev->netdev_ops;
+	struct hwtstamp_config config;
+	int err;
+
+	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
+		return -EFAULT;
+
+	err = net_hwtstamp_validate(&config);
+	if (err)
+		return err;
+
+	err = dsa_ndo_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+	if (err == 0 || err != -EOPNOTSUPP)
+		return err;

net/core/dev_ioctl.c static int dev_siocbond(struct net_device *dev, · line 326

Quoted 10 lines
net/core/dev_ioctl.c +10 -0
+
+	if (!ops->ndo_hwtstamp_set)
+		return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+
+	if (!netif_device_present(dev))
+		return -ENODEV;
+
+	err = ops->ndo_hwtstamp_set(dev, &config, NULL, NULL);
+	if (err)
+		return err;

Here, when you rebase over commit 4ee58e1e5680 ("net: promote SIOCSHWTSTAMP and SIOCGHWTSTAMP ioctls to dedicated handlers"),
I expect that you will add another helper function to include/linux/net_tstamp.h called hwtstamp_config_from_kernel(), which translates back from the struct kernel_hwtstamp_config into something on which copy_from_user() can be called, correct?

Quoted 39 lines
net/core/dev_ioctl.c +11 -7
+
+	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+		return -EFAULT;
+	return 0;
+}
+
 static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
 			      void __user *data, unsigned int cmd)
 {
@@ -391,11 +444,11 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
 		rtnl_lock();
 		return err;
 
+	case SIOCGHWTSTAMP:
+		return dev_hwtstamp_get(dev, ifr);
+
 	case SIOCSHWTSTAMP:
-		err = net_hwtstamp_validate(ifr);
-		if (err)
-			return err;
-		fallthrough;
+		return dev_hwtstamp_set(dev, ifr);
 
 	/*
 	 *	Unknown or private ioctl
@@ -407,9 +460,7 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
 
 		if (cmd == SIOCGMIIPHY ||
 		    cmd == SIOCGMIIREG ||
-		    cmd == SIOCSMIIREG ||
-		    cmd == SIOCSHWTSTAMP ||
-		    cmd == SIOCGHWTSTAMP) {
+		    cmd == SIOCSMIIREG) {
 			err = dev_eth_ioctl(dev, ifr, cmd);
 		} else if (cmd == SIOCBONDENSLAVE ||
 		    cmd == SIOCBONDRELEASE ||
Signature · 1 line
2.39.2
#6 lore raw
Quoted 14 lines · Vladimir Oltean
Quoted 12 lines · Maxim Georgiev

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

Quoted 5 lines
Quoted 2 lines

This patch is sent out as RFC.
It still pending on basic testing.

Who should do that testing? Do you have any NIC with the hardware timestamping capability?

I'm planning to do the testing with netdevsim. I don't have access to any NICs with
hardware timestamping support.

Quoted 7 lines
Quoted 4 lines

Implementing ndo_hwtstamp_get/set in netdevsim driver should allow manual testing of the request translation logic. Also is there a way to automate this testing?

Automatic testing would require manual testing as a first step, to iron out the procedure, right?

Yes, absolutely.

Quoted 5 lines

The netdevsim driver should be testable by anyone using 'echo "0 1" > /sys/bus/netdevsim/new_device', and then 'hwstamp_ctl -i eni0np1 -t 1' (or 'testptp', or 'ptp4l', or whatever). Have you tried doing at least that, did it work?

Let me rebase my patch on top of your changes. I'll test it on netdevsim then.

Quoted 112 lines
Quoted 112 lines

---
Changes in v2:
- Introduced kernel_hwtstamp_config structure
- Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters
- Reodered function variable declarations in dev_hwtstamp()
- Refactored error handling logic in dev_hwtstamp()
- Split dev_hwtstamp() into GET and SET versions
- Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter

---
 drivers/net/ethernet/intel/e1000e/netdev.c | 39 ++++++-----
 drivers/net/netdevsim/netdev.c             | 26 +++++++
 drivers/net/netdevsim/netdevsim.h          |  1 +
 include/linux/netdevice.h                  | 21 ++++++
 include/uapi/linux/net_tstamp.h            | 15 ++++
 net/core/dev_ioctl.c                       | 81 ++++++++++++++++++----
 6 files changed, 149 insertions(+), 34 deletions(-)
drivers/net/ethernet/intel/e1000e/netdev.c +18 -19
index 6f5c16aebcbf..5b98f7257c77 100644
@@ -6161,7 +6161,9 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
 /**
  * e1000e_hwtstamp_set - control hardware time stamping
  * @netdev: network interface device structure
- * @ifr: interface request
+ * @config: hwtstamp_config structure containing request parameters
+ * @kernel_config: kernel version of config parameter structure.
+ * @extack: netlink request parameters.
  *
  * Outgoing time stamping can be enabled and disabled. Play nice and
  * disable it when requested, although it shouldn't cause any overhead
@@ -6174,20 +6176,19 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  * specified. Matching the kind of event packet is not supported, with the
  * exception of "all V2 events regardless of level 2 or 4".
  **/
-static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_set(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
 {
      struct e1000_adapter *adapter = netdev_priv(netdev);
-     struct hwtstamp_config config;
      int ret_val;
 
-     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-             return -EFAULT;
-
-     ret_val = e1000e_config_hwtstamp(adapter, &config);
+     ret_val = e1000e_config_hwtstamp(adapter, config);
      if (ret_val)
              return ret_val;
 
-     switch (config.rx_filter) {
+     switch (config->rx_filter) {
      case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
      case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
      case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -6199,22 +6200,24 @@ static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
               * by hardware so notify the caller the requested packets plus
               * some others are time stamped.
               */
-             config.rx_filter = HWTSTAMP_FILTER_SOME;
+             config->rx_filter = HWTSTAMP_FILTER_SOME;
              break;
      default:
              break;
      }
-
-     return copy_to_user(ifr->ifr_data, &config,
-                         sizeof(config)) ? -EFAULT : 0;
+     return ret_val;
 }
 
-static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_get(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
 {
      struct e1000_adapter *adapter = netdev_priv(netdev);
 
-     return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
-                         sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
+     memcpy(config, &adapter->hwtstamp_config,
+            sizeof(adapter->hwtstamp_config));
+     return 0;
 }
 
 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
@@ -6224,10 +6227,6 @@ static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
      case SIOCGMIIREG:
      case SIOCSMIIREG:
              return e1000_mii_ioctl(netdev, ifr, cmd);
-     case SIOCSHWTSTAMP:
-             return e1000e_hwtstamp_set(netdev, ifr);
-     case SIOCGHWTSTAMP:
-             return e1000e_hwtstamp_get(netdev, ifr);
      default:
              return -EOPNOTSUPP;
      }
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
      .ndo_set_features = e1000_set_features,
      .ndo_fix_features = e1000_fix_features,
      .ndo_features_check     = passthru_features_check,
Quoted 8 lines

drivers/net/ethernet/intel/e1000e/netdev.c static const struct net_device_ops e1000e_netdev_ops = { · line 7369

Quoted 3 lines
drivers/net/ethernet/intel/e1000e/netdev.c +2 -0
+     .ndo_hwtstamp_get       = e1000e_hwtstamp_get,
+     .ndo_hwtstamp_set       = e1000e_hwtstamp_set,
 };

The conversion per se looks almost in line with what I was expecting to see, except for the comments. I guess you can convert a single driver first (e1000 seems fine), to get the API merged, then more people could work in parallel?

The conversions are going to be easy (that was the point of adding these NDO functions). But there is still a problem of validating these conversions with testing.
Unfortunately I don't have an e1000 card available to validate this conversion. I'll let you and Jakub decide what will be the best strategy here.

Quoted 32 lines

Or do you want netdevsim to cover hardware timestamping from the beginning too, Jakub?

Quoted 28 lines
drivers/net/ethernet/intel/e1000e/netdev.c +0 -0
 /**
drivers/net/netdevsim/netdev.c +18 -0
index 35fa1ca98671..502715c6e9e1 100644
@@ -238,6 +238,30 @@ nsim_set_features(struct net_device *dev, netdev_features_t features)
      return 0;
 }
 
+static int
+nsim_hwtstamp_get(struct net_device *dev,
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,
+               struct netlink_ext_ack *extack)
+{
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(config, &ns->hw_tstamp_config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}
+
+static int
+nsim_hwtstamp_ges(struct net_device *dev,
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,
+               struct netlink_ext_ack *extack)
+{
Quoted 8 lines

drivers/net/netdevsim/netdev.c nsim_set_features(struct net_device *dev, netdev_features_t features) · line 263

Quoted 5 lines
drivers/net/netdevsim/netdev.c +5 -0
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(&ns->hw_tstamp_config, config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}

Please keep conversion patches separate from patches which add new functionality.

Let me split my monolithic patch into a stack of patches in the next version. e1000e and netdevsim should be definitely arranged as separate patches which could be reviewed and accepted/rejected independently.

Quoted 5 lines

Also, does the netdevsim portion even do something useful? Wouldn't you need to implement ethtool_ops :: get_ts_info() in order for user space to know that there is a PHC, and that the device supports hardware timestamping?

I was thinking of exposing hw timestamping settings to testfs, but the ethtool approach can be easier to implement. Let me fix it!

Quoted 51 lines
Quoted 51 lines
drivers/net/netdevsim/netdev.c +3 -0
+
 static const struct net_device_ops nsim_netdev_ops = {
      .ndo_start_xmit         = nsim_start_xmit,
      .ndo_set_rx_mode        = nsim_set_rx_mode,
@@ -256,6 +280,8 @@ static const struct net_device_ops nsim_netdev_ops = {
      .ndo_setup_tc           = nsim_setup_tc,
      .ndo_set_features       = nsim_set_features,
      .ndo_bpf                = nsim_bpf,
+     .ndo_hwtstamp_get       = nsim_hwtstamp_get,
+     .ndo_hwtstamp_set       = nsim_hwtstamp_get,
 };
 
 static const struct net_device_ops nsim_vf_netdev_ops = {
drivers/net/netdevsim/netdevsim.h +1 -0
index 7d8ed8d8df5c..c6efd2383552 100644
@@ -102,6 +102,7 @@ struct netdevsim {
      } udp_ports;
 
      struct nsim_ethtool ethtool;
+     struct hwtstamp_config hw_tstamp_config;
 };
 
 struct netdevsim *
include/linux/netdevice.h +10 -0
index c8c634091a65..078c9284930a 100644
@@ -57,6 +57,8 @@
 struct netpoll_info;
 struct device;
 struct ethtool_ops;
+struct hwtstamp_config;
+struct kernel_hwtstamp_config;
 struct phy_device;
 struct dsa_port;
 struct ip_tunnel_parm;
@@ -1411,6 +1413,17 @@ struct netdev_net_notifier {
  *   Get hardware timestamp based on normal/adjustable time or free running
  *   cycle counter. This function is required if physical clock supports a
  *   free running cycle counter.
+ *   int (*ndo_hwtstamp_get)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Get hardware timestamping parameters currently configured  for NIC
+ *   device.
+ *   int (*ndo_hwtstamp_set)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
Quoted 8 lines

include/linux/netdevice.h struct netdev_net_notifier { · line 1426

Quoted 3 lines
include/linux/netdevice.h +3 -0
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Set hardware timestamping parameters for NIC device.

I would expect that in the next version, you only pass struct kernel_hwtstamp_config * to these and not struct hwtstamp_config *, since the former was merged in a form that contains all that struct hwtstamp_config does, right?

Yes, I guess I'll just follow your lead and use the kernel_hwtstamp_config you defined to pass parameters to ndo_hwtstamp_get/set. Jakub, do you have any objections here?

Quoted 45 lines
Quoted 31 lines
include/linux/netdevice.h +8 -0
  */
 struct net_device_ops {
      int                     (*ndo_init)(struct net_device *dev);
@@ -1645,6 +1658,14 @@ struct net_device_ops {
      ktime_t                 (*ndo_get_tstamp)(struct net_device *dev,
                                                const struct skb_shared_hwtstamps *hwtstamps,
                                                bool cycles);
+     int                     (*ndo_hwtstamp_get)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
+     int                     (*ndo_hwtstamp_set)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
 };
 
 struct xdp_metadata_ops {
include/uapi/linux/net_tstamp.h +4 -0
index a2c66b3d7f0f..547f73beb479 100644
@@ -79,6 +79,21 @@ struct hwtstamp_config {
      int rx_filter;
 };
 
+/**
+ * struct kernel_hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
+ *
+ * @dummy:   a placeholder field added to work around empty struct language

include/uapi/linux/net_tstamp.h struct hwtstamp_config { · line 95

Quoted 10 lines
include/uapi/linux/net_tstamp.h +10 -0
+ *           restriction
+ *
+ * This structure passed as a parameter to NDO methods called in
+ * response to SIOCGHWTSTAMP and SIOCSHWTSTAMP IOCTLs.
+ * The structure is effectively empty for now. Before adding new fields
+ * to the structure "dummy" placeholder field should be removed.
+ */
+struct kernel_hwtstamp_config {
+     u8 dummy;
+};

In include/uapi? No-no. That's exported to user space, which contradicts the whole point of having a structure visible just to the kernel.

Quoted 2 lines

See net-next commit c4bffeaa8d50 ("net: add struct kernel_hwtstamp_config and make net_hwtstamp_validate() use it") by the way.

Yes, I realized that I touched the wrong file when I was reading through your patch stack. You already defined kernel_hwtstamp_config, so my changes to include/uapi/linux/net_tstamp.h should be reverted anyway.

Quoted 90 lines
Quoted 90 lines
include/uapi/linux/net_tstamp.h +1 -0
+
 /* possible values for hwtstamp_config->flags */
 enum hwtstamp_flags {
      /*
net/core/dev_ioctl.c +54 -8
index 846669426236..c145afb3f77e 100644
@@ -183,22 +183,18 @@ static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cm
      return err;
 }
 
-static int net_hwtstamp_validate(struct ifreq *ifr)
+static int net_hwtstamp_validate(struct hwtstamp_config *cfg)
 {
-     struct hwtstamp_config cfg;
      enum hwtstamp_tx_types tx_type;
      enum hwtstamp_rx_filters rx_filter;
      int tx_type_valid = 0;
      int rx_filter_valid = 0;
 
-     if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-             return -EFAULT;
-
-     if (cfg.flags & ~HWTSTAMP_FLAG_MASK)
+     if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
              return -EINVAL;
 
-     tx_type = cfg.tx_type;
-     rx_filter = cfg.rx_filter;
+     tx_type = cfg->tx_type;
+     rx_filter = cfg->rx_filter;
 
      switch (tx_type) {
      case HWTSTAMP_TX_OFF:
@@ -277,6 +273,63 @@ static int dev_siocbond(struct net_device *dev,
      return -EOPNOTSUPP;
 }
 
+static int dev_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_get)
+             return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_get(dev, &config, NULL, NULL);
+     if (err)
+             return err;
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
+static int dev_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
+             return -EFAULT;
+
+     err = net_hwtstamp_validate(&config);
+     if (err)
+             return err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_set)
+             return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_set(dev, &config, NULL, NULL);
+     if (err)
Quoted 8 lines

net/core/dev_ioctl.c static int dev_siocbond(struct net_device *dev, · line 326

Quoted 1 line
net/core/dev_ioctl.c +1 -0
+             return err;

Here, when you rebase over commit 4ee58e1e5680 ("net: promote SIOCSHWTSTAMP and SIOCGHWTSTAMP ioctls to dedicated handlers"),
I expect that you will add another helper function to include/linux/net_tstamp.h called hwtstamp_config_from_kernel(), which translates back from the struct kernel_hwtstamp_config into something on which copy_from_user() can be called, correct?

Yes, that's what I'm planning to do as part of rebasing this patch.

Quoted 40 lines
Quoted 40 lines
net/core/dev_ioctl.c +11 -8
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
 static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
                            void __user *data, unsigned int cmd)
 {
@@ -391,11 +444,11 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
              rtnl_lock();
              return err;
 
+     case SIOCGHWTSTAMP:
+             return dev_hwtstamp_get(dev, ifr);
+
      case SIOCSHWTSTAMP:
-             err = net_hwtstamp_validate(ifr);
-             if (err)
-                     return err;
-             fallthrough;
+             return dev_hwtstamp_set(dev, ifr);
 
      /*
       *      Unknown or private ioctl
@@ -407,9 +460,7 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
 
              if (cmd == SIOCGMIIPHY ||
                  cmd == SIOCGMIIREG ||
-                 cmd == SIOCSMIIREG ||
-                 cmd == SIOCSHWTSTAMP ||
-                 cmd == SIOCGHWTSTAMP) {
+                 cmd == SIOCSMIIREG) {
                      err = dev_eth_ioctl(dev, ifr, cmd);
              } else if (cmd == SIOCBONDENSLAVE ||
                  cmd == SIOCBONDRELEASE ||
--

2.39.2

#7 lore raw
Quoted 18 lines · Max Georgiev
Quoted 17 lines · Vladimir Oltean
Quoted 14 lines · Maxim Georgiev

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

This patch is sent out as RFC.
It still pending on basic testing.

Quoted 7 lines
Quoted 2 lines

Who should do that testing? Do you have any NIC with the hardware timestamping capability?

I'm planning to do the testing with netdevsim. I don't have access to any NICs with
hardware timestamping support.

Hi Max!
I might do some manual tests with the hardware that support timestamping once you respin the series with the changes discussed in the code comments. I'll convert hardware drivers myself to support new NDO on top of your patches.

Quoted 457 lines
Quoted 7 lines
Quoted 4 lines

Implementing ndo_hwtstamp_get/set in netdevsim driver should allow manual testing of the request translation logic. Also is there a way to automate this testing?

Automatic testing would require manual testing as a first step, to iron out the procedure, right?

Yes, absolutely.

Quoted 5 lines

The netdevsim driver should be testable by anyone using 'echo "0 1" > /sys/bus/netdevsim/new_device', and then 'hwstamp_ctl -i eni0np1 -t 1' (or 'testptp', or 'ptp4l', or whatever). Have you tried doing at least that, did it work?

Let me rebase my patch on top of your changes. I'll test it on netdevsim then.

Quoted 112 lines
Quoted 112 lines

---
Changes in v2:
- Introduced kernel_hwtstamp_config structure
- Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters
- Reodered function variable declarations in dev_hwtstamp()
- Refactored error handling logic in dev_hwtstamp()
- Split dev_hwtstamp() into GET and SET versions
- Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter

---
  drivers/net/ethernet/intel/e1000e/netdev.c | 39 ++++++-----
  drivers/net/netdevsim/netdev.c             | 26 +++++++
  drivers/net/netdevsim/netdevsim.h          |  1 +
  include/linux/netdevice.h                  | 21 ++++++
  include/uapi/linux/net_tstamp.h            | 15 ++++
  net/core/dev_ioctl.c                       | 81 ++++++++++++++++++----
  6 files changed, 149 insertions(+), 34 deletions(-)
drivers/net/ethernet/intel/e1000e/netdev.c +18 -19
index 6f5c16aebcbf..5b98f7257c77 100644
@@ -6161,7 +6161,9 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  /**
   * e1000e_hwtstamp_set - control hardware time stamping
   * @netdev: network interface device structure
- * @ifr: interface request
+ * @config: hwtstamp_config structure containing request parameters
+ * @kernel_config: kernel version of config parameter structure.
+ * @extack: netlink request parameters.
   *
   * Outgoing time stamping can be enabled and disabled. Play nice and
   * disable it when requested, although it shouldn't cause any overhead
@@ -6174,20 +6176,19 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
   * specified. Matching the kind of event packet is not supported, with the
   * exception of "all V2 events regardless of level 2 or 4".
   **/
-static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_set(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
  {
       struct e1000_adapter *adapter = netdev_priv(netdev);
-     struct hwtstamp_config config;
       int ret_val;
 
-     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-             return -EFAULT;
-
-     ret_val = e1000e_config_hwtstamp(adapter, &config);
+     ret_val = e1000e_config_hwtstamp(adapter, config);
       if (ret_val)
               return ret_val;
 
-     switch (config.rx_filter) {
+     switch (config->rx_filter) {
       case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
       case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
       case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -6199,22 +6200,24 @@ static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
                * by hardware so notify the caller the requested packets plus
                * some others are time stamped.
                */
-             config.rx_filter = HWTSTAMP_FILTER_SOME;
+             config->rx_filter = HWTSTAMP_FILTER_SOME;
               break;
       default:
               break;
       }
-
-     return copy_to_user(ifr->ifr_data, &config,
-                         sizeof(config)) ? -EFAULT : 0;
+     return ret_val;
  }
 
-static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_get(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
  {
       struct e1000_adapter *adapter = netdev_priv(netdev);
 
-     return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
-                         sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
+     memcpy(config, &adapter->hwtstamp_config,
+            sizeof(adapter->hwtstamp_config));
+     return 0;
  }
 
  static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
@@ -6224,10 +6227,6 @@ static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
       case SIOCGMIIREG:
       case SIOCSMIIREG:
               return e1000_mii_ioctl(netdev, ifr, cmd);
-     case SIOCSHWTSTAMP:
-             return e1000e_hwtstamp_set(netdev, ifr);
-     case SIOCGHWTSTAMP:
-             return e1000e_hwtstamp_get(netdev, ifr);
       default:
               return -EOPNOTSUPP;
       }
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
       .ndo_set_features = e1000_set_features,
       .ndo_fix_features = e1000_fix_features,
       .ndo_features_check     = passthru_features_check,
Quoted 8 lines

drivers/net/ethernet/intel/e1000e/netdev.c static const struct net_device_ops e1000e_netdev_ops = { · line 7369

Quoted 3 lines
drivers/net/ethernet/intel/e1000e/netdev.c +2 -0
+     .ndo_hwtstamp_get       = e1000e_hwtstamp_get,
+     .ndo_hwtstamp_set       = e1000e_hwtstamp_set,
  };

The conversion per se looks almost in line with what I was expecting to see, except for the comments. I guess you can convert a single driver first (e1000 seems fine), to get the API merged, then more people could work in parallel?

The conversions are going to be easy (that was the point of adding these NDO functions). But there is still a problem of validating these conversions with testing.
Unfortunately I don't have an e1000 card available to validate this conversion. I'll let you and Jakub decide what will be the best strategy here.

Quoted 32 lines

Or do you want netdevsim to cover hardware timestamping from the beginning too, Jakub?

Quoted 28 lines
drivers/net/ethernet/intel/e1000e/netdev.c +0 -0
  /**
drivers/net/netdevsim/netdev.c +18 -0
index 35fa1ca98671..502715c6e9e1 100644
@@ -238,6 +238,30 @@ nsim_set_features(struct net_device *dev, netdev_features_t features)
       return 0;
  }
 
+static int
+nsim_hwtstamp_get(struct net_device *dev,
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,
+               struct netlink_ext_ack *extack)
+{
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(config, &ns->hw_tstamp_config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}
+
+static int
+nsim_hwtstamp_ges(struct net_device *dev,
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,
+               struct netlink_ext_ack *extack)
+{
Quoted 8 lines

drivers/net/netdevsim/netdev.c nsim_set_features(struct net_device *dev, netdev_features_t features) · line 263

Quoted 5 lines
drivers/net/netdevsim/netdev.c +5 -0
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(&ns->hw_tstamp_config, config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}

Please keep conversion patches separate from patches which add new functionality.

Let me split my monolithic patch into a stack of patches in the next version. e1000e and netdevsim should be definitely arranged as separate patches which could be reviewed and accepted/rejected independently.

Quoted 5 lines

Also, does the netdevsim portion even do something useful? Wouldn't you need to implement ethtool_ops :: get_ts_info() in order for user space to know that there is a PHC, and that the device supports hardware timestamping?

I was thinking of exposing hw timestamping settings to testfs, but the ethtool approach can be easier to implement. Let me fix it!

Quoted 51 lines
Quoted 51 lines
drivers/net/netdevsim/netdev.c +3 -0
+
  static const struct net_device_ops nsim_netdev_ops = {
       .ndo_start_xmit         = nsim_start_xmit,
       .ndo_set_rx_mode        = nsim_set_rx_mode,
@@ -256,6 +280,8 @@ static const struct net_device_ops nsim_netdev_ops = {
       .ndo_setup_tc           = nsim_setup_tc,
       .ndo_set_features       = nsim_set_features,
       .ndo_bpf                = nsim_bpf,
+     .ndo_hwtstamp_get       = nsim_hwtstamp_get,
+     .ndo_hwtstamp_set       = nsim_hwtstamp_get,
  };
 
  static const struct net_device_ops nsim_vf_netdev_ops = {
drivers/net/netdevsim/netdevsim.h +1 -0
index 7d8ed8d8df5c..c6efd2383552 100644
@@ -102,6 +102,7 @@ struct netdevsim {
       } udp_ports;
 
       struct nsim_ethtool ethtool;
+     struct hwtstamp_config hw_tstamp_config;
  };
 
  struct netdevsim *
include/linux/netdevice.h +10 -0
index c8c634091a65..078c9284930a 100644
@@ -57,6 +57,8 @@
  struct netpoll_info;
  struct device;
  struct ethtool_ops;
+struct hwtstamp_config;
+struct kernel_hwtstamp_config;
  struct phy_device;
  struct dsa_port;
  struct ip_tunnel_parm;
@@ -1411,6 +1413,17 @@ struct netdev_net_notifier {
   *   Get hardware timestamp based on normal/adjustable time or free running
   *   cycle counter. This function is required if physical clock supports a
   *   free running cycle counter.
+ *   int (*ndo_hwtstamp_get)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Get hardware timestamping parameters currently configured  for NIC
+ *   device.
+ *   int (*ndo_hwtstamp_set)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
Quoted 8 lines

include/linux/netdevice.h struct netdev_net_notifier { · line 1426

Quoted 3 lines
include/linux/netdevice.h +3 -0
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Set hardware timestamping parameters for NIC device.

I would expect that in the next version, you only pass struct kernel_hwtstamp_config * to these and not struct hwtstamp_config *, since the former was merged in a form that contains all that struct hwtstamp_config does, right?

Yes, I guess I'll just follow your lead and use the kernel_hwtstamp_config you defined to pass parameters to ndo_hwtstamp_get/set. Jakub, do you have any objections here?

Quoted 45 lines
Quoted 31 lines
include/linux/netdevice.h +8 -0
   */
  struct net_device_ops {
       int                     (*ndo_init)(struct net_device *dev);
@@ -1645,6 +1658,14 @@ struct net_device_ops {
       ktime_t                 (*ndo_get_tstamp)(struct net_device *dev,
                                                 const struct skb_shared_hwtstamps *hwtstamps,
                                                 bool cycles);
+     int                     (*ndo_hwtstamp_get)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
+     int                     (*ndo_hwtstamp_set)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
  };
 
  struct xdp_metadata_ops {
include/uapi/linux/net_tstamp.h +4 -0
index a2c66b3d7f0f..547f73beb479 100644
@@ -79,6 +79,21 @@ struct hwtstamp_config {
       int rx_filter;
  };
 
+/**
+ * struct kernel_hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
+ *
+ * @dummy:   a placeholder field added to work around empty struct language

include/uapi/linux/net_tstamp.h struct hwtstamp_config { · line 95

Quoted 10 lines
include/uapi/linux/net_tstamp.h +10 -0
+ *           restriction
+ *
+ * This structure passed as a parameter to NDO methods called in
+ * response to SIOCGHWTSTAMP and SIOCSHWTSTAMP IOCTLs.
+ * The structure is effectively empty for now. Before adding new fields
+ * to the structure "dummy" placeholder field should be removed.
+ */
+struct kernel_hwtstamp_config {
+     u8 dummy;
+};

In include/uapi? No-no. That's exported to user space, which contradicts the whole point of having a structure visible just to the kernel.

Quoted 2 lines

See net-next commit c4bffeaa8d50 ("net: add struct kernel_hwtstamp_config and make net_hwtstamp_validate() use it") by the way.

Yes, I realized that I touched the wrong file when I was reading through your patch stack. You already defined kernel_hwtstamp_config, so my changes to include/uapi/linux/net_tstamp.h should be reverted anyway.

Quoted 90 lines
Quoted 90 lines
include/uapi/linux/net_tstamp.h +1 -0
+
  /* possible values for hwtstamp_config->flags */
  enum hwtstamp_flags {
       /*
net/core/dev_ioctl.c +54 -8
index 846669426236..c145afb3f77e 100644
@@ -183,22 +183,18 @@ static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cm
       return err;
  }
 
-static int net_hwtstamp_validate(struct ifreq *ifr)
+static int net_hwtstamp_validate(struct hwtstamp_config *cfg)
  {
-     struct hwtstamp_config cfg;
       enum hwtstamp_tx_types tx_type;
       enum hwtstamp_rx_filters rx_filter;
       int tx_type_valid = 0;
       int rx_filter_valid = 0;
 
-     if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-             return -EFAULT;
-
-     if (cfg.flags & ~HWTSTAMP_FLAG_MASK)
+     if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
               return -EINVAL;
 
-     tx_type = cfg.tx_type;
-     rx_filter = cfg.rx_filter;
+     tx_type = cfg->tx_type;
+     rx_filter = cfg->rx_filter;
 
       switch (tx_type) {
       case HWTSTAMP_TX_OFF:
@@ -277,6 +273,63 @@ static int dev_siocbond(struct net_device *dev,
       return -EOPNOTSUPP;
  }
 
+static int dev_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_get)
+             return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_get(dev, &config, NULL, NULL);
+     if (err)
+             return err;
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
+static int dev_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
+             return -EFAULT;
+
+     err = net_hwtstamp_validate(&config);
+     if (err)
+             return err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_set)
+             return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_set(dev, &config, NULL, NULL);
+     if (err)
Quoted 8 lines

net/core/dev_ioctl.c static int dev_siocbond(struct net_device *dev, · line 326

Quoted 1 line
net/core/dev_ioctl.c +1 -0
+             return err;

Here, when you rebase over commit 4ee58e1e5680 ("net: promote SIOCSHWTSTAMP and SIOCGHWTSTAMP ioctls to dedicated handlers"),
I expect that you will add another helper function to include/linux/net_tstamp.h called hwtstamp_config_from_kernel(), which translates back from the struct kernel_hwtstamp_config into something on which copy_from_user() can be called, correct?

Yes, that's what I'm planning to do as part of rebasing this patch.

Quoted 40 lines
Quoted 40 lines
net/core/dev_ioctl.c +11 -8
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
  static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
                             void __user *data, unsigned int cmd)
  {
@@ -391,11 +444,11 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
               rtnl_lock();
               return err;
 
+     case SIOCGHWTSTAMP:
+             return dev_hwtstamp_get(dev, ifr);
+
       case SIOCSHWTSTAMP:
-             err = net_hwtstamp_validate(ifr);
-             if (err)
-                     return err;
-             fallthrough;
+             return dev_hwtstamp_set(dev, ifr);
 
       /*
        *      Unknown or private ioctl
@@ -407,9 +460,7 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
 
               if (cmd == SIOCGMIIPHY ||
                   cmd == SIOCGMIIREG ||
-                 cmd == SIOCSMIIREG ||
-                 cmd == SIOCSHWTSTAMP ||
-                 cmd == SIOCGHWTSTAMP) {
+                 cmd == SIOCSMIIREG) {
                       err = dev_eth_ioctl(dev, ifr, cmd);
               } else if (cmd == SIOCBONDENSLAVE ||
                   cmd == SIOCBONDRELEASE ||
--

2.39.2

maintainer
#14 lore raw
In reply to Vladimir Oltean
Quoted 13 lines · On Mon, 3 Apr 2023 15:26:22 +0300 Vladimir Oltean wrote:

drivers/net/ethernet/intel/e1000e/netdev.c static const struct net_device_ops e1000e_netdev_ops = { · line 7369

Quoted 7 lines
drivers/net/ethernet/intel/e1000e/netdev.c +2 -0
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
 	.ndo_set_features = e1000_set_features,
 	.ndo_fix_features = e1000_fix_features,
 	.ndo_features_check	= passthru_features_check,
+	.ndo_hwtstamp_get	= e1000e_hwtstamp_get,
+	.ndo_hwtstamp_set	= e1000e_hwtstamp_set,
 };  

The conversion per se looks almost in line with what I was expecting to see, except for the comments. I guess you can convert a single driver first (e1000 seems fine), to get the API merged, then more people could work in parallel?

Quoted 2 lines

Or do you want netdevsim to cover hardware timestamping from the beginning too, Jakub?

I'd vote to split netdevsim out, and it needs a selftest which exercises it under tools/testing/selftests/drivers/net/netdevsim/ to get merged...

#8 lore raw
In reply to Vadim Fedorenko
Quoted 28 lines · On Mon, 3 Apr 2023 18:18:18 +0100 Vadim Fedorenko <[email protected]>…
Quoted 19 lines · Max Georgiev
Quoted 17 lines · Vladimir Oltean
Quoted 14 lines · Maxim Georgiev

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

This patch is sent out as RFC.
It still pending on basic testing.

Quoted 7 lines
Quoted 2 lines

Who should do that testing? Do you have any NIC with the hardware timestamping capability?

I'm planning to do the testing with netdevsim. I don't have access to any NICs with
hardware timestamping support.

Quoted 5 lines

Hi Max!
I might do some manual tests with the hardware that support timestamping once you respin the series with the changes discussed in the code comments. I'll convert hardware drivers myself to support new NDO on top of your patches.

Same here.

Köry,

#10 lore raw
In reply to Max Georgiev
Quoted 10 lines · Max Georgiev
Quoted 4 lines

The conversion per se looks almost in line with what I was expecting to see, except for the comments. I guess you can convert a single driver first (e1000 seems fine), to get the API merged, then more people could work in parallel?

The conversions are going to be easy (that was the point of adding these NDO functions). But there is still a problem of validating these conversions with testing.
Unfortunately I don't have an e1000 card available to validate this conversion. I'll let you and Jakub decide what will be the best strategy here.

If you can convert one of the drivers under drivers/net/ethernet/freescale/ with the exception of fec_main.c, or net/dsa/ + drivers/net/dsa/sja1105/ or drivers/net/dsa/mv88e6xxx/, then I can help with testing.

By the way, after inspecting the kernel code a bit more, it also looks like we need to convert vlan_dev_ioctl() and bond_eth_ioctl() to something compatible with lower interfaces using the new and the old API, before converting any driver. Otherwise we'll need to do that later anyway, when regression reports start coming in. So these 2 are non-optional whatever you do.

maintainer
#11 lore raw
Quoted 11 lines · On Tue, 4 Apr 2023 15:30:15 +0300 Vladimir Oltean wrote:
Quoted 5 lines · Max Georgiev

The conversions are going to be easy (that was the point of adding these NDO functions). But there is still a problem of validating these conversions with testing.
Unfortunately I don't have an e1000 card available to validate this conversion. I'll let you and Jakub decide what will be the best strategy here.

If you can convert one of the drivers under drivers/net/ethernet/freescale/ with the exception of fec_main.c, or net/dsa/ + drivers/net/dsa/sja1105/ or drivers/net/dsa/mv88e6xxx/, then I can help with testing.

Quoted 6 lines

By the way, after inspecting the kernel code a bit more, it also looks like we need to convert vlan_dev_ioctl() and bond_eth_ioctl() to something compatible with lower interfaces using the new and the old API, before converting any driver. Otherwise we'll need to do that later anyway, when regression reports start coming in. So these 2 are non-optional whatever you do.

Alternatively we could have them call back to the core helper to descend to a lower device. Stuff ifr into struct kernel_hwtstamp_config for now in case lower is not converted.

#9 lore raw
In reply to Vadim Fedorenko

Hi Vadim,

I'd greatly appreciate that!
I'll keep you in the loop.

Regards!
Maxim

Quoted 492 lines · Vadim Fedorenko
Quoted 18 lines · Max Georgiev
Quoted 17 lines · Vladimir Oltean
Quoted 14 lines · Maxim Georgiev

Current NIC driver API demands drivers supporting hardware timestamping to support SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTLs. Handling these IOCTLs requires dirivers to implement request parameter structure translation between user and kernel address spaces, handling possible translation failures, etc. This translation code is pretty much identical across most of the NIC drivers that support SIOCGHWTSTAMP/ SIOCSHWTSTAMP.
This patch extends NDO functiuon set with ndo_hwtstamp_get/set functions, implements SIOCGHWTSTAMP/SIOCSHWTSTAMP IOCTL translation to ndo_hwtstamp_get/set function calls including parameter structure translation and translation error handling.

This patch is sent out as RFC.
It still pending on basic testing.

Quoted 7 lines
Quoted 2 lines

Who should do that testing? Do you have any NIC with the hardware timestamping capability?

I'm planning to do the testing with netdevsim. I don't have access to any NICs with
hardware timestamping support.

Hi Max!
I might do some manual tests with the hardware that support timestamping once you respin the series with the changes discussed in the code comments. I'll convert hardware drivers myself to support new NDO on top of your patches.

Quoted 457 lines
Quoted 7 lines
Quoted 4 lines

Implementing ndo_hwtstamp_get/set in netdevsim driver should allow manual testing of the request translation logic. Also is there a way to automate this testing?

Automatic testing would require manual testing as a first step, to iron out the procedure, right?

Yes, absolutely.

Quoted 5 lines

The netdevsim driver should be testable by anyone using 'echo "0 1" > /sys/bus/netdevsim/new_device', and then 'hwstamp_ctl -i eni0np1 -t 1' (or 'testptp', or 'ptp4l', or whatever). Have you tried doing at least that, did it work?

Let me rebase my patch on top of your changes. I'll test it on netdevsim then.

Quoted 112 lines
Quoted 112 lines

---
Changes in v2:
- Introduced kernel_hwtstamp_config structure
- Added netlink_ext_ack* and kernel_hwtstamp_config* as NDO hw timestamp function parameters
- Reodered function variable declarations in dev_hwtstamp()
- Refactored error handling logic in dev_hwtstamp()
- Split dev_hwtstamp() into GET and SET versions
- Changed net_hwtstamp_validate() to accept struct hwtstamp_config * as a parameter

---
  drivers/net/ethernet/intel/e1000e/netdev.c | 39 ++++++-----
  drivers/net/netdevsim/netdev.c             | 26 +++++++
  drivers/net/netdevsim/netdevsim.h          |  1 +
  include/linux/netdevice.h                  | 21 ++++++
  include/uapi/linux/net_tstamp.h            | 15 ++++
  net/core/dev_ioctl.c                       | 81 ++++++++++++++++++----
  6 files changed, 149 insertions(+), 34 deletions(-)
drivers/net/ethernet/intel/e1000e/netdev.c +18 -19
index 6f5c16aebcbf..5b98f7257c77 100644
@@ -6161,7 +6161,9 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  /**
   * e1000e_hwtstamp_set - control hardware time stamping
   * @netdev: network interface device structure
- * @ifr: interface request
+ * @config: hwtstamp_config structure containing request parameters
+ * @kernel_config: kernel version of config parameter structure.
+ * @extack: netlink request parameters.
   *
   * Outgoing time stamping can be enabled and disabled. Play nice and
   * disable it when requested, although it shouldn't cause any overhead
@@ -6174,20 +6176,19 @@ static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
   * specified. Matching the kind of event packet is not supported, with the
   * exception of "all V2 events regardless of level 2 or 4".
   **/
-static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_set(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
  {
       struct e1000_adapter *adapter = netdev_priv(netdev);
-     struct hwtstamp_config config;
       int ret_val;
 
-     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
-             return -EFAULT;
-
-     ret_val = e1000e_config_hwtstamp(adapter, &config);
+     ret_val = e1000e_config_hwtstamp(adapter, config);
       if (ret_val)
               return ret_val;
 
-     switch (config.rx_filter) {
+     switch (config->rx_filter) {
       case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
       case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
       case HWTSTAMP_FILTER_PTP_V2_SYNC:
@@ -6199,22 +6200,24 @@ static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
                * by hardware so notify the caller the requested packets plus
                * some others are time stamped.
                */
-             config.rx_filter = HWTSTAMP_FILTER_SOME;
+             config->rx_filter = HWTSTAMP_FILTER_SOME;
               break;
       default:
               break;
       }
-
-     return copy_to_user(ifr->ifr_data, &config,
-                         sizeof(config)) ? -EFAULT : 0;
+     return ret_val;
  }
 
-static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
+static int e1000e_hwtstamp_get(struct net_device *netdev,
+                            struct hwtstamp_config *config,
+                            struct kernel_hwtstamp_config *kernel_config,
+                            struct netlink_ext_ack *extack)
  {
       struct e1000_adapter *adapter = netdev_priv(netdev);
 
-     return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
-                         sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
+     memcpy(config, &adapter->hwtstamp_config,
+            sizeof(adapter->hwtstamp_config));
+     return 0;
  }
 
  static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
@@ -6224,10 +6227,6 @@ static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
       case SIOCGMIIREG:
       case SIOCSMIIREG:
               return e1000_mii_ioctl(netdev, ifr, cmd);
-     case SIOCSHWTSTAMP:
-             return e1000e_hwtstamp_set(netdev, ifr);
-     case SIOCGHWTSTAMP:
-             return e1000e_hwtstamp_get(netdev, ifr);
       default:
               return -EOPNOTSUPP;
       }
@@ -7365,6 +7364,8 @@ static const struct net_device_ops e1000e_netdev_ops = {
       .ndo_set_features = e1000_set_features,
       .ndo_fix_features = e1000_fix_features,
       .ndo_features_check     = passthru_features_check,
Quoted 8 lines

drivers/net/ethernet/intel/e1000e/netdev.c static const struct net_device_ops e1000e_netdev_ops = { · line 7369

Quoted 3 lines
drivers/net/ethernet/intel/e1000e/netdev.c +2 -0
+     .ndo_hwtstamp_get       = e1000e_hwtstamp_get,
+     .ndo_hwtstamp_set       = e1000e_hwtstamp_set,
  };

The conversion per se looks almost in line with what I was expecting to see, except for the comments. I guess you can convert a single driver first (e1000 seems fine), to get the API merged, then more people could work in parallel?

The conversions are going to be easy (that was the point of adding these NDO functions). But there is still a problem of validating these conversions with testing.
Unfortunately I don't have an e1000 card available to validate this conversion. I'll let you and Jakub decide what will be the best strategy here.

Quoted 32 lines

Or do you want netdevsim to cover hardware timestamping from the beginning too, Jakub?

Quoted 28 lines
drivers/net/ethernet/intel/e1000e/netdev.c +0 -0
  /**
drivers/net/netdevsim/netdev.c +18 -0
index 35fa1ca98671..502715c6e9e1 100644
@@ -238,6 +238,30 @@ nsim_set_features(struct net_device *dev, netdev_features_t features)
       return 0;
  }
 
+static int
+nsim_hwtstamp_get(struct net_device *dev,
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,
+               struct netlink_ext_ack *extack)
+{
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(config, &ns->hw_tstamp_config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}
+
+static int
+nsim_hwtstamp_ges(struct net_device *dev,
+               struct hwtstamp_config *config,
+               struct kernel_hwtstamp_config *kernel_config,
+               struct netlink_ext_ack *extack)
+{
Quoted 8 lines

drivers/net/netdevsim/netdev.c nsim_set_features(struct net_device *dev, netdev_features_t features) · line 263

Quoted 5 lines
drivers/net/netdevsim/netdev.c +5 -0
+     struct netdevsim *ns = netdev_priv(dev);
+
+     memcpy(&ns->hw_tstamp_config, config, sizeof(ns->hw_tstamp_config));
+     return 0;
+}

Please keep conversion patches separate from patches which add new functionality.

Let me split my monolithic patch into a stack of patches in the next version. e1000e and netdevsim should be definitely arranged as separate patches which could be reviewed and accepted/rejected independently.

Quoted 5 lines

Also, does the netdevsim portion even do something useful? Wouldn't you need to implement ethtool_ops :: get_ts_info() in order for user space to know that there is a PHC, and that the device supports hardware timestamping?

I was thinking of exposing hw timestamping settings to testfs, but the ethtool approach can be easier to implement. Let me fix it!

Quoted 51 lines
Quoted 51 lines
drivers/net/netdevsim/netdev.c +3 -0
+
  static const struct net_device_ops nsim_netdev_ops = {
       .ndo_start_xmit         = nsim_start_xmit,
       .ndo_set_rx_mode        = nsim_set_rx_mode,
@@ -256,6 +280,8 @@ static const struct net_device_ops nsim_netdev_ops = {
       .ndo_setup_tc           = nsim_setup_tc,
       .ndo_set_features       = nsim_set_features,
       .ndo_bpf                = nsim_bpf,
+     .ndo_hwtstamp_get       = nsim_hwtstamp_get,
+     .ndo_hwtstamp_set       = nsim_hwtstamp_get,
  };
 
  static const struct net_device_ops nsim_vf_netdev_ops = {
drivers/net/netdevsim/netdevsim.h +1 -0
index 7d8ed8d8df5c..c6efd2383552 100644
@@ -102,6 +102,7 @@ struct netdevsim {
       } udp_ports;
 
       struct nsim_ethtool ethtool;
+     struct hwtstamp_config hw_tstamp_config;
  };
 
  struct netdevsim *
include/linux/netdevice.h +10 -0
index c8c634091a65..078c9284930a 100644
@@ -57,6 +57,8 @@
  struct netpoll_info;
  struct device;
  struct ethtool_ops;
+struct hwtstamp_config;
+struct kernel_hwtstamp_config;
  struct phy_device;
  struct dsa_port;
  struct ip_tunnel_parm;
@@ -1411,6 +1413,17 @@ struct netdev_net_notifier {
   *   Get hardware timestamp based on normal/adjustable time or free running
   *   cycle counter. This function is required if physical clock supports a
   *   free running cycle counter.
+ *   int (*ndo_hwtstamp_get)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Get hardware timestamping parameters currently configured  for NIC
+ *   device.
+ *   int (*ndo_hwtstamp_set)(struct net_device *dev,
+ *                           struct hwtstamp_config *config,
Quoted 8 lines

include/linux/netdevice.h struct netdev_net_notifier { · line 1426

Quoted 3 lines
include/linux/netdevice.h +3 -0
+ *                           struct kernel_hwtstamp_config *kernel_config,
+ *                           struct netlink_ext_ack *extack);
+ *   Set hardware timestamping parameters for NIC device.

I would expect that in the next version, you only pass struct kernel_hwtstamp_config * to these and not struct hwtstamp_config *, since the former was merged in a form that contains all that struct hwtstamp_config does, right?

Yes, I guess I'll just follow your lead and use the kernel_hwtstamp_config you defined to pass parameters to ndo_hwtstamp_get/set. Jakub, do you have any objections here?

Quoted 45 lines
Quoted 31 lines
include/linux/netdevice.h +8 -0
   */
  struct net_device_ops {
       int                     (*ndo_init)(struct net_device *dev);
@@ -1645,6 +1658,14 @@ struct net_device_ops {
       ktime_t                 (*ndo_get_tstamp)(struct net_device *dev,
                                                 const struct skb_shared_hwtstamps *hwtstamps,
                                                 bool cycles);
+     int                     (*ndo_hwtstamp_get)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
+     int                     (*ndo_hwtstamp_set)(struct net_device *dev,
+                                                 struct hwtstamp_config *config,
+                                                 struct kernel_hwtstamp_config *kernel_config,
+                                                 struct netlink_ext_ack *extack);
  };
 
  struct xdp_metadata_ops {
include/uapi/linux/net_tstamp.h +4 -0
index a2c66b3d7f0f..547f73beb479 100644
@@ -79,6 +79,21 @@ struct hwtstamp_config {
       int rx_filter;
  };
 
+/**
+ * struct kernel_hwtstamp_config - %SIOCGHWTSTAMP and %SIOCSHWTSTAMP parameter
+ *
+ * @dummy:   a placeholder field added to work around empty struct language

include/uapi/linux/net_tstamp.h struct hwtstamp_config { · line 95

Quoted 10 lines
include/uapi/linux/net_tstamp.h +10 -0
+ *           restriction
+ *
+ * This structure passed as a parameter to NDO methods called in
+ * response to SIOCGHWTSTAMP and SIOCSHWTSTAMP IOCTLs.
+ * The structure is effectively empty for now. Before adding new fields
+ * to the structure "dummy" placeholder field should be removed.
+ */
+struct kernel_hwtstamp_config {
+     u8 dummy;
+};

In include/uapi? No-no. That's exported to user space, which contradicts the whole point of having a structure visible just to the kernel.

Quoted 2 lines

See net-next commit c4bffeaa8d50 ("net: add struct kernel_hwtstamp_config and make net_hwtstamp_validate() use it") by the way.

Yes, I realized that I touched the wrong file when I was reading through your patch stack. You already defined kernel_hwtstamp_config, so my changes to include/uapi/linux/net_tstamp.h should be reverted anyway.

Quoted 90 lines
Quoted 90 lines
include/uapi/linux/net_tstamp.h +1 -0
+
  /* possible values for hwtstamp_config->flags */
  enum hwtstamp_flags {
       /*
net/core/dev_ioctl.c +54 -8
index 846669426236..c145afb3f77e 100644
@@ -183,22 +183,18 @@ static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cm
       return err;
  }
 
-static int net_hwtstamp_validate(struct ifreq *ifr)
+static int net_hwtstamp_validate(struct hwtstamp_config *cfg)
  {
-     struct hwtstamp_config cfg;
       enum hwtstamp_tx_types tx_type;
       enum hwtstamp_rx_filters rx_filter;
       int tx_type_valid = 0;
       int rx_filter_valid = 0;
 
-     if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
-             return -EFAULT;
-
-     if (cfg.flags & ~HWTSTAMP_FLAG_MASK)
+     if (cfg->flags & ~HWTSTAMP_FLAG_MASK)
               return -EINVAL;
 
-     tx_type = cfg.tx_type;
-     rx_filter = cfg.rx_filter;
+     tx_type = cfg->tx_type;
+     rx_filter = cfg->rx_filter;
 
       switch (tx_type) {
       case HWTSTAMP_TX_OFF:
@@ -277,6 +273,63 @@ static int dev_siocbond(struct net_device *dev,
       return -EOPNOTSUPP;
  }
 
+static int dev_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_get)
+             return dev_eth_ioctl(dev, ifr, SIOCGHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_get(dev, &config, NULL, NULL);
+     if (err)
+             return err;
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
+static int dev_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
+{
+     const struct net_device_ops *ops = dev->netdev_ops;
+     struct hwtstamp_config config;
+     int err;
+
+     if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
+             return -EFAULT;
+
+     err = net_hwtstamp_validate(&config);
+     if (err)
+             return err;
+
+     err = dsa_ndo_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+     if (err == 0 || err != -EOPNOTSUPP)
+             return err;
+
+     if (!ops->ndo_hwtstamp_set)
+             return dev_eth_ioctl(dev, ifr, SIOCSHWTSTAMP);
+
+     if (!netif_device_present(dev))
+             return -ENODEV;
+
+     err = ops->ndo_hwtstamp_set(dev, &config, NULL, NULL);
+     if (err)
Quoted 8 lines

net/core/dev_ioctl.c static int dev_siocbond(struct net_device *dev, · line 326

Quoted 1 line
net/core/dev_ioctl.c +1 -0
+             return err;

Here, when you rebase over commit 4ee58e1e5680 ("net: promote SIOCSHWTSTAMP and SIOCGHWTSTAMP ioctls to dedicated handlers"),
I expect that you will add another helper function to include/linux/net_tstamp.h called hwtstamp_config_from_kernel(), which translates back from the struct kernel_hwtstamp_config into something on which copy_from_user() can be called, correct?

Yes, that's what I'm planning to do as part of rebasing this patch.

Quoted 40 lines
Quoted 40 lines
net/core/dev_ioctl.c +11 -8
+
+     if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
+             return -EFAULT;
+     return 0;
+}
+
  static int dev_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
                             void __user *data, unsigned int cmd)
  {
@@ -391,11 +444,11 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
               rtnl_lock();
               return err;
 
+     case SIOCGHWTSTAMP:
+             return dev_hwtstamp_get(dev, ifr);
+
       case SIOCSHWTSTAMP:
-             err = net_hwtstamp_validate(ifr);
-             if (err)
-                     return err;
-             fallthrough;
+             return dev_hwtstamp_set(dev, ifr);
 
       /*
        *      Unknown or private ioctl
@@ -407,9 +460,7 @@ static int dev_ifsioc(struct net *net, struct ifreq *ifr, void __user *data,
 
               if (cmd == SIOCGMIIPHY ||
                   cmd == SIOCGMIIREG ||
-                 cmd == SIOCSMIIREG ||
-                 cmd == SIOCSHWTSTAMP ||
-                 cmd == SIOCGHWTSTAMP) {
+                 cmd == SIOCSMIIREG) {
                       err = dev_eth_ioctl(dev, ifr, cmd);
               } else if (cmd == SIOCBONDENSLAVE ||
                   cmd == SIOCBONDRELEASE ||
--

2.39.2

#12 lore raw
In reply to Jakub Kicinski

Thank you for the comments!

I've sent out v3 of the RFC stack to let the comment flow, and to potentially unblock Köry.
I've added patches to insert ifr pointer into struct kernel_hwtstamp_config as Jakub suggested, but no code is relying on this new field yet.
I also added a patch which is supposed to fix the VLAN code path. I can implement ndo_hwtstamp_get/set in VLAN as well - please comment if it would be a good idea.
I haven't fixed bond_eth_ioctl() yet - will add it in the next version. More driver conversion patches are planned as was discussed.

Quoted 23 lines · Jakub Kicinski
Quoted 11 lines · On Tue, 4 Apr 2023 15:30:15 +0300 Vladimir Oltean wrote:
Quoted 5 lines · Max Georgiev

The conversions are going to be easy (that was the point of adding these NDO functions). But there is still a problem of validating these conversions with testing.
Unfortunately I don't have an e1000 card available to validate this conversion. I'll let you and Jakub decide what will be the best strategy here.

If you can convert one of the drivers under drivers/net/ethernet/freescale/ with the exception of fec_main.c, or net/dsa/ + drivers/net/dsa/sja1105/ or drivers/net/dsa/mv88e6xxx/, then I can help with testing.

Quoted 6 lines

By the way, after inspecting the kernel code a bit more, it also looks like we need to convert vlan_dev_ioctl() and bond_eth_ioctl() to something compatible with lower interfaces using the new and the old API, before converting any driver. Otherwise we'll need to do that later anyway, when regression reports start coming in. So these 2 are non-optional whatever you do.

Alternatively we could have them call back to the core helper to descend to a lower device. Stuff ifr into struct kernel_hwtstamp_config for now in case lower is not converted.

#13 lore raw
Quoted 12 lines · Max Georgiev

Thank you for the comments!

I've sent out v3 of the RFC stack to let the comment flow, and to potentially unblock Köry.
I've added patches to insert ifr pointer into struct kernel_hwtstamp_config as Jakub suggested, but no code is relying on this new field yet.
I also added a patch which is supposed to fix the VLAN code path. I can implement ndo_hwtstamp_get/set in VLAN as well - please comment if it would be a good idea.
I haven't fixed bond_eth_ioctl() yet - will add it in the next version. More driver conversion patches are planned as was discussed.

macvlan_eth_ioctl() needs changing too.