Skip to contentSkip to conversation

perhaps inet_csk_reqsk_queue_is_full should also allow zero backlog

DISCUSSION Zhongqiu Duan 4 participants 7 messages Dec 31, 2024 – Jan 2, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi all,

We use a proprietary library in our product, it passes hardcoded zero as the backlog of listen().
It works fine when syncookies is enabled, but when we disable syncookies by business requirement, no connection can be made.

After some investigation, the problem is focused on the inet_csk_reqsk_queue_is_full().

static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk) {
return inet_csk_reqsk_queue_len(sk) >=
READ_ONCE(sk->sk_max_ack_backlog);
}

I noticed that the stories happened to sk_acceptq_is_full() about this in the past, like
the commit c609e6a (Revert "net: correct sk_acceptq_is_full()").

Perhaps we can also avoid the problem by using ">" in the decision condition like
inet_csk_reqsk_queue_len(sk) > READ_ONCE(sk->sk_max_ack_backlog).

Best regards,
Zhongqiu

#1 lore raw
Quoted 21 lines · Zhongqiu Duan

Hi all,

We use a proprietary library in our product, it passes hardcoded zero as the backlog of listen().
It works fine when syncookies is enabled, but when we disable syncookies by business requirement, no connection can be made.

After some investigation, the problem is focused on the inet_csk_reqsk_queue_is_full().

static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk) {
return inet_csk_reqsk_queue_len(sk) >=
READ_ONCE(sk->sk_max_ack_backlog);
}

I noticed that the stories happened to sk_acceptq_is_full() about this in the past, like
the commit c609e6a (Revert "net: correct sk_acceptq_is_full()").

Quoted 6 lines

Perhaps we can also avoid the problem by using ">" in the decision condition like
inet_csk_reqsk_queue_len(sk) > READ_ONCE(sk->sk_max_ack_backlog).

Best regards,
Zhongqiu

Not sure I understand the issue you have, it seems to Work As Intended ?

If you do not post a RFC patch, it is hard to follow what is the suggestion.

#2 lore raw
Quoted 27 lines · Eric Dumazet
Quoted 25 lines · Zhongqiu Duan

Hi all,

We use a proprietary library in our product, it passes hardcoded zero as the backlog of listen().
It works fine when syncookies is enabled, but when we disable syncookies by business requirement, no connection can be made.

After some investigation, the problem is focused on the inet_csk_reqsk_queue_is_full().

static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk) {
return inet_csk_reqsk_queue_len(sk) >=
READ_ONCE(sk->sk_max_ack_backlog);
}

I noticed that the stories happened to sk_acceptq_is_full() about this in the past, like
the commit c609e6a (Revert "net: correct sk_acceptq_is_full()").

Perhaps we can also avoid the problem by using ">" in the decision condition like
inet_csk_reqsk_queue_len(sk) > READ_ONCE(sk->sk_max_ack_backlog).

Quoted 6 lines
Quoted 2 lines

Best regards,
Zhongqiu

Not sure I understand the issue you have, it seems to Work As Intended ?

If you do not post a RFC patch, it is hard to follow what is the suggestion.

I mean, when the backlog of listen() is set to zero, sk_acceptq_is_full() allows a connection to be made (with syncookies working), but inet_csk_reqsk_queue_is_full() does not.

Jason's reply to this thread describes this issue in more detail.

Thanks,
Zhongqiu

#3 lore raw
In reply to Zhongqiu Duan
Quoted 7 lines · Zhongqiu Duan

Hi all,

We use a proprietary library in our product, it passes hardcoded zero as the backlog of listen().
It works fine when syncookies is enabled, but when we disable syncookies by business requirement, no connection can be made.

I'm not that sure that the problem you encountered is the same as mine. I manage to reproduce it locally after noticing your report:
1) write the simplest c code with passing 0 as the backlog
2) adjust the value of net.ipv4.tcp_syncookies to see the different results

When net.ipv4.tcp_syncookies is set zero only, the connection will not be established.

Quoted 10 lines

After some investigation, the problem is focused on the inet_csk_reqsk_queue_is_full().

static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk) {
return inet_csk_reqsk_queue_len(sk) >=
READ_ONCE(sk->sk_max_ack_backlog);
}

Quoted 7 lines

I noticed that the stories happened to sk_acceptq_is_full() about this in the past, like
the commit c609e6a (Revert "net: correct sk_acceptq_is_full()").

Perhaps we can also avoid the problem by using ">" in the decision condition like
inet_csk_reqsk_queue_len(sk) > READ_ONCE(sk->sk_max_ack_backlog).

According to the experiment I conducted, I agree the above triggers the drop in tcp_conn_request(). When that sysctl is set to zero, the return value of tcp_syn_flood_action() is false, which leads to an immediate drop.

