Recursive Complexity

Thoughts and musings of a programmer and wanna be entrepreneur

Author Archive

The Vast and the Trivial

leave a comment »

The vast


The trivial, taking a selfie :-X


To provide context, I was at the Acadia national park on Maine’s Mount Desert island (Click here for the location on google maps). A beautiful and well maintained recreational area that includes trails, sand beaches, ocean fronts, lakes, etc to keep you occupied for the duration of your stay.

The national park borders the north Atlantic ocean and provides spectacular views of the ocean from many different spots and drives on the island. Staring at the ocean is a very spiritual experience. You are blown away by the vastness, the sheer scale and you begin to wonder about your very own existence, your purpose, and most importantly (at least for me), your importance! To give some more perspective, click on the google maps link above and see what I am talking about. The park which is 47,000 acres in area is a tiny spec which borders a very tiny part of the north Atlantic ocean. If what I saw, a tiny part of the ocean, was so very vast and unending, then what is the scale and enormity of the whole Atlantic ocean ? How big is the earth ?

Staring at this vastness puts you in trance, a very blissful and enlightening experience. I never get tired of staring at the ocean. I suppose nobody does. The sound of the waves, the smell of the water and the unending body of water. All this puts ones own prominence into proper perspective. Your everyday fight for attention, recognition, success all become trivial, a mere fight for survival. Your existence or the lack of does not make any difference to this magnificent and beautiful planet.

I urge the readers, if there are any :P, to go to place where you can stare at the ocean at length and in solitude. You will return as a different person.


Written by Vivek S

July 5, 2016 at 12:01 am

Posted in Leisure, Spiritual

How to setup a SDN using openvswitch and ODL controller

with 4 comments

I am going to demonstrate the configuration of a simple SDN using openvswitch and OpenDaylight controller. The topology we are going to use is as shown below.

Openflow - New Page

In my setup all the nodes are running Ubuntu 14.04.

Step 1: Configure the switch

Login to the switch and do..

root@Switch6~# apt-get update
root@Switch6~# apt-get install openvswitch-switch
root@Switch6~# ifconfig -a

Note down all the interfaces which are connected to neighboring switches or to hosts. Do not consider the interface that is connected to the controller.

Lets say the interface list obtained from doing the above is eth1 (connected to Host 1), eth2 (connected to Host2) and eth3 (connected to Host 3)

root@Switch:~# ovs-vsctl add-br br0
root@Switch:~# ovs-vsctl set-fail-mode br0 secure
root@Switch:~# ovs-vsctl add-port br0 eth1
root@Switch:~# ovs-vsctl add-port br0 eth2
root@Switch:~# ovs-vsctl add-port br0 eth3
root@Switch:~# ovs-vsctl set-controller br0 tcp::6633

The ip address of the controller in our case is Hence, the last command above would be “ovs-vsctl set-controller br0 tcp:”

After this make sure to enable all the interfaces that form part of the above bridge, else you won’t be able to run traffic from one host to another

root@Switch:~# ifconfig eth1 up
root@Switch6:~# ifconfig eth2 up
root@Switch:~# ifconfig eth3 up

Step 2: Configure the controller

We will be using OpenDaylight controller, Lithium release for this demo.

Login to the machine that will be configured as the controller and download ODL Lithium release. You can download whichever release you are interested in. I have downloaded a tar ball and will proceed from there.

root@Controller:~# tar -xvf distribution-karaf-0.3.1-Lithium-SR1.tar.gz
root@Controller:~# apt-get install openjdk-7-jre-headless
root@Controller:~# export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
root@Controller:~# cd distribution-karaf-0.3.1-Lithium-SR1
root@Controller:~# bin/karaf

This will start the ODL controller and ODL prompt appears. For the controller to learn the topology and for us to make use of the web UI, we need to install odl-restconf odl-l2switch-switch odl-dlux-all at the ODL prompt (NOTE: odl-dlux-all installs some additional modules that provide you the ability to see node ports and flow tables. If you do not need this, you can just install odl-dlux-core).

opendaylight-user@root> feature:install odl-restconf odl-l2switch-switch odl-dlux-all

After the installation is completed, shutdown the controller and restart it. I have observed that the installed modules do not work immediately and restart of the controller nicely works :).

opendaylight-user@root> shutdown -f
root@Controller:~# bin/karaf

It’s mentioned in the ODL wiki that it takes approximately 3 minutes for the controller to load all modules and start working. So give it some time. After that, point your browser to “http://<ip-address-of-controller&gt;:8181/index.html”. This should launch the web UI as shown below. Use localhost if you are on the controller or use the management ip address configured.

Screen Shot 2015-11-16 at 9.43.51 PM

Use “admin/admin” to login. After a successful login, you should see the switch being recognized by the controller.

Screen Shot 2015-11-16 at 9.44.12 PM

Now, from any of the hosts, ping others and refresh the topology in the web UI. You should see a complete view of the network.

Screen Shot 2015-11-16 at 9.45.33 PM

The network is fully operable now.

Issues that can arise during and after the configuration

  1. Cannot ping hosts from each other – Ensure that the interfaces that form the bridge on the switch are enabled. Install wireshark on the switch as well as controller and capture packets on the interface that connects the switch to the controller and vice-versa. Verify whether PACKET_IN and PACKET_OUT messages are being sent and received. You would have to download a version of wireshark that has support for openflow.
  2. Login failure in ODL web UI – The auth module has not loaded and initialized yet. Give it some time and it should work.
  3. No topology seen in the ODL web UI – Same as above. Give it some time.

