IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 在Mininet中使用topology zoo:Graphml转Mininet拓扑 -> 正文阅读

[Python知识库]在Mininet中使用topology zoo:Graphml转Mininet拓扑

项目地址(内含使用方法)

GitHub - yyd19981117/Graphml-To-Mininet

(如果本文帮到了忙,能否star一个?)

简介

topology-zoo:The Internet Topology Zoo

收录了世界各地的261个真实网络拓扑结构,用于网络相关研究和实验,尤其适合各种网络仿真,因为计算机网络类论文的仿真实验,如果自己搭建拓扑,太容易被审稿人怼拓扑设置简单、不符合实际等一系列问题。

因此,由于topology zoo的数据集均来源于真实世界,提供了上述问题的解决方案。

然而,topology zoo的的拓扑均为gml或graphml格式,下载之后不能立刻使用它们进行Mininet仿真,因此需要先将其转换为Mininet可执行的python拓扑脚本。

内容

这个代码主要参考了2014年Github上一个前辈的工作:

??????https://github.com/uniba-ktr/assessing-mininet/blob/master/parser/GraphML-Topo-to-Mininet-Network-Generator.py

因此,这份代码里面,前辈的注释我都保留了,只开发了自己修改的模块,并修改了前辈留在这份代码里原有的一些bug,并完成了部分to do list。

现在博主这份代码可以正确无误地生成topology zoo中的所有261个拓扑图了,上面链接我参考的这份代码存在一些bug,经过测试,仅能正确生成最多198个拓扑(198个拓扑被成功输出,但没有检查每一个的正确性,因此该数据是“最多198个”)

这份代码的具体用法,以及自动生成拓扑脚本,请戳本文最上面Github链接,里面有详细说明。

代码

#!/usr/bin/python

#################################################################################
#
# GraphML-Topo-to-Mininet-Network-Generator
#
# Parses Network Topologies in GraphML format from the Internet Topology Zoo.
# A python file for creating Mininet Topologies will be created as Output.
# Files have to be in the same directory.
#
# Arguments:
#   -f              [filename of GraphML input file]
#   --file          [filename of GraphML input file]
#   -o              [filename of GraphML output file]
#   --output        [filename of GraphML output file]
#   -b              [number as integer for DEFAULT bandwidth in mbit]
#   --bw            [number as integer for DEFAULT bandwidth in mbit]
#   --bandwidth     [number as integer for DEFAULT bandwidth in mbit]
#   -c              [controller ip as string]
#   --controller    [controller ip as string]
#   -p 				[port number as string]
#   -port 			[port number as string]
#
# sjas
# Wed Jul 17 02:59:06 PDT 2013
# 
# modified
# Tue Apr 19 2022
#
#################################################################################

import xml.etree.ElementTree as ET
import numpy as np
import sys
import math
import re
import random
import keyword
from sys import argv

input_file_name = ''
output_file_name = ''
bandwidth_argument = ''
controller_ip = ''
controller_port = ''

# This is the 17 to 24 digits in the IP address, that is X in 10.0.X.0.
# To handle the situation in which the number of nodes excceed 254.
# Host begin from 10.0.0.1, switch begin from 10.0.8.1.
# IP is limited in the field of 10.0.0.0/12
# If a host IP is 10.0.X.Y, then its corresponding switch node is 10.0.X+8.Y
# Support up to 254 * 8 nodes in current setting
# To change the node number limit, you should modify the '/12' mask setting.
ip_host_base = -1
ip_switch_base = 7

# First check commandline arguments
for i in range(len(argv)):

    if argv[i] == '-f':
        input_file_name = argv[i+1]
    if argv[i] == '--file':
        input_file_name = argv[i+1]
    if argv[i] == '-o':
        output_file_name = argv[i+1]
    if argv[i] == '--output':
        output_file_name = argv[i+1]
    if argv[i] == '-b':
        bandwidth_argument = argv[i+1]
    if argv[i] == '--bw':
        bandwidth_argument = argv[i+1]
    if argv[i] == '--bandwidth':
        bandwidth_argument = argv[i+1]
    if argv[i] == '-c':
        controller_ip = argv[i+1]
    if argv[i] == '--controller':
        controller_ip = argv[i+1]
    if argv[i] == '-p':
    	controller_port = argv[i+1]
    if argv[i] == '-port':
    	controller_port = argv[i+1]

