Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2584456
  • 博文数量: 351
  • 博客积分: 76
  • 博客等级: 上将
  • 技术积分: 3555
  • 用 户 组: 普通用户
  • 注册时间: 2004-11-13 21:27
文章分类

全部博文(351)

文章存档

2013年(1)

2012年(4)

2011年(7)

2010年(16)

2009年(34)

2008年(34)

2007年(34)

2006年(68)

2005年(82)

2004年(71)

分类: LINUX

2006-06-30 16:07:05

这几天学习《The Linux Networking Architecture: Design and Implementation of Network Protocols in the Linux Kernel》。学习了Netword Devices一节,根据书中的介绍,仿照ifconfig的输出,写了一个从内核输出网卡信息的模块。
本文包括输出结果和源代码。

源代码如下:

/*
 * kifcfg.c - list dev info
 *         use the list of dev_base
 *        
 * Author:    Zhou Weicheng
 * Date:    2006/6/20
 */

#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif

#include
#include
#include
#include
#include
#include
#include

static void get_dev_info()
{
    int n = 1;
    struct net_device *dev = dev_base;
    struct net_device_stats *stats;
    char type[32];
    struct in_device *ind = NULL;
    struct in_ifaddr *ina = NULL;

    while (dev) {
        if (dev->type == ARPHRD_ETHER)
            sprintf(type, "Ethernet");
        else if (dev->type == ARPHRD_LOOPBACK)
            sprintf(type, "Local Loopback");
        else
            sprintf(type, "%d", dev->type);

        stats = dev->get_stats(dev);

        printk("%s\t", dev->name);
        printk("Link encap:%s  ", type);
        printk("HWaddr %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n",
                dev->dev_addr[0], dev->dev_addr[1],
                dev->dev_addr[2], dev->dev_addr[3],
                dev->dev_addr[4], dev->dev_addr[5]
              );
        /*ind = (struct in_device *)dev->ip_ptr;*/
        ind = in_dev_get(dev);
        if (ind) {
            ina = (struct in_ifaddr *)ind->ifa_list;
            if (ina)
                printk("\tinet addr:%u.%u.%u.%u Bcast:%u.%u.%u.%u Mask:%u.%u.%u.%u\n",
                        NIPQUAD(ina->ifa_address),
                        NIPQUAD(ina->ifa_broadcast),
                        NIPQUAD(ina->ifa_mask));
        }

        printk("\t");
        if (dev->flags & IFF_UP) printk("UP ");
        if (dev->flags & IFF_BROADCAST) printk("BROADCAST ");
        if (dev->flags & IFF_RUNNING) printk("RUNNING ");
        if (dev->flags & IFF_LOOPBACK) printk("LOOPBACK ");
        if (dev->flags & IFF_MULTICAST) printk("MULTICAST ");
        printk("MTU:%d\n", dev->mtu);

        printk("\t");
        printk("RX packets:%lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
                stats->rx_packets, stats->rx_errors, stats->rx_dropped,
                stats->rx_over_errors, stats->rx_frame_errors);
        printk("\t");
        printk("TX packets:%lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
                stats->tx_packets, stats->tx_errors, stats->tx_dropped,
                stats->tx_aborted_errors, stats->tx_carrier_errors);

        printk("\t");
        printk("RX Bytes:%lu  TX Bytes:%lu\n",
                stats->rx_bytes, stats->tx_bytes);

        printk("\t");
        printk("Interrupt:%d Base address:0x%lx\n",
                dev->irq, dev->base_addr);

        printk("\n");
        n++;
        dev = dev->next;
    }
}

static int __init init(void)
{
    get_dev_info();
    return 0;
}

static void __exit fini(void)
{
    return;
}

module_init(init);
module_exit(fini);

输出结果如下:
eth0    Link encap:Ethernet  HWaddr 00:0C:29:1A:0E:2C
        inet addr:10.21.4.87 Bcast:10.21.255.255 Mask:255.255.0.0
        UP BROADCAST MULTICAST MTU:1500
        RX packets:14525 errors:0 dropped:0 overruns:0 frame:0
        TX packets:6720 errors:0 dropped:0 overruns:0 carrier:0
        RX Bytes:1358898  TX Bytes:1078741
        Interrupt:16 Base address:0x1080

eth1    Link encap:Ethernet  HWaddr 00:0C:29:1A:0E:36
        inet addr:1.1.1.1 Bcast:1.1.1.255 Mask:255.255.255.0
        UP BROADCAST MULTICAST MTU:1500
        RX packets:5897 errors:0 dropped:0 overruns:0 frame:0
        TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
        RX Bytes:597582  TX Bytes:468
        Interrupt:17 Base address:0x1400

lo      Link encap:Local Loopback  HWaddr 00:00:00:00:00:00
        inet addr:127.0.0.1 Bcast:0.0.0.0 Mask:255.0.0.0
        UP LOOPBACK MTU:16436
        RX packets:0 errors:0 dropped:0 overruns:0 frame:0
        TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
        RX Bytes:0  TX Bytes:0
        Interrupt:0 Base address:0x0

sit0    Link encap:776  HWaddr 00:00:00:00:00:00
        MTU:1480
        RX packets:0 errors:0 dropped:0 overruns:0 frame:0
        TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
        RX Bytes:0  TX Bytes:0
        Interrupt:0 Base address:0x0

阅读(2615) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~