Petr Horáček has uploaded a new change for review. Change subject: net: native ovs: ovs network setup ......................................................................
net: native ovs: ovs network setup TODO: commit msg, tests Change-Id: I13bb7627431bac42a4195b97167a8700b713f76f Bug-Url: https://bugzilla.redhat.com/1195208 Signed-off-by: Petr Horáček <phora...@redhat.com> --- M lib/vdsm/network/ovs/Makefile.am A lib/vdsm/network/ovs/ovs.py M lib/vdsm/network/ovs/switch.py M vdsm.spec.in 4 files changed, 130 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/53/56353/1 diff --git a/lib/vdsm/network/ovs/Makefile.am b/lib/vdsm/network/ovs/Makefile.am index 756f942..bc39611 100644 --- a/lib/vdsm/network/ovs/Makefile.am +++ b/lib/vdsm/network/ovs/Makefile.am @@ -25,6 +25,7 @@ dist_vdsmnetworkovs_PYTHON = \ __init__.py \ + ovs.py \ switch.py \ validator.py \ $(NULL) diff --git a/lib/vdsm/network/ovs/ovs.py b/lib/vdsm/network/ovs/ovs.py new file mode 100644 index 0000000..305c5d3 --- /dev/null +++ b/lib/vdsm/network/ovs/ovs.py @@ -0,0 +1,125 @@ +# Copyright 2016 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Refer to the README and COPYING files for full details of the license +# +from __future__ import absolute_import + +import six + +from .driver import create + + +BRIDGE_NAME = 'ovsbr0' # TODO: support multiple bridges + +ovsdb = create() + + +def _get_nets_by_nics(running_config): + """Transform running config into {nic: set(networks)}.""" + nets_by_nic = {} + for net, attrs in six.iteritems(running_config.networks): + nic = attrs.get('nic') + if nic is not None: + nets_by_nic.setdefault(nic, set()).add(net) + return nets_by_nic + + +def _add_nic_port(net, nic, nets_by_nic): + if nic in nets_by_nic: + nets_by_nic[nic].add(net) + return ovsdb.do_nothing() + else: + nets_by_nic[nic] = set([net]) + return ovsdb.add_port(BRIDGE_NAME, nic, may_exist=True) + + +def _del_nic_port(net, nic, nets_by_nic): + nets_by_nic[nic].remove(net) + if len(nets_by_nic[nic]) == 0: + return ovsdb.del_port(BRIDGE_NAME, nic, if_exists=True) + else: + return ovsdb.do_nothing() + + +def _add_ovs_net(net, attrs, running_config, nets_by_nic): + commands = [] + nic = attrs.get('nic') + vlan = attrs.get('vlan') + + if vlan is not None: + commands.append( + ovsdb.add_vlan(BRIDGE_NAME, vlan, fake_bridge_name=net)) + if nic is not None: + commands.append(_add_nic_port(net, nic, nets_by_nic)) + + running_config.setNetwork(net, attrs) + return commands + + +def _remove_ovs_net(net, running_config, nets_by_nic): + commands = [] + net_conf = running_config.networks.get(net) + + if 'vlan' in net_conf: + commands.append(ovsdb.del_br(net)) # TODO: use del_vlan when fixed + if 'nic' in net_conf: + commands.append(_del_nic_port(net, net_conf.get('nic'), nets_by_nic)) + + running_config.removeNetwork(net) + return commands + + +def _prepare_addition(nets, running_config): + commands = [] + nets_by_nic = _get_nets_by_nics(running_config) + for net, attrs in six.iteritems(nets): + commands.extend( + _add_ovs_net(net, attrs, running_config, nets_by_nic)) + return commands + + +def _prepare_removal(nets, running_config): + commands = [] + nets_by_nic = _get_nets_by_nics(running_config) + for net in nets: + commands.extend(_remove_ovs_net(net, running_config, nets_by_nic)) + return commands + + +def remove(nets, running_config): + commands = _prepare_removal(nets, running_config) + with ovsdb.transaction() as t: + t.add(*commands) + # TODO: We need to store running_config, so optional rollback is able to + # obtain 'ovs' switch type and do proper rollback. Note, that running + # config should contain only correct entries and this should be dropped + # when _split_switch_type_entries() is able to get switch type from kernel + # config. + running_config.save() + + +def add(nets, running_config): + commands = _prepare_addition(nets, running_config) + with ovsdb.transaction() as t: + t.add(ovsdb.add_br(BRIDGE_NAME, may_exist=True)) + t.add(*commands) + # TODO: We need to store running_config, so optional rollback is able to + # obtain 'ovs' switch type and do proper rollback. Note, that running + # config should contain only correct entries and this should be dropped + # when _split_switch_type_entries() is able to get switch type from kernel + # config. + running_config.save() diff --git a/lib/vdsm/network/ovs/switch.py b/lib/vdsm/network/ovs/switch.py index b5c3e25..79534cc 100644 --- a/lib/vdsm/network/ovs/switch.py +++ b/lib/vdsm/network/ovs/switch.py @@ -23,6 +23,7 @@ from vdsm.netconfpersistence import RunningConfig from vdsm.netinfo.cache import CachingNetInfo +from . import ovs from . import validator SWITCH_TYPE = 'ovs' @@ -44,7 +45,8 @@ bonds_to_be_added_or_edited, bonds_to_be_removed = _split_bonds_action( bonds, running_config.bonds) - # TODO: remove and add filtered networks + ovs.remove(nets_to_be_removed, running_config) + ovs.add(nets_to_be_added, running_config) def _split_nets_action(nets, configured_nets): diff --git a/vdsm.spec.in b/vdsm.spec.in index 328a4d0..fccf574 100644 --- a/vdsm.spec.in +++ b/vdsm.spec.in @@ -1171,6 +1171,7 @@ %{python_sitelib}/%{vdsm_name}/network/ip/*.py* %{python_sitelib}/%{vdsm_name}/network/legacy_switch.py* %{python_sitelib}/%{vdsm_name}/network/ovs/__init__.py* +%{python_sitelib}/%{vdsm_name}/network/ovs/ovs.py* %{python_sitelib}/%{vdsm_name}/network/ovs/switch.py* %{python_sitelib}/%{vdsm_name}/network/ovs/validator.py* %{python_sitelib}/%{vdsm_name}/network/ovs/driver/__init__.py* -- To view, visit https://gerrit.ovirt.org/56353 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I13bb7627431bac42a4195b97167a8700b713f76f Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Petr Horáček <phora...@redhat.com> _______________________________________________ vdsm-patches mailing list vdsm-patches@lists.fedorahosted.org https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches