Add a new API libbpf_num_possible_cpus()
Subsystems (7)
v6 cover Add a new API libbpf_num_possible_cpus()
Getting number of possible CPUs is commonly used for per-CPU BPF maps and perf_event_maps. Add a new API libbpf_num_possible_cpus() that helps user with per-CPU related operations and remove duplicate implementations in bpftool and selftests.
v2: Save errno before calling pr_warning in case it is changed.
v3: Make sure libbpf_num_possible_cpus never returns 0 so that user only
has to check if ret value < 0.
v4: Fix error code when reading 0 bytes from possible CPU file.
v5: Fix selftests compliation issue.
v6: Split commit to reuse libbpf_num_possible_cpus() into two commits:
One commit to remove bpf_util.h from test BPF C programs. One commit to reuse libbpf_num_possible_cpus() in bpftools and bpf_util.h.
Hechao Li (3):
bpf: add a new API libbpf_num_possible_cpus() selftests/bpf: remove bpf_util.h from BPF C progs
bpf: use libbpf_num_possible_cpus internally
tools/bpf/bpftool/common.c | 53 ++--------------- tools/lib/bpf/libbpf.c | 57 +++++++++++++++++++ tools/lib/bpf/libbpf.h | 16 ++++++ tools/lib/bpf/libbpf.map | 1 + tools/testing/selftests/bpf/bpf_endian.h | 1 + tools/testing/selftests/bpf/bpf_util.h | 37 ++---------- .../selftests/bpf/progs/sockmap_parse_prog.c | 1 - .../bpf/progs/sockmap_tcp_msg_prog.c | 2 +- .../bpf/progs/sockmap_verdict_prog.c | 1 - .../selftests/bpf/progs/test_sysctl_prog.c | 5 +- 10 files changed, 90 insertions(+), 84 deletions(-)
Signature · 1 line
2.17.1
PATCH v6 1/3 bpf: add a new API libbpf_num_possible_cpus()
Adding a new API libbpf_num_possible_cpus() that helps user with per-CPU map operations.
--- tools/lib/bpf/libbpf.c | 57 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 16 +++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 74 insertions(+)
tools/lib/bpf/libbpf.c +57 -0
@@ -3827,3 +3827,60 @@ void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear) desc->array_offset, addr); } } + +int libbpf_num_possible_cpus (void ) +{ +static const char *fcpu ="/sys/devices/system/cpu/possible" ; +int len =0 , n =0 , il =0 , ir =0 ; +unsigned int start =0 , end =0 ; +static int cpus; +char buf[128 ]; +int error =0 ; +int fd =-1 ; + +if (cpus >0 ) +return cpus; + + fd =open (fcpu,O_RDONLY ); +if (fd <0 ) { + error = errno; +pr_warning ("Failed to open file %s: %s\n" , + fcpu,strerror (error)); +return -error; + } + len =read (fd, buf,sizeof (buf)); +close (fd); +if (len <=0 ) { + error = len ? errno :EINVAL ; +pr_warning ("Failed to read # of possible cpus from %s: %s\n" , + fcpu,strerror (error)); +return -error; + } +if (len ==sizeof (buf)) { +pr_warning ("File %s size overflow\n" , fcpu); +return -EOVERFLOW ; + } + buf[len] ='\0' ; + +for (ir =0 , cpus =0 ; ir <= len; ir++) { +/* Each sub string separated by ',' has format \d+-\d+ or \d+ */ +if (buf[ir] ==',' || buf[ir] =='\0' ) { + buf[ir] ='\0' ; + n =sscanf (&buf[il],"%u-%u" , &start, &end); +if (n <=0 ) { +pr_warning ("Failed to get # CPUs from %s\n" , + &buf[il]); +return -EINVAL ; + }else if (n ==1 ) { + end = start; + } + cpus += end - start +1 ; + il = ir +1 ; + } + } +if (cpus <=0 ) { +pr_warning ("Invalid #CPUs %d from %s\n" , cpus, fcpu); +return -EINVAL ; + } +return cpus; +}
tools/lib/bpf/libbpf.h +16 -0
@@ -454,6 +454,22 @@ bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear); LIBBPF_API voidbpf_program__bpil_offs_to_addr (struct bpf_prog_info_linear *info_linear); +/* +* A helper function to get the number of possible CPUs before looking up +* per-CPU maps. Negative errno is returned on failure. +* +* Example usage: +* +* int ncpus = libbpf_num_possible_cpus(); +* if (ncpus < 0) { +* // error handling +* } +* long values[ncpus]; +* bpf_map_lookup_elem(per_cpu_map_fd, key, values); +* +*/ +LIBBPF_API int libbpf_num_possible_cpus(void); + #ifdef __cplusplus }/* extern "C" */ #endif
tools/lib/bpf/libbpf.map +1 -0
@@ -172,4 +172,5 @@ LIBBPF_0.0.4 { btf_dump__new; btf__parse_elf; bpf_object__load_xattr; + libbpf_num_possible_cpus; } LIBBPF_0.0.3;
Signature · 1 line
2.17.1
PATCH v6 2/3 selftests/bpf: remove bpf_util.h from BPF C progs
In reply to Hechao LiThough currently there is no problem including bpf_util.h in kernel space BPF C programs, in next patch in this stack, I will reuse libbpf_num_possible_cpus() in bpf_util.h thus include libbpf.h in it, which will cause BPF C programs compile error. Therefore I will first remove bpf_util.h from all test BPF programs.
This can also make it clear that bpf_util.h is a user-space utility while bpf_helpers.h is a kernel space utility.
--- tools/testing/selftests/bpf/bpf_endian.h | 1 + tools/testing/selftests/bpf/progs/sockmap_parse_prog.c | 1 - tools/testing/selftests/bpf/progs/sockmap_tcp_msg_prog.c | 2 +- tools/testing/selftests/bpf/progs/sockmap_verdict_prog.c | 1 - tools/testing/selftests/bpf/progs/test_sysctl_prog.c | 5 ++++- 5 files changed, 6 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/bpf_endian.h +1 -0
@@ -2,6 +2,7 @@#ifndef __BPF_ENDIAN__#define __BPF_ENDIAN__ +#include <linux/stddef.h> #include <linux/swab.h> /* LLVM's BPF target selects the endianness of the CPU
tools/testing/selftests/bpf/progs/sockmap_parse_prog.c +0 -1
@@ -1,6 +1,5 @@#include <linux/bpf.h> #include "bpf_helpers.h" -#include "bpf_util.h" #include "bpf_endian.h" int _version SEC("version") = 1;
tools/testing/selftests/bpf/progs/sockmap_tcp_msg_prog.c +1 -1
@@ -1,6 +1,6 @@#include <linux/bpf.h> +#include "bpf_helpers.h" -#include "bpf_util.h" #include "bpf_endian.h" int _version SEC("version") = 1;
tools/testing/selftests/bpf/progs/sockmap_verdict_prog.c +0 -1
@@ -1,6 +1,5 @@#include <linux/bpf.h> #include "bpf_helpers.h" -#include "bpf_util.h" #include "bpf_endian.h" int _version SEC("version") = 1;
tools/testing/selftests/bpf/progs/test_sysctl_prog.c +4 -1
@@ -8,7 +8,6 @@#include <linux/bpf.h> #include "bpf_helpers.h" -#include "bpf_util.h" /* Max supported length of a string with unsigned long in base 10 (pow2 - 1). */ #define MAX_ULONG_STR_LEN 0xF
@@ -16,6 +15,10 @@/* Max supported length of sysctl value string (pow2). */ #define MAX_VALUE_STR_LEN 0x40 +#ifndef ARRAY_SIZE +#define ARRAY_SIZE (x) (sizeof(x) / sizeof((x)[0])) +#endif + static __always_inline int is_tcp_mem(struct bpf_sysctl *ctx) {char tcp_mem_name[] ="net/ipv4/tcp_mem" ;
Signature · 1 line
2.17.1
PATCH v6 3/3 bpf: use libbpf_num_possible_cpus internally
In reply to Hechao LiUse the newly added bpf_num_possible_cpus() in bpftool and selftests and remove duplicate implementations.
--- tools/bpf/bpftool/common.c | 53 +++----------------------- tools/testing/selftests/bpf/bpf_util.h | 37 +++--------------- 2 files changed, 10 insertions(+), 80 deletions(-)
tools/bpf/bpftool/common.c +5 -48
@@ -21,6 +21,7 @@#include <sys/vfs.h> #include <bpf.h> +#include <libbpf.h> /* libbpf_num_possible_cpus */ #include "main.h"
@@ -439,57 +440,13 @@ unsigned int get_page_size(void)unsigned int get_possible_cpus (void ) { -static unsigned int result; -char buf[128 ]; -long int n; -char *ptr; -int fd; - -if (result) -return result; - - fd =open ("/sys/devices/system/cpu/possible" ,O_RDONLY ); -if (fd <0 ) { -p_err ("can't open sysfs possible cpus" ); -exit (-1 ); - } - - n =read (fd, buf,sizeof (buf)); -if (n <2 ) { -p_err ("can't read sysfs possible cpus" ); -exit (-1 ); - } -close (fd); +int cpus =libbpf_num_possible_cpus (); -if (n ==sizeof (buf)) { -p_err ("read sysfs possible cpus overflow" ); +if (cpus <0 ) { +p_err ("Can't get # of possible cpus: %s" ,strerror (-cpus));exit (-1 ); } - - ptr = buf; - n =0 ; -while (*ptr && *ptr !='\n' ) { -unsigned int a, b; - -if (sscanf (ptr,"%u-%u" , &a, &b) ==2 ) { - n += b - a +1 ; - - ptr =strchr (ptr,'-' ) +1 ; - }else if (sscanf (ptr,"%u" , &a) ==1 ) { - n++; - }else { -assert (0 ); - } - -while (isdigit (*ptr)) - ptr++; -if (*ptr ==',' ) - ptr++; - } - - result = n; - -return result; +return cpus; } static char *
tools/testing/selftests/bpf/bpf_util.h +5 -32
@@ -6,44 +6,17 @@#include <stdlib.h> #include <string.h> #include <errno.h> +#include <libbpf.h> /* libbpf_num_possible_cpus */ static inline unsigned int bpf_num_possible_cpus (void ) { -static const char *fcpu ="/sys/devices/system/cpu/possible" ; -unsigned int start, end, possible_cpus =0 ; -char buff[128 ]; -FILE *fp; -int len, n, i, j =0 ; +int possible_cpus =libbpf_num_possible_cpus (); - fp =fopen (fcpu,"r" ); -if (!fp) { -printf ("Failed to open %s: '%s'!\n" , fcpu,strerror (errno)); +if (possible_cpus <0 ) { +printf ("Failed to get # of possible cpus: '%s'!\n" , +strerror (-possible_cpus));exit (1 ); } - -if (!fgets (buff,sizeof (buff), fp)) { -printf ("Failed to read %s!\n" , fcpu); -exit (1 ); - } - - len =strlen (buff); -for (i =0 ; i <= len; i++) { -if (buff[i] ==',' || buff[i] =='\0' ) { - buff[i] ='\0' ; - n =sscanf (&buff[j],"%u-%u" , &start, &end); -if (n <=0 ) { -printf ("Failed to retrieve # possible CPUs!\n" ); -exit (1 ); - }else if (n ==1 ) { - end = start; - } - possible_cpus += end - start +1 ; - j = i +1 ; - } - } - -fclose (fp); -return possible_cpus; }
Signature · 1 line
2.17.1
Quoted 21 lines · Hechao Li
Getting number of possible CPUs is commonly used for per-CPU BPF maps and
perf_event_maps. Add a new APIlibbpf_num_possible_cpus()that helps user with per-CPU related operations and remove duplicate implementations in bpftool and selftests.v2: Save errno before calling
pr_warningin case it is changed.
v3: Make surelibbpf_num_possible_cpusnever returns 0 so that user only
has to check if ret value < 0.
v4: Fix error code when reading 0 bytes from possible CPU file.
v5: Fix selftests compliation issue.
v6: Split commit to reuselibbpf_num_possible_cpus()into two commits:
One commit to removebpf_util.hfrom test BPF C programs. One commit to reuselibbpf_num_possible_cpus()in bpftools andbpf_util.h.Hechao Li (3):
bpf: add a new APIlibbpf_num_possible_cpus()selftests/bpf: removebpf_util.hfrom BPF C progs
bpf: uselibbpf_num_possible_cpusinternally
Quoted 12 lines
tools/bpf/bpftool/common.c | 53 ++--------------- tools/lib/bpf/libbpf.c | 57 +++++++++++++++++++ tools/lib/bpf/libbpf.h | 16 ++++++ tools/lib/bpf/libbpf.map | 1 + tools/testing/selftests/bpf/bpf_endian.h | 1 + tools/testing/selftests/bpf/bpf_util.h | 37 ++---------- .../selftests/bpf/progs/sockmap_parse_prog.c | 1 - .../bpf/progs/sockmap_tcp_msg_prog.c | 2 +- .../bpf/progs/sockmap_verdict_prog.c | 1 - .../selftests/bpf/progs/test_sysctl_prog.c | 5 +- 10 files changed, 90 insertions(+), 84 deletions(-)
Applied, thanks!