Search in sources :

Example 16 with ImmutableByteSequence

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

Example 17 with ImmutableByteSequence

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

the class P4DataCodec method encodeHeader.

private static P4Header encodeHeader(PiHeader piHeader) {
    P4Header.Builder builder = P4Header.newBuilder();
    int i = 0;
    for (ImmutableByteSequence bitString : piHeader.bitStrings()) {
        builder.setBitstrings(i, ByteString.copyFrom(bitString.asArray()));
        i++;
    }
    return builder.setIsValid(piHeader.isValid()).build();
}
Also used : P4Header(p4.v1.P4DataOuterClass.P4Header) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence)

Example 18 with ImmutableByteSequence

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

the class ActionCodec method decode.

@Override
protected PiAction decode(P4RuntimeOuterClass.Action message, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser) throws P4InfoBrowser.NotFoundException, CodecException {
    final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo = browser.actionParams(message.getActionId());
    final String actionName = browser.actions().getById(message.getActionId()).getPreamble().getName();
    final PiAction.Builder builder = PiAction.builder().withId(PiActionId.of(actionName));
    for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
        final P4InfoOuterClass.Action.Param actionParam = paramInfo.getById(p.getParamId());
        final ImmutableByteSequence value;
        if (browser.isTypeString(actionParam.getTypeName())) {
            value = copyFrom(new String(p.getValue().toByteArray()));
        } else {
            try {
                value = copyAndFit(p.getValue().asReadOnlyByteBuffer(), actionParam.getBitwidth());
            } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
                throw new CodecException(e.getMessage());
            }
        }
        builder.withParameter(new PiActionParam(PiActionParamId.of(actionParam.getName()), value));
    }
    return builder.build();
}
Also used : PiAction(org.onosproject.net.pi.runtime.PiAction) ByteString(com.google.protobuf.ByteString) PiAction(org.onosproject.net.pi.runtime.PiAction) P4InfoOuterClass(p4.config.v1.P4InfoOuterClass) PiActionParam(org.onosproject.net.pi.runtime.PiActionParam) PiActionParam(org.onosproject.net.pi.runtime.PiActionParam) P4InfoBrowser(org.onosproject.p4runtime.ctl.utils.P4InfoBrowser) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence)

Example 19 with ImmutableByteSequence

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

the class PiCriterionTranslatorsTest method testLpmToTernaryTranslation.

@Test
public void testLpmToTernaryTranslation() throws Exception {
    IpPrefix ipPrefix = IpPrefix.valueOf("10.0.0.1/23");
    int bitWidth = ipPrefix.address().toOctets().length * Byte.SIZE;
    IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(ipPrefix);
    PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(criterion, fieldId, TERNARY, bitWidth);
    ImmutableByteSequence expectedMask = ImmutableByteSequence.prefixOnes(Integer.BYTES, 23);
    ImmutableByteSequence expectedValue = ImmutableByteSequence.copyFrom(ipPrefix.address().toOctets());
    assertThat(ternaryMatch.mask(), is(expectedMask));
    assertThat(ternaryMatch.value(), is(expectedValue));
}
Also used : IpPrefix(org.onlab.packet.IpPrefix) IPCriterion(org.onosproject.net.flow.criteria.IPCriterion) PiTernaryFieldMatch(org.onosproject.net.pi.runtime.PiTernaryFieldMatch) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) Test(org.junit.Test)

Example 20 with ImmutableByteSequence

use of org.onlab.util.ImmutableByteSequence in project TFG by mattinelorza.

the class InterpreterImpl method mapInboundPacket.

/**
 * Returns an ONS InboundPacket equivalent to the given pipeconf-specific
 * packet-in operation.
 *
 * @param packetIn packet operation
 * @param deviceId ID of the device that originated the packet-in
 * @return inbound packet
 * @throws PiInterpreterException if the packet operation cannot be mapped
 *                                to an inbound packet
 */
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
    // Find the ingress_port metadata.
    // *** TODO EXERCISE 4: modify metadata names to match P4Info
    // ---- START SOLUTION ----
    final String inportMetadataName = "ADD HERE METADATA NAME FOR THE INGRESS PORT";
    // ---- END SOLUTION ----
    Optional<PiPacketMetadata> inportMetadata = packetIn.metadatas().stream().filter(meta -> meta.id().id().equals(inportMetadataName)).findFirst();
    if (!inportMetadata.isPresent()) {
        throw new PiInterpreterException(format("Missing metadata '%s' in packet-in received from '%s': %s", inportMetadataName, deviceId, packetIn));
    }
    // Build ONOS InboundPacket instance with the given ingress port.
    // 1. Parse packet-in object into Ethernet packet instance.
    final byte[] payloadBytes = packetIn.data().asArray();
    final ByteBuffer rawData = ByteBuffer.wrap(payloadBytes);
    final Ethernet ethPkt;
    try {
        ethPkt = Ethernet.deserializer().deserialize(payloadBytes, 0, packetIn.data().size());
    } catch (DeserializationException dex) {
        throw new PiInterpreterException(dex.getMessage());
    }
    // 2. Get ingress port
    final ImmutableByteSequence portBytes = inportMetadata.get().value();
    final short portNum = portBytes.asReadOnlyBuffer().getShort();
    final ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(portNum));
    return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
}
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) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) AbstractHandlerBehaviour(org.onosproject.net.driver.AbstractHandlerBehaviour) 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) Port(org.onosproject.net.Port) Map(java.util.Map) 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) OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) PiPipelineInterpreter(org.onosproject.net.pi.model.PiPipelineInterpreter) ImmutableMap(com.google.common.collect.ImmutableMap) DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) Collection(java.util.Collection) PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) PiPacketMetadataId(org.onosproject.net.pi.model.PiPacketMetadataId) CPU_PORT_ID(org.onosproject.ngsdn.tutorial.AppConstants.CPU_PORT_ID) String.format(java.lang.String.format) CONTROLLER(org.onosproject.net.PortNumber.CONTROLLER) PiAction(org.onosproject.net.pi.runtime.PiAction) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) InboundPacket(org.onosproject.net.packet.InboundPacket) Optional(java.util.Optional) DeviceId(org.onosproject.net.DeviceId) OUTPUT(org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT) DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) PiPacketMetadata(org.onosproject.net.pi.runtime.PiPacketMetadata) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) DeserializationException(org.onlab.packet.DeserializationException) Ethernet(org.onlab.packet.Ethernet) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence)

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