1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/**
* @file bmc.c
* @note Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <string.h>
#include "bmc.h"
#include "ds.h"
static int portid_cmp(struct PortIdentity *a, struct PortIdentity *b)
{
int diff = memcmp(&a->clockIdentity, &b->clockIdentity, sizeof(a->clockIdentity));
if (diff == 0) {
diff = a->portNumber - b->portNumber;
}
return diff;
}
int dscmp2(struct dataset *a, struct dataset *b)
{
int diff;
unsigned int A = a->stepsRemoved, B = b->stepsRemoved;
if (A + 1 < B)
return A_BETTER;
if (B + 1 < A)
return B_BETTER;
/*
* We ignore the "error-1" conditions mentioned in the
* standard, since there is nothing we can do about it anyway.
*/
if (A < B) {
diff = portid_cmp(&b->receiver, &b->sender);
if (diff < 0)
return A_BETTER;
if (diff > 0)
return A_BETTER_TOPO;
/* error-1 */
return 0;
}
if (A > B) {
diff = portid_cmp(&a->receiver, &a->sender);
if (diff < 0)
return B_BETTER;
if (diff > 0)
return B_BETTER_TOPO;
/* error-1 */
return 0;
}
diff = portid_cmp(&a->sender, &b->sender);
if (diff < 0)
return A_BETTER_TOPO;
if (diff > 0)
return B_BETTER_TOPO;
if (a->receiver.portNumber < b->receiver.portNumber)
return A_BETTER_TOPO;
if (a->receiver.portNumber > b->receiver.portNumber)
return B_BETTER_TOPO;
/*
* If we got this far, it means "error-2" has occured.
*/
return 0;
}
int dscmp(struct dataset *a, struct dataset *b)
{
int diff;
if (a == b)
return 0;
if (a && !b)
return A_BETTER;
if (b && !a)
return B_BETTER;
diff = memcmp(&a->identity, &b->identity, sizeof(a->identity));
if (!diff)
return dscmp2(a, b);
if (a->priority1 < b->priority1)
return A_BETTER;
if (a->priority1 > b->priority1)
return B_BETTER;
if (a->quality.clockClass < b->quality.clockClass)
return A_BETTER;
if (a->quality.clockClass > b->quality.clockClass)
return B_BETTER;
if (a->quality.clockAccuracy < b->quality.clockAccuracy)
return A_BETTER;
if (a->quality.clockAccuracy > b->quality.clockAccuracy)
return B_BETTER;
if (a->quality.offsetScaledLogVariance <
b->quality.offsetScaledLogVariance)
return A_BETTER;
if (a->quality.offsetScaledLogVariance >
b->quality.offsetScaledLogVariance)
return B_BETTER;
if (a->priority2 < b->priority2)
return A_BETTER;
if (a->priority2 > b->priority2)
return B_BETTER;
return diff < 0 ? A_BETTER : B_BETTER;
}
enum port_state bmc_state_decision(struct clock *c, struct port *r,
int (*compare)(struct dataset *a, struct dataset *b))
{
struct dataset *clock_ds, *clock_best, *port_best;
enum port_state ps;
clock_ds = clock_default_ds(c);
clock_best = clock_best_foreign(c);
port_best = port_best_foreign(r);
ps = port_state(r);
/*
* This scenario is particularly important in the designated_slave_fsm
* when it is in PS_SLAVE state. In this scenario, there is no other
* foreign master and it will elect itself as master ultimately
* resulting in printing out some unnecessary warnings (see
* port_slave_priority_warning()).
*/
if (!port_best && port_bmca(r) == BMCA_NOOP) {
return ps;
}
if (!port_best && PS_LISTENING == ps)
return ps;
if (clock_class(c) <= 127) {
if (compare(clock_ds, port_best) > 0) {
return PS_GRAND_MASTER; /*M1*/
} else {
return PS_PASSIVE; /*P1*/
}
}
if (compare(clock_ds, clock_best) > 0) {
return PS_GRAND_MASTER; /*M2*/
}
if (clock_best_port(c) == r) {
return PS_SLAVE; /*S1*/
}
if (compare(clock_best, port_best) == A_BETTER_TOPO) {
return PS_PASSIVE; /*P2*/
} else {
return PS_MASTER; /*M3*/
}
}
|