Search in sources :

Example 1 with Update

use of p4.v1.P4RuntimeOuterClass.Update in project onos by opennetworkinglab.

the class P4InfoParser method parse.

/**
 * Parse the given URL pointing to a P4Info file (in text format) to a PI pipeline model.
 *
 * @param p4InfoUrl URL to P4Info in text form
 * @return PI pipeline model
 * @throws P4InfoParserException if the P4Info file cannot be parsed (see message)
 */
public static PiPipelineModel parse(URL p4InfoUrl) throws P4InfoParserException {
    final P4Info p4info;
    try {
        p4info = getP4InfoMessage(p4InfoUrl);
    } catch (IOException e) {
        throw new P4InfoParserException("Unable to parse protobuf " + p4InfoUrl.toString(), e);
    }
    // Generate fingerprint of the pipeline by hashing p4info file
    final int fingerprint;
    try {
        HashingInputStream hin = new HashingInputStream(Hashing.crc32(), p4InfoUrl.openStream());
        // noinspection StatementWithEmptyBody
        while (hin.read() != -1) {
        // Do nothing. Reading all input stream to update hash.
        }
        fingerprint = hin.hash().asInt();
    } catch (IOException e) {
        throw new P4InfoParserException("Unable to generate fingerprint " + p4InfoUrl.toString(), e);
    }
    // Start by parsing and mapping instances to to their integer P4Info IDs.
    // Convenient to build the table model at the end.
    final String architecture = parseArchitecture(p4info);
    // Counters.
    final Map<Integer, PiCounterModel> counterMap = Maps.newHashMap();
    counterMap.putAll(parseCounters(p4info));
    counterMap.putAll(parseDirectCounters(p4info));
    // Meters.
    final Map<Integer, PiMeterModel> meterMap = Maps.newHashMap();
    meterMap.putAll(parseMeters(p4info));
    meterMap.putAll(parseDirectMeters(p4info));
    // Registers.
    final Map<Integer, PiRegisterModel> registerMap = Maps.newHashMap();
    registerMap.putAll(parseRegisters(p4info));
    // Action profiles.
    final Map<Integer, PiActionProfileModel> actProfileMap = parseActionProfiles(p4info);
    // Actions.
    final Map<Integer, PiActionModel> actionMap = parseActions(p4info);
    // Controller packet metadatas.
    final Map<PiPacketOperationType, PiPacketOperationModel> pktOpMap = parseCtrlPktMetadatas(p4info);
    // Finally, parse tables.
    final ImmutableMap.Builder<PiTableId, PiTableModel> tableImmMapBuilder = ImmutableMap.builder();
    for (Table tableMsg : p4info.getTablesList()) {
        final PiTableId tableId = PiTableId.of(tableMsg.getPreamble().getName());
        // Parse match fields.
        final ImmutableMap.Builder<PiMatchFieldId, PiMatchFieldModel> tableFieldMapBuilder = ImmutableMap.builder();
        for (MatchField fieldMsg : tableMsg.getMatchFieldsList()) {
            final PiMatchFieldId fieldId = PiMatchFieldId.of(fieldMsg.getName());
            tableFieldMapBuilder.put(fieldId, new P4MatchFieldModel(fieldId, isFieldString(p4info, fieldMsg.getTypeName().getName()) ? P4MatchFieldModel.BIT_WIDTH_UNDEFINED : fieldMsg.getBitwidth(), mapMatchFieldType(fieldMsg.getMatchType())));
        }
        // Retrieve action models by inter IDs.
        final ImmutableMap.Builder<PiActionId, PiActionModel> tableActionMapBuilder = ImmutableMap.builder();
        tableMsg.getActionRefsList().stream().map(ActionRef::getId).map(actionMap::get).forEach(actionModel -> tableActionMapBuilder.put(actionModel.id(), actionModel));
        // Retrieve direct meters by integer IDs.
        final ImmutableMap.Builder<PiMeterId, PiMeterModel> tableMeterMapBuilder = ImmutableMap.builder();
        tableMsg.getDirectResourceIdsList().stream().map(meterMap::get).filter(Objects::nonNull).forEach(meterModel -> tableMeterMapBuilder.put(meterModel.id(), meterModel));
        // Retrieve direct counters by integer IDs.
        final ImmutableMap.Builder<PiCounterId, PiCounterModel> tableCounterMapBuilder = ImmutableMap.builder();
        tableMsg.getDirectResourceIdsList().stream().map(counterMap::get).filter(Objects::nonNull).forEach(counterModel -> tableCounterMapBuilder.put(counterModel.id(), counterModel));
        // Check if table supports one-shot only
        boolean oneShotOnly = isAnnotationPresent(ONE_SHOT_ONLY_ANNOTATION, tableMsg.getPreamble());
        tableImmMapBuilder.put(tableId, new P4TableModel(PiTableId.of(tableMsg.getPreamble().getName()), tableMsg.getImplementationId() == 0 ? PiTableType.DIRECT : PiTableType.INDIRECT, actProfileMap.get(tableMsg.getImplementationId()), tableMsg.getSize(), tableCounterMapBuilder.build(), tableMeterMapBuilder.build(), !tableMsg.getIdleTimeoutBehavior().equals(Table.IdleTimeoutBehavior.NO_TIMEOUT), tableFieldMapBuilder.build(), tableActionMapBuilder.build(), actionMap.get(tableMsg.getConstDefaultActionId()), tableMsg.getIsConstTable(), oneShotOnly));
    }
    // Get a map with proper PI IDs for some of those maps we created at the beginning.
    ImmutableMap<PiCounterId, PiCounterModel> counterImmMap = ImmutableMap.copyOf(counterMap.values().stream().collect(Collectors.toMap(PiCounterModel::id, c -> c)));
    ImmutableMap<PiMeterId, PiMeterModel> meterImmMap = ImmutableMap.copyOf(meterMap.values().stream().collect(Collectors.toMap(PiMeterModel::id, m -> m)));
    ImmutableMap<PiRegisterId, PiRegisterModel> registerImmMap = ImmutableMap.copyOf(registerMap.values().stream().collect(Collectors.toMap(PiRegisterModel::id, r -> r)));
    ImmutableMap<PiActionProfileId, PiActionProfileModel> actProfileImmMap = ImmutableMap.copyOf(actProfileMap.values().stream().collect(Collectors.toMap(PiActionProfileModel::id, a -> a)));
    return new P4PipelineModel(tableImmMapBuilder.build(), counterImmMap, meterImmMap, registerImmMap, actProfileImmMap, ImmutableMap.copyOf(pktOpMap), architecture, fingerprint);
}
Also used : PiCounterModel(org.onosproject.net.pi.model.PiCounterModel) PiTableModel(org.onosproject.net.pi.model.PiTableModel) PiActionId(org.onosproject.net.pi.model.PiActionId) PiMeterId(org.onosproject.net.pi.model.PiMeterId) PiPacketOperationType(org.onosproject.net.pi.model.PiPacketOperationType) PiPacketOperationModel(org.onosproject.net.pi.model.PiPacketOperationModel) MatchField(p4.config.v1.P4InfoOuterClass.MatchField) PiTableId(org.onosproject.net.pi.model.PiTableId) PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) ActionRef(p4.config.v1.P4InfoOuterClass.ActionRef) PiRegisterModel(org.onosproject.net.pi.model.PiRegisterModel) Table(p4.config.v1.P4InfoOuterClass.Table) PiActionModel(org.onosproject.net.pi.model.PiActionModel) PiMeterModel(org.onosproject.net.pi.model.PiMeterModel) PiActionProfileId(org.onosproject.net.pi.model.PiActionProfileId) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) HashingInputStream(com.google.common.hash.HashingInputStream) P4Info(p4.config.v1.P4InfoOuterClass.P4Info) PiActionProfileModel(org.onosproject.net.pi.model.PiActionProfileModel) PiRegisterId(org.onosproject.net.pi.model.PiRegisterId) PiMatchFieldModel(org.onosproject.net.pi.model.PiMatchFieldModel) PiCounterId(org.onosproject.net.pi.model.PiCounterId)

