MCPcopy Create free account
hub / github.com/apache/cloudstack / network_rules_for_rebooted_vm

Function network_rules_for_rebooted_vm

scripts/vm/network/security_group.py:807–866  ·  view source on GitHub ↗
(vmName)

Source from the content-addressed store, hash-verified

805
806
807def network_rules_for_rebooted_vm(vmName):
808 vm_name = vmName
809 [curr_domid, old_domid] = check_domid_changed(vm_name)
810
811 if curr_domid == old_domid:
812 return True
813
814 if old_domid == '-1':
815 return True
816
817 if curr_domid == '-1':
818 return True
819
820 logging.debug("Found a rebooted VM -- reprogramming rules for " + vm_name)
821
822 delete_rules_for_vm_in_bridge_firewall_chain(vm_name)
823
824 brName = execute("iptables-save | awk -F '-j ' '/FORWARD -o(.*)physdev-is-bridged(.*)BF/ {print $2}'").strip()
825 if not brName:
826 brName = "cloudbr0"
827 else:
828 brName = execute("iptables-save |grep physdev-is-bridged |grep FORWARD |grep BF |grep '\-o' |awk '{print $4}' | head -1").strip()
829
830 if 1 in [ vm_name.startswith(c) for c in ['r-', 's-', 'v-'] ]:
831 default_network_rules_systemvm(vm_name, brName)
832 return True
833
834 vmchain = iptables_chain_name(vm_name)
835 vmchain_default = '-'.join(vmchain.split('-')[:-1]) + "-def"
836
837 vifs = get_vifs(vmName)
838 logging.debug(vifs, brName)
839 for v in vifs:
840 execute("iptables -A " + get_br_fw(brName) + "-IN " + " -m physdev --physdev-is-bridged --physdev-in " + v + " -j " + vmchain_default)
841 execute("iptables -A " + get_br_fw(brName) + "-OUT " + " -m physdev --physdev-is-bridged --physdev-out " + v + " -j " + vmchain_default)
842 execute("ip6tables -A " + get_br_fw(brName) + "-IN " + " -m physdev --physdev-is-bridged --physdev-in " + v + " -j " + vmchain_default)
843 execute("ip6tables -A " + get_br_fw(brName) + "-OUT " + " -m physdev --physdev-is-bridged --physdev-out " + v + " -j " + vmchain_default)
844
845 #change antispoof rule in vmchain
846 try:
847 delcmd = """iptables-save | awk '/-A %s(.*)physdev/ { sub(/-A/, "-D", $1) ; print }'""" % vmchain_default
848 inscmd = """iptables-save | awk '/-A %s(.*)physdev/ { gsub(/vnet[0-9]+/, "%s") ; sub(/-A/, "-D", $1) ; print }'""" % ( vmchain_default,vifs[0] )
849 ipts = []
850 for cmd in [delcmd, inscmd]:
851 logging.debug(cmd)
852 cmds = [_f for _f in execute(cmd).split('\n') if _f]
853 for c in cmds:
854 ipt = "iptables " + c
855 ipts.append(ipt)
856
857 for ipt in ipts:
858 try:
859 execute(ipt)
860 except:
861 logging.debug("Failed to rewrite antispoofing rules for vm " + vm_name)
862 except:
863 logging.debug("No rules found for vm " + vm_name)
864

Callers

nothing calls this directly

Calls 11

check_domid_changedFunction · 0.85
iptables_chain_nameFunction · 0.85
get_vifsFunction · 0.85
get_br_fwFunction · 0.85
rewrite_rule_log_for_vmFunction · 0.85
debugMethod · 0.80
executeFunction · 0.70
joinMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected