1、main函数入口
/neutron/agent/dhcp_agent.py
def main():
register_options(cfg.CONF)
common_config.init(sys.argv[1:])
config.setup_logging()
server = neutron_service.Service.create(
binary='neutron-dhcp-agent',
topic=topics.DHCP_AGENT,
report_interval=cfg.CONF.AGENT.report_interval,
manager='neutron.agent.dhcp.agent.DhcpAgentWithStateReport')
service.launch(cfg.CONF, server).wait()
调用Service类的create方法创建一个Service对象,通过launch方法来启动DHCP agent服务。
create()方法位于/neutron/service.py,准备各种参数,包含主机名、服务名、主题、间隔时间。
launch()方法位于oslo_service,根据服务数创建进程数,launch()方法会调用到/neutron/service.py中的start()方法,launcher对象的wait()方法启动创建的线程。
2、DHCP agent服务的Manager类DhcpAgentWithStateReport
/neutron/agent/dhcp/agent.py
DhcpAgentWithStateReport类继承DhcpAgent类,定义了一个定时任务,报告DHCP agent服务的状态。
DhcpAgent类初始化方法主要获取配置文件dhcp_agent.ini中的配置项,调用了核心方法_populate_network_cache()方法,查询和缓存所有启用DHCP功能的网络。
def _populate_networks_cache(self):
"""Populate the networks cache when the DHCP-agent starts."""
try:
existing_networks = self.dhcp_driver_cls.existing_dhcp_networks(
self.conf
)
for net_id in existing_networks:
net =dhcp.NetModel({"id": net_id, "subnets": [],
"non_local_subnets": [], "ports": []})
self.cache.put(net)
except NotImplementedError:
# just go ahead with an empty networks cache
LOG.debug("The '%s' DHCP-driver does not support retrieving of a "
"list of existing networks",
self.conf.dhcp_driver)
existing_dhcp_networks()方法位于/neutron/agent/linux/dhcp.py,获取存储dhcp