当前位置:网站首页>网络中的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之间较小的值
边栏推荐
- Global and Chinese styrene acrylic lotion polymer development trend and prospect scale prediction report Ⓒ 2022 ~ 2028
- 软件测试中功能测试流程
- 简单的两个圆球loading加载
- VM虚拟机配置动态ip和静态ip访问
- 有没有大佬 遇到过flink监控postgresql数据库, 检查点无法使用的问题
- 王兴的无限游戏迎来“终极”一战
- Global and Chinese silicone defoamer production and marketing demand and investment forecast analysis report Ⓨ 2022 ~ 2027
- 5. Use of ly tab plug-in of header component
- flinkcdc要实时抽取oracle,对oracle要配置什么东西?
- Yarn restart applications record recovery
猜你喜欢

Operator-1 first acquaintance with operator

Terminal identification technology and management technology

简单的两个圆球loading加载

ROS2 Foxy depthai_ ROS tutorial

MHA high availability cluster deployment and failover of database

Project deployment is not difficult at all!
![[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)](/img/a0/e9e7506c9c34986dc73562539c8410.png)
[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)

终端识别技术和管理技术

启动solr报错The stack size specified is too small,Specify at least 328k

ZABBIX 6.0 source code installation and ha configuration
随机推荐
CS5268优势替代AG9321MCQ Typec多合一扩展坞方案
Asp.netcore利用dynamic简化数据库访问
codeforces -- 4B. Before an Exam
Google Earth Engine(GEE)——全球人类居住区网格数据 1975-1990-2000-2014 (P2016)
Have you ever encountered the problem that flynk monitors the PostgreSQL database and checkpoints cannot be used
王兴的无限游戏迎来“终极”一战
Router. use() requires a middleware function but got a Object
9. Use of better scroll and ref
Zabbix 6.0 源码安装以及 HA 配置
How to play with the reading and writing operations of blocking sockets?
The 14th five year plan of China's environmental protection industry and the report on the long-term goals for 2035 Ⓖ 2022 ~ 2028
7. Icons
8 popular recommended style layout
Wave animation color five pointed star loader loading JS special effects
MHA high availability cluster deployment and failover of database
Redis explores cache consistency
我花上万学带货:3天赚3元,成交靠刷单
China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028
Will it affect the original MySQL database to read the data of a MySQL table in full by flick MySQL CDC
Apache-atlas-2.2.0 independent compilation and deployment