Skip to contentSkip to conversation

Fix missing synack in BPF cgroup_skb filters

PATCH Superseded Kui-Feng Lee 4 participants 13 messages Jun 20, 2023 – Jun 23, 2023

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix missing synack in BPF cgroup_skb filters

TCP SYN/ACK packets of connections from processes/sockets outside a cgroup on the same host are not received by the cgroup's installed cgroup_skb filters.

There were two BPF cgroup_skb programs attached to a cgroup named "my_cgroup".

    SEC("cgroup_skb/ingress")
    int ingress(struct __sk_buff *skb)
    {
        /* .... process skb ... */
        return 1;
    }

    SEC("cgroup_skb/egress")
    int egress(struct __sk_buff *skb)
    {
        /* .... process skb ... */
        return 1;
    
    }

We discovered that when running the command "nc -6 -l 8000" in "my_group" and connecting to it from outside of "my_cgroup" with the command "nc -6 localhost 8000", the egress filter did not detect the SYN/ACK packet. However, we did observe the SYN/ACK packet at the ingress when connecting from a socket in "my_cgroup" to a socket outside of it.

We came across BPF_CGROUP_RUN_PROG_INET_EGRESS(). This macro is responsible for calling BPF programs that are attached to the egress hook of a cgroup and it skips programs if the sending socket is not the owner of the skb. Specifically, in our situation, the SYN/ACK skb is owned by a struct request_sock instance, but the sending socket is the listener socket we use to receive incoming connections. The request_sock is created to manage an incoming connection.

It has been determined that checking the owner of a skb against the sending socket is not required. Removing this check will allow the filters to receive SYN/ACK packets.

To ensure that cgroup_skb filters can receive all signaling packets, including SYN, SYN/ACK, ACK, FIN, and FIN/ACK. A new self-test has been added as well.

Changes from v2:

- Remove redundant blank lines.

Changes from v1:

- Check the number of observed packets instead of just sleeping.

- Use ASSERT_XXX() instead of CHECK()/

[v1] https://lore.kernel.org/all/[email protected]/
[v2] https://lore.kernel.org/all/[email protected]/

Kui-Feng Lee (2):
net: bpf: Always call BPF cgroup filters for egress. selftests/bpf: Verify that the cgroup_skb filters receive expected
packets.

 include/linux/bpf-cgroup.h                    |   2 +-
 tools/testing/selftests/bpf/cgroup_helpers.c  |  12 +
 tools/testing/selftests/bpf/cgroup_helpers.h  |   1 +
 tools/testing/selftests/bpf/cgroup_tcp_skb.h  |  35 ++
 .../selftests/bpf/prog_tests/cgroup_tcp_skb.c | 399 ++++++++++++++++++
 .../selftests/bpf/progs/cgroup_tcp_skb.c      | 382 +++++++++++++++++
 6 files changed, 830 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/cgroup_tcp_skb.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/2 net: bpf: Always call BPF cgroup filters for egress.

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through. In another words, skb->sk should point to the sock that it is sending through its egress. However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

---
 include/linux/bpf-cgroup.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bpf-cgroup.h +1 -1
