Chinaunix

标题: Packet Raw socket bind() and close() [打印本页]

作者: coolg92013    时间: 2013-11-08 16:47
标题: Packet Raw socket bind() and close()

It seems PF_PACKET RAW socket bind() and close() needs lots of time.

in 2.6.32 it is 7-11ms
in 2.6.18, it is about 4ms.

why so slow?

作者: crazyhadoop    时间: 2013-11-13 22:33
你是如何测得这个数据呢?

作者: coolg92013    时间: 2013-12-04 10:48
I run the following program:
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>

struct timeval time1,time2;
int timediff;

int open_socket()
{
        //Define the socket
        int arp_sock;
        struct sockaddr_ll      haddr;
        int                     retval;

        gettimeofday(&time1,NULL);
        arp_sock = socket( PF_PACKET, SOCK_RAW, htons(ETH_P_ARP) );
        if( arp_sock < 0)
        {
                printf(" failed to create arp_socket, arp_sock=%d with error(%d/%s)\n", arp_sock, errno, strerror(errno));
                return -1;
        }
        gettimeofday(&time2,NULL);
        timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
        printf(" the duration of open SOCK_RAW socket of PF_PACKET:%d us\n", timediff);

        memset(&haddr, 0, sizeof(haddr));
        haddr.sll_family   = PF_PACKET;
        haddr.sll_protocol = htons( ETH_P_ARP );
        haddr.sll_ifindex  = 2;
        gettimeofday(&time1,NULL);
        retval = bind( arp_sock,(struct sockaddr *)&haddr,sizeof( struct sockaddr_ll ) );
        if( retval < 0 )
        {
                 printf(" failed to bind, retval=%d  with error(%d/%s)\n",  retval, errno, strerror(errno));
                return -1;
        }
        gettimeofday(&time2,NULL);
        timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
        printf(" the duration of bind SOCK_RAW socket of PF_PACKET:%d us\n", timediff);
        return arp_sock;
}

int main(int argc, char *argv[])
{


        int retval = -1;
        int i;
        for( i=1; i<=10; i++)
        {
                printf("\ti=%d \n", i);
                gettimeofday(&time1,NULL);
                retval = open_socket();
                if ( retval < 0 )
                {
                        return -1;
                }

                gettimeofday(&time2,NULL);
                timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
                printf(" the duration of open and bind SOCK_RAW socket of PF_PACKET:%d us \n", timediff);
                if ( retval > 0)
                {
                        gettimeofday(&time1,NULL);
                        close(retval);
                        gettimeofday(&time2,NULL);
                        timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
                        printf(" the duration of close SOCK_RAW socket of PF_PACKET:%d us\n", timediff);
                }

               printf("\n");
        }

        return 0;
}





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2