GMAC
介绍GMAC的功能和使用方法。
模块介绍
GMAC(Gigabit Media Access Controller)模块是一个支持千兆以太网通信的控制器,负责数据帧的发送、接收及网络流量的管理。
功能介绍
- 应用层: 面向用户提供应用服务。
- 协议栈层: 实现网络协议,为应用层提供系统调用接口。
- 网络设备抽象层: 屏蔽驱动实现细节,为协议栈提供统一接口。
- 网络设备驱动层: 负责实现数据传输和设备管理。
- 物理层: 网络硬件设备。
源码结构介绍
GMAC 驱动代码位于 drivers\net\ethernet\spacemit
目录下:
drivers\net\ethernet\spacemit
|-- emac-ptp.c #提供 PTP 协议支持
|-- k1x-emac.c #K1 GMAC 驱动代码
|-- k1x-emac.h #K1 GMAC 驱动头文件
关键特性
特性
特性 | 特性说明 |
---|---|
支持10/100/1000M以太网 | 兼容多速率以太网连接 |
支持DMA | 高效数据传输降低CPU负载 |
支持NAPI | 提升中断处理效率减少CPU开销 |
中断合并机制 | 合并中断提升高负载性能 |
支持RGMII/RMII | 适应多应用场景 |
支持PTP | 实现设备间亚微秒级时间同步 |
支持电源管理 | 支持挂起恢复适应低功耗需求 |
性能参数
单网卡单工 | 单网卡双工 | 双网卡单工 | 双网卡双工 | |
---|---|---|---|---|
TX速率 (Mbps) | 942 | 930 | 942 | 797 |
RX速率 (Mbps) | 941 | 940 | 941 | 881 |
注: 双工情形下测试带宽具有一定波动
性能测试
测试环境
测试设备: 一块 K1-DEB1 板卡;一台 PC(型号:HP ProBook 450 G10,操作系统:Ubuntu 22.04.4 LTS)
网络拓扑: K1-DEB1 eth0 口与 PC 以太网口直连;K1-DEB1 eth1 口经 2.5G USB 转以太网连接器 与 PC直连
IP设置: 直连网口 IP 设置在同一个网段,PC 端开启两个 iperf 服务端
# Set IP for the PC
ifconfig <ethernet-interface> 192.168.1.100 netmask 255.255.255.0
ifconfig <usb-ethernet-interface> 192.168.2.100 netmask 255.255.255.0
# Set IP for the k1-deb1 net device
ifconfig eth0 192.168.1.200 netmask 255.255.255.0
ifconfig eth1 192.168.2.200 netmask 255.255.255.0
# Start iperf3 server on the PC
iperf3 -s -B 192.168.1.100 -A 10 -D
iperf3 -s -B 192.168.2.100 -A 11 -D
性能优化
为最大化网络吞吐量,在测试前先对 K1-DEB1 板网口中断进行合理的 CPU 绑定和分配,以充分利用多核资源
第一步: 通过以下命令查看当前中断分配情况,确认两个网卡对应的中断号:
cat /proc/interrupts | grep eth*
85: 11041 2332003 0 0 0 0 0 0 SiFive PLIC 131 Edge eth0
86: 234 0 409744 0 0 0 0 0 SiFive PLIC 133 Edge eth1
第二步: 将网口硬件中断绑定到不同 CPU 核,例如 eth0 绑定至 CPU1、eth1 绑定至 CPU2
echo 02 > /proc/irq/85/smp_affinity
echo 04 > /proc/irq/86/smp_affinity
第三步: 启用 RPS(Receive Packet Steering)进行接收端的软中断负载均衡。例如下面命令允许 CPU4 处理 eth0 上接收数据包、CPU5 处理 eth1 接收数据包,充分利用多核优势。
echo 10 > /sys/devices/platform/soc/cac80000.ethernet/net/eth0/queues/rx-0/rps_cpus
echo 4096 > /sys/devices/platform/soc/cac80000.ethernet/net/eth0/queues/rx-0/rps_flow_cnt
echo 20 > /sys/devices/platform/soc/cac81000.ethernet/net/eth1/queues/rx-0/rps_cpus
echo 4096 > /sys/devices/platform/soc/cac81000.ethernet/net/eth1/queues/rx-0/rps_flow_cnt
单网卡测试
使用 eth0 口进行单网卡测试,并将当前 iperf3 进程绑定到 CPU6
单工/TX
iperf3 -c 192.168.1.100 -B 192.168.1.200 -t 100 -A 6
单工/RX
iperf3 -c 192.168.1.100 -B 192.168.1.200 -t 100 -A 6 -R
双工
iperf3 -c 192.168.1.100 -B 192.168.1.200 -t 100 -A 6 --bidir
双网卡测试
双网卡测试中将两个 iperf3 进程分别绑定到CPU6、CPU7
单工/TX
# Bind CPU6 for the first test, bind CPU7 for the second test
iperf3 -c 192.168.2.100 -B 192.168.2.200 -t 100 -A 6 > 1.txt &
iperf3 -c 192.168.1.100 -B 192.168.1.200 -t 100 -A 7 > 2.txt &
# View the test results
cat 1.txt
cat 2.txt
单工/RX
iperf3 -c 192.168.2.100 -B 192.168.2.200 -t 100 -A 6 -R > 1.txt &
iperf3 -c 192.168.1.100 -B 192.168.1.200 -t 100 -A 7 -R > 2.txt &
cat 1.txt
cat 2.txt
双工
iperf3 -c 192.168.2.100 -B 192.168.2.200 -t 100 -A 6 --bidir > 1.txt &
iperf3 -c 192.168.1.100 -B 192.168.1.200 -t 100 -A 7 --bidir > 2.txt &
cat 1.txt
cat 2.txt
配置介绍
主要包括 驱动使能配置 和 dts配置
CONFIG配置
NET_VENDOR_SPACEMIT
:如果使用的是 SpacemiT 类型的以太网芯片,请将此选项设置 Y
config NET_VENDOR_SPACEMIT
bool "Spacemit devices"
default y
depends on SOC_SPACEMIT
help
If you have a network (Ethernet) chipset belonging to this class,
say Y.
Note that the answer to this question does not directly affect
the kernel: saying N will just cause the configurator to skip all
the questions regarding Spacemit chipsets. If you say Y, you will
be asked for your specific chipset/driver in the following questions.
K1X_EMAC
:启用 SpacemiT 的 GMAC 驱动
config K1X_EMAC
bool "k1-x Emac Driver"
depends on SOC_SPACEMIT_K1X
select PHYLIB
help
This Driver support Spacemit k1-x Ethernet MAC
Say Y to enable support for the Spacemit Ethernet.
dts配置
GMAC dts 配置,需要确定以太网使用的 pin 组,phy 复位 GPIO,phy 型号及地址。TX phase 和 RX phase 一般采用默认值。
pinctrl
查看开发板原理图,找到 GMAC 使用的 pin 组。
假设 eth0 所使用的引脚为 GPIO00 至 GPIO14 以及 GPIO45,对应 pinctrl 配置可使用 k1-x_pinctrl.dtsi
中的 pinctrl_gmac0
。
方案 dts 中 eth0 使用 gmac0 pinctrl 如下
ð0 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gmac0>;
};
gpio
查看开发板原理图,找到以太网 phy 复位信号 gpio,假设 eth0 phy 复位 gpio 为 gpio 110。
方案 dts 中 eth0 使用 gpio 110 如下。
ð0 {
emac,reset-gpio = <&gpio 110 0>;
}
phy 配置
phy 标识
查看开发板原理图,确认以太网 phy 的型号和 phy id。
如以太网 phy RTL8821F-CG,其 phy id 为 001c.c916。
Phy id 信息可以查找 phy spec 或联系 phy 厂商提供。
phy 地址
查看开发板原理图,确认以太网 phy 的地址,假设为 1。
phy 配置
根据 5.3.1 和 5.3.2 得到的 phy 标识 id 和 phy 地址,对 phy 进行配置。
方案 dts eth0 配置如下
ð0 {
...
mdio-bus {
#address-cells = <0x1>;
#size-cells = <0x0>;
rgmii0: phy@0 {
compatible = "ethernet-phy-id001c.c916";
device_type = "ethernet-phy";
reg = <0x1>;
phy-mode = "rgmii";
};
};
};
TX phase 和 RX phase
tx-phase 默认值为 90
,rx-phase 为 73
。
不同的板子 tx-phase 和 rx-phase 可能需要调整。如果 eth0 端口可以 up,但是分配不到 ip 地址,需要联系进迭时空调整 tx-phase 和 rx-phase。
ð0 {
tx-phase = <90>;
rx-phase = <73>;
};
dts 配置
综合开发板以太网硬件信息,配置如下。
ð0 {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_gmac0>;
emac,reset-gpio = <&gpio 110 0>;
emac,reset-active-low;
emac,reset-delays-us = <0 10000 100000>;
/* store forward mode */
tx-threshold = <1518>;
rx-threshold = <12>;
tx-ring-num = <128>;
rx-ring-num = <128>;
dma-burst-len = <5>;
ref-clock-from-phy;
clk-tuning-enable;
clk-tuning-by-delayline;
tx-phase = <90>;
rx-phase = <73>;
phy-handle = <&rgmii0>;
status = "okay";
mdio-bus {
#address-cells = <0x1>;
#size-cells = <0x0>;
rgmii0: phy@0 {
compatible = "ethernet-phy-id001c.c916";
device_type = "ethernet-phy";
reg = <0x1>;
phy-mode = "rgmii";
};
};
};