OpenStack之Neutron源码分析 Neutron-server初始化

       Neutron是OpenStack中用于管理网络的项目。neutron代码的入口配置文件neutron/setup.cfg,我们可以通过这个文件了解整个项目的代码结构。


部分setup.cfg内容如下:

[entry_points]
console_scripts = 
    neutron-l3-agent = neutron.cmd.eventlet.agents.l3:main
    neutron-linuxbridge-agent = neutron.plugins.ml2.drivers.linuxbridge.agent.linuxbridge_neutron_agent:main
    neutron-openvswitch-agent = neutron.cmd.eventlet.plugins.ovs_neutron_agent:main
    neutron-server = neutron.cmd.eventlet.server:main_wsgi_eventlet
    neutron-rpc-server = neutron.cmd.eventlet.server:main_rpc_eventlet
    neutron-sanity-check = neutron.cmd.sanity_check:main
neutron.core_plugins = 
    ml2 = neutron.plugins.ml2.plugin:Ml2Plugin
  • neutron-l3-agent:l3 agent部署在计算节点或者网络节点上,负责3层虚拟网络的管理。根据setup.cfg文件可以看出neutron-l3-agent的代码路径是neutron\agent\l3\agent
  • neutron-openvswitch-agent:Open vSwitch Agent部署在计算节点或者网络节点上,进行管理OVS虚拟交换机。根据setup.cfg文件可以看出neutron-openvswitch-agent的代码路径是neutron\plugins\openvswitch\agent.ovs_neutron_agent
  • neutron-server:是Neutron中唯一的一个服务进程,负责接收用户的RESTful API请求并分发处理给各种agen来完成这些的任务。根据setup.cfg文件可以看出neutron代码路径是neutron\server
  • ML2Plugin:用于提供二层虚拟网络,实现了network/subnet/port资源的操作,这些操作最终由Plugin通过RPC调用OpenvSwitch Agent来完成。根据setup.cfg文件可以看出代码路径是 neutron\plugins\ml2\plugin\Ml2Plugin。

neutron-server启动过程分析

1. neutron-server初始化

  • /etc/init.d/neutron-server
DAEMON=/usr/bin/neutron-server
DAEMON_ARGS="--log-file=$LOGFILE"
DAEMON_DIR=/var/run
if [ ! -x ${DAEMON} ] ; then
    exit 0
fi

case "$1" in
  start)
    test "$ENABLED" = "true" || exit 0
    log_daemon_msg "Starting neutron server" "neutron-server"
    start-stop-daemon -Sbmv --pidfile $PIDFILE --chdir $DAEMON_DIR --exec $DAEMON -- $DAEMON_ARGS
    log_end_msg $?
    ;;
  stop)
    test "$ENABLED" = "true" || exit 0
    log_daemon_msg "Stopping neutron server" "neutron-server"
    start-stop-daemon --stop --oknodo --pidfile ${PIDFILE}
    log_end_msg $?
    ;;
  restart|force-reload)
    test "$ENABLED" = "true" || exit 1
    $0 stop
    sleep 
    $0 start
    ;;
  status)
    test "$ENABLED" = "true" || exit 0
    status_of_proc -p $PIDFILE $DAEMON neutron-server && exit 0 || exit $?
    ;;
  *)
    log_action_msg "Usage: /etc/init.d/neutron-server {start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac

exit 0
  • neutron.server.main
    main方法的核心就是serve_wsgi、serve_rpc两个方法调用,分别创建api服务和rpc服务。

neutron.server.wsgi_eventlet.py

def _eventlet_wsgi_server():
    pool = eventlet.GreenPool()
    # 启动Restful API 以协程方式
    neutron_api = service.serve_wsgi(service.NeutronApiService)
    api_thread = pool.spawn(neutron_api.wait)

    try:
        # 启动RPC API
        neutron_rpc = service.serve_rpc()
    except NotImplementedError:
        LOG.info(_LI("RPC was already started in parent process by "
                     "plugin."))
    else:
        rpc_thread = pool.spawn(neutron_rpc.wait)

        plugin_workers = service.start_plugin_workers()
        for worker in plugin_workers:
            pool.spawn(worker.wait)

        # api and rpc should die together.  When one dies, kill the other.
        rpc_thread.link(lambda gt: api_thread.kill())
        api_thread.link(lambda gt: rpc_thread.kill())

    pool.waitall()


def main():
        server.boot_server(_eventlet_wsgi_server)

neutron.server._ init _.py

def boot_server(server_func):
    # the configuration will be read into the cfg.CONF global data structure
    config.init(sys.argv[:])
    config.setup_logging()
    if not cfg.CONF.config_file:
        sys.exit(_("ERROR: Unable to find configuration file via the default"
                   " search paths (~/.neutron/, ~/, /etc/neutron/, /etc/) and"
                   " the '--config-file' option!"))
    try:
        server_func()
    except KeyboardInterrupt:
        pass
    except RuntimeError as e:
        sys.exit(_("ERROR: %s") % e)

2. Restful Api服务初始化

http://blog.csdn.net/qiqishuang/article/details/52056491

3. rpc服务初始化

http://blog.csdn.net/qiqishuang/article/details/52056511



在人之初,别拿人当幼欺;在人之暮,别拿人当弱辱;在人之前,别拿己当众扬;在人之后,别拿人当猴谤;在人之上,别拿人不当人;在人之下,别拿己不当人。当你习惯过一种日子,那么,你的一生只过上一天;如果你生活常新,那么你每一天都会过得很精彩。