Skip to contentSkip to conversation

Support ocelot PTP Sync one-step timestamping

PATCH Superseded Yangbo Lu 5 participants 17 messages Apr 16, 2021 – Apr 20, 2021

Subsystems (9)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yangbo Lu

author
#0 lore raw

cover Support ocelot PTP Sync one-step timestamping

This patch-set is to support ocelot PTP Sync one-step timestamping. Actually before that, this patch-set cleans up and optimizes the DSA slave tx timestamp request handling process.

Yangbo Lu (3):
net: dsa: optimize tx timestamp request handling
net: mscc: ocelot: convert to ocelot_port_txtstamp_request()
net: mscc: ocelot: support PTP Sync one-step timestamping

 Documentation/networking/timestamping.rst     |  7 +-
 .../net/dsa/hirschmann/hellcreek_hwtstamp.c   | 20 +++--
 .../net/dsa/hirschmann/hellcreek_hwtstamp.h   |  2 +-
 drivers/net/dsa/mv88e6xxx/hwtstamp.c          | 21 +++--
 drivers/net/dsa/mv88e6xxx/hwtstamp.h          |  6 +-
 drivers/net/dsa/ocelot/felix.c                | 15 ++--
 drivers/net/dsa/sja1105/sja1105_ptp.c         |  6 +-
 drivers/net/dsa/sja1105/sja1105_ptp.h         |  2 +-
 drivers/net/ethernet/mscc/ocelot.c            | 81 ++++++++++++++++++-
 drivers/net/ethernet/mscc/ocelot_net.c        | 19 ++---
 include/net/dsa.h                             |  2 +-
 include/soc/mscc/ocelot.h                     |  6 +-
 net/dsa/slave.c                               | 20 ++---
 net/dsa/tag_ocelot.c                          | 25 +-----
 net/dsa/tag_ocelot_8021q.c                    | 39 +++------
 15 files changed, 158 insertions(+), 113 deletions(-)

base-commit: 392c36e5be1dee19ffce8c8ba8f07f90f5aa3f7c

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/3 net: dsa: optimize tx timestamp request handling

Optimization could be done on dsa_skb_tx_timestamp(), and dsa device drivers should adapt to it.

- Check SKBTX_HW_TSTAMP request flag at the very beginning, instead of in port_txtstamp, so that most skbs not requiring tx timestamp just return.

- No longer to identify PTP packets, and limit tx timestamping only for PTP packets. If device driver likes, let device driver do.

- It is a waste to clone skb directly in dsa_skb_tx_timestamp(). For one-step timestamping, a clone is not needed. For any failure of port_txtstamp (this may usually happen), the skb clone has to be freed. So put skb cloning into port_txtstamp where it really needs.

---
 Documentation/networking/timestamping.rst     |  7 +++++--
 .../net/dsa/hirschmann/hellcreek_hwtstamp.c   | 20 ++++++++++++------
 .../net/dsa/hirschmann/hellcreek_hwtstamp.h   |  2 +-
 drivers/net/dsa/mv88e6xxx/hwtstamp.c          | 21 +++++++++++++------
 drivers/net/dsa/mv88e6xxx/hwtstamp.h          |  6 +++---
 drivers/net/dsa/ocelot/felix.c                | 11 ++++++----
 drivers/net/dsa/sja1105/sja1105_ptp.c         |  6 +++++-
 drivers/net/dsa/sja1105/sja1105_ptp.h         |  2 +-
 include/net/dsa.h                             |  2 +-
 net/dsa/slave.c                               | 20 +++++-------------
 10 files changed, 57 insertions(+), 40 deletions(-)
Documentation/networking/timestamping.rst +5 -2
index f682e88fa87e..7f04a699a5d1 100644
@@ -635,8 +635,8 @@ in generic code: a BPF classifier (``ptp_classify_raw``) is used to identify
 PTP event messages (any other packets, including PTP general messages, are not
 timestamped), and provides two hooks to drivers:
 
-- ``.port_txtstamp()``: The driver is passed a clone of the timestampable skb
-  to be transmitted, before actually transmitting it. Typically, a switch will
+- ``.port_txtstamp()``: A clone of the timestampable skb to be transmitted
+  is needed, before actually transmitting it. Typically, a switch will
   have a PTP TX timestamp register (or sometimes a FIFO) where the timestamp
   becomes available. There may be an IRQ that is raised upon this timestamp's
   availability, or the driver might have to poll after invoking
@@ -645,6 +645,9 @@ timestamped), and provides two hooks to drivers:
   later use (when the timestamp becomes available). Each skb is annotated with
   a pointer to its clone, in ``DSA_SKB_CB(skb)->clone``, to ease the driver's
   job of keeping track of which clone belongs to which skb.
+  But one-step timestamping request is handled differently with above two-step
+  timestamping. The skb clone is no longer needed since hardware will insert
+  TX time information on packet during egress.
 
 - ``.port_rxtstamp()``: The original (and only) timestampable skb is provided
   to the driver, for it to annotate it with a timestamp, if that is immediately
drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c +14 -6
index 69dd9a2e8bb6..2ff4b7c08b72 100644
@@ -374,31 +374,39 @@ long hellcreek_hwtstamp_work(struct ptp_clock_info *ptp)
 }
 
 bool hellcreek_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type)
+			     struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct hellcreek *hellcreek = ds->priv;
 	struct hellcreek_port_hwtstamp *ps;
 	struct ptp_header *hdr;
+	unsigned int type;
 
 	ps = &hellcreek->ports[port].port_hwtstamp;
 
-	/* Check if the driver is expected to do HW timestamping */
-	if (!(skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP))
+	type = ptp_classify_raw(skb);
+	if (type == PTP_CLASS_NONE)
 		return false;
 
 	/* Make sure the message is a PTP message that needs to be timestamped
 	 * and the interaction with the HW timestamping is enabled. If not, stop
 	 * here
 	 */
-	hdr = hellcreek_should_tstamp(hellcreek, port, clone, type);
+	hdr = hellcreek_should_tstamp(hellcreek, port, skb, type);
 	if (!hdr)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	if (test_and_set_bit_lock(HELLCREEK_HWTSTAMP_TX_IN_PROGRESS,
-				  &ps->state))
+				  &ps->state)) {
+		kfree_skb(*clone);
+		*clone = NULL;
 		return false;
+	}
 
-	ps->tx_skb = clone;
+	ps->tx_skb = *clone;
 
 	/* store the number of ticks occurred since system start-up till this
 	 * moment
drivers/net/dsa/hirschmann/hellcreek_hwtstamp.h +1 -1
index c0745ffa1ebb..58cc96642076 100644
@@ -45,7 +45,7 @@ int hellcreek_port_hwtstamp_get(struct dsa_switch *ds, int port,
 bool hellcreek_port_rxtstamp(struct dsa_switch *ds, int port,
 			     struct sk_buff *clone, unsigned int type);
 bool hellcreek_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type);
+			     struct sk_buff *skb, struct sk_buff **clone);
 
 int hellcreek_get_ts_info(struct dsa_switch *ds, int port,
 			  struct ethtool_ts_info *info);
drivers/net/dsa/mv88e6xxx/hwtstamp.c +15 -6
index 094d17a1d037..280a95962861 100644
@@ -469,24 +469,33 @@ long mv88e6xxx_hwtstamp_work(struct ptp_clock_info *ptp)
 }
 
 bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type)
+			     struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct mv88e6xxx_chip *chip = ds->priv;
 	struct mv88e6xxx_port_hwtstamp *ps = &chip->port_hwtstamp[port];
 	struct ptp_header *hdr;
+	unsigned int type;
 
-	if (!(skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP))
-		return false;
+	type = ptp_classify_raw(skb);
+	if (type == PTP_CLASS_NONE)
+		return false
 
-	hdr = mv88e6xxx_should_tstamp(chip, port, clone, type);
+	hdr = mv88e6xxx_should_tstamp(chip, port, skb, type);
 	if (!hdr)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	if (test_and_set_bit_lock(MV88E6XXX_HWTSTAMP_TX_IN_PROGRESS,
-				  &ps->state))
+				  &ps->state)) {
+		kfree_skb(*clone);
+		*clone = NULL;
 		return false;
+	}
 
-	ps->tx_skb = clone;
+	ps->tx_skb = *clone;
 	ps->tx_tstamp_start = jiffies;
 	ps->tx_seq_id = be16_to_cpu(hdr->sequence_id);
 
drivers/net/dsa/mv88e6xxx/hwtstamp.h +3 -3
index 9da9f197ba02..da2b253334d0 100644
@@ -118,7 +118,7 @@ int mv88e6xxx_port_hwtstamp_get(struct dsa_switch *ds, int port,
 bool mv88e6xxx_port_rxtstamp(struct dsa_switch *ds, int port,
 			     struct sk_buff *clone, unsigned int type);
 bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type);
+			     struct sk_buff *skb, struct sk_buff **clone);
 
 int mv88e6xxx_get_ts_info(struct dsa_switch *ds, int port,
 			  struct ethtool_ts_info *info);
@@ -152,8 +152,8 @@ static inline bool mv88e6xxx_port_rxtstamp(struct dsa_switch *ds, int port,
 }
 
 static inline bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-					   struct sk_buff *clone,
-					   unsigned int type)
+					   struct sk_buff *skb,
+					   struct sk_buff **clone)
 {
 	return false;
 }
drivers/net/dsa/ocelot/felix.c +7 -4
index 6b5442be0230..cdec2f5e271c 100644
@@ -1396,14 +1396,17 @@ static bool felix_rxtstamp(struct dsa_switch *ds, int port,
 }
 
 static bool felix_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *clone, unsigned int type)
+			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct ocelot *ocelot = ds->priv;
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
-	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
-	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
+	if (ocelot->ptp && ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
+		*clone = skb_clone_sk(skb);
+		if (!(*clone))
+			return false;
+
+		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
 		return true;
 	}
 
drivers/net/dsa/sja1105/sja1105_ptp.c +5 -1
index 1b90570b257b..6a1f854a8c33 100644
@@ -436,7 +436,7 @@ bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
  * callback, where we will timestamp it synchronously.
  */
 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *skb, unsigned int type)
+			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct sja1105_private *priv = ds->priv;
 	struct sja1105_port *sp = &priv->ports[port];
@@ -444,6 +444,10 @@ bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
 	if (!sp->hwts_tx_en)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	return true;
 }
 
drivers/net/dsa/sja1105/sja1105_ptp.h +1 -1
index 3daa33e98e77..ab80b73219cb 100644
@@ -105,7 +105,7 @@ bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
 			   struct sk_buff *skb, unsigned int type);
 
 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *skb, unsigned int type);
+			   struct sk_buff *skb, struct sk_buff **clone);
 
 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
 
include/net/dsa.h +1 -1
index 1259b0f40684..c8415c324e27 100644
@@ -734,7 +734,7 @@ struct dsa_switch_ops {
 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
 				     struct ifreq *ifr);
 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
-				 struct sk_buff *clone, unsigned int type);
+				 struct sk_buff *skb, struct sk_buff **clone);
 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
 				 struct sk_buff *skb, unsigned int type);
 
net/dsa/slave.c +5 -15
index 9300cb66e500..5b746a903ef4 100644
@@ -19,7 +19,6 @@
 #include <linux/if_bridge.h>
 #include <linux/if_hsr.h>
 #include <linux/netpoll.h>
-#include <linux/ptp_classify.h>
 
 #include "dsa_priv.h"
 
@@ -555,26 +554,19 @@ static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
 				 struct sk_buff *skb)
 {
 	struct dsa_switch *ds = p->dp->ds;
-	struct sk_buff *clone;
-	unsigned int type;
+	struct sk_buff *clone = NULL;
 
-	type = ptp_classify_raw(skb);
-	if (type == PTP_CLASS_NONE)
+	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
 		return;
 
 	if (!ds->ops->port_txtstamp)
 		return;
 
-	clone = skb_clone_sk(skb);
-	if (!clone)
+	if (!ds->ops->port_txtstamp(ds, p->dp->index, skb, &clone))
 		return;
 
-	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) {
+	if (clone)
 		DSA_SKB_CB(skb)->clone = clone;
-		return;
-	}
-
-	kfree_skb(clone);
 }
 
 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
