use of org.onosproject.net.pi.service.PiTranslationException in project onos by opennetworkinglab.
the class PiGroupTranslatorImpl method translate.
/**
* Returns a PI action profile group equivalent to the given group, for the
* given pipeconf and device.
*
* @param group group
* @param pipeconf pipeconf
* @param device device
* @return PI action profile group
* @throws PiTranslationException if the group cannot be translated
*/
static PiActionProfileGroup translate(Group group, PiPipeconf pipeconf, Device device) throws PiTranslationException {
if (!SUPPORTED_GROUP_TYPES.contains(group.type())) {
throw new PiTranslationException(format("group type %s not supported", group.type()));
}
// TODO: define proper field in group class.
if (!(group.appCookie() instanceof PiGroupKey)) {
throw new PiTranslationException("group app cookie is not PI (class should be PiGroupKey)");
}
final PiGroupKey groupKey = (PiGroupKey) group.appCookie();
final PiActionProfileId actionProfileId = groupKey.actionProfileId();
// Check validity of action profile against pipeconf.
final PiActionProfileModel actionProfileModel = pipeconf.pipelineModel().actionProfiles(actionProfileId).orElseThrow(() -> new PiTranslationException(format("no such action profile '%s'", actionProfileId)));
if (!actionProfileModel.hasSelector()) {
throw new PiTranslationException(format("action profile '%s' does not support dynamic selection", actionProfileId));
}
// Check if the table associated with the action profile supports only
// one-shot action profile programming.
boolean isTableOneShot = actionProfileModel.tables().stream().map(tableId -> pipeconf.pipelineModel().table(tableId)).allMatch(piTableModel -> piTableModel.isPresent() && piTableModel.get().oneShotOnly());
if (isTableOneShot) {
throw new PiTranslationException(format("Table associated to action profile '%s' supports only one-shot action profile programming", actionProfileId));
}
// Check group validity.
if (actionProfileModel.maxGroupSize() > 0 && group.buckets().buckets().size() > actionProfileModel.maxGroupSize()) {
throw new PiTranslationException(format("too many buckets, max group size for action profile '%s' is %d", actionProfileId, actionProfileModel.maxGroupSize()));
}
// If not INDIRECT, we set the maximum group size as specified in the
// model, however this might be highly inefficient for some HW targets
// which pre-allocate resources for the whole group.
final int maxGroupSize = group.type() == GroupDescription.Type.INDIRECT ? 1 : actionProfileModel.maxGroupSize();
final PiActionProfileGroup.Builder piActionGroupBuilder = PiActionProfileGroup.builder().withId(PiActionProfileGroupId.of(group.id().id())).withActionProfileId(groupKey.actionProfileId()).withMaxSize(maxGroupSize);
// Translate group buckets to PI group members
final PiPipelineInterpreter interpreter = getInterpreterOrNull(device, pipeconf);
short bucketIdx = 0;
for (GroupBucket bucket : group.buckets().buckets()) {
/*
FIXME: the way member IDs are computed can cause collisions!
Problem: In P4Runtime action profile members, i.e. action buckets,
are associated to a numeric ID chosen at member insertion time. This
ID must be unique for the whole action profile (i.e. the group table
in OpenFlow). In ONOS, GroupBucket doesn't specify any ID.
Solutions:
- Change GroupBucket API to force application wanting to perform
group operations to specify a member id.
- Maintain state to dynamically allocate/deallocate member IDs, e.g.
in a dedicated service, or in a P4Runtime Group Provider.
Hack: Statically derive member ID by combining groupId and position
of the bucket in the list.
*/
final int memberId = Objects.hash(group.id(), bucketIdx);
if (memberId == 0) {
throw new PiTranslationException("GroupBucket produces PiActionProfileMember " + "with invalid ID 0");
}
bucketIdx++;
final PiTableAction tableAction = translateTreatment(bucket.treatment(), interpreter, groupKey.tableId(), pipeconf.pipelineModel());
if (tableAction == null) {
throw new PiTranslationException("bucket treatment translator returned null");
}
if (tableAction.type() != ACTION) {
throw new PiTranslationException(format("action of type '%s' cannot be used in action profile members", tableAction.type()));
}
final PiActionProfileMember member = PiActionProfileMember.builder().forActionProfile(groupKey.actionProfileId()).withId(PiActionProfileMemberId.of(memberId)).withAction((PiAction) tableAction).build();
// NOTE Indirect groups have weight set to -1 which is not supported
// by P4RT - setting to 1 to avoid problems with the p4rt server.
final int weight = group.type() == GroupDescription.Type.INDIRECT ? 1 : bucket.weight();
piActionGroupBuilder.addMember(member, weight);
}
return piActionGroupBuilder.build();
}
use of org.onosproject.net.pi.service.PiTranslationException in project onos by opennetworkinglab.
the class CriterionTranslatorHelper method translateCriterion.
/**
* Translates a given criterion instance to a PiFieldMatch with the given id, match type, and bit-width.
*
* @param fieldId PI match field identifier
* @param criterion criterion
* @param matchType match type
* @param bitWidth size of the field match in bits
* @return a PI field match
* @throws PiTranslationException if the criterion cannot be translated (see exception message)
*/
static PiFieldMatch translateCriterion(Criterion criterion, PiMatchFieldId fieldId, PiMatchType matchType, int bitWidth) throws PiTranslationException {
if (!TRANSLATORS.containsKey(criterion.getClass())) {
throw new PiTranslationException(format("Translation of criterion class %s is not implemented.", criterion.getClass().getSimpleName()));
}
try {
final CriterionTranslator translator = TRANSLATORS.get(criterion.getClass()).newInstance();
translator.init(criterion, bitWidth);
switch(matchType) {
case EXACT:
return new PiExactFieldMatch(fieldId, translator.exactMatch());
case OPTIONAL:
return new PiOptionalFieldMatch(fieldId, translator.exactMatch());
case TERNARY:
final Pair<ImmutableByteSequence, ImmutableByteSequence> tp = translator.ternaryMatch();
return new PiTernaryFieldMatch(fieldId, tp.getLeft(), tp.getRight());
case LPM:
final Pair<ImmutableByteSequence, Integer> lp = translator.lpmMatch();
return new PiLpmFieldMatch(fieldId, lp.getLeft(), lp.getRight());
default:
throw new PiTranslationException(format("Translation of criterion %s (%s class) to match type %s is not implemented.", criterion.type().name(), criterion.getClass().getSimpleName(), matchType.name()));
}
} catch (ByteSequenceTrimException e) {
throw new PiTranslationException(format("Size mismatch for criterion %s: %s", criterion.type(), e.getMessage()));
} catch (CriterionTranslatorException e) {
throw new PiTranslationException(format("Unable to translate criterion %s: %s", criterion.type(), e.getMessage()));
} catch (InstantiationException | IllegalAccessException e) {
// Was not able to instantiate the criterion translator.
throw new IllegalStateException(e);
}
}
use of org.onosproject.net.pi.service.PiTranslationException in project onos by opennetworkinglab.
the class PiFlowRuleTranslatorImpl method translate.
/**
* Returns a PI table entry equivalent to the given flow rule, for the given
* pipeconf and device.
*
* @param rule flow rule
* @param pipeconf pipeconf
* @param device device
* @return PI table entry
* @throws PiTranslationException if the flow rule cannot be translated
*/
static PiTableEntry translate(FlowRule rule, PiPipeconf pipeconf, Device device) throws PiTranslationException {
PiPipelineModel pipelineModel = pipeconf.pipelineModel();
// Retrieve interpreter, if any.
final PiPipelineInterpreter interpreter = getInterpreterOrNull(device, pipeconf);
// Get table model.
final PiTableId piTableId = translateTableId(rule.table(), interpreter);
final PiTableModel tableModel = getTableModel(piTableId, pipelineModel);
// Translate selector.
final PiMatchKey piMatchKey;
final boolean needPriority;
if (rule.selector().criteria().isEmpty()) {
piMatchKey = PiMatchKey.EMPTY;
needPriority = false;
} else {
final Collection<PiFieldMatch> fieldMatches = translateFieldMatches(interpreter, rule.selector(), tableModel);
piMatchKey = PiMatchKey.builder().addFieldMatches(fieldMatches).build();
// FIXME: P4Runtime limit
// Need to ignore priority if no TCAM lookup match field
needPriority = tableModel.matchFields().stream().anyMatch(match -> match.matchType() == PiMatchType.TERNARY || match.matchType() == PiMatchType.RANGE || match.matchType() == PiMatchType.OPTIONAL);
}
// Translate treatment.
final PiTableAction piTableAction = translateTreatment(rule.treatment(), interpreter, piTableId, pipelineModel);
// Build PI entry.
final PiTableEntry.Builder tableEntryBuilder = PiTableEntry.builder();
tableEntryBuilder.forTable(piTableId).withMatchKey(piMatchKey);
if (piTableAction != null) {
tableEntryBuilder.withAction(piTableAction);
}
if (needPriority) {
// FIXME: move priority check to P4Runtime driver.
final int newPriority;
if (rule.priority() > MAX_PI_PRIORITY) {
log.warn("Flow rule priority too big, setting translated priority to max value {}: {}", MAX_PI_PRIORITY, rule);
newPriority = MAX_PI_PRIORITY;
} else {
newPriority = MIN_PI_PRIORITY + rule.priority();
}
tableEntryBuilder.withPriority(newPriority);
}
if (!rule.isPermanent()) {
if (tableModel.supportsAging()) {
tableEntryBuilder.withTimeout(rule.timeout());
} else {
log.debug("Flow rule is temporary, but table '{}' doesn't support " + "aging, translating to permanent.", tableModel.id());
}
}
return tableEntryBuilder.build();
}
use of org.onosproject.net.pi.service.PiTranslationException in project onos by opennetworkinglab.
the class PiFlowRuleTranslatorImpl method checkPiAction.
private static PiTableAction checkPiAction(PiAction piAction, PiTableModel table) throws PiTranslationException {
// Table supports this action?
PiActionModel actionModel = table.action(piAction.id()).orElseThrow(() -> new PiTranslationException(format("Not such action '%s' for table '%s'", piAction.id(), table.id())));
// Is the number of runtime parameters correct?
if (actionModel.params().size() != piAction.parameters().size()) {
throw new PiTranslationException(format("Wrong number of runtime parameters for action '%s', expected %d but found %d", actionModel.id(), actionModel.params().size(), piAction.parameters().size()));
}
// Forge a new action instance with well-sized parameters.
// The same comment as in typeCheckFieldMatch() about duplicating field match instances applies here.
PiAction.Builder newActionBuilder = PiAction.builder().withId(piAction.id());
for (PiActionParam param : piAction.parameters()) {
PiActionParamModel paramModel = actionModel.param(param.id()).orElseThrow(() -> new PiTranslationException(format("Not such parameter '%s' for action '%s'", param.id(), actionModel)));
try {
newActionBuilder.withParameter(new PiActionParam(param.id(), paramModel.hasBitWidth() ? param.value().fit(paramModel.bitWidth()) : param.value()));
} catch (ByteSequenceTrimException e) {
throw new PiTranslationException(format("Size mismatch for parameter '%s' of action '%s': %s", param.id(), piAction.id(), e.getMessage()));
}
}
return newActionBuilder.build();
}
use of org.onosproject.net.pi.service.PiTranslationException in project onos by opennetworkinglab.
the class PiReplicationGroupTranslatorImpl method logicalToPipelineSpecific.
private static PortNumber logicalToPipelineSpecific(PortNumber logicalPort, Device device) throws PiTranslationException {
if (!device.is(PiPipelineInterpreter.class)) {
throw new PiTranslationException("missing interpreter, cannot map logical port " + logicalPort.toString());
}
final PiPipelineInterpreter interpreter = device.as(PiPipelineInterpreter.class);
Optional<Long> mappedPort = interpreter.mapLogicalPort(logicalPort);
if (!mappedPort.isPresent()) {
throw new PiTranslationException("interpreter cannot map logical port " + logicalPort.toString());
}
return PortNumber.portNumber(mappedPort.get());
}
Aggregations