Search in sources :

Example 1 with PiCounterCellData

use of org.onosproject.net.pi.runtime.PiCounterCellData in project onos by opennetworkinglab.

the class P4RuntimeFlowRuleProgrammable method getFlowEntries.

@Override
public Collection<FlowEntry> getFlowEntries() {
    if (!setupBehaviour("getFlowEntries()")) {
        return Collections.emptyList();
    }
    if (driverBoolProperty(READ_FROM_MIRROR, DEFAULT_READ_FROM_MIRROR)) {
        return getFlowEntriesFromMirror();
    }
    final ImmutableList.Builder<FlowEntry> result = ImmutableList.builder();
    final List<PiTableEntry> inconsistentEntries = Lists.newArrayList();
    // Read table entries from device.
    final Collection<PiTableEntry> deviceEntries = getAllTableEntriesFromDevice();
    if (deviceEntries == null) {
        // Potential error at the client level.
        return Collections.emptyList();
    }
    // Synchronize mirror with the device state.
    tableMirror.sync(deviceId, deviceEntries);
    if (deviceEntries.isEmpty()) {
        // Nothing to do.
        return Collections.emptyList();
    }
    final Map<PiTableEntryHandle, PiCounterCellData> counterCellMap = readEntryCounters(deviceEntries);
    // Forge flow entries with counter values.
    for (PiTableEntry entry : deviceEntries) {
        final PiTableEntryHandle handle = entry.handle(deviceId);
        final FlowEntry flowEntry = forgeFlowEntry(entry, handle, counterCellMap.get(handle));
        if (flowEntry == null) {
            // program via default_action, which cannot be removed.)
            if (!isOriginalDefaultEntry(entry)) {
                inconsistentEntries.add(entry);
            }
        } else {
            result.add(flowEntry);
        }
    }
    // Default entries need to be treated in a different way according to the spec:
    // the client can modify (reset or update) them but cannot remove the entries
    List<PiTableEntry> inconsistentDefaultEntries = Lists.newArrayList();
    List<PiTableEntry> tempDefaultEntries = inconsistentEntries.stream().filter(PiTableEntry::isDefaultAction).collect(Collectors.toList());
    inconsistentEntries.removeAll(tempDefaultEntries);
    // Once we have removed the default entry from inconsistentEntries we need to
    // craft for each default entry a copy without the action field. According to
    // the spec leaving the action field unset will reset the original default entry.
    tempDefaultEntries.forEach(piTableEntry -> {
        PiTableEntry resetEntry = PiTableEntry.builder().forTable(piTableEntry.table()).build();
        inconsistentDefaultEntries.add(resetEntry);
    });
    // Clean up of inconsistent entries.
    if (!inconsistentEntries.isEmpty() || !inconsistentDefaultEntries.isEmpty()) {
        WriteRequest writeRequest = client.write(p4DeviceId, pipeconf);
        // Trigger remove of inconsistent entries.
        if (!inconsistentEntries.isEmpty()) {
            log.warn("Found {} inconsistent table entries on {}, removing them...", inconsistentEntries.size(), deviceId);
            writeRequest = writeRequest.entities(inconsistentEntries, DELETE);
        }
        // Trigger reset of inconsistent default entries.
        if (!inconsistentDefaultEntries.isEmpty()) {
            log.warn("Found {} inconsistent default table entries on {}, resetting them...", inconsistentDefaultEntries.size(), deviceId);
            writeRequest = writeRequest.entities(inconsistentDefaultEntries, MODIFY);
        }
        // Submit delete request for non-default entries and modify request
        // for default entries. Updates mirror when done.
        writeRequest.submit().whenComplete((response, ex) -> {
            if (ex != null) {
                log.error("Exception removing inconsistent table entries", ex);
            } else {
                log.debug("Successfully removed {} out of {} inconsistent entries", response.success().size(), response.all().size());
            }
            // We can use the entity as the handle does not contain the action field
            // so the key will be removed even if the table entry is different
            response.success().forEach(entity -> tableMirror.remove((PiTableEntryHandle) entity.handle()));
        });
    }
    return result.build();
}
Also used : PiCounterCellData(org.onosproject.net.pi.runtime.PiCounterCellData) ImmutableList(com.google.common.collect.ImmutableList) WriteRequest(org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest) PiTableEntry(org.onosproject.net.pi.runtime.PiTableEntry) FlowEntry(org.onosproject.net.flow.FlowEntry) DefaultFlowEntry(org.onosproject.net.flow.DefaultFlowEntry) PiTableEntryHandle(org.onosproject.net.pi.runtime.PiTableEntryHandle)

Example 2 with PiCounterCellData

use of org.onosproject.net.pi.runtime.PiCounterCellData in project onos by opennetworkinglab.

the class TableEntryEncoderTest method testEncodeWithNoAction.