@@ -628,9 +620,7 @@ static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
 
 	DSA_SKB_CB(skb)->clone = NULL;
 
-	/* Identify PTP protocol packets, clone them, and pass them to the
-	 * switch driver
-	 */
+	/* Handle tx timestamp request if has */
 	dsa_skb_tx_timestamp(p, skb);
 
 	if (dsa_realloc_skb(skb, dev)) {
Signature · 1 line
2.25.1
author
#10 lore raw

PATCH 2/3 net: mscc: ocelot: convert to ocelot_port_txtstamp_request()

In reply to Yangbo Lu

Convert to a common ocelot_port_txtstamp_request() for TX timestamp request handling.

---
 drivers/net/dsa/ocelot/felix.c         | 14 +++++---------
 drivers/net/ethernet/mscc/ocelot.c     | 24 +++++++++++++++++++++---
 drivers/net/ethernet/mscc/ocelot_net.c | 18 +++++++-----------
 include/soc/mscc/ocelot.h              |  5 +++--
 4 files changed, 36 insertions(+), 25 deletions(-)
drivers/net/dsa/ocelot/felix.c +5 -9
index cdec2f5e271c..5f2cf0f31253 100644
@@ -1399,18 +1399,14 @@ static bool felix_txtstamp(struct dsa_switch *ds, int port,
 			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct ocelot *ocelot = ds->priv;
-	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
-	if (ocelot->ptp && ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-		*clone = skb_clone_sk(skb);
-		if (!(*clone))
-			return false;
+	if (!ocelot->ptp)
+		return false;
 
-		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
-		return true;
-	}
+	if (ocelot_port_txtstamp_request(ocelot, port, skb, clone))
+		return false;
 
-	return false;
+	return true;
 }
 
 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
drivers/net/ethernet/mscc/ocelot.c +21 -3
index 8d06ffaf318a..541d3b4076be 100644
@@ -530,8 +530,8 @@ void ocelot_port_disable(struct ocelot *ocelot, int port)
 }
 EXPORT_SYMBOL(ocelot_port_disable);
 
-void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
-				  struct sk_buff *clone)
+static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
+					 struct sk_buff *clone)
 {
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
@@ -545,7 +545,25 @@ void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
 
 	spin_unlock(&ocelot_port->ts_id_lock);
 }
-EXPORT_SYMBOL(ocelot_port_add_txtstamp_skb);
+
+int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
+				 struct sk_buff *skb,
+				 struct sk_buff **clone)
+{
+	struct ocelot_port *ocelot_port = ocelot->ports[port];
+	u8 ptp_cmd = ocelot_port->ptp_cmd;
+
+	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
+		*clone = skb_clone_sk(skb);
+		if (!(*clone))
+			return -ENOMEM;
+
+		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(ocelot_port_txtstamp_request);
 
 static void ocelot_get_hwtimestamp(struct ocelot *ocelot,
 				   struct timespec64 *ts)
drivers/net/ethernet/mscc/ocelot_net.c +7 -11
index 36f32a4d9b0f..8293152a6dc1 100644
@@ -507,19 +507,15 @@ static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
 
 	/* Check if timestamping is needed */
 	if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
-		rew_op = ocelot_port->ptp_cmd;
+		struct sk_buff *clone;
 
-		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-			struct sk_buff *clone;
-
-			clone = skb_clone_sk(skb);
-			if (!clone) {
-				kfree_skb(skb);
-				return NETDEV_TX_OK;
-			}
-
-			ocelot_port_add_txtstamp_skb(ocelot, port, clone);
+		if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
+			kfree_skb(skb);
+			return NETDEV_TX_OK;
+		}
 
+		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
+			rew_op = ocelot_port->ptp_cmd;
 			rew_op |= clone->cb[0] << 3;
 		}
 	}
include/soc/mscc/ocelot.h +3 -2
index 68cdc7ceaf4d..9cdaf1d9199f 100644
@@ -820,8 +820,9 @@ int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid);
 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr);
 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr);
-void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
-				  struct sk_buff *clone);
+int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
+				 struct sk_buff *skb,
+				 struct sk_buff **clone);
 void ocelot_get_txtstamp(struct ocelot *ocelot);
 void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu);
 int ocelot_get_max_mtu(struct ocelot *ocelot, int port);
Signature · 1 line
2.25.1
author
#12 lore raw

PATCH 3/3 net: mscc: ocelot: support PTP Sync one-step timestamping

In reply to Yangbo Lu

Although HWTSTAMP_TX_ONESTEP_SYNC existed in ioctl for hardware timestamp configuration, the PTP Sync one-step timestamping had never been supported.

This patch is to truely support it. The hardware timestamp request type is stored in DSA_SKB_CB_PRIV first byte per skb, so that corresponding configuration could be done during transmitting. Non-onestep-Sync packet with one-step timestamp request should fall back to use two-step timestamp.

---
 drivers/net/ethernet/mscc/ocelot.c     | 57 ++++++++++++++++++++++++++
 drivers/net/ethernet/mscc/ocelot_net.c |  5 +--
 include/soc/mscc/ocelot.h              |  1 +
 net/dsa/tag_ocelot.c                   | 25 ++---------
 net/dsa/tag_ocelot_8021q.c             | 39 +++++-------------
 5 files changed, 72 insertions(+), 55 deletions(-)
drivers/net/ethernet/mscc/ocelot.c +57 -0
index 541d3b4076be..69d36b6241ff 100644
@@ -6,6 +6,7 @@
  */
 #include <linux/dsa/ocelot.h>
 #include <linux/if_bridge.h>
+#include <linux/ptp_classify.h>
 #include <soc/mscc/ocelot_vcap.h>
 #include "ocelot.h"
 #include "ocelot_vcap.h"
@@ -546,6 +547,50 @@ static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
 	spin_unlock(&ocelot_port->ts_id_lock);
 }
 
+bool ocelot_ptp_rew_op(struct sk_buff *skb, struct sk_buff *clone, u32 *rew_op)
+{
+	/* For two-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV
+	 * and timestamp ID in clone->cb[0].
+	 * For one-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV.
+	 */
+	u8 *ptp_cmd = DSA_SKB_CB_PRIV(skb);
+
+	if (clone) {
+		*rew_op = *ptp_cmd;
+		*rew_op |= clone->cb[0] << 3;
+	} else if (*ptp_cmd) {
+		*rew_op = *ptp_cmd;
+	} else {
+		return false;
+	}
+
+	return true;
+}
+EXPORT_SYMBOL(ocelot_ptp_rew_op);
+
+static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb)
+{
+	struct ptp_header *hdr;
+	unsigned int ptp_class;
+	u8 msgtype, twostep;
+
+	ptp_class = ptp_classify_raw(skb);
+	if (ptp_class == PTP_CLASS_NONE)
+		return false;
+
+	hdr = ptp_parse_header(skb, ptp_class);
+	if (!hdr)
+		return false;
+
+	msgtype = ptp_get_msgtype(hdr, ptp_class);
+	twostep = hdr->flag_field[0] & 0x2;
+
+	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
+		return true;
+
+	return false;
+}
+
 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 				 struct sk_buff *skb,
 				 struct sk_buff **clone)
@@ -553,12 +598,24 @@ int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 	u8 ptp_cmd = ocelot_port->ptp_cmd;
 
+	/* Store ptp_cmd in first byte of DSA_SKB_CB_PRIV per skb */
+	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
+		if (ocelot_ptp_is_onestep_sync(skb)) {
+			*(u8 *)DSA_SKB_CB_PRIV(skb) = ptp_cmd;
+			return 0;
+		}
+
+		/* Fall back to two-step timestamping */
+		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
+	}
+
 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
 		*clone = skb_clone_sk(skb);
 		if (!(*clone))
 			return -ENOMEM;
 
 		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
+		*(u8 *)DSA_SKB_CB_PRIV(skb) = ptp_cmd;
 	}
 
 	return 0;
drivers/net/ethernet/mscc/ocelot_net.c +1 -4
index 8293152a6dc1..eb3d525731da 100644
@@ -514,10 +514,7 @@ static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
 			return NETDEV_TX_OK;
 		}
 
-		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-			rew_op = ocelot_port->ptp_cmd;
-			rew_op |= clone->cb[0] << 3;
-		}
+		ocelot_ptp_rew_op(skb, clone, &rew_op);
 	}
 
 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
include/soc/mscc/ocelot.h +1 -0
index 9cdaf1d9199f..19413532db0b 100644
@@ -820,6 +820,7 @@ int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid);
 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr);
 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr);
+bool ocelot_ptp_rew_op(struct sk_buff *skb, struct sk_buff *clone, u32 *rew_op);
 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 				 struct sk_buff *skb,
 				 struct sk_buff **clone);
net/dsa/tag_ocelot.c +3 -22
index f9df9cac81c5..d5c73b36f0c1 100644
@@ -5,25 +5,6 @@
 #include <soc/mscc/ocelot.h>
 #include "dsa_priv.h"
 
-static void ocelot_xmit_ptp(struct dsa_port *dp, void *injection,
-			    struct sk_buff *clone)
-{
-	struct ocelot *ocelot = dp->ds->priv;
-	struct ocelot_port *ocelot_port;
-	u64 rew_op;
-
-	ocelot_port = ocelot->ports[dp->index];
-	rew_op = ocelot_port->ptp_cmd;
-
-	/* Retrieve timestamp ID populated inside skb->cb[0] of the
-	 * clone by ocelot_port_add_txtstamp_skb
-	 */
-	if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
-		rew_op |= clone->cb[0] << 3;
-
-	ocelot_ifh_set_rew_op(injection, rew_op);
-}
-
 static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
 			       __be32 ifh_prefix, void **ifh)
 {
@@ -32,6 +13,7 @@ static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
 	struct dsa_switch *ds = dp->ds;
 	void *injection;
 	__be32 *prefix;
+	u32 rew_op = 0;
 
 	injection = skb_push(skb, OCELOT_TAG_LEN);
 	prefix = skb_push(skb, OCELOT_SHORT_PREFIX_LEN);
@@ -42,9 +24,8 @@ static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
 	ocelot_ifh_set_src(injection, ds->num_ports);
 	ocelot_ifh_set_qos_class(injection, skb->priority);
 
-	/* TX timestamping was requested */
-	if (clone)
-		ocelot_xmit_ptp(dp, injection, clone);
+	if (ocelot_ptp_rew_op(skb, clone, &rew_op))
+		ocelot_ifh_set_rew_op(injection, rew_op);
 
 	*ifh = injection;
 }
net/dsa/tag_ocelot_8021q.c +10 -29
index 5f3e8e124a82..bf32649a5a7b 100644
@@ -13,32 +13,6 @@
 #include <soc/mscc/ocelot_ptp.h>
 #include "dsa_priv.h"
 
-static struct sk_buff *ocelot_xmit_ptp(struct dsa_port *dp,
-				       struct sk_buff *skb,
-				       struct sk_buff *clone)
-{
-	struct ocelot *ocelot = dp->ds->priv;
-	struct ocelot_port *ocelot_port;
-	int port = dp->index;
-	u32 rew_op;
-
-	if (!ocelot_can_inject(ocelot, 0))
-		return NULL;
-
-	ocelot_port = ocelot->ports[port];
-	rew_op = ocelot_port->ptp_cmd;
-
-	/* Retrieve timestamp ID populated inside skb->cb[0] of the
-	 * clone by ocelot_port_add_txtstamp_skb
-	 */
-	if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
-		rew_op |= clone->cb[0] << 3;
-
-	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
-
-	return NULL;
-}
-
 static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
 				   struct net_device *netdev)
 {
@@ -47,10 +21,17 @@ static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
 	u16 queue_mapping = skb_get_queue_mapping(skb);
 	u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
 	struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
+	struct ocelot *ocelot = dp->ds->priv;
+	int port = dp->index;
+	u32 rew_op = 0;
+
+	if (ocelot_ptp_rew_op(skb, clone, &rew_op)) {
+		if (!ocelot_can_inject(ocelot, 0))
+			return NULL;
 
-	/* TX timestamping was requested, so inject through MMIO */
-	if (clone)
-		return ocelot_xmit_ptp(dp, skb, clone);
+		ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
+		return NULL;
+	}
 
 	return dsa_8021q_xmit(skb, netdev, ETH_P_8021Q,
 			      ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
Signature · 1 line
2.25.1
#13 lore raw

Re: [net-next 3/3] net: mscc: ocelot: support PTP Sync one-step timestamping

I love your patch! Yet something to improve:

Hi Yangbo,

I love your patch! Yet something to improve:

[auto build test ERROR on 392c36e5be1dee19ffce8c8ba8f07f90f5aa3f7c]

url:    https://github.com/0day-ci/linux/commits/Yangbo-Lu/Support-ocelot-PTP-Sync-one-step-timestamping/20210416-203333
base:   392c36e5be1dee19ffce8c8ba8f07f90f5aa3f7c
config: i386-randconfig-r035-20210416 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/7e3d064908a2ecf6287c2eb40e0a36d86434f41c
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Yangbo-Lu/Support-ocelot-PTP-Sync-one-step-timestamping/20210416-203333
        git checkout 7e3d064908a2ecf6287c2eb40e0a36d86434f41c
        # save the attached .config to linux build tree
        make W=1 W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "ocelot_ptp_rew_op" [net/dsa/tag_ocelot.ko] undefined!

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#2 lore raw

Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Yangbo Lu
Quoted 5 lines · Yangbo Lu

Optimization could be done on dsa_skb_tx_timestamp(), and dsa device drivers should adapt to it.

- Check SKBTX_HW_TSTAMP request flag at the very beginning, instead of in port_txtstamp, so that most skbs not requiring tx timestamp just return.

Agree that this is a trivial performance optimization with no downside that we should be making.

Quoted 2 lines

- No longer to identify PTP packets, and limit tx timestamping only for PTP packets. If device driver likes, let device driver do.

Agree that DSA has a way too heavy hand in imposing upon the driver which packets should be timestampable and which ones shouldn't.

For example, I have a latency measurement tool called isochron which is based on hardware timestamping of non-PTP packets (in order to not disturb the PTP state machines):
https://github.com/vladimiroltean/tsn-scripts

I can't use it on DSA interfaces, for rather artificial reasons.

Quoted 4 lines

- It is a waste to clone skb directly in dsa_skb_tx_timestamp(). For one-step timestamping, a clone is not needed. For any failure of port_txtstamp (this may usually happen), the skb clone has to be freed. So put skb cloning into port_txtstamp where it really needs.

Mostly agree. For two-step timestamping, it is an operation which all drivers need to do, so it is in the common potion. If we want to support one-step, we need to avoid cloning the PTP packets.

Quoted 30 lines
---
 Documentation/networking/timestamping.rst     |  7 +++++--
 .../net/dsa/hirschmann/hellcreek_hwtstamp.c   | 20 ++++++++++++------
 .../net/dsa/hirschmann/hellcreek_hwtstamp.h   |  2 +-
 drivers/net/dsa/mv88e6xxx/hwtstamp.c          | 21 +++++++++++++------
 drivers/net/dsa/mv88e6xxx/hwtstamp.h          |  6 +++---
 drivers/net/dsa/ocelot/felix.c                | 11 ++++++----
 drivers/net/dsa/sja1105/sja1105_ptp.c         |  6 +++++-
 drivers/net/dsa/sja1105/sja1105_ptp.h         |  2 +-
 include/net/dsa.h                             |  2 +-
 net/dsa/slave.c                               | 20 +++++-------------
 10 files changed, 57 insertions(+), 40 deletions(-)
Documentation/networking/timestamping.rst +2 -2
index f682e88fa87e..7f04a699a5d1 100644
@@ -635,8 +635,8 @@ in generic code: a BPF classifier (``ptp_classify_raw``) is used to identify
 PTP event messages (any other packets, including PTP general messages, are not
 timestamped), and provides two hooks to drivers:
 
-- ``.port_txtstamp()``: The driver is passed a clone of the timestampable skb
-  to be transmitted, before actually transmitting it. Typically, a switch will
+- ``.port_txtstamp()``: A clone of the timestampable skb to be transmitted
+  is needed, before actually transmitting it. Typically, a switch will
   have a PTP TX timestamp register (or sometimes a FIFO) where the timestamp
   becomes available. There may be an IRQ that is raised upon this timestamp's
   availability, or the driver might have to poll after invoking

Documentation/networking/timestamping.rst timestamped), and provides two hooks to drivers: · line 650

Quoted 6 lines
Documentation/networking/timestamping.rst +3 -0
@@ -645,6 +645,9 @@ timestamped), and provides two hooks to drivers:
   later use (when the timestamp becomes available). Each skb is annotated with
   a pointer to its clone, in ``DSA_SKB_CB(skb)->clone``, to ease the driver's
   job of keeping track of which clone belongs to which skb.
+  But one-step timestamping request is handled differently with above two-step
+  timestamping. The skb clone is no longer needed since hardware will insert
+  TX time information on packet during egress.

Bonus points for updating the documentation, but I don't quite like the end result. Please feel free to restructure more, in order to have a clearer and more coherent explanation.

Also, this paragraph from right above is no longer true:

In code, DSA provides for most of the infrastructure for timestamping already, in generic code: a BPF classifier (``ptp_classify_raw``) is used to identify PTP event messages (any other packets, including PTP general messages, are not timestamped), and provides two hooks to drivers:

It's nothing like that anymore. It's more of a passthrough now with your changes, the BPF classifier is not run by the DSA core but optionally by individual taggers.

Here is my attempt of rewriting this documentation paragraph, feel free to take which parts you consider relevant:

-----------------------------[cut here]-----------------------------

In the generic layer, DSA provides the following infrastructure for PTP timestamping:

- ``.port_txtstamp()``: a hook called prior to the transmission of packets with a hardware TX timestamping request from user space. This is required for two-step timestamping, since the hardware timestamp becomes available after the actual MAC transmission, so the driver must be prepared to correlate the timestamp with the original packet so that it can re-enqueue the packet back into the socket's error queue. To save the packet for when the timestamp becomes available, the driver can call ``skb_clone_sk`` and save the resulting clone in ``DSA_SKB_CB(skb)->clone``. Typically, a switch will have a PTP TX timestamp register (or sometimes a FIFO) where the timestamp becomes available. In case of a FIFO, the hardware might store key-value pairs of PTP sequence ID/message type/domain number and the actual timestamp. To perform the correlation correctly between the packets in a queue waiting for timestamping and the actual timestamps, drivers can use a BPF classifier (``ptp_classify_raw``) to identify the PTP transport type, and ``ptp_parse_header`` to interpret the PTP header fields. There may be an IRQ that is raised upon this timestamp's availability, or the driver might have to poll after invoking ``dev_queue_xmit()`` towards the host interface. One-step TX timestamping do not require packet cloning, since there is no follow-up message required by the PTP protocol (because the TX timestamp is embedded into the packet by the MAC), and therefore user space does not expect the packet annotated with the TX timestamp to be re-enqueued into its socket's error queue.

- ``.port_rxtstamp()``: On RX, the BPF classifier is run by DSA to identify PTP event messages (any other packets, including PTP general messages, are not timestamped). The original (and only) timestampable skb is provided to the driver, for it to annotate it with a timestamp, if that is immediately available, or defer to later. On reception, timestamps might either be available in-band (through metadata in the DSA header, or attached in other ways to the packet), or out-of-band (through another RX timestamping FIFO). Deferral on RX is typically necessary when retrieving the timestamp needs a sleepable context. In that case, it is the responsibility of the DSA driver to call ``netif_rx_ni()`` on the freshly timestamped skb.

-----------------------------[cut here]-----------------------------

Quoted 202 lines
Documentation/networking/timestamping.rst +0 -0
 - ``.port_rxtstamp()``: The original (and only) timestampable skb is provided
   to the driver, for it to annotate it with a timestamp, if that is immediately
drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c +14 -6
index 69dd9a2e8bb6..2ff4b7c08b72 100644
@@ -374,31 +374,39 @@ long hellcreek_hwtstamp_work(struct ptp_clock_info *ptp)
 }
 
 bool hellcreek_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type)
+			     struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct hellcreek *hellcreek = ds->priv;
 	struct hellcreek_port_hwtstamp *ps;
 	struct ptp_header *hdr;
+	unsigned int type;
 
 	ps = &hellcreek->ports[port].port_hwtstamp;
 
-	/* Check if the driver is expected to do HW timestamping */
-	if (!(skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP))
+	type = ptp_classify_raw(skb);
+	if (type == PTP_CLASS_NONE)
 		return false;
 
 	/* Make sure the message is a PTP message that needs to be timestamped
 	 * and the interaction with the HW timestamping is enabled. If not, stop
 	 * here
 	 */
-	hdr = hellcreek_should_tstamp(hellcreek, port, clone, type);
+	hdr = hellcreek_should_tstamp(hellcreek, port, skb, type);
 	if (!hdr)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	if (test_and_set_bit_lock(HELLCREEK_HWTSTAMP_TX_IN_PROGRESS,
-				  &ps->state))
+				  &ps->state)) {
+		kfree_skb(*clone);
+		*clone = NULL;
 		return false;
+	}
 
-	ps->tx_skb = clone;
+	ps->tx_skb = *clone;
 
 	/* store the number of ticks occurred since system start-up till this
 	 * moment
drivers/net/dsa/hirschmann/hellcreek_hwtstamp.h +1 -1
index c0745ffa1ebb..58cc96642076 100644
@@ -45,7 +45,7 @@ int hellcreek_port_hwtstamp_get(struct dsa_switch *ds, int port,
 bool hellcreek_port_rxtstamp(struct dsa_switch *ds, int port,
 			     struct sk_buff *clone, unsigned int type);
 bool hellcreek_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type);
+			     struct sk_buff *skb, struct sk_buff **clone);
 
 int hellcreek_get_ts_info(struct dsa_switch *ds, int port,
 			  struct ethtool_ts_info *info);
drivers/net/dsa/mv88e6xxx/hwtstamp.c +15 -6
index 094d17a1d037..280a95962861 100644
@@ -469,24 +469,33 @@ long mv88e6xxx_hwtstamp_work(struct ptp_clock_info *ptp)
 }
 
 bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type)
+			     struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct mv88e6xxx_chip *chip = ds->priv;
 	struct mv88e6xxx_port_hwtstamp *ps = &chip->port_hwtstamp[port];
 	struct ptp_header *hdr;
+	unsigned int type;
 
-	if (!(skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP))
-		return false;
+	type = ptp_classify_raw(skb);
+	if (type == PTP_CLASS_NONE)
+		return false
 
-	hdr = mv88e6xxx_should_tstamp(chip, port, clone, type);
+	hdr = mv88e6xxx_should_tstamp(chip, port, skb, type);
 	if (!hdr)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	if (test_and_set_bit_lock(MV88E6XXX_HWTSTAMP_TX_IN_PROGRESS,
-				  &ps->state))
+				  &ps->state)) {
+		kfree_skb(*clone);
+		*clone = NULL;
 		return false;
+	}
 
-	ps->tx_skb = clone;
+	ps->tx_skb = *clone;
 	ps->tx_tstamp_start = jiffies;
 	ps->tx_seq_id = be16_to_cpu(hdr->sequence_id);
 
drivers/net/dsa/mv88e6xxx/hwtstamp.h +3 -3
index 9da9f197ba02..da2b253334d0 100644
@@ -118,7 +118,7 @@ int mv88e6xxx_port_hwtstamp_get(struct dsa_switch *ds, int port,
 bool mv88e6xxx_port_rxtstamp(struct dsa_switch *ds, int port,
 			     struct sk_buff *clone, unsigned int type);
 bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type);
+			     struct sk_buff *skb, struct sk_buff **clone);
 
 int mv88e6xxx_get_ts_info(struct dsa_switch *ds, int port,
 			  struct ethtool_ts_info *info);
@@ -152,8 +152,8 @@ static inline bool mv88e6xxx_port_rxtstamp(struct dsa_switch *ds, int port,
 }
 
 static inline bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-					   struct sk_buff *clone,
-					   unsigned int type)
+					   struct sk_buff *skb,
+					   struct sk_buff **clone)
 {
 	return false;
 }
drivers/net/dsa/ocelot/felix.c +7 -4
index 6b5442be0230..cdec2f5e271c 100644
@@ -1396,14 +1396,17 @@ static bool felix_rxtstamp(struct dsa_switch *ds, int port,
 }
 
 static bool felix_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *clone, unsigned int type)
+			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct ocelot *ocelot = ds->priv;
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
-	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) &&
-	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
+	if (ocelot->ptp && ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
+		*clone = skb_clone_sk(skb);
+		if (!(*clone))
+			return false;
+
+		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
 		return true;
 	}
 
drivers/net/dsa/sja1105/sja1105_ptp.c +5 -1
index 1b90570b257b..6a1f854a8c33 100644
@@ -436,7 +436,7 @@ bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
  * callback, where we will timestamp it synchronously.
  */
 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *skb, unsigned int type)
