Neutron-server初始化 — Neutron L3 Agent初始化(路由& NAT)

Neutron L3 Agent(Layer-3 Networking Extension)作爲一種API擴展(通過API來創建router或者floating ip,以提供路由以及NAT的功能),向租戶提供了路由和NAT功能。l3擴展包含兩種資源:
router:在不同內部子網中轉發數據包;通過指定內部網關做NAT。每一個子網對應router上的一個端口,這個端口的ip就是子網的網關。
floating ip:代表一個外部網絡的IP,映射到內部網絡的端口上。當網絡的router:external屬性爲True時,floating ip才能定義。
這兩種資源都對應有不同的屬性。支持CRUD操作。

初始化過程分析

既然neutron中支持了l3擴展,那麼怎樣通過API來創建router或者floating ip,以提供路由以及NAT的功能的呢?
主要有以下幾個步驟:
1. 租戶通過horizon,nova命令或者自定義的腳本,發送與router或floating ip相關的操作。
2. 這些API請求發送到neutron server,通過neutron提供的API extension相對應。
3. 實現這些API extension的操作,比如說create_router,則由具體的plugin和database來共同完成。
4. plugin會通過rpc機制與計算網絡節點上運行的l3 agent來執行l3 轉發和NAT的功能。
這裏寫圖片描述

l3.py
源代碼目錄:neutron/extensions/l3.py
這個模塊中,class RouterPluginBase定義了plugin中需要實現的方法。

class RouterPluginBase(object):

    @abc.abstractmethod
    def create_router(self, context, router):
        pass

    @abc.abstractmethod
    def update_router(self, context, id, router):
        pass

    @abc.abstractmethod
    def get_router(self, context, id, fields=None):
        pass

    @abc.abstractmethod
    def delete_router(self, context, id):
        pass

    @abc.abstractmethod
    def get_routers(self, context, filters=None, fields=None,
                    sorts=None, limit=None, marker=None, page_reverse=False):
        pass

    @abc.abstractmethod
    def add_router_interface(self, context, router_id, interface_info):
        pass

    @abc.abstractmethod
    def remove_router_interface(self, context, router_id, interface_info):
        pass

    @abc.abstractmethod
    def create_floatingip(self, context, floatingip):
        pass

    @abc.abstractmethod
    def update_floatingip(self, context, id, floatingip):
        pass

    @abc.abstractmethod
    def get_floatingip(self, context, id, fields=None):
        pass

    @abc.abstractmethod
    def delete_floatingip(self, context, id):
        pass

    @abc.abstractmethod
    def get_floatingips(self, context, filters=None, fields=None,
                        sorts=None, limit=None, marker=None,
                        page_reverse=False):
        pass

    def get_routers_count(self, context, filters=None):
        raise NotImplementedError()

    def get_floatingips_count(self, context, filters=None):
        raise NotImplementedError()

l3_db.py
這裏寫圖片描述
源碼目錄:/neutron/db/l3_db.py
這個模塊中,class L3_NAT_dbonly_mixin繼承了上面l3模塊的class RouterPluginBase,因此在RouterPluginBase中定義的抽象方法就要在這裏實現了。

L3_NAT_dbonly_mixin具體方法實現類

class L3_NAT_dbonly_mixin(l3.RouterPluginBase):
    """Mixin class to add L3/NAT router methods to db_base_plugin_v2."""

    router_device_owners = (
        DEVICE_OWNER_ROUTER_INTF,
        DEVICE_OWNER_ROUTER_GW,
        DEVICE_OWNER_FLOATINGIP
    )

    @property
    def _core_plugin(self):
        return manager.NeutronManager.get_plugin()

    def _get_router(self, context, router_id):
        try:
            router = self._get_by_id(context, Router, router_id)
        except exc.NoResultFound:
            raise l3.RouterNotFound(router_id=router_id)
        return router

    def _make_router_dict(self, router, fields=None, process_extensions=True):
        res = dict((key, router[key]) for key in CORE_ROUTER_ATTRS)
        if router['gw_port_id']:
            ext_gw_info = {
                'network_id': router.gw_port['network_id'],
                'external_fixed_ips': [{'subnet_id': ip["subnet_id"],
                                        'ip_address': ip["ip_address"]}
                                       for ip in router.gw_port['fixed_ips']]}
        else:
            ext_gw_info = None
        res.update({
            EXTERNAL_GW_INFO: ext_gw_info,
            'gw_port_id': router['gw_port_id'],
        })
        # NOTE(salv-orlando): The following assumes this mixin is used in a
        # class inheriting from CommonDbMixin, which is true for all existing
        # plugins.
        if process_extensions:
            self._apply_dict_extend_functions(l3.ROUTERS, res, router)
        return self._fields(res, fields)

    def _create_router_db(self, context, router, tenant_id):
        """Create the DB object."""
        with context.session.begin(subtransactions=True):
            # pre-generate id so it will be available when
            # configuring external gw port
            router_db = Router(id=(router.get('id') or
                                   uuidutils.generate_uuid()),
                               tenant_id=tenant_id,
                               name=router['name'],
                               admin_state_up=router['admin_state_up'],
                               status="ACTIVE")
            context.session.add(router_db)
            return router_db

L3RpcNotifierMixin

class L3RpcNotifierMixin(object):
    """Mixin class to add rpc notifier attribute to db_base_plugin_v2."""

    @property
    def l3_rpc_notifier(self):
        if not hasattr(self, '_l3_rpc_notifier'):
            self._l3_rpc_notifier = l3_rpc_agent_api.L3AgentNotifyAPI()
        return self._l3_rpc_notifier

    @l3_rpc_notifier.setter
    def l3_rpc_notifier(self, value):
        self._l3_rpc_notifier = value

    def notify_router_updated(self, context, router_id,
                              operation=None):
        if router_id:
            self.l3_rpc_notifier.routers_updated(
                context, [router_id], operation)

    def notify_routers_updated(self, context, router_ids,
                               operation=None, data=None):
        if router_ids:
            self.l3_rpc_notifier.routers_updated(
                context, router_ids, operation, data)

    def notify_router_deleted(self, context, router_id):
        self.l3_rpc_notifier.router_deleted(context, router_id)

L3_NAT_db_mixin
class L3_NAT_db_mixin(L3_NAT_dbonly_mixin, L3RpcNotifierMixin)

