alt text 

Experiment I: Introduction to Network Programming and Mininet Network Emulator

Ahmet Sekercioglu and Leon Seng

Objectives

In this lab, we will make a start on learning the fundamentals of network programming in Python, and become familiar with Mininet, an interesting technology for prototyping large packet networks on a single computer.

Experiment: Exploring Your Host's Network Interfaces

Discovering Host's Name and IP Address

First, let's write, as a warm-up, our first Python program to identify and print our host's name and IP address. The Python code for doing this as follows:

# Adapted from Listing 1.1 of Python Network Programming Cookbook [sar14]

import socket

def print_machine_info():
    host_name = socket.gethostname()
#    ip_address = socket.gethostbyname(host_name)
    print("Host name: ", host_name)
#    print("IP address: ", ip_address)

if __name__ == '__main__':
    print_machine_info()

Start a an editor session on a terminal window: gedit print_machine_info.py, copy and paste above lines in, save and exit.

Run the script: on a terminal window: Type python print_machine_info.py.

Uncomment the lines involving the ip_address, and re-run your script. Depending on the network configuration of the system, it may run without an error. Does it return an IPv4 or IPv6 address?

Processing System Networking Commands

There are interesting networking related system commands that tell us plenty of information about the TCP/IP protocol stack. In this exercise we will learn about how we can use them in a Python script. Our system command for this exercise is ip route show default (type it on a terminal window to see what it does). The ip command set has a huge functionality, for details, have a look at the ip Command Cheat Sheet.

You will complete the missing parts of the function print_default_gateway() in the below script to print the interface's default gateway:

import socket
import subprocess

def print_machine_info():
    host_name = socket.gethostname()
#    ip_address = socket.gethostbyname(host_name)
    print("Host name: ", host_name)
#    print("IP address: ", ip_address)

def print_default_gateway():
    result = subprocess.run("ip route show default", shell=True, stdout=subprocess.PIPE)
    output = result.stdout.decode("utf-8").strip()
    default_gw =                 # You need to complete this line.
    print("Default gateway: ", ) # You need to complete this line.

if __name__ == '__main__':
    print_machine_info()
    # You need to add something here.

Don't forget to show the running program to your lab demonstrator.

Host's All Network Interfaces

We will now start digging deeper, and investigate the network interface(s) of the computer further. If you issue the command ip address show on a terminal window, it will display the IP addresses assigned to all interfaces. Write down the names of the all interfaces and the IP addresses assigned to them. Find out what the lo interface does. Your lab demonstrator will ask this question.

Now, here is your challenge: Let's write a Python program which will report all network interfaces, and the IPv4 and IPv6 addresses assigned to them in the following format:

iface IPv4 Address    netmask         broadcast
----- --------------- --------------- ---------------
lo    VVV.XXX.YYY.ZZZ VVV.XXX.YYY.ZZZ VVV.XXX.YYY.ZZZ
eth0  VVV.XXX.YYY.ZZZ VVV.XXX.YYY.ZZZ VVV.XXX.YYY.ZZZ
...   ...             ...             ...
...   ...             ...             ...

iface IPv6 Address           netmask
----- ---------------------- ---------------------
lo    fe80::d:bcff:fe45:91c8 ffff:ffff:ffff:ffff::
eth0  fe80::d:efef:fe99:11c5 ffff:ffff:ffff:ffff::
...   ...                    ...
...   ...                    ...

Side note/question: Why don't we have a broadcast column in the IPv6 list?

We think that this will be a good exercise for experimenting with two powerful Python data structures: lists and dictionaries (Chapter 4 of Learning Python could be very helpful).

