Search in sources :

Example 16 with PiPacketOperation

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

Example 17 with PiPacketOperation

use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.

the class FabricInterpreterTest method testMapInboundPacketWithShortMetadata.

@Test
public void testMapInboundPacketWithShortMetadata() throws ImmutableByteSequence.ByteSequenceTrimException, PiPipelineInterpreter.PiInterpreterException {
    PortNumber inputPort = PortNumber.portNumber(1);
    PiPacketMetadata pktInMetadata = PiPacketMetadata.builder().withId(P4InfoConstants.INGRESS_PORT).withValue(// deliberately smaller
    ImmutableByteSequence.copyFrom(inputPort.toLong()).fit(8)).build();
    Ethernet packet = new Ethernet();
    packet.setDestinationMACAddress(SRC_MAC);
    packet.setSourceMACAddress(DST_MAC);
    packet.setEtherType((short) 0xBA00);
    packet.setPayload(new Data());
    PiPacketOperation pktInOp = PiPacketOperation.builder().withMetadata(pktInMetadata).withData(ImmutableByteSequence.copyFrom(packet.serialize())).withType(PiPacketOperationType.PACKET_IN).build();
    InboundPacket result = interpreter.mapInboundPacket(pktInOp, DEVICE_ID);
    ConnectPoint receiveFrom = new ConnectPoint(DEVICE_ID, inputPort);
    InboundPacket expectedInboundPacket = new DefaultInboundPacket(receiveFrom, packet, ByteBuffer.wrap(packet.serialize()));
    assertEquals(result.receivedFrom(), expectedInboundPacket.receivedFrom());
    assertEquals(result.parsed(), expectedInboundPacket.parsed());
    assertEquals(result.cookie(), expectedInboundPacket.cookie());
    assertEquals(result.unparsed(), expectedInboundPacket.unparsed());
}
Also used : DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) PiPacketMetadata(org.onosproject.net.pi.runtime.PiPacketMetadata) Ethernet(org.onlab.packet.Ethernet) DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) InboundPacket(org.onosproject.net.packet.InboundPacket) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation) DriverData(org.onosproject.net.driver.DriverData) Data(org.onlab.packet.Data) PortNumber(org.onosproject.net.PortNumber) ConnectPoint(org.onosproject.net.ConnectPoint) Test(org.junit.Test)

Example 18 with PiPacketOperation

use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.

the class FabricInterpreterTest method testMapInboundPacketWithCpuPort.

@Test
public void testMapInboundPacketWithCpuPort() throws ImmutableByteSequence.ByteSequenceTrimException, PiPipelineInterpreter.PiInterpreterException {
    PortNumber inputPort = PortNumber.portNumber(Constants.PORT_CPU);
    PiPacketMetadata pktInMetadata = PiPacketMetadata.builder().withId(P4InfoConstants.INGRESS_PORT).withValue(ImmutableByteSequence.copyFrom(inputPort.toLong()).fit(P4InfoConstants.INGRESS_PORT_BITWIDTH)).build();
    Ethernet packet = new Ethernet();
    packet.setDestinationMACAddress(SRC_MAC);
    packet.setSourceMACAddress(DST_MAC);
    packet.setEtherType((short) 0xBA00);
    packet.setPayload(new Data());
    PiPacketOperation pktInOp = PiPacketOperation.builder().withMetadata(pktInMetadata).withData(ImmutableByteSequence.copyFrom(packet.serialize())).withType(PiPacketOperationType.PACKET_IN).build();
    InboundPacket result = interpreter.mapInboundPacket(pktInOp, DEVICE_ID);
    ConnectPoint receiveFrom = new ConnectPoint(DEVICE_ID, inputPort);
    InboundPacket expectedInboundPacket = new DefaultInboundPacket(receiveFrom, packet, ByteBuffer.wrap(packet.serialize()));
    assertEquals(result.receivedFrom(), expectedInboundPacket.receivedFrom());
    assertEquals(result.parsed(), expectedInboundPacket.parsed());
    assertEquals(result.cookie(), expectedInboundPacket.cookie());
    assertEquals(result.unparsed(), expectedInboundPacket.unparsed());
}
Also used : DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) PiPacketMetadata(org.onosproject.net.pi.runtime.PiPacketMetadata) Ethernet(org.onlab.packet.Ethernet) DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) InboundPacket(org.onosproject.net.packet.InboundPacket) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation) DriverData(org.onosproject.net.driver.DriverData) Data(org.onlab.packet.Data) PortNumber(org.onosproject.net.PortNumber) ConnectPoint(org.onosproject.net.ConnectPoint) Test(org.junit.Test)

Example 19 with PiPacketOperation

use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.

the class FabricInterpreterTest method testMapOutboundPacketWithForwarding.

