AM5728-UDP通信异常问题

1. 测试简介

使用开发板eth1网卡(与交换芯片88E6390X -Port9连接),经过交换芯片(Port1,对应前面板LAN1口)与PC机进行UDP通信测试。系统框图如下所示。
在这里插入图片描述
主控板运行nc命令或者运行自己编写的UDP测试程序。

 nc命令使用参考:
https://www.cnblogs.com/nmap/p/6148306.html
 UDP测试程序虚拟机路径:
在这里插入图片描述
在这里插入图片描述
 PC机使用网络调试助手
在这里插入图片描述
网络调试工具-NetAssist以及Linux下的nc网络调试工具配合使用,参考:
https://cloud.tencent.com/developer/article/1739493?from=information.detail.linux%20udp%E6%B5%8B%E8%AF%95%E7%A8%8B%E5%BA%8F

2. 测试发现的问题

对于第一章节系统连接图中,红线数据流(PC机发UDP包,主控接收,正常),绿色数据流(主控发UDP包,PC机接收,PC机无法收到主控发出的UDP包)。

实际测试截图:
PC机IP地址为:192.168.253.110
主控板eth1网卡地址:192.168.253.100

在这里插入图片描述

主控使用nc命令启动UDP监听,PC机使用网络调试助手发UDP包。从以下截图可以看到主控已经收到PC机发的信息。
在这里插入图片描述
主控使用以前编译好的udp.exe工具进行持续发包测试,同时也可以接收UDP包。(udp.exe工具没有绑定网卡,可以复现发不出UDP包的问题;udp_test工具绑定了网卡,可以发出UDP包;后面章节会详细介绍)
在这里插入图片描述
从主控发出的UDP广播包,PC机的网络调试助手并未收到。使用Wireshark抓一下UDP包,也没有收到。

wireshark使用:
选择PC机以太网5,对应的IP地址是192.168.253.110;
在这里插入图片描述
在这里插入图片描述
并未收到任何信息:
在这里插入图片描述
主控ping一下PC机,看wireshark抓包结果:
在这里插入图片描述

3. 解决办法

经过排查,最终查明问题原因。
在这里插入图片描述
问题原因:主控端路由表中默认网关为eth0,测试时发UDP广播包时socket未绑定网卡,系统会走默认网关出去,而默认网关为eth0而不是eth1,导致UDP包并没有从eth1网卡发出。

解决办法:
办法1
路由表中添加eth1默认网关,但这样会影响eth0网卡的使用,不推荐;

Linux添加默认网关的命令:
route add default gw 192.168.253.1
在这里插入图片描述
再使用udp.exe程序进行收发包测试,发现PC机已可以收到主控发出的包:

在这里插入图片描述
再用wireshark抓包看一下:

在这里插入图片描述
办法2
在应用层编程时,使用宏SO_BINDTODEVICE绑定指定的网卡设备,然后再发送UDP数据,这样UDP数据能通过绑定的网络接口的网关进行转发出去。亲测可行,推荐此方式,使用这种方式不用再添加eth1的默认网关,因为添加eth1的默认网关后会影响eth0的网络路由通信。

参考:https://www.cnblogs.com/hzijone/p/13220754.html

在这里插入图片描述
将上述代码加到了udp_test工具中。
测试情况:

在这里插入图片描述
UDP测试时注意收发端口配置,收发使用两个不同的端口。后续主控eth1网卡的UDP通信推荐使用上述方式。

具体参见udp_test源码udp.c;

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>  
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <ifaddrs.h>
#include <time.h>

#include <stdlib.h>
#include <ctype.h>

#include <sys/wait.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <net/if.h>


typedef struct UdpMsg
{
    // 192.168.1.200
    char ip[20];
    char msg[1024];
    int msg_len;
}UdpMsg;

#define MAXLINE             (100)
#define DEFAULT_NETWORK_INTERFACE_NAME  "eth0"

