这是整个项目的代码部分,对大家来说都很重要
总体详见:【基本中型网络的仿真(RYU+Mininet的SDN架构)-以校园为例】
章:【RYU+Mininet的SDN架构-设计校园网络(一)】
# !/usr/bin/python3
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import Node
from mininet.log import setLogLevel, info
from mininet.cli import CLI
import os
from mininet.node import Controller, RemoteController, OVSController
from mininet.link import TCLink
from mininet.util import quietRun
from mininet.term import makeTerms
from sys import exit, stdin, argv
from re import findall
from time import sleep
import time
from mininet.nodelib import NAT
class LinuxRouter(Node):
"A Node with IP forwarding enabled."
def config(self, **params):
super(LinuxRouter, self).config(**params)
# Enable forwarding on the router
self.cmd('sysctl net.ipv4.ip_forward=1')
def terminate(self):
self.cmd('sysctl net.ipv4.ip_forward=0')
super(LinuxRouter, self).terminate()
class NetworkTopo(Topo):
"A LinuxRouter connecting three IP subnets"
def build(self, **_opts):
defaultIP1 = '192.168.3.2/30' # IP address for r1-eth1
defaultIP2 = '192.168.3.1/30'
defaultIP3 = '192.168.3.6/30'
router1 = self.addNode('r1', cls=LinuxRouter, ip=defaultIP1)
router2 = self.addNode('r2', cls=LinuxRouter, ip=defaultIP2)
router3 = self.addNode('r3', cls=LinuxRouter, ip=defaultIP3)
nat0 = self.addNode('nat0', cls=NAT, ip='192.168.10.1/30', subnet='192.168.0.0/16', inNamespace=False)
h1 = self.addHost('h1', ip='192.168.1.1/24', defaultRoute='via 192.168.1.254') # define gateway
h2 = self.addHost('h2', ip='0.0.0.0/24', defaultRoute='via 192.168.1.254')
h3 = self.addHost('h3', ip='0.0.0.0/24', defaultRoute='via 192.168.1.254')
h4 = self.addHost('h4', ip='0.0.0.0/24', defaultRoute='via 192.168.1.254')
h5 = self.addHost('h5', ip='0.0.0.0/24', defaultRoute='via 192.168.1.254')
h6 = self.addHost('h6', ip='0.0.0.0/24', defaultRoute='via 192.168.1.254')
h7 = self.addHost('h7', ip='192.168.2.1/24', defaultRoute='via 192.168.2.254')
h8 = self.addHost('h8', ip='192.168.2.2/24', defaultRoute='via 192.168.2.254')
h9 = self.addHost('h9', ip='0.0.0.0', defaultRoute='via 192.168.2.254')
h10 = self.addHost('h10', ip='0.0.0.0', defaultRoute='via 192.168.2.254')
h11 = self.addHost('h11', ip='0.0.0.0', defaultRoute='via 192.168.2.254')
self.addLink(router1, router2, intfName1='r1-eth1', intfName2='r2-eth1')
self.addLink(router3, router1, intfName1='r3-eth1', intfName2='r1-eth2',
params2={'ip': '192.168.3.5/30'})
self.addLink(router1, nat0, intfName1='r1-eth3', intfName2='nat0-eth1',params1={'ip': '192.168.10.2/30'})
s1=self.addSwitch('s1')
s2=self.addSwitch('s2')
s3=self.addSwitch('s3')
s4=self.addSwitch('s4')
#s5=self.addSwitch('s5')
self.addLink(s1, router2, intfName2='r2-eth2',
params2={'ip': '192.168.1.254/24'})
self.addLink(s4, router3, intfName2='r3-eth2',
params2={'ip': '192.168.2.254/24'})
self.addLink(s1, s2)
self.addLink(s1, s3)
#self.addLink(s2, s3)
self.addLink(s1, h1)
self.addLink(s1, h2)
self.addLink(s2, h3)
self.addLink(s2, h4)
self.addLink(s3, h5)
self.addLink(s3, h6)
self.addLink(s3, s2)
#self.addLink(s4, s5)
self.addLink(s4, h7)
self.addLink(s4, h8)
#self.addLink(s5, h7)
#self.addLink(s5, h8)
self.addLink(s4, h9)
self.addLink(s4,h10)
self.addLink(s4,h11)
DNSTemplate = """
start 192.168.1.2
end 192.168.1.6
option subnet 255.255.255.0
option domain local
option lease 7 # seconds
"""
def makeDHCPconfig( filename, intf, gw, dns ):
"Create a DHCP configuration file"
config = (
'interface %s' % intf,
DNSTemplate,
'option router %s' % gw,
'option dns %s' % dns,
'' )
with open( filename, 'w' ) as f:
f.write( '\n'.join( config ) )
def startDHCPserver( host, gw, dns ):
"Start DHCP server on host with specified DNS server"
info( '* Starting DHCP server on', host, 'at', host.IP(), '\n' )
dhcpConfig = '/tmp/%s-udhcpd.conf' % host
makeDHCPconfig( dhcpConfig, host.defaultIntf(), gw, dns )
host.cmd( 'udhcpd -f', dhcpConfig,
'1>/tmp/%s-dhcp.log 2>&1 &' % host )
def stopDHCPserver( host ):
"Stop DHCP server on host"
info( '* Stopping DHCP server on', host, 'at', host.IP(), '\n' )
host.cmd( 'kill %udhcpd' )
# DHCP client functions
def startDHCPclient( host ):
"Start DHCP client on host"
intf = host.defaultIntf()
host.cmd( 'dhclient -v -d -r', intf )
host.cmd( 'dhclient -v -d 1> /tmp/dhclient.log 2>&1', intf, '&' )
def stopDHCPclient( host ):
host.cmd( 'kill %dhclient' )
def waitForIP( host ):
"Wait for an IP address"
info( '*', host, 'waiting for IP address' )
while True:
host.defaultIntf().updateIP()
#print(host.IP())
if host.IP():
break
info( '.' )
sleep(1)
info( '\n' )
def readline():
"Read a line from stdin"
return stdin.readline()
def prompt( s=None ):
"Print a prompt and read a line from stdin"
if s is None:
s = "Press return to continue: "
print (s),
return readline()
def DHCP(host,dhcp):
"Rogue DHCP server demonstration"
startDHCPserver( dhcp, gw="192.168.1.254", dns='8.8.8.8')
startDHCPclient( host )
waitForIP( host )
print(host.IP())
stopDHCPserver( dhcp )
stopDHCPclient( host )
def usage():
"Print usage message"
print ("%s [ -h | -text ]")
print ("-h: print this message")
print ("-t: run in text/batch vs. firefox/x11 mode")
def run():
topo = NetworkTopo()
net = Mininet(topo=topo, build=False,controller=RemoteController)
c0=net.addController('c0',ip='127.0.0.1',port=6633) #ryu controller
c1=net.addController('c1',ip='127.0.0.1',port=6634) #ryu controller
net.build()
net.start()
#net.get('s1').start([c0])
h10 = net.getNodeByName( 'h10')
dhcp = net.getNodeByName('h1')
h11 = net.getNodeByName('h11')
h8 = net.getNodeByName( 'h8')
h2 = net.getNodeByName( 'h2')
h3 = net.getNodeByName( 'h3')
h4 = net.getNodeByName( 'h4')
h5 = net.getNodeByName( 'h5')
h6 = net.getNodeByName( 'h6')
h9 = net.getNodeByName( 'h9')
s1 = net.getNodeByName( 's1')
s2 = net.getNodeByName( 's2')
s3 = net.getNodeByName( 's3')
s4 = net.getNodeByName( 's4')
#net.get('s4').start([c1])ss
DHCP(h2,dhcp)
DHCP(h3,dhcp)
DHCP(h4,dhcp)
DHCP(h5,dhcp)
DHCP(h6,dhcp)
#os.popen('ovs-vsctl add-port s1 ens36')
net.get('s1').start([c0])
net.get('s2').start([c0])
net.get('s3').start([c0])
net.get('s4').start([c1])
#DHCP(h9,dhcp)
#DHCP(h10,dhcp)
#DHCP(h11,dhcp)
info('*** Routing Table on Router:\n')
r1 = net.getNodeByName('r1')
r2 = net.getNodeByName('r2')
r3 = net.getNodeByName('r3')
nat0 = net.getNodeByName('nat0')
s1 = net.getNodeByName('s1')
s2 = net.getNodeByName('s2')
s3 = net.getNodeByName('s3')
#s1.cmd('ovs-vsctl set bridge s1 stp_enable=true')
#s2.cmd('ovs-vsctl set bridge s2 stp_enable=true')
#s3.cmd('ovs-vsctl set bridge s3 stp_enable=true')
info('starting zebra and ospfd service:\n')
r1.cmd('route add default gw 192.168.10.1 dev r1-eth3')
r2.cmd('route add default gw 192.168.3.2 dev r2-eth1')
r3.cmd('route add default gw 192.168.3.5 dev r3-eth1')
nat0.cmd('route add -net 192.168.0.0/16 gw 192.168.10.2 dev nat0-eth1')
r1.cmd('zebra -f /etc/quagga/r1zebra.conf -d -z /tmp/r1zebra.api -i /tmp/r1zebra.interface')
r2.cmd('zebra -f /etc/quagga/r2zebra.conf -d -z /tmp/r2zebra.api -i /tmp/r2zebra.interface')
r3.cmd('zebra -f /etc/quagga/r3zebra.conf -d -z /tmp/r3zebra.api -i /tmp/r3zebra.interface')
time.sleep(1) # time for zebra to create api socket
r1.cmd('ospfd -f /etc/quagga/r1ospfd.conf -d -z /tmp/r1zebra.api -i /tmp/r1ospfd.interface')
r2.cmd('ospfd -f /etc/quagga/r2ospfd.conf -d -z /tmp/r2zebra.api -i /tmp/r2ospfd.interface')
r3.cmd('ospfd -f /etc/quagga/r3ospfd.conf -d -z /tmp/r3zebra.api -i /tmp/r3ospfd.interface')
CLI(net)
net.stop()
os.system("killall -9 ospfd zebra")
os.system("rm -f /tmp/*.api")
os.system("rm -f /tmp/*.interface")
if __name__ == '__main__':
setLogLevel('info')
# setLogLevel('debug')
run()
# ! /usr/binpython
from mininet.node import Controller, ovSKernelSwitch
from mininet.log import setLogLevel, info
from mn_wifi.net import Mininet_wifi
from mn_wifi.node import Station, ovSKernelAPfrom mn_wifi.cli
import CLI
from mn_wifi.link import wmediumd
from mn_wifi.wmediumdConnector import interference
from subprocess import call
from mininet.nodelib import NAT
import os
def myNetwork( ):
net = Mininet_wifi(topo=None,
build=False,
link=wmediumd ,
wmediumd_mode=interference)
info('***Adding controllerln ' )
c1= net.addController( name= 'c1 ',controller=Controller,protocol= 'tcp ' ,port=6653)
info('***Add switches/APs\n ' )
ap1 = net.addAccessPoint( 'ap1', mac='00:00:00:00:00:01', ssid="handover",mode="g", channel="1", passwd= '123456789a',encrypt='wpa2', position='123.0,323.0,0')
ap2 = net.addAccessPoint( 'ap2' , mac='00:00:00:00:00:02' , ssid="handover ",mode="g" , channel="6",passwd=' 123456789a',encrypt='wpa2', position='375.0, 332.0,0')
ap3 = net.addAccessPoint( 'ap3 ' , mac='00:00:00:00:00:03', ssid="handover " ,node="g", channel="1",passwd=' 123456789a',encrypt='wpa2', position=' 606.0,313.0,0')
s2 = net. addSwitch( 's2 ', cls=ovSKernelSwitch)
info('***Add hosts/stations\n ' )
sta1 = net.addStation( 'sta1', ip= '192.168.4.1/24',position= '54.0,434.0,0 ', bgscan_threshold=-60,s_inverval=5, l_interval=10,bgscan_module="simpie")
sta2 = net.addStation( 'sta2 ', ip= '192.168.4.2/24 ',position= '156.0,433.0,0 ', bgscan_threshold=-60,s_inverval=5, l_interval=10,bgscan_module="simpie")
sta3 = net. addStation( 'sta3 ', ip= '192.168.4.3/24',position= '321.0,435.0,0 ', bgscan_threshold=-60,s_inverval=5, l_interval=10,bgscan_module="simpie")
sta4 = net.addStation( 'sta4 ', ip= '192.168.4.4/24',position='430.0,433.0,0 ', bgscan_threshold=-60,s_inverval=5, l_interval=10,bgscan_module="simpie")
sta5 = net.addStation( 'sta5 ', ip= '192.168.4.5/24',position= '555.0,429.0,0 ', bgscan_threshold=-60,s_inverval=5, l_interval=10,bgscan_module="simpie")
sta6 = net.addStation( 'sta6 ' , ip= '192.168.4.6/24' ,position= '556,430,0 ', bgscan_threshold=-60,s_inverval=5, l_interval=10,bgscan_module="simpie")
nat0=net. addHost( 'nat0' ,cls=NAT, ip= '192.168.4.8/24 ' , subnet=' 192.168.4.0/24',inNamespace=False)
net. addLink(nat0,s1)
info( "***Configuring Propagation Model\n" )
net.setPropagationModel( model="logDistance" , exp=3)
info( "***Configuring wifi nodes\n " )
net . configurewifiNodes( )
info(·***Add linksln ')net. addLink( s1, s2)
net.addLink(ap1, s2)
net.addLink(s2, ap2)
net. addLink(s2, ap3)
net.plotGraph( max_x=1000,max_y=1000)
info('***Starting networkin ' )
net. start()
os. popen( 'ovs-vsctl add-port s1 ens34 ')
CLI(net)
net.stop( )
ifname__=='__main__':
setLogLevel( info')
myNetwork( )
[1]Mininet-WiFi official guideline. https://mininet-wifi.github.io/
[2] R. R. Fontes, S. Afzal, S. H. B. Brito, M. A. S. Santos and C. E. Rothenberg, "Mininet-WiFi: Emulating software-defined wireless networks," 2015 11th International Conference on Network and Service Management (CNSM), 2015, pp. 384-389, doi: 10.1109/CNSM.2015.7367387.