Václav Struhár
Václav Struhár

Reputation: 1769

Time difference of closing AF_PACKET vs AF_INET?

What is the reason for such time difference between closing sockets created as AF_PACKET and AF_INET? How can I reduce the closing time for AF_PACKET?

sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
close(sockfd); // 60000 μs

sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
close(sockfd); // 30 μs

code to reproduce the behavior:

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#include <sys/socket.h>
#include <net/if.h>

int main() {
    struct timeval time_start, time_end;
    int sockfd;

    if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
        perror("socket error");
    } 

    gettimeofday(&time_start, NULL);
    close(sockfd);
    gettimeofday(&time_end, NULL);
    printf("close AF_PACKET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));


    if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
        perror("socket error");
    } 

    gettimeofday(&time_start, NULL);
    close(sockfd);
    gettimeofday(&time_end, NULL);
    printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

    return 0;
}

Upvotes: 1

Views: 580

Answers (1)

Shahul Hameed
Shahul Hameed

Reputation: 51

Logically not a significant difference for both of the close system calls

If I change your program like below,

`

int main() {
    struct timeval time_start, time_end;
    int sockfd;

    if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
        perror("socket error");
    }   

    gettimeofday(&time_start, NULL);
    close(sockfd);
    gettimeofday(&time_end, NULL);
    printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

    if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
        perror("socket error");
    }   

    gettimeofday(&time_start, NULL);
    close(sockfd);
    gettimeofday(&time_end, NULL);
    printf("close AF_PACKET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

    return 0;
}`

I see the result as

shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 3471 
close AF_PACKET: 4 
shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 8 
close AF_PACKET: 7 
shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 8 
close AF_PACKET: 6 
shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 8 
close AF_PACKET: 5 
shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 9 
close AF_PACKET: 5 

similarly, if I change as below,

int main() {
    struct timeval time_start, time_end;
    int sockfd;

    if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
        perror("socket error");
    }

    gettimeofday(&time_start, NULL);
    close(sockfd);
    gettimeofday(&time_end, NULL);
    printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

    if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
        perror("socket error");
    } 

    gettimeofday(&time_start, NULL);
    close(sockfd);
    gettimeofday(&time_end, NULL);
    printf("close AF_INET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));

    if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) {
        perror("socket error");
    }   

    gettimeofday(&time_start, NULL);
    close(sockfd);
    gettimeofday(&time_end, NULL);
    printf("close AF_PACKET: %ld \n", (time_end.tv_sec*1000000 + time_end.tv_usec) - (time_start.tv_sec*1000000 + time_start.tv_usec));



    return 0;
}

result as below,

shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 6 
close AF_INET: 3 
close AF_PACKET: 3 
shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 8 
close AF_INET: 4 
close AF_PACKET: 5 
shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 8 
close AF_INET: 4 
close AF_PACKET: 4 
shahul@shahul-VirtualBox:~/test$  ./a.out 
close AF_INET: 8 
close AF_INET: 4 
close AF_PACKET: 4 

conclusion: Basically, first close call taking more time, perhaps time is taken to map the close system call function for the first time.

Upvotes: 1

Related Questions