DEV Community

Miftahul Rizki
Miftahul Rizki

Posted on Originally published at miftahulrizki.eu.org

How to Create a Network Topology in Mininet: Built-in Topos and Custom Python Scripts

If you already have Mininet installed, the next question comes naturally: how do you actually create a network topology in Mininet? It's easy: you can spin up a virtual network with a single command — no coding required — or define your own custom topology in a Python file when you need something specific.

This hands-on guide — adapted from my Indonesian-language tutorial on creating Mininet topologies — walks you through both approaches, with commands you can run immediately on Ubuntu with Mininet installed.

What Is a Topology in Mininet?

A topology is the arrangement of hosts, switches, and links that makes up your virtual network, and it determines what scenarios you can simulate. Each virtual host runs as a Linux network namespace connected through virtual switches (usually Open vSwitch), so it behaves like a real network: it has IP addresses, responds to ping, and accepts flow rules.

Built-in Topologies with --topo

The fastest way to create a topology is the --topo option of the mn command: sudo mn --topo type,parameters. The most useful built-in types are minimal (1 switch + 2 hosts, the default), single (1 switch + N hosts), linear (N chained switches, one host each), and tree (a tree with configurable depth and fanout).

The examples I reach for most often:

  • One switch, three hosts — sudo mn --topo single,3: the go-to topology for basic practice (ping, iperf, simple OpenFlow rules).
  • Four chained switches — sudo mn --topo linear,4: great for multi-hop routing and watching packets traverse several hops.
  • Tree — sudo mn --topo tree,depth=2,fanout=2: mirrors a hierarchical campus network; ideal for subnetting and spanning-tree practice.
  • Sanity check — sudo mn: builds the default minimal topology; I always run it first to confirm a healthy install.

You can combine --topo with other options — for instance, sudo mn --topo single,3 --mac --switch ovsk --controller remote sets up three hosts with automatic MACs and a remote SDN controller such as POX or Ryu.

Custom Topologies with Python (--custom)

For a topology that matches a specific scenario, write a Python file defining your hosts, switches, and links, then load it with sudo mn --custom filename.py --topo toponame. The pattern never changes: subclass Topo, add switches with addSwitch, add hosts with addHost, connect them with addLink, and register the topology in the topos dictionary.

Here's a minimal example — one switch with three hosts, like a small office segment:

from mininet.topo import Topo

class OfficeTopo( Topo ):
    "Example office topology: 1 switch, 3 hosts."

    def __init__( self ):
        Topo.__init__( self )
        s1 = self.addSwitch( 's1' )
        h1 = self.addHost( 'h1' )
        h2 = self.addHost( 'h2' )
        h3 = self.addHost( 'h3' )
        self.addLink( h1, s1 )
        self.addLink( h2, s1 )
        self.addLink( h3, s1 )

topos = { 'office': ( lambda: OfficeTopo() ) }
Enter fullscreen mode Exit fullscreen mode

Save it as topo_office.py, then launch it:

sudo mn --custom topo_office.py --topo office
Enter fullscreen mode Exit fullscreen mode

To keep custom topologies clean: use loops for repetitive structures instead of repeating addHost, give topologies descriptive names like office or qos_test, set link parameters such as bw=10, delay='5ms' (with --link tc) to simulate slow or lossy links for QoS experiments, and keep one topology per file for easy debugging and reuse.

Testing Your Topology

Once the mininet> prompt appears, verify connectivity with pingall — a healthy topology ends with *** Results: 0% dropped (N/N received). Other useful CLI commands: net (node and link list), dump (node details including IPs), and exit to quit and clean up. One habit worth building: always run sudo mn -c before creating a new topology to clear leftover state — stale processes from a previous session are the most common cause of mysterious errors.

Start with the simplest built-in topology that answers your question, confirm pingall shows 0% dropped, then move to custom Python files as your needs grow. For the complete walkthrough with a terminal screenshot, extra tips, and an FAQ, read the full article on my blog. Happy experimenting!

Top comments (0)