Skip to content

Commit d89b1ca

Browse files
Niklas SöderlundNobody
Niklas Söderlund
authored and
Nobody
committed
bpftool: Restore support for BPF offload-enabled feature probing
Commit 1a56c18 ("bpftool: Stop supporting BPF offload-enabled feature probing") removed the support to probe for BPF offload features. This is still something that is useful for NFP NIC that can support offloading of BPF programs. The reason for the dropped support was that libbpf starting with v1.0 would drop support for passing the ifindex to the BPF prog/map/helper feature probing APIs. In order to keep this useful feature for NFP restore the functionality by moving it directly into bpftool. The code restored is a simplified version of the code that existed in libbpf which supposed passing the ifindex. The simplification is that it only targets the cases where ifindex is given and call into libbpf for the cases where it's not. Before restoring support for probing offload features: # bpftool feature probe dev ens4np0 Scanning system call availability... bpf() syscall is available Scanning eBPF program types... Scanning eBPF map types... Scanning eBPF helper functions... eBPF helpers supported for program type sched_cls: eBPF helpers supported for program type xdp: Scanning miscellaneous eBPF features... Large program size limit is NOT available Bounded loop support is NOT available ISA extension v2 is NOT available ISA extension v3 is NOT available With support for probing offload features restored: # bpftool feature probe dev ens4np0 Scanning system call availability... bpf() syscall is available Scanning eBPF program types... eBPF program_type sched_cls is available eBPF program_type xdp is available Scanning eBPF map types... eBPF map_type hash is available eBPF map_type array is available eBPF map_type prog_array is NOT available eBPF map_type perf_event_array is NOT available eBPF map_type percpu_hash is NOT available eBPF map_type percpu_array is NOT available eBPF map_type stack_trace is NOT available eBPF map_type cgroup_array is NOT available eBPF map_type lru_hash is NOT available eBPF map_type lru_percpu_hash is NOT available eBPF map_type lpm_trie is NOT available eBPF map_type array_of_maps is NOT available eBPF map_type hash_of_maps is NOT available eBPF map_type devmap is NOT available eBPF map_type sockmap is NOT available eBPF map_type cpumap is NOT available eBPF map_type xskmap is NOT available eBPF map_type sockhash is NOT available eBPF map_type cgroup_storage is NOT available eBPF map_type reuseport_sockarray is NOT available eBPF map_type percpu_cgroup_storage is NOT available eBPF map_type queue is NOT available eBPF map_type stack is NOT available eBPF map_type sk_storage is NOT available eBPF map_type devmap_hash is NOT available eBPF map_type struct_ops is NOT available eBPF map_type ringbuf is NOT available eBPF map_type inode_storage is NOT available eBPF map_type task_storage is NOT available eBPF map_type bloom_filter is NOT available Scanning eBPF helper functions... eBPF helpers supported for program type sched_cls: - bpf_map_lookup_elem - bpf_get_prandom_u32 - bpf_perf_event_output eBPF helpers supported for program type xdp: - bpf_map_lookup_elem - bpf_get_prandom_u32 - bpf_perf_event_output - bpf_xdp_adjust_head - bpf_xdp_adjust_tail Scanning miscellaneous eBPF features... Large program size limit is NOT available Bounded loop support is NOT available ISA extension v2 is NOT available ISA extension v3 is NOT available Signed-off-by: Niklas Söderlund <[email protected]> Signed-off-by: Simon Horman <[email protected]>
1 parent b3eb4dd commit d89b1ca

File tree

1 file changed

+151
-15
lines changed

1 file changed

+151
-15
lines changed

tools/bpf/bpftool/feature.c

Lines changed: 151 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <ctype.h>
55
#include <errno.h>
6+
#include <fcntl.h>
67
#include <string.h>
78
#include <unistd.h>
89
#include <net/if.h>
@@ -45,6 +46,11 @@ static bool run_as_unprivileged;
4546

