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 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345
|
diff -urN linux-2.2.13/Documentation/Configure.help linux-2.2.13-vs-0.9/Documentation/Configure.help
--- linux-2.2.13/Documentation/Configure.help Sat Oct 23 16:52:47 1999
+++ linux-2.2.13-vs-0.9/Documentation/Configure.help Tue Nov 2 16:20:14 1999
@@ -2643,6 +2643,78 @@
The module will be called ip_masq_markfw.o. If you want to compile
it as a module, say M here and read Documentation/modules.txt.
+IP: masquerading virtual server support
+CONFIG_IP_MASQUERADE_VS
+ IP Virtual Server support will let you build a virtual server
+ based on cluster of two or more real servers. This option must
+ be enabled for at least one of the clustered computers that will
+ take care of intercepting incomming connections to a single IP
+ address and scheduling them to real servers.
+ Three request dispatching techniques are implemented, they are
+ virtual server via NAT, virtual server via tunneling and virtual
+ server via direct routing. The round-robin scheduling, the weighted
+ round-robin secheduling, or the weighted least-connection scheduling
+ algorithm can be used to choose which server the connection is
+ directed to, thus load balancing can be achieved among the servers.
+ For more information and its administration program, please visit
+ the following URL:
+ http://www.linuxvirtualserver.org/
+ If you want this, say Y.
+
+IP masquerading VS table size (the Nth power of 2)
+CONFIG_IP_MASQUERADE_VS_TAB_BITS
+ Using a big ipvs hash table for virtual server will greatly reduce
+ conflicts in the ipvs hash table when there are hundreds of thousands
+ of active connections.
+ Note the table size must be power of 2. The table size will be the
+ value of 2 to the your input number power. For example, the default
+ number is 12, so the table size is 4096. Don't input the number too
+ small, otherwise you will lose performance on it. You can adapt the
+ table size yourself, according to your virtual server application. It
+ is good to set the table size not far less than the number of connections
+ per second multiplying average lasting time of connection in the table.
+ For example, your virtual server gets 200 connections per second, the
+ connection lasts for 200 seconds in average in the masquerading table,
+ the table size should be not far less than 200x200, it is good to set
+ the table size 32768 (2**15).
+ Another note that each connection occupies 128 bytes effectively and each
+ hash entry uses 8 bytes, so you can estimate how much memory is needed
+ for your box.
+
+IPVS: round-robin scheduling
+CONFIG_IP_MASQUERADE_VS_RR
+ The robin-robin scheduling algorithm simply directs network
+ connections to different real servers in a round-robin manner.
+ If you want to compile it in kernel, say Y. If you want to compile
+ it as a module, say M here and read Documentation/modules.txt.
+
+IPVS: weighted round-robin scheduling
+CONFIG_IP_MASQUERADE_VS_WRR
+ The weighted robin-robin scheduling algorithm directs network
+ connections to different real servers based on server weights
+ in a round-robin manner. Servers with higher weights receive
+ new connections first than those with less weights, and servers
+ with higher weights get more connections than those with less
+ weights and servers with equal weights get equal connections.
+ If you want to compile it in kernel, say Y. If you want to compile
+ it as a module, say M here and read Documentation/modules.txt.
+
+IPVS: least-connection scheduling
+CONFIG_IP_MASQUERADE_VS_LC
+ The least-connection scheduling algorithm directs network
+ connections to the server with the least number of active
+ connections.
+ If you want to compile it in kernel, say Y. If you want to compile
+ it as a module, say M here and read Documentation/modules.txt.
+
+IPVS: weighted least-connection scheduling
+CONFIG_IP_MASQUERADE_VS_WLC
+ The weighted least-connection scheduling algorithm directs network
+ connections to the server with the least active connections
+ normalized by the server weight.
+ If you want to compile it in kernel, say Y. If you want to compile
+ it as a module, say M here and read Documentation/modules.txt.
+
IP: always defragment (required for masquerading)
CONFIG_IP_ALWAYS_DEFRAG
If you say Y here, then all incoming fragments (parts of IP packets
diff -urN linux-2.2.13/include/linux/ip_masq.h linux-2.2.13-vs-0.9/include/linux/ip_masq.h
--- linux-2.2.13/include/linux/ip_masq.h Sat Oct 23 17:02:32 1999
+++ linux-2.2.13-vs-0.9/include/linux/ip_masq.h Fri Oct 29 10:31:12 1999
@@ -103,6 +103,26 @@
#define IP_MASQ_MFW_SCHED 0x01
+/*
+ * Virtual server stuff
+ */
+struct ip_vs_user {
+ /* virtual service options */
+ u_int16_t protocol;
+ u_int32_t vaddr; /* virtual address */
+ u_int16_t vport;
+ unsigned vs_flags; /* virtual service flags */
+ unsigned timeout; /* persistent timeout in ticks */
+ u_int32_t netmask; /* persistent netmask */
+
+ /* destination specific options */
+ u_int32_t daddr; /* real destination address */
+ u_int16_t dport;
+ unsigned masq_flags; /* destination flags */
+ int weight; /* destination weight */
+};
+
+
#define IP_FW_MASQCTL_MAX 256
#define IP_MASQ_TNAME_MAX 32
@@ -115,6 +135,7 @@
struct ip_autofw_user autofw_user;
struct ip_mfw_user mfw_user;
struct ip_masq_user user;
+ struct ip_vs_user vs_user;
unsigned char m_raw[IP_FW_MASQCTL_MAX];
} u;
};
@@ -124,7 +145,9 @@
#define IP_MASQ_TARGET_CORE 1
#define IP_MASQ_TARGET_MOD 2 /* masq_mod is selected by "name" */
#define IP_MASQ_TARGET_USER 3
-#define IP_MASQ_TARGET_LAST 4
+#define IP_MASQ_TARGET_VS 4
+#define IP_MASQ_TARGET_LAST 5
+
#define IP_MASQ_CMD_NONE 0 /* just peek */
#define IP_MASQ_CMD_INSERT 1
@@ -136,5 +159,9 @@
#define IP_MASQ_CMD_LIST 7 /* actually fake: done via /proc */
#define IP_MASQ_CMD_ENABLE 8
#define IP_MASQ_CMD_DISABLE 9
+#define IP_MASQ_CMD_ADD_DEST 10 /* for adding dest in IPVS */
+#define IP_MASQ_CMD_DEL_DEST 11 /* for deleting dest in IPVS */
+#define IP_MASQ_CMD_SET_DEST 12 /* for setting dest in IPVS */
#endif /* _LINUX_IP_MASQ_H */
+
diff -urN linux-2.2.13/include/net/ip.h linux-2.2.13-vs-0.9/include/net/ip.h
--- linux-2.2.13/include/net/ip.h Mon Oct 25 10:19:26 1999
+++ linux-2.2.13-vs-0.9/include/net/ip.h Sun Nov 28 10:21:46 1999
@@ -47,6 +47,9 @@
#define IPSKB_MASQUERADED 1
#define IPSKB_TRANSLATED 2
#define IPSKB_FORWARDED 4
+#ifdef CONFIG_IP_MASQUERADE_VS
+#define IPSKB_REDIRECTED 8
+#endif
};
struct ipcm_cookie
diff -urN linux-2.2.13/include/net/ip_masq.h linux-2.2.13-vs-0.9/include/net/ip_masq.h
--- linux-2.2.13/include/net/ip_masq.h Mon Oct 25 10:25:25 1999
+++ linux-2.2.13-vs-0.9/include/net/ip_masq.h Sun Nov 28 10:22:11 1999
@@ -14,6 +14,10 @@
#include <linux/list.h>
#endif /* __KERNEL__ */
+#ifdef CONFIG_IP_MASQUERADE_VS
+struct ip_vs_dest;
+#endif
+
/*
* This define affects the number of ports that can be handled
* by each of the protocol helper modules.
@@ -40,10 +44,6 @@
#define IP_MASQ_MOD_CTL 0x00
#define IP_MASQ_USER_CTL 0x01
-#ifdef __KERNEL__
-
-#define IP_MASQ_TAB_SIZE 256
-
#define IP_MASQ_F_NO_DADDR 0x0001 /* no daddr yet */
#define IP_MASQ_F_NO_DPORT 0x0002 /* no dport set yet */
#define IP_MASQ_F_NO_SADDR 0x0004 /* no sport set yet */
@@ -60,6 +60,22 @@
#define IP_MASQ_F_USER 0x2000 /* from uspace */
#define IP_MASQ_F_SIMPLE_HASH 0x8000 /* prevent s+d and m+d hashing */
+#ifdef CONFIG_IP_MASQUERADE_VS
+#define IP_MASQ_F_VS 0x00010000 /* virtual server releated */
+#define IP_MASQ_F_VS_NO_OUTPUT 0x00020000 /* output packets avoid masq */
+#define IP_MASQ_F_VS_INACTIVE 0x00040000 /* not established */
+#define IP_MASQ_F_VS_FWD_MASK 0x00700000 /* mask for the fdw method */
+#define IP_MASQ_F_VS_LOCALNODE 0x00100000 /* local node destination */
+#define IP_MASQ_F_VS_TUNNEL 0x00200000 /* packets will be tunneled */
+#define IP_MASQ_F_VS_DROUTE 0x00400000 /* direct routing */
+ /* masquerading otherwise */
+#define IP_MASQ_VS_FWD(ms) (ms->flags & IP_MASQ_F_VS_FWD_MASK)
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
+#ifdef __KERNEL__
+
+#define IP_MASQ_TAB_SIZE 256
+
/*
* Delta seq. info structure
* Each MASQ struct has 2 (output AND input seq. changes).
@@ -88,9 +104,12 @@
struct ip_masq *control; /* Master control connection */
atomic_t n_control; /* Number of "controlled" masqs */
unsigned flags; /* status flags */
- unsigned timeout; /* timeout */
+ unsigned long timeout; /* timeout */
unsigned state; /* state info */
struct ip_masq_timeout_table *timeout_table;
+#ifdef CONFIG_IP_MASQUERADE_VS
+ struct ip_vs_dest *dest; /* real server */
+#endif /* CONFIG_IP_MASQUERADE_VS */
};
/*
diff -urN linux-2.2.13/include/net/ip_vs.h linux-2.2.13-vs-0.9/include/net/ip_vs.h
--- linux-2.2.13/include/net/ip_vs.h Thu Jan 1 08:00:00 1970
+++ linux-2.2.13-vs-0.9/include/net/ip_vs.h Wed Dec 22 10:22:51 1999
@@ -0,0 +1,236 @@
+/*
+ * IP virtual server
+ * data structure and funcationality definitions
+ */
+
+#include <linux/config.h>
+
+#ifndef _IP_VS_H
+#define _IP_VS_H
+
+#define IP_VS_VERSION_CODE 0x000907
+
+/*
+ * Virtual Service Flags
+ */
+#define IP_VS_SVC_F_PERSISTENT 0x0001 /* persistent port */
+#define IP_VS_SVC_F_HASHED 0x0002 /* hashed entry */
+
+/*
+ * Destination Server Flags
+ */
+#define IP_VS_DEST_F_AVAILABLE 0x0001 /* Available tag */
+
+/*
+ * The default IP_VS_TEMPLATE_TIMEOUT is a little larger than average
+ * connection time plus MASQUERADE_EXPIRE_TCP_FIN(2*60*HZ). Because the
+ * template won't be released until its controlled masq entries are
+ * expired.
+ * If IP_VS_TEMPLATE_TIMEOUT is too less, the template will soon expire
+ * and will be put in expire again and again, which requires additional
+ * overhead. If it is too large, the same will always visit the same
+ * server, which will make dynamic load imbalance worse.
+ */
+#define IP_VS_TEMPLATE_TIMEOUT 6*60*HZ
+
+#ifdef __KERNEL__
+
+#ifdef CONFIG_IP_VS_DEBUG
+#define IP_VS_DBG(msg...) printk(KERN_DEBUG "IP_VS: " ## msg )
+#else /* NO DEBUGGING at ALL */
+#define IP_VS_DBG(msg...)
+#endif
+
+#define IP_VS_ERR(msg...) printk(KERN_ERR "IP_VS: " ## msg )
+#define IP_VS_INFO(msg...) printk(KERN_INFO "IP_VS: " ## msg )
+#define IP_VS_WARNING(msg...) \
+ printk(KERN_WARNING "IP_VS: " ## msg)
+
+struct ip_vs_dest;
+struct ip_vs_scheduler;
+
+/*
+ * The information about the virtual service offered to the net
+ * and the forwarding entries
+ */
+struct ip_vs_service {
+ struct list_head n_list; /* hashed d-linked list head */
+ __u32 addr; /* IP address for virtual service */
+ __u16 port; /* port number for the service */
+ __u16 protocol; /* which protocol (TCP/UDP) */
+ unsigned flags; /* service status flags */
+ unsigned timeout; /* persistent timeout in ticks */
+ __u32 netmask; /* grouping granularity */
+ struct list_head destinations; /* real server d-linked list */
+ struct ip_vs_scheduler *scheduler; /* bound scheduler object */
+ void *sched_data; /* scheduler application data */
+};
+
+
+/*
+ * The real server destination forwarding entry
+ * with ip address, port
+ */
+struct ip_vs_dest {
+ struct list_head n_list; /* d-linked list head */
+ __u32 addr; /* IP address of real server */
+ __u16 port; /* port number of the service */
+ unsigned flags; /* dest status flags */
+ unsigned masq_flags; /* flags to copy to masq */
+ atomic_t activeconns; /* active connections */
+ atomic_t inactconns; /* inactive connections */
+ atomic_t refcnt; /* reference counter */
+ int weight; /* server weight */
+
+ /* for virtual service */
+ __u16 protocol; /* which protocol (TCP/UDP) */
+ __u32 vaddr; /* IP address for virtual service */
+ __u16 vport; /* port number for the service */
+};
+
+
+/*
+ * The scheduler object
+ */
+struct ip_vs_scheduler {
+ struct list_head n_list; /* d-linked list head */
+ char *name; /* scheduler name */
+ atomic_t refcnt; /* reference counter */
+
+ /* scheduler initializing service */
+ int (*init_service)(struct ip_vs_service *svc);
+ /* scheduling service finish */
+ int (*done_service)(struct ip_vs_service *svc);
+ /* scheduler updating service */
+ int (*update_service)(struct ip_vs_service *svc);
+
+ /* selecting a server from the given service */
+ struct ip_vs_dest* (*schedule)(struct ip_vs_service *svc);
+};
+
+/*
+ * IP Virtual Server masq entry hash table
+ */
+#define IP_VS_TAB_BITS CONFIG_IP_MASQUERADE_VS_TAB_BITS
+#define IP_VS_TAB_SIZE (1 << IP_VS_TAB_BITS)
+extern struct list_head *ip_vs_table;
+
+/*
+ * Hash and unhash functions
+ */
+extern int ip_vs_hash(struct ip_masq *ms);
+extern int ip_vs_unhash(struct ip_masq *ms);
+
+/*
+ * Registering/unregistering scheduler functions
+ */
+extern int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
+extern int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler);
+
+/*
+ * Lookup functions for the hash table
+ */
+extern struct ip_masq * ip_vs_in_get(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port);
+extern struct ip_masq * ip_vs_out_get(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port);
+
+/*
+ * Creating a masquerading entry for IPVS
+ */
+extern struct ip_masq * ip_masq_new_vs(int proto, __u32 maddr, __u16 mport, __u32 saddr, __u16 sport, __u32 daddr, __u16 dport, unsigned flags);
+
+/*
+ * IPVS data and functions
+ */
+extern rwlock_t __ip_vs_lock;
+
+extern void ip_vs_set_state(struct ip_masq *ms, int new_state);
+extern void ip_vs_bind_masq(struct ip_masq *ms, struct ip_vs_dest *dest);
+extern void ip_vs_unbind_masq(struct ip_masq *ms);
+
+extern int ip_vs_ctl(int optname, struct ip_masq_ctl *mctl, int optlen);
+extern struct ip_vs_service * ip_vs_lookup_service(__u16 protocol,
+ __u32 vaddr, __u16 vport);
+extern struct ip_masq * ip_vs_schedule(struct ip_vs_service *svc,
+ struct iphdr *iph);
+extern int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb);
+extern int ip_vs_tunnel_xmit(struct sk_buff *skb, __u32 daddr);
+extern int ip_vs_dr_xmit(struct sk_buff *skb, __u32 daddr);
+
+/*
+ * init function
+ */
+extern int ip_vs_init(void);
+
+/*
+ * init function prototypes for scheduling modules
+ * these function will be called when they are built in kernel
+ */
+extern int ip_vs_rr_init(void);
+extern int ip_vs_wrr_init(void);
+extern int ip_vs_lc_init(void);
+extern int ip_vs_wlc_init(void);
+
+
+/*
+ * Slow timer functions for IPVS
+ */
+extern void add_sltimer(struct timer_list * timer);
+extern int del_sltimer(struct timer_list * timer);
+extern void mod_sltimer(struct timer_list *timer, unsigned long expires);
+
+
+/*
+ * ip_vs_fwd_tag returns the forwarding tag of the masq
+ */
+extern __inline__ char ip_vs_fwd_tag(struct ip_masq *ms)
+{
+ char fwd = 'M';
+
+ switch (IP_MASQ_VS_FWD(ms)) {
+ case IP_MASQ_F_VS_LOCALNODE: fwd = 'L'; break;
+ case IP_MASQ_F_VS_TUNNEL: fwd = 'T'; break;
+ case IP_MASQ_F_VS_DROUTE: fwd = 'R'; break;
+ }
+ return fwd;
+}
+
+
+/*
+ * ip_vs_forward forwards the packet through tunneling, direct
+ * routing or local node (passing to the upper layer).
+ * Return values mean:
+ * 0 skb must be passed to the upper layer
+ * -1 skb must be released
+ * -2 skb has been released
+ */
+extern __inline__ int ip_vs_forward(struct sk_buff *skb, struct ip_masq *ms)
+{
+ int ret = -1;
+ switch (IP_MASQ_VS_FWD(ms)) {
+ case IP_MASQ_F_VS_TUNNEL:
+ if (ip_vs_tunnel_xmit(skb, ms->saddr) == 0) {
+ IP_VS_DBG("tunneling failed.\n");
+ } else {
+ IP_VS_DBG("tunneling succeeded.\n");
+ }
+ ret = -2;
+ break;
+
+ case IP_MASQ_F_VS_DROUTE:
+ if (ip_vs_dr_xmit(skb, ms->saddr) == 0) {
+ IP_VS_DBG("direct routing failed.\n");
+ } else {
+ IP_VS_DBG("direct routing succeeded.\n");
+ }
+ ret = -2;
+ break;
+
+ case IP_MASQ_F_VS_LOCALNODE:
+ ret = 0;
+ }
+ return ret;
+}
+
+#endif /* __KERNEL__ */
+
+#endif /* _IP_VS_H */
diff -urN linux-2.2.13/net/ipv4/Config.in linux-2.2.13-vs-0.9/net/ipv4/Config.in
--- linux-2.2.13/net/ipv4/Config.in Sat Oct 23 16:52:51 1999
+++ linux-2.2.13-vs-0.9/net/ipv4/Config.in Fri Oct 29 10:13:51 1999
@@ -50,6 +50,14 @@
tristate 'IP: ipportfw masq support (EXPERIMENTAL)' CONFIG_IP_MASQUERADE_IPPORTFW
tristate 'IP: ip fwmark masq-forwarding support (EXPERIMENTAL)' CONFIG_IP_MASQUERADE_MFW
fi
+ bool 'IP: masquerading virtual server support (EXPERIMENTAL)' CONFIG_IP_MASQUERADE_VS
+ if [ "$CONFIG_IP_MASQUERADE_VS" = "y" ]; then
+ int 'IP masquerading VS table size (the Nth power of 2)' CONFIG_IP_MASQUERADE_VS_TAB_BITS 12
+ tristate 'IPVS: round-robin scheduling' CONFIG_IP_MASQUERADE_VS_RR
+ tristate 'IPVS: weighted round-robin scheduling' CONFIG_IP_MASQUERADE_VS_WRR
+ tristate 'IPVS: least-connection scheduling' CONFIG_IP_MASQUERADE_VS_LC
+ tristate 'IPVS: weighted least-connection scheduling' CONFIG_IP_MASQUERADE_VS_WLC
+ fi
fi
fi
fi
diff -urN linux-2.2.13/net/ipv4/Makefile linux-2.2.13-vs-0.9/net/ipv4/Makefile
--- linux-2.2.13/net/ipv4/Makefile Tue Jan 5 07:31:34 1999
+++ linux-2.2.13-vs-0.9/net/ipv4/Makefile Thu Sep 23 11:31:57 1999
@@ -91,6 +91,42 @@
endif
+ifeq ($(CONFIG_IP_MASQUERADE_VS),y)
+ IPV4X_OBJS += ip_vs.o
+
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_RR),y)
+ IPV4_OBJS += ip_vs_rr.o
+ else
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_RR),m)
+ M_OBJS += ip_vs_rr.o
+ endif
+ endif
+
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_WRR),y)
+ IPV4_OBJS += ip_vs_wrr.o
+ else
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_WRR),m)
+ M_OBJS += ip_vs_wrr.o
+ endif
+ endif
+
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_LC),y)
+ IPV4_OBJS += ip_vs_lc.o
+ else
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_LC),m)
+ M_OBJS += ip_vs_lc.o
+ endif
+ endif
+
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_WLC),y)
+ IPV4_OBJS += ip_vs_wlc.o
+ else
+ ifeq ($(CONFIG_IP_MASQUERADE_VS_WLC),m)
+ M_OBJS += ip_vs_wlc.o
+ endif
+ endif
+endif
+
M_OBJS += ip_masq_user.o
M_OBJS += ip_masq_ftp.o ip_masq_irc.o ip_masq_raudio.o ip_masq_quake.o
M_OBJS += ip_masq_vdolive.o ip_masq_cuseeme.o
diff -urN linux-2.2.13/net/ipv4/ip_input.c linux-2.2.13-vs-0.9/net/ipv4/ip_input.c
--- linux-2.2.13/net/ipv4/ip_input.c Sat Oct 23 16:52:51 1999
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_input.c Wed Nov 10 17:50:45 1999
@@ -249,6 +249,15 @@
*/
{
int ret;
+
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if((IPCB(skb)->flags&IPSKB_REDIRECTED)) {
+ printk(KERN_DEBUG "ip_input(): ipvs recursion detected. Check ipvs configuration\n");
+ kfree_skb(skb);
+ return 0;
+ }
+#endif
+
/*
* Some masq modules can re-inject packets if
* bad configured.
@@ -261,6 +270,12 @@
}
ret = ip_fw_demasquerade(&skb);
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (ret == -2) {
+ /* skb has already been released */
+ return 0;
+ }
+#endif
if (ret < 0) {
kfree_skb(skb);
return 0;
diff -urN linux-2.2.13/net/ipv4/ip_masq.c linux-2.2.13-vs-0.9/net/ipv4/ip_masq.c
--- linux-2.2.13/net/ipv4/ip_masq.c Sat Oct 23 16:52:50 1999
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_masq.c Wed Dec 22 21:18:22 1999
@@ -47,7 +47,12 @@
* Kai Bankett : do not toss other IP protos in proto_doff()
* Dan Kegel : pointed correct NAT behavior for UDP streams
* Julian Anastasov : use daddr and dport as hash keys
- *
+ * Wensong Zhang : Added virtual server support
+ * Peter Kese : added masq TCP state handling for input-only
+ * Julian Anastasov : step to mSR after SYN in INPUT_ONLY table
+ * Julian Anastasov : fixed huge expire bug for IPVS after bad checksum
+ * Wensong Zhang : added server status checking for IPVS
+ *
*/
#include <linux/config.h>
@@ -81,6 +86,10 @@
#include <linux/ip_fw.h>
#include <linux/ip_masq.h>
+#ifdef CONFIG_IP_MASQUERADE_VS
+#include <net/ip_vs.h>
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
int sysctl_ip_masq_debug = 0;
/*
@@ -171,29 +180,38 @@
/*fin*/ {{mTW, mFW, mSS, mTW, mFW, mTW, mCL, mTW, mLA, mLI }},
/*ack*/ {{mES, mES, mSS, mSR, mFW, mTW, mCL, mCW, mLA, mES }},
/*rst*/ {{mCL, mCL, mSS, mCL, mCL, mTW, mCL, mCL, mCL, mCL }},
+
+#ifdef CONFIG_IP_MASQUERADE_VS
+/* INPUT-ONLY */
+/* mNO, mES, mSS, mSR, mFW, mTW, mCL, mCW, mLA, mLI */
+/*syn*/ {{mSR, mES, mES, mSR, mSR, mSR, mSR, mSR, mSR, mSR }},
+/*fin*/ {{mCL, mFW, mSS, mTW, mFW, mTW, mCL, mCW, mLA, mLI }},
+/*ack*/ {{mCL, mES, mSS, mES, mFW, mTW, mCL, mCW, mCL, mLI }},
+/*rst*/ {{mCL, mCL, mCL, mSR, mCL, mCL, mCL, mCL, mLA, mLI }},
+#endif
};
-static __inline__ int masq_tcp_state_idx(struct tcphdr *th, int output)
+#define MASQ_STATE_INPUT 0
+#define MASQ_STATE_OUTPUT 4
+#define MASQ_STATE_INPUT_ONLY 8
+
+static __inline__ int masq_tcp_state_idx(struct tcphdr *th, int state_off)
{
/*
- * [0-3]: input states, [4-7]: output.
+ * [0-3]: input states, [4-7]: output, [8-11] input only states.
*/
- if (output)
- output=4;
-
if (th->rst)
- return output+3;
+ return state_off+3;
if (th->syn)
- return output+0;
+ return state_off+0;
if (th->fin)
- return output+1;
+ return state_off+1;
if (th->ack)
- return output+2;
+ return state_off+2;
return -1;
}
-
static int masq_set_state_timeout(struct ip_masq *ms, int state)
{
struct ip_masq_timeout_table *mstim = ms->timeout_table;
@@ -216,14 +234,26 @@
return state;
}
-static int masq_tcp_state(struct ip_masq *ms, int output, struct tcphdr *th)
+static int masq_tcp_state(struct ip_masq *ms, int state_off, struct tcphdr *th)
{
int state_idx;
int new_state = IP_MASQ_S_CLOSE;
- if ((state_idx = masq_tcp_state_idx(th, output)) < 0) {
+#ifdef CONFIG_IP_MASQUERADE_VS
+ /*
+ * Update state offset to INPUT_ONLY if necessary
+ * or delete NO_OUTPUT flag if output packet detected
+ */
+ if (ms->flags & IP_MASQ_F_VS_NO_OUTPUT) {
+ if (state_off == MASQ_STATE_OUTPUT)
+ ms->flags &= ~IP_MASQ_F_VS_NO_OUTPUT;
+ else state_off = MASQ_STATE_INPUT_ONLY;
+ }
+#endif
+
+ if ((state_idx = masq_tcp_state_idx(th, state_off)) < 0) {
IP_MASQ_DEBUG(1, "masq_state_idx(%d)=%d!!!\n",
- output, state_idx);
+ state_off, state_idx);
goto tcp_state_out;
}
@@ -233,7 +263,7 @@
if (new_state!=ms->state)
IP_MASQ_DEBUG(1, "%s %s [%c%c%c%c] %08lX:%04X-%08lX:%04X state: %s->%s\n",
masq_proto_name(ms->protocol),
- output? "output" : "input ",
+ (state_off==MASQ_STATE_OUTPUT) ? "output " : "input ",
th->syn? 'S' : '.',
th->fin? 'F' : '.',
th->ack? 'A' : '.',
@@ -242,6 +272,15 @@
ntohl(ms->daddr), ntohs(ms->dport),
ip_masq_state_name(ms->state),
ip_masq_state_name(new_state));
+
+#ifdef CONFIG_IP_MASQUERADE_VS
+ /*
+ * Increase/Decrease the active connection counter and
+ * set ms->flags according to ms->state and new_state.
+ */
+ ip_vs_set_state(ms, new_state);
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
return masq_set_state_timeout(ms, new_state);
}
@@ -249,7 +288,7 @@
/*
* Handle state transitions
*/
-static int masq_set_state(struct ip_masq *ms, int output, struct iphdr *iph, void *tp)
+static int masq_set_state(struct ip_masq *ms, int state_off, struct iphdr *iph, void *tp)
{
switch (iph->protocol) {
case IPPROTO_ICMP:
@@ -257,7 +296,7 @@
case IPPROTO_UDP:
return masq_set_state_timeout(ms, IP_MASQ_S_UDP);
case IPPROTO_TCP:
- return masq_tcp_state(ms, output, tp);
+ return masq_tcp_state(ms, state_off, tp);
}
return -1;
}
@@ -356,6 +395,9 @@
EXPORT_SYMBOL(ip_masq_get_debug_level);
EXPORT_SYMBOL(ip_masq_new);
+#ifdef CONFIG_IP_MASQUERADE_VS
+EXPORT_SYMBOL(ip_masq_new_vs);
+#endif /* CONFIG_IP_MASQUERADE_VS */
EXPORT_SYMBOL(ip_masq_listen);
EXPORT_SYMBOL(ip_masq_free_ports);
EXPORT_SYMBOL(ip_masq_out_get);
@@ -424,9 +466,17 @@
{
if (tout) {
ms->timer.expires = jiffies+tout;
+#ifdef CONFIG_IP_MASQUERADE_VS
+ add_sltimer(&ms->timer);
+#else
add_timer(&ms->timer);
+#endif
} else {
+#ifdef CONFIG_IP_MASQUERADE_VS
+ del_sltimer(&ms->timer);
+#else
del_timer(&ms->timer);
+#endif
}
}
@@ -742,6 +792,10 @@
struct ip_masq *ms;
read_lock(&__ip_masq_lock);
+#ifdef CONFIG_IP_MASQUERADE_VS
+ ms = ip_vs_out_get(protocol, s_addr, s_port, d_addr, d_port);
+ if (ms == NULL)
+#endif /* CONFIG_IP_MASQUERADE_VS */
ms = __ip_masq_out_get(protocol, s_addr, s_port, d_addr, d_port);
read_unlock(&__ip_masq_lock);
@@ -755,7 +809,11 @@
struct ip_masq *ms;
read_lock(&__ip_masq_lock);
- ms = __ip_masq_in_get(protocol, s_addr, s_port, d_addr, d_port);
+#ifdef CONFIG_IP_MASQUERADE_VS
+ ms = ip_vs_in_get(protocol, s_addr, s_port, d_addr, d_port);
+ if (ms == NULL)
+#endif /* CONFIG_IP_MASQUERADE_VS */
+ ms = __ip_masq_in_get(protocol, s_addr, s_port, d_addr, d_port);
read_unlock(&__ip_masq_lock);
if (ms)
@@ -825,6 +883,15 @@
if (ms->control)
ip_masq_control_del(ms);
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (ms->flags & IP_MASQ_F_VS) {
+ if (ip_vs_unhash(ms)) {
+ ip_vs_unbind_masq(ms);
+ ip_masq_unbind_app(ms);
+ }
+ }
+ else
+#endif /* CONFIG_IP_MASQUERADE_VS */
if (ip_masq_unhash(ms)) {
if (ms->flags&IP_MASQ_F_MPORT) {
atomic_dec(&mport_count);
@@ -1065,6 +1132,81 @@
return NULL;
}
+
+#ifdef CONFIG_IP_MASQUERADE_VS
+/*
+ * Create a new masquerade entry for IPVS, all parameters {maddr,
+ * mport, saddr, sport, daddr, dport, mflags} are known. No need
+ * to allocate a free mport. And, hash it into the ip_vs_table.
+ *
+ * Be careful, it can be called from u-space
+ */
+
+struct ip_masq * ip_masq_new_vs(int proto, __u32 maddr, __u16 mport, __u32 saddr, __u16 sport, __u32 daddr, __u16 dport, unsigned mflags)
+{
+ struct ip_masq *ms;
+ static int n_fails = 0;
+ int prio;
+
+ prio = (mflags&IP_MASQ_F_USER) ? GFP_KERNEL : GFP_ATOMIC;
+
+ ms = (struct ip_masq *) kmalloc(sizeof(struct ip_masq), prio);
+ if (ms == NULL) {
+ if (++n_fails < 5)
+ IP_VS_ERR("ip_masq_new_vs(proto=%s): no memory available.\n",
+ masq_proto_name(proto));
+ return NULL;
+ }
+ MOD_INC_USE_COUNT;
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,14)
+ sysctl_ip_always_defrag++;
+#endif
+ memset(ms, 0, sizeof(*ms));
+ INIT_LIST_HEAD(&ms->s_list);
+ INIT_LIST_HEAD(&ms->m_list);
+ INIT_LIST_HEAD(&ms->d_list);
+ init_timer(&ms->timer);
+ ms->timer.data = (unsigned long)ms;
+ ms->timer.function = masq_expire;
+ ms->protocol = proto;
+ ms->saddr = saddr;
+ ms->sport = sport;
+ ms->daddr = daddr;
+ ms->dport = dport;
+ ms->maddr = maddr;
+ ms->mport = mport;
+ ms->flags = mflags;
+ ms->app_data = NULL;
+ ms->control = NULL;
+
+ atomic_set(&ms->n_control,0);
+ atomic_set(&ms->refcnt,0);
+
+ if (mflags & IP_MASQ_F_USER)
+ write_lock_bh(&__ip_masq_lock);
+ else
+ write_lock(&__ip_masq_lock);
+
+ /*
+ * Hash it in the ip_vs_table
+ */
+ ip_vs_hash(ms);
+
+ if (mflags & IP_MASQ_F_USER)
+ write_unlock_bh(&__ip_masq_lock);
+ else
+ write_unlock(&__ip_masq_lock);
+
+ ip_masq_bind_app(ms);
+ n_fails = 0;
+ atomic_inc(&ms->refcnt);
+ masq_set_state_timeout(ms, IP_MASQ_S_NONE);
+ return ms;
+}
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
+
/*
* Get transport protocol data offset, check against size
* return:
@@ -1361,11 +1503,11 @@
IP_MASQ_DEBUG(2, "O-routed from %08lX:%04X with masq.addr %08lX\n",
ntohl(ms->maddr),ntohs(ms->mport),ntohl(maddr));
- masq_set_state(ms, 1, iph, h.portp);
+ masq_set_state(ms, MASQ_STATE_OUTPUT, iph, h.portp);
ip_masq_put(ms);
return 0;
- }
+}
/*
* Restore original addresses and ports in the original IP
@@ -1524,7 +1666,7 @@
ntohs(icmp_id(icmph)),
icmph->type);
- masq_set_state(ms, 1, iph, icmph);
+ masq_set_state(ms, MASQ_STATE_OUTPUT, iph, icmph);
ip_masq_put(ms);
return 1;
@@ -1643,11 +1785,23 @@
pptr[1],
ciph->saddr,
pptr[0]);
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (ms == NULL)
+ ms = ip_vs_out_get(ciph->protocol,
+ ciph->daddr, pptr[1],
+ ciph->saddr, pptr[0]);
+#endif /* CONFIG_IP_MASQUERADE_VS */
read_unlock(&__ip_masq_lock);
if (ms == NULL)
return 0;
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (IP_MASQ_VS_FWD(ms) != 0) {
+ IP_VS_INFO("shouldn't get here, because tun/dr is on the half connection\n");
+ }
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
/* Now we do real damage to this packet...! */
/* First change the source IP address, and recalc checksum */
iph->saddr = ms->maddr;
@@ -1770,7 +1924,7 @@
ntohs(icmp_id(icmph)),
icmph->type);
- masq_set_state(ms, 0, iph, icmph);
+ masq_set_state(ms, MASQ_STATE_INPUT, iph, icmph);
ip_masq_put(ms);
return 1;
@@ -1863,8 +2017,12 @@
* *outgoing* so the ports are reversed (and addresses)
*/
pptr = (__u16 *)&(((char *)ciph)[ciph->ihl*4]);
- if (ntohs(pptr[0]) < PORT_MASQ_BEGIN ||
- ntohs(pptr[0]) > PORT_MASQ_END)
+ if ((ntohs(pptr[0]) < PORT_MASQ_BEGIN ||
+ ntohs(pptr[0]) > PORT_MASQ_END)
+#ifdef CONFIG_IP_MASQUERADE_VS
+ && !ip_vs_lookup_service(ciph->protocol, ciph->saddr, pptr[0])
+#endif /* CONFIG_IP_MASQUERADE_VS */
+ )
return 0;
/* Ensure the checksum is correct */
@@ -1889,15 +2047,30 @@
pptr[1],
ciph->saddr,
pptr[0]);
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (ms == NULL)
+ ms = ip_vs_in_get(ciph->protocol,
+ ciph->daddr, pptr[1],
+ ciph->saddr, pptr[0]);
+#endif /* CONFIG_IP_MASQUERADE_VS */
read_unlock(&__ip_masq_lock);
if (ms == NULL)
return 0;
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (IP_MASQ_VS_FWD(ms) != 0) {
+ int ret = ip_vs_forward(skb, ms);
+ __ip_masq_put(ms);
+ return ret;
+ }
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
if ((skb=masq_skb_cow(skb_p, &iph, (unsigned char**)&icmph)) == NULL) {
__ip_masq_put(ms);
return -1;
}
+
ciph = (struct iphdr *) (icmph + 1);
pptr = (__u16 *)&(((char *)ciph)[ciph->ihl*4]);
@@ -1947,7 +2120,10 @@
int csum = 0;
int csum_ok = 0;
__u32 maddr;
-
+#ifdef CONFIG_IP_MASQUERADE_VS
+ struct ip_vs_service *svc = NULL;
+#endif
+
/*
* Big tappo: only PACKET_HOST (nor loopback neither mcasts)
* ... don't know why 1st test DOES NOT include 2nd (?)
@@ -1988,13 +2164,20 @@
return(ip_fw_demasq_icmp(skb_p));
case IPPROTO_TCP:
case IPPROTO_UDP:
- /*
+ /*
* Make sure packet is in the masq range
* ... or some mod-ule relaxes input range
* ... or there is still some `special' mport opened
*/
+#ifdef CONFIG_IP_MASQUERADE_VS
+ svc = ip_vs_lookup_service(iph->protocol, maddr, h.portp[1]);
+ if (!svc &&
+ (ntohs(h.portp[1]) < PORT_MASQ_BEGIN
+ || ntohs(h.portp[1]) > PORT_MASQ_END)
+#else
if ((ntohs(h.portp[1]) < PORT_MASQ_BEGIN
|| ntohs(h.portp[1]) > PORT_MASQ_END)
+#endif /* CONFIG_IP_MASQUERADE_VS */
#ifdef CONFIG_IP_MASQUERADE_MOD
&& (ip_masq_mod_in_rule(skb, iph) != 1)
#endif
@@ -2036,9 +2219,7 @@
return 0;
}
-
-
- IP_MASQ_DEBUG(2, "Incoming %s %08lX:%04X -> %08lX:%04X\n",
+ IP_MASQ_DEBUG(2, "Incoming %s %08lX:%04X -> %08lX:%04X\n",
masq_proto_name(iph->protocol),
ntohl(iph->saddr), ntohs(h.portp[0]),
ntohl(iph->daddr), ntohs(h.portp[1]));
@@ -2046,9 +2227,23 @@
/*
* reroute to original host:port if found...
*/
-
ms = ip_masq_in_get_iph(iph);
+#ifdef CONFIG_IP_MASQUERADE_VS
+ /*
+ * Checking the server status
+ */
+ if (ms && ms->dest && !(ms->dest->flags & IP_VS_DEST_F_AVAILABLE)) {
+ /*
+ * If the dest is not avaiable, don't restart the timer
+ * of the packet, but silently drop it.
+ */
+ add_sltimer(&ms->timer);
+ __ip_masq_put(ms);
+ return -1;
+ }
+#endif
+
/*
* Give additional modules a chance to create an entry
*/
@@ -2062,6 +2257,19 @@
ip_masq_mod_in_update(skb, iph, ms);
#endif
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (!ms &&
+ (h.th->syn || (iph->protocol!=IPPROTO_TCP)) && svc) {
+ /*
+ * Let the virtual server select a real server
+ * for the incomming connection, and create a
+ * masquerading entry.
+ */
+ ms = ip_vs_schedule(svc, iph);
+ if (!ms)
+ return ip_vs_leave(svc, skb);
+ }
+#endif /* CONFIG_IP_MASQUERADE_VS */
if (ms != NULL)
{
@@ -2114,13 +2322,39 @@
}
}
+
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (IP_MASQ_VS_FWD(ms) != 0) {
+ int ret;
+
+ /*
+ * Sorry for setting state of masq entry so early
+ * no matter whether the packet is forwarded
+ * successfully or not, because ip_vs_forward may
+ * have already released the skb. Although it
+ * brokes the original sematics, it won't lead to
+ * serious errors. We look forward to fixing it
+ * under the Rusty's netfilter framework both for
+ * correctness and modularization.
+ */
+ masq_set_state(ms, MASQ_STATE_INPUT, iph, h.portp);
+
+ ret = ip_vs_forward(skb, ms);
+ ip_masq_put(ms);
+ return ret;
+ }
+
+ IP_VS_DBG("masquerading packet...\n");
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
if ((skb=masq_skb_cow(skb_p, &iph, &h.raw)) == NULL) {
ip_masq_put(ms);
return -1;
}
+
iph->daddr = ms->saddr;
h.portp[1] = ms->sport;
-
+
/*
* Invalidate csum saving if tunnel has masq helper
*/
@@ -2177,11 +2411,11 @@
h.uh->check = 0xFFFF;
break;
}
- ip_send_check(iph);
+ ip_send_check(iph);
IP_MASQ_DEBUG(2, "I-routed to %08lX:%04X\n",ntohl(iph->daddr),ntohs(h.portp[1]));
- masq_set_state (ms, 0, iph, h.portp);
+ masq_set_state(ms, MASQ_STATE_INPUT, iph, h.portp);
ip_masq_put(ms);
return 1;
@@ -2296,7 +2530,6 @@
len += sprintf(buffer+len, "%-127s\n", temp);
if(len >= length) {
-
read_unlock_bh(&__ip_masq_lock);
goto done;
}
@@ -2304,9 +2537,52 @@
read_unlock_bh(&__ip_masq_lock);
}
-done:
+#ifdef CONFIG_IP_MASQUERADE_VS
+ for(idx = 0; idx < IP_VS_TAB_SIZE; idx++)
+ {
+ /*
+ * Lock is actually only need in next loop
+ * we are called from uspace: must stop bh.
+ */
+ read_lock_bh(&__ip_masq_lock);
+
+ l = &ip_vs_table[idx];
+ for (e=l->next; e!=l; e=e->next) {
+ ms = list_entry(e, struct ip_masq, m_list);
+ pos += 128;
+ if (pos <= offset) {
+ len = 0;
+ continue;
+ }
+
+ /*
+ * We have locked the tables, no need to del/add timers
+ * nor cli() 8)
+ */
+
+ sprintf(temp,"%s %08X:%04X %08X:%04X %04X %08X %6d %6d %7lu",
+ masq_proto_name(ms->protocol),
+ ntohl(ms->saddr), ntohs(ms->sport),
+ ntohl(ms->daddr), ntohs(ms->dport),
+ ntohs(ms->mport),
+ ms->out_seq.init_seq,
+ ms->out_seq.delta,
+ ms->out_seq.previous_delta,
+ ms->timer.expires-jiffies);
+ len += sprintf(buffer+len, "%-127s\n", temp);
+
+ if(len >= length) {
+ read_unlock_bh(&__ip_masq_lock);
+ goto done;
+ }
+ }
+ read_unlock_bh(&__ip_masq_lock);
+ }
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
+done:
begin = len - (pos - offset);
*start = buffer + begin;
len -= begin;
@@ -2414,6 +2690,11 @@
ret = ip_masq_mod_ctl(optname, &masq_ctl, optlen);
break;
#endif
+#ifdef CONFIG_IP_MASQUERADE_VS
+ case IP_MASQ_TARGET_VS:
+ ret = ip_vs_ctl(optname, &masq_ctl, optlen);
+ break;
+#endif
}
/*
@@ -2533,7 +2814,7 @@
(char *) IPPROTO_ICMP,
ip_masq_user_info
});
-#endif
+#endif /* CONFIG_PROC_FS */
#ifdef CONFIG_IP_MASQUERADE_IPAUTOFW
ip_autofw_init();
#endif
@@ -2542,6 +2823,9 @@
#endif
#ifdef CONFIG_IP_MASQUERADE_MFW
ip_mfw_init();
+#endif
+#ifdef CONFIG_IP_MASQUERADE_VS
+ ip_vs_init();
#endif
ip_masq_app_init();
diff -urN linux-2.2.13/net/ipv4/ip_masq_app.c linux-2.2.13-vs-0.9/net/ipv4/ip_masq_app.c
--- linux-2.2.13/net/ipv4/ip_masq_app.c Mon Oct 5 01:21:44 1998
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_masq_app.c Tue Aug 31 10:02:52 1999
@@ -189,6 +189,14 @@
/* #endif */
#endif
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (mapp == NULL)
+ /*
+ * check for virtual service.
+ */
+ mapp = ip_masq_app_get(ms->protocol, ms->mport);
+#endif
+
if (mapp != NULL) {
/*
* don't allow binding if already bound
diff -urN linux-2.2.13/net/ipv4/ip_masq_ftp.c linux-2.2.13-vs-0.9/net/ipv4/ip_masq_ftp.c
--- linux-2.2.13/net/ipv4/ip_masq_ftp.c Tue Oct 6 03:28:08 1998
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_masq_ftp.c Tue Aug 31 10:02:52 1999
@@ -16,6 +16,7 @@
* Juan Jose Ciarlante : Litl bits for 2.1
* Juan Jose Ciarlante : use ip_masq_listen()
* Juan Jose Ciarlante : use private app_data for own flag(s)
+ * Wensong Zhang : Added virtual server support
*
*
*
@@ -73,6 +74,10 @@
/* Dummy variable */
static int masq_ftp_pasv;
+#ifdef CONFIG_IP_MASQUERADE_VS
+static int ipvs_ftp_pasv; /* dummy variable for IPVS ftp passive */
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
static int
masq_ftp_init_1 (struct ip_masq_app *mapp, struct ip_masq *ms)
{
@@ -102,15 +107,122 @@
unsigned buf_len;
int diff;
+#ifdef CONFIG_IP_MASQUERADE_VS
+ if (ms->app_data == &ipvs_ftp_pasv) {
+ skb = *skb_p;
+ iph = skb->nh.iph;
+ th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
+ data = (char *)&th[1];
+ data_limit = skb->h.raw + skb->len;
+
+ while (data < data_limit && *data != ' ')
+ ++data;
+ while (data < data_limit && *data == ' ')
+ ++data;
+ data += 22;
+ p = data+1;
+ if (data >= data_limit || *data != '(')
+ return 0;
+ p1 = simple_strtoul(data+1, &data, 10);
+ if (data >= data_limit || *data != ',')
+ return 0;
+ p2 = simple_strtoul(data+1, &data, 10);
+ if (data >= data_limit || *data != ',')
+ return 0;
+ p3 = simple_strtoul(data+1, &data, 10);
+ if (data >= data_limit || *data != ',')
+ return 0;
+ p4 = simple_strtoul(data+1, &data, 10);
+ if (data >= data_limit || *data != ',')
+ return 0;
+ p5 = simple_strtoul(data+1, &data, 10);
+ if (data >= data_limit || *data != ',')
+ return 0;
+ p6 = simple_strtoul(data+1, &data, 10);
+ if (data >= data_limit || *data != ')')
+ return 0;
+
+ from = (p1<<24) | (p2<<16) | (p3<<8) | p4;
+ port = (p5<<8) | p6;
+
+ /*
+ * Now update or create an masquerade entry for it
+ */
+ IP_MASQ_DEBUG(1-debug, "PASV response %lX:%X %X:%X detected\n", ntohl(ms->saddr), 0, from, port);
+
+ n_ms = ip_masq_out_get(iph->protocol,
+ htonl(from), htons(port),
+ ms->daddr, 0);
+ if (!n_ms) {
+ n_ms = ip_masq_new(IPPROTO_TCP,
+ maddr, htons(port),
+ htonl(from), htons(port),
+ ms->daddr, 0,
+ IP_MASQ_F_NO_DPORT);
+
+ if (n_ms==NULL)
+ return 0;
+ ip_masq_control_add(n_ms, ms);
+ }
+
+ /*
+ * Replace the old passive address with the new one
+ */
+ from = ntohl(n_ms->maddr);
+ port = ntohs(n_ms->mport);
+ sprintf(buf,"%d,%d,%d,%d,%d,%d",
+ from>>24&255,from>>16&255,from>>8&255,from&255,
+ port>>8&255,port&255);
+ buf_len = strlen(buf);
+
+ IP_MASQ_DEBUG(1-debug, "new PORT %X:%X\n",from,port);
+
+ /*
+ * Calculate required delta-offset to keep TCP happy
+ */
+
+ diff = buf_len - (data-p);
+
+ /*
+ * No shift.
+ */
+
+ if (diff==0) {
+ /*
+ * simple case, just replace the old PORT cmd
+ */
+ memcpy(p,buf,buf_len);
+ } else {
+
+ *skb_p = ip_masq_skb_replace(skb, GFP_ATOMIC, p, data-p, buf, buf_len);
+ }
+
+ ms->app_data = NULL;
+ ip_masq_put(n_ms);
+
+ return diff;
+ }
+#endif /* CONFIG_IP_MASQUERADE_VS */
+
skb = *skb_p;
iph = skb->nh.iph;
th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
data = (char *)&th[1];
data_limit = skb->h.raw + skb->len - 18;
- if (skb->len >= 6 && (memcmp(data, "PASV\r\n", 6) == 0 || memcmp(data, "pasv\r\n", 6) == 0))
+ if (skb->len >= 6 && (memcmp(data, "PASV\r\n", 6) == 0 || memcmp(data, "pasv\r\n", 6) == 0)) {
ms->app_data = &masq_ftp_pasv;
+ /*
+ * Note that I never got the following print out.
+ * This passive detection never works, but it doesn't affect
+ * accessing passive ftp service from inside to outside
+ * through masquerading, that's this bug was not found
+ * for such a long time.
+ */
+ printk(KERN_INFO "ip_masq_ftp OUT: got PASV\n");
+ }
+
while (data < data_limit)
{
if (memcmp(data,"PORT ",5) && memcmp(data,"port ",5))
@@ -150,8 +262,8 @@
IP_MASQ_DEBUG(1-debug, "protocol %d %lX:%X %X:%X\n", iph->protocol, htonl(from), htons(port), iph->daddr, 0);
n_ms = ip_masq_out_get(iph->protocol,
- htonl(from), htons(port),
- iph->daddr, 0);
+ htonl(from), htons(port),
+ iph->daddr, 0);
if (!n_ms) {
n_ms = ip_masq_new(IPPROTO_TCP,
maddr, 0,
@@ -204,6 +316,7 @@
return diff;
}
+
return 0;
}
@@ -237,6 +350,102 @@
__u32 to;
__u16 port;
struct ip_masq *n_ms;
+
+#ifdef CONFIG_IP_MASQUERADE_VS
+ char *p;
+
+ /*
+ * Detecting whether it is passive
+ */
+ skb = *skb_p;
+ iph = skb->nh.iph;
+ th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]);
+ data = (char *)&th[1];
+ data_limit = skb->h.raw + skb->len;
+
+ while (data < data_limit) {
+ if (memcmp(data, "PASV\r\n", 6) == 0 || memcmp(data, "pasv\r\n", 6) == 0) {
+ IP_MASQ_DEBUG(1-debug, "got PASV at %d of %d\n",
+ data-(char *)&th[1], data_limit-(char *)&th[1]);
+ ms->app_data = &ipvs_ftp_pasv;
+
+ return 0;
+ }
+ data++;
+ }
+
+ /*
+ * To support virtual FTP server, the scenerio is as follows:
+ * FTP client ----> Load Balancer ----> FTP server
+ * First detect the port number in the application data,
+ * then create a new masquerading entry for the coming data
+ * connection.
+ */
+ data = (char *)&th[1];
+ data_limit = skb->h.raw + skb->len - 18;
+
+ while (data < data_limit)
+ {
+ if (memcmp(data,"PORT ",5) && memcmp(data,"port ",5))
+ {
+ data ++;
+ continue;
+ }
+ p = data+5;
+ p1 = simple_strtoul(data+5,&data,10);
+ if (*data!=',')
+ continue;
+ p2 = simple_strtoul(data+1,&data,10);
+ if (*data!=',')
+ continue;
+ p3 = simple_strtoul(data+1,&data,10);
+ if (*data!=',')
+ continue;
+ p4 = simple_strtoul(data+1,&data,10);
+ if (*data!=',')
+ continue;
+ p5 = simple_strtoul(data+1,&data,10);
+ if (*data!=',')
+ continue;
+ p6 = simple_strtoul(data+1,&data,10);
+ if (*data!='\r' && *data!='\n')
+ continue;
+
+ to = (p1<<24) | (p2<<16) | (p3<<8) | p4;
+ port = (p5<<8) | p6;
+
+ IP_MASQ_DEBUG(1-debug, "PORT %X:%X detected\n",to,port);
+
+ /*
+ * Now update or create an masquerade entry for it
+ */
+
+ IP_MASQ_DEBUG(1-debug, "protocol %d %lX:%X %X:%X\n", iph->protocol, htonl(to), htons(port), iph->daddr, 0);
+
+ n_ms = ip_masq_in_get(iph->protocol,
+ htonl(to), htons(port),
+ iph->daddr, 0);
+ if (!n_ms) {
+ n_ms = ip_masq_new(IPPROTO_TCP,
+ maddr, htons(ntohs(ms->mport)-1),
+ ms->saddr, htons(ntohs(ms->sport)-1),
+ htonl(to), htons(port),
+ 0);
+
+ if (n_ms==NULL)
+ return 0;
+ ip_masq_control_add(n_ms, ms);
+ }
+
+ /*
+ * Move tunnel to listen state
+ */
+ ip_masq_listen(n_ms);
+ ip_masq_put(n_ms);
+
+ return 0;
+ }
+#endif /* CONFIG_IP_MASQUERADE_VS */
if (ms->app_data != &masq_ftp_pasv)
return 0; /* quick exit if no outstanding PASV */
diff -urN linux-2.2.13/net/ipv4/ip_vs.c linux-2.2.13-vs-0.9/net/ipv4/ip_vs.c
--- linux-2.2.13/net/ipv4/ip_vs.c Thu Jan 1 08:00:00 1970
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_vs.c Wed Dec 22 21:21:53 1999
@@ -0,0 +1,2158 @@
+/*
+ * IPVS An implementation of the IP virtual server support for the
+ * LINUX operating system. IPVS is now implemented as a part
+ * of IP masquerading code. IPVS can be used to build a
+ * high-performance and highly available server based on a
+ * cluster of servers.
+ *
+ * Version: $Id: ip_vs.c,v 1.13 1999/12/22 13:21:53 wensong Exp $
+ *
+ * Authors: Wensong Zhang <wensong@iinchina.net>
+ * Peter Kese <peter.kese@ijs.si>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Changes:
+ * Wensong Zhang : fixed the overflow bug in ip_vs_procinfo
+ * Wensong Zhang : added editing dest and service functions
+ * Wensong Zhang : changed the names of some functions
+ * Wensong Zhang : fixed the unlocking bug in ip_vs_del_dest
+ * Wensong Zhang : added a separate hash table for IPVS
+ * Wensong Zhang : added slow timer for IPVS masq entries
+ * Julian Anastasov : fixed the number of active connections
+ * Wensong Zhang : added persistent port
+ * Wensong Zhang : fixed the incorrect lookup in hash table
+ * Wensong Zhang : added server status checking
+ * Wensong Zhang : fixed the incorrect slow timer vector layout
+ * Wensong Zhang : fixed the sltimer added twice bug of mst
+ * Julian Anastasov : fixed the IP_MASQ_F_VS_INACTIVE cleared bug after editing dest
+ * Wensong Zhang : added the inactive connection counter
+ * Wensong Zhang : changed the body of ip_vs_schedule
+ * Julian Anastasov : fixed the unlocking bug in ip_vs_schedule
+ * Julian Anastasov : fixed the uncounting bug in creating masqs by template
+ * Wensong Zhang : changed some condition orders for a bit performance
+ * Julian Anastasov : don't touch counters in ip_vs_unbind_masq for templates
+ * Wensong Zhang : added the hash table for virtual services
+ * Wensong Zhang : changed destination lists to d-linked lists
+ * Wensong Zhang : changed the scheduler list to the d-linked list
+ * Wensong Zhang : added new persistent service handling
+ * Julian Anastasov : fixed the counting bug in ip_vs_unbind_masq again
+ * (don't touch counters for templates)
+ * Wensong Zhang : changed some IP_VS_ERR to IP_VS_DBG in the ip_vs_tunnel_xmit
+ * Wensong Zhang : added different timeout support for persistent svc
+ * Wensong Zhang : fixed the bug that persistent svc cannot be edited
+ * Julian Anastasov : removed extra read_unlock in __ip_vs_lookup_service
+ * Julian Anastasov : changed not to restart template timers if dest is unavailable
+ * Julian Anastasov : added the destination trash
+ * Wensong Zhang : added the update_service call in ip_vs_del_dest
+ * Wensong Zhang : added the ip_vs_leave function
+ * Lars Marowsky-Bree : added persistence granularity support
+ * Julian Anastasov : changed some comestics things for debugging
+ * Wensong Zhang : use vmalloc to allocate big ipvs hash table
+ * Wensong Zhang : changed the tunneling/direct routing methods a little
+ * Julian Anastasov : fixed the return bug of ip_vs_leave(-2 instead of -3)
+ * Roberto Nibali : fixed the undefined variable bug in the IP_VS_DBG of ip_vs_dr_xmit
+ * Julian Anastasov : changed ICMP_PROT_UNREACH to ICMP_PORT_UNREACH in ip_vs_leave
+ * Wensong Zhang : added port zero support for persistent services
+ * Wensong Zhang : fixed the bug that virtual ftp service blocks other services not listed in ipvs table
+ * Wensong Zhang : invalidate a persistent template when its dest is unavailable
+ * Julian Anastasov : changed two IP_VS_ERR calls to IP_VS_DBG
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/vmalloc.h>
+#include <net/ip_masq.h>
+
+#include <linux/sysctl.h>
+#include <linux/ip_fw.h>
+#include <linux/ip_masq.h>
+#include <linux/proc_fs.h>
+
+#include <linux/inetdevice.h>
+#include <linux/ip.h>
+#include <net/icmp.h>
+#include <net/ip.h>
+#include <net/route.h>
+#include <net/ip_vs.h>
+
+#ifdef CONFIG_KMOD
+#include <linux/kmod.h>
+#endif
+
+EXPORT_SYMBOL(register_ip_vs_scheduler);
+EXPORT_SYMBOL(unregister_ip_vs_scheduler);
+EXPORT_SYMBOL(ip_vs_bind_masq);
+EXPORT_SYMBOL(ip_vs_unbind_masq);
+
+/*
+ * The following block implements slow timers for IPVS, most code is stolen
+ * from linux/kernel/sched.c
+ * Slow timer is used to avoid the overhead of cascading timers, when lots
+ * of masq entries (>50,000) are cluttered in the system.
+ */
+#define SHIFT_BITS 6
+#define TVN_BITS 8
+#define TVR_BITS 10
+#define TVN_SIZE (1 << TVN_BITS)
+#define TVR_SIZE (1 << TVR_BITS)
+#define TVN_MASK (TVN_SIZE - 1)
+#define TVR_MASK (TVR_SIZE - 1)
+
+struct sltimer_vec {
+ int index;
+ struct timer_list *vec[TVN_SIZE];
+};
+
+struct sltimer_vec_root {
+ int index;
+ struct timer_list *vec[TVR_SIZE];
+};
+
+static struct sltimer_vec sltv3 = { 0 };
+static struct sltimer_vec sltv2 = { 0 };
+static struct sltimer_vec_root sltv1 = { 0 };
+
+static struct sltimer_vec * const sltvecs[] = {
+ (struct sltimer_vec *)&sltv1, &sltv2, &sltv3
+};
+
+#define NOOF_SLTVECS (sizeof(sltvecs) / sizeof(sltvecs[0]))
+
+static unsigned long sltimer_jiffies = 0;
+
+static inline void insert_sltimer(struct timer_list *timer,
+ struct timer_list **vec, int idx)
+{
+ if ((timer->next = vec[idx]))
+ vec[idx]->prev = timer;
+ vec[idx] = timer;
+ timer->prev = (struct timer_list *)&vec[idx];
+}
+
+static inline void internal_add_sltimer(struct timer_list *timer)
+{
+ /*
+ * must be cli-ed when calling this
+ */
+ unsigned long expires = timer->expires;
+ unsigned long idx = (expires - sltimer_jiffies) >> SHIFT_BITS;
+
+ if (idx < TVR_SIZE) {
+ int i = (expires >> SHIFT_BITS) & TVR_MASK;
+ insert_sltimer(timer, sltv1.vec, i);
+ } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
+ int i = (expires >> (SHIFT_BITS+TVR_BITS)) & TVN_MASK;
+ insert_sltimer(timer, sltv2.vec, i);
+ } else if ((signed long) idx < 0) {
+ /*
+ * can happen if you add a timer with expires == jiffies,
+ * or you set a timer to go off in the past
+ */
+ insert_sltimer(timer, sltv1.vec, sltv1.index);
+ } else if (idx <= 0xffffffffUL) {
+ int i = (expires >> (SHIFT_BITS+TVR_BITS+TVN_BITS)) & TVN_MASK;
+ insert_sltimer(timer, sltv3.vec, i);
+ } else {
+ /* Can only get here on architectures with 64-bit jiffies */
+ timer->next = timer->prev = timer;
+ }
+}
+
+rwlock_t sltimerlist_lock = RW_LOCK_UNLOCKED;
+
+void add_sltimer(struct timer_list *timer)
+{
+ write_lock(&sltimerlist_lock);
+ if (timer->prev)
+ goto bug;
+ internal_add_sltimer(timer);
+out:
+ write_unlock(&sltimerlist_lock);
+ return;
+
+bug:
+ printk("bug: kernel sltimer added twice at %p.\n",
+ __builtin_return_address(0));
+ goto out;
+}
+
+static inline int detach_sltimer(struct timer_list *timer)
+{
+ struct timer_list *prev = timer->prev;
+ if (prev) {
+ struct timer_list *next = timer->next;
+ prev->next = next;
+ if (next)
+ next->prev = prev;
+ return 1;
+ }
+ return 0;
+}
+
+void mod_sltimer(struct timer_list *timer, unsigned long expires)
+{
+ write_lock(&sltimerlist_lock);
+ timer->expires = expires;
+ detach_sltimer(timer);
+ internal_add_sltimer(timer);
+ write_unlock(&sltimerlist_lock);
+}
+
+int del_sltimer(struct timer_list * timer)
+{
+ int ret;
+
+ write_lock(&sltimerlist_lock);
+ ret = detach_sltimer(timer);
+ timer->next = timer->prev = 0;
+ write_unlock(&sltimerlist_lock);
+ return ret;
+}
+
+
+static inline void cascade_sltimers(struct sltimer_vec *tv)
+{
+ /*
+ * cascade all the timers from tv up one level
+ */
+ struct timer_list *timer;
+ timer = tv->vec[tv->index];
+ /*
+ * We are removing _all_ timers from the list, so we don't have to
+ * detach them individually, just clear the list afterwards.
+ */
+ while (timer) {
+ struct timer_list *tmp = timer;
+ timer = timer->next;
+ internal_add_sltimer(tmp);
+ }
+ tv->vec[tv->index] = NULL;
+ tv->index = (tv->index + 1) & TVN_MASK;
+}
+
+static inline void run_sltimer_list(void)
+{
+ write_lock(&sltimerlist_lock);
+ while ((long)(jiffies - sltimer_jiffies) >= 0) {
+ struct timer_list *timer;
+ if (!sltv1.index) {
+ int n = 1;
+ do {
+ cascade_sltimers(sltvecs[n]);
+ } while (sltvecs[n]->index == 1 && ++n < NOOF_SLTVECS);
+ }
+ while ((timer = sltv1.vec[sltv1.index])) {
+ void (*fn)(unsigned long) = timer->function;
+ unsigned long data = timer->data;
+ detach_sltimer(timer);
+ timer->next = timer->prev = NULL;
+ write_unlock(&sltimerlist_lock);
+ fn(data);
+ write_lock(&sltimerlist_lock);
+ }
+ sltimer_jiffies += 1<<SHIFT_BITS;
+ sltv1.index = (sltv1.index + 1) & TVR_MASK;
+ }
+ write_unlock(&sltimerlist_lock);
+}
+
+static void sltimer_handler(unsigned long data);
+
+struct timer_list slow_timer = {
+ NULL, NULL,
+ 0, 0,
+ sltimer_handler,
+};
+
+/*
+ * Slow timer handler is activated every second
+ */
+#define SLTIMER_PERIOD 1*HZ
+
+void sltimer_handler(unsigned long data)
+{
+ run_sltimer_list();
+ mod_timer(&slow_timer, (jiffies + SLTIMER_PERIOD));
+}
+
+
+/*
+ * The port number of FTP service (in network order).
+ */
+__u16 ftpport;
+
+/*
+ * Lock for IPVS
+ */
+rwlock_t __ip_vs_lock = RW_LOCK_UNLOCKED;
+
+/*
+ * Hash table: for input and output packets lookups of IPVS
+ */
+#define IP_MASQ_NTABLES 3
+
+struct list_head *ip_vs_table;
+
+/*
+ * Hash table: for virtual service lookups
+ */
+#define IP_VS_SVC_TAB_BITS 8
+#define IP_VS_SVC_TAB_SIZE 256
+
+struct list_head ip_vs_svc_table[IP_VS_SVC_TAB_SIZE];
+
+/*
+ * IPVS scheduler list
+ */
+struct list_head ip_vs_schedulers;
+
+/*
+ * Trash for destinations
+ */
+struct list_head ip_vs_dest_trash;
+
+
+/*
+ * Register a scheduler in the scheduler list
+ */
+int register_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
+{
+ if (!scheduler) {
+ IP_VS_ERR("register_ip_vs_scheduler(): NULL arg\n");
+ return -EINVAL;
+ }
+
+ if (!scheduler->name) {
+ IP_MASQ_ERR("register_ip_vs_scheduler(): NULL scheduler_name\n");
+ return -EINVAL;
+ }
+
+ if (scheduler->n_list.next != &scheduler->n_list) {
+ IP_VS_ERR("register_ip_vs_scheduler(): scheduler already linked\n");
+ return -EINVAL;
+ }
+
+ /*
+ * Add it into the d-linked scheduler list
+ */
+ list_add(&scheduler->n_list, &ip_vs_schedulers);
+
+ return 0;
+}
+
+
+/*
+ * Unregister a scheduler in the scheduler list
+ */
+int unregister_ip_vs_scheduler(struct ip_vs_scheduler *scheduler)
+{
+ if (!scheduler) {
+ IP_MASQ_ERR( "unregister_ip_vs_scheduler(): NULL arg\n");
+ return -EINVAL;
+ }
+
+ /*
+ * Only allow unregistration if it is not referenced
+ */
+ if (atomic_read(&scheduler->refcnt)) {
+ IP_VS_ERR("unregister_ip_vs_scheduler(): is in use by %d guys. failed\n",
+ atomic_read(&scheduler->refcnt));
+ return -EINVAL;
+ }
+
+ if (scheduler->n_list.next == &scheduler->n_list) {
+ IP_VS_ERR("unregister_ip_vs_scheduler(): scheduler is not in the list. failed\n");
+ return -EINVAL;
+ }
+
+ /*
+ * Removed it from the d-linked scheduler list
+ */
+ list_del(&scheduler->n_list);
+
+ return 0;
+}
+
+
+/*
+ * Bind a service with a scheduler
+ * Must called with the __ip_vs_lock lock, and return bool.
+ */
+int ip_vs_bind_scheduler(struct ip_vs_service *svc,
+ struct ip_vs_scheduler *scheduler)
+{
+ if (svc == NULL) {
+ IP_VS_ERR("ip_vs_bind_scheduler(): svc arg NULL\n");
+ return -EINVAL;
+ }
+ if (scheduler == NULL) {
+ IP_VS_ERR("ip_vs_bind_scheduler(): scheduler arg NULL\n");
+ return -EINVAL;
+ }
+
+ svc->scheduler = scheduler;
+ atomic_inc(&scheduler->refcnt);
+
+ if(scheduler->init_service)
+ if(scheduler->init_service(svc) != 0) {
+ IP_VS_ERR("ip_vs_bind_scheduler(): init error\n");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+
+/*
+ * Unbind a service with its scheduler
+ * Must called with the __ip_vs_lock lock, and return bool.
+ */
+int ip_vs_unbind_scheduler(struct ip_vs_service *svc)
+{
+ struct ip_vs_scheduler *sched;
+
+ if (svc == NULL) {
+ IP_VS_ERR("ip_vs_unbind_scheduler(): svc arg NULL\n");
+ return -EINVAL;
+ }
+
+ sched = svc->scheduler;
+ if (sched == NULL) {
+ IP_VS_ERR("ip_vs_unbind_scheduler(): svc isn't bound\n");
+ return -EINVAL;
+ }
+
+ if(sched->done_service)
+ if(sched->done_service(svc) != 0) {
+ IP_VS_ERR("ip_vs_unbind_scheduler(): done error\n");
+ return -EINVAL;
+ }
+
+ atomic_dec(&sched->refcnt);
+ svc->scheduler = NULL;
+
+ return 0;
+}
+
+
+/*
+ * Get scheduler in the scheduler list by name
+ */
+struct ip_vs_scheduler * ip_vs_sched_getbyname(const char *sched_name)
+{
+ struct ip_vs_scheduler *sched;
+ struct list_head *l, *e;
+
+ IP_VS_DBG("ip_vs_sched_getbyname(): sched_name \"%s\"\n", sched_name);
+
+ read_lock_bh(&__ip_vs_lock);
+
+ l = &ip_vs_schedulers;
+ for (e=l->next; e!=l; e=e->next) {
+ sched = list_entry(e, struct ip_vs_scheduler, n_list);
+ if (strcmp(sched_name, sched->name)==0) {
+ /* HIT */
+ read_unlock_bh(&__ip_vs_lock);
+ return sched;
+ }
+ }
+
+ read_unlock_bh(&__ip_vs_lock);
+ return NULL;
+}
+
+
+/*
+ * Lookup scheduler and try to load it if it doesn't exist
+ */
+struct ip_vs_scheduler * ip_vs_lookup_scheduler(const char *sched_name)
+{
+ struct ip_vs_scheduler *sched;
+
+ /*
+ * Search for the scheduler by sched_name
+ */
+ sched = ip_vs_sched_getbyname(sched_name);
+
+ /*
+ * If scheduler not found, load the module and search again
+ */
+ if (sched == NULL) {
+ char module_name[IP_MASQ_TNAME_MAX+8];
+ sprintf(module_name,"ip_vs_%s",sched_name);
+#ifdef CONFIG_KMOD
+ request_module(module_name);
+#endif /* CONFIG_KMOD */
+ sched = ip_vs_sched_getbyname(sched_name);
+ }
+
+ return sched;
+}
+
+
+/*
+ * Returns hash value for IPVS masq entry
+ */
+
+static __inline__ unsigned
+ip_vs_hash_key(unsigned proto, __u32 addr, __u16 port)
+{
+ unsigned addrh = ntohl(addr);
+
+ return (proto^addrh^(addrh>>IP_VS_TAB_BITS)^ntohs(port))
+ & (IP_VS_TAB_SIZE-1);
+}
+
+
+/*
+ * Hashes ip_masq in ip_vs_table by proto,addr,port.
+ * should be called with locked tables.
+ * returns bool success.
+ */
+int ip_vs_hash(struct ip_masq *ms)
+{
+ unsigned hash;
+
+ if (ms->flags & IP_MASQ_F_HASHED) {
+ IP_VS_ERR("ip_vs_hash(): request for already hashed, "
+ "called from %p\n", __builtin_return_address(0));
+ return 0;
+ }
+
+ /*
+ * Note: because ip_masq_put sets masq expire only if its
+ * refcnt==IP_MASQ_NTABLES, otherwise the masq entry
+ * will never expire.
+ */
+ atomic_add(IP_MASQ_NTABLES, &ms->refcnt);
+
+ /*
+ * Hash by proto,d{addr,port},
+ * which are client address and port in IPVS.
+ */
+ hash = ip_vs_hash_key(ms->protocol, ms->daddr, ms->dport);
+ list_add(&ms->m_list, &ip_vs_table[hash]);
+
+ ms->flags |= IP_MASQ_F_HASHED;
+ return 1;
+}
+
+
+/*
+ * UNhashes ip_masq from ip_vs_table.
+ * should be called with locked tables.
+ * returns bool success.
+ */
+int ip_vs_unhash(struct ip_masq *ms)
+{
+ if (!(ms->flags & IP_MASQ_F_HASHED)) {
+ IP_VS_ERR("ip_vs_unhash(): request for unhash flagged, "
+ "called from %p\n", __builtin_return_address(0));
+ return 0;
+ }
+
+ /*
+ * Remove it from the list and decrease its reference counter.
+ */
+ list_del(&ms->m_list);
+ atomic_sub(IP_MASQ_NTABLES, &ms->refcnt);
+
+ ms->flags &= ~IP_MASQ_F_HASHED;
+ return 1;
+}
+
+
+/*
+ * Gets ip_masq associated with supplied parameters in the ip_vs_table.
+ * Called for pkts coming from OUTside-to-INside.
+ * s_addr, s_port: pkt source address (foreign host)
+ * d_addr, d_port: pkt dest address (load balancer)
+ * Caller must lock tables
+ */
+struct ip_masq * ip_vs_in_get(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
+{
+ unsigned hash;
+ struct ip_masq *ms;
+ struct list_head *l,*e;
+
+ hash = ip_vs_hash_key(protocol, s_addr, s_port);
+
+ l = &ip_vs_table[hash];
+ for (e=l->next; e!=l; e=e->next) {
+ ms = list_entry(e, struct ip_masq, m_list);
+ if (protocol==ms->protocol &&
+ d_addr==ms->maddr && d_port==ms->mport &&
+ s_addr==ms->daddr && s_port==ms->dport
+ ) {
+ /* HIT */
+ atomic_inc(&ms->refcnt);
+ return ms;
+ }
+ }
+
+ IP_VS_DBG("look/in %d %08X:%04hX->%08X:%04hX fail\n",
+ protocol,
+ s_addr,
+ s_port,
+ d_addr,
+ d_port);
+
+ return NULL;
+}
+
+
+/*
+ * Gets ip_masq associated with supplied parameters in the ip_vs_table.
+ * Called for pkts coming from inside-to-OUTside.
+ * s_addr, s_port: pkt source address (inside host)
+ * d_addr, d_port: pkt dest address (foreigh host)
+ * Caller must lock tables
+ */
+struct ip_masq * ip_vs_out_get(int protocol, __u32 s_addr, __u16 s_port, __u32 d_addr, __u16 d_port)
+{
+ unsigned hash;
+ struct ip_masq *ms;
+ struct list_head *l,*e;
+
+ /*
+ * Check for "full" addressed entries
+ */
+ hash = ip_vs_hash_key(protocol, d_addr, d_port);
+
+ l = &ip_vs_table[hash];
+ for (e=l->next; e!=l; e=e->next) {
+ ms = list_entry(e, struct ip_masq, m_list);
+ if (protocol == ms->protocol &&
+ s_addr == ms->saddr && s_port == ms->sport &&
+ d_addr == ms->daddr && d_port == ms->dport
+ ) {
+ /* HIT */
+ atomic_inc(&ms->refcnt);
+ return ms;
+ }
+ }
+
+ IP_VS_DBG("lk/out %d %08X:%04hX->%08X:%04hX fail\n",
+ protocol,
+ s_addr,
+ s_port,
+ d_addr,
+ d_port);
+
+ return NULL;
+}
+
+
+/*
+ * Returns hash value for virtual service
+ */
+static __inline__ unsigned
+ip_vs_svc_hashkey(unsigned proto, __u32 addr, __u16 port)
+{
+ register unsigned porth = ntohs(port);
+
+ return (proto^ntohl(addr)^(porth>>IP_VS_SVC_TAB_BITS)^porth)
+ & (IP_VS_SVC_TAB_SIZE-1);
+}
+
+
+/*
+ * Hashes ip_vs_service in ip_vs_svc_table by proto,addr,port.
+ * should be called with locked tables.
+ * returns bool success.
+ */
+int ip_vs_svc_hash(struct ip_vs_service *svc)
+{
+ unsigned hash;
+
+ if (svc->flags & IP_VS_SVC_F_HASHED) {
+ IP_VS_ERR("ip_vs_svc_hash(): request for already hashed, "
+ "called from %p\n", __builtin_return_address(0));
+ return 0;
+ }
+
+ /*
+ * Hash by proto,addr,port,
+ * which are the parameters of the virtual service.
+ */
+ hash = ip_vs_svc_hashkey(svc->protocol, svc->addr, svc->port);
+ list_add(&svc->n_list, &ip_vs_svc_table[hash]);
+
+ svc->flags |= IP_VS_SVC_F_HASHED;
+ return 1;
+}
+
+
+/*
+ * UNhashes ip_vs_service from ip_vs_svc_table.
+ * should be called with locked tables.
+ * returns bool success.
+ */
+int ip_vs_svc_unhash(struct ip_vs_service *svc)
+{
+ if (!(svc->flags & IP_VS_SVC_F_HASHED)) {
+ IP_VS_ERR("ip_vs_svc_unhash(): request for unhash flagged, "
+ "called from %p\n", __builtin_return_address(0));
+ return 0;
+ }
+
+ /*
+ * Remove it from the ip_vs_svc_table table.
+ */
+ list_del(&svc->n_list);
+
+ svc->flags &= ~IP_VS_SVC_F_HASHED;
+ return 1;
+}
+
+
+/*
+ * Lookup service by {proto,addr,port} in the service table.
+ */
+struct ip_vs_service * __ip_vs_lookup_service(__u16 protocol,
+ __u32 vaddr, __u16 vport)
+{
+ unsigned hash;
+ struct ip_vs_service *svc;
+ struct list_head *l,*e;
+
+ /*
+ * Check for "full" addressed entries
+ */
+ hash = ip_vs_svc_hashkey(protocol, vaddr, vport);
+
+ l = &ip_vs_svc_table[hash];
+ for (e=l->next; e!=l; e=e->next) {
+ svc = list_entry(e, struct ip_vs_service, n_list);
+ if ((svc->addr == vaddr)
+ && (svc->port == vport)) {
+ /* HIT */
+ return svc;
+ }
+ }
+
+ return NULL;
+}
+
+struct ip_vs_service *ip_vs_lookup_service(__u16 protocol,
+ __u32 vaddr, __u16 vport)
+{
+ struct ip_vs_service *svc;
+
+ read_lock(&__ip_vs_lock);
+
+ /*
+ * Check for "full" addressed entries
+ */
+ svc = __ip_vs_lookup_service(protocol, vaddr, vport);
+
+ if ((svc == NULL) && (protocol == IPPROTO_TCP)) {
+ /*
+ * Check if ftp service entry exists, the packet might
+ * belong to FTP data connections.
+ */
+ svc = __ip_vs_lookup_service(protocol, vaddr, ftpport);
+ }
+
+ if (svc == NULL) {
+ /*
+ * Check if the catch-all port (port zero) exists
+ */
+ svc = __ip_vs_lookup_service(protocol, vaddr, 0);
+ }
+
+#ifdef CONFIG_IP_VS_DEBUG
+ if (svc == NULL)
+ IP_VS_DBG("lookup_service %d %08X:%04X fail\n",
+ protocol, ntohl(vaddr), ntohs(vport));
+ else
+ IP_VS_DBG("lookup_service %d %08X:%04X succeed\n",
+ protocol, ntohl(vaddr), ntohs(vport));
+#endif
+
+ read_unlock(&__ip_vs_lock);
+ return svc;
+}
+
+
+/*
+ * Lookup destination by {addr,port} in the given service
+ */
+struct ip_vs_dest * ip_vs_lookup_dest(struct ip_vs_service *svc,
+ __u32 daddr, __u16 dport)
+{
+ struct ip_vs_dest *dest;
+ struct list_head *l, *e;
+
+ read_lock_bh(&__ip_vs_lock);
+
+ /*
+ * Find the destination for the given service
+ */
+ l = &svc->destinations;
+ for (e=l->next; e!=l; e=e->next) {
+ dest = list_entry(e, struct ip_vs_dest, n_list);
+ if ((dest->addr == daddr) && (dest->port == dport)) {
+ /* HIT */
+ read_unlock_bh(&__ip_vs_lock);
+ return dest;
+ }
+ }
+
+ read_unlock_bh(&__ip_vs_lock);
+ return NULL;
+}
+
+
+/*
+ * Lookup dest by {svc,addr,port} in the destination trash.
+ * Called by ip_vs_add_dest with the __ip_vs_lock.
+ * The destination trash is used to hold the destinations that are removed
+ * from the service table but are still referenced by some masq entries.
+ * The reason to add the destination trash is when the dest is temporary
+ * down (either by administrator or by monitor program), the dest can be
+ * picked back from the trash, the remaining connections to the dest can
+ * continue, and the counting information of the dest is also useful for
+ * scheduling.
+ */
+struct ip_vs_dest * __ip_vs_get_trash_dest(struct ip_vs_service *svc,
+ __u32 daddr, __u16 dport)
+{
+ struct ip_vs_dest *dest;
+ struct list_head *l, *e;
+
+ /*
+ * Find the destination in trash
+ */
+ l = &ip_vs_dest_trash;
+ for (e=l->next; e!=l; e=e->next) {
+ dest = list_entry(e, struct ip_vs_dest, n_list);
+ IP_VS_DBG("Destiantion %08X:%04X still in trash, refcnt=%d\n",
+ ntohl(dest->addr), ntohs(dest->port),
+ atomic_read(&dest->refcnt));
+ if ((dest->protocol == svc->protocol) &&
+ (dest->vaddr == svc->addr) && (dest->vport == svc->port) &&
+ (dest->addr == daddr) && (dest->port == dport)) {
+ /* HIT */
+ return dest;
+ }
+
+ /*
+ * Try to purge the destination from trash
+ */
+ if (atomic_read(&dest->refcnt) == 1) {
+ IP_VS_DBG("Removing destiantion %08X:%04X from trash\n",
+ ntohl(dest->addr), ntohs(dest->port));
+ e = e->prev;
+ list_del(&dest->n_list);
+ kfree_s(dest, sizeof(*dest));
+ }
+ }
+ return NULL;
+}
+
+
+/*
+ * Update a destination in the given service
+ */
+void __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
+ struct ip_masq_ctl *mctl)
+{
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+
+ /*
+ * Set the weight and the flags
+ */
+ dest->weight = mm->weight;
+ dest->masq_flags = mm->masq_flags;
+
+ dest->masq_flags |= IP_MASQ_F_VS;
+ dest->masq_flags |= IP_MASQ_F_VS_INACTIVE;
+
+ /*
+ * Check if local node and update the flags
+ */
+ if (inet_addr_type(mm->daddr) == RTN_LOCAL) {
+ dest->masq_flags = (dest->masq_flags & ~IP_MASQ_F_VS_FWD_MASK)
+ | IP_MASQ_F_VS_LOCALNODE;
+ }
+
+ /*
+ * Set the IP_MASQ_F_VS_NO_OUTPUT flag if not masquerading
+ */
+ if ((dest->masq_flags & IP_MASQ_F_VS_FWD_MASK) != 0) {
+ dest->masq_flags |= IP_MASQ_F_VS_NO_OUTPUT;
+ }
+
+ /*
+ * Set the dest status flags
+ */
+ dest->flags |= IP_VS_DEST_F_AVAILABLE;
+}
+
+
+/*
+ * Create a destination for the given service
+ */
+struct ip_vs_dest *ip_vs_new_dest(struct ip_vs_service *svc,
+ struct ip_masq_ctl *mctl)
+{
+ struct ip_vs_dest *dest;
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+
+ IP_VS_DBG("enter ip_vs_new_dest()\n");
+
+ dest = (struct ip_vs_dest*) kmalloc(sizeof(struct ip_vs_dest),
+ GFP_ATOMIC);
+ if (dest == NULL) {
+ IP_VS_ERR("ip_vs_new_dest: kmalloc failed.\n");
+ return NULL;
+ }
+ memset(dest, 0, sizeof(struct ip_vs_dest));
+
+ dest->protocol = svc->protocol;
+ dest->vaddr = svc->addr;
+ dest->vport = svc->port;
+ dest->addr = mm->daddr;
+ dest->port = mm->dport;
+
+ atomic_set(&dest->activeconns, 0);
+ atomic_set(&dest->inactconns, 0);
+ atomic_set(&dest->refcnt, 0);
+
+ __ip_vs_update_dest(svc, dest, mctl);
+
+ IP_VS_DBG("leave ip_vs_new_dest()\n");
+
+ return dest;
+}
+
+
+/*
+ * Add a destination into an existing service
+ */
+int ip_vs_add_dest(struct ip_vs_service *svc, struct ip_masq_ctl *mctl)
+{
+ struct ip_vs_dest *dest;
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+ __u32 daddr = mm->daddr;
+ __u16 dport = mm->dport;
+
+ IP_VS_DBG("enter ip_vs_add_dest()\n");
+
+ if (mm->weight < 0) {
+ IP_VS_ERR("ip_vs_add_dest(): server weight less than zero\n");
+ return -ERANGE;
+ }
+
+ /*
+ * Check if the dest already exists in the list
+ */
+ dest = ip_vs_lookup_dest(svc, daddr, dport);
+ if (dest != NULL) {
+ IP_VS_DBG("ip_vs_add_dest(): dest already exists\n");
+ return -EEXIST;
+ }
+
+ write_lock_bh(&__ip_vs_lock);
+
+ /*
+ * Check if the dest already exists in the trash and
+ * is from the same service
+ */
+ dest = __ip_vs_get_trash_dest(svc, daddr, dport);
+ if (dest != NULL) {
+ IP_VS_DBG("Get destination %08X:%04X from trash, "
+ "refcnt=%d, service %08X:%04X\n",
+ ntohl(daddr), ntohs(dport),
+ atomic_read(&dest->refcnt),
+ ntohl(dest->vaddr),
+ ntohs(dest->vport));
+
+ /*
+ * Get the destination from the trash
+ */
+ list_del(&dest->n_list);
+ list_add(&dest->n_list, &svc->destinations);
+
+ __ip_vs_update_dest(svc, dest, mctl);
+
+ write_unlock_bh(&__ip_vs_lock);
+ return 0;
+ }
+
+ /*
+ * Allocate and initialize the dest structure
+ */
+ dest = ip_vs_new_dest(svc, mctl);
+ if (dest == NULL) {
+ write_unlock_bh(&__ip_vs_lock);
+ IP_VS_ERR("ip_vs_add_dest(): out of memory\n");
+ return -ENOMEM;
+ }
+
+ /*
+ * Add the dest entry into the list
+ */
+ list_add(&dest->n_list, &svc->destinations);
+ atomic_inc(&dest->refcnt);
+
+ write_unlock_bh(&__ip_vs_lock);
+
+ return 0;
+}
+
+
+/*
+ * Edit a destination in the given service
+ */
+int ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_masq_ctl *mctl)
+{
+ struct ip_vs_dest *dest;
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+ __u32 daddr = mm->daddr;
+ __u16 dport = mm->dport;
+
+ IP_VS_DBG("enter ip_vs_edit_dest()\n");
+
+ if (mm->weight < 0) {
+ IP_VS_ERR("ip_vs_add_dest(): server weight less than zero\n");
+ return -ERANGE;
+ }
+
+ /*
+ * Lookup the destination list
+ */
+ dest = ip_vs_lookup_dest(svc, daddr, dport);
+ if (dest == NULL) {
+ IP_VS_DBG("ip_vs_edit_dest(): dest doesn't exist\n");
+ return -ENOENT;
+ }
+
+ write_lock_bh(&__ip_vs_lock);
+
+ __ip_vs_update_dest(svc, dest, mctl);
+
+ write_unlock_bh(&__ip_vs_lock);
+
+ return 0;
+}
+
+
+/*
+ * Delete a destination from the given service
+ */
+void __ip_vs_del_dest(struct ip_vs_dest *dest)
+{
+ dest->flags &= ~IP_VS_DEST_F_AVAILABLE;
+
+ /*
+ * Remove it from the d-linked destination list.
+ */
+ list_del(&dest->n_list);
+
+ /*
+ * Decrease the refcnt of the dest, and free the dest
+ * if nobody refers to it (refcnt=0). Otherwise, throw
+ * the destination into the trash.
+ */
+ if (atomic_dec_and_test(&dest->refcnt))
+ kfree_s(dest, sizeof(*dest));
+ else {
+ IP_VS_DBG("Moving dest %08X:%04X into trash, refcnt=%d\n",
+ ntohl(dest->addr), ntohs(dest->port),
+ atomic_read(&dest->refcnt));
+ list_add(&dest->n_list, &ip_vs_dest_trash);
+ atomic_inc(&dest->refcnt);
+ }
+}
+
+int ip_vs_del_dest(struct ip_vs_service *svc, struct ip_masq_ctl *mctl)
+{
+ struct ip_vs_dest *dest;
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+ __u32 daddr = mm->daddr;
+ __u16 dport = mm->dport;
+
+ IP_VS_DBG("enter ip_vs_del_dest()\n");
+
+ /*
+ * Lookup the destination list
+ */
+ dest = ip_vs_lookup_dest(svc, daddr, dport);
+ if (dest == NULL) {
+ IP_VS_DBG("ip_vs_del_dest(): destination not found!\n");
+ return -ENOENT;
+ }
+
+ write_lock_bh(&__ip_vs_lock);
+
+ /*
+ * Remove dest from the destination list
+ */
+ __ip_vs_del_dest(dest);
+
+ /*
+ * Called the update_service function of its scheduler
+ */
+ svc->scheduler->update_service(svc);
+
+ write_unlock_bh(&__ip_vs_lock);
+
+ return 0;
+}
+
+
+/*
+ * Add a service into the service hash table
+ */
+int ip_vs_add_service(struct ip_masq_ctl *mctl)
+{
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+ __u16 protocol = mm->protocol;
+ __u32 vaddr = mm->vaddr;
+ __u16 vport = mm->vport;
+ int ret = 0;
+ struct ip_vs_scheduler *sched;
+ struct ip_vs_service *svc;
+
+ /*
+ * Lookup the scheduler, by 'mctl->m_tname'
+ */
+ sched = ip_vs_lookup_scheduler(mctl->m_tname);
+ if (sched == NULL) {
+ IP_VS_INFO("Scheduler module ip_vs_%s.o not found\n",
+ mctl->m_tname);
+ return -ENOENT;
+ }
+
+ write_lock_bh(&__ip_vs_lock);
+
+ /*
+ * Check if the service already exists
+ */
+ svc = __ip_vs_lookup_service(protocol, vaddr, vport);
+ if (svc != NULL) {
+ IP_VS_DBG("ip_vs_add_service: service already exists.\n");
+ ret = -EEXIST;
+ goto out;
+ }
+
+ svc = (struct ip_vs_service*)
+ kmalloc(sizeof(struct ip_vs_service), GFP_ATOMIC);
+ if (svc == NULL) {
+ IP_VS_DBG("ip_vs_add_service: kmalloc failed.\n");
+ ret = -ENOMEM;
+ goto out;
+ }
+ memset(svc, 0, sizeof(struct ip_vs_service));
+
+ svc->addr = vaddr;
+ svc->port = vport;
+ svc->protocol = protocol;
+ svc->flags = mm->vs_flags;
+ svc->timeout = mm->timeout;
+ svc->netmask = mm->netmask;
+
+ INIT_LIST_HEAD(&svc->destinations);
+
+ /*
+ * Bind the scheduler
+ */
+ ip_vs_bind_scheduler(svc, sched);
+
+ /*
+ * Hash the service into the service table
+ */
+ ip_vs_svc_hash(svc);
+
+ out:
+ write_unlock_bh(&__ip_vs_lock);
+ return ret;
+}
+
+
+/*
+ * Edit a service and bind it with a new scheduler
+ */
+int ip_vs_edit_service(struct ip_vs_service *svc, struct ip_masq_ctl *mctl)
+{
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+ struct ip_vs_scheduler *sched;
+
+ /*
+ * Lookup the scheduler, by 'mctl->m_tname'
+ */
+ sched = ip_vs_lookup_scheduler(mctl->m_tname);
+ if (sched == NULL) {
+ IP_VS_INFO("Scheduler module ip_vs_%s.o not found\n",
+ mctl->m_tname);
+ return -ENOENT;
+ }
+
+ write_lock_bh(&__ip_vs_lock);
+
+ /*
+ * Set the flags and timeout value
+ */
+ svc->flags = mm->vs_flags | IP_VS_SVC_F_HASHED;
+ svc->timeout = mm->timeout;
+ svc->netmask = mm->netmask;
+
+ /*
+ * Unbind the old scheduler
+ */
+ ip_vs_unbind_scheduler(svc);
+
+ /*
+ * Bind the new scheduler
+ */
+ ip_vs_bind_scheduler(svc, sched);
+
+ write_unlock_bh(&__ip_vs_lock);
+
+ return 0;
+}
+
+
+/*
+ * Delete a service from the service list
+ */
+int ip_vs_del_service(struct ip_vs_service *svc)
+{
+ struct list_head *l;
+ struct ip_vs_dest *dest;
+
+ if (svc == NULL)
+ return -EEXIST;
+
+ write_lock_bh(&__ip_vs_lock);
+
+ /*
+ * Unbind scheduler
+ */
+ ip_vs_unbind_scheduler(svc);
+
+ /*
+ * Unlink the whole destination list
+ */
+ l = &svc->destinations;
+ while (l->next != l) {
+ dest = list_entry(l->next, struct ip_vs_dest, n_list);
+ __ip_vs_del_dest(dest);
+ }
+
+ /*
+ * Unhash it from the ip_vs_svc_table
+ */
+ if (ip_vs_svc_unhash(svc)) {
+ /*
+ * Free the service
+ */
+ kfree_s(svc, sizeof(struct ip_vs_service));
+ } else {
+ /*
+ * Called the update_service function of its scheduler
+ */
+ svc->scheduler->update_service(svc);
+ }
+
+ write_unlock_bh(&__ip_vs_lock);
+ return 0;
+}
+
+
+/*
+ * Flush all the virtual services
+ */
+int ip_vs_flush(void)
+{
+ int idx;
+ struct ip_vs_service *svc;
+ struct ip_vs_dest *dest;
+ struct list_head *l, *e;
+
+ write_lock_bh(&__ip_vs_lock);
+
+ for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
+ l = &ip_vs_svc_table[idx];
+ while (l->next != l) {
+ svc = list_entry(l->next, struct ip_vs_service, n_list);
+
+ /*
+ * Unbind scheduler
+ */
+ ip_vs_unbind_scheduler(svc);
+
+ /*
+ * Unlink the destination list
+ */
+ e = &svc->destinations;
+ while (e->next != e) {
+ dest = list_entry(e->next,
+ struct ip_vs_dest, n_list);
+ __ip_vs_del_dest(dest);
+ }
+
+ /*
+ * Unhash it from the ip_vs_svc_table
+ */
+ if (ip_vs_svc_unhash(svc)) {
+ /*
+ * Free the service
+ */
+ kfree_s(svc, sizeof(struct ip_vs_service));
+ } else {
+ /*
+ * Called the update_service function
+ */
+ svc->scheduler->update_service(svc);
+ goto out;
+ }
+ }
+ }
+
+ out:
+ write_unlock_bh(&__ip_vs_lock);
+ return 0;
+}
+
+
+/*
+ * Change the connection counter and the flags if the masq state changes
+ * Called by the masq_tcp_state function.
+ */
+void ip_vs_set_state(struct ip_masq *ms, int new_state)
+{
+ struct ip_vs_dest *dest = ms->dest;
+
+ if (dest &&
+ (ms->flags & IP_MASQ_F_VS) && (new_state != ms->state)) {
+ if (!(ms->flags & IP_MASQ_F_VS_INACTIVE) &&
+ (new_state != IP_MASQ_S_ESTABLISHED)) {
+ atomic_dec(&dest->activeconns);
+ atomic_inc(&dest->inactconns);
+ ms->flags |= IP_MASQ_F_VS_INACTIVE;
+ } else if ((ms->flags & IP_MASQ_F_VS_INACTIVE) &&
+ (new_state == IP_MASQ_S_ESTABLISHED)) {
+ atomic_inc(&dest->activeconns);
+ atomic_dec(&dest->inactconns);
+ ms->flags &= ~IP_MASQ_F_VS_INACTIVE;
+ }
+
+ IP_VS_DBG("Set-state masq fwd:%c s:%s c:%08X:%04X "
+ "v:%08X:%04X d:%08X:%04X flg:%X cnt:%d\n",
+ ip_vs_fwd_tag(ms), ip_masq_state_name(ms->state),
+ ntohl(ms->daddr),ntohs(ms->dport),
+ ntohl(ms->maddr),ntohs(ms->mport),
+ ntohl(ms->saddr),ntohs(ms->sport),
+ ms->flags, atomic_read(&ms->refcnt));
+ }
+}
+
+
+/*
+ * Bind a masq entry with a virtual service destination
+ * Called when a new masq entry is created for VS.
+ */
+void ip_vs_bind_masq(struct ip_masq *ms, struct ip_vs_dest *dest)
+{
+ IP_VS_DBG("Bind-masq fwd:%c s:%s c:%08X:%04X v:%08X:%04X "
+ "d:%08X:%04X flg:%X cnt:%d destcnt:%d\n",
+ ip_vs_fwd_tag(ms), ip_masq_state_name(ms->state),
+ ntohl(ms->daddr),ntohs(ms->dport),
+ ntohl(ms->maddr),ntohs(ms->mport),
+ ntohl(ms->saddr),ntohs(ms->sport),
+ ms->flags, atomic_read(&ms->refcnt),
+ atomic_read(&dest->refcnt));
+
+ ms->flags |= dest->masq_flags;
+ ms->dest = dest;
+
+ /*
+ * Increase the refcnt counter of the dest.
+ */
+ atomic_inc(&dest->refcnt);
+}
+
+
+/*
+ * Unbind a masq entry with its VS destination
+ * Called by the masq_expire function.
+ */
+void ip_vs_unbind_masq(struct ip_masq *ms)
+{
+ struct ip_vs_dest *dest = ms->dest;
+
+ IP_VS_DBG("Unbind-masq fwd:%c s:%s c:%08X:%04X v:%08X:%04X "
+ "d:%08X:%04X flg:%X cnt:%d destcnt:%d\n",
+ ip_vs_fwd_tag(ms), ip_masq_state_name(ms->state),
+ ntohl(ms->daddr),ntohs(ms->dport),
+ ntohl(ms->maddr),ntohs(ms->mport),
+ ntohl(ms->saddr),ntohs(ms->sport),
+ ms->flags, atomic_read(&ms->refcnt),
+ atomic_read(&dest->refcnt));
+
+ if (dest) {
+ /*
+ * Decrease the inactconns or activeconns counter
+ * if it is not a masq template (ms->dport!=0).
+ */
+ if (ms->dport) {
+ if (ms->flags & IP_MASQ_F_VS_INACTIVE) {
+ atomic_dec(&dest->inactconns);
+ } else {
+ atomic_dec(&dest->activeconns);
+ }
+ }
+
+ /*
+ * Decrease the refcnt of the dest, and free the dest
+ * if nobody refers to it (refcnt=0).
+ */
+ if (atomic_dec_and_test(&dest->refcnt))
+ kfree_s(dest, sizeof(*dest));
+ }
+}
+
+
+/*
+ * Checking if the destination of a masq template is available.
+ * If available, return 1, otherwise return 0 and invalidate this
+ * masq template.
+ */
+int ip_vs_check_template(struct ip_masq *mst)
+{
+ struct ip_vs_dest *dest = mst->dest;
+
+ /*
+ * Checking the dest server status.
+ */
+ if ((dest == NULL) ||
+ !(dest->flags & IP_VS_DEST_F_AVAILABLE)) {
+ IP_VS_DBG("check_template: dest not available for prot %d "
+ "src %08X:%04X dest %08X:%04X -> %08X:%04X\n",
+ mst->protocol,
+ ntohl(mst->daddr), ntohs(mst->dport),
+ ntohl(mst->maddr), ntohs(mst->mport),
+ (dest!=NULL)? ntohl(dest->addr):0,
+ (dest!=NULL)? ntohs(dest->port):0);
+
+ /*
+ * Invalidate the masq template
+ */
+ ip_vs_unhash(mst);
+ mst->sport = 65535;
+ mst->mport = 65535;
+ mst->dport = 65535;
+ ip_vs_hash(mst);
+
+ /*
+ * Simply decrease the refcnt of the template,
+ * don't restart its timer.
+ */
+ atomic_dec(&mst->refcnt);
+ return 0;
+ }
+ return 1;
+}
+
+
+/*
+ * IPVS persistent scheduling function
+ * It creates a masq entry according to its template if exists, or selects
+ * a server and creates a masq entry plus a template.
+ */
+struct ip_masq *ip_vs_sched_persist(struct ip_vs_service *svc, struct iphdr *iph)
+{
+ struct ip_masq *ms = NULL;
+ struct ip_vs_dest *dest;
+ const __u16 *portp;
+ struct ip_masq *mst;
+ __u16 dport; /* destination port to forward */
+ __u32 snet; /* source network of the client, after masking */
+
+ portp = (__u16 *)&(((char *)iph)[iph->ihl*4]);
+
+ /* Mask saddr with the netmask to adjust template granularity */
+ snet = iph->saddr & svc->netmask;
+
+ IP_VS_DBG("P-schedule: src %08X:%04X dest %08X:%04X mnet %08X\n",
+ ntohl(iph->saddr), ntohs(portp[0]),
+ ntohl(iph->daddr), ntohs(portp[1]),
+ ntohl(snet));
+
+ /*
+ * As far as we know, FTP is a very complicated network protocol, and
+ * it uses control connection and data connections. For active FTP,
+ * FTP server initilize data connection to the client, its source port
+ * is often 20. For passive FTP, FTP server tells the clients the port
+ * that it passively listens to, and the client issues the data
+ * connection. In the tunneling or direct routing mode, the load
+ * balancer is on the client-to-server half of connection, the port
+ * number is unknown to the load balancer. So, a template masq like
+ * <daddr, 0, maddr, 0, saddr, 0> is created for persistent FTP
+ * service, and a template like <daddr, 0, maddr, mport, saddr, sport>
+ * is created for other persistent services.
+ */
+ if (portp[1] == svc->port) {
+ /* Check if a template already exists */
+ if (svc->port != ftpport)
+ mst = ip_vs_in_get(iph->protocol, snet, 0,
+ iph->daddr, portp[1]);
+ else
+ mst = ip_vs_in_get(iph->protocol, snet, 0,
+ iph->daddr, 0);
+
+ if (!mst || !ip_vs_check_template(mst)) {
+ /*
+ * No template found or the dest of the masq
+ * template is not available.
+ */
+ read_lock(&__ip_vs_lock);
+
+ dest = svc->scheduler->schedule(svc);
+ if (dest == NULL) {
+ IP_VS_DBG("P-schedule: no dest found.\n");
+ read_unlock(&__ip_vs_lock);
+ return NULL;
+ }
+
+ /*
+ * Create a template <daddr,0,maddr,mport,saddr,sport>
+ * for non-ftp service, and <daddr,0,maddr,0,saddr,0>
+ * for ftp service.
+ */
+ if (svc->port != ftpport)
+ mst = ip_masq_new_vs(svc->protocol,
+ svc->addr, svc->port,
+ dest->addr, dest->port,
+ snet, 0,
+ 0);
+ else
+ mst = ip_masq_new_vs(svc->protocol,
+ svc->addr, 0,
+ dest->addr, 0,
+ snet, 0,
+ 0);
+ if (mst == NULL) {
+ IP_VS_ERR("ip_masq_new_vs template failed\n");
+ read_unlock(&__ip_vs_lock);
+ return NULL;
+ }
+
+ /*
+ * Bind the template with dest and set timeout.
+ */
+ ip_vs_bind_masq(mst, dest);
+ mst->timeout = svc->timeout;
+
+ read_unlock(&__ip_vs_lock);
+ } else {
+ /*
+ * Template found and its destination is available.
+ */
+ dest = mst->dest;
+
+ /*
+ * Delete its timer so that it can be put back.
+ */
+ del_sltimer(&mst->timer);
+ }
+ dport = dest->port;
+ } else {
+ mst = ip_vs_in_get(iph->protocol, snet, 0,
+ iph->daddr, 0);
+
+ if (!mst || !ip_vs_check_template(mst)) {
+ /*
+ * If it is not persistent port zero, return NULL,
+ * otherwise create a masq template.
+ */
+ if (svc->port)
+ return NULL;
+
+ read_lock(&__ip_vs_lock);
+
+ dest = svc->scheduler->schedule(svc);
+ if (dest == NULL) {
+ IP_VS_DBG("P-schedule: no dest found.\n");
+ read_unlock(&__ip_vs_lock);
+ return NULL;
+ }
+
+ /*
+ * Create a template <daddr,0,maddr,0,saddr,0>
+ */
+ mst = ip_masq_new_vs(svc->protocol,
+ svc->addr, 0,
+ dest->addr, 0,
+ snet, 0,
+ 0);
+ if (mst == NULL) {
+ IP_VS_ERR("ip_masq_new_vs template failed\n");
+ read_unlock(&__ip_vs_lock);
+ return NULL;
+ }
+
+ /*
+ * Bind the template with dest and set timeout.
+ */
+ ip_vs_bind_masq(mst, dest);
+ mst->timeout = svc->timeout;
+ read_unlock(&__ip_vs_lock);
+ } else {
+ dest = mst->dest;
+
+ /*
+ * Delete its timer so that it can be put back.
+ */
+ del_sltimer(&mst->timer);
+ }
+ dport = portp[1];
+ }
+
+ /*
+ * Create a new masq according to the template
+ */
+ ms = ip_masq_new_vs(iph->protocol,
+ iph->daddr, portp[1],
+ dest->addr, dport,
+ iph->saddr, portp[0],
+ 0);
+ if (ms == NULL) {
+ IP_VS_ERR("ip_masq_new_vs failed\n");
+ ip_masq_put(mst);
+ return NULL;
+ }
+
+ /*
+ * Bind the masq entry with the vs dest.
+ */
+ ip_vs_bind_masq(ms, dest);
+
+ /*
+ * Increase the inactive connection counter
+ * because it is in Syn-Received
+ * state (inactive) when the masq is created.
+ */
+ atomic_inc(&dest->inactconns);
+
+ /*
+ * Add its control
+ */
+ ip_masq_control_add(ms, mst);
+
+ ip_masq_put(mst);
+ return ms;
+}
+
+
+/*
+ * IPVS main scheduling function
+ * It selects a server according to the virtual service, and
+ * creates a masq entry.
+ */
+struct ip_masq *ip_vs_schedule(struct ip_vs_service *svc, struct iphdr *iph)
+{
+ struct ip_masq *ms = NULL;
+ struct ip_vs_dest *dest;
+ const __u16 *portp;
+
+ /*
+ * Persistent service
+ */
+ if (svc->flags & IP_VS_SVC_F_PERSISTENT)
+ return ip_vs_sched_persist(svc, iph);
+
+ /*
+ * Non-persistent service
+ */
+ portp = (__u16 *)&(((char *)iph)[iph->ihl*4]);
+ if (portp[1] != svc->port) {
+ if (!svc->port)
+ IP_VS_ERR("Schedule: port zero only supported in persistent services, check your ipvs configuration\n");
+ return NULL;
+ }
+
+ read_lock(&__ip_vs_lock);
+
+ dest = svc->scheduler->schedule(svc);
+ if (dest == NULL) {
+ IP_VS_DBG("Schedule: no dest found.\n");
+ read_unlock(&__ip_vs_lock);
+ return NULL;
+ }
+
+ /*
+ * Create a masquerading entry.
+ */
+ ms = ip_masq_new_vs(iph->protocol,
+ iph->daddr, portp[1],
+ dest->addr, dest->port,
+ iph->saddr, portp[0],
+ 0);
+ if (ms == NULL) {
+ IP_VS_ERR("Schedule: ip_masq_new_vs failed\n");
+ read_unlock(&__ip_vs_lock);
+ return NULL;
+ }
+
+ /*
+ * Bind the masq entry with the vs dest.
+ */
+ ip_vs_bind_masq(ms, dest);
+
+ /*
+ * Increase the inactive connection counter because it is in
+ * Syn-Received state (inactive) when the masq is created.
+ */
+ atomic_inc(&dest->inactconns);
+
+ IP_VS_DBG("Schedule masq fwd:%c s:%s c:%08X:%04X v:%08X:%04X "
+ "d:%08X:%04X flg:%X cnt:%d\n",
+ ip_vs_fwd_tag(ms), ip_masq_state_name(ms->state),
+ ntohl(ms->daddr),ntohs(ms->dport),
+ ntohl(ms->maddr),ntohs(ms->mport),
+ ntohl(ms->saddr),ntohs(ms->sport),
+ ms->flags, atomic_read(&ms->refcnt));
+
+ read_unlock(&__ip_vs_lock);
+
+ return ms;
+}
+
+
+/*
+ * Pass or drop the packet.
+ * Called by ip_fw_demasquerade, when the virtual service is avaiable but
+ * no destination is available for a new connection.
+ */
+int ip_vs_leave(struct ip_vs_service *svc, struct sk_buff *skb)
+{
+ struct iphdr *iph = skb->nh.iph;
+ __u16 *portp = (__u16 *)&(((char *)iph)[iph->ihl*4]);
+
+ /*
+ * When the virtual ftp service is presented, packets destined
+ * for other services on the VIP may get here (except services
+ * listed in the ipvs table), pass the packets, because it is
+ * not ipvs job to decide to drop the packets.
+ */
+ if ((svc->port == ftpport) && (portp[1] != ftpport))
+ return 0;
+
+ /*
+ * Notify the client that the destination is unreachable, and
+ * release the socket buffer.
+ * Since it is in IP layer, the TCP socket is not actually
+ * created, the TCP RST packet cannot be sent, instead that
+ * ICMP_PORT_UNREACH is sent here no matter it is TCP/UDP. --WZ
+ */
+ icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
+ kfree_skb(skb);
+ return -2;
+}
+
+
+/*
+ * IPVS user control entry
+ */
+int ip_vs_ctl(int optname, struct ip_masq_ctl *mctl, int optlen)
+{
+ struct ip_vs_service *svc = NULL;
+ struct ip_vs_user *mm = &mctl->u.vs_user;
+ __u32 vaddr = mm->vaddr;
+ __u16 vport = mm->vport;
+ int proto_num = masq_proto_num(mm->protocol);
+
+ /*
+ * Check the size of mctl, no overflow...
+ */
+ if (optlen != sizeof(*mctl))
+ return EINVAL;
+
+ /*
+ * Flush all the virtual service...
+ */
+ if (mctl->m_cmd == IP_MASQ_CMD_FLUSH)
+ return ip_vs_flush();
+
+ /*
+ * Check for valid protocol: TCP or UDP
+ */
+ if ((proto_num < 0) || (proto_num > 1)) {
+ IP_VS_INFO("vs_ctl: invalid protocol: %d"
+ "%d.%d.%d.%d:%d %s",
+ ntohs(mm->protocol),
+ NIPQUAD(vaddr), ntohs(vport), mctl->m_tname);
+ return -EFAULT;
+ }
+
+ /*
+ * Lookup the exact service by (protocol, vaddr, vport)
+ */
+ svc = __ip_vs_lookup_service(mm->protocol, vaddr, vport);
+
+ switch (mctl->m_cmd) {
+ case IP_MASQ_CMD_ADD:
+ if (svc != NULL)
+ return -EEXIST;
+
+ return ip_vs_add_service(mctl);
+
+ case IP_MASQ_CMD_SET:
+ if (svc == NULL)
+ return -ESRCH;
+
+ return ip_vs_edit_service(svc, mctl);
+
+ case IP_MASQ_CMD_DEL:
+ if (svc == NULL)
+ return -ESRCH;
+ else
+ return ip_vs_del_service(svc);
+
+ case IP_MASQ_CMD_ADD_DEST:
+ if (svc == NULL)
+ return -ESRCH;
+ else
+ return ip_vs_add_dest(svc, mctl);
+
+ case IP_MASQ_CMD_SET_DEST:
+ if (svc == NULL)
+ return -ESRCH;
+ else
+ return ip_vs_edit_dest(svc, mctl);
+
+ case IP_MASQ_CMD_DEL_DEST:
+ if (svc == NULL)
+ return -ESRCH;
+ else
+ return ip_vs_del_dest(svc, mctl);
+ }
+ return -EINVAL;
+}
+
+
+
+#ifdef CONFIG_PROC_FS
+/*
+ * Write the contents of the VS rule table to a PROCfs file.
+ */
+static int ip_vs_procinfo(char *buf, char **start, off_t offset,
+ int length, int *eof, void *data)
+{
+ int idx;
+ int len=0;
+ off_t pos=0;
+ int size;
+ struct ip_vs_service *svc;
+ struct ip_vs_dest *dest;
+ struct list_head *l, *e, *p, *q;
+
+ size = sprintf(buf+len,
+ "IP Virtual Server version 0.9.7 (size=%d)\n"
+ "Prot LocalAddress:Port Scheduler Flags\n"
+ " -> RemoteAddress:Port Forward Weight ActiveConn InActConn\n",
+ IP_VS_TAB_SIZE);
+ pos += size;
+ len += size;
+
+ read_lock_bh(&__ip_vs_lock);
+
+ for (idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
+ l = &ip_vs_svc_table[idx];
+ for (e=l->next; e!=l; e=e->next) {
+ svc = list_entry(e, struct ip_vs_service, n_list);
+ size = sprintf(buf+len, "%s %08X:%04X %s",
+ masq_proto_name(svc->protocol),
+ ntohl(svc->addr), ntohs(svc->port),
+ svc->scheduler->name);
+ len += size;
+ pos += size;
+
+ if (svc->flags & IP_VS_SVC_F_PERSISTENT)
+ size = sprintf(buf+len, " persistent %d %08X\n",
+ svc->timeout,
+ ntohl(svc->netmask));
+ else
+ size = sprintf(buf+len, "\n");
+
+ len += size;
+ pos += size;
+
+ if (pos <= offset)
+ len=0;
+ if (pos >= offset+length)
+ goto done;
+
+ p = &svc->destinations;
+ for (q=p->next; q!=p; q=q->next) {
+ char *fwd;
+
+ dest = list_entry(q, struct ip_vs_dest, n_list);
+ switch (dest->masq_flags & IP_MASQ_F_VS_FWD_MASK) {
+ case IP_MASQ_F_VS_LOCALNODE:
+ fwd = "Local";
+ break;
+ case IP_MASQ_F_VS_TUNNEL:
+ fwd = "Tunnel";
+ break;
+ case IP_MASQ_F_VS_DROUTE:
+ fwd = "Route";
+ break;
+ default:
+ fwd = "Masq";
+ }
+
+ size = sprintf(buf+len,
+ " -> %08X:%04X %-7s %-6d %-10d %-10d\n",
+ ntohl(dest->addr), ntohs(dest->port),
+ fwd, dest->weight,
+ atomic_read(&dest->activeconns),
+ atomic_read(&dest->inactconns));
+ len += size;
+ pos += size;
+
+ if (pos <= offset)
+ len=0;
+ if (pos >= offset+length)
+ goto done;
+ }
+ }
+ }
+
+ done:
+ read_unlock_bh(&__ip_vs_lock);
+
+ *start = buf+len-(pos-offset); /* Start of wanted data */
+ len = pos-offset;
+ if (len > length)
+ len = length;
+ if (len < 0)
+ len = 0;
+
+ return len;
+}
+
+struct proc_dir_entry ip_vs_proc_entry = {
+ 0, /* dynamic inode */
+ 2, "vs", /* namelen and name */
+ S_IFREG | S_IRUGO, /* mode */
+ 1, 0, 0, 0, /* nlinks, owner, group, size */
+ &proc_net_inode_operations, /* operations */
+ NULL, /* get_info */
+ NULL, /* fill_inode */
+ NULL, NULL, NULL, /* next, parent, subdir */
+ NULL, /* data */
+ &ip_vs_procinfo, /* function to generate proc data */
+};
+
+#endif
+
+
+/*
+ * This function encapsulates the packet in a new IP header, its destination
+ * will be set to the daddr. Most code of this function is from ipip.c.
+ * Usage:
+ * It is called in the ip_vs_forward() function. The load balancer
+ * selects a real server from a cluster based on a scheduling algorithm,
+ * encapsulates the packet and forwards it to the selected server. All real
+ * servers are configured with "ifconfig tunl0 <Virtual IP Address> up".
+ * When the server receives the encapsulated packet, it decapsulates the
+ * packet, processes the request and return the reply packets directly to
+ * the client without passing the load balancer. This can greatly
+ * increase the scalability of virtual server.
+ * Returns:
+ * if succeeded, return 1; otherwise, return 0.
+ */
+
+int ip_vs_tunnel_xmit(struct sk_buff *skb, __u32 daddr)
+{
+ struct rtable *rt; /* Route to the other host */
+ struct device *tdev; /* Device to other host */
+ struct iphdr *old_iph = skb->nh.iph;
+ u8 tos = old_iph->tos;
+ u16 df = old_iph->frag_off;
+ struct iphdr *iph; /* Our new IP header */
+ int max_headroom; /* The extra header space needed */
+ u32 dst = daddr;
+ u32 src = 0;
+ int mtu;
+
+ if (skb->protocol != __constant_htons(ETH_P_IP)) {
+ IP_VS_DBG("ip_vs_tunnel_xmit(): protocol error, ETH_P_IP: %d, skb protocol: %d\n",
+ __constant_htons(ETH_P_IP),skb->protocol);
+ goto tx_error;
+ }
+
+ if (ip_route_output(&rt, dst, src, RT_TOS(tos), 0)) {
+ IP_VS_DBG("ip_vs_tunnel_xmit(): route error, dst: %08X\n", dst);
+ goto tx_error_icmp;
+ }
+ tdev = rt->u.dst.dev;
+
+ mtu = rt->u.dst.pmtu - sizeof(struct iphdr);
+ if (mtu < 68) {
+ ip_rt_put(rt);
+ IP_VS_DBG("ip_vs_tunnel_xmit(): mtu less than 68\n");
+ goto tx_error;
+ }
+ if (skb->dst && mtu < skb->dst->pmtu)
+ skb->dst->pmtu = mtu;
+
+ df |= (old_iph->frag_off&__constant_htons(IP_DF));
+
+ if ((old_iph->frag_off&__constant_htons(IP_DF)) && mtu < ntohs(old_iph->tot_len)) {
+ icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(mtu));
+ ip_rt_put(rt);
+ IP_VS_DBG("ip_vs_tunnel_xmit(): frag needed\n");
+ goto tx_error;
+ }
+
+ skb->h.raw = skb->nh.raw;
+
+ /*
+ * Okay, now see if we can stuff it in the buffer as-is.
+ */
+ max_headroom = (((tdev->hard_header_len+15)&~15)+sizeof(struct iphdr));
+
+ if (skb_headroom(skb) < max_headroom || skb_cloned(skb) || skb_shared(skb)) {
+ struct sk_buff *new_skb = skb_realloc_headroom(skb, max_headroom);
+ if (!new_skb) {
+ ip_rt_put(rt);
+ kfree_skb(skb);
+ IP_VS_ERR("ip_vs_tunnel_xmit(): no memory for new_skb\n");
+ return 0;
+ }
+ kfree_skb(skb);
+ skb = new_skb;
+ }
+
+ skb->nh.raw = skb_push(skb, sizeof(struct iphdr));
+ memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
+ dst_release(skb->dst);
+ skb->dst = &rt->u.dst;
+
+ /*
+ * Push down and install the IPIP header.
+ */
+
+ iph = skb->nh.iph;
+ iph->version = 4;
+ iph->ihl = sizeof(struct iphdr)>>2;
+ iph->frag_off = df;
+ iph->protocol = IPPROTO_IPIP;
+ iph->tos = tos;
+ iph->daddr = rt->rt_dst;
+ iph->saddr = rt->rt_src;
+ iph->ttl = old_iph->ttl;
+ iph->tot_len = htons(skb->len);
+ iph->id = htons(ip_id_count++);
+ ip_send_check(iph);
+
+ IPCB(skb)->flags |= IPSKB_REDIRECTED;
+ IPCB(skb)->flags |= IPSKB_MASQUERADED;
+
+ ip_send(skb);
+ return 1;
+
+ tx_error_icmp:
+ dst_link_failure(skb);
+ tx_error:
+ kfree_skb(skb);
+ return 0;
+}
+
+
+/*
+ * Direct Routing
+ */
+int ip_vs_dr_xmit(struct sk_buff *skb, __u32 daddr)
+{
+ struct rtable *rt; /* Route to the other host */
+ struct iphdr *iph = skb->nh.iph;
+ u8 tos = iph->tos;
+
+ if (ip_route_output(&rt, daddr, 0, RT_TOS(tos), 0)) {
+ IP_VS_DBG("ip_vs_dr_xmit(): route error, dest: %08X\n", daddr);
+ goto tx_error_icmp;
+ }
+
+ dst_release(skb->dst);
+ skb->dst = &rt->u.dst;
+
+ IPCB(skb)->flags |= IPSKB_REDIRECTED;
+ IPCB(skb)->flags |= IPSKB_MASQUERADED;
+
+ ip_send(skb);
+ return 1;
+
+ tx_error_icmp:
+ dst_link_failure(skb);
+ kfree_skb(skb);
+ return 0;
+}
+
+
+/*
+ * Initialize IP virtual server
+ */
+__initfunc(int ip_vs_init(void))
+{
+ int idx;
+
+ /*
+ * Allocate the ip_vs_table and initialize its list head.
+ * Initilize ip_vs_svc_table, ip_vs_schedulers and ip_vs_dest_trash.
+ */
+ if (!(ip_vs_table = vmalloc(IP_VS_TAB_SIZE*sizeof(struct list_head)))) {
+ return -ENOMEM;
+ }
+ for(idx = 0; idx < IP_VS_TAB_SIZE; idx++) {
+ INIT_LIST_HEAD(&ip_vs_table[idx]);
+ }
+ for(idx = 0; idx < IP_VS_SVC_TAB_SIZE; idx++) {
+ INIT_LIST_HEAD(&ip_vs_svc_table[idx]);
+ }
+ INIT_LIST_HEAD(&ip_vs_schedulers);
+ INIT_LIST_HEAD(&ip_vs_dest_trash);
+
+ /*
+ * Hook the slow_timer handler in the system timer.
+ */
+ slow_timer.expires = jiffies+SLTIMER_PERIOD;
+ add_timer(&slow_timer);
+
+ /*
+ * Set the port number of FTP service (in network order).
+ */
+ ftpport = htons(21);
+
+#ifdef CONFIG_PROC_FS
+ ip_masq_proc_register(&ip_vs_proc_entry);
+#endif
+
+#ifdef CONFIG_IP_MASQUERADE_VS_RR
+ ip_vs_rr_init();
+#endif
+#ifdef CONFIG_IP_MASQUERADE_VS_WRR
+ ip_vs_wrr_init();
+#endif
+#ifdef CONFIG_IP_MASQUERADE_VS_LC
+ ip_vs_lc_init();
+#endif
+#ifdef CONFIG_IP_MASQUERADE_VS_WLC
+ ip_vs_wlc_init();
+#endif
+ return 0;
+}
diff -urN linux-2.2.13/net/ipv4/ip_vs_lc.c linux-2.2.13-vs-0.9/net/ipv4/ip_vs_lc.c
--- linux-2.2.13/net/ipv4/ip_vs_lc.c Thu Jan 1 08:00:00 1970
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_vs_lc.c Wed Oct 6 18:05:50 1999
@@ -0,0 +1,159 @@
+/*
+ * IPVS: Least-Connection Scheduling module
+ *
+ * Version: $Id: ip_vs_lc.c,v 1.3 1999/10/06 10:05:50 wensong Exp $
+ *
+ * Authors: Wensong Zhang <wensong@iinchina.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Changes:
+ * Wensong Zhang : added the ip_vs_lc_update_svc
+ * Wensong Zhang : added any dest with weight=0 is quiesced
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#ifdef CONFIG_KMOD
+#include <linux/kmod.h>
+#endif
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <net/ip_masq.h>
+#ifdef CONFIG_IP_MASQUERADE_MOD
+#include <net/ip_masq_mod.h>
+#endif
+#include <linux/sysctl.h>
+#include <linux/ip_fw.h>
+#include <net/ip_vs.h>
+
+
+static int ip_vs_lc_init_svc(struct ip_vs_service *svc)
+{
+ MOD_INC_USE_COUNT;
+ return 0;
+}
+
+
+static int ip_vs_lc_done_svc(struct ip_vs_service *svc)
+{
+ MOD_DEC_USE_COUNT;
+ return 0;
+}
+
+
+static int ip_vs_lc_update_svc(struct ip_vs_service *svc)
+{
+ return 0;
+}
+
+
+/*
+ * Least Connection scheduling
+ */
+static struct ip_vs_dest* ip_vs_lc_schedule(struct ip_vs_service *svc)
+{
+ struct list_head *l, *e;
+ struct ip_vs_dest *dest, *least;
+ int lac, dac;
+
+ IP_VS_DBG("ip_vs_lc_schedule(): Scheduling...\n");
+
+ l = &svc->destinations;
+ if (l == l->next)
+ return NULL;
+
+ /*
+ * Simply select the server with the least number of
+ * (activeconns<<5) + inactconns
+ * Except whose weight is equal to zero.
+ * If the weight is equal to zero, it means that the server is
+ * quiesced, the existing connections to the server still get
+ * served, but no new connection is assigned to the server.
+ */
+
+ for (e=l->next; e!=l; e=e->next) {
+ least = list_entry (e, struct ip_vs_dest, n_list);
+ if (least->weight > 0) {
+ lac = (atomic_read(&least->activeconns) << 5)
+ + atomic_read(&least->inactconns);
+ goto nextstage;
+ }
+ }
+ return NULL;
+
+ /*
+ * Find the destination with the least load.
+ */
+ nextstage:
+ for (e=e->next; e!=l; e=e->next) {
+ dest = list_entry(e, struct ip_vs_dest, n_list);
+ if (dest->weight == 0)
+ continue;
+ dac = (atomic_read(&dest->activeconns) << 5)
+ + atomic_read(&dest->inactconns);
+ if (dac < lac) {
+ least = dest;
+ lac = dac;
+ }
+ }
+
+ IP_VS_DBG("LC: server %d.%d.%d.%d:%d activeconns %d inactconns %d\n",
+ NIPQUAD(least->addr), ntohs(least->port),
+ atomic_read(&least->activeconns),
+ atomic_read(&least->inactconns));
+
+ return least;
+}
+
+
+static struct ip_vs_scheduler ip_vs_lc_scheduler = {
+ {0}, /* n_list */
+ "lc", /* name */
+ ATOMIC_INIT(0), /* refcnt */
+ ip_vs_lc_init_svc, /* service initializer */
+ ip_vs_lc_done_svc, /* service done */
+ ip_vs_lc_update_svc, /* service updater */
+ ip_vs_lc_schedule, /* select a server from the destination list */
+};
+
+
+__initfunc(int ip_vs_lc_init(void))
+{
+ IP_VS_INFO("Initializing LC scheduling\n");
+ INIT_LIST_HEAD(&ip_vs_lc_scheduler.n_list);
+ return register_ip_vs_scheduler(&ip_vs_lc_scheduler) ;
+}
+
+
+#ifdef MODULE
+EXPORT_NO_SYMBOLS;
+
+int init_module(void)
+{
+ INIT_LIST_HEAD(&ip_vs_lc_scheduler.n_list);
+
+ /* module initialization by 'request_module' */
+ if(register_ip_vs_scheduler(&ip_vs_lc_scheduler) != 0)
+ return -EIO;
+
+ IP_VS_INFO("LC scheduling module loaded.\n");
+
+ return 0;
+}
+
+void cleanup_module(void)
+{
+ /* module cleanup by 'release_module' */
+ if(unregister_ip_vs_scheduler(&ip_vs_lc_scheduler) != 0)
+ IP_VS_INFO("cannot remove LC scheduling module\n");
+ else
+ IP_VS_INFO("LC scheduling module unloaded.\n");
+}
+
+#endif /* MODULE */
diff -urN linux-2.2.13/net/ipv4/ip_vs_rr.c linux-2.2.13-vs-0.9/net/ipv4/ip_vs_rr.c
--- linux-2.2.13/net/ipv4/ip_vs_rr.c Thu Jan 1 08:00:00 1970
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_vs_rr.c Wed Oct 6 18:04:46 1999
@@ -0,0 +1,145 @@
+/*
+ * IPVS: Round-Robin Scheduling module
+ *
+ * Version: $Id: ip_vs_rr.c,v 1.5 1999/10/06 10:04:46 wensong Exp $
+ *
+ * Authors: Wensong Zhang <wensong@iinchina.net>
+ * Peter Kese <peter.kese@ijs.si>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Fixes/Changes:
+ * Wensong Zhang : changed the ip_vs_rr_schedule to return dest
+ * Julian Anastasov : fixed the NULL pointer access bug in debugging
+ * Wensong Zhang : changed some comestics things for debugging
+ * Wensong Zhang : changed for the d-linked destination list
+ * Wensong Zhang : added the ip_vs_rr_update_svc
+ * Wensong Zhang : added any dest with weight=0 is quiesced
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#ifdef CONFIG_KMOD
+#include <linux/kmod.h>
+#endif
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <net/ip_masq.h>
+#ifdef CONFIG_IP_MASQUERADE_MOD
+#include <net/ip_masq_mod.h>
+#endif
+#include <linux/sysctl.h>
+#include <linux/ip_fw.h>
+#include <net/ip_vs.h>
+
+
+static int ip_vs_rr_init_svc(struct ip_vs_service *svc)
+{
+ svc->sched_data = &svc->destinations;
+ MOD_INC_USE_COUNT;
+ return 0;
+}
+
+
+static int ip_vs_rr_done_svc(struct ip_vs_service *svc)
+{
+ MOD_DEC_USE_COUNT;
+ return 0;
+}
+
+
+static int ip_vs_rr_update_svc(struct ip_vs_service *svc)
+{
+ svc->sched_data = &svc->destinations;
+ return 0;
+}
+
+
+/*
+ * Round-Robin Scheduling
+ */
+static struct ip_vs_dest* ip_vs_rr_schedule(struct ip_vs_service *svc)
+{
+ register struct list_head *p, *q;
+ struct ip_vs_dest *dest;
+
+ IP_VS_DBG("ip_vs_rr_schedule(): Scheduling...\n");
+
+ p = (struct list_head *)svc->sched_data;
+ p = p->next;
+ q = p;
+ do {
+ if (q == &svc->destinations) {
+ q = q->next;
+ continue;
+ }
+ dest = list_entry(q, struct ip_vs_dest, n_list);
+ if (dest->weight > 0)
+ /* HIT */
+ goto out;
+ q = q->next;
+ } while (q != p);
+ return NULL;
+
+ out:
+ svc->sched_data = q;
+ IP_VS_DBG("RR: server %d.%d.%d.%d:%d "
+ "activeconns %d refcnt %d weight %d\n",
+ NIPQUAD(dest->addr), ntohs(dest->port),
+ atomic_read(&dest->activeconns),
+ atomic_read(&dest->refcnt), dest->weight);
+
+ return dest;
+}
+
+
+static struct ip_vs_scheduler ip_vs_rr_scheduler = {
+ {0}, /* n_list */
+ "rr", /* name */
+ ATOMIC_INIT(0), /* refcnt */
+ ip_vs_rr_init_svc, /* service initializer */
+ ip_vs_rr_done_svc, /* service done */
+ ip_vs_rr_update_svc, /* service updater */
+ ip_vs_rr_schedule, /* select a server from the destination list */
+};
+
+
+__initfunc(int ip_vs_rr_init(void))
+{
+ IP_VS_INFO("Initializing RR scheduling\n");
+ INIT_LIST_HEAD(&ip_vs_rr_scheduler.n_list);
+ return register_ip_vs_scheduler(&ip_vs_rr_scheduler) ;
+}
+
+
+#ifdef MODULE
+EXPORT_NO_SYMBOLS;
+
+int init_module(void)
+{
+ INIT_LIST_HEAD(&ip_vs_rr_scheduler.n_list);
+
+ /* module initialization by 'request_module' */
+ if(register_ip_vs_scheduler(&ip_vs_rr_scheduler) != 0)
+ return -EIO;
+
+ IP_VS_INFO("RR scheduling module loaded.\n");
+
+ return 0;
+}
+
+void cleanup_module(void)
+{
+ /* module cleanup by 'release_module' */
+ if(unregister_ip_vs_scheduler(&ip_vs_rr_scheduler) != 0)
+ IP_VS_INFO("cannot remove RR scheduling module\n");
+ else
+ IP_VS_INFO("RR scheduling module unloaded.\n");
+}
+
+#endif /* MODULE */
diff -urN linux-2.2.13/net/ipv4/ip_vs_wlc.c linux-2.2.13-vs-0.9/net/ipv4/ip_vs_wlc.c
--- linux-2.2.13/net/ipv4/ip_vs_wlc.c Thu Jan 1 08:00:00 1970
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_vs_wlc.c Wed Oct 6 18:06:19 1999
@@ -0,0 +1,181 @@
+/*
+ * IPVS: Weighted Least-Connection Scheduling module
+ *
+ * Version: $Id: ip_vs_wlc.c,v 1.5 1999/10/06 10:06:19 wensong Exp $
+ *
+ * Authors: Wensong Zhang <wensong@iinchina.net>
+ * Peter Kese <peter.kese@ijs.si>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Changes:
+ * Wensong Zhang : changed the ip_vs_wlc_schedule to return dest
+ * Wensong Zhang : changed to use the inactconns in scheduling
+ * Wensong Zhang : changed some comestics things for debugging
+ * Wensong Zhang : changed for the d-linked destination list
+ * Wensong Zhang : added the ip_vs_wlc_update_svc
+ * Wensong Zhang : added any dest with weight=0 is quiesced
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#ifdef CONFIG_KMOD
+#include <linux/kmod.h>
+#endif
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <net/ip_masq.h>
+#ifdef CONFIG_IP_MASQUERADE_MOD
+#include <net/ip_masq_mod.h>
+#endif
+#include <linux/sysctl.h>
+#include <linux/ip_fw.h>
+#include <net/ip_vs.h>
+
+
+static int
+ip_vs_wlc_init_svc (struct ip_vs_service *svc)
+{
+ MOD_INC_USE_COUNT;
+ return 0;
+}
+
+
+static int
+ip_vs_wlc_done_svc (struct ip_vs_service *svc)
+{
+ MOD_DEC_USE_COUNT;
+ return 0;
+}
+
+
+static int
+ip_vs_wlc_update_svc (struct ip_vs_service *svc)
+{
+ return 0;
+}
+
+
+/*
+ * Weighted Least Connection scheduling
+ */
+static struct ip_vs_dest *
+ip_vs_wlc_schedule (struct ip_vs_service *svc)
+{
+ register struct list_head *l, *e;
+ struct ip_vs_dest *dest, *least;
+ int loh, doh;
+
+ IP_VS_DBG ("ip_vs_wlc_schedule(): Scheduling...\n");
+
+ l = &svc->destinations;
+ if (l == l->next)
+ return NULL;
+
+ /*
+ * We think the overhead of processing active connections is fifty
+ * times than that of inactive conncetions in average. (This fifty
+ * times might be not accurate, we will change it later.) We use
+ * the following formula to estimate the overhead:
+ * dest->activeconns*50 + dest->inactconns
+ * and the load:
+ * (dest overhead) / dest->weight
+ *
+ * Remember -- no floats in kernel mode!!!
+ * The comparison of h1*w2 > h2*w1 is equivalent to that of
+ * h1/w1 > h2/w2
+ * if every weight is larger than zero.
+ *
+ * The server with weight=0 is quiesced and will not receive any
+ * new connection.
+ */
+
+ for (e=l->next; e!=l; e=e->next) {
+ least = list_entry (e, struct ip_vs_dest, n_list);
+ if (least->weight > 0) {
+ loh = atomic_read (&least->activeconns) * 50
+ + atomic_read (&least->inactconns);
+ goto nextstage;
+ }
+ }
+ return NULL;
+
+ /*
+ * Find the destination with the least load.
+ */
+ nextstage:
+ for (e=e->next; e!=l; e=e->next)
+ {
+ dest = list_entry (e, struct ip_vs_dest, n_list);
+ doh = atomic_read (&dest->activeconns) * 50
+ + atomic_read (&dest->inactconns);
+ if (loh * dest->weight > doh * least->weight)
+ {
+ least = dest;
+ loh = doh;
+ }
+ }
+
+ IP_VS_DBG ("WLC: server %d.%d.%d.%d:%d "
+ "activeconns %d refcnt %d weight %d overhead %d\n",
+ NIPQUAD (least->addr), ntohs (least->port),
+ atomic_read (&least->activeconns),
+ atomic_read (&least->refcnt), least->weight, loh);
+
+ return least;
+}
+
+
+static struct ip_vs_scheduler ip_vs_wlc_scheduler =
+{
+ {0}, /* n_list */
+ "wlc", /* name */
+ ATOMIC_INIT (0), /* refcnt */
+ ip_vs_wlc_init_svc, /* service initializer */
+ ip_vs_wlc_done_svc, /* service done */
+ ip_vs_wlc_update_svc, /* service updater */
+ ip_vs_wlc_schedule, /* select a server from the destination list */
+};
+
+
+__initfunc (int ip_vs_wlc_init (void))
+{
+ IP_VS_INFO ("Initializing WLC scheduling\n");
+ INIT_LIST_HEAD (&ip_vs_wlc_scheduler.n_list);
+ return register_ip_vs_scheduler (&ip_vs_wlc_scheduler);
+}
+
+
+#ifdef MODULE
+EXPORT_NO_SYMBOLS;
+
+int
+init_module (void)
+{
+ INIT_LIST_HEAD (&ip_vs_wlc_scheduler.n_list);
+
+ /* module initialization by 'request_module' */
+ if (register_ip_vs_scheduler (&ip_vs_wlc_scheduler) != 0)
+ return -EIO;
+
+ IP_VS_INFO ("WLC scheduling module loaded.\n");
+
+ return 0;
+}
+
+void
+cleanup_module (void)
+{
+ /* module cleanup by 'release_module' */
+ if (unregister_ip_vs_scheduler (&ip_vs_wlc_scheduler) != 0)
+ IP_VS_INFO ("cannot remove WLC scheduling module\n");
+ else
+ IP_VS_INFO ("WLC scheduling module unloaded.\n");
+}
+
+#endif /* MODULE */
diff -urN linux-2.2.13/net/ipv4/ip_vs_wrr.c linux-2.2.13-vs-0.9/net/ipv4/ip_vs_wrr.c
--- linux-2.2.13/net/ipv4/ip_vs_wrr.c Thu Jan 1 08:00:00 1970
+++ linux-2.2.13-vs-0.9/net/ipv4/ip_vs_wrr.c Wed Oct 6 18:05:15 1999
@@ -0,0 +1,204 @@
+/*
+ * IPVS: Weighted Round-Robin Scheduling module
+ *
+ * Version: $Id: ip_vs_wrr.c,v 1.5 1999/10/06 10:05:15 wensong Exp $
+ *
+ * Authors: Wensong Zhang <wensong@iinchina.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ * Changes:
+ * Wensong Zhang : changed the ip_vs_wrr_schedule to return dest
+ * Wensong Zhang : changed some comestics things for debugging
+ * Wensong Zhang : changed for the d-linked destination list
+ * Wensong Zhang : added the ip_vs_wrr_update_svc
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/module.h>
+#ifdef CONFIG_KMOD
+#include <linux/kmod.h>
+#endif
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <net/ip_masq.h>
+#ifdef CONFIG_IP_MASQUERADE_MOD
+#include <net/ip_masq_mod.h>
+#endif
+#include <linux/sysctl.h>
+#include <linux/ip_fw.h>
+#include <net/ip_vs.h>
+
+/*
+ * current destination pointer for weighted round-robin scheduling
+ */
+struct ip_vs_wrr_mark {
+ struct list_head *cl; /* current list head */
+ int cw; /* current weight */
+};
+
+
+static int ip_vs_wrr_init_svc(struct ip_vs_service *svc)
+{
+ /*
+ * Allocate the mark variable for WRR scheduling
+ */
+ svc->sched_data = kmalloc(sizeof(struct ip_vs_wrr_mark), GFP_ATOMIC);
+
+ if (svc->sched_data == NULL) {
+ IP_VS_ERR("ip_vs_wrr_init_svc(): no memory\n");
+ return ENOMEM;
+ }
+ memset(svc->sched_data, 0, sizeof(struct ip_vs_wrr_mark));
+
+ ((struct ip_vs_wrr_mark*)svc->sched_data)->cl = &svc->destinations;
+
+ MOD_INC_USE_COUNT;
+ return 0;
+}
+
+
+static int ip_vs_wrr_done_svc(struct ip_vs_service *svc)
+{
+ /*
+ * Release the mark variable
+ */
+ kfree_s(svc->sched_data, sizeof(struct ip_vs_wrr_mark));
+
+ MOD_DEC_USE_COUNT;
+ return 0;
+}
+
+
+static int ip_vs_wrr_update_svc(struct ip_vs_service *svc)
+{
+ ((struct ip_vs_wrr_mark*)svc->sched_data)->cl = &svc->destinations;
+ return 0;
+}
+
+
+/*
+ * Get the maximum weight of the service destinations.
+ */
+int ip_vs_wrr_max_weight(struct ip_vs_service *svc)
+{
+ register struct list_head *l, *e;
+ struct ip_vs_dest *dest;
+ int weight = 0;
+
+ l = &svc->destinations;
+ for (e=l->next; e!=l; e=e->next) {
+ dest = list_entry(e, struct ip_vs_dest, n_list);
+ if (dest->weight > weight)
+ weight = dest->weight;
+ }
+
+ return weight;
+}
+
+
+/*
+ * Weighted Round-Robin Scheduling
+ */
+static struct ip_vs_dest* ip_vs_wrr_schedule(struct ip_vs_service *svc)
+{
+ struct ip_vs_dest *dest;
+ struct ip_vs_wrr_mark *mark = svc->sched_data;
+
+ IP_VS_DBG("ip_vs_wrr_schedule(): Scheduling...\n");
+
+ /*
+ * This loop will always terminate, because 0<mark->cw<max_weight,
+ * and at least one server has its weight equal to max_weight.
+ */
+ while (1) {
+ if (mark->cl == &svc->destinations) {
+ /* it is at the head of the destination list */
+
+ if (mark->cl == mark->cl->next)
+ /* no dest entry */
+ return NULL;
+
+ mark->cl = svc->destinations.next;
+ mark->cw--;
+ if (mark->cw <= 0) {
+ mark->cw = ip_vs_wrr_max_weight(svc);
+ /*
+ * Still zero, which means no availabe servers.
+ */
+ if (mark->cw == 0) {
+ IP_VS_INFO("ip_vs_wrr_schedule(): "
+ "no available servers\n");
+ return NULL;
+ }
+ }
+ }
+ else mark->cl = mark->cl->next;
+
+ if (mark->cl != &svc->destinations) {
+ /* not at the head of the list */
+ dest = list_entry(mark->cl, struct ip_vs_dest, n_list);
+ if (dest->weight >= mark->cw)
+ break;
+ }
+ }
+
+ IP_VS_DBG("WRR: server %d.%d.%d.%d:%d "
+ "activeconns %d refcnt %d weight %d\n",
+ NIPQUAD(dest->addr), ntohs(dest->port),
+ atomic_read(&dest->activeconns),
+ atomic_read(&dest->refcnt), dest->weight);
+
+ return dest;
+}
+
+
+static struct ip_vs_scheduler ip_vs_wrr_scheduler = {
+ {0}, /* n_list */
+ "wrr", /* name */
+ ATOMIC_INIT(0), /* refcnt */
+ ip_vs_wrr_init_svc, /* service initializer */
+ ip_vs_wrr_done_svc, /* service done */
+ ip_vs_wrr_update_svc, /* service updater */
+ ip_vs_wrr_schedule, /* select a server from the destination list */
+};
+
+
+__initfunc(int ip_vs_wrr_init(void))
+{
+ IP_VS_INFO("Initializing WRR scheduling\n");
+ INIT_LIST_HEAD(&ip_vs_wrr_scheduler.n_list);
+ return register_ip_vs_scheduler(&ip_vs_wrr_scheduler) ;
+}
+
+#ifdef MODULE
+EXPORT_NO_SYMBOLS;
+
+int init_module(void)
+{
+ INIT_LIST_HEAD(&ip_vs_wrr_scheduler.n_list);
+
+ /* module initialization by 'request_module' */
+ if(register_ip_vs_scheduler(&ip_vs_wrr_scheduler) != 0)
+ return -EIO;
+
+ IP_VS_INFO("WRR scheduling module loaded.\n");
+
+ return 0;
+}
+
+void cleanup_module(void)
+{
+ /* module cleanup by 'release_module' */
+ if(unregister_ip_vs_scheduler(&ip_vs_wrr_scheduler) != 0)
+ IP_VS_INFO("cannot remove WRR scheduling module\n");
+ else
+ IP_VS_INFO("WRR scheduling module unloaded.\n");
+}
+
+#endif /* MODULE */
|