Neutron dhcp agent创建网络过程

来源:互联网 发布:ppt视频不流畅 知乎 编辑:程序博客网 时间:2024/05/01 22:55
neutron 中的core_plugin指定为Ml2Plugin: core_plugin = neutron.plugins.ml2.plugin.Ml2Plugin
其中Ml2Plugin中指定    # List of supported extensions
_supported_extension_aliases = ["provider", "external-net", "binding",
                                    "quotas", "security-group", "agent",
                                    "dhcp_agent_scheduler", "networkranges",
                                    "multi-provider", "allowed-address-pairs",
                                    "extra_dhcp_opt", "subnet_allocation",
                                    "net-mtu", "vlan-transparent", "vips"]

Ml2是对多种Layer2方法的抽象, 把Layer2的各种实现中通用的操作(比如db相关的操作)放到Ml2的实现中。
mechainsm_drivers:  物理的二层driver, 包括,openvswitch,linuxbridge,l2population等
extension_drivers:  为了弥补Ml2不能完全通用的情况,有些mechainsm_drivers有可能需要一些特殊的属性,通过extension_drivers来实现。 
                                 Kilo社区中实现了一种port_security extension driver,也起到了示例的作用。
type_drivers:  各个类型的driver, kilo中支持以下几种了类型: 
    flat = neutron.plugins.ml2.drivers.type_flat:FlatTypeDriver
    local = neutron.plugins.ml2.drivers.type_local:LocalTypeDriver
    vlan = neutron.plugins.ml2.drivers.type_vlan:VlanTypeDriver
    gre = neutron.plugins.ml2.drivers.type_gre:GreTypeDriver
    vxlan = neutron.plugins.ml2.drivers.type_vxlan:VxlanTypeDriver
    nexus_vxlan = neutron.plugins.ml2.drivers.cisco.nexus.type_nexus_vxlan:NexusVxlanTypeDriver
    其中type_driver主要实现了segment的分配, 如vlan中的(ml2_vlan_allocations) vxlan中的(ml2_vxlan_allocations)
    flat中的(ml2_flat_allocations) gre中的(ml2_gre_allocations

一. neutron中创建网络的过程:
neutron-server(负责创建数据库记录,并分配segmentation id,没有通知任何的agent):
1. plugins/ml2/plugin.py: Ml2Plugin.create_network(): 
    1.1  调用db_base_plugin_v2.NeutronDbPluginV2.create_network(),创建network数据库对象,但是还没有commit
    1.2  调用各个extension_driver的process_create_network(), 测试环境中没有配置extension_driver所以忽略
    1.3  调用external_net_db.External_net_db_mixin._process_l3_create:  判断是否创建的是external network, 如果是会在externalnetworks表上创建一条记录。
    1.4  调用type_manager.create_network_segments()
          1.4.1   _process_provider_create() 验证创建的参数(network_type, physical_network以及segmentation_id),其中根据network_type来获取type_driver
                     并调用type_driver的validate_provider_segment() 验证传入的segmentation_id 是否可用。
          1.4.2  如果传了segmentation_id 则会调用type_driver的reserve_provider_segment(),  没传则创建一个segment,调用type_driver的allocate_tenant_segment(),
                    创建一条allocation记录, 比如network_type为vlan则在ml2_vlan_allocations中创建一条记录。
          1.4.3  plugins/ml2/db.py. add_network_segment() 创建segment的分配记录, 操作的表为ml2_network_segments
    1.5  调用type_manager.extend_network_dict_provider() 将create_network_segment中处理的值赋值给创建的network对象
    1.6  调用mechainsm_manager的create_network_precommit(),   openvswitch并没有实现这个方法。
    1.7  前一步正常退出,则会commit数据库的create操作
    1.8  调用mechainsm_manager的create_network_postcommit()  openvswitch并没有实现这个方法。

创建网络的过程涉及的都是数据库的操作,没有实质性的操作。只有创建subnet的时候才会真正通知dhcp agent
见:neutron.api.rpc.agentnotifiers.nofity操作, 此时会执行调度策略,不管subnet是否enable dhcp
network scheduler的类型(和router的scheduler类型):
1.  ChangeScheduler: 随机分配
2.  WeightScheduler: 选择network数量最少的一个agent
接下来看dhcp-agent是怎么处理网络的创建操作。
neutron-dhcp-agent
dhcp-agent中sync_state的过程:
dhcp-agent在初始化过程中会实例化network_cache,  (_populate_networks_cache()):  里面会保存当前dhcp agent中已经存在的network的id,检查的目录/var/lib/neutron/dhcp/中目录名为uuid格式的都认为是当前的网络
sync_state只处理网络删除或者网络创建的操作, 更新的操作不由sync_state处理
sync_state中会先rpc_call neutron server: get_active_networks_info() 获取当前节点的所有网络。
这里需要注意neutron中的一个配置network_auto_schedule: 这个选项决定dhcp-agent会不会自动选择遗落的网络,  这个配置和router_auto_sheduler类似。

neutron-dhcp-agent收到neutron server创建网络的通知和neutron-dhcp-agent主动承载一个网络的操作基本上是一样的,如下
dhcp-agent.ini中dhcp_driver设置为 dhcp_driver = neutron.agent.linux.dhcp.Dnsmasq
1.  检查网络中是否有enable dhcp的subnet, 有则会调用dhcp_driver的enable操作
2.  Dnsmasq.enable() 
    2.1  如果当前存在Dnsmasq的进程,则重启, 没有则继续操作
    2.2. 调用DeviceManager的setup方法,
        2.2.1.  setup_dhcp_port() 如果有dhcp_port,则返回,没有则创建,  根据port里面的device_id选项来区分这个port是否属于这个agent的dhcp port
        2.2.2.  判断port的interface名字是否存在(如:tape613727f-c3 ),如果不存在则会创建这个interface, 调用driver的plug(这个方法中会创建namespace,创建设备(创建的过程需要openvswitch agent配合,在下面介绍),并将设备绑定到br-int上
        2.2.3.  fill_dhcp_udp_checksum(),在dhcp namespace中创建udp checksum规则?, 如: -A neutron-dhcp-age-POSTROUTING -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill
        2.2.4.  调用driver的init_l3(),初始化dhcp namespace中的路由规则, 其中包括subnet中的默认网关以及metadata相关的路由。
        2.2.5   调用dhcp_driver的spawn_process()
            2.2.5.1  在创建Dnsmasq服务之前需要初始化它的配置文件, 包括三个文件(hosts_file, addn_hosts_file, opts_file)
                         hosts_file:  记录mac, hostname, ip的映射关系
                         addn_hosts_file:  记录ip, 长hostname,短hostname的映射关系
                         opts_file:  记录默认路由以及dns server
             2.2.5.2  创建Dnsmasq进程。

这样创建一个网络,包括subnet的过程基本结束了。
0 0
原创粉丝点击