@Test
public void testEncodeWithNoAction() throws Exception {
    TableEntry tableEntryMsg = Codecs.CODECS.tableEntry().encode(piTableEntryWithoutAction, null, defaultPipeconf);
    PiTableEntry decodedPiTableEntry = Codecs.CODECS.tableEntry().decode(tableEntryMsg, null, defaultPipeconf);
    // Test equality for decoded entry.
    new EqualsTester().addEqualityGroup(piTableEntryWithoutAction, decodedPiTableEntry).testEquals();
    // Table ID.
    int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId();
    int encodedTableId = tableEntryMsg.getTableId();
    assertThat(encodedTableId, is(p4InfoTableId));
    // Ternary match.
    byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray();
    assertThat(encodedTernaryMatchValue, is(ethAddr.asArray()));
    // no action
    assertThat(tableEntryMsg.hasAction(), is(false));
    // Counter
    CounterData counterData = tableEntryMsg.getCounterData();
    PiCounterCellData encodedCounterData = new PiCounterCellData(counterData.getPacketCount(), counterData.getByteCount());
    assertThat(encodedCounterData, is(counterCellData));
// TODO: improve, assert other field match types (ternary, LPM)
}
Also used : TableEntry(p4.v1.P4RuntimeOuterClass.TableEntry) PiTableEntry(org.onosproject.net.pi.runtime.PiTableEntry) PiCounterCellData(org.onosproject.net.pi.runtime.PiCounterCellData) EqualsTester(com.google.common.testing.EqualsTester) PiTableEntry(org.onosproject.net.pi.runtime.PiTableEntry) CounterData(p4.v1.P4RuntimeOuterClass.CounterData) Test(org.junit.Test)

Example 3 with PiCounterCellData

use of org.onosproject.net.pi.runtime.PiCounterCellData in project onos by opennetworkinglab.

the class TableEntryEncoderTest method testTableEntryEncoder.

@Test
public void testTableEntryEncoder() throws Exception {
    TableEntry tableEntryMsg = Codecs.CODECS.tableEntry().encode(piTableEntry, null, defaultPipeconf);
    PiTableEntry decodedPiTableEntry = Codecs.CODECS.tableEntry().decode(tableEntryMsg, null, defaultPipeconf);
    // Test equality for decoded entry.
    new EqualsTester().addEqualityGroup(piTableEntry, decodedPiTableEntry).testEquals();
    // Table ID.
    int p4InfoTableId = browser.tables().getByName(tableId.id()).getPreamble().getId();
    int encodedTableId = tableEntryMsg.getTableId();
    assertThat(encodedTableId, is(p4InfoTableId));
    // Ternary match.
    byte[] encodedTernaryMatchValue = tableEntryMsg.getMatch(0).getTernary().getValue().toByteArray();
    assertThat(encodedTernaryMatchValue, is(ethAddr.asArray()));
    Action actionMsg = tableEntryMsg.getAction().getAction();
    // Action ID.
    int p4InfoActionId = browser.actions().getByName(outActionId.toString()).getPreamble().getId();
    int encodedActionId = actionMsg.getActionId();
    assertThat(encodedActionId, is(p4InfoActionId));
    // Action param ID.
    int p4InfoActionParamId = browser.actionParams(p4InfoActionId).getByName(portParamId.toString()).getId();
    int encodedActionParamId = actionMsg.getParams(0).getParamId();
    assertThat(encodedActionParamId, is(p4InfoActionParamId));
    // Action param value.
    byte[] encodedActionParam = actionMsg.getParams(0).getValue().toByteArray();
    assertThat(encodedActionParam, is(portValue.asArray()));
    // Counter
    CounterData counterData = tableEntryMsg.getCounterData();
    PiCounterCellData encodedCounterData = new PiCounterCellData(counterData.getPacketCount(), counterData.getByteCount());
    assertThat(encodedCounterData, is(counterCellData));
// TODO: improve, assert other field match types (ternary, LPM)
}
Also used : TableEntry(p4.v1.P4RuntimeOuterClass.TableEntry) PiTableEntry(org.onosproject.net.pi.runtime.PiTableEntry) Action(p4.v1.P4RuntimeOuterClass.Action) PiAction(org.onosproject.net.pi.runtime.PiAction) PiCounterCellData(org.onosproject.net.pi.runtime.PiCounterCellData) EqualsTester(com.google.common.testing.EqualsTester) PiTableEntry(org.onosproject.net.pi.runtime.PiTableEntry) CounterData(p4.v1.P4RuntimeOuterClass.CounterData) Test(org.junit.Test)

Aggregations

PiCounterCellData (org.onosproject.net.pi.runtime.PiCounterCellData)3 PiTableEntry (org.onosproject.net.pi.runtime.PiTableEntry)3 EqualsTester (com.google.common.testing.EqualsTester)2 Test (org.junit.Test)2 CounterData (p4.v1.P4RuntimeOuterClass.CounterData)2 TableEntry (p4.v1.P4RuntimeOuterClass.TableEntry)2 ImmutableList (com.google.common.collect.ImmutableList)1 DefaultFlowEntry (org.onosproject.net.flow.DefaultFlowEntry)1 FlowEntry (org.onosproject.net.flow.FlowEntry)1 PiAction (org.onosproject.net.pi.runtime.PiAction)1 PiTableEntryHandle (org.onosproject.net.pi.runtime.PiTableEntryHandle)1 WriteRequest (org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest)1 Action (p4.v1.P4RuntimeOuterClass.Action)1