2

I built a simple network topology using mininet python script. However, I want to extend this code by using networkX to build the topology in mininet script. Thus, firstly, I should import the networx as nx. The reason for using networkX is to find the shortest path very easy and simple between any source and destination hosts.

The topology code:

#!/usr/bin/env python
from mininet.net import Mininet  # IMPORT Mininet
from mininet.cli import CLI       ## IMPORT COMMAND LINE 
from mininet.link import  TCLink
from mininet.log import setLogLevel        # FOR DEPUG
from mininet.node import RemoteController  # TO BE ABLE O ADD REMOTE CONTROLLER

net = Mininet(link=TCLink)

# ADDING hosts with given MAC
h1 = net.addHost("h1",mac='00:00:00:00:00:01')
h2 = net.addHost("h2",mac='00:00:00:00:00:02')
h3 = net.addHost("h3",mac='00:00:00:00:00:03')

# ADDING Switch
s1 = net.addSwitch("s1")

net.addLink(h1,s1,bw=10, delay='5ms' )
net.addLink(h2,s1,bw=10, delay='5ms' )
net.addLink(h3,s1,bw=10, delay='5ms' )

# ADDING COTROLLER    
net.addController("c0",controller=RemoteController,ip="127.0.0.1",port=6633)

# START Mininet       
net.start()

CLI(net)
net.stop() 

 # EXIT Miminet

Any of you can help me to modify and connect networkX with mininet in building the toplogy?

Your assistance is appreciated.

Layally
  • 21
  • 6

1 Answers1

1

For people who are still interested in this:

Networkx is a great library for building networks and has a bunch of very useful pre-implemented functions. I did the same thing for my thesis, I had a networkx graph, which I used to simplify some computations, and programmatically built a mininet topology with the networkx graph as the model. Luckily this is very easy to do and only takes looping through the networkx nodes and edges. There used to be a link to such a piece of code on the Mininet mailing list but I saw this is dead now. Feel free to use my code instead:

from mininet.net import Mininet
import networkx as nx

def construct_mininet_from_networkx(graph, host_range):
    """ Builds the mininet from a networkx graph.

    :param graph: The networkx graph describing the network
    :param host_range: All switch indices on which to attach a single host as integers
    :return: net: the constructed 'Mininet' object
    """

    net = Mininet()
    # Construct mininet
    for n in graph.nodes:
        net.addSwitch("s_%s" % n)
        # Add single host on designated switches
        if int(n) in host_range:
            net.addHost("h%s" % n)
            # directly add the link between hosts and their gateways
            net.addLink("s_%s" % n, "h%s" % n)
    # Connect your switches to each other as defined in networkx graph
    for (n1, n2) in graph.edges:
        net.addLink('s_%s' % n1,'s_%s' % n2)
    return net

I tried to declutter my code as much as possible. This should be a good skeleton to build on. This assumes the networkx graph only holds the pure networking topology. Hosts connecting to this are added via the host_range parameter which in my case contained the indices of the switches (corresponding to the node number in the networkx graph) I wanted to connect to hosts. Add whatever you need, e.g., more than one host per host-connecting switch, specific bandwith and delay parameters on links, mac addresses, a controller...

Cerenia
  • 147
  • 7
  • Can I implement any huersitic algorithms in NetworkX to find optimal solution @Cerenia – Ba. Taj Sep 28 '20 at 16:11
  • What are you trying to compute exactly? NetworkX is a library which already has a bunch of algorithms. For example, you can find Dijkstra's shortest path algorithm out of the box: https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.shortest_paths.weighted.dijkstra_path.html Have a look at the docs to see what else is in there. – Cerenia Sep 30 '20 at 06:53
  • I need to implement PSO ,BAT algorithms in networkx @Cerenia – Ba. Taj Oct 01 '20 at 07:26