Skip to content

Commit a3cfe84

Browse files
vvfedorenkoborkmann
authored andcommitted
bpf: Add CHECKSUM_COMPLETE to bpf test progs
Add special flag to validate that TC BPF program properly updates checksum information in skb. Signed-off-by: Vadim Fedorenko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4ff5747 commit a3cfe84

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,8 @@ enum {
14251425
#define BPF_F_TEST_RUN_ON_CPU (1U << 0)
14261426
/* If set, XDP frames will be transmitted after processing */
14271427
#define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1)
1428+
/* If set, apply CHECKSUM_COMPLETE to skb and validate the checksum */
1429+
#define BPF_F_TEST_SKB_CHECKSUM_COMPLETE (1U << 2)
14281430

14291431
/* type for BPF_ENABLE_STATS */
14301432
enum bpf_stats_type {

net/bpf/test_run.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
983983
void *data;
984984
int ret;
985985

986-
if (kattr->test.flags || kattr->test.cpu || kattr->test.batch_size)
986+
if ((kattr->test.flags & ~BPF_F_TEST_SKB_CHECKSUM_COMPLETE) ||
987+
kattr->test.cpu || kattr->test.batch_size)
987988
return -EINVAL;
988989

989990
data = bpf_test_init(kattr, kattr->test.data_size_in,
@@ -1031,6 +1032,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10311032

10321033
skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
10331034
__skb_put(skb, size);
1035+
10341036
if (ctx && ctx->ifindex > 1) {
10351037
dev = dev_get_by_index(net, ctx->ifindex);
10361038
if (!dev) {
@@ -1066,9 +1068,19 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10661068
__skb_push(skb, hh_len);
10671069
if (is_direct_pkt_access)
10681070
bpf_compute_data_pointers(skb);
1071+
10691072
ret = convert___skb_to_skb(skb, ctx);
10701073
if (ret)
10711074
goto out;
1075+
1076+
if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
1077+
const int off = skb_network_offset(skb);
1078+
int len = skb->len - off;
1079+
1080+
skb->csum = skb_checksum(skb, off, len, 0);
1081+
skb->ip_summed = CHECKSUM_COMPLETE;
1082+
}
1083+
10721084
ret = bpf_test_run(prog, skb, repeat, &retval, &duration, false);
10731085
if (ret)
10741086
goto out;
@@ -1083,6 +1095,20 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10831095
}
10841096
memset(__skb_push(skb, hh_len), 0, hh_len);
10851097
}
1098+
1099+
if (kattr->test.flags & BPF_F_TEST_SKB_CHECKSUM_COMPLETE) {
1100+
const int off = skb_network_offset(skb);
1101+
int len = skb->len - off;
1102+
__wsum csum;
1103+
1104+
csum = skb_checksum(skb, off, len, 0);
1105+
1106+
if (csum_fold(skb->csum) != csum_fold(csum)) {
1107+
ret = -EBADMSG;
1108+
goto out;
1109+
}
1110+
}
1111+
10861112
convert_skb_to___skb(skb, ctx);
10871113

10881114
size = skb->len;

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,8 @@ enum {
14251425
#define BPF_F_TEST_RUN_ON_CPU (1U << 0)
14261426
/* If set, XDP frames will be transmitted after processing */
14271427
#define BPF_F_TEST_XDP_LIVE_FRAMES (1U << 1)
1428+
/* If set, apply CHECKSUM_COMPLETE to skb and validate the checksum */
1429+
#define BPF_F_TEST_SKB_CHECKSUM_COMPLETE (1U << 2)
14281430

14291431
/* type for BPF_ENABLE_STATS */
14301432
enum bpf_stats_type {

0 commit comments

Comments
 (0)