Tag Archives: Ubuntu

Understanding BSD Linked List used in AODV routing protocol in ns2.34

Hello Folks, It’s been a long time since i posted. i had been studying the AODV routing protocol and am still trying to make it energy aware. Most of the people who are reading this post must already know about BSD linked list. This list is under the directory /ns-2.34/lib/bsd-list.h. It contains few macros for defining a doubly linked list that is used in AODV for maintaining routing tables, precursor lists, neighbor caches and Broadcast ID cache.

I searched a lot trying to understand the aodv code but the usage of bsd-list was a major hindrance in understanding it. Even after a lot of searching i could not find good material to read as it involved complex use of pointers. In the end I studied the bsd-list.h and few help from the internet I was able to relate how things are actually working. In this post I would explain the working of BSD list and how it actually works. I would not go into the inner details as the macros defined do most of the functionality themselves therefore studying them in huge detail becomes irrelevant. And yes this post would assume that you first go and read about pointers and try implementing a singly linked list and a doubly linked list on your own in C++. (Believe me that would really help).

The source files needed for this program can be downloaded from here:

Step 1) bsd-list.h contains the following macros

#define LIST_HEAD(name, type) \
struct name { \
type *lh_first; /* first element */ \
}

#define LIST_ENTRY(type) \
struct { \
type *le_next; /* next element */ \
type **le_prev; /* address of previous next element */ \
}

#define LIST_INIT(head) { \
(head)->lh_first = NULL; \
}

#define LIST_INSERT_AFTER(listelm, elm, field) { \
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \
(listelm)->field.le_next->field.le_prev = \
&(elm)->field.le_next; \
(listelm)->field.le_next = (elm); \
(elm)->field.le_prev = &(listelm)->field.le_next; \
}

#define LIST_INSERT_BEFORE(listelm, elm, field) { \
(elm)->field.le_prev = (listelm)->field.le_prev; \
(elm)->field.le_next = (listelm); \
*(listelm)->field.le_prev = (elm); \
(listelm)->field.le_prev = &(elm)->field.le_next; \
}

#define LIST_INSERT_HEAD(head, elm, field) { \
if (((elm)->field.le_next = (head)->lh_first) != NULL) \
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\
(head)->lh_first = (elm); \
(elm)->field.le_prev = &(head)->lh_first; \
}

#define LIST_REMOVE(elm, field) { \
if ((elm)->field.le_next != NULL) \
(elm)->field.le_next->field.le_prev = \
(elm)->field.le_prev; \
*(elm)->field.le_prev = (elm)->field.le_next; \
}

Now i will give a certain example to understand the working of the above bsd-list.

Step 2) First of all create a .cpp file with the name workingbsdlist.cpp and inside the same directory copy the file bsd-list.h from /ns-2.34/lib/bsd-list.h directory.

The code for workingbsdlist.cpp is as follows (u can always copy paste)

#include
#include
#include"bsd-list.h"

using namespace std;

struct foo {
int a;
LIST_ENTRY(foo) pointers; // pointers is the object of the structure generated by List Entry
}*temp, *var, *ptr;

LIST_HEAD(foo_list, foo);

int main(void)
{
LIST_HEAD(foo_list, foo) head;
LIST_INIT(&head);

struct foo *item1 = new foo;
struct foo *item2 = new foo;
struct foo *item3 = new foo;
item1->a = 60;
item2->a = 120;
item3->a = 240;
LIST_INSERT_HEAD(&head, item1, pointers);
LIST_INSERT_AFTER(item1, item2, pointers);
LIST_INSERT_BEFORE(item2, item3, pointers);
//Displaying inner details of list
{
cout<<"HEAD's Address : "<<head.lh_first<<endl;
cout<<"Item 1 next value : "<pointers.le_next<<endl;
cout<<"Item 1 prev value : "<pointers.le_prev<<endl;
cout<<"HEAD's Address : "<<head.lh_first<<endl;
cout<<"Item 2 next value : "<pointers.le_next<<endl;
cout<<"Item 2 prev value : "<pointers.le_prev<<endl;
cout<<"HEAD's Address : "<<head.lh_first<<endl;
cout<<"Item 3 next value : "<pointers.le_next<<endl;
cout<<"Item 3 prev value : "<pointers.le_prev<pointers.le_next)
{
cout<a<<endl;
}
return (0);
}

Although the above example may be quite vague and non-coherent but it is good for an intermediate C++ programmer to learn.

Step 3) Now i shall explain each and every line of this program and how it uses the bsd-list.h header file to create a doubly linked list. Once u understand this it becomes very easy to understand code in aodv.cc

Firstly we create a structure named as foo and within it we declare a variable a.
Within the structure we call the macro using

LIST_ENTRY(foo) pointers;

Seeing the macro in bsd-list.h. This expands to the following (keep watching closely)

