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 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
*
* Copyright 2009-2016, LabN Consulting, L.L.C.
*
*/
/*
* File: rfapi_rib.c
* Purpose: maintain per-nve ribs and generate change lists
*/
#include "lib/zebra.h"
#include "lib/prefix.h"
#include "lib/agg_table.h"
#include "lib/vty.h"
#include "lib/memory.h"
#include "lib/log.h"
#include "lib/skiplist.h"
#include "lib/workqueue.h"
#include <typesafe.h>
#include "bgpd/bgpd.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_vnc_types.h"
#include "bgpd/rfapi/rfapi.h"
#include "bgpd/rfapi/bgp_rfapi_cfg.h"
#include "bgpd/rfapi/rfapi_import.h"
#include "bgpd/rfapi/rfapi_private.h"
#include "bgpd/rfapi/rfapi_vty.h"
#include "bgpd/rfapi/vnc_import_bgp.h"
#include "bgpd/rfapi/rfapi_rib.h"
#include "bgpd/rfapi/rfapi_monitor.h"
#include "bgpd/rfapi/rfapi_encap_tlv.h"
#include "bgpd/rfapi/vnc_debug.h"
#define DEBUG_PROCESS_PENDING_NODE 0
#define DEBUG_PENDING_DELETE_ROUTE 0
#define DEBUG_NHL 0
#define DEBUG_RIB_SL_RD 0
#define DEBUG_CLEANUP 0
/* forward decl */
#if DEBUG_NHL
static void rfapiRibShowRibSl(void *stream, const struct prefix *pfx, struct skiplist *sl);
#endif
/*
* RIB
* ---
* Model of the set of routes currently in the NVE's RIB.
*
* node->info ptr to "struct skiplist".
* MUST be NULL if there are no routes.
* key = ptr to struct prefix {vn}
* val = ptr to struct rfapi_info
* skiplist.del = NULL
* skiplist.cmp = vnc_prefix_cmp
*
* node->aggregate ptr to "struct skiplist".
* key = ptr to struct prefix {vn}
* val = ptr to struct rfapi_info
* skiplist.del = rfapi_info_free
* skiplist.cmp = vnc_prefix_cmp
*
* This skiplist at "aggregate"
* contains the routes recently
* deleted
*
*
* Pending RIB
* -----------
* Sparse list of prefixes that need to be updated. Each node
* will have the complete set of routes for the prefix.
*
* node->info ptr to "struct list" (lib/linklist.h)
* "Cost List"
* List of routes sorted lowest cost first.
* This list is how the new complete set
* of routes should look.
* Set if there are updates to the prefix;
* MUST be NULL if there are no updates.
*
* .data = ptr to struct rfapi_info
* list.cmp = NULL (sorted manually)
* list.del = rfapi_info_free
*
* Special case: if node->info is 1, it means
* "delete all routes at this prefix".
*
* node->aggregate ptr to struct skiplist
* key = ptr to struct prefix {vn} (part of ri)
* val = struct rfapi_info
* skiplist.cmp = vnc_prefix_cmp
* skiplist.del = NULL
*
* ptlist is rewritten anew each time
* rfapiRibUpdatePendingNode() is called
*
* THE ptlist VALUES ARE REFERENCES TO THE
* rfapi_info STRUCTS IN THE node->info LIST.
*/
/*
* iterate over RIB to count responses, compare with running counters
*/
void rfapiRibCheckCounts(
int checkstats, /* validate rfd & global counts */
unsigned int offset) /* number of ri's held separately */
{
struct rfapi_descriptor *rfd;
struct listnode *node;
struct bgp *bgp = bgp_get_default();
uint32_t t_pfx_active = 0;
uint32_t t_ri_active = 0;
uint32_t t_ri_deleted = 0;
uint32_t t_ri_pend = 0;
unsigned int alloc_count;
/*
* loop over NVEs
*/
for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node, rfd)) {
afi_t afi;
uint32_t pfx_active = 0;
for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
struct agg_node *rn;
for (rn = agg_route_top(rfd->rib[afi]); rn;
rn = agg_route_next(rn)) {
struct skiplist *sl = rn->info;
struct skiplist *dsl = rn->aggregate;
uint32_t ri_active = 0;
uint32_t ri_deleted = 0;
if (sl) {
ri_active = skiplist_count(sl);
assert(ri_active);
t_ri_active += ri_active;
++pfx_active;
++t_pfx_active;
}
if (dsl) {
ri_deleted = skiplist_count(dsl);
t_ri_deleted += ri_deleted;
}
}
for (rn = agg_route_top(rfd->rib_pending[afi]); rn;
rn = agg_route_next(rn)) {
struct list *l = rn->info; /* sorted by cost */
struct skiplist *sl = rn->aggregate;
uint32_t ri_pend_cost = 0;
uint32_t ri_pend_uniq = 0;
if (sl) {
ri_pend_uniq = skiplist_count(sl);
}
if (l && (l != (void *)1)) {
ri_pend_cost = l->count;
t_ri_pend += l->count;
}
assert(ri_pend_uniq == ri_pend_cost);
}
}
if (checkstats) {
if (pfx_active != rfd->rib_prefix_count) {
vnc_zlog_debug_verbose(
"%s: rfd %p actual pfx count %u != running %u",
__func__, rfd, pfx_active,
rfd->rib_prefix_count);
assert(0);
}
}
}
if (checkstats && bgp->rfapi) {
if (t_pfx_active != bgp->rfapi->rib_prefix_count_total) {
vnc_zlog_debug_verbose(
"%s: actual total pfx count %u != running %u",
__func__, t_pfx_active,
bgp->rfapi->rib_prefix_count_total);
assert(0);
}
}
/*
* Check against memory allocation count
*/
alloc_count = mtype_stats_alloc(MTYPE_RFAPI_INFO);
assert(t_ri_active + t_ri_deleted + t_ri_pend + offset == alloc_count);
}
static struct rfapi_info *rfapi_info_new(void)
{
return XCALLOC(MTYPE_RFAPI_INFO, sizeof(struct rfapi_info));
}
void rfapiFreeRfapiUnOptionChain(struct rfapi_un_option *p)
{
while (p) {
struct rfapi_un_option *next;
next = p->next;
XFREE(MTYPE_RFAPI_UN_OPTION, p);
p = next;
}
}
void rfapiFreeRfapiVnOptionChain(struct rfapi_vn_option *p)
{
while (p) {
struct rfapi_vn_option *next;
next = p->next;
XFREE(MTYPE_RFAPI_VN_OPTION, p);
p = next;
}
}
/*
* Hash to keep track of outstanding timers so we can force them to
* expire at shutdown time, thus freeing their allocated memory.
*/
PREDECL_HASH(rrtcb);
/*
* Timer control block for recently-deleted and expired routes
*/
struct rfapi_rib_tcb {
struct rrtcb_item tcbi;
struct rfapi_descriptor *rfd;
struct skiplist *sl;
struct rfapi_info *ri;
struct agg_node *rn;
int flags;
#define RFAPI_RIB_TCB_FLAG_DELETED 0x00000001
};
static int _rrtcb_cmp(const struct rfapi_rib_tcb *t1, const struct rfapi_rib_tcb *t2)
{
return (t1 != t2);
}
static uint32_t _rrtcb_hash(const struct rfapi_rib_tcb *t)
{
return (uintptr_t)t & 0xffffffff;
}
DECLARE_HASH(rrtcb, struct rfapi_rib_tcb, tcbi, _rrtcb_cmp, _rrtcb_hash);
static struct rrtcb_head _rrtcbhash;
static void rfapi_info_free(struct rfapi_info *goner)
{
if (goner) {
#if DEBUG_CLEANUP
zlog_debug("%s: ri %p, timer %p", __func__, goner, goner->timer);
#endif
if (goner->tea_options) {
rfapiFreeBgpTeaOptionChain(goner->tea_options);
goner->tea_options = NULL;
}
if (goner->un_options) {
rfapiFreeRfapiUnOptionChain(goner->un_options);
goner->un_options = NULL;
}
if (goner->vn_options) {
rfapiFreeRfapiVnOptionChain(goner->vn_options);
goner->vn_options = NULL;
}
if (goner->timer) {
struct rfapi_rib_tcb *tcb;
tcb = EVENT_ARG(goner->timer);
#if DEBUG_CLEANUP
zlog_debug("%s: ri %p, tcb %p", __func__, goner, tcb);
#endif
event_cancel(&goner->timer);
rrtcb_del(&_rrtcbhash, tcb);
XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
}
XFREE(MTYPE_RFAPI_INFO, goner);
}
}
static void _rfapiRibExpireTimer(struct rfapi_rib_tcb *tcb)
{
RFAPI_RIB_CHECK_COUNTS(1, 0);
/*
* Forget reference to thread. Otherwise rfapi_info_free() will
* attempt to free thread pointer as an option chain
*/
tcb->ri->timer = NULL;
/* "deleted" skiplist frees ri, "active" doesn't */
assert(!skiplist_delete(tcb->sl, &tcb->ri->rk, NULL));
if (!tcb->sl->del) {
/*
* XXX in this case, skiplist has no delete function: we must
* therefore delete rfapi_info explicitly.
*/
rfapi_info_free(tcb->ri);
}
if (skiplist_empty(tcb->sl)) {
if (CHECK_FLAG(tcb->flags, RFAPI_RIB_TCB_FLAG_DELETED))
tcb->rn->aggregate = NULL;
else {
struct bgp *bgp = bgp_get_default();
tcb->rn->info = NULL;
RFAPI_RIB_PREFIX_COUNT_DECR(tcb->rfd, bgp->rfapi);
}
skiplist_free(tcb->sl);
agg_unlock_node(tcb->rn);
}
rrtcb_del(&_rrtcbhash, tcb);
XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
RFAPI_RIB_CHECK_COUNTS(1, 0);
}
/*
* remove route from rib
*/
static void rfapiRibExpireTimer(struct event *t)
{
struct rfapi_rib_tcb *tcb = EVENT_ARG(t);
_rfapiRibExpireTimer(tcb);
}
static void rfapiRibStartTimer(struct rfapi_descriptor *rfd,
struct rfapi_info *ri,
struct agg_node *rn, /* route node attached to */
int deleted)
{
struct rfapi_rib_tcb *tcb = NULL;
if (ri->timer) {
tcb = EVENT_ARG(ri->timer);
event_cancel(&ri->timer);
} else {
tcb = XCALLOC(MTYPE_RFAPI_RECENT_DELETE,
sizeof(struct rfapi_rib_tcb));
}
#if DEBUG_CLEANUP
zlog_debug("%s: rfd %p, rn %p, ri %p, tcb %p", __func__, rfd, rn, ri,
tcb);
#endif
tcb->rfd = rfd;
tcb->ri = ri;
tcb->rn = rn;
if (deleted) {
tcb->sl = (struct skiplist *)rn->aggregate;
SET_FLAG(tcb->flags, RFAPI_RIB_TCB_FLAG_DELETED);
} else {
tcb->sl = (struct skiplist *)rn->info;
UNSET_FLAG(tcb->flags, RFAPI_RIB_TCB_FLAG_DELETED);
}
vnc_zlog_debug_verbose("%s: rfd %p pfx %pRN life %u", __func__, rfd, rn,
ri->lifetime);
event_add_timer(bm->master, rfapiRibExpireTimer, tcb, ri->lifetime,
&ri->timer);
rrtcb_add(&_rrtcbhash, tcb);
}
extern void rfapi_rib_key_init(struct prefix *prefix, /* may be NULL */
struct prefix_rd *rd, /* may be NULL */
struct prefix *aux, /* may be NULL */
struct rfapi_rib_key *rk)
{
memset((void *)rk, 0, sizeof(struct rfapi_rib_key));
if (prefix)
rk->vn = *prefix;
if (rd)
rk->rd = *rd;
if (aux)
rk->aux_prefix = *aux;
}
/*
* Compares two <struct rfapi_rib_key>s
*/
int rfapi_rib_key_cmp(const void *k1, const void *k2)
{
const struct rfapi_rib_key *a = (struct rfapi_rib_key *)k1;
const struct rfapi_rib_key *b = (struct rfapi_rib_key *)k2;
int ret;
if (!a || !b)
return (a - b);
ret = vnc_prefix_cmp(&a->vn, &b->vn);
if (ret)
return ret;
ret = vnc_prefix_cmp(&a->rd, &b->rd);
if (ret)
return ret;
ret = vnc_prefix_cmp(&a->aux_prefix, &b->aux_prefix);
return ret;
}
/*
* Note: this function will claim that two option chains are
* different unless their option items are in identical order.
* The consequence is that RFP updated responses can be sent
* unnecessarily, or that they might contain nexthop items
* that are not strictly needed.
*
* This function could be modified to compare option chains more
* thoroughly, but it's not clear that the extra compuation would
* be worth it.
*/
static int bgp_tea_options_cmp(struct bgp_tea_options *a,
struct bgp_tea_options *b)
{
int rc;
if (!a || !b) {
return (a - b);
}
if (a->type != b->type)
return (a->type - b->type);
if (a->length != b->length)
return (a->length = b->length);
if ((rc = memcmp(a->value, b->value, a->length)))
return rc;
if (!a->next != !b->next) { /* logical xor */
return (a->next - b->next);
}
if (a->next)
return bgp_tea_options_cmp(a->next, b->next);
return 0;
}
static int rfapi_info_cmp(struct rfapi_info *a, struct rfapi_info *b)
{
int rc;
if (!a || !b)
return (a - b);
if ((rc = rfapi_rib_key_cmp(&a->rk, &b->rk)))
return rc;
if ((rc = vnc_prefix_cmp(&a->un, &b->un)))
return rc;
if (a->cost != b->cost)
return (a->cost - b->cost);
if (a->lifetime != b->lifetime)
return (a->lifetime - b->lifetime);
if ((rc = bgp_tea_options_cmp(a->tea_options, b->tea_options)))
return rc;
return 0;
}
void rfapiRibClear(struct rfapi_descriptor *rfd)
{
struct bgp *bgp;
afi_t afi;
if (rfd->bgp)
bgp = rfd->bgp;
else
bgp = bgp_get_default();
#ifdef DEBUG_L2_EXTRA
vnc_zlog_debug_verbose("%s: rfd=%p", __func__, rfd);
#endif
for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
struct agg_node *pn;
struct agg_node *rn;
if (rfd->rib_pending[afi]) {
for (pn = agg_route_top(rfd->rib_pending[afi]); pn;
pn = agg_route_next(pn)) {
if (pn->aggregate) {
/*
* free references into the rfapi_info
* structures before
* freeing the structures themselves
*/
skiplist_free(
(struct skiplist
*)(pn->aggregate));
pn->aggregate = NULL;
agg_unlock_node(
pn); /* skiplist deleted */
}
/*
* free the rfapi_info structures
*/
if (pn->info) {
if (pn->info != (void *)1) {
list_delete(
(struct list *
*)(&pn->info));
}
pn->info = NULL;
/* linklist or 1 deleted */
agg_unlock_node(pn);
}
}
}
if (rfd->rib[afi]) {
for (rn = agg_route_top(rfd->rib[afi]); rn;
rn = agg_route_next(rn)) {
if (rn->info) {
struct rfapi_info *ri;
while (0 == skiplist_first(
(struct skiplist *)
rn->info,
NULL,
(void **)&ri)) {
if (ri->timer) {
struct rfapi_rib_tcb
*tcb;
tcb = EVENT_ARG(
ri->timer);
event_cancel(&ri->timer);
rrtcb_del(&_rrtcbhash, tcb);
XFREE(MTYPE_RFAPI_RECENT_DELETE,
tcb);
}
rfapi_info_free(ri);
skiplist_delete_first(
(struct skiplist *)
rn->info);
}
skiplist_free(
(struct skiplist *)rn->info);
rn->info = NULL;
agg_unlock_node(rn);
RFAPI_RIB_PREFIX_COUNT_DECR(rfd,
bgp->rfapi);
}
if (rn->aggregate) {
struct rfapi_info *ri_del;
/* delete skiplist & contents */
while (!skiplist_first(
(struct skiplist
*)(rn->aggregate),
NULL, (void **)&ri_del)) {
/* sl->del takes care of ri_del
*/
skiplist_delete_first((
struct skiplist
*)(rn->aggregate));
}
skiplist_free(
(struct skiplist
*)(rn->aggregate));
rn->aggregate = NULL;
agg_unlock_node(rn);
}
}
}
}
if (rfd->updated_responses_queue)
work_queue_free_and_null(&rfd->updated_responses_queue);
}
/*
* Release all dynamically-allocated memory that is part of an HD's RIB
*/
void rfapiRibFree(struct rfapi_descriptor *rfd)
{
afi_t afi;
#if DEBUG_CLEANUP
zlog_debug("%s: rfd %p", __func__, rfd);
#endif
/*
* NB rfd is typically detached from master list, so is not included
* in the count performed by RFAPI_RIB_CHECK_COUNTS
*/
/*
* Free routes attached to radix trees
*/
rfapiRibClear(rfd);
/* Now the uncounted rfapi_info's are freed, so the check should succeed
*/
RFAPI_RIB_CHECK_COUNTS(1, 0);
/*
* Free radix trees
*/
for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
if (rfd->rib_pending[afi])
agg_table_finish(rfd->rib_pending[afi]);
rfd->rib_pending[afi] = NULL;
if (rfd->rib[afi])
agg_table_finish(rfd->rib[afi]);
rfd->rib[afi] = NULL;
/* NB agg_table_finish frees only prefix nodes, not chained
* info */
if (rfd->rsp_times[afi])
agg_table_finish(rfd->rsp_times[afi]);
rfd->rib[afi] = NULL;
}
}
/*
* Copies struct bgp_path_info to struct rfapi_info, except for rk fields and un
*/
static void rfapiRibBi2Ri(struct bgp_path_info *bpi, struct rfapi_info *ri,
uint32_t lifetime)
{
struct bgp_attr_encap_subtlv *pEncap;
ri->cost = rfapiRfpCost(bpi->attr);
ri->lifetime = lifetime;
/* This loop based on rfapiRouteInfo2NextHopEntry() */
for (pEncap = bgp_attr_get_vnc_subtlvs(bpi->attr); pEncap;
pEncap = pEncap->next) {
struct bgp_tea_options *hop;
switch (pEncap->type) {
case BGP_VNC_SUBTLV_TYPE_LIFETIME:
/* use configured lifetime, not attr lifetime */
break;
case BGP_VNC_SUBTLV_TYPE_RFPOPTION:
hop = XCALLOC(MTYPE_BGP_TEA_OPTIONS,
sizeof(struct bgp_tea_options));
assert(hop);
hop->type = pEncap->value[0];
hop->length = pEncap->value[1];
hop->value = XCALLOC(MTYPE_BGP_TEA_OPTIONS_VALUE,
pEncap->length - 2);
assert(hop->value);
memcpy(hop->value, pEncap->value + 2,
pEncap->length - 2);
if (hop->length > pEncap->length - 2) {
zlog_warn(
"%s: VNC subtlv length mismatch: RFP option says %d, attr says %d (shrinking)",
__func__, hop->length,
pEncap->length - 2);
hop->length = pEncap->length - 2;
}
hop->next = ri->tea_options;
ri->tea_options = hop;
break;
default:
break;
}
}
rfapi_un_options_free(ri->un_options); /* maybe free old version */
ri->un_options = rfapi_encap_tlv_to_un_option(bpi->attr);
/*
* VN options
*/
if (bpi->extra && decode_rd_type(bpi->extra->vnc->vnc.import.rd.val) ==
RD_TYPE_VNC_ETH) {
/* ethernet route */
struct rfapi_vn_option *vo;
vo = XCALLOC(MTYPE_RFAPI_VN_OPTION,
sizeof(struct rfapi_vn_option));
assert(vo);
vo->type = RFAPI_VN_OPTION_TYPE_L2ADDR;
/* copy from RD already stored in bpi, so we don't need it_node
*/
memcpy(&vo->v.l2addr.macaddr,
bpi->extra->vnc->vnc.import.rd.val + 2, ETH_ALEN);
(void)rfapiEcommunityGetLNI(bgp_attr_get_ecommunity(bpi->attr),
&vo->v.l2addr.logical_net_id);
(void)rfapiEcommunityGetEthernetTag(
bgp_attr_get_ecommunity(bpi->attr),
&vo->v.l2addr.tag_id);
/* local_nve_id comes from RD */
vo->v.l2addr.local_nve_id =
bpi->extra->vnc->vnc.import.rd.val[1];
/* label comes from MP_REACH_NLRI label */
vo->v.l2addr.label =
BGP_PATH_INFO_NUM_LABELS(bpi)
? decode_label(&bpi->extra->labels->label[0])
: MPLS_INVALID_LABEL;
rfapi_vn_options_free(
ri->vn_options); /* maybe free old version */
ri->vn_options = vo;
}
/*
* If there is an auxiliary IP address (L2 can have it), copy it
*/
if (bpi->extra && bpi->extra->vnc->vnc.import.aux_prefix.family) {
ri->rk.aux_prefix = bpi->extra->vnc->vnc.import.aux_prefix;
}
}
/*
* rfapiRibPreloadBi
*
* Install route into NVE RIB model so as to be consistent with
* caller's response to rfapi_query().
*
* Also: return indication to caller whether this specific route
* should be included in the response to the NVE according to
* the following tests:
*
* 1. If there were prior duplicates of this route in this same
* query response, don't include the route.
*
* RETURN VALUE:
*
* 0 OK to include route in response
* !0 do not include route in response
*/
int rfapiRibPreloadBi(
struct agg_node *rfd_rib_node, /* NULL = don't preload or filter */
struct prefix *pfx_vn, struct prefix *pfx_un, uint32_t lifetime,
struct bgp_path_info *bpi)
{
struct rfapi_descriptor *rfd;
struct skiplist *slRibPt = NULL;
struct rfapi_info *ori = NULL;
struct rfapi_rib_key rk;
struct agg_node *trn;
afi_t afi;
const struct prefix *p = agg_node_get_prefix(rfd_rib_node);
if (!rfd_rib_node)
return 0;
afi = family2afi(p->family);
rfd = agg_get_table_info(agg_get_table(rfd_rib_node));
memset((void *)&rk, 0, sizeof(rk));
rk.vn = *pfx_vn;
rk.rd = bpi->extra->vnc->vnc.import.rd;
/*
* If there is an auxiliary IP address (L2 can have it), copy it
*/
if (bpi->extra->vnc->vnc.import.aux_prefix.family) {
rk.aux_prefix = bpi->extra->vnc->vnc.import.aux_prefix;
}
/*
* is this route already in NVE's RIB?
*/
slRibPt = (struct skiplist *)rfd_rib_node->info;
if (slRibPt && !skiplist_search(slRibPt, &rk, (void **)&ori)) {
if ((ori->rsp_counter == rfd->rsp_counter)
&& (ori->last_sent_time == rfd->rsp_time)) {
return -1; /* duplicate in this response */
}
/* found: update contents of existing route in RIB */
ori->un = *pfx_un;
rfapiRibBi2Ri(bpi, ori, lifetime);
} else {
/* not found: add new route to RIB */
ori = rfapi_info_new();
ori->rk = rk;
ori->un = *pfx_un;
rfapiRibBi2Ri(bpi, ori, lifetime);
if (!slRibPt) {
slRibPt = skiplist_new(0, rfapi_rib_key_cmp, NULL);
rfd_rib_node->info = slRibPt;
agg_lock_node(rfd_rib_node);
RFAPI_RIB_PREFIX_COUNT_INCR(rfd, rfd->bgp->rfapi);
}
skiplist_insert(slRibPt, &ori->rk, ori);
}
ori->last_sent_time = monotime(NULL);
/*
* poke timer
*/
RFAPI_RIB_CHECK_COUNTS(0, 0);
rfapiRibStartTimer(rfd, ori, rfd_rib_node, 0);
RFAPI_RIB_CHECK_COUNTS(0, 0);
/*
* Update last sent time for prefix
*/
trn = agg_node_get(rfd->rsp_times[afi], p); /* locks trn */
trn->info = (void *)(uintptr_t)monotime(NULL);
if (agg_node_get_lock_count(trn) > 1)
agg_unlock_node(trn);
return 0;
}
/*
* Frees rfapi_info items at node
*
* Adjust 'rib' and 'rib_pending' as follows:
*
* If rib_pending node->info is 1 (magic value):
* callback: NHL = RIB NHL with lifetime = withdraw_lifetime_value
* RIB = remove all routes at the node
* DONE
*
* For each item at rib node:
* if not present in pending node, move RIB item to "delete list"
*
* For each item at pending rib node:
* if present (same vn/un) in rib node with same lifetime & options, drop
* matching item from pending node
*
* For each remaining item at pending rib node, add or replace item
* at rib node.
*
* Construct NHL as concatenation of pending list + delete list
*
* Clear pending node
*/
static void process_pending_node(struct bgp *bgp, struct rfapi_descriptor *rfd,
afi_t afi,
struct agg_node *pn, /* pending node */
struct rfapi_next_hop_entry **head,
struct rfapi_next_hop_entry **tail)
{
struct listnode *node = NULL;
struct listnode *nnode = NULL;
struct rfapi_info *ri = NULL; /* happy valgrind */
struct rfapi_ip_prefix hp = {0}; /* pfx to put in NHE */
struct agg_node *rn = NULL;
struct skiplist *slRibPt = NULL; /* rib list */
struct skiplist *slPendPt = NULL;
struct list *lPendCost = NULL;
struct list *delete_list = NULL;
int printedprefix = 0;
int rib_node_started_nonempty = 0;
int sendingsomeroutes = 0;
const struct prefix *p;
assert(pn);
p = agg_node_get_prefix(pn);
vnc_zlog_debug_verbose("%s: afi=%d, %pRN pn->info=%p", __func__, afi,
pn, pn->info);
if (AFI_L2VPN != afi) {
rfapiQprefix2Rprefix(p, &hp);
}
RFAPI_RIB_CHECK_COUNTS(1, 0);
/*
* Find corresponding RIB node
*/
rn = agg_node_get(rfd->rib[afi], p); /* locks rn */
/*
* RIB skiplist has key=rfapi_addr={vn,un}, val = rfapi_info,
* skiplist.del = NULL
*/
slRibPt = (struct skiplist *)rn->info;
if (slRibPt)
rib_node_started_nonempty = 1;
slPendPt = (struct skiplist *)(pn->aggregate);
lPendCost = (struct list *)(pn->info);
/*
* Handle special case: delete all routes at prefix
*/
if (lPendCost == (struct list *)1) {
vnc_zlog_debug_verbose("%s: lPendCost=1 => delete all",
__func__);
if (slRibPt && !skiplist_empty(slRibPt)) {
delete_list = list_new();
while (0
== skiplist_first(slRibPt, NULL, (void **)&ri)) {
listnode_add(delete_list, ri);
vnc_zlog_debug_verbose(
"%s: after listnode_add, delete_list->count=%d",
__func__, delete_list->count);
rfapiFreeBgpTeaOptionChain(ri->tea_options);
ri->tea_options = NULL;
if (ri->timer) {
struct rfapi_rib_tcb *tcb;
tcb = EVENT_ARG(ri->timer);
event_cancel(&ri->timer);
rrtcb_del(&_rrtcbhash, tcb);
XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
}
vnc_zlog_debug_verbose(
"%s: put dl pfx=%pRN vn=%pFX un=%pFX cost=%d life=%d vn_options=%p",
__func__, pn, &ri->rk.vn, &ri->un,
ri->cost, ri->lifetime, ri->vn_options);
skiplist_delete_first(slRibPt);
}
assert(skiplist_empty(slRibPt));
skiplist_free(slRibPt);
rn->info = slRibPt = NULL;
agg_unlock_node(rn);
lPendCost = pn->info = NULL;
agg_unlock_node(pn);
goto callback;
}
if (slRibPt) {
skiplist_free(slRibPt);
rn->info = NULL;
agg_unlock_node(rn);
}
assert(!slPendPt);
if (slPendPt) { /* TBD I think we can toss this block */
skiplist_free(slPendPt);
pn->aggregate = NULL;
agg_unlock_node(pn);
}
pn->info = NULL;
agg_unlock_node(pn);
agg_unlock_node(rn); /* agg_node_get() */
if (rib_node_started_nonempty) {
RFAPI_RIB_PREFIX_COUNT_DECR(rfd, bgp->rfapi);
}
RFAPI_RIB_CHECK_COUNTS(1, 0);
return;
}
vnc_zlog_debug_verbose("%s: lPendCost->count=%d, slRibPt->count=%d",
__func__,
(lPendCost ? (int)lPendCost->count : -1),
(slRibPt ? (int)slRibPt->count : -1));
/*
* Iterate over routes at RIB Node.
* If not found at Pending Node, delete from RIB Node and add to
* deletelist
* If found at Pending Node
* If identical rfapi_info, delete from Pending Node
*/
if (slRibPt) {
void *cursor = NULL;
struct rfapi_info *ori;
/*
* Iterate over RIB List
*
*/
while (!skiplist_next(slRibPt, NULL, (void **)&ori, &cursor)) {
if (skiplist_search(slPendPt, &ori->rk, (void **)&ri)) {
/*
* Not in Pending list, so it should be deleted
*/
if (!delete_list)
delete_list = list_new();
listnode_add(delete_list, ori);
rfapiFreeBgpTeaOptionChain(ori->tea_options);
ori->tea_options = NULL;
if (ori->timer) {
struct rfapi_rib_tcb *tcb;
tcb = EVENT_ARG(ori->timer);
event_cancel(&ori->timer);
rrtcb_del(&_rrtcbhash, tcb);
XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
}
#if DEBUG_PROCESS_PENDING_NODE
/* deleted from slRibPt below, after we're done
* iterating */
vnc_zlog_debug_verbose(
"%s: slRibPt ri %p not matched in pending list, delete",
__func__, ori);
#endif
} else {
#if DEBUG_PROCESS_PENDING_NODE
vnc_zlog_debug_verbose("%s: slRibPt ri %p matched in pending list",
__func__, ori);
#endif
/*
* Found in pending list. If same lifetime,
* cost, options,
* then remove from pending list because the
* route
* hasn't changed.
*/
if (!rfapi_info_cmp(ori, ri)) {
skiplist_delete(slPendPt, &ri->rk,
NULL);
assert(lPendCost);
if (lPendCost) {
/* linear walk: might need
* optimization */
listnode_delete(lPendCost,
ri); /* XXX
doesn't
free
data!
bug? */
rfapi_info_free(
ri); /* grr... */
}
#if DEBUG_PROCESS_PENDING_NODE
vnc_zlog_debug_verbose("%s: same info", __func__);
#endif
}
}
}
/*
* Go back and delete items from RIB
*/
if (delete_list) {
for (ALL_LIST_ELEMENTS_RO(delete_list, node, ri)) {
vnc_zlog_debug_verbose(
"%s: deleting ri %p from slRibPt",
__func__, ri);
assert(!skiplist_delete(slRibPt, &ri->rk,
NULL));
}
if (skiplist_empty(slRibPt)) {
skiplist_free(slRibPt);
slRibPt = rn->info = NULL;
agg_unlock_node(rn);
}
}
}
RFAPI_RIB_CHECK_COUNTS(0, (delete_list ? delete_list->count : 0));
/*
* Iterate over routes at Pending Node
*
* If {vn} found at RIB Node, update RIB Node route contents to match PN
* If {vn} NOT found at RIB Node, add copy to RIB Node
*/
if (lPendCost) {
for (ALL_LIST_ELEMENTS_RO(lPendCost, node, ri)) {
struct rfapi_info *ori;
if (slRibPt
&& !skiplist_search(slRibPt, &ri->rk,
(void **)&ori)) {
/* found: update contents of existing route in
* RIB */
ori->un = ri->un;
ori->cost = ri->cost;
ori->lifetime = ri->lifetime;
rfapiFreeBgpTeaOptionChain(ori->tea_options);
ori->tea_options =
rfapiOptionsDup(ri->tea_options);
ori->last_sent_time = monotime(NULL);
rfapiFreeRfapiVnOptionChain(ori->vn_options);
ori->vn_options =
rfapiVnOptionsDup(ri->vn_options);
rfapiFreeRfapiUnOptionChain(ori->un_options);
ori->un_options =
rfapiUnOptionsDup(ri->un_options);
vnc_zlog_debug_verbose(
"%s: matched lPendCost item %p in slRibPt, rewrote",
__func__, ri);
} else {
/* not found: add new route to RIB */
ori = rfapi_info_new();
ori->rk = ri->rk;
ori->un = ri->un;
ori->cost = ri->cost;
ori->lifetime = ri->lifetime;
ori->tea_options =
rfapiOptionsDup(ri->tea_options);
ori->last_sent_time = monotime(NULL);
ori->vn_options =
rfapiVnOptionsDup(ri->vn_options);
ori->un_options =
rfapiUnOptionsDup(ri->un_options);
if (!slRibPt) {
slRibPt = skiplist_new(
0, rfapi_rib_key_cmp, NULL);
rn->info = slRibPt;
agg_lock_node(rn);
}
skiplist_insert(slRibPt, &ori->rk, ori);
vnc_zlog_debug_verbose(
"%s: nomatch lPendCost item %p in slRibPt, added (rd=%pRDP)",
__func__, ri, &ori->rk.rd);
}
/*
* poke timer
*/
RFAPI_RIB_CHECK_COUNTS(
0, (delete_list ? delete_list->count : 0));
rfapiRibStartTimer(rfd, ori, rn, 0);
RFAPI_RIB_CHECK_COUNTS(
0, (delete_list ? delete_list->count : 0));
}
}
callback:
/*
* Construct NHL as concatenation of pending list + delete list
*/
RFAPI_RIB_CHECK_COUNTS(0, (delete_list ? delete_list->count : 0));
if (lPendCost) {
char buf[BUFSIZ];
char buf2[BUFSIZ];
vnc_zlog_debug_verbose("%s: lPendCost->count now %d", __func__,
lPendCost->count);
vnc_zlog_debug_verbose("%s: For prefix %pRN (a)", __func__, pn);
printedprefix = 1;
for (ALL_LIST_ELEMENTS(lPendCost, node, nnode, ri)) {
struct rfapi_next_hop_entry *new;
struct agg_node *trn;
new = XCALLOC(MTYPE_RFAPI_NEXTHOP,
sizeof(struct rfapi_next_hop_entry));
if (ri->rk.aux_prefix.family) {
rfapiQprefix2Rprefix(&ri->rk.aux_prefix,
&new->prefix);
} else {
new->prefix = hp;
if (AFI_L2VPN == afi) {
/* hp is 0; need to set length to match
* AF of vn */
new->prefix.length =
(ri->rk.vn.family == AF_INET)
? 32
: 128;
}
}
new->prefix.cost = ri->cost;
new->lifetime = ri->lifetime;
rfapiQprefix2Raddr(&ri->rk.vn, &new->vn_address);
rfapiQprefix2Raddr(&ri->un, &new->un_address);
/* free option chain from ri */
rfapiFreeBgpTeaOptionChain(ri->tea_options);
ri->tea_options =
NULL; /* option chain was transferred to NHL */
new->vn_options = ri->vn_options;
ri->vn_options =
NULL; /* option chain was transferred to NHL */
new->un_options = ri->un_options;
ri->un_options =
NULL; /* option chain was transferred to NHL */
if (*tail)
(*tail)->next = new;
*tail = new;
if (!*head) {
*head = new;
}
sendingsomeroutes = 1;
++rfd->stat_count_nh_reachable;
++bgp->rfapi->stat.count_updated_response_updates;
/*
* update this NVE's timestamp for this prefix
*/
trn = agg_node_get(rfd->rsp_times[afi],
p); /* locks trn */
trn->info = (void *)(uintptr_t)monotime(NULL);
if (agg_node_get_lock_count(trn) > 1)
agg_unlock_node(trn);
rfapiRfapiIpAddr2Str(&new->vn_address, buf, BUFSIZ);
rfapiRfapiIpAddr2Str(&new->un_address, buf2, BUFSIZ);
vnc_zlog_debug_verbose(
"%s: add vn=%s un=%s cost=%d life=%d",
__func__, buf, buf2, new->prefix.cost,
new->lifetime);
}
}
RFAPI_RIB_CHECK_COUNTS(0, (delete_list ? delete_list->count : 0));
if (delete_list) {
char buf[BUFSIZ];
char buf2[BUFSIZ];
if (!printedprefix) {
vnc_zlog_debug_verbose("%s: For prefix %pRN (d)",
__func__, pn);
}
vnc_zlog_debug_verbose("%s: delete_list has %d elements",
__func__, delete_list->count);
RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
if (!CHECK_FLAG(bgp->rfapi_cfg->flags,
BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE)) {
for (ALL_LIST_ELEMENTS(delete_list, node, nnode, ri)) {
struct rfapi_next_hop_entry *new;
struct rfapi_info *ri_del;
RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
new = XCALLOC(
MTYPE_RFAPI_NEXTHOP,
sizeof(struct rfapi_next_hop_entry));
if (ri->rk.aux_prefix.family) {
rfapiQprefix2Rprefix(&ri->rk.aux_prefix,
&new->prefix);
} else {
new->prefix = hp;
if (AFI_L2VPN == afi) {
/* hp is 0; need to set length
* to match AF of vn */
new->prefix.length =
(ri->rk.vn.family
== AF_INET)
? 32
: 128;
}
}
new->prefix.cost = ri->cost;
new->lifetime = RFAPI_REMOVE_RESPONSE_LIFETIME;
rfapiQprefix2Raddr(&ri->rk.vn,
&new->vn_address);
rfapiQprefix2Raddr(&ri->un, &new->un_address);
new->vn_options = ri->vn_options;
ri->vn_options = NULL; /* option chain was
transferred to NHL */
new->un_options = ri->un_options;
ri->un_options = NULL; /* option chain was
transferred to NHL */
if (*tail)
(*tail)->next = new;
*tail = new;
if (!*head) {
*head = new;
}
++rfd->stat_count_nh_removal;
++bgp->rfapi->stat
.count_updated_response_deletes;
rfapiRfapiIpAddr2Str(&new->vn_address, buf,
BUFSIZ);
rfapiRfapiIpAddr2Str(&new->un_address, buf2,
BUFSIZ);
vnc_zlog_debug_verbose(
"%s: DEL vn=%s un=%s cost=%d life=%d",
__func__, buf, buf2, new->prefix.cost,
new->lifetime);
RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
/*
* Update/add to list of recent deletions at
* this prefix
*/
if (!rn->aggregate) {
rn->aggregate = skiplist_new(
0, rfapi_rib_key_cmp,
(void (*)(void *))
rfapi_info_free);
agg_lock_node(rn);
}
RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
/* sanity check lifetime */
if (ri->lifetime
> RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY)
ri->lifetime =
RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY;
RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
/* cancel normal expire timer */
if (ri->timer) {
struct rfapi_rib_tcb *tcb;
tcb = EVENT_ARG(ri->timer);
event_cancel(&ri->timer);
rrtcb_del(&_rrtcbhash, tcb);
XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
}
RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
/*
* Look in "recently-deleted" list
*/
if (skiplist_search(
(struct skiplist *)(rn->aggregate),
&ri->rk, (void **)&ri_del)) {
int rc;
RFAPI_RIB_CHECK_COUNTS(
0, delete_list->count);
/*
* NOT in "recently-deleted" list
*/
list_delete_node(
delete_list,
node); /* does not free ri */
rc = skiplist_insert(
(struct skiplist
*)(rn->aggregate),
&ri->rk, ri);
assert(!rc);
RFAPI_RIB_CHECK_COUNTS(
0, delete_list->count);
rfapiRibStartTimer(rfd, ri, rn, 1);
RFAPI_RIB_CHECK_COUNTS(
0, delete_list->count);
ri->last_sent_time = monotime(NULL);
#if DEBUG_RIB_SL_RD
vnc_zlog_debug_verbose(
"%s: move route to recently deleted list, rd=%pRDP",
__func__, &ri->rk.rd);
#endif
} else {
/*
* IN "recently-deleted" list
*/
RFAPI_RIB_CHECK_COUNTS(
0, delete_list->count);
rfapiRibStartTimer(rfd, ri_del, rn, 1);
RFAPI_RIB_CHECK_COUNTS(
0, delete_list->count);
ri->last_sent_time = monotime(NULL);
}
}
} else {
vnc_zlog_debug_verbose(
"%s: response removal disabled, omitting removals",
__func__);
}
delete_list->del = (void (*)(void *))rfapi_info_free;
list_delete(&delete_list);
}
RFAPI_RIB_CHECK_COUNTS(0, 0);
/*
* Reset pending lists. The final agg_unlock_node() will probably
* cause the pending node to be released.
*/
if (slPendPt) {
skiplist_free(slPendPt);
pn->aggregate = NULL;
agg_unlock_node(pn);
}
if (lPendCost) {
list_delete(&lPendCost);
pn->info = NULL;
agg_unlock_node(pn);
}
RFAPI_RIB_CHECK_COUNTS(0, 0);
if (rib_node_started_nonempty) {
if (!rn->info) {
RFAPI_RIB_PREFIX_COUNT_DECR(rfd, bgp->rfapi);
}
} else {
if (rn->info) {
RFAPI_RIB_PREFIX_COUNT_INCR(rfd, bgp->rfapi);
}
}
if (sendingsomeroutes)
rfapiMonitorTimersRestart(rfd, p);
agg_unlock_node(rn); /* agg_node_get() */
RFAPI_RIB_CHECK_COUNTS(1, 0);
}
/*
* regardless of targets, construct a single callback by doing
* only one traversal of the pending RIB
*
*
* Do callback
*
*/
static void rib_do_callback_onepass(struct rfapi_descriptor *rfd, afi_t afi)
{
struct bgp *bgp = bgp_get_default();
struct rfapi_next_hop_entry *head = NULL;
struct rfapi_next_hop_entry *tail = NULL;
struct agg_node *rn;
#ifdef DEBUG_L2_EXTRA
vnc_zlog_debug_verbose("%s: rfd=%p, afi=%d", __func__, rfd, afi);
#endif
if (!rfd->rib_pending[afi])
return;
assert(bgp->rfapi);
for (rn = agg_route_top(rfd->rib_pending[afi]); rn;
rn = agg_route_next(rn)) {
process_pending_node(bgp, rfd, afi, rn, &head, &tail);
}
if (head) {
rfapi_response_cb_t *f;
#if DEBUG_NHL
vnc_zlog_debug_verbose("%s: response callback NHL follows:",
__func__);
rfapiPrintNhl(NULL, head);
#endif
if (rfd->response_cb)
f = rfd->response_cb;
else
f = bgp->rfapi->rfp_methods.response_cb;
bgp->rfapi->flags |= RFAPI_INCALLBACK;
vnc_zlog_debug_verbose("%s: invoking updated response callback",
__func__);
(*f)(head, rfd->cookie);
bgp->rfapi->flags &= ~RFAPI_INCALLBACK;
++bgp->rfapi->response_updated_count;
}
}
static wq_item_status rfapiRibDoQueuedCallback(struct work_queue *wq,
void *data)
{
struct rfapi_descriptor *rfd;
afi_t afi;
uint32_t queued_flag;
RFAPI_RIB_CHECK_COUNTS(1, 0);
rfd = ((struct rfapi_updated_responses_queue *)data)->rfd;
afi = ((struct rfapi_updated_responses_queue *)data)->afi;
/* Make sure the HD wasn't closed after the work item was scheduled */
if (rfapi_check(rfd))
return WQ_SUCCESS;
rib_do_callback_onepass(rfd, afi);
queued_flag = RFAPI_QUEUED_FLAG(afi);
UNSET_FLAG(rfd->flags, queued_flag);
RFAPI_RIB_CHECK_COUNTS(1, 0);
return WQ_SUCCESS;
}
static void rfapiRibQueueItemDelete(struct work_queue *wq, void *data)
{
XFREE(MTYPE_RFAPI_UPDATED_RESPONSE_QUEUE, data);
}
static void updated_responses_queue_init(struct rfapi_descriptor *rfd)
{
if (rfd->updated_responses_queue)
return;
rfd->updated_responses_queue =
work_queue_new(bm->master, "rfapi updated responses");
assert(rfd->updated_responses_queue);
rfd->updated_responses_queue->spec.workfunc = rfapiRibDoQueuedCallback;
rfd->updated_responses_queue->spec.del_item_data =
rfapiRibQueueItemDelete;
rfd->updated_responses_queue->spec.max_retries = 0;
rfd->updated_responses_queue->spec.hold = 1;
}
/*
* Called when an import table node is modified. Construct a
* new complete nexthop list, sorted by cost (lowest first),
* based on the import table node.
*
* Filter out duplicate nexthops (vn address). There should be
* only one UN address per VN address from the point of view of
* a given import table, so we can probably ignore UN addresses
* while filtering.
*
* Based on rfapiNhlAddNodeRoutes()
*/
void rfapiRibUpdatePendingNode(
struct bgp *bgp, struct rfapi_descriptor *rfd,
struct rfapi_import_table *it, /* needed for L2 */
struct agg_node *it_node, uint32_t lifetime)
{
const struct prefix *prefix;
struct bgp_path_info *bpi;
struct agg_node *pn;
afi_t afi;
uint32_t queued_flag;
int count = 0;
vnc_zlog_debug_verbose("%s: entry", __func__);
if (CHECK_FLAG(bgp->rfapi_cfg->flags, BGP_VNC_CONFIG_CALLBACK_DISABLE))
return;
vnc_zlog_debug_verbose("%s: callbacks are not disabled", __func__);
RFAPI_RIB_CHECK_COUNTS(1, 0);
prefix = agg_node_get_prefix(it_node);
afi = family2afi(prefix->family);
vnc_zlog_debug_verbose("%s: prefix=%pFX", __func__, prefix);
pn = agg_node_get(rfd->rib_pending[afi], prefix);
assert(pn);
vnc_zlog_debug_verbose("%s: pn->info=%p, pn->aggregate=%p", __func__,
pn->info, pn->aggregate);
if (pn->aggregate) {
/*
* free references into the rfapi_info structures before
* freeing the structures themselves
*/
skiplist_free((struct skiplist *)(pn->aggregate));
pn->aggregate = NULL;
agg_unlock_node(pn); /* skiplist deleted */
}
/*
* free the rfapi_info structures
*/
if (pn->info) {
if (pn->info != (void *)1) {
list_delete((struct list **)(&pn->info));
}
pn->info = NULL;
agg_unlock_node(pn); /* linklist or 1 deleted */
}
/*
* The BPIs in the import table are already sorted by cost
*/
for (bpi = it_node->info; bpi; bpi = bpi->next) {
struct rfapi_info *ri;
struct prefix pfx_nh;
if (!bpi->extra) {
/* shouldn't happen */
/* TBD increment error stats counter */
continue;
}
rfapiNexthop2Prefix(bpi->attr, &pfx_nh);
/*
* Omit route if nexthop is self
*/
if (CHECK_FLAG(bgp->rfapi_cfg->flags,
BGP_VNC_CONFIG_FILTER_SELF_FROM_RSP)) {
struct prefix pfx_vn;
assert(!rfapiRaddr2Qprefix(&rfd->vn_addr, &pfx_vn));
if (prefix_same(&pfx_vn, &pfx_nh))
continue;
}
ri = rfapi_info_new();
ri->rk.vn = pfx_nh;
ri->rk.rd = bpi->extra->vnc->vnc.import.rd;
/*
* If there is an auxiliary IP address (L2 can have it), copy it
*/
if (bpi->extra->vnc->vnc.import.aux_prefix.family) {
ri->rk.aux_prefix =
bpi->extra->vnc->vnc.import.aux_prefix;
}
if (rfapiGetUnAddrOfVpnBi(bpi, &ri->un)) {
rfapi_info_free(ri);
continue;
}
if (!pn->aggregate) {
pn->aggregate =
skiplist_new(0, rfapi_rib_key_cmp, NULL);
agg_lock_node(pn);
}
/*
* If we have already added this nexthop, the insert will fail.
* Note that the skiplist key is a pointer INTO the rfapi_info
* structure which will be added to the "info" list.
* The skiplist entry VALUE is not used for anything but
* might be useful during debugging.
*/
if (skiplist_insert((struct skiplist *)pn->aggregate, &ri->rk,
ri)) {
/*
* duplicate
*/
rfapi_info_free(ri);
continue;
}
rfapiRibBi2Ri(bpi, ri, lifetime);
if (!pn->info) {
pn->info = list_new();
((struct list *)(pn->info))->del =
(void (*)(void *))rfapi_info_free;
agg_lock_node(pn);
}
listnode_add((struct list *)(pn->info), ri);
}
if (pn->info) {
count = ((struct list *)(pn->info))->count;
}
if (!count) {
assert(!pn->info);
assert(!pn->aggregate);
pn->info = (void *)1; /* magic value means this node has no
routes */
agg_lock_node(pn);
}
agg_unlock_node(pn); /* agg_node_get */
queued_flag = RFAPI_QUEUED_FLAG(afi);
if (!CHECK_FLAG(rfd->flags, queued_flag)) {
struct rfapi_updated_responses_queue *urq;
urq = XCALLOC(MTYPE_RFAPI_UPDATED_RESPONSE_QUEUE,
sizeof(struct rfapi_updated_responses_queue));
if (!rfd->updated_responses_queue)
updated_responses_queue_init(rfd);
SET_FLAG(rfd->flags, queued_flag);
urq->rfd = rfd;
urq->afi = afi;
work_queue_add(rfd->updated_responses_queue, urq);
}
RFAPI_RIB_CHECK_COUNTS(1, 0);
}
void rfapiRibUpdatePendingNodeSubtree(
struct bgp *bgp, struct rfapi_descriptor *rfd,
struct rfapi_import_table *it, struct agg_node *it_node,
struct agg_node *omit_subtree, /* may be NULL */
uint32_t lifetime)
{
/* FIXME: need to find a better way here to work without sticking our
* hands in node->link */
if (agg_node_left(it_node)
&& (agg_node_left(it_node) != omit_subtree)) {
if (agg_node_left(it_node)->info)
rfapiRibUpdatePendingNode(
bgp, rfd, it, agg_node_left(it_node), lifetime);
rfapiRibUpdatePendingNodeSubtree(bgp, rfd, it,
agg_node_left(it_node),
omit_subtree, lifetime);
}
if (agg_node_right(it_node)
&& (agg_node_right(it_node) != omit_subtree)) {
if (agg_node_right(it_node)->info)
rfapiRibUpdatePendingNode(bgp, rfd, it,
agg_node_right(it_node),
lifetime);
rfapiRibUpdatePendingNodeSubtree(bgp, rfd, it,
agg_node_right(it_node),
omit_subtree, lifetime);
}
}
/*
* RETURN VALUE
*
* 0 allow prefix to be included in response
* !0 don't allow prefix to be included in response
*/
int rfapiRibFTDFilterRecentPrefix(
struct rfapi_descriptor *rfd,
struct agg_node *it_rn, /* import table node */
struct prefix *pfx_target_original) /* query target */
{
struct bgp *bgp = rfd->bgp;
const struct prefix *p = agg_node_get_prefix(it_rn);
afi_t afi = family2afi(p->family);
time_t prefix_time;
struct agg_node *trn;
/*
* Not in FTD mode, so allow prefix
*/
if (bgp->rfapi_cfg->rfp_cfg.download_type != RFAPI_RFP_DOWNLOAD_FULL)
return 0;
/*
* TBD
* This matches behavior of now-obsolete rfapiRibFTDFilterRecent(),
* but we need to decide if that is correct.
*/
if (p->family == AF_ETHERNET)
return 0;
#ifdef DEBUG_FTD_FILTER_RECENT
{
vnc_zlog_debug_verbose("%s: prefix %pFX", __func__,
agg_node_get_prefix(it_rn));
}
#endif
/*
* prefix covers target address, so allow prefix
*/
if (prefix_match(p, pfx_target_original)) {
#ifdef DEBUG_FTD_FILTER_RECENT
vnc_zlog_debug_verbose("%s: prefix covers target, allowed",
__func__);
#endif
return 0;
}
/*
* check this NVE's timestamp for this prefix
*/
trn = agg_node_get(rfd->rsp_times[afi], p); /* locks trn */
prefix_time = (time_t)trn->info;
if (agg_node_get_lock_count(trn) > 1)
agg_unlock_node(trn);
#ifdef DEBUG_FTD_FILTER_RECENT
vnc_zlog_debug_verbose("%s: last sent time %lu, last allowed time %lu",
__func__, prefix_time,
rfd->ftd_last_allowed_time);
#endif
/*
* haven't sent this prefix, which doesn't cover target address,
* to NVE since ftd_advertisement_interval, so OK to send now.
*/
if (prefix_time <= rfd->ftd_last_allowed_time)
return 0;
return 1;
}
/*
* Call when rfapi returns from rfapi_query() so the RIB reflects
* the routes sent to the NVE before the first updated response
*
* Also: remove duplicates from response. Caller should use returned
* value of nexthop chain.
*/
struct rfapi_next_hop_entry *
rfapiRibPreload(struct bgp *bgp, struct rfapi_descriptor *rfd,
struct rfapi_next_hop_entry *response, int use_eth_resolution)
{
struct rfapi_next_hop_entry *nhp;
struct rfapi_next_hop_entry *nhp_next;
struct rfapi_next_hop_entry *head = NULL;
struct rfapi_next_hop_entry *tail = NULL;
time_t new_last_sent_time;
vnc_zlog_debug_verbose("%s: loading response=%p, use_eth_resolution=%d",
__func__, response, use_eth_resolution);
new_last_sent_time = monotime(NULL);
for (nhp = response; nhp; nhp = nhp_next) {
struct prefix pfx;
struct rfapi_rib_key rk;
afi_t afi;
struct rfapi_info *ri;
int need_insert;
struct agg_node *rn;
int rib_node_started_nonempty = 0;
struct agg_node *trn;
int allowed = 0;
/* save in case we delete nhp */
nhp_next = nhp->next;
if (nhp->lifetime == RFAPI_REMOVE_RESPONSE_LIFETIME) {
/*
* weird, shouldn't happen
*/
vnc_zlog_debug_verbose(
"%s: got nhp->lifetime == RFAPI_REMOVE_RESPONSE_LIFETIME",
__func__);
continue;
}
if (use_eth_resolution) {
/* get the prefix of the ethernet address in the L2
* option */
struct rfapi_l2address_option *pL2o;
struct rfapi_vn_option *vo;
/*
* Look for VN option of type
* RFAPI_VN_OPTION_TYPE_L2ADDR
*/
for (pL2o = NULL, vo = nhp->vn_options; vo;
vo = vo->next) {
if (RFAPI_VN_OPTION_TYPE_L2ADDR == vo->type) {
pL2o = &vo->v.l2addr;
break;
}
}
if (!pL2o) {
/*
* not supposed to happen
*/
vnc_zlog_debug_verbose("%s: missing L2 info",
__func__);
continue;
}
afi = AFI_L2VPN;
rfapiL2o2Qprefix(pL2o, &pfx);
} else {
rfapiRprefix2Qprefix(&nhp->prefix, &pfx);
afi = family2afi(pfx.family);
}
/*
* TBD for ethernet, rib must know the right way to distinguish
* duplicate routes
*
* Current approach: prefix is key to radix tree; then
* each prefix has a set of routes with unique VN addrs
*/
/*
* Look up prefix in RIB
*/
rn = agg_node_get(rfd->rib[afi], &pfx); /* locks rn */
if (rn->info) {
rib_node_started_nonempty = 1;
} else {
rn->info = skiplist_new(0, rfapi_rib_key_cmp, NULL);
agg_lock_node(rn);
}
/*
* Look up route at prefix
*/
need_insert = 0;
memset((void *)&rk, 0, sizeof(rk));
assert(!rfapiRaddr2Qprefix(&nhp->vn_address, &rk.vn));
if (use_eth_resolution) {
/* copy what came from aux_prefix to rk.aux_prefix */
rfapiRprefix2Qprefix(&nhp->prefix, &rk.aux_prefix);
if (RFAPI_0_PREFIX(&rk.aux_prefix)
&& RFAPI_HOST_PREFIX(&rk.aux_prefix)) {
/* mark as "none" if nhp->prefix is 0/32 or
* 0/128 */
rk.aux_prefix.family = AF_UNSPEC;
}
}
#if DEBUG_NHL
{
char str_aux_prefix[PREFIX_STRLEN];
str_aux_prefix[0] = 0;
prefix2str(&rk.aux_prefix, str_aux_prefix,
sizeof(str_aux_prefix));
if (!rk.aux_prefix.family) {
}
vnc_zlog_debug_verbose(
"%s: rk.vn=%pFX rk.aux_prefix=%s", __func__,
&rk.vn,
(rk.aux_prefix.family ? str_aux_prefix : "-"));
}
vnc_zlog_debug_verbose(
"%s: RIB skiplist for this prefix follows", __func__);
rfapiRibShowRibSl(NULL, agg_node_get_prefix(rn),
(struct skiplist *)rn->info);
#endif
if (!skiplist_search((struct skiplist *)rn->info, &rk,
(void **)&ri)) {
/*
* Already have this route; make values match
*/
rfapiFreeRfapiUnOptionChain(ri->un_options);
ri->un_options = NULL;
rfapiFreeRfapiVnOptionChain(ri->vn_options);
ri->vn_options = NULL;
#if DEBUG_NHL
vnc_zlog_debug_verbose("%s: found in RIB", __func__);
#endif
/*
* Filter duplicate routes from initial response.
* Check timestamps to avoid wraparound problems
*/
if ((ri->rsp_counter != rfd->rsp_counter)
|| (ri->last_sent_time != new_last_sent_time)) {
#if DEBUG_NHL
vnc_zlog_debug_verbose(
"%s: allowed due to counter/timestamp diff",
__func__);
#endif
allowed = 1;
}
} else {
#if DEBUG_NHL
vnc_zlog_debug_verbose(
"%s: allowed due to not yet in RIB", __func__);
#endif
/* not found: add new route to RIB */
ri = rfapi_info_new();
need_insert = 1;
allowed = 1;
}
ri->rk = rk;
assert(!rfapiRaddr2Qprefix(&nhp->un_address, &ri->un));
ri->cost = nhp->prefix.cost;
ri->lifetime = nhp->lifetime;
ri->vn_options = rfapiVnOptionsDup(nhp->vn_options);
ri->rsp_counter = rfd->rsp_counter;
ri->last_sent_time = monotime(NULL);
if (need_insert) {
int rc;
rc = skiplist_insert((struct skiplist *)rn->info,
&ri->rk, ri);
assert(!rc);
}
if (!rib_node_started_nonempty) {
RFAPI_RIB_PREFIX_COUNT_INCR(rfd, bgp->rfapi);
}
RFAPI_RIB_CHECK_COUNTS(0, 0);
rfapiRibStartTimer(rfd, ri, rn, 0);
RFAPI_RIB_CHECK_COUNTS(0, 0);
agg_unlock_node(rn);
/*
* update this NVE's timestamp for this prefix
*/
trn = agg_node_get(rfd->rsp_times[afi], &pfx); /* locks trn */
trn->info = (void *)(uintptr_t)monotime(NULL);
if (agg_node_get_lock_count(trn) > 1)
agg_unlock_node(trn);
vnc_zlog_debug_verbose(
"%s: added pfx=%pFX nh[vn]=%pFX, cost=%u, lifetime=%u, allowed=%d",
__func__, &pfx, &rk.vn, nhp->prefix.cost, nhp->lifetime,
allowed);
if (allowed) {
if (tail)
(tail)->next = nhp;
tail = nhp;
if (!head) {
head = nhp;
}
} else {
rfapi_un_options_free(nhp->un_options);
nhp->un_options = NULL;
rfapi_vn_options_free(nhp->vn_options);
nhp->vn_options = NULL;
XFREE(MTYPE_RFAPI_NEXTHOP, nhp);
}
}
if (tail)
tail->next = NULL;
return head;
}
void rfapiRibPendingDeleteRoute(struct bgp *bgp, struct rfapi_import_table *it,
afi_t afi, struct agg_node *it_node)
{
struct rfapi_descriptor *rfd;
struct listnode *node;
const struct prefix *p = agg_node_get_prefix(it_node);
vnc_zlog_debug_verbose("%s: entry, it=%p, afi=%d, it_node=%p, pfx=%pRN",
__func__, it, afi, it_node, it_node);
if (AFI_L2VPN == afi) {
/*
* ethernet import tables are per-LNI and each ethernet monitor
* identifies the rfd that owns it.
*/
struct rfapi_monitor_eth *m;
struct agg_node *rn;
struct skiplist *sl;
void *cursor;
int rc;
/*
* route-specific monitors
*/
if ((sl = RFAPI_MONITOR_ETH(it_node))) {
vnc_zlog_debug_verbose(
"%s: route-specific skiplist: %p", __func__,
sl);
for (cursor = NULL,
rc = skiplist_next(sl, NULL, (void **)&m, &cursor);
!rc; rc = skiplist_next(sl, NULL, (void **)&m,
&cursor)) {
#if DEBUG_PENDING_DELETE_ROUTE
vnc_zlog_debug_verbose("%s: eth monitor rfd=%p",
__func__, m->rfd);
#endif
/*
* If we have already sent a route with this
* prefix to this
* NVE, it's OK to send an update with the
* delete
*/
if ((rn = agg_node_lookup(m->rfd->rib[afi],
p))) {
rfapiRibUpdatePendingNode(
bgp, m->rfd, it, it_node,
m->rfd->response_lifetime);
agg_unlock_node(rn);
}
}
}
/*
* all-routes/FTD monitors
*/
for (m = it->eth0_queries; m; m = m->next) {
#if DEBUG_PENDING_DELETE_ROUTE
vnc_zlog_debug_verbose("%s: eth0 monitor rfd=%p",
__func__, m->rfd);
#endif
/*
* If we have already sent a route with this prefix to
* this
* NVE, it's OK to send an update with the delete
*/
if ((rn = agg_node_lookup(m->rfd->rib[afi], p))) {
rfapiRibUpdatePendingNode(
bgp, m->rfd, it, it_node,
m->rfd->response_lifetime);
agg_unlock_node(rn);
}
}
} else {
/*
* Find RFDs that reference this import table
*/
for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node,
rfd)) {
struct agg_node *rn;
vnc_zlog_debug_verbose(
"%s: comparing rfd(%p)->import_table=%p to it=%p",
__func__, rfd, rfd->import_table, it);
if (rfd->import_table != it)
continue;
vnc_zlog_debug_verbose("%s: matched rfd %p", __func__,
rfd);
/*
* If we have sent a response to this NVE with this
* prefix
* previously, we should send an updated response.
*/
if ((rn = agg_node_lookup(rfd->rib[afi], p))) {
rfapiRibUpdatePendingNode(
bgp, rfd, it, it_node,
rfd->response_lifetime);
agg_unlock_node(rn);
}
}
}
}
void rfapiRibShowResponsesSummary(void *stream)
{
int (*fp)(void *, const char *, ...);
struct vty *vty;
void *out;
const char *vty_newline;
struct bgp *bgp = bgp_get_default();
int nves = 0;
int nves_with_nonempty_ribs = 0;
struct rfapi_descriptor *rfd;
struct listnode *node;
if (rfapiStream2Vty(stream, &fp, &vty, &out, &vty_newline) == 0)
return;
if (!bgp) {
fp(out, "Unable to find default BGP instance\n");
return;
}
fp(out, "%-24s ", "Responses: (Prefixes)");
fp(out, "%-8s %-8u ", "Active:", bgp->rfapi->rib_prefix_count_total);
fp(out, "%-8s %-8u",
"Maximum:", bgp->rfapi->rib_prefix_count_total_max);
fp(out, "\n");
fp(out, "%-24s ", " (Updated)");
fp(out, "%-8s %-8u ",
"Update:", bgp->rfapi->stat.count_updated_response_updates);
fp(out, "%-8s %-8u",
"Remove:", bgp->rfapi->stat.count_updated_response_deletes);
fp(out, "%-8s %-8u", "Total:",
bgp->rfapi->stat.count_updated_response_updates
+ bgp->rfapi->stat.count_updated_response_deletes);
fp(out, "\n");
fp(out, "%-24s ", " (NVEs)");
for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node, rfd)) {
++nves;
if (rfd->rib_prefix_count)
++nves_with_nonempty_ribs;
}
fp(out, "%-8s %-8u ", "Active:", nves_with_nonempty_ribs);
fp(out, "%-8s %-8u", "Total:", nves);
fp(out, "\n");
}
void rfapiRibShowResponsesSummaryClear(void)
{
struct bgp *bgp = bgp_get_default();
bgp->rfapi->rib_prefix_count_total_max =
bgp->rfapi->rib_prefix_count_total;
}
static int print_rib_sl(int (*fp)(void *, const char *, ...), struct vty *vty,
void *out, struct skiplist *sl, int deleted,
char *str_pfx, int *printedprefix)
{
struct rfapi_info *ri;
int rc;
void *cursor;
int routes_displayed = 0;
cursor = NULL;
for (rc = skiplist_next(sl, NULL, (void **)&ri, &cursor); !rc;
rc = skiplist_next(sl, NULL, (void **)&ri, &cursor)) {
char str_vn[PREFIX_STRLEN];
char str_un[PREFIX_STRLEN];
char str_lifetime[BUFSIZ];
char str_age[BUFSIZ];
char *p;
++routes_displayed;
prefix2str(&ri->rk.vn, str_vn, sizeof(str_vn));
p = index(str_vn, '/');
if (p)
*p = 0;
prefix2str(&ri->un, str_un, sizeof(str_un));
p = index(str_un, '/');
if (p)
*p = 0;
rfapiFormatSeconds(ri->lifetime, str_lifetime, BUFSIZ);
#ifdef RFAPI_REGISTRATIONS_REPORT_AGE
rfapiFormatAge(ri->last_sent_time, str_age, BUFSIZ);
#else
{
time_t now = monotime(NULL);
time_t expire =
ri->last_sent_time + (time_t)ri->lifetime;
/* allow for delayed/async removal */
rfapiFormatSeconds((expire > now ? expire - now : 1),
str_age, BUFSIZ);
}
#endif
fp(out, " %c %-20s %-15s %-15s %-4u %-8s %-8s %pRDP\n",
deleted ? 'r' : ' ', *printedprefix ? "" : str_pfx, str_vn,
str_un, ri->cost, str_lifetime, str_age, &ri->rk.rd);
if (!*printedprefix)
*printedprefix = 1;
}
return routes_displayed;
}
#if DEBUG_NHL
/*
* This one is for debugging (set stream to NULL to send output to log)
*/
static void rfapiRibShowRibSl(void *stream, const struct prefix *pfx, struct skiplist *sl)
{
int (*fp)(void *, const char *, ...);
struct vty *vty;
void *out;
const char *vty_newline;
int nhs_displayed = 0;
char str_pfx[PREFIX_STRLEN];
int printedprefix = 0;
if (rfapiStream2Vty(stream, &fp, &vty, &out, &vty_newline) == 0)
return;
prefix2str(pfx, str_pfx, sizeof(str_pfx));
nhs_displayed +=
print_rib_sl(fp, vty, out, sl, 0, str_pfx, &printedprefix);
}
#endif
void rfapiRibShowResponses(void *stream, struct prefix *pfx_match,
int show_removed)
{
int (*fp)(void *, const char *, ...);
struct vty *vty;
void *out;
const char *vty_newline;
struct rfapi_descriptor *rfd;
struct listnode *node;
struct bgp *bgp = bgp_get_default();
int printedheader = 0;
int routes_total = 0;
int nhs_total = 0;
int nves_displayed = 0;
int routes_displayed = 0;
int nhs_displayed = 0;
if (rfapiStream2Vty(stream, &fp, &vty, &out, &vty_newline) == 0)
return;
if (!bgp) {
fp(out, "Unable to find default BGP instance\n");
return;
}
/*
* loop over NVEs
*/
for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node, rfd)) {
int printednve = 0;
afi_t afi;
for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
struct agg_node *rn;
if (!rfd->rib[afi])
continue;
for (rn = agg_route_top(rfd->rib[afi]); rn;
rn = agg_route_next(rn)) {
const struct prefix *p =
agg_node_get_prefix(rn);
struct skiplist *sl;
char str_pfx[PREFIX_STRLEN];
int printedprefix = 0;
if (!show_removed)
sl = rn->info;
else
sl = rn->aggregate;
if (!sl)
continue;
routes_total++;
nhs_total += skiplist_count(sl);
if (pfx_match && !prefix_match(pfx_match, p)
&& !prefix_match(p, pfx_match))
continue;
if (!printedheader) {
++printedheader;
fp(out, "\n[%s]\n",
show_removed ? "Removed" : "Active");
fp(out, "%-15s %-15s\n", "Querying VN",
"Querying UN");
fp(out,
" %-20s %-15s %-15s %4s %-8s %-8s\n",
"Prefix", "Registered VN",
"Registered UN", "Cost", "Lifetime",
#ifdef RFAPI_REGISTRATIONS_REPORT_AGE
"Age"
#else
"Remaining"
#endif
);
}
if (!printednve) {
char str_vn[BUFSIZ];
char str_un[BUFSIZ];
++printednve;
++nves_displayed;
fp(out, "%-15s %-15s\n",
rfapiRfapiIpAddr2Str(&rfd->vn_addr,
str_vn, BUFSIZ),
rfapiRfapiIpAddr2Str(&rfd->un_addr,
str_un,
BUFSIZ));
}
prefix2str(p, str_pfx, sizeof(str_pfx));
// fp(out, " %s\n", buf); /* prefix */
routes_displayed++;
nhs_displayed += print_rib_sl(
fp, vty, out, sl, show_removed, str_pfx,
&printedprefix);
}
}
}
if (routes_total) {
fp(out, "\n");
fp(out, "Displayed %u NVEs, and %u out of %u %s prefixes",
nves_displayed, routes_displayed, routes_total,
show_removed ? "removed" : "active");
if (nhs_displayed != routes_displayed
|| nhs_total != routes_total)
fp(out, " with %u out of %u next hops", nhs_displayed,
nhs_total);
fp(out, "\n");
}
}
void rfapi_rib_init(void)
{
rrtcb_init(&_rrtcbhash);
}
void rfapi_rib_terminate(void)
{
struct rfapi_rib_tcb *tcb;
vnc_zlog_debug_verbose("%s: cleaning up %zu pending timers", __func__,
rrtcb_count(&_rrtcbhash));
/*
* Clean up memory allocations stored in pending timers
*/
while ((tcb = rrtcb_pop(&_rrtcbhash))) {
assert(tcb == EVENT_ARG(tcb->ri->timer));
event_cancel(&tcb->ri->timer);
_rfapiRibExpireTimer(tcb); /* deletes hash entry, frees tcb */
}
}
|