Saturday, January 28, 2012

My ISP finally happy with Ubuntu box

I was struggling to make my Ubuntu box work with login server of ISP. It was not getting better at all. Sometimes after 50 attempts, ISP used to say yes and login window appeared. However I was totally annoyed by this. When I switch back to Windows, everything was fine. I started digging to the problem myself rather than asking ISP itself (just for fun and learning).

1)      ISP provides a cable modem and recognizes the packets by MAC address of cable modem.
2)      I have a wireless router attached to my laptop.
3)      The WAN cable of modem is connected to wireless router.
4)      All my connections go via wireless router to cable modem to ISP Internet gateway.
5)      Wireless router has DHCP server to provide IP address to locally connected hosts.
6)      Of-course it acts like a NAT and router, in the sense you can have multiple hosts connecting to wireless router (not sure about limit).

The network setup is something like this:

Let me come back to the problem of login server not responding. I struggled a lot. I thought the problem with wireless router and ISP may not be accepting the MAC other than my PC. No I was wrong, in windows everything was clean. Next, I pulled out wireless router and directly cabled modem to my PC. Again did not work out. I used to login from windows and work with Ubuntu for my net access. This was successful since login information was maintained in ISP side than locally in the system.

After so much of struggle, I decided to take packet captures of both windows and Ubuntu. I observed Ethernet packets and IP packets and they looked similar. Finally I gazed through TCP packets.

Aha here you are! Linux adds all TCP options to TCP packet while windows adds few. I knew this was problem but which options? I started disabling one by one

sudo sysctl net.ipv4.tcp_sack=0

This did not work

sudo sysctl net.ipv4.tcp_window_scaling=0

Na this did not work. I thought my assumption was wrong. I decided to give final try

sudo sysctl net.ipv4.tcp_timestamps=0

Hurrah!!! It worked out. I was really delighted. I updated the /etc/sysctl.conf and never faced the problem again.

So it is basically problem with tcp timestamp option which linux kernel includes by default. I really did not understand why this would give a problem. TCP timestamp just sends current clock of the system to avoid packets being recognized as duplicate during sequence number overflow. However they need not even synchronize with system clock. I raised this issue with ISP and let me see what happens further.

At the end of day, one more piece of learning and one more topic to write about ;). I just loved the investigations I carried out. It may be very silly problem to experts however for me it was seamless learning.

Here is the TCP packet capture of windows and linux. It shows Windows does not add TCP timestamp field in TCP packet during SYN state.

Courtesy: wireshark

Tuesday, January 24, 2012

A simple player simulation using pthreads

When I was playing VLC, just wondering how pause, play and resume works. I decided to simulate one using pthreads. This is just done for fun nothing official. So I used the wait and resume calls of pthreads.

1) pthread_cond_wait()
2) pthread_cond_signal()

Using above two POSIX calls I did a simulation of player pause and resume. It worked as expected. So it was done as below

1) Player is executed as a separate thread
2) Button simulation done using command line in main thread.
3) When pause is opted, pthread_cond_wait() is called to make thread wait.
4) When resume is opted, pthread_cond_signal() is called to wake up the waiting thread.

As far as wake up is concerned, there is one more variant pthread_cond_broadcast() which wakes up all threads waiting on condition variable. Please refer man pages to know detailed usage of pthread calls. Not sure about internals may be the futex is released atomically and wakeup** is called for this task (again not sure)

Here is C code, not completely tested however giving what is required. Suggestions and further improvements are always welcome.


