4571912e08e1f6afe431be2d209aa22aa951f3ab
[openstack-build/neutron-build.git] / neutron / cmd / ovs_cleanup.py
1 # Copyright (c) 2012 OpenStack Foundation.
2 # All Rights Reserved.
3 #
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
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 #    under the License.
15
16 from oslo_config import cfg
17 from oslo_log import log as logging
18
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
26
27
28 LOG = logging.getLogger(__name__)
29
30
31 def setup_conf():
32     """Setup the cfg for the clean up utility.
33
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.
36     """
37     opts = [
38         cfg.BoolOpt('ovs_all_ports',
39                     default=False,
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 '
43                            'bridges.'))
44     ]
45
46     conf = cfg.CONF
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)
51     return conf
52
53
54 def collect_neutron_ports(bridges):
55     """Collect ports created by Neutron from OVS."""
56     ports = []
57     for bridge in bridges:
58         ovs = ovs_lib.OVSBridge(bridge)
59         ports += [port.port_name for port in ovs.get_vif_ports()]
60     return ports
61
62
63 def delete_neutron_ports(ports):
64     """Delete non-internal ports created by Neutron
65
66     Non-internal OVS ports need to be removed manually.
67     """
68     for port in ports:
69         device = ip_lib.IPDevice(port)
70         if device.exists():
71             device.link.delete()
72             LOG.info(_LI("Deleting port: %s"), port)
73
74
75 def main():
76     """Main method for cleaning up OVS bridges.
77
78     The utility cleans up the integration bridges used by Neutron.
79     """
80
81     conf = setup_conf()
82     conf()
83     config.setup_logging()
84
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
90
91     if conf.ovs_all_ports:
92         bridges = ovs_bridges
93     else:
94         bridges = available_configuration_bridges
95
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)
100
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)
105
106     # Remove remaining ports created by Neutron (usually veth pair)
107     delete_neutron_ports(ports)
108
109     LOG.info(_LI("OVS cleanup completed successfully"))