use of org.onlab.packet.Ethernet 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.onlab.packet.Ethernet 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());
}
use of org.onlab.packet.Ethernet 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.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class OpenFlowPacketProviderTest method emit.
@Test
public void emit() {
MacAddress mac1 = MacAddress.of("00:00:00:11:00:01");
MacAddress mac2 = MacAddress.of("00:00:00:22:00:02");
ARP arp = new ARP();
arp.setSenderProtocolAddress(ANY).setSenderHardwareAddress(mac1.getBytes()).setTargetHardwareAddress(mac2.getBytes()).setTargetProtocolAddress(ANY).setHardwareType((short) 0).setProtocolType((short) 0).setHardwareAddressLength((byte) 6).setProtocolAddressLength((byte) 4).setOpCode((byte) 0);
Ethernet eth = new Ethernet();
eth.setVlanID(VLANID).setEtherType(Ethernet.TYPE_ARP).setSourceMACAddress("00:00:00:11:00:01").setDestinationMACAddress("00:00:00:22:00:02").setPayload(arp);
// the should-be working setup.
OutboundPacket passPkt = outPacket(DID, TR, eth);
sw.setRole(RoleState.MASTER);
provider.emit(passPkt);
assertEquals("invalid switch", sw, controller.current);
assertEquals("message not sent", PLIST.size(), sw.sent.size());
sw.sent.clear();
// Send with different IN_PORT
OutboundPacket inPortPkt = outPacket(DID, TR_ALL, eth, IN_PORT);
sw.setRole(RoleState.MASTER);
provider.emit(inPortPkt);
assertEquals("invalid switch", sw, controller.current);
assertEquals("message not sent", PLIST_ALL.size(), sw.sent.size());
OFMessage ofMessage = sw.sent.get(0);
assertEquals("Wrong OF message type", OFType.PACKET_OUT, ofMessage.getType());
OFPacketOut packetOut = (OFPacketOut) ofMessage;
assertEquals("Wrong in port", OFPort.of(IN_PORT_PN), packetOut.getInPort());
assertEquals("Unexpected number of actions", 1, packetOut.getActions().size());
OFAction ofAction = packetOut.getActions().get(0);
assertEquals("Packet out action should be type output", OFActionType.OUTPUT, ofAction.getType());
OFActionOutput ofActionOutput = (OFActionOutput) ofAction;
assertEquals("Output should be ALL", OFPort.ALL, ofActionOutput.getPort());
sw.sent.clear();
// wrong Role
// sw.setRole(RoleState.SLAVE);
// provider.emit(passPkt);
// assertEquals("invalid switch", sw, controller.current);
// assertEquals("message sent incorrectly", 0, sw.sent.size());
// sw.setRole(RoleState.MASTER);
// missing switch
OutboundPacket swFailPkt = outPacket(DID_MISSING, TR, eth);
provider.emit(swFailPkt);
assertNull("invalid switch", controller.current);
assertEquals("message sent incorrectly", 0, sw.sent.size());
// to missing port
// OutboundPacket portFailPkt = outPacket(DID, TR_MISSING, eth);
// provider.emit(portFailPkt);
// assertEquals("extra message sent", 1, sw.sent.size());
}
use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class HostMonitorTest method testMonitorIpv6HostDoesNotExistWithVlan.
@Test
public void testMonitorIpv6HostDoesNotExistWithVlan() throws Exception {
HostManager hostManager = createMock(HostManager.class);
DeviceId devId = DeviceId.deviceId("fake");
short vlan = 5;
Device device = createMock(Device.class);
expect(device.id()).andReturn(devId).anyTimes();
replay(device);
PortNumber portNum = PortNumber.portNumber(1L);
Port port = createMock(Port.class);
expect(port.number()).andReturn(portNum).anyTimes();
expect(port.isEnabled()).andReturn(true).anyTimes();
replay(port);
TestDeviceService deviceService = new TestDeviceService();
deviceService.addDevice(device, Collections.singleton(port));
ConnectPoint cp = new ConnectPoint(devId, portNum);
expect(hostManager.getHostsByIp(TARGET_IPV6_ADDR)).andReturn(Collections.emptySet()).anyTimes();
replay(hostManager);
InterfaceService interfaceService = createMock(InterfaceService.class);
expect(interfaceService.getMatchingInterfaces(TARGET_IPV6_ADDR)).andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp, Collections.singletonList(IA2), sourceMac2, VlanId.vlanId(vlan)))).anyTimes();
replay(interfaceService);
TestPacketService packetService = new TestPacketService();
// Run the test
hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
hostMonitor.addMonitoringFor(TARGET_IPV6_ADDR);
hostMonitor.run();
// Check that a packet was sent to our PacketService and that it has
// the properties we expect
assertEquals(2, packetService.packets.size());
OutboundPacket packet = packetService.packets.get(0);
// Check the output port is correct
assertEquals(1, packet.treatment().immediate().size());
Instruction instruction = packet.treatment().immediate().get(0);
assertTrue(instruction instanceof OutputInstruction);
OutputInstruction oi = (OutputInstruction) instruction;
assertEquals(portNum, oi.port());
// Check the output packet is correct (well the important bits anyway)
final byte[] pktData = new byte[packet.data().remaining()];
packet.data().get(pktData);
Ethernet eth = Ethernet.deserializer().deserialize(pktData, 0, pktData.length);
assertEquals(vlan, eth.getVlanID());
IPv6 ipv6 = (IPv6) eth.getPayload();
assertArrayEquals(SOURCE_IPV6_ADDR.toOctets(), ipv6.getSourceAddress());
NeighborSolicitation ns = (NeighborSolicitation) ipv6.getPayload().getPayload();
assertArrayEquals(sourceMac2.toBytes(), ns.getOptions().get(0).data());
assertArrayEquals(TARGET_IPV6_ADDR.toOctets(), ns.getTargetAddress());
}
Aggregations