mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
22 Commits
fix-vmlinu
...
v0.1.8
| Author | SHA1 | Date | |
|---|---|---|---|
| 049ec55e85 | |||
| 77901accf2 | |||
| 0616a2fccb | |||
| 526425a267 | |||
| 466ecdb6a4 | |||
| 752a10fa5f | |||
| 3602b502f4 | |||
| 808db2722d | |||
| 99fc5d75cc | |||
| c91e69e2f7 | |||
| dc995a1448 | |||
| 0fd6bea211 | |||
| 01d234ac86 | |||
| 2543826e85 | |||
| 2daedc5882 | |||
| 0ca835079d | |||
| 127852ee9f | |||
| 2fd4fefbcc | |||
| 016fd5de5c | |||
| 8ad5fb8a3a | |||
| bf9635e324 | |||
| cbe365d760 |
@ -7,14 +7,25 @@ This folder contains examples of BCC tutorial examples that have been ported to
|
|||||||
- You will also need `matplotlib` for vfsreadlat.py example.
|
- You will also need `matplotlib` for vfsreadlat.py example.
|
||||||
- You will also need `rich` for vfsreadlat_rich.py example.
|
- You will also need `rich` for vfsreadlat_rich.py example.
|
||||||
- You will also need `plotly` and `dash` for vfsreadlat_plotly.py example.
|
- You will also need `plotly` and `dash` for vfsreadlat_plotly.py example.
|
||||||
|
- All of these are added to `requirements.txt` file. You can install them using the following command:
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
- You'll need root privileges to run these examples. If you are using a virtualenv, use the following command to run the scripts:
|
- You'll need root privileges to run these examples. If you are using a virtualenv, use the following command to run the scripts:
|
||||||
```bash
|
```bash
|
||||||
sudo <path_to_virtualenv>/bin/python3 <script_name>.py
|
sudo <path_to_virtualenv>/bin/python3 <script_name>.py
|
||||||
```
|
```
|
||||||
|
- For the disksnoop and container-monitor examples, you need to generate the vmlinux.py file first. Follow the instructions in the [main README](https://github.com/pythonbpf/Python-BPF/tree/master?tab=readme-ov-file#first-generate-the-vmlinuxpy-file-for-your-kernel) to generate the vmlinux.py file.
|
||||||
- For vfsreadlat_plotly.py, run the following command to start the Dash server:
|
- For vfsreadlat_plotly.py, run the following command to start the Dash server:
|
||||||
```bash
|
```bash
|
||||||
sudo <path_to_virtualenv>/bin/python3 vfsreadlat_plotly/bpf_program.py
|
sudo <path_to_virtualenv>/bin/python3 vfsreadlat_plotly/bpf_program.py
|
||||||
```
|
```
|
||||||
Then open your web browser and navigate to the given URL.
|
Then open your web browser and navigate to the given URL.
|
||||||
|
- For container-monitor, you need to first copy the vmlinux.py to `container-monitor/` directory.
|
||||||
|
Then run the following command to run the example:
|
||||||
|
```bash
|
||||||
|
cp vmlinux.py container-monitor/
|
||||||
|
sudo <path_to_virtualenv>/bin/python3 container-monitor/container_monitor.py
|
||||||
|
```
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
from vmlinux import struct_request, struct_pt_regs
|
from vmlinux import struct_request, struct_pt_regs
|
||||||
from pythonbpf import bpf, section, bpfglobal, compile_to_ir, compile, map
|
from pythonbpf import bpf, section, bpfglobal, compile, map
|
||||||
from pythonbpf.helper import ktime
|
from pythonbpf.helper import ktime
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
import logging
|
|
||||||
from ctypes import c_int64, c_uint64, c_int32
|
from ctypes import c_int64, c_uint64, c_int32
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
@ -56,6 +55,4 @@ def LICENSE() -> str:
|
|||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
compile()
|
||||||
compile_to_ir("disksnoop.py", "disksnoop.ll", loglevel=logging.INFO)
|
|
||||||
compile()
|
|
||||||
|
|||||||
9
BCC-Examples/requirements.txt
Normal file
9
BCC-Examples/requirements.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# =============================================================================
|
||||||
|
# Requirements for PythonBPF BCC-Examples
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
dash
|
||||||
|
matplotlib
|
||||||
|
numpy
|
||||||
|
plotly
|
||||||
|
rich
|
||||||
46
README.md
46
README.md
@ -40,16 +40,11 @@ Python-BPF is an LLVM IR generator for eBPF programs written in Python. It uses
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Try It Out!
|
|
||||||
Run
|
|
||||||
```bash
|
|
||||||
curl -s https://raw.githubusercontent.com/pythonbpf/Python-BPF/refs/heads/master/tools/setup.sh | sudo bash
|
|
||||||
```
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Dependencies:
|
Dependencies:
|
||||||
|
|
||||||
|
* `bpftool`
|
||||||
* `clang`
|
* `clang`
|
||||||
* Python ≥ 3.8
|
* Python ≥ 3.8
|
||||||
|
|
||||||
@ -61,6 +56,38 @@ pip install pythonbpf pylibbpf
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Try It Out!
|
||||||
|
|
||||||
|
#### First, generate the vmlinux.py file for your kernel:
|
||||||
|
- Install the required dependencies:
|
||||||
|
- On Ubuntu:
|
||||||
|
```bash
|
||||||
|
sudo apt-get install bpftool clang
|
||||||
|
pip install pythonbpf pylibbpf ctypeslib2
|
||||||
|
```
|
||||||
|
- Generate the `vmlinux.py` using:
|
||||||
|
```bash
|
||||||
|
sudo tools/vmlinux-gen.py
|
||||||
|
```
|
||||||
|
- Copy this file to `BCC-Examples/`
|
||||||
|
|
||||||
|
#### Next, install requirements for BCC-Examples:
|
||||||
|
- These requirements are only required for the python notebooks, vfsreadlat and container-monitor examples.
|
||||||
|
```bash
|
||||||
|
pip install -r BCC-Examples/requirements.txt
|
||||||
|
```
|
||||||
|
- Now, follow the instructions in the [BCC-Examples/README.md](https://github.com/pythonbpf/Python-BPF/blob/master/BCC-Examples/README.md) to run the examples.
|
||||||
|
|
||||||
|
|
||||||
|
#### To spin up jupyter notebook examples:
|
||||||
|
- Run and follow the instructions on screen
|
||||||
|
```bash
|
||||||
|
curl -s https://raw.githubusercontent.com/pythonbpf/Python-BPF/refs/heads/master/tools/setup.sh | sudo bash
|
||||||
|
```
|
||||||
|
- Check the jupyter server on the web browser and run the notebooks in the `BCC-Examples/` folder.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@ -88,16 +115,15 @@ def hist() -> HashMap:
|
|||||||
@section("tracepoint/syscalls/sys_enter_clone")
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
def hello(ctx: c_void_p) -> c_int64:
|
def hello(ctx: c_void_p) -> c_int64:
|
||||||
process_id = pid()
|
process_id = pid()
|
||||||
one = 1
|
|
||||||
prev = hist.lookup(process_id)
|
prev = hist.lookup(process_id)
|
||||||
if prev:
|
if prev:
|
||||||
previous_value = prev + 1
|
previous_value = prev + 1
|
||||||
print(f"count: {previous_value} with {process_id}")
|
print(f"count: {previous_value} with {process_id}")
|
||||||
hist.update(process_id, previous_value)
|
hist.update(process_id, previous_value)
|
||||||
return c_int64(0)
|
return 0
|
||||||
else:
|
else:
|
||||||
hist.update(process_id, one)
|
hist.update(process_id, 1)
|
||||||
return c_int64(0)
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@bpf
|
@bpf
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "pythonbpf"
|
name = "pythonbpf"
|
||||||
version = "0.1.7"
|
version = "0.1.8"
|
||||||
description = "Reduced Python frontend for eBPF"
|
description = "Reduced Python frontend for eBPF"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
{ name = "r41k0u", email="pragyanshchaturvedi18@gmail.com" },
|
||||||
|
|||||||
@ -25,7 +25,7 @@ import re
|
|||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
VERSION = "v0.1.7"
|
VERSION = "v0.1.8"
|
||||||
|
|
||||||
|
|
||||||
def finalize_module(original_str):
|
def finalize_module(original_str):
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
BPF_CLANG := clang
|
BPF_CLANG := clang
|
||||||
CFLAGS := -emit-llvm -target bpf -c
|
CFLAGS := -emit-llvm -target bpf -c -D__TARGET_ARCH_x86
|
||||||
|
|
||||||
SRC := $(wildcard *.bpf.c)
|
SRC := $(wildcard *.bpf.c)
|
||||||
LL := $(SRC:.bpf.c=.bpf.ll)
|
LL := $(SRC:.bpf.c=.bpf.ll)
|
||||||
@ -10,7 +10,7 @@ LL0 := $(SRC:.bpf.c=.bpf.o0.ll)
|
|||||||
all: $(LL) $(OBJ) $(LL0)
|
all: $(LL) $(OBJ) $(LL0)
|
||||||
|
|
||||||
%.bpf.o: %.bpf.c
|
%.bpf.o: %.bpf.c
|
||||||
$(BPF_CLANG) -O2 -g -target bpf -c $< -o $@
|
$(BPF_CLANG) -O2 -D__TARGET_ARCH_x86 -g -target bpf -c $< -o $@
|
||||||
|
|
||||||
%.bpf.ll: %.bpf.c
|
%.bpf.ll: %.bpf.c
|
||||||
$(BPF_CLANG) $(CFLAGS) -O2 -g -S $< -o $@
|
$(BPF_CLANG) $(CFLAGS) -O2 -g -S $< -o $@
|
||||||
|
|||||||
@ -1,18 +1,16 @@
|
|||||||
#include <linux/bpf.h>
|
#include "vmlinux.h"
|
||||||
#include <linux/if_ether.h>
|
|
||||||
#include <linux/ip.h>
|
|
||||||
#include <bpf/bpf_helpers.h>
|
#include <bpf/bpf_helpers.h>
|
||||||
|
|
||||||
struct fake_iphdr {
|
struct fake_iphdr {
|
||||||
unsigned short useless;
|
unsigned short useless;
|
||||||
unsigned short tot_len;
|
unsigned short tot_len;
|
||||||
unsigned short id;
|
unsigned short id;
|
||||||
unsigned short frag_off;
|
unsigned short frag_off;
|
||||||
unsigned char ttl;
|
unsigned char ttl;
|
||||||
unsigned char protocol;
|
unsigned char protocol;
|
||||||
unsigned short check;
|
unsigned short check;
|
||||||
unsigned int saddr;
|
unsigned int saddr;
|
||||||
unsigned int daddr;
|
unsigned int daddr;
|
||||||
};
|
};
|
||||||
|
|
||||||
SEC("xdp")
|
SEC("xdp")
|
||||||
@ -25,9 +23,9 @@ int xdp_prog(struct xdp_md *ctx) {
|
|||||||
}
|
}
|
||||||
struct fake_iphdr *iph = (void *)data + sizeof(struct ethhdr);
|
struct fake_iphdr *iph = (void *)data + sizeof(struct ethhdr);
|
||||||
|
|
||||||
bpf_printk("%d", iph->saddr);
|
bpf_printk("%d", iph->saddr);
|
||||||
|
|
||||||
return XDP_PASS;
|
return XDP_PASS;
|
||||||
}
|
}
|
||||||
|
|
||||||
char _license[] SEC("license") = "GPL";
|
char _license[] SEC("license") = "GPL";
|
||||||
|
|||||||
Reference in New Issue
Block a user