4b595f
commit 399907db7f9dc3f57c3f9831b3b4da705a2c51a3
4b595f
Author: Erez Geva <erezgeva2@gmail.com>
4b595f
Date:   Tue Aug 28 22:05:28 2018 +0200
4b595f
4b595f
    config: Add hardware time stamp filter setting mode
4b595f
    
4b595f
    Add global option for the hardware time stamp setting.
4b595f
    The function could:
4b595f
    Normally set the filters as the PTP daemon require.
4b595f
    Check that the filters are proper but do not change them.
4b595f
    Full, set the RX filter to all and the TX filter as the PTP daemon require.
4b595f
    
4b595f
    [ RC: added missing extern keyword and fixed indentation. ]
4b595f
    
4b595f
    Signed-off-by: Erez Geva <erez.geva.ext@siemens.com>
4b595f
    Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
4b595f
4b595f
diff --git a/config.c b/config.c
4b595f
index 7914ba4..3530ce6 100644
4b595f
--- a/config.c
4b595f
+++ b/config.c
4b595f
@@ -164,6 +164,13 @@ static struct config_enum delay_mech_enu[] = {
4b595f
 	{ NULL, 0 },
4b595f
 };
4b595f
 
4b595f
+static struct config_enum hwts_filter_enu[] = {
4b595f
+	{ "normal",  HWTS_FILTER_NORMAL  },
4b595f
+	{ "check",   HWTS_FILTER_CHECK   },
4b595f
+	{ "full",    HWTS_FILTER_FULL    },
4b595f
+	{ NULL, 0 },
4b595f
+};
4b595f
+
4b595f
 static struct config_enum nw_trans_enu[] = {
4b595f
 	{ "L2",    TRANS_IEEE_802_3 },
4b595f
 	{ "UDPv4", TRANS_UDP_IPV4   },
4b595f
@@ -215,6 +222,7 @@ struct config_item config_tab[] = {
4b595f
 	GLOB_ITEM_INT("G.8275.defaultDS.localPriority", 128, 1, UINT8_MAX),
4b595f
 	PORT_ITEM_INT("G.8275.portDS.localPriority", 128, 1, UINT8_MAX),
4b595f
 	GLOB_ITEM_INT("gmCapable", 1, 0, 1),
4b595f
+	GLOB_ITEM_ENU("hwts_filter", HWTS_FILTER_NORMAL, hwts_filter_enu),
4b595f
 	PORT_ITEM_INT("hybrid_e2e", 0, 0, 1),
4b595f
 	PORT_ITEM_INT("ignore_transport_specific", 0, 0, 1),
4b595f
 	PORT_ITEM_INT("ingressLatency", 0, INT_MIN, INT_MAX),
4b595f
diff --git a/ptp4l.8 b/ptp4l.8
4b595f
index 10c5c2f..39bf36e 100644
4b595f
--- a/ptp4l.8
4b595f
+++ b/ptp4l.8
4b595f
@@ -661,6 +661,15 @@ The time source is a single byte code that gives an idea of the kind
4b595f
 of local clock in use. The value is purely informational, having no
4b595f
 effect on the outcome of the Best Master Clock algorithm, and is
4b595f
 advertised when the clock becomes grand master.
4b595f
+.TP
4b595f
+.B hwts_filter
4b595f
+Select the hardware time stamp filter setting mode.
4b595f
+Possible values are normal, check, full.
4b595f
+Normal mode set the filters as needed.
4b595f
+Check mode only check but do not set.
4b595f
+Full mode set the receive filter to mark all packets with hardware time stamp,
4b595f
+ so all applications can get them.
4b595f
+The default is normal.
4b595f
 
4b595f
 .SH UNICAST DISCOVERY OPTIONS
4b595f
 
4b595f
diff --git a/ptp4l.c b/ptp4l.c
4b595f
index 9ef8169..3a9f084 100644
4b595f
--- a/ptp4l.c
4b595f
+++ b/ptp4l.c
4b595f
@@ -191,6 +191,7 @@ int main(int argc, char *argv[])
4b595f
 	assume_two_step = config_get_int(cfg, NULL, "assume_two_step");
4b595f
 	sk_check_fupsync = config_get_int(cfg, NULL, "check_fup_sync");
4b595f
 	sk_tx_timeout = config_get_int(cfg, NULL, "tx_timestamp_timeout");
4b595f
+	sk_hwts_filter_mode = config_get_int(cfg, NULL, "hwts_filter");
4b595f
 
4b595f
 	if (config_get_int(cfg, NULL, "clock_servo") == CLOCK_SERVO_NTPSHM) {
4b595f
 		config_set_int(cfg, "kernel_leap", 0);
4b595f
diff --git a/sk.c b/sk.c
4b595f
index f18b2bf..43f1800 100644
4b595f
--- a/sk.c
4b595f
+++ b/sk.c
4b595f
@@ -40,39 +40,76 @@
4b595f
 
4b595f
 int sk_tx_timeout = 1;
4b595f
 int sk_check_fupsync;
4b595f
+enum hwts_filter_mode sk_hwts_filter_mode = HWTS_FILTER_NORMAL;
4b595f
 
4b595f
 /* private methods */
4b595f
 
4b595f
-static int hwts_init(int fd, const char *device, int rx_filter, int tx_type)
4b595f
+static void init_ifreq(struct ifreq *ifreq, struct hwtstamp_config *cfg,
4b595f
+	const char *device)
4b595f
 {
4b595f
-	struct ifreq ifreq;
4b595f
-	struct hwtstamp_config cfg, req;
4b595f
-	int err;
4b595f
+	memset(ifreq, 0, sizeof(*ifreq));
4b595f
+	memset(cfg, 0, sizeof(*cfg));
4b595f
 
4b595f
-	memset(&ifreq, 0, sizeof(ifreq));
4b595f
-	memset(&cfg, 0, sizeof(cfg));
4b595f
+	strncpy(ifreq->ifr_name, device, sizeof(ifreq->ifr_name) - 1);
4b595f
 
4b595f
-	strncpy(ifreq.ifr_name, device, sizeof(ifreq.ifr_name) - 1);
4b595f
+	ifreq->ifr_data = (void *) cfg;
4b595f
+}
4b595f
 
4b595f
-	ifreq.ifr_data = (void *) &cfg;
4b595f
-	cfg.tx_type    = tx_type;
4b595f
-	cfg.rx_filter  = rx_filter;
4b595f
-	req = cfg;
4b595f
-	err = ioctl(fd, SIOCSHWTSTAMP, &ifreq);
4b595f
-	if (err < 0)
4b595f
-		return err;
4b595f
+static int hwts_init(int fd, const char *device, int rx_filter,
4b595f
+	int rx_filter2, int tx_type)
4b595f
+{
4b595f
+	struct ifreq ifreq;
4b595f
+	struct hwtstamp_config cfg;
4b595f
+	int err;
4b595f
 
4b595f
-	if (memcmp(&cfg, &req, sizeof(cfg))) {
4b595f
+	init_ifreq(&ifreq, &cfg, device);
4b595f
 
4b595f
-		pr_debug("driver changed our HWTSTAMP options");
4b595f
-		pr_debug("tx_type   %d not %d", cfg.tx_type, req.tx_type);
4b595f
-		pr_debug("rx_filter %d not %d", cfg.rx_filter, req.rx_filter);
4b595f
+	switch (sk_hwts_filter_mode) {
4b595f
+	case HWTS_FILTER_CHECK:
4b595f
+		err = ioctl(fd, SIOCGHWTSTAMP, &ifreq);
4b595f
+		if (err < 0) {
4b595f
+			pr_err("ioctl SIOCGHWTSTAMP failed: %m");
4b595f
+			return err;
4b595f
+		}
4b595f
+		break;
4b595f
+	case HWTS_FILTER_FULL:
4b595f
+		cfg.tx_type   = tx_type;
4b595f
+		cfg.rx_filter = HWTSTAMP_FILTER_ALL;
4b595f
+		err = ioctl(fd, SIOCSHWTSTAMP, &ifreq);
4b595f
+		if (err < 0) {
4b595f
+			pr_err("ioctl SIOCSHWTSTAMP failed: %m");
4b595f
+			return err;
4b595f
+		}
4b595f
+		break;
4b595f
+	case HWTS_FILTER_NORMAL:
4b595f
+		cfg.tx_type   = tx_type;
4b595f
+		cfg.rx_filter = rx_filter;
4b595f
+		err = ioctl(fd, SIOCSHWTSTAMP, &ifreq);
4b595f
+		if (err < 0) {
4b595f
+			pr_info("driver rejected most general HWTSTAMP filter");
4b595f
 
4b595f
-		if (cfg.tx_type != req.tx_type ||
4b595f
-		    (cfg.rx_filter != HWTSTAMP_FILTER_ALL &&
4b595f
-		     cfg.rx_filter != HWTSTAMP_FILTER_PTP_V2_EVENT)) {
4b595f
-			return -1;
4b595f
+			init_ifreq(&ifreq, &cfg, device);
4b595f
+			cfg.tx_type   = tx_type;
4b595f
+			cfg.rx_filter = rx_filter2;
4b595f
+
4b595f
+			err = ioctl(fd, SIOCSHWTSTAMP, &ifreq);
4b595f
+			if (err < 0) {
4b595f
+				pr_err("ioctl SIOCSHWTSTAMP failed: %m");
4b595f
+				return err;
4b595f
+			}
4b595f
 		}
4b595f
+		break;
4b595f
+	}
4b595f
+
4b595f
+	if (cfg.tx_type != tx_type ||
4b595f
+	    (cfg.rx_filter != rx_filter &&
4b595f
+	     cfg.rx_filter != rx_filter2 &&
4b595f
+	     cfg.rx_filter != HWTSTAMP_FILTER_ALL)) {
4b595f
+		pr_debug("tx_type   %d not %d", cfg.tx_type, tx_type);
4b595f
+		pr_debug("rx_filter %d not %d or %d", cfg.rx_filter, rx_filter,
4b595f
+			 rx_filter2);
4b595f
+		pr_err("The current filter does not match the required");
4b595f
+		return -1;
4b595f
 	}
4b595f
 
4b595f
 	return 0;
4b595f
@@ -450,15 +487,9 @@ int sk_timestamping_init(int fd, const char *device, enum timestamp_type type,
4b595f
 		case TRANS_UDS:
4b595f
 			return -1;
4b595f
 		}
4b595f
-		err = hwts_init(fd, device, filter1, tx_type);
4b595f
-		if (err) {
4b595f
-			pr_info("driver rejected most general HWTSTAMP filter");
4b595f
-			err = hwts_init(fd, device, filter2, tx_type);
4b595f
-			if (err) {
4b595f
-				pr_err("ioctl SIOCSHWTSTAMP failed: %m");
4b595f
-				return err;
4b595f
-			}
4b595f
-		}
4b595f
+		err = hwts_init(fd, device, filter1, filter2, tx_type);
4b595f
+		if (err)
4b595f
+			return err;
4b595f
 	}
4b595f
 
4b595f
 	if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
4b595f
diff --git a/sk.h b/sk.h
4b595f
index d91d5d8..fd4d820 100644
4b595f
--- a/sk.h
4b595f
+++ b/sk.h
4b595f
@@ -23,6 +23,16 @@
4b595f
 #include "address.h"
4b595f
 #include "transport.h"
4b595f
 
4b595f
+/**
4b595f
+ * Defines the available Hardware time-stamp setting modes.
4b595f
+ */
4b595f
+
4b595f
+enum hwts_filter_mode {
4b595f
+	HWTS_FILTER_NORMAL,    /* set hardware filters in normal way */
4b595f
+	HWTS_FILTER_CHECK,     /* check filters but do not change them */
4b595f
+	HWTS_FILTER_FULL,      /* Use time-stamp on all received packets */
4b595f
+};
4b595f
+
4b595f
 /**
4b595f
  * Contains timestamping information returned by the GET_TS_INFO ioctl.
4b595f
  * @valid:            set to non-zero when the info struct contains valid data.
4b595f
@@ -131,4 +141,9 @@ extern int sk_tx_timeout;
4b595f
  */
4b595f
 extern int sk_check_fupsync;
4b595f
 
4b595f
+/**
4b595f
+ * Hardware time-stamp setting mode
4b595f
+ */
4b595f
+extern enum hwts_filter_mode sk_hwts_filter_mode;
4b595f
+
4b595f
 #endif