class L3_NAT_db_mixin(L3_NAT_dbonly_mixin, L3RpcNotifierMixin):
    """Mixin class to add rpc notifier methods to db_base_plugin_v2."""

    def update_router(self, context, id, router):
        router_dict = super(L3_NAT_db_mixin, self).update_router(context,
                                                                 id, router)
        self.notify_router_updated(context, router_dict['id'], None)
        return router_dict

    def delete_router(self, context, id):
        super(L3_NAT_db_mixin, self).delete_router(context, id)
        self.notify_router_deleted(context, id)

    def notify_router_interface_action(
            self, context, router_interface_info, action):
        l3_method = '%s_router_interface' % action
        super(L3_NAT_db_mixin, self).notify_routers_updated(
            context, [router_interface_info['id']], l3_method,
            {'subnet_id': router_interface_info['subnet_id']})

        mapping = {'add': 'create', 'remove': 'delete'}
        notifier = n_rpc.get_notifier('network')
        router_event = 'router.interface.%s' % mapping[action]
        notifier.info(context, router_event,
                      {'router_interface': router_interface_info})

    def add_router_interface(self, context, router_id, interface_info):
        router_interface_info = super(
            L3_NAT_db_mixin, self).add_router_interface(
                context, router_id, interface_info)
        self.notify_router_interface_action(
            context, router_interface_info, 'add')
        return router_interface_info

    def remove_router_interface(self, context, router_id, interface_info):
        router_interface_info = super(
            L3_NAT_db_mixin, self).remove_router_interface(
                context, router_id, interface_info)
        self.notify_router_interface_action(
            context, router_interface_info, 'remove')
        return router_interface_info

    def create_floatingip(self, context, floatingip,
            initial_status=l3_constants.FLOATINGIP_STATUS_ACTIVE):
        floatingip_dict = super(L3_NAT_db_mixin, self).create_floatingip(
            context, floatingip, initial_status)
        router_id = floatingip_dict['router_id']
        self.notify_router_updated(context, router_id, 'create_floatingip')
        return floatingip_dict

    def update_floatingip(self, context, id, floatingip):
        old_floatingip, floatingip = self._update_floatingip(
            context, id, floatingip)
        router_ids = self._floatingips_to_router_ids(
            [old_floatingip, floatingip])
        super(L3_NAT_db_mixin, self).notify_routers_updated(
            context, router_ids, 'update_floatingip', {})
        return floatingip

    def delete_floatingip(self, context, id):
        floating_ip = self._delete_floatingip(context, id)
        self.notify_router_updated(context, floating_ip['router_id'],
                                   'delete_floatingip')

    def disassociate_floatingips(self, context, port_id, do_notify=True):
        """Disassociate all floating IPs linked to specific port.

        @param port_id: ID of the port to disassociate floating IPs.
        @param do_notify: whether we should notify routers right away.
        @return: set of router-ids that require notification updates
                 if do_notify is False, otherwise None.
        """
        router_ids = super(L3_NAT_db_mixin, self).disassociate_floatingips(
            context, port_id)
        if do_notify:
            self.notify_routers_updated(context, router_ids)
            # since caller assumes that we handled notifications on its
            # behalf, return nothing
            return

        return router_ids

    def notify_routers_updated(self, context, router_ids):
        super(L3_NAT_db_mixin, self).notify_routers_updated(
            context, list(router_ids), 'disassociate_floatingips', {})

類註釋中寫道,Mixin class to add L3/NAT router methods to db_plugin_base_v2。在類L3RpcNotifierMixin的開始,有這樣一段代碼:
self._l3_rpc_notifier = l3_rpc_agent_api.L3AgentNotifyAPI()

class L3RpcNotifierMixin(object):
    """Mixin class to add rpc notifier attribute to db_base_plugin_v2."""

    @property
    def l3_rpc_notifier(self):
        if not hasattr(self, '_l3_rpc_notifier'):
            self._l3_rpc_notifier = l3_rpc_agent_api.L3AgentNotifyAPI()
        return self._l3_rpc_notifier

說明l3_rpc_notifier,是模塊l3_rpc_agent_api中類L3AgentNotifyAPI的一個實例。

l3_rpc_agent_api
/neutron/api/rpc/agentnotifiers/l3_rpc_agent_api.py。
這個類主要用於plugin發送rpc通知給l3 agent。