struct foo {
int a;
struct { // This is a nameless structure within a structure
struct foo *le_next; // pointer to structure foo
struct foo **le_prev; // pointer to a pointer that points to structure foo
} pointers; // structure variable of nameless structure
}*temp, *var, *ptr; // pointers to structure foo

Then we call the another macro in bsd-list.h as:

LIST_HEAD(foo_list, foo);

This expands to the following code:

struct foo_list // A new structure with the name foo_list that holds pointer to struct foo
{
foo *lh_first; //pointer to struct foo
}

Now in the main() function we call the same macro LIST_HEAD but this time an variable is also created for the structure foo_list. See below.

LIST_HEAD(foo_list, foo) head;

expands to

struct foo_list // A new structure with the name foo_list that holds pointer to struct foo
{
foo *lh_first; //pointer to struct foo
}head;

In the next statement another macro is called

LIST_INIT(&head);

This macro call expands to
(head)->lh_first = NULL;

This is same as head->lh_first = NULL, and it puts the value of the pointer lh_first to NULL. But see one thing the *lh_first is declared inside struct foo_list so it can only be accessed by variables of foo_list. Thus we can access it using head. Thus we write head->lh_first = NULL. This also means that the head element currently will point to NULL.

Next we declare 3 generate 3 new nodes item1, item2, item3 which are actually pointers to struct foo. We enter info in their variable a using item1->a = 60; etc.

Now we shall use the three macros that actually define the insertion operations. U do not need to study the internal details of these three macros. They shall do as they say. For example

The first macro we use is to insert at the head of the list.
LIST_INSERT_HEAD(&head, item1, pointers);

seeing this macro in bsd-list.h we have
#define LIST_INSERT_HEAD(head, elm, field)
This macro takes three arguments
1. address of head (remember that head initially points to nothing because we set its value to NULL initially).
2. node that you want to insert (item1, item2, item3 etc).
3. field which is structure variable of the nameless structure that resulted on macro expansion

The other two items are after and before that are defined in bsd-list.h as
#define LIST_INSERT_AFTER(listelm, elm, field) {....}
#define LIST_INSERT_BEFORE(listelm, elm, field) {....}

They take three arguments.
1. First argument is the listelm after or before which u want to insert.
2. Node which u want to insert such as item1, item2 etc.
3. field which is a structure variable of nameless structure defined above (pointers in our ex)

Similarly the remove macro. It becomes very easy to use and understand if above two are clear.

Then we display some inner details of the list and variables. Usually to see how addresses are used and nodes are referenced. What values are stored in next and prev. What are their addresses. Have we achieved success.

In the end we print all the elements of the list. This is a tricky for loop to understand and is quite different from the conventional method. Remember this for loop can be written in two ways. I shall explain both of them so that u do not face any difficulty when u see them in AODV code in ns-2.34.

1st One)

ptr = head.lh_first;
for(;;ptr = ptr->pointers.le_next)
{
cout<a<<endl;
}

First ptr is a pointer to struct foo. We want it to point to head of the list. And head is pointed to by lh_first. So we make it point using ptr = head.lh_first. (u should understand why we used head.lh_first).
This for loop will check for ptr is not equal to NULL and it will be incremented every time by
ptr = ptr->pointers.le_next.
Remember ptr is pointer variable of struct foo. And by definition we will have to use -> sign to access le_next. But le_next is inside a nameless structure whose variable is pointers.
Thus we write ptr = ptr->pointers.le_next.

2nd One) It is similar to earlier one except
for(;ptr;ptr = ptr->pointers.le_next)
This is one more method to access or traverse the list. This will check till ptr has a valid value or not.

Hope u understand this. And if any queries arise please feel free to ask.

Note:- These are my own views and I am not a professional C++ programmer. Some things may have totally different interpretations when seen by different experienced programmers. If i am wrong somewhere please correct it and accept my apologies.

[SOLVED] Ubuntu 12.04 boots to Command Line when Rebooting after Kernel Update

Hi Folks,

Recently I came around this problem, that whenever I used to update my linux (kernel) which is currently now (3.2.0-43-generic-pae), on restarting my machine after updation my machine would always boot to linux command line asking for the userid and password. It would not boot to the GUI. On searching quite a bit I found out that this problem was due to a problem with graphics card driver update. The kernel update was unable to load properly the previously installed driver and showed a message no graphics driver is installed. To overcome this problem and boot to your GUI simple steps have to be followed. These steps simply remove any installed graphics driver which cause problems and u can later install the graphics card driver specific to your machine later on when u boot to GUI.

Step 1) when u boot to command line enter your user id and password correctly. Then enter the following commands line by line.

sudo sh /usr/share/ati/fglrx-uninstall.sh

sudo apt-get remove --purge fglrx fglrx_* fglrx-amdcccle* fglrx-dev*

after these commands run successfully perform a reboot using

sudo reboot

