Esempio n. 1
0
def insert_routerinfo():
    # 将当前网络中的虚拟路由器信息插入数据库
    vm = Vminfo()
    routers_info = get_router_info()
    print routers_info
    # {u'router2': [u'10.0.224.3', u'10.0.223.6'], u'router1': [u'10.0.111.21', u'10.0.223.5']}
    vm = Vminfo()
    for router_name in routers_info.keys():
        try:
            vm.Insert_router_info(router_name, routers_info.get(router_name))
            print 'Insert routerinfo of %s successful.' % router_name
        except Exception, e:
            print e
Esempio n. 2
0
def get_vminfo_and_add_flowtable():
	vm = Vminfo()
	vminfos = vm.Get_Dict()

	# 初始化ttl_handler
	ttl_hand = Ttl_handler()
	ttl_hand.init_graph()
	
	# 获取每一个主机信息,查找其在ovs中的ofport并添加为ovs_port属性值,查找主机所连接的路由器信息,添加为access_router属性值,为列表形式,在openstack中,可能会有多个路由器连到这个网络,因此,该属性值为连接路由器列表,后面会计算最短的路径
	router_infos_net = ttl_hand.router_infos_net
	for vminfo in vminfos:
		vminfo['ovs_port'] =  str(libvirt_test.get_port_num_in_ovs(vminfo['instance_name']))
		vminfo['access_router'] = []
		for router_name in router_infos_net.keys():
			router_iplist = router_infos_net.get(router_name)
			for ip in router_iplist:
				if IP(vminfo['ip']) in IP(ip):
					vminfo['access_router'].append(router_name)
	#print vminfos
	
	#将所有的虚拟机进行组合通信
	zuhe_list = list(combinations(vminfos,2))
	#print zuhe_list
	

	for tupul in zuhe_list:
		ip1 = tupul[0]['ip']
		mac1 = tupul[0]['mac']
		host1_port = tupul[0]['ovs_port']
		host1_access_router = tupul[0]['access_router']
		ip2 = tupul[1]['ip']
		mac2 = tupul[1]['mac']
		host2_port = tupul[1]['ovs_port']
		host2_access_router = tupul[1]['access_router']
		
		#计算所有组合的间隔路由器,并计算这些路由器的路径,去路径的最小值
		access_router_zuhe = twolist_zuhe(host1_access_router,host2_access_router)
		all_path_len = []
		for tupul2 in access_router_zuhe:
			all_path_len.append(len(ttl_hand.find_shortest_path(tupul2[0],tupul2[1])))
		min_len = min(all_path_len)
		ttl = str(64-min_len)
	
		print ip1, ip2, mac1, mac2, host1_port, host2_port, ttl
		flowtable_handler = Flowtable_handler('br-int')
		flowtable_handler.add_flowtable_into_ovs(ip1, ip2, mac1, mac2, host1_port, host2_port, ttl = ttl)
Esempio n. 3
0
def get_vminfo_and_remove_flowtable():
    vm = Vminfo()
    vminfos = vm.Get_Dict()
    for vminfo in vminfos:
        vminfo['ovs_port'] = str(libvirt_test.get_port_num_in_ovs(vminfo['instance_name']))
    print vminfos

    zuhe_list = list(combinations(vminfos,2))	
	
    for tupul in zuhe_list:
        ip1 = tupul[0]['ip']
        mac1 = tupul[0]['mac']
        host1_port = tupul[0]['ovs_port']
        ip2 = tupul[1]['ip']
        mac2 = tupul[1]['mac']
        host2_port = tupul[1]['ovs_port']
        print ip1, ip2, mac1, mac2, host1_port, host2_port
        flowtable_handler = Flowtable_handler('br-int')
        flowtable_handler.del_flowtable_from_ovs(ip1,ip2)
		#del_flow_to_ovs(ip1,ip2)
        time.sleep(0.3)
Esempio n. 4
0
def main():
		
	# 提取数据库中可达网络,前提是网络节点中已经将路由表插入控制节点的数据库中
	vm = Vminfo()	
	need_nets_dict = vm.Get_access_network_Dict()
	#print need_nets_dict	
	need_nets = []
	for net_dict in need_nets_dict:
		need_nets.append(net_dict.get('network'))
	print need_nets
	# need_nets 内容为 ['10.0.223.0/24', '10.0.224.0/24', '10.0.111.0/24']	
	
	# 查找到need_nets中的所有主机信息,将其信息插入数据库中vminfo表中
	vminfos = get_need_hosts_infomation(*need_nets)
	print vminfos

	for vm_name in vminfos.keys():
		try:
			vm.Insert_vm_info(vm_name,vminfos.get(vm_name)[0],vminfos.get(vm_name)[1],vminfos.get(vm_name)[2])
			print 'Insert vminfo of %s successful.' % vm_name
		except Exception,e:
			print e.reason
Esempio n. 5
0
    def __init__(self):
        vminfo = Vminfo()
        self.router_infos_ip = {}
        self.router_infos_net = {}
        self.graph = {}
        self.routerinfos = vminfo.Get_routerinfo_Dict()

        #将数据库中路由器信息处理成接口为ip的形式
        for routerinfo in self.routerinfos:
            router_name = routerinfo.get('name')
            iplist_str = routerinfo.get('iplist')
            pattern = re.compile(
                r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
            iplist_list = []
            for ip in pattern.findall(iplist_str):
                iplist_list.append(ip)
            self.router_infos_ip[router_name] = iplist_list
        # 将路由器信息处理成连接为网络号的形式
        for key in self.router_infos_ip.keys():
            values = self.router_infos_ip.get(key)
            for value in values:
                values[values.index(value)] = IP(value).make_net(
                    '255.255.255.0')
            self.router_infos_net[key] = values
Esempio n. 6
0
def delete_all():
    vm = Vminfo()
    vm.Delete_all_in_routerinfo()
    vm.Delete_all_in_routetable()
    vm.Delete_all_in_vminfo()
Esempio n. 7
0
def handle_network_and_insert_networkinfo():
    # 获取当前数据库中的所有网络信息,格式 ({'network_id': 'a9de4d55-ccbe-4a66-98bc-b1eac089f883', 'cidr': '10.0.111.0/24', 'id': '25756bbb-f905-4336-ab51-d6e42e7f1347'},....)
    vm = Vminfo()
    network_info = vm.Get_network_info_Dict()
    #print network_info

    # 获取当前数据库routeflow中的虚拟路由器信息
    router = Vminfo()
    routerinfos = router.Get_routerinfo_Dict()
    # routerinfos格式 ({'iplist': "[u'10.0.224.3', u'10.0.223.6']", 'name': 'router2'}, {'iplist': "[u'10.0.111.21', u'10.0.223.5']", 'name': 'router1'})
    # 重新处理一下路由器信息
    routers_info = {}
    for routerinfo in routerinfos:
        router_name = routerinfo.get('name')
        iplist_str = routerinfo.get('iplist')
        pattern = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
        iplist_list = []
        for ip in pattern.findall(iplist_str):
            iplist_list.append(ip)
        routers_info[router_name] = iplist_list
    # 处理后的路由器信息 {'router2': ['10.0.224.3', '10.0.223.6'], 'router1': ['10.0.111.21', '10.0.223.5']}
    #print routers_info

    # 根据路由器的一个IP地址找到相应的网络命名空间namespace
    # 格式 {'10.0.224.3': 'qdhcp-9713a489-1bcc-418f-81e4-8d138db94c8a', '10.0.111.21': 'qdhcp-a9de4d55-ccbe-4a66-98bc-b1eac089f883'}
    routetable_needinfo = {}
    for router_name in routers_info.keys():
        ip1 = routers_info.get(router_name)[0]
        #print ip1
        for net_dict in network_info:
            if IP(ip1) in IP(net_dict.get('cidr')):
                routetable_needinfo[ip1] = 'qdhcp-' + net_dict.get(
                    'network_id')
                #print net_dict.get('network_id')
                break
    #print routetable_needinfo

    # 根据所有路由器中的路由表计算出共有的互通的网络
    routetables = {}
    routetables_filter_network = {}
    for ipaddr in routetable_needinfo.keys():
        network_list = []
        result = commands.getoutput(
            'ip netns exec %s curl http://%s:4501/route' %
            (routetable_needinfo.get(ipaddr), ipaddr))
        pattern = re.compile(r'(?<![\.\d])(?:\d{1,3}\.){3}\d{1,3}(?![\.\d])')
        for ip in pattern.findall(result):
            network_list.append(ip)
        routetables_filter_network[ipaddr] = network_list
    #print routetables_filter_network
    #取所有路由器路由表中的交集
    network_list_all = []
    for ipaddr in routetables_filter_network.keys():
        network_list_all.append(routetables_filter_network.get(ipaddr))
    new_network_list = getIntersection(network_list_all)
    #print new_network_list
    # 去掉重复和一些固定不需要的
    routetables_filter_network2 = list(set(new_network_list))
    routetables_filter_network2.remove('255.255.255.0')
    routetables_filter_network2.remove('255.255.255.255')
    routetables_filter_network2.remove('169.254.169.254')
    routetables_filter_network2.remove('0.0.0.0')
    #print routetables_filter_network2

    # routetables_filter_network2 内容为 ['10.0.223.0', '10.0.223.1', '10.0.224.0', '10.0.111.0']
    # routetables_filter_network4 为最后需要的路由表,格式为 ['10.0.223.0/24', '10.0.224.0/24', '10.0.111.0/24']
    routetables_filter_network3 = []
    routetables_filter_network4 = []
    # 将末尾是0的地址取出来
    for ip in routetables_filter_network2:
        if ip.split('.')[-1] == '0':
            routetables_filter_network3.append(ip)
    for ip in routetables_filter_network3:
        routetables_filter_network4.append(ip + '/24')
    print routetables_filter_network4

    # 将可达性网络插入数据库
    for network in routetables_filter_network4:
        vm.Insert_routetable_network(network)
        print 'insert network %s to database successful.' % network