use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.
the class PathVerificationPacketOutTest method testUncastPacket.
@Test
public void testUncastPacket() {
// Generate the VerificationPacket
OFPacketOut packet = pvs.generateVerificationPacket(sw1, OFPort.of(1), sw2, true);
// Source MAC will always be that of sw1 for both Unicast and Broadcast
byte[] srcMacActual = Arrays.copyOfRange(packet.getData(), 6, 12);
assertArrayEquals(MacAddress.of(sw1HwAddrTarget).getBytes(), srcMacActual);
// Destination MAC should be that of sw2 for Unicast Packet
byte[] dstMacActual = Arrays.copyOfRange(packet.getData(), 0, 6);
assertArrayEquals(MacAddress.of(sw2HwAddrTarget).getBytes(), dstMacActual);
// Source and Destination IP's are the respective switch IP's
byte[] srcIpActual = Arrays.copyOfRange(packet.getData(), 26, 30);
assertArrayEquals(srcIpTarget.getAddress().getAddress(), srcIpActual);
byte[] dstIpActual = Arrays.copyOfRange(packet.getData(), 30, 34);
assertArrayEquals(dstIpTarget.getAddress().getAddress(), dstIpActual);
}
use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.
the class PathVerificationService method generateDiscoveryPacket.
/**
* Return Discovery packet.
*
* @param srcSw source switch.
* @param port port.
* @param sign sign.
* @param packetId id of the packet.
* @return discovery packet.
*/
OFPacketOut generateDiscoveryPacket(IOFSwitch srcSw, OFPort port, boolean sign, Long packetId) {
try {
byte[] dpidArray = new byte[8];
ByteBuffer dpidBb = ByteBuffer.wrap(dpidArray);
DatapathId dpid = srcSw.getId();
dpidBb.putLong(dpid.getLong());
byte[] chassisId = new byte[] { 4, 0, 0, 0, 0, 0, 0 };
System.arraycopy(dpidArray, 2, chassisId, 1, 6);
// Set the optionalTLV to the full SwitchID
byte[] dpidTlvValue = Arrays.concatenate(ORGANIZATIONALLY_UNIQUE_IDENTIFIER, new byte[] { REMOTE_SWITCH_OPTIONAL_TYPE, 0, 0, 0, 0, 0, 0, 0, 0 });
System.arraycopy(dpidArray, 0, dpidTlvValue, LLDP_TLV_OPTIONAL_HEADER_SIZE_IN_BYTES, 8);
// Set src mac to be able to detect the origin of the packet.
// NB: previously we set port's address instead of switch (some switches declare unique address per port)
byte[] srcMac = new byte[6];
System.arraycopy(dpidArray, 2, srcMac, 0, 6);
byte[] portId = new byte[] { 2, 0, 0 };
ByteBuffer portBb = ByteBuffer.wrap(portId, 1, 2);
portBb.putShort(port.getShortPortNumber());
byte[] ttlValue = new byte[] { 0, 0x78 };
DiscoveryPacket dp = DiscoveryPacket.builder().chassisId(makeIdLldptvPacket(chassisId, CHASSIS_ID_LLDPTV_PACKET_TYPE)).portId(makeIdLldptvPacket(portId, PORT_ID_LLDPTV_PACKET_TYPE)).ttl(makeIdLldptvPacket(ttlValue, TTL_LLDPTV_PACKET_TYPE)).build();
// Add TLV for t0, this will be overwritten by the switch if it supports switch timestamps
dp.getOptionalTlvList().add(switchTimestampTlv(SWITCH_T0_OPTIONAL_TYPE));
// Add TLV for t1, this will be overwritten by the switch if it supports switch timestamps
dp.getOptionalTlvList().add(switchTimestampTlv(SWITCH_T1_OPTIONAL_TYPE));
LLDPTLV dpidTlv = makeIdLldptvPacket(dpidTlvValue, OPTIONAL_LLDPTV_PACKET_TYPE);
dp.getOptionalTlvList().add(dpidTlv);
// Add T0 based on format from Floodlight LLDP
long time = System.currentTimeMillis();
long swLatency = srcSw.getLatency().getValue();
byte[] timestampTlvValue = ByteBuffer.allocate(Long.SIZE / 8 + LLDP_TLV_OPTIONAL_HEADER_SIZE_IN_BYTES).put(ORGANIZATIONALLY_UNIQUE_IDENTIFIER).put(// 0x01 is what we'll use to differentiate DPID 0x00 from time 0x01
TIMESTAMP_OPTIONAL_TYPE).putLong(time + swLatency).array();
LLDPTLV timestampTlv = makeIdLldptvPacket(timestampTlvValue, OPTIONAL_LLDPTV_PACKET_TYPE);
dp.getOptionalTlvList().add(timestampTlv);
// Type
byte[] typeTlvValue = ByteBuffer.allocate(Integer.SIZE / 8 + LLDP_TLV_OPTIONAL_HEADER_SIZE_IN_BYTES).put(ORGANIZATIONALLY_UNIQUE_IDENTIFIER).put(PATH_ORDINAL_OPTIONAL_TYPE).putInt(PathType.ISL.ordinal()).array();
LLDPTLV typeTlv = makeIdLldptvPacket(typeTlvValue, OPTIONAL_LLDPTV_PACKET_TYPE);
dp.getOptionalTlvList().add(typeTlv);
if (sign) {
Builder builder = JWT.create().withClaim("dpid", dpid.getLong()).withClaim("ts", time + swLatency);
if (packetId != null) {
builder.withClaim("id", packetId);
}
String token = builder.sign(algorithm);
byte[] tokenBytes = token.getBytes(Charset.forName("UTF-8"));
byte[] tokenTlvValue = ByteBuffer.allocate(LLDP_TLV_OPTIONAL_HEADER_SIZE_IN_BYTES + tokenBytes.length).put(ORGANIZATIONALLY_UNIQUE_IDENTIFIER).put(TOKEN_OPTIONAL_TYPE).put(tokenBytes).array();
LLDPTLV tokenTlv = makeIdLldptvPacket(tokenTlvValue, OPTIONAL_LLDPTV_PACKET_TYPE);
dp.getOptionalTlvList().add(tokenTlv);
}
MacAddress dstMac = MacAddress.of(config.getVerificationBcastPacketDst());
IPv4Address dstIp = IPv4Address.of(DISCOVERY_PACKET_IP_DST);
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(DISCOVERY_PACKET_UDP_PORT));
l4.setDestinationPort(TransportPort.of(DISCOVERY_PACKET_UDP_PORT));
Ethernet l2 = new Ethernet().setSourceMACAddress(MacAddress.of(srcMac)).setDestinationMACAddress(dstMac).setEtherType(EthType.IPv4);
l2.setPayload(l3);
l3.setPayload(l4);
l4.setPayload(dp);
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 e) {
logger.error(String.format("error generating discovery packet: %s", e.getMessage()), e);
}
return null;
}
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, Long packetId) {
boolean result = false;
try {
IOFSwitch srcSwitch = switchService.getSwitch(srcSwId);
if (srcSwitch != null && srcSwitch.getPort(port) != null) {
OFPacketOut ofPacketOut = generateDiscoveryPacket(srcSwitch, port, true, packetId);
if (ofPacketOut != null) {
logger.debug("==> Sending discovery packet out {}/{} id {}: {}", srcSwitch.getId(), port.getPortNumber(), packetId, Hex.encodeHexString(ofPacketOut.getData()));
result = srcSwitch.write(ofPacketOut);
} else {
logger.error("<== Received null from generateDiscoveryPacket, inputs where: " + "srcSwitch: {}, port: {} id: {}", srcSwitch, port, packetId);
}
if (result) {
logIsl.info("push discovery package via: {}-{} id:{} OF-xid:{}", srcSwitch.getId(), port.getPortNumber(), packetId, ofPacketOut.getXid());
} else {
logger.error("Failed to send PACKET_OUT(ISL discovery packet) via {}-{} id:{} OF-xid:{}", srcSwitch.getId(), port.getPortNumber(), packetId, ofPacketOut.getXid());
}
}
} catch (Exception exception) {
logger.error(String.format("Unhandled exception in %s", getClass().getName()), exception);
}
return result;
}
use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.
the class SessionServiceTest method errorResponse.
@Test
public void errorResponse() throws Exception {
IOFSwitch sw = createMock(IOFSwitch.class);
setupSwitchMock(sw, dpId);
swWriteAlwaysSuccess(sw);
doneWithSetUp(sw);
OFFactory ofFactory = sw.getOFFactory();
OFPacketOut pktOut = makePacketOut(ofFactory, 1);
CompletableFuture<Optional<OFMessage>> future;
try (Session session = subject.open(context, sw)) {
future = session.write(pktOut);
}
Assert.assertFalse(future.isDone());
subject.handleResponse(sw.getId(), ofFactory.errorMsgs().buildBadActionErrorMsg().setXid(pktOut.getXid()).setCode(OFBadActionCode.BAD_LEN).build());
Assert.assertTrue(future.isDone());
completeSessions(sw);
expectExceptionResponse(future, SessionErrorResponseException.class);
}
use of org.projectfloodlight.openflow.protocol.OFPacketOut in project open-kilda by telstra.
the class SessionServiceTest method barrierInTheMiddle.
@Test
public void barrierInTheMiddle() throws Exception {
IOFSwitch sw = createMock(IOFSwitch.class);
setupSwitchMock(sw, dpId);
swWriteAlwaysSuccess(sw);
doneWithSetUp(sw);
OFFactory ofFactory = sw.getOFFactory();
OFPacketOut pktOut = makePacketOut(ofFactory, 1);
OFBarrierRequest barrier = ofFactory.barrierRequest();
CompletableFuture<Optional<OFMessage>> pktOutFuture;
CompletableFuture<Optional<OFMessage>> barrierFuture;
try (Session session = subject.open(context, sw)) {
pktOutFuture = session.write(pktOut);
barrierFuture = session.write(barrier);
}
Assert.assertFalse(pktOutFuture.isDone());
Assert.assertFalse(barrierFuture.isDone());
subject.handleResponse(sw.getId(), ofFactory.buildBarrierReply().setXid(barrier.getXid()).build());
Assert.assertFalse(pktOutFuture.isDone());
Assert.assertTrue(barrierFuture.isDone());
completeSessions(sw);
Assert.assertTrue(pktOutFuture.isDone());
Assert.assertTrue(barrierFuture.isDone());
Optional<OFMessage> barrierResponse = barrierFuture.get();
Assert.assertTrue(barrierResponse.isPresent());
Assert.assertEquals(OFType.BARRIER_REPLY, barrierResponse.get().getType());
Assert.assertEquals(barrier.getXid(), barrierResponse.get().getXid());
}
Aggregations