Write Neutron ML2 Mechanism Driver

Author:海峰 http://weibo.com/344736086

http://yanheven.github.io/

1. 基本概念:

core feature: network, subnet, port
plugins feature: loadbalance, firewall, vpn, etc.
ML2 core plugin: type driver,  mechanism driver.
ML2 type driver: vlan, vxlan, gre,  etc.
ML2 mechanism driver: linux bridge, openvSwitch, etc.

2. 环境准备:

devstack 开发环境搭建, local.conf 参考:

[[local|localrc]]
FORCE=yes
ADMIN_PASSWORD=password
DATABASE_PASSWORD=$ADMIN_PASSWORD
RABBIT_PASSWORD=$ADMIN_PASSWORD
SERVICE_PASSWORD=$ADMIN_PASSWORD
SERVICE_TOKEN=$ADMIN_PASSWORD
HOST_IP=192.168.56.102

LIBVIRT_TYPE=qemu
VIRT_DRIVER=libvirt
MULTI_HOST=False

DEST=/opt/stack
LOGFILE=$DEST/logs/stack.sh.log
SCREEN_LOGDIR=$DEST/logs/screen
LOG_COLOR=False
RECLONE=no
VERBOSE=False

disable_service n-net
disable_service tempest
disable_service cinder c-sch c-api c-vol
disable_service heat h-api h-api-cfn h-api-cw h-eng

enable_service q-agt
enable_service q-dhcp
enable_service q-l3
enable_service q-meta
enable_service q-svc

# Neutron related config
Q_PLUGIN=ml2
Q_AGENT=openvswitch
Q_ML2_PLUGIN_MECHANISM_DRIVERS=openvswitch,cookbook

# VLAN Related Config
ENABLE_TENANT_VLANS=TRUE
TENANT_VLAN_RANGE=1000:1100
PHYSICAL_NETWORK=physnet1
FLAT_INTERFACE=eth0
OVS_PHYSICAL_BRIDGE=br-eth0
Q_ML2_TENANT_NETWORK_TYPE=vlan

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

可以从日志 /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

可以从日志 /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.

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-16 08:05:10

Write Neutron ML2 Mechanism Driver的相关文章

配置 linux-bridge mechanism driver - 每天5分钟玩转 OpenStack(77)

本节开始我们将学习 Linux Bridge 如何实现 Neutron 的各种功能.首先需要配置 linux-bridge mechanism driver. Neutorn ML2 plugin 默认使用的 mechanism driver 是 open vswitch 而不是 linux bridge.那是否还有研究 linux bridge 的必要呢?我的答案是:很有必要! 原因如下: linux bridge 技术非常成熟,而且高效,所以业界很多 OpenStack 方案采用的是 lin

neutron ml2 network创建流程源码解析

Neutron的整体架构分为三层. Server —> plugin —>agent 启动server之后neutron会将请求路径和对应的处理函数进行映射. 具体的处理函数由plugin来提供,plugin做的事情有两个: 1)在数据库中创建资源 2)发送rpc请求到具体的agent 所有的plugin提供统一的接口,包括核心资源的增删改查. neutron原生提供ml2这一plugin,ml2plugin分为类型驱动和机制驱动. 下面从创建network的角度来看一下neutron的整个调

Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网

http://www.cnblogs.com/sammyliu/p/4985907.html 1. 基础知识 1.1 VXLAN 和 Linux 以及 Linux bridge 的关系 VXLAN 是一个新兴的SDN 标准,它定义了一种新的 overlay 网络,它主要的创造者是 VMware, Cisco 和 Arista.它被设计来消除虚拟化网络世界中的 VLAN 数目的限制.VXLAN 本身是一个多播标准,但是大多数的企业既不情愿启用多播,而且许多网络设备也不支持多播.因此,许多 VXLA

neutron ml2多种网络模式并存

http://docs.openstack.org/networking-guide/scenario_legacy_ovs.html

详解 ML2 Core Plugin(II) - 每天5分钟玩转 OpenStack(72)

上一节我们讨论了 ML2 Plugin 解决的问题,本节将继续研究 ML2 的架构. ML2 对二层网络进行抽象和建模,引入了 type driver 和 mechansim driver. 这两类 driver 解耦了 Neutron 所支持的网络类型(type)与访问这些网络类型的机制(mechanism),其结果就是使得 ML2 具有非常好的弹性,易于扩展,能够灵活支持多种 type 和 mechanism. Type Driver Neutron 支持的每一种网络类型都有一个对应的 ML

Neutron新进展|DragonFlow在Mitaka版本中的Roadmap

OpenStack网络在Mitaka版本中将有哪些新变化?1月11日到12日,DragonFlow的PTL——Eran Gampel,Kuryr的PTL——Gal Sagie,和他们的老大从以色列来到杭州,参加DragonFlow Meetup.UnitedStack有云的网络组同事苌智和康敬亭参与了这次讨论,并整理出Dragonflow在Mitaka版本中要完成的工作以为未来的Roadmap. 背景介绍Dragonflow是OpenStack网络组件Neutron的子项目,由华为以色列技术团队

Openstack入坑指南

什么是云计算 概念 云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息,可以按需求提供给计算机和其他设备.用户不需要了解”云“中的基础设施细节,不必具有相应的专业知识,也无需直接控制.云计算描述了一种基于互联网的新的IT服务增加.使用和交付模式. 我们举一个例子来理解云计算,云计算中的”云“可以理解为天上的云,天上的云可以变成雨水降落到地上,落到地上的水蒸发后又变成云彩.这样就形成了一个循环. 这里的雨水表示计算资源,比如虚拟机.存储.网络等等. 云变水的过程表示获取资源的过

启用 Open vSwitch - 每天5分钟玩转 OpenStack(127)

Linux Bridge 和 Open vSwitch 是目前 OpenStack 中使用最广泛的两种虚机交换机技术. 前面各章节我们已经学习了如何用 Linux Bridge 作为 ML2 mechanism driver 实现 Neutron 网络.今天开始我们将详细讨论如何用 Open vSwitch 实现 Neutron. 实验环境两节点的网卡分配方式与 Linux Bridge 一致,如下所示: 控制节点三个网卡(eth0, eth1, eth2),计算节点两网卡(eth0, eth1

Openstack(Kilo)安装系列之neutron(九)

控制节点 Before you configure the OpenStack Networking (neutron) service, you must create a database, service credentials, and API endpoint. 一.创建neutron数据库并授权 1.登陆数据库 mysql -u root -p 2.创建数据库并授权 CREATE DATABASE neutron; GRANT ALL PRIVILEGES ON neutron.*