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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
|
/*
* Copyright (c) 2011-2015 M3S, Srl - Italy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Rapid Spanning Tree Protocol (IEEE 802.1D-2004) state machines
* implementation.
*
* Authors:
* Martino Fornasa <mf@fornasa.it>
* Daniele Venturino <daniele.venturino@m3s.it>
* Carlo Andreotti <c.andreotti@m3s.it>
*
* References to IEEE 802.1D-2004 standard are enclosed in square brackets.
* E.g. [17.3], [Table 17-1], etc.
*
*/
#include <config.h>
#include "rstp.h"
#include "rstp-state-machines.h"
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <stdlib.h>
#include "byte-order.h"
#include "connectivity.h"
#include "openvswitch/ofpbuf.h"
#include "dp-packet.h"
#include "packets.h"
#include "seq.h"
#include "unixctl.h"
#include "util.h"
#include "openvswitch/vlog.h"
VLOG_DEFINE_THIS_MODULE(rstp_sm);
#define ROLE_FLAG_MASK 0xC
#define ROLE_FLAG_SHIFT 2
enum port_flag {
PORT_UNKN = 0,
PORT_ALT_BACK = 1,
PORT_ROOT = 2,
PORT_DES = 3
};
enum bpdu_size {
CONFIGURATION_BPDU_SIZE = 35,
TOPOLOGY_CHANGE_NOTIFICATION_BPDU_SIZE = 4,
RAPID_SPANNING_TREE_BPDU_SIZE = 36
};
/* Same is a subset of SUPERIOR, so can be used as a boolean when the
* distinction is not significant. */
enum vector_comparison {
INFERIOR = 0,
SUPERIOR = 1,
SAME = 2
};
static void decrement_timer(uint16_t *);
static void rstp_send_bpdu(struct rstp_port *, const void *, size_t)
OVS_REQUIRES(rstp_mutex);
static int validate_received_bpdu(struct rstp_port *, const void *, size_t)
OVS_REQUIRES(rstp_mutex);
static ovs_be16 time_encode(uint8_t);
static uint8_t time_decode(ovs_be16);
static enum vector_comparison
compare_rstp_priority_vectors(const struct rstp_priority_vector *,
const struct rstp_priority_vector *);
static bool rstp_times_equal(struct rstp_times *, struct rstp_times *);
/* Per-Bridge State Machine */
static int port_role_selection_sm(struct rstp *)
OVS_REQUIRES(rstp_mutex);
/* Per-Port State Machines */
static int port_receive_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_protocol_migration_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int bridge_detection_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_transmit_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_information_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_role_transition_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int port_state_transition_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
static int topology_change_sm(struct rstp_port *)
OVS_REQUIRES(rstp_mutex);
/* port_timers_sm() not defined as a state machine */
void
process_received_bpdu__(struct rstp_port *p, const void *bpdu_,
size_t bpdu_size)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *rstp = p->rstp;
struct rstp_bpdu *bpdu = (struct rstp_bpdu *)bpdu_;
if (!p->port_enabled) {
return;
}
if (p->rcvd_bpdu) {
return;
}
/* [9.2.9 Encoding of Port Role values]
* NOTE. If the Unknown value of the Port Role parameter is received, the
* state machines will effectively treat the RST BPDU as if it were a
* Configuration BPDU.
*/
if (bpdu->bpdu_type == RAPID_SPANNING_TREE_BPDU) {
uint8_t role = (bpdu->flags & ROLE_FLAG_MASK) >> ROLE_FLAG_SHIFT;
if (role == PORT_UNKN) {
bpdu->bpdu_type = CONFIGURATION_BPDU;
}
}
if (validate_received_bpdu(p, bpdu, bpdu_size) == 0) {
p->rcvd_bpdu = true;
p->rx_rstp_bpdu_cnt++;
memcpy(&p->received_bpdu_buffer, bpdu, sizeof(struct rstp_bpdu));
rstp->changes = true;
move_rstp__(rstp);
} else {
VLOG_DBG("%s, port %u: Bad STP or RSTP BPDU received", p->rstp->name,
p->port_number);
p->error_count++;
}
}
/* Returns 0 on success. */
static int
validate_received_bpdu(struct rstp_port *p, const void *bpdu, size_t bpdu_size)
OVS_REQUIRES(rstp_mutex)
{
/* Validation of received BPDU, see [9.3.4]. */
const struct rstp_bpdu *temp;
temp = bpdu;
if (bpdu_size < TOPOLOGY_CHANGE_NOTIFICATION_BPDU_SIZE ||
ntohs(temp->protocol_identifier) != 0) {
return -1;
} else {
if (temp->bpdu_type == CONFIGURATION_BPDU
&& bpdu_size >= CONFIGURATION_BPDU_SIZE
&& (time_decode(temp->message_age) < time_decode(temp->max_age))) {
if ((ntohll(temp->designated_bridge_id) !=
p->rstp->bridge_identifier)
|| ((ntohll(temp->designated_bridge_id) ==
p->rstp->bridge_identifier)
&& (ntohs(temp->designated_port_id) != p->port_id))) {
return 0;
} else {
return -1;
}
} else if (temp->bpdu_type == TOPOLOGY_CHANGE_NOTIFICATION_BPDU) {
return 0;
} else if (temp->bpdu_type == RAPID_SPANNING_TREE_BPDU &&
bpdu_size >= RAPID_SPANNING_TREE_BPDU_SIZE) {
return 0;
} else {
return -1;
}
}
}
/*
* move_rstp__()
* This method is invoked to move the State Machines. The SMs move only if the
* boolean 'changes' is true, meaning that something changed and the SMs need
* to work to process this change.
* The boolean 'changes' is set every time a SM modifies its state, a BPDU is
* received, a timer expires or port down event is detected. If a parameter is
* set by management, then 'changes' is set.
*/
#define MAX_RSTP_ITERATIONS 1000 /* safeguard */
int
move_rstp__(struct rstp *rstp)
OVS_REQUIRES(rstp_mutex)
{
int num_iterations;
num_iterations = 0;
while (rstp->changes == true && num_iterations < MAX_RSTP_ITERATIONS) {
struct rstp_port *p;
VLOG_DBG("%s: move_rstp()", rstp->name);
rstp->changes = false;
port_role_selection_sm(rstp);
HMAP_FOR_EACH (p, node, &rstp->ports) {
if (p->rstp_state != RSTP_DISABLED) {
port_receive_sm(p);
bridge_detection_sm(p);
port_information_sm(p);
port_role_transition_sm(p);
port_state_transition_sm(p);
topology_change_sm(p);
port_transmit_sm(p);
port_protocol_migration_sm(p);
}
}
num_iterations++;
seq_change(connectivity_seq_get());
}
if (num_iterations >= MAX_RSTP_ITERATIONS) {
VLOG_ERR("%s: move_rstp() reached the iteration safeguard limit!",
rstp->name);
}
return 0;
}
void decrease_rstp_port_timers__(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
decrement_timer(&p->hello_when);
decrement_timer(&p->tc_while);
decrement_timer(&p->fd_while);
decrement_timer(&p->rcvd_info_while);
decrement_timer(&p->rr_while);
decrement_timer(&p->rb_while);
decrement_timer(&p->mdelay_while);
decrement_timer(&p->edge_delay_while);
decrement_timer(&p->tx_count);
p->uptime += 1;
}
r->changes = true;
move_rstp__(r);
}
static void
decrement_timer(uint16_t *timer)
{
if (*timer != 0) {
*timer -= 1;
}
}
/* Bridge State Machine. */
/* [17.28] Port Role Selection state machine. */
static void
updt_role_disabled_tree(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
p->selected_role = ROLE_DISABLED;
}
}
static void
clear_reselect_tree(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
p->reselect = false;
}
}
void
updt_roles_tree__(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
int vsel;
struct rstp_priority_vector best_vector, candidate_vector;
enum rstp_port_role new_root_old_role = ROLE_DESIGNATED;
uint16_t old_root_port_number = 0;
uint16_t new_root_port_number = 0;
old_root_port_number = r->root_port_id & 0x00ff;
if (old_root_port_number) {
r->old_root_aux = rstp_get_port_aux__(r, old_root_port_number);
}
vsel = -1;
best_vector = r->bridge_priority;
/* Letter c1) */
r->root_times = r->bridge_times;
/* Letters a) b) c) */
HMAP_FOR_EACH (p, node, &r->ports) {
uint32_t old_root_path_cost;
uint32_t root_path_cost;
if (p->info_is != INFO_IS_RECEIVED) {
continue;
}
/* [17.6] */
candidate_vector = p->port_priority;
candidate_vector.bridge_port_id = p->port_id;
old_root_path_cost = candidate_vector.root_path_cost;
root_path_cost = old_root_path_cost + p->port_path_cost;
candidate_vector.root_path_cost = root_path_cost;
if ((candidate_vector.designated_bridge_id & 0xffffffffffffULL) ==
(r->bridge_priority.designated_bridge_id & 0xffffffffffffULL)) {
continue;
}
if (compare_rstp_priority_vectors(&candidate_vector,
&best_vector) == SUPERIOR) {
best_vector = candidate_vector;
r->root_times = p->port_times;
r->root_times.message_age++;
vsel = p->port_number;
new_root_old_role = p->role;
}
}
r->root_priority = best_vector;
r->root_port_id = best_vector.bridge_port_id;
VLOG_DBG("%s: new Root is "RSTP_ID_FMT, r->name,
RSTP_ID_ARGS(r->root_priority.root_bridge_id));
new_root_port_number = r->root_port_id & 0x00ff;
if (new_root_port_number) {
r->new_root_aux = rstp_get_port_aux__(r, new_root_port_number);
}
/* Shift learned MAC addresses from an old Root Port to an existing
* Alternate Port. */
if (!r->root_changed
&& new_root_old_role == ROLE_ALTERNATE
&& new_root_port_number
&& old_root_port_number
&& new_root_port_number != old_root_port_number) {
r->root_changed = true;
}
/* Letters d) e) */
HMAP_FOR_EACH (p, node, &r->ports) {
p->designated_priority_vector.root_bridge_id =
r->root_priority.root_bridge_id;
p->designated_priority_vector.root_path_cost =
r->root_priority.root_path_cost;
p->designated_priority_vector.designated_bridge_id =
r->bridge_identifier;
p->designated_priority_vector.designated_port_id =
p->port_id;
p->designated_times = r->root_times;
p->designated_times.hello_time = r->bridge_times.hello_time;
}
HMAP_FOR_EACH (p, node, &r->ports) {
switch (p->info_is) {
case INFO_IS_DISABLED:
p->selected_role = ROLE_DISABLED;
break;
case INFO_IS_AGED:
p->updt_info = true;
p->selected_role = ROLE_DESIGNATED;
break;
case INFO_IS_MINE:
p->selected_role = ROLE_DESIGNATED;
if (compare_rstp_priority_vectors(
&p->port_priority, &p->designated_priority_vector) != SAME
|| !rstp_times_equal(&p->designated_times, &r->root_times)) {
p->updt_info = true;
}
break;
case INFO_IS_RECEIVED:
if (vsel == p->port_number) { /* Letter i) */
p->selected_role = ROLE_ROOT;
p->updt_info = false;
} else if (compare_rstp_priority_vectors(
&p->designated_priority_vector,
&p->port_priority) == INFERIOR) {
if (p->port_priority.designated_bridge_id !=
r->bridge_identifier) {
p->selected_role = ROLE_ALTERNATE;
p->updt_info = false;
} else {
p->selected_role = ROLE_BACKUP;
p->updt_info = false;
}
} else {
p->selected_role = ROLE_DESIGNATED;
p->updt_info = true;
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
}
seq_change(connectivity_seq_get());
}
static void
set_selected_tree(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
if (p->reselect) {
return;
}
}
HMAP_FOR_EACH (p, node, &r->ports) {
p->selected = true;
}
}
static int
port_role_selection_sm(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
enum port_role_selection_state_machine old_state;
struct rstp_port *p;
old_state = r->port_role_selection_sm_state;
switch (r->port_role_selection_sm_state) {
case PORT_ROLE_SELECTION_SM_INIT:
if (r->begin) {
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_INIT_BRIDGE_EXEC;
}
break;
case PORT_ROLE_SELECTION_SM_INIT_BRIDGE_EXEC:
updt_role_disabled_tree(r);
r->port_role_selection_sm_state = PORT_ROLE_SELECTION_SM_INIT_BRIDGE;
/* fall through */
case PORT_ROLE_SELECTION_SM_INIT_BRIDGE:
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_ROLE_SELECTION_EXEC;
break;
case PORT_ROLE_SELECTION_SM_ROLE_SELECTION_EXEC:
clear_reselect_tree(r);
updt_roles_tree__(r);
set_selected_tree(r);
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_ROLE_SELECTION;
/* fall through */
case PORT_ROLE_SELECTION_SM_ROLE_SELECTION:
HMAP_FOR_EACH (p, node, &r->ports) {
if (p->reselect) {
r->port_role_selection_sm_state =
PORT_ROLE_SELECTION_SM_ROLE_SELECTION_EXEC;
break;
}
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != r->port_role_selection_sm_state) {
r->changes = true;
VLOG_DBG("%s: Port_role_selection_sm %d -> %d", r->name,
old_state, r->port_role_selection_sm_state);
}
return 0;
}
/* Port State Machines */
/* [17.23 - Port receive state machine] */
static void
updt_bpdu_version(struct rstp_port *p) /* [17.21.22] */
OVS_REQUIRES(rstp_mutex)
{
switch (p->received_bpdu_buffer.bpdu_type) {
case CONFIGURATION_BPDU:
case TOPOLOGY_CHANGE_NOTIFICATION_BPDU:
p->rcvd_rstp = false;
p->rcvd_stp = true;
break;
case RAPID_SPANNING_TREE_BPDU:
p->rcvd_rstp = true;
p->rcvd_stp = false;
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
}
static int
port_receive_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_receive_state_machine old_state;
struct rstp *r;
old_state = p->port_receive_sm_state;
r = p->rstp;
switch (p->port_receive_sm_state) {
case PORT_RECEIVE_SM_INIT:
if (r->begin || ((p->rcvd_bpdu || (p->edge_delay_while !=
r->migrate_time)) && !p->port_enabled)) {
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD_EXEC;
}
break;
case PORT_RECEIVE_SM_DISCARD_EXEC:
p->rcvd_bpdu = p->rcvd_rstp = p->rcvd_stp = false;
p->rcvd_msg = false;
p->edge_delay_while = r->migrate_time;
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD;
/* fall through */
case PORT_RECEIVE_SM_DISCARD:
if ((p->rcvd_bpdu || (p->edge_delay_while != r->migrate_time))
&& !p->port_enabled) {
/* Global transition. */
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD_EXEC;
} else if (p->rcvd_bpdu && p->port_enabled) {
p->port_receive_sm_state = PORT_RECEIVE_SM_RECEIVE_EXEC;
}
break;
case PORT_RECEIVE_SM_RECEIVE_EXEC:
updt_bpdu_version(p);
p->oper_edge = p->rcvd_bpdu = false;
p->rcvd_msg = true;
p->edge_delay_while = r->migrate_time;
p->port_receive_sm_state = PORT_RECEIVE_SM_RECEIVE;
/* fall through */
case PORT_RECEIVE_SM_RECEIVE:
if ((p->rcvd_bpdu || (p->edge_delay_while != r->migrate_time))
&& !p->port_enabled) {
/* Global transition. */
p->port_receive_sm_state = PORT_RECEIVE_SM_DISCARD_EXEC;
} else if (p->rcvd_bpdu && p->port_enabled && !p->rcvd_msg) {
p->port_receive_sm_state = PORT_RECEIVE_SM_RECEIVE_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->port_receive_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: Port_receive_sm %d -> %d", p->rstp->name,
p->port_number, old_state, p->port_receive_sm_state);
}
return 0;
}
/* [17.24 - Port Protocol Migration state machine] */
static int
port_protocol_migration_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_protocol_migration_state_machine old_state;
struct rstp *r;
old_state = p->port_protocol_migration_sm_state;
r = p->rstp;
switch (p->port_protocol_migration_sm_state) {
case PORT_PROTOCOL_MIGRATION_SM_INIT:
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC;
/* fall through */
case PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC:
p->mcheck = false;
p->send_rstp = r->rstp_version;
p->mdelay_while = r->migrate_time;
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP;
/* fall through */
case PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP:
if (p->mdelay_while == 0) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SENSING_EXEC;
} else if ((p->mdelay_while != r->migrate_time) && !p->port_enabled) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC;
}
break;
case PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP_EXEC:
p->send_rstp = false;
p->mdelay_while = r->migrate_time;
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP;
/* fall through */
case PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP:
if ((p->mdelay_while == 0) || (!p->port_enabled) || p->mcheck) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SENSING_EXEC;
}
break;
case PORT_PROTOCOL_MIGRATION_SM_SENSING_EXEC:
p->rcvd_rstp = false;
p->rcvd_stp = false;
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SENSING;
/* fall through */
case PORT_PROTOCOL_MIGRATION_SM_SENSING:
if (!p->port_enabled || p->mcheck || ((r->rstp_version) &&
!p->send_rstp && p->rcvd_rstp)) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_CHECKING_RSTP_EXEC;
} else if (p->send_rstp && p->rcvd_stp) {
p->port_protocol_migration_sm_state =
PORT_PROTOCOL_MIGRATION_SM_SELECTING_STP_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->port_protocol_migration_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: port_protocol_migration_sm %d -> %d",
p->rstp->name, p->port_number, old_state,
p->port_protocol_migration_sm_state);
}
return 0;
}
/* [17.25 - Bridge Detection state machine] */
static int
bridge_detection_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum bridge_detection_state_machine old_state;
struct rstp *r;
old_state = p->bridge_detection_sm_state;
r = p->rstp;
switch (p->bridge_detection_sm_state) {
case BRIDGE_DETECTION_SM_INIT:
if (r->begin && p->admin_edge) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_EDGE_EXEC;
} else if (r->begin && !p->admin_edge) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_NOT_EDGE_EXEC;
}
break;
case BRIDGE_DETECTION_SM_EDGE_EXEC:
p->oper_edge = true;
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_EDGE;
/* fall through */
case BRIDGE_DETECTION_SM_EDGE:
if ((!p->port_enabled && !p->admin_edge) || !p->oper_edge) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_NOT_EDGE_EXEC;
}
break;
case BRIDGE_DETECTION_SM_NOT_EDGE_EXEC:
p->oper_edge = false;
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_NOT_EDGE;
/* fall through */
case BRIDGE_DETECTION_SM_NOT_EDGE:
if ((!p->port_enabled && p->admin_edge)
|| ((p->edge_delay_while == 0) && p->auto_edge && p->send_rstp
&& p->proposing)) {
p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_EDGE_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->bridge_detection_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: bridge_detection_sm %d -> %d", p->rstp->name,
p->port_number, old_state, p->bridge_detection_sm_state);
}
return 0;
}
/* [17.26 - Port Transmit state machine] */
static void
rstp_send_bpdu(struct rstp_port *p, const void *bpdu, size_t bpdu_size)
OVS_REQUIRES(rstp_mutex)
{
struct eth_header *eth;
struct llc_header *llc;
struct dp_packet *pkt;
/* Skeleton. */
pkt = dp_packet_new(ETH_HEADER_LEN + LLC_HEADER_LEN + bpdu_size);
eth = dp_packet_put_zeros(pkt, sizeof *eth);
llc = dp_packet_put_zeros(pkt, sizeof *llc);
dp_packet_reset_offsets(pkt);
dp_packet_set_l3(pkt, dp_packet_put(pkt, bpdu, bpdu_size));
/* 802.2 header. */
eth->eth_dst = eth_addr_stp;
/* p->rstp->send_bpdu() must fill in source address. */
eth->eth_type = htons(dp_packet_size(pkt) - ETH_HEADER_LEN);
/* LLC header. */
llc->llc_dsap = STP_LLC_DSAP;
llc->llc_ssap = STP_LLC_SSAP;
llc->llc_cntl = STP_LLC_CNTL;
p->rstp->send_bpdu(pkt, p->aux, p->rstp->aux);
}
static void
record_agreement(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
r = p->rstp;
if (r->rstp_version && p->oper_point_to_point_mac &&
((p->received_bpdu_buffer.flags & BPDU_FLAG_AGREEMENT))) {
p->agreed = true;
p->proposing = false;
} else {
p->agreed = false;
}
}
static void
set_tc_flags(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* Sets rcvd_tc and/or rcvd_tc_ack if the Topology Change and/or Topology
* Change Acknowledgment flags, respectively, are set in a ConfigBPDU or
* RST BPDU.
*/
if (p->received_bpdu_buffer.bpdu_type == CONFIGURATION_BPDU ||
p->received_bpdu_buffer.bpdu_type == RAPID_SPANNING_TREE_BPDU) {
if ((p->received_bpdu_buffer.flags & BPDU_FLAG_TOPCHANGE) != 0) {
p->rcvd_tc = true;
}
if ((p->received_bpdu_buffer.flags & BPDU_FLAG_TOPCHANGEACK) != 0) {
p->rcvd_tc_ack = true;
}
}
/* Sets rcvd_tcn true if the BPDU is a TCN BPDU. */
if (p->received_bpdu_buffer.bpdu_type
== TOPOLOGY_CHANGE_NOTIFICATION_BPDU) {
p->rcvd_tcn = true;
}
}
static void
record_dispute(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
if ((p->received_bpdu_buffer.flags & BPDU_FLAG_LEARNING) != 0) {
/* 802.1D-2004 says to set the agreed flag and to clear the proposing
* flag. 802.1q-2008 instead says to set the disputed variable and to
* clear the agreed variable. */
p->disputed = true;
p->agreed = false;
}
}
static void
record_proposal(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_flag role =
((p->received_bpdu_buffer.flags) & ROLE_FLAG_MASK) >> ROLE_FLAG_SHIFT;
if ((role == PORT_DES)
&& ((p->received_bpdu_buffer.flags & BPDU_FLAG_PROPOSAL) != 0)) {
p->proposed = true;
}
}
static void
record_priority(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
p->port_priority.root_bridge_id = p->msg_priority.root_bridge_id;
p->port_priority.root_path_cost = p->msg_priority.root_path_cost;
p->port_priority.designated_bridge_id =
p->msg_priority.designated_bridge_id;
p->port_priority.designated_port_id = p->msg_priority.designated_port_id;
}
static void
record_times(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
p->port_times = p->msg_times;
if (p->msg_times.hello_time == 0) {
p->port_times.hello_time = 1;
}
}
static void
updt_rcvd_info_while(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* [17.21.23]
* The value assigned to rcvdInfoWhile is the three times the Hello Time,
* if Message Age, incremented by 1 second and rounded to the nearest whole
* second, does not exceed Max Age, and is zero otherwise.
*/
if (p->port_times.message_age < p->port_times.max_age) {
p->rcvd_info_while = p->port_times.hello_time * 3;
} else {
p->rcvd_info_while = 0;
}
}
/* Times are internally held in seconds, while the protocol uses 1/256 seconds.
* time_encode() is used to convert time values sent in bpdus, while
* time_decode() is used to convert time values received in bpdus.
*/
static ovs_be16
time_encode(uint8_t value)
{
return htons(value * 256);
}
static uint8_t
time_decode(ovs_be16 encoded)
{
return ntohs(encoded) / 256;
}
/* [17.21.19] */
static void
tx_config(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_bpdu bpdu;
memset(&bpdu, 0, sizeof bpdu);
bpdu.protocol_identifier = htons(0);
bpdu.protocol_version_identifier = 0;
bpdu.bpdu_type = CONFIGURATION_BPDU;
bpdu.root_bridge_id = htonll(p->designated_priority_vector.root_bridge_id);
bpdu.root_path_cost = htonl(p->designated_priority_vector.root_path_cost);
bpdu.designated_bridge_id =
htonll(p->designated_priority_vector.designated_bridge_id);
bpdu.designated_port_id =
htons(p->designated_priority_vector.designated_port_id);
bpdu.message_age = time_encode(p->designated_times.message_age);
bpdu.max_age = time_encode(p->designated_times.max_age);
bpdu.hello_time = time_encode(p->designated_times.hello_time);
bpdu.forward_delay = time_encode(p->designated_times.forward_delay);
bpdu.flags = 0;
if (p->tc_while != 0) {
bpdu.flags |= BPDU_FLAG_TOPCHANGE;
}
if (p->tc_ack != 0) {
bpdu.flags |= BPDU_FLAG_TOPCHANGEACK;
}
rstp_send_bpdu(p, &bpdu, sizeof(struct rstp_bpdu));
}
/* [17.21.20] */
static void
tx_rstp(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_bpdu bpdu;
memset(&bpdu, 0, sizeof bpdu);
bpdu.protocol_identifier = htons(0);
bpdu.protocol_version_identifier = 2;
bpdu.bpdu_type = RAPID_SPANNING_TREE_BPDU;
bpdu.root_bridge_id = htonll(p->designated_priority_vector.root_bridge_id);
bpdu.root_path_cost = htonl(p->designated_priority_vector.root_path_cost);
bpdu.designated_bridge_id =
htonll(p->designated_priority_vector.designated_bridge_id);
bpdu.designated_port_id =
htons(p->designated_priority_vector.designated_port_id);
bpdu.message_age = time_encode(p->designated_times.message_age);
bpdu.max_age = time_encode(p->designated_times.max_age);
bpdu.hello_time = time_encode(p->designated_times.hello_time);
bpdu.forward_delay = time_encode(p->designated_times.forward_delay);
bpdu.flags = 0;
switch (p->role) {
case ROLE_ROOT:
bpdu.flags = PORT_ROOT << ROLE_FLAG_SHIFT;
break;
case ROLE_DESIGNATED:
bpdu.flags = PORT_DES << ROLE_FLAG_SHIFT;
break;
case ROLE_ALTERNATE:
case ROLE_BACKUP:
bpdu.flags = PORT_ALT_BACK << ROLE_FLAG_SHIFT;
break;
case ROLE_DISABLED:
/* Should not happen! */
VLOG_ERR("%s transmitting bpdu in disabled role on port "
RSTP_PORT_ID_FMT, p->rstp->name, p->port_id);
break;
}
if (p->agree) {
bpdu.flags |= BPDU_FLAG_AGREEMENT;
}
if (p->proposing) {
bpdu.flags |= BPDU_FLAG_PROPOSAL;
}
if (p->tc_while != 0) {
bpdu.flags |= BPDU_FLAG_TOPCHANGE;
}
if (p->learning) {
bpdu.flags |= BPDU_FLAG_LEARNING;
}
if (p->forwarding) {
bpdu.flags |= BPDU_FLAG_FORWARDING;
}
bpdu.version1_length = 0;
rstp_send_bpdu(p, &bpdu, sizeof(struct rstp_bpdu));
}
/* [17.21.21] */
static void
tx_tcn(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_bpdu bpdu;
memset(&bpdu, 0, sizeof(struct rstp_bpdu));
bpdu.protocol_identifier = htons(0);
bpdu.protocol_version_identifier = 0;
bpdu.bpdu_type = TOPOLOGY_CHANGE_NOTIFICATION_BPDU;
rstp_send_bpdu(p, &bpdu, sizeof(struct rstp_bpdu));
}
static int
port_transmit_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_transmit_state_machine old_state;
struct rstp *r;
old_state = p->port_transmit_sm_state;
r = p->rstp;
switch (p->port_transmit_sm_state) {
case PORT_TRANSMIT_SM_INIT:
if (r->begin) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_INIT_EXEC;
}
break;
case PORT_TRANSMIT_SM_TRANSMIT_INIT_EXEC:
p->new_info = true;
p->tx_count = 0;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_INIT;
/* fall through */
case PORT_TRANSMIT_SM_TRANSMIT_INIT:
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE_EXEC;
break;
case PORT_TRANSMIT_SM_TRANSMIT_PERIODIC_EXEC:
p->new_info = p->new_info || (p->role == ROLE_DESIGNATED ||
(p->role == ROLE_ROOT && p->tc_while != 0));
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_PERIODIC;
/* fall through */
case PORT_TRANSMIT_SM_TRANSMIT_PERIODIC:
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE_EXEC;
break;
case PORT_TRANSMIT_SM_IDLE_EXEC:
p->hello_when = r->bridge_hello_time;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE;
/* fall through */
case PORT_TRANSMIT_SM_IDLE:
if (p->role == ROLE_DISABLED) {
VLOG_DBG("%s, port %u: port_transmit_sm ROLE == DISABLED.",
p->rstp->name, p->port_number);
break;
} else if (p->send_rstp && p->new_info
&& p->tx_count < r->transmit_hold_count
&& p->hello_when != 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_RSTP_EXEC;
} else if (p->hello_when == 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state =
PORT_TRANSMIT_SM_TRANSMIT_PERIODIC_EXEC;
} else if (!p->send_rstp && p->new_info && p->role == ROLE_ROOT
&& p->tx_count < r->transmit_hold_count
&& p->hello_when != 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_TCN_EXEC;
} else if (!p->send_rstp && p->new_info && p->role == ROLE_DESIGNATED
&& p->tx_count < r->transmit_hold_count
&& p->hello_when != 0 && p->selected && !p->updt_info) {
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_CONFIG_EXEC;
}
break;
case PORT_TRANSMIT_SM_TRANSMIT_CONFIG_EXEC:
p->new_info = false;
tx_config(p);
p->tx_count += 1;
p->tc_ack = false;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_CONFIG;
/* fall through */
case PORT_TRANSMIT_SM_TRANSMIT_CONFIG:
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE_EXEC;
break;
case PORT_TRANSMIT_SM_TRANSMIT_TCN_EXEC:
p->new_info = false;
tx_tcn(p);
p->tx_count += 1;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_TCN;
/* fall through */
case PORT_TRANSMIT_SM_TRANSMIT_TCN:
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE_EXEC;
break;
case PORT_TRANSMIT_SM_TRANSMIT_RSTP_EXEC:
p->new_info = false;
tx_rstp(p);
p->tx_count += 1;
p->tc_ack = false;
p->port_transmit_sm_state = PORT_TRANSMIT_SM_TRANSMIT_RSTP;
/* fall through */
case PORT_TRANSMIT_SM_TRANSMIT_RSTP:
p->port_transmit_sm_state = PORT_TRANSMIT_SM_IDLE_EXEC;
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->port_transmit_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: port_transmit_sm %d -> %d", p->rstp->name,
p->port_number, old_state, p->port_transmit_sm_state);
}
return 0;
}
/* [17.27 Port Information state machine] */
#define RECEIVED 0
#define MINE 1
static int
rcv_info(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum vector_comparison cp;
bool ct;
enum port_flag role;
p->msg_priority.root_bridge_id =
ntohll(p->received_bpdu_buffer.root_bridge_id);
p->msg_priority.root_path_cost =
ntohl(p->received_bpdu_buffer.root_path_cost);
p->msg_priority.designated_bridge_id =
ntohll(p->received_bpdu_buffer.designated_bridge_id);
p->msg_priority.designated_port_id =
ntohs(p->received_bpdu_buffer.designated_port_id);
p->msg_times.forward_delay =
time_decode(p->received_bpdu_buffer.forward_delay);
p->msg_times.hello_time = time_decode(p->received_bpdu_buffer.hello_time);
p->msg_times.max_age = time_decode(p->received_bpdu_buffer.max_age);
p->msg_times.message_age =
time_decode(p->received_bpdu_buffer.message_age);
cp = compare_rstp_priority_vectors(&p->msg_priority, &p->port_priority);
ct = rstp_times_equal(&p->port_times, &p->msg_times);
/* Configuration BPDU conveys a Designated Port Role. */
if (p->received_bpdu_buffer.bpdu_type == CONFIGURATION_BPDU) {
role = PORT_DES;
} else {
role =
(p->received_bpdu_buffer.flags & ROLE_FLAG_MASK) >> ROLE_FLAG_SHIFT;
}
/* 802.1D-2004 does not report this behaviour.
* 802.1Q-2008 says set rcvdTcn. */
if (p->received_bpdu_buffer.bpdu_type ==
TOPOLOGY_CHANGE_NOTIFICATION_BPDU) {
p->rcvd_tcn = true;
return OTHER_INFO;
}
/* Returns SuperiorDesignatedInfo if:
* a) The received message conveys a Designated Port Role, and
* 1) The message priority is superior (17.6) to the Port.s port priority
* vector, or
* 2) The message priority vector is the same as the Port.s port priority
* vector, and any of the received timer parameter values (msg_times.
* 17.19.15) differ from those already held for the Port (port_times
* 17.19.22).
* NOTE: Configuration BPDU explicitly conveys a Designated Port Role.
*/
if (role == PORT_DES && (cp == SUPERIOR || (cp == SAME && ct == false))) {
return SUPERIOR_DESIGNATED_INFO;
/* Returns RepeatedDesignatedInfo if:
* b) The received message conveys Designated Port Role, and a message
* priority vector and timer parameters that are the same as the
* Port's port priority vector or timer values. */
} else if (role == PORT_DES && cp == SAME && ct == true) {
return REPEATED_DESIGNATED_INFO;
/* Returns InferiorDesignatedInfo if:
* c) The received message conveys a Designated Port Role, and a
* message priority vector that is worse than the Port's port
* priority vector. */
} else if (role == PORT_DES && cp == INFERIOR) {
return INFERIOR_DESIGNATED_INFO;
/* Returns InferiorRootAlternateInfo if:
* d) The received message conveys a Root Port, Alternate Port, or
* Backup Port Role and a message priority that is the same as or
* worse than the port priority vector. */
} else if ((role == PORT_ROOT || role == PORT_ALT_BACK) &&
(cp == INFERIOR || cp == SAME)) {
return INFERIOR_ROOT_ALTERNATE_INFO;
/* Otherwise, returns OtherInfo. */
} else {
return OTHER_INFO;
}
}
static int
better_or_same_info(struct rstp_port *p, int new_info_is)
OVS_REQUIRES(rstp_mutex)
{
return
(new_info_is == RECEIVED && p->info_is == INFO_IS_RECEIVED
&& compare_rstp_priority_vectors(&p->msg_priority,
&p->port_priority))
|| (new_info_is == MINE && p->info_is == INFO_IS_MINE
&& compare_rstp_priority_vectors(&p->designated_priority_vector,
&p->port_priority));
}
static int
port_information_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_information_state_machine old_state;
struct rstp *r;
struct rstp_port *p1;
old_state = p->port_information_sm_state;
r = p->rstp;
switch (p->port_information_sm_state) {
case PORT_INFORMATION_SM_INIT:
if (r->begin
|| (!p->port_enabled && p->info_is != INFO_IS_DISABLED)) {
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
}
break;
case PORT_INFORMATION_SM_DISABLED_EXEC:
p->rcvd_msg = false;
p->proposing = p->proposed = p->agree = p->agreed = false;
p->rcvd_info_while = 0;
p->info_is = INFO_IS_DISABLED;
p->reselect = true;
p->selected = false;
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED;
/* fall through */
case PORT_INFORMATION_SM_DISABLED:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else if (p->port_enabled) {
p->port_information_sm_state = PORT_INFORMATION_SM_AGED_EXEC;
} else if (p->rcvd_msg) {
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
}
break;
case PORT_INFORMATION_SM_AGED_EXEC:
p->info_is = INFO_IS_AGED;
p->reselect = true;
p->selected = false;
p->port_information_sm_state = PORT_INFORMATION_SM_AGED;
/* fall through */
case PORT_INFORMATION_SM_AGED:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else if (p->selected && p->updt_info) {
p->port_information_sm_state = PORT_INFORMATION_SM_UPDATE_EXEC;
}
break;
case PORT_INFORMATION_SM_UPDATE_EXEC:
p->proposing = p->proposed = false;
/* MINE is not specified in Standard 802.1D-2004. */
p->agreed = p->agreed && better_or_same_info(p, MINE);
p->synced = p->synced && p->agreed;
p->port_priority.root_bridge_id =
p->designated_priority_vector.root_bridge_id;
p->port_priority.root_path_cost =
p->designated_priority_vector.root_path_cost;
p->port_priority.designated_bridge_id =
p->designated_priority_vector.designated_bridge_id;
p->port_priority.designated_port_id =
p->designated_priority_vector.designated_port_id;
p->port_times = p->designated_times;
p->updt_info = false;
p->info_is = INFO_IS_MINE;
p->new_info = true;
p->port_information_sm_state = PORT_INFORMATION_SM_UPDATE;
/* fall through */
case PORT_INFORMATION_SM_UPDATE:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else {
p->port_information_sm_state = PORT_INFORMATION_SM_CURRENT_EXEC;
}
break;
case PORT_INFORMATION_SM_CURRENT_EXEC:
p->port_information_sm_state = PORT_INFORMATION_SM_CURRENT;
/* fall through */
case PORT_INFORMATION_SM_CURRENT:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else if (p->rcvd_msg && !p->updt_info) {
p->port_information_sm_state = PORT_INFORMATION_SM_RECEIVE_EXEC;
} else if (p->selected && p->updt_info) {
p->port_information_sm_state = PORT_INFORMATION_SM_UPDATE_EXEC;
} else if ((p->info_is == INFO_IS_RECEIVED) &&
(p->rcvd_info_while == 0) && !p->updt_info &&
!p->rcvd_msg) {
p->port_information_sm_state = PORT_INFORMATION_SM_AGED_EXEC;
}
break;
case PORT_INFORMATION_SM_RECEIVE_EXEC:
p->rcvd_info = rcv_info(p);
p->port_information_sm_state = PORT_INFORMATION_SM_RECEIVE;
/* fall through */
case PORT_INFORMATION_SM_RECEIVE:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else {
switch (p->rcvd_info) {
case SUPERIOR_DESIGNATED_INFO:
/* 802.1q-2008 has a checkBPDUConsistency() function, called on
* a BPDU reception. checkBPDUConsistency() clears the agreed
* variable if the received message priority vector is superior
* to the port priority vector, the BPDU is an ST BPDU or an
* RST BPDU, its port role is Designated and its Learning flag
* is set. */
if (p->received_bpdu_buffer.flags & BPDU_FLAG_LEARNING) {
HMAP_FOR_EACH (p1, node, &r->ports) {
if (p1->port_number != p->port_number) {
p1->agreed = false;
}
}
}
p->port_information_sm_state =
PORT_INFORMATION_SM_SUPERIOR_DESIGNATED_EXEC;
break;
case REPEATED_DESIGNATED_INFO:
p->port_information_sm_state =
PORT_INFORMATION_SM_REPEATED_DESIGNATED_EXEC;
break;
case INFERIOR_DESIGNATED_INFO:
p->port_information_sm_state =
PORT_INFORMATION_SM_INFERIOR_DESIGNATED_EXEC;
break;
case INFERIOR_ROOT_ALTERNATE_INFO:
p->port_information_sm_state =
PORT_INFORMATION_SM_NOT_DESIGNATED_EXEC;
break;
case OTHER_INFO:
p->port_information_sm_state = PORT_INFORMATION_SM_OTHER_EXEC;
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
}
break;
case PORT_INFORMATION_SM_OTHER_EXEC:
p->rcvd_msg = false;
p->port_information_sm_state = PORT_INFORMATION_SM_OTHER;
/* fall through */
case PORT_INFORMATION_SM_OTHER:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else {
p->port_information_sm_state = PORT_INFORMATION_SM_CURRENT_EXEC;
}
break;
case PORT_INFORMATION_SM_NOT_DESIGNATED_EXEC:
record_agreement(p);
set_tc_flags(p);
p->rcvd_msg = false;
p->port_information_sm_state = PORT_INFORMATION_SM_NOT_DESIGNATED;
/* fall through */
case PORT_INFORMATION_SM_NOT_DESIGNATED:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else {
p->port_information_sm_state = PORT_INFORMATION_SM_CURRENT_EXEC;
}
break;
case PORT_INFORMATION_SM_INFERIOR_DESIGNATED_EXEC:
record_dispute(p);
p->rcvd_msg = false;
p->port_information_sm_state = PORT_INFORMATION_SM_INFERIOR_DESIGNATED;
/* fall through */
case PORT_INFORMATION_SM_INFERIOR_DESIGNATED:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else {
p->port_information_sm_state = PORT_INFORMATION_SM_CURRENT_EXEC;
}
break;
case PORT_INFORMATION_SM_REPEATED_DESIGNATED_EXEC:
record_proposal(p);
set_tc_flags(p);
/* This record_agreement() is missing in 802.1D-2004, but it's present
* in 802.1q-2008. */
record_agreement(p);
updt_rcvd_info_while(p);
p->rcvd_msg = false;
p->port_information_sm_state = PORT_INFORMATION_SM_REPEATED_DESIGNATED;
/* fall through */
case PORT_INFORMATION_SM_REPEATED_DESIGNATED:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else {
p->port_information_sm_state = PORT_INFORMATION_SM_CURRENT_EXEC;
}
break;
case PORT_INFORMATION_SM_SUPERIOR_DESIGNATED_EXEC:
p->agreed = p->proposing = false;
record_proposal(p);
set_tc_flags(p);
/* RECEIVED is not specified in Standard 802.1D-2004. */
p->agree = p->agree && better_or_same_info(p, RECEIVED);
/* This record_agreement() and the synced assignment are missing in
* 802.1D-2004, but they're present in 802.1q-2008. */
record_agreement(p);
p->synced = p->synced && p->agreed;
record_priority(p);
record_times(p);
updt_rcvd_info_while(p);
p->info_is = INFO_IS_RECEIVED;
p->reselect = true;
p->selected = false;
p->rcvd_msg = false;
p->port_information_sm_state = PORT_INFORMATION_SM_SUPERIOR_DESIGNATED;
/* fall through */
case PORT_INFORMATION_SM_SUPERIOR_DESIGNATED:
if (!p->port_enabled && p->info_is != INFO_IS_DISABLED) {
/* Global transition. */
p->port_information_sm_state = PORT_INFORMATION_SM_DISABLED_EXEC;
} else {
p->port_information_sm_state = PORT_INFORMATION_SM_CURRENT_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->port_information_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: Port_information_sm %d -> %d", p->rstp->name,
p->port_number, old_state, p->port_information_sm_state);
}
return 0;
}
/* [17.29 Port Role Transitions state machine] */
static void
set_re_root_tree(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
struct rstp_port *p1;
r = p->rstp;
HMAP_FOR_EACH (p1, node, &r->ports) {
p1->re_root = true;
}
}
static void
set_sync_tree(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
struct rstp_port *p1;
r = p->rstp;
HMAP_FOR_EACH (p1, node, &r->ports) {
p1->sync = true;
}
}
static int
hello_time(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
return p->designated_times.hello_time;
}
static int
fwd_delay(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
return p->designated_times.forward_delay;
}
static int
forward_delay(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
if (p->send_rstp) {
return hello_time(p);
} else {
return fwd_delay(p);
}
}
static int
edge_delay(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
r = p->rstp;
if (p->oper_point_to_point_mac == 1) {
return r->migrate_time;
} else {
return p->designated_times.max_age;
}
}
static int
check_selected_role_change(struct rstp_port *p, int current_role_state)
OVS_REQUIRES(rstp_mutex)
{
if (p->selected && !p->updt_info && p->role != p->selected_role
&& p->selected_role != current_role_state) {
VLOG_DBG("%s, port %u: case: current = %s role = %s selected = %d",
p->rstp->name, p->port_number,
rstp_port_role_name(current_role_state),
rstp_port_role_name(p->role), p->selected_role);
switch (p->selected_role) {
case ROLE_ROOT:
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
return true;
case ROLE_DESIGNATED:
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC;
return true;
case ROLE_ALTERNATE:
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_BLOCK_PORT_EXEC;
return true;
case ROLE_BACKUP:
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_BLOCK_PORT_EXEC;
return true;
case ROLE_DISABLED:
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DISABLE_PORT_EXEC;
return true;
}
}
return false;
}
static int
re_rooted(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
struct rstp_port *p1;
r = p->rstp;
HMAP_FOR_EACH (p1, node, &r->ports) {
if ((p1 != p) && (p1->rr_while != 0)) {
return false;
}
}
return true;
}
static int
all_synced(struct rstp *r)
OVS_REQUIRES(rstp_mutex)
{
struct rstp_port *p;
HMAP_FOR_EACH (p, node, &r->ports) {
if (!(p->selected && p->role == p->selected_role &&
(p->role == ROLE_ROOT || p->synced == true))) {
return false;
}
}
return true;
}
static int
port_role_transition_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_role_transition_state_machine old_state;
struct rstp *r;
enum rstp_port_role last_role;
old_state = p->port_role_transition_sm_state;
r = p->rstp;
last_role = p->role;
switch (p->port_role_transition_sm_state) {
case PORT_ROLE_TRANSITION_SM_INIT:
if (r->begin) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_INIT_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_INIT_PORT_EXEC:
p->role = ROLE_DISABLED;
p->learn = p->forward = false;
p->synced = false;
p->sync = p->re_root = true;
p->rr_while = p->designated_times.forward_delay;
p->fd_while = p->designated_times.max_age;
p->rb_while = 0;
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DISABLE_PORT_EXEC;
break;
case PORT_ROLE_TRANSITION_SM_DISABLE_PORT_EXEC:
p->role = p->selected_role;
p->learn = p->forward = false;
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DISABLE_PORT;
/* fall through */
case PORT_ROLE_TRANSITION_SM_DISABLE_PORT:
if (check_selected_role_change(p, ROLE_DISABLED)) {
/* Global transition. */
} else if (p->selected && !p->updt_info && !p->learning
&& !p->forwarding) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DISABLED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_DISABLED_PORT_EXEC:
p->fd_while = p->designated_times.max_age;
p->synced = true;
p->rr_while = 0;
p->sync = p->re_root = false;
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DISABLED_PORT;
/* fall through */
case PORT_ROLE_TRANSITION_SM_DISABLED_PORT:
if (check_selected_role_change(p, ROLE_DISABLED)) {
/* Global transition. */
} else if (p->selected && !p->updt_info
&& (p->fd_while != p->designated_times.max_age || p->sync
|| p->re_root || !p->synced)) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DISABLED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC:
p->role = ROLE_ROOT;
p->rr_while = p->designated_times.forward_delay;
p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_ROOT_PORT;
/* fall through */
case PORT_ROLE_TRANSITION_SM_ROOT_PORT:
if (check_selected_role_change(p, ROLE_ROOT)) {
/* Global transition. */
} else if (p->selected && !p->updt_info) {
if (p->rr_while != p->designated_times.forward_delay) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
break;
} else if (p->re_root && p->forward) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_REROOTED_EXEC;
break;
} else if ((p->fd_while == 0
|| ((re_rooted(p) && p->rb_while == 0)
&& r->rstp_version)) && !p->learn) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_LEARN_EXEC;
break;
} else if ((p->fd_while == 0
|| ((re_rooted(p) && p->rb_while == 0)
&& r->rstp_version)) && p->learn && !p->forward) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_FORWARD_EXEC;
break;
} else if (p->proposed && !p->agree) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PROPOSED_EXEC;
break;
} else if ((all_synced(r) && !p->agree) ||
(p->proposed && p->agree)) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_AGREED_EXEC;
break;
} else if (!p->forward && !p->re_root) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_REROOT_EXEC;
break;
}
}
break;
case PORT_ROLE_TRANSITION_SM_REROOT_EXEC:
if (check_selected_role_change(p, ROLE_ROOT)) {
/* Global transition. */
} else {
set_re_root_tree(p);
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_ROOT_AGREED_EXEC:
if (check_selected_role_change(p, ROLE_ROOT)) {
/* Global transition. */
} else {
p->proposed = p->sync = false;
p->agree = p->new_info = true;
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_ROOT_PROPOSED_EXEC:
set_sync_tree(p);
p->proposed = false;
if (check_selected_role_change(p, ROLE_ROOT)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_ROOT_FORWARD_EXEC:
p->fd_while = 0;
p->forward = true;
if (check_selected_role_change(p, ROLE_ROOT)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_ROOT_LEARN_EXEC:
p->fd_while = forward_delay(p);
p->learn = true;
if (check_selected_role_change(p, ROLE_ROOT)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_REROOTED_EXEC:
p->re_root = false;
if (check_selected_role_change(p, ROLE_ROOT)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ROOT_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC:
p->role = ROLE_DESIGNATED;
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT;
/* fall through */
case PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT:
if (check_selected_role_change(p, ROLE_DESIGNATED)) {
/* Global transition. */
} else if (p->selected && !p->updt_info) {
if (((p->sync && !p->synced)
|| (p->re_root && p->rr_while != 0) || p->disputed)
&& !p->oper_edge && (p->learn || p->forward)) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_DISCARD_EXEC;
} else if ((p->fd_while == 0 || p->agreed || p->oper_edge)
&& (p->rr_while == 0 || !p->re_root)
&& !p->sync && !p->learn) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_LEARN_EXEC;
} else if ((p->fd_while == 0 || p->agreed || p->oper_edge)
&& (p->rr_while == 0 || !p->re_root)
&& !p->sync && (p->learn && !p->forward)) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_FORWARD_EXEC;
} else if (!p->forward && !p->agreed && !p->proposing &&
!p->oper_edge) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PROPOSE_EXEC;
} else if ((!p->learning && !p->forwarding && !p->synced)
|| (p->agreed && !p->synced)
|| (p->oper_edge && !p->synced)
|| (p->sync && p->synced)) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_SYNCED_EXEC;
} else if (p->rr_while == 0 && p->re_root) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_RETIRED_EXEC;
}
}
break;
case PORT_ROLE_TRANSITION_SM_DESIGNATED_RETIRED_EXEC:
p->re_root = false;
if (check_selected_role_change(p, ROLE_DESIGNATED)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_DESIGNATED_SYNCED_EXEC:
p->rr_while = 0;
p->synced = true;
p->sync = false;
if (check_selected_role_change(p, ROLE_DESIGNATED)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_DESIGNATED_PROPOSE_EXEC:
p->proposing = true;
p->edge_delay_while = edge_delay(p);
p->new_info = true;
if (check_selected_role_change(p, ROLE_DESIGNATED)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_DESIGNATED_FORWARD_EXEC:
p->forward = true;
p->fd_while = 0;
p->agreed = p->send_rstp;
if (check_selected_role_change(p, ROLE_DESIGNATED)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_DESIGNATED_LEARN_EXEC:
p->learn = true;
p->fd_while = forward_delay(p);
if (check_selected_role_change(p, ROLE_DESIGNATED)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_DESIGNATED_DISCARD_EXEC:
p->learn = p->forward = p->disputed = false;
p->fd_while = forward_delay(p);
if (check_selected_role_change(p, ROLE_DESIGNATED)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_DESIGNATED_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT_EXEC:
p->fd_while = p->designated_times.forward_delay;
p->synced = true;
p->rr_while = 0;
p->sync = p->re_root = false;
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT;
/* fall through */
case PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT:
if (check_selected_role_change(p, ROLE_ALTERNATE)) {
/* Global transition. */
} else if (p->selected && !p->updt_info) {
if (p->rb_while != 2 * p->designated_times.hello_time
&& p->role == ROLE_BACKUP) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_BACKUP_PORT_EXEC;
} else if ((p->fd_while != forward_delay(p)) || p->sync
|| p->re_root || !p->synced) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT_EXEC;
} else if (p->proposed && !p->agree) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_PROPOSED_EXEC;
} else if ((all_synced(r) && !p->agree)
|| (p->proposed && p->agree)) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_AGREED_EXEC;
}
}
break;
case PORT_ROLE_TRANSITION_SM_ALTERNATE_AGREED_EXEC:
p->proposed = false;
p->agree = true;
p->new_info = true;
if (check_selected_role_change(p, ROLE_ALTERNATE)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_ALTERNATE_PROPOSED_EXEC:
set_sync_tree(p);
p->proposed = false;
if (check_selected_role_change(p, ROLE_ALTERNATE)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_BLOCK_PORT_EXEC:
p->role = p->selected_role;
p->learn = p->forward = false;
p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_BLOCK_PORT;
/* fall through */
case PORT_ROLE_TRANSITION_SM_BLOCK_PORT:
if (check_selected_role_change(p, ROLE_ALTERNATE)) {
/* Global transition. */
} else if (p->selected && !p->updt_info && !p->learning &&
!p->forwarding) {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT_EXEC;
}
break;
case PORT_ROLE_TRANSITION_SM_BACKUP_PORT_EXEC:
p->rb_while = 2 * p->designated_times.hello_time;
if (check_selected_role_change(p, ROLE_ALTERNATE)) {
/* Global transition. */
} else {
p->port_role_transition_sm_state =
PORT_ROLE_TRANSITION_SM_ALTERNATE_PORT_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->port_role_transition_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: Port_role_transition_sm %d -> %d",
p->rstp->name, p->port_number, old_state,
p->port_role_transition_sm_state);
}
if (last_role != p->role) {
VLOG_DBG("%s, port %u, port role ["RSTP_PORT_ID_FMT"] = %s",
p->rstp->name, p->port_number, p->port_id,
rstp_port_role_name(p->role));
}
return 0;
}
/* [17.30 - Port state transition state machine] */
static void
enable_learning(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* [17.21.6 enableLearning()] An implementation dependent procedure that
* causes the Learning Process (7.8) to start learning from frames received
* on the Port. The procedure does not complete until learning has been
* enabled.
*/
rstp_port_set_state__(p, RSTP_LEARNING);
}
static void
enable_forwarding(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* [17.21.5 enableForwarding()] An implementation dependent procedure that
* causes the Forwarding Process (7.7) to start forwarding frames through
* the Port. The procedure does not complete until forwarding has been
* enabled.
*/
rstp_port_set_state__(p, RSTP_FORWARDING);
}
static void
disable_learning(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* [17.21.4 - disableLearning()] An implementation dependent procedure that
* causes the Learning Process (7.8) to stop learning from the source
* address of frames received on the Port. The procedure does not complete
* until learning has stopped.
*/
rstp_port_set_state__(p, RSTP_DISCARDING);
}
static void
disable_forwarding(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
/* [17.21.3 - disableForwarding()] An implementation dependent procedure
* that causes the Forwarding Process (7.7) to stop forwarding frames
* through the Port. The procedure does not complete until forwarding has
* stopped.
*/
rstp_port_set_state__(p, RSTP_DISCARDING);
}
static int
port_state_transition_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum port_state_transition_state_machine old_state;
struct rstp *r;
old_state = p->port_state_transition_sm_state;
r = p->rstp;
switch (p->port_state_transition_sm_state) {
case PORT_STATE_TRANSITION_SM_INIT:
if (r->begin) {
p->port_state_transition_sm_state =
PORT_STATE_TRANSITION_SM_DISCARDING_EXEC;
}
break;
case PORT_STATE_TRANSITION_SM_DISCARDING_EXEC:
disable_learning(p);
p->learning = false;
disable_forwarding(p);
p->forwarding = false;
p->port_state_transition_sm_state =
PORT_STATE_TRANSITION_SM_DISCARDING;
/* fall through */
case PORT_STATE_TRANSITION_SM_DISCARDING:
if (p->learn) {
p->port_state_transition_sm_state =
PORT_STATE_TRANSITION_SM_LEARNING_EXEC;
}
break;
case PORT_STATE_TRANSITION_SM_LEARNING_EXEC:
enable_learning(p);
p->learning = true;
p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_LEARNING;
/* fall through */
case PORT_STATE_TRANSITION_SM_LEARNING:
if (!p->learn) {
p->port_state_transition_sm_state =
PORT_STATE_TRANSITION_SM_DISCARDING_EXEC;
} else if (p->forward) {
p->port_state_transition_sm_state =
PORT_STATE_TRANSITION_SM_FORWARDING_EXEC;
}
break;
case PORT_STATE_TRANSITION_SM_FORWARDING_EXEC:
enable_forwarding(p);
p->forwarding = true;
p->port_state_transition_sm_state =
PORT_STATE_TRANSITION_SM_FORWARDING;
/* fall through */
case PORT_STATE_TRANSITION_SM_FORWARDING:
if (!p->forward) {
p->port_state_transition_sm_state =
PORT_STATE_TRANSITION_SM_DISCARDING_EXEC;
}
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->port_state_transition_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: Port_state_transition_sm %d -> %d",
p->rstp->name, p->port_number, old_state,
p->port_state_transition_sm_state);
}
return 0;
}
/* [17.31 - Topology Change state machine] */
static void
new_tc_while(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
r = p->rstp;
if (p->tc_while == 0 && p->send_rstp == true) {
p->tc_while = r->bridge_hello_time + 1;
p->new_info = true;
} else if (p->tc_while == 0 && p->send_rstp == false) {
p->tc_while = r->bridge_max_age + r->bridge_forward_delay;
}
}
/* [17.21.18 setTcPropTree()]
* Sets tcprop for all Ports except the Port that called the procedure.
*/
static void
set_tc_prop_tree(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
struct rstp *r;
struct rstp_port *p1;
r = p->rstp;
HMAP_FOR_EACH (p1, node, &r->ports) {
/* Set tc_prop on every port, except the one calling this
* function. */
if (p1->port_number != p->port_number) {
p1->tc_prop = true;
}
}
}
static void
set_tc_prop_bridge(struct rstp_port *p) /* not specified in 802.1D-2004. */
OVS_REQUIRES(rstp_mutex)
{
set_tc_prop_tree(p); /* see 802.1w-2001. */
}
static int
topology_change_sm(struct rstp_port *p)
OVS_REQUIRES(rstp_mutex)
{
enum topology_change_state_machine old_state;
struct rstp *r;
old_state = p->topology_change_sm_state;
r = p->rstp;
switch (p->topology_change_sm_state) {
case TOPOLOGY_CHANGE_SM_INIT:
if (r->begin) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INACTIVE_EXEC;
}
break;
case TOPOLOGY_CHANGE_SM_INACTIVE_EXEC:
p->fdb_flush = true;
p->tc_while = 0;
p->tc_ack = false;
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INACTIVE;
/* fall through */
case TOPOLOGY_CHANGE_SM_INACTIVE:
if (p->learn && !p->fdb_flush) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_LEARNING_EXEC;
}
break;
case TOPOLOGY_CHANGE_SM_LEARNING_EXEC:
p->rcvd_tc = p->rcvd_tcn = p->rcvd_tc_ack = false;
p->tc_prop = p->rcvd_tc_ack = false;
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_LEARNING;
/* fall through */
case TOPOLOGY_CHANGE_SM_LEARNING:
if (p->role != ROLE_ROOT && p->role != ROLE_DESIGNATED &&
!(p->learn || p->learning) && !(p->rcvd_tc || p->rcvd_tcn ||
p->rcvd_tc_ack || p->tc_prop)) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INACTIVE_EXEC;
} else if (p->rcvd_tc || p->rcvd_tcn || p->rcvd_tc_ack || p->tc_prop) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_LEARNING_EXEC;
} else if ((p->role == ROLE_ROOT || p->role == ROLE_DESIGNATED)
&& p->forward && !p->oper_edge) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_DETECTED_EXEC;
}
break;
case TOPOLOGY_CHANGE_SM_DETECTED_EXEC:
new_tc_while(p);
set_tc_prop_tree(p);
p->new_info = true;
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_ACTIVE_EXEC;
/* fall through */
case TOPOLOGY_CHANGE_SM_ACTIVE_EXEC:
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_ACTIVE;
/* fall through */
case TOPOLOGY_CHANGE_SM_ACTIVE:
if ((p->role != ROLE_ROOT && p->role != ROLE_DESIGNATED)
|| p->oper_edge) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_LEARNING_EXEC;
} else if (p->rcvd_tcn) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_NOTIFIED_TCN_EXEC;
} else if (p->rcvd_tc) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_NOTIFIED_TC_EXEC;
} else if (p->tc_prop && !p->oper_edge) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_PROPAGATING_EXEC;
} else if (p->rcvd_tc_ack) {
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_ACKNOWLEDGED_EXEC;
}
break;
case TOPOLOGY_CHANGE_SM_ACKNOWLEDGED_EXEC:
p->tc_while = 0;
p->rcvd_tc_ack = false;
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_ACTIVE;
break;
case TOPOLOGY_CHANGE_SM_PROPAGATING_EXEC:
new_tc_while(p);
p->fdb_flush = true;
p->tc_prop = false;
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_ACTIVE;
break;
case TOPOLOGY_CHANGE_SM_NOTIFIED_TC_EXEC:
p->rcvd_tcn = p->rcvd_tc = false;
if (p->role == ROLE_DESIGNATED) {
p->tc_ack = true;
}
set_tc_prop_bridge(p);
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_ACTIVE;
break;
case TOPOLOGY_CHANGE_SM_NOTIFIED_TCN_EXEC:
new_tc_while(p);
p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_NOTIFIED_TC_EXEC;
break;
default:
OVS_NOT_REACHED();
/* fall through */
}
if (old_state != p->topology_change_sm_state) {
r->changes = true;
VLOG_DBG("%s, port %u: Topology_change_sm %d -> %d", p->rstp->name,
p->port_number, old_state, p->topology_change_sm_state);
}
return 0;
}
/****************************************************************************
* [17.6] Priority vector calculation helper functions
****************************************************************************/
/* compare_rstp_priority_vectors() compares two struct rstp_priority_vectors
* and returns a value indicating if the first rstp_priority_vector is
* superior, same or inferior to the second one.
*
* Zero return value indicates INFERIOR, a non-zero return value indicates
* SUPERIOR. When it makes a difference the non-zero return value SAME
* indicates the priority vectors are identical (a subset of SUPERIOR).
*/
static enum vector_comparison
compare_rstp_priority_vectors(const struct rstp_priority_vector *v1,
const struct rstp_priority_vector *v2)
{
VLOG_DBG("v1: "RSTP_ID_FMT", %u, "RSTP_ID_FMT", %d, %d",
RSTP_ID_ARGS(v1->root_bridge_id), v1->root_path_cost,
RSTP_ID_ARGS(v1->designated_bridge_id), v1->designated_port_id,
v1->bridge_port_id);
VLOG_DBG("v2: "RSTP_ID_FMT", %u, "RSTP_ID_FMT", %d, %d",
RSTP_ID_ARGS(v2->root_bridge_id), v2->root_path_cost,
RSTP_ID_ARGS(v2->designated_bridge_id), v2->designated_port_id,
v2->bridge_port_id);
/* [17.6]
* This message priority vector is superior to the port priority vector and
* will replace it if, and only if, the message priority vector is better
* than the port priority vector, or the message has been transmitted from
* the same Designated Bridge and Designated Port as the port priority
* vector, i.e., if the following is true:
*
* ((RD < RootBridgeID)) ||
* ((RD == RootBridgeID) && (RPCD < RootPathCost)) ||
* ((RD == RootBridgeID) && (RPCD == RootPathCost) &&
* (D < designated_bridge_id)) ||
* ((RD == RootBridgeID) && (RPCD == RootPathCost) &&
* (D == designated_bridge_id) && (PD < designated_port_id)) ||
* ((D == designated_bridge_id.BridgeAddress) &&
* (PD == designated_port_id.PortNumber))
*/
if ((v1->root_bridge_id < v2->root_bridge_id)
|| (v1->root_bridge_id == v2->root_bridge_id
&& v1->root_path_cost < v2->root_path_cost)
|| (v1->root_bridge_id == v2->root_bridge_id
&& v1->root_path_cost == v2->root_path_cost
&& v1->designated_bridge_id < v2->designated_bridge_id)
|| (v1->root_bridge_id == v2->root_bridge_id
&& v1->root_path_cost == v2->root_path_cost
&& v1->designated_bridge_id == v2->designated_bridge_id
&& v1->designated_port_id < v2->designated_port_id)
|| (v1->designated_bridge_id == v2->designated_bridge_id
&& v1->designated_port_id == v2->designated_port_id)) {
/* SAME is a subset of SUPERIOR. */
if (v1->root_bridge_id == v2->root_bridge_id
&& v1->root_path_cost == v2->root_path_cost
&& v1->designated_bridge_id == v2->designated_bridge_id
&& v1->designated_port_id == v2->designated_port_id) {
if (v1->bridge_port_id < v2->bridge_port_id) {
VLOG_DBG("superior");
return SUPERIOR;
}
else if (v1->bridge_port_id > v2->bridge_port_id) {
VLOG_DBG("inferior");
return INFERIOR;
}
VLOG_DBG("superior_same");
return SAME;
}
VLOG_DBG("superior");
return SUPERIOR;
}
VLOG_DBG("inferior");
return INFERIOR;
}
static bool
rstp_times_equal(struct rstp_times *t1, struct rstp_times *t2)
{
return t1->forward_delay == t2->forward_delay
&& t1->hello_time == t2->hello_time
&& t1->max_age == t2->max_age
&& t1->message_age == t2->message_age;
}
|