当前位置:网站首页>网络中的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之间较小的值
边栏推荐
- Some summary of pyqt5 learning (overview of the general meaning of some signals and methods)
- CV顶会最佳论文得主分享:好论文是怎么炼成的?
- Investment analysis and prospect prediction report of global and Chinese dimethyl sulfoxide industry Ⓦ 2022 ~ 2028
- 啟動solr報錯The stack size specified is too small,Specify at least 328k
- 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)
- Different test techniques
- Three questions about scientific entrepreneurship: timing, pain points and important decisions
- 我选的热门专业,四年后成了“天坑”
- Has anyone ever encountered this situation? When Oracle logminer is synchronized, the value of CLOB field is lost
- Tencent always takes epoll, which is annoying
猜你喜欢
Example code of second kill based on MySQL optimistic lock

MySQL statistical bill information (Part 2): data import and query

Operator-1初识Operator

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

Manage nodejs with NVM (downgrade the high version to the low version)
Reasons for MySQL reporting 1040too many connections and Solutions

Fundamentals of number theory and its code implementation

逆向调试入门-PE结构-输入表输出表05/07

codeforces -- 4B. Before an Exam

Wave animation color five pointed star loader loading JS special effects
随机推荐
Localtime can't re-enter. It's a pit
Flutter SQLite使用
Function test process in software testing
Report on the "14th five year plan" and investment strategy recommendations for China's industrial robot industry 2022 ~ 2028
Operator-1初识Operator
Detailed explanation of OSPF LSA of routing Foundation
String input function
9. Use of better scroll and ref
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......
In the next stage of digital transformation, digital twin manufacturer Youyi technology announced that it had completed a financing of more than 300 million yuan
Analysis report on production and marketing demand and investment forecast of global and Chinese diamond powder industry Ⓤ 2022 ~ 2027
La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
机器学习—性能度量
科学创业三问:关于时机、痛点与重要决策
Vs code set code auto save
Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
Declare an abstract class vehicle, which contains the private variable numofwheel and the public functions vehicle (int), horn (), setnumofwheel (int) and getnumofwheel (). Subclass mot
Investment analysis and prospect prediction report of global and Chinese p-nitrotoluene industry Ⓙ 2022 ~ 2027
Simple two ball loading