# Terminate when inputfile is missing
if input_file_name == '':
    sys.exit('\n\tNo input file was specified as argument!')

# Define string fragments for output later on
outputstring_1 = '''#!/usr/bin/python

"""
Custom topology for Mininet, generated by GraphML-Topo-to-Mininet-Network-Generator.
"""

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call
import time

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/12',
                   autoSetMacs = True
                 )

    info( '\033[1;36m*** Adding controller\033[0m\\n')\n
'''

outputstring_2a='''
    info( '\033[1;36m*** Add switches\033[0m\\n')\n
'''
outputstring_2b='''
    info( '\033[1;36m*** Add hosts\033[0m\\n')\n
'''

outputstring_2c='''
    info( '\033[1;36m*** Add links\033[0m\\n')\n
'''

outputstring_3='''
    info( '\\n\033[1;36m*** Starting network\033[0m\\n')\n
    net.build()
    info( '\033[1;36m*** Starting controllers\033[0m\\n')\n
    for controller in net.controllers:
        controller.start()

    info( '\033[1;36m*** Starting switches\033[0m\\n')\n
'''

outputstring_4a='''
    info( '\\n\033[1;36m*** Post configure switches and hosts\033[0m\\n')\n
'''

outputstring_4b = '''
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

'''

outputstring_5 = '''
'''

# WHERE TO PUT RESULTS
outputstring_to_be_exported = ''
outputstring_to_be_exported += outputstring_1

# OUTPUT the controller settings
outputstring_controller = '''    c0 = net.addController(name='c0',
                        controller=RemoteController,
                        ip='''
outputstring_controller += "'127.0.0.1'" if controller_ip == '' else "'" + controller_ip + "'"
outputstring_controller += ','
outputstring_controller += '''
                        protocol='tcp',
                        port='''
outputstring_controller += '''6633''' if controller_port == '' else controller_port
outputstring_controller += ''')
'''

outputstring_to_be_exported += outputstring_controller

# READ FILE AND DO ALL THE ACTUAL PARSING IN THE NEXT PARTS
xml_tree    = ET.parse(input_file_name)
namespace   = "{http://graphml.graphdrawing.org/xmlns}"
ns          = namespace # just doing shortcutting, namespace is needed often.

# GET ALL ELEMENTS THAT ARE PARENTS OF ELEMENTS NEEDED LATER ON
root_element    = xml_tree.getroot()
graph_element   = root_element.find(ns + 'graph')

# GET ALL ELEMENT SETS NEEDED LATER ON
index_values_set    = root_element.findall(ns + 'key')
node_set            = graph_element.findall(ns + 'node')
edge_set            = graph_element.findall(ns + 'edge')

# SET SOME VARIABLES TO SAVE FOUND DATA FIRST
# Memomorize the values' ids to search for in current topology
node_label_name_in_graphml = ''
node_latitude_name_in_graphml = ''
node_longitude_name_in_graphml = ''
node_edge_bandwidth = ''
# For saving the current values
node_index_value     = ''
node_name_value      = ''
node_longitude_value = ''
node_latitude_value  = ''
edge_bandwidth_unit = ''
edge_bandwidth_temp_info = ''
# ID: value dictionaries
id_node_name_dict   = {}     # to hold all 'id: node_name_value' pairs
id_longitude_dict   = {}     # to hold all 'id: node_longitude_value' pairs
id_latitude_dict    = {}     # to hold all 'id: node_latitude_value' pairs

