2016-04-29 143 views
1

我想用--custom选项在Mininet中运行.py脚本。我的代码如下:如何在Mininet中运行sudo mn --custom选项?

from mininet.topo import Topo 
from mininet.net import Mininet 
from mininet.util import dumpNodeConnections 
from mininet.log import setLogLevel 
from mininet.util import irange 

class LinearTopo(Topo): 
    "Linear topology of k switches, with n hosts per switch." 

    def build(self, k=2, n=1, **_opts): 
     """k: number of switches 
      n: number of hosts per switch""" 
     self.k = k 
     self.n = n 

     if n == 1: 
      genHostName = lambda i, j: 'h%s' % i 
     else: 
      genHostName = lambda i, j: 'h%ss%d' % (j, i) 

     lastSwitch = None 
     for i in irange(1, k): 
      # Add switch 
      switch = self.addSwitch('s%s' % i) 
      # Add hosts to switch 
      for j in irange(1, n): 
       host = self.addHost(genHostName(i, j)) 
       self.addLink(host, switch) 
      # Connect switch to previous 
      if lastSwitch: 
       self.addLink(switch, lastSwitch) 
      lastSwitch = switch 


def simpleTest(): 
     "Create and test a simple network" 
     topo = LinearTopo(k=4,n=8) 
     net = Mininet(topo) 
     net.start() 
     print "Dumping host connections" 
     dumpNodeConnections(net.hosts) 
     print "Testing network connectivity" 
     net.pingAll() 
     net.stop() 

if __name__ == '__main__': 
     # Tell mininet to print useful information 
     setLogLevel('info') 
     simpleTest() 

当我尝试:

sudo mn --custom topo.py --topo LinearTopo 

我得到以下错误:

*** Cleanup complete. 
[email protected]:~$ sudo mn --custom topo.py --topo LinearTopo 
-------------------------------------------------------------------------------- 
Caught exception. Cleaning up... 

Exception: Invalid topo name LinearTopo 
-------------------------------------------------------------------------------- 
*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes 
killall controller ofprotocol ofdatapath ping nox_core lt-nox_core ovs-openflowd ovs-controller udpbwtest mnexec ivs 2> /dev/null 
killall -9 controller ofprotocol ofdatapath ping nox_core lt-nox_core ovs-openflowd ovs-controller udpbwtest mnexec ivs 2> /dev/null 
pkill -9 -f "sudo mnexec" 
*** Removing junk from /tmp 
rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log 
*** Removing old X11 tunnels 
*** Removing excess kernel datapaths 
ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/' 
*** Removing OVS datapaths 
ovs-vsctl --timeout=1 list-br 
ovs-vsctl --timeout=1 list-br 
*** Removing all links of the pattern foo-ethX 
ip link show | egrep -o '([-_.[:alnum:]]+-eth[[:digit:]]+)' 
ip link show 
*** Killing stale mininet node processes 
pkill -9 -f mininet: 
*** Shutting down stale tunnels 
pkill -9 -f Tunnel=Ethernet 
pkill -9 -f .ssh/mn 
rm -f ~/.ssh/mn/* 
*** Cleanup complete. 

你能告诉我为什么得到错误:无效的地形名称?

def

回答

1

添加行不应该有缩进

TOPOS = {'LinearTopo' : (lambda : LinearTopo())} 

现在可以执行

sudo mn --custom topo.py --topo LinearTopo 

你写了上面的程序可以直接执行使用

sudo python <file_name>.py 

如果您需要使用带有值的sudo mn运行,更新代码如图所示

TOPOS = {'LinearTopo' : (lambda : LinearTopo(4,5))} 
+0

非常感谢Karthik! – KristinaP

+0

我还有一个问题。你可能知道为什么拓扑广告只有k = 2个交换机和n = 1个主机,而不是k = 4个交换机和n = 8个主机,因为它是在def simpleTest()中指定的?我尝试在def simpleTest()之前和之后添加TOPOS,但结果相同。 – KristinaP

+0

@KristinaP请参阅更新的答案 –