neutron 子网信息发给dhcp的代码
时间: 2023-08-06 19:27:01 浏览: 46
neutron-server 通过调用 neutron.agent.rpc.PluginApi 类的 create_subnet 方法将子网信息发送给 DHCP Agent,具体代码如下:
1. 在 neutron/server/rpc.py 中定义了 PluginApi 类,该类用于向各种插件发送 RPC 消息。
```python
class PluginApi(agent_rpc.PluginApi,
l3_rpc.L3PluginApi,
dhcp_rpc_base.DhcpPluginApi):
def create_subnet(self, context, subnet):
"""Create a subnet."""
return self.call(context,
self.make_msg('create_subnet',
subnet=subnet),
version='1.1')
```
2. 在 neutron/agent/rpc.py 中定义了 PluginApi 类的父类 agent_rpc.PluginApi,该类用于向各种 Agent(包括 DHCP Agent)发送 RPC 消息。
```python
class PluginApi(agent_rpc.API):
"""Agent side of the neutron RPC API."""
def create_subnet(self, context, subnet):
"""Create a subnet."""
return self._call_plugin('create_subnet', context=context,
subnet=subnet)
```
3. 在 neutron/agent/dhcp/agent.py 中定义了 DHCP Agent,该 Agent 接收 PluginApi 发送的消息,并进行相应的处理,包括配置 DHCP 服务等。
```python
class DhcpAgentWithStateReport(DhcpAgent):
def create_subnet(self, context, subnet):
"""Handle the subnet create RPC event."""
# 解析子网信息
network_id = subnet['network_id']
subnet_id = subnet['id']
cidr = subnet['cidr']
gateway_ip = subnet['gateway_ip']
# ... 其他操作
# 配置 DHCP 服务
self.enable_dhcp_helper(network_id, subnet_id, device_owner,
cidr, gateway_ip, dhcp_server_ips)
```
需要注意的是,在 neutron.conf 文件中需要配置 DHCP Agent 启用 DHCP 服务,具体配置项为:
```ini
[DEFAULT]
# ...
dhcp_agent_notification = True
```
这个配置项需要设置为 True,才能使 neutron-server 发送子网信息给 DHCP Agent。