Different linux distro may include partial definitions for eBPF
while the linux kernel (or dpdk) may support given eBPF feature.
To avoid issues define in one place all that is needed (and/or supported)
by various DPDK components.

Signed-off-by: Konstantin Ananyev <konstantin.anan...@intel.com>
---
 drivers/net/tap/tap_bpf.h    |  80 +---------
 lib/librte_net/Makefile      |   1 +
 lib/librte_net/meson.build   |   3 +-
 lib/librte_net/rte_bpf_def.h | 370 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 374 insertions(+), 80 deletions(-)
 create mode 100644 lib/librte_net/rte_bpf_def.h

diff --git a/drivers/net/tap/tap_bpf.h b/drivers/net/tap/tap_bpf.h
index 1a70ffe21..d059648d8 100644
--- a/drivers/net/tap/tap_bpf.h
+++ b/drivers/net/tap/tap_bpf.h
@@ -6,85 +6,7 @@
 #define __TAP_BPF_H__
 
 #include <tap_autoconf.h>
-
-/* Do not #include <linux/bpf.h> since eBPF must compile on different
- * distros which may include partial definitions for eBPF (while the
- * kernel itself may support eBPF). Instead define here all that is needed
- */
-
-/* BPF_MAP_UPDATE_ELEM command flags */
-#define        BPF_ANY 0 /* create a new element or update an existing */
-
-/* BPF architecture instruction struct */
-struct bpf_insn {
-       __u8    code;
-       __u8    dst_reg:4;
-       __u8    src_reg:4;
-       __s16   off;
-       __s32   imm; /* immediate value */
-};
-
-/* BPF program types */
-enum bpf_prog_type {
-       BPF_PROG_TYPE_UNSPEC,
-       BPF_PROG_TYPE_SOCKET_FILTER,
-       BPF_PROG_TYPE_KPROBE,
-       BPF_PROG_TYPE_SCHED_CLS,
-       BPF_PROG_TYPE_SCHED_ACT,
-};
-
-/* BPF commands types */
-enum bpf_cmd {
-       BPF_MAP_CREATE,
-       BPF_MAP_LOOKUP_ELEM,
-       BPF_MAP_UPDATE_ELEM,
-       BPF_MAP_DELETE_ELEM,
-       BPF_MAP_GET_NEXT_KEY,
-       BPF_PROG_LOAD,
-};
-
-/* BPF maps types */
-enum bpf_map_type {
-       BPF_MAP_TYPE_UNSPEC,
-       BPF_MAP_TYPE_HASH,
-};
-
-/* union of anonymous structs used with TAP BPF commands */
-union bpf_attr {
-       /* BPF_MAP_CREATE command */
-       struct {
-               __u32   map_type;
-               __u32   key_size;
-               __u32   value_size;
-               __u32   max_entries;
-               __u32   map_flags;
-               __u32   inner_map_fd;
-       };
-
-       /* BPF_MAP_UPDATE_ELEM, BPF_MAP_DELETE_ELEM commands */
-       struct {
-               __u32           map_fd;
-               __aligned_u64   key;
-               union {
-                       __aligned_u64 value;
-                       __aligned_u64 next_key;
-               };
-               __u64           flags;
-       };
-
-       /* BPF_PROG_LOAD command */
-       struct {
-               __u32           prog_type;
-               __u32           insn_cnt;
-               __aligned_u64   insns;
-               __aligned_u64   license;
-               __u32           log_level;
-               __u32           log_size;
-               __aligned_u64   log_buf;
-               __u32           kern_version;
-               __u32           prog_flags;
-       };
-} __attribute__((aligned(8)));
+#include <rte_bpf_def.h>
 
 #ifndef __NR_bpf
 # if defined(__i386__)
