二。回环网卡的程序设计

  

/***************************
*******回环网卡的驱动程序***
***********吕晓宁***********
*********2015.12.26*********
***************************/
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/in.h>
#include <linux/init.h> #include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/io.h> #include <linux/inet.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/ethtool.h>
#include <net/sock.h>
#include <net/checksum.h>
#include <linux/if_ether.h> /* For the statistics structure. */
#include <linux/if_arp.h> /* For ARPHRD_ETHER */
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/percpu.h>
#include <net/net_namespace.h> unsigned long bytes = ;
unsigned long packets = ;
struct net_device *dev; //发送函数
int loopback_xmit(struct sk_buff *skb,struct net_device *dev)
{
//将skb的值写入到寄存器中
skb->protocol = eth_type_trans(skb,dev); // 发送的数据量
bytes += skb->len;
packets++; netif_rx(skb); return ;
}
//接受函数
static struct net_device_stats *loopback_get_stats(struct net_device *dev)
{
//读取接受状态
struct net_device_stats *stats = &dev->stats ; //读取接受长度
stats->tx_bytes = bytes;
stats->rx_bytes = bytes;
stats->rx_packets = packets;
stats->tx_packets = packets; return stats; } struct net_device_ops loopback_ops =
{
.ndo_start_xmit = loopback_xmit,
.ndo_get_stats = loopback_get_stats, }; void static loopback_setup(struct net_device *dev)
{
//2. net_dev初始化参数
dev->mtu = (*)+++;
dev->flags = IFF_LOOPBACK;
dev->header_ops = &eth_header_ops;
dev->netdev_ops = &loopback_ops; } /* Setup and register the loopback device. */
static __net_init int loopback_net_init(struct net *net)
{
int err = -ENOMEM;
//1. 分配net_device结构
dev = alloc_netdev(,"lo%d",loopback_setup); //4.完成net_device的注册
err = register_netdev(dev); net->loopback_dev = dev; return ; } static __net_exit void loopback_net_exit(struct net *net)
{
//注销这个回环网卡驱动
unregister_netdev(dev); } /* Registered in net/core/dev.c */
struct pernet_operations __net_initdata loopback_net_ops = {
.init = loopback_net_init,
.exit = loopback_net_exit,
};

Linux 网络设备驱动程序设计(2)的更多相关文章

  1. Linux网络设备驱动架構學習(三)

    Linux网络设备驱动架構學習(三) 接下來會從以下幾個方面介紹網絡設備驅動的編寫流程: 1.網絡設備的註冊與註銷 2.網絡設備的初始化 3.網絡設備的打開與釋放 4.網絡數據發送流程 5.網絡數據接 ...

  2. linux网络设备驱动

    Linux网络设备驱动 Linux网络驱动程序的体系结构可划分为4个层次.Linux内核源代码中提供了网络设备接口及以网络子系统的上层的代码,移植特定网络硬件的驱动程序的主要工作就是完成设备驱动功能层 ...

  3. Linux网络设备驱动架構學習(二)

    Linux网络设备驱动架構學習(二) 接下來會從以下幾個方面介紹網絡設備驅動的編寫流程: 1.網絡設備的註冊與註銷 2.網絡設備的初始化 3.網絡設備的打開與釋放 4.網絡數據發送流程 5.網絡數據接 ...

  4. Linux网络设备驱动(一) _驱动模型

    Linux素来以其强大的网络功能著名,同时, 网络设备也作为三大设备之一, 成为Linux驱动学习中必不可少的设备类型, 此外, 由于历史原因, Linux并没有强制对网络设备贯彻其"一切皆 ...

  5. Linux网络设备驱动架构

    Linux网络设备驱动程序体系结构分为四层:网络协议接口层.网络设备接口层.提供实际功能的设备驱动层以及网络设备与媒介层. (1)网络协议接口层向网络层协议提供统一的数据包收发接口,不论上层协议是AR ...

  6. Linux网络设备驱动 _驱动模型

    Linux素来以其强大的网络功能著名,同时, 网络设备也作为三大设备之一, 成为Linux驱动学习中必不可少的设备类型, 此外, 由于历史原因, Linux并没有强制对网络设备贯彻其"一切皆 ...

  7. Linux 网络设备驱动开发(一) —— linux内核网络分层结构

    Preface Linux内核对网络驱动程序使用统一的接口,并且对于网络设备采用面向对象的思想设计. Linux内核采用分层结构处理网络数据包.分层结构与网络协议的结构匹配,既能简化数据包处理流程,又 ...

  8. linux 网络设备驱动

    linux 网络驱动 谨以此文纪念过往的岁月 一.前言在linux中网络驱动也是一个大头,如何去理解网络驱动是作为一个linux驱动工程师必备的技能.不过同样的设备,在不同人的手中会有不同的效果,其原 ...

  9. 《linux设备驱动开发详解》笔记——14 linux网络设备驱动

    14.1 网络设备驱动结构 网络协议接口层:硬件无关,标准收发函数dev_queue_xmit()和netif_rx();  注意,netif_rx是将接收到的数据给上层,有时也在驱动收到数据以后调用 ...

随机推荐

  1. Architecture of Device I/O Drivers, Device Driver Design

    http://www.kalinskyassociates.com/Wpaper4.html Architecture of Device I/O Drivers Many embedded syst ...

  2. jQuery Mobile和PhoneGap混合开发

    其实二者并不影响,PhoneGap负责调用系统的接口,jQuery Mobile实现一些网页效果.PhoneGap开发请看上一篇文章,jQuery Mobile开发环境搭建如下:[请先阅读上一篇文章, ...

  3. Cocos2dx Widget button透明区域过滤

    小伟哥 遇到一个命题: button透明区域过滤.当点击一个建筑button.花的时候不得不想一些方法把点击透明区域过滤掉. 让点击也没有效果滴啦. 開始搜索了半天才有所思路. 在网络上非常多贴代码的 ...

  4. 【M31】让函数根据一个以上的对象类型来决定如何虚化

    1.考虑下面的问题,游戏软件中有角色A,B,角色又可以细化为A1,A2,A3:B1,B2,B3,两类角色之间相互攻击.即A1可以攻击B1,B2,B3,B1可以攻击A1,A2,A3.C++的多态,只根据 ...

  5. hdu 5258 数长方形 离散化

    数长方形 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5258 Des ...

  6. 【JavaScript】常用的JS

    JQuery.UnderScoreJS.Zepto 原生JS等

  7. TP复习2

    ## ThinkPHP 3.1.2 输出和模型使用#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:M() 等效为 new Model();$m=M('Us ...

  8. [NodeJS] Deploy a Node Application with the Now CLI

    Now offers a friction-free way to deploy node applications right from the terminal. In this lesson, ...

  9. Android典型界面设计——ViewPage+Fragment实现区域顶部tab滑动切换

    一.问题描写叙述 本系列将结合案例应用,陆续向大家介绍一些Android典型界面的设计,首先说说tab导航,导航分为一层和两层(底部区块+区域内头部导航).主要实现方案有RadioGroup+View ...

  10. Php-SPL库中的迭代器类详解(转)

    SPL提供了多个迭代器类,分别提供了迭代访问.过滤数据.缓存结果.控制分页等功能.,因为php总是在不断壮大,我尽可能列出SPL中所有的迭代类.下面其中一些迭代器类是需要php5.4,另外一些如Sea ...