1

I am trying to setup a mininet topology consisting one switch connected to three host. The below code does exactly the same thing and bring up the topology fine. But i see, none of the host.cmd is taking effect. If i copy paste the commands on the device console, then they work fine.

The below command just dont work :

h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')

similarly, the below command configures the mac but not the IP on the interface.

link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')

Looks like the problem if specific to hosts. At the end of the program i paste the output topology.

somebody expert with working with mininet pls help

from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, UserSwitch, DefaultController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import Link, TCLink

#reference : http://intronetworks.cs.luc.edu/current/html/mininet.html

def topology():

    net = Mininet()

    #Add Controller to the Topology
    c0 = net.addController( 'c0',
         controller=DefaultController)

    # Add hosts and switches
    h1 = net.addHost( 'h1')
    h2 = net.addHost( 'h2')
    h3 = net.addHost( 'h3')

    #Add L2 Switch
    s1 = net.addSwitch( 's1')

    #Add links
    link_h1s1 = net.addLink( h1, s1 , intfName1 = 'h1-s1', intfName2 = 's1-h1')
    link_h2s1 = net.addLink( h2, s1 , intfName1 = 'h2-s1', intfName2 = 's1-h2')
    link_h3s1 = net.addLink( h3, s1 , intfName1 = 'h3-s1', intfName2 = 's1-h3')

    link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')
    link_h2s1.intf1.config(ip='100.168.0.2/24', mac = '00:00:00:00:00:02')
    link_h3s1.intf1.config(ip='100.168.0.3/24', mac = '00:00:00:00:00:03')

    link_h1s1.intf2.config(ip=None, mac = '00:00:00:00:00:04')
    link_h2s1.intf2.config(ip=None, mac = '00:00:00:00:00:05')
    link_h3s1.intf2.config(ip=None, mac = '00:00:00:00:00:06')


    #h1.setIP('122.1.1.1/32')
    h1.cmd('ifconfig h1-s1 100.168.0.1 netmask 255.255.255.0')
    h2.cmd('ifconfig h2-s1 100.168.0.2 netmask 255.255.255.0')
    h3.cmd('ifconfig h3-s1 100.168.0.3 netmask 255.255.255.0')

    #Setting lo IP addresses
    h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')
    h2.cmd('ifconfig lo 122.1.1.2 netmask 255.255.255.255')
    h3.cmd('ifconfig lo 122.1.1.3 netmask 255.255.255.255')

    #finished building the Topology
    net.build()

    #Star the Controller
    #c0.start()

    #start the Switches
    #s1.start( [c0] )

    net.start()
    print "*** Running CLI"

    CLI( net )

    print "*** Stopping network"

    net.stop()

if __name__ == '__main__':

    setLogLevel( 'info' )

    topology()

output of host h1

mininet> h1 ifconfig
h1-s1     Link encap:Ethernet  HWaddr 00:00:00:00:00:01
          inet addr:10.0.0.1  Bcast:10.255.255.255  Mask:255.0.0.0   << default IP 10.1.1.1/8 is taken
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.255.255.255                 << Default loopback addr is taken
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44

1 Answers1

1

you should build the network before running the commands

from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, UserSwitch, DefaultController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import Link, TCLink

#reference : http://intronetworks.cs.luc.edu/current/html/mininet.html

def topology():

    net = Mininet()

    #Add Controller to the Topology
    c0 = net.addController( 'c0',
         controller=DefaultController)

    # Add hosts and switches
    h1 = net.addHost( 'h1')
    h2 = net.addHost( 'h2')
    h3 = net.addHost( 'h3')

    #Add L2 Switch
    s1 = net.addSwitch( 's1')

    #Add links
    link_h1s1 = net.addLink( h1, s1 , intfName1 = 'h1-s1', intfName2 = 's1-h1')
    link_h2s1 = net.addLink( h2, s1 , intfName1 = 'h2-s1', intfName2 = 's1-h2')
    link_h3s1 = net.addLink( h3, s1 , intfName1 = 'h3-s1', intfName2 = 's1-h3')

    link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')
    link_h2s1.intf1.config(ip='100.168.0.2/24', mac = '00:00:00:00:00:02')
    link_h3s1.intf1.config(ip='100.168.0.3/24', mac = '00:00:00:00:00:03')

    link_h1s1.intf2.config(ip=None, mac = '00:00:00:00:00:04')
    link_h2s1.intf2.config(ip=None, mac = '00:00:00:00:00:05')
    link_h3s1.intf2.config(ip=None, mac = '00:00:00:00:00:06')
    net.build()
    net.start()

    #h1.setIP('122.1.1.1/32')
    h1.cmd('ifconfig h1-s1 100.168.0.1 netmask 255.255.255.0')
    h2.cmd('ifconfig h2-s1 100.168.0.2 netmask 255.255.255.0')
    h3.cmd('ifconfig h3-s1 100.168.0.3 netmask 255.255.255.0')

    #Setting lo IP addresses
    h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')
    h2.cmd('ifconfig lo 122.1.1.2 netmask 255.255.255.255')
    h3.cmd('ifconfig lo 122.1.1.3 netmask 255.255.255.255')


    #Star the Controller
    #c0.start()

    #start the Switches
    #s1.start( [c0] )

    print "*** Running CLI"

    CLI( net )

    print "*** Stopping network"

    net.stop()

if __name__ == '__main__':

    setLogLevel( 'info' )

    topology()

Output:

root@grisou-50:~/mininet# python test.py
*** Configuring hosts
h1 h2 h3
*** Starting controller
c0
*** Starting 1 switches
s1 ...
*** Running CLI
*** Starting CLI:
mininet> ifconfig
*** Unknown command: ifconfig
mininet> h1 ifconfig
h1-s1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 100.168.0.1  netmask 255.255.255.0  broadcast 100.168.0.255
        inet6 fe80::200:ff:fe00:1  prefixlen 64  scopeid 0x20<link>
        ether 00:00:00:00:00:01  txqueuelen 1000  (Ethernet)
        RX packets 19  bytes 1626 (1.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9  bytes 806 (806.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 122.1.1.1  netmask 255.255.255.255
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

mininet>

A cleanest solution can be to directly set the Ip of the hosts in the addHost function. In this case if you call dump from the CLI you should see the Ips correctly set.

from mininet.net import Mininet
from mininet.node import Controller, OVSKernelSwitch, UserSwitch, DefaultController
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import Link, TCLink

#reference : http://intronetworks.cs.luc.edu/current/html/mininet.html

def topology():

    net = Mininet()

    #Add Controller to the Topology
    c0 = net.addController( 'c0',
         controller=DefaultController)

    # Add hosts and switches
    h1 = net.addHost( 'h1', ip='100.168.0.1/24', mac = '00:00:00:00:00:01')
    h2 = net.addHost( 'h2', ip='100.168.0.2/24', mac = '00:00:00:00:00:02')
    h3 = net.addHost( 'h3', ip='100.168.0.3/24', mac = '00:00:00:00:00:03')

    #Add L2 Switch
    s1 = net.addSwitch( 's1')

    #Add links
    link_h1s1 = net.addLink( h1, s1 , intfName1 = 'h1-s1', intfName2 = 's1-h1')
    link_h2s1 = net.addLink( h2, s1 , intfName1 = 'h2-s1', intfName2 = 's1-h2')
    link_h3s1 = net.addLink( h3, s1 , intfName1 = 'h3-s1', intfName2 = 's1-h3')

    link_h1s1.intf2.config(ip=None, mac = '00:00:00:00:00:04')
    link_h2s1.intf2.config(ip=None, mac = '00:00:00:00:00:05')
    link_h3s1.intf2.config(ip=None, mac = '00:00:00:00:00:06')
    net.build()
    net.start()


    #Setting lo IP addresses
    h1.cmd('ifconfig lo 122.1.1.1 netmask 255.255.255.255')
    h2.cmd('ifconfig lo 122.1.1.2 netmask 255.255.255.255')
    h3.cmd('ifconfig lo 122.1.1.3 netmask 255.255.255.255')


    #Star the Controller
    #c0.start()

    #start the Switches
    #s1.start( [c0] )

    print "*** Running CLI"

    CLI( net )

    print "*** Stopping network"

    net.stop()

if __name__ == '__main__':

    setLogLevel( 'info' )

    topology()
Giuseppe
  • 658
  • 1
  • 6
  • 14
  • Thanks, that worked but still one problem i see. Though `h1 ifconfig` shows the correct output, on mininet shell , running `dump` shows the default IP. can you pls try that out. – Abhishek Sagar Jun 14 '20 at 19:12
  • also, IP is not being set using `link_h1s1.intf1.config(ip='100.168.0.1/24', mac = '00:00:00:00:00:01')` , only mac is taking effect. – Abhishek Sagar Jun 14 '20 at 19:14
  • it is normal that dump is showing the previous Ip, since it is not doing a dynamic check in the network, but it is checking the data structure in mininet. The ifconfig command does not modify it. For the oder part, can I ask you what are you trying to do? – Giuseppe Jun 14 '20 at 19:27
  • @AbhishekSagar I added the cleanest solution, maybe it will fix your problem completely. – Giuseppe Jun 14 '20 at 19:39
  • ok, thanks for clarifying that updates done by `cmd` wont show up in `dump` output. Your solution works good. But i have a problem in using `h1 = net.addHost( 'h1', ip='100.168.0.1/24', mac = '00:00:00:00:00:01')` This line do not tell on which interface of `h1` will this IP and mac will be configured. What if `h1` has multiple interfaces, how will i configure IP and macs to them within mininet topology (not using `cmd`). @Giuseppe – Abhishek Sagar Jun 14 '20 at 19:58
  • Ok, I see the problem. For multiple interfaces, there is the answer: https://stackoverflow.com/questions/41378338/mininet-configure-multiple-interfaces-on-a-host – Giuseppe Jun 14 '20 at 20:08
  • ok, i tried adding `h2.setIP('100.168.0.2/24', intf='h2-s1'` and `h2.setMAC('00:00:00:00:00:02', intf='h2-s1')` , only mac addr take effect. – Abhishek Sagar Jun 14 '20 at 20:36
  • Try with h2.setIP('100.168.0.2', intf='h2-s1',prefixLen = 24) @AbhishekSagar – Giuseppe Jun 14 '20 at 20:43
  • No success. Same problem. – Abhishek Sagar Jun 14 '20 at 20:46
  • you set the IP before or after the build? – Giuseppe Jun 14 '20 at 20:50
  • before the build. – Abhishek Sagar Jun 14 '20 at 20:55
  • h2.setIP() should be after the build and the start, it works in my script. Try to put it after – Giuseppe Jun 14 '20 at 20:59
  • yes, that worked for me. Great thanks. But Mininet APIs seems to be very confusing and inconsistent. I tried several methods, none of them worked. setIP works after build, setMAC works before the build ! phew ! Thank you Very much for your help and time sir. – Abhishek Sagar Jun 14 '20 at 21:06
  • No problem ;-). It can be a bit difficult at the beginning but mininet is a powerful tool. If you want more example you can find it in the Github repository: https://github.com/mininet/mininet/tree/master/examples – Giuseppe Jun 14 '20 at 21:08