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

问题描述 投票:1回答:1

我使用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并建立拓扑吗?

感谢您的协助。

python networkx sdn mininet ryu
1个回答
0
投票

对于仍对此感兴趣的人:

Networkx是用于构建网络的出色库,并且具有许多非常有用的预实现功能。我为论文做了同样的事情,我有一个networkx图,用来简化一些计算,并以networkx图为模型以编程方式构建了一个mininet拓扑。幸运的是,这很容易做到,只需要遍历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地址,控制器...

© www.soinside.com 2019 - 2024. All rights reserved.