Search in sources :

Example 1 with PiRangeFieldMatch

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

the class PiCriteriaTest method testRangeMatchPiMethod.

/**
 * Test the RangeMatchPi method.
 */
@Test
public void testRangeMatchPiMethod() {
    Criterion matchPiBytes = PiCriterion.builder().matchRange(ipv4MatchFieldId, matchRangeBytes1, matchRangeHighBytes).build();
    PiCriterion piCriterionBytes = checkAndConvert(matchPiBytes, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
    PiFieldMatch expectedMatchBytes = new PiRangeFieldMatch(ipv4MatchFieldId, copyFrom(matchRangeBytes1), copyFrom(matchRangeHighBytes));
    assertThat(piCriterionBytes.fieldMatches().iterator().next(), is(expectedMatchBytes));
    Criterion matchPiShort = PiCriterion.builder().matchRange(ipv4MatchFieldId, matchRangeShort1, matchRangeHighShort).build();
    PiCriterion piCriterionShort = checkAndConvert(matchPiShort, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
    PiFieldMatch expectedMatchShort = new PiRangeFieldMatch(ipv4MatchFieldId, copyFrom(matchRangeShort1), copyFrom(matchRangeHighShort));
    assertThat(piCriterionShort.fieldMatches().iterator().next(), is(expectedMatchShort));
    Criterion matchPiInt = PiCriterion.builder().matchRange(ipv4MatchFieldId, matchRangeInt1, matchRangeHighInt).build();
    PiCriterion piCriterionInt = checkAndConvert(matchPiInt, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
    PiFieldMatch expectedMatchInt = new PiRangeFieldMatch(ipv4MatchFieldId, copyFrom(matchRangeInt1), copyFrom(matchRangeHighInt));
    assertThat(piCriterionInt.fieldMatches().iterator().next(), is(expectedMatchInt));
    Criterion matchPiLong = PiCriterion.builder().matchRange(ipv4MatchFieldId, matchRangeLong1, matchRangeHighLong).build();
    PiCriterion piCriterionLong = checkAndConvert(matchPiLong, Criterion.Type.PROTOCOL_INDEPENDENT, PiCriterion.class);
    PiFieldMatch expectedMatchLong = new PiRangeFieldMatch(ipv4MatchFieldId, copyFrom(matchRangeLong1), copyFrom(matchRangeHighLong));
    assertThat(piCriterionLong.fieldMatches().iterator().next(), is(expectedMatchLong));
}
Also used : PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) PiFieldMatch(org.onosproject.net.pi.runtime.PiFieldMatch) Test(org.junit.Test)

Example 2 with PiRangeFieldMatch

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

the class CriterionJsonMatcher method matchCriterion.

/**
 * Matches a protocol-independent Type criterion object.
 *
 * @param criterion criterion to match
 * @return true if the JSON matches the criterion, false otherwise.
 */
private boolean matchCriterion(PiCriterion criterion) {
    Collection<PiFieldMatch> piFieldMatches = criterion.fieldMatches();
    JsonNode jsonMathes = jsonCriterion.get("matches");
    if (!jsonMathes.isArray()) {
        return false;
    }
    for (JsonNode matchNode : jsonMathes) {
        for (PiFieldMatch fieldMatch : piFieldMatches) {
            if (!Objects.equals(matchNode.get("field").textValue(), fieldMatch.fieldId().id())) {
                description.appendText("match field was " + fieldMatch.fieldId().id());
                return false;
            }
            if (!Objects.equals(matchNode.get("match").textValue(), fieldMatch.type().name().toLowerCase())) {
                description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
                return false;
            }
            switch(fieldMatch.type()) {
                case EXACT:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiExactFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiExactFieldMatch) fieldMatch).value());
                        return false;
                    }
                    break;
                case LPM:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiLpmFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiLpmFieldMatch) fieldMatch).value());
                        return false;
                    }
                    if (!Objects.equals(matchNode.get("prefixLength").intValue(), ((PiLpmFieldMatch) fieldMatch).prefixLength())) {
                        description.appendText("match prefix was " + ((PiLpmFieldMatch) fieldMatch).prefixLength());
                        return false;
                    }
                    break;
                case TERNARY:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiTernaryFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiTernaryFieldMatch) fieldMatch).value());
                        return false;
                    }
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("mask").textValue(), null)), ((PiTernaryFieldMatch) fieldMatch).mask())) {
                        description.appendText("match mask was " + ((PiTernaryFieldMatch) fieldMatch).mask());
                        return false;
                    }
                    break;
                case RANGE:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("highValue").textValue(), null)), ((PiRangeFieldMatch) fieldMatch).highValue())) {
                        description.appendText("match high value was " + ((PiRangeFieldMatch) fieldMatch).highValue());
                        return false;
                    }
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("lowValue").textValue(), null)), ((PiRangeFieldMatch) fieldMatch).lowValue())) {
                        description.appendText("match low value was " + ((PiRangeFieldMatch) fieldMatch).lowValue());
                        return false;
                    }
                    break;
                case OPTIONAL:
                    if (!Objects.equals(copyFrom(HexString.fromHexString(matchNode.get("value").textValue(), null)), ((PiOptionalFieldMatch) fieldMatch).value())) {
                        description.appendText("match value was " + ((PiOptionalFieldMatch) fieldMatch).value());
                        return false;
                    }
                    break;
                default:
                    description.appendText("match type was " + fieldMatch.type().name().toLowerCase());
                    return false;
            }
        }
    }
    return true;
}
Also used : PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) PiFieldMatch(org.onosproject.net.pi.runtime.PiFieldMatch) JsonNode(com.fasterxml.jackson.databind.JsonNode) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch)

