org.onosproject.net.pi.runtime.PiPacketOperation Java Examples

The following examples show how to use org.onosproject.net.pi.runtime.PiPacketOperation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PacketInCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
protected PiPacketOperation decode(
        P4RuntimeOuterClass.PacketIn message, Object ignored,
        PiPipeconf pipeconf, P4InfoBrowser browser)
        throws CodecException, P4InfoBrowser.NotFoundException {
    final P4InfoOuterClass.Preamble ctrlPktMetaPreamble = browser
            .controllerPacketMetadatas()
            .getByName(PACKET_IN)
            .getPreamble();
    return PiPacketOperation.builder()
            .withType(PiPacketOperationType.PACKET_IN)
            .withMetadatas(CODECS.packetMetadata().decodeAll(
                    message.getMetadataList(), ctrlPktMetaPreamble, pipeconf))
            .withData(copyFrom(message.getPayload().asReadOnlyByteBuffer()))
            .build();
}
 
Example #2
Source File: PacketOutCodec.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
protected P4RuntimeOuterClass.PacketOut encode(
        PiPacketOperation piPacket, Object ignored, PiPipeconf pipeconf,
        P4InfoBrowser browser)
        throws CodecException, P4InfoBrowser.NotFoundException {
    final P4InfoOuterClass.Preamble ctrlPktMetaPreamble = browser
            .controllerPacketMetadatas()
            .getByName(PACKET_OUT)
            .getPreamble();
    return P4RuntimeOuterClass.PacketOut.newBuilder()
            .addAllMetadata(CODECS.packetMetadata().encodeAll(
                    piPacket.metadatas(), ctrlPktMetaPreamble, pipeconf))
            .setPayload(ByteString.copyFrom(piPacket.data().asReadOnlyBuffer()))
            .build();

}
 
Example #3
Source File: StreamClientImpl.java    From onos with Apache License 2.0 6 votes vote down vote up
private void handlePacketIn(P4RuntimeOuterClass.PacketIn packetInMsg) {
    if (log.isTraceEnabled()) {
        log.trace("Received packet-in from {}: {}", deviceId, packetInMsg);
    }
    if (!pipeconfService.getPipeconf(deviceId).isPresent()) {
        log.warn("Unable to handle packet-in from {}, missing pipeconf: {}",
                 deviceId, TextFormat.shortDebugString(packetInMsg));
        return;
    }
    // Decode packet message and post event.
    // TODO: consider implementing a cache to speed up
    //  encoding/deconding of packet-in/out (e.g. LLDP, ARP)
    final PiPipeconf pipeconf = pipeconfService.getPipeconf(deviceId).get();
    final PiPacketOperation pktOperation;
    try {
        pktOperation = CODECS.packetIn().decode(
                packetInMsg, null, pipeconf);
    } catch (CodecException e) {
        log.warn("Unable to process packet-int: {}", e.getMessage());
        return;
    }
    controller.postEvent(new P4RuntimeEvent(
            P4RuntimeEvent.Type.PACKET_IN,
            new PacketInEvent(deviceId, pktOperation)));
}
 
Example #4
Source File: BasicInterpreterImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId)
        throws PiInterpreterException {
    // Assuming that the packet is ethernet, which is fine since basic.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(INGRESS_PORT))
            .findFirst();

    if (packetMetadata.isPresent()) {
        ImmutableByteSequence portByteSequence = packetMetadata.get().value();
        short s = portByteSequence.asReadOnlyBuffer().getShort();
        ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
        ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
        return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
    } else {
        throw new PiInterpreterException(format(
                "Missing metadata '%s' in packet-in received from '%s': %s",
                INGRESS_PORT, deviceId, packetIn));
    }
}
 
Example #5
Source File: PacketInCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
protected P4RuntimeOuterClass.PacketIn encode(
        PiPacketOperation piEntity, Object ignored, PiPipeconf pipeconf,
        P4InfoBrowser browser)
        throws CodecException {
    throw new CodecException("Encoding of packet-in is not supported");
}
 
Example #6
Source File: PacketOutCodec.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
protected PiPacketOperation decode(
        P4RuntimeOuterClass.PacketOut message, Object ignored,
        PiPipeconf pipeconf, P4InfoBrowser browser)
        throws CodecException {
    throw new CodecException("Decoding of packet-out is not supported");
}
 
Example #7
Source File: PacketInEventTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Setup method for packetOperation and packetOperation2.
 * @throws ImmutableByteSequence.ByteSequenceTrimException if byte sequence cannot be trimmed
 */
@Before
public void setup() throws ImmutableByteSequence.ByteSequenceTrimException {

    packetOperation = PiPacketOperation.builder()
            .withData(ImmutableByteSequence.ofOnes(512))
            .withType(PACKET_OUT)
            .withMetadata(PiPacketMetadata.builder()
                                  .withId(PiPacketMetadataId.of("egress_port"))
                                  .withValue(copyFrom(DEFAULT_ORIGINAL_VALUE).fit(DEFAULT_BIT_WIDTH))
                                  .build())
            .build();

    packetOperation2 = PiPacketOperation.builder()
            .withData(ImmutableByteSequence.ofOnes(512))
            .withType(PACKET_IN)
            .withMetadata(PiPacketMetadata.builder()
                                  .withId(PiPacketMetadataId.of("ingress_port"))
                                  .withValue(copyFrom(DEFAULT_ORIGINAL_VALUE).fit(DEFAULT_BIT_WIDTH))
                                  .build())
            .build();

    packetIn = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId, packetOperation);
    sameAsPacketIn = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(sameDeviceId, packetOperation);
    packetIn2 = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId2, packetOperation);
    packetIn3 = new org.onosproject.p4runtime.ctl.controller.PacketInEvent(deviceId, packetOperation2);
}
 
Example #8
Source File: FabricInterpreter.java    From onos with Apache License 2.0 5 votes vote down vote up
private PiPacketOperation createPiPacketOperation(
        DeviceId deviceId, ByteBuffer data, long portNumber)
        throws PiInterpreterException {
    PiPacketMetadata metadata = createPacketMetadata(portNumber);
    return PiPacketOperation.builder()
            .withType(PACKET_OUT)
            .withData(copyFrom(data))
            .withMetadatas(ImmutableList.of(metadata))
            .build();
}
 
Example #9
Source File: FabricInterpreter.java    From onos with Apache License 2.0 5 votes vote down vote up
@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(FabricConstants.INGRESS_PORT))
            .findFirst();

    if (packetMetadata.isPresent()) {
        ImmutableByteSequence portByteSequence = packetMetadata.get().value();
        short s = portByteSequence.asReadOnlyBuffer().getShort();
        ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
        ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
        return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
    } else {
        throw new PiInterpreterException(format(
                "Missing metadata '%s' in packet-in received from '%s': %s",
                FabricConstants.INGRESS_PORT, deviceId, packetIn));
    }
}
 
Example #10
Source File: BasicInterpreterImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber)
        throws PiInterpreterException {
    PiPacketMetadata metadata = createPacketMetadata(portNumber);
    return PiPacketOperation.builder()
            .withType(PACKET_OUT)
            .withData(copyFrom(data))
            .withMetadatas(ImmutableList.of(metadata))
            .build();
}
 
Example #11
Source File: PipelineInterpreterImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
        throws PiInterpreterException {

    TrafficTreatment treatment = packet.treatment();

    // We support only packet-out with OUTPUT instructions.
    if (treatment.allInstructions().size() != 1 &&
            treatment.allInstructions().get(0).type() != OUTPUT) {
        throw new PiInterpreterException(
                "Treatment not supported: " + treatment.toString());
    }

    Instruction instruction = treatment.allInstructions().get(0);
    PortNumber port = ((OutputInstruction) instruction).port();
    List<PiPacketOperation> piPacketOps = Lists.newArrayList();

    if (!port.isLogical()) {
        piPacketOps.add(createPiPacketOp(packet.data(), port.toLong()));
    } else if (port.equals(FLOOD)) {
        // Since mytunnel.p4 does not support flooding, we create a packet
        // operation for each switch port.
        DeviceService deviceService = handler().get(DeviceService.class);
        DeviceId deviceId = packet.sendThrough();
        for (Port p : deviceService.getPorts(deviceId)) {
            piPacketOps.add(createPiPacketOp(packet.data(), p.number().toLong()));
        }
    } else {
        throw new PiInterpreterException(format(
                "Output on logical port '%s' not supported", port));
    }

    return piPacketOps;
}
 
Example #12
Source File: InterpreterImpl.java    From onos-p4-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a pipeconf-specific packet-out instance with the given payload and
 * egress port.
 *
 * @param pktData    packet payload
 * @param portNumber egress port
 * @return packet-out
 * @throws PiInterpreterException if packet-out cannot be built
 */
