F-Stack / f-stack

F-Stack is an user space network development kit with high performance based on DPDK, FreeBSD TCP/IP stack and coroutine API.
http://www.f-stack.org
Other
3.77k stars 881 forks source link

ff_sendto error #587

Open yeliauk opened 3 years ago

yeliauk commented 3 years ago

I wrote a server and a client based on F-Stack.The server could not receive the information sent by the client.

But the return value of the ff_sendto function on the client side is normal. When I use Wireshark software to grab packets on the client, I find that I can't grab them.

How to solve this problem?

Any suggestion will be appreciated!!

server (UDP) :

include

include

include

include

include

include <sys/types.h>

include <sys/socket.h>

include <arpa/inet.h>

include

include

include "ff_config.h"

include "ff_api.h"

define MAX_EVENTS 512

/ kevent set / struct kevent kevSet; / events / struct kevent events[MAX_EVENTS]; / kq / int kq; int sockfd;

ifdef INET6

int sockfd6;

endif

char html[] = "hello,client!";

int len;

int loop(void *arg) { struct sockaddr_in client; bzero(&client, sizeof(client));

int client_len;

/* Wait for events to happen */
unsigned nevents = ff_kevent(kq, NULL, 0, events, MAX_EVENTS, NULL);

unsigned i;

for (i = 0; i < nevents; ++i) {
printf("happen\n");
    struct kevent event = events[i];
    int clientfd = (int)event.ident;
printf("event fd:%d\n",clientfd);

    /* Handle disconnect */
    if (event.flags & EV_EOF) {
        /* Simply close socket */
        ff_close(clientfd);

ifdef INET6

    } else if (clientfd == sockfd || clientfd == sockfd6) {

else

    } else if (clientfd == sockfd && event.filter == EVFILT_READ) {

endif

        printf("clientfd == sockfd\n");

    char buf[255];

        printf("start read\n"); 

    client_len = sizeof(struct sockaddr_in);

        if(0>(len = ff_recvfrom(clientfd, buf, sizeof(buf), 0, (struct linux_sockaddr*)&client,&client_len)))
    {
        perror("read error\n");
        ff_close(clientfd);
        exit(1);
    }

        printf("buf = %s\n",buf);

    if(0>(len=ff_sendto(clientfd, html, sizeof(html), 0, (struct linux_sockaddr *)&client,sizeof(client))))
    {
        perror("send error\n");
        ff_close(clientfd);
        exit(1);
    }

    } else {
        printf("unknown event: %8.8X\n", event.flags);
    }
}

}

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

assert((kq = ff_kqueue()) > 0);

sockfd = ff_socket(AF_INET, SOCK_DGRAM, 0);

printf("sockfd:%d\n",sockfd);

if (sockfd < 0) {
    printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
    exit(1);
}

struct sockaddr_in my_addr;
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(80);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);

int ret = ff_bind(sockfd, (struct linux_sockaddr *)&my_addr, sizeof(my_addr));
if (ret < 0) {
    printf("ff_bind failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
    exit(1);
}

EV_SET(&kevSet, sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
/* Update kqueue */
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);

printf("start loop...\n");

ff_run(loop, NULL);
return 0;

}

client (UDP) :

include

include

include

include

include

include <sys/types.h>

include <sys/socket.h>

include <arpa/inet.h>

include

include

include <sys/ioctl.h>

include "ff_config.h"

include "ff_api.h"

define MAX_EVENTS 512

/ kevent set / struct kevent kevSet; / events / struct kevent events[MAX_EVENTS]; / kq / int kq; int sockfd;

struct sockaddr_in server_sock;

char html[255];

int loop(void *arg) { unsigned nevents = ff_kevent(kq, NULL, 0, events, MAX_EVENTS, NULL); unsigned i;

int len;

printf("events number:%u\n",nevents);

for (i = 0; i < nevents; ++i) {
    struct kevent event = events[i];
    int clientfd = (int)event.ident;
    printf("event fd:%d\n", clientfd);
     /* Handle disconnect */
    if (event.flags & EV_EOF|| event.flags&EV_ERROR) {
        /* Simply close socket */
        printf("event eof or error:%d,%d,%d\n", event.flags, EV_EOF, EV_ERROR);
        ff_close(clientfd);
    } else if (event.filter == EVFILT_WRITE) {

        printf("what words do you want to tell to server:\n");
        fgets(html,255,stdin);

    if(0>(len=ff_sendto(clientfd, html, sizeof(html), 0, (struct linux_sockaddr *)&server_sock,sizeof(server_sock))))
    {
    perror("send error!\n");
    ff_close(clientfd);
    exit(1);
        }
    printf("len=%d\n",len);

    } else {
        printf("unknown event: %8.8X\n", event.flags);
    }
}

}

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

int  clientfd = ff_socket(AF_INET, SOCK_DGRAM, 0);

printf("client:%d\n", clientfd);
if (clientfd>0)
{
    int on=1;
    ff_ioctl(clientfd, FIONBIO, &on);

    bzero(&server_sock,sizeof(server_sock));
    server_sock.sin_family = AF_INET;
    server_sock.sin_port = htons(80);
    inet_pton(AF_INET,"10.16.73.139",&(server_sock.sin_addr));

}
EV_SET(&kevSet, clientfd, EVFILT_WRITE, EV_ADD, 0, MAX_EVENTS, NULL);

assert((kq = ff_kqueue()) > 0);

/* Update kqueue */
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);

ff_run(loop, NULL);
return 0;

}

vincentmli commented 3 years ago

@LiMeiqing

wireshark/tcpdump is unable to capture DPDK port, f-stack.conf support packet capture, something like below:

# Packet capture path, this will hurt performance
[pcap]
enable=1
savepath=/tmp
snaplen=98
#savelen=1000
yeliauk commented 3 years ago

@LiMeiqing

wireshark/tcpdump is unable to capture DPDK port, f-stack.conf support packet capture, something like below:

Packet capture path, this will hurt performance

[pcap] enable=1 savepath=/tmp snaplen=98

savelen=1000

Thank you!! I will have a try.

yeliauk commented 3 years ago

@vincentmli I tried it. But I still can't capture package. So I wonder if there is something wrong with my code, so that the message can't be sent out.

Tim-xiaofan commented 3 years ago

try 'dpdk pdump tool'