Search in sources :

Example 1 with OFPacketOut

use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.

the class PathVerificationService method generateVerificationPacket.

public OFPacketOut generateVerificationPacket(IOFSwitch srcSw, OFPort port, IOFSwitch dstSw, boolean sign) {
    try {
        OFPortDesc ofPortDesc = srcSw.getPort(port);
        byte[] chassisId = new byte[] { 4, 0, 0, 0, 0, 0, 0 };
        byte[] portId = new byte[] { 2, 0, 0 };
        byte[] ttlValue = new byte[] { 0, 0x78 };
        byte[] dpidTLVValue = new byte[] { 0x0, 0x26, (byte) 0xe1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        LLDPTLV dpidTLV = new LLDPTLV().setType((byte) 127).setLength((short) dpidTLVValue.length).setValue(dpidTLVValue);
        byte[] dpidArray = new byte[8];
        ByteBuffer dpidBB = ByteBuffer.wrap(dpidArray);
        ByteBuffer portBB = ByteBuffer.wrap(portId, 1, 2);
        DatapathId dpid = srcSw.getId();
        dpidBB.putLong(dpid.getLong());
        System.arraycopy(dpidArray, 2, chassisId, 1, 6);
        // Set the optionalTLV to the full SwitchID
        System.arraycopy(dpidArray, 0, dpidTLVValue, 4, 8);
        byte[] zeroMac = { 0, 0, 0, 0, 0, 0 };
        byte[] srcMac = ofPortDesc.getHwAddr().getBytes();
        if (Arrays.equals(srcMac, zeroMac)) {
            int portVal = ofPortDesc.getPortNo().getPortNumber();
            // this is a common scenario
            logger.debug("Port {}/{} has zero hardware address: overwrite with lower 6 bytes of dpid", dpid.toString(), portVal);
            System.arraycopy(dpidArray, 2, srcMac, 0, 6);
        }
        portBB.putShort(port.getShortPortNumber());
        VerificationPacket vp = new VerificationPacket();
        vp.setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) chassisId.length).setValue(chassisId));
        vp.setPortId(new LLDPTLV().setType((byte) 2).setLength((short) portId.length).setValue(portId));
        vp.setTtl(new LLDPTLV().setType((byte) 3).setLength((short) ttlValue.length).setValue(ttlValue));
        vp.getOptionalTLVList().add(dpidTLV);
        // Add the controller identifier to the TLV value.
        // vp.getOptionalTLVList().add(controllerTLV);
        // Add T0 based on format from Floodlight LLDP
        long time = System.currentTimeMillis();
        long swLatency = srcSw.getLatency().getValue();
        byte[] timestampTLVValue = ByteBuffer.allocate(Long.SIZE / 8 + 4).put((byte) 0x00).put((byte) 0x26).put((byte) 0xe1).put(// 0x01 is what we'll use to differentiate DPID (0x00) from time (0x01)
        (byte) 0x01).putLong(time + swLatency).array();
        LLDPTLV timestampTLV = new LLDPTLV().setType((byte) 127).setLength((short) timestampTLVValue.length).setValue(timestampTLVValue);
        vp.getOptionalTLVList().add(timestampTLV);
        // Type
        byte[] typeTLVValue = ByteBuffer.allocate(Integer.SIZE / 8 + 4).put((byte) 0x00).put((byte) 0x26).put((byte) 0xe1).put((byte) 0x02).putInt(PathType.ISL.ordinal()).array();
        LLDPTLV typeTLV = new LLDPTLV().setType((byte) 127).setLength((short) typeTLVValue.length).setValue(typeTLVValue);
        vp.getOptionalTLVList().add(typeTLV);
        if (sign) {
            String token = JWT.create().withClaim("dpid", dpid.getLong()).withClaim("ts", time + swLatency).sign(algorithm);
            byte[] tokenBytes = token.getBytes(Charset.forName("UTF-8"));
            byte[] tokenTLVValue = ByteBuffer.allocate(4 + tokenBytes.length).put((byte) 0x00).put((byte) 0x26).put((byte) 0xe1).put((byte) 0x03).put(tokenBytes).array();
            LLDPTLV tokenTLV = new LLDPTLV().setType((byte) 127).setLength((short) tokenTLVValue.length).setValue(tokenTLVValue);
            vp.getOptionalTLVList().add(tokenTLV);
        }
        MacAddress dstMac = MacAddress.of(VERIFICATION_BCAST_PACKET_DST);
        if (dstSw != null) {
            OFPortDesc sw2OfPortDesc = dstSw.getPort(port);
            dstMac = sw2OfPortDesc.getHwAddr();
        }
        Ethernet l2 = new Ethernet().setSourceMACAddress(MacAddress.of(srcMac)).setDestinationMACAddress(dstMac).setEtherType(EthType.IPv4);
        IPv4Address dstIp = IPv4Address.of(VERIFICATION_PACKET_IP_DST);
        if (dstSw != null) {
            dstIp = IPv4Address.of(((InetSocketAddress) dstSw.getInetAddress()).getAddress().getAddress());
        }
        IPv4 l3 = new IPv4().setSourceAddress(IPv4Address.of(((InetSocketAddress) srcSw.getInetAddress()).getAddress().getAddress())).setDestinationAddress(dstIp).setTtl((byte) 64).setProtocol(IpProtocol.UDP);
        UDP l4 = new UDP();
        l4.setSourcePort(TransportPort.of(VERIFICATION_PACKET_UDP_PORT));
        l4.setDestinationPort(TransportPort.of(VERIFICATION_PACKET_UDP_PORT));
        l2.setPayload(l3);
        l3.setPayload(l4);
        l4.setPayload(vp);
        byte[] data = l2.serialize();
        OFPacketOut.Builder pob = srcSw.getOFFactory().buildPacketOut().setBufferId(OFBufferId.NO_BUFFER).setActions(getDiscoveryActions(srcSw, port)).setData(data);
        OFMessageUtils.setInPort(pob, OFPort.CONTROLLER);
        return pob.build();
    } catch (Exception exception) {
        logger.error("error generating verification packet: {}", exception);
    }
    return null;
}
Also used : UDP(net.floodlightcontroller.packet.UDP) InetSocketAddress(java.net.InetSocketAddress) IPv4(net.floodlightcontroller.packet.IPv4) DatapathId(org.projectfloodlight.openflow.types.DatapathId) MacAddress(org.projectfloodlight.openflow.types.MacAddress) ByteBuffer(java.nio.ByteBuffer) IPv4Address(org.projectfloodlight.openflow.types.IPv4Address) OFPacketOut(org.projectfloodlight.openflow.protocol.OFPacketOut) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FloodlightModuleException(net.floodlightcontroller.core.module.FloodlightModuleException) OFPortDesc(org.projectfloodlight.openflow.protocol.OFPortDesc) OFPortDescPropEthernet(org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet) Ethernet(net.floodlightcontroller.packet.Ethernet) LLDPTLV(net.floodlightcontroller.packet.LLDPTLV)

