@@ -88,7 +88,7 @@ def destroy_network_rules_for_vm(vm_name, vif=None):
8888 if vm_name .startswith ('i-' ) or vm_name .startswith ('r-' ):
8989 vmchain_default = '-' .join (vm_name .split ('-' )[:- 1 ]) + "-def"
9090
91- destroy_ebtables_rules (vmchain )
91+ destroy_ebtables_rules (vmchain , vif )
9292
9393 try :
9494 if vmchain_default != None :
@@ -129,10 +129,28 @@ def destroy_network_rules_for_vm(vm_name, vif=None):
129129
130130 return 'true'
131131
132- def destroy_ebtables_rules (vm_name ):
133- delcmd = "ebtables-save | grep ROUTING | grep " + vm_name + " | sed 's/-A/-D/'"
134- delcmds = execute (delcmd ).split ('\n ' )
135- delcmds .pop ()
132+ def destroy_ebtables_rules (vm_name , vif ):
133+ if vif is None :
134+ return
135+ delcmd = "ebtables -t nat -L PREROUTING | grep " + vif
136+ delcmds = []
137+ try :
138+ delcmds = execute (delcmd ).split ('\n ' )
139+ delcmds .pop ()
140+ delcmds = ["-D PREROUTING " + x for x in delcmds ]
141+ except :
142+ pass
143+ postcmds = []
144+ try :
145+ postcmd = "ebtables -t nat -L POSTROUTING | grep " + vif
146+ postcmds = execute (postcmd ).split ('\n ' )
147+ postcmds .pop ()
148+ postcmds = ["-D POSTROUTING " + x for x in postcmds ]
149+ except :
150+ pass
151+
152+ delcmds += postcmds
153+
136154 for cmd in delcmds :
137155 try :
138156 execute ("ebtables -t nat " + cmd )
@@ -231,7 +249,7 @@ def default_network_rules(vm_name, vm_id, vm_ip, vm_mac, vif, brname):
231249 vmchain = vm_name
232250 vmchain_default = '-' .join (vmchain .split ('-' )[:- 1 ]) + "-def"
233251
234- destroy_ebtables_rules (vmName )
252+ destroy_ebtables_rules (vmName , vif )
235253
236254 try :
237255 execute ("iptables -N " + vmchain )
@@ -284,7 +302,7 @@ def post_default_network_rules(vm_name, vm_id, vm_ip, vm_mac, vif, brname, dhcpS
284302 pass
285303
286304 try :
287- execute ("ebtables -t nat -A PREROUTING -i " + vif + " -p IPv4 --ip-protocol tcp --ip-destination-port 80 --ip-dst " + dhcpSvr + " -j dnat --to-destination " + hostMacAddr )
305+ execute ("ebtables -t nat -I " + vmchain_in + " -p IPv4 --ip-protocol tcp --ip-destination-port 80 --ip-dst " + dhcpSvr + " -j dnat --to-destination " + hostMacAddr )
288306 except :
289307 pass
290308
0 commit comments