BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASH
BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASH maps can be used to redirect skbs between sockets or to apply policy at the socket level based on the result of a BPF (verdict) program with the help of the BPF helpers bpf_sk_redirect_map(), bpf_sk_redirect_hash(), bpf_msg_redirect_map() and bpf_msg_redirect_hash().
BPF_MAP_TYPE_SOCKMAP is backed by an array that uses an integer key as the index to look up a reference to a struct sock. The map values are socket descriptors. Similarly, BPF_MAP_TYPE_SOCKHASH is a hash backed BPF map that holds references to sockets via their socket descriptors.
These maps may have BPF programs attached to them, specifically a parser program and a verdict program. The parser program determines how much data has been parsed and therefore how much data needs to be queued to come to a verdict. The verdict program is essentially the redirect program and can return a verdict of __SK_DROP, __SK_PASS, or __SK_REDIRECT.
When a socket is inserted into one of these maps, its socket callbacks are replaced and a struct sk_psock is attached to it. Additionally, this sk_psock inherits the programs that are attached to the map.
A sock object may be in multiple maps, but can only inherit a single parse or verdict program. If adding a sock object to a map would result in having multiple parser programs the update will return an EBUSY error.
The supported programs to attach to these maps are:
;
The attach types for the map programs are:
-
msg_parserprogram -BPF_SK_MSG_VERDICT. -
stream_parserprogram -BPF_SK_SKB_STREAM_PARSER. -
stream_verdictprogram -BPF_SK_SKB_STREAM_VERDICT. -
skb_verdictprogram -BPF_SK_SKB_VERDICT.
There are additional helpers available to use with the parser and verdict programs: bpf_msg_apply_bytes() and bpf_msg_cork_bytes(). With bpf_msg_apply_bytes() BPF programs can tell the infrastructure how many bytes the given verdict should apply to. The helper bpf_msg_cork_bytes() handles a different case where a BPF program cannot reach a verdict on a msg until it receives more bytes AND the program doesn't want to forward the packet until it is known to be good.
Finally, the helpers bpf_msg_pull_data() and bpf_msg_push_data() are available to BPF_PROG_TYPE_SK_MSG BPF programs to pull in data and set the start and end pointers to given values or to add metadata to the struct sk_msg_buff *msg.
All these helpers will be described in more detail below.
Usage #
Kernel BPF #
bpf_msg_redirect_map() #
long This helper is used in programs implementing policies at the socket level. If the message msg is allowed to pass (i.e., if the verdict BPF program returns SK_PASS), redirect it to the socket referenced by map (of type BPF_MAP_TYPE_SOCKMAP) at index key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_sk_redirect_map() #
long Redirect the packet to the socket referenced by map (of type BPF_MAP_TYPE_SOCKMAP) at index key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_map_lookup_elem() #
void *socket entries of type struct sock * can be retrieved using the bpf_map_lookup_elem() helper.
bpf_sock_map_update() #
long Add an entry to, or update a map referencing sockets. The skops is used as a new value for the entry associated to key. The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element. -
BPF_NOEXIST: Create a new element only if it did not exist. -
BPF_EXIST: Update an existing element.
If the map has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.
Returns 0 on success, or a negative error in case of failure.
bpf_sock_hash_update() #
long Add an entry to, or update a sockhash map referencing sockets. The skops is used as a new value for the entry associated to key.
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element. -
BPF_NOEXIST: Create a new element only if it did not exist. -
BPF_EXIST: Update an existing element.
If the map has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.
Returns 0 on success, or a negative error in case of failure.
bpf_msg_redirect_hash() #
long This helper is used in programs implementing policies at the socket level. If the message msg is allowed to pass (i.e., if the verdict BPF program returns SK_PASS), redirect it to the socket referenced by map (of type BPF_MAP_TYPE_SOCKHASH) using hash key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_sk_redirect_hash() #
long This helper is used in programs implementing policies at the skb socket level. If the sk_buff skb is allowed to pass (i.e., if the verdict BPF program returns SK_PASS), redirect it to the socket referenced by map (of type BPF_MAP_TYPE_SOCKHASH) using hash key. Both ingress and egress interfaces can be used for redirection. The BPF_F_INGRESS value in flags is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.
Returns SK_PASS on success, or SK_DROP on error.
bpf_msg_apply_bytes() #
long For socket policies, apply the verdict of the BPF program to the next (number of bytes) of message msg. For example, this helper can be used in the following cases:
-
A single
sendmsg()orsendfile()system call contains multiple logical messages that the BPF program is supposed to read and for which it should apply a verdict. -
A BPF program only cares to read the first
bytesof amsg. If the message has a large payload, then setting up and calling the BPF program repeatedly for all bytes, even though the verdict is already known, would create unnecessary overhead.
Returns 0
bpf_msg_cork_bytes() #
long For socket policies, prevent the execution of the verdict BPF program for message msg until the number of bytes have been accumulated.
This can be used when one needs a specific number of bytes before a verdict can be assigned, even if the data spans multiple sendmsg() or sendfile() calls.
Returns 0
bpf_msg_pull_data() #
long For socket policies, pull in non-linear data from user space for msg and set pointers msg->data and msg->data_end to start and end bytes offsets into msg, respectively.
If a program of type BPF_PROG_TYPE_SK_MSG is run on a msg it can only parse data that the (data, data_end) pointers have already consumed. For sendmsg() hooks this is likely the first scatterlist element. But for calls relying on MSG_SPLICE_PAGES (e.g., sendfile()) this will be the range (0, 0) because the data is shared with user space and by default the objective is to avoid allowing user space to modify data while (or after) BPF verdict is being decided. This helper can be used to pull in data and to set the start and end pointers to given values. Data will be copied if necessary (i.e., if data was not linear and if start and end pointers do not point to the same chunk).
A call to this helper is susceptible to change the underlying packet buffer. Therefore, at load time, all checks on pointers previously done by the verifier are invalidated and must be performed again, if the helper is used in combination with direct packet access.
All values for flags are reserved for future usage, and must be left at zero.
Returns 0 on success, or a negative error in case of failure.
bpf_map_lookup_elem() #
void *Look up a socket entry in the sockmap or sockhash map.
Returns the socket entry associated to key, or NULL if no entry was found.
bpf_map_update_elem() #
long Add or update a socket entry in a sockmap or sockhash.
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element.
-
BPF_NOEXIST: Create a new element only if it did not exist.
-
BPF_EXIST: Update an existing element.
Returns 0 on success, or a negative error in case of failure.
bpf_map_delete_elem() #
long Delete a socket entry from a sockmap or a sockhash.
Returns 0 on success, or a negative error in case of failure.
User space #
bpf_map_update_elem() #
int Sockmap entries can be added or updated using the bpf_map_update_elem() function. The key parameter is the index value of the sockmap array. And the value parameter is the FD value of that socket.
Under the hood, the sockmap update function uses the socket FD value to retrieve the associated socket and its attached psock.
The flags argument can be one of the following:
-
BPF_ANY: Create a new element or update an existing element.
-
BPF_NOEXIST: Create a new element only if it did not exist.
-
BPF_EXIST: Update an existing element.
bpf_map_lookup_elem() #
int Sockmap entries can be retrieved using the bpf_map_lookup_elem() function.
bpf_map_delete_elem() #
int Sockmap entries can be deleted using the bpf_map_delete_elem() function.
Returns 0 on success, or negative error in case of failure.
Examples #
Kernel BPF #
Several examples of the use of sockmap APIs can be found in:
The following code snippet shows how to declare a sockmap.
sock_map_rx ;
The following code snippet shows a sample parser program.
int
The following code snippet shows a simple verdict program that interacts with a sockmap to redirect traffic to another socket based on the local port.
int
The following code snippet shows how to declare a sockhash map.
;
sock_hash_rx ;
The following code snippet shows a simple verdict program that interacts with a sockhash to redirect traffic to another socket based on a hash of some of the skb parameters.
static inline
void
int
User space #
Several examples of the use of sockmap APIs can be found in:
The following code sample shows how to create a sockmap, attach a parser and verdict program, as well as add a socket entry.
int