Friday, January 6, 2017

Using Python with ns-3

Though ns-3 is written in C++, it has an Python binding. The code sample below shows how to use Python to create an network experiment.

  • It creates a network of three nodes. Two of them are connected via point to point link and two of them are connected via Ethernet.

        # Topology
        #         10.1.1.0
        #    n0 -------------- n1   n2
        #       point-to-point  |    |
        #                       ======
        #                     LAN 10.1.2.0
  • It setup a Tcp socket client on node n2 which keep sending packets to a Tcp socket server on node n0.

  • To simulate packet drops, a error model was used to generate 1 packet drop per 100 packets.
  • The data transfer is captured via Pcap.

Tip
The ns.core module is created in a subtle way in ns-3 Python binding that most of the Python IDEs cannot get the intellisense from the module. That is because all the classes/functions in ns.core are imported dynamically via code (See [ns_root]/src/core/bindings/core.py).

The only way so far I found to work around it is remove the core.py file and rebuild. After that a core.so module will be generated instead of a _core.so. IDEs are happy with core.so.



Here is the code:

import sys
from ns3 import *


def create_nodes(n):
    return (Node() for _ in range(n))


def node_container(*nodes):
    c = NodeContainer()
    for n in nodes:
        c.Add(n)
    return c


def connect_nodes_with_p2p_channel(nodes, rate, delay, pcap_name):
    helper = PointToPointHelper()
    helper.SetDeviceAttribute("DataRate", StringValue(rate))
    helper.SetChannelAttribute("Delay", StringValue(delay))
    devices = helper.Install(nodes)
    if pcap_name is not None:
        helper.EnablePcapAll(pcap_name)
    return devices


def connect_nodes_with_csma_channel(nodes, rate, delay, pcap_name):
    helper = CsmaHelper()
    helper.SetChannelAttribute("DataRate", StringValue(rate))
    helper.SetChannelAttribute("Delay", StringValue(delay))
    devices = helper.Install(nodes)
    if pcap_name is not None:
        helper.EnablePcapAll(pcap_name)
    return devices


def install_internet_stack(nodes):
    stack = InternetStackHelper()
    stack.Install(nodes)


def set_receive_error(device, error_packet_seq_ids):
    of = ObjectFactory()
    of.SetTypeId('ns3::ReceiveListErrorModel')
    em = of.Create()
    em.SetList(error_packet_seq_ids)
    device.SetAttribute("ReceiveErrorModel", PointerValue(em))
    return em


def create_interfaces(devices, ip_base, mask):
    address = Ipv4AddressHelper()
    address.SetBase(Ipv4Address(ip_base), Ipv4Mask(mask))
    interfaces = address.Assign(devices)
    return interfaces


def get_protocol_type_id(protocol):
    if protocol == 'tcp':
        return 'ns3::TcpSocketFactory'
    elif protocol == 'udp':
        return 'ns3::UdpSocketFactory'
    else:
        return None


def install_packet_sink_app(node, protocol, port):
    protocol_type_id = get_protocol_type_id(protocol)
    helper = PacketSinkHelper(protocol_type_id, Address(InetSocketAddress(Ipv4Address.GetAny(), port)))
    app = helper.Install(node)
    return app


def install_on_off_app(node, protocol, server_address, server_port, rate=None, packet_size=None, on_time=None, off_time=None):
    protocol_type_id = get_protocol_type_id(protocol)
    helper = OnOffHelper(protocol_type_id, Address(InetSocketAddress(server_address, server_port)))
    if rate is not None:
        helper.SetAttribute("DataRate", DataRateValue(DataRate(rate)))
    if packet_size is not None:
        helper.SetAttribute("PacketSize", UintegerValue(packet_size))
    if on_time is not None:
        helper.SetAttribute("OnTime", get_constant_random_variable(on_time))
    if off_time is not None:
        helper.SetAttribute("OffTime", get_constant_random_variable(off_time))

    app = helper.Install(node)
    return app


def get_constant_random_variable(constant):
    # return StringValue('ns3::ConstantRandomVariable[Constant=%d]' % constant)
    v = ConstantRandomVariable()
    v.SetAttribute('Constant', StringValue(str(constant)))
    return PointerValue(v)


def main():
    cmd = CommandLine()
    cmd.Parse(sys.argv)

    # Topology
    #         10.1.1.0
    #    n0 -------------- n1   n2
    #       point-to-point  |    |
    #                       ======
    #                     LAN 10.1.2.0

    n0, n1, n2 = create_nodes(3)

    p2p_nodes = node_container(n0, n1)
    p2p_devices = connect_nodes_with_p2p_channel(p2p_nodes, '2Mbps', '2ms', 'p2p')

    csma_nodes = node_container(n1, n2)
    csma_devices = connect_nodes_with_csma_channel(csma_nodes, '10Mbps', '1ms', 'csma')

    install_internet_stack(n0)
    install_internet_stack(n1)
    install_internet_stack(n2)

    set_receive_error(p2p_devices.Get(0), [100 * i for i in range(10)])

    p2p_interfaces = create_interfaces(p2p_devices, '10.1.1.0', '255.255.255.0')
    csma_interfaces = create_interfaces(csma_devices, '10.1.2.0', '255.255.255.0')

    server_address = p2p_interfaces.GetAddress(0)
    server_port = 8888
    server_app = install_packet_sink_app(n0, 'tcp', server_port)
    server_app.Start(Seconds(1.0))
    server_app.Stop(Seconds(10.0))

    client_address = csma_interfaces.GetAddress(1)
    on_off_app = install_on_off_app(n3, 'tcp', server_address, server_port, rate='2Mbps', on_time=1, off_time=0)
    on_off_app.Start(Seconds(2.0))
    on_off_app.Stop(Seconds(10.0))

    Ipv4GlobalRoutingHelper.PopulateRoutingTables()

    Simulator.Stop(Seconds(20.0))
    Simulator.Run()
    Simulator.Destroy()


def log_cwnd(oldval, newval):
    print('cwnd old: %d, new: %d' % (oldval, newval))


def start_flow(socket, address, port):
    def write_until_buffer_full(socket, available):
        print(available)
        n = 10000
        while socket.GetTxAvailable() > 0 and n > 0:
            to_write = min(4 * 1024 * 1024, socket.GetTxAvailable())
            sent = socket.Send(Packet(to_write), 0)
            if sent < 0:
                return
            n -= 1

    socket.Connect(InetSocketAddress(address, port))
    socket.SetSendCallback(write_until_buffer_full)
    write_until_buffer_full(socket, socket.GetTxAvailable())


if __name__ == '__main__':
    main()

Visualize ns-3 network using PyViz

The steps below can be use to enable the PyViz visualizer for ns-3 python binding. For C++ code, you could follow the steps in the referenced post.

  1. Install prerequisites

    sudo apt-get install python-dev python-pygraphviz python-kiwi python-pygoocanvas python-gnome2 python-gnomedesktop python-rsvg
  2. Change your python code

    import ns.visualizer
    [...]
    cmd = ns.core.CommandLine()
    cmd.Parse(sys.argv)
    [...]
    ns.core.Simulator.Run()
  3. Run your script with command line argument

    your_script.py --SimulatorImplementationType=ns3::VisualSimulatorImpl

    or

    ./waf --pyrun your_script.py --vis



Reference: PyViz

Thursday, January 5, 2017

Hello World from Network Simulator

ns-3 is a fabulous network simulator. It allows you to setup a custom network environment and do experiments with it. The reason I want to play with it is to learn what TCP Tahoe differs from TCP Reno in fact (instead of in theory).

Here is a list of steps to get started with ns-3.

  1. Get a Ubuntu VM

  2. Install prerequisits
    a. You could follow this document for details
    b. Basically, run the following command

    sudo apt-get install gcc g++ python gcc g++ python python-dev qt4-dev-tools libqt4-dev mercurial bzr cmake libc6-dev g++-multilib gdb valgrind gsl-bin libgsl2 flex bison libfl-dev tcpdump sqlite sqlite3 libsqlite3-dev libxml2 libxml2-dev libgtk2.0-0 libgtk2.0-dev vtun lxc uncrustify doxygen graphviz imagemagick texlive texlive-extra-utils texlive-latex-extra texlive-font-utils texlive-lang-portuguese dvipng python-sphinx dia python-pygraphviz python-kiwi python-pygoocanvas libgoocanvas-dev ipython libboost-signals-dev libboost-filesystem-dev openmpi-bin openmpi-common openmpi-doc libopenmpi-dev
  3. Download ns-3 sources

    cd
    mkdir workspace
    cd workspace
    wget http://www.nsnam.org/release/ns-allinone-3.26.tar.bz2
    tar xjf ns-allinone-3.26.tar.bz2
  4. Build ns-3

    cd ns-allinone-3.26
    ./build.py --enable-examples --enable-tests
  5. Create a hello world program

    cd ns-3.25/
    cp examples/tutorial/first.cc scratch/

    You could view the code in the end of the post.

  6. Build and run the program

    ./waf
    ./waf --run scratch/first

    The output of the program will be something like the following.

    At time 2s client sent 1024 bytes to 10.1.1.2 port 9
    At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
    At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
    At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9
  7. You can also use the Python binding to write the program. However it is still a work in progress and there are some limitations.

