1. 在Main中先是载入模块,启动REST服务,而后构建一个实现了IFloodlightProviderService接口的实例(即Controller)并执行;
2. 接下来进入Controller的run()方法,此时全部的环境初始化工作已经完毕。构建一个基于netty的TCP server,最重要的是流水线factory OpenflowPipelineFactory 的设置,里面是controller上流,下流处理的handler(详细细节见===)。
当Channel建立,接收到来自OF SW的消息之后就会调用 messageReceived() 方法;依据不同的of msg类型,分发给
processOFMessage 进行详细处理;此外。假设这个消息须要进行对SW的回复,或者有其它监听者感兴趣,就呼叫 handleMessage 进行额外的处理,代码贴出来。
protected void handleMessage(IOFSwitch sw, OFMessage m, FloodlightContext bContext){
Ethernet eth = null;
switch (m.getType()) {
case PACKET_IN:
OFPacketIn pi = (OFPacketIn)m;
// 默认情况下总是true
if (Controller.ALWAYS_DECODE_ETH) {
eth = new Ethernet();
Ethernet eth = null;
switch (m.getType()) {
case PACKET_IN:
OFPacketIn pi = (OFPacketIn)m;
// 默认情况下总是true
if (Controller.ALWAYS_DECODE_ETH) {
eth = new Ethernet();
//解析packet_in消息到eth中,所以以下的bcStore能够直接存储
eth.deserialize(pi.getPacketData(), 0, pi.getPacketData().length);
counterStore.updatePacketInCounters(sw, m, eth);
}
// fall through to default case...
default:
List<IOFMessageListener> listeners = null;
if (messageListeners.containsKey(m.getType())) {
listeners = messageListeners.get(m.getType()).getOrderedListeners();
}
FloodlightContext bc = null;
if (listeners != null) {
// Check if floodlight context is passed from the calling
// function, if so use that floodlight context, otherwise
// allocate one
if (bContext == null) {
bc = flcontext_alloc();
} else {
bc = bContext;
}
if (eth != null) {
IFloodlightProviderService.bcStore.put(bc,IFloodlightProviderService.CONTEXT_PI_PAYLOAD, eth);
eth.deserialize(pi.getPacketData(), 0, pi.getPacketData().length);
counterStore.updatePacketInCounters(sw, m, eth);
}
// fall through to default case...
default:
List<IOFMessageListener> listeners = null;
if (messageListeners.containsKey(m.getType())) {
listeners = messageListeners.get(m.getType()).getOrderedListeners();
}
FloodlightContext bc = null;
if (listeners != null) {
// Check if floodlight context is passed from the calling
// function, if so use that floodlight context, otherwise
// allocate one
if (bContext == null) {
bc = flcontext_alloc();
} else {
bc = bContext;
}
if (eth != null) {
IFloodlightProviderService.bcStore.put(bc,IFloodlightProviderService.CONTEXT_PI_PAYLOAD, eth);
//缓存到hashmap中。所以当我们加入自己的模块来监听packetin消息的时候。能够从中取出。做自己的业务处理。
}
Command cmd;
for (IOFMessageListener listener : listeners) {
if (listener instanceof IOFSwitchFilter) {
if (!((IOFSwitchFilter)listener).isInterested(sw)) {
continue;
}
}
// 遍历全部对packetin感兴趣的listener。分别运行他们的receive方法;
cmd = listener.receive(sw, m, bc);
if (Command.STOP.equals(cmd)) {
break;
} } }
if ((bContext == null) && (bc != null)) flcontext_free(bc);
} }
3. 在循环中随时处理SW的更新消息。
版权声明:本文博客原创文章,博客,未经同意,不得转载。