+			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct sja1105_private *priv = ds->priv;
 	struct sja1105_port *sp = &priv->ports[port];
@@ -444,6 +444,10 @@ bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
 	if (!sp->hwts_tx_en)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	return true;
 }
 
drivers/net/dsa/sja1105/sja1105_ptp.h +1 -1
index 3daa33e98e77..ab80b73219cb 100644
@@ -105,7 +105,7 @@ bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
 			   struct sk_buff *skb, unsigned int type);
 
 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *skb, unsigned int type);
+			   struct sk_buff *skb, struct sk_buff **clone);
 
 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct ifreq *ifr);
 
include/net/dsa.h +0 -0
index 1259b0f40684..c8415c324e27 100644

include/net/dsa.h struct dsa_switch_ops { · line 737

Quoted 5 lines
include/net/dsa.h +1 -1
@@ -734,7 +734,7 @@ struct dsa_switch_ops {
 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
 				     struct ifreq *ifr);
 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
-				 struct sk_buff *clone, unsigned int type);
+				 struct sk_buff *skb, struct sk_buff **clone);

How about not passing "clone" back to DSA as an argument by reference, but instead require the driver to populate DSA_SKB_CB(skb)->clone if it needs to do so?

Also, how about changing the return type to void? Returning true or false makes no difference.

Quoted 47 lines
include/net/dsa.h +0 -0
 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
 				 struct sk_buff *skb, unsigned int type);
 
net/dsa/slave.c +4 -12
index 9300cb66e500..5b746a903ef4 100644
@@ -19,7 +19,6 @@
 #include <linux/if_bridge.h>
 #include <linux/if_hsr.h>
 #include <linux/netpoll.h>
-#include <linux/ptp_classify.h>
 
 #include "dsa_priv.h"
 
@@ -555,26 +554,19 @@ static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
 				 struct sk_buff *skb)
 {
 	struct dsa_switch *ds = p->dp->ds;
-	struct sk_buff *clone;
-	unsigned int type;
+	struct sk_buff *clone = NULL;
 
-	type = ptp_classify_raw(skb);
-	if (type == PTP_CLASS_NONE)
+	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
 		return;
 
 	if (!ds->ops->port_txtstamp)
 		return;
 
-	clone = skb_clone_sk(skb);
-	if (!clone)
+	if (!ds->ops->port_txtstamp(ds, p->dp->index, skb, &clone))
 		return;
 
-	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) {
+	if (clone)
 		DSA_SKB_CB(skb)->clone = clone;
-		return;
-	}
-
-	kfree_skb(clone);
 }
 
 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)

net/dsa/slave.c static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) · line 623

Quoted 7 lines
net/dsa/slave.c +1 -3
@@ -628,9 +620,7 @@ static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
 
 	DSA_SKB_CB(skb)->clone = NULL;
 
-	/* Identify PTP protocol packets, clone them, and pass them to the
-	 * switch driver
-	 */
+	/* Handle tx timestamp request if has */

s/if has/if any/

Quoted 6 lines
net/dsa/slave.c +0 -0
 	dsa_skb_tx_timestamp(p, skb);
 
 	if (dsa_realloc_skb(skb, dev)) {
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 2/3
#11 lore raw

Re: [net-next 2/3] net: mscc: ocelot: convert to ocelot_port_txtstamp_request()

In reply to Yangbo Lu
Quoted 26 lines · Yangbo Lu

Convert to a common ocelot_port_txtstamp_request() for TX timestamp request handling.

---
 drivers/net/dsa/ocelot/felix.c         | 14 +++++---------
 drivers/net/ethernet/mscc/ocelot.c     | 24 +++++++++++++++++++++---
 drivers/net/ethernet/mscc/ocelot_net.c | 18 +++++++-----------
 include/soc/mscc/ocelot.h              |  5 +++--
 4 files changed, 36 insertions(+), 25 deletions(-)
drivers/net/dsa/ocelot/felix.c +1 -5
index cdec2f5e271c..5f2cf0f31253 100644
@@ -1399,18 +1399,14 @@ static bool felix_txtstamp(struct dsa_switch *ds, int port,
 			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct ocelot *ocelot = ds->priv;
-	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
-	if (ocelot->ptp && ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-		*clone = skb_clone_sk(skb);
-		if (!(*clone))
-			return false;
+	if (!ocelot->ptp)

drivers/net/dsa/ocelot/felix.c static bool felix_txtstamp(struct dsa_switch *ds, int port, · line 1409

Quoted 10 lines
drivers/net/dsa/ocelot/felix.c +4 -4
+		return false;
 
-		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
-		return true;
-	}
+	if (ocelot_port_txtstamp_request(ocelot, port, skb, clone))
+		return false;
 
-	return false;
+	return true;

Considering the changes you'll have to make to patch 1 (changing the return value and populating DSA_SKB_CB(skb)->clone at the end of this function:

maintainer
#14 lore raw

Re: [net-next 3/3] net: mscc: ocelot: support PTP Sync one-step timestamping

In reply to Yangbo Lu
Quoted 4 lines · Yangbo Lu

Although HWTSTAMP_TX_ONESTEP_SYNC existed in ioctl for hardware timestamp configuration, the PTP Sync one-step timestamping had never been supported.

This patch is to truely support it.

Actually the ocelot switchdev driver does support one-step timestamping, just the felix DSA driver does not.

Quoted 27 lines

The hardware timestamp request type is
stored in DSA_SKB_CB_PRIV first byte per skb, so that corresponding configuration could be done during transmitting. Non-onestep-Sync packet with one-step timestamp request should fall back to use two-step timestamp.

---
 drivers/net/ethernet/mscc/ocelot.c     | 57 ++++++++++++++++++++++++++
 drivers/net/ethernet/mscc/ocelot_net.c |  5 +--
 include/soc/mscc/ocelot.h              |  1 +
 net/dsa/tag_ocelot.c                   | 25 ++---------
 net/dsa/tag_ocelot_8021q.c             | 39 +++++-------------
 5 files changed, 72 insertions(+), 55 deletions(-)
drivers/net/ethernet/mscc/ocelot.c +1 -0
index 541d3b4076be..69d36b6241ff 100644
@@ -6,6 +6,7 @@
  */
 #include <linux/dsa/ocelot.h>
 #include <linux/if_bridge.h>
+#include <linux/ptp_classify.h>
 #include <soc/mscc/ocelot_vcap.h>
 #include "ocelot.h"
 #include "ocelot_vcap.h"

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, · line 556

Quoted 10 lines
drivers/net/ethernet/mscc/ocelot.c +7 -0
@@ -546,6 +547,50 @@ static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port,
 	spin_unlock(&ocelot_port->ts_id_lock);
 }
 
+bool ocelot_ptp_rew_op(struct sk_buff *skb, struct sk_buff *clone, u32 *rew_op)
+{
+	/* For two-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV
+	 * and timestamp ID in clone->cb[0].
+	 * For one-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV.
+	 */
+	u8 *ptp_cmd = DSA_SKB_CB_PRIV(skb);

This is fine in the sense that it works, but please consider creating something similar to sja1105:

struct ocelot_skb_cb {
u8 ptp_cmd; /* For both one-step and two-step timestamping */ u8 ts_id; /* Only for two-step timestamping */
};

#define OCELOT_SKB_CB(skb) \
	((struct ocelot_skb_cb *)DSA_SKB_CB_PRIV(skb))

And then access as OCELOT_SKB_CB(skb)->ptp_cmd, OCELOT_SKB_CB(clone)->ts_id.

and put a comment to explain that this is done in order to have common code between Felix DSA and Ocelot switchdev. Basically Ocelot will not use the first 8 bytes of skb->cb, but there's enough space for this to not make any difference. The original skb will hold only ptp_cmd, the clone will only hold ts_id, but it helps to have the same structure in place.

If you create this ocelot_skb_cb structure, I expect the comment above to be fairly redundant, you can consider removing it.

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, · line 567

Quoted 11 lines
drivers/net/ethernet/mscc/ocelot.c +11 -0
+
+	if (clone) {
+		*rew_op = *ptp_cmd;
+		*rew_op |= clone->cb[0] << 3;
+	} else if (*ptp_cmd) {
+		*rew_op = *ptp_cmd;
+	} else {
+		return false;
+	}
+
+	return true;

Just make this function return an u32. If the packet isn't PTP, the rew_op will be 0.

Quoted 16 lines
drivers/net/ethernet/mscc/ocelot.c +16 -0
+}
+EXPORT_SYMBOL(ocelot_ptp_rew_op);
+
+static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb)
+{
+	struct ptp_header *hdr;
+	unsigned int ptp_class;
+	u8 msgtype, twostep;
+
+	ptp_class = ptp_classify_raw(skb);
+	if (ptp_class == PTP_CLASS_NONE)
+		return false;
+
+	hdr = ptp_parse_header(skb, ptp_class);
+	if (!hdr)
+		return false;

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, · line 593

Quoted 10 lines
drivers/net/ethernet/mscc/ocelot.c +10 -0
+
+	msgtype = ptp_get_msgtype(hdr, ptp_class);
+	twostep = hdr->flag_field[0] & 0x2;
+
+	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
+		return true;
+
+	return false;
+}
+

This is generic, but if you were to move it to net/core/ptp_classifier.c, I think you would have to pass the output of ptp_classify_raw() as an "unsigned int type" argument. So I think I would leave it the way it is for now - inside of ocelot - until somebody else needs something similar, and we see what is the required prototype.

Quoted 167 lines
drivers/net/ethernet/mscc/ocelot.c +12 -0
 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 				 struct sk_buff *skb,
 				 struct sk_buff **clone)
@@ -553,12 +598,24 @@ int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 	u8 ptp_cmd = ocelot_port->ptp_cmd;
 
+	/* Store ptp_cmd in first byte of DSA_SKB_CB_PRIV per skb */
+	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
+		if (ocelot_ptp_is_onestep_sync(skb)) {
+			*(u8 *)DSA_SKB_CB_PRIV(skb) = ptp_cmd;
+			return 0;
+		}
+
+		/* Fall back to two-step timestamping */
+		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
+	}
+
 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
 		*clone = skb_clone_sk(skb);
 		if (!(*clone))
 			return -ENOMEM;
 
 		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
