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