Exemplo n.º 1
0
/**
 * @param 
 *
 * @return  
 *
 */
int socket(int domain, int type, int protocol)
{
    int rc;

     ODP_FD_DEBUG("socket create start , domain %d, type %d \n", domain, type);    
   
    if ((inited == 0) ||  (AF_INET != domain) || (SOCK_STREAM != type && SOCK_DGRAM != type))
    {
        rc = real_socket(domain, type, protocol);
        ODP_FD_DEBUG("linux socket fd %d \n", rc);    

        return rc;
    }

    assert(inited);
    rc = netdpsock_socket(domain, type, protocol);
    
    if(rc > 0)
        rc += ODP_FD_BASE;
    
    ODP_FD_DEBUG("netdp socket fd %d \n", rc);    
    return rc;
}
Exemplo n.º 2
0
int main(void)
{
    int ret;
    int i = 0 ;
    int epfd;
    int data_num =0;
    struct sockaddr_in addr_in;  
    struct sockaddr_in remote_addr;  
    struct epoll_event event;
    char recv_buf[5000];
    int recv_len; 
    pthread_t id;  

    ret = netdpsock_init(NULL);
    if(ret != 0)
        printf("init sock ring failed \n");

    /* create epoll socket */
    epfd = netdpsock_epoll_create(0);
    if(epfd < 0)
    {
        printf("create epoll socket failed \n");
        return -1;
    }

    fd = netdpsock_socket(AF_INET, SOCK_STREAM, 0);	
    if(fd < 0)
    {
        printf("create socket failed \n");
        netdpsock_close(epfd);
        return -1;
    }

    memset(&remote_addr, 0, sizeof(remote_addr));      
    remote_addr.sin_family = AF_INET;  
    remote_addr.sin_port   = htons(8000);  
    remote_addr.sin_addr.s_addr = htonl(0x02020205); 
//    remote_addr.sin_addr.s_addr = htonl(0x03030303); 

    if(netdpsock_connect(fd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) < 0)     
    {     
        printf("onnect to server failed \n");
        netdpsock_close(fd);
        netdpsock_close(epfd);
        return -1;  
    } 
    
    event.data.fd = fd;  
    event.events = EPOLLIN | EPOLLET;  

    ret = netdpsock_epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
    if(ret != 0)
    {
        printf("epoll ctl failed \n");
        netdpsock_close(fd);
        netdpsock_close(epfd);
        return -1;
    }

    printf("start dpdk tcp client application \n");

    ret=pthread_create(&id, NULL, (void *) tcp_send_thread, NULL);  
    if(ret!=0)  
    {  
        printf ("Create pthread error!\n");  
        return 0;  
    }  

    int event_num = 0;
    
    while(1)
    {
        event_num = netdpsock_epoll_wait (epfd, events, 20, -1);
        if(event_num <= 0)
        {
            printf("epoll_wait failed \n");
            continue;
        }
            
        for(i = 0; i < event_num; i++)
        {
            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))  
            {  
                printf("dpdk socket(%d) error\n", events[i].data.fd);
                netdpsock_close (events[i].data.fd);  
                fd = -1;
                continue;  
            }   

            if (events[i].events & EPOLLIN)
            {
                while(1)
                {
                    recv_len = netdpsock_recvfrom(events[i].data.fd, recv_buf, 5000, 0, NULL, NULL);
                    if((recv_len < 0) && (errno == NETDP_EAGAIN))
                    {
                       // printf("no data in socket \n");

                        break;
                    }
                    else if(recv_len < 0)
                    {
                         // socket error
                         //netdpsock_close(fd);
                         break;

                    }

                    printf("Recv data,  len: %d \n", recv_len);

                    data_num++;
                }
            
            }
            else
            {
                printf("unknow event %x, fd:%d \n", events[i].events, events[i].data.fd);
            }
            
        }
    
    }



    netdpsock_close(fd);
    netdpsock_close(epfd);

    return 0;
}
Exemplo n.º 3
0
int main(int argc, char * argv[])     
{ 
    int ret;
    int server_sockfd;   
    int client_sockfd;     
    struct sockaddr_in my_addr;      
    struct sockaddr_in remote_addr;     
    int sin_size;   
    u_short  port;


    buf = malloc(1024*1024*100+1);
    memset(buf, 0, 1024*1024*100);

    ret = netdpsock_init(NULL);
    if(ret != 0)
        printf("init sock failed \n");
    

    printf("input a port number:\n");
    scanf("%u", &port);
    memset(&my_addr,0,sizeof(my_addr)); 
    my_addr.sin_family=AF_INET; 
    my_addr.sin_addr.s_addr=INADDR_ANY;   
    my_addr.sin_port=htons(port);    
  
    if((server_sockfd=netdpsock_socket(PF_INET,SOCK_STREAM, 0)) < 0)     
    {       
        printf("socket error \n");     
        return 1;     
    }     

    if (netdpsock_bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) < 0)     
    {     
        printf("bind error \n");     
        return 1;     
    }     

    if (netdpsock_listen(server_sockfd, 5) < 0)  
    {     
        printf("listen error \n");     
        return 1;     
    }   
    
    sin_size=sizeof(struct sockaddr_in);   

    int epoll_fd;  
    epoll_fd=netdpsock_epoll_create(MAX_EVENTS);  
    if(epoll_fd==-1)  
    {  
        printf("epoll_create failed \n"); 
        netdpsock_close(server_sockfd);
        return 1;     
    }  
    
    struct epoll_event ev;  
    struct epoll_event events[MAX_EVENTS];  
    ev.events=EPOLLIN;  
    ev.data.fd=server_sockfd;  

    if(netdpsock_epoll_ctl(epoll_fd,EPOLL_CTL_ADD,server_sockfd,&ev)==-1)  
    {  
        printf("epll_ctl:server_sockfd register failed");  
        netdpsock_close(server_sockfd);
        netdpsock_close(epoll_fd);
        return 1;     
    }  
    
    int nfds;

    printf("dpdk tcp server is running \n");
    
    while(1)  
    {  
		//printf("step.............................7\n");
        nfds=netdpsock_epoll_wait(epoll_fd, events, MAX_EVENTS, -1);  
        if(nfds==-1)  
        {  
            printf("start epoll_wait failed \n");  
            netdpsock_close(server_sockfd);
            netdpsock_close(epoll_fd);
            return 1;     
        }  
        else if(nfds == 0)
        {
            printf("epoll timeout \n");
            continue;
        }
        
		//printf("step.............................8\n");
        int i;  
        for(i = 0; i < nfds; i++)  
        {  
		//printf("step.............................9\n");
            if(events[i].data.fd==server_sockfd)  
            {  
                if((client_sockfd = netdpsock_accept(server_sockfd, (struct sockaddr *)&remote_addr,&sin_size)) < 0)  
                {     
                    printf("accept client_sockfd failed \n");     
                    netdpsock_close(server_sockfd);
                    netdpsock_close(epoll_fd);
                    return 1;     
                }  
                
                ev.events=EPOLLIN;  
                ev.data.fd=client_sockfd;  
                if(netdpsock_epoll_ctl(epoll_fd, EPOLL_CTL_ADD,client_sockfd,&ev)==-1)  
                {  
                    printf("epoll_ctl:client_sockfd register failed \n");  
                    netdpsock_close(server_sockfd);
                    netdpsock_close(epoll_fd);
                    return 1;     
                }  
                
               
                printf("accept client %s,  family: %d, port %d \n",inet_ntoa(remote_addr.sin_addr), remote_addr.sin_family, remote_addr.sin_port);  
                
                //netdpsock_send(client_sockfd, "I have received your message.", 20, 0);  

            }  
            else  
            {  
		//printf("step.............................1\n");
                ret = dpdk_handle_event(events[i]);
		//printf("step.............................2\n");
            }  
        }  
    }  
    return 0;     
}    
Exemplo n.º 4
0
int main(void)
{
    int ret[50];
    int i = 0 ;
    int epfd;
    int data_num =0;
    struct sockaddr_in addr_in;  
    struct sockaddr_in remote_addr[50];  
    struct epoll_event event;
    char recv_buf[5000];
    int recv_len; 
    pthread_t id[50];  
    int fd[50];
    int servers;
    int j,t;

    printf("Input a number of servers:");
    scanf("%d", &servers);
    ret[0] = netdpsock_init(NULL);
    if(ret[0] != 0)
        printf("init sock ring failed \n");

    /* create epoll socket */
    epfd = netdpsock_epoll_create(0);
    if(epfd < 0)
    {
        printf("create epoll socket failed \n");
        return -1;
    }

for(j=0; j<servers; j++)
{
printf("step....................................1 \n");
    fd[j] = netdpsock_socket(AF_INET, SOCK_STREAM, 0);	
    if(fd[j] < 0)
    {
        printf("create socket failed \n");
        netdpsock_close(epfd);
        return -1;
    }
printf("step....................................2 \n");

    memset(&remote_addr[j], 0, sizeof(remote_addr));      
    remote_addr[j].sin_family = AF_INET;  
    remote_addr[j].sin_port   = htons(7001+(u_short)j);  
    remote_addr[j].sin_addr.s_addr = inet_addr("192.168.2.193"); 
//    remote_addr.sin_addr.s_addr = htonl(0x03030303); 

printf("step....................................3 \n");
    if(netdpsock_connect(fd[j], (struct sockaddr *)&remote_addr[j], sizeof(struct sockaddr)) < 0)     
    {     
        printf("connect to server failed \n");
        netdpsock_close(fd[j]);
        netdpsock_close(epfd);
        return -1;  
    } 
    
printf("step....................................4 \n");
    event.data.fd = fd[j];  
    event.events = EPOLLIN | EPOLLET;  

    ret[j] = netdpsock_epoll_ctl(epfd, EPOLL_CTL_ADD, fd[j], &event);
    if(ret[j] != 0)
    {
        printf("epoll ctl failed \n");
        netdpsock_close(fd[j]);
        netdpsock_close(epfd);
        return -1;
    }
}
printf("step....................................5 \n");
    printf("start dpdk tcp client application \n");

for(t=0; t<servers; t++)
{
    ret[t]=pthread_create(&id[t], NULL, (void *) tcp_send_thread, &fd[t]);  
    if(ret[t]!=0)  
    {  
        printf ("Create pthread error!\n");  
        return 0;  
    }  
}
printf("step....................................6 \n");
    time_start = gettime();
    int event_num = 0;
    
    while(1)
    {
        event_num = netdpsock_epoll_wait (epfd, events, 20, -1);
        if(event_num <= 0)
        {
            printf("epoll_wait failed \n");
            continue;
        }
            
        for(i = 0; i < event_num; i++)
        {
            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))  
            {  
                printf("dpdk socket(%d) error\n", events[i].data.fd);
                netdpsock_close (events[i].data.fd);  
                //fd = -1;
                continue;  
            }   

            if (events[i].events & EPOLLIN)
            {
                while(1)
                {
                    recv_len = netdpsock_recvfrom(events[i].data.fd, recv_buf, 5000, 0, NULL, NULL);
                    if((recv_len < 0) && (errno == NETDP_EAGAIN))
                    {
                       // printf("no data in socket \n");

                        break;
                    }
                    else if(recv_len < 0)
                    {
                         // socket error
                         //netdpsock_close(fd);
                         break;

                    }

		    time_end = gettime();
                    printf("%s .elapsing time: %ld ms\n", recv_buf, time_end-time_start);

                }
            
            }
            else
            {
                printf("unknow event %x, fd:%d \n", events[i].events, events[i].data.fd);
            }
            
        }
    
    }



    //netdpsock_close(fd);
    netdpsock_close(epfd);

    return 0;
}
Exemplo n.º 5
0
int main(void)
{
    int ret;
    int i = 0 ;
    int fd, recvfd = 0;
    int epfd;
    int data_num =0;
    char send_data[2048];
    struct timeval start, end;
    struct sockaddr_in addr_in;  
    struct sockaddr_in remote_addr;  
    struct epoll_event event;
    char recv_buf[2038];
    int recv_len; 

    ret = netdpsock_init(NULL);
    if(ret != 0)
        printf("init sock ring failed \n");


    /* create epoll socket */
    epfd = netdpsock_epoll_create(0);
    if(epfd < 0)
    {
        printf("create epoll socket failed \n");
        return -1;
    }

    fd = netdpsock_socket(AF_INET, SOCK_DGRAM, 0);	
    if(fd < 0)
    {
        printf("create socket failed \n");
        netdpsock_close(epfd);
        return -1;
    }

    memset(&addr_in, 0, sizeof(addr_in));      
    addr_in.sin_family = AF_INET;  
    addr_in.sin_port   = htons(8888);  
    addr_in.sin_addr.s_addr = htonl(0x02020202); 

    ret =  netdpsock_bind(fd, (struct sockaddr *)&addr_in, sizeof(addr_in) );
    if(ret != 0)
    {
        printf("bind socket failed \n");
        netdpsock_close(fd);
        netdpsock_close(epfd);
        return -1;
    }

    memset(&remote_addr, 0, sizeof(remote_addr));      
    remote_addr.sin_family = AF_INET;  
    remote_addr.sin_port   = htons(9999);  
    remote_addr.sin_addr.s_addr = htonl(0x02020205); 


    event.data.fd = fd;  
    event.events = EPOLLIN | EPOLLET;  

    ret = netdpsock_epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
    if(ret != 0)
    {
        printf("epoll ctl failed \n");
        netdpsock_close(fd);
        netdpsock_close(epfd);
        return -1;
    }

    printf("start dpdk udp application \n");

    int event_num = 0;
    memset(send_data, 0, sizeof(send_data));
    
    while(1)
    {
        event_num = netdpsock_epoll_wait (epfd, events, 20, -1);
        if(event_num <= 0)
        {
            printf("epoll_wait failed \n");
            continue;
        }
            
        for(i = 0; i < event_num; i++)
        {
            if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))  
            {  
                printf("dpdk socket(%d) error\n", events[i].data.fd);
                netdpsock_close (events[i].data.fd);  
                continue;  
            }   
            else if (events[i].events & EPOLLIN)
            {
                while(1)
                {
                    recv_len = netdpsock_recvfrom(events[i].data.fd, recv_buf, 2048, 0, NULL, NULL);

                    if(recv_len > 0)  
                    {  
                        printf("Recv: %s \n", recv_buf);

                        data_num++;
                        sprintf(send_data, "Hello, linux_udp, num:%d !", data_num);

                        netdpsock_sendto(events[i].data.fd, send_data, strlen(send_data) + 1, 0, (struct sockaddr *)&remote_addr,  sizeof(remote_addr));
                    } 
                    else if(recv_len < 0)
                    {
                        if (errno == NETDP_EAGAIN)   
                        {
                            break;
                        }
                        else
                        {
                            printf("remote close the socket, errno %d \n", errno);
                            netdpsock_close(events[i].data.fd);
                            break;
                        }
                    }
                    else
                    {
                        printf("remote close the socket, len %d \n", recv_len);
                        netdpsock_close(events[i].data.fd);
                        break;
                    }

                }
            
            }
            else
            {
                printf("unknow event %x, fd:%d \n", events[i].events, events[i].data.fd);
            }

            
        }
    
    }



    netdpsock_close(fd);
    netdpsock_close(epfd);

    return 0;
}
Exemplo n.º 6
0
int main(void)
{
    int ret;
    int i = 0 ;
    int fd, recvfd = 0;
    char send_data[2048];
    struct timeval start, end;
    struct sockaddr_in addr_in;  
    struct sockaddr_in remote_addr;  

    int interval;

    ret = netdpsock_init("dpdk_udp");
    if(ret != 0)
        printf("init sock ring failed \n");

    
    fd = netdpsock_socket(AF_INET, SOCK_DGRAM, 0);	
    if(fd < 0)
    {
        printf("create socket failed \n");
        return -1;
    }

    memset(&addr_in, 0, sizeof(addr_in));      
    addr_in.sin_family = AF_INET;  
    addr_in.sin_port   = htons(8888);  
    addr_in.sin_addr.s_addr = htonl(0x02020202); 

    ret =  netdpsock_bind(fd, (struct sockaddr *)&addr_in, sizeof(addr_in) );
    if(ret != 0)
    {
        printf("bind socket failed \n");
        netdpsock_close(fd);
        return -1;
    }

    memset(&remote_addr, 0, sizeof(remote_addr));      
    remote_addr.sin_family = AF_INET;  
    remote_addr.sin_port   = htons(9999);  
    remote_addr.sin_addr.s_addr = htonl(0x02020205); 

    printf("start dpdk udp application \n");


    char recv_buf[2038];
    int recv_len; 
    while(i < 2000000)
    {

        sprintf(send_data, "Hello, linux_udp, num:%d !", i);

        netdpsock_sendto(fd, send_data, strlen(send_data), 0, (struct sockaddr *)&remote_addr,  sizeof(remote_addr));

        recv_len = netdpsock_recvfrom(&recvfd, recv_buf, 2048, 0, NULL, NULL);

        i++;

     //   if((i % 100000)  == 0 )
            printf("Recv: %s \n", recv_buf);
        
        
    }

    netdpsock_close(fd);
    return 0;
}