class L3AgentNotifyAPI(object):
    """API for plugin to notify L3 agent."""

    def __init__(self, topic=topics.L3_AGENT):
        target = oslo_messaging.Target(topic=topic, version='1.0')
        self.client = n_rpc.get_client(target)

    def _notification_host(self, context, method, host, use_call=False,
                           **kwargs):
        """Notify the agent that is hosting the router."""
        LOG.debug('Notify agent at %(host)s the message '
                  '%(method)s', {'host': host,
                                 'method': method})
        cctxt = self.client.prepare(server=host)
        rpc_method = cctxt.call if use_call else cctxt.cast
        rpc_method(context, method, **kwargs)

    def _agent_notification(self, context, method, router_ids, operation,
                            shuffle_agents):
        """Notify changed routers to hosting l3 agents."""
        adminContext = context if context.is_admin else context.elevated()
        plugin = manager.NeutronManager.get_service_plugins().get(
            service_constants.L3_ROUTER_NAT)
        state = agentschedulers_db.get_admin_state_up_filter()
        for router_id in router_ids:
            l3_agents = plugin.get_l3_agents_hosting_routers(
                adminContext, [router_id],
                admin_state_up=state,
                active=True)
            if shuffle_agents:
                random.shuffle(l3_agents)
            for l3_agent in l3_agents:
                LOG.debug('Notify agent at %(topic)s.%(host)s the message '
                          '%(method)s',
                          {'topic': l3_agent.topic,
                           'host': l3_agent.host,
                           'method': method})
                cctxt = self.client.prepare(topic=l3_agent.topic,
                                            server=l3_agent.host,
                                            version='1.1')
                cctxt.cast(context, method, routers=[router_id])

    def _agent_notification_arp(self, context, method, router_id,
                                operation, data):
        """Notify arp details to l3 agents hosting router."""
        if not router_id:
            return
        adminContext = (context.is_admin and
                        context or context.elevated())
        plugin = manager.NeutronManager.get_service_plugins().get(
            service_constants.L3_ROUTER_NAT)
        state = agentschedulers_db.get_admin_state_up_filter()
        l3_agents = (plugin.
                     get_l3_agents_hosting_routers(adminContext,
                                                   [router_id],
                                                   admin_state_up=state,
                                                   active=True))
        # TODO(murali): replace cast with fanout to avoid performance
        # issues at greater scale.
        for l3_agent in l3_agents:
            log_topic = '%s.%s' % (l3_agent.topic, l3_agent.host)
            LOG.debug('Casting message %(method)s with topic %(topic)s',
                      {'topic': log_topic, 'method': method})
            dvr_arptable = {'router_id': router_id,
                            'arp_table': data}
            cctxt = self.client.prepare(topic=l3_agent.topic,
                                        server=l3_agent.host,
                                        version='1.2')
            cctxt.cast(context, method, payload=dvr_arptable)

    def _notification(self, context, method, router_ids, operation,
                      shuffle_agents, schedule_routers=True):
        """Notify all the agents that are hosting the routers."""
        plugin = manager.NeutronManager.get_service_plugins().get(
            service_constants.L3_ROUTER_NAT)
        if not plugin:
            LOG.error(_LE('No plugin for L3 routing registered. Cannot notify '
                          'agents with the message %s'), method)
            return
        if utils.is_extension_supported(
                plugin, constants.L3_AGENT_SCHEDULER_EXT_ALIAS):
            adminContext = (context.is_admin and
                            context or context.elevated())
            if schedule_routers:
                plugin.schedule_routers(adminContext, router_ids)
            self._agent_notification(
                context, method, router_ids, operation, shuffle_agents)
        else:
            cctxt = self.client.prepare(fanout=True)
            cctxt.cast(context, method, routers=router_ids)

    def _notification_fanout(self, context, method, router_id):
        """Fanout the deleted router to all L3 agents."""
        LOG.debug('Fanout notify agent at %(topic)s the message '
                  '%(method)s on router %(router_id)s',
                  {'topic': topics.L3_AGENT,
                   'method': method,
                   'router_id': router_id})
        cctxt = self.client.prepare(fanout=True)
        cctxt.cast(context, method, router_id=router_id)

    def agent_updated(self, context, admin_state_up, host):
        self._notification_host(context, 'agent_updated', host,
                                payload={'admin_state_up': admin_state_up})

    def router_deleted(self, context, router_id):
        self._notification_fanout(context, 'router_deleted', router_id)

    def routers_updated(self, context, router_ids, operation=None, data=None,
                        shuffle_agents=False, schedule_routers=True):
        if router_ids:
            self._notification(context, 'routers_updated', router_ids,
                               operation, shuffle_agents, schedule_routers)

    def add_arp_entry(self, context, router_id, arp_table, operation=None):
        self._agent_notification_arp(context, 'add_arp_entry', router_id,
                                     operation, arp_table)

    def del_arp_entry(self, context, router_id, arp_table, operation=None):
        self._agent_notification_arp(context, 'del_arp_entry', router_id,
                                     operation, arp_table)

    def router_removed_from_agent(self, context, router_id, host):
        self._notification_host(context, 'router_removed_from_agent', host,
                                payload={'router_id': router_id})

    def router_added_to_agent(self, context, router_ids, host):
        # need to use call here as we want to be sure agent received
        # notification and router will not be "lost". However using call()
        # itself is not a guarantee, calling code should handle exceptions and
        # retry
        self._notification_host(context, 'router_added_to_agent', host,
                                use_call=True, payload=router_ids)

    def routers_updated_on_host(self, context, router_ids, host):
        self._notification_host(context, 'routers_updated', host,
                                routers=router_ids)