private PiPacketOperation buildPacketOut(ByteBuffer pktData, long portNumber)
        throws PiInterpreterException {

    // Make sure port number can fit in v1model port metadata bitwidth.
    final ImmutableByteSequence portBytes;
    try {
        portBytes = copyFrom(portNumber).fit(V1MODEL_PORT_BITWIDTH);
    } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
        throw new PiInterpreterException(format(
                "Port number %d too big, %s", portNumber, e.getMessage()));
    }

    // Create metadata instance for egress port.
    // TODO EXERCISE 1: modify metadata names to match P4 program
    // ---- START SOLUTION ----
    final String outPortMetadataName = "MODIFY ME";
    // ---- END SOLUTION ----
    final PiPacketMetadata outPortMetadata = PiPacketMetadata.builder()
            .withId(PiPacketMetadataId.of(outPortMetadataName))
            .withValue(portBytes)
            .build();

    // Build packet out.
    return PiPacketOperation.builder()
            .withType(PACKET_OUT)
            .withData(copyFrom(pktData))
            .withMetadata(outPortMetadata)
            .build();
}
 
Example #13
Source File: PipelineInterpreterImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId)
        throws PiInterpreterException {
    // We assume that the packet is ethernet, which is fine since mytunnel.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(metadata -> metadata.id().toString().equals(INGRESS_PORT))
            .findFirst();

    if (packetMetadata.isPresent()) {
        short s = packetMetadata.get().value().asReadOnlyBuffer().getShort();
        ConnectPoint receivedFrom = new ConnectPoint(
                deviceId, PortNumber.portNumber(s));
        return new DefaultInboundPacket(
                receivedFrom, ethPkt, packetIn.data().asReadOnlyBuffer());
    } else {
        throw new PiInterpreterException(format(
                "Missing metadata '%s' in packet-in received from '%s': %s",
                INGRESS_PORT, deviceId, packetIn));
    }
}
 
Example #14
Source File: InterpreterImpl.java    From onos-p4-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a pipeconf-specific packet-out instance with the given payload and
 * egress port.
 *
 * @param pktData    packet payload
 * @param portNumber egress port
 * @return packet-out
 * @throws PiInterpreterException if packet-out cannot be built
 */
private PiPacketOperation buildPacketOut(ByteBuffer pktData, long portNumber)
        throws PiInterpreterException {

    // Make sure port number can fit in v1model port metadata bitwidth.
    final ImmutableByteSequence portBytes;
    try {
        portBytes = copyFrom(portNumber).fit(V1MODEL_PORT_BITWIDTH);
    } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
        throw new PiInterpreterException(format(
                "Port number %d too big, %s", portNumber, e.getMessage()));
    }

    // Create metadata instance for egress port.
    // TODO EXERCISE 1: modify metadata names to match P4 program
    // ---- START SOLUTION ----
    final String outPortMetadataName = "egress_port";
    // ---- END SOLUTION ----
    final PiPacketMetadata outPortMetadata = PiPacketMetadata.builder()
            .withId(PiPacketMetadataId.of(outPortMetadataName))
            .withValue(portBytes)
            .build();

    // Build packet out.
    return PiPacketOperation.builder()
            .withType(PACKET_OUT)
            .withData(copyFrom(pktData))
            .withMetadata(outPortMetadata)
            .build();
}
 
Example #15
Source File: PipelineInterpreterImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
private PiPacketOperation createPiPacketOp(ByteBuffer data, long portNumber)
        throws PiInterpreterException {
    PiPacketMetadata metadata = createPacketMetadata(portNumber);
    return PiPacketOperation.builder()
            .withType(PACKET_OUT)
            .withData(copyFrom(data))
            .withMetadatas(ImmutableList.of(metadata))
            .build();
}
 
Example #16
Source File: InterpreterImpl.java    From ngsdn-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a pipeconf-specific packet-out instance with the given payload and
 * egress port.
 *
 * @param pktData    packet payload
 * @param portNumber egress port
 * @return packet-out
 * @throws PiInterpreterException if packet-out cannot be built
 */
private PiPacketOperation buildPacketOut(ByteBuffer pktData, long portNumber)
        throws PiInterpreterException {

    // Make sure port number can fit in v1model port metadata bitwidth.
    final ImmutableByteSequence portBytes;
    try {
        portBytes = copyFrom(portNumber).fit(V1MODEL_PORT_BITWIDTH);
    } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
        throw new PiInterpreterException(format(
                "Port number %d too big, %s", portNumber, e.getMessage()));
    }

    // Create metadata instance for egress port.
    // *** TODO EXERCISE 3: modify metadata names to match P4 program
    // ---- START SOLUTION ----
    final String outPortMetadataName = "<ADD HERE METADATA NAME FOR THE EGRESS PORT>";
    // ---- END SOLUTION ----
    final PiPacketMetadata outPortMetadata = PiPacketMetadata.builder()
            .withId(PiPacketMetadataId.of(outPortMetadataName))
            .withValue(portBytes)
            .build();

    // Build packet out.
    return PiPacketOperation.builder()
            .withType(PACKET_OUT)
            .withData(copyFrom(pktData))
            .withMetadata(outPortMetadata)
            .build();
}
 
