Neutron dhcp agent創建網絡過程

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的過程基本結束了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章