Example #1
0
def run():
    "Create and test a simple network"

    c = RemoteController('c', '118.138.235.213', 6633)
    topo = HypercubeTopo(n=3)
    net = Mininet(topo=topo, controller=None)
    net.addController(c)
    net.start()
    print "Testing network connectivity"

    for _ in range(2):
        net.pingAll()

    (first, last) = net.get('h1', 'h' + str(len(net.hosts)))
    net.iperf((first, last))

    CLI(net)

    time.sleep(120)

    node1 = net.get('s1')
    node2 = net.get('s2')
    net.delLinkBetween(node1, node2)

    for _ in range(2):
        net.pingAll()

    (first, last) = net.get('h1', 'h' + str(len(net.hosts)))
    net.iperf((first, last))

    CLI(net)

    net.stop()
Example #2
0
def run():
    "Create and test a simple network"

    c = RemoteController('c', '118.138.235.213', 6633)
    topo = LinearTopo(k=4)
    net = Mininet(topo=topo, controller=None)
    net.addController(c)
    net.start()
    print "Testing network connectivity"

    for _ in range(1):
        net.pingAll()

    for i in irange(1, len(net.hosts)):
        for j in irange(i + 1, len(net.hosts)):
            (hi, hj) = net.get('h' + str(i), 'h' + str(j))
            try:
                res = net.iperf((hi, hj))
            except:
                print('Connection between host%d and host%d blocked' % (i, j))
            else:
                print('Bandwidth between host%d and host%d is %s' %
                      (i, j, str(res)))

    CLI(net)

    time.sleep(10)

    node1 = net.get('s1')
    node2 = net.get('s2')
    net.delLinkBetween(node1, node2)

    for _ in range(1):
        net.pingAll()

    for i in reversed(irange(1, len(net.hosts))):
        for j in reversed(irange(1, i - 1)):
            (hi, hj) = net.get('h' + str(i), 'h' + str(j))
            try:
                res = net.iperf((hi, hj))
            except:
                print('Connection between host%d and host%d blocked' % (i, j))
            else:
                print('Bandwidth between host%d and host%d is %s' %
                      (i, j, str(res)))

    CLI(net)

    net.stop()
Example #3
0
def runner():
    "Create and run a custom topo with adjustable link parameters"
    topo = CompleteGraphTopo()
    c = RemoteController('c', '127.0.0.1', 6633)
    net = Mininet(topo=topo,
                  controller=None,
                  host=CPULimitedHost,
                  link=TCLink,
                  waitConnected=True,
                  autoSetMacs=True)

    net.addController(c)
    net.start()
    topo.enable_BFD(net)
    CLI(net)

    s1 = net.getNodeByName("s1")
    s2 = net.getNodeByName('s2')
    s3 = net.getNodeByName('s3')
    s6 = net.getNodeByName("s6")
    s7 = net.getNodeByName("s7")
    s8 = net.getNodeByName("s8")
    s9 = net.getNodeByName("s9")
    # h1 ping h9 & h9 ping h1
    net.delLinkBetween(s2, s9)
    #h1 ping h7
    net.delLinkBetween(s1, s3)
    #h3 ping h8
    net.delLinkBetween(s3, s7)
    #h8 ping h6
    net.delLinkBetween(s6, s8)
    # net.stop()

    test_pair = [(1, 9), (1, 7), (3, 8), (8, 6), (9, 1)]
    result = {'hop': [], 'delay': [], 'throughput': []}
    for pair in test_pair:
        host1, host2 = pair
        src = net.getNodeByName('h' + str(host1 + 1))
        dst = net.getNodeByName('h' + str(host2 + 1))
        sent, received, min_, avg = ping(net, [src, dst], 1024, 2)
        print 'Delay ', avg, ' for ', pair
        result['delay'].append(float(avg))
        hop = get_path_length(net, src, dst)
        print 'Hop ', hop, ' for ', pair
        result['hop'].append(float(hop))
        time, datasize_tx, bitrate = doIperf(net, src, dst)
        print 'Throughput ', bitrate, ' for ', pair
        result['throughput'].append(float(bitrate))
    print 'Average Number of Hop:', getavg(result['hop'])
    print 'Average Delay: ', getavg(result['delay'])
    print 'Throughput:', getavg(result['throughput'])
    net.stop()
Example #4
0
    def test_verifying_pingall_with_connection_remove_between_switches(self,switches=4):
        try:
	    topo = LinearTopo(switches)
            net = Mininet(topo=topo)
	    #net.build()
            net.start()
	    ctrl = net.addController( 'onos', controller=RemoteController, ip=self.controller, port=6653)
            for switch in net.switches:
                switch.start( [ctrl] )
            response = net.pingAll()
            log.info('Pingall response before link delete is %s'%response)
            assert_equal(response,0.0)
            log.info('Deleting link between switches s1 and s2')
            net.delLinkBetween(net.switches[0], net.switches[1], )
            response = net.pingAll()
            log.info('Pingall response after the link delete is is %s'%response)
            assert_not_equal(response,0.0)
            net.stop()
        except Exception as Error:
            log.info('Got error %s while creating topology'%Error)
            Cleanup.cleanup()
            raise
        Cleanup.cleanup()
Example #5
0
class NetManager(object):
    def __init__(self):
        self.net = Mininet(controller=Controller, autoSetMacs=True)

    def getNet(self):
        return self.net

    def setNet(self, net):
        self.net = net

    def addController(self, name):
        self.addController(name)

    def addHost(self, name, IP=None):
        if self.isHost(name):
            return False
        self.net.addHost(name, ip=IP)
        return True

    def addSwitch(self, name):
        if self.isSwitch(name):
            return False
        self.net.addSwitch(name)
        return True

    def addLink(self, node1, node2, params=None):
        if not self.isSwitch(node1) and not self.isHost(node1):
            return False
        if not self.isSwitch(node2) and not self.isHost(node2):
            return False

        # 设置默认值
        if params == None:
            self.net.addLink(node1, node2)
            #return True
        else:
            # 设置link 参数
            bw = 10000 if params.get('bw') is None else int(params.get('bw'))
            delay = 5 if params.get('delay') is None else int(params.get('bw'))
            loss = 0 if params.get('loss') is None else int(params.get('loss'))
            max_queue_size = 1000 if params.get(
                'max_queue_size') is None else int(
                    params.get('max_queue_size'))
            jitter = params.get('max_queue_size')
            self.net.addLink(node1,
                             node2,
                             bw=bw,
                             loss=loss,
                             delay=delay,
                             max_queue_size=max_queue_size,
                             jitter=jitter)

        # node 可能是 switch,configHost 会判断
        self.configHost(node1)
        self.configHost(node2)
        self.startSwitch()

    def delHost(self, name):
        if self.isHost(name) == False:
            return False
        node = self.net.get(name)
        #node.terminate()
        self.net.hosts.remove(node)
        del self.net.nameToNode[node.name]
        #self.net.delHost(node)

        return True

    def delSwitch(self, name):
        if self.isSwitch(name) == False:
            return False
        node = self.net.get(name)
        #self.net.delSwitch(node)
        self.net.switches.remove(node)
        del self.net.nameToNode[node.name]

    def delNode(self, name):
        if self.isHost(name) == False and self.isSwitch(name) == False:
            return False
        node = self.net.get(name)
        nodes = (self.net.hosts if node in self.net.hosts else
                 (self.net.switches if node in self.net.switches else []))
        self.delNodeLink(node)
        #node.stop( deleteIntfs=True )
        nodes.remove(node)
        del self.net.nameToNode[node.name]

    def delNodeLink(self, node):
        for intf in node.intfList():
            link = intf.link
            if link:
                self.delLink(link.intf1.node.name, link.intf2.node.name)

    def delLink(self, node1, node2):
        srcnode = self.net.get(node1)
        destnode = self.net.get(node2)
        if node1 == None or node2 == None:
            return False
        self.net.delLinkBetween(srcnode, destnode)

    def linkCount(self, node):
        cnt = {'host': 0, 'switch': 0}
        for intf in node.intfList():
            link = intf.link
            if not link:
                continue
            node1, node2 = link.intf1.node, link.intf2.node
            if node1 == node:
                if self.isHost(node2.name):
                    cnt['host'] += 1
                if self.isSwitch(node2.name):
                    cnt['switch'] += 1
            else:
                if self.isHost(node1.name):
                    cnt['host'] += 1
                if self.isSwitch(node1.name):
                    cnt['switch'] += 1
        return cnt

    def getHostInfo(self, hostName):
        host = self.net.getNodeByName(hostName)
        info(host.name, host.intfs, host.params)
        return host.params

    def nextIP(self):
        return self.ipAdd(
            self.net.nextIP,
            ipBaseNum=self.net.ipBaseNum,
            prefixLen=self.net.prefixLen) + '/%s' % self.net.prefixLen

    def ipAdd(self, i, prefixLen=8, ipBaseNum=0x0a000000):
        imax = 0xffffffff >> prefixLen
        assert i <= imax, 'Not enough IP addresses in the subnet'
        mask = 0xffffffff ^ imax
        ipnum = (ipBaseNum & mask) + i
        return self.ipStr(ipnum)

    def ipStr(self, ip):
        w = (ip >> 24) & 0xff
        x = (ip >> 16) & 0xff
        y = (ip >> 8) & 0xff
        z = ip & 0xff
        return "%i.%i.%i.%i" % (w, x, y, z)

    def startSwitch(self):
        net = self.net
        for switch in net.switches:
            info(switch.name + ' ')
            switch.start(net.controllers)
        started = {}
        for swclass, switches in groupby(
                sorted(net.switches, key=lambda s: str(type(s))), type):
            switches = tuple(switches)
            if hasattr(swclass, 'batchStartup'):
                success = swclass.batchStartup(switches)
                started.update({s: s for s in success})
        if net.waitConn:
            net.waitConnected()

    def configHost(self, name, ip=None, mac=None):
        node = self.net.getNodeByName(name)
        info('config' + name)
        if node == None:
            return
        intf = node.defaultIntf()
        if intf:
            node.configDefault()
        else:
            node.configDefault(ip=None, mac=None)
        if ip != None:
            node.setIP(ip)
        if mac != None:
            node.setMac(mac)

    # 判断 name 是否在网络里
    def isHost(self, name):
        nodes = getattr(self.net, 'hosts')
        for n in nodes:
            if name == n.name:
                return True
        return False

    def isSwitch(self, name):
        nodes = getattr(self.net, 'switches')
        for n in nodes:
            if name == n.name:
                return True

        return False