Almost everything you need is provided by this nice Python library called netifaces (short for network interfaces). There are more examples here but they were written for Python 2.X (In Python 2, the usage of print statement and method for accessing a list's elements are different). Nevertheless, they are still good examples to check out.

You may wish to start with the following script (let's call it print_interfaces.py):

#!/usr/bin/env python

import netifaces

def print_interfaces():
    for iface in netifaces.interfaces():
        iface_details = netifaces.ifaddresses(iface)
        print(iface, iface_details, '\n--\n')

if __name__ == '__main__':
    print_interfaces()

It is best to run this script within a jupyter-qtconsole and check out the contents of iface_details. Enter the type(iface_details) command at the prompt to see what type of data structure it is. This will help you to think about how to access list and dictionary elements individually.

Don't forget to show the running script to your lab demonstrator when you finish.

Experiment: Building Virtual Networks with Mininet

What is Mininet?

Mininet creates a realistic virtual network of virtual hosts, switches, controllers, and links on the computer it runs. The machine that mininet runs on could be a virtual machine itself like the one we access through our MoVE environment, a little bit mind boggling!

Mininet is a network emulator (as opposed to a simulator). If you are enrolled to the Network Performance unit, you will learn a bit about the discrete-event simulators. The huge advantage of using Mininet is, emulated Mininet hosts run standard Linux network software, which allows you to create an almost real network without the need for investing in networking hardware. Consequently, we can say that it is a great tool for prototyping ideas and testing performance boundaries. Chapter 18 of [dor19] has some quite useful information about Mininet.

Mininet switches support OpenFlow for highly flexible custom routing and Software-Defined Networking technologies. We will not cover these topics in-depth in our unit, but they are fertile research areas, and if you are considering post-graduate studies, it is very worthwhile to look into them.

Network Setup

We will begin our experiments by creating a network of four hosts (h1, h2, h3, and h4), two Ethernet switches (s1 and s2), and a single router (r1) as shown below:

Download this Python script: 1router_net.py, and run it: sudo python 1router_net.py. It will create an emulated network that for all practical purposes we will treat it like a real TCP/IP network, and observe packets flowing through the links connecting the nodes.

Network Connections and Packet Flows

We can now investigate the structure of the created network. At the mininet> prompt, type the command net. You should be able to see the following information:

mininet> net
r1 r1-wan0:s1-eth1 r1-wan1:s2-eth1
h1 h1-eth0:s1-eth2
h2 h2-eth0:s1-eth3
h3 h3-eth0:s2-eth2
h4 h4-eth0:s2-eth3
s1 lo:  s1-eth1:r1-wan0 s1-eth2:h1-eth0 s1-eth3:h2-eth0
s2 lo:  s2-eth1:r1-wan1 s2-eth2:h3-eth0 s2-eth3:h4-eth0
c0

It tells us that, for example, r1 has two network interfaces r1-wan0 and r1-wan1, and they are connected to the network interfaces of s1 and s2's s1-eth1 and s2-eth1 interfaces. As we mentioned earlier, they look like real network interfaces to us.

We can confirm this: Open a separate terminal window, and issue the command netstat -i. You may have noticed one important detail: Only the switch network interfaces are exposed to the underlying host. Since a packet entering into a host or a router interface has to go through a switch interface, we can see all the packets when we use Wireshark for example.

Start the Wireshark program in the second terminal window (the command is sudo wireshark), and choose an interface that the packets from h1 to h3 among the interface list Wireshark shows. You may need to consult the output of the Mininet net command.

Then, at the mininet> command prompt, issue the command h1 ping h3. Observe what happens.

Running Commands Directly on Hosts

You can run commands on each host via one of the following methods:

  • You can execute directly at the mininet prompt by entering <host> <command>. For example: mininet> h1 ip addr

  • You can launch a terminal for a host by entering the xterm <host> at the mininet prompt, and then running the command in the host terminal as you normally would do on a Linux terminal.

Network Interfaces and Addresses

Draw the network diagram of our Mininet network on a sheet of paper, and using the Python program you have developed in the first part of the experiment, perform the following tasks on all hosts

  • List the all network interfaces on each host, and

  • Write the IP and MAC addresses of each node (hosts, router, and switches)

Once you have the IP addresses, verify their correctness by performing ping commands from host h1 to all other hosts (use the IP addresses you have identified, not the host names)

Challenge I

Have you noticed the problem? Somehow h2 can not reach h3? Find the cause of the problem, and fix it. You need to examine the Mininet script 1router_net.py for this purpose.

Challenge II

This is a subnetting and IP address allocation challenge which network engineers regularly encounter in their careers. You will use Mininet to create a network to test the IP address allocations you will make. All the hints you require are in the Mininet script 1router_net.py.

The scenario is this: We have a company network with an allocated address block 192.168.0.0/16 as shown in the diagram below:

The IT engineers split the company network into four switched LANs, serviced by s1 (subnet 192.168.11.0/24), s2 (subnet 192.168.12.0/24), s3 (subnet 192.168.13.0/24), and s4 (subnet 192.168.14.0/24). You need to determine the appropriate IP addresses for router to router connections.

Starting with the Mininet script 1router_net.py, create this network, and test the connectivity by pinging each host to every other host.

Show the results to your lab demonstrator.

Your Report

Your Report

After finishing your experiments, you will need to prepare a short (maximum two pages, 10 pt Times-Roman font) report summarizing the key points you have learned in this exercise.

Please convert your report to PDF (no other formats will be accepted), zip your report with all the Python code you have written for the experiment, and upload all as a single zip file to the unit's Moodle site before the due date (we will post the deadlines at the unit's Moodle site).

References


Ahmet's Home