#NEWLINE# @ staticmethod #NEWLINE# @ utils . synchronized ( 'lock_bridge' , external = True ) #NEWLINE# def ensure_bridge( bridge , interface , net_attrs = None , gateway = True , filtering = True ) : #NEWLINE# #INDENT# "Create a bridge unless it already exists.\n\n        :param interface: the interface to create the bridge on.\n        :param net_attrs: dictionary with  attributes used to create bridge.\n        :param gateway: whether or not the bridge is a gateway.\n        :param filtering: whether or not to create filters on the bridge.\n\n        If net_attrs is set, it will add the net_attrs['gateway'] to the bridge\n        using net_attrs['broadcast'] and net_attrs['cidr'].  It will also add\n        the ip_v6 address specified in net_attrs['cidr_v6'] if use_ipv6 is set.\n\n        The code will attempt to move any IPs that already exist on the\n        interface onto the bridge and reset the default gateway if necessary.\n\n        " #NEWLINE# if ( not device_exists ( bridge ) ) : #NEWLINE# #INDENT# LOG . debug ( 'Starting Bridge %s' , bridge ) #NEWLINE# ( out , err ) = _execute ( 'brctl' , 'addbr' , bridge , check_exit_code = False , run_as_root = True ) #NEWLINE# if ( err and ( err != ( "device %s already exists; can't create bridge with the same name\n" % bridge ) ) ) : #NEWLINE# #INDENT# msg = ( _ ( 'Failed to add bridge: %s' ) % err ) #NEWLINE# raise exception . NovaException ( msg ) #NEWLINE# #UNINDENT# _execute ( 'brctl' , 'setfd' , bridge , 0 , run_as_root = True ) #NEWLINE# _execute ( 'brctl' , 'stp' , bridge , 'off' , run_as_root = True ) #NEWLINE# _execute ( 'ip' , 'link' , 'set' , bridge , 'up' , run_as_root = True ) #NEWLINE# #UNINDENT# if interface : #NEWLINE# #INDENT# LOG . debug ( 'Adding interface %(interface)s to bridge %(bridge)s' , { #NEWLINE# #INDENT# 'interface' : interface , #NEWLINE# 'bridge' : bridge , #NEWLINE# #UNINDENT# } ) #NEWLINE# ( out , err ) = _execute ( 'brctl' , 'addif' , bridge , interface , check_exit_code = False , run_as_root = True ) #NEWLINE# if ( err and ( err != ( "device %s is already a member of a bridge; can't enslave it to bridge %s.\n" % ( interface , bridge ) ) ) ) : #NEWLINE# #INDENT# msg = ( _ ( 'Failed to add interface: %s' ) % err ) #NEWLINE# raise exception . NovaException ( msg ) #NEWLINE# #UNINDENT# if ( not CONF . fake_network ) : #NEWLINE# #INDENT# interface_addrs = netifaces . ifaddresses ( interface ) #NEWLINE# interface_mac = interface_addrs [ netifaces . AF_LINK ] [ 0 ] [ 'addr' ] #NEWLINE# _execute ( 'ip' , 'link' , 'set' , bridge , 'address' , interface_mac , run_as_root = True ) #NEWLINE# #UNINDENT# ( out , err ) = _execute ( 'ip' , 'link' , 'set' , interface , 'up' , check_exit_code = False , run_as_root = True ) #NEWLINE# old_routes = [ ] #NEWLINE# ( out , err ) = _execute ( 'ip' , 'route' , 'show' , 'dev' , interface ) #NEWLINE# for line in out . split ( '\n' ) : #NEWLINE# #INDENT# fields = line . split ( ) #NEWLINE# if ( fields and ( 'via' in fields ) ) : #NEWLINE# #INDENT# old_routes . append ( fields ) #NEWLINE# _execute ( 'ip' , 'route' , 'del' , * fields , run_as_root = True ) #NEWLINE# #UNINDENT# #UNINDENT# ( out , err ) = _execute ( 'ip' , 'addr' , 'show' , 'dev' , interface , 'scope' , 'global' ) #NEWLINE# for line in out . split ( '\n' ) : #NEWLINE# #INDENT# fields = line . split ( ) #NEWLINE# if ( fields and ( fields [ 0 ] == 'inet' ) ) : #NEWLINE# #INDENT# if ( fields [ ( - 2 ) ] in ( 'secondary' , 'dynamic' ) ) : #NEWLINE# #INDENT# params = fields [ 1 : ( - 2 ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# params = fields [ 1 : ( - 1 ) ] #NEWLINE# #UNINDENT# _execute ( * _ip_bridge_cmd ( 'del' , params , fields [ ( - 1 ) ] ) , run_as_root = True , check_exit_code = [ 0 , 2 , 254 ] ) #NEWLINE# _execute ( * _ip_bridge_cmd ( 'add' , params , bridge ) , run_as_root = True , check_exit_code = [ 0 , 2 , 254 ] ) #NEWLINE# #UNINDENT# #UNINDENT# for fields in old_routes : #NEWLINE# #INDENT# _execute ( 'ip' , 'route' , 'add' , * fields , run_as_root = True ) #NEWLINE# #UNINDENT# #UNINDENT# if filtering : #NEWLINE# #INDENT# ipv4_filter = iptables_manager . ipv4 [ 'filter' ] #NEWLINE# if gateway : #NEWLINE# #INDENT# for rule in get_gateway_rules ( bridge ) : #NEWLINE# #INDENT# ipv4_filter . add_rule ( * rule ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# ipv4_filter . add_rule ( 'FORWARD' , ( '--in-interface %s -j %s' % ( bridge , CONF . iptables_drop_action ) ) ) #NEWLINE# ipv4_filter . add_rule ( 'FORWARD' , ( '--out-interface %s -j %s' % ( line , CONF . iptables_drop_action ) ) )