From 8d3c2859e4d41a60f92e642be84c996f1ea5b3ae Mon Sep 17 00:00:00 2001 From: Brandon Heller Date: Sat, 13 Mar 2010 06:30:53 -0800 Subject: [PATCH] Add port status change command --- mininet/cli.py | 11 ++++++++++- mininet/net.py | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mininet/cli.py b/mininet/cli.py index 18e93b3..688f4fb 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -33,7 +33,7 @@ Bugs/limitations: from subprocess import call from cmd import Cmd -from mininet.log import info, output +from mininet.log import info, output, error class CLI( Cmd ): "Simple command-line interface to talk to nodes." @@ -147,6 +147,15 @@ class CLI( Cmd ): for node in self.nodelist: output( '%s\n' % node ) + def do_link( self, args ): + "Bring a link up or down." + if len(args) != 3: + error( 'invalid number of args: link [up down] end1 end2\n' ) + elif args[ 0 ] not in [ 'up', 'down' ]: + error( 'invalid type: link [up down] end1 end2\n' ) + else: + self.mn.link( *args ) + def do_exit( self, args ): "Exit" return 'exited by user command' diff --git a/mininet/net.py b/mininet/net.py index e8a2ca1..5e36791 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -483,6 +483,33 @@ class Mininet( object ): "Run iperf UDP test." return self.iperf( l4Type='UDP', udpBw=udpBw ) + def link( self, type, src, dst ): + """Change link status. + type: string {up, down} + src: string + dst: string""" + if src not in self.nameToNode: + error( 'src not in network: %s\n' % src ) + elif dst not in self.nameToNode: + error( 'dst not in network: %s\n' % dst ) + else: + srcNode = self.nameToNode[ src ] + dstNode = self.nameToNode[ dst ] + srcID = int( src[ 1: ] ) + dstID = int( dst[ 1: ] ) + if self.topo.port( srcID, dstID ) is None: + error( 'src and dst not connected: %s %s\n' % ( src, dst) ) + else: + srcPort, dstPort = self.topo.port( srcID, dstID ) + srcIntf = srcNode.intfs[ srcPort ] + dstIntf = dstNode.intfs[ dstPort ] + result = srcNode.cmd( [ 'ifconfig', srcIntf, type ] ) + if result: + error( 'link src status change failed: %s\n' % result ) + result = dstNode.cmd( [ 'ifconfig', dstIntf, type ] ) + if result: + error( 'link dst status change failed: %s\n' % result ) + def interact( self ): "Start network and run our simple CLI." self.start()