History

bpf: Remove BPF_SKB_DELIVERY_TIME_NONE and rename s/delivery_time_/tstamp_/

This patch is to simplify the uapi bpf.h regarding to the tstamp type and use a similar way as the kernel to describe the value stored in __sk_buff->tstamp. My earlier thought was to avoid describing the semantic and clock base for the rcv timestamp until there is more clarity on the use case, so the __sk_buff->delivery_time_type naming instead of __sk_buff->tstamp_type. With some thoughts, it can reuse the UNSPEC naming. This patch first removes BPF_SKB_DELIVERY_TIME_NONE and also rename BPF_SKB_DELIVERY_TIME_UNSPEC to BPF_SKB_TSTAMP_UNSPEC and BPF_SKB_DELIVERY_TIME_MONO to BPF_SKB_TSTAMP_DELIVERY_MONO. The semantic of BPF_SKB_TSTAMP_DELIVERY_MONO is the same: __sk_buff->tstamp has delivery time in mono clock base. BPF_SKB_TSTAMP_UNSPEC means __sk_buff->tstamp has the (rcv) tstamp at ingress and the delivery time at egress. At egress, the clock base could be found from skb->sk->sk_clockid. __sk_buff->tstamp == 0 naturally means NONE, so NONE is not needed. With BPF_SKB_TSTAMP_UNSPEC for the rcv tstamp at ingress, the __sk_buff->delivery_time_type is also renamed to __sk_buff->tstamp_type which was also suggested in the earlier discussion: https://lore.kernel.org/bpf/b181acbe-caf8-502d-4b7b-7d96b9fc5d55@iogearbox.net/ The above will then make __sk_buff->tstamp and __sk_buff->tstamp_type the same as its kernel skb->tstamp and skb->mono_delivery_time counter part. The internal kernel function bpf_skb_convert_dtime_type_read() is then renamed to bpf_skb_convert_tstamp_type_read() and it can be simplified with the BPF_SKB_DELIVERY_TIME_NONE gone. A BPF_ALU32_IMM(BPF_AND) insn is also saved by using BPF_JMP32_IMM(BPF_JSET). The bpf helper bpf_skb_set_delivery_time() is also renamed to bpf_skb_set_tstamp(). The arg name is changed from dtime to tstamp also. It only allows setting tstamp 0 for BPF_SKB_TSTAMP_UNSPEC and it could be relaxed later if there is use case to change mono delivery time to non mono. prog->delivery_time_access is also renamed to prog->tstamp_type_access.

Martin KaFai Lau committed 4 years ago

Commit 9bb984f1 parent 9d90db94 files · +77 −93Browse files

Unified · Split

4 files
  1. include/linux/filter.h +1 −1
  2. include/uapi/linux/bpf.h +21 −19
  3. net/core/filter.c +34 −54
  4. tools/include/uapi/linux/bpf.h +21 −19
