11#include " core/bpf_map.h"
22#include " core/bpf_exception.h"
33#include " core/bpf_object.h"
4+ #include " utils/struct_parser.h"
45#include < algorithm>
56#include < cerrno>
67#include < cstring>
@@ -25,6 +26,24 @@ BpfMap::BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map,
2526 value_size_ = bpf_map__value_size (map_);
2627}
2728
29+ void BpfMap::set_value_struct (const std::string &struct_name) {
30+ auto parent = parent_obj_.lock ();
31+ if (!parent) {
32+ throw BpfException (" Parent BpfObject no longer exists" );
33+ }
34+
35+ struct_parser_ = parent->get_struct_parser ();
36+ if (!struct_parser_) {
37+ throw BpfException (" No struct definitions available in BpfObject" );
38+ }
39+
40+ if (!struct_parser_->has_struct (struct_name)) {
41+ throw BpfException (" Unknown struct: " + struct_name);
42+ }
43+
44+ value_struct_name_ = struct_name;
45+ }
46+
2847py::object BpfMap::lookup (const py::object &key) const {
2948 if (map_fd_ < 0 )
3049 throw BpfException (" Map '" + map_name_ + " ' is not initialized properly" );
@@ -42,7 +61,7 @@ py::object BpfMap::lookup(const py::object &key) const {
4261 value_span.data (), value_size_, BPF_ANY);
4362 if (ret < 0 ) {
4463 if (ret == -ENOENT)
45- throw py::key_error ( " Key not found in map ' " + map_name_ + " ' " );
64+ return py::none ( );
4665 throw BpfException (" Failed to lookup key in map '" + map_name_ +
4766 " ': " + std::strerror (-ret));
4867 }
@@ -215,7 +234,13 @@ void BpfMap::python_to_bytes_inplace(const py::object &obj,
215234 }
216235}
217236
218- py::object BpfMap::bytes_to_python (std::span<const uint8_t > data) {
237+ py::object BpfMap::bytes_to_python (std::span<const uint8_t > data) const {
238+ // NOTE: Struct parsing for value type
239+ if (struct_parser_ && !value_struct_name_.empty ()) {
240+ py::bytes py_data (reinterpret_cast <const char *>(data.data ()), data.size ());
241+ return struct_parser_->parse (value_struct_name_, py_data);
242+ }
243+
219244 if (data.size () == 4 ) {
220245 uint32_t value;
221246 std::memcpy (&value, data.data (), 4 );
0 commit comments