137 lines
4.8 KiB
Python
137 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Generated Mininet topology. Do not edit by hand."""
|
|
|
|
import shlex
|
|
|
|
from mininet.cli import CLI
|
|
from mininet.link import TCLink
|
|
from mininet.net import Mininet
|
|
|
|
|
|
def _shell_join(args):
|
|
"""Quote command arguments for execution inside a Mininet node."""
|
|
|
|
return ' '.join(shlex.quote(str(arg)) for arg in args)
|
|
|
|
|
|
def _node_cmd(node, *args):
|
|
"""Execute one safely quoted command inside a Mininet node."""
|
|
|
|
return node.cmd(_shell_join(args))
|
|
|
|
|
|
def _start_background(node, args, log_path):
|
|
"""Start one background process and return its shell PID."""
|
|
|
|
command = _shell_join(args)
|
|
shell_command = (
|
|
command
|
|
+ ' >'
|
|
+ shlex.quote(log_path)
|
|
+ ' 2>&1 & echo $!'
|
|
)
|
|
return node.cmd('sh -c ' + shlex.quote(shell_command)).strip()
|
|
|
|
|
|
def main():
|
|
"""Build, configure, expose, and clean up the planned network."""
|
|
|
|
net = Mininet(controller=None, build=False)
|
|
nodes = {}
|
|
service_processes = []
|
|
|
|
try:
|
|
# Create nodes.
|
|
nodes['h1'] = net.addHost('h1', ip=None)
|
|
nodes['h2'] = net.addHost('h2', ip=None)
|
|
nodes['h3'] = net.addHost('h3', ip=None)
|
|
nodes['s1'] = net.addSwitch('s1', failMode='standalone')
|
|
nodes['s2'] = net.addSwitch('s2', failMode='standalone')
|
|
nodes['r0'] = net.addHost('r0', ip=None)
|
|
|
|
# Create links.
|
|
net.addLink(
|
|
nodes['h1'],
|
|
nodes['s1'],
|
|
intfName1='h1-eth0',
|
|
intfName2='s1-eth0',
|
|
)
|
|
net.addLink(
|
|
nodes['h2'],
|
|
nodes['s1'],
|
|
intfName1='h2-eth0',
|
|
intfName2='s1-eth1',
|
|
)
|
|
net.addLink(
|
|
nodes['h3'],
|
|
nodes['s2'],
|
|
intfName1='h3-eth0',
|
|
intfName2='s2-eth0',
|
|
)
|
|
net.addLink(
|
|
nodes['r0'],
|
|
nodes['s1'],
|
|
intfName1='r0-eth0',
|
|
intfName2='s1-eth2',
|
|
)
|
|
net.addLink(
|
|
nodes['r0'],
|
|
nodes['s2'],
|
|
intfName1='r0-eth1',
|
|
intfName2='s2-eth1',
|
|
)
|
|
|
|
# Materialize the Mininet topology.
|
|
net.build()
|
|
net.start()
|
|
|
|
# Configure concrete interfaces.
|
|
_node_cmd(nodes['h1'], 'ip', '-4', 'addr', 'flush', 'dev', 'h1-eth0')
|
|
_node_cmd(nodes['h1'], 'ip', 'link', 'set', 'dev', 'h1-eth0', 'up')
|
|
_node_cmd(nodes['h2'], 'ip', '-4', 'addr', 'flush', 'dev', 'h2-eth0')
|
|
_node_cmd(nodes['h2'], 'ip', 'link', 'set', 'dev', 'h2-eth0', 'up')
|
|
_node_cmd(nodes['h3'], 'ip', '-4', 'addr', 'flush', 'dev', 'h3-eth0')
|
|
_node_cmd(nodes['h3'], 'ip', 'addr', 'add', '192.168.2.10/24', 'dev', 'h3-eth0')
|
|
_node_cmd(nodes['h3'], 'ip', 'link', 'set', 'dev', 'h3-eth0', 'up')
|
|
_node_cmd(nodes['r0'], 'ip', '-4', 'addr', 'flush', 'dev', 'r0-eth0')
|
|
_node_cmd(nodes['r0'], 'ip', 'addr', 'add', '192.168.1.1/24', 'dev', 'r0-eth0')
|
|
_node_cmd(nodes['r0'], 'ip', 'link', 'set', 'dev', 'r0-eth0', 'up')
|
|
_node_cmd(nodes['r0'], 'ip', '-4', 'addr', 'flush', 'dev', 'r0-eth1')
|
|
_node_cmd(nodes['r0'], 'ip', 'addr', 'add', '192.168.2.1/24', 'dev', 'r0-eth1')
|
|
_node_cmd(nodes['r0'], 'ip', 'link', 'set', 'dev', 'r0-eth1', 'up')
|
|
|
|
# Configure static default routes.
|
|
_node_cmd(nodes['h3'], 'ip', 'route', 'replace', 'default', 'via', '192.168.2.1', 'dev', 'h3-eth0')
|
|
|
|
# Apply node-level networking settings.
|
|
_node_cmd(nodes['r0'], 'sysctl', '-w', 'net.ipv4.ip_forward=1')
|
|
_node_cmd(nodes['r0'], 'sysctl', '-w', 'net.ipv4.conf.all.rp_filter=0')
|
|
_node_cmd(nodes['r0'], 'sysctl', '-w', 'net.ipv4.conf.default.rp_filter=0')
|
|
_node_cmd(nodes['r0'], 'sysctl', '-w', 'net.ipv4.conf.r0-eth0.rp_filter=0')
|
|
_node_cmd(nodes['r0'], 'sysctl', '-w', 'net.ipv4.conf.r0-eth1.rp_filter=0')
|
|
|
|
# Start planned services.
|
|
_node_cmd(nodes['r0'], 'rm', '-f', '/tmp/mininet-dnsmasq-0.leases', '/tmp/mininet-dnsmasq-0.pid')
|
|
pid = _start_background(
|
|
nodes['r0'],
|
|
['dnsmasq', '--no-daemon', '--conf-file=', '--port=0', '--bind-interfaces', '--dhcp-authoritative', '--interface=r0-eth0', '--dhcp-range=192.168.1.100,192.168.1.200,12h', '--dhcp-leasefile=/tmp/mininet-dnsmasq-0.leases', '--pid-file=/tmp/mininet-dnsmasq-0.pid', '--dhcp-option=3,192.168.1.1'],
|
|
'/tmp/mininet-dnsmasq-0.log',
|
|
)
|
|
service_processes.append((nodes['r0'], pid))
|
|
|
|
# DHCP clients are intentionally not started here.
|
|
# The assignment captures DHCP before dhclient is run manually.
|
|
CLI(net)
|
|
|
|
finally:
|
|
# Stop only the service processes started by this script.
|
|
for node, pid in reversed(service_processes):
|
|
if pid:
|
|
_node_cmd(node, 'kill', pid)
|
|
|
|
net.stop()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|