+		*(u8 *)DSA_SKB_CB_PRIV(skb) = ptp_cmd;
 	}
 
 	return 0;
drivers/net/ethernet/mscc/ocelot_net.c +1 -4
index 8293152a6dc1..eb3d525731da 100644
@@ -514,10 +514,7 @@ static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
 			return NETDEV_TX_OK;
 		}
 
-		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-			rew_op = ocelot_port->ptp_cmd;
-			rew_op |= clone->cb[0] << 3;
-		}
+		ocelot_ptp_rew_op(skb, clone, &rew_op);
 	}
 
 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
include/soc/mscc/ocelot.h +1 -0
index 9cdaf1d9199f..19413532db0b 100644
@@ -820,6 +820,7 @@ int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
 int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid);
 int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr);
 int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr);
+bool ocelot_ptp_rew_op(struct sk_buff *skb, struct sk_buff *clone, u32 *rew_op);
 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 				 struct sk_buff *skb,
 				 struct sk_buff **clone);
net/dsa/tag_ocelot.c +3 -22
index f9df9cac81c5..d5c73b36f0c1 100644
@@ -5,25 +5,6 @@
 #include <soc/mscc/ocelot.h>
 #include "dsa_priv.h"
 
-static void ocelot_xmit_ptp(struct dsa_port *dp, void *injection,
-			    struct sk_buff *clone)
-{
-	struct ocelot *ocelot = dp->ds->priv;
-	struct ocelot_port *ocelot_port;
-	u64 rew_op;
-
-	ocelot_port = ocelot->ports[dp->index];
-	rew_op = ocelot_port->ptp_cmd;
-
-	/* Retrieve timestamp ID populated inside skb->cb[0] of the
-	 * clone by ocelot_port_add_txtstamp_skb
-	 */
-	if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
-		rew_op |= clone->cb[0] << 3;
-
-	ocelot_ifh_set_rew_op(injection, rew_op);
-}
-
 static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
 			       __be32 ifh_prefix, void **ifh)
 {
@@ -32,6 +13,7 @@ static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
 	struct dsa_switch *ds = dp->ds;
 	void *injection;
 	__be32 *prefix;
+	u32 rew_op = 0;
 
 	injection = skb_push(skb, OCELOT_TAG_LEN);
 	prefix = skb_push(skb, OCELOT_SHORT_PREFIX_LEN);
@@ -42,9 +24,8 @@ static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
 	ocelot_ifh_set_src(injection, ds->num_ports);
 	ocelot_ifh_set_qos_class(injection, skb->priority);
 
-	/* TX timestamping was requested */
-	if (clone)
-		ocelot_xmit_ptp(dp, injection, clone);
+	if (ocelot_ptp_rew_op(skb, clone, &rew_op))
+		ocelot_ifh_set_rew_op(injection, rew_op);
 
 	*ifh = injection;
 }
net/dsa/tag_ocelot_8021q.c +10 -29
index 5f3e8e124a82..bf32649a5a7b 100644
@@ -13,32 +13,6 @@
 #include <soc/mscc/ocelot_ptp.h>
 #include "dsa_priv.h"
 
-static struct sk_buff *ocelot_xmit_ptp(struct dsa_port *dp,
-				       struct sk_buff *skb,
-				       struct sk_buff *clone)
-{
-	struct ocelot *ocelot = dp->ds->priv;
-	struct ocelot_port *ocelot_port;
-	int port = dp->index;
-	u32 rew_op;
-
-	if (!ocelot_can_inject(ocelot, 0))
-		return NULL;
-
-	ocelot_port = ocelot->ports[port];
-	rew_op = ocelot_port->ptp_cmd;
-
-	/* Retrieve timestamp ID populated inside skb->cb[0] of the
-	 * clone by ocelot_port_add_txtstamp_skb
-	 */
-	if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
-		rew_op |= clone->cb[0] << 3;
-
-	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
-
-	return NULL;
-}
-
 static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
 				   struct net_device *netdev)
 {
@@ -47,10 +21,17 @@ static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
 	u16 queue_mapping = skb_get_queue_mapping(skb);
 	u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
 	struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
+	struct ocelot *ocelot = dp->ds->priv;
+	int port = dp->index;
+	u32 rew_op = 0;
+
+	if (ocelot_ptp_rew_op(skb, clone, &rew_op)) {
+		if (!ocelot_can_inject(ocelot, 0))
+			return NULL;
 
-	/* TX timestamping was requested, so inject through MMIO */
-	if (clone)
-		return ocelot_xmit_ptp(dp, skb, clone);
+		ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
+		return NULL;
+	}
 
 	return dsa_8021q_xmit(skb, netdev, ETH_P_8021Q,
 			      ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
Signature · 1 line
2.25.1
#6 lore raw

Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Yangbo Lu
Quoted 6 lines · Yangbo Lu

Optimization could be done on dsa_skb_tx_timestamp(), and dsa device drivers should adapt to it.

- Check SKBTX_HW_TSTAMP request flag at the very beginning, instead of in port_txtstamp, so that most skbs not requiring tx timestamp just return.

Quoted 7 lines

- No longer to identify PTP packets, and limit tx timestamping only for PTP packets. If device driver likes, let device driver do.

- It is a waste to clone skb directly in dsa_skb_tx_timestamp(). For one-step timestamping, a clone is not needed. For any failure of port_txtstamp (this may usually happen), the skb clone has to be freed. So put skb cloning into port_txtstamp where it really needs.

This patch changes three things ^^^ at once. These are AFAICT independent changes, and so please break this one patch into three for easier review.

Thanks,
Richard

#8 lore raw

Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Yangbo Lu

net/dsa/slave.c static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) · line 623

Quoted 8 lines · Yangbo Lu
net/dsa/slave.c +1 -3
@@ -628,9 +620,7 @@ static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
 
 	DSA_SKB_CB(skb)->clone = NULL;
 
-	/* Identify PTP protocol packets, clone them, and pass them to the
-	 * switch driver
-	 */
+	/* Handle tx timestamp request if has */

"if has" what?

Quoted 6 lines
net/dsa/slave.c +0 -0
 	dsa_skb_tx_timestamp(p, skb);
 
 	if (dsa_realloc_skb(skb, dev)) {
Signature · 1 line
2.25.1

Thanks,
Richard

#3 lore raw

Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Vladimir Oltean
Quoted 7 lines · Vladimir Oltean

How about not passing "clone" back to DSA as an argument by reference, but instead require the driver to populate DSA_SKB_CB(skb)->clone if it needs to do so?

Also, how about changing the return type to void? Returning true or false makes no difference.

+1

Thanks,
Richard

maintainer Tested-by on v1 1/3
#9 lore raw

Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Yangbo Lu
Quoted 9 lines · Yangbo Lu

Optimization could be done on dsa_skb_tx_timestamp(), and dsa device drivers should adapt to it.

- Check SKBTX_HW_TSTAMP request flag at the very beginning, instead of in port_txtstamp, so that most skbs not requiring tx timestamp just return.

- No longer to identify PTP packets, and limit tx timestamping only for PTP packets. If device driver likes, let device driver do.

Quoted 6 lines

- It is a waste to clone skb directly in dsa_skb_tx_timestamp(). For one-step timestamping, a clone is not needed. For any failure of port_txtstamp (this may usually happen), the skb clone has to be freed. So put skb cloning into port_txtstamp where it really needs.

PTP still works.

author
#15 lore raw

RE: [net-next 3/3] net: mscc: ocelot: support PTP Sync one-step timestamping

In reply to Vladimir Oltean

Hi Vladimir,

Quoted 17 lines

-----Original Message-----
From: Vladimir Oltean <[email protected]>
Sent: 2021年4月18日 17:46
To: Y.b. Lu <[email protected]>
Cc: [email protected]; Richard Cochran <[email protected]>; Vladimir Oltean <[email protected]>; David S . Miller <[email protected]>; Jakub Kicinski <[email protected]>; Jonathan Corbet <[email protected]>; Kurt Kanzenbach <[email protected]>; Andrew Lunn <[email protected]>; Vivien Didelot <[email protected]>; Florian Fainelli <[email protected]>; Claudiu Manoil <[email protected]>; Alexandre Belloni <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [net-next 3/3] net: mscc: ocelot: support PTP Sync one-step timestamping

On Fri, Apr 16, 2021 at 08:36:55PM +0800, Yangbo Lu wrote:

Quoted 8 lines
Quoted 2 lines

Although HWTSTAMP_TX_ONESTEP_SYNC existed in ioctl for hardware timestamp configuration, the PTP Sync one-step timestamping had never

been supported.

Quoted 2 lines

This patch is to truely support it.

Actually the ocelot switchdev driver does support one-step timestamping, just the felix DSA driver does not.

Actually I don’t think ocelot support one-step timestamping properly.
Without one-step sync packet identification, the one-step timestamping is applying to all packets requiring timestamp. The user space PTP stack won't work.

Quoted 55 lines
Quoted 9 lines

The hardware timestamp request type is stored in DSA_SKB_CB_PRIV first byte per skb, so that corresponding configuration could be done during transmitting. Non-onestep-Sync packet with one-step timestamp request should fall back to use two-step timestamp.

---
 drivers/net/ethernet/mscc/ocelot.c     | 57

++++++++++++++++++++++++++

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct

Quoted 20 lines
 drivers/net/ethernet/mscc/ocelot_net.c |  5 +--
 include/soc/mscc/ocelot.h              |  1 +
 net/dsa/tag_ocelot.c                   | 25 ++---------
 net/dsa/tag_ocelot_8021q.c             | 39 +++++-------------
 5 files changed, 72 insertions(+), 55 deletions(-)
drivers/net/ethernet/mscc/ocelot.c +1 -0
index 541d3b4076be..69d36b6241ff 100644
@@ -6,6 +6,7 @@
  */
 #include <linux/dsa/ocelot.h>
 #include <linux/if_bridge.h>
+#include <linux/ptp_classify.h>
 #include <soc/mscc/ocelot_vcap.h>
 #include "ocelot.h"
 #include "ocelot_vcap.h"
@@ -546,6 +547,50 @@ static void ocelot_port_add_txtstamp_skb(struct

ocelot *ocelot, int port,

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, · line 556

Quoted 10 lines
drivers/net/ethernet/mscc/ocelot.c +7 -0
 	spin_unlock(&ocelot_port->ts_id_lock);
 }
 
+bool ocelot_ptp_rew_op(struct sk_buff *skb, struct sk_buff *clone,
+u32 *rew_op) {
+	/* For two-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV
+	 * and timestamp ID in clone->cb[0].
+	 * For one-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV.
+	 */
+	u8 *ptp_cmd = DSA_SKB_CB_PRIV(skb);

This is fine in the sense that it works, but please consider creating something similar to sja1105:

struct ocelot_skb_cb {
	u8 ptp_cmd; /* For both one-step and two-step timestamping */
	u8 ts_id; /* Only for two-step timestamping */ };
#define OCELOT_SKB_CB(skb) \
	((struct ocelot_skb_cb *)DSA_SKB_CB_PRIV(skb))

And then access as OCELOT_SKB_CB(skb)->ptp_cmd,
OCELOT_SKB_CB(clone)->ts_id.

Quoted 9 lines

and put a comment to explain that this is done in order to have common code between Felix DSA and Ocelot switchdev. Basically Ocelot will not use the first 8 bytes of skb->cb, but there's enough space for this to not make any difference. The original skb will hold only ptp_cmd, the clone will only hold ts_id, but it helps to have the same structure in place.

If you create this ocelot_skb_cb structure, I expect the comment above to be fairly redundant, you can consider removing it.

You're right to define the structure.
Considering patch #1, move skb cloning to drivers, and populate DSA_SKB_CB(skb)->clone if needs to do so (per suggestion). Can we totally drop dsa_skb_cb in dsa core? The only usage of it is holding a skb clone pointer, for only felix and sja1105. Actually we can move such pointer in <device>_skb_cb, instead of reserving the space of skb for any drivers.

Do you think so?

Quoted 6 lines
Quoted 6 lines
drivers/net/ethernet/mscc/ocelot.c +6 -0
+
+	if (clone) {
+		*rew_op = *ptp_cmd;
+		*rew_op |= clone->cb[0] << 3;
+	} else if (*ptp_cmd) {
+		*rew_op = *ptp_cmd;
Quoted 8 lines

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, · line 567

Quoted 5 lines
drivers/net/ethernet/mscc/ocelot.c +5 -0
+	} else {
+		return false;
+	}
+
+	return true;

Just make this function return an u32. If the packet isn't PTP, the rew_op will be 0.

Ok. Thanks.

Quoted 24 lines
Quoted 24 lines
drivers/net/ethernet/mscc/ocelot.c +23 -0
+}
+EXPORT_SYMBOL(ocelot_ptp_rew_op);
+
+static bool ocelot_ptp_is_onestep_sync(struct sk_buff *skb) {
+	struct ptp_header *hdr;
+	unsigned int ptp_class;
+	u8 msgtype, twostep;
+
+	ptp_class = ptp_classify_raw(skb);
+	if (ptp_class == PTP_CLASS_NONE)
+		return false;
+
+	hdr = ptp_parse_header(skb, ptp_class);
+	if (!hdr)
+		return false;
+
+	msgtype = ptp_get_msgtype(hdr, ptp_class);
+	twostep = hdr->flag_field[0] & 0x2;
+
+	if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0)
+		return true;
+
+	return false;
Quoted 8 lines

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct · line 592

Quoted 2 lines
drivers/net/ethernet/mscc/ocelot.c +2 -0
+}
+

This is generic, but if you were to move it to net/core/ptp_classifier.c, I think you would have to pass the output of ptp_classify_raw() as an "unsigned int type" argument. So I think I would leave it the way it is for now - inside of ocelot - until somebody else needs something similar, and we see what is the required prototype.

Yes. This is indeed generic, and useful. I'd like to leave it the way it is for now. I may make another patch-set for this in future.

Quoted 177 lines

drivers/net/ethernet/mscc/ocelot.c int ocelot_port_txtstamp_request(struct ocelot

Quoted 5 lines
drivers/net/ethernet/mscc/ocelot.c +0 -0
 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 				 struct sk_buff *skb,
 				 struct sk_buff **clone)
@@ -553,12 +598,24 @@ int ocelot_port_txtstamp_request(struct ocelot

*ocelot, int port,

drivers/net/ethernet/mscc/ocelot_net.c static netdev_tx_t ocelot_port_xmit(struct sk_buff

Quoted 30 lines
drivers/net/ethernet/mscc/ocelot.c +12 -0
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 	u8 ptp_cmd = ocelot_port->ptp_cmd;
 
+	/* Store ptp_cmd in first byte of DSA_SKB_CB_PRIV per skb */
+	if (ptp_cmd == IFH_REW_OP_ORIGIN_PTP) {
+		if (ocelot_ptp_is_onestep_sync(skb)) {
+			*(u8 *)DSA_SKB_CB_PRIV(skb) = ptp_cmd;
+			return 0;
+		}
+
+		/* Fall back to two-step timestamping */
+		ptp_cmd = IFH_REW_OP_TWO_STEP_PTP;
+	}
+
 	if (ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
 		*clone = skb_clone_sk(skb);
 		if (!(*clone))
 			return -ENOMEM;
 
 		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
+		*(u8 *)DSA_SKB_CB_PRIV(skb) = ptp_cmd;
 	}
 
 	return 0;
drivers/net/ethernet/mscc/ocelot_net.c +0 -0
index 8293152a6dc1..eb3d525731da 100644
@@ -514,10 +514,7 @@ static netdev_tx_t ocelot_port_xmit(struct sk_buff

*skb, struct net_device *dev)

Quoted 43 lines
drivers/net/ethernet/mscc/ocelot_net.c +1 -4
 			return NETDEV_TX_OK;
 		}
 
-		if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-			rew_op = ocelot_port->ptp_cmd;
-			rew_op |= clone->cb[0] << 3;
-		}
+		ocelot_ptp_rew_op(skb, clone, &rew_op);
 	}
 
 	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); diff --git

