C Fundamentals

Functions and Arrays

Functions and arrays - function calls in BPF are subject to special rules.

1.6 Functions and Storage Classes

  • Defining and calling functions
  • Parameter passing (by value, by pointer)
  • Return values
  • Storage classes: auto, register, static, extern

Used in BPF:

  • static -> file-scope visibility
  • __always_inline -> mandatory inlining
  • extern -> map reference (from userspace)

static __always_inline is mandatory in BPF - no normal function calls (except bpf-to-bpf calls on kernel 4.16+).

// WRONG (does not work in BPF)
int parse_header(void *data) { ... }

// CORRECT
static __always_inline int parse_header(void *data) { ... }

eBPFHub: All exercises use static __always_inline helper functions.

See in xdp-tutorial: common/parsing_helpers.h - all parse_* functions are inline.


1.7 Arrays

  • Array definition and initialization
  • Index access
  • Multi-dimensional arrays
  • Relationship between arrays and pointers

In BPF: MAC addresses (__u8[6]), IP addresses (__u8[4] or __u8[16]), syscall argument arrays (args[6]).

eBPFHub: In the “Reading syscall arrays” exercise, you will see char **argv pointer array traversal and the args[6] syscall argument array.