# FIND OUT WHAT KEYS ARE TO BE USED, SINCE THIS DIFFERS IN DIFFERENT GRAPHML TOPOLOGIES
for i in index_values_set:

    if i.attrib['attr.name'] == 'label' and i.attrib['for'] == 'node':
        node_label_name_in_graphml = i.attrib['id']
    if i.attrib['attr.name'] == 'Longitude':
        node_longitude_name_in_graphml = i.attrib['id']
    if i.attrib['attr.name'] == 'Latitude':
        node_latitude_name_in_graphml = i.attrib['id']
    if i.attrib['attr.name'] == 'LinkSpeed' and i.attrib['for'] == 'edge':
    	node_edge_bandwidth = i.attrib['id']
    if i.attrib['attr.name'] == 'LinkSpeedUnits' and i.attrib['for'] == 'edge':
    	edge_bandwidth_unit = i.attrib['id']
    if i.attrib['attr.name'] == 'LinkLabel' and i.attrib['for'] == 'edge':
    	edge_bandwidth_temp_info = i.attrib['id']

# Calculate THE AVERAGE LONGITUDE AND LATITUDE TO COVER NULL GEOGRAPHICAL DATA
longitude_set = []
latitude_set = []

for n in node_set:

    location_set = n.findall(ns + 'data')

    # Finally get all needed values
    for l in location_set:

        # Longitude data
        if l.attrib['key'] == node_longitude_name_in_graphml:
            longitude_set.append(float(l.text))
        # Latitude data
        if l.attrib['key'] == node_latitude_name_in_graphml:
            latitude_set.append(float(l.text))

defalult_longitude = round(np.mean(longitude_set), 5) if len(longitude_set) != 0 else 0.0
defalult_latitude = round(np.mean(latitude_set), 5) if len(latitude_set) != 0 else 0.0

# NOW PARSE ELEMENT SETS TO GET THE DATA FOR THE TOPO
# GET NODE_NAME DATA
# GET LONGITUDE DATK
# GET LATITUDE DATA
node_names = []
node_count = 0

for n in node_set:

    node_index_value = n.attrib['id']
    # Get all data elements residing under all node elements
    data_set = n.findall(ns + 'data')
    node_count = node_count + 1

    # Finally get all needed values
    for d in data_set:

        # Node name in python variable format
        if d.attrib['key'] == node_label_name_in_graphml:
            node_name_value = re.sub('[^a-zA-Z0-9_]', '', d.text)
            node_name_value = re.match('[^0-9_]+[a-zA-Z0-9_]*', node_name_value)
            if node_name_value is None:
            	node_name_value = 'NotGiven'
            else:
            	node_name_value = node_name_value.group()
            	if keyword.iskeyword(node_name_value) or node_name_value == 'None':
            		node_name_value = 'NotGiven'

            node_names.append(node_name_value)
        # Longitude data
        if d.attrib['key'] == node_longitude_name_in_graphml:
            node_longitude_value = d.text
        # Latitude data
        if d.attrib['key'] == node_latitude_name_in_graphml:
            node_latitude_value = d.text

        # Save ID: data couple
        id_node_name_dict[node_index_value] = node_name_value
        id_longitude_dict[node_index_value] = node_longitude_value if node_longitude_value != '' else defalult_longitude
        id_latitude_dict[node_index_value]  = node_latitude_value if node_latitude_value != '' else  defalult_latitude

cur_node = 1
# If the names of some nodes are same,
# They will be regarded as the same node in topology.
# Here checks whether there are same node names.
# If exist, change the node names into standard formant 's + No' (e.g. s11, s12).
if len(set(node_names)) != len(node_names) or 'NotGiven' in node_names:
	id_node_name_dict = {}
	for n in node_set:
		node_index_value = n.attrib['id']
		id_node_name_dict[node_index_value] = 's' + str(cur_node)
		cur_node = cur_node + 1

# STRING CREATION
# FIRST CREATE THE SWITCHES AND HOSTS
tempstring1 = ''
tempstring2 = ''
tempstring3 = ''
local_link_flag = 1

