History

bpf: sk_msg program helper bpf_msg_push_data

This allows user to push data into a msg using sk_msg program types. The format is as follows, bpf_msg_push_data(msg, offset, len, flags) this will insert 'len' bytes at offset 'offset'. For example to prepend 10 bytes at the front of the message the user can, bpf_msg_push_data(msg, 0, 10, 0); This will invalidate data bounds so BPF user will have to then recheck data bounds after calling this. After this the msg size will have been updated and the user is free to write into the added bytes. We allow any offset/len as long as it is within the (data, data_end) range. However, a copy will be required if the ring is full and its possible for the helper to fail with ENOMEM or EINVAL errors which need to be handled by the BPF program. This can be used similar to XDP metadata to pass data between sk_msg layer and lower layers.

John Fastabend committed 8 years ago

Commit 6fff6071 parent 5032d073 files · +158 −1Browse files

Showing the changes to include/uapi/linux (1 of 3 files). Show all

Unified · Split

include/uapi/linux/bpf.h+19−1
@@ -2240,6 +2240,23 @@ union bpf_attr { · 2239 unmodified lines
22402240 * pointer that was returned from bpf_sk_lookup_xxx\ ().
22412241 * Return
22422242 * 0 on success, or a negative error in case of failure.
2243+ *
2244+ * int bpf_msg_push_data(struct sk_buff *skb, u32 start, u32 len, u64 flags)
2245+ * Description
2246+ * For socket policies, insert *len* bytes into msg at offset
2247+ * *start*.
2248+ *
2249+ * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a
2250+ * *msg* it may want to insert metadata or options into the msg.
2251+ * This can later be read and used by any of the lower layer BPF
2252+ * hooks.
2253+ *
2254+ * This helper may fail if under memory pressure (a malloc
2255+ * fails) in these cases BPF programs will get an appropriate
2256+ * error and BPF programs will need to handle them.
2257+ *
2258+ * Return
2259+ * 0 on success, or a negative error in case of failure.
22432260 */
22442261 #define __BPF_FUNC_MAPPER(FN) \
22452262 FN(unspec), \
@@ -2331,7 +2348,8 @@ union bpf_attr { · 85 unmodified lines
23312348 FN(sk_release), \
23322349 FN(map_push_elem), \
23332350 FN(map_pop_elem), \
2334− FN(map_peek_elem),
2351+ FN(map_peek_elem), \
2352+ FN(msg_push_data),
23352353
23362354 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
23372355 * function eBPF program intends to call