Skip to content

Commit

Permalink
mini test_sockmap.c
Browse files Browse the repository at this point in the history
Signed-off-by: Geliang Tang <[email protected]>
  • Loading branch information
Geliang Tang committed May 23, 2024
1 parent fae28ee commit d5debdc
Show file tree
Hide file tree
Showing 5 changed files with 556 additions and 1,738 deletions.
16 changes: 13 additions & 3 deletions net/tls/tls_sw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1317,8 +1317,10 @@ tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
timeo = sock_rcvtimeo(sk, nonblock);

while (!tls_strp_msg_ready(ctx)) {
if (!sk_psock_queue_empty(psock))
if (!sk_psock_queue_empty(psock)) {
pr_info("%s return 0\n", __func__);
return 0;
}

if (sk->sk_err)
return sock_error(sk);
Expand All @@ -1338,8 +1340,10 @@ tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
if (sock_flag(sk, SOCK_DONE))
return 0;

if (!timeo)
if (!timeo) {
pr_info("%s return -EAGAIN\n", __func__);
return -EAGAIN;
}

released = true;
add_wait_queue(sk_sleep(sk), &wait);
Expand All @@ -1358,6 +1362,7 @@ tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,

tls_strp_msg_load(&ctx->strp, released);

pr_info("%s return 1\n", __func__);
return 1;
}

Expand Down Expand Up @@ -1973,6 +1978,8 @@ int tls_sw_recvmsg(struct sock *sk,
bool bpf_strp_enabled;
bool zc_capable;

pr_info("%s tls_sw_sock_is_readable(sk)=%u\n", __func__, tls_sw_sock_is_readable(sk));

if (unlikely(flags & MSG_ERRQUEUE))
return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);

Expand Down Expand Up @@ -2344,15 +2351,18 @@ bool tls_sw_sock_is_readable(struct sock *sk)
struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
bool ingress_empty = true;
struct sk_psock *psock;
bool ret;

rcu_read_lock();
psock = sk_psock(sk);
if (psock)
ingress_empty = list_empty(&psock->ingress_msg);
rcu_read_unlock();

return !ingress_empty || tls_strp_msg_ready(ctx) ||
ret = !ingress_empty || tls_strp_msg_ready(ctx) ||
!skb_queue_empty(&ctx->rx_list);
pr_info("%s ret=%u ingress_empty=%u\n", __func__, ret, ingress_empty);
return ret;
}

int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb)
Expand Down
3 changes: 3 additions & 0 deletions tools/testing/selftests/bpf/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gcc -o test_sockmap_standalone test_sockmap_standalone.c cgroup_helpers.c -I ~/mptcp_net-next/tools/lib/
#gcc -o test_sockmap test_sockmap.c cgroup_helpers.c -I ~/mptcp_net-next/tools/lib/ -lbpf

199 changes: 7 additions & 192 deletions tools/testing/selftests/bpf/progs/test_sockmap_kern.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,6 @@ struct {
__uint(value_size, sizeof(int));
} sock_map_redir SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, int);
} sock_apply_bytes SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, int);
} sock_cork_bytes SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 6);
__type(key, int);
__type(value, int);
} sock_bytes SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, int);
__type(value, int);
} sock_redir_flags SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 3);
Expand All @@ -92,16 +64,12 @@ struct {
__uint(value_size, sizeof(int));
} tls_sock_map SEC(".maps");

char fmt[] = "%s %d\n";

SEC("sk_skb/stream_parser")
int bpf_prog1(struct __sk_buff *skb)
{
int *f, two = 2;

f = bpf_map_lookup_elem(&sock_skb_opts, &two);
if (f && *f) {
return *f;
}
return skb->len;
return SK_PASS;
}

SEC("sk_skb/stream_verdict")
Expand All @@ -123,15 +91,13 @@ int bpf_prog2(struct __sk_buff *skb)

f = bpf_map_lookup_elem(&sock_skb_opts, &zero);
if (f && *f) {
bpf_trace_printk(fmt, sizeof(fmt), "prog2: in f=", *f);
ret = 3;
flags = *f;
}
bpf_trace_printk(fmt, sizeof(fmt), "prog2: f is null, flags=", flags);

#ifdef SOCKMAP
return bpf_sk_redirect_map(skb, &sock_map, ret, flags);
#else
return bpf_sk_redirect_hash(skb, &sock_map, &ret, flags);
#endif

}

Expand Down Expand Up @@ -161,6 +127,7 @@ int bpf_prog3(struct __sk_buff *skb)
if (f && *f) {
__u64 flags = 0;

bpf_trace_printk(fmt, sizeof(fmt), "prog3: in f=", *f);
ret = 0;
flags = *f;

Expand All @@ -171,12 +138,9 @@ int bpf_prog3(struct __sk_buff *skb)
if (err)
return SK_DROP;
bpf_write_pass(skb, 0);
#ifdef SOCKMAP
return bpf_sk_redirect_map(skb, &tls_sock_map, ret, flags);
#else
return bpf_sk_redirect_hash(skb, &tls_sock_map, &ret, flags);
#endif
}
bpf_trace_printk(fmt, sizeof(fmt), "prog3: f is null ", 0);
err = bpf_skb_adjust_room(skb, 4, 0, 0);
if (err)
return SK_DROP;
Expand All @@ -199,13 +163,8 @@ int bpf_sockmap(struct bpf_sock_ops *skops)

if (lport == 10000) {
ret = 1;
#ifdef SOCKMAP
bpf_sock_map_update(skops, &sock_map, &ret,
BPF_NOEXIST);
#else
bpf_sock_hash_update(skops, &sock_map, &ret,
BPF_NOEXIST);
#endif
}
break;
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
Expand All @@ -214,13 +173,8 @@ int bpf_sockmap(struct bpf_sock_ops *skops)

if (bpf_ntohl(rport) == 10001) {
ret = 10;
#ifdef SOCKMAP
bpf_sock_map_update(skops, &sock_map, &ret,
BPF_NOEXIST);
#else
bpf_sock_hash_update(skops, &sock_map, &ret,
BPF_NOEXIST);
#endif
}
break;
default:
Expand All @@ -233,146 +187,7 @@ int bpf_sockmap(struct bpf_sock_ops *skops)
SEC("sk_msg")
int bpf_prog4(struct sk_msg_md *msg)
{
int *bytes, zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5;
int *start, *end, *start_push, *end_push, *start_pop, *pop, err = 0;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
if (bytes)
bpf_msg_apply_bytes(msg, *bytes);
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
if (bytes)
bpf_msg_cork_bytes(msg, *bytes);
start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end)
bpf_msg_pull_data(msg, *start, *end, 0);
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push) {
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
if (err)
return SK_DROP;
}
start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
pop = bpf_map_lookup_elem(&sock_bytes, &five);
if (start_pop && pop)
bpf_msg_pop_data(msg, *start_pop, *pop, 0);
return SK_PASS;
}

SEC("sk_msg")
int bpf_prog6(struct sk_msg_md *msg)
{
int zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5, key = 0;
int *bytes, *start, *end, *start_push, *end_push, *start_pop, *pop, *f;
int err = 0;
__u64 flags = 0;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
if (bytes)
bpf_msg_apply_bytes(msg, *bytes);
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
if (bytes)
bpf_msg_cork_bytes(msg, *bytes);

start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end)
bpf_msg_pull_data(msg, *start, *end, 0);

start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push) {
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
if (err)
return SK_DROP;
}

start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
pop = bpf_map_lookup_elem(&sock_bytes, &five);
if (start_pop && pop)
bpf_msg_pop_data(msg, *start_pop, *pop, 0);

f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
if (f && *f) {
key = 2;
flags = *f;
}
#ifdef SOCKMAP
return bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
#else
return bpf_msg_redirect_hash(msg, &sock_map_redir, &key, flags);
#endif
}

SEC("sk_msg")
int bpf_prog8(struct sk_msg_md *msg)
{
void *data_end = (void *)(long) msg->data_end;
void *data = (void *)(long) msg->data;
int ret = 0, *bytes, zero = 0;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
if (bytes) {
ret = bpf_msg_apply_bytes(msg, *bytes);
if (ret)
return SK_DROP;
} else {
return SK_DROP;
}

__sink(data_end);
__sink(data);

return SK_PASS;
}

SEC("sk_msg")
int bpf_prog9(struct sk_msg_md *msg)
{
void *data_end = (void *)(long) msg->data_end;
void *data = (void *)(long) msg->data;
int ret = 0, *bytes, zero = 0;

bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
if (bytes) {
if (((__u64)data_end - (__u64)data) >= *bytes)
return SK_PASS;
ret = bpf_msg_cork_bytes(msg, *bytes);
if (ret)
return SK_DROP;
}
return SK_PASS;
}

SEC("sk_msg")
int bpf_prog10(struct sk_msg_md *msg)
{
int *bytes, *start, *end, *start_push, *end_push, *start_pop, *pop;
int zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5, err = 0;

bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
if (bytes)
bpf_msg_apply_bytes(msg, *bytes);
bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
if (bytes)
bpf_msg_cork_bytes(msg, *bytes);
start = bpf_map_lookup_elem(&sock_bytes, &zero);
end = bpf_map_lookup_elem(&sock_bytes, &one);
if (start && end)
bpf_msg_pull_data(msg, *start, *end, 0);
start_push = bpf_map_lookup_elem(&sock_bytes, &two);
end_push = bpf_map_lookup_elem(&sock_bytes, &three);
if (start_push && end_push) {
err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
if (err)
return SK_PASS;
}
start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
pop = bpf_map_lookup_elem(&sock_bytes, &five);
if (start_pop && pop)
bpf_msg_pop_data(msg, *start_pop, *pop, 0);
return SK_DROP;
}

char _license[] SEC("license") = "GPL";
Loading

0 comments on commit d5debdc

Please sign in to comment.