Example 3 with PiRangeFieldMatch

use of org.onosproject.net.pi.runtime.PiRangeFieldMatch 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)

Example 4 with PiRangeFieldMatch

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

the class FieldMatchCodec method encode.

@Override
public P4RuntimeOuterClass.FieldMatch encode(PiFieldMatch piFieldMatch, P4InfoOuterClass.Preamble tablePreamble, PiPipeconf pipeconf, P4InfoBrowser browser) throws CodecException, P4InfoBrowser.NotFoundException {
    P4RuntimeOuterClass.FieldMatch.Builder messageBuilder = P4RuntimeOuterClass.FieldMatch.newBuilder();
    // FIXME: check how field names for stacked headers are constructed in P4Runtime.
    String fieldName = piFieldMatch.fieldId().id();
    P4InfoOuterClass.MatchField matchFieldInfo = browser.matchFields(tablePreamble.getId()).getByName(fieldName);
    String entityName = format("field match '%s' of table '%s'", matchFieldInfo.getName(), tablePreamble.getName());
    int fieldId = matchFieldInfo.getId();
    int fieldBitwidth = matchFieldInfo.getBitwidth();
    boolean isSdnString = browser.isTypeString(matchFieldInfo.getTypeName());
    messageBuilder.setFieldId(fieldId);
    switch(piFieldMatch.type()) {
        case EXACT:
            PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch;
            ByteString exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer());
            if (!isSdnString) {
                assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth);
            }
            return messageBuilder.setExact(P4RuntimeOuterClass.FieldMatch.Exact.newBuilder().setValue(exactValue).build()).build();
        case TERNARY:
            PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch;
            ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().asReadOnlyBuffer());
            ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().asReadOnlyBuffer());
            if (isSdnString) {
                sdnStringUnsupported(entityName, piFieldMatch.type());
            }
            assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth);
            assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth);
            return messageBuilder.setTernary(P4RuntimeOuterClass.FieldMatch.Ternary.newBuilder().setValue(ternaryValue).setMask(ternaryMask).build()).build();
        case LPM:
            PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch;
            ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().asReadOnlyBuffer());
            int lpmPrefixLen = lpmMatch.prefixLength();
            if (isSdnString) {
                sdnStringUnsupported(entityName, piFieldMatch.type());
            }
            assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth);
            assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth);
            return messageBuilder.setLpm(P4RuntimeOuterClass.FieldMatch.LPM.newBuilder().setValue(lpmValue).setPrefixLen(lpmPrefixLen).build()).build();
        case RANGE:
            PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch;
            ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().asReadOnlyBuffer());
            ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().asReadOnlyBuffer());
            if (isSdnString) {
                sdnStringUnsupported(entityName, piFieldMatch.type());
            }
            assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth);
            assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth);
            return messageBuilder.setRange(P4RuntimeOuterClass.FieldMatch.Range.newBuilder().setHigh(rangeHighValue).setLow(rangeLowValue).build()).build();
        case OPTIONAL:
            PiOptionalFieldMatch optionalMatch = (PiOptionalFieldMatch) piFieldMatch;
            ByteString optionalValue = ByteString.copyFrom(optionalMatch.value().asReadOnlyBuffer());
            if (!isSdnString) {
                assertSize(VALUE_OF_PREFIX + entityName, optionalValue, fieldBitwidth);
            }
            return messageBuilder.setOptional(P4RuntimeOuterClass.FieldMatch.Optional.newBuilder().setValue(optionalValue).build()).build();
        default:
            throw new CodecException(format("Building of match type %s not implemented", piFieldMatch.type()));
    }
}
Also used : PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch) PiFieldMatch(org.onosproject.net.pi.runtime.PiFieldMatch) ByteString(com.google.protobuf.ByteString) PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) ByteString(com.google.protobuf.ByteString) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch) P4InfoOuterClass(p4.config.v1.P4InfoOuterClass) PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch)

Example 5 with PiRangeFieldMatch

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

the class FieldMatchCodec method decode.

