~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/virt/libvirt/vif.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from nova.virt import netutils
29
29
from nova.virt import vif
30
30
 
 
31
from nova.virt.libvirt import config
31
32
 
32
33
LOG = logging.getLogger(__name__)
33
34
 
34
 
libvirt_ovs_bridge_opt = cfg.StrOpt('libvirt_ovs_bridge',
 
35
libvirt_vif_opts = [
 
36
    cfg.StrOpt('libvirt_ovs_bridge',
35
37
        default='br-int',
36
 
        help='Name of Integration Bridge used by Open vSwitch')
 
38
        help='Name of Integration Bridge used by Open vSwitch'),
 
39
    cfg.BoolOpt('libvirt_use_virtio_for_bridges',
 
40
                default=False,
 
41
                help='Use virtio for bridge interfaces'),
 
42
]
37
43
 
38
44
FLAGS = flags.FLAGS
39
 
FLAGS.register_opt(libvirt_ovs_bridge_opt)
 
45
FLAGS.register_opts(libvirt_vif_opts)
 
46
flags.DECLARE('libvirt_type', 'nova.virt.libvirt.connection')
40
47
 
41
48
 
42
49
class LibvirtBridgeDriver(vif.VIFDriver):
43
50
    """VIF driver for Linux bridge."""
44
51
 
45
 
    def _get_configurations(self, network, mapping):
 
52
    def _get_configurations(self, instance, network, mapping):
46
53
        """Get a dictionary of VIF configurations for bridge type."""
47
 
        # Assume that the gateway also acts as the dhcp server.
48
 
        gateway_v6 = mapping.get('gateway_v6')
 
54
 
49
55
        mac_id = mapping['mac'].replace(':', '')
50
56
 
 
57
        conf = config.LibvirtConfigGuestInterface()
 
58
        conf.net_type = "bridge"
 
59
        conf.mac_addr = mapping['mac']
 
60
        conf.source_dev = network['bridge']
 
61
        conf.script = ""
 
62
        if FLAGS.libvirt_use_virtio_for_bridges:
 
63
            conf.model = "virtio"
 
64
 
 
65
        conf.filtername = "nova-instance-" + instance['name'] + "-" + mac_id
 
66
        conf.add_filter_param("IP", mapping['ips'][0]['ip'])
 
67
        if mapping['dhcp_server']:
 
68
            conf.add_filter_param("DHCPSERVER", mapping['dhcp_server'])
 
69
 
 
70
        if FLAGS.use_ipv6:
 
71
            conf.add_filter_param("RASERVER",
 
72
                                  mapping.get('gateway_v6') + "/128")
 
73
 
51
74
        if FLAGS.allow_same_net_traffic:
52
 
            template = "<parameter name=\"%s\" value=\"%s\" />\n"
53
75
            net, mask = netutils.get_net_and_mask(network['cidr'])
54
 
            values = [("PROJNET", net), ("PROJMASK", mask)]
 
76
            conf.add_filter_param("PROJNET", net)
 
77
            conf.add_filter_param("PROJMASK", mask)
55
78
            if FLAGS.use_ipv6:
56
79
                net_v6, prefixlen_v6 = netutils.get_net_and_prefixlen(
57
80
                                           network['cidr_v6'])
58
 
                values.extend([("PROJNETV6", net_v6),
59
 
                               ("PROJMASKV6", prefixlen_v6)])
60
 
 
61
 
            extra_params = "".join([template % value for value in values])
62
 
        else:
63
 
            extra_params = "\n"
64
 
 
65
 
        result = {
66
 
            'id': mac_id,
67
 
            'bridge_name': network['bridge'],
68
 
            'mac_address': mapping['mac'],
69
 
            'ip_address': mapping['ips'][0]['ip'],
70
 
            'dhcp_server': mapping['dhcp_server'],
71
 
            'extra_params': extra_params,
72
 
        }
73
 
 
74
 
        if gateway_v6:
75
 
            result['gateway_v6'] = gateway_v6 + "/128"
76
 
 
77
 
        return result
 
81
                conf.add_filter_param("PROJNET6", net_v6)
 
82
                conf.add_filter_param("PROJMASK6", prefixlen_v6)
 
83
 
 
84
        return conf
78
85
 
79
86
    def plug(self, instance, network, mapping):
80
87
        """Ensure that the bridge exists, and add VIF to it."""
84
91
                iface = FLAGS.vlan_interface or network['bridge_interface']
85
92
                LOG.debug(_('Ensuring vlan %(vlan)s and bridge %(bridge)s'),
86
93
                          {'vlan': network['vlan'],
87
 
                           'bridge': network['bridge']})
 
94
                           'bridge': network['bridge']},
 
