Commit 6879c042 authored by Yonghong Song's avatar Yonghong Song Committed by Alexei Starovoitov

tools/bpf: selftests: Add bpf_iter selftests

The added test includes the following subtests:
  - test verifier change for btf_id_or_null
  - test load/create_iter/read for
    ipv6_route/netlink/bpf_map/task/task_file
  - test anon bpf iterator
  - test anon bpf iterator reading one char at a time
  - test file bpf iterator
  - test overflow (single bpf program output not overflow)
  - test overflow (single bpf program output overflows)
  - test bpf prog returning 1

The ipv6_route tests the following verifier change
  - access fields in the variable length array of the structure.

The netlink load tests the following verifier change
  - put a btf_id ptr value in a stack and accessible to
    tracing/iter programs.

The anon bpf iterator also tests link auto attach through skeleton.

  $ test_progs -n 2
  #2/1 btf_id_or_null:OK
  #2/2 ipv6_route:OK
  #2/3 netlink:OK
  #2/4 bpf_map:OK
  #2/5 task:OK
  #2/6 task_file:OK
  #2/7 anon:OK
  #2/8 anon-read-one-char:OK
  #2/9 file:OK
  #2/10 overflow:OK
  #2/11 overflow-e2big:OK
  #2/12 prog-ret-1:OK
  #2 bpf_iter:OK
  Summary: 1/12 PASSED, 0 SKIPPED, 0 FAILED
Signed-off-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200509175923.2477637-1-yhs@fb.com
parent acf61631
This diff is collapsed.
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#define START_CHAR 'a'
#include "bpf_iter_test_kern_common.h"
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#define START_CHAR 'A'
#include "bpf_iter_test_kern_common.h"
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
char _license[] SEC("license") = "GPL";
SEC("iter/task")
int dump_task(struct bpf_iter__task *ctx)
{
struct seq_file *seq = ctx->meta->seq;
struct task_struct *task = ctx->task;
int tgid;
tgid = task->tgid;
bpf_seq_write(seq, &tgid, sizeof(tgid));
return 0;
}
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
char _license[] SEC("license") = "GPL";
__u32 map1_id = 0, map2_id = 0;
__u32 map1_accessed = 0, map2_accessed = 0;
__u64 map1_seqnum = 0, map2_seqnum1 = 0, map2_seqnum2 = 0;
static volatile const __u32 print_len;
static volatile const __u32 ret1;
SEC("iter/bpf_map")
int dump_bpf_map(struct bpf_iter__bpf_map *ctx)
{
struct seq_file *seq = ctx->meta->seq;
struct bpf_map *map = ctx->map;
__u64 seq_num;
int i, ret = 0;
if (map == (void *)0)
return 0;
/* only dump map1_id and map2_id */
if (map->id != map1_id && map->id != map2_id)
return 0;
seq_num = ctx->meta->seq_num;
if (map->id == map1_id) {
map1_seqnum = seq_num;
map1_accessed++;
}
if (map->id == map2_id) {
if (map2_accessed == 0) {
map2_seqnum1 = seq_num;
if (ret1)
ret = 1;
} else {
map2_seqnum2 = seq_num;
}
map2_accessed++;
}
/* fill seq_file buffer */
for (i = 0; i < print_len; i++)
bpf_seq_write(seq, &seq_num, sizeof(seq_num));
return ret;
}
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2020 Facebook */
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
char _license[] SEC("license") = "GPL";
int count = 0;
SEC("iter/task")
int dump_task(struct bpf_iter__task *ctx)
{
struct seq_file *seq = ctx->meta->seq;
char c;
if (count < 4) {
c = START_CHAR + count;
bpf_seq_write(seq, &c, sizeof(c));
count++;
}
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment