Skip to content

Commit 93bfd9b

Browse files
Sy0307Officeyutong
andauthored
Fix new maps example test in new bpftool & libbpf version (#445)
* update * [~] Remove def conflict --------- Co-authored-by: officeyutong <[email protected]>
1 parent 7f41aed commit 93bfd9b

15 files changed

+116
-27
lines changed

runtime/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ set(sources
5959
src/handler/perf_event_handler.cpp
6060
src/handler/prog_handler.cpp
6161
src/handler/epoll_handler.cpp
62-
62+
src/handler/memfd_handler.cpp
63+
6364
src/bpftime_shm.cpp
6465
src/bpftime_shm_internal.cpp
6566
src/bpftime_shm_json.cpp

runtime/include/bpftime_shm.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ int bpftime_add_custom_perf_event(int type, const char *attach_argument);
410410
int bpftime_poll_gpu_ringbuf_map(int mapfd, void *ctx,
411411
void (*)(const void *, uint64_t, void *));
412412
#endif
413+
int bpftime_add_memfd_handler(const char *name, int flags);
413414
}
414415

415416
#endif // BPFTIME_SHM_CPP_H

runtime/src/bpftime_shm.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,3 +700,9 @@ int bpftime_poll_gpu_ringbuf_map(int mapfd, void *ctx,
700700
return 0;
701701
}
702702
#endif
703+
704+
int bpftime_add_memfd_handler(const char *name, int flags)
705+
{
706+
auto &shm = shm_holder.global_shared_memory;
707+
return shm.add_memfd_handler(name, flags);
708+
}

runtime/src/bpftime_shm_internal.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#include "bpftime_shm.hpp"
77
#include "handler/map_handler.hpp"
8+
#include "handler/memfd_handler.hpp"
89
#include <ebpf-vm.h>
910
#include "handler/epoll_handler.hpp"
1011
#include "handler/handler_manager.hpp"
@@ -777,6 +778,11 @@ int bpftime_shm::add_bpf_map(int fd, const char *name,
777778
};
778779
verifier::set_map_descriptors(helpers);
779780
#endif
781+
if (!std::holds_alternative<unused_handler>(manager->get_handler(fd))) {
782+
SPDLOG_DEBUG(
783+
"Creating map handler at {}, which must destroy the existing handler");
784+
manager->clear_id_at(fd, segment);
785+
}
780786
return manager->set_handler(
781787
fd, bpftime::bpf_map_handler(fd, name, segment, attr), segment);
782788
}
@@ -800,6 +806,13 @@ int bpftime_shm::dup_bpf_map(int oldfd, int newfd)
800806
auto &handler =
801807
std::get<bpftime::bpf_map_handler>(manager->get_handler(oldfd));
802808
std::string new_name = std::string("dup_") + handler.name.c_str();
809+
// Destroy old handler
810+
auto &old_handler = manager->get_handler(newfd);
811+
if (!std::holds_alternative<unused_handler>(old_handler)) {
812+
SPDLOG_DEBUG("Dup to fd {}, destroying old handler", newfd);
813+
814+
manager->clear_id_at(newfd, segment);
815+
}
803816
// Create a new handler with the same parameters
804817
return manager->set_handler(
805818
newfd,
@@ -963,12 +976,22 @@ int bpftime_shm::poll_gpu_ringbuf_map(
963976

964977
auto impl_opt = map_handler.try_get_nv_gpu_ringbuf_map_impl();
965978
if (!impl_opt) {
966-
SPDLOG_ERROR("Failed to get nv_gpu_ringbuf_map_impl for mapfd {}", mapfd);
979+
SPDLOG_ERROR(
980+
"Failed to get nv_gpu_ringbuf_map_impl for mapfd {}",
981+
mapfd);
967982
return -1;
968983
}
969984
auto &impl = *impl_opt;
970985
return impl->drain_data(fn);
971986
}
972987
#endif
973-
988+
int bpftime_shm::add_memfd_handler(const char *name, int flags)
989+
{
990+
int fd = open_fake_fd();
991+
fd = manager->set_handler(fd, memfd_handler(name, flags, segment),
992+
segment);
993+
SPDLOG_DEBUG("Created memfd handler at {}, name {}, flags {}", fd, name,
994+
flags);
995+
return fd;
996+
}
974997
} // namespace bpftime