Then u shall be able to boot into your GUI. Now u need to install your graphics card driver again. If you tend to have a AMD Radeon HD 6300 series graphics card u can follow how to install post <here>.

[GUIDE To] Installing AMD Radeon HD 6300 Series Graphics Card Driver in Ubuntu 12.04

AMD Radeon 6300 HD Series

Hello Folks,

In this post, I would tell you about installing Graphics Driver for AMD Radeon HD 6300 Series (on a laptop) in Ubuntu 12.04. I had a tough time installing this driver as I had to read a ton of tutorials to actually figure out the correct way.

Note: Do not download any AMD graphics card driver from the Ubuntu Software Centre or Hardware Settings.

The problem that made me install this particular driver was that if u do not install this driver, Ubuntu just tends to constantly use your graphics card for rendering normal graphics (i.e. graphics that really do not need the use of your graphics card). This leads to heat up of the laptop so frequently that everytime it gave me thermal heatup shutdowns. It was really annoying. Thus I would recommend everyone to install graphics driver on their Ubuntu if their laptop comes with a separate graphics card.

Note: No need to install graphics driver if u have your graphics card onboard. Ubuntu just works fine with an onboard intel graphics card.

Step 1) Download the graphics card driver for your linux version from the AMD website here.

Step 2) Save the downloaded file in your home directory and Extract it in the home directory by using archive manager. Right click on the file and open with archive manager and extract it in home directory.

Step 3) Rename the extracted file to driver.run

Step 4) Now open terminal using Ctrl + Alt + T and type

sudo chmod +x driver.run

sudo ./driver.run

Step 5) An AMD catalyst control window would open. U can proceed by pressing next and performing the installation. After successful installation Reboot your laptop.

Step 6) Open Terminal Ctrl + Alt + T and type

fglrxinfo

u should get the following output

fglrxinfo
display: :0 screen: 0
OpenGL vendor string: Advanced Micro Devices, Inc.
OpenGL renderer string: AMD Radeon HD 6300M Series
OpenGL version string: 4.2.11733 Compatibility Profile Context

The graphics driver has been installed successfully.

[GUIDE TO] Configuring ns-2.34 with Eclipse Galileo in Ubuntu 12.04

Hi Folks,

In this post I shall explain about how to configure ns-2.34 with Eclipse Galileo. I faced certain problems while configuring ns-2.34 with eclipse as after successful configuration ns-2.34 would compile with errors. This gave me headaches and after lot of working around I found the correct method. So this guide would configure ns-2.34 with eclipse and ns-2.34 would build and compile without giving any errors.

Operating System   : Ubuntu 12.04
Network Simulator v: ns-2.34

Note: Hope u have downloaded and installed ns-allinone-2.34 fixed and patched by me. If you have not u can follow the guide <here>.

First of all in order to run eclipse u need to set up a JAVA environment on your UBUNTU 12.04. Three easy commands in the terminal will set it up for u, if JAVA environment is not present on your Ubuntu.

Open Terminal using Ctrl + Alt + T. And enter the following commands one after the another..

sudo apt-get install openjdk-7-jre
sudo apt-get install openjdk-7-jdk
sudo apt-get install icedtea6-plugin

This setups a Java environment for you.

Step 1) Download Eclipse Galileo (Eclipse IDE for C/C++ developers) from the eclipse website. Or u can download it from <here>.
U also need to download the CDT plugin for Eclipse. U can download it from <here> or from eclipse website.

Step 2) Extract eclipse in the home directory by right clicking on the file eclipse-cpp-galileo-linux-gtk.tar.gz and selecting open with archive manager. Now extract it using the extract button in the home directory. After extraction a directory with the name eclipse should be created in your home directory.

Step 3) Now right click on the file (i.e. the CDT plugin downloaded) cdt-master-6.0.2.zip and select open with archive manager. Now extract it using the extract button on top. Note: When u extract it, extract it inside the eclipse directory created earlier in your home directory and not anywhere else.

Step 4) Now open the eclipse folder and run eclipse. It will ask to select a workspace. Browse to the ns-allinone-2.34 directory in your home folder and select it. U can also make it default and set it to not ask each and every time u start eclipse.

Step 5) After eclipse opens. Browse to File –> New –> C++ Project.

Step 6) A window will open asking u to create a C++ Project. (Now u need to be careful)
In the Project Name field enter ns-2.34. It shall prompt that a directory/project with that name already exists. Ignore it.
Now under the Project Type. Select MakeFile Project.
And Under MakeFile Project. Select Empty Project.
Now Under the ToolChains section (on the right). Select Linux GCC.
And press Next.

Step 7) Now ns-2.34 will be added under your projects listing. To the left of the eclipse window.
Now go to Project –> Build All (It shall build without any errors.)
Now go to Project –> Clean and select ns-2.34 and press OK. (It shall compile without any errors).
Now go to Run –> Run (A % sign should appear on the console below).