4647
/* Miscellaneous utility functions */
4748

49+
static bool grep(const char *buffer, const char *pattern)
50+
{
51+
return !!strstr(buffer, pattern);
52+
}
53+
4854
static bool check_procfs(void)
4955
{
5056
struct statfs st_fs;
@@ -135,6 +141,32 @@ static void print_end_section(void)
135141

136142
/* Probing functions */
137143

144+
static int get_vendor_id(int ifindex)
145+
{
146+
char ifname[IF_NAMESIZE], path[64], buf[8];
147+
ssize_t len;
148+
int fd;
149+
150+
if (!if_indextoname(ifindex, ifname))
151+
return -1;
152+
153+
snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname);
154+
155+
fd = open(path, O_RDONLY | O_CLOEXEC);
156+
if (fd < 0)
157+
return -1;
158+
159+
len = read(fd, buf, sizeof(buf));
160+
close(fd);
161+
if (len < 0)
162+
return -1;
163+
if (len >= (ssize_t)sizeof(buf))
164+
return -1;
165+
buf[len] = '\0';
166+
167+
return strtol(buf, NULL, 0);
168+
}
169+
138170
static int read_procfs(const char *path)
139171
{
140172
char *endptr, *line = NULL;
@@ -478,6 +510,50 @@ static bool probe_bpf_syscall(const char *define_prefix)
478510
return res;
479511
}
480512

513+
static bool
514+
probe_prog_load_ifindex(enum bpf_prog_type prog_type,
515+
const struct bpf_insn *insns, size_t insns_cnt,
516+
char *log_buf, size_t log_buf_sz,
517+
__u32 ifindex)
518+
{
519+
LIBBPF_OPTS(bpf_prog_load_opts, opts,
520+
.log_buf = log_buf,
521+
.log_size = log_buf_sz,
522+
.log_level = log_buf ? 1 : 0,
523+
.prog_ifindex = ifindex,
524+
);
525+
int fd;
526+
527+
errno = 0;
528+
fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, &opts);
529+
if (fd >= 0)
530+
close(fd);
531+
532+
return fd >= 0 && errno != EINVAL && errno != EOPNOTSUPP;
533+
}
534+
535+
static bool probe_prog_type_ifindex(enum bpf_prog_type prog_type, __u32 ifindex)
536+
{
537+
struct bpf_insn insns[2] = {
538+
BPF_MOV64_IMM(BPF_REG_0, 0),
539+
BPF_EXIT_INSN()
540+
};
541+
542+
switch (prog_type) {
543+
case BPF_PROG_TYPE_SCHED_CLS:
544+
/* nfp returns -EINVAL on exit(0) with TC offload */
545+
insns[0].imm = 2;
546+
break;
547+
case BPF_PROG_TYPE_XDP:
548+
break;
549+
default:
550+
return false;
551+
}
552+
553+
return probe_prog_load_ifindex(prog_type, insns, ARRAY_SIZE(insns),
554+
NULL, 0, ifindex);
555+
}
556+
481557
static void
482558
probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
483559
const char *define_prefix, __u32 ifindex)
@@ -488,11 +564,19 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
488564
bool res;
489565

490566
if (ifindex) {
491-
p_info("BPF offload feature probing is not supported");
492-
return;
567+
switch (prog_type) {
568+
case BPF_PROG_TYPE_SCHED_CLS:
569+
case BPF_PROG_TYPE_XDP:
570+
break;
571+
default:
572+
return;
573+
}
574+
575+
res = probe_prog_type_ifindex(prog_type, ifindex);
576+
} else {
577+
res = libbpf_probe_bpf_prog_type(prog_type, NULL);
493578
}
494579

