|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Demo Python Script - Called via exec() from KernelScript |
| 4 | +
|
| 5 | +This script demonstrates the CORRECT usage pattern: |
| 6 | +- Import the auto-generated wrapper (test_exec.py) |
| 7 | +- Use maps directly as module-level variables |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | + |
| 12 | +# Import the auto-generated KernelScript wrapper |
| 13 | +# The wrapper handles all the file descriptor inheritance internally |
| 14 | +try: |
| 15 | + import test_exec as ks |
| 16 | +except ImportError: |
| 17 | + print("❌ Error: Could not import test_exec.py", file=sys.stderr) |
| 18 | + print(" Make sure the KernelScript wrapper was generated correctly", file=sys.stderr) |
| 19 | + sys.exit(1) |
| 20 | + |
| 21 | +def main(): |
| 22 | + """Main function - called via exec() from KernelScript""" |
| 23 | + print("🚀 KernelScript Python Integration Demo") |
| 24 | + print("=" * 40) |
| 25 | + |
| 26 | + # 1. Reading from maps |
| 27 | + print("\n📖 Reading from eBPF maps:") |
| 28 | + try: |
| 29 | + # Read from array map |
| 30 | + value = ks.packet_stats[0] |
| 31 | + print(f" packet_stats[0] = {value}") |
| 32 | + |
| 33 | + value = ks.packet_stats[5] |
| 34 | + print(f" packet_stats[5] = {value}") |
| 35 | + except Exception as e: |
| 36 | + print(f" packet_stats read: {e}") |
| 37 | + |
| 38 | + try: |
| 39 | + # Read from hash map |
| 40 | + value = ks.bandwidth_usage[1] |
| 41 | + print(f" bandwidth_usage[1] = {value}") |
| 42 | + except Exception as e: |
| 43 | + print(f" bandwidth_usage read: {e}") |
| 44 | + |
| 45 | + # 2. Writing to maps |
| 46 | + print("\n✏️ Writing to eBPF maps:") |
| 47 | + try: |
| 48 | + # Write to array map |
| 49 | + ks.packet_stats[0] = 100 |
| 50 | + ks.packet_stats[1] = 200 |
| 51 | + print(" packet_stats[0] = 100") |
| 52 | + print(" packet_stats[1] = 200") |
| 53 | + |
| 54 | + # Write to hash map |
| 55 | + ks.bandwidth_usage[10] = 1024 |
| 56 | + ks.bandwidth_usage[20] = 2048 |
| 57 | + print(" bandwidth_usage[10] = 1024") |
| 58 | + print(" bandwidth_usage[20] = 2048") |
| 59 | + except Exception as e: |
| 60 | + print(f" Map write error: {e}") |
| 61 | + |
| 62 | + # 3. Reading back written values |
| 63 | + print("\n🔄 Reading back written values:") |
| 64 | + try: |
| 65 | + print(f" packet_stats[0] = {ks.packet_stats[0]}") |
| 66 | + print(f" packet_stats[1] = {ks.packet_stats[1]}") |
| 67 | + print(f" bandwidth_usage[10] = {ks.bandwidth_usage[10]}") |
| 68 | + print(f" bandwidth_usage[20] = {ks.bandwidth_usage[20]}") |
| 69 | + except Exception as e: |
| 70 | + print(f" Read back error: {e}") |
| 71 | + |
| 72 | + # 4. Using auto-generated structs |
| 73 | + print("\n🏗️ Using auto-generated structs:") |
| 74 | + ctx = ks.xdp_md() |
| 75 | + ctx.data = 0x1000 |
| 76 | + ctx.data_end = 0x2000 |
| 77 | + ctx.ingress_ifindex = 5 |
| 78 | + ctx.rx_queue_index = 2 |
| 79 | + ctx.egress_ifindex = 8 |
| 80 | + |
| 81 | + packet_size = ctx.data_end - ctx.data |
| 82 | + print(f" Created xdp_md struct:") |
| 83 | + print(f" data: 0x{ctx.data:x}") |
| 84 | + print(f" data_end: 0x{ctx.data_end:x}") |
| 85 | + print(f" packet_size: {packet_size} bytes") |
| 86 | + print(f" ingress_ifindex: {ctx.ingress_ifindex}") |
| 87 | + print(f" rx_queue_index: {ctx.rx_queue_index}") |
| 88 | + print(f" egress_ifindex: {ctx.egress_ifindex}") |
| 89 | + |
| 90 | + # 5. Map operations |
| 91 | + print("\n🗑️ Map operations:") |
| 92 | + try: |
| 93 | + # Delete from hash map |
| 94 | + del ks.bandwidth_usage[10] |
| 95 | + print(" Deleted bandwidth_usage[10]") |
| 96 | + |
| 97 | + # Try to read deleted key |
| 98 | + try: |
| 99 | + value = ks.bandwidth_usage[10] |
| 100 | + print(f" bandwidth_usage[10] = {value}") |
| 101 | + except KeyError: |
| 102 | + print(" bandwidth_usage[10] not found (expected after deletion)") |
| 103 | + except Exception as e: |
| 104 | + print(f" Delete operation: {e}") |
| 105 | + |
| 106 | + print("\n✅ Demo completed successfully!") |
| 107 | + return 0 |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + exit(main()) |
0 commit comments