Sample Code

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
  Time::SetResolution (Time::NS);
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

  NodeContainer nodes;
  nodes.Create (2);

  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);

  InternetStackHelper stack;
  stack.Install (nodes);

  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

Shrink a Ubuntu Guest VDI File

When using a Ubuntu virtual box guest, sometimes we want to shrink the dynamic expanding VDI file to release the unused disk space. Here is the steps to archive that.

  1. Install ZeroFree

    sudo apt-get install zerofree
  2. Reboot into recovery mode
    a. sudo shutdown -r now
    b. Holding the left Shift key while rebooting
    c. Select Advanced and then Recovery mode and finally Drop to root shell prompt option.

  3. Remount the root partition

    mount -n -o remount,ro -t ext4 /dev/sda1 /
  4. Run ZeroFree

    sudo zerofree -v /dev/sda1
  5. Shutdown the VM

    shutdown -h now
  6. Shrink the VDI file is host OS

    SET PATH=%PATH%;"c:\Program Files\Oracle\VirtualBox
    VBoxManage modifyhd Ubuntu.vdi -compact


Reference: How to shrink a dynamically-expanding guest virtualbox image

Monday, October 6, 2014

Install OEM Windows in VirtualBox

The following steps can be followed to install OEM windows onto a virtual box.

  • Create new virtual machine
  • Edit the .vdm file of the virtual machine and add the following content under the element.
<ExtraDataItem name="VBoxInternal/Devices/pcbios/0/Config/DmiBIOSVendor" value="LENOVO"/>
<ExtraDataItem name="VBoxInternal/Devices/pcbios/0/Config/DmiSystemVendor" value="LENOVO"/>
  • Install OEM Windows onto the virtual machine.

In the example, the OEM is for IBM Corporation.

Friday, April 25, 2014

Build XulRunner with Visual C++ 2013

1. Prerequisites

1.1 MozillaBuild

1.2 Visual Studio Express 2013 for Windows Desktop

1.3 [June 2010 DirectX SDK] http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=6812

  • If you met with S1023 error whiling installing the DirectX SDK, please uninstall all Microsoft Visual C++ 2010 x86/x64 Redistributable packages (See this for details).

2. Source code

  • Download XulRunner source code from here
  • Extract to c:\build\mozilla
  • Apply the patch for vc12
  • Add #include <algorith> to C:\build\mozilla\toolkit\components\protobuf\google\protobuf\wire_format_lite_inl.h

3. Configure (x86)

Create a .mozconfig file in c:\build\mozilla folder. Enter the following content:

mk_add_options MOZ_CO_PROJECT=xulrunner
mk_add_options MOZ_OBJDIR=c:/build/mozilla/obj-x86
ac_add_options --enable-application=xulrunner

ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
ac_add_options --disable-javaxpcom
ac_add_options --disable-tests
ac_add_options --disable-ipdl-tests
ac_add_options --disable-activex
ac_add_options --disable-activex-scripting
ac_add_options --disable-installer
ac_add_options --disable-crashreporter
ac_add_options --disable-updater
ac_add_options --disable-update-channel
ac_add_options --disable-update-packaging
ac_add_options --disable-maintenance-service
ac_add_options --disable-accessibility
ac_add_options --disable-logging
ac_add_options --disable-services-healthreport
ac_add_options --disable-telemetry-reporting 
ac_add_options --disable-parental-controls 
ac_add_options --disable-windows-mobile-components 
ac_add_options --disable-necko-wifi
ac_add_options --disable-pdfjs
ac_add_options --disable-accessibility
ac_add_options --disable-gamepad

ac_add_options --with-windows-version=601

4.Build (x86)

Start /start-shell-msvc2013.bat, and run the commands below.

cd /c/build/mozilla
python build/pymake/make.py  -f client.mk build
python build/pymake/make.py  -f client.mk sdk

Once the build completed, the sdk archive can be found in C:\build\mozilla\obj-x86\dist\sdk.

For more detailed documentation. See Mozilla’s Official Build Instructions

Thursday, April 24, 2014

Download a Folder from Github

Here is way to download a single folder from a Github repository without fully clone the repo.

For example, if you want to download https://github.com/jack/foo/tree/master/bar folder, you could use the command below.

svn checkout https://github.com/jack/foo/trunk/bar

Note that the tree/master is replaced with trunk.