Skip to content

Commit b0d3569

Browse files
format chore
1 parent 44c6ced commit b0d3569

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

pythonbpf/allocation_pass.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,10 @@ def _allocate_for_attribute(builder, var_name, rval, local_sym_tab, structs_sym_
261261
if field_size_bits in [8, 16, 32, 64]:
262262
# Special case: struct_xdp_md i32 fields should allocate as i64
263263
# because load_ctx_field will zero-extend them to i64
264-
if vmlinux_struct_name == "struct_xdp_md" and field_size_bits == 32:
264+
if (
265+
vmlinux_struct_name == "struct_xdp_md"
266+
and field_size_bits == 32
267+
):
265268
actual_ir_type = ir.IntType(64)
266269
logger.info(
267270
f"Allocating {var_name} as i64 for i32 field from struct_xdp_md.{field_name} "

pythonbpf/assign_pass.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,17 @@ def handle_variable_assignment(
154154
logger.info("Handling assignment to struct field")
155155
# Special handling for struct_xdp_md i32 fields that are zero-extended to i64
156156
# The load_ctx_field already extended them, so val is i64 but val_type.type shows c_uint
157-
if (hasattr(val_type, 'type') and
158-
val_type.type.__name__ == "c_uint" and
159-
isinstance(var_type, ir.IntType) and
160-
var_type.width == 64):
157+
if (
158+
hasattr(val_type, "type")
159+
and val_type.type.__name__ == "c_uint"
160+
and isinstance(var_type, ir.IntType)
161+
and var_type.width == 64
162+
):
161163
# This is the struct_xdp_md case - value is already i64
162164
builder.store(val, var_ptr)
163-
logger.info(f"Assigned zero-extended struct_xdp_md i32 field to {var_name} (i64)")
165+
logger.info(
166+
f"Assigned zero-extended struct_xdp_md i32 field to {var_name} (i64)"
167+
)
164168
return True
165169
# TODO: handling only ctype struct fields for now. Handle other stuff too later.
166170
elif var_type == ctypes_to_ir(val_type.type.__name__):

pythonbpf/vmlinux_parser/vmlinux_exports_handler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ def handle_vmlinux_struct_field(
9595
)
9696
python_type: type = var_info.metadata
9797
struct_name = python_type.__name__
98-
globvar_ir, field_data = self.get_field_type(
99-
struct_name, field_name
100-
)
98+
globvar_ir, field_data = self.get_field_type(struct_name, field_name)
10199
builder.function.args[0].type = ir.PointerType(ir.IntType(8))
102100
field_ptr = self.load_ctx_field(
103101
builder, builder.function.args[0], globvar_ir, field_data, struct_name
@@ -183,7 +181,9 @@ def load_ctx_field(builder, ctx_arg, offset_global, field_data, struct_name=None
183181
# Load as i32 but extend to i64 before storing
184182
if struct_name == "struct_xdp_md" and int_width == 32:
185183
needs_zext = True
186-
logger.info(f"struct_xdp_md i32 field detected, will zero-extend to i64")
184+
logger.info(
185+
"struct_xdp_md i32 field detected, will zero-extend to i64"
186+
)
187187
else:
188188
logger.warning(
189189
f"Unusual field size {field_size_bits} bits, using default 64"

tests/failing_tests/vmlinux/i32_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
from ctypes import c_int64, c_int32, c_void_p
1+
from ctypes import c_int64, c_void_p
22
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile
33
from vmlinux import struct_xdp_md
44
from vmlinux import XDP_PASS
55

6+
67
@bpf
78
@section("xdp")
89
def print_xdp_dat2a(ct2x: struct_xdp_md) -> c_int64:
910
data = ct2x.data # 32-bit field: packet start pointer
1011
print(f"ct2x->data = {data}")
1112
return c_int64(XDP_PASS)
1213

14+
1315
@bpf
1416
@section("xdp")
1517
def print_xdp_data(ctx: struct_xdp_md) -> c_int64:
@@ -18,6 +20,7 @@ def print_xdp_data(ctx: struct_xdp_md) -> c_int64:
1820
print(f"ctx->data = {something}")
1921
return c_int64(XDP_PASS)
2022

23+
2124
@bpf
2225
@bpfglobal
2326
def LICENSE() -> str:

0 commit comments

Comments
 (0)