static int do_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
    int n;
    socklen_t len;
    static char mesg[MAXLINE];
    struct sockaddr_in addr_to;
    int c_ip = 0;

    len = clilen;
    /* waiting for receive data */
    n = recvfrom(sockfd, mesg, MAXLINE, MSG_DONTWAIT, pcliaddr, &len);
    if (n <= 0)
    {
        // printf("recvfrom udp error.\n", NULL);
        return 0;
    }
    // mesg[n-1]=0;
    // memcpy(&c_ip, mesg, sizeof(c_ip));
    memcpy(&addr_to, pcliaddr, sizeof(struct sockaddr_in));
    c_ip = inet_addr(inet_ntoa(addr_to.sin_addr));
    // inet_ntoa(addr_to.sin_addr);
    printf("recvfrom data:%s c_ip:%s\n", mesg, inet_ntoa(addr_to.sin_addr));
    return c_ip;

    /* sent data back to client */
    // memcpy(&addr_to, pcliaddr, sizeof(struct sockaddr_in));
    // addr_to.sin_port = htons(UDP_CLENT_PORT);
    // sendto(sockfd, mesg, n, 0, (struct sockaddr*)&addr_to, len);
}
int UdpMsgWR(const char *if_name, UdpMsg* msg, int s_port, int c_port)
{
    static int sockfd = -1;
    static struct sockaddr_in s_addr;
    static struct sockaddr_in c_addr; 

    struct ifreq ifr;

    int ret = 0;
    int udp_c_ip = 0;

    if (sockfd == -1)
    {
        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (sockfd == -1)
        {
            printf("create send sockfd error.\n");
            return -1;
        }
        printf("create send sockfd succcess. sockfd:%d\n", sockfd);
        int opt = 1;


        memset(&ifr, 0x00, sizeof(ifr));
        // strncpy(ifr.ifr_name, "eth1", IFNAMSIZ);
        strncpy(ifr.ifr_name, if_name, IFNAMSIZ);
        ret = setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&ifr, sizeof(ifr));
        if (ret == -1)
        {
            printf("setsockopt send error.\n");
            close(sockfd);
            sockfd = -1;
            return -1;
        }

#if 1
        ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(opt));
        if (ret == -1)
        {
            printf("setsockopt send error.\n");
            close(sockfd);
            sockfd = -1;
            return -1;
        }
        ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
        if (ret == -1)
        {
            printf("setsockopt send error.\n");
            close(sockfd);
            sockfd = -1;
            return -1;
        }
#endif

        bzero(&s_addr, sizeof(struct sockaddr_in));
        s_addr.sin_family = AF_INET;
        s_addr.sin_port = htons(s_port);
        s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        /* 绑定socket */
        ret = bind(sockfd, (struct sockaddr *)&s_addr, sizeof(s_addr));
        if (-1 == ret) {
            printf("bind error ret=%d\n", ret);
            close(sockfd);
            sockfd = -1;
            return -1;
        }
        printf("bind success.\n");
    }
    // printf("create send sockfd succcess.\n");
    udp_c_ip = do_echo(sockfd, (struct sockaddr *)&c_addr, sizeof(c_addr));

    bzero(&c_addr, sizeof(struct sockaddr_in));
    c_addr.sin_family = AF_INET;
    c_addr.sin_port = htons(c_port);
    c_addr.sin_addr.s_addr = inet_addr(msg->ip);
    ret = sendto(sockfd, msg->msg, msg->msg_len, 0, (struct sockaddr*)&c_addr, sizeof(c_addr));
    if (ret < 0)
    {
        printf("Send udp msg error: %d\n", ret);
        perror("udp_send_error:");
        return -1;
    }
    printf("Send msg to port:%d, msg:[%s],msg_len:[%d] sockfd:%d\n", c_port, msg->msg, msg->msg_len, sockfd);
    // ret = close(sockfd);
    // printf("close socket ret:%d.\n", ret);
    // sockfd = -1;
    return udp_c_ip;
}

