Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #ifndef _XDP_SAMPLE_BPF_H
0003 #define _XDP_SAMPLE_BPF_H
0004 
0005 #include "vmlinux.h"
0006 #include <bpf/bpf_tracing.h>
0007 #include <bpf/bpf_core_read.h>
0008 #include <bpf/bpf_helpers.h>
0009 
0010 #include "xdp_sample_shared.h"
0011 
0012 #define ETH_ALEN 6
0013 #define ETH_P_802_3_MIN 0x0600
0014 #define ETH_P_8021Q 0x8100
0015 #define ETH_P_8021AD 0x88A8
0016 #define ETH_P_IP 0x0800
0017 #define ETH_P_IPV6 0x86DD
0018 #define ETH_P_ARP 0x0806
0019 #define IPPROTO_ICMPV6 58
0020 
0021 #define EINVAL 22
0022 #define ENETDOWN 100
0023 #define EMSGSIZE 90
0024 #define EOPNOTSUPP 95
0025 #define ENOSPC 28
0026 
0027 typedef struct {
0028     __uint(type, BPF_MAP_TYPE_ARRAY);
0029     __uint(map_flags, BPF_F_MMAPABLE);
0030     __type(key, unsigned int);
0031     __type(value, struct datarec);
0032 } array_map;
0033 
0034 extern array_map rx_cnt;
0035 extern const volatile int nr_cpus;
0036 
0037 enum {
0038     XDP_REDIRECT_SUCCESS = 0,
0039     XDP_REDIRECT_ERROR = 1
0040 };
0041 
0042 static __always_inline void swap_src_dst_mac(void *data)
0043 {
0044     unsigned short *p = data;
0045     unsigned short dst[3];
0046 
0047     dst[0] = p[0];
0048     dst[1] = p[1];
0049     dst[2] = p[2];
0050     p[0] = p[3];
0051     p[1] = p[4];
0052     p[2] = p[5];
0053     p[3] = dst[0];
0054     p[4] = dst[1];
0055     p[5] = dst[2];
0056 }
0057 
0058 #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
0059     __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
0060 #define bpf_ntohs(x)        __builtin_bswap16(x)
0061 #define bpf_htons(x)        __builtin_bswap16(x)
0062 #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
0063     __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
0064 #define bpf_ntohs(x)        (x)
0065 #define bpf_htons(x)        (x)
0066 #else
0067 # error "Endianness detection needs to be set up for your compiler?!"
0068 #endif
0069 
0070 /*
0071  * Note: including linux/compiler.h or linux/kernel.h for the macros below
0072  * conflicts with vmlinux.h include in BPF files, so we define them here.
0073  *
0074  * Following functions are taken from kernel sources and
0075  * break aliasing rules in their original form.
0076  *
0077  * While kernel is compiled with -fno-strict-aliasing,
0078  * perf uses -Wstrict-aliasing=3 which makes build fail
0079  * under gcc 4.4.
0080  *
0081  * Using extra __may_alias__ type to allow aliasing
0082  * in this case.
0083  */
0084 typedef __u8  __attribute__((__may_alias__))  __u8_alias_t;
0085 typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
0086 typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
0087 typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
0088 
0089 static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
0090 {
0091     switch (size) {
0092     case 1: *(__u8_alias_t  *) res = *(volatile __u8_alias_t  *) p; break;
0093     case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
0094     case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
0095     case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
0096     default:
0097         asm volatile ("" : : : "memory");
0098         __builtin_memcpy((void *)res, (const void *)p, size);
0099         asm volatile ("" : : : "memory");
0100     }
0101 }
0102 
0103 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
0104 {
0105     switch (size) {
0106     case 1: *(volatile  __u8_alias_t *) p = *(__u8_alias_t  *) res; break;
0107     case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
0108     case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
0109     case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
0110     default:
0111         asm volatile ("" : : : "memory");
0112         __builtin_memcpy((void *)p, (const void *)res, size);
0113         asm volatile ("" : : : "memory");
0114     }
0115 }
0116 
0117 #define READ_ONCE(x)                    \
0118 ({                          \
0119     union { typeof(x) __val; char __c[1]; } __u =   \
0120         { .__c = { 0 } };           \
0121     __read_once_size(&(x), __u.__c, sizeof(x)); \
0122     __u.__val;                  \
0123 })
0124 
0125 #define WRITE_ONCE(x, val)              \
0126 ({                          \
0127     union { typeof(x) __val; char __c[1]; } __u =   \
0128         { .__val = (val) };             \
0129     __write_once_size(&(x), __u.__c, sizeof(x));    \
0130     __u.__val;                  \
0131 })
0132 
0133 /* Add a value using relaxed read and relaxed write. Less expensive than
0134  * fetch_add when there is no write concurrency.
0135  */
0136 #define NO_TEAR_ADD(x, val) WRITE_ONCE((x), READ_ONCE(x) + (val))
0137 #define NO_TEAR_INC(x) NO_TEAR_ADD((x), 1)
0138 
0139 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
0140 
0141 #endif