Example 2 with OFPacketOut

use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.

the class PathTest method testCreate.

@Test
public void testCreate() {
    OFPacketOut packetOut = pvs.generateVerificationPacket(sw1, sw1Port1.getPortNo());
    expect(packetIn.getData()).andReturn(packetOut.getData());
    expect(packetIn.getInPort()).andReturn(sw2Port1.getPortNo());
    replay(packetIn);
    byte[] d = packetIn.getData();
    System.out.println(packetOut.toString());
    System.out.println(Hex.encodeHexString(packetOut.getData()));
    System.out.println(Hex.encodeHexString(d));
// Path path = new Path(sw2, sw2Port1.getPortNo(), verPacket);
// assertTrue(path.getSource().equals(sw1Port1));
// assertTrue(path.getDestination().equals(sw2Port1));
// assertEquals(path.getLatency(), 100);
}
Also used : OFPacketOut(org.projectfloodlight.openflow.protocol.OFPacketOut) Test(org.junit.Test)

Example 3 with OFPacketOut

use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.

the class PathVerificationCommonTests method createDiscoveryPacket.

private DiscoveryPacket createDiscoveryPacket() {
    OFPacketOut packet = pvs.generateDiscoveryPacket(sw, OFPort.of(1), true, null);
    Ethernet ethernet = (Ethernet) new Ethernet().deserialize(packet.getData(), 0, packet.getData().length);
    IPv4 ipv4 = (IPv4) ethernet.getPayload();
    UDP udp = (UDP) ipv4.getPayload();
    return new DiscoveryPacket((Data) udp.getPayload(), true);
}
Also used : UDP(net.floodlightcontroller.packet.UDP) Ethernet(net.floodlightcontroller.packet.Ethernet) IPv4(net.floodlightcontroller.packet.IPv4) OFPacketOut(org.projectfloodlight.openflow.protocol.OFPacketOut)

Example 4 with OFPacketOut

use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.

the class EnableBfdResource method enableBfd.

/**
 * Setting up BFD session.
 *
 * @param json the json from request.
 * @return json response.
 * @throws JsonProcessingException if response can't be wrote to string.
 */