index 57e9e109257e..e656da531f9f 100644
@@ -199,7 +199,7 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
 #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb)			       \
 ({									       \
 	int __ret = 0;							       \
-	if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk && sk == skb->sk) { \
+	if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {		       \
 		typeof(sk) __sk = sk_to_full_sk(sk);			       \
 		if (sk_fullsock(__sk) &&				       \
 		    cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))	       \
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through. In another words, skb->sk should point to the sock that it is sending through its egress. However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

The original patch which introduced 'sk == skb->sk' is
3007098494be cgroup: add support for eBPF programs
There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

Quoted 19 lines
---
  include/linux/bpf-cgroup.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bpf-cgroup.h +1 -1
index 57e9e109257e..e656da531f9f 100644
@@ -199,7 +199,7 @@ static inline bool cgroup_bpf_sock_enabled(struct sock *sk,
  #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb)			       \
  ({									       \
  	int __ret = 0;							       \
-	if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk && sk == skb->sk) { \
+	if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {		       \
  		typeof(sk) __sk = sk_to_full_sk(sk);			       \
  		if (sk_fullsock(__sk) &&				       \
  		    cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))	       \
#3 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

Quoted 13 lines · Yonghong Song
Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through.  In another words, skb->sk should point to the sock that it is sending through its egress.  However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

Quoted 8 lines

The original patch which introduced 'sk == skb->sk' is
  3007098494be  cgroup: add support for eBPF programs
There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

I will put it down on the next version.

Quoted 23 lines
Quoted 22 lines

---
  include/linux/bpf-cgroup.h | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bpf-cgroup.h +1 -1
index 57e9e109257e..e656da531f9f 100644
@@ -199,7 +199,7 @@ static inline bool cgroup_bpf_sock_enabled(struct 
 sock *sk,
   #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb)                   \
   ({                                           \
       int __ret = 0;                                   \
-    if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk && sk == 
 skb->sk) { \
+    if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {               \
           typeof(sk) __sk = sk_to_full_sk(sk);                   \

          if (sk_fullsock(__sk) &&                       \
              cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))           \

#4 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

In reply to Yonghong Song
Quoted 13 lines · Yonghong Song
Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through.  In another words, skb->sk should point to the sock that it is sending through its egress.  However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

Quoted 8 lines

The original patch which introduced 'sk == skb->sk' is
  3007098494be  cgroup: add support for eBPF programs
There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

After checking the code and the Alexei's comment[1] again, this check may be different from what I thought. In another post[2], Daniel Borkmann mentioned

Wouldn't that mean however, when you go through stacked devices that you'd run the same eBPF cgroup program for skb->sk multiple times?

I read this paragraph several times.
This check ensures the filters are only called for the device on the top of a stack. So, I probably should change the check to

    sk == skb_to_full_sk(skb)

instead of removing it. If we remove the check, egress filters could be called multiple times for a skb, just like what Daniel said.

Does that make sense?

[1] https://lore.kernel.org/all/CAADnVQKi0c=Mf3b=z43=b6n2xBVhwPw4QoV_au5+pFE29iLkaQ@mail.gmail.com/
[2] https://lore.kernel.org/all/[email protected]/

Quoted 23 lines
Quoted 22 lines

---
  include/linux/bpf-cgroup.h | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bpf-cgroup.h +1 -1
index 57e9e109257e..e656da531f9f 100644
@@ -199,7 +199,7 @@ static inline bool cgroup_bpf_sock_enabled(struct 
 sock *sk,
   #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb)                   \
   ({                                           \
       int __ret = 0;                                   \
-    if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk && sk == 
 skb->sk) { \
+    if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {               \
           typeof(sk) __sk = sk_to_full_sk(sk);                   \

          if (sk_fullsock(__sk) &&                       \
              cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))           \

#5 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

Quoted 29 lines · Kui-Feng Lee
Quoted 13 lines · Yonghong Song
Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through.  In another words, skb->sk should point to the sock that it is sending through its egress.  However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

Quoted 8 lines

The original patch which introduced 'sk == skb->sk' is 3007098494be  cgroup: add support for eBPF programs There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

After checking the code and the Alexei's comment[1] again, this check may be different from what I thought. In another post[2], Daniel Borkmann mentioned

Quoted 8 lines

    Wouldn't that mean however, when you go through stacked devices that you'd run the same eBPF cgroup program for skb->sk multiple times?

I read this paragraph several times.
This check ensures the filters are only called for the device on the top of a stack.  So, I probably should change the check to

    sk == skb_to_full_sk(skb)

I think this should work. It exactly covers your use case:
they are owned by a request_sock but sent through
the listening sock, that is the socket listening incoming connections
and sk == skb->sk for non request_sock/listening_sock case.

I originally though whether you could do
sk == skb->sk || skb->sk->sk_state == TCP_NEW_SYN_RECV
but obviously your approach is better.

Quoted 32 lines

instead of removing it.  If we remove the check, egress filters could be called multiple times for a skb, just like what Daniel said.

Does that make sense?

[1] https://lore.kernel.org/all/CAADnVQKi0c=Mf3b=z43=b6n2xBVhwPw4QoV_au5+pFE29iLkaQ@mail.gmail.com/
[2] https://lore.kernel.org/all/[email protected]/

Quoted 23 lines
Quoted 22 lines

---
  include/linux/bpf-cgroup.h | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bpf-cgroup.h +1 -1
index 57e9e109257e..e656da531f9f 100644
@@ -199,7 +199,7 @@ static inline bool cgroup_bpf_sock_enabled(struct 
 sock *sk,
   #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb)                   \
   ({                                           \
       int __ret = 0;                                   \
-    if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk && sk == 
 skb->sk) { \
+    if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {               \
           typeof(sk) __sk = sk_to_full_sk(sk);                   \

          if (sk_fullsock(__sk) &&                       \
              cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))           \

maintainer
#6 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

Quoted 33 lines · Yonghong Song
Quoted 32 lines · Kui-Feng Lee
Quoted 11 lines · Yonghong Song
Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through.  In another words, skb->sk should point to the sock that it is sending through its egress.  However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

Quoted 8 lines

The original patch which introduced 'sk == skb->sk' is 3007098494be  cgroup: add support for eBPF programs There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

After checking the code and the Alexei's comment[1] again, this check may be different from what I thought. In another post[2], Daniel Borkmann mentioned

     Wouldn't that mean however, when you go through stacked devices that you'd run the same eBPF cgroup program for skb->sk multiple times?

I read this paragraph several times.
This check ensures the filters are only called for the device on the top of a stack.  So, I probably should change the check to

Quoted 6 lines
Quoted 1 line

     sk == skb_to_full_sk(skb)

I think this should work. It exactly covers your use case:
  they are owned by a request_sock but sent through
  the listening sock, that is the socket listening incoming connections
and sk == skb->sk for non request_sock/listening_sock case.

Just a thought, should the test look like the below?

        int __ret = 0;                                                         \
        if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {                    \
                typeof(sk) __sk = sk_to_full_sk(sk);                           \
                if (sk_fullsock(__sk) && __sk == skb_to_full_sk(skb) &&        \
                    cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))         \
                        __ret = __cgroup_bpf_run_filter_skb(__sk, skb,         \
                                                      CGROUP_INET_EGRESS); \
        }                                                                      \

Iow, we do already convert __sk to full sk, so we should then also use that for the test with skb_to_full_sk(skb).

Thanks,
Daniel

#7 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

Quoted 56 lines · Daniel Borkmann
Quoted 33 lines · Yonghong Song
Quoted 32 lines · Kui-Feng Lee
Quoted 11 lines · Yonghong Song
Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through.  In another words, skb->sk should point to the sock that it is sending through its egress.  However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

Quoted 8 lines

The original patch which introduced 'sk == skb->sk' is 3007098494be  cgroup: add support for eBPF programs There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

After checking the code and the Alexei's comment[1] again, this check may be different from what I thought. In another post[2], Daniel Borkmann mentioned

     Wouldn't that mean however, when you go through stacked devices that you'd run the same eBPF cgroup program for skb->sk multiple times?

I read this paragraph several times.
This check ensures the filters are only called for the device on the top of a stack.  So, I probably should change the check to

Quoted 6 lines
Quoted 1 line

     sk == skb_to_full_sk(skb)

I think this should work. It exactly covers your use case:
   they are owned by a request_sock but sent through
   the listening sock, that is the socket listening incoming connections and sk == skb->sk for non request_sock/listening_sock case.

Just a thought, should the test look like the below?

        int __ret =
0;                                                         \
        if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk)
{                    \
                typeof(sk) __sk =
sk_to_full_sk(sk);                           \
                if (sk_fullsock(__sk) && __sk == skb_to_full_sk(skb)
&&        \
                    cgroup_bpf_sock_enabled(__sk,
CGROUP_INET_EGRESS))         \
                        __ret = __cgroup_bpf_run_filter_skb(__sk,
skb,         \

Quoted 6 lines

CGROUP_INET_EGRESS); \

}                                                                      \

Iow, we do already convert __sk to full sk, so we should then also use that for the test with skb_to_full_sk(skb).

Agree!

Quoted 3 lines

Thanks,
Daniel

maintainer
#8 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

Quoted 53 lines · Kui-Feng Lee
Quoted 52 lines · Daniel Borkmann
Quoted 33 lines · Yonghong Song
Quoted 32 lines · Kui-Feng Lee
Quoted 11 lines · Yonghong Song
Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through.  In another words, skb->sk should point to the sock that it is sending through its egress.  However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

Quoted 8 lines

The original patch which introduced 'sk == skb->sk' is 3007098494be  cgroup: add support for eBPF programs There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

After checking the code and the Alexei's comment[1] again, this check may be different from what I thought. In another post[2], Daniel Borkmann mentioned

     Wouldn't that mean however, when you go through stacked devices that you'd run the same eBPF cgroup program for skb->sk multiple times?

I read this paragraph several times.
This check ensures the filters are only called for the device on the top of a stack.  So, I probably should change the check to

Quoted 6 lines
Quoted 1 line

     sk == skb_to_full_sk(skb)

I think this should work. It exactly covers your use case:
   they are owned by a request_sock but sent through
   the listening sock, that is the socket listening incoming connections and sk == skb->sk for non request_sock/listening_sock case.

Just a thought, should the test look like the below?

         int __ret = 0;                                                         \
         if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {                    \
                 typeof(sk) __sk = sk_to_full_sk(sk);                           \
                 if (sk_fullsock(__sk) && __sk == skb_to_full_sk(skb) &&        \
                     cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))         \
                         __ret = __cgroup_bpf_run_filter_skb(__sk, skb,         \
CGROUP_INET_EGRESS); \
}                                                                      \

Quoted 4 lines
Quoted 2 lines

Iow, we do already convert __sk to full sk, so we should then also use that for the test with skb_to_full_sk(skb).

Agree!

It would also be useful to do an in-depth analysis for the commit msg in which cases the sk == skb->sk matches and sk was not a full sock (but __sk is) given the __sk = sk_to_full_sk(sk) exists in the code to document which situation this is covering in the existing code (... perhaps it used to work back then for synack just that later changes altered it without anyone noticing until now).

Thanks,
Daniel

#9 lore raw

Re: [PATCH bpf-next v3 1/2] net: bpf: Always call BPF cgroup filters for egress.

Quoted 57 lines · Daniel Borkmann
Quoted 56 lines · Kui-Feng Lee
Quoted 54 lines · Daniel Borkmann
Quoted 33 lines · Yonghong Song
Quoted 32 lines · Kui-Feng Lee
Quoted 11 lines · Yonghong Song
Quoted 9 lines · Kui-Feng Lee

Always call BPF filters if CGROUP BPF is enabled for EGRESS without checking skb->sk against sk.

The filters were called only if skb is owned by the sock that the skb is sent out through.  In another words, skb->sk should point to the sock that it is sending through its egress.  However, the filters would miss SYNACK skbs that they are owned by a request_sock but sent through the listening sock, that is the socket listening incoming connections. This is an unnecessary restrict.

Quoted 8 lines

The original patch which introduced 'sk == skb->sk' is 3007098494be  cgroup: add support for eBPF programs There are no mentioning in commit message why 'sk == skb->sk' is needed. So it is possible that this is just restricted for use cases at that moment. Now there are use cases where 'sk != skb->sk' so removing this check can enable the new use case. Maybe you can add this into your commit message so people can understand the history of 'sk == skb->sk'.

After checking the code and the Alexei's comment[1] again, this check may be different from what I thought. In another post[2], Daniel Borkmann mentioned

     Wouldn't that mean however, when you go through stacked devices that you'd run the same eBPF cgroup program for skb->sk multiple times?

I read this paragraph several times.
This check ensures the filters are only called for the device on the top of a stack.  So, I probably should change the check to

Quoted 6 lines
Quoted 1 line

     sk == skb_to_full_sk(skb)

I think this should work. It exactly covers your use case:
   they are owned by a request_sock but sent through
   the listening sock, that is the socket listening incoming connections and sk == skb->sk for non request_sock/listening_sock case.

Just a thought, should the test look like the below?

         int __ret = 0;                                                         \
         if (cgroup_bpf_enabled(CGROUP_INET_EGRESS) && sk) {                    \
                 typeof(sk) __sk = sk_to_full_sk(sk);                           \
                 if (sk_fullsock(__sk) && __sk == skb_to_full_sk(skb) &&        \
                     cgroup_bpf_sock_enabled(__sk, CGROUP_INET_EGRESS))         \
                         __ret = __cgroup_bpf_run_filter_skb(__sk, skb,         \
CGROUP_INET_EGRESS); \
}                                                                      \

Iow, we do already convert __sk to full sk, so we should then also use that for the test with skb_to_full_sk(skb).

Quoted 7 lines
Quoted 1 line

Agree!

It would also be useful to do an in-depth analysis for the commit msg in which cases the sk == skb->sk matches and sk was not a full sock (but __sk is) given the __sk = sk_to_full_sk(sk) exists in the code to document which situation this is covering in the existing code (... perhaps it used to work back then for synack just that later changes altered it without anyone noticing until now).

I did a test that trace how a packet going through L2TP devices. I am going to include the analysis of the test and other related links of discussions in the commit log.

#10 lore raw

PATCH v3 2/2 selftests/bpf: Verify that the cgroup_skb filters receive expected packets.

In reply to Kui-Feng Lee

This test case includes four scenarios:
1. Connect to the server from outside the cgroup and close the connection from outside the cgroup.
2. Connect to the server from outside the cgroup and close the connection from inside the cgroup.
3. Connect to the server from inside the cgroup and close the connection from outside the cgroup.
4. Connect to the server from inside the cgroup and close the connection from inside the cgroup.

The test case is to verify that cgroup_skb/{egress, ingress} filters receive expected packets including SYN, SYN/ACK, ACK, FIN, and FIN/ACK.

---
 tools/testing/selftests/bpf/cgroup_helpers.c  |  12 +
 tools/testing/selftests/bpf/cgroup_helpers.h  |   1 +
 tools/testing/selftests/bpf/cgroup_tcp_skb.h  |  35 ++
 .../selftests/bpf/prog_tests/cgroup_tcp_skb.c | 399 ++++++++++++++++++
 .../selftests/bpf/progs/cgroup_tcp_skb.c      | 382 +++++++++++++++++
 5 files changed, 829 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/cgroup_tcp_skb.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c
 create mode 100644 tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c
tools/testing/selftests/bpf/cgroup_helpers.c +12 -0
index 9e95b37a7dff..2caee8423ee0 100644
@@ -277,6 +277,18 @@ int join_cgroup(const char *relative_path)
 	return join_cgroup_from_top(cgroup_path);
 }
 
+/**
+ * join_root_cgroup() - Join the root cgroup
+ *
+ * This function joins the root cgroup.
+ *
+ * On success, it returns 0, otherwise on failure it returns 1.
+ */
+int join_root_cgroup(void)
+{
+	return join_cgroup_from_top(CGROUP_MOUNT_PATH);
+}
+
 /**
  * join_parent_cgroup() - Join a cgroup in the parent process workdir
  * @relative_path: The cgroup path, relative to parent process workdir, to join
tools/testing/selftests/bpf/cgroup_helpers.h +1 -0
index f099a166c94d..5c2cb9c8b546 100644
@@ -22,6 +22,7 @@ void remove_cgroup(const char *relative_path);
 unsigned long long get_cgroup_id(const char *relative_path);
 
 int join_cgroup(const char *relative_path);
+int join_root_cgroup(void);
 int join_parent_cgroup(const char *relative_path);
 
 int setup_cgroup_environment(void);
tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +35 -0
new file mode 100644
index 000000000000..1054b3633983
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2023 Facebook */
+
+/* Define states of a socket to tracking messages sending to and from the
+ * socket.
+ *
+ * These states are based on rfc9293 with some modifications to support
+ * tracking of messages sent out from a socket. For example, when a SYN is
+ * received, a new socket is transiting to the SYN_RECV state defined in
+ * rfc9293. But, we put it in SYN_RECV_SENDING_SYN_ACK state and when
+ * SYN-ACK is sent out, it moves to SYN_RECV state. With this modification,
+ * we can track the message sent out from a socket.
+ */
+
+#ifndef __CGROUP_TCP_SKB_H__
+#define __CGROUP_TCP_SKB_H__
+
+enum {
+	INIT,
+	CLOSED,
+	SYN_SENT,
+	SYN_RECV_SENDING_SYN_ACK,
+	SYN_RECV,
+	ESTABLISHED,
+	FIN_WAIT1,
+	FIN_WAIT2,
+	CLOSE_WAIT_SENDING_ACK,
+	CLOSE_WAIT,
+	CLOSING,
+	LAST_ACK,
+	TIME_WAIT_SENDING_ACK,
+	TIME_WAIT,
+};
+
+#endif /* __CGROUP_TCP_SKB_H__ */
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +399 -0

399 lines not on this page · raw message

tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c new file +382 -0

382 lines not on this page · raw message

Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: Verify that the cgroup_skb filters receive expected packets.

Quoted 66 lines · Kui-Feng Lee

This test case includes four scenarios:
1. Connect to the server from outside the cgroup and close the connection from outside the cgroup.
2. Connect to the server from outside the cgroup and close the connection from inside the cgroup.
3. Connect to the server from inside the cgroup and close the connection from outside the cgroup.
4. Connect to the server from inside the cgroup and close the connection from inside the cgroup.

The test case is to verify that cgroup_skb/{egress, ingress} filters receive expected packets including SYN, SYN/ACK, ACK, FIN, and FIN/ACK.

---
  tools/testing/selftests/bpf/cgroup_helpers.c  |  12 +
  tools/testing/selftests/bpf/cgroup_helpers.h  |   1 +
  tools/testing/selftests/bpf/cgroup_tcp_skb.h  |  35 ++
  .../selftests/bpf/prog_tests/cgroup_tcp_skb.c | 399 ++++++++++++++++++
  .../selftests/bpf/progs/cgroup_tcp_skb.c      | 382 +++++++++++++++++
  5 files changed, 829 insertions(+)

create mode 100644 tools/testing/selftests/bpf/cgroup_tcp_skb.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c

tools/testing/selftests/bpf/cgroup_helpers.c +12 -0
index 9e95b37a7dff..2caee8423ee0 100644
@@ -277,6 +277,18 @@ int join_cgroup(const char *relative_path)
  	return join_cgroup_from_top(cgroup_path);
  }
 
+/**
+ * join_root_cgroup() - Join the root cgroup
+ *
+ * This function joins the root cgroup.
+ *
+ * On success, it returns 0, otherwise on failure it returns 1.
+ */
+int join_root_cgroup(void)
+{
+	return join_cgroup_from_top(CGROUP_MOUNT_PATH);
+}
+
  /**
   * join_parent_cgroup() - Join a cgroup in the parent process workdir
   * @relative_path: The cgroup path, relative to parent process workdir, to join
tools/testing/selftests/bpf/cgroup_helpers.h +1 -0
index f099a166c94d..5c2cb9c8b546 100644
@@ -22,6 +22,7 @@ void remove_cgroup(const char *relative_path);
  unsigned long long get_cgroup_id(const char *relative_path);
 
  int join_cgroup(const char *relative_path);
+int join_root_cgroup(void);
  int join_parent_cgroup(const char *relative_path);
 
  int setup_cgroup_environment(void);
tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +0 -0
new file mode 100644
index 000000000000..1054b3633983

tools/testing/selftests/bpf/cgroup_tcp_skb.h line 2

Quoted 2 lines
tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +2 -0
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2023 Facebook */

/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

Quoted 58 lines
tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +33 -0
+
+/* Define states of a socket to tracking messages sending to and from the
+ * socket.
+ *
+ * These states are based on rfc9293 with some modifications to support
+ * tracking of messages sent out from a socket. For example, when a SYN is
+ * received, a new socket is transiting to the SYN_RECV state defined in
+ * rfc9293. But, we put it in SYN_RECV_SENDING_SYN_ACK state and when
+ * SYN-ACK is sent out, it moves to SYN_RECV state. With this modification,
+ * we can track the message sent out from a socket.
+ */
+
+#ifndef __CGROUP_TCP_SKB_H__
+#define __CGROUP_TCP_SKB_H__
+
+enum {
+	INIT,
+	CLOSED,
+	SYN_SENT,
+	SYN_RECV_SENDING_SYN_ACK,
+	SYN_RECV,
+	ESTABLISHED,
+	FIN_WAIT1,
+	FIN_WAIT2,
+	CLOSE_WAIT_SENDING_ACK,
+	CLOSE_WAIT,
+	CLOSING,
+	LAST_ACK,
+	TIME_WAIT_SENDING_ACK,
+	TIME_WAIT,
+};
+
+#endif /* __CGROUP_TCP_SKB_H__ */
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +19 -0
new file mode 100644
index 000000000000..1b78e8ab3f02
@@ -0,0 +1,399 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Facebook */
+#include <test_progs.h>
+#include <linux/in6.h>
+#include <sys/socket.h>
+#include <sched.h>
+#include <unistd.h>
+#include "cgroup_helpers.h"
+#include "testing_helpers.h"
+#include "cgroup_tcp_skb.skel.h"
+#include "cgroup_tcp_skb.h"
+
+#define CGROUP_TCP_SKB_PATH "/test_cgroup_tcp_skb"
+
+static int install_filters(int cgroup_fd,
+			   struct bpf_link **egress_link,
+			   struct bpf_link **ingress_link,
+			   struct bpf_program *egress_prog,
+			   struct bpf_program *ingress_prog,

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c line 29

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +10 -0
+			   struct cgroup_tcp_skb *skel)
+{
+	/* Prepare filters */
+	skel->bss->g_sock_state = 0;
+	skel->bss->g_unexpected = 0;
+	*egress_link =
+		bpf_program__attach_cgroup(egress_prog,
+					   cgroup_fd);
+	if (!ASSERT_NEQ(*egress_link, NULL, "egress_link"))
+		return -1;

!ASSERT_OK_PTR(...)

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c line 34

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +5 -0
+	*ingress_link =
+		bpf_program__attach_cgroup(ingress_prog,
+					   cgroup_fd);
+	if (!ASSERT_NEQ(*ingress_link, NULL, "ingress_link"))
+		return -1;

!ASSERT_OK_PTR(...)

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +13 -0
+
+	return 0;
+}
+
+static void uninstall_filters(struct bpf_link **egress_link,
+			      struct bpf_link **ingress_link)
+{
+	bpf_link__destroy(*egress_link);
+	*egress_link = NULL;
+	bpf_link__destroy(*ingress_link);
+	*ingress_link = NULL;
+}
+

[...]

Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +5 -0
+
+static int close_connection(int *closing_fd, int *peer_fd, int *listen_fd,
+			    struct cgroup_tcp_skb *skel)
+{
+	__u32 saved_packet_count = 0;

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c line 226

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +10 -0
+	int err;
+	int i;
+
+	/* Wait for ACKs to be sent */
+	saved_packet_count = skel->bss->g_packet_count;
+	usleep(100000);		/* 0.1s */
+	while (skel->bss->g_packet_count != saved_packet_count) {
+		saved_packet_count = skel->bss->g_packet_count;
+		usleep(100000);	/* 0.1s */
+	}

Should we put a limitation in the number of loop iterations just in case that something went wrong or too slow?

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +7 -0
+
+	skel->bss->g_packet_count = 0;
+	saved_packet_count = 0;
+
+	/* Half shutdown to make sure the closing socket having a chance to
+	 * receive a FIN from the peer.
+	 */

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c line 243

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +10 -0
+	err = shutdown(*closing_fd, SHUT_WR);
+	if (!ASSERT_OK(err, "shutdown closing_fd"))
+		return -1;
+
+	/* Wait for FIN and the ACK of the FIN to be observed */
+	for (i = 0;
+	     skel->bss->g_packet_count < saved_packet_count + 2 && i < 10;
+	     i++) {
+		usleep(100000);	/* 0.1s */
+	}

'{...}' is not necessary.

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +8 -0
+	if (!ASSERT_GE(skel->bss->g_packet_count, saved_packet_count + 2,
+		       "packet_count"))
+		return -1;
+
+	saved_packet_count = skel->bss->g_packet_count;
+
+	/* Fully shutdown the connection */
+	err = close(*peer_fd);

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c line 261

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +10 -0
+	if (!ASSERT_OK(err, "close peer_fd"))
+		return -1;
+	*peer_fd = -1;
+
+	/* Wait for FIN and the ACK of the FIN to be observed */
+	for (i = 0;
+	     skel->bss->g_packet_count < saved_packet_count + 2 && i < 10;
+	     i++) {
+		usleep(100000);	/* 0.1s */
+	}

'{...}' is not necessary.

Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +15 -0
+	if (!ASSERT_GE(skel->bss->g_packet_count, saved_packet_count + 2,
+		       "packet_count"))
+		return -1;
+
+	err = close(*closing_fd);
+	if (!ASSERT_OK(err, "close closing_fd"))
+		return -1;
+	*closing_fd = -1;
+
+	close(*listen_fd);
+	*listen_fd = -1;
+
+	return 0;
+}
+

[...]

tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c line 2

Quoted 8 lines
tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c new file +2 -0
new file mode 100644
index 000000000000..372a1548798c
@@ -0,0 +1,382 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Facebook */

/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

Quoted 14 lines
tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c new file +14 -0
+#include <linux/bpf.h>
+#include <bpf/bpf_endian.h>
+#include <bpf/bpf_helpers.h>
+
+#include <linux/if_ether.h>
+#include <linux/in.h>
+#include <linux/in6.h>
+#include <linux/ipv6.h>
+#include <linux/tcp.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include "cgroup_tcp_skb.h"

[...]

#12 lore raw

Re: [PATCH bpf-next v3 2/2] selftests/bpf: Verify that the cgroup_skb filters receive expected packets.

Quoted 136 lines · Yonghong Song
Quoted 63 lines · Kui-Feng Lee

This test case includes four scenarios:
1. Connect to the server from outside the cgroup and close the connection from outside the cgroup.
2. Connect to the server from outside the cgroup and close the connection from inside the cgroup.
3. Connect to the server from inside the cgroup and close the connection from outside the cgroup.
4. Connect to the server from inside the cgroup and close the connection from inside the cgroup.

The test case is to verify that cgroup_skb/{egress, ingress} filters receive expected packets including SYN, SYN/ACK, ACK, FIN, and FIN/ACK.

---

  tools/testing/selftests/bpf/cgroup_helpers.c  |  12 +
  tools/testing/selftests/bpf/cgroup_helpers.h  |   1 +
  tools/testing/selftests/bpf/cgroup_tcp_skb.h  |  35 ++
  .../selftests/bpf/prog_tests/cgroup_tcp_skb.c | 399 ++++++++++++++++++
  .../selftests/bpf/progs/cgroup_tcp_skb.c      | 382 +++++++++++++++++
  5 files changed, 829 insertions(+)

  create mode 100644 tools/testing/selftests/bpf/cgroup_tcp_skb.h create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c create mode 100644 tools/testing/selftests/bpf/progs/cgroup_tcp_skb.c

tools/testing/selftests/bpf/cgroup_helpers.c +12 -0
index 9e95b37a7dff..2caee8423ee0 100644
@@ -277,6 +277,18 @@ int join_cgroup(const char *relative_path)
       return join_cgroup_from_top(cgroup_path);
   }
+/**
+ * join_root_cgroup() - Join the root cgroup
+ *
+ * This function joins the root cgroup.
+ *
+ * On success, it returns 0, otherwise on failure it returns 1.
+ */
+int join_root_cgroup(void)
+{
+    return join_cgroup_from_top(CGROUP_MOUNT_PATH);
+}
+
   /**
    * join_parent_cgroup() - Join a cgroup in the parent process workdir
    * @relative_path: The cgroup path, relative to parent process workdir, to join
tools/testing/selftests/bpf/cgroup_helpers.h +1 -0
index f099a166c94d..5c2cb9c8b546 100644
@@ -22,6 +22,7 @@ void remove_cgroup(const char *relative_path);
   unsigned long long get_cgroup_id(const char *relative_path);
   int join_cgroup(const char *relative_path);
+int join_root_cgroup(void);
   int join_parent_cgroup(const char *relative_path);
   int setup_cgroup_environment(void);
tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +0 -0
new file mode 100644
index 000000000000..1054b3633983

tools/testing/selftests/bpf/cgroup_tcp_skb.h line 2

Quoted 2 lines
tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +2 -0
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2023 Facebook */

/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

Quoted 65 lines
tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +2 -0
+
+/* Define states of a socket to tracking messages sending to and from 

the
+ * socket.
+ *
+ * These states are based on rfc9293 with some modifications to support
+ * tracking of messages sent out from a socket. For example, when a SYN is
+ * received, a new socket is transiting to the SYN_RECV state defined in
+ * rfc9293. But, we put it in SYN_RECV_SENDING_SYN_ACK state and when
+ * SYN-ACK is sent out, it moves to SYN_RECV state. With this modification,
+ * we can track the message sent out from a socket.
+ */

tools/testing/selftests/bpf/cgroup_tcp_skb.h new file +22 -0
+
+#ifndef __CGROUP_TCP_SKB_H__
+#define __CGROUP_TCP_SKB_H__
+
+enum {
+    INIT,
+    CLOSED,
+    SYN_SENT,
+    SYN_RECV_SENDING_SYN_ACK,
+    SYN_RECV,
+    ESTABLISHED,
+    FIN_WAIT1,
+    FIN_WAIT2,
+    CLOSE_WAIT_SENDING_ACK,
+    CLOSE_WAIT,
+    CLOSING,
+    LAST_ACK,
+    TIME_WAIT_SENDING_ACK,
+    TIME_WAIT,
+};
+
+#endif /* __CGROUP_TCP_SKB_H__ */
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +23 -0
new file mode 100644
index 000000000000..1b78e8ab3f02
@@ -0,0 +1,399 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Facebook */
+#include <test_progs.h>
+#include <linux/in6.h>
+#include <sys/socket.h>
+#include <sched.h>
+#include <unistd.h>
+#include "cgroup_helpers.h"
+#include "testing_helpers.h"
+#include "cgroup_tcp_skb.skel.h"
+#include "cgroup_tcp_skb.h"
+
+#define CGROUP_TCP_SKB_PATH "/test_cgroup_tcp_skb"
+
+static int install_filters(int cgroup_fd,
+               struct bpf_link **egress_link,
+               struct bpf_link **ingress_link,
+               struct bpf_program *egress_prog,
+               struct bpf_program *ingress_prog,
+               struct cgroup_tcp_skb *skel)
+{
+    /* Prepare filters */
+    skel->bss->g_sock_state = 0;
Quoted 8 lines

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c line 29

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +6 -0
+    skel->bss->g_unexpected = 0;
+    *egress_link =
+        bpf_program__attach_cgroup(egress_prog,
+                       cgroup_fd);
+    if (!ASSERT_NEQ(*egress_link, NULL, "egress_link"))
+        return -1;

!ASSERT_OK_PTR(...)

Sure!

Quoted 34 lines

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c line 34

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +5 -0
+    *ingress_link =
+        bpf_program__attach_cgroup(ingress_prog,
+                       cgroup_fd);
+    if (!ASSERT_NEQ(*ingress_link, NULL, "ingress_link"))
+        return -1;

!ASSERT_OK_PTR(...)

Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +13 -0
+
+    return 0;
+}
+
+static void uninstall_filters(struct bpf_link **egress_link,
+                  struct bpf_link **ingress_link)
+{
+    bpf_link__destroy(*egress_link);
+    *egress_link = NULL;
+    bpf_link__destroy(*ingress_link);
+    *ingress_link = NULL;
+}
+

[...]

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +2 -0
+
+static int close_connection(int *closing_fd, int *peer_fd, int 

*listen_fd,
+                struct cgroup_tcp_skb *skel)

tools/testing/selftests/bpf/prog_tests/cgroup_tcp_skb.c new file +7 -0
+{
+    __u32 saved_packet_count = 0;
+    int err;
+    int i;
+
+    /* Wait for ACKs to be sent */
+    saved_packet_count = skel->bss->g_packet_count;
Quoted 8 lines
Quoted 5 lines

+    usleep(100000);        /* 0.1s */
+    while (skel->bss->g_packet_count != saved_packet_count) { +        saved_packet_count = skel->bss->g_packet_count; +        usleep(100000);    /* 0.1s */
+    }

Should we put a limitation in the number of loop iterations just in case that something went wrong or too slow?

Sure! It make sense to me.

[...]