Search in sources :

Example 1 with CodecException

use of org.onosproject.p4runtime.ctl.codec.CodecException in project up4 by omec-project.

the class Up4NorthComponentTest method readCounterTest.

private void readCounterTest(PiCounterId counterId, long expectedPackets, long expectedBytes) {
    MockStreamObserver<P4RuntimeOuterClass.ReadResponse> responseObserver = new MockStreamObserver<>();
    int cellIndex = 1;
    PiCounterCell requestedCell = new PiCounterCell(PiCounterCellId.ofIndirect(counterId, cellIndex), 0, 0);
    P4RuntimeOuterClass.Entity entity;
    try {
        entity = Codecs.CODECS.entity().encode(requestedCell, null, pipeconf);
    } catch (CodecException e) {
        fail("Unable to encode counter cell to p4runtime entity.");
        return;
    }
    P4RuntimeOuterClass.ReadRequest request = P4RuntimeOuterClass.ReadRequest.newBuilder().addEntities(entity).setDeviceId(NorthTestConstants.P4RUNTIME_DEVICE_ID).build();
    up4NorthService.read(request, responseObserver);
    var response = responseObserver.lastResponse();
    PiCounterCell expectedCell = new PiCounterCell(PiCounterCellId.ofIndirect(counterId, cellIndex), expectedPackets, expectedBytes);
    P4RuntimeOuterClass.Entity expectedEntity;
    try {
        expectedEntity = Codecs.CODECS.entity().encode(expectedCell, null, pipeconf);
    } catch (CodecException e) {
        fail("Unable to encode counter cell to p4runtime entity.");
        return;
    }
    assertThat(response.getEntitiesCount(), equalTo(1));
    assertThat(response.getEntitiesList().get(0), equalTo(expectedEntity));
}
Also used : P4RuntimeOuterClass(p4.v1.P4RuntimeOuterClass) PiCounterCell(org.onosproject.net.pi.runtime.PiCounterCell) CodecException(org.onosproject.p4runtime.ctl.codec.CodecException)

Example 2 with CodecException

use of org.onosproject.p4runtime.ctl.codec.CodecException in project up4 by omec-project.

the class Up4NorthComponentTest method entityToCounterCell.

private PiCounterCell entityToCounterCell(P4RuntimeOuterClass.Entity entity) {
    PiEntity responsePiEntity;
    try {
        responsePiEntity = Codecs.CODECS.entity().decode(entity, null, pipeconf);
    } catch (CodecException e) {
        fail("Unable to decode p4runtime entity from read response.");
        return null;
    }
    assertThat(responsePiEntity.piEntityType(), equalTo(PiEntityType.COUNTER_CELL));
    return (PiCounterCell) responsePiEntity;
}
Also used : PiEntity(org.onosproject.net.pi.runtime.PiEntity) PiCounterCell(org.onosproject.net.pi.runtime.PiCounterCell) CodecException(org.onosproject.p4runtime.ctl.codec.CodecException)

Example 3 with CodecException

use of org.onosproject.p4runtime.ctl.codec.CodecException in project onos by opennetworkinglab.

the class StreamClientImpl method packetOut.

@Override
public void packetOut(long p4DeviceId, PiPacketOperation packet, PiPipeconf pipeconf) {
    checkArgument(this.p4DeviceId == p4DeviceId);
    if (!isSessionOpen(p4DeviceId)) {
        log.warn("Dropping packet-out request for {}, session is closed", deviceId);
        return;
    }
    if (log.isTraceEnabled()) {
        log.trace("Sending packet-out to {}: {}", deviceId, packet);
    }
    try {
        // Encode the PiPacketOperation into a PacketOut
        final P4RuntimeOuterClass.PacketOut packetOut = CODECS.packetOut().encode(packet, null, pipeconf);
        // Build the request
        final StreamMessageRequest packetOutRequest = StreamMessageRequest.newBuilder().setPacket(packetOut).build();
        // Send.
        streamChannelManager.send(packetOutRequest);
    } catch (CodecException e) {
        log.error("Unable to send packet-out: {}", e.getMessage());
    }
}
Also used : P4RuntimeOuterClass(p4.v1.P4RuntimeOuterClass) CodecException(org.onosproject.p4runtime.ctl.codec.CodecException) StreamMessageRequest(p4.v1.P4RuntimeOuterClass.StreamMessageRequest)

Example 4 with CodecException