for i in range(0, len(id_node_name_dict)):

    # Create switch
    temp1 =  '    '
    temp1 += id_node_name_dict[str(i)]
    temp1 += " = net.addSwitch('s"
    temp1 += str(i+1)
    temp1 += "', cls=OVSKernelSwitch, dpid='"
    temp1 += (16-len(str(i+1))) * '0'
    temp1 += str(i+1)
    temp1 += "')\n"

    # Create corresponding host
    temp2 =  '    '
    temp2 += id_node_name_dict[str(i)]
    temp2 += "_host = net.addHost('h"
    temp2 += str((i+1))
    if i % 254 == 0:
    	ip_host_base = ip_host_base + 1
    temp2 += "', cls=Host, ip='10.0."
    temp2 += str(ip_host_base)
    temp2 += '.'
    temp2 += str((i % 254) + 1)
    temp2 += "', defaultRoute=None"
    temp2 += ")\n"
    tempstring1 += temp1
    tempstring2 += temp2

    # Link each switch and its host...
    # Local links bewteen switch and its corresponding host is 1000 Mbps by default
    # Linkname = 'local_link'
    linkname = '    local_link'
    value = " = {'bw':1000.0, 'delay':'0ms'}\n"
    if local_link_flag:
    	tempstring3 += linkname
    	tempstring3 += value
    	local_link_flag = 0
    temp3 =  '    net.addLink('
    temp3 += id_node_name_dict[str(i)]
    temp3 += ', '
    temp3 += id_node_name_dict[str(i)]
    temp3 += "_host, cls=TCLink, **"
    # Temp3 += id_node_name_dict[str(i)] + '_local'
    temp3 += "local_link"
    temp3 += ")"
    temp3 += '\n'
    tempstring3 += temp3

outputstring_to_be_exported += outputstring_2a
outputstring_to_be_exported += tempstring1
outputstring_to_be_exported += outputstring_2b
outputstring_to_be_exported += tempstring2
outputstring_to_be_exported += outputstring_2c

tempstring3 += '\n'
outputstring_to_be_exported += tempstring3

# SECOND CALCULATE DISTANCES BETWEEN SWITCHES,
# Set global bandwidth and create the edges between switches,
# And link each single host to its corresponding switch

tempstring4 = ''
tempstring5 = ''
distance = 0.0
latency = 0.0
citylinknum = 0
edge_count = 0

