use of p4.config.v1.P4InfoOuterClass.P4Info in project onos by opennetworkinglab.
the class PipeconfHelper method getP4Info.
/**
* Extracts and returns a P4Info protobuf message from the given pipeconf. If the pipeconf does not define any
* extension of type {@link PiPipeconf.ExtensionType#P4_INFO_TEXT}, returns null;
*
* @param pipeconf pipeconf
* @return P4Info or null
*/
public static P4Info getP4Info(PiPipeconf pipeconf) {
return P4INFOS.computeIfAbsent(pipeconf.fingerprint(), piPipeconfId -> {
if (!pipeconf.extension(P4_INFO_TEXT).isPresent()) {
log.warn("Missing P4Info extension in pipeconf {}", pipeconf.id());
return null;
}
InputStream p4InfoStream = pipeconf.extension(P4_INFO_TEXT).get();
P4Info.Builder p4iInfoBuilder = P4Info.newBuilder();
try {
TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), ExtensionRegistry.getEmptyRegistry(), p4iInfoBuilder);
} catch (IOException ex) {
log.warn("Unable to parse P4Info of pipeconf {}: {}", pipeconf.id(), ex.getMessage());
return null;
}
return p4iInfoBuilder.build();
});
}
use of p4.config.v1.P4InfoOuterClass.P4Info in project onos by opennetworkinglab.
the class P4InfoParserTest method testParseP4RuntimeTranslationAndOptional.
/**
* Tests parse method with P4Runtime translation fields and optional fields.
* @throws Exception if equality group objects dose not match as expected
*/
@Test
public void testParseP4RuntimeTranslationAndOptional() throws Exception {
PiPipelineModel model = P4InfoParser.parse(p4InfoUrl2);
// Generate a P4Info object from the file
final P4Info p4info;
try {
p4info = getP4InfoMessage(p4InfoUrl2);
} catch (IOException e) {
throw new P4InfoParserException("Unable to parse protobuf " + p4InfoUrl.toString(), e);
}
List<Table> tableMsgs = p4info.getTablesList();
PiTableId table0Id = PiTableId.of(tableMsgs.get(0).getPreamble().getName());
// parse tables
PiTableModel table0Model = model.table(table0Id).orElse(null);
// Check matchFields
List<MatchField> matchFieldList = tableMsgs.get(0).getMatchFieldsList();
List<PiMatchFieldModel> piMatchFieldList = new ArrayList<>();
for (MatchField matchFieldIter : matchFieldList) {
MatchField.MatchType matchType = matchFieldIter.getMatchType();
PiMatchType piMatchType;
switch(matchType) {
case EXACT:
piMatchType = PiMatchType.EXACT;
break;
case LPM:
piMatchType = PiMatchType.LPM;
break;
case TERNARY:
piMatchType = PiMatchType.TERNARY;
break;
case RANGE:
piMatchType = PiMatchType.RANGE;
break;
case OPTIONAL:
piMatchType = PiMatchType.OPTIONAL;
break;
default:
Assert.fail();
return;
}
if (matchFieldIter.getTypeName().getName().equals("mac_addr_t")) {
piMatchFieldList.add(new P4MatchFieldModel(PiMatchFieldId.of(matchFieldIter.getName()), P4MatchFieldModel.BIT_WIDTH_UNDEFINED, piMatchType));
} else {
piMatchFieldList.add(new P4MatchFieldModel(PiMatchFieldId.of(matchFieldIter.getName()), matchFieldIter.getBitwidth(), piMatchType));
}
}
assertThat("Incorrect order for matchFields", table0Model.matchFields(), IsIterableContainingInOrder.contains(piMatchFieldList.get(0), piMatchFieldList.get(1), piMatchFieldList.get(2), piMatchFieldList.get(3)));
// check table0 actionsRefs
List<ActionRef> actionRefList = tableMsgs.get(0).getActionRefsList();
assertThat("Incorrect size for actionRefs", actionRefList.size(), is(equalTo(4)));
// create action instances
// Set egress with string as parameter
PiActionId actionId = PiActionId.of("set_egress_port");
PiActionParamId piActionParamId = PiActionParamId.of("port");
PiActionParamModel actionParamModel = new P4ActionParamModel(piActionParamId, P4ActionParamModel.BIT_WIDTH_UNDEFINED);
ImmutableMap<PiActionParamId, PiActionParamModel> params = new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().put(piActionParamId, actionParamModel).build();
PiActionModel setEgressPortAction = new P4ActionModel(actionId, params);
// Set egress with 32 bit as parameter
actionId = PiActionId.of("set_egress_port2");
piActionParamId = PiActionParamId.of("port");
int bitWitdth = 32;
actionParamModel = new P4ActionParamModel(piActionParamId, bitWitdth);
params = new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().put(piActionParamId, actionParamModel).build();
PiActionModel setEgressPortAction2 = new P4ActionModel(actionId, params);
actionId = PiActionId.of("send_to_cpu");
PiActionModel sendToCpuAction = new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
actionId = PiActionId.of("drop");
PiActionModel dropAction = new P4ActionModel(actionId, new ImmutableMap.Builder<PiActionParamId, PiActionParamModel>().build());
// check table0 actions
assertThat("action dose not match", table0Model.actions(), IsIterableContainingInAnyOrder.containsInAnyOrder(setEgressPortAction, setEgressPortAction2, sendToCpuAction, dropAction));
}
use of p4.config.v1.P4InfoOuterClass.P4Info in project onos by opennetworkinglab.
the class P4InfoParser method getP4InfoMessage.
private static P4Info getP4InfoMessage(URL p4InfoUrl) throws IOException {
InputStream p4InfoStream = p4InfoUrl.openStream();
P4Info.Builder p4InfoBuilder = P4Info.newBuilder();
TextFormat.getParser().merge(new InputStreamReader(p4InfoStream), ExtensionRegistry.getEmptyRegistry(), p4InfoBuilder);
return p4InfoBuilder.build();
}
use of p4.config.v1.P4InfoOuterClass.P4Info 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.config.v1.P4InfoOuterClass.P4Info 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