X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FClassify.py;h=6d4b84c1cc7329bca5fe4d9df957dbf71cbfe9cf;hb=7b73d46872db5adfc8f4603a9ca783be7d3fa323;hp=1938688900caa00b027752dd24de0d8cc29a4e78;hpb=10e0393fde6d919cf0e5848bc5e506d981642ef8;p=csit.git diff --git a/resources/libraries/python/Classify.py b/resources/libraries/python/Classify.py index 1938688900..6d4b84c1cc 100644 --- a/resources/libraries/python/Classify.py +++ b/resources/libraries/python/Classify.py @@ -16,12 +16,13 @@ import binascii import re -from socket import AF_INET, AF_INET6, inet_aton, inet_pton +from ipaddress import ip_address from robot.api import logger +from resources.libraries.python.Constants import Constants from resources.libraries.python.topology import Topology -from resources.libraries.python.PapiExecutor import PapiExecutor +from resources.libraries.python.PapiExecutor import PapiSocketExecutor class Classify(object): @@ -40,9 +41,10 @@ class Classify(object): :returns MAC ACL mask in hexstring format. :rtype: str """ + return ('{!s:0>12}{!s:0>12}{!s:0>4}'.format( - dst_mac.replace(':', ''), src_mac.replace(':', ''), ether_type)).\ - rstrip('0') + dst_mac.replace(':', ''), src_mac.replace(':', ''), + ether_type)).decode('hex').rstrip('\0') @staticmethod def _build_ip_mask(proto='', src_ip='', dst_ip='', src_port='', @@ -62,8 +64,10 @@ class Classify(object): :returns: IP mask in hexstring format. :rtype: str """ + return ('{!s:0>20}{!s:0>12}{!s:0>8}{!s:0>4}{!s:0>4}'.format( - proto, src_ip, dst_ip, src_port, dst_port)).rstrip('0') + proto, src_ip, dst_ip, src_port, dst_port)).decode('hex').\ + rstrip('\0') @staticmethod def _build_ip6_mask(next_hdr='', src_ip='', dst_ip='', src_port='', @@ -83,8 +87,10 @@ class Classify(object): :returns: IPv6 ACL mask in hexstring format. :rtype: str """ + return ('{!s:0>14}{!s:0>34}{!s:0>32}{!s:0>4}{!s:0>4}'.format( - next_hdr, src_ip, dst_ip, src_port, dst_port)).rstrip('0') + next_hdr, src_ip, dst_ip, src_port, dst_port)).decode('hex').\ + rstrip('\0') @staticmethod def _build_mac_match(dst_mac='', src_mac='', ether_type=''): @@ -99,21 +105,18 @@ class Classify(object): :returns: MAC ACL match data in hexstring format. :rtype: str """ - if dst_mac: - dst_mac = dst_mac.replace(':', '') - if src_mac: - src_mac = src_mac.replace(':', '') return ('{!s:0>12}{!s:0>12}{!s:0>4}'.format( - dst_mac, src_mac, ether_type)).rstrip('0') + dst_mac.replace(':', ''), src_mac.replace(':', ''), + ether_type)).decode('hex').rstrip('\0') @staticmethod def _build_ip_match(proto=0, src_ip='', dst_ip='', src_port=0, dst_port=0): - """Build IP ACL match data in hexstring format. + """Build IP ACL match data in byte-string format. :param proto: Protocol number with valid option "x". - :param src_ip: Source ip address with format of "x.x.x.x". - :param dst_ip: Destination ip address with format of "x.x.x.x". + :param src_ip: Source ip address in packed format. + :param dst_ip: Destination ip address in packed format. :param src_port: Source port number "x". :param dst_port: Destination port number "x". :type proto: int @@ -121,27 +124,22 @@ class Classify(object): :type dst_ip: str :type src_port: int :type dst_port: int - :returns: IP ACL match data in hexstring format. + :returns: IP ACL match data in byte-string format. :rtype: str """ - if src_ip: - src_ip = binascii.hexlify(inet_aton(src_ip)) - if dst_ip: - dst_ip = binascii.hexlify(inet_aton(dst_ip)) return ('{!s:0>20}{!s:0>12}{!s:0>8}{!s:0>4}{!s:0>4}'.format( hex(proto)[2:], src_ip, dst_ip, hex(src_port)[2:], - hex(dst_port)[2:])).rstrip('0') + hex(dst_port)[2:])).decode('hex').rstrip('\0') @staticmethod def _build_ip6_match(next_hdr=0, src_ip='', dst_ip='', src_port=0, dst_port=0): - """Build IPv6 ACL match data in hexstring format. + """Build IPv6 ACL match data in byte-string format. :param next_hdr: Next header number with valid option "x". - :param src_ip: Source ip6 address with format of "xxx:xxxx::xxxx". - :param dst_ip: Destination ip6 address with format of - "xxx:xxxx::xxxx". + :param src_ip: Source ip6 address in packed format. + :param dst_ip: Destination ip6 address in packed format. :param src_port: Source port number "x". :param dst_port: Destination port number "x". :type next_hdr: int @@ -149,42 +147,36 @@ class Classify(object): :type dst_ip: str :type src_port: int :type dst_port: int - :returns: IPv6 ACL match data in hexstring format. + :returns: IPv6 ACL match data in byte-string format. :rtype: str """ - if src_ip: - src_ip = binascii.hexlify(inet_pton(AF_INET6, src_ip)) - if dst_ip: - dst_ip = binascii.hexlify(inet_pton(AF_INET6, dst_ip)) return ('{!s:0>14}{!s:0>34}{!s:0>32}{!s:0>4}{!s:0>4}'.format( hex(next_hdr)[2:], src_ip, dst_ip, hex(src_port)[2:], - hex(dst_port)[2:])).rstrip('0') + hex(dst_port)[2:])).decode('hex').rstrip('\0') @staticmethod - def _classify_add_del_table(node, is_add, mask, match_n_vectors=1, - table_index=0xFFFFFFFF, nbuckets=2, - memory_size=2097152, skip_n_vectors=0, - next_table_index=0xFFFFFFFF, - miss_next_index=0xFFFFFFFF, current_data_flag=0, - current_data_offset=0): + def _classify_add_del_table( + node, is_add, mask, match_n_vectors=Constants.BITWISE_NON_ZERO, + table_index=Constants.BITWISE_NON_ZERO, nbuckets=2, + memory_size=2097152, skip_n_vectors=Constants.BITWISE_NON_ZERO, + next_table_index=Constants.BITWISE_NON_ZERO, + miss_next_index=Constants.BITWISE_NON_ZERO, + current_data_flag=0, current_data_offset=0): """Add or delete a classify table. :param node: VPP node to create classify table. :param is_add: If 1 the table is added, if 0 the table is deleted. :param mask: ACL mask in hexstring format. - :param match_n_vectors: Number of vectors to match (Default value = 1). - :param table_index: Index of the classify table. - (Default value = 0xFFFFFFFF) + :param match_n_vectors: Number of vectors to match (Default value = ~0). + :param table_index: Index of the classify table. (Default value = ~0) :param nbuckets: Number of buckets when adding a table. (Default value = 2) :param memory_size: Memory size when adding a table. (Default value = 2097152) - :param skip_n_vectors: Number of skip vectors (Default value = 0). - :param next_table_index: Index of next table. - (Default value = 0xFFFFFFFF) - :param miss_next_index: Index of miss table. - (Default value = 0xFFFFFFFF) + :param skip_n_vectors: Number of skip vectors (Default value = ~0). + :param next_table_index: Index of next table. (Default value = ~0) + :param miss_next_index: Index of miss table. (Default value = ~0) :param current_data_flag: Option to use current node's packet payload as the starting point from where packets are classified. This option is only valid for L2/L3 input ACL for now. @@ -215,9 +207,7 @@ class Classify(object): match_n: Number of match vectors. :rtype: tuple(int, int, int) """ - mask_len = ((len(mask) - 1) / 16 + 1) * 16 - mask = mask + '\0' * (mask_len - len(mask)) - + cmd = 'classify_add_del_table' args = dict( is_add=is_add, table_index=table_index, @@ -229,26 +219,24 @@ class Classify(object): miss_next_index=miss_next_index, current_data_flag=current_data_flag, current_data_offset=current_data_offset, - mask_len=mask_len, + mask_len=len(mask), mask=mask ) - - cmd = 'classify_add_del_table' err_msg = "Failed to create a classify table on host {host}".format( host=node['host']) - with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + reply = papi_exec.add(cmd, **args).get_reply(err_msg) - return int(data["new_table_index"]), int(data["skip_n_vectors"]),\ - int(data["match_n_vectors"]) + return int(reply["new_table_index"]), int(reply["skip_n_vectors"]),\ + int(reply["match_n_vectors"]) @staticmethod - def _classify_add_del_session(node, is_add, table_index, match, - opaque_index=0xFFFFFFFF, - hit_next_index=0xFFFFFFFF, advance=0, - action=0, metadata=0): + def _classify_add_del_session( + node, is_add, table_index, match, + opaque_index=Constants.BITWISE_NON_ZERO, + hit_next_index=Constants.BITWISE_NON_ZERO, advance=0, + action=0, metadata=0): """Add or delete a classify session. :param node: VPP node to create classify session. @@ -258,9 +246,9 @@ class Classify(object): include bytes in front with length of skip_n_vectors of target table times sizeof (u32x4) (values of those bytes will be ignored). :param opaque_index: For add, opaque_index of new session. - (Default value = 0xFFFFFFFF) + (Default value = ~0) :param hit_next_index: For add, hit_next_index of new session. - (Default value = 0xFFFFFFFF) + (Default value = ~0) :param advance: For add, advance value for session. (Default value = 0) :param action: 0: No action (by default) metadata is not used. 1: Classified IP packets will be looked up from the specified ipv4 @@ -286,9 +274,7 @@ class Classify(object): :type action: int :type metadata: int """ - - match_len = ((len(match) - 1) / 16 + 1) * 16 - match = match + '\0' * (match_len - len(match)) + cmd = 'classify_add_del_session' args = dict( is_add=is_add, table_index=table_index, @@ -297,16 +283,14 @@ class Classify(object): advance=advance, action=action, metadata=metadata, - match_len=match_len, + match_len=len(match), match=match ) - cmd = 'classify_add_del_session' err_msg = "Failed to create a classify session on host {host}".format( host=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def _macip_acl_add(node, rules, tag=""): @@ -329,9 +313,8 @@ class Classify(object): err_msg = "Failed to create a classify session on host {host}".format( host=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def _acl_interface_set_acl_list(node, sw_if_index, acl_type, acls): @@ -358,9 +341,8 @@ class Classify(object): err_msg = "Failed to set acl list for interface {idx} on host {host}".\ format(idx=sw_if_index, host=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def _acl_add_replace(node, acl_idx, rules, tag=""): @@ -377,7 +359,7 @@ class Classify(object): """ cmd = "acl_add_replace" args = dict( - tag=tag, + tag=tag.encode("utf-8"), acl_index=4294967295 if acl_idx is None else acl_idx, count=len(rules), r=rules @@ -386,23 +368,22 @@ class Classify(object): err_msg = "Failed to add/replace acls on host {host}".format( host=node['host']) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg). \ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod - def vpp_creates_classify_table_l3(node, ip_version, direction, ip_addr): + def vpp_creates_classify_table_l3(node, ip_version, direction, netmask): """Create classify table for IP address filtering. :param node: VPP node to create classify table. :param ip_version: Version of IP protocol. :param direction: Direction of traffic - src/dst. - :param ip_addr: IPv4 or Ipv6 (depending on the parameter 'ip_version') - address. + :param netmask: IPv4 or Ipv6 (depending on the parameter 'ip_version') + netmask (decimal, e.g. 255.255.255.255). :type node: dict :type ip_version: str :type direction: str - :type ip_addr: str + :type netmask: str :returns: (table_index, skip_n, match_n) table_index: Classify table index. skip_n: Number of skip vectors. @@ -415,183 +396,114 @@ class Classify(object): ip4=Classify._build_ip_mask, ip6=Classify._build_ip6_mask ) - if ip_version == "ip4": - ip_addr = binascii.hexlify(inet_aton(ip_addr)) - elif ip_version == "ip6": - ip_addr = binascii.hexlify(inet_pton(AF_INET6, ip_addr)) - else: - raise ValueError("IP version {ver} is not supported.". - format(ver=ip_version)) - if direction == "src": - mask = mask_f[ip_version](src_ip=ip_addr) - elif direction == "dst": - mask = mask_f[ip_version](dst_ip=ip_addr) + if ip_version == "ip4" or ip_version == "ip6": + netmask = binascii.hexlify(ip_address(unicode(netmask)).packed) else: - raise ValueError("Direction {dir} is not supported.". - format(dir=direction)) + raise ValueError("IP version {ver} is not supported.".format( + ver=ip_version)) - return Classify._classify_add_del_table( - node, - is_add=1, - mask=binascii.unhexlify(mask), - match_n_vectors=(len(mask) - 1) // 32 + 1 - ) - - @staticmethod - def vpp_creates_classify_table_l2(node, direction, mac=""): - """Create classify table for MAC address filtering. - - :param node: VPP node to create classify table. - :param direction: Direction of traffic - src/dst. - :param mac: Source or destination (depending on the parameter - 'direction') MAC address. - :type node: dict - :type direction: str - :type mac: str - :returns: (table_index, skip_n, match_n) - table_index: Classify table index. - skip_n: Number of skip vectors. - match_n: Number of match vectors. - :rtype: tuple(int, int, int) - :raises ValueError: If the parameter 'direction' has incorrect value. - """ if direction == "src": - mask = Classify._build_mac_mask(src_mac=mac) + mask = mask_f[ip_version](src_ip=netmask) elif direction == "dst": - mask = Classify._build_mac_mask(dst_mac=mac) + mask = mask_f[ip_version](dst_ip=netmask) else: - raise ValueError("Direction {dir} is not supported.". - format(dir=direction)) + raise ValueError("Direction {dir} is not supported.".format( + dir=direction)) - return Classify._classify_add_del_table( - node, - is_add=1, - mask=binascii.unhexlify(mask), - match_n_vectors=(len(mask) - 1) // 32 + 1 - ) + # Add l2 ethernet header to mask + mask = 14 * '\0' + mask - @staticmethod - def vpp_creates_classify_table_hex(node, hex_mask): - """Create classify table with hex mask. + # Get index of the first significant mask octet + i = len(mask) - len(mask.lstrip('\0')) + + # Compute skip_n parameter + skip_n = i // 16 + # Remove octets to be skipped from the mask + mask = mask[skip_n*16:] + # Pad mask to an even multiple of the vector size + mask = mask + (16 - len(mask) % 16 if len(mask) % 16 else 0) * '\0' + # Compute match_n parameter + match_n = len(mask) // 16 - :param node: VPP node to create classify table based on hex mask. - :param hex_mask: Classify hex mask. - :type node: dict - :type hex_mask: str - :returns: (table_index, skip_n, match_n) - table_index: Classify table index. - skip_n: Number of skip vectors. - match_n: Number of match vectors. - :rtype: tuple(int, int, int) - """ return Classify._classify_add_del_table( node, is_add=1, - mask=binascii.unhexlify(hex_mask), - match_n_vectors=(len(hex_mask) - 1) // 32 + 1 + mask=mask, + match_n_vectors=match_n, + skip_n_vectors=skip_n ) @staticmethod - def vpp_configures_classify_session_l3(node, acl_method, table_index, - ip_version, direction, address): + def vpp_configures_classify_session_l3( + node, acl_method, table_index, skip_n, match_n, ip_version, + direction, address, hit_next_index=Constants.BITWISE_NON_ZERO, + opaque_index=Constants.BITWISE_NON_ZERO): """Configuration of classify session for IP address filtering. :param node: VPP node to setup classify session. :param acl_method: ACL method - deny/permit. :param table_index: Classify table index. + :param skip_n: Number of skip vectors. + :param match_n: Number of vectors to match. :param ip_version: Version of IP protocol. :param direction: Direction of traffic - src/dst. :param address: IPv4 or IPv6 address. + :param hit_next_index: hit_next_index of new session. + (Default value = ~0) + :param opaque_index: opaque_index of new session. (Default value = ~0) :type node: dict :type acl_method: str :type table_index: int + :type skip_n: int + :type match_n: int :type ip_version: str :type direction: str :type address: str + :type hit_next_index: int + :type opaque_index: int :raises ValueError: If the parameter 'direction' has incorrect value. """ match_f = dict( ip4=Classify._build_ip_match, ip6=Classify._build_ip6_match ) - if direction == "src": - match = match_f[ip_version](src_ip=address) - elif direction == "dst": - match = match_f[ip_version](dst_ip=address) - else: - raise ValueError("Direction {dir} is not supported.". - format(dir=direction)) action = dict( permit=0, deny=1 ) - Classify._classify_add_del_session( - node, - is_add=1, - table_index=table_index, - match=binascii.unhexlify(match), - action=action[acl_method]) - @staticmethod - def vpp_configures_classify_session_l2(node, acl_method, table_index, - direction, address): - """Configuration of classify session for MAC address filtering. + if ip_version == "ip4" or ip_version == "ip6": + address = binascii.hexlify(ip_address(unicode(address)).packed) + else: + raise ValueError("IP version {ver} is not supported.".format( + ver=ip_version)) - :param node: VPP node to setup classify session. - :param acl_method: ACL method - deny/permit. - :param table_index: Classify table index. - :param direction: Direction of traffic - src/dst. - :param address: MAC address. - :type node: dict - :type acl_method: str - :type table_index: int - :type direction: str - :type address: str - :raises ValueError: If the parameter 'direction' has incorrect value. - """ if direction == "src": - match = Classify._build_mac_match(src_mac=address) + match = match_f[ip_version](src_ip=address) elif direction == "dst": - match = Classify._build_mac_match(dst_mac=address) + match = match_f[ip_version](dst_ip=address) else: - raise ValueError("Direction {dir} is not supported.". - format(dir=direction)) - action = dict( - permit=0, - deny=1 - ) - Classify._classify_add_del_session( - node, - is_add=1, - table_index=table_index, - match=binascii.unhexlify(match), - action=action[acl_method]) + raise ValueError("Direction {dir} is not supported.".format( + dir=direction)) - @staticmethod - def vpp_configures_classify_session_hex(node, acl_method, table_index, - hex_value): - """Configuration of classify session with hex value. + # Prepend match with l2 ethernet header part + match = 14 * '\0' + match + + # Pad match to match skip_n_vector + match_n_vector size + match = match + ((match_n + skip_n) * 16 - len(match) + if len(match) < (match_n + skip_n) * 16 + else 0) * '\0' - :param node: VPP node to setup classify session. - :param acl_method: ACL method - deny/permit. - :param table_index: Classify table index. - :param hex_value: Classify hex value. - :type node: dict - :type acl_method: str - :type table_index: int - :type hex_value: str - """ - action = dict( - permit=0, - deny=1 - ) Classify._classify_add_del_session( node, is_add=1, table_index=table_index, - match=binascii.unhexlify(hex_value), - action=action[acl_method]) + hit_next_index=hit_next_index, + opaque_index=opaque_index, + match=match, + action=action[acl_method] + ) @staticmethod def compute_classify_hex_mask(ip_version, protocol, direction): @@ -687,11 +599,9 @@ class Classify(object): args = dict( table_id=int(table_index) ) - with PapiExecutor(node) as papi_exec: - data = papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) - - return data + with PapiSocketExecutor(node) as papi_exec: + reply = papi_exec.add(cmd, **args).get_reply(err_msg) + return reply @staticmethod def get_classify_session_data(node, table_index): @@ -704,14 +614,26 @@ class Classify(object): :returns: List of classify session settings. :rtype: list or dict """ + cmd = "classify_session_dump" args = dict( table_id=int(table_index) ) - with PapiExecutor(node) as papi_exec: - dump = papi_exec.add("classify_session_dump", **args).\ - get_dump().reply[0]["api_reply"]["classify_session_details"] + with PapiSocketExecutor(node) as papi_exec: + details = papi_exec.add(cmd, **args).get_details() - return dump + return details + + @staticmethod + def show_classify_tables_verbose(node): + """Show classify tables verbose. + + :param node: Topology node. + :type node: dict + :returns: Classify tables verbose data. + :rtype: str + """ + return PapiSocketExecutor.run_cli_cmd( + node, "show classify tables verbose") @staticmethod def vpp_log_plugin_acl_settings(node): @@ -721,7 +643,7 @@ class Classify(object): :param node: VPP node. :type node: dict """ - PapiExecutor.dump_and_log(node, ["acl_dump", ]) + PapiSocketExecutor.dump_and_log(node, ["acl_dump", ]) @staticmethod def vpp_log_plugin_acl_interface_assignment(node): @@ -731,7 +653,7 @@ class Classify(object): :param node: VPP node. :type node: dict """ - PapiExecutor.dump_and_log(node, ["acl_interface_list_dump", ]) + PapiSocketExecutor.dump_and_log(node, ["acl_interface_list_dump", ]) @staticmethod def set_acl_list_for_interface(node, interface, acl_type, acl_idx=None): @@ -760,21 +682,24 @@ class Classify(object): acls=acls) @staticmethod - def add_replace_acl_multi_entries(node, acl_idx=None, rules=None): + def add_replace_acl_multi_entries(node, acl_idx=None, rules=None, tag=""): """Add a new ACL or replace the existing one. To replace an existing ACL, pass the ID of this ACL. :param node: VPP node to set ACL on. :param acl_idx: ID of ACL. (Optional) :param rules: Required rules. (Optional) + :param tag: ACL tag (Optional). :type node: dict :type acl_idx: int :type rules: str + :type tag: str """ reg_ex_src_ip = re.compile(r'(src [0-9a-fA-F.:/\d{1,2}]*)') reg_ex_dst_ip = re.compile(r'(dst [0-9a-fA-F.:/\d{1,2}]*)') reg_ex_sport = re.compile(r'(sport \d{1,5})') reg_ex_dport = re.compile(r'(dport \d{1,5})') + reg_ex_proto = re.compile(r'(proto \d{1,5})') acl_rules = list() for rule in rules.split(", "): @@ -785,15 +710,13 @@ class Classify(object): groups = re.search(reg_ex_src_ip, rule) if groups: grp = groups.group(1).split(' ')[1].split('/') - acl_rule["src_ip_addr"] = str(inet_pton( - AF_INET6 if acl_rule["is_ipv6"] else AF_INET, grp[0])) + acl_rule["src_ip_addr"] = ip_address(unicode(grp[0])).packed acl_rule["src_ip_prefix_len"] = int(grp[1]) groups = re.search(reg_ex_dst_ip, rule) if groups: grp = groups.group(1).split(' ')[1].split('/') - acl_rule["dst_ip_addr"] = str(inet_pton( - AF_INET6 if acl_rule["is_ipv6"] else AF_INET, grp[0])) + acl_rule["dst_ip_addr"] = ip_address(unicode(grp[0])).packed acl_rule["dst_ip_prefix_len"] = int(grp[1]) groups = re.search(reg_ex_sport, rule) @@ -801,18 +724,30 @@ class Classify(object): port = int(groups.group(1).split(' ')[1]) acl_rule["srcport_or_icmptype_first"] = port acl_rule["srcport_or_icmptype_last"] = port + else: + acl_rule["srcport_or_icmptype_first"] = 0 + acl_rule["srcport_or_icmptype_last"] = 65535 groups = re.search(reg_ex_dport, rule) if groups: port = int(groups.group(1).split(' ')[1]) acl_rule["dstport_or_icmpcode_first"] = port acl_rule["dstport_or_icmpcode_last"] = port + else: + acl_rule["dstport_or_icmpcode_first"] = 0 + acl_rule["dstport_or_icmpcode_last"] = 65535 - acl_rule["proto"] = 0 + groups = re.search(reg_ex_proto, rule) + if groups: + proto = int(groups.group(1).split(' ')[1]) + acl_rule["proto"] = proto + else: + acl_rule["proto"] = 0 acl_rules.append(acl_rule) - Classify._acl_add_replace(node, acl_idx=acl_idx, rules=acl_rules) + Classify._acl_add_replace( + node, acl_idx=acl_idx, rules=acl_rules, tag=tag) @staticmethod def add_macip_acl_multi_entries(node, rules=""): @@ -836,18 +771,17 @@ class Classify(object): groups = re.search(reg_ex_mac, rule) if groups: mac = groups.group(1).split(' ')[1].replace(':', '') - acl_rule["src_mac"] = unicode(mac) + acl_rule["src_mac"] = binascii.unhexlify(unicode(mac)) groups = re.search(reg_ex_mask, rule) if groups: mask = groups.group(1).split(' ')[1].replace(':', '') - acl_rule["src_mac_mask"] = unicode(mask) + acl_rule["src_mac_mask"] = binascii.unhexlify(unicode(mask)) groups = re.search(reg_ex_ip, rule) if groups: grp = groups.group(1).split(' ')[1].split('/') - acl_rule["src_ip_addr"] = str(inet_pton( - AF_INET6 if acl_rule["is_ipv6"] else AF_INET, grp[0])) + acl_rule["src_ip_addr"] = ip_address(unicode(grp[0])).packed acl_rule["src_ip_prefix_len"] = int(grp[1]) acl_rules.append(acl_rule) @@ -862,7 +796,7 @@ class Classify(object): :param node: VPP node. :type node: dict """ - PapiExecutor.dump_and_log(node, ["macip_acl_dump", ]) + PapiSocketExecutor.dump_and_log(node, ["macip_acl_dump", ]) @staticmethod def add_del_macip_acl_interface(node, interface, action, acl_idx): @@ -893,9 +827,8 @@ class Classify(object): sw_if_index=int(sw_if_index), acl_index=int(acl_idx) ) - with PapiExecutor(node) as papi_exec: - papi_exec.add(cmd, **args).get_replies(err_msg).\ - verify_reply(err_msg=err_msg) + with PapiSocketExecutor(node) as papi_exec: + papi_exec.add(cmd, **args).get_reply(err_msg) @staticmethod def vpp_log_macip_acl_interface_assignment(node): @@ -907,6 +840,6 @@ class Classify(object): cmd = 'macip_acl_interface_get' err_msg = "Failed to get 'macip_acl_interface' on host {host}".format( host=node['host']) - with PapiExecutor(node) as papi_exec: - rpl = papi_exec.add(cmd).get_replies(err_msg).reply[0]["api_reply"] - logger.info(rpl["macip_acl_interface_get_reply"]) + with PapiSocketExecutor(node) as papi_exec: + reply = papi_exec.add(cmd).get_reply(err_msg) + logger.info(reply)