|
28 | 28 | from pyroute2.netlink import nlmsg_atoms
|
29 | 29 | from pyroute2.netlink.exceptions import NetlinkError
|
30 | 30 | from pyroute2.netlink.generic import GenericNetlinkSocket
|
| 31 | + import pyroute2 |
| 32 | + |
31 | 33 | except ModuleNotFoundError:
|
32 |
| - print("Need to install the python pyroute2 package.") |
| 34 | + print("Need to install the python pyroute2 package >= 0.6.") |
33 | 35 | sys.exit(0)
|
34 | 36 |
|
35 | 37 |
|
@@ -1117,12 +1119,14 @@ class ovs_key_ct_tuple_ipv4(ovs_key_proto):
|
1117 | 1119 | "src",
|
1118 | 1120 | lambda x: str(ipaddress.IPv4Address(x)),
|
1119 | 1121 | int,
|
| 1122 | + convert_ipv4, |
1120 | 1123 | ),
|
1121 | 1124 | (
|
1122 | 1125 | "dst",
|
1123 | 1126 | "dst",
|
1124 |
| - lambda x: str(ipaddress.IPv6Address(x)), |
| 1127 | + lambda x: str(ipaddress.IPv4Address(x)), |
1125 | 1128 | int,
|
| 1129 | + convert_ipv4, |
1126 | 1130 | ),
|
1127 | 1131 | ("tp_src", "tp_src", "%d", int),
|
1128 | 1132 | ("tp_dst", "tp_dst", "%d", int),
|
@@ -1904,6 +1908,32 @@ def add_flow(self, dpifindex, flowmsg):
|
1904 | 1908 | raise ne
|
1905 | 1909 | return reply
|
1906 | 1910 |
|
| 1911 | + def del_flows(self, dpifindex): |
| 1912 | + """ |
| 1913 | + Send a del message to the kernel that will drop all flows. |
| 1914 | +
|
| 1915 | + dpifindex should be a valid datapath obtained by calling |
| 1916 | + into the OvsDatapath lookup |
| 1917 | + """ |
| 1918 | + |
| 1919 | + flowmsg = OvsFlow.ovs_flow_msg() |
| 1920 | + flowmsg["cmd"] = OVS_FLOW_CMD_DEL |
| 1921 | + flowmsg["version"] = OVS_DATAPATH_VERSION |
| 1922 | + flowmsg["reserved"] = 0 |
| 1923 | + flowmsg["dpifindex"] = dpifindex |
| 1924 | + |
| 1925 | + try: |
| 1926 | + reply = self.nlm_request( |
| 1927 | + flowmsg, |
| 1928 | + msg_type=self.prid, |
| 1929 | + msg_flags=NLM_F_REQUEST | NLM_F_ACK, |
| 1930 | + ) |
| 1931 | + reply = reply[0] |
| 1932 | + except NetlinkError as ne: |
| 1933 | + print(flowmsg) |
| 1934 | + raise ne |
| 1935 | + return reply |
| 1936 | + |
1907 | 1937 | def dump(self, dpifindex, flowspec=None):
|
1908 | 1938 | """
|
1909 | 1939 | Returns a list of messages containing flows.
|
@@ -1998,6 +2028,12 @@ def main(argv):
|
1998 | 2028 | nlmsg_atoms.ovskey = ovskey
|
1999 | 2029 | nlmsg_atoms.ovsactions = ovsactions
|
2000 | 2030 |
|
| 2031 | + # version check for pyroute2 |
| 2032 | + prverscheck = pyroute2.__version__.split(".") |
| 2033 | + if int(prverscheck[0]) == 0 and int(prverscheck[1]) < 6: |
| 2034 | + print("Need to upgrade the python pyroute2 package to >= 0.6.") |
| 2035 | + sys.exit(0) |
| 2036 | + |
2001 | 2037 | parser = argparse.ArgumentParser()
|
2002 | 2038 | parser.add_argument(
|
2003 | 2039 | "-v",
|
@@ -2060,6 +2096,9 @@ def main(argv):
|
2060 | 2096 | addflcmd.add_argument("flow", help="Flow specification")
|
2061 | 2097 | addflcmd.add_argument("acts", help="Flow actions")
|
2062 | 2098 |
|
| 2099 | + delfscmd = subparsers.add_parser("del-flows") |
| 2100 | + delfscmd.add_argument("flsbr", help="Datapath name") |
| 2101 | + |
2063 | 2102 | args = parser.parse_args()
|
2064 | 2103 |
|
2065 | 2104 | if args.verbose > 0:
|
@@ -2143,6 +2182,11 @@ def main(argv):
|
2143 | 2182 | flow = OvsFlow.ovs_flow_msg()
|
2144 | 2183 | flow.parse(args.flow, args.acts, rep["dpifindex"])
|
2145 | 2184 | ovsflow.add_flow(rep["dpifindex"], flow)
|
| 2185 | + elif hasattr(args, "flsbr"): |
| 2186 | + rep = ovsdp.info(args.flsbr, 0) |
| 2187 | + if rep is None: |
| 2188 | + print("DP '%s' not found." % args.flsbr) |
| 2189 | + ovsflow.del_flows(rep["dpifindex"]) |
2146 | 2190 |
|
2147 | 2191 | return 0
|
2148 | 2192 |
|
|
0 commit comments