bmc.c 9.4 KB
Newer Older
1
/*
2 3
 * Aurelio Colosimo for CERN, 2011 -- GNU LGPL v2.1 or later
 * Based on PTPd project v. 2.1.0 (see AUTHORS for details)
4
 */
5

6
#include <pptp/pptp.h>
7
#include <pptp/diag.h>
8

9 10 11 12 13 14 15 16
/* Flag Field bits symbolic names (table 57, pag. 151) */
#define FFB_LI61	0x01
#define FFB_LI59	0x02
#define FFB_UTCV	0x04
#define FFB_PTP		0x08
#define FFB_TTRA	0x10
#define FFB_FTRA	0x20

17 18 19 20 21 22 23 24 25 26 27 28
/* Local clock is becoming Master. Table 13 (9.3.5) of the spec. */
void m1(struct pp_instance *ppi)
{
	/* Current data set update */
	DSCUR(ppi)->stepsRemoved = 0;
	DSCUR(ppi)->offsetFromMaster.nanoseconds = 0;
	DSCUR(ppi)->offsetFromMaster.seconds = 0;
	DSCUR(ppi)->meanPathDelay.nanoseconds = 0;
	DSCUR(ppi)->meanPathDelay.seconds = 0;

	/* Parent data set */
	pp_memcpy(DSPAR(ppi)->parentPortIdentity.clockIdentity,
29
		  DSDEF(ppi)->clockIdentity, PP_CLOCK_IDENTITY_LENGTH);
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	DSPAR(ppi)->parentPortIdentity.portNumber = 0;
	DSPAR(ppi)->parentStats = PP_DEFAULT_PARENTS_STATS;
	DSPAR(ppi)->observedParentClockPhaseChangeRate = 0;
	DSPAR(ppi)->observedParentOffsetScaledLogVariance = 0;
	pp_memcpy(DSPAR(ppi)->grandmasterIdentity, DSDEF(ppi)->clockIdentity,
		  PP_CLOCK_IDENTITY_LENGTH);
	DSPAR(ppi)->grandmasterClockQuality.clockAccuracy =
		DSDEF(ppi)->clockQuality.clockAccuracy;
	DSPAR(ppi)->grandmasterClockQuality.clockClass =
		DSDEF(ppi)->clockQuality.clockClass;
	DSPAR(ppi)->grandmasterClockQuality.offsetScaledLogVariance =
		DSDEF(ppi)->clockQuality.offsetScaledLogVariance;
	DSPAR(ppi)->grandmasterPriority1 = DSDEF(ppi)->priority1;
	DSPAR(ppi)->grandmasterPriority2 = DSDEF(ppi)->priority2;

	/* Time Properties data set */
	DSPRO(ppi)->timeSource = INTERNAL_OSCILLATOR;
}


/* Local clock is synchronized to Ebest Table 16 (9.3.5) of the spec. */
51
void s1(struct pp_instance *ppi, MsgHeader *hdr, MsgAnnounce *ann)
52 53
{
	/* Current DS */
54
	DSCUR(ppi)->stepsRemoved = ann->stepsRemoved + 1;
55 56 57

	/* Parent DS */
	pp_memcpy(DSPAR(ppi)->parentPortIdentity.clockIdentity,
58 59
		hdr->sourcePortIdentity.clockIdentity,
		PP_CLOCK_IDENTITY_LENGTH);
60
	DSPAR(ppi)->parentPortIdentity.portNumber =
61
		hdr->sourcePortIdentity.portNumber;
62 63

	pp_memcpy(DSPAR(ppi)->grandmasterIdentity,
64
		ann->grandmasterIdentity, PP_CLOCK_IDENTITY_LENGTH);
65 66

	DSPAR(ppi)->grandmasterClockQuality.clockAccuracy =
67
		ann->grandmasterClockQuality.clockAccuracy;
68
	DSPAR(ppi)->grandmasterClockQuality.clockClass =
69
		ann->grandmasterClockQuality.clockClass;
70
	DSPAR(ppi)->grandmasterClockQuality.offsetScaledLogVariance =
71 72 73
		ann->grandmasterClockQuality.offsetScaledLogVariance;
	DSPAR(ppi)->grandmasterPriority1 = ann->grandmasterPriority1;
	DSPAR(ppi)->grandmasterPriority2 = ann->grandmasterPriority2;
74 75

	/* Timeproperties DS */
76
	DSPRO(ppi)->currentUtcOffset = ann->currentUtcOffset;
77
	/* "Valid" is bit 2 in second octet of flagfield */
78 79
	DSPRO(ppi)->currentUtcOffsetValid = ((hdr->flagField[1] & FFB_UTCV)
		!= 0);
80

81 82 83 84 85
	DSPRO(ppi)->leap59 = ((hdr->flagField[1] & FFB_LI59) != 0);
	DSPRO(ppi)->leap61 = ((hdr->flagField[1] & FFB_LI61) != 0);
	DSPRO(ppi)->timeTraceable = ((hdr->flagField[1] & FFB_TTRA) != 0);
	DSPRO(ppi)->frequencyTraceable = ((hdr->flagField[1] & FFB_FTRA) != 0);
	DSPRO(ppi)->ptpTimescale = ((hdr->flagField[1] & FFB_PTP) != 0);
86
	DSPRO(ppi)->timeSource = ann->timeSource;
87 88 89
}


