当前位置:网站首页>网络中的listen
网络中的listen
2022-07-01 13:01:00 【为了维护世界和平_】
listen主要工作
申请和初始化接收队列,全连接接收队列和半接收队列
int __sys_listen(int fd, int backlog)
{
struct socket *sock;
int err, fput_needed;
int somaxconn;
//根据fd查找socket内核对象
sock = sockfd_lookup_light(fd, &err, &fput_needed);
if (sock) {
//获取内核somaxconn参数;net.core.somaxconn,与backlog 取其中小的
somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
if ((unsigned int)backlog > somaxconn)
backlog = somaxconn;
err = security_socket_listen(sock, backlog);
if (!err)//调用协议栈初始化listen
err = sock->ops->listen(sock, backlog);
fput_light(sock->file, fput_needed);
}
return err;
}backlog与系统里的net.core.somaxconn内核参数选择一个小的
sock->ops->listen函数
int inet_listen(struct socket *sock, int backlog)
{
struct sock *sk = sock->sk;
unsigned char old_state;
int err, tcp_fastopen;
//如果是监听状态,则允许backlog参数调整,设置全连接队列长度
WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
//不是listen状态,进入监听
if (old_state != TCP_LISTEN) {
tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
(tcp_fastopen & TFO_SERVER_ENABLE) &&
!inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
fastopen_queue_tune(sk, backlog);
tcp_fastopen_init_key_once(sock_net(sk));
}
//开始监听
err = inet_csk_listen_start(sk, backlog);
if (err)
goto out;
tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);
}
err = 0;
out:
release_sock(sk);
return err;
}全连接队列结构体
struct inet_connection_sock {
/* inet_sock has to be the first member! */
struct inet_sock icsk_inet;
struct request_sock_queue icsk_accept_queue;
...
}
struct request_sock_queue {
spinlock_t rskq_lock;
u8 rskq_defer_accept;
u32 synflood_warned;
atomic_t qlen;
atomic_t young;
//全连接队列
struct request_sock *rskq_accept_head;
struct request_sock *rskq_accept_tail;
struct fastopen_queue fastopenq; /* Check max_qlen != 0 to determine
* if TFO is enabled.
*/
};int inet_csk_listen_start(struct sock *sk, int backlog)
{
struct inet_connection_sock *icsk = inet_csk(sk);
struct inet_sock *inet = inet_sk(sk);
int err = -EADDRINUSE;
//icsk_accept_queue接收队列
//接收队列内核对象初始化
reqsk_queue_alloc(&icsk->icsk_accept_queue);
sk->sk_ack_backlog = 0;
inet_csk_delack_init(sk);
inet_sk_state_store(sk, TCP_LISTEN);
if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
inet->inet_sport = htons(inet->inet_num);
sk_dst_reset(sk);
err = sk->sk_prot->hash(sk);
if (likely(!err))
return 0;
}
inet_sk_set_state(sk, TCP_CLOSE);
return err;
}
static inline void fastopen_queue_tune(struct sock *sk, int backlog)
{
struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
int somaxconn = READ_ONCE(sock_net(sk)->core.sysctl_somaxconn);
//半连接队列长度
queue->fastopenq.max_qlen = min_t(unsigned int, backlog, somaxconn);
}总结:listen主要工作申请和初始化接收队列,全连接接收队列和半接收队列
全连接接收队列长度
backlog与net.core.somaxconn之间较小的值
边栏推荐
- 有没有大佬 遇到过flink监控postgresql数据库, 检查点无法使用的问题
- VM虚拟机配置动态ip和静态ip访问
- flinkcdc要实时抽取oracle,对oracle要配置什么东西?
- Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
- Report on the 14th five year plan and future development trend of China's integrated circuit packaging industry Ⓓ 2022 ~ 2028
- How to play with the reading and writing operations of blocking sockets?
- 北斗通信模块 北斗gps模块 北斗通信终端DTU
- 【大型电商项目开发】性能压测-压力测试基本概念&JMeter-38
- Meta enlarge again! VR new model posted on CVPR oral: read and understand voice like a human
- Vs code setting Click to open a new file window without overwriting the previous window
猜你喜欢
![[development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38](/img/50/819b9c2f69534afc6dc391c9de5f05.png)
[development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38

Simple two ball loading

Different test techniques

彩色五角星SVG动态网页背景js特效

Application of 5g industrial gateway in scientific and technological overload control; off-site joint law enforcement for over limit, overweight and overspeed

Vs code setting Click to open a new file window without overwriting the previous window

硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

Zabbix 6.0 源码安装以及 HA 配置

啟動solr報錯The stack size specified is too small,Specify at least 328k
随机推荐
1553B environment construction
Router.use() requires a middleware function but got a Object
Wang Xing's infinite game ushers in the "ultimate" battle
学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......
Development trend and market demand analysis report of China's high purity copper industry Ⓕ 2022 ~ 2028
leetcode 322. Coin change (medium)
Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
华为HMS Core携手超图为三维GIS注入新动能
不同的测试技术区分
C language learning
北斗通信模块 北斗gps模块 北斗通信终端DTU
图灵奖得主Judea Pearl:最近值得一读的19篇因果推断论文
Tencent always takes epoll, which is annoying
股票开户要认识谁?实际上网上开户安全么?
Redis exploration: cache breakdown, cache avalanche, cache penetration
MySQL Replication中的并行复制示例详解
Meta enlarge again! VR new model posted on CVPR oral: read and understand voice like a human
游戏公会在去中心化游戏中的未来
minimum spanning tree
波浪动画彩色五角星loader加载js特效