use of org.onlab.util.ImmutableByteSequence 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.onlab.util.ImmutableByteSequence in project onos by opennetworkinglab.
the class P4DataCodec method encodeHeader.
private static P4Header encodeHeader(PiHeader piHeader) {
P4Header.Builder builder = P4Header.newBuilder();
int i = 0;
for (ImmutableByteSequence bitString : piHeader.bitStrings()) {
builder.setBitstrings(i, ByteString.copyFrom(bitString.asArray()));
i++;
}
return builder.setIsValid(piHeader.isValid()).build();
}
use of org.onlab.util.ImmutableByteSequence in project onos by opennetworkinglab.
the class ActionCodec method decode.
@Override
protected PiAction decode(P4RuntimeOuterClass.Action message, Object ignored, PiPipeconf pipeconf, P4InfoBrowser browser) throws P4InfoBrowser.NotFoundException, CodecException {
final P4InfoBrowser.EntityBrowser<P4InfoOuterClass.Action.Param> paramInfo = browser.actionParams(message.getActionId());
final String actionName = browser.actions().getById(message.getActionId()).getPreamble().getName();
final PiAction.Builder builder = PiAction.builder().withId(PiActionId.of(actionName));
for (P4RuntimeOuterClass.Action.Param p : message.getParamsList()) {
final P4InfoOuterClass.Action.Param actionParam = paramInfo.getById(p.getParamId());
final ImmutableByteSequence value;
if (browser.isTypeString(actionParam.getTypeName())) {
value = copyFrom(new String(p.getValue().toByteArray()));
} else {
try {
value = copyAndFit(p.getValue().asReadOnlyByteBuffer(), actionParam.getBitwidth());
} catch (ImmutableByteSequence.ByteSequenceTrimException e) {
throw new CodecException(e.getMessage());
}
}
builder.withParameter(new PiActionParam(PiActionParamId.of(actionParam.getName()), value));
}
return builder.build();
}
use of org.onlab.util.ImmutableByteSequence in project onos by opennetworkinglab.
the class PiCriterionTranslatorsTest method testLpmToTernaryTranslation.
@Test
public void testLpmToTernaryTranslation() throws Exception {
IpPrefix ipPrefix = IpPrefix.valueOf("10.0.0.1/23");
int bitWidth = ipPrefix.address().toOctets().length * Byte.SIZE;
IPCriterion criterion = (IPCriterion) Criteria.matchIPDst(ipPrefix);
PiTernaryFieldMatch ternaryMatch = (PiTernaryFieldMatch) translateCriterion(criterion, fieldId, TERNARY, bitWidth);
ImmutableByteSequence expectedMask = ImmutableByteSequence.prefixOnes(Integer.BYTES, 23);
ImmutableByteSequence expectedValue = ImmutableByteSequence.copyFrom(ipPrefix.address().toOctets());
assertThat(ternaryMatch.mask(), is(expectedMask));
assertThat(ternaryMatch.value(), is(expectedValue));
}
use of org.onlab.util.ImmutableByteSequence in project TFG by mattinelorza.
the class InterpreterImpl method mapInboundPacket.
/**
* Returns an ONS InboundPacket equivalent to the given pipeconf-specific
* packet-in operation.
*
* @param packetIn packet operation
* @param deviceId ID of the device that originated the packet-in
* @return inbound packet
* @throws PiInterpreterException if the packet operation cannot be mapped
* to an inbound packet
*/
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
// Find the ingress_port metadata.
// *** TODO EXERCISE 4: modify metadata names to match P4Info
// ---- START SOLUTION ----
final String inportMetadataName = "ADD HERE METADATA NAME FOR THE INGRESS PORT";
// ---- END SOLUTION ----
Optional<PiPacketMetadata> inportMetadata = packetIn.metadatas().stream().filter(meta -> meta.id().id().equals(inportMetadataName)).findFirst();
if (!inportMetadata.isPresent()) {
throw new PiInterpreterException(format("Missing metadata '%s' in packet-in received from '%s': %s", inportMetadataName, deviceId, packetIn));
}
// Build ONOS InboundPacket instance with the given ingress port.
// 1. Parse packet-in object into Ethernet packet instance.
final byte[] payloadBytes = packetIn.data().asArray();
final ByteBuffer rawData = ByteBuffer.wrap(payloadBytes);
final Ethernet ethPkt;
try {
ethPkt = Ethernet.deserializer().deserialize(payloadBytes, 0, packetIn.data().size());
} catch (DeserializationException dex) {
throw new PiInterpreterException(dex.getMessage());
}
// 2. Get ingress port
final ImmutableByteSequence portBytes = inportMetadata.get().value();
final short portNum = portBytes.asReadOnlyBuffer().getShort();
final ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(portNum));
return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
}
Aggregations