runtime/src/bpftime_shm_internal.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ class bpftime_shm {
202202
void close_fd(int fd);
203203
bool is_exist_fake_fd(int fd) const;
204204

205+
int add_memfd_handler(const char *name, int flags);
206+
205207
#if BPFTIME_ENABLE_MPK
206208
void enable_mpk();
207209
void disable_mpk();

runtime/src/handler/handler_manager.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define _HANDLER_MANAGER_HPP
88
#include "bpftime_config.hpp"
99
#include "handler/epoll_handler.hpp"
10+
#include "handler/memfd_handler.hpp"
1011
#include "spdlog/spdlog.h"
1112
#include <boost/interprocess/interprocess_fwd.hpp>
1213
#include <cstddef>
@@ -51,7 +52,8 @@ constexpr const char *DEFAULT_GLOBAL_SHM_NAME = "bpftime_maps_shm";
5152
constexpr const char *DEFAULT_GLOBAL_HANDLER_NAME = "bpftime_handler";
5253
constexpr const char *DEFAULT_SYSCALL_PID_SET_NAME = "bpftime_syscall_pid_set";
5354
constexpr const char *DEFAULT_AGENT_CONFIG_NAME = "bpftime_agent_config";
54-
constexpr const char* DEFAULT_ALIVE_AGENT_PIDS_NAME = "bpftime_alive_agent_pids";
55+
constexpr const char *DEFAULT_ALIVE_AGENT_PIDS_NAME =
56+
"bpftime_alive_agent_pids";
5557
inline const char *get_global_shm_name()
5658
{
5759
const char *name = getenv("BPFTIME_GLOBAL_SHM_NAME");
@@ -81,7 +83,8 @@ struct shm_remove {
8183

8284
using handler_variant =
8385
std::variant<unused_handler, bpf_map_handler, bpf_link_handler,
84-
bpf_prog_handler, bpf_perf_event_handler, epoll_handler>;
86+
bpf_prog_handler, bpf_perf_event_handler, epoll_handler,
87+
memfd_handler>;
8588

8689
using handler_variant_allocator =
8790
allocator<handler_variant, managed_shared_memory::segment_manager>;
@@ -93,8 +96,7 @@ using handler_variant_vector =
9396
// This struct will be put on shared memory
9497
class handler_manager {
9598
public:
96-
handler_manager(managed_shared_memory &mem,
97-
size_t max_fd_count);
99+
handler_manager(managed_shared_memory &mem, size_t max_fd_count);
98100

99101
~handler_manager();
100102

runtime/src/handler/map_handler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bpftime_map_ops global_map_ops_table[(int)bpf_map_type::BPF_MAP_TYPE_MAX] = {
5252
{ 0 }
5353
};
5454

55-
std::string bpf_map_handler::get_container_name()
55+
std::string bpf_map_handler::get_container_name() const
5656
{
5757
return "ebpf_map_fd_" + std::string(name.c_str());
5858
}
@@ -837,7 +837,7 @@ int bpf_map_handler::map_init(managed_shared_memory &memory)
837837
return 0;
838838
}
839839

840-
void bpf_map_handler::map_free(managed_shared_memory &memory)
840+
void bpf_map_handler::map_free(managed_shared_memory &memory) const
841841
{
842842
auto container_name = get_container_name();
843843
switch (type) {

runtime/src/handler/map_handler.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class bpf_map_handler {
191191
// *
192192
int bpf_map_get_next_key(const void *key, void *next_key,
193193
bool from_syscall = false) const;
194-
void map_free(boost::interprocess::managed_shared_memory &memory);
194+
void map_free(boost::interprocess::managed_shared_memory &memory) const;
195195
int map_init(boost::interprocess::managed_shared_memory &memory);
196196
uint32_t get_userspace_value_size() const;
197197
std::optional<ringbuf_map_impl *> try_get_ringbuf_map_impl() const;
@@ -220,10 +220,10 @@ class bpf_map_handler {
220220
#endif
221221
private:
222222
int id = 0;
223-
std::string get_container_name();
223+
std::string get_container_name() const;
224224
mutable pthread_spinlock_t map_lock;
225225
// The underlying data structure of the map
226-
general_map_impl_ptr map_impl_ptr;
226+
mutable general_map_impl_ptr map_impl_ptr;
227227
uint32_t max_entries = 0;
228228
[[maybe_unused]] uint64_t flags = 0;
229229
uint32_t key_size = 0;

runtime/src/handler/memfd_handler.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "memfd_handler.hpp"
2+
3+
using namespace bpftime;
4+
5+
memfd_handler::memfd_handler(const char *name, int flags,
6+
boost::interprocess::managed_shared_memory &memory)
7+
: name(char_allocator(memory.get_segment_manager()))
8+
{
9+
this->name = name;
10+
this->flags = flags;
11+
}

runtime/src/handler/memfd_handler.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef _BPFTIME_MEMFD_HANDLER_HPP
2+
#define _BPFTIME_MEMFD_HANDLER_HPP
3+
4+
#include <boost/interprocess/containers/string.hpp>
5+
#include <boost/interprocess/interprocess_fwd.hpp>
6+
#include <boost/interprocess/managed_shared_memory.hpp>
7+
namespace bpftime
8+
{
9+
10+
using char_allocator = boost::interprocess::allocator<
11+
char, boost::interprocess::managed_shared_memory::segment_manager>;
12+
using boost_shm_string =
13+
boost::interprocess::basic_string<char, std::char_traits<char>,
14+
char_allocator>;
15+
16+
struct memfd_handler {
17+
int flags;
18+
boost_shm_string name;
19+
memfd_handler(const char *name, int flags,
20+
boost::interprocess::managed_shared_memory &memory);
21+
};
22+
} // namespace bpftime
23+
24+
#endif

0 commit comments

Comments
 (0)