Last active
December 27, 2016 10:08
-
-
Save kostyll/8ed4eb81ab1d53383d2fcf863b0d8654 to your computer and use it in GitHub Desktop.
mininet_linux_router_works_under_pox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Works with RemoteController: | |
first install and run pox controller: | |
502 git clone http://github.com/noxrepo/pox | |
503 cd pox/ | |
505 git checkout dart | |
511 ./pox.py log.level --DEBUG misc.of_tutorial | |
!!!$ ./pox.py log.level --DEBUG forwarding.hub | |
then run this script | |
""" | |
from abc import abstractmethod | |
class CustomService(object): | |
def __init__(self, host, port=None): | |
self._host = host | |
if not port: | |
self._port = self.getDefaultPort() | |
else: | |
self._port = port | |
# print "Created with port %s, dP = %s, gP = %s" % (self._port, self.getDefaultPort(), self.getPort()) | |
@property | |
def host(self): | |
return self._host | |
@abstractmethod | |
def getDefaultPort(self): | |
pass | |
def getPort(self): | |
if not self._port: | |
return self.getDefaultPort() | |
return self._port | |
@abstractmethod | |
def start(): | |
pass | |
@abstractmethod | |
def stop(): | |
pass | |
@abstractmethod | |
def isRunning(): | |
pass | |
class CustomServicesManager(object): | |
def __init__(self): | |
self._services = {} | |
@property | |
def services(self): | |
return self._services | |
def addServiceToHost(self, host, service_class_or_instance): | |
try: | |
if not issubclass(service_class_or_instance, CustomService): | |
raise ValueError("service_class_or_instance variable must instance of|or implementation of CustomService") | |
service = service_class_or_instance(host) | |
except TypeError: | |
if not issubclass(service_class_or_instance.__class__, CustomService): | |
raise ValueError("service_class_or_instance variable must instance of|or implementation of CustomService") | |
if service_class_or_instance.host is not host: | |
raise ValueError("host must be the save as a first argument") | |
service = service_class_or_instance | |
service_index = (host, service.getPort()) | |
if service_index in self.services: | |
self.services[service_index].stop() | |
self._services.update({service_index: service}) | |
def startServices(self): | |
for service_index, service in self.services.items(): | |
if not service.isRunning(): | |
service.start() | |
def stopServices(self): | |
for service_index, service in self.services.items(): | |
if service.isRunning(): | |
service.stop() | |
class PythonSimpleHTTPServer(CustomService): | |
def __init__(self, host): | |
super(PythonSimpleHTTPServer, self).__init__(host) | |
self.popen_obj = None | |
self.pid = 0 | |
def getDefaultPort(self): | |
return 8080; | |
def isRunning(self): | |
if self.pid == 0: return False | |
try: | |
os.kill(self.pid, 0) | |
return True | |
except OSError: | |
return False | |
def start(self): | |
port = str(self.getPort()) | |
# print "Starting simple http service at host %s and port %s" % (self.host.IP(), port) | |
if self.isRunning(): | |
return | |
self.popen_obj = self.host.popen(["python", "-m", "SimpleHTTPServer", port]) | |
self.pid = self.popen_obj.pid | |
# print "started with pid %s" % self.pid | |
def stop(self): | |
try: | |
self.popen_obj.terminate() | |
self.popen_obj.wait() | |
self.pid = 0 | |
except OSError: | |
self.pid = 0 | |
except: | |
return | |
#!/usr/bin/python | |
""" | |
linuxrouter.py: Example network with Linux IP router | |
This example converts a Node into a router using IP forwarding | |
already built into Linux. | |
The example topology creates a router and three IP subnets: | |
- 192.168.1.0/24 (r0-eth1, IP: 192.168.1.1) | |
- 172.16.0.0/12 (r0-eth2, IP: 172.16.0.1) | |
- 10.0.0.0/8 (r0-eth3, IP: 10.0.0.1) | |
Each subnet consists of a single host connected to | |
a single switch: | |
r0-eth1 - s1-eth1 - h1-eth0 (IP: 192.168.1.100) | |
r0-eth2 - s2-eth1 - h2-eth0 (IP: 172.16.0.100) | |
r0-eth3 - s3-eth1 - h3-eth0 (IP: 10.0.0.100) | |
The example relies on default routing entries that are | |
automatically created for each router interface, as well | |
as 'defaultRoute' parameters for the host interfaces. | |
Additional routes may be added to the router or hosts by | |
executing 'ip route' or 'route' commands on the router or hosts. | |
""" | |
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 | |
#from services import CustomServicesManager, PythonSimpleHTTPServer | |
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 ): | |
defaultIP = '192.168.1.1/24' # IP address for r0-eth1 | |
router = self.addNode( 'r0', cls=LinuxRouter, ip=defaultIP ) | |
s1, s2, s3 = [ self.addSwitch( s ) for s in ( 's1', 's2', 's3' ) ] | |
self.addLink( s1, router, intfName2='r0-eth1', | |
params2={ 'ip' : defaultIP } ) # for clarity | |
self.addLink( s2, router, intfName2='r0-eth2', | |
params2={ 'ip' : '172.16.0.1/12' } ) | |
self.addLink( s3, router, intfName2='r0-eth3', | |
params2={ 'ip' : '10.0.0.1/8' } ) | |
h1 = self.addHost( 'h1', ip='192.168.1.100/24', | |
defaultRoute='via 192.168.1.1' ) | |
h2 = self.addHost( 'h2', ip='172.16.0.100/12', | |
defaultRoute='via 172.16.0.1' ) | |
h3 = self.addHost( 'h3', ip='10.0.0.100/8', | |
defaultRoute='via 10.0.0.1' ) | |
h4 = self.addHost( 'h4', ip='10.0.0.101/8', | |
defaultRoute='via 10.0.0.1' ) | |
h5 = self.addHost( 'h5', ip='10.0.0.125/8', | |
defaultRoute='via 10.0.0.1' ) | |
for h, s in [ | |
(h1, s1), (h2, s2), (h3, s3), (h4, s3), (h5, s3) | |
]: | |
self.addLink( h, s ) | |
from mininet.node import Controller, \ | |
RemoteController | |
def run(): | |
"Test linux router" | |
topo = NetworkTopo() | |
net = Mininet( topo=topo, controller = RemoteController) # controller is used by s1-s3 | |
h1 = net.hosts[0] | |
hScanner = net.hosts[1] | |
h3 = net.hosts[2] | |
h4 = net.hosts[3] | |
h5 = net.hosts[4] | |
# hScanner = net.hosts[3] | |
net.start() | |
services_manager = CustomServicesManager() | |
services_manager.addServiceToHost(h4, PythonSimpleHTTPServer) | |
services_manager.startServices() | |
print hScanner.popen("urxvt") | |
info( '*** Routing Table on Router:\n' ) | |
info( net[ 'r0' ].cmd( 'route' ) ) | |
CLI( net ) | |
net.stop() | |
if __name__ == '__main__': | |
setLogLevel( 'info' ) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment