Revision 496b5f9e mininet/net.py

View differences:

mininet/net.py
52 52
import os
53 53
import re
54 54
import signal
55
from subprocess import call
56
import sys
57 55
from time import sleep
58 56

  
57
from mininet.cli import CLI
59 58
from mininet.log import lg
60 59
from mininet.node import KernelSwitch, OVSKernelSwitch
61 60
from mininet.util import quietRun, fixLimits
......
490 489
    def interact( self ):
491 490
        "Start network and run our simple CLI."
492 491
        self.start()
493
        result = MininetCLI( self )
492
        result = CLI( self )
494 493
        self.stop()
495 494
        return result
496

  
497

  
498
class MininetCLI( object ):
499
    "Simple command-line interface to talk to nodes."
500
    cmds = [ '?', 'help', 'nodes', 'net', 'sh', 'ping_all', 'exit', \
501
            'ping_pair', 'iperf', 'iperf_udp', 'intfs', 'dump' ]
502

  
503
    def __init__( self, mininet ):
504
        self.mn = mininet
505
        self.nodemap = {} # map names to Node objects
506
        for node in self.mn.nodes.values():
507
            self.nodemap[ node.name ] = node
508
        for cname, cnode in self.mn.controllers.iteritems():
509
            self.nodemap[ cname ] = cnode
510
        self.nodelist = self.nodemap.values()
511
        self.run()
512

  
513
    # Disable pylint "Unused argument: 'arg's'" messages.
514
    # Each CLI function needs the same interface.
515
    # pylint: disable-msg=W0613
516

  
517
    # Commands
518
    def help( self, args ):
519
        "Semi-useful help for CLI."
520
        helpStr = ( 'Available commands are:' + str( self.cmds ) + '\n'
521
                   'You may also send a command to a node using:\n'
522
                   '  <node> command {args}\n'
523
                   'For example:\n'
524
                   '  mininet> h0 ifconfig\n'
525
                   '\n'
526
                   'The interpreter automatically substitutes IP '
527
                   'addresses\n'
528
                   'for node names, so commands like\n'
529
                   '  mininet> h0 ping -c1 h1\n'
530
                   'should work.\n'
531
                   '\n\n'
532
                   'Interactive commands are not really supported yet,\n'
533
                   'so please limit commands to ones that do not\n'
534
                   'require user interaction and will terminate\n'
535
                   'after a reasonable amount of time.\n' )
536
        print( helpStr )
537

  
538
    def nodes( self, args ):
539
        "List all nodes."
540
        nodes = ' '.join( [ node.name for node in sorted( self.nodelist ) ] )
541
        lg.info( 'available nodes are: \n%s\n' % nodes )
542

  
543
    def net( self, args ):
544
        "List network connections."
545
        for switchDpid in self.mn.topo.switches():
546
            switch = self.mn.nodes[ switchDpid ]
547
            lg.info( '%s <->', switch.name )
548
            for intf in switch.intfs:
549
                node = switch.connection[ intf ]
550
                lg.info( ' %s' % node.name )
551
            lg.info( '\n' )
552

  
553
    def sh( self, args ):
554
        "Run an external shell command"
555
        call( [ 'sh', '-c' ] + args )
556

  
557
    def pingAll( self, args ):
558
        "Ping between all hosts."
559
        self.mn.pingAll()
560

  
561
    def pingPair( self, args ):
562
        "Ping between first two hosts, useful for testing."
563
        self.mn.pingPair()
564

  
565
    def iperf( self, args ):
566
        "Simple iperf TCP test between two hosts."
567
        self.mn.iperf()
568

  
569
    def iperfUdp( self, args ):
570
        "Simple iperf UDP test between two hosts."
571
        udpBw = args[ 0 ] if len( args ) else '10M'
572
        self.mn.iperfUdp( udpBw )
573

  
574
    def intfs( self, args ):
575
        "List interfaces."
576
        for node in self.mn.nodes.values():
577
            lg.info( '%s: %s\n' % ( node.name, ' '.join( node.intfs ) ) )
578

  
579
    def dump( self, args ):
580
        "Dump node info."
581
        for node in self.mn.nodes.values():
582
            lg.info( '%s\n' % node )
583

  
584
    # Re-enable pylint "Unused argument: 'arg's'" messages.
585
    # pylint: enable-msg=W0613
586

  
587
    def run( self ):
588
        "Read and execute commands."
589
        lg.warn( '*** Starting CLI:\n' )
590
        while True:
591
            lg.warn( 'mininet> ' )
592
            inputLine = sys.stdin.readline()
593
            if inputLine == '':
594
                break
595
            if inputLine[ -1 ] == '\n':
596
                inputLine = inputLine[ :-1 ]
597
            cmd = inputLine.split( ' ' )
598
            first = cmd[ 0 ]
599
            rest = cmd[ 1: ]
600
            if first in self.cmds and hasattr( self, first ):
601
                getattr( self, first )( rest )
602
            elif first in self.nodemap and rest != []:
603
                node = self.nodemap[ first ]
604
                # Substitute IP addresses for node names in command
605
                rest = [ self.nodemap[ arg ].IP()
606
                    if arg in self.nodemap else arg
607
                    for arg in rest ]
608
                rest = ' '.join( rest )
609
                # Interactive commands don't work yet, and
610
                # there are still issues with control-c
611
                lg.warn( '*** %s: running %s\n' % ( node.name, rest ) )
612
                node.sendCmd( rest )
613
                while True:
614
                    try:
615
                        done, data = node.monitor()
616
                        lg.info( '%s\n' % data )
617
                        if done:
618
                            break
619
                    except KeyboardInterrupt:
620
                        node.sendInt()
621
            elif first == '':
622
                pass
623
            elif first in [ 'exit', 'quit' ]:
624
                break
625
            elif first == '?':
626
                self.help( rest )
627
            else:
628
                lg.error( 'CLI: unknown node or command: < %s >\n' % first )
629
            #lg.info( '*** CLI: command complete\n' )
630
        return 'exited by user command'

Also available in: Unified diff