Logo
Network

Network Simulation with Mininet

Network Simulation with Mininet
4 min read
#Network

If you’re a software or network engineer looking to test or prototype network-centric applications, let me tell you about a tool that’s been a game-changer for me: Mininet. It’s like having a mini playground for networks right on your computer! You don’t need to be a networking wizard to get started, and it’s free to use, which makes it even better.

What Is Mininet?

Mininet is a tool that allows you to create virtual networks. Imagine you want to test how a new app or system would behave in a complex network with multiple devices, switches, and routers. Instead of buying a bunch of hardware or setting up a complicated lab, Mininet lets you create that entire setup virtually on your laptop or desktop. It simulates everything — hosts, switches, links — so you can run your experiments without breaking the bank.

Why Mininet Is So Useful

Mininet isn’t just for researchers or advanced network engineers; it’s for anyone who wants to tinker with networks. Here’s why I think it’s amazing:

  1. It’s Lightweight: You don’t need a powerful computer to run it. Mininet works great on an average laptop, and you can create surprisingly complex topologies without slowing things down.

  2. Realistic Simulation: Mininet doesn’t just pretend to be a network. It’s a real network running in a virtual environment. That means you can run actual applications on it, like a web server or database, and see how they perform.

  3. Flexibility: Whether you want a simple network with two devices or a complex one with dozens of switches and hosts, Mininet can handle it. You can even simulate different bandwidths, delays, or packet loss to test how your app would behave under different network conditions.

  4. OpenFlow Support: For those into software-defined networking (SDN), Mininet supports OpenFlow, which lets you experiment with cutting-edge network technologies.

  5. Easy to Learn: Mininet uses Python scripts to define networks, but even if you’re not familiar with Python, the commands are straightforward. Plus, there are tons of tutorials and examples online to help you get started.

What Can You Do With Mininet?

Here are just a few examples of what you can do with Mininet:

  • Test Your Applications: If you’re building a networked app, you can see how it performs under different conditions without needing a physical network. You can easily introduce undesirable conditions such as excessive latency or even device failures to see how your application handles these conditions.

  • Learn Networking: Mininet is a great way to learn about networking concepts like routing, switching, and protocols without needing expensive equipment.

  • Prototype SDN Solutions: If you’re into SDN, you can use Mininet to test your ideas and see how they work in a virtual network.

  • Experiment with Security: Want to test a firewall rule or analyze network traffic? Mininet can simulate scenarios that help you explore network security concepts.

Getting Started with Mininet

The best part is how easy it is to get started. You can install Mininet on a Linux system or even use a virtual machine if you’re running Windows or macOS. Once it’s set up, you can create a basic network with just one command. From there, you can customize your network with Python or use the built-in examples to experiment.

Here’s a simple example to create a network with two hosts and a switch and a unit test to verify the two hosts can ping each other:

import pytest
import logging

from mininet.topo import Topo
from mininet.net import Mininet

LOGGER = logging.getLogger('TestSingleSwitchTopology')

class SingleSwitchTopo(Topo):
    """
    Single switch connected to two hosts.
    """
    def build(self):
        switch = self.addSwitch('s1')

        host1 = self.addHost('h1')
        host2 = self.addHost('h2')

        self.addLink(host1, switch)
        self.addLink(host2, switch)


class TestPing():

    @pytest.fixture(autouse=True)
    def setup(self):
        topo = SingleSwitchTopo()
        self.net = Mininet(topo)
        self.net.start()
        print(f"\nSet up network topology...")

        yield

        print(f"\nTear down network topology...")
        self.net.stop()


    def testPingSuccess(self):
        h1 = self.net.get('h1')
        h2 = self.net.get('h2')
        pkt_loss = self.net.ping([h1, h2])
        assert pkt_loss == 0.0, "Ping test failed with packet loss"

    def testPingHostUnreachable(self):
        h1 = self.net.get('h1')
        h2 = self.net.get('h2')
        LOGGER.debug("\nDisabling h2's network interface...")
        h2.cmd('ifconfig h2-eth0 down')
        pkt_loss = self.net.ping([h1, h2])
        assert pkt_loss == 100.0, "Expected 100% packet loss with host 2 down"

This command creates a network with two virtual hosts connected to a switch. You can ping between the hosts, test applications, or dive deeper into custom topologies.