Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2596f64c authored by John Fastabend's avatar John Fastabend Committed by Daniel Borkmann
Browse files

bpf: sockmap, add BPF_F_INGRESS tests



Add a set of tests to verify ingress flag in redirect helpers
works correctly with various msg sizes.

Signed-off-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 8934ce2f
Loading
Loading
Loading
Loading
+31 −10
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ struct bpf_map_def SEC("maps") sock_map_redir = {
	.type = BPF_MAP_TYPE_SOCKMAP,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1,
	.max_entries = 20,
};

struct bpf_map_def SEC("maps") sock_apply_bytes = {
@@ -78,6 +78,13 @@ struct bpf_map_def SEC("maps") sock_pull_bytes = {
	.max_entries = 2
};

struct bpf_map_def SEC("maps") sock_redir_flags = {
	.type = BPF_MAP_TYPE_ARRAY,
	.key_size = sizeof(int),
	.value_size = sizeof(int),
	.max_entries = 1
};


SEC("sk_skb1")
int bpf_prog1(struct __sk_buff *skb)
@@ -197,8 +204,9 @@ int bpf_prog5(struct sk_msg_md *msg)
SEC("sk_msg3")
int bpf_prog6(struct sk_msg_md *msg)
{
	int *bytes, zero = 0, one = 1;
	int *start, *end;
	int *bytes, zero = 0, one = 1, key = 0;
	int *start, *end, *f;
	__u64 flags = 0;

	bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
	if (bytes)
@@ -210,15 +218,22 @@ int bpf_prog6(struct sk_msg_md *msg)
	end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
	if (start && end)
		bpf_msg_pull_data(msg, *start, *end, 0);
	return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0);
	f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
	if (f && *f) {
		key = 2;
		flags = *f;
	}
	return bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
}

SEC("sk_msg4")
int bpf_prog7(struct sk_msg_md *msg)
{
	int err1 = 0, err2 = 0, zero = 0, one = 1;
	int *bytes, *start, *end, len1, len2;
	int err1 = 0, err2 = 0, zero = 0, one = 1, key = 0;
	int *f, *bytes, *start, *end, len1, len2;
	__u64 flags = 0;

		int err;
	bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
	if (bytes)
		err1 = bpf_msg_apply_bytes(msg, *bytes);
@@ -229,7 +244,6 @@ int bpf_prog7(struct sk_msg_md *msg)
	start = bpf_map_lookup_elem(&sock_pull_bytes, &zero);
	end = bpf_map_lookup_elem(&sock_pull_bytes, &one);
	if (start && end) {
		int err;

		bpf_printk("sk_msg2: pull(%i:%i)\n",
			   start ? *start : 0, end ? *end : 0);
@@ -241,9 +255,16 @@ int bpf_prog7(struct sk_msg_md *msg)
		bpf_printk("sk_msg2: length update %i->%i\n",
			   len1, len2);
	}
	bpf_printk("sk_msg3: redirect(%iB) err1=%i err2=%i\n",
		   len1, err1, err2);
	return bpf_msg_redirect_map(msg, &sock_map_redir, zero, 0);
	f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
	if (f && *f) {
		key = 2;
		flags = *f;
	}
	bpf_printk("sk_msg3: redirect(%iB) flags=%i err=%i\n",
		   len1, flags, err1 ? err1 : err2);
	err = bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
	bpf_printk("sk_msg3: err %i\n", err);
	return err;
}

SEC("sk_msg5")
+21 −1
Original line number Diff line number Diff line
#Test a bunch of positive cases to verify basic functionality
for prog in "--txmsg" "--txmsg_redir" "--txmsg_drop"; do
for prog in "--txmsg_redir --txmsg_ingress" "--txmsg" "--txmsg_redir" "--txmsg_redir --txmsg_ingress" "--txmsg_drop"; do
for t in "sendmsg" "sendpage"; do
for r in 1 10 100; do
	for i in 1 10 100; do
@@ -100,6 +100,16 @@ for t in "sendmsg" "sendpage"; do
	sleep 2
done

prog="--txmsg_redir --txmsg_apply 1 --txmsg_ingress"

for t in "sendmsg" "sendpage"; do
	TEST="./sockmap --cgroup /mnt/cgroup2/ -t $t -r $r -i $i -l $l $prog"
	echo $TEST
	$TEST
	sleep 2
done


# Test apply and redirect with larger value than send
r=1
i=8
@@ -113,6 +123,16 @@ for t in "sendmsg" "sendpage"; do
	sleep 2
done

prog="--txmsg_redir --txmsg_apply 2048 --txmsg_ingress"

for t in "sendmsg" "sendpage"; do
	TEST="./sockmap --cgroup /mnt/cgroup2/ -t $t -r $r -i $i -l $l $prog"
	echo $TEST
	$TEST
	sleep 2
done


# Test apply and redirect with apply that never reaches limit
r=1024
i=1
+35 −0
Original line number Diff line number Diff line
@@ -64,6 +64,7 @@ int txmsg_apply;
int txmsg_cork;
int txmsg_start;
int txmsg_end;
int txmsg_ingress;

static const struct option long_options[] = {
	{"help",	no_argument,		NULL, 'h' },
@@ -83,6 +84,7 @@ static const struct option long_options[] = {
	{"txmsg_cork",	required_argument,	NULL, 'k'},
	{"txmsg_start", required_argument,	NULL, 's'},
	{"txmsg_end",	required_argument,	NULL, 'e'},
	{"txmsg_ingress", no_argument,		&txmsg_ingress, 1 },
	{0, 0, NULL, 0 }
};

@@ -793,6 +795,39 @@ int main(int argc, char **argv)
				return err;
			}
		}

		if (txmsg_ingress) {
			int in = BPF_F_INGRESS;

			i = 0;
			err = bpf_map_update_elem(map_fd[6], &i, &in, BPF_ANY);
			if (err) {
				fprintf(stderr,
					"ERROR: bpf_map_update_elem (txmsg_ingress): %d (%s)\n",
					err, strerror(errno));
			}
			i = 1;
			err = bpf_map_update_elem(map_fd[1], &i, &p1, BPF_ANY);
			if (err) {
				fprintf(stderr,
					"ERROR: bpf_map_update_elem (p1 txmsg): %d (%s)\n",
					err, strerror(errno));
			}
			err = bpf_map_update_elem(map_fd[2], &i, &p1, BPF_ANY);
			if (err) {
				fprintf(stderr,
					"ERROR: bpf_map_update_elem (p1 redir): %d (%s)\n",
					err, strerror(errno));
			}

			i = 2;
			err = bpf_map_update_elem(map_fd[2], &i, &p2, BPF_ANY);
			if (err) {
				fprintf(stderr,
					"ERROR: bpf_map_update_elem (p2 txmsg): %d (%s)\n",
					err, strerror(errno));
			}
		}
	}

	if (txmsg_drop)