use of org.onosproject.net.pi.runtime.PiTableAction 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.runtime.PiTableAction 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.runtime.PiTableAction in project onos by opennetworkinglab.
the class PiGroupTranslatorImplTest method selectOutputBucket.
private static GroupBucket selectOutputBucket(int portNum) {
ImmutableByteSequence paramVal = copyFrom(portNum);
PiActionParam param = new PiActionParam(PORT, paramVal);
PiTableAction action = PiAction.builder().withId(INGRESS_WCMP_CONTROL_SET_EGRESS_PORT).withParameter(param).build();
TrafficTreatment treatment = DefaultTrafficTreatment.builder().add(Instructions.piTableAction(action)).build();
return DefaultGroupBucket.createSelectGroupBucket(treatment);
}
use of org.onosproject.net.pi.runtime.PiTableAction in project onos by opennetworkinglab.
the class InstructionCodecTest method piInstructionEncodingTest.
/**
* Tests the encoding of protocol-independent instructions.
*/
@Test
public void piInstructionEncodingTest() {
PiActionId actionId = PiActionId.of("set_egress_port");
PiActionParamId actionParamId = PiActionParamId.of("port");
PiActionParam actionParam = new PiActionParam(actionParamId, ImmutableByteSequence.copyFrom(10));
PiTableAction action = PiAction.builder().withId(actionId).withParameter(actionParam).build();
final PiInstruction actionInstruction = Instructions.piTableAction(action);
final ObjectNode actionInstructionJson = instructionCodec.encode(actionInstruction, context);
assertThat(actionInstructionJson, matchesInstruction(actionInstruction));
PiTableAction actionGroupId = PiActionProfileGroupId.of(10);
final PiInstruction actionGroupIdInstruction = Instructions.piTableAction(actionGroupId);
final ObjectNode actionGroupIdInstructionJson = instructionCodec.encode(actionGroupIdInstruction, context);
assertThat(actionGroupIdInstructionJson, matchesInstruction(actionGroupIdInstruction));
PiTableAction actionProfileMemberId = PiActionProfileMemberId.of(10);
final PiInstruction actionProfileMemberIdInstruction = Instructions.piTableAction(actionProfileMemberId);
final ObjectNode actionProfileMemberIdInstructionJson = instructionCodec.encode(actionProfileMemberIdInstruction, context);
assertThat(actionProfileMemberIdInstructionJson, matchesInstruction(actionProfileMemberIdInstruction));
}
use of org.onosproject.net.pi.runtime.PiTableAction in project onos by opennetworkinglab.
the class InstructionCodecTest method piInstructionDecodingTest.
/**
* Tests the decoding of protocol-independent instructions.
*/
@Test
public void piInstructionDecodingTest() throws IOException {
Instruction actionInstruction = getInstruction("PiActionInstruction.json");
Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT));
PiTableAction action = ((PiInstruction) actionInstruction).action();
Assert.assertThat(action.type(), is(PiTableAction.Type.ACTION));
Assert.assertThat(((PiAction) action).id().id(), is("set_egress_port"));
Assert.assertThat(((PiAction) action).parameters().size(), is(1));
Collection<PiActionParam> actionParams = ((PiAction) action).parameters();
PiActionParam actionParam = actionParams.iterator().next();
Assert.assertThat(actionParam.id().id(), is("port"));
Assert.assertThat(actionParam.value(), is(copyFrom((byte) 0x1)));
Instruction actionGroupIdInstruction = getInstruction("PiActionProfileGroupIdInstruction.json");
Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT));
PiTableAction actionGroupId = ((PiInstruction) actionGroupIdInstruction).action();
Assert.assertThat(actionGroupId.type(), is(PiTableAction.Type.ACTION_PROFILE_GROUP_ID));
Assert.assertThat(((PiActionProfileGroupId) actionGroupId).id(), is(100));
Instruction actionMemberIdInstruction = getInstruction("PiActionProfileMemberIdInstruction.json");
Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT));
PiTableAction actionMemberId = ((PiInstruction) actionMemberIdInstruction).action();
Assert.assertThat(actionMemberId.type(), is(PiTableAction.Type.ACTION_PROFILE_MEMBER_ID));
Assert.assertThat(((PiActionProfileMemberId) actionMemberId).id(), is(100));
}
Aggregations