简单linux网络驱动程序

本文代码参考《LINUX设备驱动程序》第十七章 网络驱动程序

网络地址和虚拟主机地址为:

snullnet0 192.168.0.0

snullnet1 192.168.1.0

local0 192.168.0.1

remote0 192.168.0.2

local1 192.168.1.2

remote1 192.168.1.1

注:ldd自带网络驱动程序中的很多函数和数据结构在最新2.6.32内核上有所变动,导致编译不通过。

修改源码参考:http://blog.csdn.net/xiebiwei/article/details/6210887#quote

修改后的代码下载

测试功能
1) #make
2) #insmod snull.ko
3) #ls /sys/devices/virtual/net/
    lo  sn0  sn1
4) # ifconfig sn0 192.168.0.1
5) # ifconfig sn1 192.168.1.2
6) # ifconfig
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

sn0       Link encap:Ethernet  HWaddr 00:53:4E:55:4C:30
          inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::253:4eff:fe55:4c30/64 Scope:Link
          UP BROADCAST RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:1 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:70 (70.0 b)  TX bytes:210 (210.0 b)

sn1       Link encap:Ethernet  HWaddr 00:53:4E:55:4C:31
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::253:4eff:fe55:4c31/64 Scope:Link
          UP BROADCAST RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:3 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:210 (210.0 b)  TX bytes:70 (70.0 b)
7) # ping -c 2 192.168.0.2
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.052 ms
64 bytes from 192.168.0.2: icmp_seq=2 ttl=64 time=0.022 ms

--- 192.168.0.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.022/0.037/0.052/0.015 ms
8) # ping -c 2 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.028 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.022 ms

--- 192.168.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.022/0.025/0.028/0.003 ms

你可能感兴趣的:(linux)