当前位置:网站首页>Ifconfig how to get the statistics of network card
Ifconfig how to get the statistics of network card
2022-04-23 00:39:00 【Choumin】
1)ifconfig It's through parsing /proc/net/dev File to obtain the statistical information of the network card , As shown below :
$ cat /proc/net/dev
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
enp3s0: 9666434 145598 0 0 0 0 0 20 28796033 143998 0 0 0 0 0 0
enp0s3f0: 89753829 1339304 0 0 0 0 0 0 220083622 1303148 0 0 0 0 0 0
lo: 8551535 24076 0 0 0 0 0 0 8551535 24076 0 0 0 0 0 0
virbr0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
virbr0-nic: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2)/proc/net/dev How the content is generated ?
/proc/net/dev It's made up of the kernel dev_seq_show() Function output , Its implementation is as follows :
static int dev_seq_show(struct seq_file *seq, void *v)
{
if (v == SEQ_START_TOKEN)
seq_puts(seq, "Inter-| Receive "
" | Transmit\n"
" face |bytes packets errs drop fifo frame "
"compressed multicast|bytes packets errs "
"drop fifo colls carrier compressed\n");
else
dev_seq_printf_stats(seq, v);
return 0;
}
among ,dev_seq_printf_stats() Function completes the final output , Its implementation is as follows :
static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
{
struct rtnl_link_stats64 temp;
const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp);
seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu "
"%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n",
dev->name, stats->rx_bytes, stats->rx_packets,
stats->rx_errors,
stats->rx_dropped + stats->rx_missed_errors,
stats->rx_fifo_errors,
stats->rx_length_errors + stats->rx_over_errors +
stats->rx_crc_errors + stats->rx_frame_errors,
stats->rx_compressed, stats->multicast,
stats->tx_bytes, stats->tx_packets,
stats->tx_errors, stats->tx_dropped,
stats->tx_fifo_errors, stats->collisions,
stats->tx_carrier_errors +
stats->tx_aborted_errors +
stats->tx_window_errors +
stats->tx_heartbeat_errors,
stats->tx_compressed);
}
among ,dev_get_stats() The function is used to obtain network card statistics , Its implementation is as follows :
struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
struct rtnl_link_stats64 *storage)
{
const struct net_device_ops *ops = dev->netdev_ops;
if (ops->ndo_get_stats64) {
memset(storage, 0, sizeof(*storage));
ops->ndo_get_stats64(dev, storage);
} else if (ops->ndo_get_stats) {
netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
} else {
netdev_stats_to_stats64(storage, &dev->stats);
}
storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
return storage;
}
You can see from above ,dev_get_stats() Function first determines whether the network card driver has realized dev->netdev_ops->ndo_get_stats64()、dev->netdev_ops->ndo_get_stats() These two interfaces , If it's done , Then the statistical information of the network card is obtained through these two interfaces , If not , Then use netdev_stats_to_stats64() Function to get the statistics of the network card , The implementation of this function is as follows :
void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
const struct net_device_stats *netdev_stats)
{
#if BITS_PER_LONG == 64
BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
/* zero out counters that only exist in rtnl_link_stats64 */
memset((char *)stats64 + sizeof(*netdev_stats), 0,
sizeof(*stats64) - sizeof(*netdev_stats));
#else
size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
const unsigned long *src = (const unsigned long *)netdev_stats;
u64 *dst = (u64 *)stats64;
BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
for (i = 0; i < n; i++)
dst[i] = src[i];
/* zero out counters that only exist in rtnl_link_stats64 */
memset((char *)stats64 + n * sizeof(u64), 0,
sizeof(*stats64) - n * sizeof(u64));
#endif
}
You can see ,netdev_stats_to_stats64() The function just makes a copy of the data , The data source comes from the corresponding network card struct net_device In structure stats member , Its type is struct net_device_stats. With Gmac For example , It is in the process of processing a single package , By the way, the members in the structure are updated . for example , In driven contracting function stmmac_xmit() Update the number of bytes sent in :
static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
{
...
dev->stats.tx_bytes += skb->len;
...
}
版权声明
本文为[Choumin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230035392577.html
边栏推荐
- ethtool查看网卡统计信息的流程
- [essay contest] the first essay contest of tidb community column. Come and gather all the surrounding areas at one time!
- 深度学习基础学习-RNN与LTSM
- Making 3D planning drawings with ArcGIS
- JVM performance tuning 1
- C语言 #和 ##
- 【图像分类】用最简单的代码复现SENet,初学者一定不要错过(pytorch)
- L2-021 praise crazy devil (25 points)
- 2.56 - try running show with different sample values_ Bytes code.
- [image classification] reproduce senet with the simplest code. Beginners must not miss (pytorch)
猜你喜欢

【图像分类】用最简短的代码复现SeNet,小白一定要收藏(keras,Tensorflow2.x)

多测师杭州拱墅校区肖sir_高级金牌讲师_简历实战

Static and dynamic control nixie tube

while (n-- > 0) 的用法

396. Rotation function / Sword finger offer II 013 Sum of two-dimensional submatrix

Research on swarm intelligence collaborative work and cognitive computing technology

The thymeleaf template < img th: SRC = "${map. User. Headerurl}" used in idea reports an error cannot resolve 'user‘

C# WPF UI框架MahApps切换主题

2.58 - write the program is little endian, which returns 1 when compiled and run on the small end method machine and 0 when compiled and run on the large end method machine. This program should be abl

Usage of while (n -- > 0)
随机推荐
[C#]给地球点颜色看看
Improvement of ref and struct in C 11
Mp2459 is a perfect replacement for 60v0 with power MOSFET fs2459 integrated inside 5A step-down IC
leetcode 134. 加油站
openresty安装与入门
Deep learning basic learning - residual
Deletes all specified elements in the vector
L2-021 praise crazy devil (25 points)
Symbolization of ArcGIS surface tin surface data
多测师肖sir_高级金牌讲师_面试题
L2-013 red alarm (25 points)
L2-002 链表去重 (25 分) 标程
Snap installation repo problem
At the age of 33, he has worked for ten years and was laid off. The so-called experience is not worth money at all
Progrès de la recherche sur la télédétection des paramètres phénologiques de la végétation
Making 3D planning drawings with ArcGIS
信息系统项目管理-立项管理
智能无线传输模组,CV5200助力无人机mesh组网,无线通信传输方案
How do programmers find jobs? What if it becomes more and more difficult for programmers to find a job?
C#/. Net uses questpdf operation to generate PDF faster and more efficient!