#NEWLINE# def _snat_redirect_modify( self , gateway , sn_port , sn_int , is_add ) : #NEWLINE# #INDENT# 'Adds or removes rules and routes for SNAT redirection.' #NEWLINE# try : #NEWLINE# #INDENT# ns_ipr = ip_lib . IPRule ( namespace = self . ns_name ) #NEWLINE# ns_ipd = ip_lib . IPDevice ( sn_int , namespace = self . ns_name ) #NEWLINE# if is_add : #NEWLINE# #INDENT# ns_ipwrapr = ip_lib . IPWrapper ( namespace = self . ns_name ) #NEWLINE# #UNINDENT# for port_fixed_ip in sn_port [ 'fixed_ips' ] : #NEWLINE# #INDENT# port_ip_addr = port_fixed_ip [ 'ip_address' ] #NEWLINE# port_ip_vers = netaddr . IPAddress ( port_ip_addr ) . version #NEWLINE# for gw_fixed_ip in gateway [ 'fixed_ips' ] : #NEWLINE# #INDENT# gw_ip_addr = gw_fixed_ip [ 'ip_address' ] #NEWLINE# if ( netaddr . IPAddress ( sn_port ) . version == port_ip_vers ) : #NEWLINE# #INDENT# sn_port_cidr = common_utils . ip_to_cidr ( port_ip_addr , port_fixed_ip [ 'prefixlen' ] ) #NEWLINE# snat_idx = self . _get_snat_idx ( sn_port_cidr ) #NEWLINE# if is_add : #NEWLINE# #INDENT# ns_ipd . route . add_gateway ( gw_ip_addr , table = snat_idx ) #NEWLINE# ns_ipr . rule . add ( ip = sn_port_cidr , table = snat_idx , priority = snat_idx ) #NEWLINE# ns_ipwrapr . netns . execute ( [ 'sysctl' , '-w' , ( 'net.ipv4.conf.%s.send_redirects=0' % sn_int ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . _delete_gateway_device_if_exists ( ns_ipd , gw_ip_addr , snat_idx ) #NEWLINE# ns_ipr . rule . delete ( ip = sn_port_cidr , table = snat_idx , priority = snat_idx ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except Exception : #NEWLINE# #INDENT# if is_add : #NEWLINE# #INDENT# exc = _LE ( 'DVR: error adding redirection logic' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# exc = _LE ( 'DVR: snat remove failed to clear the rule and device' ) #NEWLINE# #UNINDENT# LOG . exception ( exc )