From 1da51f7b07f0080c44063a355c84fafd1fdf02bc Mon Sep 17 00:00:00 2001 From: Ryu Ishimoto Date: Mon, 11 Jul 2011 04:38:27 +0900 Subject: Moved 'setup_compute_network' logic into the virt layer --- nova/virt/libvirt/connection.py | 13 ++++-- nova/virt/libvirt/vif.py | 99 ++++++++++++++++++++++++++++++++++++++++ nova/virt/libvirt/vif_drivers.py | 76 ------------------------------ nova/virt/vif.py | 29 ++++++++++++ 4 files changed, 138 insertions(+), 79 deletions(-) create mode 100644 nova/virt/libvirt/vif.py delete mode 100644 nova/virt/libvirt/vif_drivers.py create mode 100644 nova/virt/vif.py (limited to 'nova/virt') diff --git a/nova/virt/libvirt/connection.py b/nova/virt/libvirt/connection.py index 606bc18bc..15ad85176 100644 --- a/nova/virt/libvirt/connection.py +++ b/nova/virt/libvirt/connection.py @@ -123,8 +123,8 @@ flags.DEFINE_bool('start_guests_on_host_boot', False, flags.DEFINE_string('libvirt_vif_type', 'bridge', 'Type of VIF to create.') flags.DEFINE_string('libvirt_vif_driver', - 'nova.virt.libvirt.vif_drivers.BridgeDriver', - 'The VIF driver to configure the VIFs.') + 'nova.virt.libvirt.vif.LibvirtVlanBridgeDriver', + 'The libvirt VIF driver to configure the VIFs.') def get_connection(read_only): @@ -259,6 +259,12 @@ class LibvirtConnection(driver.ComputeDriver): infos.append(info) return infos + def setup_vif_network(self, ctxt, instance_id): + """Set up VIF networking on the host.""" + networks = db.network_get_all_by_instance(ctxt, instance_id) + for network in networks: + self.vif_driver.plug(network) + def destroy(self, instance, cleanup=True): instance_name = instance['name'] @@ -950,7 +956,8 @@ class LibvirtConnection(driver.ComputeDriver): nics = [] for (network, mapping) in network_info: - nics.append(self.vif_driver(instance, network, mapping)) + nics.append(self.vif_driver.get_configurations(instance, network, + mapping)) # FIXME(vish): stick this in db inst_type_id = instance['instance_type_id'] inst_type = instance_types.get_instance_type(inst_type_id) diff --git a/nova/virt/libvirt/vif.py b/nova/virt/libvirt/vif.py new file mode 100644 index 000000000..50c2d444c --- /dev/null +++ b/nova/virt/libvirt/vif.py @@ -0,0 +1,99 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (C) 2011 Midokura KK +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""VIF drivers for libvirt.""" + +from nova import flags +from nova.network import linux_net +from nova.virt.libvirt import netutils +from nova.virt.vif import VIFDriver + +FLAGS = flags.FLAGS +flags.DEFINE_bool('allow_project_net_traffic', + True, + 'Whether to allow in project network traffic') + + +class LibvirtVIF(object): + """VIF class for libvirt""" + + def get_configurations(self, instance, network, mapping): + """Get a dictionary of VIF configuration for libvirt interfaces.""" + raise NotImplementedError() + + +class LibvirtBridge(LibvirtVIF): + """Linux bridge VIF for Libvirt.""" + + def get_configurations(self, instance, network, mapping): + """Get a dictionary of VIF configurations for bridge type.""" + # Assume that the gateway also acts as the dhcp server. + dhcp_server = mapping['gateway'] + gateway6 = mapping.get('gateway6') + mac_id = mapping['mac'].replace(':', '') + + if FLAGS.allow_project_net_traffic: + template = "\n" + net, mask = netutils.get_net_and_mask(network['cidr']) + values = [("PROJNET", net), ("PROJMASK", mask)] + if FLAGS.use_ipv6: + net_v6, prefixlen_v6 = netutils.get_net_and_prefixlen( + network['cidr_v6']) + values.extend([("PROJNETV6", net_v6), + ("PROJMASKV6", prefixlen_v6)]) + + extra_params = "".join([template % value for value in values]) + else: + extra_params = "\n" + + result = { + 'id': mac_id, + 'bridge_name': network['bridge'], + 'mac_address': mapping['mac'], + 'ip_address': mapping['ips'][0]['ip'], + 'dhcp_server': dhcp_server, + 'extra_params': extra_params, + } + + if gateway6: + result['gateway6'] = gateway6 + "/128" + + return result + + +class LibvirtBridgeDriver(VIFDriver, LibvirtBridge): + """VIF driver for Linux bridge.""" + + def plug(self, network): + """Ensure that the bridge exists, and add VIF to it.""" + linux_net.ensure_bridge(network['bridge'], + network['bridge_interface']) + + def unplug(self, network): + pass + + +class LibvirtVlanBridgeDriver(VIFDriver, LibvirtBridge): + """VIF driver for Linux bridge with VLAN.""" + + def plug(self, network): + """Ensure that VLAN and bridge exist and add VIF to the bridge.""" + linux_net.ensure_vlan_bridge(network['vlan'], network['bridge'], + network['bridge_interface']) + + def unplug(self, network): + pass diff --git a/nova/virt/libvirt/vif_drivers.py b/nova/virt/libvirt/vif_drivers.py deleted file mode 100644 index 9ef218a21..000000000 --- a/nova/virt/libvirt/vif_drivers.py +++ /dev/null @@ -1,76 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright (C) 2011 Midokura KK -# Administrator of the National Aeronautics and Space Administration. -# All Rights Reserved. -# Copyright (c) 2010 Citrix Systems, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -"""Drivers responsible for VIF creation in libvirt.""" - -from nova import flags -from nova.virt.libvirt import netutils - -FLAGS = flags.FLAGS -flags.DEFINE_bool('allow_project_net_traffic', - True, - 'Whether to allow in project network traffic') - - -class VIFDriver(object): - """Base class that defines generic interfaces for VIF drivers.""" - - def get_configuration(self, instance, network, mapping): - """Get a dictionary of VIF configuration for libvirt interfaces.""" - raise NotImplementedError() - - -class BridgeDriver(VIFDriver): - """Class that generates VIF configuration of bridge interface type.""" - - def get_configuration(self, instance, network, mapping): - """Get a dictionary of VIF configurations for bridge type.""" - # Assume that the gateway also acts as the dhcp server. - dhcp_server = mapping['gateway'] - gateway6 = mapping.get('gateway6') - mac_id = mapping['mac'].replace(':', '') - - if FLAGS.allow_project_net_traffic: - template = "\n" - net, mask = netutils.get_net_and_mask(network['cidr']) - values = [("PROJNET", net), ("PROJMASK", mask)] - if FLAGS.use_ipv6: - net_v6, prefixlen_v6 = netutils.get_net_and_prefixlen( - network['cidr_v6']) - values.extend([("PROJNETV6", net_v6), - ("PROJMASKV6", prefixlen_v6)]) - - extra_params = "".join([template % value for value in values]) - else: - extra_params = "\n" - - result = { - 'id': mac_id, - 'bridge_name': network['bridge'], - 'mac_address': mapping['mac'], - 'ip_address': mapping['ips'][0]['ip'], - 'dhcp_server': dhcp_server, - 'extra_params': extra_params, - } - - if gateway6: - result['gateway6'] = gateway6 + "/128" - - return result - diff --git a/nova/virt/vif.py b/nova/virt/vif.py new file mode 100644 index 000000000..1ff41ed7e --- /dev/null +++ b/nova/virt/vif.py @@ -0,0 +1,29 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright (C) 2011 Midokura KK +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""VIF module common to all virt layers.""" + +class VIFDriver(object): + """Abstract class that defines generic interfaces for all VIF drivers.""" + + def plug(self, network): + """Plug VIF into network.""" + raise NotImplementedError() + + def unplug(self, network): + """Unplug VIF from network.""" + raise NotImplementedError() -- cgit