Your changes in tcp_conn_request() can solve this issue, but you're solving a not that valid issue which can be handled in a decent way as below [1]. I can't see any good reason for passing zero as a backlog value in listen() since the sk_max_ack_backlog would be zero for sure.

[1]
I would also suggest trying the following two steps first like other people do:
1) pass a larger backlog number when calling listen().
2) adjust the sysctl net.core.somaxconn, say, a much larger one, like 40960

Thanks,
Jason

#4 lore raw
Quoted 10 lines · Jason Xing
Quoted 7 lines · Zhongqiu Duan

Hi all,

We use a proprietary library in our product, it passes hardcoded zero as the backlog of listen().
It works fine when syncookies is enabled, but when we disable syncookies by business requirement, no connection can be made.

Quoted 8 lines

I'm not that sure that the problem you encountered is the same as mine. I manage to reproduce it locally after noticing your report:
1) write the simplest c code with passing 0 as the backlog
2) adjust the value of net.ipv4.tcp_syncookies to see the different results

When net.ipv4.tcp_syncookies is set zero only, the connection will not be established.

Yes, that's the problem I want to describe.

Quoted 28 lines
Quoted 10 lines

After some investigation, the problem is focused on the inet_csk_reqsk_queue_is_full().

static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk) {
return inet_csk_reqsk_queue_len(sk) >=
READ_ONCE(sk->sk_max_ack_backlog);
}

Quoted 7 lines

I noticed that the stories happened to sk_acceptq_is_full() about this in the past, like
the commit c609e6a (Revert "net: correct sk_acceptq_is_full()").

Perhaps we can also avoid the problem by using ">" in the decision condition like
inet_csk_reqsk_queue_len(sk) > READ_ONCE(sk->sk_max_ack_backlog).

According to the experiment I conducted, I agree the above triggers the drop in tcp_conn_request(). When that sysctl is set to zero, the return value of tcp_syn_flood_action() is false, which leads to an immediate drop.

Your changes in tcp_conn_request() can solve this issue, but you're solving a not that valid issue which can be handled in a decent way as below [1]. I can't see any good reason for passing zero as a backlog value in listen() since the sk_max_ack_backlog would be zero for sure.

Quoted 7 lines

[1]
I would also suggest trying the following two steps first like other people do:
1) pass a larger backlog number when calling listen().
2) adjust the sysctl net.core.somaxconn, say, a much larger one, like 40960

Thanks,
Jason

Even though only one connection is needed for this proprietary library to work properly, I don't see any reason to set the backlog to zero either. But it just happened. We simply bin patch the 3rd party library to set a larger value for the backlog as a workaround.

Thanks for your suggestions, and I almost totally agree with you. I just want to discuss whether it should and deserves to make some changes in the kernel to keep the same behavior between sk_acceptq_is_full() and inet_csk_reqsk_queue_is_full().

Regards,
Zhongqiu

#5 lore raw

From: Zhongqiu Duan <[email protected]>
Date: Wed, 1 Jan 2025 23:02:56 +0800

Quoted 55 lines · Jason Xing
Quoted 10 lines · Zhongqiu Duan
Quoted 7 lines

Hi all,

We use a proprietary library in our product, it passes hardcoded zero as the backlog of listen().
It works fine when syncookies is enabled, but when we disable syncookies by business requirement, no connection can be made.

Quoted 8 lines

I'm not that sure that the problem you encountered is the same as mine. I manage to reproduce it locally after noticing your report:
1) write the simplest c code with passing 0 as the backlog
2) adjust the value of net.ipv4.tcp_syncookies to see the different results

When net.ipv4.tcp_syncookies is set zero only, the connection will not be established.

Yes, that's the problem I want to describe.

Quoted 33 lines
Quoted 10 lines

After some investigation, the problem is focused on the inet_csk_reqsk_queue_is_full().

static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk) {
return inet_csk_reqsk_queue_len(sk) >=
READ_ONCE(sk->sk_max_ack_backlog);
}

Quoted 7 lines

I noticed that the stories happened to sk_acceptq_is_full() about this in the past, like
the commit c609e6a (Revert "net: correct sk_acceptq_is_full()").

Perhaps we can also avoid the problem by using ">" in the decision condition like
inet_csk_reqsk_queue_len(sk) > READ_ONCE(sk->sk_max_ack_backlog).

According to the experiment I conducted, I agree the above triggers the drop in tcp_conn_request(). When that sysctl is set to zero, the return value of tcp_syn_flood_action() is false, which leads to an immediate drop.

Your changes in tcp_conn_request() can solve this issue, but you're solving a not that valid issue which can be handled in a decent way as below [1]. I can't see any good reason for passing zero as a backlog value in listen() since the sk_max_ack_backlog would be zero for sure.

[1]
I would also suggest trying the following two steps first like other people do:
1) pass a larger backlog number when calling listen().
2) adjust the sysctl net.core.somaxconn, say, a much larger one, like 40960

