首页 > 代码库 > Floodlight 启动流程分析

Floodlight 启动流程分析


 
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();
                    //解析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);
                        //缓存到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的更新消息。