Search in sources :

Example 11 with ImmutableByteSequence

use of org.onlab.util.ImmutableByteSequence in project fabric-tna by stratum.

the class FabricInterpreter method mapInboundPacket.

@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
    // Assuming that the packet is ethernet, which is fine since fabric.p4
    // can deparse only ethernet packets.
    Ethernet ethPkt;
    try {
        ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, packetIn.data().size());
    } catch (DeserializationException dex) {
        throw new PiInterpreterException(dex.getMessage());
    }
    // Returns the ingress port packet metadata.
    Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas().stream().filter(m -> m.id().equals(P4InfoConstants.INGRESS_PORT)).findFirst();
    if (packetMetadata.isPresent()) {
        try {
            ImmutableByteSequence portByteSequence = packetMetadata.get().value().fit(P4InfoConstants.INGRESS_PORT_BITWIDTH);
            UnsignedInteger ui = UnsignedInteger.fromIntBits(portByteSequence.asReadOnlyBuffer().getInt());
            ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(ui.longValue()));
            if (!receivedFrom.port().hasName()) {
                receivedFrom = translateSwitchPort(receivedFrom);
            }
            ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
            return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
        } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
            throw new PiInterpreterException(format("Malformed metadata '%s' in packet-in received from '%s': %s", P4InfoConstants.INGRESS_PORT, deviceId, packetIn));
        }
    } else {
        throw new PiInterpreterException(format("Missing metadata '%s' in packet-in received from '%s': %s", P4InfoConstants.INGRESS_PORT, deviceId, packetIn));
    }
}
Also used : PiTableId(org.onosproject.net.pi.model.PiTableId) PiPacketMetadata(org.onosproject.net.pi.runtime.PiPacketMetadata) PACKET_OUT(org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT) ZERO(org.stratumproject.fabric.tna.Constants.ZERO) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) FabricTreatmentInterpreter.mapNextTreatment(org.stratumproject.fabric.tna.behaviour.FabricTreatmentInterpreter.mapNextTreatment) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) Ethernet(org.onlab.packet.Ethernet) ImmutableList(com.google.common.collect.ImmutableList) DeserializationException(org.onlab.packet.DeserializationException) OutboundPacket(org.onosproject.net.packet.OutboundPacket) ImmutableByteSequence.copyFrom(org.onlab.util.ImmutableByteSequence.copyFrom) UnsignedInteger(com.google.common.primitives.UnsignedInteger) Port(org.onosproject.net.Port) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation) Criterion(org.onosproject.net.flow.criteria.Criterion) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) FLOOD(org.onosproject.net.PortNumber.FLOOD) Instructions(org.onosproject.net.flow.instructions.Instructions) PiPipelineInterpreter(org.onosproject.net.pi.model.PiPipelineInterpreter) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) Collection(java.util.Collection) FabricTreatmentInterpreter.mapForwardingTreatment(org.stratumproject.fabric.tna.behaviour.FabricTreatmentInterpreter.mapForwardingTreatment) Set(java.util.Set) PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) SlicingService(org.stratumproject.fabric.tna.slicing.api.SlicingService) FabricTreatmentInterpreter.mapAclTreatment(org.stratumproject.fabric.tna.behaviour.FabricTreatmentInterpreter.mapAclTreatment) String.format(java.lang.String.format) CONTROLLER(org.onosproject.net.PortNumber.CONTROLLER) PiAction(org.onosproject.net.pi.runtime.PiAction) TABLE(org.onosproject.net.PortNumber.TABLE) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DriverHandler(org.onosproject.net.driver.DriverHandler) InboundPacket(org.onosproject.net.packet.InboundPacket) FabricTreatmentInterpreter.mapEgressNextTreatment(org.stratumproject.fabric.tna.behaviour.FabricTreatmentInterpreter.mapEgressNextTreatment) Optional(java.util.Optional) DeviceId(org.onosproject.net.DeviceId) OUTPUT(org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT) ONE(org.stratumproject.fabric.tna.Constants.ONE) FabricTreatmentInterpreter.mapPreNextTreatment(org.stratumproject.fabric.tna.behaviour.FabricTreatmentInterpreter.mapPreNextTreatment) DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) PiPacketMetadata(org.onosproject.net.pi.runtime.PiPacketMetadata) Ethernet(org.onlab.packet.Ethernet) UnsignedInteger(com.google.common.primitives.UnsignedInteger) ConnectPoint(org.onosproject.net.ConnectPoint) ByteBuffer(java.nio.ByteBuffer) DeserializationException(org.onlab.packet.DeserializationException) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence)

