Skip to contentSkip to conversation

bpf: allow to change skb mark in test_run

PATCH Applied Nikita V. Shirokov 2 participants 2 messages Dec 18, 2019 – Dec 19, 2019

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

PATCH 1/1 bpf: allow to change skb mark in test_run

allow to pass skb's mark field into bpf_prog_test_run ctx for BPF_PROG_TYPE_SCHED_CLS prog type. that would allow to test bpf programs which are doing decision based on this field

---
 net/bpf/test_run.c                               | 10 +++++++++-
 tools/testing/selftests/bpf/prog_tests/skb_ctx.c |  5 +++++
 tools/testing/selftests/bpf/progs/test_skb_ctx.c |  1 +
 3 files changed, 15 insertions(+), 1 deletion(-)
net/bpf/test_run.c +9 -1
index 93a9c87787e0..d555c0d8657d 100644
@@ -251,7 +251,13 @@ static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
 		return 0;
 
 	/* make sure the fields we don't use are zeroed */
-	if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, priority)))
+	if (!range_is_zero(__skb, 0, offsetof(struct __sk_buff, mark)))
+		return -EINVAL;
+
+	/* mark is allowed */
+
+	if (!range_is_zero(__skb, offsetofend(struct __sk_buff, mark),
+			   offsetof(struct __sk_buff, priority)))
 		return -EINVAL;
 
 	/* priority is allowed */
@@ -274,6 +280,7 @@ static int convert___skb_to_skb(struct sk_buff *skb, struct __sk_buff *__skb)
 			   sizeof(struct __sk_buff)))
 		return -EINVAL;
 
+	skb->mark = __skb->mark;
 	skb->priority = __skb->priority;
 	skb->tstamp = __skb->tstamp;
 	memcpy(&cb->data, __skb->cb, QDISC_CB_PRIV_LEN);
@@ -301,6 +308,7 @@ static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
 	if (!__skb)
 		return;
 
+	__skb->mark = skb->mark;
 	__skb->priority = skb->priority;
 	__skb->tstamp = skb->tstamp;
 	memcpy(__skb->cb, &cb->data, QDISC_CB_PRIV_LEN);
tools/testing/selftests/bpf/prog_tests/skb_ctx.c +5 -0
index edf5e8c7d400..c6d6b685a946 100644
@@ -13,6 +13,7 @@ void test_skb_ctx(void)
 		.tstamp = 7,
 		.wire_len = 100,
 		.gso_segs = 8,
+		.mark = 9,
 	};
 	struct bpf_prog_test_run_attr tattr = {
 		.data_in = &pkt_v4,
@@ -93,4 +94,8 @@ void test_skb_ctx(void)
 		   "ctx_out_tstamp",
 		   "skb->tstamp == %lld, expected %d\n",
 		   skb.tstamp, 8);
+	CHECK_ATTR(skb.mark != 10,
+		   "ctx_out_mark",
+		   "skb->mark == %u, expected %d\n",
+		   skb.mark, 10);
 }
tools/testing/selftests/bpf/progs/test_skb_ctx.c +1 -0
index 534fbf9a7344..e18da87fe84f 100644
@@ -17,6 +17,7 @@ int process(struct __sk_buff *skb)
 	}
 	skb->priority++;
 	skb->tstamp++;
+	skb->mark++;
 
 	if (skb->wire_len != 100)
 		return 1;
Signature · 1 line
2.15.1
#1 lore raw
Quoted 7 lines · Nikita V. Shirokov

allow to pass skb's mark field into bpf_prog_test_run ctx for BPF_PROG_TYPE_SCHED_CLS prog type. that would allow to test bpf programs which are doing decision based on this field

Applied. Thanks.

Please cc bpf@vger next time.