90
/* Copy local data set into header and ann message. 9.3.4 table 12. */
91
void copy_d0( struct pp_instance *ppi, MsgHeader *hdr, MsgAnnounce *ann)
92
{
93 94
	ann->grandmasterPriority1 = DSDEF(ppi)->priority1;
	pp_memcpy(ann->grandmasterIdentity, DSDEF(ppi)->clockIdentity,
95
	       PP_CLOCK_IDENTITY_LENGTH);
96
	ann->grandmasterClockQuality.clockClass =
97
		DSDEF(ppi)->clockQuality.clockClass;
98
	ann->grandmasterClockQuality.clockAccuracy =
99
		DSDEF(ppi)->clockQuality.clockAccuracy;
100
	ann->grandmasterClockQuality.offsetScaledLogVariance =
101
		DSDEF(ppi)->clockQuality.offsetScaledLogVariance;
102 103
	ann->grandmasterPriority2 = DSDEF(ppi)->priority2;
	ann->stepsRemoved = 0;
104
	pp_memcpy(hdr->sourcePortIdentity.clockIdentity,
105 106 107 108 109 110 111 112
	       DSDEF(ppi)->clockIdentity, PP_CLOCK_IDENTITY_LENGTH);
}


/*
 * Data set comparison bewteen two foreign masters (9.3.4 fig 27)
 * return similar to memcmp()
 */
113 114 115
Integer8 bmc_dataset_cmp(struct pp_instance *ppi,
			 MsgHeader *hdr_a, MsgAnnounce *ann_a,
			 MsgHeader *hdr_b, MsgAnnounce *ann_b)
116 117 118 119
{
	short comp = 0;
	Octet *ppci;

120 121
	DBGV("Data set comparison\n");

122 123
	/* Identity comparison */
	if (!pp_memcmp(ann_a->grandmasterIdentity,
124
		       ann_b->grandmasterIdentity, PP_CLOCK_IDENTITY_LENGTH)) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

		/* Algorithm part2 Fig 28 */
		if (ann_a->stepsRemoved > ann_b->stepsRemoved + 1)
			return 1;

		else if (ann_b->stepsRemoved > ann_a->stepsRemoved + 1)
			return -1;

		else { /* A within 1 of B */

			ppci = DSPAR(ppi)->parentPortIdentity.clockIdentity;

			if (ann_a->stepsRemoved > ann_b->stepsRemoved) {
				if (!pp_memcmp(
					hdr_a->sourcePortIdentity.clockIdentity,
					ppci,
					PP_CLOCK_IDENTITY_LENGTH)) {
					/* FIXME
					 * DBG("Sender=Receiver : Error -1");
					 */
					return 0;
				} else
					return 1;

			} else if (ann_b->stepsRemoved > ann_a->stepsRemoved) {
				if (!pp_memcmp(
					hdr_b->sourcePortIdentity.clockIdentity,
					ppci,
					PP_CLOCK_IDENTITY_LENGTH)) {
					/* FIXME
					 * DBG("Sender=Receiver : Error -1");
					 */
					return 0;
				} else {
					return -1;
				}
161
			} else { /* steps removed A == steps removed B */
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
				if (!pp_memcmp(
					hdr_a->sourcePortIdentity.clockIdentity,
					hdr_b->sourcePortIdentity.clockIdentity,
					PP_CLOCK_IDENTITY_LENGTH)) {
					/* FIXME
					 * DBG("Sender=Receiver : Error -2");
					 */
					return 0;
				} else if ((pp_memcmp(
					hdr_a->sourcePortIdentity.clockIdentity,
					hdr_b->sourcePortIdentity.clockIdentity,
					PP_CLOCK_IDENTITY_LENGTH)) < 0)
					return -1;
				else
					return 1;
			}
		}
	} else { /* GrandMaster are not identical */
		/* FIXME: rewrite in a more readable way */
181
		if (ann_a->grandmasterPriority1 == ann_b->grandmasterPriority1) {
182 183 184 185 186
			if (ann_a->grandmasterClockQuality.clockClass ==
			    ann_b->grandmasterClockQuality.clockClass) {
				if (ann_a->grandmasterClockQuality.clockAccuracy == ann_b->grandmasterClockQuality.clockAccuracy) {
					if (ann_a->grandmasterClockQuality.offsetScaledLogVariance == ann_b->grandmasterClockQuality.offsetScaledLogVariance) {
						if (ann_a->grandmasterPriority2 == ann_b->grandmasterPriority2) {
187
							comp = pp_memcmp(ann_a->grandmasterIdentity, ann_b->grandmasterIdentity, PP_CLOCK_IDENTITY_LENGTH);
188 189 190 191 192 193 194 195
							if (comp < 0)
								return -1;
							else if (comp > 0)
								return 1;
							else
								return 0;
						} else {
						/* Priority2 are not identical */
196
							comp = pp_memcmp(&ann_a->grandmasterPriority2, &ann_b->grandmasterPriority2, 1);
197 198 199 200 201 202 203 204 205
							if (comp < 0)
								return -1;
							else if (comp > 0)
								return 1;
							else
								return 0;
						}
					} else {
						/* offsetScaledLogVariance are not identical */
206
						comp = pp_memcmp(&ann_a->grandmasterClockQuality.clockClass, &ann_b->grandmasterClockQuality.clockClass, 1);
207 208 209 210 211 212 213 214 215
						if (comp < 0)
							return -1;
						else if (comp > 0)
							return 1;
						else
							return 0;
					}

				} else { /*  Accuracy are not identitcal */
216
					comp = pp_memcmp(&ann_a->grandmasterClockQuality.clockAccuracy, &ann_b->grandmasterClockQuality.clockAccuracy, 1);
217 218 219 220 221 222 223 224
					if (comp < 0)
						return -1;
					else if (comp > 0)
						return 1;
					else
						return 0;
				}
			} else { /* ClockClass are not identical */
225
				comp = pp_memcmp(&ann_a->grandmasterClockQuality.clockClass, &ann_b->grandmasterClockQuality.clockClass, 1);
226 227 228 229 230 231 232 233
				if (comp < 0)
					return -1;
				else if (comp > 0)
					return 1;
				else
					return 0;
			}
		} else { /*  Priority1 are not identical */
234
			comp = pp_memcmp(&ann_a->grandmasterPriority1, &ann_b->grandmasterPriority1, 1);
235 236 237 238 239 240 241 242 243 244 245
			if (comp < 0)
				return -1;
			else if (comp > 0)
				return 1;
			else
				return 0;
		}
	}
}

