Skip to content

Program type BPF_PROG_TYPE_LWT_XMIT

v4.10

Extension programs can be used to dynamically extend another BPF program.

Usage

This program type can be attached to route, the program will be called when transmitting to said route. For example

ip route add 192.168.253.2/32 encap bpf out obj {elf file}.o section {prog section} dev veth0

The initial use cases listed for this program type are:

  • Collect statistics and generate sampling data for a subset of traffic based on the destination utilized by the packet thus allowing to extend the existing realms.
  • Apply additional per route/destination filters to prohibit certain outgoing or incoming packets based on BPF filters. In particular, this allows to maintain per destination custom state across multiple packets in BPF maps and apply filters based on statistics and behaviour observed over time.
  • Attachment of L2 headers at transmit where resolving the L2 address is not required.

LWT xmit programs are called after the IP header has been assembled and thus it can safely modify the packet. The program can also prepend a L2 header to the packet using the bpf_skb_change_head helper function.

Context

Socket SKB programs are called by the kernel with a __sk_buff context.

This program type isn't allowed to read from and write to all fields of the context since doing so might break assumptions in the kernel or because data isn't available at the point where the program is hooked into the kernel.

Context fields
Field Read Write
len
pkt_type
mark
queue_mapping
protocol
vlan_present
vlan_tci
vlan_proto
priority
ingress_ifindex
ifindex
tc_index
cb
hash
tc_classid
data
data_end
napi_id
family
remote_ip4
local_ip4
remote_ip4
remote_ip6
local_ip6
remote_port
local_port
data_meta
flow_keys
tstamp
wire_len
gso_segs
sk
gso_size
tstamp_type
hwtstamp

Attachment

This program type can only be attached via netlink or commands such as ip from (iproute2) which use netlink under the hood:

ip route add 192.168.253.2/32 encap bpf xmit obj {elf file}.o section {prog section} dev veth0

Example

Docs could be improved

This part of the docs is incomplete, contributions are very welcome

Helper functions

Not all helper functions are available in all program types. These are the helper calls available for LWT programs:

Supported helper functions

KFuncs

Supported kfuncs