Epoll模型

Linux2.6 内核epoll介绍
    先介绍2本书《The Linux Networking Architecture--Design and Implementation of Network Protocols in the Linux Kernel》,以2.4内核讲解Linux TCP/IP实现,相当不错.作为一个现实世界中的实现,很多时候你必须作很多权衡,这时候参考一个久经考验的系统更有实际意义。举个例子,linux内 核中sk_buff结构为了追求速度和安全,牺牲了部分内存,所以在发送TCP包的时候,无论应用层数据多大,sk_buff最小也有272的字节.其实 对于socket应用层程序来说,另外一本书《UNIX Network Programming Volume 1》意义更大一点.2003年的时候,这本书出了最新的第3版本,不过主要还是修订第2版本。其中第6章《I/O Multiplexing》是最重要的。Stevens给出了网络IO的基本模型。在这里最重要的莫过于select模型和Asynchronous I/O模型.从理论上说,AIO似乎是最高效的,你的IO操作可以立即返回,然后等待os告诉你IO操作完成。但是一直以来,如何实现就没有一个完美的方 案。最著名的windows完成端口实现的AIO,实际上也是内部用线程池实现的罢了,最后的结果是IO有个线程池,你应用也需要一个线程池...... 很多文档其实已经指出了这带来的线程context-switch带来的代价。在linux 平台上,关于网络AIO一直是改动最多的地方,2.4的年代就有很多AIO内核patch,最著名的应该算是SGI那个。但是一直到2.6内核发布,网络 模块的AIO一直没有进入稳定内核版本(大部分都是使用用户线程模拟方法,在使用了NPTL的linux上面其实和windows的完成端口基本上差不多 了)。2.6内核所支持的AIO特指磁盘的AIO---支持io_submit(),io_getevents()以及对Direct IO的支持(就是绕过VFS系统buffer直接写硬盘,对于流服务器在内存平稳性上有相当帮助)。
所以,剩下的select模型基本上就是我们在linux上面的唯一选择,其实,如果加上no-block socket的配置,可以完成一个"伪"AIO的实现,只不过推动力在于你而不是os而已。不过传统的select/poll函数有着一些无法忍受的缺 点,所以改进一直是2.4-2.5开发版本内核的任务,包括/dev/poll,realtime signal等等。最终,Davide Libenzi开发的epoll进入2.6内核成为正式的解决方案

3、epoll的优点
<1>支持一个进程打开大数 目的socket描述符(FD)
    select 最不能忍受的是一个进程所打开的FD是有一定限制的,由FD_SETSIZE设置,默认值是2048。对于那些需要支持的上万连接数目的IM服务器来说显 然太少了。这时候你一是可以选择修改这个宏然后重新编译内核,不过资料也同时指出这样会带来网络效率的下降,二是可以选择多进程的解决方案(传统的 Apache方案),不过虽然linux上面创建进程的代价比较小,但仍旧是不可忽视的,加上进程间数据同步远比不上线程间同步的高效,所以也不是一种完 美的方案。不过 epoll则没有这个限制,它所支持的FD上限是最大可以打开文件的数目,这个数字一般远大于2048,举个例子,在1GB内存的机器上大约是10万左 右,具体数目可以cat /proc/sys/fs/file-max察看,一般来说这个数目和系统内存关系很大。

<2>IO 效率不随FD数目增加而线性下降
     传统的select/poll另一个致命弱点就是当你拥有一个很大的socket集合,不过由于网络延时,任一时间只有部分的socket是"活跃"的, 但是select/poll每次调用都会线性扫描全部的集合,导致效率呈现线性下降。但是epoll不存在这个问题,它只会对"活跃"的socket进行 操作---这是因为在内核实现中epoll是根据每个fd上面的callback函数实现的。那么,只有"活跃"的socket才会主动的去调用 callback函数,其他idle状态socket则不会,在这点上,epoll实现了一个"伪"AIO,因为这时候推动力在os内核。在一些 benchmark中,如果所有的socket基本上都是活跃的---比如一个高速LAN环境,epoll并不比select/poll有什么效率,相 反,如果过多使用epoll_ctl,效率相比还有稍微的下降。但是一旦使用idle connections模拟WAN环境,epoll的效率就远在select/poll之上了。

