一、epoll_create & epoll_create1

SYSCALL_DEFINE1(epoll_create, int, size)

sys_epoll_create->sys_epoll_create1

SYSCALL_DEFINE1(epoll_create1, int, flags)

sys_epoll_create1(入参检测等)->ep_alloc(分配eventpoll,并初始化锁、等待队列等结构)->[sys_epoll_create1]get_unused_fd_flags(分配fd)->[sys_epoll_create1]anon_inode_getfile(分配file)->[sys_epoll_create1]fd_install(关联file和fd)

anon_inode_getfile:

  1. 该函数创建的文件共享使用一个inode,节省内存避免代码重复,inode:anon_inode_inode、 sb:anon_inode_mnt->mnt_sb、 fs:anon_inode_fs_type,初始化位置[anon_inode_init]。

  2. 使用alloc_file分配一个文件,file->f_op = eventpoll_fops;

  3. 设置file->f_flags = (O_RDWR | (flags & O_CLOEXEC)) & (O_ACCMODE | O_NONBLOCK); file->private_data =ep;

其中eventpoll_fops注册了ep_show_fdinfo函数,允许我们在proc中查看对应epfd的epi信息

    1. lybxin@Inspiron:~$more /proc/1469/task/1469/fdinfo/4
    2. pos: 0
    3. flags: 02000002
    4. mnt_id: 11
    5. tfd:        5 events:       19 data:     55b8247c8da0
    6. tfd:       13 events:       19 data:     55b8247ccc90
    7. tfd:       14 events:       19 data:     55b8248079a0
    8. tfd:        9 events:       19 data:     55b8247e9860
    9. tfd:       12 events:       1a data:     55b8247e9b40
    10. tfd:        8 events:       19 data:     55b8247caf90
    11. tfd:        7 events:       19 data:     55b824806490

二、epoll_ctl

SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,struct epoll_event __user *, event)

sys_epoll_ctl:

  1. 如果不是EPOLL_CTL_DEL操作,则从用户空间复制epoll_event结构

  2. 获取fd结构,epfd->f,需要操作的fd->tf

  3. 目标fd对应的fd结构必须支持tf.file->f_op->poll操作

  4. 处理EPOLLWAKEUP标志

  5. 检验epfd对应有效的epoll文件描述符,且需要操作的fd与epfd不是对应同一个文件

  6. 通过ep_loop_check检测epfd是否构成闭环或者连续epfd的深度超过5,对应宏EP_MAX_NESTS[4]

  7. 通过ep_find查找这个epfd是否已经添加了目标fd文件

  8. ADD/MOD操作自动添加POLLERR | POLLHUP这两个标志位

ep_loop_check:

  1. visited_list:表示已经处理过的节点,假设epfd1下挂epfd2和epfd3,而epfd2和epfd3又同时挂epfd4,那么保证epfd只处理一次

  2. tfile_check_list:保存非epoll文件的fd用于反向检查

  3. 从源码和下面的测试来看这个闭环和深度检测只能从添加的fd向下检测,而不能向上检测,因此并不是所有场景都能有效的检测出来,如下测试,另外还有一种场景因为会跳过已经visit的节点,所以visit的节点的最大深度也可能会超过5。

    1. ---------------test1 start---------------  //正向查找只检测target fd
    2. add epfd2 to epfd1:add 1 success
    3. add epfd3 to epfd2:add 2 success
    4. add epfd4 to epfd3:add 3 success
    5. add epfd5 to epfd4:add 4 success
    6. add epfd6 to epfd5:add 5 success
    7. add epfd7 to epfd6:add 6 success
    8. add epfd8 to epfd7:add 7 success
    9. add epfd9 to epfd8:add 8 success
    10. ---------------test1 end---------------
    11. ---------------test2 start---------------  //正向查找只检测target fd
    12. add epfd1 to epfd2:add 1 success
    13. add epfd2 to epfd3:add 2 success
    14. add epfd3 to epfd4:add 3 success
    15. add epfd4 to epfd5:add 4 success
    16. add epfd5 to epfd6:add 5 success
    17. add epfd6 to epfd7:epoll_ctl error:Too many levels of symbolic links(errno:40)
    18. add epfd7 to epfd8:add 6 success
    19. add epfd8 to epfd9:add 7 success
    20. ---------------test2 end---------------
    21. ---------------test3 start---------------  //正向查找形成闭环 操作失败
    22. add epfd2 to epfd1:add 1 success
    23. add epfd3 to epfd2:add 2 success
    24. add epfd1 to epfd3:epoll_ctl error:Too many levels of symbolic links(errno:40)
    25. ---------------test3 end---------------

