免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2177 | 回复: 2
打印 上一主题 下一主题

[网络] Packet Raw socket bind() and close() [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-11-08 16:47 |只看该作者 |倒序浏览

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?

论坛徽章:
1
天蝎座
日期:2013-12-06 18:23:58
2 [报告]
发表于 2013-11-13 22:33 |只看该作者
你是如何测得这个数据呢?

论坛徽章:
0
3 [报告]
发表于 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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP