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


    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include    /** type  */
    #include 
    #include 
    #include 
    
    /**
     *  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");
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
  • 相关阅读:
    手写raft(一) 实现leader选举
    QT+OSG/osgEarth编译之二十五:libzip+Qt编译(一套代码、一套框架,跨平台编译,版本:libzip-1.9.2)
    SQL刷题查漏补缺6
    React TypeScript 样式报错
    30天Python入门(第十六天:深入了解Python中的日期与时间)
    Deepwalk详解
    【实习】DLL相关
    锥形相位掩模的Talbot图像
    配置Hive使用Spark执行引擎
    MATLAB 保存和加载变量
  • 原文地址:https://blog.csdn.net/oHeHui1/article/details/128152272