neutron.plugins.ml2 bind port

def bind_port(self, context)


2020
-06-08 19:24:05.615 54668 DEBUG neutron.plugins.ml2.managers [req-737d8789-ad68-42d8-adab-0334f94509e6 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Attempting to bind port fe63f730-8d74-4936-aa4c-5b4112d11640 on host c2b298f4-3fdf-44fb-b67d-efefca11dbf9 for vnic_type baremetal with profile {"local_link_information": [{"switch_info": "JG05-SA-01-CE8850", "port_id": "100GE1/0/12", "switch_id": "30:e9:8e:e0:0e:21"}]} bind_port /usr/lib/python2.7/site-packages/neutron/plugins/ml2/managers.py:745 [root@bogon neutron]# neutron port-show fe63f730-8d74-4936-aa4c-5b4112d11640 neutron CLI is deprecated and will be removed in the future. Use openstack CLI instead. +-----------------------+--------------------------------------------------------------------------------------------------------------------------------+ | Field | Value | +-----------------------+--------------------------------------------------------------------------------------------------------------------------------+ | admin_state_up | True | | allowed_address_pairs | | | binding:host_id | c2b298f4-3fdf-44fb-b67d-efefca11dbf9 | | binding:profile | {"local_link_information": [{"switch_info": "JG05-SA-01-CE8850", "port_id": "100GE1/0/12", "switch_id": "30:e9:8e:e0:0e:21"}]} | | binding:vif_details | {} | | binding:vif_type | other | | binding:vnic_type | baremetal | | created_at | 2020-06-08T11:15:04Z | | description | | | device_id | e621ae44-34fd-40cb-8562-870caf408131 | | device_owner | compute:nova | | extra_dhcp_opts | | | fixed_ips | {"subnet_id": "479cefc6-9df2-4f0a-ab1d-6c0248816d22", "ip_address": "172.16.101.29"} | | id | fe63f730-8d74-4936-aa4c-5b4112d11640 | | mac_address | ac:8d:34:89:ae:ec | | name | | | network_id | d25bad06-1316-4321-837e-9dde06147c05 | | port_security_enabled | True | | project_id | 46cc02c6fab04f7095c4bc687d397610 | | revision_number | 17 | | security_groups | e70c2156-a68d-4343-a167-4caffee8c643 | | status | ACTIVE | | tags | | | tenant_id | 46cc02c6fab04f7095c4bc687d397610 | | updated_at | 2020-06-08T11:24:06Z | +-----------------------+--------------------------------------------------------------------------------------------------------------------------------+ [root@bogon neutron]#

def _bind_port_level(self, context, level, segments_to_bind):

2020-06-08 19:24:05.616 54668 DEBUG neutron.plugins.ml2.managers [req-737d8789-ad68-42d8-adab-0334f94509e6 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Attempting to bind port fe63f730-8d74-4936-aa4c-5b4112d11640 on host c2b298f4-3fdf-44fb-b67d-efefca11dbf9 at level 0 using segments [{'network_id': 'd25bad06-1316-4321-837e-9dde06147c05', 'segmentation_id': 101, 'physical_network': u'physnet_vlan', 'id': '4b55b5de-7fc5-4da5-b422-2ebbed7a7fce', 'network_type': u'vlan'}] _bind_port_level /usr/lib/python2.7/site-packages/neutron/plugins/ml2/managers.py:766
neutron.plugins.ml2.managers -->
neutron.plugins.ml2.drivers.mech_agent
2020-06-10 13:43:06.183 47569 DEBUG neutron.plugins.ml2.managers [req-1b0dc29e-7eed-44e7-aaed-1580ab35b0b7 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Attempting to bind port d3992859-5853-43dc-bc5c-c3ee01865f23 on host 031edf6d-1b96-4f54-8eea-1aa6e9d9fdb0 at level 0 using segments [{'network_id': '664ea18c-f066-406a-9b01-b9b155670ec9', 'segmentation_id': 1030, 'physical_network': u'physnet_vlan', 'id': 'c754421f-5ad9-46ef-b347-29f1089cc48d', 'network_type': u'vlan'}] _bind_port_level /usr/lib/python2.7/site-packages/neutron/plugins/ml2/managers.py:766
2020-06-10 13:43:06.184 47569 DEBUG neutron.plugins.ml2.drivers.mech_agent [req-1b0dc29e-7eed-44e7-aaed-1580ab35b0b7 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Attempting to bind port d3992859-5853-43dc-bc5c-c3ee01865f23 on network 664ea18c-f066-406a-9b01-b9b155670ec9 bind_port /usr/lib/python2.7/site-packages/neutron/plugins/ml2/drivers/mech_agent.py:87
67 [neutron.ml2.mechanism_drivers]
 68 fake_agent = neutron.tests.unit.plugins.ml2.drivers.mech_fake_agent:FakeAgentMechanismDriver
 69 faulty_agent = neutron.tests.unit.plugins.ml2.drivers.mech_faulty_agent:FaultyAgentMechanismDriver
 70 l2population = neutron.plugins.ml2.drivers.l2pop.mech_driver:L2populationMechanismDriver
 71 linuxbridge = neutron.plugins.ml2.drivers.linuxbridge.mech_driver.mech_linuxbridge:LinuxbridgeMechanismDriver
 72 logger = neutron.tests.unit.plugins.ml2.drivers.mechanism_logger:LoggerMechanismDriver
 73 macvtap = neutron.plugins.ml2.drivers.macvtap.mech_driver.mech_macvtap:MacvtapMechanismDriver
 74 openvswitch = neutron.plugins.ml2.drivers.openvswitch.mech_driver.mech_openvswitch:OpenvswitchMechanismDriver
 75 sriovnicswitch = neutron.plugins.ml2.drivers.mech_sriov.mech_driver.mech_driver:SriovNicSwitchMechanismDriver
 76 test = neutron.tests.unit.plugins.ml2.drivers.mechanism_test:TestMechanismDriver

 

class GenericSwitchDriver(api.MechanismDriver):

2020-06-11 11:25:20.339 19566 INFO networking_generic_switch.generic_switch_mech [req-94fb8ba6-da74-490c-940c-698776242448 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Successfully bound port c0e6680f-7a2d-452e-b038-93898450a246 in segment 1030 on device JG05-SA-01-CE8850
[root@bogon neutron]# grep 'c0e6680f-7a2d-452e-b038-93898450a246' server.log | grep managers.py
2020-06-11 11:25:01.382 19566 DEBUG neutron.plugins.ml2.managers [req-94fb8ba6-da74-490c-940c-698776242448 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Attempting to bind port c0e6680f-7a2d-452e-b038-93898450a246 on host a48f667e-184f-4778-adb0-3572eb3e6e53 for vnic_type baremetal with profile {"local_link_information": [{"switch_info": "JG05-SA-01-CE8850", "port_id": "100GE1/0/12", "switch_id": "30:e9:8e:e0:0e:21"}]} bind_port /usr/lib/python2.7/site-packages/neutron/plugins/ml2/managers.py:745


2020-06-11 11:25:01.383 19566 DEBUG neutron.plugins.ml2.managers [req-94fb8ba6-da74-490c-940c-698776242448 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Attempting to bind port c0e6680f-7a2d-452e-b038-93898450a246 on host a48f667e-184f-4778-adb0-3572eb3e6e53 at level 0 using segments [{'network_id': '38c0d85e-bdd1-4809-8c95-aeb9e62a8263', 'segmentation_id': 1030, 'physical_network': u'physnet_vlan', 'id': 'e71fba0c-fd16-420d-9f9b-b7ec497bbf37', 'network_type': u'vlan'}] _bind_port_level /usr/lib/python2.7/site-packages/neutron/plugins/ml2/managers.py:766

 


2020-06-11 11:25:20.340 19566 DEBUG neutron.plugins.ml2.managers [req-94fb8ba6-da74-490c-940c-698776242448 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Bound port: c0e6680f-7a2d-452e-b038-93898450a246, host: a48f667e-184f-4778-adb0-3572eb3e6e53, vif_type: other, vif_details: {}, binding_levels: [{'bound_driver': 'genericswitch', 'bound_segment': {'network_id': '38c0d85e-bdd1-4809-8c95-aeb9e62a8263', 'segmentation_id': 1030, 'physical_network': u'physnet_vlan', 'id': 'e71fba0c-fd16-420d-9f9b-b7ec497bbf37', 'network_type': u'vlan'}}] _bind_port_level /usr/lib/python2.7/site-packages/neutron/plugins/ml2/managers.py:814


[root@bogon neutron]# grep 'c0e6680f-7a2d-452e-b038-93898450a246' server.log | grep mech_agent.py
2020-06-11 11:25:01.383 19566 DEBUG neutron.plugins.ml2.drivers.mech_agent [req-94fb8ba6-da74-490c-940c-698776242448 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Attempting to bind port c0e6680f-7a2d-452e-b038-93898450a246 on network 38c0d85e-bdd1-4809-8c95-aeb9e62a8263 bind_port /usr/lib/python2.7/site-packages/neutron/plugins/ml2/drivers/mech_agent.py:87


[root@bogon neutron]# grep 'c0e6680f-7a2d-452e-b038-93898450a246' server.log | grep generic_switch_mech
2020-06-11 11:25:01.384 19566 INFO networking_generic_switch.generic_switch_mech [req-94fb8ba6-da74-490c-940c-698776242448 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] port id c0e6680f-7a2d-452e-b038-93898450a246
2020-06-11 11:25:20.339 19566 INFO networking_generic_switch.generic_switch_mech [req-94fb8ba6-da74-490c-940c-698776242448 fde919fba81146bfa6a74a5a8e483f45 42272601916748e2ba64d8a1c5413ba4 - default default] Successfully bound port c0e6680f-7a2d-452e-b038-93898450a246 in segment 1030 on device JG05-SA-01-CE8850
2020-06-11 11:36:40.767 19566 INFO networking_generic_switch.generic_switch_mech [req-6b6b22e6-c926-48fd-8e4e-bea105ab1bc9 33c5f89c308d40869e04811a2463898f 42272601916748e2ba64d8a1c5413ba4 - default default] Port c0e6680f-7a2d-452e-b038-93898450a246 has been unplugged from network 38c0d85e-bdd1-4809-8c95-aeb9e62a8263 on device JG05-SA-01-CE8850
[root@bogon neutron]#


2020-06-11 09:40:04.098 19568 DEBUG neutron.plugins.ml2.drivers.mech_agent [req-22dd19a5-54cb-4383-9d65-c361a60de633 - - - - -] Checking agent: {'binary': u'neutron-linuxbridge-agent', 'description': None, 'availability_zone': None, 'heartbeat_timestamp': datetime.datetime(2020, 6, 11, 1, 39, 54), 'admin_state_up': True, 'alive': True, 'topic': u'N/A', 'host': u'bogon', 'agent_type': u'Linux bridge agent', 'resource_versions': {u'Subnet': u'1.0', u'Log': u'1.0', u'SubPort': u'1.0', u'SecurityGroup': u'1.0', u'SecurityGroupRule': u'1.0', u'Trunk': u'1.1', u'QosPolicy': u'1.7', u'Port': u'1.1', u'Network': u'1.0'}, 'created_at': datetime.datetime(2019, 7, 29, 7, 32, 17), 'started_at': datetime.datetime(2020, 6, 11, 1, 16, 24), 'id': '8bc3c21c-00e8-4a07-b7e7-6ac8839a1ab5', 'configurations': {u'extensions': [], u'interface_mappings': {u'physnet_vlan': u'enahisic2i3'}, u'bridge_mappings': {}, u'devices': 2}} bind_port /usr/lib/python2.7/site-packages/neutron/plugins/ml2/drivers/mech_agent.py:103

 

 

 46585 2020-06-11 09:40:04.099 19568 DEBUG neutron.plugins.ml2.drivers.mech_agent [req-22dd19a5-54cb-4383-9d65-          c361a60de633 - - - - -] Checking segment: {'network_id': '38c0d85e-bdd1-4809-8c95-aeb9e62a8263',                  'segmentation_id': 1030, 'physical_network': u'physnet_vlan', 'id': 'e71fba0c-fd16-420d-9f9b-                     b7ec497bbf37', 'network_type': u'vlan'} for mappings: {u'physnet_vlan': u'enahisic2i3'} with network              types: ['local', 'flat', 'vlan'] check_segment_for_agent /usr/lib/python2.7/site-packages/neutron/plugins/        ml2/drivers/mech_agent.py:236
二 Mechanism Manager的处理
Mechanism Manager的处理逻辑很简单,仅仅是实现bind_port()方法:依次尝试调用每一个注册的Mechanism Driver,直到成功bind。
#neutron/plugins/ml2/managers.py
    def bind_port(self, context):
        binding = context._binding
        #依次尝试调用每一个注册的Mechanism Driver
        for driver in self.ordered_mech_drivers:
            try:
                #非常直接的调用每一个注册的Mechanim Driver
                driver.obj.bind_port(context)
                if binding.segment:
                         #bind成功
                    binding.driver = driver.name
                    return
三 Mechanism Driver的处理
以Linux Bridge Driver为例进行说明。
#neutron/plugins/ml2/drivers/mech_agent.py
class AgentMechanismDriverBase(api.MechanismDriver):
def bind_port(self, context):
        vnic_type = context.current.get(portbindings.VNIC_TYPE,
                                        portbindings.VNIC_NORMAL)
        if vnic_type not in self.supported_vnic_types:
            return
        for agent in context.host_agents(self.agent_type):
            LOG.debug(_("Checking agent: %s"), agent)
            if agent['alive']:
                for segment in context.network.network_segments:
                    if self.try_to_bind_segment_for_agent(context, segment,
                                                          agent):
                        return
AgentMechanismDriverBase实现了公用的bind_port()方法。
它的基本执行逻辑是:
寻找port所在host的所有该Driver的Agent(对于Linux Bridge Driver来说仅有一个),并确保Agent处于live状态,然后通过具体Driver对每个Segment进行逐一检查,如果此Segment能够提供符合条件的port,则返回binding信息给Ml2Plugin。

转载

https://blog.csdn.net/yanheven1/article/details/47357537

3. 创建简单的ML2 Mechanism driver, 名字叫”cookbook”:
3.1. 在devstack安装目录下的neutron目录下:
/opt/stack/neutron/neutron/plugins/ml2/drivers
创建文件 ml2_mech_driver.py 如下:

# Import Neutron Database API
from neutron.db import api as db
try:
from neutron.openstack.common import log as logger
except ImportError:
from oslo_log import log as logger
from neutron.plugins.ml2 import driver_api as api

driver_logger = logger.getLogger(__name__)


class CookbookMechanismDriver(api.MechanismDriver):

def initialize(self):
driver_logger.info("Inside Mech Driver Initialize")
 
3.2. 配置neutron server 使用上面这个ML2 mechanism driver,
编辑文件: /etc/neutron/plugins/ml2/ml2_conf.ini

[ml2]
tenant_network_types = vlan
type_drivers = local,flat,vlan,gre,vxlan
mechanism_drivers = openvswitch,cookbook
 
编辑入口配置文件: /opt/stack/neutron/neutron.egg-info/entry_points.txt
在 [neutron.ml2.mechanism_drivers] 配置部分, 增加一行指定cookbook 入口:

[neutron.ml2.mechanism_drivers]
...
neutron.plugins.ml2.drivers.ml2_mech_driver.CookbookMechanismDriver
 
重启neutron server, 从日志 /opt/stack/logs/q-svc.log 中可以看到我们的改动.

4. 完善cookbook mechanism driver, 增加网络处理模块:
增加文件 /opt/stack/neutron/neutron/plugins/ml2/drivers/ml2_mech_driver_network.py 如下:

try:
from neutron.openstack.common import log as logger
except ImportError:
from oslo_log import log as logger
from neutron.plugins.ml2 import driver_api as api

driver_logger = logger.getLogger(__name__)


class CookbookNetworkMechanismDriver(api.MechanismDriver):

def _log_network_information(self, method_name, current_context, prev_context):
driver_logger.info("**** %s ****" % (method_name))
# Print the Network Name using the context
driver_logger.info("Current Network Name: %s" % (current_context['name']))
# For create operation prev_context will be None.
if prev_context is not None:
driver_logger.info("Previous Network Name: %s" % (prev_context['name']))
# Print the Network Type
driver_logger.info("Current Network Type: %s" % current_context['provider:network_type'])
driver_logger.info("**** %s ****" % (method_name))

def create_network_postcommit(self, context):
# Extract the current and the previous network context
current_network_context = context.current
previous_network_context = context.original
self._log_network_information("Create Network PostCommit", current_network_context, previous_network_context)

def update_network_postcommit(self, context):
# Extract the current and the previous network context
current_network_context = context.current
previous_network_context = context.original
self._log_network_information("Update Network PostCommit", current_network_context, previous_network_context)
 
编辑/opt/stack/neutron/neutron/plugins/ml2/drivers/ml2_mech_driver.py 如下:

# Import Neutron Database API
from neutron.db import api as db
try:
from neutron.openstack.common import log as logger
except ImportError:
from oslo_log import log as logger
from neutron.plugins.ml2 import driver_api as api
import ml2_mech_driver_network as cookbook_network_driver

driver_logger = logger.getLogger(__name__)


class CookbookMechanismDriver(api.MechanismDriver, ml2_mech_driver_network.CookbookNetworkMechanismDriver):

def initialize(self):
driver_logger.info("Inside Mech Driver Initialize")
 
重启neutron 服务, 创建网络:

$neutron net-create CookbookNetwork1
1
可以从日志 /opt/stack/log/q-svc.log 看到打印出来的网络信息.

5. 完善cookbook mechanism driver, 增加子网处理模块:
增加文件 /opt/stack/neutron/neutron/plugins/ml2/drivers/ml2_mech_driver_subnet.py 如下:

# Import Neutron Database API
from neutron.db import api as db
try:
from neutron.openstack.common import log as logger
except ImportError:
from oslo_log import log as logger
from neutron.plugins.ml2 import driver_api as api

# Import ML2 Database API
from neutron.plugins.ml2 import db as ml2_db


driver_logger = logger.getLogger(__name__)


class CookbookSubnetMechanismDriver(api.MechanismDriver):

def _log_subnet_information(self, method_name, current_context, prev_context):
driver_logger.info("**** %s ****" % (method_name))
driver_logger.info("Current Subnet Name: %s" % (current_context['name']))
driver_logger.info("Current Subnet CIDR: %s" % (current_context['cidr']))
# Extract the Network ID from the Subnet Context
network_id = current_context['network_id']
# Get the Neutron DB Session Handle
session = db.get_session()
# Using ML2 DB API, fetch the Network that matches the Network ID
networks = ml2_db.get_network_segments(session, network_id)
driver_logger.info("Network associated to the Subnet: %s" % (networks))
driver_logger.info("**** %s ****" % (method_name))

def create_subnet_postcommit(self, context):
# Extract the current and the previous Subnet context
current_subnet_context = context.current
previous_subnet_context = context.original
self._log_subnet_information("Create Subnet PostCommit", current_subnet_context, previous_subnet_context)
 
编辑/opt/stack/neutron/neutron/plugins/ml2/drivers/ml2_mech_driver.py 如下:

# Import Neutron Database API
from neutron.db import api as db
try:
from neutron.openstack.common import log as logger
except ImportError:
from oslo_log import log as logger
from neutron.plugins.ml2 import driver_api as api
import ml2_mech_driver_network as cookbook_network_driver
import ml2_mech_driver_subnet as cookbook_subnet_driver


driver_logger = logger.getLogger(__name__)


class CookbookMechanismDriver(api.MechanismDriver, ml2_mech_driver_network.CookbookNetworkMechanismDriver, cookbook_subnet_driver.CookbookSubnetMechanismDriver):

def initialize(self):
driver_logger.info("Inside Mech Driver Initialize")
 
重启neutron 服务, 创建子网:

$eutron subnet-create --name CookbookSubnet2 CookbookNetwork2 10.0.0.0/24
1
可以从日志 /opt/stack/log/q-svc.log 看到打印出来的网络信息.

6. 完善cookbook mechanism driver, 增加网络接口port处理模块:
增加文件 /opt/stack/neutron/neutron/plugins/ml2/drivers/ml2_mech_driver_port.py 如下:

try:
from neutron.openstack.common import log as logger
except ImportError:
from oslo_log import log as logger
from neutron.plugins.ml2 import driver_api as api

driver_logger = logger.getLogger(__name__)


class CookbookPortMechanismDriver(api.MechanismDriver):

def _log_port_information(self, method_name, context):
driver_logger.info("**** %s ****" % (method_name))
# Extract the current Port context
current_port_context = context.current
# Extract the associated Network Context
network_context = context.network
driver_logger.info("Port Type: %s" % (current_port_context['device_owner']))
driver_logger.info("IP Address of the Port: %s" % ((current_port_context['fixed_ips'][0])['ip_address']))
driver_logger.info("Network name for the Port: %s" % (network_context.current['name']))
driver_logger.info("Network type for the Port: %s" % (network_context.current['provider:network_type']))
driver_logger.info("Segmentation ID for the Port: %s" % (network_context.current['provider:segmentation_id']))
driver_logger.info("**** %s ****" % (method_name))

def create_port_postcommit(self, context):
self._log_port_information("Create Port PostCommit", context)
 
编辑/opt/stack/neutron/neutron/plugins/ml2/drivers/ml2_mech_driver.py 如下:

# Import Neutron Database API
from neutron.db import api as db
try:
from neutron.openstack.common import log as logger
except ImportError:
from oslo_log import log as logger
from neutron.plugins.ml2 import driver_api as api
import ml2_mech_driver_network as cookbook_network_driver
import ml2_mech_driver_port as cookbook_port_driver
import ml2_mech_driver_subnet as cookbook_subnet_driver


driver_logger = logger.getLogger(__name__)


class CookbookMechanismDriver(api.MechanismDriver, ml2_mech_driver_network.CookbookNetworkMechanismDriver, cookbook_subnet_driver.CookbookSubnetMechanismDriver, cookbook_port_driver.CookbookPortMechanismDriver):

def initialize(self):
driver_logger.info("Inside Mech Driver Initialize")
 
重启neutron 服务, 创建一个路由, 然后连接一个子网到路由, 就会触发创建port的方法:

$neutron router-create CookbookRouter
$neutron router-interface-add CookbookRouter CookbookSubnet2
 
可以从日志 /opt/stack/log/q-svc.log 看到打印出来的网络信息. 可以看到port type 是 network:router_interface.

 https://github.com/reachsrirams/packt-openstack-networking-cookbook
 

原文地址:https://www.cnblogs.com/dream397/p/13070603.html