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 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138
|
/* Copyright (c) 2015, 2016, 2017 Nicira, Inc.
*
* 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.
*/
#include <config.h>
#include "bitmap.h"
#include "byte-order.h"
#include "coverage.h"
#include "dirs.h"
#include "dp-packet.h"
#include "flow.h"
#include "hash.h"
#include "hindex.h"
#include "lflow.h"
#include "ofctrl.h"
#include "openflow/openflow.h"
#include "openvswitch/dynamic-string.h"
#include "openvswitch/hmap.h"
#include "openvswitch/list.h"
#include "openvswitch/match.h"
#include "openvswitch/ofp-actions.h"
#include "openvswitch/ofp-bundle.h"
#include "openvswitch/ofp-ct.h"
#include "openvswitch/ofp-flow.h"
#include "openvswitch/ofp-group.h"
#include "openvswitch/ofp-match.h"
#include "openvswitch/ofp-msgs.h"
#include "openvswitch/ofp-meter.h"
#include "openvswitch/ofp-packet.h"
#include "openvswitch/ofp-print.h"
#include "openvswitch/ofp-util.h"
#include "openvswitch/ofpbuf.h"
#include "openvswitch/vlog.h"
#include "ovn/actions.h"
#include "lib/extend-table.h"
#include "lib/lb.h"
#include "openvswitch/poll-loop.h"
#include "physical.h"
#include "openvswitch/rconn.h"
#include "socket-util.h"
#include "timeval.h"
#include "util.h"
#include "vswitch-idl.h"
#include "ovn-sb-idl.h"
#include "ct-zone.h"
#include "ecmp-next-hop-monitor.h"
#include "acl-ids.h"
VLOG_DEFINE_THIS_MODULE(ofctrl);
COVERAGE_DEFINE(ofctrl_msg_too_long);
/* An OpenFlow flow. */
struct ovn_flow {
/* Key. */
uint8_t table_id;
uint16_t priority;
struct minimatch match;
/* Hash. */
uint32_t hash;
/* Data. */
struct ofpact *ofpacts;
size_t ofpacts_len;
uint64_t cookie;
uint32_t ctrl_meter_id; /* Meter to be used for controller actions. */
};
/* A desired flow, in struct ovn_desired_flow_table, calculated by the
* incremental processing engine.
* - They are added/removed incrementally when I-P engine is able to process
* the changes incrementally, or
* - Completely cleared and recomputed by I-P engine when recompute happens.
*
* Links are maintained between desired flows and SB data. The relationship
* is M to N. The struct sb_flow_ref is used to link a pair of desired flow
* and SB UUID. The below diagram depicts the data structure.
*
* SB UUIDs
* +-----+-----+-----+-----+-----+-----+-----+
* | | | | | | | |
* +--+--+--+--+--+--+-----+--+--+--+--+--+--+
* | | | | | |
* Desired Flows | | | | | |
* +----+ +-+-+ | +-+-+ | +-+-+ |
* | +-------+ +-------+ +-------------+ | |
* +----+ +---+ | +-+-+ | +---+ |
* | | | | | |
* +----+ | | +-+-+ |
* | +-------------------------------+ | |
* +----+ +---+ | +---+ |
* | +-------------+ | | |
* +----+ +---+ | |
* | | | |
* +----+ +-+-+ +-+-+
* | +-------------------+ +-------------------+ |
* +----+ +---+ +---+
* | |
* +----+
*
* The links are updated whenever there is a change in desired flows, which is
* usually triggered by a SB data change in I-P engine.
*
* ** Tracking **
*
* A desired flow can be tracked - listed in ovn_desired_flow_table's
* tracked_flows.
*
* Tracked flows is initially empty, and stays empty after the first run of I-P
* engine when installed flows are initially populated. After that, flow
* changes are tracked when I-P engine incrementally computes flow changes.
* Tracked flows are then processed and removed completely in ofctrl_put.
* ("processed" means OpenFlow change messages are composed and sent/queued to
* OVS, which ensures flows in OVS is always in sync (eventually) with the
* installed flows table).
*
* In case of full recompute of I-P engine, tracked flows are not
* added/removed, and ofctrl_put will not rely on tracked flows. (It is I-P
* engine's responsibility to ensure the tracked flows are cleared before
* recompute).
*
* Tracked flows can be preserved across multiple I-P engine runs - if in some
* iterations ofctrl_put() is skipped. Tracked flows are cleared only when it
* is consumed or when flow recompute happens.
*
* The "change_tracked" member of desired flow table maintains the status of
* whether flow changes are tracked or not. It is always set to true when
* ofctrl_put is completed, and transition to false whenever
* ovn_desired_flow_table_clear is called.
*
* NOTE: A tracked flow is just a reference to a desired flow, instead of a new
* copy. When a desired flow is removed and tracked, it is removed from the
* match_flow_table and uuid_flow_table indexes, and added to the tracked_flows
* list, marking is_deleted = true, but not immediately destroyed. It is
* destroyed when the tracking is processed for installed flow updates.
*/
struct desired_flow {
struct ovn_flow flow;
struct hmap_node match_hmap_node; /* For match based hashing. */
struct ovs_list list_node; /* For handling lists of flows. */
/* A list of struct sb_flow_ref nodes, which references this flow. (There
* are cases that multiple SB entities share the same desired OpenFlow
* flow, e.g. when conjunction is used.) */
struct ovs_list references;
/* The corresponding flow in installed table. */
struct installed_flow *installed_flow;
/* Node in installed_flow.desired_refs list. */
struct ovs_list installed_ref_list_node;
/* For tracking. */
struct ovs_list track_list_node; /* node in ovn_desired_flow_table's
* tracked_flows list. */
bool is_deleted; /* If the tracked flow is deleted. */
};
struct sb_to_flow {
struct hmap_node hmap_node; /* Node in
ovn_desired_flow_table.uuid_flow_table. */
struct uuid sb_uuid;
struct ovs_list flows; /* A list of struct sb_flow_ref nodes that
are referenced by the sb_uuid. */
struct ovs_list addrsets; /* A list of struct sb_addrset_ref. */
};
struct sb_flow_ref {
struct ovs_list sb_list; /* Node in desired_flow.references. */
struct ovs_list flow_list; /* Node in sb_to_flow.flows. */
struct ovs_list as_ip_flow_list; /* Node in as_ip_to_flow_node.flows. */
struct desired_flow *flow;
struct uuid sb_uuid;
};
struct sb_addrset_ref {
struct ovs_list list_node; /* List node in sb_to_flow.addrsets. */
char *name; /* Name of the address set. */
struct hmap as_ip_to_flow_map; /* map from IPs in the address set to flows.
Each node is as_ip_to_flow_node. */
};
struct as_ip_to_flow_node {
struct hmap_node hmap_node; /* Node in sb_addrset_ref.as_ip_to_flow_map. */
struct in6_addr as_ip;
struct in6_addr as_mask;
/* A list of struct sb_flow_ref. A single IP in an address set can be
* used by multiple flows. e.g., in match:
* ip.src == $as1 && ip.dst == $as1. */
struct ovs_list flows;
};
/* An installed flow, in static variable installed_lflows/installed_pflows.
*
* Installed flows are updated in ofctrl_put for maintaining the flow
* installation to OVS. They are updated according to desired flows: either by
* processing the tracked desired flow changes, or by comparing desired flows
* with currently installed flows when tracked desired flows changes are not
* available.
*
* In addition, when ofctrl state machine enters S_CLEAR, the installed flows
* will be cleared. (This happens in initialization phase and also when
* ovs-vswitchd is disconnected/reconnected).
*
* Links are maintained between installed flows and desired flows. The
* relationship is 1 to N. A link is added when a flow addition is processed.
* A link is removed when a flow deletion is processed, the desired flow
* table is cleared, or the installed flow table is cleared.
*
* To ensure predictable behavior, the list of desired flows is maintained
* partially sorted in the following way (from least restrictive to most
* restrictive wrt. match):
* - allow flows without action conjunction.
* - drop flows without action conjunction.
* - a single flow with action conjunction.
*
* The first desired_flow in the list is the active one, the one that is
* actually installed.
*/
struct installed_flow {
struct ovn_flow flow;
struct hmap_node match_hmap_node; /* For match based hashing. */
/* A list of desired ovn_flow nodes (linked by
* desired_flow.installed_ref_list_node), which reference this installed
* flow. (There are cases that multiple desired flows reference the same
* installed flow, e.g. when there are conflict/duplicated ACLs that
* generates same match conditions). */
struct ovs_list desired_refs;
};
/* Global ofctrl memory usage specific statistics, all in bytes. */
struct ofctrl_mem_stats {
uint64_t sb_flow_ref_usage;
uint64_t desired_flow_usage;
uint64_t installed_flow_usage;
uint64_t oflow_update_usage;
};
static struct ofctrl_mem_stats mem_stats;
typedef bool
(*desired_flow_match_cb)(const struct desired_flow *candidate,
const void *arg);
static struct desired_flow *desired_flow_alloc(
uint8_t table_id,
uint16_t priority,
uint64_t cookie,
const struct match *match,
const struct ofpbuf *actions,
uint32_t meter_id);
static size_t desired_flow_size(const struct desired_flow *);
static struct desired_flow *desired_flow_lookup(
struct ovn_desired_flow_table *,
const struct ovn_flow *target);
static struct desired_flow *desired_flow_lookup_check_uuid(
struct ovn_desired_flow_table *,
const struct ovn_flow *target,
const struct uuid *);
static struct desired_flow *desired_flow_lookup_conjunctive(
struct ovn_desired_flow_table *,
const struct ovn_flow *target);
static void desired_flow_destroy(struct desired_flow *);
static struct installed_flow *installed_flow_lookup(
const struct ovn_flow *target, struct hmap *installed_flows);
static void installed_flow_destroy(struct installed_flow *);
static struct installed_flow *installed_flow_dup(struct desired_flow *);
static size_t installed_flow_size(const struct installed_flow *);
static struct desired_flow *installed_flow_get_active(struct installed_flow *);
static uint32_t ovn_flow_match_hash(const struct ovn_flow *);
static char *ovn_flow_to_string(const struct ovn_flow *);
static void ovn_flow_log(const struct ovn_flow *, const char *action);
static void remove_flows_from_sb_to_flow(struct ovn_desired_flow_table *,
struct sb_to_flow *,
const char *log_msg,
struct uuidset *flood_remove_nodes);
/* OpenFlow connection to the switch. */
static struct rconn *swconn;
/* Symbol table for OVN expressions. */
static struct shash symtab;
/* Last seen sequence number for 'swconn'. When this differs from
* rconn_get_connection_seqno(rconn), 'swconn' has reconnected. */
static unsigned int seqno;
/* Connection state machine. */
#define STATES \
STATE(S_NEW) \
STATE(S_TLV_TABLE_REQUESTED) \
STATE(S_TLV_TABLE_MOD_SENT) \
STATE(S_WAIT_BEFORE_CLEAR) \
STATE(S_CLEAR_FLOWS) \
STATE(S_UPDATE_FLOWS)
enum ofctrl_state {
#define STATE(NAME) NAME,
STATES
#undef STATE
};
/* An in-flight update to the switch's flow table.
*
* When we receive a barrier reply from the switch with the given 'xid', we
* know that the switch is caught up to the requested sequence number
* 'req_cfg' (and make that available to the client via ofctrl_get_cur_cfg(),
* so that it can store it into external state, e.g., our Chassis record's
* nb_cfg column). */
struct ofctrl_flow_update {
struct ovs_list list_node; /* In 'flow_updates'. */
ovs_be32 xid; /* OpenFlow transaction ID for barrier. */
uint64_t req_cfg; /* Requested sequence number. */
};
static struct ofctrl_flow_update *
ofctrl_flow_update_from_list_node(const struct ovs_list *list_node)
{
return CONTAINER_OF(list_node, struct ofctrl_flow_update, list_node);
}
static size_t
ofctrl_flow_update_size(const struct ofctrl_flow_update *fup)
{
return sizeof *fup;
}
/* Currently in-flight updates. */
static struct ovs_list flow_updates;
/* req_cfg of latest committed flow update. */
static uint64_t cur_cfg;
/* Current state. */
static enum ofctrl_state state;
/* Release wait before clear stage. */
static bool wait_before_clear_proceed = false;
/* Transaction IDs for messages in flight to the switch. */
static ovs_be32 xid, xid2;
/* Counter for in-flight OpenFlow messages on 'swconn'. We only send a new
* round of flow table modifications to the switch when the counter falls to
* zero, to avoid unbounded buffering. */
static struct rconn_packet_counter *tx_counter;
/* Flow table of "struct ovn_flow"s, that holds the logical flow table
* currently installed in the switch. */
static struct hmap installed_lflows;
/* Flow table of "struct ovn_flow"s, that holds the physical flow table
* currently installed in the switch. */
static struct hmap installed_pflows;
/* A reference to the group_table. */
static struct ovn_extend_table *groups;
/* A reference to the meter_table. */
static struct ovn_extend_table *meters;
/* Installed meter bands. */
struct meter_band_data {
int64_t burst_size;
int64_t rate;
};
struct meter_band_entry {
struct meter_band_data *bands;
size_t n_bands;
};
static struct shash meter_bands;
static void ofctrl_meter_bands_destroy(void);
static void ofctrl_meter_bands_clear(void);
/* MFF_* field ID for our Geneve option. In S_TLV_TABLE_MOD_SENT, this is
* the option we requested (we don't know whether we obtained it yet). In
* S_CLEAR_FLOWS or S_UPDATE_FLOWS, this is really the option we have. */
static enum mf_field_id mff_ovn_geneve;
/* Indicates if we just went through the S_CLEAR_FLOWS state, which means we
* need to perform a one time deletion for all the existing flows, groups and
* meters. This can happen during initialization or OpenFlow reconnection
* (e.g. after OVS restart). */
static bool ofctrl_initial_clear;
static ovs_be32 queue_msg(struct ofpbuf *);
static struct ofpbuf *encode_flow_mod(struct ofputil_flow_mod *);
static struct ofpbuf *encode_group_mod(const struct ofputil_group_mod *);
static struct ofpbuf *encode_meter_mod(const struct ofputil_meter_mod *);
static void ovn_installed_flow_table_clear(void);
static void ovn_installed_flow_table_destroy(void);
static void ofctrl_recv(const struct ofp_header *, enum ofptype);
void
ofctrl_init(struct ovn_extend_table *group_table,
struct ovn_extend_table *meter_table)
{
swconn = rconn_create(0, 0, DSCP_DEFAULT, 1 << OFP15_VERSION);
tx_counter = rconn_packet_counter_create();
hmap_init(&installed_lflows);
hmap_init(&installed_pflows);
ecmp_nexthop_init();
ovs_list_init(&flow_updates);
ovn_init_symtab(&symtab);
groups = group_table;
meters = meter_table;
shash_init(&meter_bands);
}
/* S_NEW, for a new connection.
*
* Sends NXT_TLV_TABLE_REQUEST and transitions to
* S_TLV_TABLE_REQUESTED. */
static void
run_S_NEW(void)
{
struct ofpbuf *buf = ofpraw_alloc(OFPRAW_NXT_TLV_TABLE_REQUEST,
rconn_get_version(swconn), 0);
xid = queue_msg(buf);
wait_before_clear_proceed = false;
state = S_TLV_TABLE_REQUESTED;
}
static void
recv_S_NEW(const struct ofp_header *oh OVS_UNUSED,
enum ofptype type OVS_UNUSED,
struct shash *pending_ct_zones OVS_UNUSED,
struct tracked_acl_ids *tracked_acl_ids OVS_UNUSED)
{
OVS_NOT_REACHED();
}
/* S_TLV_TABLE_REQUESTED, when NXT_TLV_TABLE_REQUEST has been sent
* and we're waiting for a reply.
*
* If we receive an NXT_TLV_TABLE_REPLY:
*
* - If it contains our tunnel metadata option, assign its field ID to
* mff_ovn_geneve and transition to S_WAIT_BEFORE_CLEAR.
*
* - Otherwise, if there is an unused tunnel metadata field ID, send
* NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST, and transition to
* S_TLV_TABLE_MOD_SENT.
*
* - Otherwise, log an error, disable Geneve, and transition to
* S_WAIT_BEFORE_CLEAR.
*
* If we receive an OFPT_ERROR:
*
* - Log an error, disable Geneve, and transition to S_WAIT_BEFORE_CLEAR.
*/
static void
run_S_TLV_TABLE_REQUESTED(void)
{
}
static bool
process_tlv_table_reply(const struct ofputil_tlv_table_reply *reply)
{
const struct ofputil_tlv_map *map;
uint64_t md_free = UINT64_MAX;
BUILD_ASSERT(TUN_METADATA_NUM_OPTS == 64);
LIST_FOR_EACH (map, list_node, &reply->mappings) {
if (map->option_class == OVN_GENEVE_CLASS
&& map->option_type == OVN_GENEVE_TYPE
&& map->option_len == OVN_GENEVE_LEN) {
if (map->index >= TUN_METADATA_NUM_OPTS) {
VLOG_ERR("desired Geneve tunnel option 0x%"PRIx16","
"%"PRIu8",%"PRIu8" already in use with "
"unsupported index %"PRIu16,
map->option_class, map->option_type,
map->option_len, map->index);
return false;
} else {
mff_ovn_geneve = MFF_TUN_METADATA0 + map->index;
state = S_WAIT_BEFORE_CLEAR;
return true;
}
}
if (map->index < TUN_METADATA_NUM_OPTS) {
md_free &= ~(UINT64_C(1) << map->index);
}
}
VLOG_DBG("OVN Geneve option not found");
if (!md_free) {
VLOG_ERR("no Geneve options free for use by OVN");
return false;
}
unsigned int index = rightmost_1bit_idx(md_free);
mff_ovn_geneve = MFF_TUN_METADATA0 + index;
struct ofputil_tlv_map tm;
tm.option_class = OVN_GENEVE_CLASS;
tm.option_type = OVN_GENEVE_TYPE;
tm.option_len = OVN_GENEVE_LEN;
tm.index = index;
struct ofputil_tlv_table_mod ttm;
ttm.command = NXTTMC_ADD;
ovs_list_init(&ttm.mappings);
ovs_list_push_back(&ttm.mappings, &tm.list_node);
xid = queue_msg(ofputil_encode_tlv_table_mod(OFP15_VERSION, &ttm));
xid2 = queue_msg(ofputil_encode_barrier_request(OFP15_VERSION));
state = S_TLV_TABLE_MOD_SENT;
return true;
}
static void
recv_S_TLV_TABLE_REQUESTED(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones OVS_UNUSED,
struct tracked_acl_ids *tracked_acl_ids OVS_UNUSED)
{
if (oh->xid != xid) {
ofctrl_recv(oh, type);
return;
} else if (type == OFPTYPE_NXT_TLV_TABLE_REPLY) {
struct ofputil_tlv_table_reply reply;
enum ofperr error = ofputil_decode_tlv_table_reply(oh, &reply);
if (!error) {
bool ok = process_tlv_table_reply(&reply);
ofputil_uninit_tlv_table(&reply.mappings);
if (ok) {
return;
}
} else {
VLOG_ERR("failed to decode TLV table request (%s)",
ofperr_to_string(error));
}
} else if (type == OFPTYPE_ERROR) {
VLOG_ERR("switch refused to allocate Geneve option (%s)",
ofperr_to_string(ofperr_decode_msg(oh, NULL)));
} else {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 1);
VLOG_ERR("unexpected reply to TLV table request (%s)", s);
free(s);
}
/* Error path. */
mff_ovn_geneve = 0;
state = S_WAIT_BEFORE_CLEAR;
}
/* S_TLV_TABLE_MOD_SENT, when NXT_TLV_TABLE_MOD and OFPT_BARRIER_REQUEST
* have been sent and we're waiting for a reply to one or the other.
*
* If we receive an OFPT_ERROR:
*
* - If the error is NXTTMFC_ALREADY_MAPPED or NXTTMFC_DUP_ENTRY, we
* raced with some other controller. Transition to S_NEW.
*
* - Otherwise, log an error, disable Geneve, and transition to
* S_WAIT_BEFORE_CLEAR.
*
* If we receive OFPT_BARRIER_REPLY:
*
* - Set the tunnel metadata field ID to the one that we requested.
* Transition to S_WAIT_BEFORE_CLEAR.
*/
static void
run_S_TLV_TABLE_MOD_SENT(void)
{
}
static void
recv_S_TLV_TABLE_MOD_SENT(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones OVS_UNUSED,
struct tracked_acl_ids *tracked_acl_ids OVS_UNUSED)
{
if (oh->xid != xid && oh->xid != xid2) {
ofctrl_recv(oh, type);
} else if (oh->xid == xid2 && type == OFPTYPE_BARRIER_REPLY) {
state = S_WAIT_BEFORE_CLEAR;
} else if (oh->xid == xid && type == OFPTYPE_ERROR) {
enum ofperr error = ofperr_decode_msg(oh, NULL);
if (error == OFPERR_NXTTMFC_ALREADY_MAPPED ||
error == OFPERR_NXTTMFC_DUP_ENTRY) {
VLOG_INFO("raced with another controller adding "
"Geneve option (%s); trying again",
ofperr_to_string(error));
state = S_NEW;
} else {
VLOG_ERR("error adding Geneve option (%s)",
ofperr_to_string(error));
goto error;
}
} else {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 1);
VLOG_ERR("unexpected reply to Geneve option allocation request (%s)",
s);
free(s);
goto error;
}
return;
error:
state = S_WAIT_BEFORE_CLEAR;
}
/* S_WAIT_BEFORE_CLEAR, we are almost ready to set up flows, but just wait for
* a while until the initial flow compute to complete before we clear the
* existing flows in OVS, so that we won't end up with an empty flow table,
* which may cause data plane down time. */
static void
run_S_WAIT_BEFORE_CLEAR(void)
{
if (wait_before_clear_proceed) {
state = S_CLEAR_FLOWS;
}
}
static void
recv_S_WAIT_BEFORE_CLEAR(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones OVS_UNUSED,
struct tracked_acl_ids *tracked_acl_ids OVS_UNUSED)
{
ofctrl_recv(oh, type);
}
/* S_CLEAR_FLOWS, after we've established a Geneve metadata field ID and it's
* time to set up some flows.
*
* Sends an OFPT_TABLE_MOD to clear all flows, then transitions to
* S_UPDATE_FLOWS. */
static void
run_S_CLEAR_FLOWS(void)
{
VLOG_DBG("clearing all flows");
/* Set the flag so that the ofctrl_run() can clear the existing flows,
* groups and meters. We clear them in ofctrl_run() right before the new
* ones are installed to avoid data plane downtime. */
ofctrl_initial_clear = true;
/* Clear installed_flows, to match the state of the switch. */
ovn_installed_flow_table_clear();
/* Clear existing groups, to match the state of the switch. */
if (groups) {
ovn_extend_table_clear(groups, true);
}
/* Clear existing meters, to match the state of the switch. */
if (meters) {
ovn_extend_table_clear(meters, true);
ofctrl_meter_bands_clear();
}
/* All flow updates are irrelevant now. */
struct ofctrl_flow_update *fup;
LIST_FOR_EACH_SAFE (fup, list_node, &flow_updates) {
mem_stats.oflow_update_usage -= ofctrl_flow_update_size(fup);
ovs_list_remove(&fup->list_node);
free(fup);
}
state = S_UPDATE_FLOWS;
/* Give a chance for the main loop to call ofctrl_put() in case there were
* pending flows waiting ofctrl state change to S_UPDATE_FLOWS. */
poll_immediate_wake();
}
static void
recv_S_CLEAR_FLOWS(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones OVS_UNUSED,
struct tracked_acl_ids *tracked_acl_ids OVS_UNUSED)
{
ofctrl_recv(oh, type);
}
/* S_UPDATE_FLOWS, for maintaining the flow table over time.
*
* Compare the installed flows to the ones we want. Send OFPT_FLOW_MOD as
* necessary.
*
* This is a terminal state. We only transition out of it if the connection
* drops. */
static void
run_S_UPDATE_FLOWS(void)
{
/* Nothing to do here.
*
* Being in this state enables ofctrl_put() to work, however. */
}
static void
flow_updates_handle_barrier_reply(const struct ofp_header *oh,
struct shash *pending_ct_zones)
{
if (ovs_list_is_empty(&flow_updates)) {
return;
}
struct ofctrl_flow_update *fup = ofctrl_flow_update_from_list_node(
ovs_list_front(&flow_updates));
if (fup->xid == oh->xid) {
if (fup->req_cfg >= cur_cfg) {
cur_cfg = fup->req_cfg;
}
mem_stats.oflow_update_usage -= ofctrl_flow_update_size(fup);
ovs_list_remove(&fup->list_node);
free(fup);
}
/* If the barrier xid is associated with an outstanding conntrack
* flush, the flush succeeded. Move the pending ct zone entry
* to the next stage. */
struct shash_node *iter;
SHASH_FOR_EACH (iter, pending_ct_zones) {
struct ct_zone_pending_entry *ctzpe = iter->data;
if (ctzpe->state == CT_ZONE_OF_SENT && ctzpe->of_xid == oh->xid) {
ctzpe->state = CT_ZONE_DB_QUEUED;
}
}
}
static void
recv_S_UPDATE_FLOWS(const struct ofp_header *oh, enum ofptype type,
struct shash *pending_ct_zones,
struct tracked_acl_ids *tracked_acl_ids)
{
if (type == OFPTYPE_BARRIER_REPLY) {
flow_updates_handle_barrier_reply(oh, pending_ct_zones);
acl_ids_handle_barrier_reply(tracked_acl_ids, oh->xid);
} else if (!acl_ids_handle_non_barrier_reply(oh, type, tracked_acl_ids)) {
ofctrl_recv(oh, type);
}
}
enum mf_field_id
ofctrl_get_mf_field_id(void)
{
if (!rconn_is_connected(swconn)) {
return 0;
}
return (state == S_WAIT_BEFORE_CLEAR
|| state == S_CLEAR_FLOWS
|| state == S_UPDATE_FLOWS
? mff_ovn_geneve : 0);
}
/* Runs the OpenFlow state machine against 'br_int', which is local to the
* hypervisor on which we are running. Attempts to negotiate a Geneve option
* field for class OVN_GENEVE_CLASS, type OVN_GENEVE_TYPE.
*
* Returns 'true' if an OpenFlow reconnect happened; 'false' otherwise.
*/
bool
ofctrl_run(const char *conn_target, int probe_interval,
const struct ovsrec_open_vswitch_table *ovs_table,
struct shash *pending_ct_zones,
struct tracked_acl_ids *tracked_acl_ids)
{
bool reconnected = false;
ovn_update_swconn_at(swconn, conn_target, probe_interval, "ofctrl");
rconn_run(swconn);
if (!rconn_is_connected(swconn) || !pending_ct_zones) {
return reconnected;
}
if (seqno != rconn_get_connection_seqno(swconn)) {
seqno = rconn_get_connection_seqno(swconn);
reconnected = true;
state = S_NEW;
/* Reset the state of any outstanding ct flushes to resend them. */
struct shash_node *iter;
SHASH_FOR_EACH(iter, pending_ct_zones) {
struct ct_zone_pending_entry *ctzpe = iter->data;
if (ctzpe->state == CT_ZONE_OF_SENT) {
ctzpe->state = CT_ZONE_OF_QUEUED;
}
}
}
const struct ovsrec_open_vswitch *cfg =
ovsrec_open_vswitch_table_first(ovs_table);
ovs_assert(cfg);
bool progress = true;
for (int i = 0; progress && i < 50; i++) {
/* Allow the state machine to run. */
enum ofctrl_state old_state = state;
switch (state) {
#define STATE(NAME) case NAME: run_##NAME(); break;
STATES
#undef STATE
default:
OVS_NOT_REACHED();
}
/* Try to process a received packet. */
struct ofpbuf *msg = rconn_recv(swconn);
if (msg) {
const struct ofp_header *oh = msg->data;
enum ofptype type;
enum ofperr error;
error = ofptype_decode(&type, oh);
if (!error) {
switch (state) {
#define STATE(NAME) case NAME: recv_##NAME(oh, type, pending_ct_zones, \
tracked_acl_ids); break;
STATES
#undef STATE
default:
OVS_NOT_REACHED();
}
} else {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 1);
VLOG_WARN("could not decode OpenFlow message (%s): %s",
ofperr_to_string(error), s);
free(s);
}
ofpbuf_delete(msg);
}
/* If we did some work, plan to go around again. */
progress = old_state != state || msg;
}
if (progress) {
/* We bailed out to limit the amount of work we do in one go, to allow
* other code a chance to run. We were still making progress at that
* point, so ensure that we come back again without waiting. */
poll_immediate_wake();
}
return reconnected;
}
void
ofctrl_wait(void)
{
rconn_run_wait(swconn);
rconn_recv_wait(swconn);
}
void
ofctrl_destroy(void)
{
rconn_destroy(swconn);
ovn_installed_flow_table_destroy();
rconn_packet_counter_destroy(tx_counter);
expr_symtab_destroy(&symtab);
shash_destroy(&symtab);
ofctrl_meter_bands_destroy();
ecmp_nexthop_destroy();
}
uint64_t
ofctrl_get_cur_cfg(void)
{
return cur_cfg;
}
static ovs_be32
queue_msg(struct ofpbuf *msg)
{
const struct ofp_header *oh = msg->data;
ovs_be32 xid_ = oh->xid;
rconn_send(swconn, msg, tx_counter);
return xid_;
}
static void
log_openflow_rl(struct vlog_rate_limit *rl, enum vlog_level level,
const struct ofp_header *oh, const char *title)
{
if (!vlog_should_drop(&this_module, level, rl)) {
char *s = ofp_to_string(oh, ntohs(oh->length), NULL, NULL, 2);
vlog(&this_module, level, "%s: %s", title, s);
free(s);
}
}
static void
ofctrl_recv(const struct ofp_header *oh, enum ofptype type)
{
if (type == OFPTYPE_ECHO_REQUEST) {
queue_msg(ofputil_encode_echo_reply(oh));
} else if (type == OFPTYPE_ERROR) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
log_openflow_rl(&rl, VLL_INFO, oh, "OpenFlow error");
rconn_reconnect(swconn);
} else {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
log_openflow_rl(&rl, VLL_DBG, oh, "OpenFlow packet ignored");
}
}
static bool
flow_action_has_drop(const struct ovn_flow *f)
{
return f->ofpacts_len == 0;
}
static bool
flow_action_has_conj(const struct ovn_flow *f)
{
const struct ofpact *a = NULL;
OFPACT_FOR_EACH (a, f->ofpacts, f->ofpacts_len) {
if (a->type == OFPACT_CONJUNCTION) {
return true;
}
}
return false;
}
static bool
flow_action_has_allow(const struct ovn_flow *f)
{
return !flow_action_has_drop(f) && !flow_action_has_conj(f);
}
/* Returns true if flow 'a' is preferred over flow 'b'. */
static bool
flow_is_preferred(const struct ovn_flow *a, const struct ovn_flow *b)
{
if (flow_action_has_allow(b)) {
return false;
}
if (flow_action_has_allow(a)) {
return true;
}
if (flow_action_has_drop(b)) {
return false;
}
if (flow_action_has_drop(a)) {
return true;
}
/* Flows 'a' and 'b' should never both have action conjunction. */
OVS_NOT_REACHED();
}
/* Adds the desired flow to the list of desired flows that have same match
* conditions as the installed flow.
*
* It is caller's responsibility to make sure the link between the pair didn't
* exist before.
*
* Returns true if the newly added desired flow is selected to be the active
* one.
*/
static bool
link_installed_to_desired(struct installed_flow *i, struct desired_flow *d)
{
struct desired_flow *f;
/* Find first 'f' such that 'd' is preferred over 'f'. If no such desired
* flow exists then 'f' will point after the last element of the list.
*/
LIST_FOR_EACH (f, installed_ref_list_node, &i->desired_refs) {
if (flow_is_preferred(&d->flow, &f->flow)) {
break;
}
}
if (!f) {
ovs_list_insert(&i->desired_refs, &d->installed_ref_list_node);
} else {
ovs_list_insert(&f->installed_ref_list_node,
&d->installed_ref_list_node);
}
d->installed_flow = i;
return installed_flow_get_active(i) == d;
}
/* Replaces 'old_desired' with 'new_desired' in the list of desired flows
* that have same match conditions as the installed flow.
*/
static void
replace_installed_to_desired(struct installed_flow *i,
struct desired_flow *old_desired,
struct desired_flow *new_desired)
{
ovs_assert(old_desired->installed_flow == i);
ovs_list_replace(&new_desired->installed_ref_list_node,
&old_desired->installed_ref_list_node);
old_desired->installed_flow = NULL;
new_desired->installed_flow = i;
}
/* Removes the desired flow from the list of desired flows that have the same
* match conditions as the installed flow.
*
* Returns true if the desired flow was the previously active flow.
*/
static bool
unlink_installed_to_desired(struct installed_flow *i, struct desired_flow *d)
{
struct desired_flow *old_active = installed_flow_get_active(i);
ovs_assert(d && d->installed_flow == i);
ovs_list_remove(&d->installed_ref_list_node);
d->installed_flow = NULL;
return old_active == d;
}
static void
unlink_all_refs_for_installed_flow(struct installed_flow *i)
{
struct desired_flow *d;
LIST_FOR_EACH_SAFE (d, installed_ref_list_node, &i->desired_refs) {
unlink_installed_to_desired(i, d);
}
}
static void
track_flow_add_or_modify(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f)
{
if (!flow_table->change_tracked) {
return;
}
/* If same node (flow adding/modifying) was tracked, remove it from
* tracking first. */
if (!ovs_list_is_empty(&f->track_list_node)) {
ovs_list_remove(&f->track_list_node);
}
f->is_deleted = false;
ovs_list_push_back(&flow_table->tracked_flows, &f->track_list_node);
}
static void
track_flow_del(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f)
{
if (!flow_table->change_tracked) {
return;
}
/* If same node (flow adding/modifying) was tracked, remove it from
* tracking first. */
if (!ovs_list_is_empty(&f->track_list_node)) {
ovs_list_remove(&f->track_list_node);
if (!f->installed_flow) {
/* If it is not installed yet, simply destroy it. */
desired_flow_destroy(f);
return;
}
}
f->is_deleted = true;
ovs_list_push_back(&flow_table->tracked_flows, &f->track_list_node);
}
/* When a desired flow is being removed, depending on "change_tracked", this
* function either unlinks a desired flow from installed flow and destroy it,
* or do nothing but track it. */
static void
track_or_destroy_for_flow_del(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f)
{
if (flow_table->change_tracked) {
track_flow_del(flow_table, f);
} else {
if (f->installed_flow) {
unlink_installed_to_desired(f->installed_flow, f);
}
desired_flow_destroy(f);
}
}
static size_t
sb_flow_ref_size(const struct sb_flow_ref *sfr)
{
return sizeof *sfr;
}
static size_t
sb_to_flow_size(const struct sb_to_flow *stf)
{
return sizeof *stf;
}
static size_t
sb_addrset_ref_size(const struct sb_addrset_ref *sar)
{
return sizeof *sar + strlen(sar->name) + 1;
}
static struct sb_to_flow *
sb_to_flow_find(struct hmap *uuid_flow_table, const struct uuid *sb_uuid)
{
struct sb_to_flow *stf;
HMAP_FOR_EACH_WITH_HASH (stf, hmap_node, uuid_hash(sb_uuid),
uuid_flow_table) {
if (uuid_equals(sb_uuid, &stf->sb_uuid)) {
return stf;
}
}
return NULL;
}
static struct as_ip_to_flow_node *
as_ip_to_flow_find(struct hmap *as_ip_to_flow_map,
const struct in6_addr *as_ip,
const struct in6_addr *as_mask)
{
uint32_t hash = hash_bytes(as_ip, sizeof *as_ip, 0);
struct as_ip_to_flow_node *itfn;
HMAP_FOR_EACH_WITH_HASH (itfn, hmap_node, hash, as_ip_to_flow_map) {
if (ipv6_addr_equals(&itfn->as_ip, as_ip)
&& ipv6_addr_equals(&itfn->as_mask, as_mask)) {
return itfn;
}
}
return NULL;
}
static void
link_flow_to_sb(struct ovn_desired_flow_table *flow_table,
struct desired_flow *f, const struct uuid *sb_uuid,
const struct addrset_info *as_info)
{
struct sb_flow_ref *sfr = xmalloc(sizeof *sfr);
mem_stats.sb_flow_ref_usage += sb_flow_ref_size(sfr);
sfr->flow = f;
sfr->sb_uuid = *sb_uuid;
ovs_list_insert(&f->references, &sfr->sb_list);
struct sb_to_flow *stf = sb_to_flow_find(&flow_table->uuid_flow_table,
sb_uuid);
if (!stf) {
stf = xmalloc(sizeof *stf);
mem_stats.sb_flow_ref_usage += sb_to_flow_size(stf);
stf->sb_uuid = *sb_uuid;
ovs_list_init(&stf->flows);
ovs_list_init(&stf->addrsets);
hmap_insert(&flow_table->uuid_flow_table, &stf->hmap_node,
uuid_hash(sb_uuid));
}
ovs_list_insert(&stf->flows, &sfr->flow_list);
if (!as_info) {
ovs_list_init(&sfr->as_ip_flow_list);
return;
}
/* link flow to address_set + ip */
struct sb_addrset_ref *sar;
bool found = false;
LIST_FOR_EACH (sar, list_node, &stf->addrsets) {
if (!strcmp(sar->name, as_info->name)) {
found = true;
break;
}
}
if (!found) {
sar = xmalloc(sizeof *sar);
sar->name = xstrdup(as_info->name);
mem_stats.sb_flow_ref_usage += sb_addrset_ref_size(sar);
hmap_init(&sar->as_ip_to_flow_map);
ovs_list_insert(&stf->addrsets, &sar->list_node);
}
struct as_ip_to_flow_node * itfn =
as_ip_to_flow_find(&sar->as_ip_to_flow_map, &as_info->ip,
&as_info->mask);
if (!itfn) {
itfn = xmalloc(sizeof *itfn);
mem_stats.sb_flow_ref_usage += sizeof *itfn;
itfn->as_ip = as_info->ip;
itfn->as_mask = as_info->mask;
ovs_list_init(&itfn->flows);
uint32_t hash = hash_bytes(&as_info->ip, sizeof as_info->ip, 0);
hmap_insert(&sar->as_ip_to_flow_map, &itfn->hmap_node, hash);
}
ovs_list_insert(&itfn->flows, &sfr->as_ip_flow_list);
}
/* Flow table interfaces to the rest of ovn-controller. */
/* Adds a flow to 'desired_flows' with the specified 'match' and 'actions' to
* the OpenFlow table numbered 'table_id' with the given 'priority', OpenFlow
* 'cookie' and 'meter_id'. The caller retains ownership of 'match' and
* 'actions'.
*
* The flow is also linked to the sb_uuid that generates it.
*
* This just assembles the desired flow table in memory. Nothing is actually
* sent to the switch until a later call to ofctrl_put().
*
* The caller should initialize its own hmap to hold the flows. */
void
ofctrl_check_and_add_flow_metered(struct ovn_desired_flow_table *flow_table,
uint8_t table_id, uint16_t priority,
uint64_t cookie,
const struct match *match,
const struct ofpbuf *actions,
const struct uuid *sb_uuid,
uint32_t meter_id,
const struct addrset_info *as_info,
bool log_duplicate_flow)
{
struct desired_flow *f = desired_flow_alloc(table_id, priority, cookie,
match, actions,
meter_id);
if (desired_flow_lookup_check_uuid(flow_table, &f->flow, sb_uuid)) {
if (log_duplicate_flow) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
if (!VLOG_DROP_DBG(&rl)) {
char *s = ovn_flow_to_string(&f->flow);
VLOG_DBG("dropping duplicate flow: %s", s);
free(s);
}
}
desired_flow_destroy(f);
return;
}
hmap_insert(&flow_table->match_flow_table, &f->match_hmap_node,
f->flow.hash);
link_flow_to_sb(flow_table, f, sb_uuid, as_info);
track_flow_add_or_modify(flow_table, f);
ovn_flow_log(&f->flow, "ofctrl_add_flow");
}
void
ofctrl_add_flow(struct ovn_desired_flow_table *desired_flows,
uint8_t table_id, uint16_t priority, uint64_t cookie,
const struct match *match, const struct ofpbuf *actions,
const struct uuid *sb_uuid)
{
ofctrl_add_flow_metered(desired_flows, table_id, priority, cookie,
match, actions, sb_uuid, NX_CTLR_NO_METER, NULL);
}
void
ofctrl_add_flow_metered(struct ovn_desired_flow_table *desired_flows,
uint8_t table_id, uint16_t priority, uint64_t cookie,
const struct match *match,
const struct ofpbuf *actions,
const struct uuid *sb_uuid, uint32_t meter_id,
const struct addrset_info *as_info)
{
ofctrl_check_and_add_flow_metered(desired_flows, table_id, priority,
cookie, match, actions, sb_uuid,
meter_id, as_info, true);
}
struct ofpact_ref {
struct hmap_node hmap_node;
struct ofpact *ofpact;
};
static struct ofpact_ref *
ofpact_ref_find(const struct hmap *refs, const struct ofpact *ofpact)
{
uint32_t hash = hash_bytes(ofpact, ofpact->len, 0);
struct ofpact_ref *ref;
HMAP_FOR_EACH_WITH_HASH (ref, hmap_node, hash, refs) {
if (ofpacts_equal(ref->ofpact, ref->ofpact->len,
ofpact, ofpact->len)) {
return ref;
}
}
return NULL;
}
static void
ofpact_refs_destroy(struct hmap *refs)
{
struct ofpact_ref *ref;
HMAP_FOR_EACH_POP (ref, hmap_node, refs) {
free(ref);
}
hmap_destroy(refs);
}
/* Either add a new flow, or append actions on an existing flow. If the
* flow existed, a new link will also be created between the new sb_uuid
* and the existing flow. */
void
ofctrl_add_or_append_flow(struct ovn_desired_flow_table *desired_flows,
uint8_t table_id, uint16_t priority, uint64_t cookie,
const struct match *match,
const struct ofpbuf *actions,
const struct uuid *sb_uuid,
uint32_t meter_id,
const struct addrset_info *as_info)
{
struct desired_flow *existing;
struct desired_flow *f;
f = desired_flow_alloc(table_id, priority, cookie, match, actions,
meter_id);
existing = desired_flow_lookup_conjunctive(desired_flows, &f->flow);
if (existing) {
struct hmap existing_conj = HMAP_INITIALIZER(&existing_conj);
struct ofpact *ofpact;
OFPACT_FOR_EACH (ofpact, existing->flow.ofpacts,
existing->flow.ofpacts_len) {
if (ofpact->type != OFPACT_CONJUNCTION) {
continue;
}
struct ofpact_ref *ref = xmalloc(sizeof *ref);
ref->ofpact = ofpact;
uint32_t hash = hash_bytes(ofpact, ofpact->len, 0);
hmap_insert(&existing_conj, &ref->hmap_node, hash);
}
/* There's already a flow with this particular match and action
* 'conjunction'. Append the action to that flow rather than
* adding a new flow.
*/
uint64_t compound_stub[64 / 8];
struct ofpbuf compound;
ofpbuf_use_stub(&compound, compound_stub, sizeof(compound_stub));
ofpbuf_put(&compound, existing->flow.ofpacts,
existing->flow.ofpacts_len);
OFPACT_FOR_EACH (ofpact, f->flow.ofpacts, f->flow.ofpacts_len) {
if (ofpact->type != OFPACT_CONJUNCTION ||
!ofpact_ref_find(&existing_conj, ofpact)) {
ofpbuf_put(&compound, ofpact, OFPACT_ALIGN(ofpact->len));
}
}
ofpact_refs_destroy(&existing_conj);
mem_stats.desired_flow_usage -= desired_flow_size(existing);
free(existing->flow.ofpacts);
existing->flow.ofpacts = xmemdup(compound.data, compound.size);
existing->flow.ofpacts_len = compound.size;
mem_stats.desired_flow_usage += desired_flow_size(existing);
ofpbuf_uninit(&compound);
desired_flow_destroy(f);
f = existing;
/* Since the flow now shared by more than one SB lflows, don't track
* it with address set ips. So remove any existed as_info tracking, and
* then add the new sb link without as_info.
*
* XXX: this may still be tracked if the flow is shared by different
* lflows, but we need to remove the related conjunction from the
* actions properly when handle addrset ip deletion, instead of simply
* delete the flow. */
struct sb_flow_ref *sfr;
LIST_FOR_EACH (sfr, sb_list, &f->references) {
ovs_list_remove(&sfr->as_ip_flow_list);
ovs_list_init(&sfr->as_ip_flow_list);
}
link_flow_to_sb(desired_flows, f, sb_uuid, NULL);
} else {
hmap_insert(&desired_flows->match_flow_table, &f->match_hmap_node,
f->flow.hash);
link_flow_to_sb(desired_flows, f, sb_uuid, as_info);
}
track_flow_add_or_modify(desired_flows, f);
if (existing) {
ovn_flow_log(&f->flow, "ofctrl_add_or_append_flow (append)");
} else {
ovn_flow_log(&f->flow, "ofctrl_add_or_append_flow (add)");
}
}
void
ofctrl_remove_flows(struct ovn_desired_flow_table *flow_table,
const struct uuid *sb_uuid)
{
struct sb_to_flow *stf = sb_to_flow_find(&flow_table->uuid_flow_table,
sb_uuid);
if (stf) {
remove_flows_from_sb_to_flow(flow_table, stf, "ofctrl_remove_flow",
NULL);
}
/* remove any related group and meter info */
ovn_extend_table_remove_desired(groups, sb_uuid);
ovn_extend_table_remove_desired(meters, sb_uuid);
}
static void
flood_remove_flows_for_sb_uuid(struct ovn_desired_flow_table *flow_table,
const struct uuid *sb_uuid,
struct uuidset *flood_remove_nodes)
{
struct sb_to_flow *stf = sb_to_flow_find(&flow_table->uuid_flow_table,
sb_uuid);
if (!stf) {
return;
}
remove_flows_from_sb_to_flow(flow_table, stf, "flood remove",
flood_remove_nodes);
}
void
ofctrl_flood_remove_flows(struct ovn_desired_flow_table *flow_table,
struct uuidset *flood_remove_nodes)
{
/* flood_remove_flows_for_sb_uuid() will modify the 'flood_remove_nodes'
* hash map by inserting new items, so we can't use it for iteration.
* Copying the sb_uuids into an array. */
struct uuid *sb_uuids = uuidset_array(flood_remove_nodes);
size_t n = uuidset_count(flood_remove_nodes);
for (size_t i = 0; i < n; i++) {
flood_remove_flows_for_sb_uuid(flow_table, &sb_uuids[i],
flood_remove_nodes);
}
free(sb_uuids);
/* remove any related group and meter info */
struct uuidset_node *ofrn;
UUIDSET_FOR_EACH (ofrn, flood_remove_nodes) {
ovn_extend_table_remove_desired(groups, &ofrn->uuid);
ovn_extend_table_remove_desired(meters, &ofrn->uuid);
}
}
/* Remove desired flows related to the specified 'addrset_info' for the
* 'lflow_uuid'. Returns true if it can be processed completely, otherwise
* returns false, which would trigger a reprocessing of the lflow of
* 'lflow_uuid'. The expected_count is checked against the actual flows
* deleted, and if it doesn't match, return false, too. */
bool
ofctrl_remove_flows_for_as_ip(struct ovn_desired_flow_table *flow_table,
const struct uuid *lflow_uuid,
const struct addrset_info *as_info,
size_t expected_count)
{
struct sb_to_flow *stf = sb_to_flow_find(&flow_table->uuid_flow_table,
lflow_uuid);
if (!stf) {
/* No such flow, nothing needs to be done. */
return true;
}
struct sb_addrset_ref *sar;
bool found = false;
LIST_FOR_EACH (sar, list_node, &stf->addrsets) {
if (!strcmp(sar->name, as_info->name)) {
found = true;
break;
}
}
if (!found) {
/* No address set tracking infomation found, can't perform the
* deletion. */
return false;
}
struct as_ip_to_flow_node *itfn =
as_ip_to_flow_find(&sar->as_ip_to_flow_map, &as_info->ip,
&as_info->mask);
if (!itfn) {
/* This ip wasn't tracked, probably because it maps to a flow that has
* compound conjunction actions for the same ip from multiple address
* sets. */
return false;
}
struct sb_flow_ref *sfr;
size_t count = 0;
LIST_FOR_EACH_SAFE (sfr, as_ip_flow_list, &itfn->flows) {
/* If the desired flow is referenced by multiple sb lflows, it
* shouldn't have been indexed by address set. */
ovs_assert(ovs_list_is_short(&sfr->sb_list));
ovs_list_remove(&sfr->sb_list);
ovs_list_remove(&sfr->flow_list);
ovs_list_remove(&sfr->as_ip_flow_list);
struct desired_flow *f = sfr->flow;
mem_stats.sb_flow_ref_usage -= sb_flow_ref_size(sfr);
free(sfr);
ovs_assert(ovs_list_is_empty(&f->list_node));
ovs_assert(ovs_list_is_empty(&f->references));
ovn_flow_log(&f->flow, "remove_flows_for_as_ip");
hmap_remove(&flow_table->match_flow_table,
&f->match_hmap_node);
track_or_destroy_for_flow_del(flow_table, f);
count++;
}
hmap_remove(&sar->as_ip_to_flow_map, &itfn->hmap_node);
mem_stats.sb_flow_ref_usage -= sizeof *itfn;
free(itfn);
return (count == expected_count);
}
/* Remove ovn_flows for the given "sb_to_flow" node in the uuid_flow_table.
* Optionally log the message for each flow that is acturally removed, if
* log_msg is not NULL. */
static void
remove_flows_from_sb_to_flow(struct ovn_desired_flow_table *flow_table,
struct sb_to_flow *stf,
const char *log_msg,
struct uuidset *flood_remove_nodes)
{
/* ovn_flows that have other references and waiting to be removed. */
struct ovs_list to_be_removed = OVS_LIST_INITIALIZER(&to_be_removed);
/* Traverse all flows for the given sb_uuid. */
struct sb_flow_ref *sfr;
LIST_FOR_EACH_SAFE (sfr, flow_list, &stf->flows) {
ovs_list_remove(&sfr->sb_list);
ovs_list_remove(&sfr->flow_list);
ovs_list_remove(&sfr->as_ip_flow_list);
struct desired_flow *f = sfr->flow;
mem_stats.sb_flow_ref_usage -= sb_flow_ref_size(sfr);
free(sfr);
ovs_assert(ovs_list_is_empty(&f->list_node));
if (ovs_list_is_empty(&f->references)) {
if (log_msg) {
ovn_flow_log(&f->flow, log_msg);
}
hmap_remove(&flow_table->match_flow_table,
&f->match_hmap_node);
track_or_destroy_for_flow_del(flow_table, f);
} else if (flood_remove_nodes) {
ovs_list_insert(&to_be_removed, &f->list_node);
}
}
struct sb_addrset_ref *sar;
LIST_FOR_EACH_SAFE (sar, list_node, &stf->addrsets) {
ovs_list_remove(&sar->list_node);
struct as_ip_to_flow_node *itfn;
HMAP_FOR_EACH_SAFE (itfn, hmap_node, &sar->as_ip_to_flow_map) {
hmap_remove(&sar->as_ip_to_flow_map, &itfn->hmap_node);
ovs_assert(ovs_list_is_empty(&itfn->flows));
mem_stats.sb_flow_ref_usage -= sizeof *itfn;
free(itfn);
}
hmap_destroy(&sar->as_ip_to_flow_map);
mem_stats.sb_flow_ref_usage -= sb_addrset_ref_size(sar);
free(sar->name);
free(sar);
}
hmap_remove(&flow_table->uuid_flow_table, &stf->hmap_node);
mem_stats.sb_flow_ref_usage -= sb_to_flow_size(stf);
free(stf);
/* Traverse other referencing sb_uuids for the flows in the to_be_removed
* list. */
/* Detach the items in f->references from the sfr.flow_list lists,
* so that recursive calls will not mess up the sfr.sb_list list. */
struct desired_flow *f;
LIST_FOR_EACH (f, list_node, &to_be_removed) {
ovs_assert(!ovs_list_is_empty(&f->references));
LIST_FOR_EACH (sfr, sb_list, &f->references) {
ovs_list_remove(&sfr->flow_list);
ovs_list_remove(&sfr->as_ip_flow_list);
}
}
LIST_FOR_EACH_SAFE (f, list_node, &to_be_removed) {
LIST_FOR_EACH_SAFE (sfr, sb_list, &f->references) {
if (!uuidset_find(flood_remove_nodes, &sfr->sb_uuid)) {
uuidset_insert(flood_remove_nodes, &sfr->sb_uuid);
flood_remove_flows_for_sb_uuid(flow_table, &sfr->sb_uuid,
flood_remove_nodes);
}
ovs_list_remove(&sfr->sb_list);
mem_stats.sb_flow_ref_usage -= sb_flow_ref_size(sfr);
free(sfr);
}
ovs_list_remove(&f->list_node);
if (log_msg) {
ovn_flow_log(&f->flow, log_msg);
}
hmap_remove(&flow_table->match_flow_table,
&f->match_hmap_node);
track_or_destroy_for_flow_del(flow_table, f);
}
}
/* flow operations. */
static void
ovn_flow_init(struct ovn_flow *f, uint8_t table_id, uint16_t priority,
uint64_t cookie, const struct match *match,
const struct ofpbuf *actions, uint32_t meter_id)
{
f->table_id = table_id;
f->priority = priority;
minimatch_init(&f->match, match);
f->ofpacts = xmemdup(actions->data, actions->size);
f->ofpacts_len = actions->size;
f->hash = ovn_flow_match_hash(f);
f->cookie = cookie;
f->ctrl_meter_id = meter_id;
}
static size_t
desired_flow_size(const struct desired_flow *f)
{
return sizeof *f + f->flow.ofpacts_len;
}
static struct desired_flow *
desired_flow_alloc(uint8_t table_id, uint16_t priority, uint64_t cookie,
const struct match *match, const struct ofpbuf *actions,
uint32_t meter_id)
{
struct desired_flow *f = xmalloc(sizeof *f);
ovs_list_init(&f->references);
ovs_list_init(&f->list_node);
ovs_list_init(&f->installed_ref_list_node);
ovs_list_init(&f->track_list_node);
f->installed_flow = NULL;
f->is_deleted = false;
ovn_flow_init(&f->flow, table_id, priority, cookie, match, actions,
meter_id);
mem_stats.desired_flow_usage += desired_flow_size(f);
return f;
}
/* Returns a hash of the match key in 'f'. */
static uint32_t
ovn_flow_match_hash(const struct ovn_flow *f)
{
return hash_2words((f->table_id << 16) | f->priority,
minimatch_hash(&f->match, 0));
}
static size_t
installed_flow_size(const struct installed_flow *f)
{
return sizeof *f + f->flow.ofpacts_len;
}
/* Duplicate a desired flow to an installed flow. */
static struct installed_flow *
installed_flow_dup(struct desired_flow *src)
{
struct installed_flow *dst = xmalloc(sizeof *dst);
ovs_list_init(&dst->desired_refs);
dst->flow.table_id = src->flow.table_id;
dst->flow.priority = src->flow.priority;
minimatch_clone(&dst->flow.match, &src->flow.match);
dst->flow.ofpacts = xmemdup(src->flow.ofpacts, src->flow.ofpacts_len);
dst->flow.ofpacts_len = src->flow.ofpacts_len;
dst->flow.hash = src->flow.hash;
dst->flow.cookie = src->flow.cookie;
dst->flow.ctrl_meter_id = src->flow.ctrl_meter_id;
mem_stats.installed_flow_usage += installed_flow_size(dst);
return dst;
}
static struct desired_flow *
installed_flow_get_active(struct installed_flow *f)
{
if (!ovs_list_is_empty(&f->desired_refs)) {
return CONTAINER_OF(ovs_list_front(&f->desired_refs),
struct desired_flow,
installed_ref_list_node);
}
return NULL;
}
static struct desired_flow *
desired_flow_lookup__(struct ovn_desired_flow_table *flow_table,
const struct ovn_flow *target,
desired_flow_match_cb match_cb,
const void *arg)
{
struct desired_flow *d;
HMAP_FOR_EACH_WITH_HASH (d, match_hmap_node, target->hash,
&flow_table->match_flow_table) {
struct ovn_flow *f = &d->flow;
if (f->table_id == target->table_id
&& f->priority == target->priority
&& f->ctrl_meter_id == target->ctrl_meter_id
&& minimatch_equal(&f->match, &target->match)) {
if (!match_cb || match_cb(d, arg)) {
return d;
}
}
}
return NULL;
}
/* Finds and returns a desired_flow in 'flow_table' whose key is identical to
* 'target''s key, or NULL if there is none.
*/
static struct desired_flow *
desired_flow_lookup(struct ovn_desired_flow_table *flow_table,
const struct ovn_flow *target)
{
return desired_flow_lookup__(flow_table, target, NULL, NULL);
}
static bool
flow_lookup_match_uuid_cb(const struct desired_flow *candidate,
const void *arg)
{
const struct uuid *sb_uuid = arg;
struct sb_flow_ref *sfr;
LIST_FOR_EACH (sfr, sb_list, &candidate->references) {
if (uuid_equals(sb_uuid, &sfr->sb_uuid)) {
return true;
}
}
return false;
}
/* Finds and returns a desired_flow in 'flow_table' whose key is identical to
* 'target''s key, or NULL if there is none.
*
* The function will also check if the found flow is referenced by the
* 'sb_uuid'.
*/
static struct desired_flow *
desired_flow_lookup_check_uuid(struct ovn_desired_flow_table *flow_table,
const struct ovn_flow *target,
const struct uuid *sb_uuid)
{
return desired_flow_lookup__(flow_table, target, flow_lookup_match_uuid_cb,
sb_uuid);
}
static bool
flow_lookup_match_conj_cb(const struct desired_flow *candidate,
const void *arg OVS_UNUSED)
{
return flow_action_has_conj(&candidate->flow);
}
/* Finds and returns a desired_flow in 'flow_table' whose key is identical to
* 'target''s key, or NULL if there is none.
*
* The function will only return a matching flow if it contains action
* 'conjunction'.
*/
static struct desired_flow *
desired_flow_lookup_conjunctive(struct ovn_desired_flow_table *flow_table,
const struct ovn_flow *target)
{
return desired_flow_lookup__(flow_table, target, flow_lookup_match_conj_cb,
NULL);
}
/* Finds and returns an installed_flow in installed_flows whose key is
* identical to 'target''s key, or NULL if there is none. */
static struct installed_flow *
installed_flow_lookup(const struct ovn_flow *target,
struct hmap *installed_flows)
{
struct installed_flow *i;
HMAP_FOR_EACH_WITH_HASH (i, match_hmap_node, target->hash,
installed_flows) {
struct ovn_flow *f = &i->flow;
if (f->table_id == target->table_id
&& f->priority == target->priority
&& minimatch_equal(&f->match, &target->match)) {
return i;
}
}
return NULL;
}
static char *
ovn_flow_to_string(const struct ovn_flow *f)
{
struct ds s = DS_EMPTY_INITIALIZER;
ds_put_format(&s, "cookie=%"PRIx64", ", f->cookie);
ds_put_format(&s, "table_id=%"PRIu8", ", f->table_id);
ds_put_format(&s, "priority=%"PRIu16", ", f->priority);
minimatch_format(&f->match, NULL, NULL, &s, OFP_DEFAULT_PRIORITY);
ds_put_cstr(&s, ", actions=");
struct ofpact_format_params fp = { .s = &s };
ofpacts_format(f->ofpacts, f->ofpacts_len, &fp);
return ds_steal_cstr(&s);
}
static void
ovn_flow_log(const struct ovn_flow *f, const char *action)
{
if (VLOG_IS_DBG_ENABLED()) {
char *s = ovn_flow_to_string(f);
VLOG_DBG("%s flow: %s", action, s);
free(s);
}
}
static void
ovn_flow_log_size_err(const struct ovn_flow *f)
{
COVERAGE_INC(ofctrl_msg_too_long);
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
char *s = ovn_flow_to_string(f);
VLOG_ERR_RL(&rl, "The FLOW_MOD message is too big: %s", s);
free(s);
}
static void
ovn_flow_uninit(struct ovn_flow *f)
{
minimatch_destroy(&f->match);
free(f->ofpacts);
}
static void
desired_flow_destroy(struct desired_flow *f)
{
if (f) {
ovs_assert(ovs_list_is_empty(&f->references));
ovs_assert(!f->installed_flow);
mem_stats.desired_flow_usage -= desired_flow_size(f);
ovn_flow_uninit(&f->flow);
free(f);
}
}
static void
installed_flow_destroy(struct installed_flow *f)
{
if (f) {
ovs_assert(!installed_flow_get_active(f));
mem_stats.installed_flow_usage -= installed_flow_size(f);
ovn_flow_uninit(&f->flow);
free(f);
}
}
/* Desired flow table operations. */
void
ovn_desired_flow_table_init(struct ovn_desired_flow_table *flow_table)
{
hmap_init(&flow_table->match_flow_table);
hmap_init(&flow_table->uuid_flow_table);
ovs_list_init(&flow_table->tracked_flows);
flow_table->change_tracked = false;
}
void
ovn_desired_flow_table_clear(struct ovn_desired_flow_table *flow_table)
{
flow_table->change_tracked = false;
struct desired_flow *f;
LIST_FOR_EACH_SAFE (f, track_list_node, &flow_table->tracked_flows) {
ovs_list_remove(&f->track_list_node);
if (f->is_deleted) {
if (f->installed_flow) {
unlink_installed_to_desired(f->installed_flow, f);
}
desired_flow_destroy(f);
}
}
struct sb_to_flow *stf;
HMAP_FOR_EACH_SAFE (stf, hmap_node, &flow_table->uuid_flow_table) {
remove_flows_from_sb_to_flow(flow_table, stf, NULL, NULL);
}
}
void
ovn_desired_flow_table_destroy(struct ovn_desired_flow_table *flow_table)
{
ovn_desired_flow_table_clear(flow_table);
hmap_destroy(&flow_table->match_flow_table);
hmap_destroy(&flow_table->uuid_flow_table);
}
/* Installed flow table operations. */
static void
ovn_installed_flow_table_clear(void)
{
struct installed_flow *f;
HMAP_FOR_EACH_SAFE (f, match_hmap_node, &installed_lflows) {
hmap_remove(&installed_lflows, &f->match_hmap_node);
unlink_all_refs_for_installed_flow(f);
installed_flow_destroy(f);
}
HMAP_FOR_EACH_SAFE (f, match_hmap_node, &installed_pflows) {
hmap_remove(&installed_pflows, &f->match_hmap_node);
unlink_all_refs_for_installed_flow(f);
installed_flow_destroy(f);
}
}
static void
ovn_installed_flow_table_destroy(void)
{
ovn_installed_flow_table_clear();
hmap_destroy(&installed_lflows);
hmap_destroy(&installed_pflows);
}
/* Flow table update. */
static struct ofpbuf *
encode_flow_mod(struct ofputil_flow_mod *fm)
{
fm->buffer_id = UINT32_MAX;
fm->out_port = OFPP_ANY;
fm->out_group = OFPG_ANY;
return ofputil_encode_flow_mod(fm, OFPUTIL_P_OF15_OXM);
}
static struct ofpbuf *
encode_bundle_add(struct ofpbuf *msg, struct ofputil_bundle_ctrl_msg *bc)
{
struct ofputil_bundle_add_msg bam = {
.bundle_id = bc->bundle_id,
.flags = bc->flags,
.msg = msg->data,
};
return ofputil_encode_bundle_add(OFP15_VERSION, &bam);
}
static bool
add_flow_mod(struct ofputil_flow_mod *fm,
struct ofputil_bundle_ctrl_msg *bc,
struct ovs_list *msgs)
{
struct ofpbuf *msg = encode_flow_mod(fm);
struct ofpbuf *bundle_msg = encode_bundle_add(msg, bc);
uint32_t flow_mod_len = msg->size;
uint32_t bundle_len = bundle_msg->size;
ofpbuf_delete(msg);
if (flow_mod_len > UINT16_MAX || bundle_len > UINT16_MAX) {
ofpbuf_delete(bundle_msg);
return false;
}
ovs_list_push_back(msgs, &bundle_msg->list_node);
return true;
}
/* group_table. */
static struct ofpbuf *
encode_group_mod(const struct ofputil_group_mod *gm)
{
return ofputil_encode_group_mod(OFP15_VERSION, gm, NULL, -1);
}
static void
add_group_mod(struct ofputil_group_mod *gm,
struct ofputil_bundle_ctrl_msg *bc,
struct ovs_list *msgs)
{
struct ofpbuf *msg = encode_group_mod(gm);
if ((msg->size + sizeof(struct ofp14_bundle_ctrl_msg)) <= UINT16_MAX) {
struct ofpbuf *bundle_msg = encode_bundle_add(msg, bc);
ofpbuf_delete(msg);
ovs_list_push_back(msgs, &bundle_msg->list_node);
return;
}
/* This group mod request is too large to fit in a single OF message
* since the header can only specify a 16-bit size. We need to break
* this into multiple group_mod requests.
*/
/* Pull the first bucket. All buckets are approximately the same length
* since they contain near-identical actions. Using its length can give
* us a good approximation of how many buckets we can fit in a single
* OF message.
*/
ofpraw_pull_assert(msg);
struct ofp15_group_mod *ogm = ofpbuf_pull(msg, sizeof(*ogm));
struct ofp15_bucket *of_bucket = ofpbuf_pull(msg, sizeof(*of_bucket));
uint16_t bucket_size = ntohs(of_bucket->len);
ofpbuf_delete(msg);
/* Dividing by 2 here ensures that just in case there are variations in
* the size of the buckets, we will not put too many in our new group_mod
* message.
*/
size_t max_buckets = ((UINT16_MAX - sizeof *ogm -
sizeof(struct ofp14_bundle_ctrl_msg)) / bucket_size)
/ 2;
ovs_assert(max_buckets < ovs_list_size(&gm->buckets));
uint16_t command = OFPGC15_INSERT_BUCKET;
if (gm->command == OFPGC15_DELETE ||
gm->command == OFPGC15_REMOVE_BUCKET) {
command = OFPGC15_REMOVE_BUCKET;
}
struct ofputil_group_mod split = {
.command = command,
.type = gm->type,
.group_id = gm->group_id,
.command_bucket_id = OFPG15_BUCKET_LAST,
};
ovs_list_init(&split.buckets);
size_t i = 0;
struct ofputil_bucket *bucket;
LIST_FOR_EACH (bucket, list_node, &gm->buckets) {
if (i++ < max_buckets) {
continue;
}
break;
}
ovs_list_splice(&split.buckets, &bucket->list_node, &gm->buckets);
struct ofpbuf *orig = encode_group_mod(gm);
struct ofpbuf *bundle_msg = encode_bundle_add(orig, bc);
ofpbuf_delete(orig);
ovs_list_push_back(msgs, &bundle_msg->list_node);
/* We call this recursively just in case our new
* INSERT_BUCKET/REMOVE_BUCKET group_mod is still too
* large for an OF message. This will allow for it to
* be broken into pieces, too.
*/
add_group_mod(&split, bc, msgs);
ofputil_uninit_group_mod(&split);
}
static struct ofpbuf *
encode_meter_mod(const struct ofputil_meter_mod *mm)
{
return ofputil_encode_meter_mod(OFP15_VERSION, mm);
}
static void
add_meter_mod(const struct ofputil_meter_mod *mm, struct ovs_list *msgs)
{
struct ofpbuf *msg = encode_meter_mod(mm);
ovs_list_push_back(msgs, &msg->list_node);
}
static void
add_ct_flush_zone(uint16_t zone_id, struct ovs_list *msgs)
{
struct ofpbuf *msg = ofpraw_alloc(OFPRAW_NXT_CT_FLUSH_ZONE,
rconn_get_version(swconn), 0);
struct nx_zone_id *nzi = ofpbuf_put_zeros(msg, sizeof *nzi);
nzi->zone_id = htons(zone_id);
ovs_list_push_back(msgs, &msg->list_node);
}
static void
add_meter_string(struct ovn_extend_table_info *m_desired,
struct ovs_list *msgs)
{
/* Create and install new meter. */
struct ofputil_meter_mod mm;
enum ofputil_protocol usable_protocols;
char *meter_string = xasprintf("meter=%"PRIu32",%s",
m_desired->table_id,
&m_desired->name[52]);
char *error = parse_ofp_meter_mod_str(&mm, meter_string, OFPMC13_ADD,
&usable_protocols);
if (!error) {
add_meter_mod(&mm, msgs);
free(mm.meter.bands);
} else {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_ERR_RL(&rl, "new meter %s %s", error, meter_string);
free(error);
}
free(meter_string);
}
static void
update_ovs_meter(struct ovn_extend_table_info *entry,
const struct sbrec_meter *sb_meter, int cmd,
struct ovs_list *msgs)
{
struct ofputil_meter_mod mm;
mm.command = cmd;
mm.meter.meter_id = entry->table_id;
mm.meter.flags = OFPMF13_STATS;
if (!strcmp(sb_meter->unit, "pktps")) {
mm.meter.flags |= OFPMF13_PKTPS;
} else {
mm.meter.flags |= OFPMF13_KBPS;
}
mm.meter.n_bands = sb_meter->n_bands;
mm.meter.bands = xcalloc(mm.meter.n_bands, sizeof *mm.meter.bands);
for (size_t i = 0; i < sb_meter->n_bands; i++) {
struct sbrec_meter_band *sb_band = sb_meter->bands[i];
struct ofputil_meter_band *mm_band = &mm.meter.bands[i];
if (!strcmp(sb_band->action, "drop")) {
mm_band->type = OFPMBT13_DROP;
}
mm_band->prec_level = 0;
mm_band->rate = sb_band->rate;
mm_band->burst_size = sb_band->burst_size;
if (mm_band->burst_size) {
mm.meter.flags |= OFPMF13_BURST;
}
}
add_meter_mod(&mm, msgs);
free(mm.meter.bands);
}
static void
ofctrl_meter_bands_clear(void)
{
struct shash_node *node;
SHASH_FOR_EACH_SAFE (node, &meter_bands) {
struct meter_band_entry *mb = node->data;
shash_delete(&meter_bands, node);
free(mb->bands);
free(mb);
}
}
static void
ofctrl_meter_bands_destroy(void)
{
ofctrl_meter_bands_clear();
shash_destroy(&meter_bands);
}
static bool
ofctrl_meter_bands_is_equal(const struct sbrec_meter *sb_meter,
struct meter_band_entry *mb)
{
if (mb->n_bands != sb_meter->n_bands) {
return false;
}
for (int i = 0; i < sb_meter->n_bands; i++) {
int j;
for (j = 0; j < mb->n_bands; j++) {
if (sb_meter->bands[i]->rate == mb->bands[j].rate &&
sb_meter->bands[i]->burst_size == mb->bands[j].burst_size) {
break;
}
}
if (j == mb->n_bands) {
return false;
}
}
return true;
}
static void
ofctrl_meter_bands_alloc(const struct sbrec_meter *sb_meter,
struct ovn_extend_table_info *entry,
struct ovs_list *msgs)
{
struct meter_band_entry *mb = xzalloc(sizeof *mb);
mb->n_bands = sb_meter->n_bands;
mb->bands = xcalloc(mb->n_bands, sizeof *mb->bands);
for (int i = 0; i < sb_meter->n_bands; i++) {
mb->bands[i].rate = sb_meter->bands[i]->rate;
mb->bands[i].burst_size = sb_meter->bands[i]->burst_size;
}
shash_add(&meter_bands, entry->name, mb);
update_ovs_meter(entry, sb_meter, OFPMC13_ADD, msgs);
}
static void
ofctrl_meter_bands_update(const struct sbrec_meter *sb_meter,
struct ovn_extend_table_info *entry,
struct ovs_list *msgs)
{
struct meter_band_entry *mb =
shash_find_data(&meter_bands, entry->name);
if (!mb) {
ofctrl_meter_bands_alloc(sb_meter, entry, msgs);
return;
}
if (ofctrl_meter_bands_is_equal(sb_meter, mb)) {
return;
}
free(mb->bands);
mb->n_bands = sb_meter->n_bands;
mb->bands = xcalloc(mb->n_bands, sizeof *mb->bands);
for (int i = 0; i < sb_meter->n_bands; i++) {
mb->bands[i].rate = sb_meter->bands[i]->rate;
mb->bands[i].burst_size = sb_meter->bands[i]->burst_size;
}
update_ovs_meter(entry, sb_meter, OFPMC13_MODIFY, msgs);
}
static void
ofctrl_meter_bands_erase(struct ovn_extend_table_info *entry,
struct ovs_list *msgs)
{
struct meter_band_entry *mb =
shash_find_and_delete(&meter_bands, entry->name);
if (mb) {
/* Delete the meter. */
struct ofputil_meter_mod mm = {
.command = OFPMC13_DELETE,
.meter = { .meter_id = entry->table_id },
};
add_meter_mod(&mm, msgs);
free(mb->bands);
free(mb);
}
}
static const struct sbrec_meter *
sb_meter_lookup_by_name(struct ovsdb_idl_index *sbrec_meter_by_name,
const char *name)
{
const struct sbrec_meter *sb_meter;
struct sbrec_meter *index_row;
index_row = sbrec_meter_index_init_row(sbrec_meter_by_name);
sbrec_meter_index_set_name(index_row, name);
sb_meter = sbrec_meter_index_find(sbrec_meter_by_name, index_row);
sbrec_meter_index_destroy_row(index_row);
return sb_meter;
}
static void
ofctrl_meter_bands_sync(struct ovn_extend_table_info *m_existing,
struct ovsdb_idl_index *sbrec_meter_by_name,
struct ovs_list *msgs)
{
const struct sbrec_meter *sb_meter;
sb_meter = sb_meter_lookup_by_name(sbrec_meter_by_name, m_existing->name);
if (sb_meter) {
/* OFPMC13_ADD or OFPMC13_MODIFY */
ofctrl_meter_bands_update(sb_meter, m_existing, msgs);
} else {
/* OFPMC13_DELETE */
ofctrl_meter_bands_erase(m_existing, msgs);
}
}
static void
add_meter(struct ovn_extend_table_info *m_desired,
struct ovsdb_idl_index *sbrec_meter_by_name,
struct ovs_list *msgs)
{
const struct sbrec_meter *sb_meter;
sb_meter = sb_meter_lookup_by_name(sbrec_meter_by_name, m_desired->name);
if (!sb_meter) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_ERR_RL(&rl, "could not find meter named \"%s\"", m_desired->name);
return;
}
ofctrl_meter_bands_alloc(sb_meter, m_desired, msgs);
}
static void
installed_flow_add(struct ovn_flow *d,
struct ofputil_bundle_ctrl_msg *bc,
struct ovs_list *msgs)
{
/* Send flow_mod to add flow. */
struct ofputil_flow_mod fm = {
.match = d->match,
.priority = d->priority,
.table_id = d->table_id,
.ofpacts = d->ofpacts,
.ofpacts_len = d->ofpacts_len,
.new_cookie = htonll(d->cookie),
.command = OFPFC_ADD,
};
if (!add_flow_mod(&fm, bc, msgs)) {
ovn_flow_log_size_err(d);
}
}
static void
installed_flow_mod(struct ovn_flow *i, struct ovn_flow *d,
struct ofputil_bundle_ctrl_msg *bc,
struct ovs_list *msgs)
{
/* Update actions in installed flow. */
struct ofputil_flow_mod fm = {
.match = i->match,
.priority = i->priority,
.table_id = i->table_id,
.ofpacts = d->ofpacts,
.ofpacts_len = d->ofpacts_len,
.command = OFPFC_MODIFY_STRICT,
};
/* Update cookie if it is changed. */
if (i->cookie != d->cookie) {
fm.modify_cookie = true;
fm.new_cookie = htonll(d->cookie);
/* Use OFPFC_ADD so that cookie can be updated. */
fm.command = OFPFC_ADD;
}
bool result = add_flow_mod(&fm, bc, msgs);
/* Replace 'i''s actions and cookie by 'd''s. */
mem_stats.installed_flow_usage -= i->ofpacts_len - d->ofpacts_len;
free(i->ofpacts);
i->ofpacts = xmemdup(d->ofpacts, d->ofpacts_len);
i->ofpacts_len = d->ofpacts_len;
i->cookie = d->cookie;
if (!result) {
ovn_flow_log_size_err(i);
}
}
static void
installed_flow_del(struct ovn_flow *i,
struct ofputil_bundle_ctrl_msg *bc,
struct ovs_list *msgs)
{
struct ofputil_flow_mod fm = {
.match = i->match,
.priority = i->priority,
.table_id = i->table_id,
.command = OFPFC_DELETE_STRICT,
};
if (!add_flow_mod(&fm, bc, msgs)) {
ovn_flow_log_size_err(i);
}
}
static void
update_installed_flows_by_compare(struct ovn_desired_flow_table *flow_table,
struct ofputil_bundle_ctrl_msg *bc,
struct hmap *installed_flows,
struct ovs_list *msgs)
{
ovs_assert(ovs_list_is_empty(&flow_table->tracked_flows));
/* Iterate through all of the installed flows. If any of them are no
* longer desired, delete them; if any of them should have different
* actions, update them. */
struct installed_flow *i;
HMAP_FOR_EACH_SAFE (i, match_hmap_node, installed_flows) {
unlink_all_refs_for_installed_flow(i);
struct desired_flow *d = desired_flow_lookup(flow_table, &i->flow);
if (!d) {
/* Installed flow is no longer desirable. Delete it from the
* switch and from installed_flows. */
installed_flow_del(&i->flow, bc, msgs);
ovn_flow_log(&i->flow, "removing installed");
hmap_remove(installed_flows, &i->match_hmap_node);
installed_flow_destroy(i);
} else {
if (!ofpacts_equal(i->flow.ofpacts, i->flow.ofpacts_len,
d->flow.ofpacts, d->flow.ofpacts_len) ||
i->flow.cookie != d->flow.cookie) {
installed_flow_mod(&i->flow, &d->flow, bc, msgs);
ovn_flow_log(&i->flow, "updating installed");
}
link_installed_to_desired(i, d);
}
}
/* Iterate through the desired flows and add those that aren't found
* in the installed flow table. */
struct desired_flow *d;
HMAP_FOR_EACH (d, match_hmap_node, &flow_table->match_flow_table) {
i = installed_flow_lookup(&d->flow, installed_flows);
if (!i) {
ovn_flow_log(&d->flow, "adding installed");
installed_flow_add(&d->flow, bc, msgs);
/* Copy 'd' from 'flow_table' to installed_flows. */
i = installed_flow_dup(d);
hmap_insert(installed_flows, &i->match_hmap_node, i->flow.hash);
link_installed_to_desired(i, d);
} else if (!d->installed_flow) {
/* This is a desired_flow that conflicts with one installed
* previously but not linked yet. However, if this flow becomes
* active, e.g., it is less restrictive than the previous active
* flow then modify the installed flow.
*/
if (link_installed_to_desired(i, d)) {
installed_flow_mod(&i->flow, &d->flow, bc, msgs);
ovn_flow_log(&i->flow, "updating installed (conflict)");
}
}
}
}
/* Finds and returns a desired_flow in 'deleted_flows' that is exactly the
* same as 'target', including cookie and actions.
*/
static struct desired_flow *
deleted_flow_lookup(struct hmap *deleted_flows, struct ovn_flow *target)
{
struct desired_flow *d;
HMAP_FOR_EACH_WITH_HASH (d, match_hmap_node, target->hash,
deleted_flows) {
struct ovn_flow *f = &d->flow;
if (f->table_id == target->table_id
&& f->priority == target->priority
&& minimatch_equal(&f->match, &target->match)
&& f->cookie == target->cookie
&& ofpacts_equal(f->ofpacts, f->ofpacts_len, target->ofpacts,
target->ofpacts_len)) {
/* del_f must have been installed, otherwise it should have
* been removed during track_flow_del. */
ovs_assert(d->installed_flow);
/* Now we also need to make sure the desired flow being
* added/updated has exact same action and cookie as the installed
* flow of d. Otherwise, don't merge them, so that the
* installed flow can be updated later. */
struct ovn_flow *f_i = &d->installed_flow->flow;
if (f_i->cookie == target->cookie
&& ofpacts_equal(f_i->ofpacts, f_i->ofpacts_len,
target->ofpacts, target->ofpacts_len)) {
return d;
}
}
}
return NULL;
}
/* This function scans the tracked flow changes in the order and merges "add"
* or "update" after "deleted" operations for exactly same flow (priority,
* table, match, action and cookie), to avoid unnecessary OF messages being
* sent to OVS. */
static void
merge_tracked_flows(struct ovn_desired_flow_table *flow_table)
{
struct hmap deleted_flows = HMAP_INITIALIZER(&deleted_flows);
struct desired_flow *f;
LIST_FOR_EACH_SAFE (f, track_list_node,
&flow_table->tracked_flows) {
if (f->is_deleted) {
/* reuse f->match_hmap_node field since it is already removed from
* the desired flow table's match index. */
hmap_insert(&deleted_flows, &f->match_hmap_node,
f->flow.hash);
} else {
struct desired_flow *del_f = deleted_flow_lookup(&deleted_flows,
&f->flow);
if (!del_f) {
continue;
}
if (!f->installed_flow) {
/* f is not installed yet. */
replace_installed_to_desired(del_f->installed_flow, del_f, f);
} else {
/* f has been installed before, and now was updated to exact
* the same flow as del_f. */
ovs_assert(f->installed_flow == del_f->installed_flow);
unlink_installed_to_desired(del_f->installed_flow, del_f);
}
hmap_remove(&deleted_flows, &del_f->match_hmap_node);
ovs_list_remove(&del_f->track_list_node);
desired_flow_destroy(del_f);
ovs_list_remove(&f->track_list_node);
ovs_list_init(&f->track_list_node);
}
}
HMAP_FOR_EACH_SAFE (f, match_hmap_node, &deleted_flows) {
hmap_remove(&deleted_flows, &f->match_hmap_node);
}
hmap_destroy(&deleted_flows);
}
static void
update_installed_flows_by_track(struct ovn_desired_flow_table *flow_table,
struct ofputil_bundle_ctrl_msg *bc,
struct hmap *installed_flows,
struct ovs_list *msgs)
{
merge_tracked_flows(flow_table);
struct desired_flow *f;
LIST_FOR_EACH_SAFE (f, track_list_node,
&flow_table->tracked_flows) {
ovs_list_remove(&f->track_list_node);
if (f->is_deleted) {
/* The desired flow was deleted */
if (f->installed_flow) {
struct installed_flow *i = f->installed_flow;
bool was_active = unlink_installed_to_desired(i, f);
struct desired_flow *d = installed_flow_get_active(i);
if (!d) {
installed_flow_del(&i->flow, bc, msgs);
ovn_flow_log(&i->flow, "removing installed (tracked)");
hmap_remove(installed_flows, &i->match_hmap_node);
installed_flow_destroy(i);
} else if (was_active) {
/* There are other desired flow(s) referencing this
* installed flow, so update the OVS flow for the new
* active flow (at least the cookie will be different,
* even if the actions are the same). */
installed_flow_mod(&i->flow, &d->flow, bc, msgs);
ovn_flow_log(&i->flow, "updating installed (tracked)");
}
}
desired_flow_destroy(f);
} else {
/* The desired flow was added or modified. */
struct installed_flow *i = installed_flow_lookup(&f->flow,
installed_flows);
if (!i) {
/* Adding a new flow. */
installed_flow_add(&f->flow, bc, msgs);
ovn_flow_log(&f->flow, "adding installed (tracked)");
/* Copy 'f' from 'flow_table' to installed_flows. */
struct installed_flow *new_node = installed_flow_dup(f);
hmap_insert(installed_flows, &new_node->match_hmap_node,
new_node->flow.hash);
link_installed_to_desired(new_node, f);
} else if (installed_flow_get_active(i) == f) {
/* The installed flow is installed for f, but f has change
* tracked, so it must have been modified. */
installed_flow_mod(&i->flow, &f->flow, bc, msgs);
ovn_flow_log(&i->flow, "updating installed (tracked)");
} else if (!f->installed_flow) {
/* Adding a new flow that conflicts with an existing installed
* flow, so add it to the link. If this flow becomes active,
* e.g., it is less restrictive than the previous active flow
* then modify the installed flow.
*/
if (link_installed_to_desired(i, f)) {
installed_flow_mod(&i->flow, &f->flow, bc, msgs);
ovn_flow_log(&i->flow,
"updating installed (tracked conflict)");
}
}
/* The track_list_node emptyness is used to check if the node is
* already added to track list, so initialize it again here. */
ovs_list_init(&f->track_list_node);
}
}
}
static void
add_ct_flush_tuple(const struct ovn_lb_5tuple *tuple,
struct ovs_list *msgs)
{
if (VLOG_IS_DBG_ENABLED()) {
struct ds ds = DS_EMPTY_INITIALIZER;
ds_put_cstr(&ds, "Flushing CT for 5-tuple: vip=");
ipv6_format_mapped(&tuple->vip_ip, &ds);
ds_put_format(&ds, ":%"PRIu16", backend=", tuple->vip_port);
ipv6_format_mapped(&tuple->backend_ip, &ds);
ds_put_format(&ds, ":%"PRIu16", protocol=%"PRIu8,
tuple->backend_port, tuple->proto);
VLOG_DBG("%s", ds_cstr(&ds));
ds_destroy(&ds);
}
struct ofp_ct_match match = {
.ip_proto = tuple->proto,
.tuple_orig.dst = tuple->vip_ip,
.tuple_orig.dst_port = htons(tuple->vip_port),
.tuple_reply.src = tuple->backend_ip,
.tuple_reply.src_port = htons(tuple->backend_port),
};
struct ofpbuf *msg = ofp_ct_match_encode(&match, NULL, OFP15_VERSION);
ovs_list_push_back(msgs, &msg->list_node);
}
bool
ofctrl_has_backlog(void)
{
if (rconn_packet_counter_n_packets(tx_counter)
|| rconn_get_version(swconn) < 0) {
return true;
}
return false;
}
/* The flow table can be updated if the connection to the switch is up and
* in the correct state and not backlogged with existing flow_mods. (Our
* criteria for being backlogged appear very conservative, but the socket
* between ovn-controller and OVS provides some buffering.) */
static bool
ofctrl_can_put(void)
{
if (state != S_UPDATE_FLOWS
|| ofctrl_has_backlog()) {
return false;
}
return true;
}
/* Replaces the flow table on the switch, if possible, by the flows added
* with ofctrl_add_flow().
*
* Replaces the group table and meter table on the switch, if possible,
* by the contents of '->desired'.
*
* Sends conntrack flush messages to each zone in 'pending_ct_zones' that
* is in the CT_ZONE_OF_QUEUED state and then moves the zone into the
* CT_ZONE_OF_SENT state.
*
* This should be called after ofctrl_run() within the main loop. */
void
ofctrl_put(struct ovn_desired_flow_table *lflow_table,
struct ovn_desired_flow_table *pflow_table,
struct shash *pending_ct_zones,
struct shash *current_ct_zones,
struct hmap *pending_lb_tuples,
const struct hmap *local_datapaths,
struct ovsdb_idl_index *sbrec_meter_by_name,
const struct sbrec_ecmp_nexthop_table *enh_table,
uint64_t req_cfg,
bool lflows_changed,
bool pflows_changed,
struct tracked_acl_ids *tracked_acl_ids,
bool monitor_cond_complete)
{
static bool skipped_last_time = false;
static uint64_t old_req_cfg = 0;
bool need_put = false;
if (state == S_WAIT_BEFORE_CLEAR) {
/* If no more monitored condition changes expected, release wait
* before clear stage and skip over poll wait. */
if (monitor_cond_complete) {
wait_before_clear_proceed = true;
poll_immediate_wake();
}
skipped_last_time = true;
return;
}
if (lflows_changed || pflows_changed || skipped_last_time ||
ofctrl_initial_clear) {
need_put = true;
old_req_cfg = req_cfg;
} else if (req_cfg != old_req_cfg) {
/* req_cfg changed since last ofctrl_put() call */
if (cur_cfg == old_req_cfg) {
/* If there are no updates pending, we were up-to-date already,
* update with the new req_cfg.
*/
if (ovs_list_is_empty(&flow_updates)) {
cur_cfg = req_cfg;
old_req_cfg = req_cfg;
}
} else {
need_put = true;
old_req_cfg = req_cfg;
}
}
/* OpenFlow messages to send to the switch to bring it up-to-date. */
struct ovs_list msgs = OVS_LIST_INITIALIZER(&msgs);
if (local_datapaths) {
need_put |= ecmp_nexthop_monitor_run(enh_table, local_datapaths,
current_ct_zones, swconn, &msgs);
}
if (!need_put) {
ovs_assert(ovs_list_is_empty(&msgs));
VLOG_DBG("ofctrl_put not needed");
return;
}
if (!ofctrl_can_put()) {
VLOG_DBG("ofctrl_put can't be performed");
struct ofpbuf *msg;
LIST_FOR_EACH_POP (msg, list_node, &msgs) {
ofpbuf_delete(msg);
}
skipped_last_time = true;
return;
}
/* Iterate through ct zones that need to be flushed. */
struct shash_node *iter;
SHASH_FOR_EACH(iter, pending_ct_zones) {
struct ct_zone_pending_entry *ctzpe = iter->data;
if (ctzpe->state == CT_ZONE_OF_QUEUED) {
add_ct_flush_zone(ctzpe->ct_zone.zone, &msgs);
ctzpe->state = CT_ZONE_OF_SENT;
ctzpe->of_xid = 0;
}
}
if (ofctrl_initial_clear) {
/* Send a meter_mod to delete all meters.
* XXX: Ideally, we should include the meter deletion and
* reinstallation in the same bundle just like for flows and groups,
* for minimum data plane interruption. However, OVS doesn't support
* METER_MOD in bundle yet. */
struct ofputil_meter_mod mm;
memset(&mm, 0, sizeof mm);
mm.command = OFPMC13_DELETE;
mm.meter.meter_id = OFPM13_ALL;
add_meter_mod(&mm, &msgs);
}
/* Iterate through all the desired meters. If there are new ones,
* add them to the switch. */
struct ovn_extend_table_info *m_desired;
HMAP_FOR_EACH (m_desired, hmap_node, &meters->desired) {
struct ovn_extend_table_info *m_existing =
ovn_extend_table_lookup(&meters->existing, m_desired);
if (!m_existing) {
if (!strncmp(m_desired->name, "__string: ", 10)) {
/* The "set-meter" action creates a meter entry name that
* describes the meter itself. */
add_meter_string(m_desired, &msgs);
} else {
add_meter(m_desired, sbrec_meter_by_name, &msgs);
}
} else {
ofctrl_meter_bands_sync(m_existing, sbrec_meter_by_name, &msgs);
}
}
/* Add all flow updates into a bundle. */
static int bundle_id = 0;
struct ofputil_bundle_ctrl_msg bc = {
.bundle_id = bundle_id++,
.flags = OFPBF_ORDERED | OFPBF_ATOMIC,
};
struct ofpbuf *bundle_open, *bundle_commit;
/* Open a new bundle. */
bc.type = OFPBCT_OPEN_REQUEST;
bundle_open = ofputil_encode_bundle_ctrl_request(OFP15_VERSION, &bc);
ovs_list_push_back(&msgs, &bundle_open->list_node);
if (ofctrl_initial_clear) {
/* Send a flow_mod to delete all flows. */
struct ofputil_flow_mod fm = {
.table_id = OFPTT_ALL,
.command = OFPFC_DELETE,
};
minimatch_init_catchall(&fm.match);
add_flow_mod(&fm, &bc, &msgs);
minimatch_destroy(&fm.match);
/* Send a group_mod to delete all groups. */
struct ofputil_group_mod gm;
memset(&gm, 0, sizeof gm);
gm.command = OFPGC11_DELETE;
gm.group_id = OFPG_ALL;
gm.command_bucket_id = OFPG15_BUCKET_ALL;
ovs_list_init(&gm.buckets);
add_group_mod(&gm, &bc, &msgs);
ofputil_uninit_group_mod(&gm);
ofctrl_initial_clear = false;
}
/* Iterate through all the desired groups. If there are new ones,
* add them to the switch. */
struct ovn_extend_table_info *desired;
EXTEND_TABLE_FOR_EACH_UNINSTALLED (desired, groups) {
/* Create and install new group. */
struct ofputil_group_mod gm;
enum ofputil_protocol usable_protocols;
char *group_string = xasprintf("group_id=%"PRIu32",%s",
desired->table_id,
desired->name);
char *error = parse_ofp_group_mod_str(&gm, OFPGC15_ADD, group_string,
NULL, NULL, &usable_protocols);
if (!error) {
add_group_mod(&gm, &bc, &msgs);
} else {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_ERR_RL(&rl, "new group %s %s", error, group_string);
free(error);
}
free(group_string);
ofputil_uninit_group_mod(&gm);
}
/* If skipped last time, then process the flow table
* (tracked) flows even if lflows_changed is not set.
* Same for pflows_changed. */
if (lflows_changed || skipped_last_time) {
if (lflow_table->change_tracked) {
update_installed_flows_by_track(lflow_table, &bc,
&installed_lflows,
&msgs);
} else {
update_installed_flows_by_compare(lflow_table, &bc,
&installed_lflows,
&msgs);
}
}
if (pflows_changed || skipped_last_time) {
if (pflow_table->change_tracked) {
update_installed_flows_by_track(pflow_table, &bc,
&installed_pflows,
&msgs);
} else {
update_installed_flows_by_compare(pflow_table, &bc,
&installed_pflows,
&msgs);
}
}
skipped_last_time = false;
/* Iterate through the installed groups from previous runs. If they
* are not needed delete them. */
struct ovn_extend_table_info *installed;
EXTEND_TABLE_FOR_EACH_INSTALLED (installed, groups) {
/* Delete the group. */
struct ofputil_group_mod gm;
enum ofputil_protocol usable_protocols;
char *group_string = xasprintf("group_id=%"PRIu32"",
installed->table_id);
char *error = parse_ofp_group_mod_str(&gm, OFPGC15_DELETE,
group_string, NULL, NULL,
&usable_protocols);
if (!error) {
add_group_mod(&gm, &bc, &msgs);
} else {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 1);
VLOG_ERR_RL(&rl, "Error deleting group %d: %s",
installed->table_id, error);
free(error);
}
free(group_string);
ofputil_uninit_group_mod(&gm);
ovn_extend_table_remove_existing(groups, installed);
}
if (ovs_list_back(&msgs) == &bundle_open->list_node) {
/* No flow updates. Removing the bundle open request. */
ovs_list_pop_back(&msgs);
ofpbuf_delete(bundle_open);
} else {
/* Committing the bundle. */
bc.type = OFPBCT_COMMIT_REQUEST;
bundle_commit = ofputil_encode_bundle_ctrl_request(OFP15_VERSION, &bc);
ovs_list_push_back(&msgs, &bundle_commit->list_node);
}
/* Sync the contents of groups->desired to groups->existing. */
ovn_extend_table_sync(groups);
/* Iterate through the installed meters from previous runs. If they
* are not needed delete them. */
struct ovn_extend_table_info *m_installed;
EXTEND_TABLE_FOR_EACH_INSTALLED (m_installed, meters) {
/* Delete the meter. */
ofctrl_meter_bands_erase(m_installed, &msgs);
if (!strncmp(m_installed->name, "__string: ", 10)) {
struct ofputil_meter_mod mm = {
.command = OFPMC13_DELETE,
.meter = { .meter_id = m_installed->table_id },
};
add_meter_mod(&mm, &msgs);
}
ovn_extend_table_remove_existing(meters, m_installed);
}
/* Sync the contents of meters->desired to meters->existing. */
ovn_extend_table_sync(meters);
if (ovs_feature_is_supported(OVS_CT_TUPLE_FLUSH_SUPPORT)) {
struct ovn_lb_5tuple *tuple;
HMAP_FOR_EACH_POP (tuple, hmap_node, pending_lb_tuples) {
add_ct_flush_tuple(tuple, &msgs);
free(tuple);
}
}
acl_ids_flush_expired(tracked_acl_ids, rconn_get_version(swconn), &msgs);
if (!ovs_list_is_empty(&msgs)) {
/* Add a barrier to the list of messages. */
struct ofpbuf *barrier = ofputil_encode_barrier_request(OFP15_VERSION);
const struct ofp_header *oh = barrier->data;
ovs_be32 xid_ = oh->xid;
ovs_list_push_back(&msgs, &barrier->list_node);
acl_ids_record_barrier_xid(tracked_acl_ids, xid_);
/* Queue the messages. */
struct ofpbuf *msg;
LIST_FOR_EACH_POP (msg, list_node, &msgs) {
queue_msg(msg);
}
/* Store the barrier's xid with any newly sent ct flushes. */
SHASH_FOR_EACH(iter, pending_ct_zones) {
struct ct_zone_pending_entry *ctzpe = iter->data;
if (ctzpe->state == CT_ZONE_OF_SENT && !ctzpe->of_xid) {
ctzpe->of_xid = xid_;
}
}
/* Track the flow update. */
struct ofctrl_flow_update *fup;
LIST_FOR_EACH_REVERSE_SAFE (fup, list_node, &flow_updates) {
if (req_cfg < fup->req_cfg) {
/* This ofctrl_flow_update is for a configuration later than
* 'req_cfg'. This should not normally happen, because it
* means that the local seqno decreased and it should normally
* be monotonically increasing. */
VLOG_WARN("req_cfg regressed from %"PRId64" to %"PRId64,
fup->req_cfg, req_cfg);
mem_stats.oflow_update_usage -= ofctrl_flow_update_size(fup);
ovs_list_remove(&fup->list_node);
free(fup);
} else if (req_cfg == fup->req_cfg) {
/* This ofctrl_flow_update is for the same configuration as
* 'req_cfg'. Probably, some change to the physical topology
* means that we had to revise the OpenFlow flow table even
* though the logical topology did not change. Update fp->xid,
* so that we don't send a notification that we're up-to-date
* until we're really caught up. */
VLOG_DBG("advanced xid target for req_cfg=%"PRId64, req_cfg);
fup->xid = xid_;
goto done;
} else {
break;
}
}
/* Add a flow update. */
fup = xmalloc(sizeof *fup);
ovs_list_push_back(&flow_updates, &fup->list_node);
fup->xid = xid_;
fup->req_cfg = req_cfg;
mem_stats.oflow_update_usage += ofctrl_flow_update_size(fup);
done:;
} else if (!ovs_list_is_empty(&flow_updates)) {
/* Getting up-to-date with 'req_cfg' didn't require any extra flow
* table changes, so whenever we get up-to-date with the most recent
* flow table update, we're also up-to-date with 'req_cfg'. */
struct ofctrl_flow_update *fup = ofctrl_flow_update_from_list_node(
ovs_list_back(&flow_updates));
fup->req_cfg = req_cfg;
} else {
/* We were completely up-to-date before and still are. */
cur_cfg = req_cfg;
}
lflow_table->change_tracked = true;
ovs_assert(ovs_list_is_empty(&lflow_table->tracked_flows));
pflow_table->change_tracked = true;
ovs_assert(ovs_list_is_empty(&pflow_table->tracked_flows));
}
/* Looks up the logical port with the name 'port_name' in 'br_int_'. If
* found, returns true and sets '*portp' to the OpenFlow port number
* assigned to the port. Otherwise, returns false. */
static bool
ofctrl_lookup_port(const void *br_int_, const char *port_name,
unsigned int *portp)
{
const struct ovsrec_bridge *br_int = br_int_;
for (int i = 0; i < br_int->n_ports; i++) {
const struct ovsrec_port *port_rec = br_int->ports[i];
for (int j = 0; j < port_rec->n_interfaces; j++) {
const struct ovsrec_interface *iface_rec = port_rec->interfaces[j];
const char *iface_id = smap_get(&iface_rec->external_ids,
"iface-id");
if (iface_id && !strcmp(iface_id, port_name)) {
if (!iface_rec->n_ofport) {
continue;
}
int64_t ofport = iface_rec->ofport[0];
if (ofport < 1 || ofport > ofp_to_u16(OFPP_MAX)) {
continue;
}
*portp = ofport;
return true;
}
}
}
return false;
}
/* Generates a packet described by 'flow_s' in the syntax of an OVN
* logical expression and injects it into 'br_int'. The flow
* description must contain an ingress logical port that is present on
* 'br_int'.
*
* Returns NULL if successful, otherwise an error message that the caller
* must free(). */
char *
ofctrl_inject_pkt(const struct ovsrec_bridge *br_int, const char *flow_s,
const struct shash *addr_sets,
const struct shash *port_groups,
const struct smap *template_vars)
{
int version = rconn_get_version(swconn);
if (version < 0) {
return xstrdup("OpenFlow channel not ready.");
}
struct lex_str flow_exp_s;
if (!lexer_parse_template_string(&flow_exp_s, flow_s,
template_vars, NULL)) {
return xasprintf("Could not parse flow \"%s\"", flow_s);
}
struct flow uflow;
char *error = expr_parse_microflow(lex_str_get(&flow_exp_s), &symtab,
addr_sets, port_groups,
ofctrl_lookup_port, br_int, &uflow);
lex_str_free(&flow_exp_s);
if (error) {
return error;
}
/* The physical OpenFlow port was stored in the logical ingress
* port, so put it in the correct location for a flow structure. */
uflow.in_port.ofp_port = u16_to_ofp(uflow.regs[MFF_LOG_INPORT - MFF_REG0]);
uflow.regs[MFF_LOG_INPORT - MFF_REG0] = 0;
if (!uflow.in_port.ofp_port) {
return xstrdup("ingress port not found on hypervisor.");
}
uint64_t packet_stub[128 / 8];
struct dp_packet packet;
dp_packet_use_stub(&packet, packet_stub, sizeof packet_stub);
flow_compose(&packet, &uflow, NULL, 64, false);
uint64_t ofpacts_stub[1024 / 8];
struct ofpbuf ofpacts = OFPBUF_STUB_INITIALIZER(ofpacts_stub);
struct ofpact_resubmit *resubmit = ofpact_put_RESUBMIT(&ofpacts);
resubmit->in_port = OFPP_IN_PORT;
resubmit->table_id = 0;
struct ofputil_packet_out po = {
.packet = dp_packet_data(&packet),
.packet_len = dp_packet_size(&packet),
.buffer_id = UINT32_MAX,
.ofpacts = ofpacts.data,
.ofpacts_len = ofpacts.size,
};
match_set_in_port(&po.flow_metadata, uflow.in_port.ofp_port);
enum ofputil_protocol proto = ofputil_protocol_from_ofp_version(version);
queue_msg(ofputil_encode_packet_out(&po, proto));
dp_packet_uninit(&packet);
ofpbuf_uninit(&ofpacts);
return NULL;
}
bool
ofctrl_is_connected(void)
{
return rconn_is_connected(swconn);
}
void
ofctrl_get_memory_usage(struct simap *usage)
{
simap_increase(usage, "ofctrl_sb_flow_ref_usage-KB",
ROUND_UP(mem_stats.sb_flow_ref_usage, 1024) / 1024);
simap_increase(usage, "ofctrl_desired_flow_usage-KB",
ROUND_UP(mem_stats.desired_flow_usage, 1024) / 1024);
simap_increase(usage, "ofctrl_installed_flow_usage-KB",
ROUND_UP(mem_stats.installed_flow_usage, 1024) / 1024);
simap_increase(usage, "oflow_update_usage-KB",
ROUND_UP(mem_stats.oflow_update_usage, 1024) / 1024);
simap_increase(usage, "ofctrl_rconn_packet_counter-KB",
ROUND_UP(rconn_packet_counter_n_bytes(tx_counter), 1024)
/ 1024);
}
|