for e in edge_set:

    # GET IDS FOR EASIER HANDLING
    src_id = e.attrib['source']
    dst_id = e.attrib['target']
    bandwidth_from_text = ''
    bandwidth_unit = ''
    bandwidth_temp_info = ''
    edge_data = e.findall(ns + 'data')
    edge_count = edge_count + 1

    for e_d in edge_data:

    	if e_d.attrib['key'] == node_edge_bandwidth:
    		bandwidth_from_text = e_d.text
    	if e_d.attrib['key'] == edge_bandwidth_unit:
    		bandwidth_unit = e_d.text
    	if e_d.attrib['key'] == edge_bandwidth_temp_info:
    		bandwidth_temp_info = e_d.text

    # CALCULATE DELAYS

    #    CALCULATION EXPLANATION
    #
    #    formula: (for distance)
    #    dist(SP,EP) = arccos{ sin(La[EP]) * sin(La[SP]) + cos(La[EP]) * cos(La[SP]) * cos(Lo[EP] - Lo[SP])} * r
    #    r = 6378.137 km
    #
    #    formula: (speed of light, not within a vacuumed box)
    #    v = 1.97 * 10**8 m/s
    #
    #    formula: (latency being calculated from distance and light speed)
    #    t = distance / speed of light
    #    t (in ms) = ( distance in km * 1000 (for meters) ) / ( speed of light / 1000 (for ms))

    #    ACTUAL CALCULATION: implementing this was no fun.

    latitude_src	= math.radians(float(id_latitude_dict[src_id]))
    latitude_dst	= math.radians(float(id_latitude_dict[dst_id]))
    longitude_src	= math.radians(float(id_longitude_dict[src_id]))
    longitude_dst	= math.radians(float(id_longitude_dict[dst_id]))

    first_product               = math.sin(latitude_dst) * math.sin(latitude_src)
    second_product_first_part   = math.cos(latitude_dst) * math.cos(latitude_src)
    second_product_second_part  = math.cos(longitude_dst - longitude_src)

    # If some latitude or longtitude data is empty, acos may fail,
    # Use random latency instead.
    try:
    	distance = math.acos(first_product + (second_product_first_part * second_product_second_part)) * 6378.137
    	latency = round(( distance * 1000 ) / ( 197000 ), 4)
    except ValueError as latitude_NULL:
    	latency = round(random.uniform(0, 5), 4)

    # t (in ms) = ( distance in km * 1000 (for meters) ) / ( speed of light / 1000 (for ms))
    # t         = ( distance       * 1000              ) / ( 1.97 * 10**8   / 1000         )
    
    # Set the DEFAULT bandwidth first,
    # If the bandwidth data exist, change it later.
    real_bandwidth = float(bandwidth_argument) if bandwidth_argument != '' else 128.0

    # Get the edge bandwidth data from GRAPHML fields.
    if bandwidth_from_text != '':
        real_bandwidth = float(bandwidth_from_text)
        # Check whether the bandwidth unit is Gbps.
        if bandwidth_unit == 'G':
        	real_bandwidth = real_bandwidth * 1024

  	# if the bandwidth cannot be found in LinkSpeed, use LinkLabel instead
    elif bandwidth_from_text == '' and bandwidth_temp_info != '':
    	bandwidth_digits = re.findall("\d+[.?\d+]", bandwidth_temp_info)
    	for digits in range (0, len(bandwidth_digits)):
    		bandwidth_digits[digits] = float(bandwidth_digits[digits])
    	if len(bandwidth_digits) > 0:
    		real_bandwidth = np.mean(bandwidth_digits)
    	if bandwidth_temp_info.find('Gb') >= 0:
    		real_bandwidth = real_bandwidth * 1024

    # Mininet does not support custom bandwidth setting that excceed 1000 Mbps.
    if real_bandwidth > 1000.0:
    	real_bandwidth = 1000.0

    citylinknum = citylinknum + 1
    # Link all corresponding switches with each other
    linkname = '    CityLink' + str(citylinknum)
    value = " = {'bw':"
    value += str(real_bandwidth)
    value += ", 'delay':'"
    value += str(latency)
    value += "ms'}\n"
    tempstring4 += linkname
    tempstring4 += value
    temp4 =  '    net.addLink('
    temp4 += id_node_name_dict[src_id]
    temp4 += ', '
    temp4 += id_node_name_dict[dst_id]
    temp4 += ", cls=TCLink, **"
    temp4 += "CityLink"
    temp4 += str(citylinknum)
    temp4 += ")"
    temp4 += '\n'
    # Next line so i dont have to look up other possible settings
    # temp4 += "ms', loss=0, max_queue_size=1000, use_htb=True)"
    tempstring4 += temp4

tempstring5 = ''
for i in range(0, len(id_node_name_dict)):
	temp5 = "    net.get('s"
	temp5 += str(i+1)
	temp5 += "').start([c0])\n"
	tempstring5 += temp5

outputstring_to_be_exported += tempstring4
outputstring_to_be_exported += outputstring_3
outputstring_to_be_exported += tempstring5
outputstring_to_be_exported += outputstring_4a

# Configure switch IP
switch_addr = ''
for i in range(0, len(id_node_name_dict)):
	addr = '    '
	addr += id_node_name_dict[str(i)]
	addr += ".cmd('ifconfig s"
	addr += str(i+1)
	addr += " 10.0."
	if i % 254 == 0:
		ip_switch_base = ip_switch_base + 1
	addr += str(ip_switch_base)
	addr += '.'
	addr += str((i % 254) + 1)
	addr += "')"
	addr += '\n'
	switch_addr += addr

outputstring_to_be_exported += switch_addr
outputstring_to_be_exported += outputstring_4b

# GENERATION FINISHED, WRITE STRING TO FILE
outputfile = ''
if output_file_name == '':
    output_file_name = input_file_name + '-Mininet-Topo.py'

outputfile = open(output_file_name, 'w')
outputfile.write(outputstring_to_be_exported)
outputfile.close()

print "Generate \033[0;33m" + input_file_name + "\033[0m SUCCESSFUL! \033[0;36m" + \
	  "(" + str(node_count) + " Switches, " + \
	  str(edge_count) + " Links)\033[0m"

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-04-29 12:06:21  更:2022-04-29 12:06:25 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/28 8:09:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计