Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement bpf_object__open_mem #3416

Merged
merged 1 commit into from
Apr 2, 2024
Merged

Conversation

Alan-Jowett
Copy link
Member

Resolves: #3352

Description

This pull request introduces changes that allow the EBPF verifier to load EBPF programs not only from files but also from memory. This is achieved by using std::variant to hold either a file path or a memory buffer. The most significant changes include adding a new method bpf_object__open_mem for opening an ELF file from memory, modifying several methods in libs/api/Verifier.cpp and libs/api/ebpf_api.cpp to handle the variant type, and adding a new test case for loading an EBPF program from memory.

Changes to the API:

  • ebpfapi/Source.def: Added a new function bpf_object__open_mem to the exports.
  • libs/api/api_internal.h: Defined a new function ebpf_object_open_memory that opens an ELF file from memory without loading the programs.
  • libs/api/libbpf_object.cpp: Implemented bpf_object__open_mem that uses ebpf_object_open_memory to open an ELF file from memory.

Changes to the Verifier:

  • libs/api/Verifier.cpp and libs/api/Verifier.h: Modified _get_program_and_map_names and load_byte_code to accept a std::variant that can be either a file path or a memory buffer. [1] [2] [3] [4] [5]

Changes to the EBPF API:

  • libs/api/ebpf_api.cpp: Added the implementation for ebpf_object_open_memory and modified _initialize_ebpf_object_from_elf, _initialize_ebpf_object_from_file and other related methods to handle the std::variant type. [1] [2] [3] [4]

Changes to the test suite:

Testing

CI/CD + new tests.

Documentation

No.

Installation

No.

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
std::string path = std::get<std::string>(file_or_data);
new_object->file_name = cxplat_duplicate_string(path.c_str());
} else {
new_object->file_name = cxplat_duplicate_string("memory");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe make this more unique? eg. EBPF_PROG_ID_MEMORY_BUF or similar

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like libbpf uses the name from the opts file, so I will switch it.

libs/api/ebpf_api.cpp Show resolved Hide resolved
libs/api/ebpf_api.cpp Show resolved Hide resolved
@Alan-Jowett Alan-Jowett added this pull request to the merge queue Apr 2, 2024
@Alan-Jowett Alan-Jowett mentioned this pull request Apr 2, 2024
Merged via the queue into microsoft:main with commit 6eccc18 Apr 2, 2024
84 checks passed
@Alan-Jowett Alan-Jowett deleted the issue3352 branch April 2, 2024 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement bpf_object__open_mem
3 participants