Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

samples/bpf: split common macros to net_shared.h

Currently, many programs under sample/bpf often include individual
macros by directly including the header under "linux/" rather than
using the "vmlinux.h" header.

However, there are some problems with migrating to "vmlinux.h" because
there is no definition for utility functions such as endianness
conversion (ntohs/htons). Fortunately, the xdp_sample program already
has a function that can be replaced to solve this problem.

Therefore, this commit attempts to separate these functions into a file
called net_shared.h to make them universally available. Additionally,
this file includes network-related macros that are not defined in
"vmlinux.h". (inspired by 'selftests' bpf_tracing_net.h)

Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
Link: https://lore.kernel.org/r/20230115071613.125791-8-danieltimlee@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Daniel T. Lee and committed by
Alexei Starovoitov
e69fe845 a1f93c8f

+27 -21
+26
samples/bpf/net_shared.h
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #ifndef _NET_SHARED_H 3 + #define _NET_SHARED_H 4 + 5 + #define ETH_ALEN 6 6 + #define ETH_P_802_3_MIN 0x0600 7 + #define ETH_P_8021Q 0x8100 8 + #define ETH_P_8021AD 0x88A8 9 + #define ETH_P_IP 0x0800 10 + #define ETH_P_IPV6 0x86DD 11 + #define ETH_P_ARP 0x0806 12 + #define IPPROTO_ICMPV6 58 13 + 14 + #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ 15 + __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 16 + #define bpf_ntohs(x) __builtin_bswap16(x) 17 + #define bpf_htons(x) __builtin_bswap16(x) 18 + #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ 19 + __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 20 + #define bpf_ntohs(x) (x) 21 + #define bpf_htons(x) (x) 22 + #else 23 + # error "Endianness detection needs to be set up for your compiler?!" 24 + #endif 25 + 26 + #endif
+1 -21
samples/bpf/xdp_sample.bpf.h
··· 7 7 #include <bpf/bpf_core_read.h> 8 8 #include <bpf/bpf_helpers.h> 9 9 10 + #include "net_shared.h" 10 11 #include "xdp_sample_shared.h" 11 - 12 - #define ETH_ALEN 6 13 - #define ETH_P_802_3_MIN 0x0600 14 - #define ETH_P_8021Q 0x8100 15 - #define ETH_P_8021AD 0x88A8 16 - #define ETH_P_IP 0x0800 17 - #define ETH_P_IPV6 0x86DD 18 - #define ETH_P_ARP 0x0806 19 - #define IPPROTO_ICMPV6 58 20 12 21 13 #define EINVAL 22 22 14 #define ENETDOWN 100 ··· 46 54 p[4] = dst[1]; 47 55 p[5] = dst[2]; 48 56 } 49 - 50 - #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ 51 - __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 52 - #define bpf_ntohs(x) __builtin_bswap16(x) 53 - #define bpf_htons(x) __builtin_bswap16(x) 54 - #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ 55 - __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 56 - #define bpf_ntohs(x) (x) 57 - #define bpf_htons(x) (x) 58 - #else 59 - # error "Endianness detection needs to be set up for your compiler?!" 60 - #endif 61 57 62 58 /* 63 59 * Note: including linux/compiler.h or linux/kernel.h for the macros below