Fork me on GitHub

Simulations

I am attempting to figure out how to do some network simulations using Linux. I learned about network namespaces when I was reading about Mininet and how it simulates SDN. Some googling pointed me to this article. It states that network namespaces give you the ability to have different and separate instances of network interfaces and routing tables. These instances are completely independent of each other.

Note

Please note that all of the commands shown in this post are run at the Linux terminal as root. If you are not root, you must add sudo in front of each command.

Host-Host Ping

This article makes me think I can do some simulations using network namespaces. The first thing I want to try is to set up two network namespaces and have them ping each other. The network topology is shown below.

--------         --------
|      |         |      |
|  H1  |<------->|  H2  |
|      |         |      |
--------         --------

I used the following steps to accomplish that.

ip netns add host1
ip netns add host2
ip link add h1 type veth peer name h2
ip link set h1 netns host1
ip link set h2 netns host2
ip netns exec host1 ip address add 10.0.0.1/24 dev h1
ip netns exec host2 ip address add 10.0.0.2/24 dev h2

The first two commands above create two network namespaces (host1 and host2). The purpose of those two namespaces to simulate two different hosts. The third line creates a Linux virtual Ethernet port pair. The two interfaces are called h1 and h2. The fourth and fifth lines assign interface h1 to the host1 namespace and interface h2 to the host2 namespace. The last two lines assign IP addresses to those two interfaces.

Once all of that setup is complete, I can ping each "host" from the other.

ip netns exec host1 ping 10.0.0.2
ip netns exec host2 ping 10.0.0.1

These two "hosts" are isolated from each other. Their interfaces do not appear on my default host when I run ip address`. They can only communicate with each other because of the virtual Ethernet Port pair that was created. I understand the virtual Ethernet Port pair to basically be two ports that are directly connected to each other. Data into one comes out the other and vice versa.

Bridge

Now that I can set up two "hosts" to talk to each other, I want to add a Bridge (switch) to the topology. The topology is shown below.

--------         ---------         --------
|      |         |       |         |      |
|  H1  |<------->|1 SW1 2|<------->|  H2  |
|      |         |       |         |      |
--------         ---------         --------

The commands below assume starting from scratch and do not build upon the commands above.

ip netns add host1
ip netns add host2
ip netns add sw1

The commands above create the network namespaces for the two hosts and the switch.

ip link add h1 type veth peer name sw1p1
ip link add h2 type veth peer name sw1p2
ip link add bridge1 type bridge

The commands above create two virtual Ethernet port pairs plus one Ethernet bridge. The first virtual Ethernet pair is the the link between H1 and SW1. The second is for the link between H2 and SW2. The bridge will be used on the switch.

ip link set h1 netns host1
ip link set h2 netns host2
ip link set sw1p1 netns sw1
ip link set sw1p2 netns sw1
ip link set bridge1 netns sw1

The commands above assign the interfaces to the appropriate network namespaces. h1 goes in host1. h2 goes in host2. sw1p1 and sw1p2 go in sw1. Finally, the bridge interface goes in sw1.

ip netns exec sw1 ip link set sw1p1 master bridge1
ip netns exec sw1 ip link set sw1p2 master bridge1

The commands above assign the two ports, sw1p1 and sw1p2, to the bridge bridge1.

ip netns exec host1 ip link set h1 up
ip netns exec host2 ip link set h2 up
ip netns exec sw1 ip link set bridge1 up
ip netns exec sw1 ip link set sw1p1 up
ip netns exec sw1 ip link set sw1p2 up

The commands above bring all of the interfaces up.

ip netns exec host1 ip address add 10.0.0.1/24 dev h1
ip netns exec host2 ip address add 10.0.0.2/24 dev h2

The commands above assign IP addresses to host1 and host2.

Once all of that setup is complete, I can ping each "host" from the other.

ip netns exec host1 ping 10.0.0.2
ip netns exec host2 ping 10.0.0.1

Comments