| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import uuid |
| 14 |
|
| 15 |
from oslo_log import log as logging |
| 16 |
|
| 17 |
from vif_plug_ovs import constants |
| 18 |
from vif_plug_ovs import linux_net |
| 19 |
from vif_plug_ovs import ovs |
| 20 |
from vif_plug_ovs.ovsdb import api as ovsdb_api |
| 21 |
|
| 22 |
|
| 23 |
LOG = logging.getLogger(__name__) |
| 24 |
QOS_UUID_NAMESPACE = uuid.UUID("68da264a-847f-42a8-8ab0-5e774aee3d95") |
| 25 |
|
| 26 |
|
| 27 |
class BaseOVS(object): |
| 28 |
|
| 29 |
def __init__(self, config): |
| 30 |
self.timeout = config.ovs_vsctl_timeout |
| 31 |
self.connection = config.ovsdb_connection |
| 32 |
self.interface = config.ovsdb_interface |
| 33 |
self._ovsdb = None |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
@property |
| 41 |
def ovsdb(self): |
| 42 |
if not self._ovsdb: |
| 43 |
self._ovsdb = ovsdb_api.get_instance(self) |
| 44 |
return self._ovsdb |
| 45 |
|
| 46 |
def _ovs_supports_mtu_requests(self): |
| 47 |
return self.ovsdb.has_table_column('Interface', 'mtu_request') |
| 48 |
|
| 49 |
def _set_mtu_request(self, txn, dev, mtu): |
| 50 |
txn.add( |
| 51 |
self.ovsdb.db_set( |
| 52 |
'Interface', dev, ('mtu_request', mtu) |
| 53 |
) |
| 54 |
) |
| 55 |
|
| 56 |
def update_device_mtu(self, txn, dev, mtu, interface_type=None): |
| 57 |
if not mtu: |
| 58 |
return |
| 59 |
if interface_type not in [ |
| 60 |
constants.OVS_VHOSTUSER_INTERFACE_TYPE, |
| 61 |
constants.OVS_VHOSTUSER_CLIENT_INTERFACE_TYPE]: |
| 62 |
linux_net.set_device_mtu(dev, mtu) |
| 63 |
elif self._ovs_supports_mtu_requests(): |
| 64 |
self._set_mtu_request(txn, dev, mtu) |
| 65 |
else: |
| 66 |
LOG.debug("MTU not set on %(interface_name)s interface " |
| 67 |
"of type %(interface_type)s.", |
| 68 |
{'interface_name': dev, |
| 69 |
'interface_type': interface_type}) |
| 70 |
|
| 71 |
def ensure_ovs_bridge(self, bridge, datapath_type): |
| 72 |
return self.ovsdb.add_br(bridge, may_exist=True, |
| 73 |
datapath_type=datapath_type).execute() |
| 74 |
|
| 75 |
def delete_ovs_bridge(self, bridge): |
| 76 |
"""Delete ovs bridge by name |
| 77 |
|
| 78 |
:param bridge: bridge name as a string |
| 79 |
|
| 80 |
.. note:: Do Not call with br-int !!! |
| 81 |
""" |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
return self.ovsdb.del_br(bridge).execute() |
| 86 |
|
| 87 |
def create_patch_port_pair( |
| 88 |
self, port_bridge, port_bridge_port, int_bridge, int_bridge_port, |
| 89 |
iface_id, mac, instance_id, tag=None |
| 90 |
): |
| 91 |
"""Create a patch port pair between any two bridges. |
| 92 |
|
| 93 |
:param port_bridge: the source bridge name for the patch port pair. |
| 94 |
:param port_bridge_port: the name of the patch port on the |
| 95 |
source bridge. |
| 96 |
:param int_bridge: the target bridge name, typically br-int. |
| 97 |
:param int_bridge_port: the name of the patch port on the |
| 98 |
target bridge. |
| 99 |
:param iface_id: neutron port ID. |
| 100 |
:param mac: port MAC. |
| 101 |
:param instance_id: instance uuid. |
| 102 |
:param mtu: port MTU. |
| 103 |
:param tag: OVS interface tag used for vlan isolation. |
| 104 |
""" |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
with self.ovsdb.transaction() as txn: |
| 112 |
|
| 113 |
external_ids = { |
| 114 |
'iface-id': iface_id, 'iface-status': 'active', |
| 115 |
'attached-mac': mac, 'vm-uuid': instance_id |
| 116 |
} |
| 117 |
col_values = [ |
| 118 |
('external_ids', external_ids), |
| 119 |
('type', 'patch'), |
| 120 |
('options', {'peer': port_bridge_port}) |
| 121 |
] |
| 122 |
|
| 123 |
txn.add(self.ovsdb.add_port(int_bridge, int_bridge_port)) |
| 124 |
if tag: |
| 125 |
txn.add( |
| 126 |
self.ovsdb.db_set('Port', int_bridge_port, ('tag', tag))) |
| 127 |
txn.add( |
| 128 |
self.ovsdb.db_set('Interface', int_bridge_port, *col_values)) |
| 129 |
|
| 130 |
|
| 131 |
col_values = [ |
| 132 |
('type', 'patch'), |
| 133 |
('options', {'peer': int_bridge_port}) |
| 134 |
] |
| 135 |
txn.add(self.ovsdb.add_port(port_bridge, port_bridge_port)) |
| 136 |
txn.add( |
| 137 |
self.ovsdb.db_set('Interface', port_bridge_port, *col_values)) |
| 138 |
|
| 139 |
def create_ovs_vif_port( |
| 140 |
self, bridge, dev, iface_id, mac, instance_id, |
| 141 |
mtu=None, interface_type=None, vhost_server_path=None, |
| 142 |
tag=None, pf_pci=None, vf_num=None, set_ids=True, datapath_type=None, |
| 143 |
qos_type=None, vlan_mode=None, trunks=None |
| 144 |
): |
| 145 |
"""Create OVS port |
| 146 |
|
| 147 |
:param bridge: bridge name to create the port on. |
| 148 |
:param dev: port name. |
| 149 |
:param iface_id: port ID. |
| 150 |
:param mac: port MAC. |
| 151 |
:param instance_id: VM ID on which the port is attached to. |
| 152 |
:param mtu: port MTU. |
| 153 |
:param interface_type: OVS interface type. |
| 154 |
:param vhost_server_path: path to socket file of vhost server. |
| 155 |
:param tag: OVS interface tag. |
| 156 |
:param pf_pci: PCI address of PF for dpdk representor port. |
| 157 |
:param vf_num: VF number of PF for dpdk representor port. |
| 158 |
:param set_ids: set external ids on port (bool). |
| 159 |
:param datapath_type: datapath type for port's bridge |
| 160 |
:param qos_type: qos type for a port |
| 161 |
|
| 162 |
.. note:: create DPDK representor port by setting all three values: |
| 163 |
`interface_type`, `pf_pci` and `vf_num`. if interface type is |
| 164 |
not `OVS_DPDK_INTERFACE_TYPE` then `pf_pci` and `vf_num` values |
| 165 |
are ignored. |
| 166 |
""" |
| 167 |
external_ids = {'iface-id': iface_id, |
| 168 |
'iface-status': 'active', |
| 169 |
'attached-mac': mac, |
| 170 |
'vm-uuid': instance_id} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if ovs.is_trunk_bridge(bridge): |
| 184 |
external_ids['bridge_name'] = bridge |
| 185 |
|
| 186 |
col_values = [('external_ids', external_ids)] if set_ids else [] |
| 187 |
if interface_type: |
| 188 |
col_values.append(('type', interface_type)) |
| 189 |
if vhost_server_path: |
| 190 |
col_values.append(('options', |
| 191 |
{'vhost-server-path': vhost_server_path})) |
| 192 |
if (interface_type == constants.OVS_DPDK_INTERFACE_TYPE and |
| 193 |
pf_pci and vf_num): |
| 194 |
devargs_string = "{PF_PCI},representor=[{VF_NUM}]".format( |
| 195 |
PF_PCI=pf_pci, VF_NUM=vf_num) |
| 196 |
col_values.append(('options', |
| 197 |
{'dpdk-devargs': devargs_string})) |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
qid = None |
| 206 |
if qos_type: |
| 207 |
self.delete_qos_if_exists(dev, qos_type) |
| 208 |
qos_id = uuid.uuid5(QOS_UUID_NAMESPACE, dev) |
| 209 |
qos_external_ids = {'id': str(qos_id), '_type': qos_type} |
| 210 |
self.ovsdb.db_create( |
| 211 |
'QoS', type=qos_type, external_ids=qos_external_ids |
| 212 |
).execute(check_error=True) |
| 213 |
record = self.get_qos(dev, qos_type) |
| 214 |
qid = record[0]['_uuid'] |
| 215 |
|
| 216 |
with self.ovsdb.transaction() as txn: |
| 217 |
if datapath_type: |
| 218 |
txn.add(self.ovsdb.add_br(bridge, may_exist=True, |
| 219 |
datapath_type=datapath_type)) |
| 220 |
txn.add(self.ovsdb.add_port(bridge, dev)) |
| 221 |
if tag: |
| 222 |
txn.add(self.ovsdb.db_set('Port', dev, ('tag', tag))) |
| 223 |
if vlan_mode: |
| 224 |
txn.add(self.ovsdb.db_set('Port', dev, |
| 225 |
('vlan_mode', vlan_mode))) |
| 226 |
if trunks: |
| 227 |
txn.add(self.ovsdb.db_set('Port', dev, ('trunks', trunks))) |
| 228 |
if qid: |
| 229 |
txn.add(self.ovsdb.db_set('Port', dev, ('qos', qid))) |
| 230 |
if col_values: |
| 231 |
txn.add(self.ovsdb.db_set('Interface', dev, *col_values)) |
| 232 |
self.update_device_mtu( |
| 233 |
txn, dev, mtu, interface_type=interface_type |
| 234 |
) |
| 235 |
|
| 236 |
def port_exists(self, port_name, bridge): |
| 237 |
ports = self.ovsdb.list_ports(bridge).execute() |
| 238 |
return ports is not None and port_name in ports |
| 239 |
|
| 240 |
def get_qos(self, dev, qos_type): |
| 241 |
qos_id = uuid.uuid5(QOS_UUID_NAMESPACE, dev) |
| 242 |
external_ids = {'id': str(qos_id), '_type': qos_type} |
| 243 |
return self.ovsdb.db_find( |
| 244 |
'QoS', ('external_ids', '=', external_ids), |
| 245 |
colmuns=['_uuid'] |
| 246 |
).execute() |
| 247 |
|
| 248 |
def delete_qos_if_exists(self, dev, qos_type): |
| 249 |
qos_ids = self.get_qos(dev, qos_type) |
| 250 |
if qos_ids is not None and len(qos_ids) > 0: |
| 251 |
for qos_id in qos_ids: |
| 252 |
if '_uuid' in qos_id: |
| 253 |
self.ovsdb.db_destroy( |
| 254 |
'QoS', str(qos_id['_uuid']) |
| 255 |
).execute() |
| 256 |
|
| 257 |
def update_ovs_vif_port(self, dev, mtu=None, interface_type=None): |
| 258 |
with self.ovsdb.transaction() as txn: |
| 259 |
self.update_device_mtu( |
| 260 |
txn, dev, mtu, interface_type=interface_type |
| 261 |
) |
| 262 |
|
| 263 |
def delete_ovs_vif_port( |
| 264 |
self, bridge, dev, delete_netdev=True, qos_type=None |
| 265 |
): |
| 266 |
self.ovsdb.del_port(dev, bridge=bridge, if_exists=True).execute() |
| 267 |
if qos_type: |
| 268 |
self.delete_qos_if_exists(dev, qos_type) |
| 269 |
if delete_netdev: |
| 270 |
linux_net.delete_net_dev(dev) |