There are 2 problems here based on the fact that Mininet considers one interface in a host as the "default" interface for the node.
In a host with many interfaces, mininet assigns an IP only to the default interface, so you have to assign the IP with a coherent IP plan.
See here for details.
You can see in the following code that you can have a different result changing only the order in which you add the links (when you add a link, the interfaces are also created and if the new interface is the first in the host, it will be the "default", thus with an IP):
#!/usr/bin/python
from mininet.net import Mininet
from mininet.log import setLogLevel
import os,time
if __name__ == '__main__':
#setLogLevel( 'info' )
net = Mininet()
num_nodes = 8
hosts=[None] * num_nodes
for i in range(1,num_nodes):
hosts[i]=net.addHost("h%d"%i)
for i in range(2,num_nodes,2):
net.addLink(hosts[i], hosts[i-1])
for i in range(3,num_nodes,2):
net.addLink(hosts[i], hosts[i-1])
net.start()
net.pingAll()
net.stop()
This is the output:
h1 -> h2 X X X X X
h2 -> h1 X X X X X
h3 -> X X h4 X X X
h4 -> X X h3 X X X
h5 -> X X X X h6 X
h6 -> X X X X h5 X
h7 -> X X X X X X
pingAll() tries to ping from each host to the IP of the default interface of any other host. This can work only if you enable the ip forwarding in every host in this way:
host.cmd('sysctl -w net.ipv4.ip_forward=1')
otherwise you have to ping the neighbors of each host giving the right IP.
Here you can find the code that can assign the IPs to the interfaces and ping all the neighbors for each node.
#!/usr/bin/python
from mininet.net import Mininet
from mininet.log import output
if __name__ == '__main__':
net = Mininet()
num_nodes = 8
# from 1 to num_nodes
for i in range(1,num_nodes+1):
net.addHost('h%d'%i,ip=None)
# from 1 to num_nodes-1 because the index starts from zero
for i in range(1,num_nodes):
h1 = net.hosts[i-1]
h2 = net.hosts[i]
net.addLink(h2, h1, intfName1 = '%s-eth0'%h2.name, intfName2 = '%s-eth1'%h1.name)
h2.intf('%s-eth0'%h2.name).setIP('10.0.%d.1'%i, 24)
h1.intf('%s-eth1'%h1.name).setIP('10.0.%d.2'%i, 24)
net.start()
# Ping forward and backward nodes
for i in range(0,num_nodes):
node = net.hosts[i]
fwdNode, bwNode = None, None
output('%s -> '%node.name)
if i!=0:
bwNode = net.hosts[i-1]
bwIP = bwNode.IP('%s-eth1'%bwNode.name)
result = node.cmd('ping -c1 %s'%bwIP)
sent, received = net._parsePing(result)
output(('%s '%bwNode.name) if received else 'X ')
if i!=7:
fwdNode = net.hosts[i+1]
fwdIP = fwdNode.IP('%s-eth0'%fwdNode.name)
result = node.cmd('ping -c1 %s'%fwdIP)
sent, received = net._parsePing(result)
output(('%s '%fwdNode.name) if received else 'X ')
output('\n')
net.stop()
and this is the output:
h1 -> h2
h2 -> h1 h3
h3 -> h2 h4
h4 -> h3 h5
h5 -> h4 h6
h6 -> h5 h7
h7 -> h6 h8
h8 -> h7