控制器与交换机建立连接的过程主要分为三个阶段:控制器启动监听6633端口、交换机与控制器建立连接、控制器与交换机版本协商。
控制器启动监听6633端口
控制器启动的入口函数为
@Activate
OpenFlowControllerImpl.activate(Component context)
OSGI模块启动时候,就会调用这个函数,其中Activate注解说明了这一点:
The Activate annotation defines the method which is used to activate the component.
在OpenFlowControllerImpl类初始化的过程中,首先会实例化Controller和内部类OpenFlowSwitchAgent,OpenFlowSwitchAgent类十分重要,它用于跟踪已经建立连接的交换机以及它处于的状态。
private final Controller ctrl = new Controller();
protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
然后启动控制器Controller.start(OpenFlowAgent agent),主要是配置参数,启动server端,监听端口,等待交换机建立连接。
public void run() {
try {
final ServerBootstrap bootstrap = createServerBootStrap();
bootstrap.setOption("reuseAddr", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
ChannelPipelineFactory pfact =
new OpenflowPipelineFactory(this, null);
bootstrap.setPipelineFactory(pfact);
InetSocketAddress sa = new InetSocketAddress(openFlowPort);
cg = new DefaultChannelGroup();
cg.add(bootstrap.bind(sa));
log.info("Listening for switch connections on {}", sa);
} c