ep_insert:

  1. max_user_watches:/proc/sys/fs/epoll/max_user_watches 每个用户同时watch的最大fd数目

  2. 如果watch的总数超过max_user_watches,则返回ENOSPC

  3. 如果从epi_cache分配epitem失败,则返回ENOMEM

  4. 根据EPOLLWAKEUP标志注册wake up

  5. 通过ep_item_poll把item添加到poll钩子中,并获取当前revents。最终会通过ep_ptable_queue_proc函数把eppoll_entry添加到sk->sk_wq->wait的头部,并通过pwq->llink添加到epi->pwqlist的尾部。这里每个epi对应一个pwqlist链表的原因是poll一些文件的时候,需要添加两次等待队列,如/dev/bsg/目录下面的文件。

  6. 把epi插入到f_ep_links链表的尾部,list_add_tail_rcu(&epi->fllink, &tfile->f_ep_links);

  7. 把epi插入到ep的红黑树中,ep_rbtree_insert(ep, epi);

  8. 通过reverse_path_check进行反向检查

  9. 如果获取到的revents中有用户关注的事件,并且epi未在ready链表中,那么把epi插入ready链表尾部 list_add_tail(&epi->rdllink, &ep->rdllist);并尝试唤醒epoll_wait进程wake_up_locked(&ep->wq);以及file->poll()等待进程ep_poll_safewake(&ep->poll_wait)

  10. 自增ep->user->epoll_watches

reverse_path_check:

  1. 对于第一层反向检查不限制数目。

  2. 对于第2-5层,限制引用数目分别为500、100、50、10,如下变量定义了上限,其中该变量第一个成员1000仅作占位使用,并不限制第一层引用总数,参考path_count_inc。static const int path_limits[PATH_ARR_SIZE] = { 1000, 500, 100, 50, 10 };

  3. 对于5层以上则直接返回错误

    1. ---------------test4 反向查找---------------  //反向查找层数超过5
    2. add epfd2 to epfd1:add 1 success
    3. add epfd3 to epfd2:add 2 success
    4. add epfd4 to epfd3:add 3 success
    5. add listen_fd to epfd4:add 4 success
    6. add epfd5 to epfd4:add 5 success
    7. add listen_fd to epfd5:add 6 success
    8. add epfd6 to epfd5:add 7 success
    9. add listen_fd to epfd6:epoll_ctl error:Invalid argument(errno:22)
    10. add epfd7 to epfd6:add 8 success
    11. add listen_fd to epfd7:epoll_ctl error:Invalid argument(errno:22)
    12. add epfd8 to epfd7:add 9 success
    13. add epfd9 to epfd8:add 10 success
    14. add listen_fd to epfd9:epoll_ctl error:Invalid argument(errno:22)
    15. ---------------test4 end---------------
    16. ---------------test5 反向查找 i:0 ---------------  //第一层反向查找直到fd数目的上限才会失败添加第一层
    17. 添加第一层 add error num:1021  error:Bad file descriptor(errno:9)
    18. ---------------test5 end i:1020 ---------------
    19. ---------------test6 反向查找 i:0 ---------------   //第二层反向查找的限制为path_limits[1]
    20. 第二层 add error i:501  error:Invalid argument(errno:22)
    21. ---------------test6 end i:500 ---------------

ep_remove:

  1. 移除一个epi

  2. 从poll wait中移除

  3. 从file的f_ep_links链表移除

  4. 从红黑树中移除

  5. 从ready链表中移除

  6. 取消wakeup注册

  7. 自减ep->user->epoll_watches

ep_modify:修改epi

  1. 更新epi->event.events和epi->event.data

  2. 根据EPOLLWAKEUP更新wake up

  3. 刷新内存屏障smp_mb

  4. 通过ep_item_poll获取revents,相比ep_insert差异在于并不会调用ep_ptable_queue_proc重新注册

  5. 如果获取到的revents中有用户关注的事件,并且epi未在ready链表中,那么把epi插入ready链表尾部 list_add_tail(&epi->rdllink, &ep->rdllist);并尝试唤醒epoll_wait进程wake_up_locked(&ep->wq);以及file->poll()等待进程ep_poll_safewake(&ep->poll_wait)

三、epoll_wait&epoll_pwait

SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,int, maxevents, int, timeout)

sys_epoll_pwait->sys_epoll_wait

sys_epoll_wait:主要做参数检查和epfd 的校验,然后通过ep_poll进行操作

