Version 22 (modified by, 7 years ago) (diff)


OpenFlow NAT Router



In this tutorial you will learn how to build a router for a network with a private address space that needs a one-to-many NAT (IP Masquerade) using OpenFlow. We will use the following network topology for this experiment. You will also learn how to take advantage of kernel L3 routing while using OVS . route topology


For this tutorial you need :


All the tools will already be installed at your nodes. For your reference we are going to use a Ryu controller.

Where to get help:

For any questions or problem with the tutorial please email


If you have already reserved the topology from a previous tutorial you can move to Execute.

1. Verify your Environment Setup:

This exercise assumes you have already setup your account at the GENI Portal. In particular ensure that:
  1. You can login to the GENI Portal
  2. You are a member of a GENI Project (there is at least one project listed under the ''Projects'' tab)
  3. You have setup your ssh keys (there is at least one key listed under the ''Profile->SSH Keys'' tab)

2. Setup the Topology:

  1. Login to the GENI Portal
  2. Reserve:
    1. the topology from an InstaGENI rack using the OpenFlow OVS all XEN RSpec (In Portal: "OpenFlow OVS all XEN"; URL:
    2. at a different InstaGENI rack reserve a XEN OpenFlow Controller RSpec (In Portal: "XEN OpenFlow Controller"; URL:


3. Test reachability before starting controller

3.1 Login to your hosts

To start our experiment we need to ssh into all the hosts (controller, ovs, host1, host2, host3). Depending on which tool and OS you are using there is a slightly different process for logging in. If you don't know how to SSH to your reserved hosts take a look in this page. Once you have logged in, follow the rest of the instructions.

3.1a Configure OVS

  1. Write down the interface names that correspond to the connections to your hosts (use ifconfig). The correspondence is:
    • h1_if: Interface with IP to host1 - ethX
    • h2_if: Interface with IP to host2 - ethY
    • h3_if: Interface with IP to host3 - ethZ
  2. In the OVS node run:
    wget ; chmod +x
    sudo ./ <h1_if> <h2_if> <h3_if> <controller_ip>
    }}} The controller_ip is the public IP of your controller node, look at the end for a tip on how to get the IP of a node. 
     iii. Look around to see what the script did: 
        sudo ovs-vsctly show
    === 3.1b Configure hosts ===
    The hosts in your topology are all in the same subnet, We will move host3 to a different subnet: 
      i. '''host3''': Assign to host3.
       sudo ifconfig eth1
     ii. '''host1, host2''': Setup routes at `host1` and `host1` to subnet: 
       sudo route add -net netmask gw
    === 3.2 Test reachability ===
    a. First we start a ping from `ihost1` to `host2`, which should work since they are both inside the same LAN.
    host1:~$ ping -c 10
    b. Then we start a ping from `host3` to `host1`, which should timeout as there is no routing information in its routing table. You can use `route -n` to verify that. 
    host3:~$ ping -c 10
    c. Similarly, we cannot ping from `host1` to `host3`.
    d. You can also use Netcat (`nc`) to test reachability of TCP and UDP. The behavior should be the same.
    == 4 Start controller to enable NAT ==
    === 4.1 Access a server from behind the NAT ===
    You can try to write your own controller to implement NAT. However, we a provide you a functional controller. 
    a. Download the NAT Ryu module. At your controller node run:
    cd /tmp/ryu/
    tar xvfz ryu-nat.tar.gz
    b. Start the controller on `controller` host:
     cd /tmp/ryu/; ./bin/ryu-manager
    You should see output similar to following log after the switch is connected to the controller
    loading app
    loading app ryu.controller.dpset
    loading app ryu.controller.ofp_handler
    instantiating app ryu.controller.dpset of DPSet
    instantiating app ryu.controller.ofp_handler of OFPHandler
    instantiating app of NAT
    switch connected <ryu.controller.controller.Datapath object at 0x2185210>
    c. On `host3`, we start a nc server:
    host3:~$ nc -l 6666
    and we start a nc client on `host1` to connect it:
    host1:~$ nc 6666
    d. Now send message between each other and try the same thing between `host3` and `host2`.
    e. On the terminal of `controller`, in which you started your controller, you should see a log similar to:
    Created mapping 31596 to 59997
    Note that there should be only one log per connection, because the rest of the communication will re-use the mapping.
    === 4.2 Outside source ===
    You may be wondering whether it will behave the same if we use `host1` or `host2` hosts to be the nc server. You can try it and the answer is no. That's due to the nature of dynamic NAT.
    However, it will work if we can access the translation table on the switch.
    a. Look back into the log we got previously:
    Created mapping 31596 to 59997
    Now we know there is mapping between these two pairs.
    b. Now we start a nc server on `inside2` (`inside1` if your mapping shows on the according port:
    host1:~$ nc -l 31596
    c. Then on `host3`, we start a nc client:
    host3:~$ nc 59997
    d. `host3` and `host2` should be able to send messages to each other.
    e. Common solution of handling outside source is providing some way to manually create mapping in advance. We will leave it as an exercise for you to implement it.
    == 5 Handle ARP and ICMP ==
    One of very common mistakes that people make, when writing OF controller, is forgetting to handle ARP and ICMP message and finding their controller does not work as expected.
    === 5.1 ARP ===
    As we mentioned before, we should insert rules into the OF switch that allow ARP packets to go through, probably after the switch is connected.
    === 5.2 ICMP ===
    Handling ARP is trivial as NAT does not involve ARP. However, it's not the case for ICMP. If you only process translation for TCP/UDP, you will find you cannot ping between `host3` and `host1` while nc is working properly. Handling ICMP is even not as straightforward as for TCP/UDP. Because for ICMP, you cannot get port information to bind with. Our provided solution makes use of ICMP echo identifier. You may come up with different approach involves ICMP sequence number or others.
    a. On `host1`, start a ping to `host3`.
    host1:~$ ping
    b. Do the same thing on `host2`.
    host2:~$ ping
    You should see both pinging are working.
    c. On `host3`, use `tcpdump` to check the packets it receives.
    host3:~$ sudo tcpdump -i eth1 -n icmp
    You should see it's receiving two groups of icmp packets, differentiated by id.
    '''Make sure to delete the bridges especially if you are using the same topology for another tutorial.'''
      sudo ovs-vsctl del-br OVS1
      sudo ovs-vsctl del-br OVS2
    <table  border="0" cellpadding="0" cellspacing="0">
         <td valign="top" align="left">
    	<img src=""  width="150" alt="Finish"></a>
                   <h3><u> 6. Cleanup </u></h3>
               After you are done with the exercise and you have captured everything requested for the writeup, you should release your resources so that other experimenters can use
    them. In order to cleanup your slice :
                  <ol type="a">
                     <li>In Portal, press the <b>Delete</b> button in the bottom of your canvas </li>
                     <li> Select <b>Delete at used managers</b> and <b>confirm</b> your selection. </li>
    Wait and after a few moments all the resources will have been released and you will have an empty canvas again. Notice that your slice is still there. There is no way to delete a slice, it will be removed automatically after its expiration date, but remember that a slice is just an empty container so it doesn't take up any resources.
    [[Image(GENIExperimenter/Tutorials/Graphics:tip.png, 40, left)]]
    = Tips =
      * Remember that you can use “ifconfig” to determine which Ethernet interface (e.g., eth0) is bound to what IP address at each of the nodes.
      * In order to enable IP forwarding of packets on a node you have to execute the following command:
    sudo sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'