Written by Vivek S

November 17, 2015 at 3:32 am

Posted in Tech

Tagged with , , ,

Modulo operator in C

leave a comment »

If you are programming in C, then you would know that the way to find the remainder when an integer a is divided by n is by using the modulo operator ( % ), like this, r = a % n. This modulo operator or fundamentally the task of finding the remainder when an integer is divided by another integer, can be used to find whether an integer is odd or even. If an integer leaves remainder 1 when divided by 2, it’s odd, else it’s even. Let’s implement a function in C to do the same.

* This function returns true if an integer is odd, else false
bool is_odd(int a)
        return a % 2 == 1;

This is a pretty simple function and should help us in our quest. Let’s use this function to find whether the integers 1 and -1 are odd or not.

int main(int argc, char **argv)
        if (is_odd(1))
               printf("1 is odd");

       if (is_odd(-1))
               printf("-1 is odd");

       return 0;

Run this simple program on a Windows and a Linux machine and be startled. Instead of returning true for both values, the function is_odd returns true for 1 and false for -1, though it’s common knowledge that -1 is odd. Why is it so ? This is due to the way the modulo operation is implemented in the C language. In fact, different languages implement the task of finding the remainder differently and a programmer should be careful when using the modulo operator or a function that does the same in any programming language. The implementation in C is known as the truncated division operation to find the remainder. This Wikipedia article describes the different ways the modulo operation can be implemented. It also shows a table specifying the behaviour of the modulo operation in different programming languages.

In C, the truncated division operation gives a remainder whose sign is the same as that of the dividend. Hence, in function is_odd, a % 2, for a = -1 returns a remainder of -1 and the subsequent comparison with 1 returns false. The safe way to find whether an integer is odd or even using the modulo operator in C is to do the below.

bool is_odd(int a)
        return a % 2 != 0;

Thanks to this blog from my friend Shreevatsa which helped me look up the different ways of finding the remainder and their choice in a programming language like C or C++.

Written by Vivek S

January 12, 2015 at 5:24 pm

Posted in Tech

Tagged with ,

What’s wrong with this piece of code ?

with 6 comments

struct foo {
    int a, b;

void kstrtol(long *x)
    *x = -1;

int main(void)
    struct foo foo;
    foo.b = 42;
    kstrtol((long *)&foo.a);
    return 0;

Written by Vivek S

July 23, 2013 at 4:02 pm

Posted in Tech

Tagged with , , , ,

Application Of Logarithms To Exponential Values And Other Integers

leave a comment »

We will be dealing with positive integers and logarithms to base 10.

How do you calculate the no of digits present in an exponential value or for that matter a very huge integer ? The answer is simple as shown below.

no of digits = floor(log (x ^ y)) + 1

Explanation : The logarithm of any number to base 10 is the number of times 10 is multiplied with itself to obtain that number. A little experimentation will show that when 10 is multiplied with itself x times or put another way, is raised to power x, we obtain an integer which contains x + 1 digits. Hence, obtaining the base 10 logarithm of any number and adding 1 to it will give you the number of digits in that number. Carefully note the floor function being used around the logarithm as logarithms generally give you floating point or decimal values.

Find the first n digits of an exponential value. Eg – Find first 5 digits of 12^13.

x = log 12^13 = 13 * log 12 = 14.0293561

y = floor(10^(x - floor(x) + n - 1)) = floor(10^(14.0293561 - 14 + 5 - 1))

= floor(10^(4.0293561)) = 10699

y is the first five digits of 12^13.

12^13 = 106993205379072.
log 12^13 = 14.0293561.
10^14.0293561 = (10^14)×(10^0.0293561) = (100000000000000)×(10^0.0293561) ≈ 106993205379072
The above argument shows that 10^0.0293561 when multiplied by 10^14 adds 06993205379072 to 100000000000000. In other words, it gives the first 15 digits of 12^13. Hence, if we want the first 5 digits of 12^13 multiply 10^0.0293561 with 10^4 which is also the same as raising 10 to power 4.0293561

NOTE: As n increases, the logarithm log x^y also needs to be calculated for more decimal places.

Written by Vivek S

May 15, 2013 at 4:53 am

Posted in Tech

Tagged with , , ,

Finding the offset of a field inside a structure in C

leave a comment »

How do you find the offset of a field inside a structure from the beginning of the structure in C given only the structure name and the field name.

Below is the code.

#define offset_of(a, b) &(((a*)0)->b)

struct mystruct {
    int a;
    long b;
    int c;

void main()
    printf("%llu\n", offset_of(struct mystruct, c));

//output on a 64 bit machine


What we are doing here is typecasting 0 to be of type mystruct pointer which sets the beginning of the structure as 0 and then return the address of ‘c’ which should get us the offset. This is possible because of the ability to typecast variables to required types which is one of the strengths of C.

Written by Vivek S

April 18, 2013 at 8:43 am

Posted in Tech

Tagged with ,

Find Direction Of Growth Of Stack In C

leave a comment »

#include <stdio.h>

typedef unsigned long long UINT64;

UINT64 f(int b){
    return (UINT64)&b;

bool downwards(int a)
    int b = 0;

    if ((&a - f(b)) < 0)
        return false;
        return true;

int main(int argc, char* argv[])
    int a = 0;

    if (downwards(a))

    return 0;

Written by Vivek S

March 27, 2013 at 5:07 am

Posted in Tech

Tagged with , ,

TED Blog

The TED Blog shares interesting news about TED, TED Talks video, the TED Prize and more.