4693f0
commit ac92a711614c413b75a4963eab95cd1aa2de8293
4693f0
Author: Miroslav Lichvar <mlichvar@redhat.com>
4693f0
Date:   Mon Mar 27 17:43:06 2017 +0200
4693f0
4693f0
    phc2sys: don't synchronize clock to itself.
4693f0
    
4693f0
    When ptp4l is using multiple interfaces sharing the same clock, phc2sys
4693f0
    in the automatic mode should not try to synchronize them to each other.
4693f0
    
4693f0
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
4693f0
    Reported-by: Stefan Lange <s.lange@gateware.de>
4693f0
4693f0
diff --git a/phc2sys.c b/phc2sys.c
4693f0
index aa4186b..4c8b552 100644
4693f0
--- a/phc2sys.c
4693f0
+++ b/phc2sys.c
4693f0
@@ -72,6 +72,7 @@
4693f0
 struct clock {
4693f0
 	LIST_ENTRY(clock) list;
4693f0
 	clockid_t clkid;
4693f0
+	int phc_index;
4693f0
 	int sysoff_supported;
4693f0
 	int is_utc;
4693f0
 	int dest_only;
4693f0
@@ -127,7 +128,7 @@ static int clock_handle_leap(struct node *node, struct clock *clock,
4693f0
 static int run_pmc_get_utc_offset(struct node *node, int timeout);
4693f0
 static void run_pmc_events(struct node *node);
4693f0
 
4693f0
-static clockid_t clock_open(char *device)
4693f0
+static clockid_t clock_open(char *device, int *phc_index)
4693f0
 {
4693f0
 	struct sk_ts_info ts_info;
4693f0
 	char phc_device[16];
4693f0
@@ -157,6 +158,7 @@ static clockid_t clock_open(char *device)
4693f0
 	clkid = phc_open(phc_device);
4693f0
 	if (clkid == CLOCK_INVALID)
4693f0
 		fprintf(stderr, "cannot open %s: %m\n", device);
4693f0
+	*phc_index = ts_info.phc_index;
4693f0
 	return clkid;
4693f0
 }
4693f0
 
4693f0
@@ -164,11 +166,11 @@ static struct clock *clock_add(struct node *node, char *device)
4693f0
 {
4693f0
 	struct clock *c;
4693f0
 	clockid_t clkid = CLOCK_INVALID;
4693f0
-	int max_ppb;
4693f0
+	int max_ppb, phc_index = -1;
4693f0
 	double ppb;
4693f0
 
4693f0
 	if (device) {
4693f0
-		clkid = clock_open(device);
4693f0
+		clkid = clock_open(device, &phc_index);
4693f0
 		if (clkid == CLOCK_INVALID)
4693f0
 			return NULL;
4693f0
 	}
4693f0
@@ -179,6 +181,7 @@ static struct clock *clock_add(struct node *node, char *device)
4693f0
 		return NULL;
4693f0
 	}
4693f0
 	c->clkid = clkid;
4693f0
+	c->phc_index = phc_index;
4693f0
 	c->servo_state = SERVO_UNLOCKED;
4693f0
 	c->device = strdup(device);
4693f0
 
4693f0
@@ -638,6 +641,13 @@ static int do_loop(struct node *node, int subscriptions)
4693f0
 			if (!update_needed(clock))
4693f0
 				continue;
4693f0
 
4693f0
+			/* don't try to synchronize the clock to itself */
4693f0
+			if (clock->clkid == node->master->clkid ||
4693f0
+			    (clock->phc_index >= 0 &&
4693f0
+			     clock->phc_index == node->master->phc_index) ||
4693f0
+			    !strcmp(clock->device, node->master->device))
4693f0
+				continue;
4693f0
+
4693f0
 			if (clock->clkid == CLOCK_REALTIME &&
4693f0
 			    node->master->sysoff_supported) {
4693f0
 				/* use sysoff */