Example 2 with Update

use of p4.v1.P4RuntimeOuterClass.Update in project onos by opennetworkinglab.

the class P4RuntimeGroupTest method testInsertPiActionProfileGroup.

@Test
public void testInsertPiActionProfileGroup() throws Exception {
    CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1);
    client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP).submitSync();
    assertTrue(client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP).submitSync().isSuccess());
    complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
    WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0);
    assertEquals(1, result.getDeviceId());
    assertEquals(1, result.getUpdatesCount());
    assertEquals(DEFAULT_ELECTION_ID, result.getElectionId());
    Update update = result.getUpdatesList().get(0);
    assertEquals(Update.Type.INSERT, update.getType());
    Entity entity = update.getEntity();
    ActionProfileGroup actionProfileGroup = entity.getActionProfileGroup();
    assertNotNull(actionProfileGroup);
    assertEquals(P4_INFO_ACT_PROF_ID, actionProfileGroup.getActionProfileId());
    assertEquals(3, actionProfileGroup.getMembersCount());
    List<ActionProfileGroup.Member> members = actionProfileGroup.getMembersList();
    for (ActionProfileGroup.Member member : members) {
        // XXX: We can't guarantee the order of member, just make sure we
        // have these member ids
        assertTrue(MEMBER_IDS.contains(member.getMemberId()));
        assertEquals(DEFAULT_MEMBER_WEIGHT, member.getWeight());
    }
}
Also used : Entity(p4.v1.P4RuntimeOuterClass.Entity) WriteRequest(p4.v1.P4RuntimeOuterClass.WriteRequest) ActionProfileGroup(p4.v1.P4RuntimeOuterClass.ActionProfileGroup) PiActionProfileGroup(org.onosproject.net.pi.runtime.PiActionProfileGroup) Update(p4.v1.P4RuntimeOuterClass.Update) ActionProfileMember(p4.v1.P4RuntimeOuterClass.ActionProfileMember) PiActionProfileMember(org.onosproject.net.pi.runtime.PiActionProfileMember) Test(org.junit.Test)

Example 3 with Update

use of p4.v1.P4RuntimeOuterClass.Update in project onos by opennetworkinglab.

the class P4RuntimeGroupTest method testInsertPiActionMembers.

@Test
public void testInsertPiActionMembers() throws Exception {
    CompletableFuture<Void> complete = p4RuntimeServerImpl.expectRequests(1);
    assertTrue(client.write(P4_DEVICE_ID, PIPECONF).insert(GROUP_MEMBER_INSTANCES).submitSync().isSuccess());
    complete.get(DEFAULT_TIMEOUT_TIME, TimeUnit.SECONDS);
    WriteRequest result = p4RuntimeServerImpl.getWriteReqs().get(0);
    assertEquals(1, result.getDeviceId());
    assertEquals(3, result.getUpdatesCount());
    assertEquals(DEFAULT_ELECTION_ID, result.getElectionId());
    List<Update> updates = result.getUpdatesList();
    for (Update update : updates) {
        assertEquals(Update.Type.INSERT, update.getType());
        Entity entity = update.getEntity();
        ActionProfileMember member = entity.getActionProfileMember();
        assertNotNull(member);
        assertEquals(P4_INFO_ACT_PROF_ID, member.getActionProfileId());
        assertTrue(MEMBER_IDS.contains(member.getMemberId()));
        Action action = member.getAction();
        assertEquals(SET_EGRESS_PORT_ID, action.getActionId());
        assertEquals(1, action.getParamsCount());
        Action.Param param = action.getParamsList().get(0);
        assertEquals(1, param.getParamId());
        byte outPort = (byte) (member.getMemberId() - BASE_MEM_ID);
        ByteString bs = ByteString.copyFrom(new byte[] { 0, outPort });
        assertEquals(bs, param.getValue());
    }
}
Also used : Entity(p4.v1.P4RuntimeOuterClass.Entity) Action(p4.v1.P4RuntimeOuterClass.Action) PiAction(org.onosproject.net.pi.runtime.PiAction) WriteRequest(p4.v1.P4RuntimeOuterClass.WriteRequest) ByteString(com.google.protobuf.ByteString) Update(p4.v1.P4RuntimeOuterClass.Update) ActionProfileMember(p4.v1.P4RuntimeOuterClass.ActionProfileMember) PiActionProfileMember(org.onosproject.net.pi.runtime.PiActionProfileMember) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 PiActionProfileMember (org.onosproject.net.pi.runtime.PiActionProfileMember)2 ActionProfileMember (p4.v1.P4RuntimeOuterClass.ActionProfileMember)2 Entity (p4.v1.P4RuntimeOuterClass.Entity)2 Update (p4.v1.P4RuntimeOuterClass.Update)2 WriteRequest (p4.v1.P4RuntimeOuterClass.WriteRequest)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashingInputStream (com.google.common.hash.HashingInputStream)1 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 PiActionId (org.onosproject.net.pi.model.PiActionId)1 PiActionModel (org.onosproject.net.pi.model.PiActionModel)1 PiActionProfileId (org.onosproject.net.pi.model.PiActionProfileId)1 PiActionProfileModel (org.onosproject.net.pi.model.PiActionProfileModel)1 PiCounterId (org.onosproject.net.pi.model.PiCounterId)1 PiCounterModel (org.onosproject.net.pi.model.PiCounterModel)1 PiMatchFieldId (org.onosproject.net.pi.model.PiMatchFieldId)1 PiMatchFieldModel (org.onosproject.net.pi.model.PiMatchFieldModel)1 PiMeterId (org.onosproject.net.pi.model.PiMeterId)1 PiMeterModel (org.onosproject.net.pi.model.PiMeterModel)1