1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2011 Citrix Systems, Inc.
4
# Copyright 2011 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
"""VIF drivers for VMWare."""
21
from nova import exception
22
from nova import flags
23
from nova import log as logging
24
from nova import utils
25
from nova.virt.vif import VIFDriver
26
from nova.virt.vmwareapi_conn import VMWareAPISession
27
from nova.virt.vmwareapi import network_utils
30
LOG = logging.getLogger("nova.virt.vmwareapi.vif")
35
class VMWareVlanBridgeDriver(VIFDriver):
36
"""VIF driver for Linux VLAN bridge."""
38
def plug(self, instance, network, mapping):
39
"""Create a vlan and bridge unless they already exist."""
40
vlan_num = network['vlan']
41
bridge = network['bridge']
42
bridge_interface = network['bridge_interface']
44
# Open vmwareapi session
45
host_ip = FLAGS.vmwareapi_host_ip
46
host_username = FLAGS.vmwareapi_host_username
47
host_password = FLAGS.vmwareapi_host_password
48
if not host_ip or host_username is None or host_password is None:
49
raise Exception(_('Must specify vmwareapi_host_ip, '
50
'vmwareapi_host_username '
51
'and vmwareapi_host_password to use '
52
'connection_type=vmwareapi'))
53
session = VMWareAPISession(host_ip, host_username, host_password,
54
FLAGS.vmwareapi_api_retry_count)
55
vlan_interface = bridge_interface
56
# Check if the vlan_interface physical network adapter exists on the host
57
if not network_utils.check_if_vlan_interface_exists(session,
59
raise exception.NetworkAdapterNotFound(adapter=vlan_interface)
61
# Get the vSwitch associated with the Physical Adapter
62
vswitch_associated = network_utils.get_vswitch_for_vlan_interface(
63
session, vlan_interface)
64
if vswitch_associated is None:
65
raise exception.SwicthNotFoundForNetworkAdapter(adapter=vlan_interface)
66
# Check whether bridge already exists and retrieve the the ref of the
67
# network whose name_label is "bridge"
68
network_ref = network_utils.get_network_with_the_name(session, bridge)
69
if network_ref is None:
70
# Create a port group on the vSwitch associated with the vlan_interface
71
# corresponding physical network adapter on the ESX host
72
network_utils.create_port_group(session, bridge, vswitch_associated,
75
# Get the vlan id and vswitch corresponding to the port group
76
pg_vlanid, pg_vswitch = \
77
network_utils.get_vlanid_and_vswitch_for_portgroup(session, bridge)
79
# Check if the vswitch associated is proper
80
if pg_vswitch != vswitch_associated:
81
raise exception.InvalidVLANPortGroup(bridge=bridge,
82
expected=vswitch_associated,
85
# Check if the vlan id is proper for the port group
86
if pg_vlanid != vlan_num:
87
raise exception.InvalidVLANTag(bridge=bridge, tag=vlan_num,
91
def unplug(self, instance, network, mapping):