a/include/soc/mscc/ocelot.h b/include/soc/mscc/ocelot.h index 9cdaf1d9199f..19413532db0b 100644

include/soc/mscc/ocelot.h +0 -0
@@ -820,6 +820,7 @@ int ocelot_vlan_add(struct ocelot *ocelot, int
 port, u16 vid, bool pvid,  int ocelot_vlan_del(struct ocelot *ocelot,

int port, u16 vid); int ocelot_hwstamp_get(struct ocelot *ocelot, int port, struct ifreq *ifr); int ocelot_hwstamp_set(struct ocelot *ocelot, int port, struct ifreq *ifr);

include/soc/mscc/ocelot.h +2 -0
+bool ocelot_ptp_rew_op(struct sk_buff *skb, struct sk_buff *clone,
+u32 *rew_op);
 int ocelot_port_txtstamp_request(struct ocelot *ocelot, int port,
 				 struct sk_buff *skb,
 				 struct sk_buff **clone);
net/dsa/tag_ocelot.c index renamed from net/dsa/tag_ocelot.c +0 -0

f9df9cac81c5..d5c73b36f0c1 100644

net/dsa/tag_ocelot.c +0 -10
@@ -5,25 +5,6 @@
 #include <soc/mscc/ocelot.h>
 #include "dsa_priv.h"
 
-static void ocelot_xmit_ptp(struct dsa_port *dp, void *injection,
-			    struct sk_buff *clone)
-{
-	struct ocelot *ocelot = dp->ds->priv;
-	struct ocelot_port *ocelot_port;
-	u64 rew_op;
-
-	ocelot_port = ocelot->ports[dp->index];
-	rew_op = ocelot_port->ptp_cmd;
-

net/dsa/tag_ocelot.c line 8

Quoted 10 lines
net/dsa/tag_ocelot.c +0 -9
-	/* Retrieve timestamp ID populated inside skb->cb[0] of the
-	 * clone by ocelot_port_add_txtstamp_skb
-	 */
-	if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
-		rew_op |= clone->cb[0] << 3;
-
-	ocelot_ifh_set_rew_op(injection, rew_op);
-}
-
 static void ocelot_xmit_common(struct sk_buff *skb, struct net_device

*netdev,

net/dsa/tag_ocelot.c line 9

Quoted 1 line
net/dsa/tag_ocelot.c +0 -0
 			       __be32 ifh_prefix, void **ifh)  { @@ -32,6 +13,7 @@

static

net/dsa/tag_ocelot.c static void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,

Quoted 8 lines
net/dsa/tag_ocelot.c +0 -0
 void ocelot_xmit_common(struct sk_buff *skb, struct net_device *netdev,
net/dsa/tag_ocelot.c +1 -0
 	struct dsa_switch *ds = dp->ds;
 	void *injection;
 	__be32 *prefix;
+	u32 rew_op = 0;
 
 	injection = skb_push(skb, OCELOT_TAG_LEN);
 	prefix = skb_push(skb, OCELOT_SHORT_PREFIX_LEN); @@ -42,9 +24,8

@@

Quoted 1 line

static void ocelot_xmit_common(struct sk_buff *skb, struct net_device

*netdev,

net/dsa/tag_ocelot_8021q.c static struct sk_buff *ocelot_xmit(struct sk_buff

Quoted 49 lines
net/dsa/tag_ocelot.c +2 -3
 	ocelot_ifh_set_src(injection, ds->num_ports);
 	ocelot_ifh_set_qos_class(injection, skb->priority);
 
-	/* TX timestamping was requested */
-	if (clone)
-		ocelot_xmit_ptp(dp, injection, clone);
+	if (ocelot_ptp_rew_op(skb, clone, &rew_op))
+		ocelot_ifh_set_rew_op(injection, rew_op);
 
 	*ifh = injection;
 }
net/dsa/tag_ocelot_8021q.c +0 -26
index 5f3e8e124a82..bf32649a5a7b 100644
@@ -13,32 +13,6 @@
 #include <soc/mscc/ocelot_ptp.h>
 #include "dsa_priv.h"
 
-static struct sk_buff *ocelot_xmit_ptp(struct dsa_port *dp,
-				       struct sk_buff *skb,
-				       struct sk_buff *clone)
-{
-	struct ocelot *ocelot = dp->ds->priv;
-	struct ocelot_port *ocelot_port;
-	int port = dp->index;
-	u32 rew_op;
-
-	if (!ocelot_can_inject(ocelot, 0))
-		return NULL;
-
-	ocelot_port = ocelot->ports[port];
-	rew_op = ocelot_port->ptp_cmd;
-
-	/* Retrieve timestamp ID populated inside skb->cb[0] of the
-	 * clone by ocelot_port_add_txtstamp_skb
-	 */
-	if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP)
-		rew_op |= clone->cb[0] << 3;
-
-	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
-
-	return NULL;
-}
-
 static struct sk_buff *ocelot_xmit(struct sk_buff *skb,
 				   struct net_device *netdev)
 {
@@ -47,10 +21,17 @@ static struct sk_buff *ocelot_xmit(struct sk_buff

*skb,

Quoted 23 lines
net/dsa/tag_ocelot_8021q.c +10 -4
 	u16 queue_mapping = skb_get_queue_mapping(skb);
 	u8 pcp = netdev_txq_to_tc(netdev, queue_mapping);
 	struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
+	struct ocelot *ocelot = dp->ds->priv;
+	int port = dp->index;
+	u32 rew_op = 0;
+
+	if (ocelot_ptp_rew_op(skb, clone, &rew_op)) {
+		if (!ocelot_can_inject(ocelot, 0))
+			return NULL;
 
-	/* TX timestamping was requested, so inject through MMIO */
-	if (clone)
-		return ocelot_xmit_ptp(dp, skb, clone);
+		ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
+		return NULL;
+	}
 
 	return dsa_8021q_xmit(skb, netdev, ETH_P_8021Q,
 			      ((pcp << VLAN_PRIO_SHIFT) | tx_vid));
--

2.25.1

author
#4 lore raw

RE: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Richard Cochran
Quoted 14 lines

-----Original Message-----
From: Richard Cochran <[email protected]>
Sent: 2021年4月18日 23:11
To: Vladimir Oltean <[email protected]>
Cc: Y.b. Lu <[email protected]>; [email protected]; Vladimir Oltean <[email protected]>; David S . Miller <[email protected]>; Jakub Kicinski <[email protected]>; Jonathan Corbet <[email protected]>; Kurt Kanzenbach <[email protected]>; Andrew Lunn <[email protected]>; Vivien Didelot <[email protected]>; Florian Fainelli <[email protected]>; Claudiu Manoil <[email protected]>; Alexandre Belloni <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

Quoted 8 lines
Quoted 7 lines · Vladimir Oltean

How about not passing "clone" back to DSA as an argument by reference, but instead require the driver to populate DSA_SKB_CB(skb)->clone if it needs to do so?

Also, how about changing the return type to void? Returning true or false makes no difference.

Thank you. That's good idea.
And how about letting driver store the skb clone pointer, or not? I copied my comments in patch #3 here,

" Can we totally drop dsa_skb_cb in dsa core? The only usage of it is holding a skb clone pointer, for only felix and sja1105. Actually we can move such pointer in <device>_skb_cb, instead of reserving the space of skb for any drivers."

Quoted 5 lines

+1

Thanks,
Richard

author
#7 lore raw

RE: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Richard Cochran
Quoted 24 lines

-----Original Message-----
From: Richard Cochran <[email protected]>
Sent: 2021年4月18日 23:06
To: Y.b. Lu <[email protected]>
Cc: [email protected]; Vladimir Oltean <[email protected]>; David S . Miller <[email protected]>; Jakub Kicinski <[email protected]>; Jonathan Corbet <[email protected]>; Kurt Kanzenbach <[email protected]>; Andrew Lunn <[email protected]>; Vivien Didelot <[email protected]>; Florian Fainelli <[email protected]>; Claudiu Manoil <[email protected]>; Alexandre Belloni <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

Quoted 9 lines · Yangbo Lu

Optimization could be done on dsa_skb_tx_timestamp(), and dsa device drivers should adapt to it.

- Check SKBTX_HW_TSTAMP request flag at the very beginning, instead of in port_txtstamp, so that most skbs not requiring tx timestamp just return.

- No longer to identify PTP packets, and limit tx timestamping only for PTP packets. If device driver likes, let device driver do.

Quoted 7 lines
Quoted 4 lines

- It is a waste to clone skb directly in dsa_skb_tx_timestamp(). For one-step timestamping, a clone is not needed. For any failure of port_txtstamp (this may usually happen), the skb clone has to be freed. So put skb cloning into port_txtstamp where it really needs.

This patch changes three things ^^^ at once. These are AFAICT independent changes, and so please break this one patch into three for easier review.

Will split it in next version. Thank you.

Quoted 3 lines

Thanks,
Richard

author
#5 lore raw

RE: [net-next 1/3] net: dsa: optimize tx timestamp request handling

In reply to Vladimir Oltean
Quoted 44 lines

-----Original Message-----
From: Vladimir Oltean <[email protected]>
Sent: 2021年4月18日 17:19
To: Y.b. Lu <[email protected]>
Cc: [email protected]; Richard Cochran <[email protected]>; Vladimir Oltean <[email protected]>; David S . Miller <[email protected]>; Jakub Kicinski <[email protected]>; Jonathan Corbet <[email protected]>; Kurt Kanzenbach <[email protected]>; Andrew Lunn <[email protected]>; Vivien Didelot <[email protected]>; Florian Fainelli <[email protected]>; Claudiu Manoil <[email protected]>; Alexandre Belloni <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [net-next 1/3] net: dsa: optimize tx timestamp request handling

Quoted 5 lines · Yangbo Lu

Optimization could be done on dsa_skb_tx_timestamp(), and dsa device drivers should adapt to it.

- Check SKBTX_HW_TSTAMP request flag at the very beginning, instead of in port_txtstamp, so that most skbs not requiring tx timestamp just return.

Agree that this is a trivial performance optimization with no downside that we should be making.

Quoted 2 lines

- No longer to identify PTP packets, and limit tx timestamping only for PTP packets. If device driver likes, let device driver do.

Agree that DSA has a way too heavy hand in imposing upon the driver which packets should be timestampable and which ones shouldn't.

For example, I have a latency measurement tool called isochron which is based on hardware timestamping of non-PTP packets (in order to not disturb the PTP state machines):
https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.
com%2Fvladimiroltean%2Ftsn-scripts&amp;data=04%7C01%7Cyangbo.lu%40 nxp.com%7C3772f63deb6f4491933208d9024af750%7C686ea1d3bc2b4c6fa9 2cd99c5c301635%7C0%7C0%7C637543343267914018%7CUnknown%7CTW FpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXV CI6Mn0%3D%7C1000&amp;sdata=deOn03j6biPEUwppNkv%2F9if1u5HIdLEtzz AkWW0p1rc%3D&amp;reserved=0

I can't use it on DSA interfaces, for rather artificial reasons.

Quoted 9 lines
Quoted 4 lines

- It is a waste to clone skb directly in dsa_skb_tx_timestamp(). For one-step timestamping, a clone is not needed. For any failure of port_txtstamp (this may usually happen), the skb clone has to be freed. So put skb cloning into port_txtstamp where it really needs.

Mostly agree. For two-step timestamping, it is an operation which all drivers need to do, so it is in the common potion. If we want to support one-step, we need to avoid cloning the PTP packets.

Thanks a lot for your ACK.

Quoted 109 lines
Quoted 6 lines
---
 Documentation/networking/timestamping.rst     |  7 +++++--
 .../net/dsa/hirschmann/hellcreek_hwtstamp.c   | 20 ++++++++++++------
 .../net/dsa/hirschmann/hellcreek_hwtstamp.h   |  2 +-
 drivers/net/dsa/mv88e6xxx/hwtstamp.c          | 21

+++++++++++++------

Quoted 14 lines
 drivers/net/dsa/mv88e6xxx/hwtstamp.h          |  6 +++---
 drivers/net/dsa/ocelot/felix.c                | 11 ++++++----
 drivers/net/dsa/sja1105/sja1105_ptp.c         |  6 +++++-
 drivers/net/dsa/sja1105/sja1105_ptp.h         |  2 +-
 include/net/dsa.h                             |  2 +-
 net/dsa/slave.c                               | 20 +++++-------------
 10 files changed, 57 insertions(+), 40 deletions(-)
Documentation/networking/timestamping.rst +0 -0
index f682e88fa87e..7f04a699a5d1 100644
@@ -635,8 +635,8 @@ in generic code: a BPF classifier
Quoted 2 lines

(``ptp_classify_raw``) is used to identify PTP event messages (any other packets, including PTP general messages, are not timestamped), and

provides two hooks to drivers:

Documentation/networking/timestamping.rst

Quoted 10 lines

-- ``.port_txtstamp()``: The driver is passed a clone of the timestampable skb
- to be transmitted, before actually transmitting it. Typically, a switch will
+- ``.port_txtstamp()``: A clone of the timestampable skb to be

Documentation/networking/timestamping.rst +3 -0
+transmitted
+  is needed, before actually transmitting it. Typically, a switch
+will
   have a PTP TX timestamp register (or sometimes a FIFO) where the

timestamp

Quoted 1 line

becomes available. There may be an IRQ that is raised upon this

timestamp's

Quoted 3 lines

availability, or the driver might have to poll after invoking @@
-645,6 +645,9 @@ timestamped), and provides two hooks to drivers:
later use (when the timestamp becomes available). Each skb is annotated

with

Quoted 5 lines

a pointer to its clone, in ``DSA_SKB_CB(skb)->clone``, to ease the driver's job of keeping track of which clone belongs to which skb.
+ But one-step timestamping request is handled differently with above
+ two-step timestamping. The skb clone is no longer needed since
+ hardware will insert TX time information on packet during egress.

Bonus points for updating the documentation, but I don't quite like the end result. Please feel free to restructure more, in order to have a clearer and more coherent explanation.

Also, this paragraph from right above is no longer true:

In code, DSA provides for most of the infrastructure for timestamping
already,
in generic code: a BPF classifier (``ptp_classify_raw``) is used to identify PTP event messages (any other packets, including PTP general messages,
are not
timestamped), and provides two hooks to drivers:

It's nothing like that anymore. It's more of a passthrough now with your changes, the BPF classifier is not run by the DSA core but optionally by individual taggers.

Here is my attempt of rewriting this documentation paragraph, feel free to take which parts you consider relevant:

-----------------------------[cut here]-----------------------------

In the generic layer, DSA provides the following infrastructure for PTP timestamping:

- ``.port_txtstamp()``: a hook called prior to the transmission of packets with a hardware TX timestamping request from user space. This is required for two-step timestamping, since the hardware timestamp becomes available after the actual MAC transmission, so the driver must be prepared to correlate the timestamp with the original packet so that it can re-enqueue the packet back into the socket's error queue. To save the packet for when the timestamp becomes available, the driver can call ``skb_clone_sk`` and save the resulting clone in ``DSA_SKB_CB(skb)->clone``. Typically, a switch will have a PTP TX timestamp register (or sometimes a FIFO) where the timestamp becomes available. In case of a FIFO, the hardware might store key-value pairs of PTP sequence ID/message type/domain number and the actual timestamp. To perform the correlation correctly between the packets in a queue waiting for timestamping and the actual timestamps, drivers can use a BPF classifier (``ptp_classify_raw``) to identify the PTP transport type, and ``ptp_parse_header`` to interpret the PTP header fields. There may be an IRQ that is raised upon this timestamp's availability, or the driver might have to poll after invoking ``dev_queue_xmit()`` towards the host interface. One-step TX timestamping do not require packet cloning, since there is no follow-up message required by the PTP protocol (because the TX timestamp is embedded into the packet by the MAC), and therefore user space does not expect the packet annotated with the TX timestamp to be re-enqueued into its socket's error queue.

- ``.port_rxtstamp()``: On RX, the BPF classifier is run by DSA to identify PTP event messages (any other packets, including PTP general messages, are not timestamped). The original (and only) timestampable skb is provided to the driver, for it to annotate it with a timestamp, if that is immediately available, or defer to later. On reception, timestamps might either be available in-band (through metadata in the DSA header, or attached in other ways to the packet), or out-of-band (through another RX timestamping FIFO). Deferral on RX is typically necessary when retrieving the timestamp needs a sleepable context. In that case, it is the responsibility of the DSA driver to call ``netif_rx_ni()`` on the freshly timestamped skb.

Quoted 2 lines

-----------------------------[cut here]-----------------------------

That's really considerate and accuracy description. I will update with this. (Will adjust if we can drop dsa_skb_cb in dsa core.) Thanks.

Quoted 281 lines
Quoted 20 lines

- ``.port_rxtstamp()``: The original (and only) timestampable skb is provided to the driver, for it to annotate it with a timestamp, if that is
immediately diff --git a/drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c b/drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c
index 69dd9a2e8bb6..2ff4b7c08b72 100644

drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c +2 -1
@@ -374,31 +374,39 @@ long hellcreek_hwtstamp_work(struct
 ptp_clock_info *ptp)  }
 
 bool hellcreek_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type)
+			     struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct hellcreek *hellcreek = ds->priv;
 	struct hellcreek_port_hwtstamp *ps;
 	struct ptp_header *hdr;
+	unsigned int type;

drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c long hellcreek_hwtstamp_work(struct · line 390

Quoted 10 lines
drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c +2 -2
 
 	ps = &hellcreek->ports[port].port_hwtstamp;
 
-	/* Check if the driver is expected to do HW timestamping */
-	if (!(skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP))
+	type = ptp_classify_raw(skb);
+	if (type == PTP_CLASS_NONE)
 		return false;
 
 	/* Make sure the message is a PTP message that needs to be

timestamped

drivers/net/dsa/mv88e6xxx/hwtstamp.h int mv88e6xxx_port_hwtstamp_get(struct

Quoted 90 lines
drivers/net/dsa/hirschmann/hellcreek_hwtstamp.c +10 -3
 	 * and the interaction with the HW timestamping is enabled. If not, stop
 	 * here
 	 */
-	hdr = hellcreek_should_tstamp(hellcreek, port, clone, type);
+	hdr = hellcreek_should_tstamp(hellcreek, port, skb, type);
 	if (!hdr)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	if (test_and_set_bit_lock(HELLCREEK_HWTSTAMP_TX_IN_PROGRESS,
-				  &ps->state))
+				  &ps->state)) {
+		kfree_skb(*clone);
+		*clone = NULL;
 		return false;
+	}
 
-	ps->tx_skb = clone;
+	ps->tx_skb = *clone;
 
 	/* store the number of ticks occurred since system start-up till this
 	 * moment
drivers/net/dsa/hirschmann/hellcreek_hwtstamp.h +1 -1
index c0745ffa1ebb..58cc96642076 100644
@@ -45,7 +45,7 @@ int hellcreek_port_hwtstamp_get(struct dsa_switch
 *ds, int port,  bool hellcreek_port_rxtstamp(struct dsa_switch *ds, int port,
 			     struct sk_buff *clone, unsigned int type);  bool
 hellcreek_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type);
+			     struct sk_buff *skb, struct sk_buff **clone);
 
 int hellcreek_get_ts_info(struct dsa_switch *ds, int port,
 			  struct ethtool_ts_info *info);
drivers/net/dsa/mv88e6xxx/hwtstamp.c +15 -6
index 094d17a1d037..280a95962861 100644
@@ -469,24 +469,33 @@ long mv88e6xxx_hwtstamp_work(struct
 ptp_clock_info *ptp)  }
 
 bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type)
+			     struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct mv88e6xxx_chip *chip = ds->priv;
 	struct mv88e6xxx_port_hwtstamp *ps = &chip->port_hwtstamp[port];
 	struct ptp_header *hdr;
+	unsigned int type;
 
-	if (!(skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP))
-		return false;
+	type = ptp_classify_raw(skb);
+	if (type == PTP_CLASS_NONE)
+		return false
 
-	hdr = mv88e6xxx_should_tstamp(chip, port, clone, type);
+	hdr = mv88e6xxx_should_tstamp(chip, port, skb, type);
 	if (!hdr)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	if (test_and_set_bit_lock(MV88E6XXX_HWTSTAMP_TX_IN_PROGRESS,
-				  &ps->state))
+				  &ps->state)) {
+		kfree_skb(*clone);
+		*clone = NULL;
 		return false;
+	}
 
-	ps->tx_skb = clone;
+	ps->tx_skb = *clone;
 	ps->tx_tstamp_start = jiffies;
 	ps->tx_seq_id = be16_to_cpu(hdr->sequence_id);
 
drivers/net/dsa/mv88e6xxx/hwtstamp.h +0 -0
index 9da9f197ba02..da2b253334d0 100644
@@ -118,7 +118,7 @@ int mv88e6xxx_port_hwtstamp_get(struct

dsa_switch

drivers/net/dsa/mv88e6xxx/hwtstamp.h int mv88e6xxx_port_hwtstamp_get(struct · line 118

Quoted 1 line
drivers/net/dsa/mv88e6xxx/hwtstamp.h +0 -0
 *ds, int port,  bool mv88e6xxx_port_rxtstamp(struct dsa_switch *ds, int

port,

Quoted 24 lines
drivers/net/dsa/mv88e6xxx/hwtstamp.h +3 -3
 			     struct sk_buff *clone, unsigned int type);  bool
 mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-			     struct sk_buff *clone, unsigned int type);
+			     struct sk_buff *skb, struct sk_buff **clone);
 
 int mv88e6xxx_get_ts_info(struct dsa_switch *ds, int port,
 			  struct ethtool_ts_info *info);
@@ -152,8 +152,8 @@ static inline bool mv88e6xxx_port_rxtstamp(struct
 dsa_switch *ds, int port,  }
 
 static inline bool mv88e6xxx_port_txtstamp(struct dsa_switch *ds, int port,
-					   struct sk_buff *clone,
-					   unsigned int type)
+					   struct sk_buff *skb,
+					   struct sk_buff **clone)
 {
 	return false;
 }
(quoted diff) +0 -0

b/drivers/net/dsa/ocelot/felix.c index 6b5442be0230..cdec2f5e271c 100644

drivers/net/dsa/ocelot/felix.c static bool felix_rxtstamp(struct dsa_switch · line 1404

Quoted 10 lines
drivers/net/dsa/ocelot/felix.c +1 -2
@@ -1396,14 +1396,17 @@ static bool felix_rxtstamp(struct dsa_switch
 *ds, int port,  }
 
 static bool felix_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *clone, unsigned int type)
+			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct ocelot *ocelot = ds->priv;
 	struct ocelot_port *ocelot_port = ocelot->ports[port];
 
-	if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP)

&&

drivers/net/dsa/ocelot/felix.c static bool felix_rxtstamp(struct dsa_switch *ds, int port,

Quoted 3 lines
drivers/net/dsa/ocelot/felix.c +1 -2
-	    ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
-		ocelot_port_add_txtstamp_skb(ocelot, port, clone);
+	if (ocelot->ptp && ocelot_port->ptp_cmd ==

IFH_REW_OP_TWO_STEP_PTP) {

drivers/net/dsa/sja1105/sja1105_ptp.c bool sja1105_port_rxtstamp(struct dsa_switch *ds,

Quoted 14 lines
drivers/net/dsa/ocelot/felix.c +5 -0
+		*clone = skb_clone_sk(skb);
+		if (!(*clone))
+			return false;
+
+		ocelot_port_add_txtstamp_skb(ocelot, port, *clone);
 		return true;
 	}
 
drivers/net/dsa/sja1105/sja1105_ptp.c +0 -0
index 1b90570b257b..6a1f854a8c33 100644
@@ -436,7 +436,7 @@ bool sja1105_port_rxtstamp(struct dsa_switch *ds,

int port,

drivers/net/dsa/sja1105/sja1105_ptp.h bool sja1105_port_rxtstamp(struct dsa_switch *ds,

Quoted 25 lines
drivers/net/dsa/sja1105/sja1105_ptp.c +1 -1
  * callback, where we will timestamp it synchronously.
  */
 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *skb, unsigned int type)
