基于Mininet的网络拓扑搭建

#!/usr/bin/env python

import logging
import os
import time
import thread
import multiprocessing

from mininet.net import Mininet
from mininet.node import RemoteController
from mininet.cli import CLI
from mininet.log import setLogLevel
from mininet.link import TCLink
from mininet.topo import Topo

logger = logging.getLogger(__name__)

REQUEST_NUMBER = 3

class NMTree(Topo):
logger.debug("Class NMTree")
switchList = []
hostList = []

def __init__(self,level_number,node_number):
logger.info("Class NMTree init")
self.level_number = level_number
self.node_number = node_number
#init Topo
Topo.__init__(self)

def create_topo(self):
self.create_switch(self.level_number)
self.create_host(self.level_number,self.node_number)

def create_switch(self,number):
logger.debug("Create switch")
for i in range(1,number+1):
index = 0
prefix = "s"+str(i)
if i == 1:
self.switchList.append(self.addSwitch(prefix + str(i)))
else:
for k in range(1,2**(i-1)+1):
self.switchList.append(self.addSwitch(prefix+str(k)))
self.addLink(self.switchList[2**(i-2)+index-1],self.switchList[2**(i-1)+k-2])
if k % 2 == 0:
index += 1

def create_host(self,level,node):
logger.debug("Create host")
for i in range(1,2**(level-1)+1):
prefix = "h"+str(i)
for j in range(1,node+1):
self.hostList.append(self.addHost(prefix+str(j)))
self.addLink(self.switchList[2**(level-1)+i-2],self.hostList[(i-1)*node+j-1])


def start_log_server(net, topo):
time.sleep(15)

# h21 ip = 10.0.0.4
h21 = net.get(topo.hostList[3])
# Start ITGLog server on h21
h21.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGLog")


def start_recv_server(net, topo):
time.sleep(10)
# h23 ip = 10.0.0.6
h23 = net.get(topo.hostList[5])
# Start ITGRecv server on h23
h23.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGRecv")


def generate_traffic_by_n_itg(net, topo):
time.sleep(20)

pool = multiprocessing.Pool(processes=3)
# h13 ip = 10.0.0.3
h13 = net.get(topo.hostList[2])

# Start ITGSend server on h13 and send TCP streams to h23
pool.apply_async(h13.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGSend -a 10.0.0.6 -rp 10001 -c 100 -C 10 -t 80000 \
-T TCP -l send_tcp_log -L 10.0.0.4 TCP -x recv_tcp_log -X 10.0.0.4 TCP"))
logger.info("end send tcp")
# Start ITGSend server on h13 and send UDP streams to h23
pool.apply_async(h13.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGSend -a 10.0.0.6 -rp 10001 -c 100 -C 10 -t 15000 \
-T UDP -l send_udp_log -L 10.0.0.4 UDP -x recv_udp_log -X 10.0.0.4 UDP"))
logger.info("end send udp")
# Start ITGSend server on h13 and send ICMP streams to h23
pool.apply_async(h13.cmdPrint("~/D-ITG-2.8.1-r1023/bin/ITGSend -a 10.0.0.6 -rp 10001 -c 100 -C 10 -t 5000 \
-T ICMP -l send_icmp_log -L 10.0.0.4 TCP -x recv_icmp_log -X 10.0.0.4 TCP"))
logger.info("end send icmp")
pool.close()
pass


def create_topo():
logging.debug("Create NMTree")
topo = NMTree(3,3)
topo.create_topo()
logging.debug("Start Mininet")
controller_ip = "127.0.0.1"
controller_port = 6633
net = Mininet(topo=topo, link=TCLink, controller=None, autoSetMacs=True)
net.addController('controller',controller=RemoteController,ip=controller_ip, port=controller_port)
net.start()
thread.start_new_thread(generate_traffic_by_n_itg, (net, topo))
thread.start_new_thread(start_log_server, (net, topo))
thread.start_new_thread(start_recv_server, (net, topo))
CLI(net)
net.stop()

if __name__ == '__main__':
setLogLevel('info')
if os.getuid() != 0:
logger.debug("You are NOT root")
elif os.getuid() == 0:
create_topo()

上一篇:HDU 3342 Legal or Not


下一篇:拓扑排序