private ImmutableByteSequence doBitwiseOp(ImmutableByteSequence other, BitwiseOp op) { checkArgument(other != null && this.size() == other.size(), "Other sequence must be non null and with same size as this"); byte[] newBytes = new byte[this.size()]; byte[] thisBytes = this.asArray(); byte[] otherBytes = other.asArray(); for (int i = 0; i < this.size(); i++) { switch (op) { case AND: newBytes[i] = (byte) (thisBytes[i] & otherBytes[i]); break; case OR: newBytes[i] = (byte) (thisBytes[i] | otherBytes[i]); break; case XOR: newBytes[i] = (byte) (thisBytes[i] ^ otherBytes[i]); break; default: throw new IllegalArgumentException( "Unknown bitwise operator " + op.name()); } } return ImmutableByteSequence.copyFrom(newBytes); }
if (bitWidth == original.size() * 8) { if (newByteWidth > original.size()) { int numPadBytes = newByteWidth - original.size(); for (int i = 0; i < numPadBytes; i++) { newBuffer.put((byte) 0x00); newBuffer.put(original.asReadOnlyBuffer()); } else { if (bitWidth > original.msbIndex()) { int diff = original.size() - newByteWidth; ByteBuffer originalBuffer = original.asReadOnlyBuffer(); for (int i = diff; i < original.size(); i++) { newBuffer.put(originalBuffer.get(i)); return new ImmutableByteSequence(newBuffer);
/** * Creates a new byte array view of this sequence. * * @return a new byte array */ public byte[] asArray() { ByteBuffer bb = asReadOnlyBuffer(); byte[] bytes = new byte[size()]; bb.get(bytes); return bytes; }
@Override public ImmutableByteSequence read(Kryo kryo, Input input, Class<ImmutableByteSequence> type) { int length = input.readInt(); byte[] data = new byte[length]; int bytesRead = input.read(data); if (bytesRead != length) { throw new IllegalStateException("Byte sequence serializer read expected " + length + " but got " + bytesRead); } return ImmutableByteSequence.copyFrom(data); } }
Integer prefixLength = 0; byte[] byteArray = mask.asArray(); int byteArrayIndex = 0; while (byteArrayIndex < byteArray.length && byteArray[byteArrayIndex] == (byte) 0xff) {
@Override public void handlePacketIn(Bmv2Device device, int inputPort, ImmutableByteSequence packet) { Ethernet ethPkt = new Ethernet(); ethPkt.deserialize(packet.asArray(), 0, packet.size()); DeviceId deviceId = device.asDeviceId(); ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(inputPort)); ByteBuffer rawData = ByteBuffer.wrap(packet.asArray()); InboundPacket inPkt = new DefaultInboundPacket(receivedFrom, ethPkt, rawData); OutboundPacket outPkt = new DefaultOutboundPacket(deviceId, null, rawData); PacketContext pktCtx = new Bmv2PacketContext(System.currentTimeMillis(), inPkt, outPkt, false); providerService.processPacket(pktCtx); } }
case EXACT: return new PiExactFieldMatch(fieldMatch.fieldId(), ((PiExactFieldMatch) fieldMatch).value().fit(modelBitWidth)); 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: .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)); default:
@Override public ImmutableByteSequence exactMatch() throws CriterionTranslatorException { switch (initType) { case EXACT: break; case TERNARY: ImmutableByteSequence exactMask = ImmutableByteSequence.ofOnes(value.size()); if (!mask.equals(exactMask)) { throw new CriterionTranslator.CriterionTranslatorException( "trying to use masked field as an exact match, but mask is not exact"); } break; case LPM: if (prefixLength < bitWidth) { throw new CriterionTranslator.CriterionTranslatorException( "trying to use LPM field as an exact match, but prefix is not full"); } break; default: throw new IllegalArgumentException("Unrecognized init type " + initType.name()); } return value; }
@Override public Pair<ImmutableByteSequence, ImmutableByteSequence> ternaryMatch() { switch (initType) { case EXACT: mask = ImmutableByteSequence.prefixZeros(value.size(), prefixPadding()); break; case TERNARY: break; case LPM: mask = getMaskFromPrefixLength(prefixLength, value.size()); break; default: throw new IllegalArgumentException("Unrecognized init type " + initType.name()); } return Pair.of(value, mask); }
/** * Computes the prefix padding size (in bits) based on value and bit width. * * @return prefix padding in bits */ private int prefixPadding() { return value.size() * Byte.SIZE - this.bitWidth; }
/** * Creates a new byte sequence of the given size where all bits are 0. * * @param size number of bytes * @return a new immutable byte sequence */ public static ImmutableByteSequence ofZeros(int size) { // array is initialized to all 0's by default return new ImmutableByteSequence(ByteBuffer.wrap(new byte[size])); }
@Override public void init(Criterion criterion, int bitWidth) throws ByteSequenceTrimException { IPProtocolCriterion c = (IPProtocolCriterion) criterion; initAsExactMatch(copyFrom(c.protocol()), bitWidth); } }
@Override public void write(Kryo kryo, Output output, ImmutableByteSequence object) { byte[] data = object.asArray(); output.writeInt(data.length); output.write(data); }
/** * Creates a new byte sequence of 8 bytes containing the given long value. * * @param original a long value * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(long original) { return new ImmutableByteSequence( ByteBuffer.allocate(Long.BYTES).putLong(original)); }
@Override public void init(Criterion criterion, int bitWidth) throws ByteSequenceTrimException { TunnelIdCriterion c = (TunnelIdCriterion) criterion; initAsExactMatch(copyFrom(c.tunnelId()), bitWidth); } }
private ObjectNode parsePiMatchTernary(PiTernaryFieldMatch ternaryFieldMatch) { ObjectNode matchTernaryNode = context.mapper().createObjectNode(); matchTernaryNode.put(CriterionCodec.PI_MATCH_FIELD_ID, ternaryFieldMatch.fieldId().id()); matchTernaryNode.put(CriterionCodec.PI_MATCH_TYPE, PiMatchType.TERNARY.name().toLowerCase()); matchTernaryNode.put(CriterionCodec.PI_MATCH_VALUE, HexString.toHexString(ternaryFieldMatch.value().asArray(), null)); matchTernaryNode.put(CriterionCodec.PI_MATCH_MASK, HexString.toHexString(ternaryFieldMatch.mask().asArray(), null)); return matchTernaryNode; }
/** * Creates a new byte sequence of 2 bytes containing the given short value. * * @param original a short value * @return a new immutable byte sequence */ public static ImmutableByteSequence copyFrom(short original) { return new ImmutableByteSequence( ByteBuffer.allocate(Short.BYTES).putShort(original)); }