Example 12 with ImmutableByteSequence

use of org.onlab.util.ImmutableByteSequence 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 13 with ImmutableByteSequence

use of org.onlab.util.ImmutableByteSequence in project onos by opennetworkinglab.

the class PiTernaryFieldMatchTest method testConstruction.

/**
 * Checks the construction of a PiTernaryFieldMatch object.
 */
@Test
public void testConstruction() {
    final ImmutableByteSequence value = copyFrom(0x0a01010a);
    final ImmutableByteSequence mask = copyFrom(0x00ffffff);
    final PiMatchFieldId piMatchField = PiMatchFieldId.of(IPV4_HEADER_NAME + DOT + DST_ADDR);
    PiTernaryFieldMatch piTernaryFieldMatch = new PiTernaryFieldMatch(piMatchField, value, mask);
    assertThat(piTernaryFieldMatch, is(notNullValue()));
    assertThat(piTernaryFieldMatch.value(), is(value));
    assertThat(piTernaryFieldMatch.mask(), is(mask));
    assertThat(piTernaryFieldMatch.type(), is(PiMatchType.TERNARY));
}
Also used : PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) Test(org.junit.Test)

Example 14 with ImmutableByteSequence

use of org.onlab.util.ImmutableByteSequence in project onos by opennetworkinglab.

the class PiOptionalFieldMatchTest method testConstruction.

/**
 * Checks the construction of a PiOptionalFieldMatch object.
 */
@Test
public void testConstruction() {
    final ImmutableByteSequence value = copyFrom(0x0806);
    final PiMatchFieldId piMatchField = PiMatchFieldId.of(ETH_HEADER_NAME + DOT + ETH_TYPE);
    PiOptionalFieldMatch piOptionalFieldMatch = new PiOptionalFieldMatch(piMatchField, value);
    assertThat(piOptionalFieldMatch, is(notNullValue()));
    assertThat(piOptionalFieldMatch.value(), is(value));
    assertThat(piOptionalFieldMatch.type(), is(PiMatchType.OPTIONAL));
}
Also used : PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) PiConstantsTest(org.onosproject.net.pi.runtime.PiConstantsTest) Test(org.junit.Test)

Example 15 with ImmutableByteSequence

use of org.onlab.util.ImmutableByteSequence in project onos by opennetworkinglab.

the class PiActionParamTest method testConstruction.

/**
 * Checks the construction of a PiActionParam object.
 */
@Test
public void testConstruction() {
    ImmutableByteSequence value = copyFrom(0x0b010102);
    final PiActionParamId piActionParamId = PiActionParamId.of(SRC_ADDR);
    final PiActionParam piActionParam = new PiActionParam(piActionParamId, value);
    assertThat(piActionParam, is(notNullValue()));
    assertThat(piActionParam.id(), is(piActionParamId));
    assertThat(piActionParam.value(), is(value));
}
Also used : PiActionParamId(org.onosproject.net.pi.model.PiActionParamId) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) Test(org.junit.Test)

Aggregations

ImmutableByteSequence (org.onlab.util.ImmutableByteSequence)20 PiMatchFieldId (org.onosproject.net.pi.model.PiMatchFieldId)10 Test (org.junit.Test)8 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)5 PiAction (org.onosproject.net.pi.runtime.PiAction)5 ImmutableList (com.google.common.collect.ImmutableList)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 String.format (java.lang.String.format)4 ByteBuffer (java.nio.ByteBuffer)4 Collection (java.util.Collection)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors.toList (java.util.stream.Collectors.toList)4 DeserializationException (org.onlab.packet.DeserializationException)4 Ethernet (org.onlab.packet.Ethernet)4 ImmutableByteSequence.copyFrom (org.onlab.util.ImmutableByteSequence.copyFrom)4 ConnectPoint (org.onosproject.net.ConnectPoint)4 DeviceId (org.onosproject.net.DeviceId)4 Port (org.onosproject.net.Port)4 PortNumber (org.onosproject.net.PortNumber)4