Search in sources :

Example 6 with PiOptionalFieldMatch

use of org.onosproject.net.pi.runtime.PiOptionalFieldMatch in project onos by opennetworkinglab.

the class PiFlowRuleTranslatorImpl method typeCheckFieldMatch.

private static PiFieldMatch typeCheckFieldMatch(PiFieldMatch fieldMatch, PiMatchFieldModel fieldModel) throws PiTranslationException {
    // Check parameter type and size
    if (!fieldModel.matchType().equals(fieldMatch.type())) {
        throw new PiTranslationException(format("Wrong match type for field '%s', expected %s, but found %s", fieldMatch.fieldId(), fieldModel.matchType().name(), fieldMatch.type().name()));
    }
    // Check if the arbitrary bit width is supported
    if (!fieldModel.hasBitWidth() && !fieldModel.matchType().equals(PiMatchType.EXACT) && !fieldModel.matchType().equals(PiMatchType.OPTIONAL)) {
        throw new PiTranslationException(format("Arbitrary bit width for field '%s' and match type %s is not supported", fieldMatch.fieldId(), fieldModel.matchType().name()));
    }
    int modelBitWidth = fieldModel.bitWidth();
    try {
        switch(fieldModel.matchType()) {
            case EXACT:
                PiExactFieldMatch exactField = (PiExactFieldMatch) fieldMatch;
                return new PiExactFieldMatch(fieldMatch.fieldId(), fieldModel.hasBitWidth() ? exactField.value().fit(modelBitWidth) : exactField.value());
            case TERNARY:
                PiTernaryFieldMatch ternField = (PiTernaryFieldMatch) fieldMatch;
                ImmutableByteSequence ternMask = ternField.mask().fit(modelBitWidth);
                ImmutableByteSequence ternValue = ternField.value().fit(modelBitWidth).bitwiseAnd(ternMask);
                return new PiTernaryFieldMatch(fieldMatch.fieldId(), ternValue, ternMask);
            case LPM:
                PiLpmFieldMatch lpmfield = (PiLpmFieldMatch) fieldMatch;
                if (lpmfield.prefixLength() > modelBitWidth) {
                    throw new PiTranslationException(format("Invalid prefix length for LPM field '%s', found %d but field has bit-width %d", fieldMatch.fieldId(), lpmfield.prefixLength(), modelBitWidth));
                }
                ImmutableByteSequence lpmValue = lpmfield.value().fit(modelBitWidth);
                ImmutableByteSequence lpmMask = prefixOnes(lpmValue.size(), lpmfield.prefixLength());
                lpmValue = lpmValue.bitwiseAnd(lpmMask);
                return new PiLpmFieldMatch(fieldMatch.fieldId(), lpmValue, lpmfield.prefixLength());
            case RANGE:
                return new PiRangeFieldMatch(fieldMatch.fieldId(), ((PiRangeFieldMatch) fieldMatch).lowValue().fit(modelBitWidth), ((PiRangeFieldMatch) fieldMatch).highValue().fit(modelBitWidth));
            case OPTIONAL:
                PiOptionalFieldMatch optionalField = (PiOptionalFieldMatch) fieldMatch;
                return new PiOptionalFieldMatch(fieldMatch.fieldId(), fieldModel.hasBitWidth() ? optionalField.value().fit(modelBitWidth) : optionalField.value());
            default:
                // Should never be here.
                throw new IllegalArgumentException("Unrecognized match type " + fieldModel.matchType().name());
        }
    } catch (ByteSequenceTrimException e) {
        throw new PiTranslationException(format("Size mismatch for field %s: %s", fieldMatch.fieldId(), e.getMessage()));
    }
}
Also used : PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiTranslationException(org.onosproject.net.pi.service.PiTranslationException) ByteSequenceTrimException(org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch)

Aggregations

PiOptionalFieldMatch (org.onosproject.net.pi.runtime.PiOptionalFieldMatch)6 PiExactFieldMatch (org.onosproject.net.pi.runtime.PiExactFieldMatch)5 PiLpmFieldMatch (org.onosproject.net.pi.runtime.PiLpmFieldMatch)5 PiTernaryFieldMatch (org.onosproject.net.pi.runtime.PiTernaryFieldMatch)5 PiFieldMatch (org.onosproject.net.pi.runtime.PiFieldMatch)4 ImmutableByteSequence (org.onlab.util.ImmutableByteSequence)3 PiRangeFieldMatch (org.onosproject.net.pi.runtime.PiRangeFieldMatch)3 ByteString (com.google.protobuf.ByteString)2 ByteSequenceTrimException (org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException)2 P4InfoOuterClass (p4.config.v1.P4InfoOuterClass)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Test (org.junit.Test)1 CriterionTranslatorException (org.onosproject.net.pi.impl.CriterionTranslator.CriterionTranslatorException)1 ArpHaCriterionTranslator (org.onosproject.net.pi.impl.CriterionTranslators.ArpHaCriterionTranslator)1 ArpOpCriterionTranslator (org.onosproject.net.pi.impl.CriterionTranslators.ArpOpCriterionTranslator)1 ArpPaCriterionTranslator (org.onosproject.net.pi.impl.CriterionTranslators.ArpPaCriterionTranslator)1 EthCriterionTranslator (org.onosproject.net.pi.impl.CriterionTranslators.EthCriterionTranslator)1 EthTypeCriterionTranslator (org.onosproject.net.pi.impl.CriterionTranslators.EthTypeCriterionTranslator)1 IPDscpCriterionTranslator (org.onosproject.net.pi.impl.CriterionTranslators.IPDscpCriterionTranslator)1 IPEcnCriterionTranslator (org.onosproject.net.pi.impl.CriterionTranslators.IPEcnCriterionTranslator)1