bmc.c 9.12 KB
Newer Older
1 2 3
/*
 * FIXME header
 */
4
#include <pptp/pptp.h>
5
#include <pptp/diag.h>
6 7 8 9 10 11 12 13 14 15 16 17 18

/* 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,
19
		  DSDEF(ppi)->clockIdentity, PP_CLOCK_IDENTITY_LENGTH);
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
	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. */
41
void s1(struct pp_instance *ppi, MsgHeader *hdr, MsgAnnounce *ann)
42 43
{
	/* Current DS */
44
	DSCUR(ppi)->stepsRemoved = ann->stepsRemoved + 1;
45 46 47

	/* Parent DS */
	pp_memcpy(DSPAR(ppi)->parentPortIdentity.clockIdentity,
48 49
		hdr->sourcePortIdentity.clockIdentity,
		PP_CLOCK_IDENTITY_LENGTH);
50
	DSPAR(ppi)->parentPortIdentity.portNumber =
51
		hdr->sourcePortIdentity.portNumber;
52 53

	pp_memcpy(DSPAR(ppi)->grandmasterIdentity,
54
		ann->grandmasterIdentity, PP_CLOCK_IDENTITY_LENGTH);
55 56

	DSPAR(ppi)->grandmasterClockQuality.clockAccuracy =
57
		ann->grandmasterClockQuality.clockAccuracy;
58
	DSPAR(ppi)->grandmasterClockQuality.clockClass =
59
		ann->grandmasterClockQuality.clockClass;
60
	DSPAR(ppi)->grandmasterClockQuality.offsetScaledLogVariance =
61 62 63
		ann->grandmasterClockQuality.offsetScaledLogVariance;
	DSPAR(ppi)->grandmasterPriority1 = ann->grandmasterPriority1;
	DSPAR(ppi)->grandmasterPriority2 = ann->grandmasterPriority2;
64 65

	/* Timeproperties DS */
66
	DSPRO(ppi)->currentUtcOffset = ann->currentUtcOffset;
67
	/* "Valid" is bit 2 in second octet of flagfield */
68
	DSPRO(ppi)->currentUtcOffsetValid = ((hdr->flagField[1] & 0x04) ==
69 70
					   0x04);

71 72 73 74
	DSPRO(ppi)->leap59 = ((hdr->flagField[1] & 0x02) == 0x02);
	DSPRO(ppi)->leap61 = ((hdr->flagField[1] & 0x01) == 0x01);
	DSPRO(ppi)->timeTraceable = ((hdr->flagField[1] & 0x10) == 0x10);
	DSPRO(ppi)->frequencyTraceable = ((hdr->flagField[1] & 0x20)
75
					   == 0x20);
76
	DSPRO(ppi)->ptpTimescale = ((hdr->flagField[1] & 0x08) == 0x08);
77
	DSPRO(ppi)->timeSource = ann->timeSource;
78 79 80
}


81
/* Copy local data set into header and ann message. 9.3.4 table 12. */
82
void copy_d0( struct pp_instance *ppi, MsgHeader *hdr, MsgAnnounce *ann)
83
{
84 85
	ann->grandmasterPriority1 = DSDEF(ppi)->priority1;
	pp_memcpy(ann->grandmasterIdentity, DSDEF(ppi)->clockIdentity,
86
	       PP_CLOCK_IDENTITY_LENGTH);
87
	ann->grandmasterClockQuality.clockClass =
88
		DSDEF(ppi)->clockQuality.clockClass;
89
	ann->grandmasterClockQuality.clockAccuracy =
90
		DSDEF(ppi)->clockQuality.clockAccuracy;
91
	ann->grandmasterClockQuality.offsetScaledLogVariance =
92
		DSDEF(ppi)->clockQuality.offsetScaledLogVariance;
93 94
	ann->grandmasterPriority2 = DSDEF(ppi)->priority2;
	ann->stepsRemoved = 0;
95
	pp_memcpy(hdr->sourcePortIdentity.clockIdentity,
96 97 98 99 100 101 102 103
	       DSDEF(ppi)->clockIdentity, PP_CLOCK_IDENTITY_LENGTH);
}


/*
 * Data set comparison bewteen two foreign masters (9.3.4 fig 27)
 * return similar to memcmp()
 */
104 105 106
Integer8 bmc_dataset_cmp(struct pp_instance *ppi,
			 MsgHeader *hdr_a, MsgAnnounce *ann_a,
			 MsgHeader *hdr_b, MsgAnnounce *ann_b)
107 108 109 110
{
	short comp = 0;
	Octet *ppci;

111 112
	DBGV("Data set comparison\n");

113 114
	/* Identity comparison */
	if (!pp_memcmp(ann_a->grandmasterIdentity,
115
		       ann_b->grandmasterIdentity, PP_CLOCK_IDENTITY_LENGTH)) {
116 117 118 119 120 121 122 123 124 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

		/* 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;
				}
152
			} else { /* steps removed A == steps removed B */
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
				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 */
172
		if (ann_a->grandmasterPriority1 == ann_b->grandmasterPriority1) {
173 174 175 176 177
			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) {
178
							comp = pp_memcmp(ann_a->grandmasterIdentity, ann_b->grandmasterIdentity, PP_CLOCK_IDENTITY_LENGTH);
179 180 181 182 183 184 185 186
							if (comp < 0)
								return -1;
							else if (comp > 0)
								return 1;
							else
								return 0;
						} else {
						/* Priority2 are not identical */
187
							comp = pp_memcmp(&ann_a->grandmasterPriority2, &ann_b->grandmasterPriority2, 1);
188 189 190 191 192 193 194 195 196
							if (comp < 0)
								return -1;
							else if (comp > 0)
								return 1;
							else
								return 0;
						}
					} else {
						/* offsetScaledLogVariance are not identical */
197
						comp = pp_memcmp(&ann_a->grandmasterClockQuality.clockClass, &ann_b->grandmasterClockQuality.clockClass, 1);
198 199 200 201 202 203 204 205 206
						if (comp < 0)
							return -1;
						else if (comp > 0)
							return 1;
						else
							return 0;
					}

				} else { /*  Accuracy are not identitcal */
207
					comp = pp_memcmp(&ann_a->grandmasterClockQuality.clockAccuracy, &ann_b->grandmasterClockQuality.clockAccuracy, 1);
208 209 210 211 212 213 214 215
					if (comp < 0)
						return -1;
					else if (comp > 0)
						return 1;
					else
						return 0;
				}
			} else { /* ClockClass are not identical */
216
				comp = pp_memcmp(&ann_a->grandmasterClockQuality.clockClass, &ann_b->grandmasterClockQuality.clockClass, 1);
217 218 219 220 221 222 223 224
				if (comp < 0)
					return -1;
				else if (comp > 0)
					return 1;
				else
					return 0;
			}
		} else { /*  Priority1 are not identical */
225
			comp = pp_memcmp(&ann_a->grandmasterPriority1, &ann_b->grandmasterPriority1, 1);
226 227 228 229 230 231 232 233 234 235 236
			if (comp < 0)
				return -1;
			else if (comp > 0)
				return 1;
			else
				return 0;
		}
	}
}

/* State decision algorithm 9.3.3 Fig 26 */
237 238 239
UInteger8 bmc_state_decision( struct pp_instance *ppi,
			      MsgHeader *hdr, MsgAnnounce *ann,
			      struct pp_runtime_opts *rt_opts)
240 241 242 243
{
	int cmpres;

	if (rt_opts->slave_only) {
244
		s1(ppi, hdr, ann);
245 246 247 248 249 250
		return PPS_SLAVE;
	}

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

251
	copy_d0(ppi, &ppi->msg_tmp_header, &ppi->msg_tmp.announce);
252 253


254 255
	cmpres = bmc_dataset_cmp(ppi,
				 &ppi->msg_tmp_header,
256
				 &ppi->msg_tmp.announce,
257
				 hdr, ann);
258 259 260 261 262 263 264

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

		if (cmpres < 0) {
			m1(ppi);
			return PPS_MASTER;
		} else if (cmpres > 0) {
265
			s1(ppi, hdr, ann);
266 267 268 269 270 271 272 273 274 275 276
			return PPS_PASSIVE;
		} else {
			/* FIXME DBG("Error in bmcDataSetComparison..\n"); */
		}

	} else {

		if (cmpres < 0) {
			m1(ppi);
			return PPS_MASTER;
		} else if (cmpres > 0) {
277
			s1(ppi, hdr, ann);
278 279 280 281 282 283 284 285 286 287 288 289 290 291
			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;
}



292 293
UInteger8 bmc(struct pp_instance *ppi, struct pp_frgn_master *frgn_master,
	      struct pp_runtime_opts *rt_opts)
294 295 296 297 298 299 300 301 302
{
	Integer16 i, best;

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

303
	for (i = 1, best = 0; i < ppi->number_foreign_records; i++)
304 305
		if (bmc_dataset_cmp(ppi,
				     &frgn_master[i].hdr,
306 307
				     &frgn_master[i].ann,
				     &frgn_master[best].hdr,
308
				     &frgn_master[best].ann) < 0)
309 310
			best = i;

311
	DBGV_ARGS("Best record : %d\n", best);
312 313
	ppi->foreign_record_best = best;

314
	return bmc_state_decision(ppi, &frgn_master[best].hdr,
315
				   &frgn_master[best].ann,
316
				   rt_opts);
317
}