mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-02-12 16:10:59 +00:00
Compare commits
4 Commits
80396c78a6
...
e5741562f6
| Author | SHA1 | Date | |
|---|---|---|---|
| e5741562f6 | |||
| 93634a4769 | |||
| 9b8462f1ed | |||
| 785182787c |
@ -40,69 +40,106 @@ def process_vmlinux_post_ast(
|
|||||||
type_length: Optional[int] = None
|
type_length: Optional[int] = None
|
||||||
module_name = getattr(elem_type_class, "__module__", None)
|
module_name = getattr(elem_type_class, "__module__", None)
|
||||||
|
|
||||||
if hasattr(elem_type_class, "_length_") and is_complex_type:
|
|
||||||
type_length = elem_type_class._length_
|
|
||||||
|
|
||||||
if current_symbol_name in processing_stack:
|
if current_symbol_name in processing_stack:
|
||||||
logger.debug(
|
logger.info(f"Circular dependency detected for {current_symbol_name}, skipping")
|
||||||
f"Circular dependency detected for {current_symbol_name}, skipping"
|
|
||||||
)
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Check if already processed
|
# Check if already processed
|
||||||
if handler.has_node(current_symbol_name):
|
if handler.has_node(current_symbol_name):
|
||||||
existing_node = handler.get_node(current_symbol_name)
|
logger.info(f"Node {current_symbol_name} already processed and ready")
|
||||||
# If the node exists and is ready, we're done
|
return True
|
||||||
if existing_node and existing_node.is_ready:
|
|
||||||
logger.info(f"Node {current_symbol_name} already processed and ready")
|
|
||||||
return True
|
|
||||||
|
|
||||||
processing_stack.add(current_symbol_name)
|
processing_stack.add(current_symbol_name)
|
||||||
|
|
||||||
if module_name == "vmlinux":
|
if module_name == "vmlinux":
|
||||||
if hasattr(elem_type_class, "_type_"):
|
if hasattr(elem_type_class, "_type_"):
|
||||||
is_complex_type = True
|
pass
|
||||||
containing_type = elem_type_class._type_
|
|
||||||
if containing_type.__module__ == "vmlinux":
|
|
||||||
print("Very weird type ig for containing type", containing_type)
|
|
||||||
elif containing_type.__module__ == ctypes.__name__:
|
|
||||||
if isinstance(elem_type_class, type):
|
|
||||||
if issubclass(elem_type_class, ctypes.Array):
|
|
||||||
ctype_complex_type = ctypes.Array
|
|
||||||
elif issubclass(elem_type_class, ctypes._Pointer):
|
|
||||||
ctype_complex_type = ctypes._Pointer
|
|
||||||
else:
|
|
||||||
raise TypeError("Unsupported ctypes subclass")
|
|
||||||
# handle ctype complex type
|
|
||||||
|
|
||||||
else:
|
|
||||||
raise ImportError(f"Unsupported module of {containing_type}")
|
|
||||||
else:
|
else:
|
||||||
new_dep_node = DependencyNode(name=current_symbol_name)
|
new_dep_node = DependencyNode(name=current_symbol_name)
|
||||||
handler.add_node(new_dep_node)
|
handler.add_node(new_dep_node)
|
||||||
class_obj = getattr(imported_module, current_symbol_name)
|
class_obj = getattr(imported_module, current_symbol_name)
|
||||||
# Inspect the class fields
|
# Inspect the class fields
|
||||||
if hasattr(class_obj, "_fields_"):
|
if hasattr(class_obj, "_fields_"):
|
||||||
for field_name, field_type in class_obj._fields_:
|
for field_elem in class_obj._fields_:
|
||||||
field_table[field_name] = field_type
|
field_name = None
|
||||||
|
field_type = None
|
||||||
|
bitfield_size = None
|
||||||
|
if len(field_elem) == 2:
|
||||||
|
field_name, field_type = field_elem
|
||||||
|
elif len(field_elem) == 3:
|
||||||
|
field_name, field_type, bitfield_size = field_elem
|
||||||
|
field_table[field_name] = [field_type, bitfield_size]
|
||||||
elif hasattr(class_obj, "__annotations__"):
|
elif hasattr(class_obj, "__annotations__"):
|
||||||
for field_name, field_type in class_obj.__annotations__.items():
|
for field_elem in class_obj.__annotations__.items():
|
||||||
field_table[field_name] = field_type
|
field_name = None
|
||||||
|
field_type = None
|
||||||
|
bitfield_size = None
|
||||||
|
if len(field_elem) == 2:
|
||||||
|
field_name, field_type = field_elem
|
||||||
|
elif len(field_elem) == 3:
|
||||||
|
field_name, field_type, bitfield_size = field_elem
|
||||||
|
field_table[field_name] = [field_type, bitfield_size]
|
||||||
else:
|
else:
|
||||||
raise TypeError("Could not get required class and definition")
|
raise TypeError("Could not get required class and definition")
|
||||||
|
|
||||||
logger.info(f"Extracted fields for {current_symbol_name}: {field_table}")
|
logger.debug(f"Extracted fields for {current_symbol_name}: {field_table}")
|
||||||
|
for elem in field_table.items():
|
||||||
for elem_name, elem_type in field_table.items():
|
elem_name, elem_temp_list = elem
|
||||||
|
[elem_type, elem_bitfield_size] = elem_temp_list
|
||||||
local_module_name = getattr(elem_type, "__module__", None)
|
local_module_name = getattr(elem_type, "__module__", None)
|
||||||
if local_module_name == ctypes.__name__:
|
if local_module_name == ctypes.__name__:
|
||||||
new_dep_node.add_field(elem_name, elem_type, ready=True)
|
new_dep_node.add_field(elem_name, elem_type, ready=False)
|
||||||
|
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
|
||||||
|
new_dep_node.set_field_ready(elem_name, is_ready=True)
|
||||||
logger.info(f"Field {elem_name} is direct ctypes type: {elem_type}")
|
logger.info(f"Field {elem_name} is direct ctypes type: {elem_type}")
|
||||||
elif local_module_name == "vmlinux":
|
elif local_module_name == "vmlinux":
|
||||||
new_dep_node.add_field(elem_name, elem_type, ready=False)
|
new_dep_node.add_field(elem_name, elem_type, ready=False)
|
||||||
|
new_dep_node.set_field_bitfield_size(elem_name, elem_bitfield_size)
|
||||||
logger.debug(
|
logger.debug(
|
||||||
f"Processing vmlinux field: {elem_name}, type: {elem_type}"
|
f"Processing vmlinux field: {elem_name}, type: {elem_type}"
|
||||||
)
|
)
|
||||||
|
if hasattr(elem_type, "_type_"):
|
||||||
|
is_complex_type = True
|
||||||
|
containing_type = elem_type._type_
|
||||||
|
if hasattr(elem_type, "_length_") and is_complex_type:
|
||||||
|
type_length = elem_type._length_
|
||||||
|
if containing_type.__module__ == "vmlinux":
|
||||||
|
pass
|
||||||
|
elif containing_type.__module__ == ctypes.__name__:
|
||||||
|
if isinstance(elem_type, type):
|
||||||
|
if issubclass(elem_type, ctypes.Array):
|
||||||
|
ctype_complex_type = ctypes.Array
|
||||||
|
elif issubclass(elem_type, ctypes._Pointer):
|
||||||
|
ctype_complex_type = ctypes._Pointer
|
||||||
|
else:
|
||||||
|
raise TypeError("Unsupported ctypes subclass")
|
||||||
|
else:
|
||||||
|
raise ImportError(
|
||||||
|
f"Unsupported module of {containing_type}"
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
f"{containing_type} containing type of parent {elem_name} with {elem_type} and ctype {ctype_complex_type} and length {type_length}"
|
||||||
|
)
|
||||||
|
new_dep_node.set_field_containing_type(
|
||||||
|
elem_name, containing_type
|
||||||
|
)
|
||||||
|
new_dep_node.set_field_type_size(elem_name, type_length)
|
||||||
|
new_dep_node.set_field_ctype_complex_type(
|
||||||
|
elem_name, ctype_complex_type
|
||||||
|
)
|
||||||
|
new_dep_node.set_field_type(elem_name, elem_type)
|
||||||
|
if containing_type.__module__ == "vmlinux":
|
||||||
|
if process_vmlinux_post_ast(
|
||||||
|
containing_type, llvm_handler, handler, processing_stack
|
||||||
|
):
|
||||||
|
new_dep_node.set_field_ready(elem_name, True)
|
||||||
|
elif containing_type.__module__ == ctypes.__name__:
|
||||||
|
logger.info(f"Processing ctype internal{containing_type}")
|
||||||
|
else:
|
||||||
|
raise TypeError(
|
||||||
|
"Module not supported in recursive resolution"
|
||||||
|
)
|
||||||
|
continue
|
||||||
if process_vmlinux_post_ast(
|
if process_vmlinux_post_ast(
|
||||||
elem_type, llvm_handler, handler, processing_stack
|
elem_type, llvm_handler, handler, processing_stack
|
||||||
):
|
):
|
||||||
@ -111,7 +148,6 @@ def process_vmlinux_post_ast(
|
|||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"{elem_name} with type {elem_type} from module {module_name} not supported in recursive resolver"
|
f"{elem_name} with type {elem_type} from module {module_name} not supported in recursive resolver"
|
||||||
)
|
)
|
||||||
print("")
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ImportError("UNSUPPORTED Module")
|
raise ImportError("UNSUPPORTED Module")
|
||||||
|
|||||||
@ -9,8 +9,10 @@ class Field:
|
|||||||
|
|
||||||
name: str
|
name: str
|
||||||
type: type
|
type: type
|
||||||
|
ctype_complex_type: Optional[Any]
|
||||||
containing_type: Optional[Any]
|
containing_type: Optional[Any]
|
||||||
type_size: Optional[int]
|
type_size: Optional[int]
|
||||||
|
bitfield_size: Optional[int]
|
||||||
value: Any = None
|
value: Any = None
|
||||||
ready: bool = False
|
ready: bool = False
|
||||||
|
|
||||||
@ -44,6 +46,20 @@ class Field:
|
|||||||
if mark_ready:
|
if mark_ready:
|
||||||
self.ready = True
|
self.ready = True
|
||||||
|
|
||||||
|
def set_ctype_complex_type(
|
||||||
|
self, ctype_complex_type: Any, mark_ready: bool = True
|
||||||
|
) -> None:
|
||||||
|
"""Set the ctype_complex_type of this field and optionally mark it as ready."""
|
||||||
|
self.ctype_complex_type = ctype_complex_type
|
||||||
|
if mark_ready:
|
||||||
|
self.ready = True
|
||||||
|
|
||||||
|
def set_bitfield_size(self, bitfield_size: Any, mark_ready: bool = True) -> None:
|
||||||
|
"""Set the bitfield_size of this field and optionally mark it as ready."""
|
||||||
|
self.bitfield_size = bitfield_size
|
||||||
|
if mark_ready:
|
||||||
|
self.ready = True
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DependencyNode:
|
class DependencyNode:
|
||||||
@ -100,6 +116,8 @@ class DependencyNode:
|
|||||||
initial_value: Any = None,
|
initial_value: Any = None,
|
||||||
containing_type: Optional[Any] = None,
|
containing_type: Optional[Any] = None,
|
||||||
type_size: Optional[int] = None,
|
type_size: Optional[int] = None,
|
||||||
|
ctype_complex_type: Optional[int] = None,
|
||||||
|
bitfield_size: Optional[int] = None,
|
||||||
ready: bool = False,
|
ready: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Add a field to the node with an optional initial value and readiness state."""
|
"""Add a field to the node with an optional initial value and readiness state."""
|
||||||
@ -110,6 +128,8 @@ class DependencyNode:
|
|||||||
ready=ready,
|
ready=ready,
|
||||||
containing_type=containing_type,
|
containing_type=containing_type,
|
||||||
type_size=type_size,
|
type_size=type_size,
|
||||||
|
ctype_complex_type=ctype_complex_type,
|
||||||
|
bitfield_size=bitfield_size,
|
||||||
)
|
)
|
||||||
# Invalidate readiness cache
|
# Invalidate readiness cache
|
||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
@ -158,6 +178,28 @@ class DependencyNode:
|
|||||||
# Invalidate readiness cache
|
# Invalidate readiness cache
|
||||||
self._ready_cache = None
|
self._ready_cache = None
|
||||||
|
|
||||||
|
def set_field_ctype_complex_type(
|
||||||
|
self, name: str, ctype_complex_type: Any, mark_ready: bool = True
|
||||||
|
) -> None:
|
||||||
|
"""Set a field's ctype_complex_type and optionally mark it as ready."""
|
||||||
|
if name not in self.fields:
|
||||||
|
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
|
||||||
|
|
||||||
|
self.fields[name].set_ctype_complex_type(ctype_complex_type, mark_ready)
|
||||||
|
# Invalidate readiness cache
|
||||||
|
self._ready_cache = None
|
||||||
|
|
||||||
|
def set_field_bitfield_size(
|
||||||
|
self, name: str, bitfield_size: Any, mark_ready: bool = True
|
||||||
|
) -> None:
|
||||||
|
"""Set a field's bitfield_size and optionally mark it as ready."""
|
||||||
|
if name not in self.fields:
|
||||||
|
raise KeyError(f"Field '{name}' does not exist in node '{self.name}'")
|
||||||
|
|
||||||
|
self.fields[name].set_bitfield_size(bitfield_size, mark_ready)
|
||||||
|
# Invalidate readiness cache
|
||||||
|
self._ready_cache = None
|
||||||
|
|
||||||
def set_field_ready(self, name: str, is_ready: bool = True) -> None:
|
def set_field_ready(self, name: str, is_ready: bool = True) -> None:
|
||||||
"""Mark a field as ready or not ready."""
|
"""Mark a field as ready or not ready."""
|
||||||
if name not in self.fields:
|
if name not in self.fields:
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
from pythonbpf import bpf, map, section, bpfglobal, compile, compile_to_ir
|
from pythonbpf import bpf, map, section, bpfglobal, compile_to_ir
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
from pythonbpf.helper import XDP_PASS
|
from pythonbpf.helper import XDP_PASS
|
||||||
from vmlinux import struct_xdp_md
|
from vmlinux import struct_xdp_md
|
||||||
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
|
|
||||||
|
|
||||||
# from vmlinux import struct_xdp_buff # noqa: F401
|
from vmlinux import struct_ring_buffer_per_cpu # noqa: F401
|
||||||
|
from vmlinux import struct_xdp_buff # noqa: F401
|
||||||
|
|
||||||
# from vmlinux import struct_xdp_md
|
# from vmlinux import struct_xdp_md
|
||||||
from ctypes import c_int64
|
from ctypes import c_int64
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user