+			   struct sk_buff *skb, struct sk_buff **clone)
 {
 	struct sja1105_private *priv = ds->priv;
 	struct sja1105_port *sp = &priv->ports[port]; @@ -444,6 +444,10 @@

bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,

drivers/net/dsa/sja1105/sja1105_ptp.c +4 -0
 	if (!sp->hwts_tx_en)
 		return false;
 
+	*clone = skb_clone_sk(skb);
+	if (!(*clone))
+		return false;
+
 	return true;
 }
drivers/net/dsa/sja1105/sja1105_ptp.h +0 -0
index 3daa33e98e77..ab80b73219cb 100644
@@ -105,7 +105,7 @@ bool sja1105_port_rxtstamp(struct dsa_switch *ds,

int port,

Quoted 14 lines
drivers/net/dsa/sja1105/sja1105_ptp.h +1 -1
 			   struct sk_buff *skb, unsigned int type);
 
 bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
-			   struct sk_buff *skb, unsigned int type);
+			   struct sk_buff *skb, struct sk_buff **clone);
 
 int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, struct
 ifreq *ifr);
include/net/dsa.h index renamed from include/net/dsa.h +0 -0

1259b0f40684..c8415c324e27 100644

include/net/dsa.h struct dsa_switch_ops { · line 737

Quoted 5 lines
include/net/dsa.h +1 -1
@@ -734,7 +734,7 @@ struct dsa_switch_ops {
 	int	(*port_hwtstamp_set)(struct dsa_switch *ds, int port,
 				     struct ifreq *ifr);
 	bool	(*port_txtstamp)(struct dsa_switch *ds, int port,
-				 struct sk_buff *clone, unsigned int type);
+				 struct sk_buff *skb, struct sk_buff **clone);

How about not passing "clone" back to DSA as an argument by reference, but instead require the driver to populate DSA_SKB_CB(skb)->clone if it needs to do so?

Also, how about changing the return type to void? Returning true or false makes no difference.

net/dsa/slave.c static void dsa_skb_tx_timestamp(struct

Quoted 16 lines
include/net/dsa.h +0 -0
 	bool	(*port_rxtstamp)(struct dsa_switch *ds, int port,
 				 struct sk_buff *skb, unsigned int type);
 
net/dsa/slave.c index renamed from net/dsa/slave.c +0 -0

9300cb66e500..5b746a903ef4 100644

net/dsa/slave.c +0 -1
@@ -19,7 +19,6 @@
 #include <linux/if_bridge.h>
 #include <linux/if_hsr.h>
 #include <linux/netpoll.h>
-#include <linux/ptp_classify.h>
 
 #include "dsa_priv.h"
 
@@ -555,26 +554,19 @@ static void dsa_skb_tx_timestamp(struct

dsa_slave_priv *p,

Quoted 33 lines
net/dsa/slave.c +4 -11
 				 struct sk_buff *skb)
 {
 	struct dsa_switch *ds = p->dp->ds;
-	struct sk_buff *clone;
-	unsigned int type;
+	struct sk_buff *clone = NULL;
 
-	type = ptp_classify_raw(skb);
-	if (type == PTP_CLASS_NONE)
+	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
 		return;
 
 	if (!ds->ops->port_txtstamp)
 		return;
 
-	clone = skb_clone_sk(skb);
-	if (!clone)
+	if (!ds->ops->port_txtstamp(ds, p->dp->index, skb, &clone))
 		return;
 
-	if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) {
+	if (clone)
 		DSA_SKB_CB(skb)->clone = clone;
-		return;
-	}
-
-	kfree_skb(clone);
 }
 
 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device

*dev) @@ -628,9 +620,7 @@ static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)