<3>使用mmap加速内核 与用户空间的消息传递。
    这点实际上涉及到epoll的具体实现了。无论是select,poll还是epoll都需要内核把FD消息通知给用户空间,如何避免不必要的内存拷贝就 很重要,在这点上,epoll是通过内核于用户空间mmap同一块内存实现的。而如果你想我一样从2.5内核就关注epoll的话,一定不会忘记手工 mmap这一步的。

<4>内核微调
    这一点其实不算epoll的优点了,而是整个linux平台的优点。也许你可以怀疑 linux平台,但是你无法回避linux平台赋予你微调内核的能力。比如,内核TCP/IP协议栈使用内存池管理sk_buff结构,那么可以在运行时 期动态调整这个内存pool(skb_head_pool)的大小--- 通过echo XXXX>/proc/sys/net/core/hot_list_length完成。再比如listen函数的第2个参数(TCP完成3次握手 的数据包队列长度),也可以根据你平台内存大小动态调整。更甚至在一个数据包面数目巨大但同时每个数据包本身大小却很小的特殊系统上尝试最新的NAPI网 卡驱动架构。

4、epoll的工作模式
    令人高兴的是,2.6内核的epoll比其2.5开发版本的/dev/epoll简洁了许多,所以,大部分情况下,强大的东西往往是简单的。唯一有点麻烦 是epoll有2种工作方式:LT和ET。
LT(level triggered)是缺省的工作方式,并且同时支持block和no-block socket.在这种做法中,内核告诉你一个文件描述符是否就绪了,然后你可以对这个就绪的fd进行IO操作。如果你不作任何操作,内核还是会继续通知你 的,所以,这种模式编程出错误可能性要小一点。传统的select/poll都是这种模型的代表.
ET (edge-triggered)是高速工作方式,只支持no-block socket。在这种模式下,当描述符从未就绪变为就绪时,内核通过epoll告诉你。然后它会假设你知道文件描述符已经就绪,并且不会再为那个文件描述 符发送更多的就绪通知,直到你做了某些操作导致那个文件描述符不再为就绪状态了(比如,你在发送,接收或者接收请求,或者发送接收的数据少于一定量时导致 了一个EWOULDBLOCK 错误)。但是请注意,如果一直不对这个fd作IO操作(从而导致它再次变成未就绪),内核不会发送更多的通知(only once),不过在TCP协议中,ET模式的加速效用仍需要更多的benchmark确认。
epoll只有epoll_create,epoll_ctl,epoll_wait 3个系统调用,具体用法请参考http://www.xmailserver.org/linux-patches/nio-improve.html ,在http://www.kegel.com/rn/也有一个完整的例子,大家一看就知道如何使用了
Leader/follower模式线程 pool实现,以及和epoll的配合。

5、 epoll的使用方法
    首先通过create_epoll(int maxfds)来创建一个epoll的句柄,其中maxfds为你epoll所支持的最大句柄数。这个函数会返回一个新的epoll句柄,之后的所有操作 将通过这个句柄来进行操作。在用完之后,记得用close()来关闭这个创建出来的epoll句柄。之后在你的网络主循环里面,每一帧的调用 epoll_wait(int epfd, epoll_event events, int max events, int timeout)来查询所有的网络接口,看哪一个可以读,哪一个可以写了。基本的语法为:
nfds = epoll_wait(kdpfd, events, maxevents, -1);
    其中kdpfd为用epoll_create创建之后的句柄,events是一个 epoll_event*的指针,当epoll_wait这个函数操作成功之后,epoll_events里面将储存所有的读写事件。 max_events是当前需要监听的所有socket句柄数。最后一个timeout是 epoll_wait的超时,为0的时候表示马上返回,为-1的时候表示一直等下去,直到有事件范围,为任意正整数的时候表示等这么长的时间,如果一直没 有事件,则范围。一般如果网络主循环是单独的线程的话,可以用-1来等,这样可以保证一些效率,如果是和主逻辑在同一个线程的话,则可以用0来保证主循环 的效率。