ep_poll:

  1. 根据入参估计超时时间to和slack,或者设置timed_out标志位

  2. 如果epoll_wait入参定时时间为0,那么直接通过ep_events_available判断当前是否有用户感兴趣的事件发生,如果有则通过ep_send_events进行处理

  3. 如果定时时间大于0,并且当前没有用户关注的事件发生,则进行休眠,并添加到ep->wq等待队列的头部。 对等待事件描述符设置WQ_FLAG_EXCLUSIVE标志

  4. ep_poll被事件唤醒后会重新检查是否有关注事件,如果对应的事件已经被抢走,那么ep_poll会继续休眠等待。

  1. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$./nesttest
  2. -----------test8 测试epoll和accept同时等待的唤醒情况 epfd1:4,epfd:5,listen_fd:3-----------
  3. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$ss -tlnap | grep 9877
  4. LISTEN     0      128          *:9877                     *:*                   users:(("nesttest",pid=6895,fd=3),("nesttest",pid=6894,fd=3),("nesttest",pid=6893,fd=3))
  5. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
  6. epfd2 epoll_wait return:
  7. i:0,nfds:1,fd:3,sec:238
  8. epfd1 epoll_wait return:
  9. i:0,nfds:1,fd:3,sec:238
  10. accept return connfd:6,sec:238
  11. ^C
  12. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
  13. epfd2 epoll_wait return:
  14. accept return connfd:7,sec:240
  15. i:0,nfds:1,fd:3,sec:240
  16. ^C
  17. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
  18. epfd1 epoll_wait return:
  19. accept return connfd:8,sec:242
  20. i:0,nfds:1,fd:3,sec:242
  21. ^C
  22. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
  23. epfd2 epoll_wait return:
  24. accept return connfd:9,sec:244
  25. i:0,nfds:1,fd:3,sec:244
  26. ^C
  27. lybxin@Inspiron:~/MyRes/LNP/tcp/epolltest$nc  127.0.0.1 9877
  28. epfd1 epoll_wait return:
  29. i:0,nfds:1,fd:3,sec:246
  30. epfd2 epoll_wait return:
  31. i:0,nfds:1,fd:3,sec:246
  32. accept return connfd:10,sec:246
  33. ^C

select_estimate_accuracy:

  1. 估计slack,最大为MAX_SLACK(100ms),最小为current->timer_slack_ns(默认值为50000ns,即50 usec),timer_slack_ns可以通过prctl的PR_SET_TIMERSLACK选项设置

  2. nice 进程取定时时间的0.5%,普通进程取0.1%

ep_events_available:

  1. 如果ready链ep->rdllist非空或者ep->ovflist有效,则表示当前有关注的event发生

ep_scan_ready_list[ep_send_events]:

  1. epoll_wait的时候传递函数指针ep_send_events_proc给ep_scan_ready_list,epfd进行poll的时候则传递函数指针ep_read_events_proc

  2. 把ep->rdllist链接到txlist,并清空ep->rdllist,设置ep->ovflist = NULL,表示当前正在往用户空间发送数据,新事件触发的epi插入到ep->ovflist的头部,参考ep_send_events_proc函数的注释

  3. 调用传入的函数指针处理txlist

  4. 把ep->ovflist插入到ep->rdllist

  5. 设置ep->ovflist = EP_UNACTIVE_PTR; 表示当前需要往ready 链表插入事件epi

  6. 把txlist中剩余元素插入ep->rdllist

  7. 如果ready链表非空,尝试唤醒ep->wq和ep->poll_wait等待队列

ep_send_events_proc[ep_scan_ready_list]:

  1. 读取txlist中已经ready的事件,获取事件的events,复制到用户空间,复制失败则把epi重新插入到ready链表

  2. 如果设置了EPOLLONESHOT标志位,则设置epi->event.events &= EP_PRIVATE_BITS,其定义如下#define EP_PRIVATE_BITS (EPOLLWAKEUP | EPOLLONESHOT | EPOLLET),后续根据EP_PRIVATE_BITS判断不再加入ep->rdllist或者ep->ovflist。注意设置了EPOLLONESHOT触发一次后并没有删除epi,因而通过epoll_ctl进行ADD操作后会提示File exists错误。

  3. 如果设置了水平触发(没有EPOLLET标志位),那么即使已经成功把事件传递到了用户空间也会把epi重新添加到ready链表尾部,这样下次进行epoll_wait的时候可以重新检查这个epi。注意EPOLLONESHOT优先于水平触发的处理,即同时设置水平触发和EPOLLONESHOT并不会把epi添加到ready链表。