#define GET_IP_TINEOUT              (10)
int getip_by_ifname(const char *if_name, int *ip)
{
    int fd, iface = 0;
    static uint32_t last_time = 0;
    uint32_t now_time = 0;
    struct ifreq buf[INET_ADDRSTRLEN];
    struct ifconf ifc;
    static int last_ip = 0;
    struct sockaddr_in srv_addr = {0};

    if (!if_name || !ip) {
        return -1;
    }
    now_time = time(NULL); 
    if (0 == last_time || now_time - last_time > GET_IP_TINEOUT || now_time < last_time){
        last_time = now_time;
    }
    else{
        *ip = last_ip;
        return 0;
    }
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {

        ifc.ifc_len = sizeof(buf);

        // caddr_t,linux内核源码里定义的:typedef void *caddr_t
        ifc.ifc_buf = (caddr_t)buf;

        if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) {
            iface = ifc.ifc_len / sizeof(struct ifreq);

            while (iface-- > 0) {
                if (!(ioctl(fd, SIOCGIFADDR, (char *)&buf[iface]))){
                    // ip=(inet_ntoa(((struct sockaddr_in*)(&buf[iface].ifr_addr))->sin_addr));
                    // printf("IP:%s\n", ip);
                    // printf("interface name : %s\n", buf[iface].ifr_name);
                    if (strcmp(buf[iface].ifr_name, if_name) == 0) {
                        printf("interface name : %s, ip [%8x]\n", buf[iface].ifr_name, ((struct sockaddr_in *)&buf[iface].ifr_addr)->sin_addr.s_addr);
                        *ip = ((struct sockaddr_in *)&buf[iface].ifr_addr)->sin_addr.s_addr;
                        last_ip = *ip;
                        srv_addr.sin_addr.s_addr = last_ip;
                        printf("get %s ip : %s\n", if_name, inet_ntoa(srv_addr.sin_addr));
                        close(fd);
                        return 0;
                    }
                }
            }
        }
        close(fd);
    }
    return -1;
}

int main(int argc, char* argv[])
{
    int ret = 0;
    int tem_ip = 0, udp_c_ip = 0;
    UdpMsg udp_send_data;
    char send_msg[] = "hello world!\n\r";
    struct sockaddr_in srv_addr = {0};
    int send_port, recv_port;

    if(argc<3){
        printf("usage:./udp.exe [ifname] [send_port] [recv_port]\n");
        printf("example:./udp.exe eth0 9801 9802\n");
        return 0;
    }
    
    ret = getip_by_ifname(argv[1], &tem_ip);
    if (ret) {
        printf("get network interface %s ip fail\n", argv[1]);
    }
    srv_addr.sin_addr.s_addr = tem_ip;
    printf("get %s ip : %s\n", argv[1], inet_ntoa(srv_addr.sin_addr));  
    tem_ip |= 0xff000000;
    send_port = atoi(argv[2]);
    recv_port = atoi(argv[3]);
    printf("udp send_port:%d recv_port:%d\n", send_port, recv_port);
    while(1)
    {        
        memcpy(udp_send_data.ip, &tem_ip, sizeof(tem_ip));
        strncpy(udp_send_data.msg, send_msg, strlen(send_msg));
        udp_send_data.msg_len = strlen(send_msg);
        udp_c_ip = UdpMsgWR(argv[1], &udp_send_data, recv_port, send_port);
        // LOG_INFO("udp_c_ip:%x", udp_c_ip);
        if ((-1 != udp_c_ip) && (0 != udp_c_ip)) {
            srv_addr.sin_addr.s_addr = tem_ip;
            printf("get %s ip : %s\n", argv[1], inet_ntoa(srv_addr.sin_addr));  
        }
        int time = 3000000;
        do {
            time = usleep(time);
        } while(time > 0);
    }
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

heat.huang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值