use of p4.v1.P4RuntimeOuterClass.Action 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[] { outPort });
assertEquals(bs, param.getValue());
}
}
use of p4.v1.P4RuntimeOuterClass.Action 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.canonical().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)
}
use of p4.v1.P4RuntimeOuterClass.Action in project onos by opennetworkinglab.
the class P4InfoParser method parseActions.
private static Map<Integer, PiActionModel> parseActions(P4Info p4info) {
final Map<Integer, PiActionModel> actionMap = Maps.newHashMap();
for (Action actionMsg : p4info.getActionsList()) {
final ImmutableMap.Builder<PiActionParamId, PiActionParamModel> paramMapBuilder = ImmutableMap.builder();
actionMsg.getParamsList().forEach(paramMsg -> {
final PiActionParamId paramId = PiActionParamId.of(paramMsg.getName());
paramMapBuilder.put(paramId, new P4ActionParamModel(PiActionParamId.of(paramMsg.getName()), isFieldString(p4info, paramMsg.getTypeName().getName()) ? P4ActionParamModel.BIT_WIDTH_UNDEFINED : paramMsg.getBitwidth()));
});
actionMap.put(actionMsg.getPreamble().getId(), new P4ActionModel(PiActionId.of(actionMsg.getPreamble().getName()), paramMapBuilder.build()));
}
return actionMap;
}
use of p4.v1.P4RuntimeOuterClass.Action 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);
}
use of p4.v1.P4RuntimeOuterClass.Action in project onos by opennetworkinglab.
the class P4InfoParser method parseActionProfiles.
private static Map<Integer, PiActionProfileModel> parseActionProfiles(P4Info p4info) throws P4InfoParserException {
final Map<Integer, PiActionProfileModel> actProfileMap = Maps.newHashMap();
for (ActionProfile actProfileMsg : p4info.getActionProfilesList()) {
final ImmutableSet.Builder<PiTableId> tableIdSetBuilder = ImmutableSet.builder();
for (int tableId : actProfileMsg.getTableIdsList()) {
tableIdSetBuilder.add(PiTableId.of(getTableName(tableId, p4info)));
}
// TODO: we should copy all annotations to model classes for later
// use in the PI framework.
// This is a temporary workaround to the inability of p4c to
// correctly interpret P4Runtime-defined max_group_size annotation:
// https://s3-us-west-2.amazonaws.com/p4runtime/docs/master/
// P4Runtime-Spec.html#sec-p4info-action-profile
final String maxSizeAnnString = getAnnotationValue(MAX_GROUP_SIZE_ANNOTATION, actProfileMsg.getPreamble());
final int maxSizeAnn = maxSizeAnnString != null ? Integer.valueOf(maxSizeAnnString) : 0;
final int maxGroupSize;
if (actProfileMsg.getMaxGroupSize() == 0 && maxSizeAnn != 0) {
log.warn("Found valid 'max_group_size' annotation for " + "ActionProfile {}, using that...", actProfileMsg.getPreamble().getName());
maxGroupSize = maxSizeAnn;
} else {
maxGroupSize = actProfileMsg.getMaxGroupSize();
}
actProfileMap.put(actProfileMsg.getPreamble().getId(), new P4ActionProfileModel(PiActionProfileId.of(actProfileMsg.getPreamble().getName()), tableIdSetBuilder.build(), actProfileMsg.getWithSelector(), actProfileMsg.getSize(), maxGroupSize));
}
return actProfileMap;
}
Aggregations