95
                          instance=instance)
88
96
                linux_net.LinuxBridgeInterfaceDriver.ensure_vlan_bridge(
89
97
                                             network['vlan'],
90
98
                                             network['bridge'],
91
99
                                             iface)
92
100
            else:
93
101
                iface = FLAGS.flat_interface or network['bridge_interface']
94
 
                LOG.debug(_("Ensuring bridge %s"), network['bridge'])
 
102
                LOG.debug(_("Ensuring bridge %s"), network['bridge'],
 
103
                          instance=instance)
95
104
                linux_net.LinuxBridgeInterfaceDriver.ensure_bridge(
96
105
                                        network['bridge'],
97
106
                                        iface)
98
107
 
99
 
        return self._get_configurations(network, mapping)
 
108
        return self._get_configurations(instance, network, mapping)
100
109
 
101
110
    def unplug(self, instance, network, mapping):
102
111
        """No manual unplugging required."""
139
148
                "external-ids:vm-uuid=%s" % instance['uuid'],
140
149
                run_as_root=True)
141
150
 
142
 
        result = {
143
 
            'script': '',
144
 
            'name': dev,
145
 
            'mac_address': mapping['mac']}
146
 
        return result
 
151
        conf = config.LibvirtConfigGuestInterface()
 
152
 
 
153
        conf.net_type = "ethernet"
 
154
        conf.target_dev = dev
 
155
        conf.script = ""
 
156
        conf.mac_addr = mapping['mac']
 
157
 
 
158
        return conf
147
159
 
148
160
    def unplug(self, instance, network, mapping):
149
161
        """Unplug the VIF from the network by deleting the port from
154
166
                          FLAGS.libvirt_ovs_bridge, dev, run_as_root=True)
155
167
            utils.execute('ip', 'link', 'delete', dev, run_as_root=True)
156
168
        except exception.ProcessExecutionError:
157
 
            LOG.exception(_("Failed while unplugging vif of instance '%s'"),
158
 
                        instance['name'])
 
169
            LOG.exception(_("Failed while unplugging vif"), instance=instance)
159
170
 
160
171
 
161
172
class LibvirtOpenVswitchVirtualPortDriver(vif.VIFDriver):
164
175
 
165
176
    def plug(self, instance, network, mapping):
166
177
        """ Pass data required to create OVS virtual port element"""
167
 
        return {
168
 
            'bridge_name': FLAGS.libvirt_ovs_bridge,
169
 
            'ovs_interfaceid': mapping['vif_uuid'],
170
 
            'mac_address': mapping['mac']}
 
178
 
 
179
        conf = config.LibvirtConfigGuestInterface()
 
180
 
 
181
        conf.net_type = "bridge"
 
182
        conf.source_dev = FLAGS.libvirt_ovs_bridge
 
183
        conf.mac_addr = mapping['mac']
 
184
        if FLAGS.libvirt_use_virtio_for_bridges:
 
185
            conf.model = "virtio"
 
186
        conf.vporttype = "openvswitch"
 
187
        conf.add_vport_param("interfaceid", mapping['vif_uuid'])
 
188
 
 
189
        return conf
171
190
 
172
191
    def unplug(self, instance, network, mapping):
173
192
        """No action needed.  Libvirt takes care of cleanup"""
183
202
    def plug(self, instance, network, mapping):
184
203
        iface_id = mapping['vif_uuid']
185
204
        dev = self.get_dev_name(iface_id)
186
 
        linux_net.QuantumLinuxBridgeInterfaceDriver.create_tap_dev(dev)
187
 
 
188
 
        result = {
189
 
            'script': '',
190
 
            'name': dev,
191
 
            'mac_address': mapping['mac']}
192
 
        return result
 
205
 
 
206
        if FLAGS.libvirt_type != 'xen':
 
207
            linux_net.QuantumLinuxBridgeInterfaceDriver.create_tap_dev(dev)
 
208
 
 
209
        conf = config.LibvirtConfigGuestInterface()
 
210
 
 
211
        conf.net_type = "ethernet"
 
212
        conf.target_dev = dev
 
213
        conf.script = ""
 
214
        conf.mac_addr = mapping['mac']
 
215
 
 
216
        return conf
193
217
 
194
218
    def unplug(self, instance, network, mapping):
195
219
        """Unplug the VIF from the network by deleting the port from
198
222
        try:
199
223
            utils.execute('ip', 'link', 'delete', dev, run_as_root=True)
200
224
        except exception.ProcessExecutionError:
201
 
            LOG.warning(_("Failed while unplugging vif of instance '%s'"),
202
 
                        instance['name'])
 
225
            LOG.warning(_("Failed while unplugging vif"), instance=instance)
203
226
            raise