def setUp(self): self.mox = mox.Mox() self.INT_BRIDGE = 'integration_bridge' self.TUN_BRIDGE = 'tunnel_bridge' self.INT_OFPORT = 11111 self.TUN_OFPORT = 22222 self.mox.StubOutClassWithMocks(ovs_lib, 'OVSBridge') self.mock_int_bridge = ovs_lib.OVSBridge(self.INT_BRIDGE, 'sudo') self.mock_int_bridge.delete_port('patch-tun') self.mock_int_bridge.remove_all_flows() self.mock_int_bridge.add_flow(priority=1, actions='normal') self.mock_tun_bridge = ovs_lib.OVSBridge(self.TUN_BRIDGE, 'sudo') self.mock_tun_bridge.reset_bridge() self.mock_int_bridge.add_patch_port( 'patch-tun', 'patch-int').AndReturn(self.TUN_OFPORT) self.mock_tun_bridge.add_patch_port( 'patch-int', 'patch-tun').AndReturn(self.INT_OFPORT) self.mock_tun_bridge.remove_all_flows() self.mock_tun_bridge.add_flow(priority=1, actions='drop') self.mox.StubOutWithMock(utils, 'get_interface_mac') utils.get_interface_mac(self.INT_BRIDGE).AndReturn('000000000001')
def main(): """Main method for cleaning up OVS bridges. The utility cleans up the integration bridges used by Quantum. """ conf = setup_conf() conf() config.setup_logging(conf) configuration_bridges = set( [conf.ovs_integration_bridge, conf.external_network_bridge]) ovs_bridges = set(ovs_lib.get_bridges(conf.AGENT.root_helper)) available_configuration_bridges = configuration_bridges & ovs_bridges if conf.ovs_all_ports: bridges = ovs_bridges else: bridges = available_configuration_bridges # Collect existing ports created by Quantum on configuration bridges. # After deleting ports from OVS bridges, we cannot determine which # ports were created by Quantum, so port information is collected now. ports = collect_quantum_ports(available_configuration_bridges, conf.AGENT.root_helper) for bridge in bridges: LOG.info(_("Cleaning %s"), bridge) ovs = ovs_lib.OVSBridge(bridge, conf.AGENT.root_helper) ovs.delete_ports(all_ports=conf.ovs_all_ports) # Remove remaining ports created by Quantum (usually veth pair) delete_quantum_ports(ports, conf.AGENT.root_helper) LOG.info(_("OVS cleanup completed successfully"))
def collect_quantum_ports(bridges, root_helper): """Collect ports created by Quantum from OVS""" ports = [] for bridge in bridges: ovs = ovs_lib.OVSBridge(bridge, root_helper) ports += [port.port_name for port in ovs.get_vif_ports()] return ports
def setUp(self): self.BR_NAME = "br-int" self.TO = "--timeout=2" self.mox = mox.Mox() self.root_helper = 'sudo' self.br = ovs_lib.OVSBridge(self.BR_NAME, self.root_helper) self.mox.StubOutWithMock(utils, "execute")
def unplug(self, device_name, bridge=None, namespace=None, prefix=None): """Unplug the interface.""" if not bridge: bridge = self.conf.ovs_integration_bridge self.check_bridge_exists(bridge) bridge = ovs_lib.OVSBridge(bridge, self.conf.root_helper) bridge.delete_port(device_name)
def setup_physical_bridges(self, bridge_mappings): '''Setup the physical network bridges. Creates physical network bridges and links them to the integration bridge using veths. :param bridge_mappings: map physical network names to bridge names. ''' self.phys_brs = {} self.int_ofports = {} self.phys_ofports = {} ip_wrapper = ip_lib.IPWrapper(self.root_helper) for physical_network, bridge in bridge_mappings.iteritems(): LOG.info( _("Mapping physical network %(physical_network)s to " "bridge %(bridge)s"), { 'physical_network': physical_network, 'bridge': bridge }) # setup physical bridge if not ip_lib.device_exists(bridge, self.root_helper): LOG.error( _("Bridge %(bridge)s for physical network " "%(physical_network)s does not exist. Agent " "terminated!"), { 'physical_network': physical_network, 'bridge': bridge }) sys.exit(1) br = ovs_lib.OVSBridge(bridge, self.root_helper) br.remove_all_flows() br.add_flow(priority=1, actions="normal") self.phys_brs[physical_network] = br # create veth to patch physical bridge with integration bridge int_veth_name = constants.VETH_INTEGRATION_PREFIX + bridge self.int_br.delete_port(int_veth_name) phys_veth_name = constants.VETH_PHYSICAL_PREFIX + bridge br.delete_port(phys_veth_name) if ip_lib.device_exists(int_veth_name, self.root_helper): ip_lib.IPDevice(int_veth_name, self.root_helper).link.delete() int_veth, phys_veth = ip_wrapper.add_veth(int_veth_name, phys_veth_name) self.int_ofports[physical_network] = self.int_br.add_port(int_veth) self.phys_ofports[physical_network] = br.add_port(phys_veth) # block all untranslated traffic over veth between bridges self.int_br.add_flow(priority=2, in_port=self.int_ofports[physical_network], actions="drop") br.add_flow(priority=2, in_port=self.phys_ofports[physical_network], actions="drop") # enable veth to pass traffic int_veth.link.set_up() phys_veth.link.set_up()
def setUp(self): super(OVS_Lib_Test, self).setUp() self.BR_NAME = "br-int" self.TO = "--timeout=2" self.mox = mox.Mox() self.root_helper = 'sudo' self.br = ovs_lib.OVSBridge(self.BR_NAME, self.root_helper) self.mox.StubOutWithMock(utils, "execute") self.addCleanup(self.mox.UnsetStubs)
def setup_integration_br(self, integ_br): '''Setup the integration bridge. Create patch ports and remove all existing flows. :param integ_br: the name of the integration bridge.''' self.int_br = ovs_lib.OVSBridge(integ_br, self.root_helper) self.int_br.delete_port("patch-tun") self.int_br.remove_all_flows() # switch all traffic using L2 learning self.int_br.add_flow(priority=1, actions="normal")
def unplug_device(conf, device): try: device.link.delete() except RuntimeError: # Maybe the device is OVS port, so try to delete bridge_name = ovs_lib.get_bridge_for_iface(conf.root_helper, device.name) if bridge_name: bridge = ovs_lib.OVSBridge(bridge_name, conf.root_helper) bridge.delete_port(device.name) else: LOG.debug(_('Unable to find bridge for device: %s') % device.name)
def unplug(self, device_name, bridge=None, namespace=None): """Unplug the interface.""" if not bridge: bridge = self.conf.ovs_integration_bridge self.check_bridge_exists(bridge) bridge = ovs_lib.OVSBridge(bridge, self.conf.root_helper) bridge.delete_port(device_name) if namespace: ip = ip_lib.IPWrapper(self.conf.root_helper, namespace) ip.garbage_collect_namespace()
def setup_tunnel_br(self, tun_br): '''Setup the tunnel bridge. Creates tunnel bridge, and links it to the integration bridge using a patch port. :param tun_br: the name of the tunnel bridge.''' self.tun_br = ovs_lib.OVSBridge(tun_br, self.root_helper) self.tun_br.reset_bridge() self.patch_int_ofport = self.tun_br.add_patch_port("patch-int", "patch-tun") self.tun_br.remove_all_flows() self.tun_br.add_flow(priority=1, actions="drop")
def setup_integration_br(self, bridge_name): '''Setup the integration bridge. Create patch ports and remove all existing flows. :param bridge_name: the name of the integration bridge. :returns: the integration bridge ''' int_br = ovs_lib.OVSBridge(bridge_name, self.root_helper) int_br.delete_port(cfg.CONF.OVS.int_peer_patch_port) int_br.remove_all_flows() # switch all traffic using L2 learning int_br.add_flow(priority=1, actions="normal") return int_br
def plug(self, network_id, port_id, device_name, mac_address, bridge=None, namespace=None, prefix=None): """Plug in the interface.""" super(RyuInterfaceDriver, self).plug(network_id, port_id, device_name, mac_address, bridge=bridge, namespace=namespace, prefix=prefix) if not bridge: bridge = self.conf.ovs_integration_bridge self.check_bridge_exists(bridge) ovs_br = ovs_lib.OVSBridge(bridge, self.conf.root_helper) datapath_id = ovs_br.get_datapath_id() port_no = ovs_br.get_port_ofport(device_name) self.ryu_client.create_port(network_id, datapath_id, port_no)
def unplug(self, device_name, bridge=None, namespace=None, prefix=None): """Unplug the interface.""" if not bridge: bridge = self.conf.ovs_integration_bridge tap_name = self._get_tap_name(device_name, prefix) self.check_bridge_exists(bridge) ovs = ovs_lib.OVSBridge(bridge, self.conf.root_helper) try: ovs.delete_port(tap_name) device = ip_lib.IPDevice(device_name, self.conf.root_helper, namespace) device.link.delete() LOG.debug(_("Unplugged interface '%s'") % device_name) except RuntimeError: LOG.error(_("Failed unplugging interface '%s'") % device_name)
def __init__(self, integ_br, root_helper, polling_interval): '''Constructor. :param integ_br: name of the integration bridge. :param root_helper: utility to use when running shell cmds. :param polling_interval: interval (secs) to check the bridge. ''' self.int_br = ovs_lib.OVSBridge(integ_br, root_helper) self.polling_interval = polling_interval self.host = socket.gethostname() self.agent_id = 'nec-q-agent.%s' % self.host self.datapath_id = "0x%s" % self.int_br.get_datapath_id() # RPC network init self.context = context.RequestContext('quantum', 'quantum', is_admin=False) self.conn = rpc.create_connection(new=True)
def setup_tunnel_br(self, tun_br): '''Setup the tunnel bridge. Creates tunnel bridge, and links it to the integration bridge using a patch port. :param tun_br: the name of the tunnel bridge.''' self.tun_br = ovs_lib.OVSBridge(tun_br, self.root_helper) self.tun_br.reset_bridge() self.patch_tun_ofport = self.int_br.add_patch_port("patch-tun", "patch-int") self.patch_int_ofport = self.tun_br.add_patch_port("patch-int", "patch-tun") if int(self.patch_tun_ofport) < 0 or int(self.patch_int_ofport) < 0: LOG.error("Failed to create OVS patch port. Cannot have tunneling " "enabled on this agent, since this version of OVS does " "not support tunnels or patch ports.") exit(1) self.tun_br.remove_all_flows() self.tun_br.add_flow(priority=1, actions="drop")
def setup_tunnel_br(self, tun_br): '''Setup the tunnel bridge. Creates tunnel bridge, and links it to the integration bridge using a patch port. :param tun_br: the name of the tunnel bridge.''' self.tun_br = ovs_lib.OVSBridge(tun_br, self.root_helper) self.tun_br.reset_bridge() self.patch_tun_ofport = self.int_br.add_patch_port( cfg.CONF.OVS.int_peer_patch_port, cfg.CONF.OVS.tun_peer_patch_port) self.patch_int_ofport = self.tun_br.add_patch_port( cfg.CONF.OVS.tun_peer_patch_port, cfg.CONF.OVS.int_peer_patch_port) if int(self.patch_tun_ofport) < 0 or int(self.patch_int_ofport) < 0: LOG.error(_("Failed to create OVS patch port. Cannot have " "tunneling enabled on this agent, since this version " "of OVS does not support tunnels or patch ports. " "Agent terminated!")) exit(1) self.tun_br.remove_all_flows() self.tun_br.add_flow(priority=1, actions="drop")
def __init__(self, integ_br, root_helper, polling_interval): '''Constructor. :param integ_br: name of the integration bridge. :param root_helper: utility to use when running shell cmds. :param polling_interval: interval (secs) to check the bridge. ''' self.int_br = ovs_lib.OVSBridge(integ_br, root_helper) self.polling_interval = polling_interval self.cur_ports = [] self.datapath_id = "0x%s" % self.int_br.get_datapath_id() self.agent_state = { 'binary': 'quantum-nec-agent', 'host': config.CONF.host, 'topic': q_const.L2_AGENT_TOPIC, 'configurations': {}, 'agent_type': q_const.AGENT_TYPE_NEC, 'start_flag': True } self.setup_rpc()
def setup_integration_br(self, integ_br): self.int_br = ovs_lib.OVSBridge(integ_br, self.root_helper) self.int_br.remove_all_flows() # switch all traffic using L2 learning self.int_br.add_flow(priority=1, actions="normal")
def setUp(self): super(TunnelTest, self).setUp() cfg.CONF.set_override('rpc_backend', 'quantum.openstack.common.rpc.impl_fake') cfg.CONF.set_override('report_interval', 0, 'AGENT') self.mox = mox.Mox() self.addCleanup(self.mox.UnsetStubs) self.INT_BRIDGE = 'integration_bridge' self.TUN_BRIDGE = 'tunnel_bridge' self.MAP_TUN_BRIDGE = 'tunnel_bridge_mapping' self.NET_MAPPING = {'net1': self.MAP_TUN_BRIDGE} self.INT_OFPORT = 11111 self.TUN_OFPORT = 22222 self.MAP_TUN_OFPORT = 33333 self.inta = self.mox.CreateMock(ip_lib.IPDevice) self.intb = self.mox.CreateMock(ip_lib.IPDevice) self.inta.link = self.mox.CreateMock(ip_lib.IpLinkCommand) self.intb.link = self.mox.CreateMock(ip_lib.IpLinkCommand) self.mox.StubOutClassWithMocks(ovs_lib, 'OVSBridge') self.mock_int_bridge = ovs_lib.OVSBridge(self.INT_BRIDGE, 'sudo') self.mock_int_bridge.delete_port('patch-tun') self.mock_int_bridge.remove_all_flows() self.mock_int_bridge.add_flow(priority=1, actions='normal') self.mock_map_tun_bridge = ovs_lib.OVSBridge(self.MAP_TUN_BRIDGE, 'sudo') self.mock_map_tun_bridge.remove_all_flows() self.mock_map_tun_bridge.add_flow(priority=1, actions='normal') self.mock_int_bridge.delete_port('int-tunnel_bridge_mapping') self.mock_map_tun_bridge.delete_port('phy-tunnel_bridge_mapping') self.mock_int_bridge.add_port(self.inta) self.mock_map_tun_bridge.add_port(self.intb) self.inta.link.set_up() self.intb.link.set_up() self.mock_int_bridge.add_flow(priority=2, in_port=None, actions='drop') self.mock_map_tun_bridge.add_flow(priority=2, in_port=None, actions='drop') self.mock_tun_bridge = ovs_lib.OVSBridge(self.TUN_BRIDGE, 'sudo') self.mock_tun_bridge.reset_bridge() self.mock_int_bridge.add_patch_port( 'patch-tun', 'patch-int').AndReturn(self.TUN_OFPORT) self.mock_tun_bridge.add_patch_port( 'patch-int', 'patch-tun').AndReturn(self.INT_OFPORT) self.mock_tun_bridge.remove_all_flows() self.mock_tun_bridge.add_flow(priority=1, actions='drop') self.mox.StubOutWithMock(ip_lib, 'device_exists') ip_lib.device_exists('tunnel_bridge_mapping', 'sudo').AndReturn(True) ip_lib.device_exists('int-tunnel_bridge_mapping', 'sudo').AndReturn(True) self.mox.StubOutWithMock(ip_lib.IpLinkCommand, 'delete') ip_lib.IPDevice('int-tunnel_bridge_mapping').link.delete() self.mox.StubOutClassWithMocks(ip_lib, 'IPWrapper') ip_lib.IPWrapper('sudo').add_veth( 'int-tunnel_bridge_mapping', 'phy-tunnel_bridge_mapping').AndReturn([self.inta, self.intb]) self.mock_int_bridge.get_local_port_mac().AndReturn('000000000001')