Search in sources :

Example 11 with OutboundPacket

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());
}
Also used : OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) Instruction(org.onosproject.net.flow.instructions.Instruction) ConnectPoint(org.onosproject.net.ConnectPoint) OutboundPacket(org.onosproject.net.packet.OutboundPacket) InterfaceService(org.onosproject.net.intf.InterfaceService) Ethernet(org.onlab.packet.Ethernet) PortNumber(org.onosproject.net.PortNumber) Interface(org.onosproject.net.intf.Interface) ARP(org.onlab.packet.ARP) Test(org.junit.Test)

Example 12 with OutboundPacket

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());
}
Also used : OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) OutputInstruction(org.onosproject.net.flow.instructions.Instructions.OutputInstruction) Instruction(org.onosproject.net.flow.instructions.Instruction) ConnectPoint(org.onosproject.net.ConnectPoint) OutboundPacket(org.onosproject.net.packet.OutboundPacket) InterfaceService(org.onosproject.net.intf.InterfaceService) Ethernet(org.onlab.packet.Ethernet) PortNumber(org.onosproject.net.PortNumber) Interface(org.onosproject.net.intf.Interface) ARP(org.onlab.packet.ARP) Test(org.junit.Test)

Example 13 with OutboundPacket

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());
    }
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) Device(org.onosproject.net.Device) ConnectPoint(org.onosproject.net.ConnectPoint) DefaultPort(org.onosproject.net.DefaultPort) OutboundPacket(org.onosproject.net.packet.OutboundPacket) Test(org.junit.Test)

Example 14 with OutboundPacket

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();
}
Also used : PiTableId(org.onosproject.net.pi.model.PiTableId) PiPacketMetadata(org.onosproject.net.pi.runtime.PiPacketMetadata) PACKET_OUT(org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT) ImmutableByteSequence(org.onlab.util.ImmutableByteSequence) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) ByteBuffer(java.nio.ByteBuffer) ConnectPoint(org.onosproject.net.ConnectPoint) Ethernet(org.onlab.packet.Ethernet) ImmutableList(com.google.common.collect.ImmutableList) DeserializationException(org.onlab.packet.DeserializationException) OutboundPacket(org.onosproject.net.packet.OutboundPacket) ImmutableByteSequence.copyFrom(org.onlab.util.ImmutableByteSequence.copyFrom) Port(org.onosproject.net.Port) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation) Criterion(org.onosproject.net.flow.criteria.Criterion) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) FLOOD(org.onosproject.net.PortNumber.FLOOD) Instructions(org.onosproject.net.flow.instructions.Instructions) PiPipelineInterpreter(org.onosproject.net.pi.model.PiPipelineInterpreter) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) DefaultInboundPacket(org.onosproject.net.packet.DefaultInboundPacket) Collection(java.util.Collection) Set(java.util.Set) PiMatchFieldId(org.onosproject.net.pi.model.PiMatchFieldId) FabricConstants(org.onosproject.pipelines.fabric.FabricConstants) String.format(java.lang.String.format) CONTROLLER(org.onosproject.net.PortNumber.CONTROLLER) PiAction(org.onosproject.net.pi.runtime.PiAction) TABLE(org.onosproject.net.PortNumber.TABLE) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DriverHandler(org.onosproject.net.driver.DriverHandler) InboundPacket(org.onosproject.net.packet.InboundPacket) Optional(java.util.Optional) DeviceId(org.onosproject.net.DeviceId) OUTPUT(org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT) DeviceId(org.onosproject.net.DeviceId) ImmutableList(com.google.common.collect.ImmutableList) Port(org.onosproject.net.Port) DeviceService(org.onosproject.net.device.DeviceService) Instructions(org.onosproject.net.flow.instructions.Instructions) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation)

Example 15 with OutboundPacket

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);
}
Also used : DeviceId(org.onosproject.net.DeviceId) Device(org.onosproject.net.Device) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) OutboundPacket(org.onosproject.net.packet.OutboundPacket) DefaultOutboundPacket(org.onosproject.net.packet.DefaultOutboundPacket) EthType(org.onlab.packet.EthType) P4RuntimePacketIn(org.onosproject.p4runtime.api.P4RuntimePacketIn) InboundPacket(org.onosproject.net.packet.InboundPacket) PiPacketOperation(org.onosproject.net.pi.runtime.PiPacketOperation) PiPipelineInterpreter(org.onosproject.net.pi.model.PiPipelineInterpreter) DefaultPacketContext(org.onosproject.net.packet.DefaultPacketContext) PacketContext(org.onosproject.net.packet.PacketContext)

Aggregations

OutboundPacket (org.onosproject.net.packet.OutboundPacket)46 DefaultOutboundPacket (org.onosproject.net.packet.DefaultOutboundPacket)35 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)29 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)25 Ethernet (org.onlab.packet.Ethernet)20 Test (org.junit.Test)17 ConnectPoint (org.onosproject.net.ConnectPoint)16 PortNumber (org.onosproject.net.PortNumber)15 DeviceId (org.onosproject.net.DeviceId)12 ByteBuffer (java.nio.ByteBuffer)11 Port (org.onosproject.net.Port)9 InboundPacket (org.onosproject.net.packet.InboundPacket)9 PiPacketOperation (org.onosproject.net.pi.runtime.PiPacketOperation)9 ImmutableList (com.google.common.collect.ImmutableList)8 Instruction (org.onosproject.net.flow.instructions.Instruction)8 DefaultInboundPacket (org.onosproject.net.packet.DefaultInboundPacket)8 PiPacketMetadata (org.onosproject.net.pi.runtime.PiPacketMetadata)8 Optional (java.util.Optional)7 Device (org.onosproject.net.Device)7 Interface (org.onosproject.net.intf.Interface)6