@Post("json")
@Put("json")
public String enableBfd(String json) {
    ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes().get(ISwitchManager.class.getCanonicalName());
    NoviBfdSession request;
    try {
        request = MAPPER.readValue(json, NoviBfdSession.class);
        if (request.getIntervalMs() < CONSTRAINT_INTERVAL_MIN) {
            throw new IllegalArgumentException(String.format("Invalid bfd session interval value: %d < %d", request.getIntervalMs(), CONSTRAINT_INTERVAL_MIN));
        }
        DatapathId datapathIdtarget = DatapathId.of(request.getTarget().getDatapath().toLong());
        IOFSwitch iofSwitch = switchManager.lookupSwitch(datapathIdtarget);
        OFPacketOut outPacket = makeSessionConfigMessage(request, iofSwitch, switchManager);
        if (!iofSwitch.write(outPacket)) {
            throw new IllegalStateException("Failed to set up BFD session");
        }
    } catch (IOException e) {
        logger.error("Message received is not valid BFD Request: {}", json);
        MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, now(), ErrorType.DATA_INVALID.toString(), "Message received is not valid BFD Request", e.getMessage());
        return generateJson(responseMessage);
    } catch (SwitchNotFoundException e) {
        MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, now(), ErrorType.DATA_INVALID.toString(), "Switch not found", e.getMessage());
        return generateJson(responseMessage);
    }
    return generateJson("ok");
}
Also used : IOFSwitch(net.floodlightcontroller.core.IOFSwitch) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) MessageError(org.openkilda.messaging.error.MessageError) DatapathId(org.projectfloodlight.openflow.types.DatapathId) IOException(java.io.IOException) SwitchNotFoundException(org.openkilda.floodlight.error.SwitchNotFoundException) NoviBfdSession(org.openkilda.messaging.model.NoviBfdSession) OFPacketOut(org.projectfloodlight.openflow.protocol.OFPacketOut) Post(org.restlet.resource.Post) Put(org.restlet.resource.Put)

Example 5 with OFPacketOut

use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.

the class PathVerificationService method sendDiscoveryMessage.

@Override
public boolean sendDiscoveryMessage(DatapathId srcSwId, OFPort port, DatapathId dstSwId) {
    boolean result = false;
    try {
        IOFSwitch srcSwitch = switchService.getSwitch(srcSwId);
        if (srcSwitch != null && srcSwitch.getPort(port) != null) {
            IOFSwitch dstSwitch = (dstSwId == null) ? null : switchService.getSwitch(dstSwId);
            OFPacketOut ofPacketOut = generateVerificationPacket(srcSwitch, port, dstSwitch, true);
            if (ofPacketOut != null) {
                logger.debug("==> Sending verification packet out {}/{}: {}", srcSwitch.getId().toString(), port.getPortNumber(), Hex.encodeHexString(ofPacketOut.getData()));
                result = srcSwitch.write(ofPacketOut);
            } else {
                logger.error("<== Received null from generateVerificationPacket, inputs where: " + "srcSwitch: {}, port: {}, dstSwitch: {}", srcSwitch, port, dstSwitch);
            }
        }
    } catch (Exception exception) {
        logger.error("Error trying to sendDiscoveryMessage: {}", exception);
    }
    return result;
}
Also used : IOFSwitch(net.floodlightcontroller.core.IOFSwitch) OFPacketOut(org.projectfloodlight.openflow.protocol.OFPacketOut) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FloodlightModuleException(net.floodlightcontroller.core.module.FloodlightModuleException)

Aggregations

OFPacketOut (org.projectfloodlight.openflow.protocol.OFPacketOut)17 Test (org.junit.Test)10 IOFSwitch (net.floodlightcontroller.core.IOFSwitch)8 Ethernet (net.floodlightcontroller.packet.Ethernet)6 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Optional (java.util.Optional)4 FloodlightModuleException (net.floodlightcontroller.core.module.FloodlightModuleException)4 IPv4 (net.floodlightcontroller.packet.IPv4)4 UDP (net.floodlightcontroller.packet.UDP)4 DatapathId (org.projectfloodlight.openflow.types.DatapathId)4 InetSocketAddress (java.net.InetSocketAddress)3 OFFactory (org.projectfloodlight.openflow.protocol.OFFactory)3 OFMessage (org.projectfloodlight.openflow.protocol.OFMessage)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ByteBuffer (java.nio.ByteBuffer)2 IPacket (net.floodlightcontroller.packet.IPacket)2 LLDPTLV (net.floodlightcontroller.packet.LLDPTLV)2 OFPortDescPropEthernet (org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet)2 IPv4Address (org.projectfloodlight.openflow.types.IPv4Address)2