use of org.onosproject.p4runtime.ctl.codec.CodecException in project up4 by omec-project.

the class Up4NorthComponent method readCountersAndTranslate.

/**
 * Read the all p4 counter cell requested by the message, and translate them to p4runtime
 * entities for crafting a p4runtime read response.
 *
 * @param message a p4runtime CounterEntry message from a read request
 * @return the requested counter cells' contents, as a list of p4runtime entities
 * @throws StatusException if the counter index is out of range
 */
private List<P4RuntimeOuterClass.Entity> readCountersAndTranslate(P4RuntimeOuterClass.CounterEntry message) throws StatusException {
    ArrayList<PiCounterCell> responseCells = new ArrayList<>();
    Integer index = null;
    // FYI a counter read message with no index corresponds to a wildcard read of all indices
    if (message.hasIndex()) {
        index = (int) message.getIndex().getIndex();
    }
    String counterName = null;
    PiCounterId piCounterId = null;
    int counterId = message.getCounterId();
    // FYI a counterId of 0 corresponds to a wildcard read of all counters
    if (counterId != 0) {
        try {
            counterName = PipeconfHelper.getP4InfoBrowser(pipeconf).counters().getById(message.getCounterId()).getPreamble().getName();
            piCounterId = PiCounterId.of(counterName);
        } catch (P4InfoBrowser.NotFoundException e) {
            log.warn("Unable to find UP4 counter with ID {}", counterId);
            throw INVALID_ARGUMENT.withDescription("Invalid UP4 counter identifier.").asException();
        }
    }
    // cell was requested.
    if (counterName != null && index != null) {
        // A single counter cell was requested
        UpfCounter ctrValues;
        try {
            ctrValues = up4Service.readCounter(index);
        } catch (UpfProgrammableException e) {
            throw INVALID_ARGUMENT.withDescription(e.getMessage()).asException();
        }
        long pkts;
        long bytes;
        if (piCounterId.equals(PRE_QOS_PIPE_PRE_QOS_COUNTER)) {
            pkts = ctrValues.getIngressPkts();
            bytes = ctrValues.getIngressBytes();
        } else if (piCounterId.equals(POST_QOS_PIPE_POST_QOS_COUNTER)) {
            pkts = ctrValues.getEgressPkts();
            bytes = ctrValues.getEgressBytes();
        } else {
            log.warn("Received read request for unknown counter {}", piCounterId);
            throw INVALID_ARGUMENT.withDescription("Invalid UP4 counter identifier.").asException();
        }
        responseCells.add(new PiCounterCell(PiCounterCellId.ofIndirect(piCounterId, index), pkts, bytes));
    } else {
        // All cells were requested, either for a specific counter or all counters
        // FIXME: only read the counter that was requested, instead of both ingress and egress unconditionally
        Collection<UpfCounter> allStats;
        try {
            allStats = up4Service.readCounters(-1);
        } catch (UpfProgrammableException e) {
            throw io.grpc.Status.UNKNOWN.withDescription(e.getMessage()).asException();
        }
        for (UpfCounter stat : allStats) {
            if (piCounterId == null || piCounterId.equals(PRE_QOS_PIPE_PRE_QOS_COUNTER)) {
                // If all counters were requested, or just the ingress one
                responseCells.add(new PiCounterCell(PiCounterCellId.ofIndirect(PRE_QOS_PIPE_PRE_QOS_COUNTER, stat.getCellId()), stat.getIngressPkts(), stat.getIngressBytes()));
            }
            if (piCounterId == null || piCounterId.equals(POST_QOS_PIPE_POST_QOS_COUNTER)) {
                // If all counters were requested, or just the egress one
                responseCells.add(new PiCounterCell(PiCounterCellId.ofIndirect(POST_QOS_PIPE_POST_QOS_COUNTER, stat.getCellId()), stat.getEgressPkts(), stat.getEgressBytes()));
            }
        }
    }
    List<P4RuntimeOuterClass.Entity> responseEntities = new ArrayList<>();
    for (PiCounterCell cell : responseCells) {
        try {
            responseEntities.add(Codecs.CODECS.entity().encode(cell, null, pipeconf));
            log.trace("Encoded response to counter read request for counter {} and index {}", cell.cellId().counterId(), cell.cellId().index());
        } catch (CodecException e) {
            log.error("Unable to encode counter cell into a p4runtime entity: {}", e.getMessage());
            throw io.grpc.Status.INTERNAL.withDescription("Unable to encode counter cell into a p4runtime entity.").asException();
        }
    }
    log.debug("Encoded response to counter read request for {} cells", responseEntities.size());
    return responseEntities;
}
Also used : UpfEntity(org.onosproject.net.behaviour.upf.UpfEntity) PiEntity(org.onosproject.net.pi.runtime.PiEntity) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) HexString(org.onlab.util.HexString) UpfCounter(org.onosproject.net.behaviour.upf.UpfCounter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UpfProgrammableException(org.onosproject.net.behaviour.upf.UpfProgrammableException) PiCounterCell(org.onosproject.net.pi.runtime.PiCounterCell) CodecException(org.onosproject.p4runtime.ctl.codec.CodecException) PiCounterId(org.onosproject.net.pi.model.PiCounterId) P4InfoBrowser(org.onosproject.p4runtime.ctl.utils.P4InfoBrowser)

Example 5 with CodecException

use of org.onosproject.p4runtime.ctl.codec.CodecException in project up4 by omec-project.

the class Up4NorthComponentTest method readPartialWildcardCounterTest.

private void readPartialWildcardCounterTest(PiCounterId counterId) {
    // A counter read request with a counterID but no cellId
    // Encode a dummy cell just so we can get the p4runtime counter integer ID from the encoder
    PiCounterCell dummyCell = new PiCounterCell(PiCounterCellId.ofIndirect(counterId, 1), 0, 0);
    P4RuntimeOuterClass.Entity dummyEntity;
    try {
        dummyEntity = Codecs.CODECS.entity().encode(dummyCell, null, pipeconf);
    } catch (CodecException e) {
        fail("Unable to encode counter cell to p4runtime entity.");
        return;
    }
    int intCounterId = dummyEntity.getCounterEntry().getCounterId();
    // Now build the actual request
    MockStreamObserver<P4RuntimeOuterClass.ReadResponse> responseObserver = new MockStreamObserver<>();
    P4RuntimeOuterClass.ReadRequest request = P4RuntimeOuterClass.ReadRequest.newBuilder().addEntities(P4RuntimeOuterClass.Entity.newBuilder().setCounterEntry(P4RuntimeOuterClass.CounterEntry.newBuilder().setCounterId(intCounterId).build()).build()).build();
    up4NorthService.read(request, responseObserver);
    var response = responseObserver.lastResponse();
    assertThat(response.getEntitiesList().size(), equalTo(TestImplConstants.PHYSICAL_COUNTER_SIZE));
    for (P4RuntimeOuterClass.Entity entity : response.getEntitiesList()) {
        PiCounterCell responseCell = entityToCounterCell(entity);
        assertThat(responseCell.cellId().counterId(), equalTo(counterId));
    }
}
Also used : P4RuntimeOuterClass(p4.v1.P4RuntimeOuterClass) PiCounterCell(org.onosproject.net.pi.runtime.PiCounterCell) CodecException(org.onosproject.p4runtime.ctl.codec.CodecException)

Aggregations

CodecException (org.onosproject.p4runtime.ctl.codec.CodecException)7 PiCounterCell (org.onosproject.net.pi.runtime.PiCounterCell)4 P4RuntimeOuterClass (p4.v1.P4RuntimeOuterClass)4 PiEntity (org.onosproject.net.pi.runtime.PiEntity)3 ArrayList (java.util.ArrayList)2 UpfEntity (org.onosproject.net.behaviour.upf.UpfEntity)2 UpfProgrammableException (org.onosproject.net.behaviour.upf.UpfProgrammableException)2 ByteString (com.google.protobuf.ByteString)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HexString (org.onlab.util.HexString)1 UpfCounter (org.onosproject.net.behaviour.upf.UpfCounter)1 UpfEntityType (org.onosproject.net.behaviour.upf.UpfEntityType)1 PiCounterId (org.onosproject.net.pi.model.PiCounterId)1 PiPipeconf (org.onosproject.net.pi.model.PiPipeconf)1 PiPacketOperation (org.onosproject.net.pi.runtime.PiPacketOperation)1 P4RuntimeEvent (org.onosproject.p4runtime.api.P4RuntimeEvent)1 PacketInEvent (org.onosproject.p4runtime.ctl.controller.PacketInEvent)1 P4InfoBrowser (org.onosproject.p4runtime.ctl.utils.P4InfoBrowser)1 StreamMessageRequest (p4.v1.P4RuntimeOuterClass.StreamMessageRequest)1