495-
res = libbpf_probe_bpf_prog_type(prog_type, NULL);
496580
#ifdef USE_LIBCAP
497581
/* Probe may succeed even if program load fails, for unprivileged users
498582
* check that we did not fail because of insufficient permissions
@@ -521,6 +605,35 @@ probe_prog_type(enum bpf_prog_type prog_type, bool *supported_types,
521605
define_prefix);
522606
}
523607

608+
static bool probe_map_type_ifindex(enum bpf_map_type map_type, __u32 ifindex)
609+
{
610+
LIBBPF_OPTS(bpf_map_create_opts, opts);
611+
int key_size, value_size, max_entries;
612+
int fd;
613+
614+
opts.map_ifindex = ifindex;
615+
616+
key_size = sizeof(__u32);
617+
value_size = sizeof(__u32);
618+
max_entries = 1;
619+
620+
switch (map_type) {
621+
case BPF_MAP_TYPE_HASH:
622+
case BPF_MAP_TYPE_ARRAY:
623+
break;
624+
default:
625+
return false;
626+
}
627+
628+
fd = bpf_map_create(map_type, NULL, key_size, value_size, max_entries,
629+
&opts);
630+
631+
if (fd >= 0)
632+
close(fd);
633+
634+
return fd >= 0;
635+
}
636+
524637
static void
525638
probe_map_type(enum bpf_map_type map_type, const char *define_prefix,
526639
__u32 ifindex)
@@ -530,12 +643,10 @@ probe_map_type(enum bpf_map_type map_type, const char *define_prefix,
530643
size_t maxlen;
531644
bool res;
532645

533-
if (ifindex) {
534-
p_info("BPF offload feature probing is not supported");
535-
return;
536-
}
537-
538-
res = libbpf_probe_bpf_map_type(map_type, NULL);
646+
if (ifindex)
647+
res = probe_map_type_ifindex(map_type, ifindex);
648+
else
649+
res = libbpf_probe_bpf_map_type(map_type, NULL);
539650

540651
/* Probe result depends on the success of map creation, no additional
541652
* check required for unprivileged users
@@ -559,6 +670,33 @@ probe_map_type(enum bpf_map_type map_type, const char *define_prefix,
559670
define_prefix);
560671
}
561672

673+
static bool
674+
probe_helper_ifindex(enum bpf_func_id id, enum bpf_prog_type prog_type,
675+
__u32 ifindex)
676+
{
677+
struct bpf_insn insns[2] = {
678+
BPF_EMIT_CALL(id),
679+
BPF_EXIT_INSN()
680+
};
681+
char buf[4096] = {};
682+
bool res;
683+
684+
probe_prog_load_ifindex(prog_type, insns, ARRAY_SIZE(insns), buf,
685+
sizeof(buf), ifindex);
686+
res = !grep(buf, "invalid func ") && !grep(buf, "unknown func ");
687+
688+
switch (get_vendor_id(ifindex)) {
689+
case 0x19ee: /* Netronome specific */
690+
res = res && !grep(buf, "not supported by FW") &&
691+
!grep(buf, "unsupported function id");
692+
break;
693+
default:
694+
break;
695+
}
696+
697+
return res;
698+
}
699+
562700
static void
563701
probe_helper_for_progtype(enum bpf_prog_type prog_type, bool supported_type,
564702
const char *define_prefix, unsigned int id,
@@ -567,12 +705,10 @@ probe_helper_for_progtype(enum bpf_prog_type prog_type, bool supported_type,
567705
bool res = false;
568706

569707
if (supported_type) {
570-
if (ifindex) {
571-
p_info("BPF offload feature probing is not supported");
572-
return;
573-
}
574-
575-
res = libbpf_probe_bpf_helper(prog_type, id, NULL);
708+
if (ifindex)
709+
res = probe_helper_ifindex(id, prog_type, ifindex);
710+
else
711+
res = libbpf_probe_bpf_helper(prog_type, id, NULL);
576712
#ifdef USE_LIBCAP
577713
/* Probe may succeed even if program load fails, for
578714
* unprivileged users check that we did not fail because of

0 commit comments

Comments
 (0)