BPF List
 help / color / mirror / Atom feed
From: Dave Marchevsky <davemarchevsky@meta.com>
To: Grant Seltzer Richman <grantseltzer@gmail.com>,
	bpf <bpf@vger.kernel.org>
Subject: Re: Best way to share maps between multiple files/objects?
Date: Fri, 11 Nov 2022 02:34:06 -0500	[thread overview]
Message-ID: <91787040-3612-e847-b512-a38a3dae199e@meta.com> (raw)
In-Reply-To: <CAO658oUrud+RaV4dAWQ+JYkDttgW00xyDmsoa8-vCeknQNjVtg@mail.gmail.com>

On 11/10/22 7:32 PM, Grant Seltzer Richman wrote:
> Hi folks,
> 
> I want to organize my BPF programs so that I can load them
> individually. I want this so that if loading one fails (because of
> lack of kernel support for BPF features), I can load a fall-back
> replacement program. To do so, I've organized the BPF programs into
> their own source code files and compiled them individually. Each BPF
> program references what is supposed to be the same ringbuffer. Using
> libbpf I open them and attempt to load each in order.
> 
> My question is, how am I supposed to share maps such as ringbuffers
> between them? If I have identical map definitions in each, they have
> their own file descriptors. Is the best way to call
> `bpf_map__reuse_fd()` on each handle of the maps in each BPF object?

Sounds like each of the source files have the exact same map definitions,
including name? And each is compiled into a separate BPF object?

If so, adding __uint(pinning, LIBBPF_PIN_BY_NAME); to
each definition will probably be the easiest way to get the map reuse
behavior you want. The first bpf object in the set that successfully loads
will pin its maps by name in /sys/fs/bpf and future objects which load same
maps will reuse them instead of creating new maps.

selftests/bpf/progs/test_pinning.c demonstrates this behavior.

I'm curious, though: is this a single BPF program with various fallbacks,
with goal of running only one? Or a set of N programs working together using
same maps, each of which might have fallbacks, with goal of running some
version of all N programs?

> I'd also take advice on how to better achieve my overall goal of being
> able to load programs individually!

You can group each program together with its fallbacks in the same
source file / BPF object by disabling autoload for all variants of the
program via SEC("?foobar") syntax. Then in userspace you could turn
autoload on for the first version you'd like to try after opening
the BPF object, try loading the object, try with 2nd variant if that
fails, etc.

selftests/bpf/progs/dynptr_fail.c + verify_fail function in
selftests/bpf/prog_tests/dynptr.c is an example of this pattern.

> Thanks so much for your help,
> Grant Seltzer

  reply	other threads:[~2022-11-11  7:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-11  0:32 Best way to share maps between multiple files/objects? Grant Seltzer Richman
2022-11-11  7:34 ` Dave Marchevsky [this message]
2022-11-14 19:02   ` Grant Seltzer Richman
2022-11-18  0:38     ` Andrii Nakryiko

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=91787040-3612-e847-b512-a38a3dae199e@meta.com \
    --to=davemarchevsky@meta.com \
    --cc=bpf@vger.kernel.org \
    --cc=grantseltzer@gmail.com \
    /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