diff --git a/lib/librte_net/Makefile b/lib/librte_net/Makefile
index 95ff54900..8876b3dcc 100644
--- a/lib/librte_net/Makefile
+++ b/lib/librte_net/Makefile
@@ -20,5 +20,6 @@ SYMLINK-$(CONFIG_RTE_LIBRTE_NET)-include := rte_ip.h 
rte_tcp.h rte_udp.h rte_esp
 SYMLINK-$(CONFIG_RTE_LIBRTE_NET)-include += rte_sctp.h rte_icmp.h rte_arp.h
 SYMLINK-$(CONFIG_RTE_LIBRTE_NET)-include += rte_ether.h rte_gre.h rte_net.h
 SYMLINK-$(CONFIG_RTE_LIBRTE_NET)-include += rte_net_crc.h
+SYMLINK-$(CONFIG_RTE_LIBRTE_NET)-include += rte_bpf_def.h
 
 include $(RTE_SDK)/mk/rte.lib.mk
diff --git a/lib/librte_net/meson.build b/lib/librte_net/meson.build
index 78c0f03e5..ab0b82962 100644
--- a/lib/librte_net/meson.build
+++ b/lib/librte_net/meson.build
@@ -12,7 +12,8 @@ headers = files('rte_ip.h',
        'rte_ether.h',
        'rte_gre.h',
        'rte_net.h',
-       'rte_net_crc.h')
+       'rte_net_crc.h',
+       'rte_bpf_def.h')
 
 sources = files('rte_arp.c', 'rte_net.c', 'rte_net_crc.c')
 deps += ['mbuf']
