netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Hechao Li <hechaol@fb.com>
Cc: bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH v3 bpf-next 1/2] bpf: add a new API libbpf_num_possible_cpus()
Date: Thu, 6 Jun 2019 15:10:19 -0700	[thread overview]
Message-ID: <CAEf4Bza5EyUoEmo-mwQup7Zc_X5zM6p+LROVd04nJ-sjv588Mg@mail.gmail.com> (raw)
In-Reply-To: <20190606193927.2489147-2-hechaol@fb.com>

On Thu, Jun 6, 2019 at 1:17 PM Hechao Li <hechaol@fb.com> wrote:
>
> Adding a new API libbpf_num_possible_cpus() that helps user with
> per-CPU map operations.
>
> Signed-off-by: Hechao Li <hechaol@fb.com>
> ---
>  tools/lib/bpf/libbpf.c   | 57 ++++++++++++++++++++++++++++++++++++++++
>  tools/lib/bpf/libbpf.h   | 16 +++++++++++
>  tools/lib/bpf/libbpf.map |  1 +
>  3 files changed, 74 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index ba89d9727137..06497c8a3372 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -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 = errno;

As Martin mentioned, you should handle len == 0 case separately, as
errno will be wrong in that case (read doesn't change errno in that
case). So something like:

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;
> +}
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 1af0d48178c8..f5e82eb2e5d4 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -454,6 +454,22 @@ bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
>  LIBBPF_API void
>  bpf_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
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index 46dcda89df21..2c6d835620d2 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -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;
> --
> 2.17.1
>

  reply	other threads:[~2019-06-06 22:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-06 19:39 [PATCH v3 bpf-next 0/2] bpf: Add a new API libbpf_num_possible_cpus() Hechao Li
2019-06-06 19:39 ` [PATCH v3 bpf-next 1/2] bpf: add " Hechao Li
2019-06-06 22:10   ` Andrii Nakryiko [this message]
2019-06-06 19:39 ` [PATCH v3 bpf-next 2/2] bpf: use libbpf_num_possible_cpus in bpftool and selftests Hechao Li
2019-06-06 20:26 ` [PATCH v3 bpf-next 0/2] bpf: Add a new API libbpf_num_possible_cpus() Song Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEf4Bza5EyUoEmo-mwQup7Zc_X5zM6p+LROVd04nJ-sjv588Mg@mail.gmail.com \
    --to=andrii.nakryiko@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=hechaol@fb.com \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).