History

bpf: Add csum_level helper for fixing up csum levels

Add a bpf_csum_level() helper which BPF programs can use in combination with bpf_skb_adjust_room() when they pass in BPF_F_ADJ_ROOM_NO_CSUM_RESET flag to the latter to avoid falling back to CHECKSUM_NONE. The bpf_csum_level() allows to adjust CHECKSUM_UNNECESSARY skb->csum_levels via BPF_CSUM_LEVEL_{INC,DEC} which calls __skb_{incr,decr}_checksum_unnecessary() on the skb. The helper also allows a BPF_CSUM_LEVEL_RESET which sets the skb's csum to CHECKSUM_NONE as well as a BPF_CSUM_LEVEL_QUERY to just return the current level. Without this helper, there is no way to otherwise adjust the skb->csum_level. I did not add an extra dummy flags as there is plenty of free bitspace in level argument itself iff ever needed in future.

Daniel Borkmann committed 6 years ago

Commit 7cdec541 parent 836e66c3 files · +122 −2Browse files

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

Unified · Split

include/uapi/linux/bpf.h+42−1
@@ -3220,6 +3220,38 @@ union bpf_attr { · 3219 unmodified lines
32203220 * calculation.
32213221 * Return
32223222 * Requested value, or 0, if flags are not recognized.
3223+ *
3224+ * int bpf_csum_level(struct sk_buff *skb, u64 level)
3225+ * Description
3226+ * Change the skbs checksum level by one layer up or down, or
3227+ * reset it entirely to none in order to have the stack perform
3228+ * checksum validation. The level is applicable to the following
3229+ * protocols: TCP, UDP, GRE, SCTP, FCOE. For example, a decap of
3230+ * | ETH | IP | UDP | GUE | IP | TCP | into | ETH | IP | TCP |
3231+ * through **bpf_skb_adjust_room**\ () helper with passing in
3232+ * **BPF_F_ADJ_ROOM_NO_CSUM_RESET** flag would require one call
3233+ * to **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_DEC** since
3234+ * the UDP header is removed. Similarly, an encap of the latter
3235+ * into the former could be accompanied by a helper call to
3236+ * **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_INC** if the
3237+ * skb is still intended to be processed in higher layers of the
3238+ * stack instead of just egressing at tc.
3239+ *
3240+ * There are three supported level settings at this time:
3241+ *
3242+ * * **BPF_CSUM_LEVEL_INC**: Increases skb->csum_level for skbs
3243+ * with CHECKSUM_UNNECESSARY.
3244+ * * **BPF_CSUM_LEVEL_DEC**: Decreases skb->csum_level for skbs
3245+ * with CHECKSUM_UNNECESSARY.
3246+ * * **BPF_CSUM_LEVEL_RESET**: Resets skb->csum_level to 0 and
3247+ * sets CHECKSUM_NONE to force checksum validation by the stack.
3248+ * * **BPF_CSUM_LEVEL_QUERY**: No-op, returns the current
3249+ * skb->csum_level.
3250+ * Return
3251+ * 0 on success, or a negative error in case of failure. In the
3252+ * case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level
3253+ * is returned or the error code -EACCES in case the skb is not
3254+ * subject to CHECKSUM_UNNECESSARY.
32233255 */
32243256 #define __BPF_FUNC_MAPPER(FN) \
32253257 FN(unspec), \
@@ -3356,7 +3388,8 @@ union bpf_attr { · 130 unmodified lines
33563388 FN(ringbuf_reserve), \
33573389 FN(ringbuf_submit), \
33583390 FN(ringbuf_discard), \
3359− FN(ringbuf_query),
3391+ FN(ringbuf_query), \
3392+ FN(csum_level),
33603393
33613394 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
33623395 * function eBPF program intends to call
@@ -3433,6 +3466,14 @@ enum { · 70 unmodified lines
34333466 BPF_F_CURRENT_NETNS = (-1L),
34343467 };
34353468
3469+/* BPF_FUNC_csum_level level values. */
3470+enum {
3471+ BPF_CSUM_LEVEL_QUERY,
3472+ BPF_CSUM_LEVEL_INC,
3473+ BPF_CSUM_LEVEL_DEC,
3474+ BPF_CSUM_LEVEL_RESET,
3475+};
3476+
34363477 /* BPF_FUNC_skb_adjust_room flags. */
34373478 enum {
34383479 BPF_F_ADJ_ROOM_FIXED_GSO = (1ULL << 0),

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t