diff --git a/lib/librte_net/rte_bpf_def.h b/lib/librte_net/rte_bpf_def.h
new file mode 100644
index 000000000..3f4a5a3e7
--- /dev/null
+++ b/lib/librte_net/rte_bpf_def.h
@@ -0,0 +1,370 @@
+/* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
+ * Copyright 2017 Mellanox Technologies, Ltd.
+ */
+
+#ifndef _RTE_BPF_DEF_H_
+#define _RTE_BPF_DEF_H_
+
+#ifdef __linux__
+#include <linux/types.h>
+#else
+
+typedef uint8_t __u8;
+typedef uint16_t __u16;
+typedef uint32_t __u32;
+typedef uint64_t __u64;
+
+typedef int8_t __s8;
+typedef int16_t __s16;
+typedef int32_t __s32;
+
+#define __aligned_u64 __u64 __attribute__((aligned(8)))
+
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Do not #include <linux/bpf.h> since eBPF must compile on different
+ * distros which may include partial definitions for eBPF (while the
+ * kernel itself may support eBPF). Instead define here all that is needed
+ * by various DPDK components.
+ */
+
+/* Instruction classes */
+#define BPF_CLASS(code) ((code) & 0x07)
+#define                BPF_LD          0x00
+#define                BPF_LDX         0x01
+#define                BPF_ST          0x02
+#define                BPF_STX         0x03
+#define                BPF_ALU         0x04
+#define                BPF_JMP         0x05
+#define                BPF_RET         0x06
+#define                BPF_MISC        0x07
+
+/* ld/ldx fields */
+#define BPF_SIZE(code)  ((code) & 0x18)
+#define                BPF_W           0x00
+#define                BPF_H           0x08
+#define                BPF_B           0x10
+#define BPF_MODE(code)  ((code) & 0xe0)
+#define                BPF_IMM         0x00
+#define                BPF_ABS         0x20
+#define                BPF_IND         0x40
+#define                BPF_MEM         0x60
+#define                BPF_LEN         0x80
+#define                BPF_MSH         0xa0
+
+/* alu/jmp fields */
+#define BPF_OP(code)    ((code) & 0xf0)
+#define                BPF_ADD         0x00
+#define                BPF_SUB         0x10
+#define                BPF_MUL         0x20
+#define                BPF_DIV         0x30
+#define                BPF_OR          0x40
+#define                BPF_AND         0x50
+#define                BPF_LSH         0x60
+#define                BPF_RSH         0x70
+#define                BPF_NEG         0x80
+#define                BPF_MOD         0x90
+#define                BPF_XOR         0xa0
+
+#define                BPF_JA          0x00
+#define                BPF_JEQ         0x10
+#define                BPF_JGT         0x20
+#define                BPF_JGE         0x30
+#define                BPF_JSET        0x40
+#define BPF_SRC(code)   ((code) & 0x08)
+#define                BPF_K           0x00
+#define                BPF_X           0x08
+
+#ifndef BPF_MAXINSNS
+#define BPF_MAXINSNS 4096
+#endif
+
+/* Extended instruction set based on top of classic BPF */
+
+/* instruction classes */
+#define BPF_ALU64      0x07    /* alu mode in double word width */
+
+/* ld/ldx fields */
+#define BPF_DW         0x18    /* double word */
+#define BPF_XADD       0xc0    /* exclusive add */
+
+/* alu/jmp fields */
+#define BPF_MOV                0xb0    /* mov reg to reg */
+#define BPF_ARSH       0xc0    /* sign extending arithmetic shift right */
+
+/* change endianness of a register */
+#define BPF_END                0xd0    /* flags for endianness conversion: */
+#define BPF_TO_LE      0x00    /* convert to little-endian */
+#define BPF_TO_BE      0x08    /* convert to big-endian */
+#define BPF_FROM_LE    BPF_TO_LE
+#define BPF_FROM_BE    BPF_TO_BE
+
+/* jmp encodings */
+#define BPF_JNE                0x50    /* jump != */
+#define BPF_JLT                0xa0    /* LT is unsigned, '<' */
+#define BPF_JLE                0xb0    /* LE is unsigned, '<=' */
+#define BPF_JSGT       0x60    /* SGT is signed '>', GT in x86 */
+#define BPF_JSGE       0x70    /* SGE is signed '>=', GE in x86 */
+#define BPF_JSLT       0xc0    /* SLT is signed, '<' */
+#define BPF_JSLE       0xd0    /* SLE is signed, '<=' */
+#define BPF_CALL       0x80    /* function call */
+#define BPF_EXIT       0x90    /* function return */
+
+/* Register numbers */
+enum {
+       BPF_REG_0 = 0,
+       BPF_REG_1,
+       BPF_REG_2,
+       BPF_REG_3,
+       BPF_REG_4,
+       BPF_REG_5,
+       BPF_REG_6,
+       BPF_REG_7,
+       BPF_REG_8,
+       BPF_REG_9,
+       BPF_REG_10,
+       __MAX_BPF_REG,
+};
+
+/* BPF has 10 general purpose 64-bit registers and stack frame. */
+#define MAX_BPF_REG    __MAX_BPF_REG
+
+struct bpf_insn {
+       __u8    code;           /* opcode */
+       __u8    dst_reg:4;      /* dest register */
+       __u8    src_reg:4;      /* source register */
+       __s16   off;            /* signed offset */
+       __s32   imm;            /* signed immediate constant */
+};
+
+/* BPF syscall commands, see bpf(2) man-page for details. */
+enum bpf_cmd {
+       BPF_MAP_CREATE,
+       BPF_MAP_LOOKUP_ELEM,
+       BPF_MAP_UPDATE_ELEM,
+       BPF_MAP_DELETE_ELEM,
+       BPF_MAP_GET_NEXT_KEY,
+       BPF_PROG_LOAD,
+       BPF_OBJ_PIN,
+       BPF_OBJ_GET,
+       BPF_PROG_ATTACH,
+       BPF_PROG_DETACH,
+       BPF_PROG_TEST_RUN,
+       BPF_PROG_GET_NEXT_ID,
+       BPF_MAP_GET_NEXT_ID,
+       BPF_PROG_GET_FD_BY_ID,
+       BPF_MAP_GET_FD_BY_ID,
+       BPF_OBJ_GET_INFO_BY_FD,
+};
+
+enum bpf_map_type {
+       BPF_MAP_TYPE_UNSPEC,
+       BPF_MAP_TYPE_HASH,
+       BPF_MAP_TYPE_ARRAY,
+       BPF_MAP_TYPE_PROG_ARRAY,
+       BPF_MAP_TYPE_PERF_EVENT_ARRAY,
+       BPF_MAP_TYPE_PERCPU_HASH,
+       BPF_MAP_TYPE_PERCPU_ARRAY,
+       BPF_MAP_TYPE_STACK_TRACE,
+       BPF_MAP_TYPE_CGROUP_ARRAY,
+       BPF_MAP_TYPE_LRU_HASH,
+       BPF_MAP_TYPE_LRU_PERCPU_HASH,
+       BPF_MAP_TYPE_LPM_TRIE,
+       BPF_MAP_TYPE_ARRAY_OF_MAPS,
+       BPF_MAP_TYPE_HASH_OF_MAPS,
+       BPF_MAP_TYPE_DEVMAP,
+       BPF_MAP_TYPE_SOCKMAP,
+};
+
+enum bpf_prog_type {
+       BPF_PROG_TYPE_UNSPEC,
+       BPF_PROG_TYPE_SOCKET_FILTER,
+       BPF_PROG_TYPE_KPROBE,
+       BPF_PROG_TYPE_SCHED_CLS,
+       BPF_PROG_TYPE_SCHED_ACT,
+       BPF_PROG_TYPE_TRACEPOINT,
+       BPF_PROG_TYPE_XDP,
+       BPF_PROG_TYPE_PERF_EVENT,
+       BPF_PROG_TYPE_CGROUP_SKB,
+       BPF_PROG_TYPE_CGROUP_SOCK,
+       BPF_PROG_TYPE_LWT_IN,
+       BPF_PROG_TYPE_LWT_OUT,
+       BPF_PROG_TYPE_LWT_XMIT,
+       BPF_PROG_TYPE_SOCK_OPS,
+       BPF_PROG_TYPE_SK_SKB,
+};
+
+enum bpf_attach_type {
+       BPF_CGROUP_INET_INGRESS,
+       BPF_CGROUP_INET_EGRESS,
+       BPF_CGROUP_INET_SOCK_CREATE,
+       BPF_CGROUP_SOCK_OPS,
+       BPF_SK_SKB_STREAM_PARSER,
+       BPF_SK_SKB_STREAM_VERDICT,
+       __MAX_BPF_ATTACH_TYPE
+};
+
+#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
+
+/* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
+ * to the given target_fd cgroup the descendent cgroup will be able to
+ * override effective bpf program that was inherited from this cgroup
+ */
+#define BPF_F_ALLOW_OVERRIDE   (1U << 0)
+
+/* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
+ * verifier will perform strict alignment checking as if the kernel
+ * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
+ * and NET_IP_ALIGN defined to 2.
+ */
+#define BPF_F_STRICT_ALIGNMENT (1U << 0)
+
+#define BPF_PSEUDO_MAP_FD      1
+
+/* flags for BPF_MAP_UPDATE_ELEM command */
+#define BPF_ANY                0 /* create new element or update existing */
+#define BPF_NOEXIST    1 /* create new element if it didn't exist */
+#define BPF_EXIST      2 /* update existing element */
+
+/* flags for BPF_MAP_CREATE command */
+#define BPF_F_NO_PREALLOC      (1U << 0)
+/* Instead of having one common LRU list in the
+ * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
+ * which can scale and perform better.
+ * Note, the LRU nodes (including free nodes) cannot be moved
+ * across different LRU lists.
+ */
+#define BPF_F_NO_COMMON_LRU    (1U << 1)
+/* Specify numa node during map creation */
+#define BPF_F_NUMA_NODE                (1U << 2)
+
+union bpf_attr {
+       struct { /* anonymous struct used by BPF_MAP_CREATE command */
+               __u32   map_type;       /* one of enum bpf_map_type */
+               __u32   key_size;       /* size of key in bytes */
+               __u32   value_size;     /* size of value in bytes */
+               __u32   max_entries;    /* max number of entries in a map */
+               __u32   map_flags;      /* BPF_MAP_CREATE related
+                                        * flags defined above.
+                                        */
+               __u32   inner_map_fd;   /* fd pointing to the inner map */
+               __u32   numa_node;      /* numa node (effective only if
+                                        * BPF_F_NUMA_NODE is set).
+                                        */
+       };
+
+       struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
+               __u32           map_fd;
+               __aligned_u64   key;
+               union {
+                       __aligned_u64 value;
+                       __aligned_u64 next_key;
+               };
+               __u64           flags;
+       };
+
+       struct { /* anonymous struct used by BPF_PROG_LOAD command */
+               __u32           prog_type;      /* one of enum bpf_prog_type */
+               __u32           insn_cnt;
+               __aligned_u64   insns;
+               __aligned_u64   license;
+               __u32           log_level;
+               /* verbosity level of verifier */
+               __u32           log_size;       /* size of user buffer */
+               __aligned_u64   log_buf;        /* user supplied buffer */
+               __u32           kern_version;
+               /* checked when prog_type=kprobe */
+               __u32           prog_flags;
+       };
+
+       struct { /* anonymous struct used by BPF_OBJ_* commands */
+               __aligned_u64   pathname;
+               __u32           bpf_fd;
+       };
+
+       struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
+               __u32           target_fd;
+               /* container object to attach to */
+               __u32           attach_bpf_fd;  /* eBPF program to attach */
+               __u32           attach_type;
+               __u32           attach_flags;
+       };
+
+       struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
+               __u32           prog_fd;
+               __u32           retval;
+               __u32           data_size_in;
+               __u32           data_size_out;
+               __aligned_u64   data_in;
+               __aligned_u64   data_out;
+               __u32           repeat;
+               __u32           duration;
+       } test;
+
+       struct { /* anonymous struct used by BPF_*_GET_*_ID */
+               union {
+                       __u32           start_id;
+                       __u32           prog_id;
+                       __u32           map_id;
+               };
+               __u32           next_id;
+       };
+
+       struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
+               __u32           bpf_fd;
+               __u32           info_len;
+               __aligned_u64   info;
+       } info;
+} __attribute__((aligned(8)));
+
+/* Generic BPF return codes which all BPF program types may support.
+ * The values are binary compatible with their TC_ACT_* counter-part to
+ * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
+ * programs.
+ *
+ * XDP is handled seprately, see XDP_*.
+ */
+enum bpf_ret_code {
+       BPF_OK = 0,
+       /* 1 reserved */
+       BPF_DROP = 2,
+       /* 3-6 reserved */
+       BPF_REDIRECT = 7,
+       /* >127 are reserved for prog type specific return codes */
+};
+
+enum sk_action {
+       SK_DROP = 0,
+       SK_PASS,
+};
+
+#define BPF_TAG_SIZE   8
+
+struct bpf_prog_info {
+       __u32 type;
+       __u32 id;
+       __u8  tag[BPF_TAG_SIZE];
+       __u32 jited_prog_len;
+       __u32 xlated_prog_len;
+       __aligned_u64 jited_prog_insns;
+       __aligned_u64 xlated_prog_insns;
+} __attribute__((aligned(8)));
+
+struct bpf_map_info {
+       __u32 type;
+       __u32 id;
+       __u32 key_size;
+       __u32 value_size;
+       __u32 max_entries;
+       __u32 map_flags;
+} __attribute__((aligned(8)));
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _RTE_BPF_DEF_H_ */
-- 
2.13.6

Reply via email to