Quoted 8 lines

net/dsa/slave.c static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) · line 623

Quoted 6 lines
net/dsa/slave.c +1 -3
 	DSA_SKB_CB(skb)->clone = NULL;
 
-	/* Identify PTP protocol packets, clone them, and pass them to the
-	 * switch driver
-	 */
+	/* Handle tx timestamp request if has */

s/if has/if any/

Sorry. Will fix.

Quoted 7 lines
Quoted 7 lines
net/dsa/slave.c +0 -1
 	dsa_skb_tx_timestamp(p, skb);
 
 	if (dsa_realloc_skb(skb, dev)) {
--

2.25.1

maintainer
#16 lore raw

Re: [net-next 3/3] net: mscc: ocelot: support PTP Sync one-step timestamping

In reply to Y.b. Lu
Quoted 29 lines · Y.b. Lu
Quoted 19 lines

drivers/net/ethernet/mscc/ocelot.c static void ocelot_port_add_txtstamp_skb(struct ocelot *ocelot, int port, · line 556

Quoted 5 lines
drivers/net/ethernet/mscc/ocelot.c +5 -0
+	/* For two-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV
+	 * and timestamp ID in clone->cb[0].
+	 * For one-step timestamp, retrieve ptp_cmd in DSA_SKB_CB_PRIV.
+	 */
+	u8 *ptp_cmd = DSA_SKB_CB_PRIV(skb);

This is fine in the sense that it works, but please consider creating something similar to sja1105:

struct ocelot_skb_cb {
	u8 ptp_cmd; /* For both one-step and two-step timestamping */
	u8 ts_id; /* Only for two-step timestamping */ };
#define OCELOT_SKB_CB(skb) \
	((struct ocelot_skb_cb *)DSA_SKB_CB_PRIV(skb))

And then access as OCELOT_SKB_CB(skb)->ptp_cmd,
OCELOT_SKB_CB(clone)->ts_id.

Quoted 9 lines

and put a comment to explain that this is done in order to have common code between Felix DSA and Ocelot switchdev. Basically Ocelot will not use the first 8 bytes of skb->cb, but there's enough space for this to not make any difference. The original skb will hold only ptp_cmd, the clone will only hold ts_id, but it helps to have the same structure in place.

If you create this ocelot_skb_cb structure, I expect the comment above to be fairly redundant, you can consider removing it.

Quoted 6 lines

You're right to define the structure.
Considering patch #1, move skb cloning to drivers, and populate DSA_SKB_CB(skb)->clone if needs to do so (per suggestion). Can we totally drop dsa_skb_cb in dsa core? The only usage of it is holding a skb clone pointer, for only felix and sja1105. Actually we can move such pointer in <device>_skb_cb, instead of reserving the space of skb for any drivers.

Do you think so?

The trouble with skb->cb is that it isn't zero-initialized. But somebody needs to initialize the clone pointer to NULL, otherwise you don't know if this is a valid pointer or not. Because dsa_skb_tx_timestamp() is called before p->xmit(), the driver has no way to initialize the clone pointer by itself. So this was done directly from dsa_slave_xmit(), and not from any driver-specific hook. So this is why there is a DSA_SKB_CB(skb)->clone and not SJA1105_SKB_CB(skb)->clone. The alternative would be to memset(skb->cb, 0, 48) which is a bit sub-optimal because it initializes more than it needs. Alternatively, it might be possible to introduce a new property in struct dsa_device_ops which holds sizeof(struct sja1105_skb_cb), and the generic code will only zero-initialize this number of bytes.
I don't know, if you can get it to work in a way that does not incur a noticeable performance penalty, I'm okay with whatever you come up with.