use of org.onosproject.net.pi.runtime.PiLpmFieldMatch in project onos by opennetworkinglab.
the class PiFlowRuleTranslatorImpl method typeCheckFieldMatch.
private static PiFieldMatch typeCheckFieldMatch(PiFieldMatch fieldMatch, PiMatchFieldModel fieldModel) throws PiTranslationException {
// Check parameter type and size
if (!fieldModel.matchType().equals(fieldMatch.type())) {
throw new PiTranslationException(format("Wrong match type for field '%s', expected %s, but found %s", fieldMatch.fieldId(), fieldModel.matchType().name(), fieldMatch.type().name()));
}
// Check if the arbitrary bit width is supported
if (!fieldModel.hasBitWidth() && !fieldModel.matchType().equals(PiMatchType.EXACT) && !fieldModel.matchType().equals(PiMatchType.OPTIONAL)) {
throw new PiTranslationException(format("Arbitrary bit width for field '%s' and match type %s is not supported", fieldMatch.fieldId(), fieldModel.matchType().name()));
}
int modelBitWidth = fieldModel.bitWidth();
try {
switch(fieldModel.matchType()) {
case EXACT:
PiExactFieldMatch exactField = (PiExactFieldMatch) fieldMatch;
return new PiExactFieldMatch(fieldMatch.fieldId(), fieldModel.hasBitWidth() ? exactField.value().fit(modelBitWidth) : exactField.value());
case TERNARY:
PiTernaryFieldMatch ternField = (PiTernaryFieldMatch) fieldMatch;
ImmutableByteSequence ternMask = ternField.mask().fit(modelBitWidth);
ImmutableByteSequence ternValue = ternField.value().fit(modelBitWidth).bitwiseAnd(ternMask);
return new PiTernaryFieldMatch(fieldMatch.fieldId(), ternValue, ternMask);
case LPM:
PiLpmFieldMatch lpmfield = (PiLpmFieldMatch) fieldMatch;
if (lpmfield.prefixLength() > modelBitWidth) {
throw new PiTranslationException(format("Invalid prefix length for LPM field '%s', found %d but field has bit-width %d", fieldMatch.fieldId(), lpmfield.prefixLength(), modelBitWidth));
}
ImmutableByteSequence lpmValue = lpmfield.value().fit(modelBitWidth);
ImmutableByteSequence lpmMask = prefixOnes(lpmValue.size(), lpmfield.prefixLength());
lpmValue = lpmValue.bitwiseAnd(lpmMask);
return new PiLpmFieldMatch(fieldMatch.fieldId(), lpmValue, lpmfield.prefixLength());
case RANGE:
return new PiRangeFieldMatch(fieldMatch.fieldId(), ((PiRangeFieldMatch) fieldMatch).lowValue().fit(modelBitWidth), ((PiRangeFieldMatch) fieldMatch).highValue().fit(modelBitWidth));
case OPTIONAL:
PiOptionalFieldMatch optionalField = (PiOptionalFieldMatch) fieldMatch;
return new PiOptionalFieldMatch(fieldMatch.fieldId(), fieldModel.hasBitWidth() ? optionalField.value().fit(modelBitWidth) : optionalField.value());
default:
// Should never be here.
throw new IllegalArgumentException("Unrecognized match type " + fieldModel.matchType().name());
}
} catch (ByteSequenceTrimException e) {
throw new PiTranslationException(format("Size mismatch for field %s: %s", fieldMatch.fieldId(), e.getMessage()));
}
}
use of org.onosproject.net.pi.runtime.PiLpmFieldMatch in project onos by opennetworkinglab.
the class FieldMatchCodec method encode.
@Override
public P4RuntimeOuterClass.FieldMatch encode(PiFieldMatch piFieldMatch, P4InfoOuterClass.Preamble tablePreamble, PiPipeconf pipeconf, P4InfoBrowser browser) throws CodecException, P4InfoBrowser.NotFoundException {
P4RuntimeOuterClass.FieldMatch.Builder messageBuilder = P4RuntimeOuterClass.FieldMatch.newBuilder();
// FIXME: check how field names for stacked headers are constructed in P4Runtime.
String fieldName = piFieldMatch.fieldId().id();
P4InfoOuterClass.MatchField matchFieldInfo = browser.matchFields(tablePreamble.getId()).getByName(fieldName);
String entityName = format("field match '%s' of table '%s'", matchFieldInfo.getName(), tablePreamble.getName());
int fieldId = matchFieldInfo.getId();
int fieldBitwidth = matchFieldInfo.getBitwidth();
boolean isSdnString = browser.isTypeString(matchFieldInfo.getTypeName());
messageBuilder.setFieldId(fieldId);
switch(piFieldMatch.type()) {
case EXACT:
PiExactFieldMatch fieldMatch = (PiExactFieldMatch) piFieldMatch;
ByteString exactValue;
if (isSdnString) {
exactValue = ByteString.copyFrom(fieldMatch.value().asReadOnlyBuffer());
} else {
exactValue = ByteString.copyFrom(fieldMatch.value().canonical().asReadOnlyBuffer());
assertSize(VALUE_OF_PREFIX + entityName, exactValue, fieldBitwidth);
}
return messageBuilder.setExact(P4RuntimeOuterClass.FieldMatch.Exact.newBuilder().setValue(exactValue).build()).build();
case TERNARY:
PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) piFieldMatch;
ByteString ternaryValue = ByteString.copyFrom(ternaryMatch.value().canonical().asReadOnlyBuffer());
ByteString ternaryMask = ByteString.copyFrom(ternaryMatch.mask().canonical().asReadOnlyBuffer());
if (isSdnString) {
sdnStringUnsupported(entityName, piFieldMatch.type());
}
assertSize(VALUE_OF_PREFIX + entityName, ternaryValue, fieldBitwidth);
assertSize(MASK_OF_PREFIX + entityName, ternaryMask, fieldBitwidth);
return messageBuilder.setTernary(P4RuntimeOuterClass.FieldMatch.Ternary.newBuilder().setValue(ternaryValue).setMask(ternaryMask).build()).build();
case LPM:
PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) piFieldMatch;
ByteString lpmValue = ByteString.copyFrom(lpmMatch.value().canonical().asReadOnlyBuffer());
int lpmPrefixLen = lpmMatch.prefixLength();
if (isSdnString) {
sdnStringUnsupported(entityName, piFieldMatch.type());
}
assertSize(VALUE_OF_PREFIX + entityName, lpmValue, fieldBitwidth);
assertPrefixLen(entityName, lpmPrefixLen, fieldBitwidth);
return messageBuilder.setLpm(P4RuntimeOuterClass.FieldMatch.LPM.newBuilder().setValue(lpmValue).setPrefixLen(lpmPrefixLen).build()).build();
case RANGE:
PiRangeFieldMatch rangeMatch = (PiRangeFieldMatch) piFieldMatch;
ByteString rangeHighValue = ByteString.copyFrom(rangeMatch.highValue().canonical().asReadOnlyBuffer());
ByteString rangeLowValue = ByteString.copyFrom(rangeMatch.lowValue().canonical().asReadOnlyBuffer());
if (isSdnString) {
sdnStringUnsupported(entityName, piFieldMatch.type());
}
assertSize(HIGH_RANGE_VALUE_OF_PREFIX + entityName, rangeHighValue, fieldBitwidth);
assertSize(LOW_RANGE_VALUE_OF_PREFIX + entityName, rangeLowValue, fieldBitwidth);
return messageBuilder.setRange(P4RuntimeOuterClass.FieldMatch.Range.newBuilder().setHigh(rangeHighValue).setLow(rangeLowValue).build()).build();
case OPTIONAL:
PiOptionalFieldMatch optionalMatch = (PiOptionalFieldMatch) piFieldMatch;
ByteString optionalValue;
if (isSdnString) {
optionalValue = ByteString.copyFrom(optionalMatch.value().asReadOnlyBuffer());
} else {
optionalValue = ByteString.copyFrom(optionalMatch.value().canonical().asReadOnlyBuffer());
assertSize(VALUE_OF_PREFIX + entityName, optionalValue, fieldBitwidth);
}
return messageBuilder.setOptional(P4RuntimeOuterClass.FieldMatch.Optional.newBuilder().setValue(optionalValue).build()).build();
default:
throw new CodecException(format("Building of match type %s not implemented", piFieldMatch.type()));
}
}
use of org.onosproject.net.pi.runtime.PiLpmFieldMatch in project onos by opennetworkinglab.
the class FieldMatchCodec method decode.
@Override
public PiFieldMatch decode(P4RuntimeOuterClass.FieldMatch message, P4InfoOuterClass.Preamble tablePreamble, PiPipeconf pipeconf, P4InfoBrowser browser) throws CodecException, P4InfoBrowser.NotFoundException {
final P4InfoOuterClass.MatchField matchField = browser.matchFields(tablePreamble.getId()).getById(message.getFieldId());
final int fieldBitwidth = matchField.getBitwidth();
final PiMatchFieldId headerFieldId = PiMatchFieldId.of(matchField.getName());
final boolean isSdnString = browser.isTypeString(matchField.getTypeName());
final P4RuntimeOuterClass.FieldMatch.FieldMatchTypeCase typeCase = message.getFieldMatchTypeCase();
try {
switch(typeCase) {
case EXACT:
P4RuntimeOuterClass.FieldMatch.Exact exactFieldMatch = message.getExact();
final ImmutableByteSequence exactValue;
if (isSdnString) {
exactValue = copyFrom(new String(exactFieldMatch.getValue().toByteArray()));
} else {
exactValue = copyAndFit(exactFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
}
return new PiExactFieldMatch(headerFieldId, exactValue);
case TERNARY:
P4RuntimeOuterClass.FieldMatch.Ternary ternaryFieldMatch = message.getTernary();
ImmutableByteSequence ternaryValue = copyAndFit(ternaryFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
ImmutableByteSequence ternaryMask = copyAndFit(ternaryFieldMatch.getMask().asReadOnlyByteBuffer(), fieldBitwidth);
return new PiTernaryFieldMatch(headerFieldId, ternaryValue, ternaryMask);
case LPM:
P4RuntimeOuterClass.FieldMatch.LPM lpmFieldMatch = message.getLpm();
ImmutableByteSequence lpmValue = copyAndFit(lpmFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
int lpmPrefixLen = lpmFieldMatch.getPrefixLen();
return new PiLpmFieldMatch(headerFieldId, lpmValue, lpmPrefixLen);
case RANGE:
P4RuntimeOuterClass.FieldMatch.Range rangeFieldMatch = message.getRange();
ImmutableByteSequence rangeHighValue = copyAndFit(rangeFieldMatch.getHigh().asReadOnlyByteBuffer(), fieldBitwidth);
ImmutableByteSequence rangeLowValue = copyAndFit(rangeFieldMatch.getLow().asReadOnlyByteBuffer(), fieldBitwidth);
return new PiRangeFieldMatch(headerFieldId, rangeLowValue, rangeHighValue);
case OPTIONAL:
P4RuntimeOuterClass.FieldMatch.Optional optionalFieldMatch = message.getOptional();
final ImmutableByteSequence optionalValue;
if (isSdnString) {
optionalValue = copyFrom(new String(optionalFieldMatch.getValue().toByteArray()));
} else {
optionalValue = copyAndFit(optionalFieldMatch.getValue().asReadOnlyByteBuffer(), fieldBitwidth);
}
return new PiOptionalFieldMatch(headerFieldId, optionalValue);
default:
throw new CodecException(format("Decoding of field match type '%s' not implemented", typeCase.name()));
}
} catch (ImmutableByteSequence.ByteSequenceTrimException e) {
throw new CodecException(e.getMessage());
}
}
use of org.onosproject.net.pi.runtime.PiLpmFieldMatch in project onos by opennetworkinglab.
the class PiCriterionTranslatorsTest method testIpCriterion.
@Test
public void testIpCriterion() throws Exception {
IpPrefix prefix1 = IpPrefix.valueOf(random.nextInt(), random.nextInt(32));
int bitWidth = prefix1.address().toOctets().length * 8;
IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(prefix1);
PiLpmFieldMatch lpmMatch = (PiLpmFieldMatch) translateCriterion(criterion, fieldId, LPM, bitWidth);
assertThat(lpmMatch.value().asArray(), is(criterion.ip().address().toOctets()));
assertThat(lpmMatch.prefixLength(), is(criterion.ip().prefixLength()));
}
use of org.onosproject.net.pi.runtime.PiLpmFieldMatch in project up4 by omec-project.
the class Up4TranslatorImpl method upfEntityToUp4TableEntry.
@Override
public PiTableEntry upfEntityToUp4TableEntry(UpfEntity entity) throws Up4TranslationException {
PiTableEntry.Builder tableEntryBuilder = PiTableEntry.builder();
PiAction.Builder actionBuilder = PiAction.builder();
PiMatchKey.Builder matchBuilder = PiMatchKey.builder();
switch(entity.type()) {
case INTERFACE:
tableEntryBuilder.forTable(PRE_QOS_PIPE_INTERFACES);
UpfInterface upfIntf = (UpfInterface) entity;
byte direction;
byte srcIface;
actionBuilder.withId(PRE_QOS_PIPE_SET_SOURCE_IFACE);
if (upfIntf.isAccess()) {
srcIface = IFACE_ACCESS;
direction = DIRECTION_UPLINK;
} else if (upfIntf.isCore()) {
srcIface = IFACE_CORE;
direction = DIRECTION_DOWNLINK;
} else {
throw new Up4TranslationException("UPF Interface is not Access nor CORE: " + upfIntf);
}
actionBuilder.withParameter(new PiActionParam(SRC_IFACE, srcIface)).withParameter(new PiActionParam(DIRECTION, direction)).withParameter(new PiActionParam(SLICE_ID, (byte) upfIntf.sliceId()));
matchBuilder.addFieldMatch(new PiLpmFieldMatch(HDR_IPV4_DST_PREFIX, ImmutableByteSequence.copyFrom(upfIntf.prefix().address().toOctets()), upfIntf.prefix().prefixLength()));
break;
case SESSION_UPLINK:
tableEntryBuilder.forTable(PRE_QOS_PIPE_SESSIONS_UPLINK);
UpfSessionUplink sessionUplink = (UpfSessionUplink) entity;
matchBuilder.addFieldMatch(new PiExactFieldMatch(HDR_N3_ADDRESS, ImmutableByteSequence.copyFrom(sessionUplink.tunDstAddr().toOctets()))).addFieldMatch(new PiExactFieldMatch(HDR_TEID, ImmutableByteSequence.copyFrom(sessionUplink.teid())));
if (sessionUplink.needsDropping()) {
actionBuilder.withId(PRE_QOS_PIPE_SET_SESSION_UPLINK_DROP);
} else {
actionBuilder.withId(PRE_QOS_PIPE_SET_SESSION_UPLINK);
actionBuilder.withParameter(new PiActionParam(SESSION_METER_IDX, sessionUplink.sessionMeterIdx()));
}
break;
case SESSION_DOWNLINK:
tableEntryBuilder.forTable(PRE_QOS_PIPE_SESSIONS_DOWNLINK);
UpfSessionDownlink sessionDownlink = (UpfSessionDownlink) entity;
matchBuilder.addFieldMatch(new PiExactFieldMatch(HDR_UE_ADDRESS, ImmutableByteSequence.copyFrom(sessionDownlink.ueAddress().toOctets())));
if (sessionDownlink.needsDropping() && sessionDownlink.needsBuffering()) {
log.error("We don't support DROP + BUFF on the UP4 northbound! Defaulting to only BUFF");
actionBuilder.withId(PRE_QOS_PIPE_SET_SESSION_DOWNLINK_BUFF);
actionBuilder.withParameter(new PiActionParam(SESSION_METER_IDX, sessionDownlink.sessionMeterIdx()));
} else if (sessionDownlink.needsDropping()) {
actionBuilder.withId(PRE_QOS_PIPE_SET_SESSION_DOWNLINK_DROP);
} else {
actionBuilder.withParameter(new PiActionParam(SESSION_METER_IDX, sessionDownlink.sessionMeterIdx()));
if (sessionDownlink.needsBuffering()) {
actionBuilder.withId(PRE_QOS_PIPE_SET_SESSION_DOWNLINK_BUFF);
} else {
actionBuilder.withParameter(new PiActionParam(TUNNEL_PEER_ID, ImmutableByteSequence.copyFrom(sessionDownlink.tunPeerId())));
actionBuilder.withId(PRE_QOS_PIPE_SET_SESSION_DOWNLINK);
}
}
break;
case TERMINATION_UPLINK:
tableEntryBuilder.forTable(PRE_QOS_PIPE_TERMINATIONS_UPLINK);
UpfTerminationUplink upfTerminationUl = (UpfTerminationUplink) entity;
matchBuilder.addFieldMatch(new PiExactFieldMatch(HDR_UE_ADDRESS, ImmutableByteSequence.copyFrom(upfTerminationUl.ueSessionId().toOctets()))).addFieldMatch(new PiExactFieldMatch(HDR_APP_ID, ImmutableByteSequence.copyFrom(upfTerminationUl.applicationId())));
actionBuilder.withParameter(new PiActionParam(CTR_IDX, upfTerminationUl.counterId()));
if (upfTerminationUl.needsDropping()) {
actionBuilder.withId(PRE_QOS_PIPE_UPLINK_TERM_DROP);
} else {
actionBuilder.withId(PRE_QOS_PIPE_UPLINK_TERM_FWD).withParameter(new PiActionParam(TC, upfTerminationUl.trafficClass())).withParameter(new PiActionParam(APP_METER_IDX, upfTerminationUl.appMeterIdx()));
}
break;
case TERMINATION_DOWNLINK:
tableEntryBuilder.forTable(PRE_QOS_PIPE_TERMINATIONS_DOWNLINK);
UpfTerminationDownlink upfTerminationDl = (UpfTerminationDownlink) entity;
matchBuilder.addFieldMatch(new PiExactFieldMatch(HDR_UE_ADDRESS, ImmutableByteSequence.copyFrom(upfTerminationDl.ueSessionId().toOctets()))).addFieldMatch(new PiExactFieldMatch(HDR_APP_ID, ImmutableByteSequence.copyFrom(upfTerminationDl.applicationId())));
actionBuilder.withParameter(new PiActionParam(CTR_IDX, upfTerminationDl.counterId()));
if (upfTerminationDl.needsDropping()) {
actionBuilder.withId(PRE_QOS_PIPE_DOWNLINK_TERM_DROP);
} else {
actionBuilder.withId(PRE_QOS_PIPE_DOWNLINK_TERM_FWD).withParameter(new PiActionParam(TEID, upfTerminationDl.teid())).withParameter(new PiActionParam(QFI, upfTerminationDl.qfi())).withParameter(new PiActionParam(TC, upfTerminationDl.trafficClass())).withParameter(new PiActionParam(APP_METER_IDX, upfTerminationDl.appMeterIdx()));
}
break;
case TUNNEL_PEER:
tableEntryBuilder.forTable(PRE_QOS_PIPE_TUNNEL_PEERS);
UpfGtpTunnelPeer gtpTunnelPeer = (UpfGtpTunnelPeer) entity;
matchBuilder.addFieldMatch(new PiExactFieldMatch(HDR_TUNNEL_PEER_ID, ImmutableByteSequence.copyFrom(gtpTunnelPeer.tunPeerId())));
actionBuilder.withId(PRE_QOS_PIPE_LOAD_TUNNEL_PARAM).withParameter(new PiActionParam(SRC_ADDR, gtpTunnelPeer.src().toOctets())).withParameter(new PiActionParam(DST_ADDR, gtpTunnelPeer.dst().toOctets())).withParameter(new PiActionParam(SPORT, gtpTunnelPeer.srcPort()));
break;
case APPLICATION:
tableEntryBuilder.forTable(PRE_QOS_PIPE_APPLICATIONS);
UpfApplication application = (UpfApplication) entity;
tableEntryBuilder.withPriority(application.priority());
actionBuilder.withId(PRE_QOS_PIPE_SET_APP_ID).withParameter(new PiActionParam(APP_ID, application.appId()));
matchBuilder.addFieldMatch(new PiExactFieldMatch(HDR_SLICE_ID, ImmutableByteSequence.copyFrom((byte) application.sliceId())));
if (application.ip4Prefix().isPresent()) {
Ip4Prefix ipPrefix = application.ip4Prefix().get();
matchBuilder.addFieldMatch(new PiLpmFieldMatch(HDR_APP_IP_ADDR, ImmutableByteSequence.copyFrom(ipPrefix.address().toOctets()), ipPrefix.prefixLength()));
}
if (application.l4PortRange().isPresent()) {
Range<Short> portRange = application.l4PortRange().get();
matchBuilder.addFieldMatch(new PiRangeFieldMatch(HDR_APP_L4_PORT, ImmutableByteSequence.copyFrom(portRange.lowerEndpoint()), ImmutableByteSequence.copyFrom(portRange.upperEndpoint())));
}
if (application.ipProto().isPresent()) {
byte ipProto = application.ipProto().get();
matchBuilder.addFieldMatch(new PiTernaryFieldMatch(HDR_APP_IP_PROTO, ImmutableByteSequence.copyFrom(ipProto), ImmutableByteSequence.ofOnes(1)));
}
break;
case SESSION_METER:
case APPLICATION_METER:
case SLICE_METER:
case COUNTER:
case INGRESS_COUNTER:
case EGRESS_COUNTER:
default:
throw new Up4TranslationException("Attempting to translate an unsupported UPF entity to a table entry! " + entity);
}
return tableEntryBuilder.withMatchKey(matchBuilder.build()).withAction(actionBuilder.build()).build();
}
Aggregations