TCP源码—epoll源码及测试的更多相关文章

  1. epoll源码分析

    epoll源码分析 最近在使用libev过程中遇到一个场景:一个fd从一个ev_loop迁移到另一个ev_loop,会出现这个fd同时存在两个epoll的瞬间.不禁要问了,一个fd同时被两个epoll ...

  2. epoll源码分析(基于linux-5.1.4)

    API epoll提供给用户进程的接口有如下四个,本文基于linux-5.1.4源码详细分析每个API具体做了啥工作,通过UML时序图理清内核内部的函数调用关系. int epoll_create1( ...

  3. EventBus源码解析 源码阅读记录

    EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...

  4. Flink源码分析 - 源码构建

    原文地址:https://mp.weixin.qq.com/s?__biz=MzU2Njg5Nzk0NQ==&mid=2247483692&idx=1&sn=18cddc1ee ...

  5. Elasticsearch源码分析 - 源码构建

    原文地址:https://mp.weixin.qq.com/s?__biz=MzU2Njg5Nzk0NQ==&mid=2247483694&idx=1&sn=bd03afe5a ...

  6. Vue源码探究-源码文件组织

    Vue源码探究-源码文件组织 源码探究基于最新开发分支,当前发布版本为v2.5.17-beta.0 Vue 2.0版本的大整改不仅在于使用功能上的优化和调整,整个代码库也发生了天翻地覆的重组.可见随着 ...

  7. Flink 源码解析 —— 源码编译运行

    更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...

  8. ios源码-ios游戏源码-ios源码下载

    游戏源码   一款休闲类的音乐小游戏源码 该源码实现了一款休闲类的音乐小游戏源码,该游戏的源码很简单,而且游戏的玩法也很容易学会,只要我们点击视图中的grid,就可以 人气:2943运行环境:/Xco ...

  9. C#UDP(接收和发送源码)源码完整

    C#UDP(接收和发送源码)源码完整 最近做了一个UDP的服务接收和发送的东西.希望能对初学的朋友一点帮助. 源码如下: 一.逻辑--UdpServer.cs using System;using S ...

随机推荐

  1. docker 设置映射端口 目录挂载

    docker run -p 3092:9092 -p 3093:9093 -p 3094:9094 -p 3181:2181 --name="kafka_map_port_3092_4_31 ...

  2. BZOJ4543 Hotel加强版

    题面 $\text{BZOJ}$间接权限题 洛谷的弱化版 题解 三点距离两两相等要满足以下条件: 有一个相同的$\text{LCA}$ 所以如果存在一个点,使得另外两个点在它子树中,距离为$d$,且$ ...

  3. 3504: [Cqoi2014]危桥

    3504: [Cqoi2014]危桥 链接 分析: 首先往返的可以转化为全是“往”,那么只要将容量除以2即可. 然后S向a1连边容量为an(除以2之前为2*an),S向a2连边容量为an,b1,b2向 ...

  4. 实现对象属性的lazy-loading(延迟加载)

    一.延迟加载器LazyLoader作用:       说到延迟加载,应该经常接触到,尤其是使用Hibernate的时候,本篇将通过一个实例分析延迟加载的实现方式.LazyLoader接口继承了Call ...

  5. 实验二:ICMP重定向攻击

    -:实验原理 ICMP重定向信息是路由器向主机提供实时的路由信息,当一个主机收到ICMP重定向信息时,它就会根据这个信息来更新自己的路由表.由于缺乏必要的合法性检查,如果一个黑客想要被攻击的主机修改它 ...

  6. 洛咕 P3965 [TJOI2013]循环格

    同tjoi2010 打扫房间,每个点入度,出度都为1,可以向相邻4个点连边,但只有原来存在的边费用为0. // luogu-judger-enable-o2 #include<bits/stdc ...

  7. Properties、ResourceBundle

    两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单. Properties 该类继承Hashtable,将键值对存储在集合中.基于输 ...

  8. pycharm字体放大缩小设置

    放大设置 File —> settings—> Keymap —>在搜寻框中输入:increase —> Increase Font Size(双击) —> 在弹出的对话 ...

  9. CSS快速入门-浮动(float)

    一.float概述 浮动(float)是CSS布局常用的一个属性.它可以左右移动,直至它的外边缘碰到包含框或另一个浮动框的外边框. float被设计出来的初衷是用于文字环绕效果.如下代码: <! ...

  10. UWP 轨道视图Orbit View

    先看一下效果吧 这是我的Music Tags App里面的效果图,当然你也可以做的比我的更炫. 其实这个效果的实现来自于控件UWP Community Toolkit的OrbitView,所以大家要多 ...