-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
When using the pin keyword with map declarations (e.g., pin var cpu_counters : array<...>), the current implementation faces two primary issues that prevent the maps from being correctly pinned to the BPF filesystem.
The Problems
- Incorrect Treatment as Global Variables: The IR generator incorrectly creates both an IR map definition and an IR global variable for pinned maps. This causes the userspace codegen to treat the map as a "pinned global variable," looking for a non-existent
__pinned_globalsmap and resulting in the error:Failed to find pinned globals map in eBPF object. - Missing Setup Code: Setup and pinning logic for maps is only generated if the userspace code explicitly performs map operations. However, pinned maps require setup (pinning to
/sys/fs/bpf/...) even if they are only accessed within the eBPF program itself. - Variable Redefinition: If multiple pinned maps exist, the generated C code uses the same variable name (
existing_fd) for all of them, leading to compilation errors. - Missing Directory Creation: The generated code fails to pin maps if the parent directory (e.g.,
/sys/fs/bpf/<project>/maps/) does not already exist.
Proposed Changes
The provided patch addresses these issues with the following logic:
- IR Generator: Modified
ir_generator.mlto ensure that map-type global declarations only produce map definitions, not global variable entries. - Userspace Codegen:
- Updated
userspace_codegen.mlto trigger map setup and FD declarations if any pinned maps are detected (has_pinned_maps), regardless of whether they are used in userspace C code. - Implemented unique variable naming (e.g.,
%s_existing_fd) to prevent C redefinition errors. - Added a C helper function
ensure_bpf_dirto recursively create the necessary BPF filesystem directories before pinning. - Included necessary headers (
<sys/stat.h>,<sys/types.h>) when pinning is required.
Verification
Testing with examples/maps_demo.ks confirms:
- The "Failed to find pinned globals map" error is resolved.
- Maps are correctly pinned to
/sys/fs/bpf/maps_demo/maps/. - Userspace programs compile and run correctly with multiple pinned maps.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels