Blame bmc.c

Packit 9c3e7e
/**
Packit 9c3e7e
 * @file bmc.c
Packit 9c3e7e
 * @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is free software; you can redistribute it and/or modify
Packit 9c3e7e
 * it under the terms of the GNU General Public License as published by
Packit 9c3e7e
 * the Free Software Foundation; either version 2 of the License, or
Packit 9c3e7e
 * (at your option) any later version.
Packit 9c3e7e
 *
Packit 9c3e7e
 * This program is distributed in the hope that it will be useful,
Packit 9c3e7e
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9c3e7e
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9c3e7e
 * GNU General Public License for more details.
Packit 9c3e7e
 *
Packit 9c3e7e
 * You should have received a copy of the GNU General Public License along
Packit 9c3e7e
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 9c3e7e
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 9c3e7e
 */
Packit 9c3e7e
#include <string.h>
Packit 9c3e7e
Packit 9c3e7e
#include "bmc.h"
Packit 9c3e7e
#include "ds.h"
Packit 9c3e7e
Packit 9c3e7e
int dscmp2(struct dataset *a, struct dataset *b)
Packit 9c3e7e
{
Packit 9c3e7e
	int diff;
Packit 9c3e7e
	unsigned int A = a->stepsRemoved, B = b->stepsRemoved;
Packit 9c3e7e
Packit 9c3e7e
	if (A + 1 < B)
Packit 9c3e7e
		return A_BETTER;
Packit 9c3e7e
	if (B + 1 < A)
Packit 9c3e7e
		return B_BETTER;
Packit 9c3e7e
	/*
Packit 9c3e7e
	 * We ignore the "error-1" conditions mentioned in the
Packit 9c3e7e
	 * standard, since there is nothing we can do about it anyway.
Packit 9c3e7e
	 */
Packit 9c3e7e
	if (A < B) {
Packit 9c3e7e
		diff = memcmp(&b->receiver, &b->sender, sizeof(b->receiver));
Packit 9c3e7e
		if (diff < 0)
Packit 9c3e7e
			return A_BETTER;
Packit 9c3e7e
		if (diff > 0)
Packit 9c3e7e
			return A_BETTER_TOPO;
Packit 9c3e7e
		/* error-1 */
Packit 9c3e7e
		return 0;
Packit 9c3e7e
	}
Packit 9c3e7e
	if (A > B) {
Packit 9c3e7e
		diff = memcmp(&a->receiver, &a->sender, sizeof(a->receiver));
Packit 9c3e7e
		if (diff < 0)
Packit 9c3e7e
			return B_BETTER;
Packit 9c3e7e
		if (diff > 0)
Packit 9c3e7e
			return B_BETTER_TOPO;
Packit 9c3e7e
		/* error-1 */
Packit 9c3e7e
		return 0;
Packit 9c3e7e
	}
Packit 9c3e7e
Packit 9c3e7e
	diff = memcmp(&a->sender, &b->sender, sizeof(a->sender));
Packit 9c3e7e
	if (diff < 0)
Packit 9c3e7e
		return A_BETTER_TOPO;
Packit 9c3e7e
	if (diff > 0)
Packit 9c3e7e
		return B_BETTER_TOPO;
Packit 9c3e7e
Packit 9c3e7e
	if (a->receiver.portNumber < b->receiver.portNumber)
Packit 9c3e7e
		return A_BETTER_TOPO;
Packit 9c3e7e
	if (a->receiver.portNumber > b->receiver.portNumber)
Packit 9c3e7e
		return B_BETTER_TOPO;
Packit 9c3e7e
	/*
Packit 9c3e7e
	 * If we got this far, it means "error-2" has occured.
Packit 9c3e7e
	 */
Packit 9c3e7e
	return 0;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
int dscmp(struct dataset *a, struct dataset *b)
Packit 9c3e7e
{
Packit 9c3e7e
	int diff;
Packit 9c3e7e
Packit 9c3e7e
	if (a == b)
Packit 9c3e7e
		return 0;
Packit 9c3e7e
	if (a && !b)
Packit 9c3e7e
		return A_BETTER;
Packit 9c3e7e
	if (b && !a)
Packit 9c3e7e
		return B_BETTER;
Packit 9c3e7e
Packit 9c3e7e
	diff = memcmp(&a->identity, &b->identity, sizeof(a->identity));
Packit 9c3e7e
Packit 9c3e7e
	if (!diff)
Packit 9c3e7e
		return dscmp2(a, b);
Packit 9c3e7e
Packit 9c3e7e
	if (a->priority1 < b->priority1)
Packit 9c3e7e
		return A_BETTER;
Packit 9c3e7e
	if (a->priority1 > b->priority1)
Packit 9c3e7e
		return B_BETTER;
Packit 9c3e7e
Packit 9c3e7e
	if (a->quality.clockClass < b->quality.clockClass)
Packit 9c3e7e
		return A_BETTER;
Packit 9c3e7e
	if (a->quality.clockClass > b->quality.clockClass)
Packit 9c3e7e
		return B_BETTER;
Packit 9c3e7e
Packit 9c3e7e
	if (a->quality.clockAccuracy < b->quality.clockAccuracy)
Packit 9c3e7e
		return A_BETTER;
Packit 9c3e7e
	if (a->quality.clockAccuracy > b->quality.clockAccuracy)
Packit 9c3e7e
		return B_BETTER;
Packit 9c3e7e
Packit 9c3e7e
	if (a->quality.offsetScaledLogVariance <
Packit 9c3e7e
	    b->quality.offsetScaledLogVariance)
Packit 9c3e7e
		return A_BETTER;
Packit 9c3e7e
	if (a->quality.offsetScaledLogVariance >
Packit 9c3e7e
	    b->quality.offsetScaledLogVariance)
Packit 9c3e7e
		return B_BETTER;
Packit 9c3e7e
Packit 9c3e7e
	if (a->priority2 < b->priority2)
Packit 9c3e7e
		return A_BETTER;
Packit 9c3e7e
	if (a->priority2 > b->priority2)
Packit 9c3e7e
		return B_BETTER;
Packit 9c3e7e
Packit 9c3e7e
	return diff < 0 ? A_BETTER : B_BETTER;
Packit 9c3e7e
}
Packit 9c3e7e
Packit 9c3e7e
enum port_state bmc_state_decision(struct clock *c, struct port *r,
Packit 9c3e7e
				   int (*compare)(struct dataset *a, struct dataset *b))
Packit 9c3e7e
{
Packit 9c3e7e
	struct dataset *clock_ds, *clock_best, *port_best;
Packit 9c3e7e
	enum port_state ps;
Packit 9c3e7e
Packit 9c3e7e
	clock_ds = clock_default_ds(c);
Packit 9c3e7e
	clock_best = clock_best_foreign(c);
Packit 9c3e7e
	port_best = port_best_foreign(r);
Packit 9c3e7e
	ps = port_state(r);
Packit 9c3e7e
Packit 9c3e7e
	if (!port_best && PS_LISTENING == ps)
Packit 9c3e7e
		return ps;
Packit 9c3e7e
Packit 9c3e7e
	if (clock_class(c) <= 127) {
Packit 9c3e7e
		if (compare(clock_ds, port_best) > 0) {
Packit 9c3e7e
			return PS_GRAND_MASTER; /*M1*/
Packit 9c3e7e
		} else {
Packit 9c3e7e
			return PS_PASSIVE; /*P1*/
Packit 9c3e7e
		}
Packit 9c3e7e
	}
Packit 9c3e7e
Packit 9c3e7e
	if (compare(clock_ds, clock_best) > 0) {
Packit 9c3e7e
		return PS_GRAND_MASTER; /*M2*/
Packit 9c3e7e
	}
Packit 9c3e7e
Packit 9c3e7e
	if (clock_best_port(c) == r) {
Packit 9c3e7e
		return PS_SLAVE; /*S1*/
Packit 9c3e7e
	}
Packit 9c3e7e
Packit 9c3e7e
	if (compare(clock_best, port_best) == A_BETTER_TOPO) {
Packit 9c3e7e
		return PS_PASSIVE; /*P2*/
Packit 9c3e7e
	} else {
Packit 9c3e7e
		return PS_MASTER; /*M3*/
Packit 9c3e7e
	}
Packit 9c3e7e
}