PPP驱动程序的基本原理_第1页
PPP驱动程序的基本原理_第2页
PPP驱动程序的基本原理_第3页
PPP驱动程序的基本原理_第4页
PPP驱动程序的基本原理_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PPP 驱动程序的基本原理 1 ppp 设备是指在点对点的物理链路之间使用 PPP 帧进行分组交换的内核网络接口设备 由于 Linux 内核将串行设备作为终端设备来驱动 于是引入 PPP 终端规程来实现终端设备 与 PPP 设备的接口 根据终端设备的物理传输特性的不同 PPP 规程分为异步规程 N PPP 和同步规程 N SYNC PPP 两种 对于普通串口设备使用异步 PPP 规程 2 在 PPP 驱动程序中 每一 tty 终端设备对应于一条 PPP 传输通道 chanell 每一 ppp 网络 设备对应于一个 PPP 接口单元 unit 从终端设备上接收到的数据流通过 PPP 传输通道解码 后转换成 PPP 帧传递到 PPP 网络接口单元 PPP 接口单元再将 PPP 帧转换为 PPP 设备的接 收帧 反之 当 PPP 设备发射数据帧时 发射帧通过 PPP 接口单元转换成 PPP 帧传递给 PPP 通道 PPP 通道负责将 PPP 帧编码后写入终端设备 在配置了多链路 PPP 时 CONFIG PPP MULTILINK 多个 PPP 传输通道可连接到同一 PPP 接口单元 PPP 接口单 元将 PPP 帧分割成若干个片段传递给不同的 PPP 传输通道 反之 PPP 传输通道接收到的 PPP 帧片段被 PPP 接口单元重组成完整的 PPP 帧 3 在 Linux 2 4 中 应用程序可通过字符设备 dev ppp 监控内核 PPP 驱动程序 用户可以用 ioctl PPPIOCATTACH 将文件绑定到 PPP 接口单元上 来读写 PPP 接口单元的输出帧 也 可以用 ioctl PPPIOCATTCHAN 将文件绑定到 PPP 传输通道上 来读写 PPP 传输通道的输 入帧 4 PPP 传输通道用 channel 结构描述 系统中所有打开的传输通道在 all channels 链表中 PPP 接口单元用 ppp 结构描述 系统中所有建立的接口单元在 all ppp units 链表中 当终端 设备的物理链路连接成功后 用户使用 ioctl TIOCSETD 将终端切换到 PPP 规程 PPP 规程 初始化时 将建立终端设备的传输通道和通道驱动结构 对于异步 PPP 规程来说 通道驱动 结构为 asyncppp 它包含通道操作表 async ops 传输通道和接口单元各自包含自已的设备 文件 dev ppp 参数结构 ppp file drivers char tty io c int tty register ldisc int disc struct tty ldisc new ldisc if disc NR LDISCS return EINVAL if new ldisc ldiscs disc new ldisc ldiscs disc flags LDISC FLAG DEFINED ldiscs disc num disc else memset return 0 int tty ioctl struct inode inode struct file file unsigned int cmd unsigned long arg struct tty struct tty real tty int retval tty struct tty struct file private data if tty paranoia check tty inode i rdev tty ioctl return EINVAL real tty tty if tty driver type TTY DRIVER TYPE PTY switch cmd case TIOCGETD return put user tty ldisc num int arg case TIOCSETD return tiocsetd tty int arg if tty driver ioctl int retval tty driver ioctl tty file cmd arg if retval ENOIOCTLCMD return retval if tty ldisc ioctl int retval tty ldisc ioctl tty file cmd arg if retval ENOIOCTLCMD return retval return EINVAL static int tiocsetd struct tty struct tty int arg int retval ldisc retval get user ldisc arg if retval return retval return tty set ldisc tty ldisc Set the discipline of a tty line static int tty set ldisc struct tty struct tty int ldisc int retval 0 struct tty ldisc o ldisc char buf 64 if ldisc NR LDISCS return EINVAL Eduardo Blanco Cyrus Durgin if ldiscs ldisc flags sprintf modname tty ldisc d ldisc request module modname 尝试加载模块 if ldiscs ldisc flags if tty ldisc num ldisc return 0 We are already in the desired discipline o ldisc tty ldisc tty wait until sent tty 0 等待终端输出设备的数据发送完 Shutdown the current discipline if tty ldisc close tty ldisc close tty 关闭原来的规程 Now set up the new line discipline tty ldisc ldiscs ldisc tty termios c line ldisc if tty ldisc open retval tty ldisc open tty 打开新规程 if retval ldisc o ldisc tty termios c line tty ldisc num if tty ldisc open tty termios c line N TTY if tty ldisc open int r tty ldisc open tty if r ldisc num o ldisc num return retval drivers char tty ioctl c Internal flag options for termios setting behavior define TERMIOS FLUSH 1 define TERMIOS WAIT 2 define TERMIOS TERMIO 4 void tty wait until sent struct tty struct tty long timeout DECLARE WAITQUEUE wait current ifdef TTY DEBUG WAIT UNTIL SENT char buf 64 printk s wait until sent n tty name tty buf endif if tty driver chars in buffer return add wait queue if timeout timeout MAX SCHEDULE TIMEOUT do ifdef TTY DEBUG WAIT UNTIL SENT printk waiting s d n tty name tty buf tty driver chars in buffer tty endif set current state TASK INTERRUPTIBLE if signal pending current goto stop waiting if tty driver chars in buffer tty break timeout schedule timeout timeout while timeout if tty driver wait until sent tty driver wait until sent tty timeout stop waiting current state TASK RUNNING remove wait queue drivers net ppp async c The basic PPP frame define PPP HDRLEN 4 octets for standard ppp header define PPP FCSLEN 2 octets for FCS define PPP MRU 1500 default MRU max length of info field define OBUFSIZE 256 Structure for storing local state struct asyncppp 异步 PPP 通道的驱动结构 struct tty struct tty unsigned int flags unsigned int state unsigned int rbits int mru spinlock t xmit lock spinlock t recv lock unsigned long xmit flags u32 xaccm 8 终端字符转换位图 u32 raccm unsigned int bytes sent unsigned int bytes rcvd struct sk buff tpkt int tpkt pos u16 tfcs unsigned char optr unsigned char olim unsigned long last xmit struct sk buff rpkt int lcp fcs struct ppp channel chan interface to generic ppp layer unsigned char obuf OBUFSIZE static struct tty ldisc ppp ldisc 异步 PPP 规程操作表 magic TTY LDISC MAGIC name ppp open ppp asynctty open close ppp asynctty close read ppp asynctty read write ppp asynctty write ioctl ppp asynctty ioctl poll ppp asynctty poll receive room ppp asynctty room receive buf ppp asynctty receive write wakeup ppp asynctty wakeup struct ppp channel ops async ops PPP 通道驱动操作表 ppp async send 发送 PPP 帧到终端设备 ppp async ioctl int ppp async init void 模块初始化 int err err tty register ldisc N PPP 注册 N PPP 规程 if err 0 printk KERN ERR PPP async error d registering line disc n err return err Called when a tty is put into PPP line discipline static int ppp asynctty open struct tty struct tty 打开异步 PPP 规程 struct asyncppp ap int err MOD INC USE COUNT err ENOMEM ap kmalloc sizeof ap GFP KERNEL if ap 0 goto out initialize the asyncppp structure memset ap 0 sizeof ap ap tty tty 在驱动结构上设置打开终端指针 ap mru PPP MRU spin lock init spin lock init ap xaccm 0 0U ap xaccm 3 0 x60000000U ap raccm 0U ap optr ap obuf ap olim ap obuf ap lcp fcs 1 ap chan private ap 在一般的 PPP 驱动结构上设置异步驱动结构指针 ap chan ops 异步通道操作表 ap chan mtu PPP MRU err ppp register channel 建立通道驱动程序的传输通道结构 if err goto out free tty disc data ap 在打开终端结构上设置驱动结构指针 return 0 out free kfree ap out MOD DEC USE COUNT return err Called when the tty is put into another line discipline or it hangs up We assume that while we are in this routine the tty layer won t call any of the other line discipline entries for the same tty static void ppp asynctty close struct tty struct tty struct asyncppp ap tty disc data if ap 0 return tty disc data 0 ppp unregister channel if ap rpkt 0 kfree skb ap rpkt if ap tpkt 0 kfree skb ap tpkt kfree ap MOD DEC USE COUNT Read does nothing no data is ever available this way Pppd reads and writes packets via dev ppp instead static ssize t ppp asynctty read struct tty struct tty struct file file unsigned char buf size t count return EAGAIN Write on the tty does nothing the packets all come in from the ppp generic stuff static ssize t ppp asynctty write struct tty struct tty struct file file const unsigned char buf size t count return EAGAIN static int ppp asynctty ioctl struct tty struct tty struct file file unsigned int cmd unsigned long arg struct asyncppp ap tty disc data int err val err EFAULT switch cmd case PPPIOCGCHAN 取通道号 err ENXIO if ap 0 break err EFAULT if put user ppp channel index err 0 break case PPPIOCGUNIT 取接口单元号 err ENXIO if ap 0 break err EFAULT if put user ppp unit number err 0 break case TCGETS case TCGETA err n tty ioctl tty file cmd arg break case TCFLSH flush our buffers and the serial port s buffer if arg TCIOFLUSH arg TCOFLUSH ppp async flush output ap err n tty ioctl tty file cmd arg break case FIONREAD val 0 if put user val int arg break err 0 break case PPPIOCATTACH 将传输通道连接到接口单元 case PPPIOCDETACH 将传输通道与接口单元脱离 err ppp channel ioctl break default err ENOIOCTLCMD return err Flush output from our internal buffers Called for the TCFLSH ioctl static void ppp async flush output struct asyncppp ap int done 0 spin lock bh ap optr ap olim if ap tpkt NULL kfree skb ap tpkt ap tpkt 0 clear bit XMIT FULL done 1 spin unlock bh if done ppp output wakeup No kernel lock fine static unsigned int ppp asynctty poll struct tty struct tty struct file file poll table wait return 0 static int ppp asynctty room struct tty struct tty return 65535 drivers net ppp generic c Private data structure for each channel This includes the data structure used for multilink struct channel 传输通道结构 struct ppp file file stuff for read write poll struct ppp channel chan public channel data structure spinlock t downl protects chan file xq dequeue struct ppp ppp ppp unit we re connected to struct list head clist link in list of channels per unit rwlock t upl protects ppp and ulist ifdef CONFIG PPP MULTILINK u8 avail flag used in multilink stuff u8 had frag 1 fragments have been sent u32 lastseq MP last sequence received endif CONFIG PPP MULTILINK struct ppp channel 传输通道的通用驱动结构 void private channel private data struct ppp channel ops ops operations for this channel int mtu max transmit packet size int hdrlen amount of headroom channel needs void ppp opaque to channel the following are not used at present int speed transfer rate bytes second int latency overhead time in milliseconds struct ppp channel ops 驱动操作表 Send a packet or multilink fragment on this channel Returns 1 if it was accepted 0 if not int start xmit struct ppp channel struct sk buff Handle an ioctl call that has come in via dev ppp int ioctl struct ppp channel unsigned int unsigned long Data structure describing one ppp unit A ppp unit corresponds to a ppp network interface device and represents a multilink bundle It can have 0 or more ppp channels connected to it struct ppp struct ppp file file stuff for read write poll struct list head channels list of attached channels int n channels how many channels are attached spinlock t rlock lock for receive side spinlock t wlock lock for transmit side int mru max receive unit unsigned int flags control bits unsigned int xstate transmit state bits unsigned int rstate receive state bits int debug debug flags struct slcompress vj state for VJ header compression enum NPmode npmode NUM NP what to do with each net proto struct sk buff xmit pending a packet ready to go out struct compressor xcomp transmit packet compressor void xc state its internal state struct compressor rcomp receive decompressor void rc state its internal state unsigned long last xmit jiffies when last pkt sent unsigned long last recv jiffies when last pkt rcvd struct net device dev network interface device ifdef CONFIG PPP MULTILINK int nxchan next channel to send something on u32 nxseq next sequence number to send int mrru MP max reconst receive unit u32 nextseq MP seq no of next packet u32 minseq MP min of most recent seqnos struct sk buff head mrq MP receive reconstruction queue endif CONFIG PPP MULTILINK struct net device stats stats statistics An instance of dev ppp can be associated with either a ppp interface unit or a ppp channel In both cases file private data points to one of these struct ppp file 监控文件结构参数 enum INTERFACE 1 CHANNEL kind struct sk buff head xq pppd transmit queue struct sk buff head rq receive queue for pppd wait queue head t rwait for poll on reading dev ppp atomic t refcnt refs incl dev ppp attached int hdrlen space to leave for headers struct list head list link in all list int index interface unit channel number all ppp lock protects the all ppp units It also ensures that finding a ppp unit in the all ppp units list and updating its file refcnt field is atomic static spinlock t all ppp lock SPIN LOCK UNLOCKED static LIST HEAD all ppp units all channels lock protects all channels and last channel index and the atomicity of find a channel and updating its file refcnt field static spinlock t all channels lock SPIN LOCK UNLOCKED static LIST HEAD all channels static int last channel index Create a new unattached ppp channel int ppp register channel struct ppp channel chan struct channel pch pch kmalloc sizeof struct channel GFP ATOMIC if pch 0 return ENOMEM memset pch 0 sizeof struct channel pch ppp NULL pch chan chan chan ppp pch init ppp file pch file hdrlen chan hdrlen ifdef CONFIG PPP MULTILINK pch lastseq 1 endif CONFIG PPP MULTILINK spin lock init pch upl RW LOCK UNLOCKED spin lock bh pch file index last channel index list add spin unlock bh MOD INC USE COUNT return 0 Initialize a ppp file structure static void init ppp file struct ppp file pf int kind pf kind kind skb queue head init skb queue head init atomic set init waitqueue head void int ppp channel ioctl struct ppp channel chan unsigned int cmd unsigned long arg struct channel pch chan ppp int err ENOTTY int unit if capable CAP NET ADMIN return EPERM if pch 0 return EINVAL switch cmd case PPPIOCATTACH if get user unit int arg break err ppp connect channel pch unit break case PPPIOCDETACH err ppp disconnect channel pch break return err Connect a PPP channel to a PPP interface un

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论