我是靠谱客的博主 复杂海燕,最近开发中收集的这篇文章主要介绍RGMII以太网测试方案一 方案框图 二 实现步骤三 测试代码,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一 方案框图

 二 实现步骤

1.模组的MAC通过RGMII接口与PHY连接

2.MAC层数据经过RGMII到达PHY的PCS层

3.设置PHY的PCS层回环,使PCS层数据回到MAC层

4.判断MAC层TX与RX接收的数据。如果MAC能接收到数据,测试PASS,否则失败

三 测试代码

int main(int argc, char **argv)
{
    int i, datalen, frame_length, sd, bytes;
    char *interface = "eth0";
    uint8_t data[IP_MAXPACKET];
    uint8_t src_mac[6];
    uint8_t dst_mac[6];
    uint8_t ether_frame[IP_MAXPACKET];
    struct sockaddr_ll device;
    struct ifreq ifr;
	int recvlen=0;
	uint8_t buffer[IP_MAXPACKET];
    int loopback_state=0;

    // Submit request for a socket descriptor to look up interface.
    if ((sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
    { 
        perror("socket() failed to get socket descriptor for using ioctl()");
        exit(EXIT_FAILURE);
    }

    // Use ioctl() to look up interface name and get its MAC address.
    memset(&ifr, 0, sizeof(ifr));
    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", interface);
    if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
    {
        perror("ioctl() failed to get source MAC address ");
        return (EXIT_FAILURE);
    }
    close(sd);

    // Copy source MAC address.
    memcpy(src_mac, ifr.ifr_hwaddr.sa_data, 6);

    // Report source MAC address to stdout.
    printf("MAC address for interface %s is ", interface);
    for (i = 0; i < 5; i++)
    {
        printf("%02x:", src_mac[i]);
    }
    printf("%02xn", src_mac[5]);

    // Find interface index from interface name and store index in
    // struct sockaddr_ll device, which will be used as an argument of sendto().
    memset(&device, 0, sizeof(device));
    if ((device.sll_ifindex = if_nametoindex(interface)) == 0)
    {
        perror("if_nametoindex() failed to obtain interface index ");
        exit(EXIT_FAILURE);
    }
    printf("Index for interface %s is %in", interface, device.sll_ifindex);

    // Set destination MAC address: you need to fill these out
    dst_mac[0] = 0x10; //设置目的网卡地址
    dst_mac[1] = 0x78;
    dst_mac[2] = 0xd2;
    dst_mac[3] = 0xc6;
    dst_mac[4] = 0x2f;
    dst_mac[5] = 0x89;

    // Fill out sockaddr_ll.
    device.sll_family = AF_PACKET;
    memcpy(device.sll_addr, src_mac, 6);
    device.sll_halen = htons(6);

    // 发送的data,长度可以任意,但是抓包时看到最小数据长度为46,这是以太网协议规定以太网帧数据域部分最小为46字节,不足的自动补零处理
    datalen = 12;
    data[0] = 0x11;
    data[1] = 0x12;
    data[2] = 0x13;
    data[3] = 0x14;
    data[4] = 0x15;
    data[5] = 0x16;
    data[6] = 0x17;
    data[7] = 0x18;
    data[8] = 0x19;
    data[9] = 0x20;
    data[10] = 0x21;
    data[11] = 0x22;

    // Fill out ethernet frame header.
    frame_length = 6 + 6 + 2 + datalen;
    // Destination and Source MAC addresses
    memcpy(ether_frame, dst_mac, 6);
    memcpy(ether_frame + 6, src_mac, 6);

    ether_frame[12] = ETH_P_DEAN / 256;
    ether_frame[13] = ETH_P_DEAN % 256;

    // data
    memcpy(ether_frame + 14, data, datalen);
    // Submit request for a raw socket descriptor.
    if ((sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
    { //创建正真发送的socket
        perror("socket() failed ");
        exit(EXIT_FAILURE);
    }
    // Send ethernet frame to socket.
    if ((bytes = sendto(sd, ether_frame, frame_length, 0, (struct sockaddr *)&device, sizeof(device))) <= 0)
    {
        perror("sendto() failed");
        exit(EXIT_FAILURE);
    }
   	for(i=0;i<bytes;i++){
			printf(" %02x",ether_frame[i]);
			if((i+1)%16==0){
					printf("n");
			}
	}
	printf("n");
	

	recvlen=recvfrom(sd,buffer,sizeof(buffer),0,NULL,NULL);

	for(i=0;i<recvlen;i++){
		printf(" %02x",buffer[i]);
		if((i+1)%16==0){
				printf("n");
		}
	}
	printf("n");

    for(i=0;i<recvlen;i++){

        if(bytes>recvlen)
        {
            loopback_state=0;
            break;
        }
        else if(ether_frame[i]!=buffer[i])
        {
            loopback_state=0;
            break;
        }
    }
	if (loopback_state == 1)
    {
         printf("phy loopback OKn");
    }
    else
    {
        printf("phy loopback errorn");
    }
    // Close socket descriptor.
    close(sd);

    return (EXIT_SUCCESS);
}

最后

以上就是复杂海燕为你收集整理的RGMII以太网测试方案一 方案框图 二 实现步骤三 测试代码的全部内容,希望文章能够帮你解决RGMII以太网测试方案一 方案框图 二 实现步骤三 测试代码所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部