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
|
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
# Controls the openvswitch module. Part of the kselftest suite, but
# can be used for some diagnostic purpose as well.
import argparse
import errno
import ipaddress
import logging
import math
import multiprocessing
import re
import socket
import struct
import sys
import time
import types
import uuid
try:
from pyroute2 import NDB
from pyroute2.netlink import NLA_F_NESTED
from pyroute2.netlink import NLM_F_ACK
from pyroute2.netlink import NLM_F_DUMP
from pyroute2.netlink import NLM_F_REQUEST
from pyroute2.netlink import genlmsg
from pyroute2.netlink import nla
from pyroute2.netlink import nlmsg_atoms
from pyroute2.netlink.event import EventSocket
from pyroute2.netlink.exceptions import NetlinkError
from pyroute2.netlink.generic import GenericNetlinkSocket
from pyroute2.netlink.nlsocket import Marshal
import pyroute2
import pyroute2.iproute
except ModuleNotFoundError:
print("Need to install the python pyroute2 package >= 0.6.")
sys.exit(1)
OVS_DATAPATH_FAMILY = "ovs_datapath"
OVS_VPORT_FAMILY = "ovs_vport"
OVS_FLOW_FAMILY = "ovs_flow"
OVS_PACKET_FAMILY = "ovs_packet"
OVS_METER_FAMILY = "ovs_meter"
OVS_CT_LIMIT_FAMILY = "ovs_ct_limit"
OVS_DATAPATH_VERSION = 2
OVS_DP_CMD_NEW = 1
OVS_DP_CMD_DEL = 2
OVS_DP_CMD_GET = 3
OVS_DP_CMD_SET = 4
OVS_VPORT_CMD_NEW = 1
OVS_VPORT_CMD_DEL = 2
OVS_VPORT_CMD_GET = 3
OVS_VPORT_CMD_SET = 4
OVS_FLOW_CMD_NEW = 1
OVS_FLOW_CMD_DEL = 2
OVS_FLOW_CMD_GET = 3
OVS_FLOW_CMD_SET = 4
UINT32_MAX = 0xFFFFFFFF
def macstr(mac):
outstr = ":".join(["%02X" % i for i in mac])
return outstr
def strcspn(str1, str2):
tot = 0
for char in str1:
if str2.find(char) != -1:
return tot
tot += 1
return tot
def strspn(str1, str2):
tot = 0
for char in str1:
if str2.find(char) == -1:
return tot
tot += 1
return tot
def intparse(statestr, defmask="0xffffffff"):
totalparse = strspn(statestr, "0123456789abcdefABCDEFx/")
# scan until "/"
count = strspn(statestr, "x0123456789abcdefABCDEF")
firstnum = statestr[:count]
if firstnum[-1] == "/":
firstnum = firstnum[:-1]
k = int(firstnum, 0)
m = None
if defmask is not None:
secondnum = defmask
if statestr[count] == "/":
secondnum = statestr[count + 1 :] # this is wrong...
m = int(secondnum, 0)
return statestr[totalparse + 1 :], k, m
def parse_flags(flag_str, flag_vals):
bitResult = 0
maskResult = 0
if len(flag_str) == 0:
return flag_str, bitResult, maskResult
if flag_str[0].isdigit():
idx = 0
while flag_str[idx].isdigit() or flag_str[idx] == "x":
idx += 1
digits = flag_str[:idx]
flag_str = flag_str[idx:]
bitResult = int(digits, 0)
maskResult = int(digits, 0)
while len(flag_str) > 0 and (flag_str[0] == "+" or flag_str[0] == "-"):
if flag_str[0] == "+":
setFlag = True
elif flag_str[0] == "-":
setFlag = False
flag_str = flag_str[1:]
flag_len = 0
while (
flag_str[flag_len] != "+"
and flag_str[flag_len] != "-"
and flag_str[flag_len] != ","
and flag_str[flag_len] != ")"
):
flag_len += 1
flag = flag_str[0:flag_len]
if flag in flag_vals:
if maskResult & flag_vals[flag]:
raise KeyError(
"Flag %s set once, cannot be set in multiples" % flag
)
if setFlag:
bitResult |= flag_vals[flag]
maskResult |= flag_vals[flag]
else:
raise KeyError("Missing flag value: %s" % flag)
flag_str = flag_str[flag_len:]
return flag_str, bitResult, maskResult
def parse_ct_state(statestr):
ct_flags = {
"new": 1 << 0,
"est": 1 << 1,
"rel": 1 << 2,
"rpl": 1 << 3,
"inv": 1 << 4,
"trk": 1 << 5,
"snat": 1 << 6,
"dnat": 1 << 7,
}
return parse_flags(statestr, ct_flags)
def convert_mac(data):
def to_bytes(mac):
mac_split = mac.split(":")
ret = bytearray([int(i, 16) for i in mac_split])
return bytes(ret)
mac_str, _, mask_str = data.partition('/')
if not mac_str:
mac_str = mask_str = "00:00:00:00:00:00"
elif not mask_str:
mask_str = "FF:FF:FF:FF:FF:FF"
return to_bytes(mac_str), to_bytes(mask_str)
def convert_ipv4(data):
ip, _, mask = data.partition('/')
if not ip:
ip = mask = 0
elif not mask:
mask = 0xFFFFFFFF
elif mask.isdigit():
mask = (0xFFFFFFFF << (32 - int(mask))) & 0xFFFFFFFF
return int(ipaddress.IPv4Address(ip)), int(ipaddress.IPv4Address(mask))
def convert_ipv6(data):
ip, _, mask = data.partition('/')
if not ip:
ip = mask = 0
elif not mask:
mask = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
elif mask.isdigit():
mask = ipaddress.IPv6Network("::/" + mask).hostmask
return ipaddress.IPv6Address(ip).packed, ipaddress.IPv6Address(mask).packed
def convert_int(size):
def convert_int_sized(data):
value, _, mask = data.partition('/')
if not value:
return 0, 0
elif not mask:
return int(value, 0), pow(2, size) - 1
else:
return int(value, 0), int(mask, 0)
return convert_int_sized
def parse_starts_block(block_str, scanstr, returnskipped, scanregex=False):
if scanregex:
m = re.search(scanstr, block_str)
if m is None:
if returnskipped:
return block_str
return False
if returnskipped:
block_str = block_str[len(m.group(0)) :]
return block_str
return True
if block_str.startswith(scanstr):
if returnskipped:
block_str = block_str[len(scanstr) :]
else:
return True
if returnskipped:
return block_str
return False
def parse_extract_field(
block_str, fieldstr, scanfmt, convert, masked=False, defval=None
):
if fieldstr and not block_str.startswith(fieldstr):
return block_str, defval
if fieldstr:
str_skiplen = len(fieldstr)
str_skipped = block_str[str_skiplen:]
if str_skiplen == 0:
return str_skipped, defval
else:
str_skiplen = 0
str_skipped = block_str
m = re.search(scanfmt, str_skipped)
if m is None:
raise ValueError("Bad fmt string")
data = m.group(0)
if convert:
data = convert(m.group(0))
str_skipped = str_skipped[len(m.group(0)) :]
if masked:
if str_skipped[0] == "/":
raise ValueError("Masking support TBD...")
str_skipped = str_skipped[strspn(str_skipped, ", ") :]
return str_skipped, data
def parse_attrs(actstr, attr_desc):
"""Parses the given action string and returns a list of netlink
attributes based on a list of attribute descriptions.
Each element in the attribute description list is a tuple such as:
(name, attr_name, parse_func)
where:
name: is the string representing the attribute
attr_name: is the name of the attribute as defined in the uAPI.
parse_func: is a callable accepting a string and returning either
a single object (the parsed attribute value) or a tuple of
two values (the parsed attribute value and the remaining string)
Returns a list of attributes and the remaining string.
"""
def parse_attr(actstr, key, func):
actstr = actstr[len(key) :]
if not func:
return None, actstr
delim = actstr[0]
actstr = actstr[1:]
if delim == "=":
pos = strcspn(actstr, ",)")
ret = func(actstr[:pos])
else:
ret = func(actstr)
if isinstance(ret, tuple):
(datum, actstr) = ret
else:
datum = ret
actstr = actstr[strcspn(actstr, ",)"):]
if delim == "(":
if not actstr or actstr[0] != ")":
raise ValueError("Action contains unbalanced parentheses")
actstr = actstr[1:]
actstr = actstr[strspn(actstr, ", ") :]
return datum, actstr
attrs = []
attr_desc = list(attr_desc)
while actstr and actstr[0] != ")" and attr_desc:
found = False
for i, (key, attr, func) in enumerate(attr_desc):
if actstr.startswith(key):
datum, actstr = parse_attr(actstr, key, func)
attrs.append([attr, datum])
found = True
del attr_desc[i]
if not found:
raise ValueError("Unknown attribute: '%s'" % actstr)
actstr = actstr[strspn(actstr, ", ") :]
if actstr[0] != ")":
raise ValueError("Action string contains extra garbage or has "
"unbalanced parenthesis: '%s'" % actstr)
return attrs, actstr[1:]
class ovs_dp_msg(genlmsg):
# include the OVS version
# We need a custom header rather than just being able to rely on
# genlmsg because fields ends up not expressing everything correctly
# if we use the canonical example of setting fields = (('customfield',),)
fields = genlmsg.fields + (("dpifindex", "I"),)
class ovsactions(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_ACTION_ATTR_UNSPEC", "none"),
("OVS_ACTION_ATTR_OUTPUT", "uint32"),
("OVS_ACTION_ATTR_USERSPACE", "userspace"),
("OVS_ACTION_ATTR_SET", "ovskey"),
("OVS_ACTION_ATTR_PUSH_VLAN", "none"),
("OVS_ACTION_ATTR_POP_VLAN", "flag"),
("OVS_ACTION_ATTR_SAMPLE", "sample"),
("OVS_ACTION_ATTR_RECIRC", "uint32"),
("OVS_ACTION_ATTR_HASH", "none"),
("OVS_ACTION_ATTR_PUSH_MPLS", "none"),
("OVS_ACTION_ATTR_POP_MPLS", "flag"),
("OVS_ACTION_ATTR_SET_MASKED", "ovskey"),
("OVS_ACTION_ATTR_CT", "ctact"),
("OVS_ACTION_ATTR_TRUNC", "uint32"),
("OVS_ACTION_ATTR_PUSH_ETH", "none"),
("OVS_ACTION_ATTR_POP_ETH", "flag"),
("OVS_ACTION_ATTR_CT_CLEAR", "flag"),
("OVS_ACTION_ATTR_PUSH_NSH", "none"),
("OVS_ACTION_ATTR_POP_NSH", "flag"),
("OVS_ACTION_ATTR_METER", "none"),
("OVS_ACTION_ATTR_CLONE", "recursive"),
("OVS_ACTION_ATTR_CHECK_PKT_LEN", "none"),
("OVS_ACTION_ATTR_ADD_MPLS", "none"),
("OVS_ACTION_ATTR_DEC_TTL", "none"),
("OVS_ACTION_ATTR_DROP", "uint32"),
("OVS_ACTION_ATTR_PSAMPLE", "psample"),
)
class psample(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_PSAMPLE_ATTR_UNSPEC", "none"),
("OVS_PSAMPLE_ATTR_GROUP", "uint32"),
("OVS_PSAMPLE_ATTR_COOKIE", "array(uint8)"),
)
def dpstr(self, more=False):
args = "group=%d" % self.get_attr("OVS_PSAMPLE_ATTR_GROUP")
cookie = self.get_attr("OVS_PSAMPLE_ATTR_COOKIE")
if cookie:
args += ",cookie(%s)" % \
"".join(format(x, "02x") for x in cookie)
return "psample(%s)" % args
def parse(self, actstr):
desc = (
("group", "OVS_PSAMPLE_ATTR_GROUP", int),
("cookie", "OVS_PSAMPLE_ATTR_COOKIE",
lambda x: list(bytearray.fromhex(x)))
)
attrs, actstr = parse_attrs(actstr, desc)
for attr in attrs:
self["attrs"].append(attr)
return actstr
class sample(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_SAMPLE_ATTR_UNSPEC", "none"),
("OVS_SAMPLE_ATTR_PROBABILITY", "uint32"),
("OVS_SAMPLE_ATTR_ACTIONS", "ovsactions"),
)
def dpstr(self, more=False):
args = []
args.append("sample={:.2f}%".format(
100 * self.get_attr("OVS_SAMPLE_ATTR_PROBABILITY") /
UINT32_MAX))
actions = self.get_attr("OVS_SAMPLE_ATTR_ACTIONS")
if actions:
args.append("actions(%s)" % actions.dpstr(more))
return "sample(%s)" % ",".join(args)
def parse(self, actstr):
def parse_nested_actions(actstr):
subacts = ovsactions()
parsed_len = subacts.parse(actstr)
return subacts, actstr[parsed_len :]
def percent_to_rate(percent):
percent = float(percent.strip('%'))
return int(math.floor(UINT32_MAX * (percent / 100.0) + .5))
desc = (
("sample", "OVS_SAMPLE_ATTR_PROBABILITY", percent_to_rate),
("actions", "OVS_SAMPLE_ATTR_ACTIONS", parse_nested_actions),
)
attrs, actstr = parse_attrs(actstr, desc)
for attr in attrs:
self["attrs"].append(attr)
return actstr
class ctact(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_CT_ATTR_NONE", "none"),
("OVS_CT_ATTR_COMMIT", "flag"),
("OVS_CT_ATTR_ZONE", "uint16"),
("OVS_CT_ATTR_MARK", "none"),
("OVS_CT_ATTR_LABELS", "none"),
("OVS_CT_ATTR_HELPER", "asciiz"),
("OVS_CT_ATTR_NAT", "natattr"),
("OVS_CT_ATTR_FORCE_COMMIT", "flag"),
("OVS_CT_ATTR_EVENTMASK", "uint32"),
("OVS_CT_ATTR_TIMEOUT", "asciiz"),
)
class natattr(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_NAT_ATTR_NONE", "none"),
("OVS_NAT_ATTR_SRC", "flag"),
("OVS_NAT_ATTR_DST", "flag"),
("OVS_NAT_ATTR_IP_MIN", "ipaddr"),
("OVS_NAT_ATTR_IP_MAX", "ipaddr"),
("OVS_NAT_ATTR_PROTO_MIN", "uint16"),
("OVS_NAT_ATTR_PROTO_MAX", "uint16"),
("OVS_NAT_ATTR_PERSISTENT", "flag"),
("OVS_NAT_ATTR_PROTO_HASH", "flag"),
("OVS_NAT_ATTR_PROTO_RANDOM", "flag"),
)
def dpstr(self, more=False):
print_str = "nat("
if self.get_attr("OVS_NAT_ATTR_SRC"):
print_str += "src"
elif self.get_attr("OVS_NAT_ATTR_DST"):
print_str += "dst"
else:
print_str += "XXX-unknown-nat"
if self.get_attr("OVS_NAT_ATTR_IP_MIN") or self.get_attr(
"OVS_NAT_ATTR_IP_MAX"
):
if self.get_attr("OVS_NAT_ATTR_IP_MIN"):
print_str += "=%s," % str(
self.get_attr("OVS_NAT_ATTR_IP_MIN")
)
if self.get_attr("OVS_NAT_ATTR_IP_MAX"):
print_str += "-%s," % str(
self.get_attr("OVS_NAT_ATTR_IP_MAX")
)
else:
print_str += ","
if self.get_attr("OVS_NAT_ATTR_PROTO_MIN"):
print_str += "proto_min=%d," % self.get_attr(
"OVS_NAT_ATTR_PROTO_MIN"
)
if self.get_attr("OVS_NAT_ATTR_PROTO_MAX"):
print_str += "proto_max=%d," % self.get_attr(
"OVS_NAT_ATTR_PROTO_MAX"
)
if self.get_attr("OVS_NAT_ATTR_PERSISTENT"):
print_str += "persistent,"
if self.get_attr("OVS_NAT_ATTR_HASH"):
print_str += "hash,"
if self.get_attr("OVS_NAT_ATTR_RANDOM"):
print_str += "random"
print_str += ")"
return print_str
def dpstr(self, more=False):
print_str = "ct("
if self.get_attr("OVS_CT_ATTR_COMMIT") is not None:
print_str += "commit,"
if self.get_attr("OVS_CT_ATTR_ZONE") is not None:
print_str += "zone=%d," % self.get_attr("OVS_CT_ATTR_ZONE")
if self.get_attr("OVS_CT_ATTR_HELPER") is not None:
print_str += "helper=%s," % self.get_attr("OVS_CT_ATTR_HELPER")
if self.get_attr("OVS_CT_ATTR_NAT") is not None:
print_str += self.get_attr("OVS_CT_ATTR_NAT").dpstr(more)
print_str += ","
if self.get_attr("OVS_CT_ATTR_FORCE_COMMIT") is not None:
print_str += "force,"
if self.get_attr("OVS_CT_ATTR_EVENTMASK") is not None:
print_str += "emask=0x%X," % self.get_attr(
"OVS_CT_ATTR_EVENTMASK"
)
if self.get_attr("OVS_CT_ATTR_TIMEOUT") is not None:
print_str += "timeout=%s" % self.get_attr(
"OVS_CT_ATTR_TIMEOUT"
)
print_str += ")"
return print_str
class userspace(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_USERSPACE_ATTR_UNUSED", "none"),
("OVS_USERSPACE_ATTR_PID", "uint32"),
("OVS_USERSPACE_ATTR_USERDATA", "array(uint8)"),
("OVS_USERSPACE_ATTR_EGRESS_TUN_PORT", "uint32"),
)
def dpstr(self, more=False):
print_str = "userspace("
if self.get_attr("OVS_USERSPACE_ATTR_PID") is not None:
print_str += "pid=%d," % self.get_attr(
"OVS_USERSPACE_ATTR_PID"
)
if self.get_attr("OVS_USERSPACE_ATTR_USERDATA") is not None:
print_str += "userdata="
for f in self.get_attr("OVS_USERSPACE_ATTR_USERDATA"):
print_str += "%x." % f
if self.get_attr("OVS_USERSPACE_ATTR_EGRESS_TUN_PORT") is not None:
print_str += "egress_tun_port=%d" % self.get_attr(
"OVS_USERSPACE_ATTR_EGRESS_TUN_PORT"
)
print_str += ")"
return print_str
def parse(self, actstr):
attrs_desc = (
("pid", "OVS_USERSPACE_ATTR_PID", int),
("userdata", "OVS_USERSPACE_ATTR_USERDATA",
lambda x: list(bytearray.fromhex(x))),
("egress_tun_port", "OVS_USERSPACE_ATTR_EGRESS_TUN_PORT", int)
)
attrs, actstr = parse_attrs(actstr, attrs_desc)
for attr in attrs:
self["attrs"].append(attr)
return actstr
def dpstr(self, more=False):
print_str = ""
for field in self["attrs"]:
if field[1] == "none" or self.get_attr(field[0]) is None:
continue
if print_str != "":
print_str += ","
if field[0] == "OVS_ACTION_ATTR_OUTPUT":
print_str += "%d" % int(self.get_attr(field[0]))
elif field[0] == "OVS_ACTION_ATTR_RECIRC":
print_str += "recirc(0x%x)" % int(self.get_attr(field[0]))
elif field[0] == "OVS_ACTION_ATTR_TRUNC":
print_str += "trunc(%d)" % int(self.get_attr(field[0]))
elif field[0] == "OVS_ACTION_ATTR_DROP":
print_str += "drop(%d)" % int(self.get_attr(field[0]))
elif field[0] == "OVS_ACTION_ATTR_CT_CLEAR":
print_str += "ct_clear"
elif field[0] == "OVS_ACTION_ATTR_POP_VLAN":
print_str += "pop_vlan"
elif field[0] == "OVS_ACTION_ATTR_POP_ETH":
print_str += "pop_eth"
elif field[0] == "OVS_ACTION_ATTR_POP_NSH":
print_str += "pop_nsh"
elif field[0] == "OVS_ACTION_ATTR_POP_MPLS":
print_str += "pop_mpls"
else:
datum = self.get_attr(field[0])
if field[0] == "OVS_ACTION_ATTR_CLONE":
print_str += "clone("
print_str += datum.dpstr(more)
print_str += ")"
elif field[0] == "OVS_ACTION_ATTR_SET" or \
field[0] == "OVS_ACTION_ATTR_SET_MASKED":
print_str += "set"
field = datum
mask = None
if field[0] == "OVS_ACTION_ATTR_SET_MASKED":
print_str += "_masked"
field = datum[0]
mask = datum[1]
print_str += "("
print_str += field.dpstr(mask, more)
print_str += ")"
else:
try:
print_str += datum.dpstr(more)
except:
print_str += "{ATTR: %s not decoded}" % field[0]
return print_str
def parse(self, actstr):
totallen = len(actstr)
while len(actstr) != 0:
parsed = False
parencount = 0
if actstr.startswith("drop"):
# If no reason is provided, the implicit drop is used (i.e no
# action). If some reason is given, an explicit action is used.
reason = None
if actstr.startswith("drop("):
parencount += 1
actstr, reason = parse_extract_field(
actstr,
"drop(",
r"([0-9]+)",
lambda x: int(x, 0),
False,
None,
)
if reason is not None:
self["attrs"].append(["OVS_ACTION_ATTR_DROP", reason])
parsed = True
else:
actstr = actstr[len("drop"): ]
return (totallen - len(actstr))
elif parse_starts_block(actstr, r"^(\d+)", False, True):
actstr, output = parse_extract_field(
actstr, None, r"(\d+)", lambda x: int(x), False, "0"
)
self["attrs"].append(["OVS_ACTION_ATTR_OUTPUT", output])
parsed = True
elif parse_starts_block(actstr, "recirc(", False):
actstr, recircid = parse_extract_field(
actstr,
"recirc(",
r"([0-9a-fA-Fx]+)",
lambda x: int(x, 0),
False,
0,
)
parencount += 1
self["attrs"].append(["OVS_ACTION_ATTR_RECIRC", recircid])
parsed = True
parse_flat_map = (
("ct_clear", "OVS_ACTION_ATTR_CT_CLEAR"),
("pop_vlan", "OVS_ACTION_ATTR_POP_VLAN"),
("pop_eth", "OVS_ACTION_ATTR_POP_ETH"),
("pop_nsh", "OVS_ACTION_ATTR_POP_NSH"),
)
for flat_act in parse_flat_map:
if parse_starts_block(actstr, flat_act[0], False):
actstr = actstr[len(flat_act[0]):]
self["attrs"].append([flat_act[1], True])
actstr = actstr[strspn(actstr, ", ") :]
parsed = True
if parse_starts_block(actstr, "clone(", False):
parencount += 1
subacts = ovsactions()
actstr = actstr[len("clone("):]
parsedLen = subacts.parse(actstr)
lst = []
self["attrs"].append(("OVS_ACTION_ATTR_CLONE", subacts))
actstr = actstr[parsedLen:]
parsed = True
elif parse_starts_block(actstr, "set(", False):
parencount += 1
k = ovskey()
actstr = actstr[len("set("):]
actstr = k.parse(actstr, None)
self["attrs"].append(("OVS_ACTION_ATTR_SET", k))
if not actstr.startswith(")"):
actstr = ")" + actstr
parsed = True
elif parse_starts_block(actstr, "set_masked(", False):
parencount += 1
k = ovskey()
m = ovskey()
actstr = actstr[len("set_masked("):]
actstr = k.parse(actstr, m)
self["attrs"].append(("OVS_ACTION_ATTR_SET_MASKED", [k, m]))
if not actstr.startswith(")"):
actstr = ")" + actstr
parsed = True
elif parse_starts_block(actstr, "ct(", False):
parencount += 1
actstr = actstr[len("ct(") :]
ctact = ovsactions.ctact()
for scan in (
("commit", "OVS_CT_ATTR_COMMIT", None),
("force_commit", "OVS_CT_ATTR_FORCE_COMMIT", None),
("zone", "OVS_CT_ATTR_ZONE", int),
("mark", "OVS_CT_ATTR_MARK", int),
("helper", "OVS_CT_ATTR_HELPER", lambda x, y: str(x)),
("timeout", "OVS_CT_ATTR_TIMEOUT", lambda x, y: str(x)),
):
if actstr.startswith(scan[0]):
actstr = actstr[len(scan[0]) :]
if scan[2] is not None:
if actstr[0] != "=":
raise ValueError("Invalid ct attr")
actstr = actstr[1:]
pos = strcspn(actstr, ",)")
datum = scan[2](actstr[:pos], 0)
ctact["attrs"].append([scan[1], datum])
actstr = actstr[pos:]
else:
ctact["attrs"].append([scan[1], None])
actstr = actstr[strspn(actstr, ", ") :]
# it seems strange to put this here, but nat() is a complex
# sub-action and this lets it sit anywhere in the ct() action
if actstr.startswith("nat"):
actstr = actstr[3:]
natact = ovsactions.ctact.natattr()
if actstr.startswith("("):
parencount += 1
t = None
actstr = actstr[1:]
if actstr.startswith("src"):
t = "OVS_NAT_ATTR_SRC"
actstr = actstr[3:]
elif actstr.startswith("dst"):
t = "OVS_NAT_ATTR_DST"
actstr = actstr[3:]
actstr, ip_block_min = parse_extract_field(
actstr, "=", r"([0-9a-fA-F\.]+)", str, False
)
actstr, ip_block_max = parse_extract_field(
actstr, "-", r"([0-9a-fA-F\.]+)", str, False
)
actstr, proto_min = parse_extract_field(
actstr, ":", r"(\d+)", int, False
)
actstr, proto_max = parse_extract_field(
actstr, "-", r"(\d+)", int, False
)
if t is not None:
natact["attrs"].append([t, None])
if ip_block_min is not None:
natact["attrs"].append(
["OVS_NAT_ATTR_IP_MIN", ip_block_min]
)
if ip_block_max is not None:
natact["attrs"].append(
["OVS_NAT_ATTR_IP_MAX", ip_block_max]
)
if proto_min is not None:
natact["attrs"].append(
["OVS_NAT_ATTR_PROTO_MIN", proto_min]
)
if proto_max is not None:
natact["attrs"].append(
["OVS_NAT_ATTR_PROTO_MAX", proto_max]
)
for natscan in (
("persistent", "OVS_NAT_ATTR_PERSISTENT"),
("hash", "OVS_NAT_ATTR_PROTO_HASH"),
("random", "OVS_NAT_ATTR_PROTO_RANDOM"),
):
if actstr.startswith(natscan[0]):
actstr = actstr[len(natscan[0]) :]
natact["attrs"].append([natscan[1], None])
actstr = actstr[strspn(actstr, ", ") :]
ctact["attrs"].append(["OVS_CT_ATTR_NAT", natact])
actstr = actstr[strspn(actstr, ", ") :]
self["attrs"].append(["OVS_ACTION_ATTR_CT", ctact])
parsed = True
elif parse_starts_block(actstr, "sample(", False):
sampleact = self.sample()
actstr = sampleact.parse(actstr[len("sample(") : ])
self["attrs"].append(["OVS_ACTION_ATTR_SAMPLE", sampleact])
parsed = True
elif parse_starts_block(actstr, "psample(", False):
psampleact = self.psample()
actstr = psampleact.parse(actstr[len("psample(") : ])
self["attrs"].append(["OVS_ACTION_ATTR_PSAMPLE", psampleact])
parsed = True
elif parse_starts_block(actstr, "userspace(", False):
uact = self.userspace()
actstr = uact.parse(actstr[len("userspace(") : ])
self["attrs"].append(["OVS_ACTION_ATTR_USERSPACE", uact])
parsed = True
elif parse_starts_block(actstr, "trunc(", False):
parencount += 1
actstr, val = parse_extract_field(
actstr,
"trunc(",
r"([0-9]+)",
int,
False,
None,
)
self["attrs"].append(["OVS_ACTION_ATTR_TRUNC", val])
parsed = True
actstr = actstr[strspn(actstr, ", ") :]
while parencount > 0:
parencount -= 1
actstr = actstr[strspn(actstr, " "):]
if len(actstr) and actstr[0] != ")":
raise ValueError("Action str: '%s' unbalanced" % actstr)
actstr = actstr[1:]
if len(actstr) and actstr[0] == ")":
return (totallen - len(actstr))
actstr = actstr[strspn(actstr, ", ") :]
if not parsed:
raise ValueError("Action str: '%s' not supported" % actstr)
return (totallen - len(actstr))
class ovskey(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_KEY_ATTR_UNSPEC", "none"),
("OVS_KEY_ATTR_ENCAP", "none"),
("OVS_KEY_ATTR_PRIORITY", "uint32"),
("OVS_KEY_ATTR_IN_PORT", "uint32"),
("OVS_KEY_ATTR_ETHERNET", "ethaddr"),
("OVS_KEY_ATTR_VLAN", "uint16"),
("OVS_KEY_ATTR_ETHERTYPE", "be16"),
("OVS_KEY_ATTR_IPV4", "ovs_key_ipv4"),
("OVS_KEY_ATTR_IPV6", "ovs_key_ipv6"),
("OVS_KEY_ATTR_TCP", "ovs_key_tcp"),
("OVS_KEY_ATTR_UDP", "ovs_key_udp"),
("OVS_KEY_ATTR_ICMP", "ovs_key_icmp"),
("OVS_KEY_ATTR_ICMPV6", "ovs_key_icmpv6"),
("OVS_KEY_ATTR_ARP", "ovs_key_arp"),
("OVS_KEY_ATTR_ND", "ovs_key_nd"),
("OVS_KEY_ATTR_SKB_MARK", "uint32"),
("OVS_KEY_ATTR_TUNNEL", "ovs_key_tunnel"),
("OVS_KEY_ATTR_SCTP", "ovs_key_sctp"),
("OVS_KEY_ATTR_TCP_FLAGS", "be16"),
("OVS_KEY_ATTR_DP_HASH", "uint32"),
("OVS_KEY_ATTR_RECIRC_ID", "uint32"),
("OVS_KEY_ATTR_MPLS", "array(ovs_key_mpls)"),
("OVS_KEY_ATTR_CT_STATE", "uint32"),
("OVS_KEY_ATTR_CT_ZONE", "uint16"),
("OVS_KEY_ATTR_CT_MARK", "uint32"),
("OVS_KEY_ATTR_CT_LABELS", "none"),
("OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4", "ovs_key_ct_tuple_ipv4"),
("OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6", "ovs_key_ct_tuple_ipv6"),
("OVS_KEY_ATTR_NSH", "none"),
("OVS_KEY_ATTR_PACKET_TYPE", "none"),
("OVS_KEY_ATTR_ND_EXTENSIONS", "none"),
("OVS_KEY_ATTR_TUNNEL_INFO", "none"),
("OVS_KEY_ATTR_IPV6_EXTENSIONS", "none"),
)
class ovs_key_proto(nla):
fields = (
("src", "!H"),
("dst", "!H"),
)
fields_map = (
("src", "src", "%d", lambda x: int(x) if x else 0,
convert_int(16)),
("dst", "dst", "%d", lambda x: int(x) if x else 0,
convert_int(16)),
)
def __init__(
self,
protostr,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
self.proto_str = protostr
nla.__init__(
self,
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
def parse(self, flowstr, typeInst):
if not flowstr.startswith(self.proto_str):
return None, None
k = typeInst()
m = typeInst()
flowstr = flowstr[len(self.proto_str) :]
if flowstr.startswith("("):
flowstr = flowstr[1:]
keybits = b""
maskbits = b""
for f in self.fields_map:
if flowstr.startswith(f[1]):
# the following assumes that the field looks
# something like 'field.' where '.' is a
# character that we don't exactly care about.
flowstr = flowstr[len(f[1]) + 1 :]
splitchar = 0
for c in flowstr:
if c == "," or c == ")":
break
splitchar += 1
data = flowstr[:splitchar]
flowstr = flowstr[splitchar:]
else:
data = ""
if len(f) > 4:
k[f[0]], m[f[0]] = f[4](data)
else:
k[f[0]] = f[3](data)
m[f[0]] = f[3](data)
flowstr = flowstr[strspn(flowstr, ", ") :]
if len(flowstr) == 0:
return flowstr, k, m
flowstr = flowstr[strspn(flowstr, "), ") :]
return flowstr, k, m
def dpstr(self, masked=None, more=False):
outstr = self.proto_str + "("
first = False
for f in self.fields_map:
if first:
outstr += ","
if masked is None:
outstr += "%s=" % f[0]
if isinstance(f[2], str):
outstr += f[2] % self[f[1]]
else:
outstr += f[2](self[f[1]])
first = True
elif more or f[3](masked[f[1]]) != 0:
outstr += "%s=" % f[0]
if isinstance(f[2], str):
outstr += f[2] % self[f[1]]
else:
outstr += f[2](self[f[1]])
outstr += "/"
if isinstance(f[2], str):
outstr += f[2] % masked[f[1]]
else:
outstr += f[2](masked[f[1]])
first = True
outstr += ")"
return outstr
class ethaddr(ovs_key_proto):
fields = (
("src", "!6s"),
("dst", "!6s"),
)
fields_map = (
(
"src",
"src",
macstr,
lambda x: int.from_bytes(x, "big"),
convert_mac,
),
(
"dst",
"dst",
macstr,
lambda x: int.from_bytes(x, "big"),
convert_mac,
),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"eth",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_ipv4(ovs_key_proto):
fields = (
("src", "!I"),
("dst", "!I"),
("proto", "B"),
("tos", "B"),
("ttl", "B"),
("frag", "B"),
)
fields_map = (
(
"src",
"src",
lambda x: str(ipaddress.IPv4Address(x)),
int,
convert_ipv4,
),
(
"dst",
"dst",
lambda x: str(ipaddress.IPv4Address(x)),
int,
convert_ipv4,
),
("proto", "proto", "%d", lambda x: int(x) if x else 0,
convert_int(8)),
("tos", "tos", "%d", lambda x: int(x) if x else 0,
convert_int(8)),
("ttl", "ttl", "%d", lambda x: int(x) if x else 0,
convert_int(8)),
("frag", "frag", "%d", lambda x: int(x) if x else 0,
convert_int(8)),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"ipv4",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_ipv6(ovs_key_proto):
fields = (
("src", "!16s"),
("dst", "!16s"),
("label", "!I"),
("proto", "B"),
("tclass", "B"),
("hlimit", "B"),
("frag", "B"),
)
fields_map = (
(
"src",
"src",
lambda x: str(ipaddress.IPv6Address(x)),
lambda x: ipaddress.IPv6Address(x).packed if x else 0,
convert_ipv6,
),
(
"dst",
"dst",
lambda x: str(ipaddress.IPv6Address(x)),
lambda x: ipaddress.IPv6Address(x).packed if x else 0,
convert_ipv6,
),
("label", "label", "%d", lambda x: int(x) if x else 0),
("proto", "proto", "%d", lambda x: int(x) if x else 0),
("tclass", "tclass", "%d", lambda x: int(x) if x else 0),
("hlimit", "hlimit", "%d", lambda x: int(x) if x else 0),
("frag", "frag", "%d", lambda x: int(x) if x else 0),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"ipv6",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_tcp(ovs_key_proto):
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"tcp",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_udp(ovs_key_proto):
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"udp",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_sctp(ovs_key_proto):
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"sctp",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_icmp(ovs_key_proto):
fields = (
("type", "B"),
("code", "B"),
)
fields_map = (
("type", "type", "%d", lambda x: int(x) if x else 0),
("code", "code", "%d", lambda x: int(x) if x else 0),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"icmp",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_icmpv6(ovs_key_icmp):
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"icmpv6",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_arp(ovs_key_proto):
fields = (
("sip", "!I"),
("tip", "!I"),
("op", "!H"),
("sha", "!6s"),
("tha", "!6s"),
("pad", "xx"),
)
fields_map = (
(
"sip",
"sip",
lambda x: str(ipaddress.IPv4Address(x)),
int,
convert_ipv4,
),
(
"tip",
"tip",
lambda x: str(ipaddress.IPv4Address(x)),
int,
convert_ipv4,
),
("op", "op", "%d", lambda x: int(x) if x else 0),
(
"sha",
"sha",
macstr,
lambda x: int.from_bytes(x, "big"),
convert_mac,
),
(
"tha",
"tha",
macstr,
lambda x: int.from_bytes(x, "big"),
convert_mac,
),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"arp",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_nd(ovs_key_proto):
fields = (
("target", "!16s"),
("sll", "!6s"),
("tll", "!6s"),
)
fields_map = (
(
"target",
"target",
lambda x: str(ipaddress.IPv6Address(x)),
convert_ipv6,
),
("sll", "sll", macstr, lambda x: int.from_bytes(x, "big")),
("tll", "tll", macstr, lambda x: int.from_bytes(x, "big")),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"nd",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_ct_tuple_ipv4(ovs_key_proto):
fields = (
("src", "!I"),
("dst", "!I"),
("tp_src", "!H"),
("tp_dst", "!H"),
("proto", "B"),
)
fields_map = (
(
"src",
"src",
lambda x: str(ipaddress.IPv4Address(x)),
int,
convert_ipv4,
),
(
"dst",
"dst",
lambda x: str(ipaddress.IPv4Address(x)),
int,
convert_ipv4,
),
("tp_src", "tp_src", "%d", int),
("tp_dst", "tp_dst", "%d", int),
("proto", "proto", "%d", int),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"ct_tuple4",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_ct_tuple_ipv6(nla):
fields = (
("src", "!16s"),
("dst", "!16s"),
("tp_src", "!H"),
("tp_dst", "!H"),
("proto", "B"),
)
fields_map = (
(
"src",
"src",
lambda x: str(ipaddress.IPv6Address(x)),
convert_ipv6,
),
(
"dst",
"dst",
lambda x: str(ipaddress.IPv6Address(x)),
convert_ipv6,
),
("tp_src", "tp_src", "%d", int),
("tp_dst", "tp_dst", "%d", int),
("proto", "proto", "%d", int),
)
def __init__(
self,
data=None,
offset=None,
parent=None,
length=None,
init=None,
):
ovskey.ovs_key_proto.__init__(
self,
"ct_tuple6",
data=data,
offset=offset,
parent=parent,
length=length,
init=init,
)
class ovs_key_tunnel(nla):
nla_flags = NLA_F_NESTED
nla_map = (
("OVS_TUNNEL_KEY_ATTR_ID", "be64"),
("OVS_TUNNEL_KEY_ATTR_IPV4_SRC", "ipaddr"),
("OVS_TUNNEL_KEY_ATTR_IPV4_DST", "ipaddr"),
("OVS_TUNNEL_KEY_ATTR_TOS", "uint8"),
("OVS_TUNNEL_KEY_ATTR_TTL", "uint8"),
("OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT", "flag"),
("OVS_TUNNEL_KEY_ATTR_CSUM", "flag"),
("OVS_TUNNEL_KEY_ATTR_OAM", "flag"),
("OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS", "array(uint32)"),
("OVS_TUNNEL_KEY_ATTR_TP_SRC", "be16"),
("OVS_TUNNEL_KEY_ATTR_TP_DST", "be16"),
("OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS", "none"),
("OVS_TUNNEL_KEY_ATTR_IPV6_SRC", "ipaddr"),
("OVS_TUNNEL_KEY_ATTR_IPV6_DST", "ipaddr"),
("OVS_TUNNEL_KEY_ATTR_PAD", "none"),
("OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS", "none"),
("OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE", "flag"),
)
def parse(self, flowstr, mask=None):
if not flowstr.startswith("tunnel("):
return None, None
k = ovskey.ovs_key_tunnel()
if mask is not None:
mask = ovskey.ovs_key_tunnel()
flowstr = flowstr[len("tunnel("):]
v6_address = None
fields = [
("tun_id=", r"(\d+)", int, "OVS_TUNNEL_KEY_ATTR_ID",
0xffffffffffffffff, None, None),
("src=", r"([0-9a-fA-F\.]+)", str,
"OVS_TUNNEL_KEY_ATTR_IPV4_SRC", "255.255.255.255", "0.0.0.0",
False),
("dst=", r"([0-9a-fA-F\.]+)", str,
"OVS_TUNNEL_KEY_ATTR_IPV4_DST", "255.255.255.255", "0.0.0.0",
False),
("ipv6_src=", r"([0-9a-fA-F:]+)", str,
"OVS_TUNNEL_KEY_ATTR_IPV6_SRC",
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "::", True),
("ipv6_dst=", r"([0-9a-fA-F:]+)", str,
"OVS_TUNNEL_KEY_ATTR_IPV6_DST",
"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "::", True),
("tos=", r"(\d+)", int, "OVS_TUNNEL_KEY_ATTR_TOS", 255, 0,
None),
("ttl=", r"(\d+)", int, "OVS_TUNNEL_KEY_ATTR_TTL", 255, 0,
None),
("tp_src=", r"(\d+)", int, "OVS_TUNNEL_KEY_ATTR_TP_SRC",
65535, 0, None),
("tp_dst=", r"(\d+)", int, "OVS_TUNNEL_KEY_ATTR_TP_DST",
65535, 0, None),
]
forced_include = ["OVS_TUNNEL_KEY_ATTR_TTL"]
for prefix, regex, typ, attr_name, mask_val, default_val, v46_flag in fields:
flowstr, value = parse_extract_field(flowstr, prefix, regex, typ, False)
if not attr_name:
raise Exception("Bad list value in tunnel fields")
if value is None and attr_name in forced_include:
value = default_val
mask_val = default_val
if value is not None:
if v46_flag is not None:
if v6_address is None:
v6_address = v46_flag
if v46_flag != v6_address:
raise ValueError("Cannot mix v6 and v4 addresses")
k["attrs"].append([attr_name, value])
if mask is not None:
mask["attrs"].append([attr_name, mask_val])
else:
if v46_flag is not None:
if v6_address is None or v46_flag != v6_address:
continue
if mask is not None:
mask["attrs"].append([attr_name, default_val])
if k["attrs"][0][0] != "OVS_TUNNEL_KEY_ATTR_ID":
raise ValueError("Needs a tunid set")
if flowstr.startswith("flags("):
flowstr = flowstr[len("flags("):]
flagspos = flowstr.find(")")
flags = flowstr[:flagspos]
flowstr = flowstr[flagspos + 1:]
flag_attrs = {
"df": "OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT",
"csum": "OVS_TUNNEL_KEY_ATTR_CSUM",
"oam": "OVS_TUNNEL_KEY_ATTR_OAM"
}
for flag in flags.split("|"):
if flag in flag_attrs:
k["attrs"].append([flag_attrs[flag], True])
if mask is not None:
mask["attrs"].append([flag_attrs[flag], True])
flowstr = flowstr[strspn(flowstr, ", ") :]
return flowstr, k, mask
def dpstr(self, mask=None, more=False):
print_str = "tunnel("
flagsattrs = []
for k in self["attrs"]:
noprint = False
if k[0] == "OVS_TUNNEL_KEY_ATTR_ID":
print_str += "tun_id=%d" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_IPV4_SRC":
print_str += "src=%s" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_IPV4_DST":
print_str += "dst=%s" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_IPV6_SRC":
print_str += "ipv6_src=%s" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_IPV6_DST":
print_str += "ipv6_dst=%s" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_TOS":
print_str += "tos=%d" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_TTL":
print_str += "ttl=%d" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_TP_SRC":
print_str += "tp_src=%d" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_TP_DST":
print_str += "tp_dst=%d" % k[1]
elif k[0] == "OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT":
noprint = True
flagsattrs.append("df")
elif k[0] == "OVS_TUNNEL_KEY_ATTR_CSUM":
noprint = True
flagsattrs.append("csum")
elif k[0] == "OVS_TUNNEL_KEY_ATTR_OAM":
noprint = True
flagsattrs.append("oam")
if not noprint:
print_str += ","
if len(flagsattrs):
print_str += "flags(" + "|".join(flagsattrs) + ")"
print_str += ")"
return print_str
class ovs_key_mpls(nla):
fields = (("lse", ">I"),)
def parse(self, flowstr, mask=None):
for field in (
("OVS_KEY_ATTR_PRIORITY", "skb_priority", intparse),
("OVS_KEY_ATTR_SKB_MARK", "skb_mark", intparse),
("OVS_KEY_ATTR_RECIRC_ID", "recirc_id", intparse),
("OVS_KEY_ATTR_TUNNEL", "tunnel", ovskey.ovs_key_tunnel),
("OVS_KEY_ATTR_DP_HASH", "dp_hash", intparse),
("OVS_KEY_ATTR_CT_STATE", "ct_state", parse_ct_state),
("OVS_KEY_ATTR_CT_ZONE", "ct_zone", intparse),
("OVS_KEY_ATTR_CT_MARK", "ct_mark", intparse),
("OVS_KEY_ATTR_IN_PORT", "in_port", intparse),
(
"OVS_KEY_ATTR_ETHERNET",
"eth",
ovskey.ethaddr,
),
(
"OVS_KEY_ATTR_ETHERTYPE",
"eth_type",
lambda x: intparse(x, "0xffff"),
),
(
"OVS_KEY_ATTR_IPV4",
"ipv4",
ovskey.ovs_key_ipv4,
),
(
"OVS_KEY_ATTR_IPV6",
"ipv6",
ovskey.ovs_key_ipv6,
),
(
"OVS_KEY_ATTR_ARP",
"arp",
ovskey.ovs_key_arp,
),
(
"OVS_KEY_ATTR_TCP",
"tcp",
ovskey.ovs_key_tcp,
),
(
"OVS_KEY_ATTR_UDP",
"udp",
ovskey.ovs_key_udp,
),
(
"OVS_KEY_ATTR_ICMP",
"icmp",
ovskey.ovs_key_icmp,
),
(
"OVS_KEY_ATTR_TCP_FLAGS",
"tcp_flags",
lambda x: parse_flags(x, None),
),
):
fld = field[1] + "("
if not flowstr.startswith(fld):
continue
if not isinstance(field[2], types.FunctionType):
nk = field[2]()
flowstr, k, m = nk.parse(flowstr, field[2])
else:
flowstr = flowstr[len(fld) :]
flowstr, k, m = field[2](flowstr)
if m and mask is not None:
mask["attrs"].append([field[0], m])
self["attrs"].append([field[0], k])
flowstr = flowstr[strspn(flowstr, "), ") :]
return flowstr
def dpstr(self, mask=None, more=False):
print_str = ""
for field in (
(
"OVS_KEY_ATTR_PRIORITY",
"skb_priority",
"%d",
lambda x: False,
True,
),
(
"OVS_KEY_ATTR_SKB_MARK",
"skb_mark",
"%d",
lambda x: False,
True,
),
(
"OVS_KEY_ATTR_RECIRC_ID",
"recirc_id",
"0x%08X",
lambda x: False,
True,
),
(
"OVS_KEY_ATTR_DP_HASH",
"dp_hash",
"0x%08X",
lambda x: False,
True,
),
(
"OVS_KEY_ATTR_TUNNEL",
"tunnel",
None,
False,
False,
),
(
"OVS_KEY_ATTR_CT_STATE",
"ct_state",
"0x%04x",
lambda x: False,
True,
),
(
"OVS_KEY_ATTR_CT_ZONE",
"ct_zone",
"0x%04x",
lambda x: False,
True,
),
(
"OVS_KEY_ATTR_CT_MARK",
"ct_mark",
"0x%08x",
lambda x: False,
True,
),
(
"OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4",
None,
None,
False,
False,
),
(
"OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6",
None,
None,
False,
False,
),
(
"OVS_KEY_ATTR_IN_PORT",
"in_port",
"%d",
lambda x: True,
True,
),
("OVS_KEY_ATTR_ETHERNET", None, None, False, False),
(
"OVS_KEY_ATTR_ETHERTYPE",
"eth_type",
"0x%04x",
lambda x: int(x) == 0xFFFF,
True,
),
("OVS_KEY_ATTR_IPV4", None, None, False, False),
("OVS_KEY_ATTR_IPV6", None, None, False, False),
("OVS_KEY_ATTR_ARP", None, None, False, False),
("OVS_KEY_ATTR_TCP", None, None, False, False),
(
"OVS_KEY_ATTR_TCP_FLAGS",
"tcp_flags",
"0x%04x",
lambda x: False,
True,
),
("OVS_KEY_ATTR_UDP", None, None, False, False),
("OVS_KEY_ATTR_SCTP", None, None, False, False),
("OVS_KEY_ATTR_ICMP", None, None, False, False),
("OVS_KEY_ATTR_ICMPV6", None, None, False, False),
("OVS_KEY_ATTR_ND", None, None, False, False),
):
v = self.get_attr(field[0])
if v is not None:
m = None if mask is None else mask.get_attr(field[0])
if field[4] is False:
print_str += v.dpstr(m, more)
print_str += ","
else:
if m is None or field[3](m):
print_str += field[1] + "("
print_str += field[2] % v
print_str += "),"
elif more or m != 0:
print_str += field[1] + "("
print_str += (field[2] % v) + "/" + (field[2] % m)
print_str += "),"
return print_str
class OvsPacket(GenericNetlinkSocket):
OVS_PACKET_CMD_MISS = 1 # Flow table miss
OVS_PACKET_CMD_ACTION = 2 # USERSPACE action
OVS_PACKET_CMD_EXECUTE = 3 # Apply actions to packet
class ovs_packet_msg(ovs_dp_msg):
nla_map = (
("OVS_PACKET_ATTR_UNSPEC", "none"),
("OVS_PACKET_ATTR_PACKET", "array(uint8)"),
("OVS_PACKET_ATTR_KEY", "ovskey"),
("OVS_PACKET_ATTR_ACTIONS", "ovsactions"),
("OVS_PACKET_ATTR_USERDATA", "none"),
("OVS_PACKET_ATTR_EGRESS_TUN_KEY", "none"),
("OVS_PACKET_ATTR_UNUSED1", "none"),
("OVS_PACKET_ATTR_UNUSED2", "none"),
("OVS_PACKET_ATTR_PROBE", "none"),
("OVS_PACKET_ATTR_MRU", "uint16"),
("OVS_PACKET_ATTR_LEN", "uint32"),
("OVS_PACKET_ATTR_HASH", "uint64"),
)
def __init__(self):
GenericNetlinkSocket.__init__(self)
self.bind(OVS_PACKET_FAMILY, OvsPacket.ovs_packet_msg)
def upcall_handler(self, up=None):
print("listening on upcall packet handler:", self.epid)
while True:
try:
msgs = self.get()
for msg in msgs:
if not up:
continue
if msg["cmd"] == OvsPacket.OVS_PACKET_CMD_MISS:
up.miss(msg)
elif msg["cmd"] == OvsPacket.OVS_PACKET_CMD_ACTION:
up.action(msg)
elif msg["cmd"] == OvsPacket.OVS_PACKET_CMD_EXECUTE:
up.execute(msg)
else:
print("Unkonwn cmd: %d" % msg["cmd"])
except NetlinkError as ne:
raise ne
class OvsDatapath(GenericNetlinkSocket):
OVS_DP_F_VPORT_PIDS = 1 << 1
OVS_DP_F_DISPATCH_UPCALL_PER_CPU = 1 << 3
class dp_cmd_msg(ovs_dp_msg):
"""
Message class that will be used to communicate with the kernel module.
"""
nla_map = (
("OVS_DP_ATTR_UNSPEC", "none"),
("OVS_DP_ATTR_NAME", "asciiz"),
("OVS_DP_ATTR_UPCALL_PID", "array(uint32)"),
("OVS_DP_ATTR_STATS", "dpstats"),
("OVS_DP_ATTR_MEGAFLOW_STATS", "megaflowstats"),
("OVS_DP_ATTR_USER_FEATURES", "uint32"),
("OVS_DP_ATTR_PAD", "none"),
("OVS_DP_ATTR_MASKS_CACHE_SIZE", "uint32"),
("OVS_DP_ATTR_PER_CPU_PIDS", "array(uint32)"),
)
class dpstats(nla):
fields = (
("hit", "=Q"),
("missed", "=Q"),
("lost", "=Q"),
("flows", "=Q"),
)
class megaflowstats(nla):
fields = (
("mask_hit", "=Q"),
("masks", "=I"),
("padding", "=I"),
("cache_hits", "=Q"),
("pad1", "=Q"),
)
def __init__(self):
GenericNetlinkSocket.__init__(self)
self.bind(OVS_DATAPATH_FAMILY, OvsDatapath.dp_cmd_msg)
def info(self, dpname, ifindex=0):
msg = OvsDatapath.dp_cmd_msg()
msg["cmd"] = OVS_DP_CMD_GET
msg["version"] = OVS_DATAPATH_VERSION
msg["reserved"] = 0
msg["dpifindex"] = ifindex
msg["attrs"].append(["OVS_DP_ATTR_NAME", dpname])
try:
reply = self.nlm_request(
msg, msg_type=self.prid, msg_flags=NLM_F_REQUEST
)
reply = reply[0]
except NetlinkError as ne:
if ne.code == errno.ENODEV:
reply = None
else:
raise ne
return reply
def create(
self, dpname, shouldUpcall=False, versionStr=None, p=OvsPacket()
):
msg = OvsDatapath.dp_cmd_msg()
msg["cmd"] = OVS_DP_CMD_NEW
if versionStr is None:
msg["version"] = OVS_DATAPATH_VERSION
else:
msg["version"] = int(versionStr.split(":")[0], 0)
msg["reserved"] = 0
msg["dpifindex"] = 0
msg["attrs"].append(["OVS_DP_ATTR_NAME", dpname])
dpfeatures = 0
if versionStr is not None and versionStr.find(":") != -1:
dpfeatures = int(versionStr.split(":")[1], 0)
else:
if versionStr is None or versionStr.find(":") == -1:
dpfeatures |= OvsDatapath.OVS_DP_F_DISPATCH_UPCALL_PER_CPU
dpfeatures &= ~OvsDatapath.OVS_DP_F_VPORT_PIDS
nproc = multiprocessing.cpu_count()
procarray = []
for i in range(1, nproc):
procarray += [int(p.epid)]
msg["attrs"].append(["OVS_DP_ATTR_UPCALL_PID", procarray])
msg["attrs"].append(["OVS_DP_ATTR_USER_FEATURES", dpfeatures])
if not shouldUpcall:
msg["attrs"].append(["OVS_DP_ATTR_UPCALL_PID", [0]])
try:
reply = self.nlm_request(
msg, msg_type=self.prid, msg_flags=NLM_F_REQUEST | NLM_F_ACK
)
reply = reply[0]
except NetlinkError as ne:
if ne.code == errno.EEXIST:
reply = None
else:
raise ne
return reply
def destroy(self, dpname):
msg = OvsDatapath.dp_cmd_msg()
msg["cmd"] = OVS_DP_CMD_DEL
msg["version"] = OVS_DATAPATH_VERSION
msg["reserved"] = 0
msg["dpifindex"] = 0
msg["attrs"].append(["OVS_DP_ATTR_NAME", dpname])
try:
reply = self.nlm_request(
msg, msg_type=self.prid, msg_flags=NLM_F_REQUEST | NLM_F_ACK
)
reply = reply[0]
except NetlinkError as ne:
if ne.code == errno.ENODEV:
reply = None
else:
raise ne
return reply
class OvsVport(GenericNetlinkSocket):
OVS_VPORT_TYPE_NETDEV = 1
OVS_VPORT_TYPE_INTERNAL = 2
OVS_VPORT_TYPE_GRE = 3
OVS_VPORT_TYPE_VXLAN = 4
OVS_VPORT_TYPE_GENEVE = 5
class ovs_vport_msg(ovs_dp_msg):
nla_map = (
("OVS_VPORT_ATTR_UNSPEC", "none"),
("OVS_VPORT_ATTR_PORT_NO", "uint32"),
("OVS_VPORT_ATTR_TYPE", "uint32"),
("OVS_VPORT_ATTR_NAME", "asciiz"),
("OVS_VPORT_ATTR_OPTIONS", "vportopts"),
("OVS_VPORT_ATTR_UPCALL_PID", "array(uint32)"),
("OVS_VPORT_ATTR_STATS", "vportstats"),
("OVS_VPORT_ATTR_PAD", "none"),
("OVS_VPORT_ATTR_IFINDEX", "uint32"),
("OVS_VPORT_ATTR_NETNSID", "uint32"),
)
class vportopts(nla):
nla_map = (
("OVS_TUNNEL_ATTR_UNSPEC", "none"),
("OVS_TUNNEL_ATTR_DST_PORT", "uint16"),
("OVS_TUNNEL_ATTR_EXTENSION", "none"),
)
class vportstats(nla):
fields = (
("rx_packets", "=Q"),
("tx_packets", "=Q"),
("rx_bytes", "=Q"),
("tx_bytes", "=Q"),
("rx_errors", "=Q"),
("tx_errors", "=Q"),
("rx_dropped", "=Q"),
("tx_dropped", "=Q"),
)
def type_to_str(vport_type):
if vport_type == OvsVport.OVS_VPORT_TYPE_NETDEV:
return "netdev"
elif vport_type == OvsVport.OVS_VPORT_TYPE_INTERNAL:
return "internal"
elif vport_type == OvsVport.OVS_VPORT_TYPE_GRE:
return "gre"
elif vport_type == OvsVport.OVS_VPORT_TYPE_VXLAN:
return "vxlan"
elif vport_type == OvsVport.OVS_VPORT_TYPE_GENEVE:
return "geneve"
raise ValueError("Unknown vport type:%d" % vport_type)
def str_to_type(vport_type):
if vport_type == "netdev":
return OvsVport.OVS_VPORT_TYPE_NETDEV
elif vport_type == "internal":
return OvsVport.OVS_VPORT_TYPE_INTERNAL
elif vport_type == "gre":
return OvsVport.OVS_VPORT_TYPE_INTERNAL
elif vport_type == "vxlan":
return OvsVport.OVS_VPORT_TYPE_VXLAN
elif vport_type == "geneve":
return OvsVport.OVS_VPORT_TYPE_GENEVE
raise ValueError("Unknown vport type: '%s'" % vport_type)
def __init__(self, packet=OvsPacket()):
GenericNetlinkSocket.__init__(self)
self.bind(OVS_VPORT_FAMILY, OvsVport.ovs_vport_msg)
self.upcall_packet = packet
def info(self, vport_name, dpifindex=0, portno=None):
msg = OvsVport.ovs_vport_msg()
msg["cmd"] = OVS_VPORT_CMD_GET
msg["version"] = OVS_DATAPATH_VERSION
msg["reserved"] = 0
msg["dpifindex"] = dpifindex
if portno is None:
msg["attrs"].append(["OVS_VPORT_ATTR_NAME", vport_name])
else:
msg["attrs"].append(["OVS_VPORT_ATTR_PORT_NO", portno])
try:
reply = self.nlm_request(
msg, msg_type=self.prid, msg_flags=NLM_F_REQUEST
)
reply = reply[0]
except NetlinkError as ne:
if ne.code == errno.ENODEV:
reply = None
else:
raise ne
return reply
def attach(self, dpindex, vport_ifname, ptype, dport, lwt):
msg = OvsVport.ovs_vport_msg()
msg["cmd"] = OVS_VPORT_CMD_NEW
msg["version"] = OVS_DATAPATH_VERSION
msg["reserved"] = 0
msg["dpifindex"] = dpindex
port_type = OvsVport.str_to_type(ptype)
msg["attrs"].append(["OVS_VPORT_ATTR_NAME", vport_ifname])
msg["attrs"].append(
["OVS_VPORT_ATTR_UPCALL_PID", [self.upcall_packet.epid]]
)
TUNNEL_DEFAULTS = [("geneve", 6081),
("vxlan", 4789)]
for tnl in TUNNEL_DEFAULTS:
if ptype == tnl[0]:
if not dport:
dport = tnl[1]
if not lwt:
vportopt = OvsVport.ovs_vport_msg.vportopts()
vportopt["attrs"].append(
["OVS_TUNNEL_ATTR_DST_PORT", socket.htons(dport)]
)
msg["attrs"].append(
["OVS_VPORT_ATTR_OPTIONS", vportopt]
)
else:
port_type = OvsVport.OVS_VPORT_TYPE_NETDEV
ipr = pyroute2.iproute.IPRoute()
if tnl[0] == "geneve":
ipr.link("add", ifname=vport_ifname, kind=tnl[0],
geneve_port=dport,
geneve_collect_metadata=True,
geneve_udp_zero_csum6_rx=1)
elif tnl[0] == "vxlan":
ipr.link("add", ifname=vport_ifname, kind=tnl[0],
vxlan_learning=0, vxlan_collect_metadata=1,
vxlan_udp_zero_csum6_rx=1, vxlan_port=dport)
break
msg["attrs"].append(["OVS_VPORT_ATTR_TYPE", port_type])
try:
reply = self.nlm_request(
msg, msg_type=self.prid, msg_flags=NLM_F_REQUEST | NLM_F_ACK
)
reply = reply[0]
except NetlinkError as ne:
if ne.code == errno.EEXIST:
reply = None
else:
raise ne
return reply
def reset_upcall(self, dpindex, vport_ifname, p=None):
msg = OvsVport.ovs_vport_msg()
msg["cmd"] = OVS_VPORT_CMD_SET
msg["version"] = OVS_DATAPATH_VERSION
msg["reserved"] = 0
msg["dpifindex"] = dpindex
msg["attrs"].append(["OVS_VPORT_ATTR_NAME", vport_ifname])
if p == None:
p = self.upcall_packet
else:
self.upcall_packet = p
msg["attrs"].append(["OVS_VPORT_ATTR_UPCALL_PID", [p.epid]])
try:
reply = self.nlm_request(
msg, msg_type=self.prid, msg_flags=NLM_F_REQUEST | NLM_F_ACK
)
reply = reply[0]
except NetlinkError as ne:
raise ne
return reply
def detach(self, dpindex, vport_ifname):
msg = OvsVport.ovs_vport_msg()
msg["cmd"] = OVS_VPORT_CMD_DEL
msg["version"] = OVS_DATAPATH_VERSION
msg["reserved"] = 0
msg["dpifindex"] = dpindex
msg["attrs"].append(["OVS_VPORT_ATTR_NAME", vport_ifname])
try:
reply = self.nlm_request(
msg, msg_type=self.prid, msg_flags=NLM_F_REQUEST | NLM_F_ACK
)
reply = reply[0]
except NetlinkError as ne:
if ne.code == errno.ENODEV:
reply = None
else:
raise ne
return reply
def upcall_handler(self, handler=None):
self.upcall_packet.upcall_handler(handler)
class OvsFlow(GenericNetlinkSocket):
class ovs_flow_msg(ovs_dp_msg):
nla_map = (
("OVS_FLOW_ATTR_UNSPEC", "none"),
("OVS_FLOW_ATTR_KEY", "ovskey"),
("OVS_FLOW_ATTR_ACTIONS", "ovsactions"),
("OVS_FLOW_ATTR_STATS", "flowstats"),
("OVS_FLOW_ATTR_TCP_FLAGS", "uint8"),
("OVS_FLOW_ATTR_USED", "uint64"),
("OVS_FLOW_ATTR_CLEAR", "none"),
("OVS_FLOW_ATTR_MASK", "ovskey"),
("OVS_FLOW_ATTR_PROBE", "none"),
("OVS_FLOW_ATTR_UFID", "array(uint32)"),
("OVS_FLOW_ATTR_UFID_FLAGS", "uint32"),
)
class flowstats(nla):
fields = (
("packets", "=Q"),
("bytes", "=Q"),
)
def dpstr(self, more=False):
ufid = self.get_attr("OVS_FLOW_ATTR_UFID")
ufid_str = ""
if ufid is not None:
ufid_str = (
"ufid:{:08x}-{:04x}-{:04x}-{:04x}-{:04x}{:08x}".format(
ufid[0],
ufid[1] >> 16,
ufid[1] & 0xFFFF,
ufid[2] >> 16,
ufid[2] & 0,
ufid[3],
)
)
key_field = self.get_attr("OVS_FLOW_ATTR_KEY")
keymsg = None
if key_field is not None:
keymsg = key_field
mask_field = self.get_attr("OVS_FLOW_ATTR_MASK")
maskmsg = None
if mask_field is not None:
maskmsg = mask_field
acts_field = self.get_attr("OVS_FLOW_ATTR_ACTIONS")
actsmsg = None
if acts_field is not None:
actsmsg = acts_field
print_str = ""
if more:
print_str += ufid_str + ","
if keymsg is not None:
print_str += keymsg.dpstr(maskmsg, more)
stats = self.get_attr("OVS_FLOW_ATTR_STATS")
if stats is None:
print_str += " packets:0, bytes:0,"
else:
print_str += " packets:%d, bytes:%d," % (
stats["packets"],
stats["bytes"],
)
used = self.get_attr("OVS_FLOW_ATTR_USED")
print_str += " used:"
if used is None:
print_str += "never,"
else:
used_time = int(used)
cur_time_sec = time.clock_gettime(time.CLOCK_MONOTONIC)
used_time = (cur_time_sec * 1000) - used_time
print_str += "{}s,".format(used_time / 1000)
print_str += " actions:"
if (
actsmsg is None
or "attrs" not in actsmsg
or len(actsmsg["attrs"]) == 0
):
print_str += "drop"
else:
print_str += actsmsg.dpstr(more)
return print_str
def parse(self, flowstr, actstr, dpidx=0):
OVS_UFID_F_OMIT_KEY = 1 << 0
OVS_UFID_F_OMIT_MASK = 1 << 1
OVS_UFID_F_OMIT_ACTIONS = 1 << 2
self["cmd"] = 0
self["version"] = 0
self["reserved"] = 0
self["dpifindex"] = 0
if flowstr.startswith("ufid:"):
count = 5
while flowstr[count] != ",":
count += 1
ufidstr = flowstr[5:count]
flowstr = flowstr[count + 1 :]
else:
ufidstr = str(uuid.uuid4())
uuidRawObj = uuid.UUID(ufidstr).fields
self["attrs"].append(
[
"OVS_FLOW_ATTR_UFID",
[
uuidRawObj[0],
uuidRawObj[1] << 16 | uuidRawObj[2],
uuidRawObj[3] << 24
| uuidRawObj[4] << 16
| uuidRawObj[5] & (0xFF << 32) >> 32,
uuidRawObj[5] & (0xFFFFFFFF),
],
]
)
self["attrs"].append(
[
"OVS_FLOW_ATTR_UFID_FLAGS",
int(
OVS_UFID_F_OMIT_KEY
| OVS_UFID_F_OMIT_MASK
| OVS_UFID_F_OMIT_ACTIONS
),
]
)
k = ovskey()
m = ovskey()
k.parse(flowstr, m)
self["attrs"].append(["OVS_FLOW_ATTR_KEY", k])
self["attrs"].append(["OVS_FLOW_ATTR_MASK", m])
a = ovsactions()
a.parse(actstr)
self["attrs"].append(["OVS_FLOW_ATTR_ACTIONS", a])
def __init__(self):
GenericNetlinkSocket.__init__(self)
self.bind(OVS_FLOW_FAMILY, OvsFlow.ovs_flow_msg)
def add_flow(self, dpifindex, flowmsg):
"""
Send a new flow message to the kernel.
dpifindex should be a valid datapath obtained by calling
into the OvsDatapath lookup
flowmsg is a flow object obtained by calling a dpparse
"""
flowmsg["cmd"] = OVS_FLOW_CMD_NEW
flowmsg["version"] = OVS_DATAPATH_VERSION
flowmsg["reserved"] = 0
flowmsg["dpifindex"] = dpifindex
try:
reply = self.nlm_request(
flowmsg,
msg_type=self.prid,
msg_flags=NLM_F_REQUEST | NLM_F_ACK,
)
reply = reply[0]
except NetlinkError as ne:
print(flowmsg)
raise ne
return reply
def del_flows(self, dpifindex):
"""
Send a del message to the kernel that will drop all flows.
dpifindex should be a valid datapath obtained by calling
into the OvsDatapath lookup
"""
flowmsg = OvsFlow.ovs_flow_msg()
flowmsg["cmd"] = OVS_FLOW_CMD_DEL
flowmsg["version"] = OVS_DATAPATH_VERSION
flowmsg["reserved"] = 0
flowmsg["dpifindex"] = dpifindex
try:
reply = self.nlm_request(
flowmsg,
msg_type=self.prid,
msg_flags=NLM_F_REQUEST | NLM_F_ACK,
)
reply = reply[0]
except NetlinkError as ne:
print(flowmsg)
raise ne
return reply
def dump(self, dpifindex, flowspec=None):
"""
Returns a list of messages containing flows.
dpifindex should be a valid datapath obtained by calling
into the OvsDatapath lookup
flowpsec is a string which represents a flow in the dpctl
format.
"""
msg = OvsFlow.ovs_flow_msg()
msg["cmd"] = OVS_FLOW_CMD_GET
msg["version"] = OVS_DATAPATH_VERSION
msg["reserved"] = 0
msg["dpifindex"] = dpifindex
msg_flags = NLM_F_REQUEST | NLM_F_ACK
if flowspec is None:
msg_flags |= NLM_F_DUMP
rep = None
try:
rep = self.nlm_request(
msg,
msg_type=self.prid,
msg_flags=msg_flags,
)
except NetlinkError as ne:
raise ne
return rep
def miss(self, packetmsg):
seq = packetmsg["header"]["sequence_number"]
keystr = "(none)"
key_field = packetmsg.get_attr("OVS_PACKET_ATTR_KEY")
if key_field is not None:
keystr = key_field.dpstr(None, True)
pktdata = packetmsg.get_attr("OVS_PACKET_ATTR_PACKET")
pktpres = "yes" if pktdata is not None else "no"
print("MISS upcall[%d/%s]: %s" % (seq, pktpres, keystr), flush=True)
def execute(self, packetmsg):
print("userspace execute command", flush=True)
def action(self, packetmsg):
print("userspace action command", flush=True)
class psample_sample(genlmsg):
nla_map = (
("PSAMPLE_ATTR_IIFINDEX", "none"),
("PSAMPLE_ATTR_OIFINDEX", "none"),
("PSAMPLE_ATTR_ORIGSIZE", "none"),
("PSAMPLE_ATTR_SAMPLE_GROUP", "uint32"),
("PSAMPLE_ATTR_GROUP_SEQ", "none"),
("PSAMPLE_ATTR_SAMPLE_RATE", "uint32"),
("PSAMPLE_ATTR_DATA", "array(uint8)"),
("PSAMPLE_ATTR_GROUP_REFCOUNT", "none"),
("PSAMPLE_ATTR_TUNNEL", "none"),
("PSAMPLE_ATTR_PAD", "none"),
("PSAMPLE_ATTR_OUT_TC", "none"),
("PSAMPLE_ATTR_OUT_TC_OCC", "none"),
("PSAMPLE_ATTR_LATENCY", "none"),
("PSAMPLE_ATTR_TIMESTAMP", "none"),
("PSAMPLE_ATTR_PROTO", "none"),
("PSAMPLE_ATTR_USER_COOKIE", "array(uint8)"),
)
def dpstr(self):
fields = []
data = ""
for (attr, value) in self["attrs"]:
if attr == "PSAMPLE_ATTR_SAMPLE_GROUP":
fields.append("group:%d" % value)
if attr == "PSAMPLE_ATTR_SAMPLE_RATE":
fields.append("rate:%d" % value)
if attr == "PSAMPLE_ATTR_USER_COOKIE":
value = "".join(format(x, "02x") for x in value)
fields.append("cookie:%s" % value)
if attr == "PSAMPLE_ATTR_DATA" and len(value) > 0:
data = "data:%s" % "".join(format(x, "02x") for x in value)
return ("%s %s" % (",".join(fields), data)).strip()
class psample_msg(Marshal):
PSAMPLE_CMD_SAMPLE = 0
PSAMPLE_CMD_GET_GROUP = 1
PSAMPLE_CMD_NEW_GROUP = 2
PSAMPLE_CMD_DEL_GROUP = 3
PSAMPLE_CMD_SET_FILTER = 4
msg_map = {PSAMPLE_CMD_SAMPLE: psample_sample}
class PsampleEvent(EventSocket):
genl_family = "psample"
mcast_groups = ["packets"]
marshal_class = psample_msg
def read_samples(self):
print("listening for psample events", flush=True)
while True:
try:
for msg in self.get():
print(msg.dpstr(), flush=True)
except NetlinkError as ne:
raise ne
def print_ovsdp_full(dp_lookup_rep, ifindex, ndb=NDB(), vpl=OvsVport()):
dp_name = dp_lookup_rep.get_attr("OVS_DP_ATTR_NAME")
base_stats = dp_lookup_rep.get_attr("OVS_DP_ATTR_STATS")
megaflow_stats = dp_lookup_rep.get_attr("OVS_DP_ATTR_MEGAFLOW_STATS")
user_features = dp_lookup_rep.get_attr("OVS_DP_ATTR_USER_FEATURES")
masks_cache_size = dp_lookup_rep.get_attr("OVS_DP_ATTR_MASKS_CACHE_SIZE")
print("%s:" % dp_name)
print(
" lookups: hit:%d missed:%d lost:%d"
% (base_stats["hit"], base_stats["missed"], base_stats["lost"])
)
print(" flows:%d" % base_stats["flows"])
pkts = base_stats["hit"] + base_stats["missed"]
avg = (megaflow_stats["mask_hit"] / pkts) if pkts != 0 else 0.0
print(
" masks: hit:%d total:%d hit/pkt:%f"
% (megaflow_stats["mask_hit"], megaflow_stats["masks"], avg)
)
print(" caches:")
print(" masks-cache: size:%d" % masks_cache_size)
if user_features is not None:
print(" features: 0x%X" % user_features)
# port print out
for iface in ndb.interfaces:
rep = vpl.info(iface.ifname, ifindex)
if rep is not None:
opts = ""
vpo = rep.get_attr("OVS_VPORT_ATTR_OPTIONS")
if vpo:
dpo = vpo.get_attr("OVS_TUNNEL_ATTR_DST_PORT")
if dpo:
opts += " tnl-dport:%s" % socket.ntohs(dpo)
print(
" port %d: %s (%s%s)"
% (
rep.get_attr("OVS_VPORT_ATTR_PORT_NO"),
rep.get_attr("OVS_VPORT_ATTR_NAME"),
OvsVport.type_to_str(rep.get_attr("OVS_VPORT_ATTR_TYPE")),
opts,
)
)
def main(argv):
nlmsg_atoms.ovskey = ovskey
nlmsg_atoms.ovsactions = ovsactions
# version check for pyroute2
prverscheck = pyroute2.__version__.split(".")
if int(prverscheck[0]) == 0 and int(prverscheck[1]) < 6:
print("Need to upgrade the python pyroute2 package to >= 0.6.")
sys.exit(0)
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
action="count",
help="Increment 'verbose' output counter.",
default=0,
)
subparsers = parser.add_subparsers(dest="subcommand")
showdpcmd = subparsers.add_parser("show")
showdpcmd.add_argument(
"showdp", metavar="N", type=str, nargs="?", help="Datapath Name"
)
adddpcmd = subparsers.add_parser("add-dp")
adddpcmd.add_argument("adddp", help="Datapath Name")
adddpcmd.add_argument(
"-u",
"--upcall",
action="store_true",
help="Leave open a reader for upcalls",
)
adddpcmd.add_argument(
"-V",
"--versioning",
required=False,
help="Specify a custom version / feature string",
)
deldpcmd = subparsers.add_parser("del-dp")
deldpcmd.add_argument("deldp", help="Datapath Name")
addifcmd = subparsers.add_parser("add-if")
addifcmd.add_argument("dpname", help="Datapath Name")
addifcmd.add_argument("addif", help="Interface name for adding")
addifcmd.add_argument(
"-u",
"--upcall",
action="store_true",
help="Leave open a reader for upcalls",
)
addifcmd.add_argument(
"-t",
"--ptype",
type=str,
default="netdev",
choices=["netdev", "internal", "geneve", "vxlan"],
help="Interface type (default netdev)",
)
addifcmd.add_argument(
"-p",
"--dport",
type=int,
default=0,
help="Destination port (0 for default)"
)
addifcmd.add_argument(
"-l",
"--lwt",
type=bool,
default=True,
help="Use LWT infrastructure instead of vport (default true)."
)
delifcmd = subparsers.add_parser("del-if")
delifcmd.add_argument("dpname", help="Datapath Name")
delifcmd.add_argument("delif", help="Interface name for adding")
delifcmd.add_argument("-d",
"--dellink",
type=bool, default=False,
help="Delete the link as well.")
dumpflcmd = subparsers.add_parser("dump-flows")
dumpflcmd.add_argument("dumpdp", help="Datapath Name")
addflcmd = subparsers.add_parser("add-flow")
addflcmd.add_argument("flbr", help="Datapath name")
addflcmd.add_argument("flow", help="Flow specification")
addflcmd.add_argument("acts", help="Flow actions")
delfscmd = subparsers.add_parser("del-flows")
delfscmd.add_argument("flsbr", help="Datapath name")
subparsers.add_parser("psample-events")
args = parser.parse_args()
if args.verbose > 0:
if args.verbose > 1:
logging.basicConfig(level=logging.DEBUG)
ovspk = OvsPacket()
ovsdp = OvsDatapath()
ovsvp = OvsVport(ovspk)
ovsflow = OvsFlow()
ndb = NDB()
sys.setrecursionlimit(100000)
if args.subcommand == "psample-events":
PsampleEvent().read_samples()
if hasattr(args, "showdp"):
found = False
for iface in ndb.interfaces:
rep = None
if args.showdp is None:
rep = ovsdp.info(iface.ifname, 0)
elif args.showdp == iface.ifname:
rep = ovsdp.info(iface.ifname, 0)
if rep is not None:
found = True
print_ovsdp_full(rep, iface.index, ndb, ovsvp)
if not found:
msg = "No DP found"
if args.showdp is not None:
msg += ":'%s'" % args.showdp
print(msg)
elif hasattr(args, "adddp"):
rep = ovsdp.create(args.adddp, args.upcall, args.versioning, ovspk)
if rep is None:
print("DP '%s' already exists" % args.adddp)
else:
print("DP '%s' added" % args.adddp)
if args.upcall:
ovspk.upcall_handler(ovsflow)
elif hasattr(args, "deldp"):
ovsdp.destroy(args.deldp)
elif hasattr(args, "addif"):
rep = ovsdp.info(args.dpname, 0)
if rep is None:
print("DP '%s' not found." % args.dpname)
return 1
dpindex = rep["dpifindex"]
rep = ovsvp.attach(rep["dpifindex"], args.addif, args.ptype,
args.dport, args.lwt)
msg = "vport '%s'" % args.addif
if rep and rep["header"]["error"] is None:
msg += " added."
else:
msg += " failed to add."
if args.upcall:
if rep is None:
rep = ovsvp.reset_upcall(dpindex, args.addif, ovspk)
ovsvp.upcall_handler(ovsflow)
elif hasattr(args, "delif"):
rep = ovsdp.info(args.dpname, 0)
if rep is None:
print("DP '%s' not found." % args.dpname)
return 1
rep = ovsvp.detach(rep["dpifindex"], args.delif)
msg = "vport '%s'" % args.delif
if rep and rep["header"]["error"] is None:
msg += " removed."
else:
msg += " failed to remove."
if args.dellink:
ipr = pyroute2.iproute.IPRoute()
ipr.link("del", index=ipr.link_lookup(ifname=args.delif)[0])
elif hasattr(args, "dumpdp"):
rep = ovsdp.info(args.dumpdp, 0)
if rep is None:
print("DP '%s' not found." % args.dumpdp)
return 1
rep = ovsflow.dump(rep["dpifindex"])
for flow in rep:
print(flow.dpstr(True if args.verbose > 0 else False))
elif hasattr(args, "flbr"):
rep = ovsdp.info(args.flbr, 0)
if rep is None:
print("DP '%s' not found." % args.flbr)
return 1
flow = OvsFlow.ovs_flow_msg()
flow.parse(args.flow, args.acts, rep["dpifindex"])
ovsflow.add_flow(rep["dpifindex"], flow)
elif hasattr(args, "flsbr"):
rep = ovsdp.info(args.flsbr, 0)
if rep is None:
print("DP '%s' not found." % args.flsbr)
ovsflow.del_flows(rep["dpifindex"])
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))
|