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