use of org.onosproject.net.pi.runtime.PiPacketOperation in project onos by opennetworkinglab.
the class PipelineInterpreterImpl method mapInboundPacket.
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
// We assume that the packet is ethernet, which is fine since mytunnel.p4
// can deparse only ethernet packets.
Ethernet ethPkt;
try {
ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, packetIn.data().size());
} catch (DeserializationException dex) {
throw new PiInterpreterException(dex.getMessage());
}
// Returns the ingress port packet metadata.
Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas().stream().filter(metadata -> metadata.id().toString().equals(INGRESS_PORT)).findFirst();
if (packetMetadata.isPresent()) {
short s = packetMetadata.get().value().asReadOnlyBuffer().getShort();
ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
return new DefaultInboundPacket(receivedFrom, ethPkt, packetIn.data().asReadOnlyBuffer());
} else {
throw new PiInterpreterException(format("Missing metadata '%s' in packet-in received from '%s': %s", INGRESS_PORT, deviceId, packetIn));
}
}
use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.
the class FabricInterpreter method mapInboundPacket.
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
// Assuming that the packet is ethernet, which is fine since fabric.p4
// can deparse only ethernet packets.
Ethernet ethPkt;
try {
ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, packetIn.data().size());
} catch (DeserializationException dex) {
throw new PiInterpreterException(dex.getMessage());
}
// Returns the ingress port packet metadata.
Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas().stream().filter(m -> m.id().equals(P4InfoConstants.INGRESS_PORT)).findFirst();
if (packetMetadata.isPresent()) {
try {
ImmutableByteSequence portByteSequence = packetMetadata.get().value().fit(P4InfoConstants.INGRESS_PORT_BITWIDTH);
UnsignedInteger ui = UnsignedInteger.fromIntBits(portByteSequence.asReadOnlyBuffer().getInt());
ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(ui.longValue()));
if (!receivedFrom.port().hasName()) {
receivedFrom = translateSwitchPort(receivedFrom);
}
ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
} catch (ImmutableByteSequence.ByteSequenceTrimException e) {
throw new PiInterpreterException(format("Malformed metadata '%s' in packet-in received from '%s': %s", P4InfoConstants.INGRESS_PORT, deviceId, packetIn));
}
} else {
throw new PiInterpreterException(format("Missing metadata '%s' in packet-in received from '%s': %s", P4InfoConstants.INGRESS_PORT, deviceId, packetIn));
}
}
use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.
the class FabricInterpreterTest method testMapInboundPacket.
@Test
public void testMapInboundPacket() throws ImmutableByteSequence.ByteSequenceTrimException, PiPipelineInterpreter.PiInterpreterException {
PortNumber inputPort = PortNumber.portNumber(1);
PiPacketMetadata pktInMetadata = PiPacketMetadata.builder().withId(P4InfoConstants.INGRESS_PORT).withValue(ImmutableByteSequence.copyFrom(inputPort.toLong()).fit(P4InfoConstants.INGRESS_PORT_BITWIDTH)).build();
Ethernet packet = new Ethernet();
packet.setDestinationMACAddress(SRC_MAC);
packet.setSourceMACAddress(DST_MAC);
packet.setEtherType((short) 0xBA00);
packet.setPayload(new Data());
PiPacketOperation pktInOp = PiPacketOperation.builder().withMetadata(pktInMetadata).withData(ImmutableByteSequence.copyFrom(packet.serialize())).withType(PiPacketOperationType.PACKET_IN).build();
InboundPacket result = interpreter.mapInboundPacket(pktInOp, DEVICE_ID);
ConnectPoint receiveFrom = new ConnectPoint(DEVICE_ID, inputPort);
InboundPacket expectedInboundPacket = new DefaultInboundPacket(receiveFrom, packet, ByteBuffer.wrap(packet.serialize()));
assertEquals(result.receivedFrom(), expectedInboundPacket.receivedFrom());
assertEquals(result.parsed(), expectedInboundPacket.parsed());
assertEquals(result.cookie(), expectedInboundPacket.cookie());
assertEquals(result.unparsed(), expectedInboundPacket.unparsed());
}
use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.
the class FabricInterpreterTest method testMapOutboundPacketWithoutForwarding.
@Test
public void testMapOutboundPacketWithoutForwarding() throws PiPipelineInterpreter.PiInterpreterException, ImmutableByteSequence.ByteSequenceTrimException {
PortNumber outputPort = PortNumber.portNumber(1);
TrafficTreatment outputTreatment = DefaultTrafficTreatment.builder().setOutput(outputPort).build();
ByteBuffer data = ByteBuffer.allocate(64);
OutboundPacket outPkt = new DefaultOutboundPacket(DEVICE_ID, outputTreatment, data);
Collection<PiPacketOperation> result = interpreter.mapOutboundPacket(outPkt);
assertEquals(result.size(), 1);
ImmutableList.Builder<PiPacketMetadata> builder = ImmutableList.builder();
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD0).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD0_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.EGRESS_PORT).withValue(ImmutableByteSequence.copyFrom(outputPort.toLong()).fit(P4InfoConstants.EGRESS_PORT_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD1).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD1_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.QUEUE_ID).withValue(ImmutableByteSequence.copyFrom(SYSTEM_QUEUE_ID).fit(P4InfoConstants.QUEUE_ID_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD2).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD2_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.CPU_LOOPBACK_MODE).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.CPU_LOOPBACK_MODE_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.DO_FORWARDING).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.DO_FORWARDING_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD3).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD3_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.PAD4).withValue(ImmutableByteSequence.copyFrom(0).fit(P4InfoConstants.PAD4_BITWIDTH)).build());
builder.add(PiPacketMetadata.builder().withId(P4InfoConstants.ETHER_TYPE).withValue(ImmutableByteSequence.copyFrom(0xBF01).fit(P4InfoConstants.ETHER_TYPE_BITWIDTH)).build());
PiPacketOperation expectedPktOp = PiPacketOperation.builder().withType(PiPacketOperationType.PACKET_OUT).withData(ImmutableByteSequence.copyFrom(data)).withMetadatas(builder.build()).build();
assertEquals(expectedPktOp, result.iterator().next());
}
use of org.onosproject.net.pi.runtime.PiPacketOperation in project fabric-tna by stratum.
the class FabricInterpreterTest method testMapInboundPacketWithPortTranslation.
@Test
public void testMapInboundPacketWithPortTranslation() throws ImmutableByteSequence.ByteSequenceTrimException, PiPipelineInterpreter.PiInterpreterException {
PortNumber inputPort = PortNumber.portNumber(1, "ONE");
ConnectPoint receiveFrom = new ConnectPoint(DEVICE_ID, inputPort);
Port port = createNiceMock(Port.class);
expect(port.number()).andReturn(inputPort).anyTimes();
replay(port);
expect(deviceService.getPort(receiveFrom)).andReturn(port).anyTimes();
replay(deviceService);
PiPacketMetadata pktInMetadata = PiPacketMetadata.builder().withId(P4InfoConstants.INGRESS_PORT).withValue(ImmutableByteSequence.copyFrom(inputPort.toLong()).fit(P4InfoConstants.INGRESS_PORT_BITWIDTH)).build();
Ethernet packet = new Ethernet();
packet.setDestinationMACAddress(SRC_MAC);
packet.setSourceMACAddress(DST_MAC);
packet.setEtherType((short) 0xBA00);
packet.setPayload(new Data());
PiPacketOperation pktInOp = PiPacketOperation.builder().withMetadata(pktInMetadata).withData(ImmutableByteSequence.copyFrom(packet.serialize())).withType(PiPacketOperationType.PACKET_IN).build();
InboundPacket result = interpreter.mapInboundPacket(pktInOp, DEVICE_ID);
InboundPacket expectedInboundPacket = new DefaultInboundPacket(receiveFrom, packet, ByteBuffer.wrap(packet.serialize()));
assertEquals(result.receivedFrom(), expectedInboundPacket.receivedFrom());
assertEquals(result.receivedFrom().port().name(), expectedInboundPacket.receivedFrom().port().name());
assertEquals(result.parsed(), expectedInboundPacket.parsed());
assertEquals(result.cookie(), expectedInboundPacket.cookie());
assertEquals(result.unparsed(), expectedInboundPacket.unparsed());
}
Aggregations