Skip to content
Snippets Groups Projects
Commit ab012e5c authored by Kanzhe Jiang's avatar Kanzhe Jiang
Browse files

Add REST API to mac-based packettracer

parent 8758aff3
No related branches found
No related tags found
No related merge requests found
......@@ -213,15 +213,17 @@ public class OFMessageFilterManager implements IOFMessageListener {
if (m.getType() == OFType.PACKET_IN) {
eth = IFloodlightProvider.bcStore.get(cntx,
IFloodlightProvider.CONTEXT_PI_PAYLOAD);
} else if (m.getType() == OFType.PACKET_OUT) {
eth = new Ethernet();
OFPacketOut p = (OFPacketOut) m;
// No MAC match if packetOut doesn't have the packet.
if (p.getPacketData() == null) return null;
eth.deserialize(p.getPacketData(), 0, p.getPacketData().length);
} else if (m.getType() == OFType.FLOW_MOD) {
// no action performed.
// flow-mod can't be matched by mac.
return null;
}
if (eth == null) return null;
......@@ -238,10 +240,11 @@ public class OFMessageFilterManager implements IOFMessageListener {
while (fieldIt.hasNext()) {
String filterFieldType = fieldIt.next();
String filterFieldValue = filter.get(filterFieldType);
if (filterFieldType == "mac") {
if (filterFieldType.equals("mac")) {
String srcMac = HexString.toHexString(eth.getSourceMACAddress());
String dstMac = HexString.toHexString(eth.getDestinationMACAddress());
log.debug("srcMac: {}, dstMac: {}", srcMac, dstMac);
if (filterFieldValue.equals(srcMac) ||
filterFieldValue.equals(dstMac)){
......@@ -343,7 +346,7 @@ public class OFMessageFilterManager implements IOFMessageListener {
@Override
public boolean isCallbackOrderingPostreq(OFType type, String name) {
return false;
return (type == OFType.PACKET_IN && name.equals("learningswitch"));
}
@Override
......@@ -368,7 +371,7 @@ public class OFMessageFilterManager implements IOFMessageListener {
log.error("sendPacket exception: {}", e);
}
}
return Command.CONTINUE;
}
......@@ -421,7 +424,6 @@ public class OFMessageFilterManager implements IOFMessageListener {
OFPacketIn pktIn = (OFPacketIn)msg;
packet.setSwPortTuple(new SwitchPortTuple(sw.getId(), pktIn.getInPort()));
bb = ChannelBuffers.buffer(pktIn.getLength());
log.debug("Packet-In length is: {}", pktIn.getLength());
pktIn.writeTo(bb);
packet.setData(getData(sw, msg, cntx));
break;
......
......@@ -43,6 +43,7 @@ public class CoreWebRoutable implements RestletRoutable {
router.attach("/counter/categories/{switchId}/{counterName}/{layer}/json", SwitchCounterCategoriesResource.class);
router.attach("/memory/json", ControllerMemoryResource.class);
router.attach("/staticflowentrypusher/json", StaticFlowEntryPusherResource.class);
router.attach("/packettrace/json", PacketTraceResource.class);
return router;
}
}
package net.floodlightcontroller.core.web;
import java.util.concurrent.ConcurrentHashMap;
import org.restlet.data.Status;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.floodlightcontroller.core.OFMessageFilterManager;
public class PacketTraceResource extends ServerResource {
protected static Logger log = LoggerFactory.getLogger(PacketTraceResource.class);
public static class FilterParameters {
protected String sessionId = null;
protected String mac = null;
protected Integer period = null;
protected String direction = null;
protected String output = null;
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
public Integer getPeriod() {
return period;
}
public void setPeriod(Integer period) {
this.period = period;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output = output;
}
public String toString() {
return "SessionID: " + sessionId +
"\tmac" + mac +
"\tperiod" + period +
"\tdirection" + direction +
"\toutput" + output;
}
}
public static class PacketTraceOutput {
protected String sessionId = null;
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
}
@Post("json")
public PacketTraceOutput packettrace(FilterParameters fp) {
ConcurrentHashMap <String,String> filter = new ConcurrentHashMap<String,String> ();
String sid = null;
PacketTraceOutput output = new PacketTraceOutput();
OFMessageFilterManager manager =
(OFMessageFilterManager)getContext()
.getAttributes().get("messageFilterManager");
if (manager == null) {
sid = null;
setStatus(Status.SERVER_ERROR_SERVICE_UNAVAILABLE);
}
if (fp.getSessionId() != null) {
filter.put("sessionId", fp.getSessionId());
}
if (fp.getMac() != null) {
filter.put("mac", fp.getMac());
}
if (fp.getDirection() != null) {
filter.put("direction", fp.getDirection());
}
if (filter.isEmpty()) {
log.warn ("restlet packettrace: empty filter");
setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
} else {
log.debug ("Call setupFilter: sid:{} filter:{}, period:{}",
new Object[] {fp.getSessionId(), filter, fp.getPeriod()});
sid = manager.setupFilter(fp.getSessionId(), filter, fp.getPeriod());
output.setSessionId(sid);
setStatus(Status.SUCCESS_OK);
}
return output;
}
}
......@@ -67,8 +67,9 @@ public class PacketStreamerHandler implements PacketStreamer.Iface {
public List<ByteBuffer> getPackets(String sessionid)
throws org.apache.thrift.TException {
List<ByteBuffer> packets = new ArrayList<ByteBuffer>();
while (!msgQueues.containsKey(sessionid)) {
int count = 0;
while (!msgQueues.containsKey(sessionid) && count++ < 100) {
log.debug("Queue for session {} doesn't exist yet.", sessionid);
try {
Thread.sleep(100); // Wait 100 ms to check again.
......@@ -77,14 +78,16 @@ public class PacketStreamerHandler implements PacketStreamer.Iface {
}
}
SessionQueue pQueue = msgQueues.get(sessionid);
BlockingQueue<ByteBuffer> queue = pQueue.getQueue();
// Block if queue is empty
try {
packets.add(queue.take());
queue.drainTo(packets);
} catch (InterruptedException e) {
log.error(e.toString());
if (count < 100) {
SessionQueue pQueue = msgQueues.get(sessionid);
BlockingQueue<ByteBuffer> queue = pQueue.getQueue();
// Block if queue is empty
try {
packets.add(queue.take());
queue.drainTo(packets);
} catch (InterruptedException e) {
log.error(e.toString());
}
}
return packets;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment