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

从文件夹的命名也基本可以得出该目录代码的作用,几个重要的文件夹如下:
agent: 主要是l3 agent及l3 agent ha的相关代码;
common: 主要是各底层驱动与linux系统命令的交互层;
db: 是neutron各功能与数据库交互数据的代码;
extensions: 主要包括的一些扩展功能,包括dvr的代码等;
plugins: 是core plugin的代码,包括ovs,ml2和各个厂商ibm、ryu提供等的plugin;
scheduler: 是创建dhcp服务和router到各l3 agent的调度分配相关代码;
server: 是neutron server相关的代码;
services: 则是包含了lbaas、aas、fwaas、l3-router、metering等服务的plugin和agent代码;

Neutron是OpenStack中用于管理网络的项目。neutron代码的入口配置文件neutron.setup.cfg,我们可以通过这个文件了解整个项目的代码结构。
neutron/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启动过程分析

Neutron-Server启动,无外乎就是加载配置,router各种resource,然后就等待请求了。其中router哪些resource完全是由配置文件来决定的。 当然,在启动的过程中也会初始化db,这也就是为何在安装neutron的时候无需像nova,glance等要执行db sync的原因了。

neutron-server初始化

1. /etc/init.d/neutron-server
该脚本:新建日志目录,服务目录,启动neutron-server守护进程。支持CURD操作。

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 1
    $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

2. neutron/server/main主函数
main方法的核心就是serve_wsgi、serve_rpc两个方法调用,分别创建api服务和rpc服务。新版本对main方法做了高度的抽象与松耦合处理。

1) 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.rpc_eventlet.py

def _eventlet_rpc_server():
    pool = eventlet.GreenPool()
    LOG.info(_LI("Eventlet based AMQP RPC server starting..."))
    try:
        # 启动RPC API
        neutron_rpc = service.serve_rpc()
    except NotImplementedError:
        LOG.info(_LI("RPC was already started in parent process by "
                     "plugin."))
    else:
        pool.spawn(neutron_rpc.wait)
    pool.waitall()


def main():
    server.boot_server(_eventlet_rpc_server)

2) 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[1:])
    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)

其中:wsgi服务以及rpc服务的启动过程如下:

Api服务初始化

serve_wsgi服务:
http://blog.csdn.net/qiqishuang/article/details/52056491

Rpc服务初始化

serve_rpc服务:
http://blog.csdn.net/qiqishuang/article/details/52056511

L2-Agent服务初始化

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

L3-Agent服务初始化

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

参考:
About云:http://www.aboutyun.com/thread-10306-1-1.html

你可能感兴趣的:(OpenStack)