include/linux/filter.h+1−1
@@ -573,7 +573,7 @@ struct bpf_prog { · 572 unmodified lines
573573 enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
574574 call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
575575 call_get_func_ip:1, /* Do we call get_func_ip() */
576− delivery_time_access:1; /* Accessed __sk_buff->delivery_time_type */
576+ tstamp_type_access:1; /* Accessed __sk_buff->tstamp_type */
577577 enum bpf_prog_type type; /* Type of BPF program */
578578 enum bpf_attach_type expected_attach_type; /* For some prog types */
579579 u32 len; /* Number of filter blocks */
include/uapi/linux/bpf.h+21−19
@@ -5090,23 +5090,22 @@ union bpf_attr { · 5089 unmodified lines
50905090 * 0 on success, or a negative error in case of failure. On error
50915091 * *dst* buffer is zeroed out.
50925092 *
5093− * long bpf_skb_set_delivery_time(struct sk_buff *skb, u64 dtime, u32 dtime_type)
5093+ * long bpf_skb_set_tstamp(struct sk_buff *skb, u64 tstamp, u32 tstamp_type)
50945094 * Description
5095− * Set a *dtime* (delivery time) to the __sk_buff->tstamp and also
5096− * change the __sk_buff->delivery_time_type to *dtime_type*.
5095+ * Change the __sk_buff->tstamp_type to *tstamp_type*
5096+ * and set *tstamp* to the __sk_buff->tstamp together.
50975097 *
5098− * When setting a delivery time (non zero *dtime*) to
5099− * __sk_buff->tstamp, only BPF_SKB_DELIVERY_TIME_MONO *dtime_type*
5100− * is supported. It is the only delivery_time_type that will be
5101− * kept after bpf_redirect_*().
5102− *
5103− * If there is no need to change the __sk_buff->delivery_time_type,
5104− * the delivery time can be directly written to __sk_buff->tstamp
5098+ * If there is no need to change the __sk_buff->tstamp_type,
5099+ * the tstamp value can be directly written to __sk_buff->tstamp
51055100 * instead.
51065101 *
5107− * *dtime* 0 and *dtime_type* BPF_SKB_DELIVERY_TIME_NONE
5108− * can be used to clear any delivery time stored in
5109− * __sk_buff->tstamp.
5102+ * BPF_SKB_TSTAMP_DELIVERY_MONO is the only tstamp that
5103+ * will be kept during bpf_redirect_*(). A non zero
5104+ * *tstamp* must be used with the BPF_SKB_TSTAMP_DELIVERY_MONO
5105+ * *tstamp_type*.
5106+ *
5107+ * A BPF_SKB_TSTAMP_UNSPEC *tstamp_type* can only be used
5108+ * with a zero *tstamp*.
51105109 *
51115110 * Only IPv4 and IPv6 skb->protocol are supported.
51125111 *
@@ -5119,7 +5118,7 @@ union bpf_attr { · 6 unmodified lines
51195118 * Return
51205119 * 0 on success.
51215120 * **-EINVAL** for invalid input
5122− * **-EOPNOTSUPP** for unsupported delivery_time_type and protocol
5121+ * **-EOPNOTSUPP** for unsupported protocol
51235122 */
51245123 #define __BPF_FUNC_MAPPER(FN) \
51255124 FN(unspec), \
@@ -5314,7 +5313,7 @@ union bpf_attr { · 188 unmodified lines
53145313 FN(xdp_load_bytes), \
53155314 FN(xdp_store_bytes), \
53165315 FN(copy_from_user_task), \
5317− FN(skb_set_delivery_time), \
5316+ FN(skb_set_tstamp), \
53185317 /* */
53195318
53205319 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -5505,9 +5504,12 @@ union { \ · 184 unmodified lines
55055504 } __attribute__((aligned(8)))
55065505
55075506 enum {
5508− BPF_SKB_DELIVERY_TIME_NONE,
5509− BPF_SKB_DELIVERY_TIME_UNSPEC,
5510− BPF_SKB_DELIVERY_TIME_MONO,
5507+ BPF_SKB_TSTAMP_UNSPEC,
5508+ BPF_SKB_TSTAMP_DELIVERY_MONO, /* tstamp has mono delivery time */
5509+ /* For any BPF_SKB_TSTAMP_* that the bpf prog cannot handle,
5510+ * the bpf prog should handle it like BPF_SKB_TSTAMP_UNSPEC
5511+ * and try to deduce it by ingress, egress or skb->sk->sk_clockid.
5512+ */
55115513 };
55125514
55135515 /* user accessible mirror of in-kernel sk_buff.
@@ -5550,7 +5552,7 @@ struct __sk_buff { · 36 unmodified lines
55505552 __u32 gso_segs;
55515553 __bpf_md_ptr(struct bpf_sock *, sk);
55525554 __u32 gso_size;
5553− __u8 delivery_time_type;
5555+ __u8 tstamp_type;
55545556 __u32 :24; /* Padding, future use. */
55555557 __u64 hwtstamp;
55565558 };
net/core/filter.c+34−54
@@ -7388,36 +7388,36 @@ static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = { · 7387 unmodified lines
73887388 .arg3_type = ARG_ANYTHING,
73897389 };
73907390
7391−BPF_CALL_3(bpf_skb_set_delivery_time, struct sk_buff *, skb,
7392− u64, dtime, u32, dtime_type)
7391+BPF_CALL_3(bpf_skb_set_tstamp, struct sk_buff *, skb,
7392+ u64, tstamp, u32, tstamp_type)
73937393 {
73947394 /* skb_clear_delivery_time() is done for inet protocol */
73957395 if (skb->protocol != htons(ETH_P_IP) &&
73967396 skb->protocol != htons(ETH_P_IPV6))
73977397 return -EOPNOTSUPP;
73987398
7399− switch (dtime_type) {
7400− case BPF_SKB_DELIVERY_TIME_MONO:
7401− if (!dtime)
7399+ switch (tstamp_type) {
7400+ case BPF_SKB_TSTAMP_DELIVERY_MONO:
7401+ if (!tstamp)
74027402 return -EINVAL;
7403− skb->tstamp = dtime;
7403+ skb->tstamp = tstamp;
74047404 skb->mono_delivery_time = 1;
74057405 break;
7406− case BPF_SKB_DELIVERY_TIME_NONE:
7407− if (dtime)
7406+ case BPF_SKB_TSTAMP_UNSPEC:
7407+ if (tstamp)
74087408 return -EINVAL;
74097409 skb->tstamp = 0;
74107410 skb->mono_delivery_time = 0;
74117411 break;
74127412 default:
7413− return -EOPNOTSUPP;
7413+ return -EINVAL;
74147414 }
74157415
74167416 return 0;
74177417 }
74187418
7419−static const struct bpf_func_proto bpf_skb_set_delivery_time_proto = {
7420− .func = bpf_skb_set_delivery_time,
7419+static const struct bpf_func_proto bpf_skb_set_tstamp_proto = {
7420+ .func = bpf_skb_set_tstamp,
74217421 .gpl_only = false,
74227422 .ret_type = RET_INTEGER,
74237423 .arg1_type = ARG_PTR_TO_CTX,
@@ -7786,8 +7786,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 362 unmodified lines
77867786 return &bpf_tcp_gen_syncookie_proto;
77877787 case BPF_FUNC_sk_assign:
77887788 return &bpf_sk_assign_proto;
7789− case BPF_FUNC_skb_set_delivery_time:
7790− return &bpf_skb_set_delivery_time_proto;
7789+ case BPF_FUNC_skb_set_tstamp:
7790+ return &bpf_skb_set_tstamp_proto;
77917791 #endif
77927792 default:
77937793 return bpf_sk_base_func_proto(func_id);
@@ -8127,9 +8127,9 @@ static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type · 333 unmodified lines
81278127 return false;
81288128 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
81298129 break;
8130− case offsetof(struct __sk_buff, delivery_time_type):
8130+ case offsetof(struct __sk_buff, tstamp_type):
81318131 return false;
8132− case offsetofend(struct __sk_buff, delivery_time_type) ... offsetof(struct __sk_buff, hwtstamp) - 1:
8132+ case offsetofend(struct __sk_buff, tstamp_type) ... offsetof(struct __sk_buff, hwtstamp) - 1:
81338133 /* Explicitly prohibit access to padding in __sk_buff. */
81348134 return false;
81358135 default:
@@ -8484,14 +8484,14 @@ static bool tc_cls_act_is_valid_access(int off, int size, · 348 unmodified lines
84848484 break;
84858485 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
84868486 return false;
8487− case offsetof(struct __sk_buff, delivery_time_type):
8487+ case offsetof(struct __sk_buff, tstamp_type):
84888488 /* The convert_ctx_access() on reading and writing
84898489 * __sk_buff->tstamp depends on whether the bpf prog
8490− * has used __sk_buff->delivery_time_type or not.
8491− * Thus, we need to set prog->delivery_time_access
8490+ * has used __sk_buff->tstamp_type or not.
8491+ * Thus, we need to set prog->tstamp_type_access
84928492 * earlier during is_valid_access() here.
84938493 */
8494− ((struct bpf_prog *)prog)->delivery_time_access = 1;
8494+ ((struct bpf_prog *)prog)->tstamp_type_access = 1;
84958495 return size == sizeof(__u8);
84968496 }
84978497
@@ -8888,42 +8888,22 @@ static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type, · 390 unmodified lines
88888888 return insn - insn_buf;
88898889 }
88908890
8891−static struct bpf_insn *bpf_convert_dtime_type_read(const struct bpf_insn *si,
8892− struct bpf_insn *insn)
8891+static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si,
8892+ struct bpf_insn *insn)
88938893 {
88948894 __u8 value_reg = si->dst_reg;
88958895 __u8 skb_reg = si->src_reg;
8896+ /* AX is needed because src_reg and dst_reg could be the same */
88968897 __u8 tmp_reg = BPF_REG_AX;
88978898
88988899 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg,
88998900 PKT_VLAN_PRESENT_OFFSET);
8900− *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg,
8901− SKB_MONO_DELIVERY_TIME_MASK);
8902− *insn++ = BPF_JMP32_IMM(BPF_JEQ, tmp_reg, 0, 2);
8903− /* value_reg = BPF_SKB_DELIVERY_TIME_MONO */
8904− *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_DELIVERY_TIME_MONO);
8905− *insn++ = BPF_JMP_A(IS_ENABLED(CONFIG_NET_CLS_ACT) ? 10 : 5);
8906−
8907− *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, skb_reg,
8908− offsetof(struct sk_buff, tstamp));
8909− *insn++ = BPF_JMP_IMM(BPF_JNE, tmp_reg, 0, 2);
8910− /* value_reg = BPF_SKB_DELIVERY_TIME_NONE */
8911− *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_DELIVERY_TIME_NONE);
8912− *insn++ = BPF_JMP_A(IS_ENABLED(CONFIG_NET_CLS_ACT) ? 6 : 1);
8913−
8914−#ifdef CONFIG_NET_CLS_ACT
8915− *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, PKT_VLAN_PRESENT_OFFSET);
8916− *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, TC_AT_INGRESS_MASK);
8917− *insn++ = BPF_JMP32_IMM(BPF_JEQ, tmp_reg, 0, 2);
8918− /* At ingress, value_reg = 0 */
8919− *insn++ = BPF_MOV32_IMM(value_reg, 0);
8901+ *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg,
8902+ SKB_MONO_DELIVERY_TIME_MASK, 2);
8903+ *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_UNSPEC);
89208904 *insn++ = BPF_JMP_A(1);
8921−#endif
8922−
8923− /* value_reg = BPF_SKB_DELIVERYT_TIME_UNSPEC */
8924− *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_DELIVERY_TIME_UNSPEC);
8905+ *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_DELIVERY_MONO);
89258906
8926− /* 15 insns with CONFIG_NET_CLS_ACT */
89278907 return insn;
89288908 }
89298909
@@ -8956,11 +8936,11 @@ static struct bpf_insn *bpf_convert_tstamp_read(const struct bpf_prog *prog, · 26 unmodified lines
89568936 __u8 skb_reg = si->src_reg;
89578937
89588938 #ifdef CONFIG_NET_CLS_ACT
8959− /* If the delivery_time_type is read,
8939+ /* If the tstamp_type is read,
89608940 * the bpf prog is aware the tstamp could have delivery time.
8961− * Thus, read skb->tstamp as is if delivery_time_access is true.
8941+ * Thus, read skb->tstamp as is if tstamp_type_access is true.
89628942 */
8963− if (!prog->delivery_time_access) {
8943+ if (!prog->tstamp_type_access) {
89648944 /* AX is needed because src_reg and dst_reg could be the same */
89658945 __u8 tmp_reg = BPF_REG_AX;
89668946
@@ -8990,13 +8970,13 @@ static struct bpf_insn *bpf_convert_tstamp_write(const struct bpf_prog *prog, · 23 unmodified lines
89908970 __u8 skb_reg = si->dst_reg;
89918971
89928972 #ifdef CONFIG_NET_CLS_ACT
8993− /* If the delivery_time_type is read,
8973+ /* If the tstamp_type is read,
89948974 * the bpf prog is aware the tstamp could have delivery time.
8995− * Thus, write skb->tstamp as is if delivery_time_access is true.
8975+ * Thus, write skb->tstamp as is if tstamp_type_access is true.
89968976 * Otherwise, writing at ingress will have to clear the
89978977 * mono_delivery_time bit also.
89988978 */
8999− if (!prog->delivery_time_access) {
8979+ if (!prog->tstamp_type_access) {
90008980 __u8 tmp_reg = BPF_REG_AX;
90018981
90028982 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, PKT_VLAN_PRESENT_OFFSET);
@@ -9329,8 +9309,8 @@ static u32 bpf_convert_ctx_access(enum bpf_access_type type, · 326 unmodified lines
93299309 insn = bpf_convert_tstamp_read(prog, si, insn);
93309310 break;
93319311
9332− case offsetof(struct __sk_buff, delivery_time_type):
9333− insn = bpf_convert_dtime_type_read(si, insn);
9312+ case offsetof(struct __sk_buff, tstamp_type):
9313+ insn = bpf_convert_tstamp_type_read(si, insn);
93349314 break;
93359315
93369316 case offsetof(struct __sk_buff, gso_segs):
tools/include/uapi/linux/bpf.h+21−19
@@ -5090,23 +5090,22 @@ union bpf_attr { · 5089 unmodified lines
50905090 * 0 on success, or a negative error in case of failure. On error
50915091 * *dst* buffer is zeroed out.
50925092 *
5093− * long bpf_skb_set_delivery_time(struct sk_buff *skb, u64 dtime, u32 dtime_type)
5093+ * long bpf_skb_set_tstamp(struct sk_buff *skb, u64 tstamp, u32 tstamp_type)
50945094 * Description
5095− * Set a *dtime* (delivery time) to the __sk_buff->tstamp and also
5096− * change the __sk_buff->delivery_time_type to *dtime_type*.
5095+ * Change the __sk_buff->tstamp_type to *tstamp_type*
5096+ * and set *tstamp* to the __sk_buff->tstamp together.
50975097 *
5098− * When setting a delivery time (non zero *dtime*) to
5099− * __sk_buff->tstamp, only BPF_SKB_DELIVERY_TIME_MONO *dtime_type*
5100− * is supported. It is the only delivery_time_type that will be
5101− * kept after bpf_redirect_*().
5102− *
5103− * If there is no need to change the __sk_buff->delivery_time_type,
5104− * the delivery time can be directly written to __sk_buff->tstamp
5098+ * If there is no need to change the __sk_buff->tstamp_type,
5099+ * the tstamp value can be directly written to __sk_buff->tstamp
51055100 * instead.
51065101 *
5107− * *dtime* 0 and *dtime_type* BPF_SKB_DELIVERY_TIME_NONE
5108− * can be used to clear any delivery time stored in
5109− * __sk_buff->tstamp.
5102+ * BPF_SKB_TSTAMP_DELIVERY_MONO is the only tstamp that
5103+ * will be kept during bpf_redirect_*(). A non zero
5104+ * *tstamp* must be used with the BPF_SKB_TSTAMP_DELIVERY_MONO
5105+ * *tstamp_type*.
5106+ *
5107+ * A BPF_SKB_TSTAMP_UNSPEC *tstamp_type* can only be used
5108+ * with a zero *tstamp*.
51105109 *
51115110 * Only IPv4 and IPv6 skb->protocol are supported.
51125111 *
@@ -5119,7 +5118,7 @@ union bpf_attr { · 6 unmodified lines
51195118 * Return
51205119 * 0 on success.
51215120 * **-EINVAL** for invalid input
5122− * **-EOPNOTSUPP** for unsupported delivery_time_type and protocol
5121+ * **-EOPNOTSUPP** for unsupported protocol
51235122 */
51245123 #define __BPF_FUNC_MAPPER(FN) \
51255124 FN(unspec), \
@@ -5314,7 +5313,7 @@ union bpf_attr { · 188 unmodified lines
53145313 FN(xdp_load_bytes), \
53155314 FN(xdp_store_bytes), \
53165315 FN(copy_from_user_task), \
5317− FN(skb_set_delivery_time), \
5316+ FN(skb_set_tstamp), \
53185317 /* */
53195318
53205319 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -5505,9 +5504,12 @@ union { \ · 184 unmodified lines
55055504 } __attribute__((aligned(8)))
55065505
55075506 enum {
5508− BPF_SKB_DELIVERY_TIME_NONE,
5509− BPF_SKB_DELIVERY_TIME_UNSPEC,
5510− BPF_SKB_DELIVERY_TIME_MONO,
5507+ BPF_SKB_TSTAMP_UNSPEC,
5508+ BPF_SKB_TSTAMP_DELIVERY_MONO, /* tstamp has mono delivery time */
5509+ /* For any BPF_SKB_TSTAMP_* that the bpf prog cannot handle,
5510+ * the bpf prog should handle it like BPF_SKB_TSTAMP_UNSPEC
5511+ * and try to deduce it by ingress, egress or skb->sk->sk_clockid.
5512+ */
55115513 };
55125514
55135515 /* user accessible mirror of in-kernel sk_buff.
@@ -5550,7 +5552,7 @@ struct __sk_buff { · 36 unmodified lines
55505552 __u32 gso_segs;
55515553 __bpf_md_ptr(struct bpf_sock *, sk);
55525554 __u32 gso_size;
5553− __u8 delivery_time_type;
5555+ __u8 tstamp_type;
55545556 __u32 :24; /* Padding, future use. */
55555557 __u64 hwtstamp;
55565558 };