/* State decision algorithm 9.3.3 Fig 26 */
246 247 248
UInteger8 bmc_state_decision( struct pp_instance *ppi,
			      MsgHeader *hdr, MsgAnnounce *ann,
			      struct pp_runtime_opts *rt_opts)
249 250 251 252
{
	int cmpres;

	if (rt_opts->slave_only) {
253
		s1(ppi, hdr, ann);
254 255 256 257 258 259
		return PPS_SLAVE;
	}

	if ((!ppi->number_foreign_records) && (ppi->state == PPS_LISTENING))
		return PPS_LISTENING;

260
	copy_d0(ppi, &ppi->msg_tmp_header, &ppi->msg_tmp.announce);
261 262


263 264
	cmpres = bmc_dataset_cmp(ppi,
				 &ppi->msg_tmp_header,
265
				 &ppi->msg_tmp.announce,
266
				 hdr, ann);
267 268 269 270 271 272 273

	if (DSDEF(ppi)->clockQuality.clockClass < 128) {

		if (cmpres < 0) {
			m1(ppi);
			return PPS_MASTER;
		} else if (cmpres > 0) {
274
			s1(ppi, hdr, ann);
275 276 277 278 279 280 281 282 283 284 285
			return PPS_PASSIVE;
		} else {
			/* FIXME DBG("Error in bmcDataSetComparison..\n"); */
		}

	} else {

		if (cmpres < 0) {
			m1(ppi);
			return PPS_MASTER;
		} else if (cmpres > 0) {
286
			s1(ppi, hdr, ann);
287 288 289 290 291 292 293 294 295 296 297 298 299 300
			return PPS_SLAVE;
		} else {
			/* FIXME DBG("Error in bmcDataSetComparison..\n"); */
		}

	}

	/*  MB: Is this the return code below correct? */
	/*  Anyway, it's a valid return code. */
	return PPS_FAULTY;
}



301 302
UInteger8 bmc(struct pp_instance *ppi, struct pp_frgn_master *frgn_master,
	      struct pp_runtime_opts *rt_opts)
303 304 305 306 307 308 309 310 311
{
	Integer16 i, best;

	if (!ppi->number_foreign_records)
		if (ppi->state == PPS_MASTER)	{
			m1(ppi);
			return ppi->state;
		}

312
	for (i = 1, best = 0; i < ppi->number_foreign_records; i++)
313 314
		if (bmc_dataset_cmp(ppi,
				     &frgn_master[i].hdr,
315 316
				     &frgn_master[i].ann,
				     &frgn_master[best].hdr,
317
				     &frgn_master[best].ann) < 0)
318 319
			best = i;

320
	DBGV_ARGS("Best record : %d\n", best);
321 322
	ppi->foreign_record_best = best;

323
	return bmc_state_decision(ppi, &frgn_master[best].hdr,
324
				   &frgn_master[best].ann,
325
				   rt_opts);
326
}