• 周五. 4月 26th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Linux嵌入式设备怎么确定网络端口的速率

admin

11月 28, 2021

Linux嵌入式设备怎么确定网络端口的速率

突发奇想,就是Linux下面我能不能查询到端口的速率,以此来判断要不要频繁的发送网络数据包呢?
或者更换包利用率更高的协议呢。
于是抱着这样的想法,我开始学习。
首先Linux下查询网络的命令是ifconfig

ifconfig

那么我能不能用ifconfig查询网络速率呢?

$ ifconfig
eth0      Link encap:Ethernet  HWaddr 42:4C:4E:00:01:02
        inet addr:192.168.0.66  Bcast:192.168.0.255  Mask:255.255.255.0
        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
        RX packets:37204 errors:0 dropped:123 overruns:0 frame:0
        TX packets:1014 errors:0 dropped:0 overruns:0 carrier:0
        collisions:0 txqueuelen:1000
        RX bytes:2382367 (2.2 MiB)  TX bytes:128498 (125.4 KiB)
        Interrupt:62

lo        Link encap:Local Loopback
        inet addr:127.0.0.1  Mask:255.0.0.0
        UP LOOPBACK RUNNING  MTU:65536  Metric:1
        RX packets:147 errors:0 dropped:0 overruns:0 frame:0
        TX packets:147 errors:0 dropped:0 overruns:0 carrier:0
        collisions:0 txqueuelen:1000
        RX bytes:14144 (13.8 KiB)  TX bytes:14144 (13.8 KiB)

可以看出ifconfig只能看到IP地址是多少,并不能看到端口的速率。
然后我看了一下网上的方法,说ethtool可以查看端口速率。
那么ethtool可不可以?

ethtool

ethtool的查看方法是ethtool 网卡名,例如: ethtool eth0
所以说前面的ifconfig就是这一步的基础,可以看到有什么网卡。

$ ethtool eth0
Settings for eth0:
    Supported ports: [ TP  ]
    Supported link modes:   10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Supported FEC modes: Not reported
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Advertised FEC modes: Not reported
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: off (auto)
Cannot get wake-on-lan settings: Operation not permitted
    Current message level: 0x00000007 (7)
                           drv probe link
    Link detected: yes

发现是可以的
Speed: 1000Mb/sDuplex: Full表示这个端口是全双工的1Gbps的速率。
也就是说发送和接收的最高速率都是1G,注意这个是小b,也就是说1000Mb/8 = 125MB。
125MB每秒,这个是理论的最高速率,实际达不到这么高。但也不会说特别慢。
那么问题来了,实际上我在嵌入式设备上没有ethtool指令,这时候我该怎么办呢。
我可以交叉编译一下ethtool,确实是个好办法。
其实只要获取端口速率的话,还有个办法。

dmesg

dmesg是非常通用的调试指令,基本上很多内核消息都会打印在里面。Linux很多设备的初始化信息都会记录在里面

$ dmesg | grep eth0
[   10.309153 ] gmac-dwmac ffc40000.ethernet eth0: No Safety Features support found
[   10.309205 ] gmac-dwmac ffc40000.ethernet eth0: IEEE 1588-2008 Advanced Timestamp supported
[   10.309662 ] gmac-dwmac ffc40000.ethernet eth0: registered PTP clock
[   11.332929 ] gmac-dwmac ffc40000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx

可以看到Link is Up - 1Gbps/Full,所以是全双工的1Gbps。
那我们只要加上过滤eth0: Link is Up这个信息就好了。
问题点
不过我们的嵌入式设备不一定所有设备都有eth0,这个时候需要借助ifconfig来判断。
另外这个只在网口上线的时候才会有打印,实际上也不一定所有网口都上线了。
除开这两点,dmesg仍然不失为一个能快速查询网络端口速率的好办法,我们只要配合ifconfig,然后再加上定时查询或必要的时候查询就可以了。

总结和思考

那么有没有更高效的办法呢,我通过查看phy驱动代码phy.c看到Link is Up是phy_print_status()这个函数里面打印的
所以其实只要在驱动打印这个的时候同时上抛一份到应用层就好,但是我不是做驱动这块的所以暂时还不知道怎么处理。
有知道怎么做的小伙伴欢迎在评论区留言。

那么嵌入式查看网络端口速率的方式是ifconfig查看虚拟网卡名称,再通过ethtool或者dmesg查看具体虚拟网卡的速率。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注