typedef enum {

struct player {
    pthread_mutex_t player_mutex;
    pthread_cond_t player_cond;
    pthread_t player_tid;
    player_state player_s;

static void init_player(struct player *__arg);
static void print_player_menu(void);
static int start_player(struct player *__arg);
static int pause_player(struct player *__arg);
static int resume_player(struct player * __arg, const player_state player_s);
static int stop_player(struct player *__arg);
static char read_char(void);
void* start_player_thread(void *__arg);

int main()
    struct player bazooka_player;

    while (1) {
        char c = read_char();

        switch (c) {
            case 'S':
            case 's':
                if (bazooka_player.player_s == STOP) {
                } else {
                    printf("Invalid State to call player start\n");

            case 'P':
            case 'p':
                if (bazooka_player.player_s == PLAY) {
                } else {
                    printf("Invalid State to call player pause\n");

            case 'R':
            case 'r':
                if (bazooka_player.player_s == PAUSE) {
                    resume_player(&bazooka_player, PLAY);
                } else {
                    printf("Invalid State to call player resume\n");

            case 'Q':
            case 'q':
                exit(EXIT_SUCCESS); //Any better way?

                printf("Invalid player option\n");

static void init_player(struct player *__arg)
     * Make sure you do proper initialization
     * Otherwise pthread_cond_signal() would result in deadlock
    __arg->player_s = STOP;
    __arg->player_tid = -1;
    pthread_cond_init(&__arg->player_cond, NULL);
    pthread_mutex_init(&__arg->player_mutex, NULL);

static char read_char(void)
     * This is taken from net.
     * It basically turns of echo for the current terminal.
     * Takes single character without expecting new line.
     * Restores old tty settings.
     * returns character.
    char c;
    struct termios old_term, new_term;
    tcgetattr(STDIN_FILENO, &old_term);
    new_term = old_term;
    new_term.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &new_term);
    c = getchar();
    tcsetattr(STDIN_FILENO, TCSANOW, &old_term);
    return c;

static void print_player_menu(void)
    printf("PLAYER OPTIONS\n");
    printf("S or s: Start play\n");
    printf("P or p: Pause play\n");
    printf("R or r: Resume play\n");
    printf("Q or q: Stop play\n");

static int start_player(struct player *__arg)
     * This could get racy if we set state to PLAY after pthread_create().
     * Do we need explicit memory barrier in multi-core platform?
     * smp_wmb()? Not sure :-)
    __arg->player_s = PLAY;
    return pthread_create(&__arg->player_tid, NULL, start_player_thread, __arg);

static int pause_player(struct player *__arg)
    __arg->player_s = PAUSE;

static int resume_player(struct player *__arg, const player_state player_s)
    printf("Preparing to resume\n");
    __arg->player_s = player_s;
     * Again Do we need smp_wmb()?
    return pthread_cond_signal(&__arg->player_cond);

static int stop_player(struct player *__arg)
     * This may hang the thread if the player is paused.
     * We will have to explicitly do signal in case, player is paused

    if (__arg->player_s == PAUSE) {
        resume_player(__arg, STOP);
    } else {
        __arg->player_s = STOP;

    return ((__arg->player_tid == -1)?0:pthread_join(__arg->player_tid, NULL));

void* start_player_thread(void* __arg)
    struct player* pl_s = (struct player*) __arg;
    while (1) {
        switch (pl_s->player_s) {
            case PLAY:
                printf("playing the song :-)\n");
                sleep(1); //Not sure how much safe to call this inside a thread

            case PAUSE:
                printf("preparing to pause :-)\n");
                pthread_cond_wait(&pl_s->player_cond, &pl_s->player_mutex);

            case STOP:
                printf("good bye! Hope you enjoyed it :-)\n");

Compile with: gcc -o <binary_name> <saved_c_file> -lpthread
Execute with: ./<binary_name> and test output

Monday, January 16, 2012

The VirtualBox 4.1.8

The new version of virtual box seems to have improvements on the native graphic resolution. My guest OS screen now fits into entire screen of host OS rather than it was limited to a specific area in earlier versions. This was happening even if  guest add-ons were installed. The problem seems to have gone by now. Now I am getting real feeling of linux box on vista without installation of guest additions. I recently installed OpenSUSE 12.1 32 bit edition with XFCE desktop as guest on top of Vista and looks magnificent. Here are some screen shots


That really looks nice. Of-course this was only for experiment. Personally I use linux installed onto the hardware system :-).