RPC調用處理
在上面的l3_db.py中,會將涉及router和floating ip的處理讀取或者寫入到數據中。但是還有一些操作不僅如此,還需要通過rpc(通過調用l3_rpc_agent_api中的函數,這些操作大部分會去 調用routers_updated),通知l3 agent進行處理。
這些需要處理的地方包括(對router,floating_ip,interface的刪del、改update操作):update_router,delete_router,add_router_interface,remove_router_interface,create_floatingip,update_floatingip,delete_floatingip,disassociate_floatingips 等操作。

l3_agent.py
源碼目錄:neutron/agent/l3_agent.py
l3 agent使用Linux ip協議棧和iptables來實現router和NAT的功能。

這時候,如果在horizon的界面創建一個路由,不進行任何操作的話,plugin只會操作數據庫,l3 agent不會作處理。而當update router,如設置外部網關時,l3纔會去處理請求。

l3 agent使用service框架啓動服務,其manager類爲
neutron.agent.l3_agent.L3NATAgentWithStateReport,該類繼承自L3NATAgent,主要實現了基於rpc的_report_state向PluginReportStateAPI(topic爲q-plugin)彙報狀態信息,這些信息由各個plugin來處理(比如ml2中通過start_rpc_listeners來註冊該topic的消費者)。
L3NATAgent類是最主要的L3 Manager類,該類繼承關係爲
class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback, manager.Manager);
FWaaSL3AgentRpcCallback主要是加載防火牆驅動FWaaS Driver,並創建RPC與Plugin通信。
再來看L3NATAgent的創建過程:

class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback,
                 ha.AgentMixin,
                 dvr.AgentMixin,
                 manager.Manager):
    """Manager for L3NatAgent

        API version history:
        1.0 initial Version
        1.1 changed the type of the routers parameter
            to the routers_updated method.
            It was previously a list of routers in dict format.
            It is now a list of router IDs only.
            Per rpc versioning rules,  it is backwards compatible.
        1.2 - DVR support: new L3 agent methods added.
              - add_arp_entry
              - del_arp_entry
              Needed by the L3 service when dealing with DVR
    """

L3NATAgent
該類是最主要的L3 Manager類
這裏寫圖片描述
其中:

self.plugin_rpc = L3PluginApi(topics.L3PLUGIN, host)

該self.plugin_rpc會處理neutron-server轉發過來的請求,這個請求是通過service_plugins的方式處理的:

neutron.service_plugins = 
    dummy = neutron.tests.unit.dummy_plugin:DummyServicePlugin
    router = neutron.services.l3_router.l3_router_plugin:L3RouterPlugin
    bigswitch_l3 = neutron.plugins.bigswitch.l3_router_plugin:L3RestProxy
    brocade_vyatta_l3 = neutron.services.l3_router.brocade.vyatta.vrouter_neutron_plugin:VyattaVRouterPlugin
    brocade_mlx_l3 = neutron.services.l3_router.brocade.mlx.l3_router_plugin:BrocadeRouterPlugin
    firewall = neutron_fwaas.services.firewall.fwaas_plugin:FirewallPlugin
    fsl_firewall = neutron_fwaas.services.firewall.freescale.fwaas_plugin:FirewallPlugin
    lbaas = neutron_lbaas.services.loadbalancer.plugin:LoadBalancerPlugin
    vpnaas = neutron_vpnaas.services.vpn.plugin:VPNDriverPlugin
    metering = neutron.services.metering.metering_plugin:MeteringPlugin
    neutron.services.firewall.fwaas_plugin.FirewallPlugin = neutron_fwaas.services.firewall.fwaas_plugin:FirewallPlugin
    neutron.services.loadbalancer.plugin.LoadBalancerPlugin = neutron_lbaas.services.loadbalancer.plugin:LoadBalancerPlugin
    neutron.services.vpn.plugin.VPNDriverPlugin = neutron_vpnaas.services.vpn.plugin:VPNDriverPlugin
    ibm_l3 = neutron.services.l3_router.l3_sdnve:SdnveL3ServicePlugin
    qos = neutron.services.qos.qos_plugin:QoSPlugin

其中:
文件路徑: neutron/agent/l3/agent.py
L3NATAgentWithStateReport

class L3NATAgentWithStateReport(L3NATAgent):

    def __init__(self, host, conf=None):
        super(L3NATAgentWithStateReport, self).__init__(host=host, conf=conf)
        self.state_rpc = agent_rpc.PluginReportStateAPI(topics.PLUGIN)
        self.agent_state = {
            'binary': 'neutron-l3-agent',
            'host': host,
            'topic': topics.L3_AGENT,
            'configurations': {
                'agent_mode': self.conf.agent_mode,
                'use_namespaces': self.conf.use_namespaces,
                'router_id': self.conf.router_id,
                'handle_internal_only_routers':
                self.conf.handle_internal_only_routers,
                'external_network_bridge': self.conf.external_network_bridge,
                'gateway_external_network_id':
                self.conf.gateway_external_network_id,
                'interface_driver': self.conf.interface_driver,
                'log_agent_heartbeats': self.conf.AGENT.log_agent_heartbeats},
            'start_flag': True,
            'agent_type': l3_constants.AGENT_TYPE_L3}
        report_interval = self.conf.AGENT.report_interval
        if report_interval:
            # self.heartbeat會循環檢測從plugin發送過來的rpc請求:
            self.heartbeat = loopingcall.FixedIntervalLoopingCall(
                self._report_state)
            self.heartbeat.start(interval=report_interval)

_process_routers_loop

    def _process_routers_loop(self):
        LOG.debug("Starting _process_routers_loop")
        pool = eventlet.GreenPool(size=8)
        while True:
            pool.spawn_n(self._process_router_update)

_process_routers_loop
如果有rpc請求過來,即需要更新路由信息,或者添加路由子接口,創建floating ip等操作,都會在這裏執行。這個函數裏會去調用_process_routers_loop函數,在_process_routers_loop函數中會去創建綠色線程,執行_process_router_update函數。可以說,l3 agent調用網絡設備的工作都會在_process_router_update中進行。

_process_router_update

    def _process_router_update(self):
        for rp, update in self._queue.each_update_to_next_router():
            LOG.debug("Starting router update for %s, action %s, priority %s",
                      update.id, update.action, update.priority)
            if update.action == queue.PD_UPDATE:
                self.pd.process_prefix_update()
                LOG.debug("Finished a router update for %s", update.id)
                continue
            router = update.router
            if update.action != queue.DELETE_ROUTER and not router:
                try:
                    update.timestamp = timeutils.utcnow()
                    routers = self.plugin_rpc.get_routers(self.context,
                                                          [update.id])
                except Exception:
                    msg = _LE("Failed to fetch router information for '%s'")
                    LOG.exception(msg, update.id)
                    self._resync_router(update)
                    continue

                if routers:
                    router = routers[0]

            if not router:
                removed = self._safe_router_removed(update.id)
                if not removed:
                    self._resync_router(update)
                else:
                    # need to update timestamp of removed router in case
                    # there are older events for the same router in the
                    # processing queue (like events from fullsync) in order to
                    # prevent deleted router re-creation
                    rp.fetched_and_processed(update.timestamp)
                LOG.debug("Finished a router update for %s", update.id)
                continue

            try:
                self._process_router_if_compatible(router)
            except n_exc.RouterNotCompatibleWithAgent as e:
                LOG.exception(e.msg)
                # Was the router previously handled by this agent?
                if router['id'] in self.router_info:
                    LOG.error(_LE("Removing incompatible router '%s'"),
                              router['id'])
                    self._safe_router_removed(router['id'])
            except Exception:
                msg = _LE("Failed to process compatible router '%s'")
                LOG.exception(msg, update.id)
                self._resync_router(update)
                continue

            LOG.debug("Finished a router update for %s", update.id)
            rp.fetched_and_processed(update.timestamp)

_process_router_update函數所做的工作有:
1.處理內部接口
這個是在router添加和刪除子接口時工作。它會調用internal_network_added和internal_network_removed這個兩個函數。
在internal_network_added和internal_network_removed這個兩個函數會去調用OVSInterfaceDriver的plug和unplug 函數,這兩個函數最終會用ip link 和ip addr的命令去處理接口和ip地址。

2.處理外部網關
router添加和刪除外部網關。調用external_gateway_added和external_gateway_removed函數,同樣也會調用plug和unplug函數,用ip link 和ip addr的命令進行最終處理

3.爲外部網關做SNAT
調用_handle_router_snat_rules函數,使用iptables來加鏈和刪除鏈。

在我的測試網絡中,router上有3個接口,外部網關地址爲192.168.39.2,內部兩個子網的網關爲10.1.0.1,10.2.0.1。iptables規則如下:

iptables -t nat -A POSTROUTING ! -i qg-fcb1a762-1f ! -o qg-fcb1a762-1f -m conntrack ! --ctstate DNAT -j ACCEPT
iptables -t nat -A snat -s 10.2.0.1/24 -j SNAT --to-source 192.168.39.2
iptables -t nat -A snat -s 10.1.0.1/24 -j SNAT --to-source 192.168.39.2

qg-fcb1a762-1f爲外部網關接口的索引,使用ip netns exec $namespace ip link list可查看。

4.爲floating ip做SNAT/DNAT

和浮動IP相關,如創建,更新,刪除,綁定到一個雲主機的接口,解綁定等。

不同neutron版本這部分的處理不同,這裏是基於Icehouse rc1版本的,在havava stable版本,只有一個函數來處理iptables規則和floating ip。

process_router_floating_ip_nat_rules :當floating ip與雲主機綁定時,會先清除已有的floating_ip規則,再加上要添加的iptables規則,同時重新加載清除的iptables規則。

比如,一個雲主機10.1.0.2上綁定了一個floating ip(192.168.39.5)。那麼最終會在iptable不同的鏈中添加iptables規則,float-snat爲neutron自定義鏈。

iptables -t nat -A PREROUTING -d 192.168.39.5 -j DNAT --to 10.1.0.2
iptables -t nat -A OUTPUT -d 192.168.39.5 -j DNAT --to 10.1.0.2
iptables -t nat -A float-snat -s 10.1.0.2 -j SNAT --to 192.168.39.5

process_router_floating_ip_addresses:
將floating ip和雲主機綁定時,使用ip addr add命令添加ip地址。
解除floating ip和雲主機綁定時,使用ip addr del命令將floating ip刪除。
這裏寫圖片描述

參考:
about雲: http://www.aboutyun.com/thread-9529-1-1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章