@Test
public void testMapOutboundPacketWithForwarding() throws PiPipelineInterpreter.PiInterpreterException, ImmutableByteSequence.ByteSequenceTrimException {
    PortNumber outputPort = PortNumber.TABLE;
    TrafficTreatment outputTreatment = DefaultTrafficTreatment.builder().setOutput(outputPort).build();
    ByteBuffer data = ByteBuffer.allocate(64);
    OutboundPacket outPkt = new DefaultOutboundPacket(DEVICE_ID, outputTreatment, data);
    Collection<PiPacketOperation> result = interpreter.mapOutboundPacket(outPkt);
    assertEquals(result.size(), 1);
    ImmutableList.Builder<PiPacketMetadata> builder = ImmutableList.builder();
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD0).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD0_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.EGRESS_PORT).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.EGRESS_PORT_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD1).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD1_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.QUEUE_ID).withValue(ImmutableByteSequence.copyFrom(SYSTEM_QUEUE_ID).fit(P4InfoConstants.QUEUE_ID_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD2).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD2_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.CPU_LOOPBACK_MODE).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.CPU_LOOPBACK_MODE_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.DO_FORWARDING).withValue(ImmutableByteSequence.copyFrom(1).fit(P4InfoConstants.DO_FORWARDING_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD3).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD3_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD4).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD4_BITWIDTH)).build());
    builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.ETHER_TYPE).withValue(ImmutableByteSequence.copyFrom(0xBF01).fit(P4InfoConstants.ETHER_TYPE_BITWIDTH)).build());
    PiPacketOperation expectedPktOp = PiPacketOperation.builder().withType(PiPacketOperationType.PACKET_OUT).withData(ImmutableByteSequence.copyFrom(data)).withMetadatas(builder.build()).build();
    assertEquals(expectedPktOp, result.iterator().next());
}
Also used : PiPacketMetadata(org.onosproject.net.pi.runtime.PiPacketMetadata) ImmutableList(com.google.common.collect.ImmutableList) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) PortNumber(org.onosproject.net.PortNumber) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) ByteBuffer(java.nio.ByteBuffer) OutboundPacket(org.onosproject.net.packet.OutboundPacket) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) Test(org.junit.Test)

Example 20 with PiPacketOperation

use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.

the class FabricInterpreter method mapOutboundPacket.

@Override
public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) throws PiInterpreterException {
    TrafficTreatment treatment = packet.treatment();
    // We support only OUTPUT instructions.
    List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions().stream().filter(i -> i.type().equals(OUTPUT)).map(i -> (Instructions.OutputInstruction) i).collect(toList());
    if (treatment.allInstructions().size() != outInstructions.size()) {
        // There are other instructions that are not of type OUTPUT.
        throw new PiInterpreterException("Treatment not supported: " + treatment);
    }
    ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
    for (Instructions.OutputInstruction outInst : outInstructions) {
        if (outInst.port().equals(TABLE)) {
            // Logical port. Forward using the switch tables like a regular packet.
            builder.add(createPiPacketOperation(packet.data(), 0, true));
        } else if (outInst.port().equals(FLOOD)) {
            // Logical port. Create a packet operation for each switch port.
            final DeviceService deviceService = handler().get(DeviceService.class);
            for (Port port : deviceService.getPorts(packet.sendThrough())) {
                builder.add(createPiPacketOperation(packet.data(), port.number().toLong(), false));
            }
        } else if (outInst.port().isLogical()) {
            throw new PiInterpreterException(format("Output on logical port '%s' not supported", outInst.port()));
        } else {
            // Send as-is to given port bypassing all switch tables.
            builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong(), false));
        }
    }
    return builder.build();
}
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) ImmutableList(com.google.common.collect.ImmutableList) Port(org.onosproject.net.Port) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation) DeviceService(org.onosproject.net.device.DeviceService) Instructions(org.onosproject.net.flow.instructions.Instructions) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment)

Aggregations

PiPacketOperation (org.onosproject.net.pi.runtime.PiPacketOperation)20 PortNumber (org.onosproject.net.PortNumber)18 PiPacketMetadata (org.onosproject.net.pi.runtime.PiPacketMetadata)17 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)14 InboundPacket (org.onosproject.net.packet.InboundPacket)14 OutboundPacket (org.onosproject.net.packet.OutboundPacket)14 ImmutableList (com.google.common.collect.ImmutableList)13 ByteBuffer (java.nio.ByteBuffer)13 Ethernet (org.onlab.packet.Ethernet)13 ConnectPoint (org.onosproject.net.ConnectPoint)13 DefaultInboundPacket (org.onosproject.net.packet.DefaultInboundPacket)13 DeviceId (org.onosproject.net.DeviceId)11 Port (org.onosproject.net.Port)11 DeviceService (org.onosproject.net.device.DeviceService)10 PiPipelineInterpreter (org.onosproject.net.pi.model.PiPipelineInterpreter)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 String.format (java.lang.String.format)9 Collection (java.util.Collection)9 List (java.util.List)9 Optional (java.util.Optional)9