selftests/bpf: Add a few tcp helper functions and macros to bpf_tracing_net.h
authorMartin KaFai Lau <martin.lau@kernel.org>
Thu, 9 May 2024 17:50:18 +0000 (10:50 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 9 May 2024 18:13:11 +0000 (11:13 -0700)
This patch adds a few tcp related helper functions to bpf_tracing_net.h.
They will be useful for both tcp-cc and network tracing related
bpf progs. They have already been in the bpf_tcp_helpers.h. This change
is needed to retire the bpf_tcp_helpers.h and consolidate all tests
to vmlinux.h (i.e. bpf_tracing_net.h).

Some of the helpers (tcp_sk and inet_csk) are also defined in
bpf_cc_cubic.c and they are removed. While at it, remove
the vmlinux.h from bpf_cc_cubic.c. bpf_tracing_net.h (which has
vmlinux.h after this patch) is enough and will be consistent
with the other tcp-cc tests in the later patches.

The other TCP_* macro additions will be needed for the bpf_dctcp
changes in the later patch.

Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20240509175026.3423614-3-martin.lau@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/bpf_cc_cubic.c
tools/testing/selftests/bpf/progs/bpf_tracing_net.h

index 5c7697c70e2aaab53a9d0d2097aee6619897f222..2004be380683b6427fde723334f568d2d735dcf3 100644 (file)
  *    kernel functions.
  */
 
-#include "vmlinux.h"
-
+#include "bpf_tracing_net.h"
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
-#include "bpf_tracing_net.h"
 
 #define BPF_STRUCT_OPS(name, args...) \
 SEC("struct_ops/"#name) \
@@ -40,16 +38,6 @@ extern __u32 tcp_reno_undo_cwnd(struct sock *sk) __ksym;
 extern void cubictcp_acked(struct sock *sk, const struct ack_sample *sample) __ksym;
 extern void cubictcp_cong_avoid(struct sock *sk, __u32 ack, __u32 acked) __ksym;
 
-static struct inet_connection_sock *inet_csk(const struct sock *sk)
-{
-       return (struct inet_connection_sock *)sk;
-}
-
-static struct tcp_sock *tcp_sk(const struct sock *sk)
-{
-       return (struct tcp_sock *)sk;
-}
-
 static bool before(__u32 seq1, __u32 seq2)
 {
        return (__s32)(seq1-seq2) < 0;
index f9ec630dfcd5c519690a69c84f9622670c822f6c..ba4ca033458674ac6960c492ce4453cd4a1eed95 100644 (file)
@@ -2,6 +2,9 @@
 #ifndef __BPF_TRACING_NET_H__
 #define __BPF_TRACING_NET_H__
 
+#include <vmlinux.h>
+#include <bpf/bpf_core_read.h>
+
 #define AF_INET                        2
 #define AF_INET6               10
 
 #define TCP_CA_NAME_MAX                16
 #define TCP_NAGLE_OFF          1
 
+#define TCP_ECN_OK              1
+#define TCP_ECN_QUEUE_CWR       2
+#define TCP_ECN_DEMAND_CWR      4
+#define TCP_ECN_SEEN            8
+
+#define TCP_CONG_NEEDS_ECN     0x2
+
 #define ICSK_TIME_RETRANS      1
 #define ICSK_TIME_PROBE0       3
 #define ICSK_TIME_LOSS_PROBE   5
 
 #define tcp_jiffies32 ((__u32)bpf_jiffies64())
 
+static inline struct inet_connection_sock *inet_csk(const struct sock *sk)
+{
+       return (struct inet_connection_sock *)sk;
+}
+
+static inline void *inet_csk_ca(const struct sock *sk)
+{
+       return (void *)inet_csk(sk)->icsk_ca_priv;
+}
+
+static inline struct tcp_sock *tcp_sk(const struct sock *sk)
+{
+       return (struct tcp_sock *)sk;
+}
+
+static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
+{
+       return tp->snd_cwnd < tp->snd_ssthresh;
+}
+
+static inline bool tcp_is_cwnd_limited(const struct sock *sk)
+{
+       const struct tcp_sock *tp = tcp_sk(sk);
+
+       /* If in slow start, ensure cwnd grows to twice what was ACKed. */
+       if (tcp_in_slow_start(tp))
+               return tp->snd_cwnd < 2 * tp->max_packets_out;
+
+       return !!BPF_CORE_READ_BITFIELD(tp, is_cwnd_limited);
+}
+
 #endif