Example #17
Source File: InterpreterImpl.java    From ngsdn-tutorial with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a pipeconf-specific packet-out instance with the given payload and
 * egress port.
 *
 * @param pktData    packet payload
 * @param portNumber egress port
 * @return packet-out
 * @throws PiInterpreterException if packet-out cannot be built
 */
private PiPacketOperation buildPacketOut(ByteBuffer pktData, long portNumber)
        throws PiInterpreterException {

    // Make sure port number can fit in v1model port metadata bitwidth.
    final ImmutableByteSequence portBytes;
    try {
        portBytes = copyFrom(portNumber).fit(V1MODEL_PORT_BITWIDTH);
    } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
        throw new PiInterpreterException(format(
                "Port number %d too big, %s", portNumber, e.getMessage()));
    }

    // Create metadata instance for egress port.
    // TODO EXERCISE 3: modify metadata names to match P4 program
    // ---- START SOLUTION ----
    final String outPortMetadataName = "egress_port";
    // ---- END SOLUTION ----
    final PiPacketMetadata outPortMetadata = PiPacketMetadata.builder()
            .withId(PiPacketMetadataId.of(outPortMetadataName))
            .withValue(portBytes)
            .build();

    // Build packet out.
    return PiPacketOperation.builder()
            .withType(PACKET_OUT)
            .withData(copyFrom(pktData))
            .withMetadata(outPortMetadata)
            .build();
}
 
Example #18
Source File: InterpreterImpl.java    From ngsdn-tutorial with Apache License 2.0 4 votes vote down vote up
/**
 * 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 3: modify metadata names to match P4 program
    // ---- START SOLUTION ----
    final String inportMetadataName = "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);
}
 
Example #19
Source File: P4RuntimeClientImpl.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void packetOut(long p4DeviceId, PiPacketOperation packet, PiPipeconf pipeconf) {
    if (streamClients.containsKey(p4DeviceId)) {
        streamClients.get(p4DeviceId).packetOut(p4DeviceId, packet, pipeconf);
    }
}
 
Example #20
Source File: PacketInEvent.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public PiPacketOperation packetOperation() {
    return operation;
}
 
Example #21
Source File: PacketInEvent.java    From onos with Apache License 2.0 4 votes vote down vote up
public PacketInEvent(DeviceId deviceId, PiPacketOperation operation) {
    this.deviceId = checkNotNull(deviceId);
    this.operation = checkNotNull(operation);
}
 
Example #22
Source File: InterpreterImpl.java    From onos-p4-tutorial with Apache License 2.0 4 votes vote down vote up
/**
 * 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 1: modify metadata names to match P4 program
    // ---- START SOLUTION ----
    final String inportMetadataName = "MODIFY ME";
    // ---- 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);
}
 
Example #23
Source File: InterpreterImpl.java    From onos-p4-tutorial with Apache License 2.0 4 votes vote down vote up
/**
 * 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 1: modify metadata names to match P4 program
    // ---- START SOLUTION ----
    final String inportMetadataName = "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);
}
 
Example #24
Source File: InterpreterImpl.java    From ngsdn-tutorial with Apache License 2.0 4 votes vote down vote up
/**
 * 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 3: 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);
}
 
Example #25
Source File: P4RuntimePacketIn.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the packet operation corresponding to this packet-in event.
 *
 * @return pi packet operation
 */
PiPacketOperation packetOperation();
 
Example #26
Source File: P4RuntimeStreamClient.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Sends a packet-out for the given P4Runtime-internal device ID.
 *
 * @param p4DeviceId P4Runtime-internal device ID
 * @param packet     packet-out operation to be performed by the device
 * @param pipeconf   pipeconf currently deployed on the device
 */
void packetOut(long p4DeviceId, PiPacketOperation packet, PiPipeconf pipeconf);
 
Example #27
Source File: PiPipelineInterpreter.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a collection of PI packet operations equivalent to the given
 * outbound packet instance.
 *
 * @param packet outbound packet
 * @return collection of PI packet operations
 * @throws PiInterpreterException if the packet treatments cannot be
 *                                executed by this pipeline
 */
Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
        throws PiInterpreterException;
 
Example #28
Source File: PiPipelineInterpreter.java    From onos with Apache License 2.0 2 votes vote down vote up
/**
 * Returns an inbound packet equivalent to the given PI packet-in operation
 * for the given device.
 *
 * @param packetOperation 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
 */
InboundPacket mapInboundPacket(PiPacketOperation packetOperation, DeviceId deviceId)
        throws PiInterpreterException;