google linux kernel tcp reuseport patch

This patch implements so_reuseport (SO_REUSEPORT socket option) forTCP and UDP. 
For TCP,so_reuseport allows multiple listener socketsto be bound to the same port.  In the
case of UDP, so_reuseport allows multiple sockets to bind to the same port.  To prevent port 
hijacking all sockets bound to the same port using so_reuseport must have the same uid.  
Received packets are distributed to multiple sockets bound to the same port using a 4-tuple 
hash.

该补丁为TCP/UDP增加了了so_reuseport(SO_REUSEPORT socket选项), so_reuseport允许
不同的listen socket绑定同一个端口号(TCP),不同的socket绑定同一个端口号(UDP),为了防止
端口劫持绑定同一端口号的这些listen socket必须有相同的uid,当数据到来时究竟选择哪一个
listen socket是通过一个4元组的hash来实现的,尽量保证各个listen socket均衡收到请求

The motivating case for so_resuseport in TCP would be something like a web server binding to 
port 80 running with multiple threads, where each thread might have it's own listener socket.  
This could be done as an alternative to other models: 1) have one listener thread which 
dispatches completed connections to workers. 2) accept on a single listener socket from multiple
threads.  In case #1 the listener threadcan easily become the bottleneck with high connection 
turn-over rate. In case #2, the proportion of connections accepted per thread tends to be uneven
under high connection load (assuming simple event loop:while (1) { accept(); process() }, wakeup 
does not promote fairness among the sockets.  We have seen the  disproportion to be as high  as 
3:1 ratio between thread accepting most connections and the one accepting the fewest.  With 
so_reusport the distribution is uniform.

TCP的so_resueport主要使用在高负载的webserver领域,webserver一般有多个线程,这些线程
都需要使用80端口号, 以前这些线程的listen socket是没有办法同时绑定80端口号的,只能用其他的
一些方法:1)使用一个线程去监听80端口号,将已完成的连接分发给各个工作线程去处理(memcache). 
2)多个线程使用同一个listen socket.
第一种方式在高并发的情况下监听线程很容易成为瓶颈. 第二种方式在高并发下每个线程accept的连
接数目会变的很不均匀,有的多有的少, 假设我们线程的处理逻辑很简单:

//************************************************************************
//while(1){
//        accept();   //线程阻塞在accept,等待底层唤醒接收新的连接, 因为
//                         // 大家使用的同一listen socket 只有一个进程被唤醒后能
//                         //成功接收新的连接
//        process();
//}
//************************************************************************

每个连接被唤醒的概率是不公平的,我们见过最多的线程和最少的之间接收的连接数目相差3倍,使用
reuseport后这些线程将会变得非常均匀.
补丁diff文件:

https://gist.github.com/fcicq/3993833
http://patchwork.ozlabs.org/patch/50430/

上面是补丁的介绍,除了扩展一些数据结构外主要更改了:
bind逻辑   如果设置了so_reuseport 选项,放开对同一端口绑定的限制,
另外就是TCP/UDP的收报流程,如TCP的SYN建连报文来时,需要在这些绑定的相同的端口的 listen 
socket 均衡的选择一个合适的,下面代码增加了查找相同端口号的listen socket 的逻辑




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值