Skip to main content

convert cbpf code to C for BCC

Project description

About pycbpf


Inspired by cbpfc.

pycbpf2c converts tcpdump filter args to C code which can be injected to a BCC script.

This aims to provide native python support, so bcc can import it directly.

And it provides a BCC script for dump filtered packet to pcap format.

You can save packets to a pcap file like below to sniffer packet from dev_queue_xmit

python3 -m pycbpf.c2ebpf -i eth0 -w file.pcap <tcpdump expresion>

Or you can pipe output to stdout and use tcpdump analyze

python3 -m pycbpf.c2ebpf -i eth0 <tcpdump expresion> | tcpdump -r -

Of course you can generate a C program from tcpdump expresion and implement your own bcc script. Command line below will generate the C program, which can be used directly in BCC.

python3 -m pycbpf.cbpf2c <tcpdump expression>

import package

from bcc import BPF
from pycbpf import cbpf2c, filter2cbpf

generate cbpf and compile to C program, and enable BPF for trace

prog = filter2cbpf.cbpf_prog(["ip"])
prog_c = cbpf2c.cbpf_c(prog)
cfun = prog_c.compile_cbpf_to_c()
test_text = bpf_text%cfun
bpf_ctx = BPF(text=test_text, debug=4)

For example, filter ip packets, will generate C program

static inline u32
cbpf_filter_func (const u8 *const data, const u8 *const data_end) {
      __attribute__((unused)) u32 A, X, M[16];
      __attribute__((unused)) const u8 *indirect;

      if (data + 12 > data_end) { return 0; }
      A = bpf_ntohs(*((u16 *)(data + 12)));
      if (A != 0x800) {goto label3;}
      return 262144;
label3:
      return 0;
}

Use the inline function cbpf_filter_func in you trace program and handle return value properly.

see c2ebpf.py as an example to save packets to pcap files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycbpf-0.0.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

pycbpf-0.0.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pycbpf-0.0.1.tar.gz.

File metadata

  • Download URL: pycbpf-0.0.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for pycbpf-0.0.1.tar.gz
Algorithm Hash digest
SHA256 33075d1edf903a70f669cc2fc9daef994b3bc5c68fa0055f38b7be6d764336bc
MD5 d419772ab47bf3144563f709c9e5dbf5
BLAKE2b-256 c31115ad6f381505909601a204bf00eee2b351653b59d093fae29861222b039d

See more details on using hashes here.

File details

Details for the file pycbpf-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pycbpf-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for pycbpf-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5e9b1c98b731f9aed5af9bcc4782406ee845985362968c53e1658f3f90d10f0
MD5 40499358e9d5c43bc5411b7ce04ad71e
BLAKE2b-256 2bae70c1f2811e25faf7503d055ae5ed64b7111edde8abe5682d3bed5c3c2fdc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page