如何在mininet中使用networkX构建拓扑



我使用mininet-python脚本构建了一个简单的网络拓扑。但是,我想通过使用networkX在mininet脚本中构建拓扑来扩展此代码。因此,首先,我应该将networx导入为nx。使用networkX的原因是为了在任何源主机和目标主机之间找到非常简单的最短路径。

拓扑代码:

#!/usr/bin/env python
from mininet.net import Mininet  # IMPORT Mininet
from mininet.cli import CLI       ## IMPORT COMMAND LINE 
from mininet.link import  TCLink
from mininet.log import setLogLevel        # FOR DEPUG
from mininet.node import RemoteController  # TO BE ABLE O ADD REMOTE CONTROLLER
net = Mininet(link=TCLink)
# ADDING hosts with given MAC
h1 = net.addHost("h1",mac='00:00:00:00:00:01')
h2 = net.addHost("h2",mac='00:00:00:00:00:02')
h3 = net.addHost("h3",mac='00:00:00:00:00:03')
# ADDING Switch
s1 = net.addSwitch("s1")
net.addLink(h1,s1,bw=10, delay='5ms' )
net.addLink(h2,s1,bw=10, delay='5ms' )
net.addLink(h3,s1,bw=10, delay='5ms' )
# ADDING COTROLLER    
net.addController("c0",controller=RemoteController,ip="127.0.0.1",port=6633)
# START Mininet       
net.start()
CLI(net)
net.stop() 
# EXIT Miminet

你们中的任何人都可以帮助我在构建拓扑结构时修改并连接networkX和mininet吗?

感谢您的协助。

对于仍然对此感兴趣的人:

Networkx是一个很好的构建网络的库,它有一堆非常有用的预实现函数。我在论文中也做了同样的事情,我有一个networkx图,我用它来简化一些计算,并以networkx图为模型以编程方式构建了一个小型网络拓扑。幸运的是,这很容易做到,只需要通过networkx节点和边进行循环。Mininet邮件列表上曾经有一个链接到这样一段代码,但我看到它现在已经死了。请随意使用我的代码:

from mininet.net import Mininet
import networkx as nx
def construct_mininet_from_networkx(graph, host_range):
""" Builds the mininet from a networkx graph.
:param graph: The networkx graph describing the network
:param host_range: All switch indices on which to attach a single host as integers
:return: net: the constructed 'Mininet' object
"""
net = Mininet()
# Construct mininet
for n in graph.nodes:
net.addSwitch("s_%s" % n)
# Add single host on designated switches
if int(n) in host_range:
net.addHost("h%s" % n)
# directly add the link between hosts and their gateways
net.addLink("s_%s" % n, "h%s" % n)
# Connect your switches to each other as defined in networkx graph
for (n1, n2) in graph.edges:
net.addLink('s_%s' % n1,'s_%s' % n2)
return net

我试着尽可能地清理我的代码。这应该是一个很好的框架。这假设networkx图只包含纯网络拓扑。连接到此的主机是通过host_range参数添加的,在我的情况下,该参数包含我要连接到主机的交换机的索引(对应于networkx图中的节点号(。添加您需要的任何内容,例如,每个主机连接一个以上的交换机、链路上的特定带宽和延迟参数、mac地址、控制器。。。

最新更新