1 # Copyright (c) 2012 OpenStack Foundation.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
16 from oslo_config import cfg
17 from oslo_log import log as logging
19 from neutron._i18n import _, _LI
20 from neutron.agent.common import config as agent_config
21 from neutron.agent.common import ovs_lib
22 from neutron.agent.l3 import config as l3_config
23 from neutron.agent.linux import interface
24 from neutron.agent.linux import ip_lib
25 from neutron.common import config
28 LOG = logging.getLogger(__name__)
32 """Setup the cfg for the clean up utility.
34 Use separate setup_conf for the utility because there are many options
35 from the main config that do not apply during clean-up.
38 cfg.BoolOpt('ovs_all_ports',
40 help=_('True to delete all ports on all the OpenvSwitch '
41 'bridges. False to delete ports created by '
42 'Neutron on integration and external network '
47 conf.register_cli_opts(opts)
48 conf.register_opts(l3_config.OPTS)
49 conf.register_opts(interface.OPTS)
50 agent_config.register_interface_driver_opts_helper(conf)
54 def collect_neutron_ports(bridges):
55 """Collect ports created by Neutron from OVS."""
57 for bridge in bridges:
58 ovs = ovs_lib.OVSBridge(bridge)
59 ports += [port.port_name for port in ovs.get_vif_ports()]
63 def delete_neutron_ports(ports):
64 """Delete non-internal ports created by Neutron
66 Non-internal OVS ports need to be removed manually.
69 device = ip_lib.IPDevice(port)
72 LOG.info(_LI("Deleting port: %s"), port)
76 """Main method for cleaning up OVS bridges.
78 The utility cleans up the integration bridges used by Neutron.
83 config.setup_logging()
85 configuration_bridges = set([conf.ovs_integration_bridge,
86 conf.external_network_bridge])
87 ovs = ovs_lib.BaseOVS()
88 ovs_bridges = set(ovs.get_bridges())
89 available_configuration_bridges = configuration_bridges & ovs_bridges
91 if conf.ovs_all_ports:
94 bridges = available_configuration_bridges
96 # Collect existing ports created by Neutron on configuration bridges.
97 # After deleting ports from OVS bridges, we cannot determine which
98 # ports were created by Neutron, so port information is collected now.
99 ports = collect_neutron_ports(available_configuration_bridges)
101 for bridge in bridges:
102 LOG.info(_LI("Cleaning bridge: %s"), bridge)
103 ovs = ovs_lib.OVSBridge(bridge)
104 ovs.delete_ports(all_ports=conf.ovs_all_ports)
106 # Remove remaining ports created by Neutron (usually veth pair)
107 delete_neutron_ports(ports)
109 LOG.info(_LI("OVS cleanup completed successfully"))