2

我使用 mininet python 脚本构建了一个简单的网络拓扑。但是,我想通过使用 networkX 在 mininet 脚本中构建拓扑来扩展此代码。因此,首先,我应该将网络导入为 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连接以构建拓扑吗?

感谢您的帮助。

4

1 回答 1

1

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

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 地址、控制器......

于 2020-04-16T12:34:58.653 回答