use of org.onosproject.net.packet.OutboundPacket in project onos by opennetworkinglab.
the class HostMonitorTest method testMonitorIpv4HostDoesNotExist.
@Test
public void testMonitorIpv4HostDoesNotExist() throws Exception {
HostManager hostManager = createMock(HostManager.class);
DeviceId devId = DeviceId.deviceId("fake");
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_IPV4_ADDR)).andReturn(Collections.emptySet()).anyTimes();
replay(hostManager);
InterfaceService interfaceService = createMock(InterfaceService.class);
expect(interfaceService.getMatchingInterfaces(TARGET_IPV4_ADDR)).andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp, Collections.singletonList(IA1), sourceMac, VlanId.NONE))).anyTimes();
replay(interfaceService);
TestPacketService packetService = new TestPacketService();
// Run the test
hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
hostMonitor.addMonitoringFor(TARGET_IPV4_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(Ethernet.VLAN_UNTAGGED, eth.getVlanID());
ARP arp = (ARP) eth.getPayload();
assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(), arp.getSenderProtocolAddress());
assertArrayEquals(sourceMac.toBytes(), arp.getSenderHardwareAddress());
assertArrayEquals(TARGET_IPV4_ADDR.toOctets(), arp.getTargetProtocolAddress());
}
use of org.onosproject.net.packet.OutboundPacket in project onos by opennetworkinglab.
the class HostMonitorTest method testMonitorIpv4HostDoesNotExistWithVlan.
@Test
public void testMonitorIpv4HostDoesNotExistWithVlan() 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_IPV4_ADDR)).andReturn(Collections.emptySet()).anyTimes();
replay(hostManager);
InterfaceService interfaceService = createMock(InterfaceService.class);
expect(interfaceService.getMatchingInterfaces(TARGET_IPV4_ADDR)).andReturn(Collections.singleton(new Interface(Interface.NO_INTERFACE_NAME, cp, Collections.singletonList(IA1), sourceMac, VlanId.vlanId(vlan)))).anyTimes();
replay(interfaceService);
TestPacketService packetService = new TestPacketService();
// Run the test
hostMonitor = new HostMonitor(packetService, hostManager, interfaceService, edgePortService, deviceService);
hostMonitor.addMonitoringFor(TARGET_IPV4_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());
ARP arp = (ARP) eth.getPayload();
assertArrayEquals(SOURCE_IPV4_ADDR.toOctets(), arp.getSenderProtocolAddress());
assertArrayEquals(sourceMac.toBytes(), arp.getSenderHardwareAddress());
assertArrayEquals(TARGET_IPV4_ADDR.toOctets(), arp.getTargetProtocolAddress());
}
use of org.onosproject.net.packet.OutboundPacket in project onos by opennetworkinglab.
the class EdgeManagerTest method testEmit.
@Test
public void testEmit() {
byte[] arr = new byte[10];
Device referenceDevice;
DeviceEvent event;
int numDevices = 10;
int numInfraPorts = 5;
totalPorts = 10;
defaultPopulator(numDevices, numInfraPorts);
for (byte byteIndex = 0; byteIndex < arr.length; byteIndex++) {
arr[byteIndex] = byteIndex;
}
for (int i = 0; i < numDevices; i++) {
referenceDevice = NetTestTools.device(Integer.toString(i));
testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice, new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)));
}
mgr.emitPacket(ByteBuffer.wrap(arr), Optional.empty());
assertEquals("There were an unexpected number of emitted packets", (totalPorts - numInfraPorts) * numDevices, packets.size());
Iterator<OutboundPacket> packetIter = packets.iterator();
OutboundPacket packet;
while (packetIter.hasNext()) {
packet = packetIter.next();
assertEquals("The packet had an incorrect payload.", arr, packet.data().array());
}
// Start testing emission to a specific device
packets.clear();
mgr.emitPacket(NetTestTools.did(Integer.toString(1)), ByteBuffer.wrap(arr), Optional.empty());
assertEquals("Unexpected number of outbound packets were emitted.", totalPorts - numInfraPorts, packets.size());
packetIter = packets.iterator();
while (packetIter.hasNext()) {
packet = packetIter.next();
assertEquals("The packet had an incorrect payload", arr, packet.data().array());
}
}
use of org.onosproject.net.packet.OutboundPacket in project onos by opennetworkinglab.
the class FabricInterpreter method mapOutboundPacket.
@Override
public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) throws PiInterpreterException {
DeviceId deviceId = packet.sendThrough();
TrafficTreatment treatment = packet.treatment();
// fabric.p4 supports only OUTPUT instructions.
List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions().stream().filter(i -> i.type().equals(OUTPUT)).map(i -> (Instructions.OutputInstruction) i).collect(toList());
if (treatment.allInstructions().size() != outInstructions.size()) {
// There are other instructions that are not of type OUTPUT.
throw new PiInterpreterException("Treatment not supported: " + treatment);
}
ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
for (Instructions.OutputInstruction outInst : outInstructions) {
if (outInst.port().equals(TABLE)) {
// Logical port. Forward using the switch tables like a regular packet.
builder.add(createPiPacketOperation(packet.data(), -1, true));
} else if (outInst.port().equals(FLOOD)) {
// Logical port. Create a packet operation for each switch port.
final DeviceService deviceService = handler().get(DeviceService.class);
for (Port port : deviceService.getPorts(packet.sendThrough())) {
builder.add(createPiPacketOperation(packet.data(), port.number().toLong(), false));
}
} else if (outInst.port().isLogical()) {
throw new PiInterpreterException(format("Output on logical port '%s' not supported", outInst.port()));
} else {
// Send as-is to given port bypassing all switch tables.
builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong(), false));
}
}
return builder.build();
}
use of org.onosproject.net.packet.OutboundPacket in project onos by opennetworkinglab.
the class P4RuntimePacketProvider method handleP4RuntimeEvent.
private void handleP4RuntimeEvent(P4RuntimeEvent event) {
// FIXME we need the device ID into p4RuntimeEvnetSubject to check for mastsership
if (!(event.subject() instanceof P4RuntimePacketIn) || event.type() != P4RuntimeEvent.Type.PACKET_IN) {
log.debug("Unrecognized event type {}, discarding", event.type());
// Not a packet-in event, ignore it.
return;
}
P4RuntimePacketIn eventSubject = (P4RuntimePacketIn) event.subject();
DeviceId deviceId = eventSubject.deviceId();
Device device = deviceService.getDevice(eventSubject.deviceId());
if (device == null) {
log.warn("Unable to process packet-in from {}, device is null in the core", deviceId);
return;
}
if (!device.is(PiPipelineInterpreter.class)) {
log.warn("Unable to process packet-in from {}, device has no PiPipelineInterpreter behaviour", deviceId);
return;
}
PiPacketOperation operation = eventSubject.packetOperation();
InboundPacket inPkt;
try {
inPkt = device.as(PiPipelineInterpreter.class).mapInboundPacket(operation, deviceId);
} catch (PiPipelineInterpreter.PiInterpreterException e) {
log.warn("Unable to interpret inbound packet from {}: {}", deviceId, e.getMessage());
return;
}
if (log.isTraceEnabled()) {
final EthType.EtherType etherType = getEtherType(inPkt.unparsed());
log.trace("Received PACKET-IN <<< device={} ingress_port={} eth_type={}", inPkt.receivedFrom().deviceId(), inPkt.receivedFrom().port(), etherType.ethType().toString());
}
if (inPkt == null) {
log.debug("Received null inbound packet. Ignoring.");
return;
}
OutboundPacket outPkt = new DefaultOutboundPacket(eventSubject.deviceId(), null, operation.data().asReadOnlyBuffer());
PacketContext pktCtx = new P4RuntimePacketContext(System.currentTimeMillis(), inPkt, outPkt, false);
// Pushing the packet context up for processing.
providerService.processPacket(pktCtx);
}
Aggregations