Epoll模型详解 - 成 - 我的博客
    Epoll模型主要负责对大量并发用户的请求进行及时处理,完成服务器与客户端的数据交互。其具体的实现步骤如下:
(a) 使用epoll_create()函数创建文件描述,设定将可管理的最大socket描述符数目。
(b) 创建与epoll关联的接收线程,应用程序可以创建多个接收线程来处理epoll上的读通知事件,线程的数量依赖于程序的具体需要。
(c) 创建一个侦听socket描述符ListenSock;将该描述符设定为非阻塞模式,调用Listen()函数在套接字上侦听有无新的连接请求,在 epoll_event结构中设置要处理的事件类型EPOLLIN,工作方式为 epoll_ET,以提高工作效率,同时使用epoll_ctl()注册事件,最后启动网络监视线程。
(d) 网络监视线程启动循环,epoll_wait()等待epoll事件发生。
(e) 如果epoll事件表明有新的连接请求,则调用accept()函数,将用户socket描述符添加到epoll_data联合体,同时设定该描述符为非 阻塞,并在epoll_event结构中设置要处理的事件类型为读和写,工作方式为epoll_ET.
(f) 如果epoll事件表明socket描述符上有数据可读,则将该socket描述符加入可读队列,通知接收线程读入数据,并将接收到的数据放入到接收数据 的链表中,经逻辑处理后,将反馈的数据包放入到发送数据链表中,等待由发送线程发送。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Design and Implementation of Network Protocols in the Linux Kernel Part I: The Linux Kernel 1 Chapter 1. Motivation 3 Section 1.1. The Linux Operating System 4 Section 1.2. What is Linux? 5 Section 1.3. Reasons for Using Linux 6 Chapter 2. The Kernel Structure 9 Section 2.1. Monolithic Architectures and Microkernels 11 Section 2.2. Activities in the Linux Kernel 12 Section 2.3. Locking—Atomic Operations 17 Section 2.4. Kernel Modules 23 Section 2.5. Device Drivers 29 Section 2.6. Memory Management in the Kernel 31 Section 2.7. Timing in the Linux Kernel 35 Section 2.8. The Proc File System 40 Section 2.9. Versioning 43 Part II: Architecture of Network Implementation 45 Chapter 3. The Architecture of Communication Systems 47 Section 3.1. Layer-Based Communication Models 47 Section 3.2. Services and Protocols 52 Chapter 4. Managing Network Packets in the Kernel 55 Section 4.1. Socket Buffers 55 Section 4.2. Socket-Buffer Queues 66 Chapter 5. Network Devices 71 Section 5.1. The net_device Interface 73 Section 5.2. Managing Network Devices 82 Section 5.3. Network Drivers 92 Part III: Layer I + II—Medium Access and Logical Link Layer 115 Chapter 6. Introduction to the Data-Link Layer 117 Section 6.1. Structure of the Data-Link Layer 117 Section 6.2. Processes on the Data-Link Layer 119 Section 6.3. Managing Layer-3 Protocols 127 Chapter 7. The Serial-Line Internet Protocol (SLIP) 132 Section 7.1. Introduction 132 Section 7.2. Slip Implementation in the Linux Kernel 134 Chapter 8. The Point-to-Point Protocol (PPP) 145 Section 8.1. Introduction 145 Section 8.2. PPP Configuration in Linux 148 Section 8.3. PPP Implementation in the Linux Kernel 150 Section 8.4. Implementing the PPP Daemon 158 Chapter 9. PPP over Ethernet 161 Section 9.1. Introduction 161 Section 9.2. PPPOE Specification in RFC 2516 161 Section 9.3. Implementation in the User Space 163 Section 9.4. Implementation in the Linux Kernel 164 Chapter 10. Asynchronous Transfer Mode—ATM 168 Section 10.1. Introduction 168 Section 10.2. Implementing ATM in Linux 169 Section 10.3. Configuration 177 Chapter 11. Bluetooth in Linux 179 Section 11.1. Host Controller Interface (HCI) 181 Section 11.2. L2CAP 185 Section 11.3. Other Protocols 188 Chapter 12. Transparent Bridges 189 Section 12.1. Introduction 189 Section 12.2. Basics 190 Section 12.3. Configuring a Bridge in Linux 199 Section 12.4. Implementation 202 Part IV: Network Layer 221 Chapter 13. The TCP/IP Protocols 223 Section 13.1. The Internet Protocol Suite 224 Chapter 14. The Internet Protocol V4 227 Section 14.1. Properties of the Internet Protocol 228 Section 14.2. Implementing the Internet Protocol 233 Section 14.3. IP Options 250 Section 14.4. Internet Control Message Protocol (ICMP) 262 Chapter 15. Address Resolution Protocol (ARP) 273 Section 15.1. Using the Address Resolution Protocol 274 Section 15.2. The ARP Command 276 Section 15.3. Implementing the ARP Instance in the Linux Kernel 277 Chapter 16. IP Routing 293 Section 16.1. Introduction 293 Section 16.2. Configuration 301 Section 16.3. Implementation 309 Chapter 17. IP Multicast for Group Communication 330 Section 17.1. Group Communication 331 Section 17.2. IP Multicast 333 Section 17.3. Internet Group Management Protocol (IGMP) 339 Section 17.4. Multicast Data Path in the Linux Kernel 345 Section 17.5. Multicasting in Today's Internet 355 Section 17.6. Multicast Transport Protocols 364 Chapter 18. Using Traffic Control to Support Quality of Service (QoS) 366 Section 18.1. Introduction 366 Section 18.2. Basic Structure of Traffic Control in Linux 367 Section 18.3. Traffic Control in the Outgoing Direction 367 Section 18.4. Kernel Structures and Interfaces 369 Section 18.5. Ingress Policing 378 Section 18.6. Implementing a Queuing Discipline 378 Section 18.7. Configuration 381 Chapter 19. Packet Filters and Firewalls 383 Section 19.1. Introduction 383 Section 19.2. The Ipchains Architecture of Linux 2.2 386 Section 19.3. The Netfilter Architecture of Linux 2.4 391 Chapter 20. Connection Tracking 399 Section 20.1. Introduction 399 Section 20.2. Implementation 400 Chapter 21. Network Address Translation (NAT) 410 Section 21.1. Introduction 410 Section 21.2. Configuring NAT in Linux 414 Section 21.3. Implementing the NAT Module 416 Section 21.4. Interfaces to Extend the NAT Module 422 Chapter 22. Extending the Linux Network Architecture Functionality—KIDS 426 Section 22.1. Managing Dynamically Extendable Functionalities 426 Section 22.2. Structure of the KIDS Construction System 428 Section 22.3. Using the KIDS Example to Extend the Linux Network Architecture 431 Chapter 23. IPv6—Internet Protocol Version 6 443 Section 23.1. Introduction 443 Section 23.2. IPv6 Features 443 Section 23.3. IPv6 Implementation 450 Part V: Layer IV—Transport Layer 455 Chapter 24. Transmission Control Protocol (TCP) 457 Section 24.1. Overview 457 Section 24.2. Implementing The TCP Protocol Instance 460 Section 24.3. Connection Management 476 Section 24.4. Protocol Mechanisms For Data Exchange 486 Section 24.5. Timer Management In TCP 508 Chapter 25. User Datagram Protocol (UDP) 513 Section 25.1. Introduction 513 Section 25.2. Data Structures 514 Section 25.3. Sending and Receiving UDP Datagrams 519 Chapter 26. The Concept of Sockets 522 Section 26.1. Introduction 522 Section 26.2. BSD Sockets 522 Section 26.3. Protocol-Specific Sockets
• Table of Contents • Index The Linux® Networking Architecture: Design and Implementation of Network Protocols in the Linux Kernel By Klaus Wehrle, Frank Pählke, Hartmut Ritter, Daniel Müller, Marc Bechler Publisher : Prentice Hall Pub Date : August 01, 2004 ISBN : 0-13-177720-3 Pages : 648 Copyright ii Preface xiii Organization of this Book xiv Additional Sources of Information xv Conventions Used in this Book xvi Acknowledgments xvii Part I: The Linux Kernel 1 Chapter 1. Motivation 3 Section 1.1. The Linux Operating System 4 Section 1.2. What is Linux? 5 Section 1.3. Reasons for Using Linux 6 Chapter 2. The Kernel Structure 9 Section 2.1. Monolithic Architectures and Microkernels 11 Section 2.2. Activities in the Linux Kernel 12 Section 2.3. Locking—Atomic Operations 17 Section 2.4. Kernel Modules 23 Section 2.5. Device Drivers 29 Section 2.6. Memory Management in the Kernel 31 Section 2.7. Timing in the Linux Kernel 35 Section 2.8. The Proc File System 40 Section 2.9. Versioning 43 Part II: Architecture of Network Implementation 45 Chapter 3. The Architecture of Communication Systems 47 Section 3.1. Layer-Based Communication Models 47 Section 3.2. Services and Protocols 52 Chapter 4. Managing Network Packets in the Kernel 55 Section 4.1. Socket Buffers 55 Section 4.2. Socket-Buffer Queues 66 Chapter 5. Network Devices 71 Section 5.1. The net_device Interface 73 Section 5.2. Managing Network Devices 82 Section 5.3. Network Drivers 92 Part III: Layer I + II—Medium Access and Logical Link Layer 115 Chapter 6. Introduction to the Data-Link Layer 117 Section 6.1. Structure of the Data-Link Layer 117 Section 6.2. Processes on the Data-Link Layer 119 Section 6.3. Managing Layer-3 Protocols 127 Chapter 7. The Serial-Line Internet Protocol (SLIP) 132 Section 7.1. Introduction 132 Section 7.2. Slip Implementation in the Linux Kernel 134 Chapter 8. The Point-to-Point Protocol (PPP) 145 Section 8.1. Introduction 145 Section 8.2. PPP Configuration in Linux 148 Section 8.3. PPP Implementation in the Linux Kernel 150 Section 8.4. Implementing the PPP Daemon 158 Chapter 9. PPP over Ethernet 161 Section 9.1. Introduction 161 Section 9.2. PPPOE Specification in RFC 2516 161 Section 9.3. Implementation in the User Space 163 Section 9.4. Implementation in the Linux Kernel 164 Chapter 10. Asynchronous Transfer Mode—ATM 168 Section 10.1. Introduction 168 Section 10.2. Implementing ATM in Linux 169 Section 10.3. Configuration 177 Chapter 11. Bluetooth in Linux 179 Section 11.1. Host Controller Interface (HCI) 181 Section 11.2. L2CAP 185 Section 11.3. Other Protocols 188 Chapter 12. Transparent Bridges 189 Section 12.1. Introduction 189 Section 12.2. Basics 190 Section 12.3. Configuring a Bridge in Linux 199 Section 12.4. Implementation 202 Part IV: Network Layer 221 Chapter 13. The TCP/IP Protocols 223 Section 13.1. The Internet Protocol Suite 224 Chapter 14. The Internet Protocol V4 227 Section 14.1. Properties of the Internet Protocol 228 Section 14.2. Implementing the Internet Protocol 233 Section 14.3. IP Options 250 Section 14.4. Internet Control Message Protocol (ICMP) 262 Chapter 15. Address Resolution Protocol (ARP) 273 Section 15.1. Using the Address Resolution Protocol 274 Section 15.2. The ARP Command 276 Section 15.3. Implementing the ARP Instance in the Linux Kernel 277 Chapter 16. IP Routing 293 Section 16.1. Introduction 293 Section 16.2. Configuration 301 Section 16.3. Implementation 309 Chapter 17. IP Multicast for Group Communication 330 Section 17.1. Group Communication 331 Section 17.2. IP Multicast 333 Section 17.3. Internet Group Management Protocol (IGMP) 339 Section 17.4. Multicast Data Path in the Linux Kernel 345 Section 17.5. Multicasting in Today's Internet 355 Section 17.6. Multicast Transport Protocols 364 Chapter 18. Using Traffic Control to Support Quality of Service (QoS) 366 Section 18.1. Introduction 366 Section 18.2. Basic Structure of Traffic Control in Linux 367 Section 18.3. Traffic Control in the Outgoing Direction 367 Section 18.4. Kernel Structures and Interfaces 369 Section 18.5. Ingress Policing 378 Section 18.6. Implementing a Queuing Discipline 378 Section 18.7. Configuration 381 Chapter 19. Packet Filters and Firewalls 383 Section 19.1. Introduction 383 Section 19.2. The Ipchains Architecture of Linux 2.2 386 Section 19.3. The Netfilter Architecture of Linux 2.4 391 Chapter 20. Connection Tracking 399 Section 20.1. Introduction 399 Section 20.2. Implementation 400 Chapter 21. Network Address Translation (NAT) 410 Section 21.1. Introduction 410 Section 21.2. Configuring NAT in Linux 414 Section 21.3. Implementing the NAT Module 416 Section 21.4. Interfaces to Extend the NAT Module 422 Chapter 22. Extending the Linux Network Architecture Functionality—KIDS 426 Section 22.1. Managing Dynamically Extendable Functionalities 426 Section 22.2. Structure of the KIDS Construction System 428 Section 22.3. Using the KIDS Example to Extend the Linux Network Architecture 431 Chapter 23. IPv6—Internet Protocol Version 6 443 Section 23.1. Introduction 443 Section 23.2. IPv6 Features 443 Section 23.3. IPv6 Implementation 450 Part V: Layer IV—Transport Layer 455 Chapter 24. Transmission Control Protocol (TCP) 457 Section 24.1. Overview 457 Section 24.2. Implementing The TCP Protocol Instance 460 Section 24.3. Connection Management 476 Section 24.4. Protocol Mechanisms For Data Exchange 486 Section 24.5. Timer Management In TCP 508 Chapter 25. User Datagram Protocol (UDP) 513 Section 25.1. Introduction 513 Section 25.2. Data Structures 514 Section 25.3. Sending and Receiving UDP Datagrams 519 Chapter 26. The Concept of Sockets 522 Section 26.1. Introduction 522 Section 26.2. BSD Sockets 522 Section 26.3. Protocol-Specific Sockets 526 Part VI: Layer V—Application Layer 533 Chapter 27. Network Programming With Sockets 535 Section 27.1. Introduction 535 Section 27.2. Functions of the Socket API 538 Section 27.3. Examples 548 Part VII: Appendices 549 Appendix A. The LXR Source-Code Browser 551 Section A.1. Functionality 551 Section A.2. Installation 555 Appendix B. Debugging in the Linux Kernel 557 Section B.1. Log Outputs From the Linux Kernel 557 Section B.2. Creating Strings in the Kernel 561 Section B.3. Information in the /proc Directory 564 Section B.4. Using a Debugger with the Linux Kernel 569 Appendix C. Tools and Commands for Network Operation 572 Section C.1. Using ifconfig to Manage Network Devices 572 Section C.2. Using ping to Test the Reachability 575 Section C.3. Using netstat to View the Network State 576 Section C.4. Using route for Routing Information 578 Section C.5. Using tcpdump for Network Analysis 579 Section C.6. USING traceroute TO TRACE PACKETS 582 Section C.7. Other Tools 584 Appendix D. Example for a Kernel Module 588 Appendix E. Example for a Network-Layer Protocol 591 Appendix F. Example for a Transport Protocol 593 Appendix G. Example for Communication over Sockets 595 Section G.1. SERVER 595 Section G.2. CLIENT 598 Bibliography Index

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值