SDN控制器Floodlight源码学习(四)--控制器和交换机交互(2)

来源:互联网 发布:java程序员好找工作吗 编辑:程序博客网 时间:2024/06/05 16:12

上一节学习了
SDN控制器Floodlight源码学习(三)–控制器和交换机交互(1)

http://blog.csdn.net/crystonesc/article/details/70143117

今天接着上一节的线索往下看,上一节我们看到这里,代码如下:

class CompleteState extends OFChannelState{        CompleteState() {            super(true);        }        @Override        void enterState() throws IOException{            setSwitchHandshakeTimeout();            //处理非openflow 1.3的连接            if (featuresReply.getVersion().compareTo(OFVersion.OF_13) < 0){            //新建一个OFConnection的连接类            connection = new OFConnection(featuresReply.getDatapathId(), factory, channel, OFAuxId.MAIN, debugCounters, timer);            }            // Handle 1.3 connections            else {                //新建一个OF 1.3的连接                connection = new OFConnection(featuresReply.getDatapathId(), factory, channel, featuresReply.getAuxiliaryId(), debugCounters, timer);                if (!featuresReply.getAuxiliaryId().equals(OFAuxId.MAIN)) {                    setAuxChannelIdle();                }            }            connection.updateLatency(U64.of(featuresLatency));            echoSendTime = 0;            notifyConnectionOpened(connection);        }    };

也就是在Switch和Controller完成握手之后,需要建立交换机和控制器的连接,OFConnection这个类就是用来封装Switch和Controller连接的类,并提供一些处理链接的能力.我们继续往下看这句:

private final void notifyConnectionOpened(OFConnection connection){        this.connection = connection;        this.newConnectionListener.connectionOpened(connection, featuresReply);    }

notifyConnnectionOpened用于通知监听链接的newConnectionListener,这里OFSwitchManager扮演者坚挺者的角色,其实现的connectionOpened方法用于处理链接请求:

@Override    public void connectionOpened(IOFConnectionBackend connection, OFFeaturesReply featuresReply) {        DatapathId dpid = connection.getDatapathId();        OFAuxId auxId = connection.getAuxId();        log.debug("{} opened", connection);        //OFAuxId.MAIN为交换机与控制器的主链接,也就是说交换机连接的时候会带上auxId,如果auxId为0(0x0),则为主链接        if(auxId.equals(OFAuxId.MAIN)) {            // Create a new switch handshake handler            OFSwitchHandshakeHandler handler =                    new OFSwitchHandshakeHandler(connection, featuresReply, this,                            floodlightProvider.getRoleManager(), timer);            OFSwitchHandshakeHandler oldHandler = switchHandlers.put(dpid, handler);            // Disconnect all the handler's connections            if(oldHandler != null){                log.debug("{} is a new main connection, killing old handler connections", connection);                oldHandler.cleanup();            }            handler.beginHandshake();        } else {            OFSwitchHandshakeHandler handler = switchHandlers.get(dpid);            if(handler != null) {                //新建一个辅助链接                handler.auxConnectionOpened(connection);            }            // Connections have arrived before the switchhandler is ready            else {                log.warn("{} arrived before main connection, closing connection", connection);                connection.disconnect();            }        }    }

上面的代码大致使用OFSwitchHandshakeHandler处理接下来交换机和控制器的交互.这里要大多说下交换机和控制器之间的连接,交换机和控制器是可以包含多条连接的,其中有一条是主链接(Main Connection),其余的均为辅助链接(Aux Connection),主链接和辅助链接都是用相同的datapathId(交换机的唯一标识),不同的auxId,其中auxId为0(0x0)的是主链接,所以上面的代码首先判断了下auxId,如果是0,则建立一个主链接,如果非0,则建立辅助链接。需要注意的是,如果需要断开链接,需要先断开所有的辅助链接,再断开主链接,可以看看断开链接的代码,在OFSwitchHandshakeHandler类中,同时我们可以看到OFSwitchManager通过switchHandlers维护着与所有交换机的连接,每一个OFSwitchHandshakeHandler又通过auxConnections维护着所有的辅助链接(注意这是针对OF1.3的版本来看的,OF其它版本通过代码来看处理方式应该有所区别)

void cleanup() {        for (IOFConnectionBackend conn : this.auxConnections.values()) {            conn.disconnect();        }        this.mainConnection.disconnect();    }

那么接下来就进入OFSwitchHandshakeHandler的beginHandshake方法,来完成接下来的交互.

0 0