Reputation: 1
I want to write a python code to create the following network: h1 with ip=10.0.1.1\24, linked to switch s1, s1 linked to router r0, r0 linked to switch s2, s2 linked to h2 with ip=10.0.2.1\24 and I must not use a controller!
this is the code I used to create the net:
#!/usr/bin/env python2
from mininet.net import Mininet
from mininet.node import Node
from mininet.topo import Topo
from mininet.cli import CLI
from mininet.log import setLogLevel
class LinuxRouter(Node):
def config(self, **params):
super(LinuxRouter, self).config(**params)
self.cmd('sysctl net.ipv4.ip_forward=1')
def terminate(self):
self.cmd('sysctl net.ipv4.ip_forward=0')
super(LinuxRouter, self).terminate()
class CustomTopology(Topo):
def build(self):
# Create hosts
h1 = self.addHost('h1', ip='10.0.1.1/24')
h2 = self.addHost('h2', ip='10.0.2.1/24')
# Create switches
s1 = self.addSwitch('s1')
s2 = self.addSwitch('s2')
# Create router
r0 = self.addNode('r0', cls=LinuxRouter)
# Add links
self.addLink(h1, s1, intfName1='h1-eth0', intfName2='s1-eth1')
self.addLink(s1, r0, intfName1='s1-eth2', intfName2='r0-eth1', params2={'ip': '10.0.1.254/24'})
self.addLink(r0, s2, intfName1='r0-eth2', intfName2='s2-eth1', params2={'ip': '10.0.2.254/24'})
self.addLink(s2, h2, intfName1='s2-eth2', intfName2='h2-eth0')
if __name__ == '__main__':
setLogLevel('info')
topo = CustomTopology()
net = Mininet(topo=topo, controller=None) # Remove the controller
net.start()
CLI(net)
net.stop()
and I am adding flows as follows:
#!/usr/bin/env python3
import subprocess
def run_command(command):
"""
Run a shell command and handle errors.
"""
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode != 0:
print(f"Error running command: {command}\n{result.stderr}")
else:
print(f"Command succeeded: {command}\n{result.stdout}")
def push_flows():
# Flow entries for switch s1
run_command('sudo ovs-ofctl add-flow s1 priority=100,arp,actions=output:NORMAL')
run_command('sudo ovs-ofctl add-flow s1 priority=100,ip,actions=output:NORMAL')
run_command('sudo ovs-ofctl add-flow s1 priority=100,icmp,actions=output:NORMAL')
# Flow entries for switch s2
run_command('sudo ovs-ofctl add-flow s2 priority=100,arp,actions=output:NORMAL')
run_command('sudo ovs-ofctl add-flow s2 priority=100,ip,actions=output:NORMAL')
run_command('sudo ovs-ofctl add-flow s2 priority=100,icmp,actions=output:NORMAL')
if __name__ == '__main__':
push_flows()
But it gives me this when I try to ping h2 from h1: mininet> h1 ping h2 -c 10 ping: connect: Network is unreachable
How can I fix it?
Upvotes: 0
Views: 52