예제 #1
0
 def get_match_field_pb(self, table_name, match_field_name, value):
     p4info_match = self.get_match_field(table_name, match_field_name)
     bitwidth = p4info_match.bitwidth
     p4runtime_match = p4runtime_pb2.FieldMatch()
     p4runtime_match.field_id = p4info_match.id
     match_type = p4info_match.match_type
     # if match_type == p4info_pb2.MatchField.VALID:
     #     valid = p4runtime_match.valid
     #     valid.value = bool(value)
     if match_type == p4info_pb2.MatchField.EXACT:
         exact = p4runtime_match.exact
         exact.value = encode(value, bitwidth)
     elif match_type == p4info_pb2.MatchField.LPM:
         lpm = p4runtime_match.lpm
         lpm.value = encode(value[0], bitwidth)
         lpm.prefix_len = value[1]
     elif match_type == p4info_pb2.MatchField.TERNARY:
         lpm = p4runtime_match.ternary
         lpm.value = encode(value[0], bitwidth)
         lpm.mask = encode(value[1], bitwidth)
     elif match_type == p4info_pb2.MatchField.RANGE:
         lpm = p4runtime_match.range
         lpm.low = encode(value[0], bitwidth)
         lpm.high = encode(value[1], bitwidth)
     else:
         raise Exception("Unsupported match type with type %r" % match_type)
     return p4runtime_match
예제 #2
0
    def get_match_field_pb(self, table_name, match_field_name, value):
        logger.info(
            'Retrieving match field on table name - [%s], match field - [%s], '
            'value - [%s]', table_name, match_field_name, value)

        p4info_match = self.get_match_field(table_name, match_field_name)
        bit_width = p4info_match.bitwidth
        p4runtime_match = p4runtime_pb2.FieldMatch()
        p4runtime_match.field_id = p4info_match.id
        match_type = p4info_match.match_type

        logger.info('Encoding value [%s] for exact match with bitwidth - [%s]',
                    value, bit_width)

        if match_type == p4info_pb2.MatchField.EXACT:
            logger.info('Encoding for EXACT matches')
            exact = p4runtime_match.exact
            if isinstance(value, list) or isinstance(value, tuple):
                exact.value = encode(value[0], bit_width)
            else:
                exact.value = encode(value, bit_width)
        elif match_type == p4info_pb2.MatchField.LPM:
            logger.info('Encoding for LPM matches')
            lpm = p4runtime_match.lpm
            lpm.value = encode(value[0], bit_width)
            lpm.prefix_len = value[1]
        elif match_type == p4info_pb2.MatchField.TERNARY:
            logger.info('Encoding for TERNARY matches')
            ternary = p4runtime_match.ternary
            ternary.value = encode(value[0], bit_width)
            ternary.mask = encode(value[1], bit_width)
        elif match_type == p4info_pb2.MatchField.RANGE:
            logger.info('Encoding for RANGE matches')
            range_match = p4runtime_match.range
            range_match.low = encode(value[0], bit_width)
            range_match.high = encode(value[1], bit_width)
        else:
            raise Exception("Unsupported match type with type %r" % match_type)
        return p4runtime_match
예제 #3
0
 def get_match_field_pb(self, table_name, match_field_name, value):
     """
     Converts a match field value to something readable by the switch.
     For each match type, the user provides:
     EXACT:
         <value>
     LPM:
         [<string (ip address)>, <prefix length>]
     TERNARY:
         [<value>, <bit mask>]
     RANGE:
         [<low value>, <high value>]
     """
     p4info_match = self.get_match_field(table_name, match_field_name)
     bitwidth = p4info_match.bitwidth
     p4runtime_match = p4runtime_pb2.FieldMatch()
     p4runtime_match.field_id = p4info_match.id
     match_type = p4info_match.match_type
     if match_type == p4info_pb2.MatchField.EXACT:
         exact = p4runtime_match.exact
         exact.value = encode(value, bitwidth)
     elif match_type == p4info_pb2.MatchField.LPM:
         lpm = p4runtime_match.lpm
         lpm.value = encode(value[0], bitwidth)
         lpm.prefix_len = value[1]
     elif match_type == p4info_pb2.MatchField.TERNARY:
         lpm = p4runtime_match.ternary
         lpm.value = encode(value[0], bitwidth)
         lpm.mask = encode(value[1], bitwidth)
     elif match_type == p4info_pb2.MatchField.RANGE:
         lpm = p4runtime_match.range
         lpm.low = encode(value[0], bitwidth)
         lpm.high = encode(value[1], bitwidth)
     else:
         raise Exception("Unsupported match type with type %r" % match_type)
     return p4runtime_match