@Override
public PiFieldMatch decode(P4RuntimeOuterClass.FieldMatch message, P4InfoOuterClass.Preamble tablePreamble, PiPipeconf pipeconf, P4InfoBrowser browser) throws CodecException, P4InfoBrowser.NotFoundException {
    final P4InfoOuterClass.MatchField matchField = browser.matchFields(tablePreamble.getId()).getById(message.getFieldId());
    final int fieldBitwidth = matchField.getBitwidth();
    final PiMatchFieldId headerFieldId = PiMatchFieldId.of(matchField.getName());
    final boolean isSdnString = browser.isTypeString(matchField.getTypeName());
    final P4RuntimeOuterClass.FieldMatch.FieldMatchTypeCase typeCase = message.getFieldMatchTypeCase();
    try {
        switch(typeCase) {
            case EXACT:
                P4RuntimeOuterClass.FieldMatch.Exact exactFieldMatch = message.getExact();
                final ImmutableByteSequence exactValue;
                if (isSdnString) {
                    exactValue = copyFrom(new String(exactFieldMatch.getValue().toByteArray()));
                } else {
                    exactValue = copyAndFit(exactFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
                }
                return new PiExactFieldMatch(headerFieldId, exactValue);
            case TERNARY:
                P4RuntimeOuterClass.FieldMatch.Ternary ternaryFieldMatch = message.getTernary();
                ImmutableByteSequence ternaryValue = copyAndFit(ternaryFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
                ImmutableByteSequence ternaryMask = copyAndFit(ternaryFieldMatch.getMask().asReadOnlyByteBuffer(), fieldBitwidth);
                return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask);
            case LPM:
                P4RuntimeOuterClass.FieldMatch.LPM lpmFieldMatch = message.getLpm();
                ImmutableByteSequence lpmValue = copyAndFit(lpmFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
                int lpmPrefixLen = lpmFieldMatch.getPrefixLen();
                return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen);
            case RANGE:
                P4RuntimeOuterClass.FieldMatch.Range rangeFieldMatch = message.getRange();
                ImmutableByteSequence rangeHighValue = copyAndFit(rangeFieldMatch.getHigh().asReadOnlyByteBuffer(), fieldBitwidth);
                ImmutableByteSequence rangeLowValue = copyAndFit(rangeFieldMatch.getLow().asReadOnlyByteBuffer(), fieldBitwidth);
                return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue);
            case OPTIONAL:
                P4RuntimeOuterClass.FieldMatch.Optional optionalFieldMatch = message.getOptional();
                final ImmutableByteSequence optionalValue;
                if (isSdnString) {
                    optionalValue = copyFrom(new String(optionalFieldMatch.getValue().toByteArray()));
                } else {
                    optionalValue = copyAndFit(optionalFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
                }
                return new PiOptionalFieldMatch(headerFieldId, optionalValue);
            default:
                throw new CodecException(format("Decoding of field match type '%s' not implemented", typeCase.name()));
        }
    } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
        throw new CodecException(e.getMessage());
    }
}
Also used : PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch) PiFieldMatch(org.onosproject.net.pi.runtime.PiFieldMatch) PiRangeFieldMatch(org.onosproject.net.pi.runtime.PiRangeFieldMatch) ByteString(com.google.protobuf.ByteString) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) PiLpmFieldMatch(org.onosproject.net.pi.runtime.PiLpmFieldMatch) P4InfoOuterClass(p4.config.v1.P4InfoOuterClass) PiOptionalFieldMatch(org.onosproject.net.pi.runtime.PiOptionalFieldMatch) PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) PiExactFieldMatch(org.onosproject.net.pi.runtime.PiExactFieldMatch) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence)

Aggregations

PiRangeFieldMatch (org.onosproject.net.pi.runtime.PiRangeFieldMatch)7 PiExactFieldMatch (org.onosproject.net.pi.runtime.PiExactFieldMatch)5 PiFieldMatch (org.onosproject.net.pi.runtime.PiFieldMatch)5 PiLpmFieldMatch (org.onosproject.net.pi.runtime.PiLpmFieldMatch)5 PiTernaryFieldMatch (org.onosproject.net.pi.runtime.PiTernaryFieldMatch)5 PiOptionalFieldMatch (org.onosproject.net.pi.runtime.PiOptionalFieldMatch)4 ByteString (com.google.protobuf.ByteString)2 ImmutableByteSequence (org.onlab.util.ImmutableByteSequence)2 P4InfoOuterClass (p4.config.v1.P4InfoOuterClass)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Test (org.junit.Test)1 Ip4Prefix (org.onlab.packet.Ip4Prefix)1 ByteSequenceTrimException (org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException)1 UpfApplication (org.onosproject.net.behaviour.upf.UpfApplication)1 UpfGtpTunnelPeer (org.onosproject.net.behaviour.upf.UpfGtpTunnelPeer)1 UpfInterface (org.onosproject.net.behaviour.upf.UpfInterface)1 UpfSessionDownlink (org.onosproject.net.behaviour.upf.UpfSessionDownlink)1 UpfSessionUplink (org.onosproject.net.behaviour.upf.UpfSessionUplink)1 UpfTerminationDownlink (org.onosproject.net.behaviour.upf.UpfTerminationDownlink)1 UpfTerminationUplink (org.onosproject.net.behaviour.upf.UpfTerminationUplink)1