Programmable Flow Protection (Beta)·续2
There are two state tables available to your program.
Caution
If an incoming packet's source IP is blocklisted in the source IP state table, the packet is immediately dropped and your program does not run.
Source IP state table
The source IP state table stores state keyed by source IP address. Each entry contains:
| Field | Type | Description |
|---|---|---|
| Status | Enum | The status of the source IP: None (0), Challenged (1), Verified (2), or Blocklisted (3). |
| User data | u64 |
A user-defined value you can set for any purpose. |
The default maximum capacity is 1,000 entries.
Use the following helper functions to interact with this table:
get_src_ip_status— Retrieve the status of the current packet's source IP.set_src_ip_status— Set the status of the current packet's source IP.get_src_ip_data— Retrieve the user data for the current packet's source IP.set_src_ip_data— Store user data for the current packet's source IP.
An entry is created in the source IP state table under the following conditions:
- the program calls
set_src_ip_statusto mark a source IP as Challenged, Verified, or Blocklisted. - the program calls
set_src_ip_datato store custom u64 data for a source IP. - the program calls
set_challengefor a new source IP that does not have an existing entry in the table.
Flow state table
The flow state table stores state keyed by the 4-tuple: source IP, source port, destination IP, and destination port. Each entry contains a u64 value you can set for any purpose.
The default maximum capacity is 10,000 entries.
Use the following helper functions to interact with this table:
get_flow_data— Retrieve the user data for the current flow.set_flow_data— Store user data for the current flow.
An entry is created in the flow state table under the following conditions:
- the program calls
set_flow_datato store custom u64 data for a flow.
Cache behavior
Both state tables are LRU (least recently used) caches. If a table reaches its maximum capacity, the oldest entry is evicted to make room for new entries. Entries are also garbage collected if they have not been accessed in one hour.
BPF helper functions and structures
A helper function is a function provided by the Cloudflare runtime that a customer program calls.
Helper functions are crucial because the BPF Instruction Set Architecture (ISA) only supports certain system calls. For safety purposes, Cloudflare will only compile a BPF object file with a predetermined list of known libraries that a program developer cannot modify.
Helper function definitions and verifier wrapper sources are available on GitHub ↗︎.
Note
Helper functions may be removed or changed. New helper functions may be introduced in the future.
Helper functions
parse_packet_data
Constructs cf_ebpf_parsed_headers from cf_ebpf_generic_ctx and cf_ebpf_packet_data. Performs required memory checks to pass the verifier.
static inline int parse_packet_data(
struct cf_ebpf_generic_ctx *ctx,
struct cf_ebpf_packet_data **out_p,
struct cf_ebpf_parsed_headers *out_headers
);
Arguments:
ctx— Pointer to the generic context passed into the BPF program.out_p— Pointer to receive the packet data structure.out_headers— Pointer to receive the parsed headers structure.
Returns: 0 on success, 1 on failure (for example, packet too short or invalid length). On success, out_headers contains valid IP and UDP header pointers.
rand
Generates a random unsigned integer.
uint64_t rand(void);
Returns: A random uint64_t value.
timestamp
Returns the current UNIX timestamp (number of non-leap seconds since January 1, 1970 0:00:00 UTC).
int64_t timestamp(void);
Returns: The current timestamp as an int64_t.
hash_md5
Computes MD5 hash of the source buffer and stores the result in the destination buffer.
int hash_md5(uint8_t *src, size_t src_len, uint8_t *dest, size_t dest_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to the destination buffer (must be at least 16 bytes).dest_len— Length of the destination buffer in bytes.
Returns:
- Positive value (number of bytes written) on success.
-1if the source buffer is invalid.-2if the destination buffer is null or too small.
hash_sha256
Computes SHA-256 hash of the source buffer and stores the result in the destination buffer.
int hash_sha256(uint8_t *src, size_t src_len, uint8_t *dest, size_t dest_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to the destination buffer (must be at least 32 bytes).dest_len— Length of the destination buffer in bytes.
Returns:
- Positive value (number of bytes written) on success.
-1if the source buffer is invalid.-2if the destination buffer is null or too small.
hash_sha512
Computes SHA-512 hash of the source buffer and stores the result in the destination buffer.
int hash_sha512(uint8_t *src, size_t src_len, uint8_t *dest, size_t dest_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to the destination buffer (must be at least 64 bytes).dest_len— Length of the destination buffer in bytes.
Returns:
- Positive value (number of bytes written) on success.
-1if the source buffer is invalid.-2if the destination buffer is null or too small.
hash_crc32
Computes CRC32 hash of the source buffer and stores the result as a 64-bit integer. This is a convenience wrapper that handles the byte-to-integer conversion internally.
int hash_crc32(uint8_t *src, size_t src_len, uint64_t *dest);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to auint64_tto receive the CRC32 result.
Returns:
- Positive value (number of bytes written internally, always 8) on success.
-1if the source buffer is invalid.-2if the destination buffer is null.
hash_blake2b512
Computes BLAKE2B-512 hash of the source buffer and stores the result in the destination buffer.
int hash_blake2b512(const uint8_t *src, size_t src_len, uint8_t *dest, size_t dest_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to the destination buffer (must be at least 64 bytes).dest_len— Length of the destination buffer in bytes.
Returns:
- Positive value (number of bytes written) on success.
-1if the source buffer is invalid.-2if the destination buffer is null or too small.
hmac_sha256
Computes HMAC-SHA256 of the source buffer and stores the result in the destination buffer. The private key is configured at the platform level and is not exposed directly to the BPF program. The key is unique per server and per customer.
int hmac_sha256(uint8_t *src, size_t src_len, uint8_t *dest, size_t dest_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to the destination buffer (must be at least 32 bytes).dest_len— Length of the destination buffer in bytes.
Returns:
- Positive value (number of bytes written) on success.
-1if the source buffer is invalid.-2if the destination buffer is null or too small.
hmac_sha512
Computes HMAC-SHA512 of the source buffer and stores the result in the destination buffer. The private key is configured at the platform level and is not exposed directly to the BPF program. The key is unique per server and per customer.
int hmac_sha512(uint8_t *src, size_t src_len, uint8_t *dest, size_t dest_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to the destination buffer (must be at least 64 bytes).dest_len— Length of the destination buffer in bytes.
Returns:
- Positive value (number of bytes written) on success.
-1if the source buffer is invalid.-2if the destination buffer is null or too small.
hmac_blake2b512
Computes BLAKE2B-512 HMAC of the source buffer and stores the result in the destination buffer. The private key is configured at the platform level and is not exposed directly to the BPF program. The key is unique per server and per customer.
int hmac_blake2b512(const uint8_t *src, size_t src_len, uint8_t *dest, size_t dest_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.dest— Pointer to the destination buffer (must be at least 64 bytes).dest_len— Length of the destination buffer in bytes.
Returns:
- Positive value (number of bytes written) on success.
-1if the source buffer is invalid.-2if the destination buffer is null or too small.
set_challenge
Sets challenge data for the current packet. Use this to send a challenge packet back to the client.
int set_challenge(uint8_t *src, size_t src_len);
Arguments:
src— Pointer to the challenge data buffer.src_len— Length of the challenge data in bytes. If0, the challenge buffer is reset.
Returns:
0on success.-4if the source buffer is invalid or exceeds the maximum allowed size.-5if challenges are not enabled.-6if a challenge was sent too recently to this source IP or the global rate limit is exceeded.
get_src_ip_status
Retrieves the status value associated with the source IP address from the state table.
int get_src_ip_status(uint8_t *status, uint64_t *expiry);
Arguments:
status— Pointer to receive the status value (CF_EBPF_SRC_IP_STATUS_CHALLENGED,CF_EBPF_SRC_IP_STATUS_VERIFIED, orCF_EBPF_SRC_IP_STATUS_BLOCKLISTED). Can be null if only expiry is needed.expiry— Pointer to receive the expiry timestamp. Can be null if only status is needed.
Returns:
0on success.-1if no entry exists for the source IP.-2if no source IP context is set for the current packet.-3if the provided buffer is too small.-4if bothstatusandexpiryare null.-5if the source IP state table is not enabled.
set_src_ip_status
Sets the status value associated with the source IP address in the state table.
int set_src_ip_status(uint8_t status, uint64_t expiry_secs);
Arguments:
status— The status value to set (CF_EBPF_SRC_IP_STATUS_CHALLENGED,CF_EBPF_SRC_IP_STATUS_VERIFIED, orCF_EBPF_SRC_IP_STATUS_BLOCKLISTED).expiry_secs— Number of seconds until the status expires. If0, the status never expires.
Returns:
0on success.-2if no source IP context is set for the current packet.-5if the source IP state table is not enabled.
get_src_ip_data
Retrieves custom data associated with the source IP address from the state table.
int get_src_ip_data(uint64_t *data);
Arguments:
data— Pointer to receive the stored data value.
Returns:
0on success.-1if no entry exists for the source IP.-2if no source IP context is set for the current packet.-3if the provided buffer is too small.-4ifdatais null.-5if the source IP state table is not enabled.
set_src_ip_data
Stores custom data associated with the source IP address in the state table.
int set_src_ip_data(uint64_t data);
Arguments:
data— The data value to store.
Returns:
0on success.-2if no source IP context is set for the current packet.-5if the source IP state table is not enabled.
get_flow_data
Retrieves custom data associated with the current flow from the state table.
int get_flow_data(uint64_t *data);
Arguments:
data— Pointer to receive the stored data value.
Returns:
0on success.-1if no entry exists for the flow.-2if no flow context is set for the current packet.-3if the provided buffer is too small.-4ifdatais null or unaligned.-5if the flow state table is not enabled.
set_flow_data
Stores custom data associated with the current flow in the state table.
int set_flow_data(uint64_t data);
Arguments:
data— The data value to store.
Returns:
0on success.-2if no flow context is set for the current packet.-5if the flow state table is not enabled.
entropy
Calculates the Shannon entropy of the source buffer. The result is returned in millibits, ranging from 0 (all identical bytes) to 8000 (all 256 byte values equally distributed).
int64_t entropy(uint8_t *src, size_t src_len);
Arguments:
src— Pointer to the source buffer.src_len— Length of the source buffer in bytes.
Returns:
- Entropy value in millibits (0-8000) on success.
-1if the source buffer is invalid.
set_network_analytics_tag
Sets a custom tag for network analytics reporting. The tag appears along with the packet sample in the Network Analytics dashboard. By default, packets are sampled at 1/10,000 rate. Only one tag is set per program execution. If program execution calls set_network_analytics_tag multiple times, the last tag value applies to the packet sample.
int set_network_analytics_tag(uint64_t tag);
Arguments:
tag— The tag value to set. Defaults to0if not set.
Returns: 0 on success.
ntohs
Converts a 16-bit integer from network byte order to host byte order.
uint16_t ntohs(uint16_t netshort);
Arguments:
