linux内核获取本机网卡的统计信息

#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kernel_stat.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#include <linux/in.h>
#include <net/neighbour.h>
#include <net/arp.h>
#include <linux/if_arp.h>   /** type  */
#include <net/ip.h>
#include <net/netevent.h>
#include <linux/inetdevice.h>

/**
 *  sudo  arp-scan -I eth0  --localnet
 */

#define HBUFFERLEN 30
struct net_device *mynet_device;

int myprobe(struct platform_device *pdev) {
	int k, j;
	char hbuffer[HBUFFERLEN];
	struct in_ifaddr *ifa;
	struct net_device_stats *stats;
	const struct net_device_ops *ops;
	mynet_device = dev_get_by_name(&init_net, "eth0");
	ops = mynet_device->netdev_ops;
	if (!ops) {
		pr_err("netdev_ops is null \n");
		return -1;
	}

	pr_info("mynet_device name: %s\n", mynet_device->name);
	pr_info("mynet_device type: %d\n", mynet_device->type);

	for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < mynet_device->addr_len; j++) {
		hbuffer[k++] = hex_asc_hi(mynet_device->perm_addr[j]);
		hbuffer[k++] = hex_asc_lo(mynet_device->perm_addr[j]);
		hbuffer[k++] = ':';
	}
	if (k != 0)
		--k;
	hbuffer[k] = 0;
	pr_info("mac: %s\n", hbuffer);
	ifa = mynet_device->ip_ptr->ifa_list;
	printk(KERN_INFO "ipaddr=%pI4\n", &(ifa->ifa_local));
	printk(KERN_INFO "mask=%pI4\n", &(ifa->ifa_mask));
	stats = &mynet_device->stats;
	if (!stats) {
		return -1;
	}

	dev_info(&mynet_device->dev, "tx_packets: %ld", stats->tx_packets);
	dev_info(&mynet_device->dev, "rx_packets: %ld", stats->rx_packets);

	dev_info(&mynet_device->dev, "tx_bytes: %ld", stats->tx_bytes);
	dev_info(&mynet_device->dev, "rx_bytes: %ld", stats->rx_bytes);

	dev_info(&mynet_device->dev, "tx_errors: %ld", stats->tx_errors);
	dev_info(&mynet_device->dev, "rx_errors: %ld", stats->rx_errors);

	dev_info(&mynet_device->dev, "rx_dropped: %ld", stats->rx_dropped);
	dev_info(&mynet_device->dev, "tx_dropped: %ld", stats->tx_dropped);

	return 0;
}

int myremove(struct platform_device *pdev) {
	pr_info("myplatformdriver myremove \n");
	return 0;
}

struct of_device_id my_of_match_table =
		{ .compatible = "my_platform_device_003", };

struct platform_driver my_platform_driver = { .driver = { .of_match_table =
		&my_of_match_table, .name = "my-platform-driver", .owner =
THIS_MODULE, }, .probe = myprobe, .remove = myremove, };

module_platform_driver(my_platform_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值