History

bpf: Switch BPF UAPI #define constants used from BPF program side to enums

Switch BPF UAPI constants, previously defined as #define macro, to anonymous enum values. This preserves constants values and behavior in expressions, but has added advantaged of being captured as part of DWARF and, subsequently, BTF type info. Which, in turn, greatly improves usefulness of generated vmlinux.h for BPF applications, as it will not require BPF users to copy/paste various flags and constants, which are frequently used with BPF helpers. Only those constants that are used/useful from BPF program side are converted.

Andrii Nakryiko committed 6 years ago

Commit 1aae4bd1 parent 320a3602 files · +219 −133Browse files

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

Unified · Split

include/uapi/linux/bpf.h+109−66
@@ -325,44 +325,46 @@ enum bpf_attach_type { · 324 unmodified lines
325325 #define BPF_PSEUDO_CALL 1
326326
327327 /* flags for BPF_MAP_UPDATE_ELEM command */
328−#define BPF_ANY 0 /* create new element or update existing */
329−#define BPF_NOEXIST 1 /* create new element if it didn't exist */
330−#define BPF_EXIST 2 /* update existing element */
331−#define BPF_F_LOCK 4 /* spin_lock-ed map_lookup/map_update */
328+enum {
329+ BPF_ANY = 0, /* create new element or update existing */
330+ BPF_NOEXIST = 1, /* create new element if it didn't exist */
331+ BPF_EXIST = 2, /* update existing element */
332+ BPF_F_LOCK = 4, /* spin_lock-ed map_lookup/map_update */
333+};
332334
333335 /* flags for BPF_MAP_CREATE command */
334−#define BPF_F_NO_PREALLOC (1U << 0)
336+enum {
337+ BPF_F_NO_PREALLOC = (1U << 0),
335338 /* Instead of having one common LRU list in the
336339 * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
337340 * which can scale and perform better.
338341 * Note, the LRU nodes (including free nodes) cannot be moved
339342 * across different LRU lists.
340343 */
341−#define BPF_F_NO_COMMON_LRU (1U << 1)
344+ BPF_F_NO_COMMON_LRU = (1U << 1),
342345 /* Specify numa node during map creation */
343−#define BPF_F_NUMA_NODE (1U << 2)
344−
345−#define BPF_OBJ_NAME_LEN 16U
346+ BPF_F_NUMA_NODE = (1U << 2),
346347
347348 /* Flags for accessing BPF object from syscall side. */
348−#define BPF_F_RDONLY (1U << 3)
349−#define BPF_F_WRONLY (1U << 4)
349+ BPF_F_RDONLY = (1U << 3),
350+ BPF_F_WRONLY = (1U << 4),
350351
351352 /* Flag for stack_map, store build_id+offset instead of pointer */
352−#define BPF_F_STACK_BUILD_ID (1U << 5)
353+ BPF_F_STACK_BUILD_ID = (1U << 5),
353354
354355 /* Zero-initialize hash function seed. This should only be used for testing. */
355−#define BPF_F_ZERO_SEED (1U << 6)
356+ BPF_F_ZERO_SEED = (1U << 6),
356357
357358 /* Flags for accessing BPF object from program side. */
358−#define BPF_F_RDONLY_PROG (1U << 7)
359−#define BPF_F_WRONLY_PROG (1U << 8)
359+ BPF_F_RDONLY_PROG = (1U << 7),
360+ BPF_F_WRONLY_PROG = (1U << 8),
360361
361362 /* Clone map from listener for newly accepted socket */
362−#define BPF_F_CLONE (1U << 9)
363+ BPF_F_CLONE = (1U << 9),
363364
364365 /* Enable memory-mapping BPF map */
365−#define BPF_F_MMAPABLE (1U << 10)
366+ BPF_F_MMAPABLE = (1U << 10),
367+};
366368
367369 /* Flags for BPF_PROG_QUERY. */
368370
@@ -391,6 +393,8 @@ struct bpf_stack_build_id { · 22 unmodified lines
391393 };
392394 };
393395
396+#define BPF_OBJ_NAME_LEN 16U
397+
394398 union bpf_attr {
395399 struct { /* anonymous struct used by BPF_MAP_CREATE command */
396400 __u32 map_type; /* one of enum bpf_map_type */
@@ -3045,72 +3049,100 @@ enum bpf_func_id { · 2648 unmodified lines
30453049 /* All flags used by eBPF helper functions, placed here. */
30463050
30473051 /* BPF_FUNC_skb_store_bytes flags. */
3048−#define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
3049−#define BPF_F_INVALIDATE_HASH (1ULL << 1)
3052+enum {
3053+ BPF_F_RECOMPUTE_CSUM = (1ULL << 0),
3054+ BPF_F_INVALIDATE_HASH = (1ULL << 1),
3055+};
30503056
30513057 /* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
30523058 * First 4 bits are for passing the header field size.
30533059 */
3054−#define BPF_F_HDR_FIELD_MASK 0xfULL
3060+enum {
3061+ BPF_F_HDR_FIELD_MASK = 0xfULL,
3062+};
30553063
30563064 /* BPF_FUNC_l4_csum_replace flags. */
3057−#define BPF_F_PSEUDO_HDR (1ULL << 4)
3058−#define BPF_F_MARK_MANGLED_0 (1ULL << 5)
3059−#define BPF_F_MARK_ENFORCE (1ULL << 6)
3065+enum {
3066+ BPF_F_PSEUDO_HDR = (1ULL << 4),
3067+ BPF_F_MARK_MANGLED_0 = (1ULL << 5),
3068+ BPF_F_MARK_ENFORCE = (1ULL << 6),
3069+};
30603070
30613071 /* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
3062−#define BPF_F_INGRESS (1ULL << 0)
3072+enum {
3073+ BPF_F_INGRESS = (1ULL << 0),
3074+};
30633075
30643076 /* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
3065−#define BPF_F_TUNINFO_IPV6 (1ULL << 0)
3077+enum {
3078+ BPF_F_TUNINFO_IPV6 = (1ULL << 0),
3079+};
30663080
30673081 /* flags for both BPF_FUNC_get_stackid and BPF_FUNC_get_stack. */
3068−#define BPF_F_SKIP_FIELD_MASK 0xffULL
3069−#define BPF_F_USER_STACK (1ULL << 8)
3082+enum {
3083+ BPF_F_SKIP_FIELD_MASK = 0xffULL,
3084+ BPF_F_USER_STACK = (1ULL << 8),
30703085 /* flags used by BPF_FUNC_get_stackid only. */
3071−#define BPF_F_FAST_STACK_CMP (1ULL << 9)
3072−#define BPF_F_REUSE_STACKID (1ULL << 10)
3086+ BPF_F_FAST_STACK_CMP = (1ULL << 9),
3087+ BPF_F_REUSE_STACKID = (1ULL << 10),
30733088 /* flags used by BPF_FUNC_get_stack only. */
3074−#define BPF_F_USER_BUILD_ID (1ULL << 11)
3089+ BPF_F_USER_BUILD_ID = (1ULL << 11),
3090+};
30753091
30763092 /* BPF_FUNC_skb_set_tunnel_key flags. */
3077−#define BPF_F_ZERO_CSUM_TX (1ULL << 1)
3078−#define BPF_F_DONT_FRAGMENT (1ULL << 2)
3079−#define BPF_F_SEQ_NUMBER (1ULL << 3)
3093+enum {
3094+ BPF_F_ZERO_CSUM_TX = (1ULL << 1),
3095+ BPF_F_DONT_FRAGMENT = (1ULL << 2),
3096+ BPF_F_SEQ_NUMBER = (1ULL << 3),
3097+};
30803098
30813099 /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and
30823100 * BPF_FUNC_perf_event_read_value flags.
30833101 */
3084−#define BPF_F_INDEX_MASK 0xffffffffULL
3085−#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
3102+enum {
3103+ BPF_F_INDEX_MASK = 0xffffffffULL,
3104+ BPF_F_CURRENT_CPU = BPF_F_INDEX_MASK,
30863105 /* BPF_FUNC_perf_event_output for sk_buff input context. */
3087−#define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
3106+ BPF_F_CTXLEN_MASK = (0xfffffULL << 32),
3107+};
30883108
30893109 /* Current network namespace */
3090−#define BPF_F_CURRENT_NETNS (-1L)
3110+enum {
3111+ BPF_F_CURRENT_NETNS = (-1L),
3112+};
30913113
30923114 /* BPF_FUNC_skb_adjust_room flags. */
3093−#define BPF_F_ADJ_ROOM_FIXED_GSO (1ULL << 0)
3115+enum {
3116+ BPF_F_ADJ_ROOM_FIXED_GSO = (1ULL << 0),
3117+ BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 = (1ULL << 1),
3118+ BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 = (1ULL << 2),
3119+ BPF_F_ADJ_ROOM_ENCAP_L4_GRE = (1ULL << 3),
3120+ BPF_F_ADJ_ROOM_ENCAP_L4_UDP = (1ULL << 4),
3121+};
30943122
3095−#define BPF_ADJ_ROOM_ENCAP_L2_MASK 0xff
3096−#define BPF_ADJ_ROOM_ENCAP_L2_SHIFT 56
3123+enum {
3124+ BPF_ADJ_ROOM_ENCAP_L2_MASK = 0xff,
3125+ BPF_ADJ_ROOM_ENCAP_L2_SHIFT = 56,
3126+};
30973127
3098−#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 (1ULL << 1)
3099−#define BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 (1ULL << 2)
3100−#define BPF_F_ADJ_ROOM_ENCAP_L4_GRE (1ULL << 3)
3101−#define BPF_F_ADJ_ROOM_ENCAP_L4_UDP (1ULL << 4)
31023128 #define BPF_F_ADJ_ROOM_ENCAP_L2(len) (((__u64)len & \
31033129 BPF_ADJ_ROOM_ENCAP_L2_MASK) \
31043130 << BPF_ADJ_ROOM_ENCAP_L2_SHIFT)
31053131
31063132 /* BPF_FUNC_sysctl_get_name flags. */
3107−#define BPF_F_SYSCTL_BASE_NAME (1ULL << 0)
3133+enum {
3134+ BPF_F_SYSCTL_BASE_NAME = (1ULL << 0),
3135+};
31083136
31093137 /* BPF_FUNC_sk_storage_get flags */
3110−#define BPF_SK_STORAGE_GET_F_CREATE (1ULL << 0)
3138+enum {
3139+ BPF_SK_STORAGE_GET_F_CREATE = (1ULL << 0),
3140+};
31113141
31123142 /* BPF_FUNC_read_branch_records flags. */
3113−#define BPF_F_GET_BRANCH_RECORDS_SIZE (1ULL << 0)
3143+enum {
3144+ BPF_F_GET_BRANCH_RECORDS_SIZE = (1ULL << 0),
3145+};
31143146
31153147 /* Mode for BPF_FUNC_skb_adjust_room helper. */
31163148 enum bpf_adj_room_mode {
@@ -3529,13 +3561,14 @@ struct bpf_sock_ops { · 412 unmodified lines
35293561 };
35303562
35313563 /* Definitions for bpf_sock_ops_cb_flags */
3532−#define BPF_SOCK_OPS_RTO_CB_FLAG (1<<0)
3533−#define BPF_SOCK_OPS_RETRANS_CB_FLAG (1<<1)
3534−#define BPF_SOCK_OPS_STATE_CB_FLAG (1<<2)
3535−#define BPF_SOCK_OPS_RTT_CB_FLAG (1<<3)
3536−#define BPF_SOCK_OPS_ALL_CB_FLAGS 0xF /* Mask of all currently
3537− * supported cb flags
3538− */
3564+enum {
3565+ BPF_SOCK_OPS_RTO_CB_FLAG = (1<<0),
3566+ BPF_SOCK_OPS_RETRANS_CB_FLAG = (1<<1),
3567+ BPF_SOCK_OPS_STATE_CB_FLAG = (1<<2),
3568+ BPF_SOCK_OPS_RTT_CB_FLAG = (1<<3),
3569+/* Mask of all currently supported cb flags */
3570+ BPF_SOCK_OPS_ALL_CB_FLAGS = 0xF,
3571+};
35393572
35403573 /* List of known BPF sock_ops operators.
35413574 * New entries can only be added at the end
@@ -3614,8 +3647,10 @@ enum { · 72 unmodified lines
36143647 BPF_TCP_MAX_STATES /* Leave at the end! */
36153648 };
36163649
3617−#define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
3618−#define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
3650+enum {
3651+ TCP_BPF_IW = 1001, /* Set TCP initial congestion window */
3652+ TCP_BPF_SNDCWND_CLAMP = 1002, /* Set sndcwnd_clamp */
3653+};
36193654
36203655 struct bpf_perf_event_value {
36213656 __u64 counter;
@@ -3623,12 +3658,16 @@ struct bpf_perf_event_value { · 1 unmodified line
36233658 __u64 running;
36243659 };
36253660
3626−#define BPF_DEVCG_ACC_MKNOD (1ULL << 0)
3627−#define BPF_DEVCG_ACC_READ (1ULL << 1)
3628−#define BPF_DEVCG_ACC_WRITE (1ULL << 2)
3661+enum {
3662+ BPF_DEVCG_ACC_MKNOD = (1ULL << 0),
3663+ BPF_DEVCG_ACC_READ = (1ULL << 1),
3664+ BPF_DEVCG_ACC_WRITE = (1ULL << 2),
3665+};
36293666
3630−#define BPF_DEVCG_DEV_BLOCK (1ULL << 0)
3631−#define BPF_DEVCG_DEV_CHAR (1ULL << 1)
3667+enum {
3668+ BPF_DEVCG_DEV_BLOCK = (1ULL << 0),
3669+ BPF_DEVCG_DEV_CHAR = (1ULL << 1),
3670+};
36323671
36333672 struct bpf_cgroup_dev_ctx {
36343673 /* access_type encoded as (BPF_DEVCG_ACC_* << 16) | BPF_DEVCG_DEV_* */
@@ -3644,8 +3683,10 @@ struct bpf_raw_tracepoint_args { · 9 unmodified lines
36443683 /* DIRECT: Skip the FIB rules and go to FIB table associated with device
36453684 * OUTPUT: Do lookup from egress perspective; default is ingress
36463685 */
3647−#define BPF_FIB_LOOKUP_DIRECT (1U << 0)
3648−#define BPF_FIB_LOOKUP_OUTPUT (1U << 1)
3686+enum {
3687+ BPF_FIB_LOOKUP_DIRECT = (1U << 0),
3688+ BPF_FIB_LOOKUP_OUTPUT = (1U << 1),
3689+};
36493690
36503691 enum {
36513692 BPF_FIB_LKUP_RET_SUCCESS, /* lookup successful */
@@ -3717,9 +3758,11 @@ enum bpf_task_fd_type { · 65 unmodified lines
37173758 BPF_FD_TYPE_URETPROBE, /* filename + offset */
37183759 };
37193760
3720−#define BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG (1U << 0)
3721−#define BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL (1U << 1)
3722−#define BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP (1U << 2)
3761+enum {
3762+ BPF_FLOW_DISSECTOR_F_PARSE_1ST_FRAG = (1U << 0),
3763+ BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL = (1U << 1),
3764+ BPF_FLOW_DISSECTOR_F_STOP_AT_ENCAP = (1U << 2),
3765+};
37233766
37243767 struct bpf_flow_keys {
37253768 __u16 nhoff;