Quoted 7 lines
Quoted 2 lines

Thanks,
Jason

Even though only one connection is needed for this proprietary library to work properly, I don't see any reason to set the backlog to zero either. But it just happened. We simply bin patch the 3rd party library to set a larger value for the backlog as a workaround.

A common technique is to specify -1 for listen() backlog.

Then you even need not know somaxconn but can use it as the max backlog. (see __sys_listen_socket())

This is especially useful in a container env where app is not allowed to read sysctl knobs.

Quoted 5 lines

Thanks for your suggestions, and I almost totally agree with you. I just want to discuss whether it should and deserves to make some changes in the kernel to keep the same behavior between sk_acceptq_is_full() and inet_csk_reqsk_queue_is_full().

I think you can post a patch to make it consistent with 64a146513f8f:

---8<---
commit 64a146513f8f12ba204b7bf5cb7e9505594ead42

Author: David S. Miller <[email protected]>
Date:   Tue Mar 6 11:21:05 2007 -0800

[NET]: Revert incorrect accept queue backlog changes.
...
A backlog value of N really does mean allow "N + 1" connections to queue to a listening socket. This allows one to specify "0" as the backlog and still get 1 connection.
---8<---

#6 lore raw
Quoted 66 lines · Kuniyuki Iwashima

From: Zhongqiu Duan <[email protected]>
Date: Wed, 1 Jan 2025 23:02:56 +0800

Quoted 55 lines · Jason Xing
Quoted 10 lines · Zhongqiu Duan
Quoted 7 lines

Hi all,

We use a proprietary library in our product, it passes hardcoded zero as the backlog of listen().
It works fine when syncookies is enabled, but when we disable syncookies by business requirement, no connection can be made.

Quoted 8 lines

I'm not that sure that the problem you encountered is the same as mine. I manage to reproduce it locally after noticing your report:
1) write the simplest c code with passing 0 as the backlog
2) adjust the value of net.ipv4.tcp_syncookies to see the different results

When net.ipv4.tcp_syncookies is set zero only, the connection will not be established.

Yes, that's the problem I want to describe.

Quoted 33 lines
Quoted 10 lines

After some investigation, the problem is focused on the inet_csk_reqsk_queue_is_full().

static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk) {
return inet_csk_reqsk_queue_len(sk) >=
READ_ONCE(sk->sk_max_ack_backlog);
}

Quoted 7 lines

I noticed that the stories happened to sk_acceptq_is_full() about this in the past, like
the commit c609e6a (Revert "net: correct sk_acceptq_is_full()").

Perhaps we can also avoid the problem by using ">" in the decision condition like
inet_csk_reqsk_queue_len(sk) > READ_ONCE(sk->sk_max_ack_backlog).

According to the experiment I conducted, I agree the above triggers the drop in tcp_conn_request(). When that sysctl is set to zero, the return value of tcp_syn_flood_action() is false, which leads to an immediate drop.

Your changes in tcp_conn_request() can solve this issue, but you're solving a not that valid issue which can be handled in a decent way as below [1]. I can't see any good reason for passing zero as a backlog value in listen() since the sk_max_ack_backlog would be zero for sure.

[1]
I would also suggest trying the following two steps first like other people do:
1) pass a larger backlog number when calling listen().
2) adjust the sysctl net.core.somaxconn, say, a much larger one, like 40960

Quoted 7 lines
Quoted 2 lines

Thanks,
Jason

Even though only one connection is needed for this proprietary library to work properly, I don't see any reason to set the backlog to zero either. But it just happened. We simply bin patch the 3rd party library to set a larger value for the backlog as a workaround.

Quoted 9 lines

A common technique is to specify -1 for listen() backlog.

Then you even need not know somaxconn but can use it as the max backlog. (see __sys_listen_socket())

This is especially useful in a container env where app is not allowed to read sysctl knobs.

Thanks for sharing this information I do not know.

Quoted 13 lines
Quoted 5 lines

Thanks for your suggestions, and I almost totally agree with you. I just want to discuss whether it should and deserves to make some changes in the kernel to keep the same behavior between sk_acceptq_is_full() and inet_csk_reqsk_queue_is_full().

I think you can post a patch to make it consistent with 64a146513f8f:

---8<---
commit 64a146513f8f12ba204b7bf5cb7e9505594ead42

Author: David S. Miller <[email protected]>
Date:   Tue Mar 6 11:21:05 2007 -0800
Quoted 6 lines

[NET]: Revert incorrect accept queue backlog changes.
...
A backlog value of N really does mean allow "N + 1" connections to queue to a listening socket. This allows one to specify "0" as the backlog and still get 1 connection.
---8<---

Okay, I will post a patch later.

Best regards,
Zhongqiu